blob: e6ab15f108b158401e2f603271bd480c32252e7c [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68#define NFSDBG_FACILITY NFSDBG_PROC
69
Trond Myklebust2066fe82006-09-15 08:30:46 -040070#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NFS4_POLL_RETRY_MAX (15*HZ)
72
Trond Myklebusta78cb572009-08-09 15:06:19 -040073#define NFS4_MAX_LOOP_ON_RECOVER (10)
74
Trond Myklebustef159e92012-02-06 19:50:40 -050075static unsigned short max_session_slots = NFS4_DEF_SLOT_TABLE_SIZE;
76
Trond Myklebustcdd4e682006-01-03 09:55:12 +010077struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010078static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080079static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050081static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Chuck Lever81934dd2012-03-01 17:01:57 -050082static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Bryan Schumakerbae36242012-05-10 15:07:31 -040083static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *);
Trond Myklebust99367812007-07-17 21:52:41 -040084static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040085static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
86 struct nfs_fattr *fattr, struct iattr *sattr,
87 struct nfs4_state *state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040088#ifdef CONFIG_NFS_V4_1
Bryan Schumaker1cab0652012-01-31 10:39:29 -050089static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *);
90static int nfs41_free_stateid(struct nfs_server *, nfs4_stateid *);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040091#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070092/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050093static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Trond Myklebust52567b02009-10-23 14:46:42 -040095 if (err >= -1000)
96 return err;
97 switch (err) {
98 case -NFS4ERR_RESOURCE:
99 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000100 case -NFS4ERR_WRONGSEC:
101 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800102 case -NFS4ERR_BADOWNER:
103 case -NFS4ERR_BADNAME:
104 return -EINVAL;
Trond Myklebust52567b02009-10-23 14:46:42 -0400105 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700107 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400108 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400110 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
113/*
114 * This is our standard bitmap for GETATTR requests.
115 */
116const u32 nfs4_fattr_bitmap[2] = {
117 FATTR4_WORD0_TYPE
118 | FATTR4_WORD0_CHANGE
119 | FATTR4_WORD0_SIZE
120 | FATTR4_WORD0_FSID
121 | FATTR4_WORD0_FILEID,
122 FATTR4_WORD1_MODE
123 | FATTR4_WORD1_NUMLINKS
124 | FATTR4_WORD1_OWNER
125 | FATTR4_WORD1_OWNER_GROUP
126 | FATTR4_WORD1_RAWDEV
127 | FATTR4_WORD1_SPACE_USED
128 | FATTR4_WORD1_TIME_ACCESS
129 | FATTR4_WORD1_TIME_METADATA
130 | FATTR4_WORD1_TIME_MODIFY
131};
132
133const u32 nfs4_statfs_bitmap[2] = {
134 FATTR4_WORD0_FILES_AVAIL
135 | FATTR4_WORD0_FILES_FREE
136 | FATTR4_WORD0_FILES_TOTAL,
137 FATTR4_WORD1_SPACE_AVAIL
138 | FATTR4_WORD1_SPACE_FREE
139 | FATTR4_WORD1_SPACE_TOTAL
140};
141
Trond Myklebust4ce79712005-06-22 17:16:21 +0000142const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 FATTR4_WORD0_MAXLINK
144 | FATTR4_WORD0_MAXNAME,
145 0
146};
147
Fred Isamandae100c2011-07-30 20:52:37 -0400148const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 | FATTR4_WORD0_MAXREAD
150 | FATTR4_WORD0_MAXWRITE
151 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700152 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400153 | FATTR4_WORD1_FS_LAYOUT_TYPES,
154 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155};
156
Manoj Naik830b8e32006-06-09 09:34:25 -0400157const u32 nfs4_fs_locations_bitmap[2] = {
158 FATTR4_WORD0_TYPE
159 | FATTR4_WORD0_CHANGE
160 | FATTR4_WORD0_SIZE
161 | FATTR4_WORD0_FSID
162 | FATTR4_WORD0_FILEID
163 | FATTR4_WORD0_FS_LOCATIONS,
164 FATTR4_WORD1_MODE
165 | FATTR4_WORD1_NUMLINKS
166 | FATTR4_WORD1_OWNER
167 | FATTR4_WORD1_OWNER_GROUP
168 | FATTR4_WORD1_RAWDEV
169 | FATTR4_WORD1_SPACE_USED
170 | FATTR4_WORD1_TIME_ACCESS
171 | FATTR4_WORD1_TIME_METADATA
172 | FATTR4_WORD1_TIME_MODIFY
173 | FATTR4_WORD1_MOUNTED_ON_FILEID
174};
175
Al Virobc4785c2006-10-19 23:28:51 -0700176static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 struct nfs4_readdir_arg *readdir)
178{
Al Viro0dbb4c62006-10-19 23:28:49 -0700179 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 BUG_ON(readdir->count < 80);
182 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000183 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
185 return;
186 }
187
188 readdir->cookie = 0;
189 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
190 if (cookie == 2)
191 return;
192
193 /*
194 * NFSv4 servers do not return entries for '.' and '..'
195 * Therefore, we fake these entries here. We let '.'
196 * have cookie 0 and '..' have cookie 1. Note that
197 * when talking to the server, we always send cookie 0
198 * instead of 1 or 2.
199 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800200 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 if (cookie == 0) {
203 *p++ = xdr_one; /* next */
204 *p++ = xdr_zero; /* cookie, first word */
205 *p++ = xdr_one; /* cookie, second word */
206 *p++ = xdr_one; /* entry len */
207 memcpy(p, ".\0\0\0", 4); /* entry */
208 p++;
209 *p++ = xdr_one; /* bitmap length */
210 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
211 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400212 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
214
215 *p++ = xdr_one; /* next */
216 *p++ = xdr_zero; /* cookie, first word */
217 *p++ = xdr_two; /* cookie, second word */
218 *p++ = xdr_two; /* entry len */
219 memcpy(p, "..\0\0", 4); /* entry */
220 p++;
221 *p++ = xdr_one; /* bitmap length */
222 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
223 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400224 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 readdir->pgbase = (char *)p - (char *)start;
227 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800228 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230
Trond Myklebust65de8722008-12-23 15:21:44 -0500231static int nfs4_wait_clnt_recover(struct nfs_client *clp)
232{
233 int res;
234
235 might_sleep();
236
Trond Myklebuste005e802008-12-23 15:21:48 -0500237 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400238 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500239 return res;
240}
241
242static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
243{
244 int res = 0;
245
246 might_sleep();
247
248 if (*timeout <= 0)
249 *timeout = NFS4_POLL_RETRY_MIN;
250 if (*timeout > NFS4_POLL_RETRY_MAX)
251 *timeout = NFS4_POLL_RETRY_MAX;
Jeff Laytond3103102011-12-01 22:44:39 +0100252 freezable_schedule_timeout_killable(*timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500253 if (fatal_signal_pending(current))
254 res = -ERESTARTSYS;
255 *timeout <<= 1;
256 return res;
257}
258
259/* This is the error handling routine for processes that are allowed
260 * to sleep.
261 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800262static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500263{
264 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500265 struct nfs4_state *state = exception->state;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500266 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500267 int ret = errorcode;
268
269 exception->retry = 0;
270 switch(errorcode) {
271 case 0:
272 return 0;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500273 case -NFS4ERR_OPENMODE:
Trond Myklebust14977482012-03-27 18:31:25 -0400274 if (inode && nfs_have_delegation(inode, FMODE_READ)) {
Trond Myklebust3114ea72012-03-07 16:39:06 -0500275 nfs_inode_return_delegation(inode);
276 exception->retry = 1;
277 return 0;
278 }
279 if (state == NULL)
280 break;
281 nfs4_schedule_stateid_recovery(server, state);
282 goto wait_on_recovery;
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500283 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500284 case -NFS4ERR_ADMIN_REVOKED:
285 case -NFS4ERR_BAD_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500286 if (state == NULL)
287 break;
Trond Myklebust14977482012-03-27 18:31:25 -0400288 nfs_remove_bad_delegation(state->inode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500289 nfs4_schedule_stateid_recovery(server, state);
290 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400291 case -NFS4ERR_EXPIRED:
292 if (state != NULL)
293 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500294 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500295 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500296 nfs4_schedule_lease_recovery(clp);
297 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500298#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400299 case -NFS4ERR_BADSESSION:
300 case -NFS4ERR_BADSLOT:
301 case -NFS4ERR_BAD_HIGH_SLOT:
302 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
303 case -NFS4ERR_DEADSESSION:
304 case -NFS4ERR_SEQ_FALSE_RETRY:
305 case -NFS4ERR_SEQ_MISORDERED:
306 dprintk("%s ERROR: %d Reset session\n", __func__,
307 errorcode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500308 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -0400309 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500310 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500311#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500312 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500313 if (exception->timeout > HZ) {
314 /* We have retried a decent amount, time to
315 * fail
316 */
317 ret = -EBUSY;
318 break;
319 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500320 case -NFS4ERR_GRACE:
321 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500322 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500323 ret = nfs4_delay(server->client, &exception->timeout);
324 if (ret != 0)
325 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400326 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500327 case -NFS4ERR_OLD_STATEID:
328 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800329 break;
330 case -NFS4ERR_BADOWNER:
331 /* The following works around a Linux server bug! */
332 case -NFS4ERR_BADNAME:
333 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
334 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
335 exception->retry = 1;
336 printk(KERN_WARNING "NFS: v4 server %s "
337 "does not accept raw "
338 "uid/gids. "
339 "Reenabling the idmapper.\n",
340 server->nfs_client->cl_hostname);
341 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500342 }
343 /* We failed to handle the error */
344 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500345wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500346 ret = nfs4_wait_clnt_recover(clp);
347 if (ret == 0)
348 exception->retry = 1;
349 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500350}
351
352
Trond Myklebust452e9352010-07-31 14:29:06 -0400353static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 spin_lock(&clp->cl_lock);
356 if (time_before(clp->cl_last_renewal,timestamp))
357 clp->cl_last_renewal = timestamp;
358 spin_unlock(&clp->cl_lock);
359}
360
Trond Myklebust452e9352010-07-31 14:29:06 -0400361static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
362{
363 do_renew_lease(server->nfs_client, timestamp);
364}
365
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400366#if defined(CONFIG_NFS_V4_1)
367
Benny Halevy510b8172009-04-01 09:22:14 -0400368/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400369 * nfs4_free_slot - free a slot and efficiently update slot table.
370 *
371 * freeing a slot is trivially done by clearing its respective bit
372 * in the bitmap.
373 * If the freed slotid equals highest_used_slotid we want to update it
374 * so that the server would be able to size down the slot table if needed,
375 * otherwise we know that the highest_used_slotid is still in use.
376 * When updating highest_used_slotid there may be "holes" in the bitmap
377 * so we need to scan down from highest_used_slotid to 0 looking for the now
378 * highest slotid in use.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500379 * If none found, highest_used_slotid is set to NFS4_NO_SLOT.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500380 *
381 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400382 */
383static void
Trond Myklebust45d43c22012-02-06 19:38:51 -0500384nfs4_free_slot(struct nfs4_slot_table *tbl, u32 slotid)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400385{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500386 BUG_ON(slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400387 /* clear used bit in bitmap */
388 __clear_bit(slotid, tbl->used_slots);
389
390 /* update highest_used_slotid when it is freed */
391 if (slotid == tbl->highest_used_slotid) {
392 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500393 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400394 tbl->highest_used_slotid = slotid;
395 else
Trond Myklebust45d43c22012-02-06 19:38:51 -0500396 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400397 }
Trond Myklebust45d43c22012-02-06 19:38:51 -0500398 dprintk("%s: slotid %u highest_used_slotid %d\n", __func__,
399 slotid, tbl->highest_used_slotid);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400400}
401
Trond Myklebust961a8282012-01-17 22:57:37 -0500402bool nfs4_set_task_privileged(struct rpc_task *task, void *dummy)
403{
404 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
405 return true;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400406}
407
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800408/*
Andy Adamson42acd022011-01-06 02:04:34 +0000409 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800410 */
Andy Adamson42acd022011-01-06 02:04:34 +0000411static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800412{
Trond Myklebusta2118c32010-06-16 09:52:26 -0400413 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Trond Myklebust961a8282012-01-17 22:57:37 -0500414 rpc_wake_up_first(&ses->fc_slot_table.slot_tbl_waitq,
415 nfs4_set_task_privileged, NULL);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800416 return;
417 }
418
Trond Myklebust45d43c22012-02-06 19:38:51 -0500419 if (ses->fc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800420 return;
421
Andy Adamson42acd022011-01-06 02:04:34 +0000422 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
423 complete(&ses->fc_slot_table.complete);
424}
425
426/*
427 * Signal state manager thread if session back channel is drained
428 */
429void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
430{
431 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
Trond Myklebust45d43c22012-02-06 19:38:51 -0500432 ses->bc_slot_table.highest_used_slotid != NFS4_NO_SLOT)
Andy Adamson42acd022011-01-06 02:04:34 +0000433 return;
434 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
435 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800436}
437
Trond Myklebustd185a332010-06-16 09:52:25 -0400438static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400439{
440 struct nfs4_slot_table *tbl;
441
Trond Myklebustd185a332010-06-16 09:52:25 -0400442 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400443 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400444 /* just wake up the next guy waiting since
445 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500446 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500447 return;
Andy Adamson13615872009-04-01 09:22:17 -0400448 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500449
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500450 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonaabd0b42011-11-09 13:58:22 -0500451 nfs4_free_slot(tbl, res->sr_slot - tbl->slots);
Andy Adamson42acd022011-01-06 02:04:34 +0000452 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500453 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400454 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400455}
456
Trond Myklebust14516c32010-07-31 14:29:06 -0400457static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400458{
459 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400460 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400461
462 /*
463 * sr_status remains 1 if an RPC level error occurred. The server
464 * may or may not have processed the sequence operation..
465 * Proceed as if the server received and processed the sequence
466 * operation.
467 */
468 if (res->sr_status == 1)
469 res->sr_status = NFS_OK;
470
Bryan Schumaker468f8612011-04-18 15:57:32 -0400471 /* don't increment the sequence number if the task wasn't sent */
472 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400473 goto out;
474
Andy Adamson691daf32009-12-04 15:55:39 -0500475 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400476 switch (res->sr_status) {
477 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400478 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400479 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400480 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400481 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400482 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500483 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500484 if (res->sr_status_flags != 0)
485 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400486 break;
487 case -NFS4ERR_DELAY:
488 /* The server detected a resend of the RPC call and
489 * returned NFS4ERR_DELAY as per Section 2.10.6.2
490 * of RFC5661.
491 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200492 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400493 __func__,
494 res->sr_slot - res->sr_session->fc_slot_table.slots,
495 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400496 goto out_retry;
497 default:
498 /* Just update the slot sequence no. */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400499 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400500 }
501out:
502 /* The session may be reset by one of the error handlers. */
503 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400504 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400505 return 1;
506out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400507 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400508 goto out;
509 rpc_delay(task, NFS4_POLL_RETRY_MAX);
510 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400511}
512
Trond Myklebust14516c32010-07-31 14:29:06 -0400513static int nfs4_sequence_done(struct rpc_task *task,
514 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400515{
Trond Myklebust14516c32010-07-31 14:29:06 -0400516 if (res->sr_session == NULL)
517 return 1;
518 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400519}
520
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400521/*
Benny Halevy510b8172009-04-01 09:22:14 -0400522 * nfs4_find_slot - efficiently look for a free slot
523 *
524 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
525 * If found, we mark the slot as used, update the highest_used_slotid,
526 * and respectively set up the sequence operation args.
Trond Myklebust45d43c22012-02-06 19:38:51 -0500527 * The slot number is returned if found, or NFS4_NO_SLOT otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400528 *
529 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400530 */
Trond Myklebust45d43c22012-02-06 19:38:51 -0500531static u32
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800532nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400533{
Trond Myklebust45d43c22012-02-06 19:38:51 -0500534 u32 slotid;
535 u32 ret_id = NFS4_NO_SLOT;
Benny Halevy510b8172009-04-01 09:22:14 -0400536
Trond Myklebust45d43c22012-02-06 19:38:51 -0500537 dprintk("--> %s used_slots=%04lx highest_used=%u max_slots=%u\n",
Benny Halevy510b8172009-04-01 09:22:14 -0400538 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
539 tbl->max_slots);
540 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
541 if (slotid >= tbl->max_slots)
542 goto out;
543 __set_bit(slotid, tbl->used_slots);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500544 if (slotid > tbl->highest_used_slotid ||
545 tbl->highest_used_slotid == NFS4_NO_SLOT)
Benny Halevy510b8172009-04-01 09:22:14 -0400546 tbl->highest_used_slotid = slotid;
547 ret_id = slotid;
548out:
549 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
550 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400551 return ret_id;
552}
553
Trond Myklebust9d12b212012-01-17 22:04:25 -0500554static void nfs41_init_sequence(struct nfs4_sequence_args *args,
555 struct nfs4_sequence_res *res, int cache_reply)
556{
557 args->sa_session = NULL;
558 args->sa_cache_this = 0;
559 if (cache_reply)
560 args->sa_cache_this = 1;
561 res->sr_session = NULL;
562 res->sr_slot = NULL;
563}
564
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000565int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400566 struct nfs4_sequence_args *args,
567 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400568 struct rpc_task *task)
569{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400570 struct nfs4_slot *slot;
571 struct nfs4_slot_table *tbl;
Trond Myklebust45d43c22012-02-06 19:38:51 -0500572 u32 slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400573
574 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400575 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400576 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400577 return 0;
578
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400579 tbl = &session->fc_slot_table;
580
581 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400582 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800583 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500584 /* The state manager will wait until the slot table is empty */
Andy Adamson05f0d232009-12-04 15:55:37 -0500585 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400586 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500587 dprintk("%s session is draining\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500588 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400589 }
590
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800591 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
592 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
593 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
594 spin_unlock(&tbl->slot_tbl_lock);
595 dprintk("%s enforce FIFO order\n", __func__);
596 return -EAGAIN;
597 }
598
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800599 slotid = nfs4_find_slot(tbl);
Trond Myklebust45d43c22012-02-06 19:38:51 -0500600 if (slotid == NFS4_NO_SLOT) {
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400601 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
602 spin_unlock(&tbl->slot_tbl_lock);
603 dprintk("<-- %s: no free slots\n", __func__);
604 return -EAGAIN;
605 }
606 spin_unlock(&tbl->slot_tbl_lock);
607
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800608 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400609 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400610 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400611 args->sa_slotid = slotid;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400612
613 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
614
Benny Halevy99fe60d2009-04-01 09:22:29 -0400615 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400616 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400617 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400618 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400619 /*
620 * sr_status is only set in decode_sequence, and so will remain
621 * set to 1 if an rpc level failure occurs.
622 */
623 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400624 return 0;
625}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000626EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400627
Trond Myklebust035168a2010-06-16 09:52:26 -0400628int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400629 struct nfs4_sequence_args *args,
630 struct nfs4_sequence_res *res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400631 struct rpc_task *task)
632{
Trond Myklebust035168a2010-06-16 09:52:26 -0400633 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400634 int ret = 0;
635
Trond Myklebust9d12b212012-01-17 22:04:25 -0500636 if (session == NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400637 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400638
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200639 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400640 __func__, session->clp, session, res->sr_slot ?
641 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400642
Trond Myklebust9d12b212012-01-17 22:04:25 -0500643 ret = nfs41_setup_sequence(session, args, res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400644out:
645 dprintk("<-- %s status=%d\n", __func__, ret);
646 return ret;
647}
648
649struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400650 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400651 struct nfs4_sequence_args *seq_args;
652 struct nfs4_sequence_res *seq_res;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400653};
654
655static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
656{
657 struct nfs41_call_sync_data *data = calldata;
658
Trond Myklebust035168a2010-06-16 09:52:26 -0400659 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
660
661 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -0500662 data->seq_res, task))
Andy Adamsonce5039c2009-04-01 09:22:13 -0400663 return;
664 rpc_call_start(task);
665}
666
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800667static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
668{
669 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
670 nfs41_call_sync_prepare(task, calldata);
671}
672
Andy Adamson69ab40c2009-04-01 09:22:19 -0400673static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
674{
675 struct nfs41_call_sync_data *data = calldata;
676
Trond Myklebust14516c32010-07-31 14:29:06 -0400677 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400678}
679
Trond Myklebust17280172012-03-11 13:11:00 -0400680static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400681 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400682 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400683};
684
Trond Myklebust17280172012-03-11 13:11:00 -0400685static const struct rpc_call_ops nfs41_call_priv_sync_ops = {
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800686 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
687 .rpc_call_done = nfs41_call_sync_done,
688};
689
Bryan Schumaker7c513052011-03-24 17:12:24 +0000690static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
691 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400692 struct rpc_message *msg,
693 struct nfs4_sequence_args *args,
694 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800695 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400696{
697 int ret;
698 struct rpc_task *task;
699 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400700 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400701 .seq_args = args,
702 .seq_res = res,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400703 };
704 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000705 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400706 .rpc_message = msg,
707 .callback_ops = &nfs41_call_sync_ops,
708 .callback_data = &data
709 };
710
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800711 if (privileged)
712 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400713 task = rpc_run_task(&task_setup);
714 if (IS_ERR(task))
715 ret = PTR_ERR(task);
716 else {
717 ret = task->tk_status;
718 rpc_put_task(task);
719 }
720 return ret;
721}
722
Bryan Schumaker7c513052011-03-24 17:12:24 +0000723int _nfs4_call_sync_session(struct rpc_clnt *clnt,
724 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400725 struct rpc_message *msg,
726 struct nfs4_sequence_args *args,
727 struct nfs4_sequence_res *res,
728 int cache_reply)
729{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500730 nfs41_init_sequence(args, res, cache_reply);
731 return nfs4_call_sync_sequence(clnt, server, msg, args, res, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400732}
733
Trond Myklebustdf896452010-06-16 09:52:26 -0400734#else
Trond Myklebust9d12b212012-01-17 22:04:25 -0500735static inline
736void nfs41_init_sequence(struct nfs4_sequence_args *args,
737 struct nfs4_sequence_res *res, int cache_reply)
738{
739}
740
Trond Myklebust14516c32010-07-31 14:29:06 -0400741static int nfs4_sequence_done(struct rpc_task *task,
742 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400743{
Trond Myklebust14516c32010-07-31 14:29:06 -0400744 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400745}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400746#endif /* CONFIG_NFS_V4_1 */
747
Bryan Schumaker7c513052011-03-24 17:12:24 +0000748int _nfs4_call_sync(struct rpc_clnt *clnt,
749 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400750 struct rpc_message *msg,
751 struct nfs4_sequence_args *args,
752 struct nfs4_sequence_res *res,
753 int cache_reply)
754{
Trond Myklebust9d12b212012-01-17 22:04:25 -0500755 nfs41_init_sequence(args, res, cache_reply);
Bryan Schumaker7c513052011-03-24 17:12:24 +0000756 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400757}
758
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000759static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000760int nfs4_call_sync(struct rpc_clnt *clnt,
761 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000762 struct rpc_message *msg,
763 struct nfs4_sequence_args *args,
764 struct nfs4_sequence_res *res,
765 int cache_reply)
766{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000767 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
768 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000769}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400770
Trond Myklebust38478b22006-05-25 01:40:57 -0400771static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Trond Myklebust38478b22006-05-25 01:40:57 -0400773 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Trond Myklebust38478b22006-05-25 01:40:57 -0400775 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400776 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700777 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400778 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700779 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400780 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781}
782
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100783struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400784 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100785 struct nfs_openargs o_arg;
786 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100787 struct nfs_open_confirmargs c_arg;
788 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -0500789 struct nfs4_string owner_name;
790 struct nfs4_string group_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100791 struct nfs_fattr f_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100792 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400793 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100794 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400795 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100796 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100797 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400798 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100799 int rpc_status;
800 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100801};
802
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400803
804static void nfs4_init_opendata_res(struct nfs4_opendata *p)
805{
806 p->o_res.f_attr = &p->f_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400807 p->o_res.seqid = p->o_arg.seqid;
808 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400809 p->o_res.server = p->o_arg.server;
810 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500811 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400812}
813
Al Viro82a2c1b2011-06-22 18:30:55 -0400814static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500815 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400816 const struct iattr *attrs,
817 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100818{
Al Viro82a2c1b2011-06-22 18:30:55 -0400819 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100820 struct inode *dir = parent->d_inode;
821 struct nfs_server *server = NFS_SERVER(dir);
822 struct nfs4_opendata *p;
823
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400824 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100825 if (p == NULL)
826 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400827 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100828 if (p->o_arg.seqid == NULL)
829 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400830 nfs_sb_active(dentry->d_sb);
831 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100832 p->dir = parent;
833 p->owner = sp;
834 atomic_inc(&sp->so_count);
835 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500836 p->o_arg.open_flags = flags;
837 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400838 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -0400839 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
840 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400841 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100842 p->o_arg.server = server;
843 p->o_arg.bitmask = server->attr_bitmask;
844 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust536e43d2012-01-17 22:04:26 -0500845 if (attrs != NULL && attrs->ia_valid != 0) {
Chuck Levercd937102012-03-02 17:14:31 -0500846 __be32 verf[2];
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400847
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100848 p->o_arg.u.attrs = &p->attrs;
849 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -0500850
851 verf[0] = jiffies;
852 verf[1] = current->pid;
853 memcpy(p->o_arg.u.verifier.data, verf,
854 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100855 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100856 p->c_arg.fh = &p->o_res.fh;
857 p->c_arg.stateid = &p->o_res.stateid;
858 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400859 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400860 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100861 return p;
862err_free:
863 kfree(p);
864err:
865 dput(parent);
866 return NULL;
867}
868
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400869static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100870{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400871 struct nfs4_opendata *p = container_of(kref,
872 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400873 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400874
875 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400876 if (p->state != NULL)
877 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400878 nfs4_put_state_owner(p->owner);
879 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400880 dput(p->dentry);
881 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -0500882 nfs_fattr_free_names(&p->f_attr);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400883 kfree(p);
884}
885
886static void nfs4_opendata_put(struct nfs4_opendata *p)
887{
888 if (p != NULL)
889 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100890}
891
Trond Myklebust06f814a2006-01-03 09:55:07 +0100892static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
893{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100894 int ret;
895
Trond Myklebust06f814a2006-01-03 09:55:07 +0100896 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100897 return ret;
898}
899
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500900static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400901{
902 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500903
Trond Myklebust536e43d2012-01-17 22:04:26 -0500904 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500905 goto out;
906 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400907 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500908 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
909 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400910 break;
911 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500912 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
913 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400914 break;
915 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500916 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
917 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400918 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500919out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400920 return ret;
921}
922
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500923static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400924{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500925 if (delegation == NULL)
926 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500927 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400928 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500929 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400930 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500931 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400932 return 1;
933}
934
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500935static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100936{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500937 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100938 case FMODE_WRITE:
939 state->n_wronly++;
940 break;
941 case FMODE_READ:
942 state->n_rdonly++;
943 break;
944 case FMODE_READ|FMODE_WRITE:
945 state->n_rdwr++;
946 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500947 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100948}
949
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500950static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400951{
952 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -0500953 nfs4_stateid_copy(&state->stateid, stateid);
954 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500955 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400956 case FMODE_READ:
957 set_bit(NFS_O_RDONLY_STATE, &state->flags);
958 break;
959 case FMODE_WRITE:
960 set_bit(NFS_O_WRONLY_STATE, &state->flags);
961 break;
962 case FMODE_READ|FMODE_WRITE:
963 set_bit(NFS_O_RDWR_STATE, &state->flags);
964 }
965}
966
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500967static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400968{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400969 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500970 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400971 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400972}
973
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500974static 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 -0700975{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400976 /*
977 * Protect the call to nfs4_state_set_mode_locked and
978 * serialise the stateid update
979 */
980 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400981 if (deleg_stateid != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500982 nfs4_stateid_copy(&state->stateid, deleg_stateid);
Trond Myklebust003707c2007-07-05 18:07:55 -0400983 set_bit(NFS_DELEGATED_STATE, &state->flags);
984 }
985 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500986 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400987 write_sequnlock(&state->seqlock);
988 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500989 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700990 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991}
992
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500993static 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 -0500994{
995 struct nfs_inode *nfsi = NFS_I(state->inode);
996 struct nfs_delegation *deleg_cur;
997 int ret = 0;
998
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500999 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001000
1001 rcu_read_lock();
1002 deleg_cur = rcu_dereference(nfsi->delegation);
1003 if (deleg_cur == NULL)
1004 goto no_delegation;
1005
1006 spin_lock(&deleg_cur->lock);
1007 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001008 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001009 goto no_delegation_unlock;
1010
1011 if (delegation == NULL)
1012 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001013 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001014 goto no_delegation_unlock;
1015
Trond Myklebustb7391f42008-12-23 15:21:52 -05001016 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001017 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001018 ret = 1;
1019no_delegation_unlock:
1020 spin_unlock(&deleg_cur->lock);
1021no_delegation:
1022 rcu_read_unlock();
1023
1024 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001025 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001026 ret = 1;
1027 }
1028
1029 return ret;
1030}
1031
1032
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001033static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001034{
1035 struct nfs_delegation *delegation;
1036
1037 rcu_read_lock();
1038 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001039 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001040 rcu_read_unlock();
1041 return;
1042 }
1043 rcu_read_unlock();
1044 nfs_inode_return_delegation(inode);
1045}
1046
Trond Myklebust6ee41262007-07-08 14:11:36 -04001047static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001048{
1049 struct nfs4_state *state = opendata->state;
1050 struct nfs_inode *nfsi = NFS_I(state->inode);
1051 struct nfs_delegation *delegation;
Trond Myklebust536e43d2012-01-17 22:04:26 -05001052 int open_mode = opendata->o_arg.open_flags & (O_EXCL|O_TRUNC);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001053 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001054 nfs4_stateid stateid;
1055 int ret = -EAGAIN;
1056
Trond Myklebustaac00a82007-07-05 19:02:21 -04001057 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001058 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001059 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001060 if (can_open_cached(state, fmode, open_mode)) {
1061 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001062 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001063 goto out_return_state;
1064 }
1065 spin_unlock(&state->owner->so_lock);
1066 }
Trond Myklebust34310432008-12-23 15:21:38 -05001067 rcu_read_lock();
1068 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001069 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001070 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001071 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001072 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001073 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001074 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001075 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001076 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001077 if (ret != 0)
1078 goto out;
1079 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001080
1081 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001082 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001083 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001084 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001085out:
1086 return ERR_PTR(ret);
1087out_return_state:
1088 atomic_inc(&state->count);
1089 return state;
1090}
1091
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001092static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1093{
1094 struct inode *inode;
1095 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001096 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001097 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001098
Trond Myklebustaac00a82007-07-05 19:02:21 -04001099 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001100 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001101 goto out;
1102 }
1103
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001104 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001105 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001106 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001107 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001108 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001109 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001110 goto err;
1111 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001112 state = nfs4_get_open_state(inode, data->owner);
1113 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001114 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001115 if (data->o_res.delegation_type != 0) {
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001116 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001117 int delegation_flags = 0;
1118
Trond Myklebust003707c2007-07-05 18:07:55 -04001119 rcu_read_lock();
1120 delegation = rcu_dereference(NFS_I(inode)->delegation);
1121 if (delegation)
1122 delegation_flags = delegation->flags;
1123 rcu_read_unlock();
Trond Myklebust652f89f2011-12-09 19:05:58 -05001124 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1125 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1126 "returning a delegation for "
1127 "OPEN(CLAIM_DELEGATE_CUR)\n",
Trond Myklebust4e0038b2012-03-01 17:01:05 -05001128 clp->cl_hostname);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001129 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001130 nfs_inode_set_delegation(state->inode,
1131 data->owner->so_cred,
1132 &data->o_res);
1133 else
1134 nfs_inode_reclaim_delegation(state->inode,
1135 data->owner->so_cred,
1136 &data->o_res);
1137 }
Trond Myklebust34310432008-12-23 15:21:38 -05001138
1139 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001140 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001141 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001142out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001143 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001144err_put_inode:
1145 iput(inode);
1146err:
1147 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001148}
1149
Trond Myklebust864472e2006-01-03 09:55:15 +01001150static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1151{
1152 struct nfs_inode *nfsi = NFS_I(state->inode);
1153 struct nfs_open_context *ctx;
1154
1155 spin_lock(&state->inode->i_lock);
1156 list_for_each_entry(ctx, &nfsi->open_files, list) {
1157 if (ctx->state != state)
1158 continue;
1159 get_nfs_open_context(ctx);
1160 spin_unlock(&state->inode->i_lock);
1161 return ctx;
1162 }
1163 spin_unlock(&state->inode->i_lock);
1164 return ERR_PTR(-ENOENT);
1165}
1166
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001167static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1168{
1169 struct nfs4_opendata *opendata;
1170
Al Viro3d4ff432011-06-22 18:40:12 -04001171 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001172 if (opendata == NULL)
1173 return ERR_PTR(-ENOMEM);
1174 opendata->state = state;
1175 atomic_inc(&state->count);
1176 return opendata;
1177}
1178
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001179static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001180{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001181 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001182 int ret;
1183
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001184 opendata->o_arg.open_flags = 0;
1185 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001186 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1187 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1188 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001189 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001190 if (ret != 0)
1191 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001192 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001193 if (IS_ERR(newstate))
1194 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001195 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001196 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001197 return 0;
1198}
1199
1200static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1201{
Trond Myklebust864472e2006-01-03 09:55:15 +01001202 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001203 int ret;
1204
1205 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001206 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001207 smp_rmb();
1208 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001209 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001210 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001211 if (ret != 0)
1212 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001213 if (newstate != state)
1214 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001215 }
1216 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001217 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001218 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001219 if (ret != 0)
1220 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001221 if (newstate != state)
1222 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001223 }
1224 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001225 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001226 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001227 if (ret != 0)
1228 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001229 if (newstate != state)
1230 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001231 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001232 /*
1233 * We may have performed cached opens for all three recoveries.
1234 * Check if we need to update the current stateid.
1235 */
1236 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001237 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001238 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001239 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001240 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001241 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001242 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001243 return 0;
1244}
1245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246/*
1247 * OPEN_RECLAIM:
1248 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001250static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001252 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001253 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001254 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 int status;
1256
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001257 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1258 if (IS_ERR(opendata))
1259 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001260 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1261 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001262 rcu_read_lock();
1263 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001264 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001265 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001266 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001267 opendata->o_arg.u.delegation_type = delegation_type;
1268 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001269 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 return status;
1271}
1272
Trond Myklebust539cd032007-06-05 11:46:42 -04001273static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
1275 struct nfs_server *server = NFS_SERVER(state->inode);
1276 struct nfs4_exception exception = { };
1277 int err;
1278 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001279 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001280 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001281 break;
1282 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 } while (exception.retry);
1284 return err;
1285}
1286
Trond Myklebust864472e2006-01-03 09:55:15 +01001287static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1288{
1289 struct nfs_open_context *ctx;
1290 int ret;
1291
1292 ctx = nfs4_state_find_open_context(state);
1293 if (IS_ERR(ctx))
1294 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001295 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001296 put_nfs_open_context(ctx);
1297 return ret;
1298}
1299
Trond Myklebust13437e12007-07-06 15:10:43 -04001300static 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 -07001301{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001302 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001303 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001305 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1306 if (IS_ERR(opendata))
1307 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001308 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebustf597c532012-03-04 18:13:56 -05001309 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust864472e2006-01-03 09:55:15 +01001310 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001311 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001312 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313}
1314
Trond Myklebust13437e12007-07-06 15:10:43 -04001315int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316{
1317 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001318 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 int err;
1320 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001321 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 switch (err) {
1323 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001324 case -ENOENT:
1325 case -ESTALE:
1326 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001327 case -NFS4ERR_BADSESSION:
1328 case -NFS4ERR_BADSLOT:
1329 case -NFS4ERR_BAD_HIGH_SLOT:
1330 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1331 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001332 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001333 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 case -NFS4ERR_STALE_CLIENTID:
1335 case -NFS4ERR_STALE_STATEID:
1336 case -NFS4ERR_EXPIRED:
1337 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001338 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001339 goto out;
1340 case -ERESTARTSYS:
1341 /*
1342 * The show must go on: exit, but mark the
1343 * stateid as needing recovery.
1344 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001345 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001346 case -NFS4ERR_ADMIN_REVOKED:
1347 case -NFS4ERR_BAD_STATEID:
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001348 nfs_inode_find_state_and_recover(state->inode,
1349 stateid);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001350 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001351 case -EKEYEXPIRED:
1352 /*
1353 * User RPCSEC_GSS context has expired.
1354 * We cannot recover this stateid now, so
1355 * skip it and allow recovery thread to
1356 * proceed.
1357 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001358 case -ENOMEM:
1359 err = 0;
1360 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 }
1362 err = nfs4_handle_exception(server, err, &exception);
1363 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001364out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 return err;
1366}
1367
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001368static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1369{
1370 struct nfs4_opendata *data = calldata;
1371
1372 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001373 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001374 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05001375 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001376 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001377 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001378 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001379}
1380
1381static void nfs4_open_confirm_release(void *calldata)
1382{
1383 struct nfs4_opendata *data = calldata;
1384 struct nfs4_state *state = NULL;
1385
1386 /* If this request hasn't been cancelled, do nothing */
1387 if (data->cancelled == 0)
1388 goto out_free;
1389 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001390 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001391 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001392 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001393 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001394 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001395out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001396 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001397}
1398
1399static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001400 .rpc_call_done = nfs4_open_confirm_done,
1401 .rpc_release = nfs4_open_confirm_release,
1402};
1403
1404/*
1405 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1406 */
1407static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1408{
1409 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1410 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001411 struct rpc_message msg = {
1412 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1413 .rpc_argp = &data->c_arg,
1414 .rpc_resp = &data->c_res,
1415 .rpc_cred = data->owner->so_cred,
1416 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001417 struct rpc_task_setup task_setup_data = {
1418 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001419 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001420 .callback_ops = &nfs4_open_confirm_ops,
1421 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001422 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001423 .flags = RPC_TASK_ASYNC,
1424 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 int status;
1426
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001427 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001428 data->rpc_done = 0;
1429 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001430 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001431 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001432 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001433 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001434 status = nfs4_wait_for_completion_rpc_task(task);
1435 if (status != 0) {
1436 data->cancelled = 1;
1437 smp_wmb();
1438 } else
1439 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001440 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 return status;
1442}
1443
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001444static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001446 struct nfs4_opendata *data = calldata;
1447 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001448
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001449 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1450 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001451 /*
1452 * Check if we still need to send an OPEN call, or if we can use
1453 * a delegation instead.
1454 */
1455 if (data->state != NULL) {
1456 struct nfs_delegation *delegation;
1457
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001458 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001459 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001460 rcu_read_lock();
1461 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001462 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1463 can_open_delegated(delegation, data->o_arg.fmode))
1464 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001465 rcu_read_unlock();
1466 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001467 /* Update client id. */
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001468 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001469 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001470 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001471 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1472 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001473 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001474 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001475 &data->o_arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05001476 &data->o_res.seq_res, task))
Andy Adamsond8985282009-04-01 09:22:21 -04001477 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001478 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001479 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001480unlock_no_action:
1481 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001482out_no_action:
1483 task->tk_action = NULL;
1484
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001485}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001487static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1488{
1489 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1490 nfs4_open_prepare(task, calldata);
1491}
1492
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001493static void nfs4_open_done(struct rpc_task *task, void *calldata)
1494{
1495 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001497 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001498
Trond Myklebust14516c32010-07-31 14:29:06 -04001499 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1500 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001501
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001502 if (task->tk_status == 0) {
1503 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001504 case S_IFREG:
1505 break;
1506 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001507 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001508 break;
1509 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001510 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001511 break;
1512 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001513 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001514 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001515 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001516 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1517 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001518 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001519 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001520}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001521
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001522static void nfs4_open_release(void *calldata)
1523{
1524 struct nfs4_opendata *data = calldata;
1525 struct nfs4_state *state = NULL;
1526
1527 /* If this request hasn't been cancelled, do nothing */
1528 if (data->cancelled == 0)
1529 goto out_free;
1530 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001531 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001532 goto out_free;
1533 /* In case we need an open_confirm, no cleanup! */
1534 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1535 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001536 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001537 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001538 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001539out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001540 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001541}
1542
1543static const struct rpc_call_ops nfs4_open_ops = {
1544 .rpc_call_prepare = nfs4_open_prepare,
1545 .rpc_call_done = nfs4_open_done,
1546 .rpc_release = nfs4_open_release,
1547};
1548
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001549static const struct rpc_call_ops nfs4_recover_open_ops = {
1550 .rpc_call_prepare = nfs4_recover_open_prepare,
1551 .rpc_call_done = nfs4_open_done,
1552 .rpc_release = nfs4_open_release,
1553};
1554
1555static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001556{
1557 struct inode *dir = data->dir->d_inode;
1558 struct nfs_server *server = NFS_SERVER(dir);
1559 struct nfs_openargs *o_arg = &data->o_arg;
1560 struct nfs_openres *o_res = &data->o_res;
1561 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001562 struct rpc_message msg = {
1563 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1564 .rpc_argp = o_arg,
1565 .rpc_resp = o_res,
1566 .rpc_cred = data->owner->so_cred,
1567 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001568 struct rpc_task_setup task_setup_data = {
1569 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001570 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001571 .callback_ops = &nfs4_open_ops,
1572 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001573 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001574 .flags = RPC_TASK_ASYNC,
1575 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001576 int status;
1577
Trond Myklebust9d12b212012-01-17 22:04:25 -05001578 nfs41_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001579 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001580 data->rpc_done = 0;
1581 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001582 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001583 if (isrecover)
1584 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001585 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001586 if (IS_ERR(task))
1587 return PTR_ERR(task);
1588 status = nfs4_wait_for_completion_rpc_task(task);
1589 if (status != 0) {
1590 data->cancelled = 1;
1591 smp_wmb();
1592 } else
1593 status = data->rpc_status;
1594 rpc_put_task(task);
1595
1596 return status;
1597}
1598
1599static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1600{
1601 struct inode *dir = data->dir->d_inode;
1602 struct nfs_openres *o_res = &data->o_res;
1603 int status;
1604
1605 status = nfs4_run_open_task(data, 1);
1606 if (status != 0 || !data->rpc_done)
1607 return status;
1608
Trond Myklebust6926afd2012-01-07 13:22:46 -05001609 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
1610
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001611 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1612 status = _nfs4_proc_open_confirm(data);
1613 if (status != 0)
1614 return status;
1615 }
1616
1617 return status;
1618}
1619
1620/*
1621 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1622 */
1623static int _nfs4_proc_open(struct nfs4_opendata *data)
1624{
1625 struct inode *dir = data->dir->d_inode;
1626 struct nfs_server *server = NFS_SERVER(dir);
1627 struct nfs_openargs *o_arg = &data->o_arg;
1628 struct nfs_openres *o_res = &data->o_res;
1629 int status;
1630
1631 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001632 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001633 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001634 if (status != 0) {
1635 if (status == -NFS4ERR_BADNAME &&
1636 !(o_arg->open_flags & O_CREAT))
1637 return -ENOENT;
1638 return status;
1639 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001640
Trond Myklebust6926afd2012-01-07 13:22:46 -05001641 nfs_fattr_map_and_free_names(server, &data->f_attr);
1642
Trond Myklebust90ff0c52012-04-27 13:48:18 -04001643 if (o_arg->open_flags & O_CREAT)
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001644 update_changeattr(dir, &o_res->cinfo);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001645 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1646 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001648 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001650 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 }
1652 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001653 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001654 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655}
1656
Andy Adamsond83217c2011-03-01 01:34:17 +00001657static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001658{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001659 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001660 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001661
Trond Myklebusta78cb572009-08-09 15:06:19 -04001662 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001663 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001664 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001665 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001666 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1667 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001668 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001669 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001670 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001671 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001672 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001673}
1674
Andy Adamsond83217c2011-03-01 01:34:17 +00001675static int nfs4_recover_expired_lease(struct nfs_server *server)
1676{
1677 return nfs4_client_recover_expired_lease(server->nfs_client);
1678}
1679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680/*
1681 * OPEN_EXPIRED:
1682 * reclaim state on the server after a network partition.
1683 * Assumes caller holds the appropriate lock
1684 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001685static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001687 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001688 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001690 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1691 if (IS_ERR(opendata))
1692 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001693 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001694 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001695 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001696 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001697 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698}
1699
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001700static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001701{
Trond Myklebust539cd032007-06-05 11:46:42 -04001702 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001703 struct nfs4_exception exception = { };
1704 int err;
1705
1706 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001707 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001708 switch (err) {
1709 default:
1710 goto out;
1711 case -NFS4ERR_GRACE:
1712 case -NFS4ERR_DELAY:
1713 nfs4_handle_exception(server, err, &exception);
1714 err = 0;
1715 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001716 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001717out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001718 return err;
1719}
1720
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1722{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001724 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Trond Myklebust864472e2006-01-03 09:55:15 +01001726 ctx = nfs4_state_find_open_context(state);
1727 if (IS_ERR(ctx))
1728 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001729 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001730 put_nfs_open_context(ctx);
1731 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732}
1733
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001734#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001735static int nfs41_check_expired_stateid(struct nfs4_state *state, nfs4_stateid *stateid, unsigned int flags)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001736{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001737 int status = NFS_OK;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001738 struct nfs_server *server = NFS_SERVER(state->inode);
1739
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05001740 if (state->flags & flags) {
1741 status = nfs41_test_stateid(server, stateid);
1742 if (status != NFS_OK) {
1743 nfs41_free_stateid(server, stateid);
1744 state->flags &= ~flags;
1745 }
1746 }
1747 return status;
1748}
1749
1750static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1751{
1752 int deleg_status, open_status;
1753 int deleg_flags = 1 << NFS_DELEGATED_STATE;
1754 int open_flags = (1 << NFS_O_RDONLY_STATE) | (1 << NFS_O_WRONLY_STATE) | (1 << NFS_O_RDWR_STATE);
1755
1756 deleg_status = nfs41_check_expired_stateid(state, &state->stateid, deleg_flags);
1757 open_status = nfs41_check_expired_stateid(state, &state->open_stateid, open_flags);
1758
1759 if ((deleg_status == NFS_OK) && (open_status == NFS_OK))
1760 return NFS_OK;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001761 return nfs4_open_expired(sp, state);
1762}
1763#endif
1764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001766 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1767 * fields corresponding to attributes that were used to store the verifier.
1768 * Make sure we clobber those fields in the later setattr call
1769 */
1770static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1771{
1772 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1773 !(sattr->ia_valid & ATTR_ATIME_SET))
1774 sattr->ia_valid |= ATTR_ATIME;
1775
1776 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1777 !(sattr->ia_valid & ATTR_MTIME_SET))
1778 sattr->ia_valid |= ATTR_MTIME;
1779}
1780
1781/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001782 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 */
Al Viro82a2c1b2011-06-22 18:30:55 -04001784static int _nfs4_do_open(struct inode *dir, struct dentry *dentry, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785{
1786 struct nfs4_state_owner *sp;
1787 struct nfs4_state *state = NULL;
1788 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001789 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001790 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
1792 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05001794 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
1795 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1797 goto out_err;
1798 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001799 status = nfs4_recover_expired_lease(server);
1800 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001801 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001802 if (dentry->d_inode != NULL)
1803 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001804 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001805 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001806 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001807 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
Al Viro82a2c1b2011-06-22 18:30:55 -04001809 if (dentry->d_inode != NULL)
1810 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001811
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001812 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001814 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001816 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001817 status = PTR_ERR(state);
1818 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001819 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001820 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001821 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001822
1823 if (opendata->o_arg.open_flags & O_EXCL) {
1824 nfs4_exclusive_attrset(opendata, sattr);
1825
1826 nfs_fattr_init(opendata->o_res.f_attr);
1827 status = nfs4_do_setattr(state->inode, cred,
1828 opendata->o_res.f_attr, sattr,
1829 state);
1830 if (status == 0)
1831 nfs_setattr_update_inode(state->inode, sattr);
1832 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1833 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001834 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 *res = state;
1837 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001838err_opendata_put:
1839 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001840err_put_state_owner:
1841 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 *res = NULL;
1844 return status;
1845}
1846
1847
Al Viro82a2c1b2011-06-22 18:30:55 -04001848static struct nfs4_state *nfs4_do_open(struct inode *dir, struct dentry *dentry, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849{
1850 struct nfs4_exception exception = { };
1851 struct nfs4_state *res;
1852 int status;
1853
1854 do {
Al Viro82a2c1b2011-06-22 18:30:55 -04001855 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 if (status == 0)
1857 break;
1858 /* NOTE: BAD_SEQID means the server and client disagree about the
1859 * book-keeping w.r.t. state-changing operations
1860 * (OPEN/CLOSE/LOCK/LOCKU...)
1861 * It is actually a sign of a bug on the client or on the server.
1862 *
1863 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001864 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 * have unhashed the old state_owner for us, and that we can
1866 * therefore safely retry using a new one. We should still warn
1867 * the user though...
1868 */
1869 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04001870 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001871 " returned a bad sequence-id error!\n",
1872 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 exception.retry = 1;
1874 continue;
1875 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001876 /*
1877 * BAD_STATEID on OPEN means that the server cancelled our
1878 * state before it received the OPEN_CONFIRM.
1879 * Recover by retrying the request as per the discussion
1880 * on Page 181 of RFC3530.
1881 */
1882 if (status == -NFS4ERR_BAD_STATEID) {
1883 exception.retry = 1;
1884 continue;
1885 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001886 if (status == -EAGAIN) {
1887 /* We must have found a delegation */
1888 exception.retry = 1;
1889 continue;
1890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1892 status, &exception));
1893 } while (exception.retry);
1894 return res;
1895}
1896
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001897static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1898 struct nfs_fattr *fattr, struct iattr *sattr,
1899 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001901 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001903 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 .iap = sattr,
1905 .server = server,
1906 .bitmask = server->attr_bitmask,
1907 };
1908 struct nfs_setattrres res = {
1909 .fattr = fattr,
1910 .server = server,
1911 };
1912 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001913 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1914 .rpc_argp = &arg,
1915 .rpc_resp = &res,
1916 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001918 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001919 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Trond Myklebust0e574af2005-10-27 22:12:38 -04001921 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Trond Myklebust4fc87962012-03-08 17:42:01 -05001923 if (state != NULL) {
1924 nfs4_select_rw_stateid(&arg.stateid, state, FMODE_WRITE,
1925 current->files, current->tgid);
1926 } else if (nfs4_copy_delegation_stateid(&arg.stateid, inode,
1927 FMODE_WRITE)) {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001928 /* Use that stateid */
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001929 } else
Trond Myklebustf597c532012-03-04 18:13:56 -05001930 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
Bryan Schumaker7c513052011-03-24 17:12:24 +00001932 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001933 if (status == 0 && state != NULL)
1934 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001935 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936}
1937
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001938static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1939 struct nfs_fattr *fattr, struct iattr *sattr,
1940 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001942 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001943 struct nfs4_exception exception = {
1944 .state = state,
Trond Myklebust3114ea72012-03-07 16:39:06 -05001945 .inode = inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05001946 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 int err;
1948 do {
Trond Myklebust451146b2012-04-18 16:29:11 -04001949 err = _nfs4_do_setattr(inode, cred, fattr, sattr, state);
1950 switch (err) {
1951 case -NFS4ERR_OPENMODE:
1952 if (state && !(state->state & FMODE_WRITE)) {
1953 err = -EBADF;
1954 if (sattr->ia_valid & ATTR_OPEN)
1955 err = -EACCES;
1956 goto out;
1957 }
1958 }
1959 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04001961out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 return err;
1963}
1964
1965struct nfs4_closedata {
1966 struct inode *inode;
1967 struct nfs4_state *state;
1968 struct nfs_closeargs arg;
1969 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001970 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001971 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00001972 bool roc;
1973 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974};
1975
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001976static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001977{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001978 struct nfs4_closedata *calldata = data;
1979 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04001980 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07001981
Fred Isamanf7e89172011-01-06 11:36:32 +00001982 if (calldata->roc)
1983 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001984 nfs4_put_open_state(calldata->state);
1985 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001986 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04001987 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07001988 kfree(calldata);
1989}
1990
Trond Myklebust88069f72009-12-08 08:33:16 -05001991static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1992 fmode_t fmode)
1993{
1994 spin_lock(&state->owner->so_lock);
1995 if (!(fmode & FMODE_READ))
1996 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1997 if (!(fmode & FMODE_WRITE))
1998 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1999 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2000 spin_unlock(&state->owner->so_lock);
2001}
2002
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002003static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002005 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 struct nfs_server *server = NFS_SERVER(calldata->inode);
2008
Chuck Levera3ca5652012-03-01 17:00:40 -05002009 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04002010 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
2011 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 /* hmm. we are done with the inode, and in the process of freeing
2013 * the state_owner. we keep this around to process errors
2014 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 switch (task->tk_status) {
2016 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00002017 if (calldata->roc)
2018 pnfs_roc_set_barrier(state->inode,
2019 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04002020 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002021 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05002022 nfs4_close_clear_stateid_flags(state,
2023 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 break;
2025 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002026 case -NFS4ERR_OLD_STATEID:
2027 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002029 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002030 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05002032 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
2033 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 }
Trond Myklebust72211db2009-12-15 14:47:36 -05002035 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04002036 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05002037 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038}
2039
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002040static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002042 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07002043 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05002044 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07002045
Chuck Levera3ca5652012-03-01 17:00:40 -05002046 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002047 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07002048 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04002049
Trond Myklebust88069f72009-12-08 08:33:16 -05002050 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
2051 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05002052 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04002053 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01002054 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04002055 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002056 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
2057 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2058 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04002059 }
2060 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002061 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2062 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2063 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002064 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002065 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002066 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002067
2068 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002069 /* Note: exit _without_ calling nfs4_close_done */
2070 task->tk_action = NULL;
Chuck Levera3ca5652012-03-01 17:00:40 -05002071 goto out;
Trond Myklebust95121352005-10-18 14:20:12 -07002072 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002073
Fred Isamanf7e89172011-01-06 11:36:32 +00002074 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002075 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002076 if (calldata->roc &&
2077 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2078 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2079 task, NULL);
Chuck Levera3ca5652012-03-01 17:00:40 -05002080 goto out;
Fred Isamanf7e89172011-01-06 11:36:32 +00002081 }
2082 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002083
Trond Myklebust516a6af2005-10-27 22:12:41 -04002084 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002085 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002086 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Trond Myklebust9d12b212012-01-17 22:04:25 -05002087 &calldata->arg.seq_args,
2088 &calldata->res.seq_res,
2089 task))
Chuck Levera3ca5652012-03-01 17:00:40 -05002090 goto out;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002091 rpc_call_start(task);
Chuck Levera3ca5652012-03-01 17:00:40 -05002092out:
2093 dprintk("%s: done!\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094}
2095
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002096static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002097 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002098 .rpc_call_done = nfs4_close_done,
2099 .rpc_release = nfs4_free_closedata,
2100};
2101
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102/*
2103 * It is possible for data to be read/written from a mem-mapped file
2104 * after the sys_close call (which hits the vfs layer as a flush).
2105 * This means that we can't safely call nfsv4 close on a file until
2106 * the inode is cleared. This in turn means that we are not good
2107 * NFSv4 citizens - we do not indicate to the server to update the file's
2108 * share state even when we are done with one of the three share
2109 * stateid's in the inode.
2110 *
2111 * NOTE: Caller must be holding the sp->so_owner semaphore!
2112 */
Al Viro643168c2011-06-22 18:20:23 -04002113int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002115 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002117 struct nfs4_state_owner *sp = state->owner;
2118 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002119 struct rpc_message msg = {
2120 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2121 .rpc_cred = state->owner->so_cred,
2122 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002123 struct rpc_task_setup task_setup_data = {
2124 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002125 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002126 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002127 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002128 .flags = RPC_TASK_ASYNC,
2129 };
Trond Myklebust95121352005-10-18 14:20:12 -07002130 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002132 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002134 goto out;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002135 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002136 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002138 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002139 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002141 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002142 if (calldata->arg.seqid == NULL)
2143 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002144 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002145 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002146 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002147 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002148 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002149 calldata->roc = roc;
Al Viro643168c2011-06-22 18:20:23 -04002150 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002151
Trond Myklebust1174dd12010-12-21 10:52:24 -05002152 msg.rpc_argp = &calldata->arg;
2153 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002154 task_setup_data.callback_data = calldata;
2155 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002156 if (IS_ERR(task))
2157 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002158 status = 0;
2159 if (wait)
2160 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002161 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002162 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002163out_free_calldata:
2164 kfree(calldata);
2165out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002166 if (roc)
2167 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002168 nfs4_put_open_state(state);
2169 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002170 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171}
2172
Trond Myklebust2b484292010-09-17 10:56:51 -04002173static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002174nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 struct nfs4_state *state;
2177
Trond Myklebust565277f2007-10-15 18:17:53 -04002178 /* Protect against concurrent sillydeletes */
Al Viro3d4ff432011-06-22 18:40:12 -04002179 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002180 if (IS_ERR(state))
2181 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002182 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002183 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184}
2185
Trond Myklebust1185a552009-12-03 15:54:02 -05002186static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002187{
2188 if (ctx->state == NULL)
2189 return;
2190 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002191 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002192 else
Al Viro643168c2011-06-22 18:20:23 -04002193 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002194}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
2196static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2197{
Benny Halevy43652ad2009-04-01 09:21:54 -04002198 struct nfs4_server_caps_arg args = {
2199 .fhandle = fhandle,
2200 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 struct nfs4_server_caps_res res = {};
2202 struct rpc_message msg = {
2203 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002204 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 .rpc_resp = &res,
2206 };
2207 int status;
2208
Bryan Schumaker7c513052011-03-24 17:12:24 +00002209 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 if (status == 0) {
2211 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002212 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2213 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2214 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2215 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2216 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2218 server->caps |= NFS_CAP_ACLS;
2219 if (res.has_links != 0)
2220 server->caps |= NFS_CAP_HARDLINKS;
2221 if (res.has_symlinks != 0)
2222 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002223 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2224 server->caps |= NFS_CAP_FILEID;
2225 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2226 server->caps |= NFS_CAP_MODE;
2227 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2228 server->caps |= NFS_CAP_NLINK;
2229 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2230 server->caps |= NFS_CAP_OWNER;
2231 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2232 server->caps |= NFS_CAP_OWNER_GROUP;
2233 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2234 server->caps |= NFS_CAP_ATIME;
2235 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2236 server->caps |= NFS_CAP_CTIME;
2237 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2238 server->caps |= NFS_CAP_MTIME;
2239
Trond Myklebusta65318b2009-03-11 14:10:28 -04002240 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2241 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2242 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05002244 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002246
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 return status;
2248}
2249
Trond Myklebust55a97592006-06-09 09:34:19 -04002250int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251{
2252 struct nfs4_exception exception = { };
2253 int err;
2254 do {
2255 err = nfs4_handle_exception(server,
2256 _nfs4_server_capabilities(server, fhandle),
2257 &exception);
2258 } while (exception.retry);
2259 return err;
2260}
2261
2262static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2263 struct nfs_fsinfo *info)
2264{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 struct nfs4_lookup_root_arg args = {
2266 .bitmask = nfs4_fattr_bitmap,
2267 };
2268 struct nfs4_lookup_res res = {
2269 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002270 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 .fh = fhandle,
2272 };
2273 struct rpc_message msg = {
2274 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2275 .rpc_argp = &args,
2276 .rpc_resp = &res,
2277 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002278
Trond Myklebust0e574af2005-10-27 22:12:38 -04002279 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002280 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281}
2282
2283static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2284 struct nfs_fsinfo *info)
2285{
2286 struct nfs4_exception exception = { };
2287 int err;
2288 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002289 err = _nfs4_lookup_root(server, fhandle, info);
2290 switch (err) {
2291 case 0:
2292 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002293 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002294 default:
2295 err = nfs4_handle_exception(server, err, &exception);
2296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04002298out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 return err;
2300}
2301
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002302static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2303 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2304{
2305 struct rpc_auth *auth;
2306 int ret;
2307
2308 auth = rpcauth_create(flavor, server->client);
2309 if (!auth) {
2310 ret = -EIO;
2311 goto out;
2312 }
2313 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002314out:
2315 return ret;
2316}
2317
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002318static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002319 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002321 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002322 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002324 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2325 flav_array[len] = RPC_AUTH_NULL;
2326 len += 1;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002327
2328 for (i = 0; i < len; i++) {
2329 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002330 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002331 continue;
2332 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002333 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002334 /*
2335 * -EACCESS could mean that the user doesn't have correct permissions
2336 * to access the mount. It could also mean that we tried to mount
2337 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2338 * existing mount programs don't handle -EACCES very well so it should
2339 * be mapped to -EPERM instead.
2340 */
2341 if (status == -EACCES)
2342 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002343 return status;
2344}
2345
2346/*
2347 * get the file handle for the "/" directory on the server
2348 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04002349int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
2350 struct nfs_fsinfo *info)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002351{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002352 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002353 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002354 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2355 /*
2356 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2357 * by nfs4_map_errors() as this function exits.
2358 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002359 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 if (status == 0)
2361 status = nfs4_server_capabilities(server, fhandle);
2362 if (status == 0)
2363 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002364 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365}
2366
Bryan Schumakerbae36242012-05-10 15:07:31 -04002367static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
2368 struct nfs_fsinfo *info)
2369{
2370 int error;
2371 struct nfs_fattr *fattr = info->fattr;
2372
2373 error = nfs4_server_capabilities(server, mntfh);
2374 if (error < 0) {
2375 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
2376 return error;
2377 }
2378
2379 error = nfs4_proc_getattr(server, mntfh, fattr);
2380 if (error < 0) {
2381 dprintk("nfs4_get_root: getattr error = %d\n", -error);
2382 return error;
2383 }
2384
2385 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
2386 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
2387 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
2388
2389 return error;
2390}
2391
Manoj Naik6b97fd32006-06-09 09:34:29 -04002392/*
2393 * Get locations and (maybe) other attributes of a referral.
2394 * Note that we'll actually follow the referral later when
2395 * we detect fsid mismatch in inode revalidation
2396 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002397static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
2398 const struct qstr *name, struct nfs_fattr *fattr,
2399 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002400{
2401 int status = -ENOMEM;
2402 struct page *page = NULL;
2403 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002404
2405 page = alloc_page(GFP_KERNEL);
2406 if (page == NULL)
2407 goto out;
2408 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2409 if (locations == NULL)
2410 goto out;
2411
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002412 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002413 if (status != 0)
2414 goto out;
2415 /* Make sure server returned a different fsid for the referral */
2416 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002417 dprintk("%s: server did not return a different fsid for"
2418 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002419 status = -EIO;
2420 goto out;
2421 }
Andy Adamson533eb462011-06-13 18:25:56 -04002422 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2423 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002424
Andy Adamson533eb462011-06-13 18:25:56 -04002425 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002426 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002427 memset(fhandle, 0, sizeof(struct nfs_fh));
2428out:
2429 if (page)
2430 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002431 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002432 return status;
2433}
2434
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2436{
2437 struct nfs4_getattr_arg args = {
2438 .fh = fhandle,
2439 .bitmask = server->attr_bitmask,
2440 };
2441 struct nfs4_getattr_res res = {
2442 .fattr = fattr,
2443 .server = server,
2444 };
2445 struct rpc_message msg = {
2446 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2447 .rpc_argp = &args,
2448 .rpc_resp = &res,
2449 };
2450
Trond Myklebust0e574af2005-10-27 22:12:38 -04002451 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002452 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453}
2454
2455static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2456{
2457 struct nfs4_exception exception = { };
2458 int err;
2459 do {
2460 err = nfs4_handle_exception(server,
2461 _nfs4_proc_getattr(server, fhandle, fattr),
2462 &exception);
2463 } while (exception.retry);
2464 return err;
2465}
2466
2467/*
2468 * The file is not closed if it is opened due to the a request to change
2469 * the size of the file. The open call will not be needed once the
2470 * VFS layer lookup-intents are implemented.
2471 *
2472 * Close is called when the inode is destroyed.
2473 * If we haven't opened the file for O_WRONLY, we
2474 * need to in the size_change case to obtain a stateid.
2475 *
2476 * Got race?
2477 * Because OPEN is always done by name in nfsv4, it is
2478 * possible that we opened a different file by the same
2479 * name. We can recognize this race condition, but we
2480 * can't do anything about it besides returning an error.
2481 *
2482 * This will be fixed with VFS changes (lookup-intent).
2483 */
2484static int
2485nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2486 struct iattr *sattr)
2487{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002488 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002489 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002490 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 int status;
2492
Benny Halevy8a1636c2010-07-14 15:43:57 -04002493 if (pnfs_ld_layoutret_on_setattr(inode))
2494 pnfs_return_layout(inode);
2495
Trond Myklebust0e574af2005-10-27 22:12:38 -04002496 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
Trond Myklebustd5308382005-11-04 15:33:38 -05002498 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002499 if (sattr->ia_valid & ATTR_FILE) {
2500 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002501
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002502 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002503 if (ctx) {
2504 cred = ctx->cred;
2505 state = ctx->state;
2506 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002507 }
2508
Trond Myklebust536e43d2012-01-17 22:04:26 -05002509 /* Deal with open(O_TRUNC) */
2510 if (sattr->ia_valid & ATTR_OPEN)
2511 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME|ATTR_OPEN);
2512
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002513 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002514 if (status == 0)
2515 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 return status;
2517}
2518
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002519static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2520 const struct qstr *name, struct nfs_fh *fhandle,
2521 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002522{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002523 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002524 int status;
2525 struct nfs4_lookup_arg args = {
2526 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002527 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002528 .name = name,
2529 };
2530 struct nfs4_lookup_res res = {
2531 .server = server,
2532 .fattr = fattr,
2533 .fh = fhandle,
2534 };
2535 struct rpc_message msg = {
2536 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2537 .rpc_argp = &args,
2538 .rpc_resp = &res,
2539 };
2540
2541 nfs_fattr_init(fattr);
2542
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002544 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 dprintk("NFS reply lookup: %d\n", status);
2546 return status;
2547}
2548
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002549static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002550{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002551 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002552 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002553 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2554 fattr->nlink = 2;
2555}
2556
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002557static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
2558 struct qstr *name, struct nfs_fh *fhandle,
2559 struct nfs_fattr *fattr)
2560{
2561 struct nfs4_exception exception = { };
2562 struct rpc_clnt *client = *clnt;
2563 int err;
2564 do {
2565 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr);
2566 switch (err) {
2567 case -NFS4ERR_BADNAME:
2568 err = -ENOENT;
2569 goto out;
2570 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002571 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002572 goto out;
2573 case -NFS4ERR_WRONGSEC:
2574 err = -EPERM;
2575 if (client != *clnt)
2576 goto out;
2577
2578 client = nfs4_create_sec_client(client, dir, name);
2579 if (IS_ERR(client))
2580 return PTR_ERR(client);
2581
2582 exception.retry = 1;
2583 break;
2584 default:
2585 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
2586 }
2587 } while (exception.retry);
2588
2589out:
2590 if (err == 0)
2591 *clnt = client;
2592 else if (client != *clnt)
2593 rpc_shutdown_client(client);
2594
2595 return err;
2596}
2597
Bryan Schumaker80a16b22012-04-27 13:27:46 -04002598static int nfs4_proc_lookup(struct inode *dir, struct qstr *name,
Bryan Schumaker7c513052011-03-24 17:12:24 +00002599 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002601 int status;
2602 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002603
Bryan Schumaker72de53e2012-04-27 13:27:40 -04002604 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2605 if (client != NFS_CLIENT(dir)) {
2606 rpc_shutdown_client(client);
2607 nfs_fixup_secinfo_attributes(fattr);
2608 }
2609 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610}
2611
Bryan Schumakerf05d1472012-04-27 13:27:41 -04002612struct rpc_clnt *
2613nfs4_proc_lookup_mountpoint(struct inode *dir, struct qstr *name,
2614 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2615{
2616 int status;
2617 struct rpc_clnt *client = rpc_clone_client(NFS_CLIENT(dir));
2618
2619 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr);
2620 if (status < 0) {
2621 rpc_shutdown_client(client);
2622 return ERR_PTR(status);
2623 }
2624 return client;
2625}
2626
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2628{
Trond Myklebust76b32992007-08-10 17:45:11 -04002629 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 struct nfs4_accessargs args = {
2631 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05002632 .bitmask = server->cache_consistency_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002634 struct nfs4_accessres res = {
2635 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002636 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 struct rpc_message msg = {
2638 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2639 .rpc_argp = &args,
2640 .rpc_resp = &res,
2641 .rpc_cred = entry->cred,
2642 };
2643 int mode = entry->mask;
2644 int status;
2645
2646 /*
2647 * Determine which access bits we want to ask for...
2648 */
2649 if (mode & MAY_READ)
2650 args.access |= NFS4_ACCESS_READ;
2651 if (S_ISDIR(inode->i_mode)) {
2652 if (mode & MAY_WRITE)
2653 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2654 if (mode & MAY_EXEC)
2655 args.access |= NFS4_ACCESS_LOOKUP;
2656 } else {
2657 if (mode & MAY_WRITE)
2658 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2659 if (mode & MAY_EXEC)
2660 args.access |= NFS4_ACCESS_EXECUTE;
2661 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002662
2663 res.fattr = nfs_alloc_fattr();
2664 if (res.fattr == NULL)
2665 return -ENOMEM;
2666
Bryan Schumaker7c513052011-03-24 17:12:24 +00002667 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 if (!status) {
2669 entry->mask = 0;
2670 if (res.access & NFS4_ACCESS_READ)
2671 entry->mask |= MAY_READ;
2672 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2673 entry->mask |= MAY_WRITE;
2674 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2675 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002676 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002678 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 return status;
2680}
2681
2682static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2683{
2684 struct nfs4_exception exception = { };
2685 int err;
2686 do {
2687 err = nfs4_handle_exception(NFS_SERVER(inode),
2688 _nfs4_proc_access(inode, entry),
2689 &exception);
2690 } while (exception.retry);
2691 return err;
2692}
2693
2694/*
2695 * TODO: For the time being, we don't try to get any attributes
2696 * along with any of the zero-copy operations READ, READDIR,
2697 * READLINK, WRITE.
2698 *
2699 * In the case of the first three, we want to put the GETATTR
2700 * after the read-type operation -- this is because it is hard
2701 * to predict the length of a GETATTR response in v4, and thus
2702 * align the READ data correctly. This means that the GETATTR
2703 * may end up partially falling into the page cache, and we should
2704 * shift it into the 'tail' of the xdr_buf before processing.
2705 * To do this efficiently, we need to know the total length
2706 * of data received, which doesn't seem to be available outside
2707 * of the RPC layer.
2708 *
2709 * In the case of WRITE, we also want to put the GETATTR after
2710 * the operation -- in this case because we want to make sure
2711 * we get the post-operation mtime and size. This means that
2712 * we can't use xdr_encode_pages() as written: we need a variant
2713 * of it which would leave room in the 'tail' iovec.
2714 *
2715 * Both of these changes to the XDR layer would in fact be quite
2716 * minor, but I decided to leave them for a subsequent patch.
2717 */
2718static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2719 unsigned int pgbase, unsigned int pglen)
2720{
2721 struct nfs4_readlink args = {
2722 .fh = NFS_FH(inode),
2723 .pgbase = pgbase,
2724 .pglen = pglen,
2725 .pages = &page,
2726 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002727 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 struct rpc_message msg = {
2729 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2730 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002731 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 };
2733
Bryan Schumaker7c513052011-03-24 17:12:24 +00002734 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 -07002735}
2736
2737static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2738 unsigned int pgbase, unsigned int pglen)
2739{
2740 struct nfs4_exception exception = { };
2741 int err;
2742 do {
2743 err = nfs4_handle_exception(NFS_SERVER(inode),
2744 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2745 &exception);
2746 } while (exception.retry);
2747 return err;
2748}
2749
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750/*
2751 * Got race?
2752 * We will need to arrange for the VFS layer to provide an atomic open.
2753 * Until then, this create/open method is prone to inefficiency and race
2754 * conditions due to the lookup, create, and open VFS calls from sys_open()
2755 * placed on the wire.
2756 *
2757 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2758 * The file will be opened again in the subsequent VFS open call
2759 * (nfs4_proc_file_open).
2760 *
2761 * The open for read will just hang around to be used by any process that
2762 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2763 */
2764
2765static int
2766nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002767 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768{
Al Viro82a2c1b2011-06-22 18:30:55 -04002769 struct dentry *de = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002771 struct rpc_cred *cred = NULL;
2772 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 int status = 0;
2774
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002775 if (ctx != NULL) {
2776 cred = ctx->cred;
Al Viro3d4ff432011-06-22 18:40:12 -04002777 de = ctx->dentry;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002778 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002780 sattr->ia_mode &= ~current_umask();
Al Viro82a2c1b2011-06-22 18:30:55 -04002781 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002782 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 if (IS_ERR(state)) {
2784 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002785 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002787 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002788 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002789 if (ctx != NULL)
2790 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002791 else
Al Viro643168c2011-06-22 18:20:23 -04002792 nfs4_close_sync(state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793out:
2794 return status;
2795}
2796
2797static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2798{
Trond Myklebust16e42952005-10-27 22:12:44 -04002799 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002800 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002802 .name.len = name->len,
2803 .name.name = name->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002805 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002806 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002807 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002809 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2810 .rpc_argp = &args,
2811 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 };
Trond Myklebust778d2812012-04-27 13:48:19 -04002813 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04002814
Bryan Schumaker7c513052011-03-24 17:12:24 +00002815 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04002816 if (status == 0)
Trond Myklebust16e42952005-10-27 22:12:44 -04002817 update_changeattr(dir, &res.cinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 return status;
2819}
2820
2821static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2822{
2823 struct nfs4_exception exception = { };
2824 int err;
2825 do {
2826 err = nfs4_handle_exception(NFS_SERVER(dir),
2827 _nfs4_proc_remove(dir, name),
2828 &exception);
2829 } while (exception.retry);
2830 return err;
2831}
2832
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002833static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002835 struct nfs_server *server = NFS_SERVER(dir);
2836 struct nfs_removeargs *args = msg->rpc_argp;
2837 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002839 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05002841 nfs41_init_sequence(&args->seq_args, &res->seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842}
2843
Bryan Schumaker34e137c2012-03-19 14:54:41 -04002844static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
2845{
2846 if (nfs4_setup_sequence(NFS_SERVER(data->dir),
2847 &data->args.seq_args,
2848 &data->res.seq_res,
2849 task))
2850 return;
2851 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852}
2853
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002854static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002856 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2857
Trond Myklebust14516c32010-07-31 14:29:06 -04002858 if (!nfs4_sequence_done(task, &res->seq_res))
2859 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002860 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002861 return 0;
2862 update_changeattr(dir, &res->cinfo);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002863 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864}
2865
Jeff Laytond3d41522010-09-17 17:31:57 -04002866static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2867{
2868 struct nfs_server *server = NFS_SERVER(dir);
2869 struct nfs_renameargs *arg = msg->rpc_argp;
2870 struct nfs_renameres *res = msg->rpc_resp;
2871
2872 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04002873 res->server = server;
Trond Myklebust9d12b212012-01-17 22:04:25 -05002874 nfs41_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04002875}
2876
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04002877static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
2878{
2879 if (nfs4_setup_sequence(NFS_SERVER(data->old_dir),
2880 &data->args.seq_args,
2881 &data->res.seq_res,
2882 task))
2883 return;
2884 rpc_call_start(task);
Jeff Laytond3d41522010-09-17 17:31:57 -04002885}
2886
2887static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2888 struct inode *new_dir)
2889{
2890 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2891
2892 if (!nfs4_sequence_done(task, &res->seq_res))
2893 return 0;
2894 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2895 return 0;
2896
2897 update_changeattr(old_dir, &res->old_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002898 update_changeattr(new_dir, &res->new_cinfo);
Jeff Laytond3d41522010-09-17 17:31:57 -04002899 return 1;
2900}
2901
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2903 struct inode *new_dir, struct qstr *new_name)
2904{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002905 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002906 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 .old_dir = NFS_FH(old_dir),
2908 .new_dir = NFS_FH(new_dir),
2909 .old_name = old_name,
2910 .new_name = new_name,
2911 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002912 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002913 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002914 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 struct rpc_message msg = {
2916 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2917 .rpc_argp = &arg,
2918 .rpc_resp = &res,
2919 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002920 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921
Bryan Schumaker7c513052011-03-24 17:12:24 +00002922 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 if (!status) {
2924 update_changeattr(old_dir, &res.old_cinfo);
2925 update_changeattr(new_dir, &res.new_cinfo);
2926 }
2927 return status;
2928}
2929
2930static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2931 struct inode *new_dir, struct qstr *new_name)
2932{
2933 struct nfs4_exception exception = { };
2934 int err;
2935 do {
2936 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2937 _nfs4_proc_rename(old_dir, old_name,
2938 new_dir, new_name),
2939 &exception);
2940 } while (exception.retry);
2941 return err;
2942}
2943
2944static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2945{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002946 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 struct nfs4_link_arg arg = {
2948 .fh = NFS_FH(inode),
2949 .dir_fh = NFS_FH(dir),
2950 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002951 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002953 struct nfs4_link_res res = {
2954 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002955 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 struct rpc_message msg = {
2957 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2958 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002959 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002961 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962
Trond Myklebust136f2622010-04-16 16:22:49 -04002963 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04002964 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04002965 goto out;
2966
Bryan Schumaker7c513052011-03-24 17:12:24 +00002967 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002968 if (!status) {
2969 update_changeattr(dir, &res.cinfo);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002970 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002971 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002972out:
Trond Myklebust136f2622010-04-16 16:22:49 -04002973 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 return status;
2975}
2976
2977static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2978{
2979 struct nfs4_exception exception = { };
2980 int err;
2981 do {
2982 err = nfs4_handle_exception(NFS_SERVER(inode),
2983 _nfs4_proc_link(inode, dir, name),
2984 &exception);
2985 } while (exception.retry);
2986 return err;
2987}
2988
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002989struct nfs4_createdata {
2990 struct rpc_message msg;
2991 struct nfs4_create_arg arg;
2992 struct nfs4_create_res res;
2993 struct nfs_fh fh;
2994 struct nfs_fattr fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002995};
2996
2997static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2998 struct qstr *name, struct iattr *sattr, u32 ftype)
2999{
3000 struct nfs4_createdata *data;
3001
3002 data = kzalloc(sizeof(*data), GFP_KERNEL);
3003 if (data != NULL) {
3004 struct nfs_server *server = NFS_SERVER(dir);
3005
3006 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
3007 data->msg.rpc_argp = &data->arg;
3008 data->msg.rpc_resp = &data->res;
3009 data->arg.dir_fh = NFS_FH(dir);
3010 data->arg.server = server;
3011 data->arg.name = name;
3012 data->arg.attrs = sattr;
3013 data->arg.ftype = ftype;
3014 data->arg.bitmask = server->attr_bitmask;
3015 data->res.server = server;
3016 data->res.fh = &data->fh;
3017 data->res.fattr = &data->fattr;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003018 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003019 }
3020 return data;
3021}
3022
3023static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
3024{
Bryan Schumaker7c513052011-03-24 17:12:24 +00003025 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003026 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003027 if (status == 0) {
3028 update_changeattr(dir, &data->res.dir_cinfo);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003029 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
3030 }
3031 return status;
3032}
3033
3034static void nfs4_free_createdata(struct nfs4_createdata *data)
3035{
3036 kfree(data);
3037}
3038
Chuck Lever4f390c12006-08-22 20:06:22 -04003039static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003040 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003042 struct nfs4_createdata *data;
3043 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044
Chuck Lever94a6d752006-08-22 20:06:23 -04003045 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003046 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04003047
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003048 status = -ENOMEM;
3049 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
3050 if (data == NULL)
3051 goto out;
3052
3053 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
3054 data->arg.u.symlink.pages = &page;
3055 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003057 status = nfs4_do_create(dir, dentry, data);
3058
3059 nfs4_free_createdata(data);
3060out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 return status;
3062}
3063
Chuck Lever4f390c12006-08-22 20:06:22 -04003064static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04003065 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066{
3067 struct nfs4_exception exception = { };
3068 int err;
3069 do {
3070 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04003071 _nfs4_proc_symlink(dir, dentry, page,
3072 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 &exception);
3074 } while (exception.retry);
3075 return err;
3076}
3077
3078static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3079 struct iattr *sattr)
3080{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003081 struct nfs4_createdata *data;
3082 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003084 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
3085 if (data == NULL)
3086 goto out;
3087
3088 status = nfs4_do_create(dir, dentry, data);
3089
3090 nfs4_free_createdata(data);
3091out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 return status;
3093}
3094
3095static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
3096 struct iattr *sattr)
3097{
3098 struct nfs4_exception exception = { };
3099 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003100
3101 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 do {
3103 err = nfs4_handle_exception(NFS_SERVER(dir),
3104 _nfs4_proc_mkdir(dir, dentry, sattr),
3105 &exception);
3106 } while (exception.retry);
3107 return err;
3108}
3109
3110static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003111 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112{
3113 struct inode *dir = dentry->d_inode;
3114 struct nfs4_readdir_arg args = {
3115 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003116 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 .pgbase = 0,
3118 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003119 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003120 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 };
3122 struct nfs4_readdir_res res;
3123 struct rpc_message msg = {
3124 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3125 .rpc_argp = &args,
3126 .rpc_resp = &res,
3127 .rpc_cred = cred,
3128 };
3129 int status;
3130
Harvey Harrison3110ff82008-05-02 13:42:44 -07003131 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003132 dentry->d_parent->d_name.name,
3133 dentry->d_name.name,
3134 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3136 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003137 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 -05003138 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003140 status += args.pgbase;
3141 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003142
3143 nfs_invalidate_atime(dir);
3144
Harvey Harrison3110ff82008-05-02 13:42:44 -07003145 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 return status;
3147}
3148
3149static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003150 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151{
3152 struct nfs4_exception exception = { };
3153 int err;
3154 do {
3155 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3156 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003157 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 &exception);
3159 } while (exception.retry);
3160 return err;
3161}
3162
3163static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3164 struct iattr *sattr, dev_t rdev)
3165{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003166 struct nfs4_createdata *data;
3167 int mode = sattr->ia_mode;
3168 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169
3170 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3171 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003172
3173 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3174 if (data == NULL)
3175 goto out;
3176
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003178 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003180 data->arg.ftype = NF4BLK;
3181 data->arg.u.device.specdata1 = MAJOR(rdev);
3182 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 }
3184 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003185 data->arg.ftype = NF4CHR;
3186 data->arg.u.device.specdata1 = MAJOR(rdev);
3187 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003190 status = nfs4_do_create(dir, dentry, data);
3191
3192 nfs4_free_createdata(data);
3193out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 return status;
3195}
3196
3197static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3198 struct iattr *sattr, dev_t rdev)
3199{
3200 struct nfs4_exception exception = { };
3201 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003202
3203 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 do {
3205 err = nfs4_handle_exception(NFS_SERVER(dir),
3206 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3207 &exception);
3208 } while (exception.retry);
3209 return err;
3210}
3211
3212static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3213 struct nfs_fsstat *fsstat)
3214{
3215 struct nfs4_statfs_arg args = {
3216 .fh = fhandle,
3217 .bitmask = server->attr_bitmask,
3218 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003219 struct nfs4_statfs_res res = {
3220 .fsstat = fsstat,
3221 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 struct rpc_message msg = {
3223 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3224 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003225 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 };
3227
Trond Myklebust0e574af2005-10-27 22:12:38 -04003228 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003229 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230}
3231
3232static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3233{
3234 struct nfs4_exception exception = { };
3235 int err;
3236 do {
3237 err = nfs4_handle_exception(server,
3238 _nfs4_proc_statfs(server, fhandle, fsstat),
3239 &exception);
3240 } while (exception.retry);
3241 return err;
3242}
3243
3244static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3245 struct nfs_fsinfo *fsinfo)
3246{
3247 struct nfs4_fsinfo_arg args = {
3248 .fh = fhandle,
3249 .bitmask = server->attr_bitmask,
3250 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003251 struct nfs4_fsinfo_res res = {
3252 .fsinfo = fsinfo,
3253 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 struct rpc_message msg = {
3255 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3256 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003257 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 };
3259
Bryan Schumaker7c513052011-03-24 17:12:24 +00003260 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261}
3262
3263static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3264{
3265 struct nfs4_exception exception = { };
3266 int err;
3267
3268 do {
3269 err = nfs4_handle_exception(server,
3270 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3271 &exception);
3272 } while (exception.retry);
3273 return err;
3274}
3275
3276static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3277{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003278 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3280}
3281
3282static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3283 struct nfs_pathconf *pathconf)
3284{
3285 struct nfs4_pathconf_arg args = {
3286 .fh = fhandle,
3287 .bitmask = server->attr_bitmask,
3288 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003289 struct nfs4_pathconf_res res = {
3290 .pathconf = pathconf,
3291 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 struct rpc_message msg = {
3293 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3294 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003295 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 };
3297
3298 /* None of the pathconf attributes are mandatory to implement */
3299 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3300 memset(pathconf, 0, sizeof(*pathconf));
3301 return 0;
3302 }
3303
Trond Myklebust0e574af2005-10-27 22:12:38 -04003304 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003305 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306}
3307
3308static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3309 struct nfs_pathconf *pathconf)
3310{
3311 struct nfs4_exception exception = { };
3312 int err;
3313
3314 do {
3315 err = nfs4_handle_exception(server,
3316 _nfs4_proc_pathconf(server, fhandle, pathconf),
3317 &exception);
3318 } while (exception.retry);
3319 return err;
3320}
3321
Benny Halevyd20581a2011-05-22 19:52:03 +03003322void __nfs4_read_done_cb(struct nfs_read_data *data)
3323{
Fred Isamancd841602012-04-20 14:47:44 -04003324 nfs_invalidate_atime(data->header->inode);
Benny Halevyd20581a2011-05-22 19:52:03 +03003325}
3326
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003327static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328{
Fred Isamancd841602012-04-20 14:47:44 -04003329 struct nfs_server *server = NFS_SERVER(data->header->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003331 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003332 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003333 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003335
Benny Halevyd20581a2011-05-22 19:52:03 +03003336 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003338 renew_lease(server, data->timestamp);
3339 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340}
3341
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003342static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3343{
3344
3345 dprintk("--> %s\n", __func__);
3346
3347 if (!nfs4_sequence_done(task, &data->res.seq_res))
3348 return -EAGAIN;
3349
Benny Halevyd20581a2011-05-22 19:52:03 +03003350 return data->read_done_cb ? data->read_done_cb(task, data) :
3351 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003352}
3353
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003354static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003357 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003358 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003359 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360}
3361
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003362static void nfs4_proc_read_rpc_prepare(struct rpc_task *task, struct nfs_read_data *data)
3363{
Fred Isamancd841602012-04-20 14:47:44 -04003364 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerea7c3302012-03-19 14:54:40 -04003365 &data->args.seq_args,
3366 &data->res.seq_res,
3367 task))
3368 return;
3369 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370}
3371
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003372/* Reset the the nfs_read_data to send the read to the MDS. */
3373void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3374{
Fred Isamancd841602012-04-20 14:47:44 -04003375 struct nfs_pgio_header *hdr = data->header;
3376 struct inode *inode = hdr->inode;
3377
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003378 dprintk("%s Reset task for i/o through\n", __func__);
Fred Isamancd841602012-04-20 14:47:44 -04003379 data->ds_clp = NULL;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003380 /* offsets will differ in the dense stripe case */
3381 data->args.offset = data->mds_offset;
Fred Isamancd841602012-04-20 14:47:44 -04003382 data->args.fh = NFS_FH(inode);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003383 data->read_done_cb = nfs4_read_done_cb;
Fred Isamancd841602012-04-20 14:47:44 -04003384 task->tk_ops = hdr->mds_ops;
3385 rpc_task_reset_client(task, NFS_CLIENT(inode));
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003386}
3387EXPORT_SYMBOL_GPL(nfs4_reset_read);
3388
Fred Isamanb029bc92011-03-03 15:13:42 +00003389static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390{
Fred Isamancd841602012-04-20 14:47:44 -04003391 struct inode *inode = data->header->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003393 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003394 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003395 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003397 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust5a37f852012-04-28 14:55:16 -04003399 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003400 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003401 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402}
3403
Fred Isamanb029bc92011-03-03 15:13:42 +00003404static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3405{
3406 if (!nfs4_sequence_done(task, &data->res.seq_res))
3407 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003408 return data->write_done_cb ? data->write_done_cb(task, data) :
3409 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003410}
3411
Fred Isamana69aef12011-03-03 15:13:47 +00003412/* Reset the the nfs_write_data to send the write to the MDS. */
3413void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3414{
Fred Isamancd841602012-04-20 14:47:44 -04003415 struct nfs_pgio_header *hdr = data->header;
3416 struct inode *inode = hdr->inode;
3417
Fred Isamana69aef12011-03-03 15:13:47 +00003418 dprintk("%s Reset task for i/o through\n", __func__);
Fred Isamancd841602012-04-20 14:47:44 -04003419 data->ds_clp = NULL;
Fred Isamana69aef12011-03-03 15:13:47 +00003420 data->write_done_cb = nfs4_write_done_cb;
Fred Isamancd841602012-04-20 14:47:44 -04003421 data->args.fh = NFS_FH(inode);
Fred Isamana69aef12011-03-03 15:13:47 +00003422 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3423 data->args.offset = data->mds_offset;
3424 data->res.fattr = &data->fattr;
Fred Isamancd841602012-04-20 14:47:44 -04003425 task->tk_ops = hdr->mds_ops;
3426 rpc_task_reset_client(task, NFS_CLIENT(inode));
Fred Isamana69aef12011-03-03 15:13:47 +00003427}
3428EXPORT_SYMBOL_GPL(nfs4_reset_write);
3429
Trond Myklebust5a37f852012-04-28 14:55:16 -04003430static
3431bool nfs4_write_need_cache_consistency_data(const struct nfs_write_data *data)
3432{
3433 const struct nfs_pgio_header *hdr = data->header;
3434
3435 /* Don't request attributes for pNFS or O_DIRECT writes */
3436 if (data->ds_clp != NULL || hdr->dreq != NULL)
3437 return false;
3438 /* Otherwise, request attributes if and only if we don't hold
3439 * a delegation
3440 */
3441 return nfs_have_delegation(hdr->inode, FMODE_READ) == 0;
3442}
3443
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003444static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445{
Fred Isamancd841602012-04-20 14:47:44 -04003446 struct nfs_server *server = NFS_SERVER(data->header->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003447
Trond Myklebust5a37f852012-04-28 14:55:16 -04003448 if (!nfs4_write_need_cache_consistency_data(data)) {
Fred Isaman7ffd1062011-03-03 15:13:46 +00003449 data->args.bitmask = NULL;
3450 data->res.fattr = NULL;
3451 } else
3452 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04003453
Fred Isamanb029bc92011-03-03 15:13:42 +00003454 if (!data->write_done_cb)
3455 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003456 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 data->timestamp = jiffies;
3458
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003459 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003460 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461}
3462
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003463static void nfs4_proc_write_rpc_prepare(struct rpc_task *task, struct nfs_write_data *data)
3464{
Fred Isamancd841602012-04-20 14:47:44 -04003465 if (nfs4_setup_sequence(NFS_SERVER(data->header->inode),
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04003466 &data->args.seq_args,
3467 &data->res.seq_res,
3468 task))
3469 return;
3470 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471}
3472
Fred Isaman0b7c0152012-04-20 14:47:39 -04003473static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
3474{
3475 if (nfs4_setup_sequence(NFS_SERVER(data->inode),
3476 &data->args.seq_args,
3477 &data->res.seq_res,
3478 task))
3479 return;
3480 rpc_call_start(task);
3481}
3482
3483static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003486
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003487 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003488 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003489 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003491 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492}
3493
Fred Isaman0b7c0152012-04-20 14:47:39 -04003494static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00003495{
3496 if (!nfs4_sequence_done(task, &data->res.seq_res))
3497 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04003498 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00003499}
3500
Fred Isaman0b7c0152012-04-20 14:47:39 -04003501static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003503 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003504
Fred Isaman0b7c0152012-04-20 14:47:39 -04003505 if (data->commit_done_cb == NULL)
3506 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003507 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003508 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Trond Myklebust9d12b212012-01-17 22:04:25 -05003509 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510}
3511
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003512struct nfs4_renewdata {
3513 struct nfs_client *client;
3514 unsigned long timestamp;
3515};
3516
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517/*
3518 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3519 * standalone procedure for queueing an asynchronous RENEW.
3520 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003521static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003522{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003523 struct nfs4_renewdata *data = calldata;
3524 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003525
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003526 if (atomic_read(&clp->cl_count) > 1)
3527 nfs4_schedule_state_renewal(clp);
3528 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003529 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003530}
3531
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003532static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003534 struct nfs4_renewdata *data = calldata;
3535 struct nfs_client *clp = data->client;
3536 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537
3538 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003539 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003540 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3541 return;
3542 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003543 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003544 return;
3545 }
3546 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003548 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549}
3550
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003551static const struct rpc_call_ops nfs4_renew_ops = {
3552 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003553 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003554};
3555
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003556static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557{
3558 struct rpc_message msg = {
3559 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3560 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003561 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003563 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003565 if (renew_flags == 0)
3566 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003567 if (!atomic_inc_not_zero(&clp->cl_count))
3568 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003569 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003570 if (data == NULL)
3571 return -ENOMEM;
3572 data->client = clp;
3573 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003575 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576}
3577
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003578static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579{
3580 struct rpc_message msg = {
3581 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3582 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003583 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 };
3585 unsigned long now = jiffies;
3586 int status;
3587
3588 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3589 if (status < 0)
3590 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003591 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 return 0;
3593}
3594
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003595static inline int nfs4_server_supports_acls(struct nfs_server *server)
3596{
3597 return (server->caps & NFS_CAP_ACLS)
3598 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3599 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3600}
3601
3602/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3603 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3604 * the stack.
3605 */
3606#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3607
Neil Hormane9e3d722011-03-04 19:26:03 -05003608static int buf_to_pages_noslab(const void *buf, size_t buflen,
3609 struct page **pages, unsigned int *pgbase)
3610{
3611 struct page *newpage, **spages;
3612 int rc = 0;
3613 size_t len;
3614 spages = pages;
3615
3616 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003617 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003618 newpage = alloc_page(GFP_KERNEL);
3619
3620 if (newpage == NULL)
3621 goto unwind;
3622 memcpy(page_address(newpage), buf, len);
3623 buf += len;
3624 buflen -= len;
3625 *pages++ = newpage;
3626 rc++;
3627 } while (buflen != 0);
3628
3629 return rc;
3630
3631unwind:
3632 for(; rc > 0; rc--)
3633 __free_page(spages[rc-1]);
3634 return -ENOMEM;
3635}
3636
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003637struct nfs4_cached_acl {
3638 int cached;
3639 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003640 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003641};
3642
3643static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003644{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003645 struct nfs_inode *nfsi = NFS_I(inode);
3646
3647 spin_lock(&inode->i_lock);
3648 kfree(nfsi->nfs4_acl);
3649 nfsi->nfs4_acl = acl;
3650 spin_unlock(&inode->i_lock);
3651}
3652
3653static void nfs4_zap_acl_attr(struct inode *inode)
3654{
3655 nfs4_set_cached_acl(inode, NULL);
3656}
3657
3658static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3659{
3660 struct nfs_inode *nfsi = NFS_I(inode);
3661 struct nfs4_cached_acl *acl;
3662 int ret = -ENOENT;
3663
3664 spin_lock(&inode->i_lock);
3665 acl = nfsi->nfs4_acl;
3666 if (acl == NULL)
3667 goto out;
3668 if (buf == NULL) /* user is just asking for length */
3669 goto out_len;
3670 if (acl->cached == 0)
3671 goto out;
3672 ret = -ERANGE; /* see getxattr(2) man page */
3673 if (acl->len > buflen)
3674 goto out;
3675 memcpy(buf, acl->data, acl->len);
3676out_len:
3677 ret = acl->len;
3678out:
3679 spin_unlock(&inode->i_lock);
3680 return ret;
3681}
3682
Sachin Prabhu5794d212012-04-17 14:36:40 +01003683static 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 +00003684{
3685 struct nfs4_cached_acl *acl;
3686
Sachin Prabhu5794d212012-04-17 14:36:40 +01003687 if (pages && acl_len <= PAGE_SIZE) {
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003688 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3689 if (acl == NULL)
3690 goto out;
3691 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01003692 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003693 } else {
3694 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3695 if (acl == NULL)
3696 goto out;
3697 acl->cached = 0;
3698 }
3699 acl->len = acl_len;
3700out:
3701 nfs4_set_cached_acl(inode, acl);
3702}
3703
Andy Adamsonbf118a32011-12-07 11:55:27 -05003704/*
3705 * The getxattr API returns the required buffer length when called with a
3706 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3707 * the required buf. On a NULL buf, we send a page of data to the server
3708 * guessing that the ACL request can be serviced by a page. If so, we cache
3709 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3710 * the cache. If not so, we throw away the page, and cache the required
3711 * length. The next getxattr call will then produce another round trip to
3712 * the server, this time with the input buf of the required size.
3713 */
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003714static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003715{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003716 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003717 struct nfs_getaclargs args = {
3718 .fh = NFS_FH(inode),
3719 .acl_pages = pages,
3720 .acl_len = buflen,
3721 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003722 struct nfs_getaclres res = {
3723 .acl_len = buflen,
3724 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003725 struct rpc_message msg = {
3726 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3727 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003728 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003729 };
Andy Adamsonbf118a32011-12-07 11:55:27 -05003730 int ret = -ENOMEM, npages, i, acl_len = 0;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003731
Andy Adamsonbf118a32011-12-07 11:55:27 -05003732 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3733 /* As long as we're doing a round trip to the server anyway,
3734 * let's be prepared for a page of acl data. */
3735 if (npages == 0)
3736 npages = 1;
3737
Sachin Prabhu5a006892012-04-17 14:35:39 +01003738 /* Add an extra page to handle the bitmap returned */
3739 npages++;
3740
Andy Adamsonbf118a32011-12-07 11:55:27 -05003741 for (i = 0; i < npages; i++) {
3742 pages[i] = alloc_page(GFP_KERNEL);
3743 if (!pages[i])
3744 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003745 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01003746
3747 /* for decoding across pages */
3748 res.acl_scratch = alloc_page(GFP_KERNEL);
3749 if (!res.acl_scratch)
3750 goto out_free;
3751
Andy Adamsonbf118a32011-12-07 11:55:27 -05003752 args.acl_len = npages * PAGE_SIZE;
3753 args.acl_pgbase = 0;
Sachin Prabhu5a006892012-04-17 14:35:39 +01003754
Andy Adamsonbf118a32011-12-07 11:55:27 -05003755 /* Let decode_getfacl know not to fail if the ACL data is larger than
3756 * the page we send as a guess */
3757 if (buf == NULL)
3758 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003759
Peng Taode040be2012-01-10 22:42:47 +08003760 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05003761 __func__, buf, buflen, npages, args.acl_len);
3762 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3763 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003764 if (ret)
3765 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003766
3767 acl_len = res.acl_len - res.acl_data_offset;
3768 if (acl_len > args.acl_len)
Sachin Prabhu5794d212012-04-17 14:36:40 +01003769 nfs4_write_cached_acl(inode, NULL, 0, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003770 else
Sachin Prabhu5794d212012-04-17 14:36:40 +01003771 nfs4_write_cached_acl(inode, pages, res.acl_data_offset,
Andy Adamsonbf118a32011-12-07 11:55:27 -05003772 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003773 if (buf) {
3774 ret = -ERANGE;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003775 if (acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003776 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003777 _copy_from_pages(buf, pages, res.acl_data_offset,
Sachin Prabhu20e0fa92012-03-22 16:46:28 +00003778 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003779 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003780 ret = acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003781out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003782 for (i = 0; i < npages; i++)
3783 if (pages[i])
3784 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05003785 if (res.acl_scratch)
3786 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003787 return ret;
3788}
3789
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003790static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3791{
3792 struct nfs4_exception exception = { };
3793 ssize_t ret;
3794 do {
3795 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3796 if (ret >= 0)
3797 break;
3798 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3799 } while (exception.retry);
3800 return ret;
3801}
3802
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003803static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3804{
3805 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003806 int ret;
3807
3808 if (!nfs4_server_supports_acls(server))
3809 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003810 ret = nfs_revalidate_inode(server, inode);
3811 if (ret < 0)
3812 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003813 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3814 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003815 ret = nfs4_read_cached_acl(inode, buf, buflen);
3816 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003817 /* -ENOENT is returned if there is no ACL or if there is an ACL
3818 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003819 return ret;
3820 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003821}
3822
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003823static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003824{
3825 struct nfs_server *server = NFS_SERVER(inode);
3826 struct page *pages[NFS4ACL_MAXPAGES];
3827 struct nfs_setaclargs arg = {
3828 .fh = NFS_FH(inode),
3829 .acl_pages = pages,
3830 .acl_len = buflen,
3831 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003832 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003833 struct rpc_message msg = {
3834 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3835 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003836 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003837 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003838 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003839
3840 if (!nfs4_server_supports_acls(server))
3841 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003842 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3843 if (i < 0)
3844 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003845 nfs_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003846 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003847
3848 /*
3849 * Free each page after tx, so the only ref left is
3850 * held by the network stack
3851 */
3852 for (; i > 0; i--)
3853 put_page(pages[i-1]);
3854
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003855 /*
3856 * Acl update can result in inode attribute update.
3857 * so mark the attribute cache invalid.
3858 */
3859 spin_lock(&inode->i_lock);
3860 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3861 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003862 nfs_access_zap_cache(inode);
3863 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003864 return ret;
3865}
3866
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003867static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3868{
3869 struct nfs4_exception exception = { };
3870 int err;
3871 do {
3872 err = nfs4_handle_exception(NFS_SERVER(inode),
3873 __nfs4_proc_set_acl(inode, buf, buflen),
3874 &exception);
3875 } while (exception.retry);
3876 return err;
3877}
3878
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003880nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003882 struct nfs_client *clp = server->nfs_client;
3883
3884 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 return 0;
3886 switch(task->tk_status) {
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05003887 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003888 case -NFS4ERR_ADMIN_REVOKED:
3889 case -NFS4ERR_BAD_STATEID:
Trond Myklebust14977482012-03-27 18:31:25 -04003890 if (state == NULL)
3891 break;
3892 nfs_remove_bad_delegation(state->inode);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003893 case -NFS4ERR_OPENMODE:
3894 if (state == NULL)
3895 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003896 nfs4_schedule_stateid_recovery(server, state);
3897 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003898 case -NFS4ERR_EXPIRED:
3899 if (state != NULL)
3900 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003902 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003903 nfs4_schedule_lease_recovery(clp);
3904 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003905#if defined(CONFIG_NFS_V4_1)
3906 case -NFS4ERR_BADSESSION:
3907 case -NFS4ERR_BADSLOT:
3908 case -NFS4ERR_BAD_HIGH_SLOT:
3909 case -NFS4ERR_DEADSESSION:
3910 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3911 case -NFS4ERR_SEQ_FALSE_RETRY:
3912 case -NFS4ERR_SEQ_MISORDERED:
3913 dprintk("%s ERROR %d, Reset session\n", __func__,
3914 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003915 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -04003916 task->tk_status = 0;
3917 return -EAGAIN;
3918#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003920 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003921 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003922 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3924 task->tk_status = 0;
3925 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003926 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 case -NFS4ERR_OLD_STATEID:
3928 task->tk_status = 0;
3929 return -EAGAIN;
3930 }
3931 task->tk_status = nfs4_map_errors(task->tk_status);
3932 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003933wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003934 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003935 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3936 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3937 task->tk_status = 0;
3938 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939}
3940
Chuck Levercd937102012-03-02 17:14:31 -05003941static void nfs4_construct_boot_verifier(struct nfs_client *clp,
3942 nfs4_verifier *bootverf)
3943{
3944 __be32 verf[2];
3945
3946 verf[0] = htonl((u32)clp->cl_boot_time.tv_sec);
3947 verf[1] = htonl((u32)clp->cl_boot_time.tv_nsec);
3948 memcpy(bootverf->data, verf, sizeof(bootverf->data));
3949}
3950
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003951int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3952 unsigned short port, struct rpc_cred *cred,
3953 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954{
3955 nfs4_verifier sc_verifier;
3956 struct nfs4_setclientid setclientid = {
3957 .sc_verifier = &sc_verifier,
3958 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003959 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 };
3961 struct rpc_message msg = {
3962 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3963 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003964 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003965 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 int loop = 0;
3968 int status;
3969
Chuck Levercd937102012-03-02 17:14:31 -05003970 nfs4_construct_boot_verifier(clp, &sc_verifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971
3972 for(;;) {
Trond Myklebust2446ab62012-03-01 17:00:56 -05003973 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003975 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003976 clp->cl_ipaddr,
3977 rpc_peeraddr2str(clp->cl_rpcclient,
3978 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003979 rpc_peeraddr2str(clp->cl_rpcclient,
3980 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003981 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 clp->cl_id_uniquifier);
3983 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003984 sizeof(setclientid.sc_netid),
3985 rpc_peeraddr2str(clp->cl_rpcclient,
3986 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003988 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 clp->cl_ipaddr, port >> 8, port & 255);
Trond Myklebust2446ab62012-03-01 17:00:56 -05003990 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003992 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 if (status != -NFS4ERR_CLID_INUSE)
3994 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003995 if (loop != 0) {
3996 ++clp->cl_id_uniquifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003998 }
3999 ++loop;
4000 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 }
4002 return status;
4003}
4004
Trond Myklebustfd954ae2011-04-24 14:28:18 -04004005int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004006 struct nfs4_setclientid_res *arg,
4007 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008{
4009 struct nfs_fsinfo fsinfo;
4010 struct rpc_message msg = {
4011 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04004012 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01004014 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 };
4016 unsigned long now;
4017 int status;
4018
4019 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004020 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 if (status == 0) {
4022 spin_lock(&clp->cl_lock);
4023 clp->cl_lease_time = fsinfo.lease_time * HZ;
4024 clp->cl_last_renewal = now;
4025 spin_unlock(&clp->cl_lock);
4026 }
4027 return status;
4028}
4029
Trond Myklebustfe650402006-01-03 09:55:18 +01004030struct nfs4_delegreturndata {
4031 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004032 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01004033 struct nfs_fh fh;
4034 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004035 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004036 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01004037 int rpc_status;
4038};
4039
Trond Myklebustfe650402006-01-03 09:55:18 +01004040static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
4041{
4042 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04004043
Trond Myklebust14516c32010-07-31 14:29:06 -04004044 if (!nfs4_sequence_done(task, &data->res.seq_res))
4045 return;
Andy Adamson938e1012009-04-01 09:22:28 -04004046
Ricardo Labiaga79708862009-12-07 09:23:21 -05004047 switch (task->tk_status) {
4048 case -NFS4ERR_STALE_STATEID:
4049 case -NFS4ERR_EXPIRED:
4050 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01004051 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004052 break;
4053 default:
4054 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
4055 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004056 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05004057 return;
4058 }
4059 }
4060 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01004061}
4062
4063static void nfs4_delegreturn_release(void *calldata)
4064{
Trond Myklebustfe650402006-01-03 09:55:18 +01004065 kfree(calldata);
4066}
4067
Andy Adamson938e1012009-04-01 09:22:28 -04004068#if defined(CONFIG_NFS_V4_1)
4069static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
4070{
4071 struct nfs4_delegreturndata *d_data;
4072
4073 d_data = (struct nfs4_delegreturndata *)data;
4074
Trond Myklebust035168a2010-06-16 09:52:26 -04004075 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04004076 &d_data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004077 &d_data->res.seq_res, task))
Andy Adamson938e1012009-04-01 09:22:28 -04004078 return;
4079 rpc_call_start(task);
4080}
4081#endif /* CONFIG_NFS_V4_1 */
4082
Jesper Juhlc8d149f2006-03-20 13:44:07 -05004083static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04004084#if defined(CONFIG_NFS_V4_1)
4085 .rpc_call_prepare = nfs4_delegreturn_prepare,
4086#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01004087 .rpc_call_done = nfs4_delegreturn_done,
4088 .rpc_release = nfs4_delegreturn_release,
4089};
4090
Trond Myklebuste6f81072008-01-24 18:14:34 -05004091static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01004092{
4093 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01004094 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01004095 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004096 struct rpc_message msg = {
4097 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
4098 .rpc_cred = cred,
4099 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004100 struct rpc_task_setup task_setup_data = {
4101 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04004102 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004103 .callback_ops = &nfs4_delegreturn_ops,
4104 .flags = RPC_TASK_ASYNC,
4105 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05004106 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01004107
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004108 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01004109 if (data == NULL)
4110 return -ENOMEM;
Trond Myklebust9d12b212012-01-17 22:04:25 -05004111 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebustfe650402006-01-03 09:55:18 +01004112 data->args.fhandle = &data->fh;
4113 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04004114 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01004115 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05004116 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01004117 data->res.fattr = &data->fattr;
4118 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004119 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004120 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01004121 data->rpc_status = 0;
4122
Trond Myklebustc970aa82007-07-14 15:39:59 -04004123 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05004124 msg.rpc_argp = &data->args;
4125 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004126 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004127 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01004128 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004129 if (!issync)
4130 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01004131 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004132 if (status != 0)
4133 goto out;
4134 status = data->rpc_status;
Trond Myklebuste144cbc2012-04-28 16:05:03 -04004135 if (status == 0)
4136 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
4137 else
4138 nfs_refresh_inode(inode, &data->fattr);
Trond Myklebuste6f81072008-01-24 18:14:34 -05004139out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004140 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01004141 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142}
4143
Trond Myklebuste6f81072008-01-24 18:14:34 -05004144int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145{
4146 struct nfs_server *server = NFS_SERVER(inode);
4147 struct nfs4_exception exception = { };
4148 int err;
4149 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05004150 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 switch (err) {
4152 case -NFS4ERR_STALE_STATEID:
4153 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 case 0:
4155 return 0;
4156 }
4157 err = nfs4_handle_exception(server, err, &exception);
4158 } while (exception.retry);
4159 return err;
4160}
4161
4162#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
4163#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
4164
4165/*
4166 * sleep, with exponential backoff, and retry the LOCK operation.
4167 */
4168static unsigned long
4169nfs4_set_lock_task_retry(unsigned long timeout)
4170{
Jeff Laytond3103102011-12-01 22:44:39 +01004171 freezable_schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 timeout <<= 1;
4173 if (timeout > NFS4_LOCK_MAXTIMEOUT)
4174 return NFS4_LOCK_MAXTIMEOUT;
4175 return timeout;
4176}
4177
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4179{
4180 struct inode *inode = state->inode;
4181 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04004182 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004183 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004185 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004187 struct nfs_lockt_res res = {
4188 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 };
4190 struct rpc_message msg = {
4191 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
4192 .rpc_argp = &arg,
4193 .rpc_resp = &res,
4194 .rpc_cred = state->owner->so_cred,
4195 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 struct nfs4_lock_state *lsp;
4197 int status;
4198
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004199 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004200 status = nfs4_set_lock_state(state, request);
4201 if (status != 0)
4202 goto out;
4203 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004204 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004205 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004206 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004207 switch (status) {
4208 case 0:
4209 request->fl_type = F_UNLCK;
4210 break;
4211 case -NFS4ERR_DENIED:
4212 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004214 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004215out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 return status;
4217}
4218
4219static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4220{
4221 struct nfs4_exception exception = { };
4222 int err;
4223
4224 do {
4225 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4226 _nfs4_proc_getlk(state, cmd, request),
4227 &exception);
4228 } while (exception.retry);
4229 return err;
4230}
4231
4232static int do_vfs_lock(struct file *file, struct file_lock *fl)
4233{
4234 int res = 0;
4235 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4236 case FL_POSIX:
4237 res = posix_lock_file_wait(file, fl);
4238 break;
4239 case FL_FLOCK:
4240 res = flock_lock_file_wait(file, fl);
4241 break;
4242 default:
4243 BUG();
4244 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 return res;
4246}
4247
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004248struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004249 struct nfs_locku_args arg;
4250 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004251 struct nfs4_lock_state *lsp;
4252 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004253 struct file_lock fl;
4254 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004255 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004256};
4257
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004258static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4259 struct nfs_open_context *ctx,
4260 struct nfs4_lock_state *lsp,
4261 struct nfs_seqid *seqid)
4262{
4263 struct nfs4_unlockdata *p;
4264 struct inode *inode = lsp->ls_state->inode;
4265
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004266 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004267 if (p == NULL)
4268 return NULL;
4269 p->arg.fh = NFS_FH(inode);
4270 p->arg.fl = &p->fl;
4271 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004272 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004273 p->arg.stateid = &lsp->ls_stateid;
4274 p->lsp = lsp;
4275 atomic_inc(&lsp->ls_count);
4276 /* Ensure we don't close file until we're done freeing locks! */
4277 p->ctx = get_nfs_open_context(ctx);
4278 memcpy(&p->fl, fl, sizeof(p->fl));
4279 p->server = NFS_SERVER(inode);
4280 return p;
4281}
4282
Trond Myklebust06f814a2006-01-03 09:55:07 +01004283static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004284{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004285 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004286 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004287 nfs4_put_lock_state(calldata->lsp);
4288 put_nfs_open_context(calldata->ctx);
4289 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004290}
4291
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004292static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004293{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004294 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004295
Trond Myklebust14516c32010-07-31 14:29:06 -04004296 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4297 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004298 switch (task->tk_status) {
4299 case 0:
Trond Myklebustf597c532012-03-04 18:13:56 -05004300 nfs4_stateid_copy(&calldata->lsp->ls_stateid,
4301 &calldata->res.stateid);
Trond Myklebust26e976a2006-01-03 09:55:21 +01004302 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004303 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004304 case -NFS4ERR_BAD_STATEID:
4305 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004306 case -NFS4ERR_STALE_STATEID:
4307 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004308 break;
4309 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004310 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004311 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004312 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004313}
4314
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004315static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004316{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004317 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004319 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004320 return;
4321 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004322 /* Note: exit _without_ running nfs4_locku_done */
4323 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004324 return;
4325 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004326 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004327 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004328 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004329 &calldata->res.seq_res, task))
Andy Adamsona8936932009-04-01 09:22:23 -04004330 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004331 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332}
4333
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004334static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004335 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004336 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004337 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004338};
4339
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004340static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4341 struct nfs_open_context *ctx,
4342 struct nfs4_lock_state *lsp,
4343 struct nfs_seqid *seqid)
4344{
4345 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004346 struct rpc_message msg = {
4347 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4348 .rpc_cred = ctx->cred,
4349 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004350 struct rpc_task_setup task_setup_data = {
4351 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004352 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004353 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004354 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004355 .flags = RPC_TASK_ASYNC,
4356 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004357
Frank Filz137d6ac2007-07-09 15:32:29 -07004358 /* Ensure this is an unlock - when canceling a lock, the
4359 * canceled lock is passed in, and it won't be an unlock.
4360 */
4361 fl->fl_type = F_UNLCK;
4362
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004363 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4364 if (data == NULL) {
4365 nfs_free_seqid(seqid);
4366 return ERR_PTR(-ENOMEM);
4367 }
4368
Trond Myklebust9d12b212012-01-17 22:04:25 -05004369 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004370 msg.rpc_argp = &data->arg;
4371 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004372 task_setup_data.callback_data = data;
4373 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004374}
4375
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4377{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004378 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004379 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004380 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004381 struct rpc_task *task;
4382 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004383 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384
Trond Myklebust9b073572006-06-29 16:38:34 -04004385 status = nfs4_set_lock_state(state, request);
4386 /* Unlock _before_ we do the RPC call */
4387 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004388 down_read(&nfsi->rwsem);
4389 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4390 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004391 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004392 }
4393 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004394 if (status != 0)
4395 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004396 /* Is this a delegated lock? */
4397 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004398 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004399 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004400 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004401 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004402 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004403 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004404 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004405 status = PTR_ERR(task);
4406 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004407 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004408 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004409 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004410out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004411 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004412 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413}
4414
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004415struct nfs4_lockdata {
4416 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004417 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004418 struct nfs4_lock_state *lsp;
4419 struct nfs_open_context *ctx;
4420 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004421 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004422 int rpc_status;
4423 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004424 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004425};
4426
4427static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004428 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4429 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004430{
4431 struct nfs4_lockdata *p;
4432 struct inode *inode = lsp->ls_state->inode;
4433 struct nfs_server *server = NFS_SERVER(inode);
4434
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004435 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004436 if (p == NULL)
4437 return NULL;
4438
4439 p->arg.fh = NFS_FH(inode);
4440 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004441 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004442 if (p->arg.open_seqid == NULL)
4443 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004444 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004445 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004446 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004447 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004448 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05004449 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004450 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004451 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004452 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004453 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004454 atomic_inc(&lsp->ls_count);
4455 p->ctx = get_nfs_open_context(ctx);
4456 memcpy(&p->fl, fl, sizeof(p->fl));
4457 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004458out_free_seqid:
4459 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004460out_free:
4461 kfree(p);
4462 return NULL;
4463}
4464
4465static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4466{
4467 struct nfs4_lockdata *data = calldata;
4468 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469
Harvey Harrison3110ff82008-05-02 13:42:44 -07004470 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004471 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4472 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004473 /* Do we need to do an open_to_lock_owner? */
4474 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004475 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4476 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004477 data->arg.open_stateid = &state->stateid;
4478 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004479 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004480 } else
4481 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004482 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004483 if (nfs4_setup_sequence(data->server,
4484 &data->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05004485 &data->res.seq_res, task))
Andy Adamson66179ef2009-04-01 09:22:22 -04004486 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004487 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004488 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004489}
4490
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004491static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4492{
4493 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4494 nfs4_lock_prepare(task, calldata);
4495}
4496
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004497static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4498{
4499 struct nfs4_lockdata *data = calldata;
4500
Harvey Harrison3110ff82008-05-02 13:42:44 -07004501 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004502
Trond Myklebust14516c32010-07-31 14:29:06 -04004503 if (!nfs4_sequence_done(task, &data->res.seq_res))
4504 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004505
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004506 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004507 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004508 if (data->rpc_status == 0)
4509 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4510 else
4511 goto out;
4512 }
4513 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05004514 nfs4_stateid_copy(&data->lsp->ls_stateid, &data->res.stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004515 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Al Viro3d4ff432011-06-22 18:40:12 -04004516 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004517 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004518out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004519 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004520}
4521
4522static void nfs4_lock_release(void *calldata)
4523{
4524 struct nfs4_lockdata *data = calldata;
4525
Harvey Harrison3110ff82008-05-02 13:42:44 -07004526 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004527 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004528 if (data->cancelled != 0) {
4529 struct rpc_task *task;
4530 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4531 data->arg.lock_seqid);
4532 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004533 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004534 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004535 } else
4536 nfs_free_seqid(data->arg.lock_seqid);
4537 nfs4_put_lock_state(data->lsp);
4538 put_nfs_open_context(data->ctx);
4539 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004540 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004541}
4542
4543static const struct rpc_call_ops nfs4_lock_ops = {
4544 .rpc_call_prepare = nfs4_lock_prepare,
4545 .rpc_call_done = nfs4_lock_done,
4546 .rpc_release = nfs4_lock_release,
4547};
4548
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004549static const struct rpc_call_ops nfs4_recover_lock_ops = {
4550 .rpc_call_prepare = nfs4_recover_lock_prepare,
4551 .rpc_call_done = nfs4_lock_done,
4552 .rpc_release = nfs4_lock_release,
4553};
4554
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004555static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4556{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004557 switch (error) {
4558 case -NFS4ERR_ADMIN_REVOKED:
4559 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004560 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004561 if (new_lock_owner != 0 ||
4562 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004563 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004564 break;
4565 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004566 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004567 case -NFS4ERR_EXPIRED:
4568 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004569 };
4570}
4571
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004572static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004573{
4574 struct nfs4_lockdata *data;
4575 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004576 struct rpc_message msg = {
4577 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4578 .rpc_cred = state->owner->so_cred,
4579 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004580 struct rpc_task_setup task_setup_data = {
4581 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004582 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004583 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004584 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004585 .flags = RPC_TASK_ASYNC,
4586 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004587 int ret;
4588
Harvey Harrison3110ff82008-05-02 13:42:44 -07004589 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004590 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004591 fl->fl_u.nfs4_fl.owner,
4592 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004593 if (data == NULL)
4594 return -ENOMEM;
4595 if (IS_SETLKW(cmd))
4596 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004597 if (recovery_type > NFS_LOCK_NEW) {
4598 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004599 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004600 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4601 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05004602 nfs41_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05004603 msg.rpc_argp = &data->arg;
4604 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004605 task_setup_data.callback_data = data;
4606 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004607 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004608 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004609 ret = nfs4_wait_for_completion_rpc_task(task);
4610 if (ret == 0) {
4611 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004612 if (ret)
4613 nfs4_handle_setlk_error(data->server, data->lsp,
4614 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004615 } else
4616 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004617 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004618 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004619 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620}
4621
4622static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4623{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004624 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004625 struct nfs4_exception exception = {
4626 .inode = state->inode,
4627 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004628 int err;
4629
4630 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004631 /* Cache the lock if possible... */
4632 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4633 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004634 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004635 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004636 break;
4637 nfs4_handle_exception(server, err, &exception);
4638 } while (exception.retry);
4639 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640}
4641
4642static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4643{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004644 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04004645 struct nfs4_exception exception = {
4646 .inode = state->inode,
4647 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00004648 int err;
4649
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004650 err = nfs4_set_lock_state(state, request);
4651 if (err != 0)
4652 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004653 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004654 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4655 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004656 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004657 switch (err) {
4658 default:
4659 goto out;
4660 case -NFS4ERR_GRACE:
4661 case -NFS4ERR_DELAY:
4662 nfs4_handle_exception(server, err, &exception);
4663 err = 0;
4664 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004665 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004666out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004667 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668}
4669
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004670#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004671static int nfs41_check_expired_locks(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004672{
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004673 int status, ret = NFS_OK;
4674 struct nfs4_lock_state *lsp;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004675 struct nfs_server *server = NFS_SERVER(state->inode);
4676
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004677 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
4678 if (lsp->ls_flags & NFS_LOCK_INITIALIZED) {
4679 status = nfs41_test_stateid(server, &lsp->ls_stateid);
4680 if (status != NFS_OK) {
4681 nfs41_free_stateid(server, &lsp->ls_stateid);
4682 lsp->ls_flags &= ~NFS_LOCK_INITIALIZED;
4683 ret = status;
4684 }
4685 }
4686 };
4687
4688 return ret;
4689}
4690
4691static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4692{
4693 int status = NFS_OK;
4694
4695 if (test_bit(LK_STATE_IN_USE, &state->flags))
4696 status = nfs41_check_expired_locks(state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004697 if (status == NFS_OK)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05004698 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004699 return nfs4_lock_expired(state, request);
4700}
4701#endif
4702
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4704{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004705 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004706 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004707 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004709 if ((fl_flags & FL_POSIX) &&
4710 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4711 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004712 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004713 status = nfs4_set_lock_state(state, request);
4714 if (status != 0)
4715 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004716 request->fl_flags |= FL_ACCESS;
4717 status = do_vfs_lock(request->fl_file, request);
4718 if (status < 0)
4719 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004720 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004721 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004722 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004723 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004724 request->fl_flags = fl_flags & ~FL_SLEEP;
4725 status = do_vfs_lock(request->fl_file, request);
4726 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004727 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004728 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004729 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004730 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004731 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004732 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004733 if (do_vfs_lock(request->fl_file, request) < 0)
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004734 printk(KERN_WARNING "NFS: %s: VFS is out of sync with lock "
4735 "manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004736out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004737 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004738out:
4739 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 return status;
4741}
4742
4743static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4744{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004745 struct nfs4_exception exception = {
4746 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04004747 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004748 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 int err;
4750
4751 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004752 err = _nfs4_proc_setlk(state, cmd, request);
4753 if (err == -NFS4ERR_DENIED)
4754 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004756 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 } while (exception.retry);
4758 return err;
4759}
4760
4761static int
4762nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4763{
4764 struct nfs_open_context *ctx;
4765 struct nfs4_state *state;
4766 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4767 int status;
4768
4769 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004770 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 state = ctx->state;
4772
4773 if (request->fl_start < 0 || request->fl_end < 0)
4774 return -EINVAL;
4775
Trond Myklebustd9531262009-07-21 19:22:38 -04004776 if (IS_GETLK(cmd)) {
4777 if (state != NULL)
4778 return nfs4_proc_getlk(state, F_GETLK, request);
4779 return 0;
4780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781
4782 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4783 return -EINVAL;
4784
Trond Myklebustd9531262009-07-21 19:22:38 -04004785 if (request->fl_type == F_UNLCK) {
4786 if (state != NULL)
4787 return nfs4_proc_unlck(state, cmd, request);
4788 return 0;
4789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790
Trond Myklebustd9531262009-07-21 19:22:38 -04004791 if (state == NULL)
4792 return -ENOLCK;
Trond Myklebust55725512012-04-18 12:48:35 -04004793 /*
4794 * Don't rely on the VFS having checked the file open mode,
4795 * since it won't do this for flock() locks.
4796 */
4797 switch (request->fl_type & (F_RDLCK|F_WRLCK|F_UNLCK)) {
4798 case F_RDLCK:
4799 if (!(filp->f_mode & FMODE_READ))
4800 return -EBADF;
4801 break;
4802 case F_WRLCK:
4803 if (!(filp->f_mode & FMODE_WRITE))
4804 return -EBADF;
4805 }
4806
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 do {
4808 status = nfs4_proc_setlk(state, cmd, request);
4809 if ((status != -EAGAIN) || IS_SETLK(cmd))
4810 break;
4811 timeout = nfs4_set_lock_task_retry(timeout);
4812 status = -ERESTARTSYS;
4813 if (signalled())
4814 break;
4815 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816 return status;
4817}
4818
Trond Myklebust888e6942005-11-04 15:38:11 -05004819int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4820{
4821 struct nfs_server *server = NFS_SERVER(state->inode);
4822 struct nfs4_exception exception = { };
4823 int err;
4824
4825 err = nfs4_set_lock_state(state, fl);
4826 if (err != 0)
4827 goto out;
4828 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004829 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004830 switch (err) {
4831 default:
Weston Andros Adamsona0308892012-01-26 13:32:23 -05004832 printk(KERN_ERR "NFS: %s: unhandled error "
4833 "%d.\n", __func__, err);
Trond Myklebustd5122202009-06-17 13:22:58 -07004834 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004835 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004836 goto out;
4837 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004838 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004839 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004840 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004841 nfs4_schedule_lease_recovery(server->nfs_client);
4842 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004843 case -NFS4ERR_BADSESSION:
4844 case -NFS4ERR_BADSLOT:
4845 case -NFS4ERR_BAD_HIGH_SLOT:
4846 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4847 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004848 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004849 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004850 case -ERESTARTSYS:
4851 /*
4852 * The show must go on: exit, but mark the
4853 * stateid as needing recovery.
4854 */
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05004855 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004856 case -NFS4ERR_ADMIN_REVOKED:
4857 case -NFS4ERR_BAD_STATEID:
4858 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004859 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004860 err = 0;
4861 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004862 case -EKEYEXPIRED:
4863 /*
4864 * User RPCSEC_GSS context has expired.
4865 * We cannot recover this stateid now, so
4866 * skip it and allow recovery thread to
4867 * proceed.
4868 */
4869 err = 0;
4870 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004871 case -ENOMEM:
4872 case -NFS4ERR_DENIED:
4873 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4874 err = 0;
4875 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004876 case -NFS4ERR_DELAY:
4877 break;
4878 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004879 err = nfs4_handle_exception(server, err, &exception);
4880 } while (exception.retry);
4881out:
4882 return err;
4883}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004884
Trond Myklebustcf470c32012-03-07 13:49:12 -05004885struct nfs_release_lockowner_data {
4886 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004887 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004888 struct nfs_release_lockowner_args args;
4889};
4890
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004891static void nfs4_release_lockowner_release(void *calldata)
4892{
Trond Myklebustcf470c32012-03-07 13:49:12 -05004893 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004894 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004895 kfree(calldata);
4896}
4897
Trond Myklebust17280172012-03-11 13:11:00 -04004898static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004899 .rpc_release = nfs4_release_lockowner_release,
4900};
4901
Trond Myklebustcf470c32012-03-07 13:49:12 -05004902int nfs4_release_lockowner(struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004903{
4904 struct nfs_server *server = lsp->ls_state->owner->so_server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004905 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004906 struct rpc_message msg = {
4907 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4908 };
4909
4910 if (server->nfs_client->cl_mvops->minor_version != 0)
Trond Myklebustcf470c32012-03-07 13:49:12 -05004911 return -EINVAL;
4912 data = kmalloc(sizeof(*data), GFP_NOFS);
4913 if (!data)
4914 return -ENOMEM;
4915 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04004916 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05004917 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
4918 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
4919 data->args.lock_owner.s_dev = server->s_dev;
4920 msg.rpc_argp = &data->args;
4921 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
4922 return 0;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004923}
4924
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004925#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4926
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004927static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4928 const void *buf, size_t buflen,
4929 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004930{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004931 if (strcmp(key, "") != 0)
4932 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004933
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004934 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004935}
4936
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004937static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4938 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004939{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004940 if (strcmp(key, "") != 0)
4941 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004942
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004943 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004944}
4945
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004946static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4947 size_t list_len, const char *name,
4948 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004949{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004950 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004951
J. Bruce Fields096455a2006-03-20 23:23:42 -05004952 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4953 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004954
4955 if (list && len <= list_len)
4956 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004957 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004958}
4959
Andy Adamson533eb462011-06-13 18:25:56 -04004960/*
4961 * nfs_fhget will use either the mounted_on_fileid or the fileid
4962 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004963static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4964{
Andy Adamson533eb462011-06-13 18:25:56 -04004965 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4966 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4967 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05004968 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004969 return;
4970
4971 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05004972 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004973 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4974 fattr->nlink = 2;
4975}
4976
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004977static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
4978 const struct qstr *name,
4979 struct nfs4_fs_locations *fs_locations,
4980 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004981{
4982 struct nfs_server *server = NFS_SERVER(dir);
4983 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004984 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004985 };
4986 struct nfs4_fs_locations_arg args = {
4987 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004988 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004989 .page = page,
4990 .bitmask = bitmask,
4991 };
Benny Halevy22958462009-04-01 09:22:02 -04004992 struct nfs4_fs_locations_res res = {
4993 .fs_locations = fs_locations,
4994 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004995 struct rpc_message msg = {
4996 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4997 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004998 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004999 };
5000 int status;
5001
Harvey Harrison3110ff82008-05-02 13:42:44 -07005002 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04005003
5004 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
5005 * is not supported */
5006 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
5007 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
5008 else
5009 bitmask[0] |= FATTR4_WORD0_FILEID;
5010
Trond Myklebustc228fd32007-01-13 02:28:11 -05005011 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005012 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04005013 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005014 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07005015 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04005016 return status;
5017}
5018
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005019int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
5020 const struct qstr *name,
5021 struct nfs4_fs_locations *fs_locations,
5022 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005023{
5024 struct nfs4_exception exception = { };
5025 int err;
5026 do {
5027 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumakerf05d1472012-04-27 13:27:41 -04005028 _nfs4_proc_fs_locations(client, dir, name, fs_locations, page),
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04005029 &exception);
5030 } while (exception.retry);
5031 return err;
5032}
5033
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005034static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
5035{
5036 int status;
5037 struct nfs4_secinfo_arg args = {
5038 .dir_fh = NFS_FH(dir),
5039 .name = name,
5040 };
5041 struct nfs4_secinfo_res res = {
5042 .flavors = flavors,
5043 };
5044 struct rpc_message msg = {
5045 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
5046 .rpc_argp = &args,
5047 .rpc_resp = &res,
5048 };
5049
5050 dprintk("NFS call secinfo %s\n", name->name);
5051 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
5052 dprintk("NFS reply secinfo: %d\n", status);
5053 return status;
5054}
5055
Bryan Schumaker72de53e2012-04-27 13:27:40 -04005056int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
5057 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005058{
5059 struct nfs4_exception exception = { };
5060 int err;
5061 do {
5062 err = nfs4_handle_exception(NFS_SERVER(dir),
5063 _nfs4_proc_secinfo(dir, name, flavors),
5064 &exception);
5065 } while (exception.retry);
5066 return err;
5067}
5068
Andy Adamson557134a2009-04-01 09:21:53 -04005069#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04005070/*
Andy Adamson357f54d2010-12-14 10:11:57 -05005071 * Check the exchange flags returned by the server for invalid flags, having
5072 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
5073 * DS flags set.
5074 */
5075static int nfs4_check_cl_exchange_flags(u32 flags)
5076{
5077 if (flags & ~EXCHGID4_FLAG_MASK_R)
5078 goto out_inval;
5079 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
5080 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
5081 goto out_inval;
5082 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
5083 goto out_inval;
5084 return NFS_OK;
5085out_inval:
5086 return -NFS4ERR_INVAL;
5087}
5088
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005089static bool
5090nfs41_same_server_scope(struct server_scope *a, struct server_scope *b)
5091{
5092 if (a->server_scope_sz == b->server_scope_sz &&
5093 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
5094 return true;
5095
5096 return false;
5097}
5098
Andy Adamson357f54d2010-12-14 10:11:57 -05005099/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04005100 * nfs4_proc_exchange_id()
5101 *
5102 * Since the clientid has expired, all compounds using sessions
5103 * associated with the stale clientid will be returning
5104 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
5105 * be in some phase of session reset.
5106 */
Andy Adamson4d643d12009-12-04 15:52:24 -05005107int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04005108{
5109 nfs4_verifier verifier;
5110 struct nfs41_exchange_id_args args = {
Chuck Levercd937102012-03-02 17:14:31 -05005111 .verifier = &verifier,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005112 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05005113 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04005114 };
5115 struct nfs41_exchange_id_res res = {
5116 .client = clp,
5117 };
5118 int status;
5119 struct rpc_message msg = {
5120 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
5121 .rpc_argp = &args,
5122 .rpc_resp = &res,
5123 .rpc_cred = cred,
5124 };
Benny Halevy99fe60d2009-04-01 09:22:29 -04005125
5126 dprintk("--> %s\n", __func__);
5127 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04005128
Chuck Levercd937102012-03-02 17:14:31 -05005129 nfs4_construct_boot_verifier(clp, &verifier);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005130
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005131 args.id_len = scnprintf(args.id, sizeof(args.id),
5132 "%s/%s.%s/%u",
5133 clp->cl_ipaddr,
5134 init_utsname()->nodename,
5135 init_utsname()->domainname,
5136 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005137
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005138 res.server_scope = kzalloc(sizeof(struct server_scope), GFP_KERNEL);
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005139 if (unlikely(!res.server_scope)) {
5140 status = -ENOMEM;
5141 goto out;
5142 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005143
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005144 res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_KERNEL);
5145 if (unlikely(!res.impl_id)) {
5146 status = -ENOMEM;
5147 goto out_server_scope;
5148 }
5149
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005150 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonc7a360b2011-01-25 19:15:32 -05005151 if (!status)
5152 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005153
5154 if (!status) {
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005155 /* use the most recent implementation id */
5156 kfree(clp->impl_id);
5157 clp->impl_id = res.impl_id;
5158 } else
5159 kfree(res.impl_id);
5160
5161 if (!status) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005162 if (clp->server_scope &&
5163 !nfs41_same_server_scope(clp->server_scope,
5164 res.server_scope)) {
5165 dprintk("%s: server_scope mismatch detected\n",
5166 __func__);
5167 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
5168 kfree(clp->server_scope);
5169 clp->server_scope = NULL;
5170 }
5171
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005172 if (!clp->server_scope) {
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005173 clp->server_scope = res.server_scope;
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005174 goto out;
5175 }
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04005176 }
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005177
5178out_server_scope:
Weston Andros Adamsonabe9a6d2012-02-16 11:17:05 -05005179 kfree(res.server_scope);
5180out:
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05005181 if (clp->impl_id)
5182 dprintk("%s: Server Implementation ID: "
5183 "domain: %s, name: %s, date: %llu,%u\n",
5184 __func__, clp->impl_id->domain, clp->impl_id->name,
5185 clp->impl_id->date.seconds,
5186 clp->impl_id->date.nseconds);
Benny Halevy99fe60d2009-04-01 09:22:29 -04005187 dprintk("<-- %s status= %d\n", __func__, status);
5188 return status;
5189}
5190
Andy Adamson2050f0c2009-04-01 09:22:30 -04005191struct nfs4_get_lease_time_data {
5192 struct nfs4_get_lease_time_args *args;
5193 struct nfs4_get_lease_time_res *res;
5194 struct nfs_client *clp;
5195};
5196
5197static void nfs4_get_lease_time_prepare(struct rpc_task *task,
5198 void *calldata)
5199{
5200 int ret;
5201 struct nfs4_get_lease_time_data *data =
5202 (struct nfs4_get_lease_time_data *)calldata;
5203
5204 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005205 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005206 /* just setup sequence, do not trigger session recovery
5207 since we're invoked within one */
5208 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005209 &data->args->la_seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005210 &data->res->lr_seq_res, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005211
5212 BUG_ON(ret == -EAGAIN);
5213 rpc_call_start(task);
5214 dprintk("<-- %s\n", __func__);
5215}
5216
5217/*
5218 * Called from nfs4_state_manager thread for session setup, so don't recover
5219 * from sequence operation or clientid errors.
5220 */
5221static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
5222{
5223 struct nfs4_get_lease_time_data *data =
5224 (struct nfs4_get_lease_time_data *)calldata;
5225
5226 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005227 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
5228 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04005229 switch (task->tk_status) {
5230 case -NFS4ERR_DELAY:
5231 case -NFS4ERR_GRACE:
5232 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
5233 rpc_delay(task, NFS4_POLL_RETRY_MIN);
5234 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005235 /* fall through */
5236 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005237 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005238 return;
5239 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04005240 dprintk("<-- %s\n", __func__);
5241}
5242
Trond Myklebust17280172012-03-11 13:11:00 -04005243static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04005244 .rpc_call_prepare = nfs4_get_lease_time_prepare,
5245 .rpc_call_done = nfs4_get_lease_time_done,
5246};
5247
5248int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
5249{
5250 struct rpc_task *task;
5251 struct nfs4_get_lease_time_args args;
5252 struct nfs4_get_lease_time_res res = {
5253 .lr_fsinfo = fsinfo,
5254 };
5255 struct nfs4_get_lease_time_data data = {
5256 .args = &args,
5257 .res = &res,
5258 .clp = clp,
5259 };
5260 struct rpc_message msg = {
5261 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
5262 .rpc_argp = &args,
5263 .rpc_resp = &res,
5264 };
5265 struct rpc_task_setup task_setup = {
5266 .rpc_client = clp->cl_rpcclient,
5267 .rpc_message = &msg,
5268 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005269 .callback_data = &data,
5270 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04005271 };
5272 int status;
5273
Trond Myklebust9d12b212012-01-17 22:04:25 -05005274 nfs41_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Andy Adamson2050f0c2009-04-01 09:22:30 -04005275 dprintk("--> %s\n", __func__);
5276 task = rpc_run_task(&task_setup);
5277
5278 if (IS_ERR(task))
5279 status = PTR_ERR(task);
5280 else {
5281 status = task->tk_status;
5282 rpc_put_task(task);
5283 }
5284 dprintk("<-- %s return %d\n", __func__, status);
5285
5286 return status;
5287}
5288
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005289static struct nfs4_slot *nfs4_alloc_slots(u32 max_slots, gfp_t gfp_flags)
5290{
5291 return kcalloc(max_slots, sizeof(struct nfs4_slot), gfp_flags);
5292}
5293
5294static void nfs4_add_and_init_slots(struct nfs4_slot_table *tbl,
5295 struct nfs4_slot *new,
5296 u32 max_slots,
5297 u32 ivalue)
5298{
5299 struct nfs4_slot *old = NULL;
5300 u32 i;
5301
5302 spin_lock(&tbl->slot_tbl_lock);
5303 if (new) {
5304 old = tbl->slots;
5305 tbl->slots = new;
5306 tbl->max_slots = max_slots;
5307 }
5308 tbl->highest_used_slotid = -1; /* no slot is currently used */
5309 for (i = 0; i < tbl->max_slots; i++)
5310 tbl->slots[i].seq_nr = ivalue;
5311 spin_unlock(&tbl->slot_tbl_lock);
5312 kfree(old);
5313}
5314
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005315/*
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005316 * (re)Initialise a slot table
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005317 */
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005318static int nfs4_realloc_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5319 u32 ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005320{
Andy Adamson104aeba2010-01-14 17:45:10 -05005321 struct nfs4_slot *new = NULL;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005322 int ret = -ENOMEM;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005323
Andy Adamson104aeba2010-01-14 17:45:10 -05005324 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5325 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005326
Andy Adamson104aeba2010-01-14 17:45:10 -05005327 /* Does the newly negotiated max_reqs match the existing slot table? */
5328 if (max_reqs != tbl->max_slots) {
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005329 new = nfs4_alloc_slots(max_reqs, GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005330 if (!new)
5331 goto out;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005332 }
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005333 ret = 0;
5334
5335 nfs4_add_and_init_slots(tbl, new, max_reqs, ivalue);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005336 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5337 tbl, tbl->slots, tbl->max_slots);
5338out:
5339 dprintk("<-- %s: return %d\n", __func__, ret);
5340 return ret;
5341}
5342
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005343/* Destroy the slot table */
5344static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5345{
5346 if (session->fc_slot_table.slots != NULL) {
5347 kfree(session->fc_slot_table.slots);
5348 session->fc_slot_table.slots = NULL;
5349 }
5350 if (session->bc_slot_table.slots != NULL) {
5351 kfree(session->bc_slot_table.slots);
5352 session->bc_slot_table.slots = NULL;
5353 }
5354 return;
5355}
5356
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005357/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005358 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005359 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005360static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005361{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005362 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005363 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005364
Andy Adamsonaacd5532011-11-09 13:58:21 -05005365 dprintk("--> %s\n", __func__);
5366 /* Fore channel */
5367 tbl = &ses->fc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005368 status = nfs4_realloc_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5369 if (status) /* -ENOMEM */
5370 return status;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005371 /* Back channel */
5372 tbl = &ses->bc_slot_table;
Trond Myklebustf86f36a2012-02-14 20:33:19 -05005373 status = nfs4_realloc_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
5374 if (status && tbl->slots == NULL)
5375 /* Fore and back channel share a connection so get
5376 * both slot tables or neither */
5377 nfs4_destroy_slot_tables(ses);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005378 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005379}
5380
5381struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5382{
5383 struct nfs4_session *session;
5384 struct nfs4_slot_table *tbl;
5385
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005386 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005387 if (!session)
5388 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005389
Andy Adamson557134a2009-04-01 09:21:53 -04005390 tbl = &session->fc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005391 tbl->highest_used_slotid = NFS4_NO_SLOT;
Andy Adamson557134a2009-04-01 09:21:53 -04005392 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005393 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005394 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005395
5396 tbl = &session->bc_slot_table;
Trond Myklebust45d43c22012-02-06 19:38:51 -05005397 tbl->highest_used_slotid = NFS4_NO_SLOT;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005398 spin_lock_init(&tbl->slot_tbl_lock);
5399 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005400 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005401
Trond Myklebust1055d762010-06-16 09:52:27 -04005402 session->session_state = 1<<NFS4_SESSION_INITING;
5403
Andy Adamson557134a2009-04-01 09:21:53 -04005404 session->clp = clp;
5405 return session;
5406}
5407
5408void nfs4_destroy_session(struct nfs4_session *session)
5409{
Trond Myklebust2446ab62012-03-01 17:00:56 -05005410 struct rpc_xprt *xprt;
5411
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005412 nfs4_proc_destroy_session(session);
Trond Myklebust2446ab62012-03-01 17:00:56 -05005413
5414 rcu_read_lock();
5415 xprt = rcu_dereference(session->clp->cl_rpcclient->cl_xprt);
5416 rcu_read_unlock();
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005417 dprintk("%s Destroy backchannel for xprt %p\n",
Trond Myklebust2446ab62012-03-01 17:00:56 -05005418 __func__, xprt);
5419 xprt_destroy_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005420 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005421 kfree(session);
5422}
5423
Andy Adamsonfc931582009-04-01 09:22:31 -04005424/*
5425 * Initialize the values to be used by the client in CREATE_SESSION
5426 * If nfs4_init_session set the fore channel request and response sizes,
5427 * use them.
5428 *
5429 * Set the back channel max_resp_sz_cached to zero to force the client to
5430 * always set csa_cachethis to FALSE because the current implementation
5431 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5432 */
5433static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5434{
5435 struct nfs4_session *session = args->client->cl_session;
5436 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5437 mxresp_sz = session->fc_attrs.max_resp_sz;
5438
5439 if (mxrqst_sz == 0)
5440 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5441 if (mxresp_sz == 0)
5442 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5443 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005444 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5445 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005446 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05005447 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04005448
5449 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005450 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005451 __func__,
5452 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005453 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005454
5455 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005456 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5457 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5458 args->bc_attrs.max_resp_sz_cached = 0;
5459 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5460 args->bc_attrs.max_reqs = 1;
5461
5462 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5463 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5464 __func__,
5465 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5466 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5467 args->bc_attrs.max_reqs);
5468}
5469
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005470static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005471{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005472 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5473 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5474
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005475 if (rcvd->max_resp_sz > sent->max_resp_sz)
5476 return -EINVAL;
5477 /*
5478 * Our requested max_ops is the minimum we need; we're not
5479 * prepared to break up compounds into smaller pieces than that.
5480 * So, no point even trying to continue if the server won't
5481 * cooperate:
5482 */
5483 if (rcvd->max_ops < sent->max_ops)
5484 return -EINVAL;
5485 if (rcvd->max_reqs == 0)
5486 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005487 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
5488 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005489 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005490}
5491
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005492static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5493{
5494 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5495 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005496
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005497 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5498 return -EINVAL;
5499 if (rcvd->max_resp_sz < sent->max_resp_sz)
5500 return -EINVAL;
5501 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5502 return -EINVAL;
5503 /* These would render the backchannel useless: */
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005504 if (rcvd->max_ops != sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005505 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04005506 if (rcvd->max_reqs != sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005507 return -EINVAL;
5508 return 0;
5509}
Andy Adamson8d353012009-04-01 09:22:32 -04005510
Andy Adamson8d353012009-04-01 09:22:32 -04005511static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5512 struct nfs4_session *session)
5513{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005514 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005515
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005516 ret = nfs4_verify_fore_channel_attrs(args, session);
5517 if (ret)
5518 return ret;
5519 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005520}
5521
Andy Adamsonfc931582009-04-01 09:22:31 -04005522static int _nfs4_proc_create_session(struct nfs_client *clp)
5523{
5524 struct nfs4_session *session = clp->cl_session;
5525 struct nfs41_create_session_args args = {
5526 .client = clp,
5527 .cb_program = NFS4_CALLBACK,
5528 };
5529 struct nfs41_create_session_res res = {
5530 .client = clp,
5531 };
5532 struct rpc_message msg = {
5533 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5534 .rpc_argp = &args,
5535 .rpc_resp = &res,
5536 };
5537 int status;
5538
5539 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005540 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005541
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005542 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005543
Andy Adamson8d353012009-04-01 09:22:32 -04005544 if (!status)
5545 /* Verify the session's negotiated channel_attrs values */
5546 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005547 if (!status) {
5548 /* Increment the clientid slot sequence id */
5549 clp->cl_seqid++;
5550 }
5551
5552 return status;
5553}
5554
5555/*
5556 * Issues a CREATE_SESSION operation to the server.
5557 * It is the responsibility of the caller to verify the session is
5558 * expired before calling this routine.
5559 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005560int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005561{
5562 int status;
5563 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005564 struct nfs4_session *session = clp->cl_session;
5565
5566 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5567
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005568 status = _nfs4_proc_create_session(clp);
Andy Adamsonfc931582009-04-01 09:22:31 -04005569 if (status)
5570 goto out;
5571
Andy Adamsonaacd5532011-11-09 13:58:21 -05005572 /* Init or reset the session slot tables */
5573 status = nfs4_setup_session_slot_tables(session);
5574 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005575 if (status)
5576 goto out;
5577
5578 ptr = (unsigned *)&session->sess_id.data[0];
5579 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5580 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005581out:
5582 dprintk("<-- %s\n", __func__);
5583 return status;
5584}
5585
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005586/*
5587 * Issue the over-the-wire RPC DESTROY_SESSION.
5588 * The caller must serialize access to this routine.
5589 */
5590int nfs4_proc_destroy_session(struct nfs4_session *session)
5591{
5592 int status = 0;
5593 struct rpc_message msg;
5594
5595 dprintk("--> nfs4_proc_destroy_session\n");
5596
5597 /* session is still being setup */
5598 if (session->clp->cl_cons_state != NFS_CS_READY)
5599 return status;
5600
5601 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5602 msg.rpc_argp = session;
5603 msg.rpc_resp = NULL;
5604 msg.rpc_cred = NULL;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005605 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005606
5607 if (status)
5608 printk(KERN_WARNING
Weston Andros Adamsonf9fd2d92012-01-26 13:32:22 -05005609 "NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005610 "Session has been destroyed regardless...\n", status);
5611
5612 dprintk("<-- nfs4_proc_destroy_session\n");
5613 return status;
5614}
5615
Trond Myklebustfccba802009-07-21 16:48:07 -04005616int nfs4_init_session(struct nfs_server *server)
5617{
5618 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005619 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005620 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005621 int ret;
5622
5623 if (!nfs4_has_session(clp))
5624 return 0;
5625
Trond Myklebust1055d762010-06-16 09:52:27 -04005626 session = clp->cl_session;
5627 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5628 return 0;
5629
Andy Adamson68bf05e2009-12-15 12:55:02 -05005630 rsize = server->rsize;
5631 if (rsize == 0)
5632 rsize = NFS_MAX_FILE_IO_SIZE;
5633 wsize = server->wsize;
5634 if (wsize == 0)
5635 wsize = NFS_MAX_FILE_IO_SIZE;
5636
Andy Adamson68bf05e2009-12-15 12:55:02 -05005637 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5638 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005639
Trond Myklebustfccba802009-07-21 16:48:07 -04005640 ret = nfs4_recover_expired_lease(server);
5641 if (!ret)
5642 ret = nfs4_check_client_ready(clp);
5643 return ret;
5644}
5645
Andy Adamsond83217c2011-03-01 01:34:17 +00005646int nfs4_init_ds_session(struct nfs_client *clp)
5647{
5648 struct nfs4_session *session = clp->cl_session;
5649 int ret;
5650
5651 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5652 return 0;
5653
5654 ret = nfs4_client_recover_expired_lease(clp);
5655 if (!ret)
5656 /* Test for the DS role */
5657 if (!is_ds_client(clp))
5658 ret = -ENODEV;
5659 if (!ret)
5660 ret = nfs4_check_client_ready(clp);
5661 return ret;
5662
5663}
5664EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5665
5666
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005667/*
5668 * Renew the cl_session lease.
5669 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005670struct nfs4_sequence_data {
5671 struct nfs_client *clp;
5672 struct nfs4_sequence_args args;
5673 struct nfs4_sequence_res res;
5674};
5675
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005676static void nfs41_sequence_release(void *data)
5677{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005678 struct nfs4_sequence_data *calldata = data;
5679 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005680
Alexandros Batsakis71358402010-02-05 03:45:05 -08005681 if (atomic_read(&clp->cl_count) > 1)
5682 nfs4_schedule_state_renewal(clp);
5683 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005684 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005685}
5686
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005687static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5688{
5689 switch(task->tk_status) {
5690 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005691 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5692 return -EAGAIN;
5693 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005694 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005695 }
5696 return 0;
5697}
5698
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005699static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005700{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005701 struct nfs4_sequence_data *calldata = data;
5702 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005703
Trond Myklebust14516c32010-07-31 14:29:06 -04005704 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5705 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005706
5707 if (task->tk_status < 0) {
5708 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005709 if (atomic_read(&clp->cl_count) == 1)
5710 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005711
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005712 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5713 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005714 return;
5715 }
5716 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005717 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005718out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005719 dprintk("<-- %s\n", __func__);
5720}
5721
5722static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5723{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005724 struct nfs4_sequence_data *calldata = data;
5725 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005726 struct nfs4_sequence_args *args;
5727 struct nfs4_sequence_res *res;
5728
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005729 args = task->tk_msg.rpc_argp;
5730 res = task->tk_msg.rpc_resp;
5731
Trond Myklebust9d12b212012-01-17 22:04:25 -05005732 if (nfs41_setup_sequence(clp->cl_session, args, res, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005733 return;
5734 rpc_call_start(task);
5735}
5736
5737static const struct rpc_call_ops nfs41_sequence_ops = {
5738 .rpc_call_done = nfs41_sequence_call_done,
5739 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005740 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005741};
5742
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005743static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005744{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005745 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005746 struct rpc_message msg = {
5747 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5748 .rpc_cred = cred,
5749 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005750 struct rpc_task_setup task_setup_data = {
5751 .rpc_client = clp->cl_rpcclient,
5752 .rpc_message = &msg,
5753 .callback_ops = &nfs41_sequence_ops,
5754 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5755 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005756
Alexandros Batsakis71358402010-02-05 03:45:05 -08005757 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005758 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005759 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005760 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005761 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005762 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005763 }
Trond Myklebust9d12b212012-01-17 22:04:25 -05005764 nfs41_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005765 msg.rpc_argp = &calldata->args;
5766 msg.rpc_resp = &calldata->res;
5767 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005768 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005769
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005770 return rpc_run_task(&task_setup_data);
5771}
5772
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005773static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005774{
5775 struct rpc_task *task;
5776 int ret = 0;
5777
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005778 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5779 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005780 task = _nfs41_proc_sequence(clp, cred);
5781 if (IS_ERR(task))
5782 ret = PTR_ERR(task);
5783 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005784 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005785 dprintk("<-- %s status=%d\n", __func__, ret);
5786 return ret;
5787}
5788
5789static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5790{
5791 struct rpc_task *task;
5792 int ret;
5793
5794 task = _nfs41_proc_sequence(clp, cred);
5795 if (IS_ERR(task)) {
5796 ret = PTR_ERR(task);
5797 goto out;
5798 }
5799 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005800 if (!ret) {
5801 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5802
5803 if (task->tk_status == 0)
5804 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005805 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005806 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005807 rpc_put_task(task);
5808out:
5809 dprintk("<-- %s status=%d\n", __func__, ret);
5810 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005811}
5812
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005813struct nfs4_reclaim_complete_data {
5814 struct nfs_client *clp;
5815 struct nfs41_reclaim_complete_args arg;
5816 struct nfs41_reclaim_complete_res res;
5817};
5818
5819static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5820{
5821 struct nfs4_reclaim_complete_data *calldata = data;
5822
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005823 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005824 if (nfs41_setup_sequence(calldata->clp->cl_session,
5825 &calldata->arg.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005826 &calldata->res.seq_res, task))
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005827 return;
5828
5829 rpc_call_start(task);
5830}
5831
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005832static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5833{
5834 switch(task->tk_status) {
5835 case 0:
5836 case -NFS4ERR_COMPLETE_ALREADY:
5837 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5838 break;
5839 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005840 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005841 /* fall through */
5842 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005843 return -EAGAIN;
5844 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005845 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005846 }
5847 return 0;
5848}
5849
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005850static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5851{
5852 struct nfs4_reclaim_complete_data *calldata = data;
5853 struct nfs_client *clp = calldata->clp;
5854 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5855
5856 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005857 if (!nfs41_sequence_done(task, res))
5858 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005859
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005860 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5861 rpc_restart_call_prepare(task);
5862 return;
5863 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005864 dprintk("<-- %s\n", __func__);
5865}
5866
5867static void nfs4_free_reclaim_complete_data(void *data)
5868{
5869 struct nfs4_reclaim_complete_data *calldata = data;
5870
5871 kfree(calldata);
5872}
5873
5874static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5875 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5876 .rpc_call_done = nfs4_reclaim_complete_done,
5877 .rpc_release = nfs4_free_reclaim_complete_data,
5878};
5879
5880/*
5881 * Issue a global reclaim complete.
5882 */
5883static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5884{
5885 struct nfs4_reclaim_complete_data *calldata;
5886 struct rpc_task *task;
5887 struct rpc_message msg = {
5888 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5889 };
5890 struct rpc_task_setup task_setup_data = {
5891 .rpc_client = clp->cl_rpcclient,
5892 .rpc_message = &msg,
5893 .callback_ops = &nfs4_reclaim_complete_call_ops,
5894 .flags = RPC_TASK_ASYNC,
5895 };
5896 int status = -ENOMEM;
5897
5898 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005899 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005900 if (calldata == NULL)
5901 goto out;
5902 calldata->clp = clp;
5903 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005904
Trond Myklebust9d12b212012-01-17 22:04:25 -05005905 nfs41_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005906 msg.rpc_argp = &calldata->arg;
5907 msg.rpc_resp = &calldata->res;
5908 task_setup_data.callback_data = calldata;
5909 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005910 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005911 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005912 goto out;
5913 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005914 status = nfs4_wait_for_completion_rpc_task(task);
5915 if (status == 0)
5916 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005917 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005918 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005919out:
5920 dprintk("<-- %s status=%d\n", __func__, status);
5921 return status;
5922}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005923
5924static void
5925nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5926{
5927 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005928 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005929
5930 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005931 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5932 * right now covering the LAYOUTGET we are about to send.
5933 * However, that is not so catastrophic, and there seems
5934 * to be no way to prevent it completely.
5935 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005936 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05005937 &lgp->res.seq_res, task))
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005938 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005939 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5940 NFS_I(lgp->args.inode)->layout,
5941 lgp->args.ctx->state)) {
5942 rpc_exit(task, NFS4_OK);
5943 return;
5944 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005945 rpc_call_start(task);
5946}
5947
5948static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5949{
5950 struct nfs4_layoutget *lgp = calldata;
5951 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5952
5953 dprintk("--> %s\n", __func__);
5954
5955 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5956 return;
5957
5958 switch (task->tk_status) {
5959 case 0:
5960 break;
5961 case -NFS4ERR_LAYOUTTRYLATER:
5962 case -NFS4ERR_RECALLCONFLICT:
5963 task->tk_status = -NFS4ERR_DELAY;
5964 /* Fall through */
5965 default:
5966 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5967 rpc_restart_call_prepare(task);
5968 return;
5969 }
5970 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005971 dprintk("<-- %s\n", __func__);
5972}
5973
5974static void nfs4_layoutget_release(void *calldata)
5975{
5976 struct nfs4_layoutget *lgp = calldata;
5977
5978 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005979 put_nfs_open_context(lgp->args.ctx);
5980 kfree(calldata);
5981 dprintk("<-- %s\n", __func__);
5982}
5983
5984static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5985 .rpc_call_prepare = nfs4_layoutget_prepare,
5986 .rpc_call_done = nfs4_layoutget_done,
5987 .rpc_release = nfs4_layoutget_release,
5988};
5989
5990int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5991{
5992 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5993 struct rpc_task *task;
5994 struct rpc_message msg = {
5995 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5996 .rpc_argp = &lgp->args,
5997 .rpc_resp = &lgp->res,
5998 };
5999 struct rpc_task_setup task_setup_data = {
6000 .rpc_client = server->client,
6001 .rpc_message = &msg,
6002 .callback_ops = &nfs4_layoutget_call_ops,
6003 .callback_data = lgp,
6004 .flags = RPC_TASK_ASYNC,
6005 };
6006 int status = 0;
6007
6008 dprintk("--> %s\n", __func__);
6009
Weston Andros Adamson35124a02011-03-24 16:48:21 -04006010 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006011 lgp->res.seq_res.sr_slot = NULL;
Trond Myklebust9d12b212012-01-17 22:04:25 -05006012 nfs41_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006013 task = rpc_run_task(&task_setup_data);
6014 if (IS_ERR(task))
6015 return PTR_ERR(task);
6016 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00006017 if (status == 0)
6018 status = task->tk_status;
6019 if (status == 0)
6020 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006021 rpc_put_task(task);
6022 dprintk("<-- %s status=%d\n", __func__, status);
6023 return status;
6024}
6025
Benny Halevycbe82602011-05-22 19:52:37 +03006026static void
6027nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
6028{
6029 struct nfs4_layoutreturn *lrp = calldata;
6030
6031 dprintk("--> %s\n", __func__);
6032 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006033 &lrp->res.seq_res, task))
Benny Halevycbe82602011-05-22 19:52:37 +03006034 return;
6035 rpc_call_start(task);
6036}
6037
6038static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
6039{
6040 struct nfs4_layoutreturn *lrp = calldata;
6041 struct nfs_server *server;
Trond Myklebusta56aaa02011-06-15 11:59:10 -04006042 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03006043
6044 dprintk("--> %s\n", __func__);
6045
6046 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
6047 return;
6048
6049 server = NFS_SERVER(lrp->args.inode);
6050 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006051 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03006052 return;
6053 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04006054 spin_lock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03006055 if (task->tk_status == 0) {
Benny Halevycbe82602011-05-22 19:52:37 +03006056 if (lrp->res.lrs_present) {
Benny Halevycbe82602011-05-22 19:52:37 +03006057 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Benny Halevycbe82602011-05-22 19:52:37 +03006058 } else
6059 BUG_ON(!list_empty(&lo->plh_segs));
6060 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04006061 lo->plh_block_lgets--;
6062 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03006063 dprintk("<-- %s\n", __func__);
6064}
6065
6066static void nfs4_layoutreturn_release(void *calldata)
6067{
6068 struct nfs4_layoutreturn *lrp = calldata;
6069
6070 dprintk("--> %s\n", __func__);
Trond Myklebusta56aaa02011-06-15 11:59:10 -04006071 put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03006072 kfree(calldata);
6073 dprintk("<-- %s\n", __func__);
6074}
6075
6076static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
6077 .rpc_call_prepare = nfs4_layoutreturn_prepare,
6078 .rpc_call_done = nfs4_layoutreturn_done,
6079 .rpc_release = nfs4_layoutreturn_release,
6080};
6081
6082int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
6083{
6084 struct rpc_task *task;
6085 struct rpc_message msg = {
6086 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
6087 .rpc_argp = &lrp->args,
6088 .rpc_resp = &lrp->res,
6089 };
6090 struct rpc_task_setup task_setup_data = {
6091 .rpc_client = lrp->clp->cl_rpcclient,
6092 .rpc_message = &msg,
6093 .callback_ops = &nfs4_layoutreturn_call_ops,
6094 .callback_data = lrp,
6095 };
6096 int status;
6097
6098 dprintk("--> %s\n", __func__);
Trond Myklebust9d12b212012-01-17 22:04:25 -05006099 nfs41_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03006100 task = rpc_run_task(&task_setup_data);
6101 if (IS_ERR(task))
6102 return PTR_ERR(task);
6103 status = task->tk_status;
6104 dprintk("<-- %s status=%d\n", __func__, status);
6105 rpc_put_task(task);
6106 return status;
6107}
6108
Andy Adamson7f11d8d2011-07-30 20:52:35 -04006109/*
6110 * Retrieve the list of Data Server devices from the MDS.
6111 */
6112static int _nfs4_getdevicelist(struct nfs_server *server,
6113 const struct nfs_fh *fh,
6114 struct pnfs_devicelist *devlist)
6115{
6116 struct nfs4_getdevicelist_args args = {
6117 .fh = fh,
6118 .layoutclass = server->pnfs_curr_ld->id,
6119 };
6120 struct nfs4_getdevicelist_res res = {
6121 .devlist = devlist,
6122 };
6123 struct rpc_message msg = {
6124 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
6125 .rpc_argp = &args,
6126 .rpc_resp = &res,
6127 };
6128 int status;
6129
6130 dprintk("--> %s\n", __func__);
6131 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
6132 &res.seq_res, 0);
6133 dprintk("<-- %s status=%d\n", __func__, status);
6134 return status;
6135}
6136
6137int nfs4_proc_getdevicelist(struct nfs_server *server,
6138 const struct nfs_fh *fh,
6139 struct pnfs_devicelist *devlist)
6140{
6141 struct nfs4_exception exception = { };
6142 int err;
6143
6144 do {
6145 err = nfs4_handle_exception(server,
6146 _nfs4_getdevicelist(server, fh, devlist),
6147 &exception);
6148 } while (exception.retry);
6149
6150 dprintk("%s: err=%d, num_devs=%u\n", __func__,
6151 err, devlist->num_devs);
6152
6153 return err;
6154}
6155EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
6156
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006157static int
6158_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6159{
6160 struct nfs4_getdeviceinfo_args args = {
6161 .pdev = pdev,
6162 };
6163 struct nfs4_getdeviceinfo_res res = {
6164 .pdev = pdev,
6165 };
6166 struct rpc_message msg = {
6167 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
6168 .rpc_argp = &args,
6169 .rpc_resp = &res,
6170 };
6171 int status;
6172
6173 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00006174 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04006175 dprintk("<-- %s status=%d\n", __func__, status);
6176
6177 return status;
6178}
6179
6180int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
6181{
6182 struct nfs4_exception exception = { };
6183 int err;
6184
6185 do {
6186 err = nfs4_handle_exception(server,
6187 _nfs4_proc_getdeviceinfo(server, pdev),
6188 &exception);
6189 } while (exception.retry);
6190 return err;
6191}
6192EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
6193
Andy Adamson863a3c62011-03-23 13:27:54 +00006194static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
6195{
6196 struct nfs4_layoutcommit_data *data = calldata;
6197 struct nfs_server *server = NFS_SERVER(data->args.inode);
6198
6199 if (nfs4_setup_sequence(server, &data->args.seq_args,
Trond Myklebust9d12b212012-01-17 22:04:25 -05006200 &data->res.seq_res, task))
Andy Adamson863a3c62011-03-23 13:27:54 +00006201 return;
6202 rpc_call_start(task);
6203}
6204
6205static void
6206nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
6207{
6208 struct nfs4_layoutcommit_data *data = calldata;
6209 struct nfs_server *server = NFS_SERVER(data->args.inode);
6210
6211 if (!nfs4_sequence_done(task, &data->res.seq_res))
6212 return;
6213
6214 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006215 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
6216 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
6217 case -NFS4ERR_BADLAYOUT: /* no layout */
6218 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00006219 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006220 break;
6221 case 0:
Andy Adamson863a3c62011-03-23 13:27:54 +00006222 nfs_post_op_update_inode_force_wcc(data->args.inode,
6223 data->res.fattr);
Trond Myklebuste59d27e2012-03-27 18:22:19 -04006224 break;
6225 default:
6226 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
6227 rpc_restart_call_prepare(task);
6228 return;
6229 }
6230 }
Andy Adamson863a3c62011-03-23 13:27:54 +00006231}
6232
6233static void nfs4_layoutcommit_release(void *calldata)
6234{
6235 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04006236 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07006237 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00006238
Andy Adamsondb29c082011-07-30 20:52:38 -04006239 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00006240 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04006241 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
6242 list_del_init(&lseg->pls_lc_list);
6243 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
6244 &lseg->pls_flags))
6245 put_lseg(lseg);
6246 }
Peng Tao92407e72011-10-23 20:21:17 -07006247
6248 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
6249 smp_mb__after_clear_bit();
6250 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
6251
Andy Adamson863a3c62011-03-23 13:27:54 +00006252 put_rpccred(data->cred);
6253 kfree(data);
6254}
6255
6256static const struct rpc_call_ops nfs4_layoutcommit_ops = {
6257 .rpc_call_prepare = nfs4_layoutcommit_prepare,
6258 .rpc_call_done = nfs4_layoutcommit_done,
6259 .rpc_release = nfs4_layoutcommit_release,
6260};
6261
6262int
Andy Adamsonef311532011-03-12 02:58:10 -05006263nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00006264{
6265 struct rpc_message msg = {
6266 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
6267 .rpc_argp = &data->args,
6268 .rpc_resp = &data->res,
6269 .rpc_cred = data->cred,
6270 };
6271 struct rpc_task_setup task_setup_data = {
6272 .task = &data->task,
6273 .rpc_client = NFS_CLIENT(data->args.inode),
6274 .rpc_message = &msg,
6275 .callback_ops = &nfs4_layoutcommit_ops,
6276 .callback_data = data,
6277 .flags = RPC_TASK_ASYNC,
6278 };
6279 struct rpc_task *task;
6280 int status = 0;
6281
6282 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6283 "lbw: %llu inode %lu\n",
6284 data->task.tk_pid, sync,
6285 data->args.lastbytewritten,
6286 data->args.inode->i_ino);
6287
Trond Myklebust9d12b212012-01-17 22:04:25 -05006288 nfs41_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00006289 task = rpc_run_task(&task_setup_data);
6290 if (IS_ERR(task))
6291 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006292 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006293 goto out;
6294 status = nfs4_wait_for_completion_rpc_task(task);
6295 if (status != 0)
6296 goto out;
6297 status = task->tk_status;
6298out:
6299 dprintk("%s: status %d\n", __func__, status);
6300 rpc_put_task(task);
6301 return status;
6302}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006303
6304static int
6305_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6306 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6307{
6308 struct nfs41_secinfo_no_name_args args = {
6309 .style = SECINFO_STYLE_CURRENT_FH,
6310 };
6311 struct nfs4_secinfo_res res = {
6312 .flavors = flavors,
6313 };
6314 struct rpc_message msg = {
6315 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6316 .rpc_argp = &args,
6317 .rpc_resp = &res,
6318 };
6319 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6320}
6321
6322static int
6323nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6324 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6325{
6326 struct nfs4_exception exception = { };
6327 int err;
6328 do {
6329 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6330 switch (err) {
6331 case 0:
6332 case -NFS4ERR_WRONGSEC:
6333 case -NFS4ERR_NOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006334 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006335 default:
6336 err = nfs4_handle_exception(server, err, &exception);
6337 }
6338 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04006339out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006340 return err;
6341}
6342
6343static int
6344nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6345 struct nfs_fsinfo *info)
6346{
6347 int err;
6348 struct page *page;
6349 rpc_authflavor_t flavor;
6350 struct nfs4_secinfo_flavors *flavors;
6351
6352 page = alloc_page(GFP_KERNEL);
6353 if (!page) {
6354 err = -ENOMEM;
6355 goto out;
6356 }
6357
6358 flavors = page_address(page);
6359 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6360
6361 /*
6362 * Fall back on "guess and check" method if
6363 * the server doesn't support SECINFO_NO_NAME
6364 */
6365 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6366 err = nfs4_find_root_sec(server, fhandle, info);
6367 goto out_freepage;
6368 }
6369 if (err)
6370 goto out_freepage;
6371
6372 flavor = nfs_find_best_sec(flavors);
6373 if (err == 0)
6374 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6375
6376out_freepage:
6377 put_page(page);
6378 if (err == -EACCES)
6379 return -EPERM;
6380out:
6381 return err;
6382}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006383
6384static int _nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006385{
6386 int status;
6387 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006388 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04006389 };
6390 struct nfs41_test_stateid_res res;
6391 struct rpc_message msg = {
6392 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6393 .rpc_argp = &args,
6394 .rpc_resp = &res,
6395 };
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006396
Trond Myklebust9d12b212012-01-17 22:04:25 -05006397 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006398 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
6399
6400 if (status == NFS_OK)
6401 return res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04006402 return status;
6403}
6404
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006405static int nfs41_test_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker7d974792011-06-02 14:59:08 -04006406{
6407 struct nfs4_exception exception = { };
6408 int err;
6409 do {
6410 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006411 _nfs41_test_stateid(server, stateid),
Bryan Schumaker7d974792011-06-02 14:59:08 -04006412 &exception);
6413 } while (exception.retry);
6414 return err;
6415}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006416
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006417static int _nfs4_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006418{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006419 struct nfs41_free_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006420 .stateid = stateid,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006421 };
6422 struct nfs41_free_stateid_res res;
6423 struct rpc_message msg = {
6424 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6425 .rpc_argp = &args,
6426 .rpc_resp = &res,
6427 };
6428
Trond Myklebust9d12b212012-01-17 22:04:25 -05006429 nfs41_init_sequence(&args.seq_args, &res.seq_res, 0);
6430 return nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006431}
6432
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006433static int nfs41_free_stateid(struct nfs_server *server, nfs4_stateid *stateid)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006434{
6435 struct nfs4_exception exception = { };
6436 int err;
6437 do {
6438 err = nfs4_handle_exception(server,
Bryan Schumaker1cab0652012-01-31 10:39:29 -05006439 _nfs4_free_stateid(server, stateid),
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006440 &exception);
6441 } while (exception.retry);
6442 return err;
6443}
Trond Myklebust36281ca2012-03-04 18:13:56 -05006444
6445static bool nfs41_match_stateid(const nfs4_stateid *s1,
6446 const nfs4_stateid *s2)
6447{
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006448 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006449 return false;
6450
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006451 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006452 return true;
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05006453 if (s1->seqid == 0 || s2->seqid == 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05006454 return true;
6455
6456 return false;
6457}
6458
Andy Adamson557134a2009-04-01 09:21:53 -04006459#endif /* CONFIG_NFS_V4_1 */
6460
Trond Myklebust36281ca2012-03-04 18:13:56 -05006461static bool nfs4_match_stateid(const nfs4_stateid *s1,
6462 const nfs4_stateid *s2)
6463{
Trond Myklebustf597c532012-03-04 18:13:56 -05006464 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05006465}
6466
6467
Trond Myklebust17280172012-03-11 13:11:00 -04006468static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006469 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006470 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006471 .recover_open = nfs4_open_reclaim,
6472 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006473 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006474 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006475};
6476
Andy Adamson591d71c2009-04-01 09:22:47 -04006477#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006478static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006479 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6480 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6481 .recover_open = nfs4_open_reclaim,
6482 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006483 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006484 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006485 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006486};
6487#endif /* CONFIG_NFS_V4_1 */
6488
Trond Myklebust17280172012-03-11 13:11:00 -04006489static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006490 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006491 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006492 .recover_open = nfs4_open_expired,
6493 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006494 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006495 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006496};
6497
Andy Adamson591d71c2009-04-01 09:22:47 -04006498#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006499static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04006500 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6501 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006502 .recover_open = nfs41_open_expired,
6503 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006504 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006505 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006506};
6507#endif /* CONFIG_NFS_V4_1 */
6508
Trond Myklebust17280172012-03-11 13:11:00 -04006509static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006510 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006511 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006512 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006513};
6514
6515#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04006516static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04006517 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006518 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006519 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006520};
6521#endif
6522
Trond Myklebust97dc1352010-06-16 09:52:26 -04006523static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6524 .minor_version = 0,
6525 .call_sync = _nfs4_call_sync,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006526 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006527 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006528 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6529 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6530 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006531};
6532
6533#if defined(CONFIG_NFS_V4_1)
6534static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6535 .minor_version = 1,
6536 .call_sync = _nfs4_call_sync_session,
Trond Myklebust36281ca2012-03-04 18:13:56 -05006537 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006538 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006539 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6540 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6541 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006542};
6543#endif
6544
Trond Myklebust97dc1352010-06-16 09:52:26 -04006545const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6546 [0] = &nfs_v4_0_minor_ops,
6547#if defined(CONFIG_NFS_V4_1)
6548 [1] = &nfs_v4_1_minor_ops,
6549#endif
6550};
6551
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006552static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006553 .permission = nfs_permission,
6554 .getattr = nfs_getattr,
6555 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006556 .getxattr = generic_getxattr,
6557 .setxattr = generic_setxattr,
6558 .listxattr = generic_listxattr,
6559 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006560};
6561
David Howells509de812006-08-22 20:06:11 -04006562const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006563 .version = 4, /* protocol version */
6564 .dentry_ops = &nfs4_dentry_operations,
6565 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006566 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006567 .file_ops = &nfs4_file_operations,
Bryan Schumakerbae36242012-05-10 15:07:31 -04006568 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04006569 .submount = nfs4_submount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006570 .getattr = nfs4_proc_getattr,
6571 .setattr = nfs4_proc_setattr,
6572 .lookup = nfs4_proc_lookup,
6573 .access = nfs4_proc_access,
6574 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006575 .create = nfs4_proc_create,
6576 .remove = nfs4_proc_remove,
6577 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04006578 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579 .unlink_done = nfs4_proc_unlink_done,
6580 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006581 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04006582 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04006583 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006584 .link = nfs4_proc_link,
6585 .symlink = nfs4_proc_symlink,
6586 .mkdir = nfs4_proc_mkdir,
6587 .rmdir = nfs4_proc_remove,
6588 .readdir = nfs4_proc_readdir,
6589 .mknod = nfs4_proc_mknod,
6590 .statfs = nfs4_proc_statfs,
6591 .fsinfo = nfs4_proc_fsinfo,
6592 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006593 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006594 .decode_dirent = nfs4_decode_dirent,
6595 .read_setup = nfs4_proc_read_setup,
Bryan Schumakerea7c3302012-03-19 14:54:40 -04006596 .read_rpc_prepare = nfs4_proc_read_rpc_prepare,
Trond Myklebustec06c092006-03-20 13:44:27 -05006597 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006598 .write_setup = nfs4_proc_write_setup,
Bryan Schumakerc6cb80d2012-03-19 14:54:39 -04006599 .write_rpc_prepare = nfs4_proc_write_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006600 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006601 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04006602 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006603 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006604 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006605 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006606 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006607 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00006608 .init_client = nfs4_init_client,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006609};
6610
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006611static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6612 .prefix = XATTR_NAME_NFSV4_ACL,
6613 .list = nfs4_xattr_list_nfs4_acl,
6614 .get = nfs4_xattr_get_nfs4_acl,
6615 .set = nfs4_xattr_set_nfs4_acl,
6616};
6617
6618const struct xattr_handler *nfs4_xattr_handlers[] = {
6619 &nfs4_xattr_nfs4_acl_handler,
6620 NULL
6621};
6622
Trond Myklebustef159e92012-02-06 19:50:40 -05006623module_param(max_session_slots, ushort, 0644);
6624MODULE_PARM_DESC(max_session_slots, "Maximum number of outstanding NFSv4.1 "
6625 "requests the client will negotiate");
6626
Linus Torvalds1da177e2005-04-16 15:20:36 -07006627/*
6628 * Local variables:
6629 * c-basic-offset: 8
6630 * End:
6631 */