blob: 0b8bae1199766ee9355015e63dafde8f284a8c71 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/sunrpc/clnt.h>
44#include <linux/nfs.h>
45#include <linux/nfs4.h>
46#include <linux/nfs_fs.h>
47#include <linux/nfs_page.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070049#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040050#include <linux/module.h>
Andy Adamson5a0ffe52009-04-01 09:23:18 -040051#include <linux/sunrpc/bc_xprt.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000052#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050053#include <linux/utsname.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Trond Myklebust4ce79712005-06-22 17:16:21 +000055#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050057#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050058#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040059#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040060#include "pnfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62#define NFSDBG_FACILITY NFSDBG_PROC
63
Trond Myklebust2066fe82006-09-15 08:30:46 -040064#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#define NFS4_POLL_RETRY_MAX (15*HZ)
66
Trond Myklebusta78cb572009-08-09 15:06:19 -040067#define NFS4_MAX_LOOP_ON_RECOVER (10)
68
Trond Myklebustcdd4e682006-01-03 09:55:12 +010069struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010070static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080071static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050073static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Bryan Schumaker7c513052011-03-24 17:12:24 +000074static int _nfs4_proc_lookup(struct rpc_clnt *client, struct inode *dir,
75 const struct qstr *name, struct nfs_fh *fhandle,
76 struct nfs_fattr *fattr);
Trond Myklebust99367812007-07-17 21:52:41 -040077static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040078static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
79 struct nfs_fattr *fattr, struct iattr *sattr,
80 struct nfs4_state *state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Linus Torvalds1da177e2005-04-16 15:20:36 -070082/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050083static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
Trond Myklebust52567b02009-10-23 14:46:42 -040085 if (err >= -1000)
86 return err;
87 switch (err) {
88 case -NFS4ERR_RESOURCE:
89 return -EREMOTEIO;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -080090 case -NFS4ERR_BADOWNER:
91 case -NFS4ERR_BADNAME:
92 return -EINVAL;
Trond Myklebust52567b02009-10-23 14:46:42 -040093 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -070095 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -040096 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 }
Trond Myklebust52567b02009-10-23 14:46:42 -040098 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
101/*
102 * This is our standard bitmap for GETATTR requests.
103 */
104const u32 nfs4_fattr_bitmap[2] = {
105 FATTR4_WORD0_TYPE
106 | FATTR4_WORD0_CHANGE
107 | FATTR4_WORD0_SIZE
108 | FATTR4_WORD0_FSID
109 | FATTR4_WORD0_FILEID,
110 FATTR4_WORD1_MODE
111 | FATTR4_WORD1_NUMLINKS
112 | FATTR4_WORD1_OWNER
113 | FATTR4_WORD1_OWNER_GROUP
114 | FATTR4_WORD1_RAWDEV
115 | FATTR4_WORD1_SPACE_USED
116 | FATTR4_WORD1_TIME_ACCESS
117 | FATTR4_WORD1_TIME_METADATA
118 | FATTR4_WORD1_TIME_MODIFY
119};
120
121const u32 nfs4_statfs_bitmap[2] = {
122 FATTR4_WORD0_FILES_AVAIL
123 | FATTR4_WORD0_FILES_FREE
124 | FATTR4_WORD0_FILES_TOTAL,
125 FATTR4_WORD1_SPACE_AVAIL
126 | FATTR4_WORD1_SPACE_FREE
127 | FATTR4_WORD1_SPACE_TOTAL
128};
129
Trond Myklebust4ce79712005-06-22 17:16:21 +0000130const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 FATTR4_WORD0_MAXLINK
132 | FATTR4_WORD0_MAXNAME,
133 0
134};
135
136const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
137 | FATTR4_WORD0_MAXREAD
138 | FATTR4_WORD0_MAXWRITE
139 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700140 FATTR4_WORD1_TIME_DELTA
Andy Adamson504913f2010-10-20 00:17:57 -0400141 | FATTR4_WORD1_FS_LAYOUT_TYPES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142};
143
Manoj Naik830b8e32006-06-09 09:34:25 -0400144const u32 nfs4_fs_locations_bitmap[2] = {
145 FATTR4_WORD0_TYPE
146 | FATTR4_WORD0_CHANGE
147 | FATTR4_WORD0_SIZE
148 | FATTR4_WORD0_FSID
149 | FATTR4_WORD0_FILEID
150 | FATTR4_WORD0_FS_LOCATIONS,
151 FATTR4_WORD1_MODE
152 | FATTR4_WORD1_NUMLINKS
153 | FATTR4_WORD1_OWNER
154 | FATTR4_WORD1_OWNER_GROUP
155 | FATTR4_WORD1_RAWDEV
156 | FATTR4_WORD1_SPACE_USED
157 | FATTR4_WORD1_TIME_ACCESS
158 | FATTR4_WORD1_TIME_METADATA
159 | FATTR4_WORD1_TIME_MODIFY
160 | FATTR4_WORD1_MOUNTED_ON_FILEID
161};
162
Al Virobc4785c2006-10-19 23:28:51 -0700163static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 struct nfs4_readdir_arg *readdir)
165{
Al Viro0dbb4c62006-10-19 23:28:49 -0700166 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 BUG_ON(readdir->count < 80);
169 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000170 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
172 return;
173 }
174
175 readdir->cookie = 0;
176 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
177 if (cookie == 2)
178 return;
179
180 /*
181 * NFSv4 servers do not return entries for '.' and '..'
182 * Therefore, we fake these entries here. We let '.'
183 * have cookie 0 and '..' have cookie 1. Note that
184 * when talking to the server, we always send cookie 0
185 * instead of 1 or 2.
186 */
Al Viro0dbb4c62006-10-19 23:28:49 -0700187 start = p = kmap_atomic(*readdir->pages, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 if (cookie == 0) {
190 *p++ = xdr_one; /* next */
191 *p++ = xdr_zero; /* cookie, first word */
192 *p++ = xdr_one; /* cookie, second word */
193 *p++ = xdr_one; /* entry len */
194 memcpy(p, ".\0\0\0", 4); /* entry */
195 p++;
196 *p++ = xdr_one; /* bitmap length */
197 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
198 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400199 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
201
202 *p++ = xdr_one; /* next */
203 *p++ = xdr_zero; /* cookie, first word */
204 *p++ = xdr_two; /* cookie, second word */
205 *p++ = xdr_two; /* entry len */
206 memcpy(p, "..\0\0", 4); /* entry */
207 p++;
208 *p++ = xdr_one; /* bitmap length */
209 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
210 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400211 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
213 readdir->pgbase = (char *)p - (char *)start;
214 readdir->count -= readdir->pgbase;
215 kunmap_atomic(start, KM_USER0);
216}
217
Trond Myklebust65de8722008-12-23 15:21:44 -0500218static int nfs4_wait_clnt_recover(struct nfs_client *clp)
219{
220 int res;
221
222 might_sleep();
223
Trond Myklebuste005e802008-12-23 15:21:48 -0500224 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400225 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500226 return res;
227}
228
229static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
230{
231 int res = 0;
232
233 might_sleep();
234
235 if (*timeout <= 0)
236 *timeout = NFS4_POLL_RETRY_MIN;
237 if (*timeout > NFS4_POLL_RETRY_MAX)
238 *timeout = NFS4_POLL_RETRY_MAX;
239 schedule_timeout_killable(*timeout);
240 if (fatal_signal_pending(current))
241 res = -ERESTARTSYS;
242 *timeout <<= 1;
243 return res;
244}
245
246/* This is the error handling routine for processes that are allowed
247 * to sleep.
248 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800249static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500250{
251 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500252 struct nfs4_state *state = exception->state;
Trond Myklebust65de8722008-12-23 15:21:44 -0500253 int ret = errorcode;
254
255 exception->retry = 0;
256 switch(errorcode) {
257 case 0:
258 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500259 case -NFS4ERR_ADMIN_REVOKED:
260 case -NFS4ERR_BAD_STATEID:
261 case -NFS4ERR_OPENMODE:
262 if (state == NULL)
263 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500264 nfs4_schedule_stateid_recovery(server, state);
265 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500266 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500267 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust65de8722008-12-23 15:21:44 -0500268 case -NFS4ERR_EXPIRED:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500269 nfs4_schedule_lease_recovery(clp);
270 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500271#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400272 case -NFS4ERR_BADSESSION:
273 case -NFS4ERR_BADSLOT:
274 case -NFS4ERR_BAD_HIGH_SLOT:
275 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
276 case -NFS4ERR_DEADSESSION:
277 case -NFS4ERR_SEQ_FALSE_RETRY:
278 case -NFS4ERR_SEQ_MISORDERED:
279 dprintk("%s ERROR: %d Reset session\n", __func__,
280 errorcode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500281 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -0400282 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500283 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500284#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500285 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500286 if (exception->timeout > HZ) {
287 /* We have retried a decent amount, time to
288 * fail
289 */
290 ret = -EBUSY;
291 break;
292 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500293 case -NFS4ERR_GRACE:
294 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500295 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500296 ret = nfs4_delay(server->client, &exception->timeout);
297 if (ret != 0)
298 break;
299 case -NFS4ERR_OLD_STATEID:
300 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800301 break;
302 case -NFS4ERR_BADOWNER:
303 /* The following works around a Linux server bug! */
304 case -NFS4ERR_BADNAME:
305 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
306 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
307 exception->retry = 1;
308 printk(KERN_WARNING "NFS: v4 server %s "
309 "does not accept raw "
310 "uid/gids. "
311 "Reenabling the idmapper.\n",
312 server->nfs_client->cl_hostname);
313 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500314 }
315 /* We failed to handle the error */
316 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500317wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500318 ret = nfs4_wait_clnt_recover(clp);
319 if (ret == 0)
320 exception->retry = 1;
321 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500322}
323
324
Trond Myklebust452e9352010-07-31 14:29:06 -0400325static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 spin_lock(&clp->cl_lock);
328 if (time_before(clp->cl_last_renewal,timestamp))
329 clp->cl_last_renewal = timestamp;
330 spin_unlock(&clp->cl_lock);
331}
332
Trond Myklebust452e9352010-07-31 14:29:06 -0400333static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
334{
335 do_renew_lease(server->nfs_client, timestamp);
336}
337
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400338#if defined(CONFIG_NFS_V4_1)
339
Benny Halevy510b8172009-04-01 09:22:14 -0400340/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400341 * nfs4_free_slot - free a slot and efficiently update slot table.
342 *
343 * freeing a slot is trivially done by clearing its respective bit
344 * in the bitmap.
345 * If the freed slotid equals highest_used_slotid we want to update it
346 * so that the server would be able to size down the slot table if needed,
347 * otherwise we know that the highest_used_slotid is still in use.
348 * When updating highest_used_slotid there may be "holes" in the bitmap
349 * so we need to scan down from highest_used_slotid to 0 looking for the now
350 * highest slotid in use.
351 * If none found, highest_used_slotid is set to -1.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500352 *
353 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400354 */
355static void
Benny Halevydfb4f3092010-09-24 09:17:01 -0400356nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *free_slot)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400357{
Benny Halevydfb4f3092010-09-24 09:17:01 -0400358 int free_slotid = free_slot - tbl->slots;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400359 int slotid = free_slotid;
360
Benny Halevydfb4f3092010-09-24 09:17:01 -0400361 BUG_ON(slotid < 0 || slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400362 /* clear used bit in bitmap */
363 __clear_bit(slotid, tbl->used_slots);
364
365 /* update highest_used_slotid when it is freed */
366 if (slotid == tbl->highest_used_slotid) {
367 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500368 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400369 tbl->highest_used_slotid = slotid;
370 else
371 tbl->highest_used_slotid = -1;
372 }
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400373 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
374 free_slotid, tbl->highest_used_slotid);
375}
376
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800377/*
Andy Adamson42acd022011-01-06 02:04:34 +0000378 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800379 */
Andy Adamson42acd022011-01-06 02:04:34 +0000380static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800381{
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800382 struct rpc_task *task;
383
Trond Myklebusta2118c32010-06-16 09:52:26 -0400384 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800385 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
386 if (task)
387 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800388 return;
389 }
390
391 if (ses->fc_slot_table.highest_used_slotid != -1)
392 return;
393
Andy Adamson42acd022011-01-06 02:04:34 +0000394 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
395 complete(&ses->fc_slot_table.complete);
396}
397
398/*
399 * Signal state manager thread if session back channel is drained
400 */
401void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
402{
403 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
404 ses->bc_slot_table.highest_used_slotid != -1)
405 return;
406 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
407 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800408}
409
Trond Myklebustd185a332010-06-16 09:52:25 -0400410static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400411{
412 struct nfs4_slot_table *tbl;
413
Trond Myklebustd185a332010-06-16 09:52:25 -0400414 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400415 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400416 /* just wake up the next guy waiting since
417 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500418 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500419 return;
Andy Adamson13615872009-04-01 09:22:17 -0400420 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500421
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500422 spin_lock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400423 nfs4_free_slot(tbl, res->sr_slot);
Andy Adamson42acd022011-01-06 02:04:34 +0000424 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500425 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400426 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400427}
428
Trond Myklebust14516c32010-07-31 14:29:06 -0400429static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400430{
431 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400432 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400433
434 /*
435 * sr_status remains 1 if an RPC level error occurred. The server
436 * may or may not have processed the sequence operation..
437 * Proceed as if the server received and processed the sequence
438 * operation.
439 */
440 if (res->sr_status == 1)
441 res->sr_status = NFS_OK;
442
443 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400444 if (!res->sr_slot)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400445 goto out;
446
Andy Adamson691daf32009-12-04 15:55:39 -0500447 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400448 switch (res->sr_status) {
449 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400450 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400451 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400452 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400453 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400454 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500455 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500456 if (res->sr_status_flags != 0)
457 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400458 break;
459 case -NFS4ERR_DELAY:
460 /* The server detected a resend of the RPC call and
461 * returned NFS4ERR_DELAY as per Section 2.10.6.2
462 * of RFC5661.
463 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200464 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400465 __func__,
466 res->sr_slot - res->sr_session->fc_slot_table.slots,
467 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400468 goto out_retry;
469 default:
470 /* Just update the slot sequence no. */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400471 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400472 }
473out:
474 /* The session may be reset by one of the error handlers. */
475 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400476 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400477 return 1;
478out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400479 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400480 goto out;
481 rpc_delay(task, NFS4_POLL_RETRY_MAX);
482 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400483}
484
Trond Myklebust14516c32010-07-31 14:29:06 -0400485static int nfs4_sequence_done(struct rpc_task *task,
486 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400487{
Trond Myklebust14516c32010-07-31 14:29:06 -0400488 if (res->sr_session == NULL)
489 return 1;
490 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400491}
492
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400493/*
Benny Halevy510b8172009-04-01 09:22:14 -0400494 * nfs4_find_slot - efficiently look for a free slot
495 *
496 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
497 * If found, we mark the slot as used, update the highest_used_slotid,
498 * and respectively set up the sequence operation args.
499 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400500 *
501 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400502 */
503static u8
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800504nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400505{
506 int slotid;
507 u8 ret_id = NFS4_MAX_SLOT_TABLE;
508 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
509
Benny Halevy510b8172009-04-01 09:22:14 -0400510 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
511 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
512 tbl->max_slots);
513 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
514 if (slotid >= tbl->max_slots)
515 goto out;
516 __set_bit(slotid, tbl->used_slots);
517 if (slotid > tbl->highest_used_slotid)
518 tbl->highest_used_slotid = slotid;
519 ret_id = slotid;
520out:
521 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
522 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400523 return ret_id;
524}
525
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000526int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400527 struct nfs4_sequence_args *args,
528 struct nfs4_sequence_res *res,
529 int cache_reply,
530 struct rpc_task *task)
531{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400532 struct nfs4_slot *slot;
533 struct nfs4_slot_table *tbl;
534 u8 slotid;
535
536 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400537 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400538 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400539 return 0;
540
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400541 tbl = &session->fc_slot_table;
542
543 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400544 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800545 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamsonea028ac2009-12-04 15:55:38 -0500546 /*
547 * The state manager will wait until the slot table is empty.
548 * Schedule the reset thread
549 */
Andy Adamson05f0d232009-12-04 15:55:37 -0500550 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400551 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500552 dprintk("%s Schedule Session Reset\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500553 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400554 }
555
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800556 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
557 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
558 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
559 spin_unlock(&tbl->slot_tbl_lock);
560 dprintk("%s enforce FIFO order\n", __func__);
561 return -EAGAIN;
562 }
563
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800564 slotid = nfs4_find_slot(tbl);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400565 if (slotid == NFS4_MAX_SLOT_TABLE) {
566 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
567 spin_unlock(&tbl->slot_tbl_lock);
568 dprintk("<-- %s: no free slots\n", __func__);
569 return -EAGAIN;
570 }
571 spin_unlock(&tbl->slot_tbl_lock);
572
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800573 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400574 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400575 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400576 args->sa_slotid = slotid;
577 args->sa_cache_this = cache_reply;
578
579 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
580
Benny Halevy99fe60d2009-04-01 09:22:29 -0400581 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400582 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400583 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400584 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400585 /*
586 * sr_status is only set in decode_sequence, and so will remain
587 * set to 1 if an rpc level failure occurs.
588 */
589 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400590 return 0;
591}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000592EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400593
Trond Myklebust035168a2010-06-16 09:52:26 -0400594int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400595 struct nfs4_sequence_args *args,
596 struct nfs4_sequence_res *res,
597 int cache_reply,
598 struct rpc_task *task)
599{
Trond Myklebust035168a2010-06-16 09:52:26 -0400600 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400601 int ret = 0;
602
Trond Myklebust035168a2010-06-16 09:52:26 -0400603 if (session == NULL) {
604 args->sa_session = NULL;
605 res->sr_session = NULL;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400606 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400607 }
608
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200609 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400610 __func__, session->clp, session, res->sr_slot ?
611 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400612
613 ret = nfs41_setup_sequence(session, args, res, cache_reply,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400614 task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400615out:
616 dprintk("<-- %s status=%d\n", __func__, ret);
617 return ret;
618}
619
620struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400621 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400622 struct nfs4_sequence_args *seq_args;
623 struct nfs4_sequence_res *seq_res;
624 int cache_reply;
625};
626
627static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
628{
629 struct nfs41_call_sync_data *data = calldata;
630
Trond Myklebust035168a2010-06-16 09:52:26 -0400631 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
632
633 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400634 data->seq_res, data->cache_reply, task))
635 return;
636 rpc_call_start(task);
637}
638
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800639static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
640{
641 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
642 nfs41_call_sync_prepare(task, calldata);
643}
644
Andy Adamson69ab40c2009-04-01 09:22:19 -0400645static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
646{
647 struct nfs41_call_sync_data *data = calldata;
648
Trond Myklebust14516c32010-07-31 14:29:06 -0400649 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400650}
651
Andy Adamsonce5039c2009-04-01 09:22:13 -0400652struct rpc_call_ops nfs41_call_sync_ops = {
653 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400654 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400655};
656
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800657struct rpc_call_ops nfs41_call_priv_sync_ops = {
658 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
659 .rpc_call_done = nfs41_call_sync_done,
660};
661
Bryan Schumaker7c513052011-03-24 17:12:24 +0000662static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
663 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400664 struct rpc_message *msg,
665 struct nfs4_sequence_args *args,
666 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800667 int cache_reply,
668 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400669{
670 int ret;
671 struct rpc_task *task;
672 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400673 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400674 .seq_args = args,
675 .seq_res = res,
676 .cache_reply = cache_reply,
677 };
678 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000679 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400680 .rpc_message = msg,
681 .callback_ops = &nfs41_call_sync_ops,
682 .callback_data = &data
683 };
684
Benny Halevydfb4f3092010-09-24 09:17:01 -0400685 res->sr_slot = NULL;
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800686 if (privileged)
687 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400688 task = rpc_run_task(&task_setup);
689 if (IS_ERR(task))
690 ret = PTR_ERR(task);
691 else {
692 ret = task->tk_status;
693 rpc_put_task(task);
694 }
695 return ret;
696}
697
Bryan Schumaker7c513052011-03-24 17:12:24 +0000698int _nfs4_call_sync_session(struct rpc_clnt *clnt,
699 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400700 struct rpc_message *msg,
701 struct nfs4_sequence_args *args,
702 struct nfs4_sequence_res *res,
703 int cache_reply)
704{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000705 return nfs4_call_sync_sequence(clnt, server, msg, args, res, cache_reply, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400706}
707
Trond Myklebustdf896452010-06-16 09:52:26 -0400708#else
Trond Myklebust14516c32010-07-31 14:29:06 -0400709static int nfs4_sequence_done(struct rpc_task *task,
710 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400711{
Trond Myklebust14516c32010-07-31 14:29:06 -0400712 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400713}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400714#endif /* CONFIG_NFS_V4_1 */
715
Bryan Schumaker7c513052011-03-24 17:12:24 +0000716int _nfs4_call_sync(struct rpc_clnt *clnt,
717 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400718 struct rpc_message *msg,
719 struct nfs4_sequence_args *args,
720 struct nfs4_sequence_res *res,
721 int cache_reply)
722{
Benny Halevyf3752972009-04-01 09:22:04 -0400723 args->sa_session = res->sr_session = NULL;
Bryan Schumaker7c513052011-03-24 17:12:24 +0000724 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400725}
726
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000727static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000728int nfs4_call_sync(struct rpc_clnt *clnt,
729 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000730 struct rpc_message *msg,
731 struct nfs4_sequence_args *args,
732 struct nfs4_sequence_res *res,
733 int cache_reply)
734{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000735 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
736 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000737}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400738
Trond Myklebust38478b22006-05-25 01:40:57 -0400739static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Trond Myklebust38478b22006-05-25 01:40:57 -0400741 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Trond Myklebust38478b22006-05-25 01:40:57 -0400743 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400744 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
745 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400746 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400747 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400748 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749}
750
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100751struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400752 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100753 struct nfs_openargs o_arg;
754 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100755 struct nfs_open_confirmargs c_arg;
756 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100757 struct nfs_fattr f_attr;
758 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400759 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100760 struct dentry *dir;
761 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400762 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100763 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100764 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400765 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100766 int rpc_status;
767 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100768};
769
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400770
771static void nfs4_init_opendata_res(struct nfs4_opendata *p)
772{
773 p->o_res.f_attr = &p->f_attr;
774 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400775 p->o_res.seqid = p->o_arg.seqid;
776 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400777 p->o_res.server = p->o_arg.server;
778 nfs_fattr_init(&p->f_attr);
779 nfs_fattr_init(&p->dir_attr);
780}
781
Trond Myklebustad389da2007-06-05 12:30:00 -0400782static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500783 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400784 const struct iattr *attrs,
785 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100786{
Trond Myklebustad389da2007-06-05 12:30:00 -0400787 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100788 struct inode *dir = parent->d_inode;
789 struct nfs_server *server = NFS_SERVER(dir);
790 struct nfs4_opendata *p;
791
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400792 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100793 if (p == NULL)
794 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400795 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100796 if (p->o_arg.seqid == NULL)
797 goto err_free;
Al Virof6948692010-01-30 13:51:04 -0500798 path_get(path);
799 p->path = *path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100800 p->dir = parent;
801 p->owner = sp;
802 atomic_inc(&sp->so_count);
803 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500804 p->o_arg.open_flags = flags;
805 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400806 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400807 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400808 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100809 p->o_arg.server = server;
810 p->o_arg.bitmask = server->attr_bitmask;
811 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400812 if (flags & O_CREAT) {
813 u32 *s;
814
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100815 p->o_arg.u.attrs = &p->attrs;
816 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400817 s = (u32 *) p->o_arg.u.verifier.data;
818 s[0] = jiffies;
819 s[1] = current->pid;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100820 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100821 p->c_arg.fh = &p->o_res.fh;
822 p->c_arg.stateid = &p->o_res.stateid;
823 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400824 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400825 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100826 return p;
827err_free:
828 kfree(p);
829err:
830 dput(parent);
831 return NULL;
832}
833
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400834static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100835{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400836 struct nfs4_opendata *p = container_of(kref,
837 struct nfs4_opendata, kref);
838
839 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400840 if (p->state != NULL)
841 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400842 nfs4_put_state_owner(p->owner);
843 dput(p->dir);
Jan Blunck31f31db12008-05-02 13:42:45 -0700844 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400845 kfree(p);
846}
847
848static void nfs4_opendata_put(struct nfs4_opendata *p)
849{
850 if (p != NULL)
851 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100852}
853
Trond Myklebust06f814a2006-01-03 09:55:07 +0100854static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
855{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100856 int ret;
857
Trond Myklebust06f814a2006-01-03 09:55:07 +0100858 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100859 return ret;
860}
861
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500862static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400863{
864 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500865
866 if (open_mode & O_EXCL)
867 goto out;
868 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400869 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500870 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
871 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400872 break;
873 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500874 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
875 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400876 break;
877 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500878 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
879 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400880 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500881out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400882 return ret;
883}
884
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500885static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400886{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500887 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400888 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500889 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400890 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500891 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400892 return 1;
893}
894
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500895static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100896{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500897 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100898 case FMODE_WRITE:
899 state->n_wronly++;
900 break;
901 case FMODE_READ:
902 state->n_rdonly++;
903 break;
904 case FMODE_READ|FMODE_WRITE:
905 state->n_rdwr++;
906 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500907 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100908}
909
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500910static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400911{
912 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
913 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
914 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500915 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400916 case FMODE_READ:
917 set_bit(NFS_O_RDONLY_STATE, &state->flags);
918 break;
919 case FMODE_WRITE:
920 set_bit(NFS_O_WRONLY_STATE, &state->flags);
921 break;
922 case FMODE_READ|FMODE_WRITE:
923 set_bit(NFS_O_RDWR_STATE, &state->flags);
924 }
925}
926
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500927static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400928{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400929 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500930 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400931 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400932}
933
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500934static 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 -0700935{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400936 /*
937 * Protect the call to nfs4_state_set_mode_locked and
938 * serialise the stateid update
939 */
940 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400941 if (deleg_stateid != NULL) {
942 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
943 set_bit(NFS_DELEGATED_STATE, &state->flags);
944 }
945 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500946 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400947 write_sequnlock(&state->seqlock);
948 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500949 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700950 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951}
952
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500953static 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 -0500954{
955 struct nfs_inode *nfsi = NFS_I(state->inode);
956 struct nfs_delegation *deleg_cur;
957 int ret = 0;
958
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500959 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500960
961 rcu_read_lock();
962 deleg_cur = rcu_dereference(nfsi->delegation);
963 if (deleg_cur == NULL)
964 goto no_delegation;
965
966 spin_lock(&deleg_cur->lock);
967 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500968 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500969 goto no_delegation_unlock;
970
971 if (delegation == NULL)
972 delegation = &deleg_cur->stateid;
973 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
974 goto no_delegation_unlock;
975
Trond Myklebustb7391f42008-12-23 15:21:52 -0500976 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500977 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500978 ret = 1;
979no_delegation_unlock:
980 spin_unlock(&deleg_cur->lock);
981no_delegation:
982 rcu_read_unlock();
983
984 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500985 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500986 ret = 1;
987 }
988
989 return ret;
990}
991
992
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500993static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400994{
995 struct nfs_delegation *delegation;
996
997 rcu_read_lock();
998 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500999 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001000 rcu_read_unlock();
1001 return;
1002 }
1003 rcu_read_unlock();
1004 nfs_inode_return_delegation(inode);
1005}
1006
Trond Myklebust6ee41262007-07-08 14:11:36 -04001007static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001008{
1009 struct nfs4_state *state = opendata->state;
1010 struct nfs_inode *nfsi = NFS_I(state->inode);
1011 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001012 int open_mode = opendata->o_arg.open_flags & O_EXCL;
1013 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001014 nfs4_stateid stateid;
1015 int ret = -EAGAIN;
1016
Trond Myklebustaac00a82007-07-05 19:02:21 -04001017 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001018 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001019 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001020 if (can_open_cached(state, fmode, open_mode)) {
1021 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001022 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001023 goto out_return_state;
1024 }
1025 spin_unlock(&state->owner->so_lock);
1026 }
Trond Myklebust34310432008-12-23 15:21:38 -05001027 rcu_read_lock();
1028 delegation = rcu_dereference(nfsi->delegation);
1029 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001030 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001031 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001032 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001033 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001034 /* Save the delegation */
1035 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1036 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001037 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001038 if (ret != 0)
1039 goto out;
1040 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001041
1042 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001043 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001044 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001045 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001046out:
1047 return ERR_PTR(ret);
1048out_return_state:
1049 atomic_inc(&state->count);
1050 return state;
1051}
1052
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001053static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1054{
1055 struct inode *inode;
1056 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001057 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001058 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001059
Trond Myklebustaac00a82007-07-05 19:02:21 -04001060 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001061 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001062 goto out;
1063 }
1064
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001065 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001066 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001067 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001068 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001069 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001070 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001071 goto err;
1072 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001073 state = nfs4_get_open_state(inode, data->owner);
1074 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001075 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001076 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001077 int delegation_flags = 0;
1078
Trond Myklebust003707c2007-07-05 18:07:55 -04001079 rcu_read_lock();
1080 delegation = rcu_dereference(NFS_I(inode)->delegation);
1081 if (delegation)
1082 delegation_flags = delegation->flags;
1083 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -05001084 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001085 nfs_inode_set_delegation(state->inode,
1086 data->owner->so_cred,
1087 &data->o_res);
1088 else
1089 nfs_inode_reclaim_delegation(state->inode,
1090 data->owner->so_cred,
1091 &data->o_res);
1092 }
Trond Myklebust34310432008-12-23 15:21:38 -05001093
1094 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001095 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001096 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001097out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001098 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001099err_put_inode:
1100 iput(inode);
1101err:
1102 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001103}
1104
Trond Myklebust864472e2006-01-03 09:55:15 +01001105static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1106{
1107 struct nfs_inode *nfsi = NFS_I(state->inode);
1108 struct nfs_open_context *ctx;
1109
1110 spin_lock(&state->inode->i_lock);
1111 list_for_each_entry(ctx, &nfsi->open_files, list) {
1112 if (ctx->state != state)
1113 continue;
1114 get_nfs_open_context(ctx);
1115 spin_unlock(&state->inode->i_lock);
1116 return ctx;
1117 }
1118 spin_unlock(&state->inode->i_lock);
1119 return ERR_PTR(-ENOENT);
1120}
1121
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001122static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1123{
1124 struct nfs4_opendata *opendata;
1125
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001126 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001127 if (opendata == NULL)
1128 return ERR_PTR(-ENOMEM);
1129 opendata->state = state;
1130 atomic_inc(&state->count);
1131 return opendata;
1132}
1133
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001134static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001135{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001136 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001137 int ret;
1138
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001139 opendata->o_arg.open_flags = 0;
1140 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001141 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1142 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1143 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001144 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001145 if (ret != 0)
1146 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001147 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001148 if (IS_ERR(newstate))
1149 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001150 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001151 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001152 return 0;
1153}
1154
1155static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1156{
Trond Myklebust864472e2006-01-03 09:55:15 +01001157 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001158 int ret;
1159
1160 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001161 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001162 smp_rmb();
1163 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001164 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001165 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001166 if (ret != 0)
1167 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001168 if (newstate != state)
1169 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001170 }
1171 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001172 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001173 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001174 if (ret != 0)
1175 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001176 if (newstate != state)
1177 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001178 }
1179 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001180 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001181 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001182 if (ret != 0)
1183 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001184 if (newstate != state)
1185 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001186 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001187 /*
1188 * We may have performed cached opens for all three recoveries.
1189 * Check if we need to update the current stateid.
1190 */
1191 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1192 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001193 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001194 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1195 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001196 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001197 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001198 return 0;
1199}
1200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201/*
1202 * OPEN_RECLAIM:
1203 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001205static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001207 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001208 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001209 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 int status;
1211
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001212 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1213 if (IS_ERR(opendata))
1214 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001215 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1216 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001217 rcu_read_lock();
1218 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001219 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001220 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001221 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001222 opendata->o_arg.u.delegation_type = delegation_type;
1223 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001224 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 return status;
1226}
1227
Trond Myklebust539cd032007-06-05 11:46:42 -04001228static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
1230 struct nfs_server *server = NFS_SERVER(state->inode);
1231 struct nfs4_exception exception = { };
1232 int err;
1233 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001234 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001235 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001236 break;
1237 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 } while (exception.retry);
1239 return err;
1240}
1241
Trond Myklebust864472e2006-01-03 09:55:15 +01001242static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1243{
1244 struct nfs_open_context *ctx;
1245 int ret;
1246
1247 ctx = nfs4_state_find_open_context(state);
1248 if (IS_ERR(ctx))
1249 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001250 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001251 put_nfs_open_context(ctx);
1252 return ret;
1253}
1254
Trond Myklebust13437e12007-07-06 15:10:43 -04001255static 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 -07001256{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001257 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001258 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001260 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1261 if (IS_ERR(opendata))
1262 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001263 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001264 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001265 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001266 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001267 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001268 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269}
1270
Trond Myklebust13437e12007-07-06 15:10:43 -04001271int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
1273 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001274 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 int err;
1276 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001277 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 switch (err) {
1279 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001280 case -ENOENT:
1281 case -ESTALE:
1282 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001283 case -NFS4ERR_BADSESSION:
1284 case -NFS4ERR_BADSLOT:
1285 case -NFS4ERR_BAD_HIGH_SLOT:
1286 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1287 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001288 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001289 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 case -NFS4ERR_STALE_CLIENTID:
1291 case -NFS4ERR_STALE_STATEID:
1292 case -NFS4ERR_EXPIRED:
1293 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001294 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001295 goto out;
1296 case -ERESTARTSYS:
1297 /*
1298 * The show must go on: exit, but mark the
1299 * stateid as needing recovery.
1300 */
1301 case -NFS4ERR_ADMIN_REVOKED:
1302 case -NFS4ERR_BAD_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001303 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001304 case -EKEYEXPIRED:
1305 /*
1306 * User RPCSEC_GSS context has expired.
1307 * We cannot recover this stateid now, so
1308 * skip it and allow recovery thread to
1309 * proceed.
1310 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001311 case -ENOMEM:
1312 err = 0;
1313 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 }
1315 err = nfs4_handle_exception(server, err, &exception);
1316 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001317out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 return err;
1319}
1320
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001321static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1322{
1323 struct nfs4_opendata *data = calldata;
1324
1325 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001326 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001327 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1328 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001329 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001330 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001331 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001332 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001333}
1334
1335static void nfs4_open_confirm_release(void *calldata)
1336{
1337 struct nfs4_opendata *data = calldata;
1338 struct nfs4_state *state = NULL;
1339
1340 /* If this request hasn't been cancelled, do nothing */
1341 if (data->cancelled == 0)
1342 goto out_free;
1343 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001344 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001345 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001346 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001347 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001348 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001349out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001350 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001351}
1352
1353static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001354 .rpc_call_done = nfs4_open_confirm_done,
1355 .rpc_release = nfs4_open_confirm_release,
1356};
1357
1358/*
1359 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1360 */
1361static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1362{
1363 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1364 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001365 struct rpc_message msg = {
1366 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1367 .rpc_argp = &data->c_arg,
1368 .rpc_resp = &data->c_res,
1369 .rpc_cred = data->owner->so_cred,
1370 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001371 struct rpc_task_setup task_setup_data = {
1372 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001373 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001374 .callback_ops = &nfs4_open_confirm_ops,
1375 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001376 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001377 .flags = RPC_TASK_ASYNC,
1378 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 int status;
1380
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001381 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001382 data->rpc_done = 0;
1383 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001384 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001385 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001386 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001387 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001388 status = nfs4_wait_for_completion_rpc_task(task);
1389 if (status != 0) {
1390 data->cancelled = 1;
1391 smp_wmb();
1392 } else
1393 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001394 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 return status;
1396}
1397
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001398static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001400 struct nfs4_opendata *data = calldata;
1401 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001402
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001403 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1404 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001405 /*
1406 * Check if we still need to send an OPEN call, or if we can use
1407 * a delegation instead.
1408 */
1409 if (data->state != NULL) {
1410 struct nfs_delegation *delegation;
1411
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001412 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001413 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001414 rcu_read_lock();
1415 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1416 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001417 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001418 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001419 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001420 }
1421 rcu_read_unlock();
1422 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001423 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001424 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001425 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001426 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001427 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001428 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1429 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001430 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001431 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001432 &data->o_arg.seq_args,
1433 &data->o_res.seq_res, 1, task))
1434 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001435 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001436 return;
1437out_no_action:
1438 task->tk_action = NULL;
1439
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001440}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001442static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1443{
1444 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1445 nfs4_open_prepare(task, calldata);
1446}
1447
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001448static void nfs4_open_done(struct rpc_task *task, void *calldata)
1449{
1450 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001452 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001453
Trond Myklebust14516c32010-07-31 14:29:06 -04001454 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1455 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001456
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001457 if (task->tk_status == 0) {
1458 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001459 case S_IFREG:
1460 break;
1461 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001462 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001463 break;
1464 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001465 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001466 break;
1467 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001468 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001469 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001470 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001471 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1472 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001473 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001474 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001475}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001476
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001477static void nfs4_open_release(void *calldata)
1478{
1479 struct nfs4_opendata *data = calldata;
1480 struct nfs4_state *state = NULL;
1481
1482 /* If this request hasn't been cancelled, do nothing */
1483 if (data->cancelled == 0)
1484 goto out_free;
1485 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001486 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001487 goto out_free;
1488 /* In case we need an open_confirm, no cleanup! */
1489 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1490 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001491 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001492 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001493 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001494out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001495 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001496}
1497
1498static const struct rpc_call_ops nfs4_open_ops = {
1499 .rpc_call_prepare = nfs4_open_prepare,
1500 .rpc_call_done = nfs4_open_done,
1501 .rpc_release = nfs4_open_release,
1502};
1503
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001504static const struct rpc_call_ops nfs4_recover_open_ops = {
1505 .rpc_call_prepare = nfs4_recover_open_prepare,
1506 .rpc_call_done = nfs4_open_done,
1507 .rpc_release = nfs4_open_release,
1508};
1509
1510static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001511{
1512 struct inode *dir = data->dir->d_inode;
1513 struct nfs_server *server = NFS_SERVER(dir);
1514 struct nfs_openargs *o_arg = &data->o_arg;
1515 struct nfs_openres *o_res = &data->o_res;
1516 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001517 struct rpc_message msg = {
1518 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1519 .rpc_argp = o_arg,
1520 .rpc_resp = o_res,
1521 .rpc_cred = data->owner->so_cred,
1522 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001523 struct rpc_task_setup task_setup_data = {
1524 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001525 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001526 .callback_ops = &nfs4_open_ops,
1527 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001528 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001529 .flags = RPC_TASK_ASYNC,
1530 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001531 int status;
1532
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001533 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001534 data->rpc_done = 0;
1535 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001536 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001537 if (isrecover)
1538 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001539 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001540 if (IS_ERR(task))
1541 return PTR_ERR(task);
1542 status = nfs4_wait_for_completion_rpc_task(task);
1543 if (status != 0) {
1544 data->cancelled = 1;
1545 smp_wmb();
1546 } else
1547 status = data->rpc_status;
1548 rpc_put_task(task);
1549
1550 return status;
1551}
1552
1553static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1554{
1555 struct inode *dir = data->dir->d_inode;
1556 struct nfs_openres *o_res = &data->o_res;
1557 int status;
1558
1559 status = nfs4_run_open_task(data, 1);
1560 if (status != 0 || !data->rpc_done)
1561 return status;
1562
1563 nfs_refresh_inode(dir, o_res->dir_attr);
1564
1565 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1566 status = _nfs4_proc_open_confirm(data);
1567 if (status != 0)
1568 return status;
1569 }
1570
1571 return status;
1572}
1573
1574/*
1575 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1576 */
1577static int _nfs4_proc_open(struct nfs4_opendata *data)
1578{
1579 struct inode *dir = data->dir->d_inode;
1580 struct nfs_server *server = NFS_SERVER(dir);
1581 struct nfs_openargs *o_arg = &data->o_arg;
1582 struct nfs_openres *o_res = &data->o_res;
1583 int status;
1584
1585 status = nfs4_run_open_task(data, 0);
Trond Myklebust3e309912007-07-07 13:19:59 -04001586 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001587 return status;
1588
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001589 if (o_arg->open_flags & O_CREAT) {
1590 update_changeattr(dir, &o_res->cinfo);
1591 nfs_post_op_update_inode(dir, o_res->dir_attr);
1592 } else
1593 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001594 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1595 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001597 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001599 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 }
1601 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001602 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001603 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604}
1605
Andy Adamsond83217c2011-03-01 01:34:17 +00001606static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001607{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001608 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001609 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001610
Trond Myklebusta78cb572009-08-09 15:06:19 -04001611 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001612 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001613 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001614 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001615 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1616 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001617 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001618 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001619 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001620 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001621 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001622}
1623
Andy Adamsond83217c2011-03-01 01:34:17 +00001624static int nfs4_recover_expired_lease(struct nfs_server *server)
1625{
1626 return nfs4_client_recover_expired_lease(server->nfs_client);
1627}
1628
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629/*
1630 * OPEN_EXPIRED:
1631 * reclaim state on the server after a network partition.
1632 * Assumes caller holds the appropriate lock
1633 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001634static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001636 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001637 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001639 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1640 if (IS_ERR(opendata))
1641 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001642 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001643 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001644 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001645 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001646 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647}
1648
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001649static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001650{
Trond Myklebust539cd032007-06-05 11:46:42 -04001651 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001652 struct nfs4_exception exception = { };
1653 int err;
1654
1655 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001656 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001657 switch (err) {
1658 default:
1659 goto out;
1660 case -NFS4ERR_GRACE:
1661 case -NFS4ERR_DELAY:
1662 nfs4_handle_exception(server, err, &exception);
1663 err = 0;
1664 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001665 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001666out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001667 return err;
1668}
1669
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1671{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001673 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Trond Myklebust864472e2006-01-03 09:55:15 +01001675 ctx = nfs4_state_find_open_context(state);
1676 if (IS_ERR(ctx))
1677 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001678 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001679 put_nfs_open_context(ctx);
1680 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681}
1682
1683/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001684 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1685 * fields corresponding to attributes that were used to store the verifier.
1686 * Make sure we clobber those fields in the later setattr call
1687 */
1688static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1689{
1690 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1691 !(sattr->ia_valid & ATTR_ATIME_SET))
1692 sattr->ia_valid |= ATTR_ATIME;
1693
1694 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1695 !(sattr->ia_valid & ATTR_MTIME_SET))
1696 sattr->ia_valid |= ATTR_MTIME;
1697}
1698
1699/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001700 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001702static int _nfs4_do_open(struct inode *dir, struct path *path, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703{
1704 struct nfs4_state_owner *sp;
1705 struct nfs4_state *state = NULL;
1706 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001707 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001708 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
1710 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 status = -ENOMEM;
1712 if (!(sp = nfs4_get_state_owner(server, cred))) {
1713 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1714 goto out_err;
1715 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001716 status = nfs4_recover_expired_lease(server);
1717 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001718 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001719 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001720 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001721 status = -ENOMEM;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001722 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001723 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001724 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Trond Myklebustaac00a82007-07-05 19:02:21 -04001726 if (path->dentry->d_inode != NULL)
1727 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1728
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001729 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001731 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001733 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001734 status = PTR_ERR(state);
1735 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001736 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001737 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001738 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001739
1740 if (opendata->o_arg.open_flags & O_EXCL) {
1741 nfs4_exclusive_attrset(opendata, sattr);
1742
1743 nfs_fattr_init(opendata->o_res.f_attr);
1744 status = nfs4_do_setattr(state->inode, cred,
1745 opendata->o_res.f_attr, sattr,
1746 state);
1747 if (status == 0)
1748 nfs_setattr_update_inode(state->inode, sattr);
1749 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1750 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001751 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 *res = state;
1754 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001755err_opendata_put:
1756 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001757err_put_state_owner:
1758 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 *res = NULL;
1761 return status;
1762}
1763
1764
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001765static struct nfs4_state *nfs4_do_open(struct inode *dir, struct path *path, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766{
1767 struct nfs4_exception exception = { };
1768 struct nfs4_state *res;
1769 int status;
1770
1771 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001772 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 if (status == 0)
1774 break;
1775 /* NOTE: BAD_SEQID means the server and client disagree about the
1776 * book-keeping w.r.t. state-changing operations
1777 * (OPEN/CLOSE/LOCK/LOCKU...)
1778 * It is actually a sign of a bug on the client or on the server.
1779 *
1780 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001781 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 * have unhashed the old state_owner for us, and that we can
1783 * therefore safely retry using a new one. We should still warn
1784 * the user though...
1785 */
1786 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001787 printk(KERN_WARNING "NFS: v4 server %s "
1788 " returned a bad sequence-id error!\n",
1789 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 exception.retry = 1;
1791 continue;
1792 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001793 /*
1794 * BAD_STATEID on OPEN means that the server cancelled our
1795 * state before it received the OPEN_CONFIRM.
1796 * Recover by retrying the request as per the discussion
1797 * on Page 181 of RFC3530.
1798 */
1799 if (status == -NFS4ERR_BAD_STATEID) {
1800 exception.retry = 1;
1801 continue;
1802 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001803 if (status == -EAGAIN) {
1804 /* We must have found a delegation */
1805 exception.retry = 1;
1806 continue;
1807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1809 status, &exception));
1810 } while (exception.retry);
1811 return res;
1812}
1813
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001814static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1815 struct nfs_fattr *fattr, struct iattr *sattr,
1816 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001818 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001820 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 .iap = sattr,
1822 .server = server,
1823 .bitmask = server->attr_bitmask,
1824 };
1825 struct nfs_setattrres res = {
1826 .fattr = fattr,
1827 .server = server,
1828 };
1829 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001830 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1831 .rpc_argp = &arg,
1832 .rpc_resp = &res,
1833 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001835 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001836 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Trond Myklebust0e574af2005-10-27 22:12:38 -04001838 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001840 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1841 /* Use that stateid */
1842 } else if (state != NULL) {
Trond Myklebust77041ed2010-07-01 12:49:11 -04001843 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001844 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1846
Bryan Schumaker7c513052011-03-24 17:12:24 +00001847 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001848 if (status == 0 && state != NULL)
1849 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001850 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851}
1852
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001853static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1854 struct nfs_fattr *fattr, struct iattr *sattr,
1855 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001857 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 struct nfs4_exception exception = { };
1859 int err;
1860 do {
1861 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001862 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 &exception);
1864 } while (exception.retry);
1865 return err;
1866}
1867
1868struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001869 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 struct inode *inode;
1871 struct nfs4_state *state;
1872 struct nfs_closeargs arg;
1873 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001874 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001875 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00001876 bool roc;
1877 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878};
1879
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001880static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001881{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001882 struct nfs4_closedata *calldata = data;
1883 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001884
Fred Isamanf7e89172011-01-06 11:36:32 +00001885 if (calldata->roc)
1886 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001887 nfs4_put_open_state(calldata->state);
1888 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001889 nfs4_put_state_owner(sp);
Jan Blunck31f31db12008-05-02 13:42:45 -07001890 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001891 kfree(calldata);
1892}
1893
Trond Myklebust88069f72009-12-08 08:33:16 -05001894static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1895 fmode_t fmode)
1896{
1897 spin_lock(&state->owner->so_lock);
1898 if (!(fmode & FMODE_READ))
1899 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1900 if (!(fmode & FMODE_WRITE))
1901 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1902 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1903 spin_unlock(&state->owner->so_lock);
1904}
1905
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001906static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001908 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 struct nfs_server *server = NFS_SERVER(calldata->inode);
1911
Trond Myklebust14516c32010-07-31 14:29:06 -04001912 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1913 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 /* hmm. we are done with the inode, and in the process of freeing
1915 * the state_owner. we keep this around to process errors
1916 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 switch (task->tk_status) {
1918 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00001919 if (calldata->roc)
1920 pnfs_roc_set_barrier(state->inode,
1921 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04001922 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001923 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001924 nfs4_close_clear_stateid_flags(state,
1925 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 break;
1927 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001928 case -NFS4ERR_OLD_STATEID:
1929 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001931 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001932 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001934 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1935 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001937 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001938 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939}
1940
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001941static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001943 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001944 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001945 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001946
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001947 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001948 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001949
Trond Myklebust88069f72009-12-08 08:33:16 -05001950 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1951 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001952 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001953 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001954 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001955 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001956 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1957 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1958 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001959 }
1960 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001961 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1962 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1963 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001964 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001965 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001966 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05001967
1968 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001969 /* Note: exit _without_ calling nfs4_close_done */
1970 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001971 return;
1972 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001973
Fred Isamanf7e89172011-01-06 11:36:32 +00001974 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001975 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00001976 if (calldata->roc &&
1977 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
1978 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
1979 task, NULL);
1980 return;
1981 }
1982 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001983
Trond Myklebust516a6af2005-10-27 22:12:41 -04001984 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001985 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001986 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Andy Adamson19ddab02009-04-01 09:22:20 -04001987 &calldata->arg.seq_args, &calldata->res.seq_res,
1988 1, task))
1989 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001990 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991}
1992
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001993static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001994 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001995 .rpc_call_done = nfs4_close_done,
1996 .rpc_release = nfs4_free_closedata,
1997};
1998
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999/*
2000 * It is possible for data to be read/written from a mem-mapped file
2001 * after the sys_close call (which hits the vfs layer as a flush).
2002 * This means that we can't safely call nfsv4 close on a file until
2003 * the inode is cleared. This in turn means that we are not good
2004 * NFSv4 citizens - we do not indicate to the server to update the file's
2005 * share state even when we are done with one of the three share
2006 * stateid's in the inode.
2007 *
2008 * NOTE: Caller must be holding the sp->so_owner semaphore!
2009 */
Fred Isamanf7e89172011-01-06 11:36:32 +00002010int nfs4_do_close(struct path *path, struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002012 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002014 struct nfs4_state_owner *sp = state->owner;
2015 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002016 struct rpc_message msg = {
2017 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2018 .rpc_cred = state->owner->so_cred,
2019 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002020 struct rpc_task_setup task_setup_data = {
2021 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002022 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002023 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002024 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002025 .flags = RPC_TASK_ASYNC,
2026 };
Trond Myklebust95121352005-10-18 14:20:12 -07002027 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002029 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002031 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002032 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002034 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002035 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002037 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002038 if (calldata->arg.seqid == NULL)
2039 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002040 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002041 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002042 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002043 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002044 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002045 calldata->roc = roc;
Al Virof6948692010-01-30 13:51:04 -05002046 path_get(path);
2047 calldata->path = *path;
Trond Myklebust95121352005-10-18 14:20:12 -07002048
Trond Myklebust1174dd12010-12-21 10:52:24 -05002049 msg.rpc_argp = &calldata->arg;
2050 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002051 task_setup_data.callback_data = calldata;
2052 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002053 if (IS_ERR(task))
2054 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002055 status = 0;
2056 if (wait)
2057 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002058 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002059 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002060out_free_calldata:
2061 kfree(calldata);
2062out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002063 if (roc)
2064 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002065 nfs4_put_open_state(state);
2066 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002067 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068}
2069
Trond Myklebust2b484292010-09-17 10:56:51 -04002070static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002071nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 struct nfs4_state *state;
2074
Trond Myklebust565277f2007-10-15 18:17:53 -04002075 /* Protect against concurrent sillydeletes */
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002076 state = nfs4_do_open(dir, &ctx->path, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002077 if (IS_ERR(state))
2078 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002079 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002080 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081}
2082
Trond Myklebust1185a552009-12-03 15:54:02 -05002083static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002084{
2085 if (ctx->state == NULL)
2086 return;
2087 if (is_sync)
2088 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2089 else
2090 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2091}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
2093static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2094{
Benny Halevy43652ad2009-04-01 09:21:54 -04002095 struct nfs4_server_caps_arg args = {
2096 .fhandle = fhandle,
2097 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 struct nfs4_server_caps_res res = {};
2099 struct rpc_message msg = {
2100 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002101 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 .rpc_resp = &res,
2103 };
2104 int status;
2105
Bryan Schumaker7c513052011-03-24 17:12:24 +00002106 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 if (status == 0) {
2108 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002109 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2110 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2111 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2112 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2113 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2115 server->caps |= NFS_CAP_ACLS;
2116 if (res.has_links != 0)
2117 server->caps |= NFS_CAP_HARDLINKS;
2118 if (res.has_symlinks != 0)
2119 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002120 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2121 server->caps |= NFS_CAP_FILEID;
2122 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2123 server->caps |= NFS_CAP_MODE;
2124 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2125 server->caps |= NFS_CAP_NLINK;
2126 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2127 server->caps |= NFS_CAP_OWNER;
2128 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2129 server->caps |= NFS_CAP_OWNER_GROUP;
2130 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2131 server->caps |= NFS_CAP_ATIME;
2132 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2133 server->caps |= NFS_CAP_CTIME;
2134 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2135 server->caps |= NFS_CAP_MTIME;
2136
Trond Myklebusta65318b2009-03-11 14:10:28 -04002137 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2138 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2139 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 server->acl_bitmask = res.acl_bitmask;
2141 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002142
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 return status;
2144}
2145
Trond Myklebust55a97592006-06-09 09:34:19 -04002146int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147{
2148 struct nfs4_exception exception = { };
2149 int err;
2150 do {
2151 err = nfs4_handle_exception(server,
2152 _nfs4_server_capabilities(server, fhandle),
2153 &exception);
2154 } while (exception.retry);
2155 return err;
2156}
2157
2158static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2159 struct nfs_fsinfo *info)
2160{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 struct nfs4_lookup_root_arg args = {
2162 .bitmask = nfs4_fattr_bitmap,
2163 };
2164 struct nfs4_lookup_res res = {
2165 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002166 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 .fh = fhandle,
2168 };
2169 struct rpc_message msg = {
2170 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2171 .rpc_argp = &args,
2172 .rpc_resp = &res,
2173 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002174
Trond Myklebust0e574af2005-10-27 22:12:38 -04002175 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002176 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177}
2178
2179static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2180 struct nfs_fsinfo *info)
2181{
2182 struct nfs4_exception exception = { };
2183 int err;
2184 do {
2185 err = nfs4_handle_exception(server,
2186 _nfs4_lookup_root(server, fhandle, info),
2187 &exception);
2188 } while (exception.retry);
2189 return err;
2190}
2191
David Howells54ceac42006-08-22 20:06:13 -04002192/*
2193 * get the file handle for the "/" directory on the server
2194 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002196 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 int status;
2199
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 if (status == 0)
2202 status = nfs4_server_capabilities(server, fhandle);
2203 if (status == 0)
2204 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002205 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206}
2207
Manoj Naik6b97fd32006-06-09 09:34:29 -04002208/*
2209 * Get locations and (maybe) other attributes of a referral.
2210 * Note that we'll actually follow the referral later when
2211 * we detect fsid mismatch in inode revalidation
2212 */
Trond Myklebust56659e92007-07-17 21:52:39 -04002213static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002214{
2215 int status = -ENOMEM;
2216 struct page *page = NULL;
2217 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002218
2219 page = alloc_page(GFP_KERNEL);
2220 if (page == NULL)
2221 goto out;
2222 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2223 if (locations == NULL)
2224 goto out;
2225
Trond Myklebustc228fd32007-01-13 02:28:11 -05002226 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002227 if (status != 0)
2228 goto out;
2229 /* Make sure server returned a different fsid for the referral */
2230 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07002231 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002232 status = -EIO;
2233 goto out;
2234 }
2235
2236 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2237 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2238 if (!fattr->mode)
2239 fattr->mode = S_IFDIR;
2240 memset(fhandle, 0, sizeof(struct nfs_fh));
2241out:
2242 if (page)
2243 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002244 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002245 return status;
2246}
2247
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2249{
2250 struct nfs4_getattr_arg args = {
2251 .fh = fhandle,
2252 .bitmask = server->attr_bitmask,
2253 };
2254 struct nfs4_getattr_res res = {
2255 .fattr = fattr,
2256 .server = server,
2257 };
2258 struct rpc_message msg = {
2259 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2260 .rpc_argp = &args,
2261 .rpc_resp = &res,
2262 };
2263
Trond Myklebust0e574af2005-10-27 22:12:38 -04002264 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002265 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266}
2267
2268static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2269{
2270 struct nfs4_exception exception = { };
2271 int err;
2272 do {
2273 err = nfs4_handle_exception(server,
2274 _nfs4_proc_getattr(server, fhandle, fattr),
2275 &exception);
2276 } while (exception.retry);
2277 return err;
2278}
2279
2280/*
2281 * The file is not closed if it is opened due to the a request to change
2282 * the size of the file. The open call will not be needed once the
2283 * VFS layer lookup-intents are implemented.
2284 *
2285 * Close is called when the inode is destroyed.
2286 * If we haven't opened the file for O_WRONLY, we
2287 * need to in the size_change case to obtain a stateid.
2288 *
2289 * Got race?
2290 * Because OPEN is always done by name in nfsv4, it is
2291 * possible that we opened a different file by the same
2292 * name. We can recognize this race condition, but we
2293 * can't do anything about it besides returning an error.
2294 *
2295 * This will be fixed with VFS changes (lookup-intent).
2296 */
2297static int
2298nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2299 struct iattr *sattr)
2300{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002301 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002302 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002303 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 int status;
2305
Trond Myklebust0e574af2005-10-27 22:12:38 -04002306 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
Trond Myklebustd5308382005-11-04 15:33:38 -05002308 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002309 if (sattr->ia_valid & ATTR_FILE) {
2310 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002311
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002312 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002313 if (ctx) {
2314 cred = ctx->cred;
2315 state = ctx->state;
2316 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002317 }
2318
2319 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002320 if (status == 0)
2321 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 return status;
2323}
2324
Bryan Schumaker7c513052011-03-24 17:12:24 +00002325static int _nfs4_proc_lookupfh(struct rpc_clnt *clnt, struct nfs_server *server,
2326 const struct nfs_fh *dirfh, const struct qstr *name,
2327 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002328{
2329 int status;
2330 struct nfs4_lookup_arg args = {
2331 .bitmask = server->attr_bitmask,
2332 .dir_fh = dirfh,
2333 .name = name,
2334 };
2335 struct nfs4_lookup_res res = {
2336 .server = server,
2337 .fattr = fattr,
2338 .fh = fhandle,
2339 };
2340 struct rpc_message msg = {
2341 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2342 .rpc_argp = &args,
2343 .rpc_resp = &res,
2344 };
2345
2346 nfs_fattr_init(fattr);
2347
2348 dprintk("NFS call lookupfh %s\n", name->name);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002349 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002350 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002351 return status;
2352}
2353
2354static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2355 struct qstr *name, struct nfs_fh *fhandle,
2356 struct nfs_fattr *fattr)
2357{
2358 struct nfs4_exception exception = { };
2359 int err;
2360 do {
Bryan Schumaker7c513052011-03-24 17:12:24 +00002361 err = _nfs4_proc_lookupfh(server->client, server, dirfh, name, fhandle, fattr);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002362 /* FIXME: !!!! */
2363 if (err == -NFS4ERR_MOVED) {
2364 err = -EREMOTE;
2365 break;
2366 }
2367 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002368 } while (exception.retry);
2369 return err;
2370}
2371
Bryan Schumaker7c513052011-03-24 17:12:24 +00002372static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2373 const struct qstr *name, struct nfs_fh *fhandle,
2374 struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002376 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
2378 dprintk("NFS call lookup %s\n", name->name);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002379 status = _nfs4_proc_lookupfh(clnt, NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002380 if (status == -NFS4ERR_MOVED)
2381 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 dprintk("NFS reply lookup: %d\n", status);
2383 return status;
2384}
2385
Bryan Schumaker7c513052011-03-24 17:12:24 +00002386static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2387 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388{
2389 struct nfs4_exception exception = { };
2390 int err;
2391 do {
2392 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumaker7c513052011-03-24 17:12:24 +00002393 _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 &exception);
2395 } while (exception.retry);
2396 return err;
2397}
2398
2399static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2400{
Trond Myklebust76b32992007-08-10 17:45:11 -04002401 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 struct nfs4_accessargs args = {
2403 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002404 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002406 struct nfs4_accessres res = {
2407 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002408 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 struct rpc_message msg = {
2410 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2411 .rpc_argp = &args,
2412 .rpc_resp = &res,
2413 .rpc_cred = entry->cred,
2414 };
2415 int mode = entry->mask;
2416 int status;
2417
2418 /*
2419 * Determine which access bits we want to ask for...
2420 */
2421 if (mode & MAY_READ)
2422 args.access |= NFS4_ACCESS_READ;
2423 if (S_ISDIR(inode->i_mode)) {
2424 if (mode & MAY_WRITE)
2425 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2426 if (mode & MAY_EXEC)
2427 args.access |= NFS4_ACCESS_LOOKUP;
2428 } else {
2429 if (mode & MAY_WRITE)
2430 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2431 if (mode & MAY_EXEC)
2432 args.access |= NFS4_ACCESS_EXECUTE;
2433 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002434
2435 res.fattr = nfs_alloc_fattr();
2436 if (res.fattr == NULL)
2437 return -ENOMEM;
2438
Bryan Schumaker7c513052011-03-24 17:12:24 +00002439 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 if (!status) {
2441 entry->mask = 0;
2442 if (res.access & NFS4_ACCESS_READ)
2443 entry->mask |= MAY_READ;
2444 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2445 entry->mask |= MAY_WRITE;
2446 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2447 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002448 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002450 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 return status;
2452}
2453
2454static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2455{
2456 struct nfs4_exception exception = { };
2457 int err;
2458 do {
2459 err = nfs4_handle_exception(NFS_SERVER(inode),
2460 _nfs4_proc_access(inode, entry),
2461 &exception);
2462 } while (exception.retry);
2463 return err;
2464}
2465
2466/*
2467 * TODO: For the time being, we don't try to get any attributes
2468 * along with any of the zero-copy operations READ, READDIR,
2469 * READLINK, WRITE.
2470 *
2471 * In the case of the first three, we want to put the GETATTR
2472 * after the read-type operation -- this is because it is hard
2473 * to predict the length of a GETATTR response in v4, and thus
2474 * align the READ data correctly. This means that the GETATTR
2475 * may end up partially falling into the page cache, and we should
2476 * shift it into the 'tail' of the xdr_buf before processing.
2477 * To do this efficiently, we need to know the total length
2478 * of data received, which doesn't seem to be available outside
2479 * of the RPC layer.
2480 *
2481 * In the case of WRITE, we also want to put the GETATTR after
2482 * the operation -- in this case because we want to make sure
2483 * we get the post-operation mtime and size. This means that
2484 * we can't use xdr_encode_pages() as written: we need a variant
2485 * of it which would leave room in the 'tail' iovec.
2486 *
2487 * Both of these changes to the XDR layer would in fact be quite
2488 * minor, but I decided to leave them for a subsequent patch.
2489 */
2490static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2491 unsigned int pgbase, unsigned int pglen)
2492{
2493 struct nfs4_readlink args = {
2494 .fh = NFS_FH(inode),
2495 .pgbase = pgbase,
2496 .pglen = pglen,
2497 .pages = &page,
2498 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002499 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 struct rpc_message msg = {
2501 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2502 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002503 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 };
2505
Bryan Schumaker7c513052011-03-24 17:12:24 +00002506 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 -07002507}
2508
2509static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2510 unsigned int pgbase, unsigned int pglen)
2511{
2512 struct nfs4_exception exception = { };
2513 int err;
2514 do {
2515 err = nfs4_handle_exception(NFS_SERVER(inode),
2516 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2517 &exception);
2518 } while (exception.retry);
2519 return err;
2520}
2521
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522/*
2523 * Got race?
2524 * We will need to arrange for the VFS layer to provide an atomic open.
2525 * Until then, this create/open method is prone to inefficiency and race
2526 * conditions due to the lookup, create, and open VFS calls from sys_open()
2527 * placed on the wire.
2528 *
2529 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2530 * The file will be opened again in the subsequent VFS open call
2531 * (nfs4_proc_file_open).
2532 *
2533 * The open for read will just hang around to be used by any process that
2534 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2535 */
2536
2537static int
2538nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002539 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540{
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002541 struct path my_path = {
Trond Myklebustad389da2007-06-05 12:30:00 -04002542 .dentry = dentry,
2543 };
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002544 struct path *path = &my_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002546 struct rpc_cred *cred = NULL;
2547 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 int status = 0;
2549
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002550 if (ctx != NULL) {
2551 cred = ctx->cred;
2552 path = &ctx->path;
2553 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002555 sattr->ia_mode &= ~current_umask();
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002556 state = nfs4_do_open(dir, path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002557 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 if (IS_ERR(state)) {
2559 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002560 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002562 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002563 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002564 if (ctx != NULL)
2565 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002566 else
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002567 nfs4_close_sync(path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568out:
2569 return status;
2570}
2571
2572static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2573{
Trond Myklebust16e42952005-10-27 22:12:44 -04002574 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002575 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002577 .name.len = name->len,
2578 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002579 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002581 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002582 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002583 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002585 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2586 .rpc_argp = &args,
2587 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002589 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
Trond Myklebustd3468902010-04-16 16:22:50 -04002591 res.dir_attr = nfs_alloc_fattr();
2592 if (res.dir_attr == NULL)
2593 goto out;
2594
Bryan Schumaker7c513052011-03-24 17:12:24 +00002595 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002596 if (status == 0) {
2597 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002598 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002599 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002600 nfs_free_fattr(res.dir_attr);
2601out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 return status;
2603}
2604
2605static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2606{
2607 struct nfs4_exception exception = { };
2608 int err;
2609 do {
2610 err = nfs4_handle_exception(NFS_SERVER(dir),
2611 _nfs4_proc_remove(dir, name),
2612 &exception);
2613 } while (exception.retry);
2614 return err;
2615}
2616
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002617static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002619 struct nfs_server *server = NFS_SERVER(dir);
2620 struct nfs_removeargs *args = msg->rpc_argp;
2621 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
Trond Myklebusta65318b2009-03-11 14:10:28 -04002623 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002624 res->server = server;
Benny Halevydfb4f3092010-09-24 09:17:01 -04002625 res->seq_res.sr_slot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627}
2628
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002629static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002631 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2632
Trond Myklebust14516c32010-07-31 14:29:06 -04002633 if (!nfs4_sequence_done(task, &res->seq_res))
2634 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002635 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002636 return 0;
2637 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002638 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002639 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640}
2641
Jeff Laytond3d41522010-09-17 17:31:57 -04002642static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2643{
2644 struct nfs_server *server = NFS_SERVER(dir);
2645 struct nfs_renameargs *arg = msg->rpc_argp;
2646 struct nfs_renameres *res = msg->rpc_resp;
2647
2648 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2649 arg->bitmask = server->attr_bitmask;
2650 res->server = server;
2651}
2652
2653static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2654 struct inode *new_dir)
2655{
2656 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2657
2658 if (!nfs4_sequence_done(task, &res->seq_res))
2659 return 0;
2660 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2661 return 0;
2662
2663 update_changeattr(old_dir, &res->old_cinfo);
2664 nfs_post_op_update_inode(old_dir, res->old_fattr);
2665 update_changeattr(new_dir, &res->new_cinfo);
2666 nfs_post_op_update_inode(new_dir, res->new_fattr);
2667 return 1;
2668}
2669
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2671 struct inode *new_dir, struct qstr *new_name)
2672{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002673 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002674 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 .old_dir = NFS_FH(old_dir),
2676 .new_dir = NFS_FH(new_dir),
2677 .old_name = old_name,
2678 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002679 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002681 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002682 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002683 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 struct rpc_message msg = {
2685 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2686 .rpc_argp = &arg,
2687 .rpc_resp = &res,
2688 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002689 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
Trond Myklebust011fff72010-04-16 16:22:49 -04002691 res.old_fattr = nfs_alloc_fattr();
2692 res.new_fattr = nfs_alloc_fattr();
2693 if (res.old_fattr == NULL || res.new_fattr == NULL)
2694 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695
Bryan Schumaker7c513052011-03-24 17:12:24 +00002696 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 if (!status) {
2698 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002699 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002701 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002703out:
2704 nfs_free_fattr(res.new_fattr);
2705 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 return status;
2707}
2708
2709static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2710 struct inode *new_dir, struct qstr *new_name)
2711{
2712 struct nfs4_exception exception = { };
2713 int err;
2714 do {
2715 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2716 _nfs4_proc_rename(old_dir, old_name,
2717 new_dir, new_name),
2718 &exception);
2719 } while (exception.retry);
2720 return err;
2721}
2722
2723static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2724{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002725 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 struct nfs4_link_arg arg = {
2727 .fh = NFS_FH(inode),
2728 .dir_fh = NFS_FH(dir),
2729 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002730 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002732 struct nfs4_link_res res = {
2733 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002734 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 struct rpc_message msg = {
2736 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2737 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002738 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002740 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741
Trond Myklebust136f2622010-04-16 16:22:49 -04002742 res.fattr = nfs_alloc_fattr();
2743 res.dir_attr = nfs_alloc_fattr();
2744 if (res.fattr == NULL || res.dir_attr == NULL)
2745 goto out;
2746
Bryan Schumaker7c513052011-03-24 17:12:24 +00002747 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002748 if (!status) {
2749 update_changeattr(dir, &res.cinfo);
2750 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002751 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002752 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002753out:
2754 nfs_free_fattr(res.dir_attr);
2755 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 return status;
2757}
2758
2759static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2760{
2761 struct nfs4_exception exception = { };
2762 int err;
2763 do {
2764 err = nfs4_handle_exception(NFS_SERVER(inode),
2765 _nfs4_proc_link(inode, dir, name),
2766 &exception);
2767 } while (exception.retry);
2768 return err;
2769}
2770
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002771struct nfs4_createdata {
2772 struct rpc_message msg;
2773 struct nfs4_create_arg arg;
2774 struct nfs4_create_res res;
2775 struct nfs_fh fh;
2776 struct nfs_fattr fattr;
2777 struct nfs_fattr dir_fattr;
2778};
2779
2780static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2781 struct qstr *name, struct iattr *sattr, u32 ftype)
2782{
2783 struct nfs4_createdata *data;
2784
2785 data = kzalloc(sizeof(*data), GFP_KERNEL);
2786 if (data != NULL) {
2787 struct nfs_server *server = NFS_SERVER(dir);
2788
2789 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2790 data->msg.rpc_argp = &data->arg;
2791 data->msg.rpc_resp = &data->res;
2792 data->arg.dir_fh = NFS_FH(dir);
2793 data->arg.server = server;
2794 data->arg.name = name;
2795 data->arg.attrs = sattr;
2796 data->arg.ftype = ftype;
2797 data->arg.bitmask = server->attr_bitmask;
2798 data->res.server = server;
2799 data->res.fh = &data->fh;
2800 data->res.fattr = &data->fattr;
2801 data->res.dir_fattr = &data->dir_fattr;
2802 nfs_fattr_init(data->res.fattr);
2803 nfs_fattr_init(data->res.dir_fattr);
2804 }
2805 return data;
2806}
2807
2808static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2809{
Bryan Schumaker7c513052011-03-24 17:12:24 +00002810 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00002811 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002812 if (status == 0) {
2813 update_changeattr(dir, &data->res.dir_cinfo);
2814 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2815 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2816 }
2817 return status;
2818}
2819
2820static void nfs4_free_createdata(struct nfs4_createdata *data)
2821{
2822 kfree(data);
2823}
2824
Chuck Lever4f390c12006-08-22 20:06:22 -04002825static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002826 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002828 struct nfs4_createdata *data;
2829 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
Chuck Lever94a6d752006-08-22 20:06:23 -04002831 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002832 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002833
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002834 status = -ENOMEM;
2835 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2836 if (data == NULL)
2837 goto out;
2838
2839 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2840 data->arg.u.symlink.pages = &page;
2841 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002843 status = nfs4_do_create(dir, dentry, data);
2844
2845 nfs4_free_createdata(data);
2846out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 return status;
2848}
2849
Chuck Lever4f390c12006-08-22 20:06:22 -04002850static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002851 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852{
2853 struct nfs4_exception exception = { };
2854 int err;
2855 do {
2856 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002857 _nfs4_proc_symlink(dir, dentry, page,
2858 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 &exception);
2860 } while (exception.retry);
2861 return err;
2862}
2863
2864static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2865 struct iattr *sattr)
2866{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002867 struct nfs4_createdata *data;
2868 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002870 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2871 if (data == NULL)
2872 goto out;
2873
2874 status = nfs4_do_create(dir, dentry, data);
2875
2876 nfs4_free_createdata(data);
2877out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 return status;
2879}
2880
2881static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2882 struct iattr *sattr)
2883{
2884 struct nfs4_exception exception = { };
2885 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002886
2887 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 do {
2889 err = nfs4_handle_exception(NFS_SERVER(dir),
2890 _nfs4_proc_mkdir(dir, dentry, sattr),
2891 &exception);
2892 } while (exception.retry);
2893 return err;
2894}
2895
2896static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002897 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898{
2899 struct inode *dir = dentry->d_inode;
2900 struct nfs4_readdir_arg args = {
2901 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002902 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 .pgbase = 0,
2904 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002905 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04002906 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 };
2908 struct nfs4_readdir_res res;
2909 struct rpc_message msg = {
2910 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2911 .rpc_argp = &args,
2912 .rpc_resp = &res,
2913 .rpc_cred = cred,
2914 };
2915 int status;
2916
Harvey Harrison3110ff82008-05-02 13:42:44 -07002917 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002918 dentry->d_parent->d_name.name,
2919 dentry->d_name.name,
2920 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2922 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00002923 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 -05002924 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05002926 status += args.pgbase;
2927 }
Trond Myklebustc4812992007-09-28 17:11:45 -04002928
2929 nfs_invalidate_atime(dir);
2930
Harvey Harrison3110ff82008-05-02 13:42:44 -07002931 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 return status;
2933}
2934
2935static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002936 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937{
2938 struct nfs4_exception exception = { };
2939 int err;
2940 do {
2941 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2942 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002943 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 &exception);
2945 } while (exception.retry);
2946 return err;
2947}
2948
2949static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2950 struct iattr *sattr, dev_t rdev)
2951{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002952 struct nfs4_createdata *data;
2953 int mode = sattr->ia_mode;
2954 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955
2956 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2957 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002958
2959 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2960 if (data == NULL)
2961 goto out;
2962
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002964 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002966 data->arg.ftype = NF4BLK;
2967 data->arg.u.device.specdata1 = MAJOR(rdev);
2968 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 }
2970 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002971 data->arg.ftype = NF4CHR;
2972 data->arg.u.device.specdata1 = MAJOR(rdev);
2973 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002976 status = nfs4_do_create(dir, dentry, data);
2977
2978 nfs4_free_createdata(data);
2979out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 return status;
2981}
2982
2983static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2984 struct iattr *sattr, dev_t rdev)
2985{
2986 struct nfs4_exception exception = { };
2987 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002988
2989 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 do {
2991 err = nfs4_handle_exception(NFS_SERVER(dir),
2992 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2993 &exception);
2994 } while (exception.retry);
2995 return err;
2996}
2997
2998static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2999 struct nfs_fsstat *fsstat)
3000{
3001 struct nfs4_statfs_arg args = {
3002 .fh = fhandle,
3003 .bitmask = server->attr_bitmask,
3004 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003005 struct nfs4_statfs_res res = {
3006 .fsstat = fsstat,
3007 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 struct rpc_message msg = {
3009 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3010 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003011 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 };
3013
Trond Myklebust0e574af2005-10-27 22:12:38 -04003014 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003015 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016}
3017
3018static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3019{
3020 struct nfs4_exception exception = { };
3021 int err;
3022 do {
3023 err = nfs4_handle_exception(server,
3024 _nfs4_proc_statfs(server, fhandle, fsstat),
3025 &exception);
3026 } while (exception.retry);
3027 return err;
3028}
3029
3030static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3031 struct nfs_fsinfo *fsinfo)
3032{
3033 struct nfs4_fsinfo_arg args = {
3034 .fh = fhandle,
3035 .bitmask = server->attr_bitmask,
3036 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003037 struct nfs4_fsinfo_res res = {
3038 .fsinfo = fsinfo,
3039 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 struct rpc_message msg = {
3041 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3042 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003043 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 };
3045
Bryan Schumaker7c513052011-03-24 17:12:24 +00003046 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047}
3048
3049static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3050{
3051 struct nfs4_exception exception = { };
3052 int err;
3053
3054 do {
3055 err = nfs4_handle_exception(server,
3056 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3057 &exception);
3058 } while (exception.retry);
3059 return err;
3060}
3061
3062static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3063{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003064 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3066}
3067
3068static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3069 struct nfs_pathconf *pathconf)
3070{
3071 struct nfs4_pathconf_arg args = {
3072 .fh = fhandle,
3073 .bitmask = server->attr_bitmask,
3074 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003075 struct nfs4_pathconf_res res = {
3076 .pathconf = pathconf,
3077 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 struct rpc_message msg = {
3079 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3080 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003081 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 };
3083
3084 /* None of the pathconf attributes are mandatory to implement */
3085 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3086 memset(pathconf, 0, sizeof(*pathconf));
3087 return 0;
3088 }
3089
Trond Myklebust0e574af2005-10-27 22:12:38 -04003090 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003091 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092}
3093
3094static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3095 struct nfs_pathconf *pathconf)
3096{
3097 struct nfs4_exception exception = { };
3098 int err;
3099
3100 do {
3101 err = nfs4_handle_exception(server,
3102 _nfs4_proc_pathconf(server, fhandle, pathconf),
3103 &exception);
3104 } while (exception.retry);
3105 return err;
3106}
3107
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003108static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109{
Trond Myklebustec06c092006-03-20 13:44:27 -05003110 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003112 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003113 nfs_restart_rpc(task, server->nfs_client);
Trond Myklebustec06c092006-03-20 13:44:27 -05003114 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003116
3117 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003119 renew_lease(server, data->timestamp);
3120 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121}
3122
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003123static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3124{
3125
3126 dprintk("--> %s\n", __func__);
3127
3128 if (!nfs4_sequence_done(task, &data->res.seq_res))
3129 return -EAGAIN;
3130
3131 return data->read_done_cb(task, data);
3132}
3133
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003134static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003137 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003138 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139}
3140
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003141/* Reset the the nfs_read_data to send the read to the MDS. */
3142void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3143{
3144 dprintk("%s Reset task for i/o through\n", __func__);
3145 put_lseg(data->lseg);
3146 data->lseg = NULL;
3147 /* offsets will differ in the dense stripe case */
3148 data->args.offset = data->mds_offset;
3149 data->ds_clp = NULL;
3150 data->args.fh = NFS_FH(data->inode);
3151 data->read_done_cb = nfs4_read_done_cb;
3152 task->tk_ops = data->mds_ops;
3153 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3154}
3155EXPORT_SYMBOL_GPL(nfs4_reset_read);
3156
Fred Isamanb029bc92011-03-03 15:13:42 +00003157static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 struct inode *inode = data->inode;
3160
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003161 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003162 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003163 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003165 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003167 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003168 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003169 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170}
3171
Fred Isamanb029bc92011-03-03 15:13:42 +00003172static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3173{
3174 if (!nfs4_sequence_done(task, &data->res.seq_res))
3175 return -EAGAIN;
3176 return data->write_done_cb(task, data);
3177}
3178
Fred Isamana69aef12011-03-03 15:13:47 +00003179/* Reset the the nfs_write_data to send the write to the MDS. */
3180void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3181{
3182 dprintk("%s Reset task for i/o through\n", __func__);
3183 put_lseg(data->lseg);
3184 data->lseg = NULL;
3185 data->ds_clp = NULL;
3186 data->write_done_cb = nfs4_write_done_cb;
3187 data->args.fh = NFS_FH(data->inode);
3188 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3189 data->args.offset = data->mds_offset;
3190 data->res.fattr = &data->fattr;
3191 task->tk_ops = data->mds_ops;
3192 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3193}
3194EXPORT_SYMBOL_GPL(nfs4_reset_write);
3195
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003196static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003198 struct nfs_server *server = NFS_SERVER(data->inode);
3199
Fred Isaman7ffd1062011-03-03 15:13:46 +00003200 if (data->lseg) {
3201 data->args.bitmask = NULL;
3202 data->res.fattr = NULL;
3203 } else
3204 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isamanb029bc92011-03-03 15:13:42 +00003205 if (!data->write_done_cb)
3206 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003207 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 data->timestamp = jiffies;
3209
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003210 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211}
3212
Trond Myklebust788e7a82006-03-20 13:44:27 -05003213static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 struct inode *inode = data->inode;
3216
Trond Myklebust14516c32010-07-31 14:29:06 -04003217 if (!nfs4_sequence_done(task, &data->res.seq_res))
3218 return -EAGAIN;
3219
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003220 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003221 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003222 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003224 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003225 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226}
3227
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003228static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003230 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231
Trond Myklebusta65318b2009-03-11 14:10:28 -04003232 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003233 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003234 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235}
3236
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003237struct nfs4_renewdata {
3238 struct nfs_client *client;
3239 unsigned long timestamp;
3240};
3241
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242/*
3243 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3244 * standalone procedure for queueing an asynchronous RENEW.
3245 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003246static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003247{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003248 struct nfs4_renewdata *data = calldata;
3249 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003250
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003251 if (atomic_read(&clp->cl_count) > 1)
3252 nfs4_schedule_state_renewal(clp);
3253 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003254 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003255}
3256
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003257static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003259 struct nfs4_renewdata *data = calldata;
3260 struct nfs_client *clp = data->client;
3261 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262
3263 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003264 /* Unless we're shutting down, schedule state recovery! */
3265 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003266 nfs4_schedule_lease_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 return;
3268 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003269 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270}
3271
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003272static const struct rpc_call_ops nfs4_renew_ops = {
3273 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003274 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003275};
3276
David Howellsadfa6f92006-08-22 20:06:08 -04003277int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278{
3279 struct rpc_message msg = {
3280 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3281 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003282 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003284 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003286 if (!atomic_inc_not_zero(&clp->cl_count))
3287 return -EIO;
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003288 data = kmalloc(sizeof(*data), GFP_KERNEL);
3289 if (data == NULL)
3290 return -ENOMEM;
3291 data->client = clp;
3292 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003294 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295}
3296
David Howellsadfa6f92006-08-22 20:06:08 -04003297int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298{
3299 struct rpc_message msg = {
3300 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3301 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003302 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 };
3304 unsigned long now = jiffies;
3305 int status;
3306
3307 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3308 if (status < 0)
3309 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003310 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 return 0;
3312}
3313
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003314static inline int nfs4_server_supports_acls(struct nfs_server *server)
3315{
3316 return (server->caps & NFS_CAP_ACLS)
3317 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3318 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3319}
3320
3321/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3322 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3323 * the stack.
3324 */
3325#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3326
3327static void buf_to_pages(const void *buf, size_t buflen,
3328 struct page **pages, unsigned int *pgbase)
3329{
3330 const void *p = buf;
3331
3332 *pgbase = offset_in_page(buf);
3333 p -= *pgbase;
3334 while (p < buf + buflen) {
3335 *(pages++) = virt_to_page(p);
3336 p += PAGE_CACHE_SIZE;
3337 }
3338}
3339
Neil Hormane9e3d722011-03-04 19:26:03 -05003340static int buf_to_pages_noslab(const void *buf, size_t buflen,
3341 struct page **pages, unsigned int *pgbase)
3342{
3343 struct page *newpage, **spages;
3344 int rc = 0;
3345 size_t len;
3346 spages = pages;
3347
3348 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003349 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003350 newpage = alloc_page(GFP_KERNEL);
3351
3352 if (newpage == NULL)
3353 goto unwind;
3354 memcpy(page_address(newpage), buf, len);
3355 buf += len;
3356 buflen -= len;
3357 *pages++ = newpage;
3358 rc++;
3359 } while (buflen != 0);
3360
3361 return rc;
3362
3363unwind:
3364 for(; rc > 0; rc--)
3365 __free_page(spages[rc-1]);
3366 return -ENOMEM;
3367}
3368
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003369struct nfs4_cached_acl {
3370 int cached;
3371 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003372 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003373};
3374
3375static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003376{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003377 struct nfs_inode *nfsi = NFS_I(inode);
3378
3379 spin_lock(&inode->i_lock);
3380 kfree(nfsi->nfs4_acl);
3381 nfsi->nfs4_acl = acl;
3382 spin_unlock(&inode->i_lock);
3383}
3384
3385static void nfs4_zap_acl_attr(struct inode *inode)
3386{
3387 nfs4_set_cached_acl(inode, NULL);
3388}
3389
3390static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3391{
3392 struct nfs_inode *nfsi = NFS_I(inode);
3393 struct nfs4_cached_acl *acl;
3394 int ret = -ENOENT;
3395
3396 spin_lock(&inode->i_lock);
3397 acl = nfsi->nfs4_acl;
3398 if (acl == NULL)
3399 goto out;
3400 if (buf == NULL) /* user is just asking for length */
3401 goto out_len;
3402 if (acl->cached == 0)
3403 goto out;
3404 ret = -ERANGE; /* see getxattr(2) man page */
3405 if (acl->len > buflen)
3406 goto out;
3407 memcpy(buf, acl->data, acl->len);
3408out_len:
3409 ret = acl->len;
3410out:
3411 spin_unlock(&inode->i_lock);
3412 return ret;
3413}
3414
3415static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3416{
3417 struct nfs4_cached_acl *acl;
3418
3419 if (buf && acl_len <= PAGE_SIZE) {
3420 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3421 if (acl == NULL)
3422 goto out;
3423 acl->cached = 1;
3424 memcpy(acl->data, buf, acl_len);
3425 } else {
3426 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3427 if (acl == NULL)
3428 goto out;
3429 acl->cached = 0;
3430 }
3431 acl->len = acl_len;
3432out:
3433 nfs4_set_cached_acl(inode, acl);
3434}
3435
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003436static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003437{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003438 struct page *pages[NFS4ACL_MAXPAGES];
3439 struct nfs_getaclargs args = {
3440 .fh = NFS_FH(inode),
3441 .acl_pages = pages,
3442 .acl_len = buflen,
3443 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003444 struct nfs_getaclres res = {
3445 .acl_len = buflen,
3446 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003447 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003448 struct rpc_message msg = {
3449 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3450 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003451 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003452 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003453 struct page *localpage = NULL;
3454 int ret;
3455
3456 if (buflen < PAGE_SIZE) {
3457 /* As long as we're doing a round trip to the server anyway,
3458 * let's be prepared for a page of acl data. */
3459 localpage = alloc_page(GFP_KERNEL);
3460 resp_buf = page_address(localpage);
3461 if (localpage == NULL)
3462 return -ENOMEM;
3463 args.acl_pages[0] = localpage;
3464 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003465 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003466 } else {
3467 resp_buf = buf;
3468 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3469 }
Bryan Schumaker7c513052011-03-24 17:12:24 +00003470 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003471 if (ret)
3472 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003473 if (res.acl_len > args.acl_len)
3474 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003475 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003476 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003477 if (buf) {
3478 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003479 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003480 goto out_free;
3481 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003482 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003483 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003484 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003485out_free:
3486 if (localpage)
3487 __free_page(localpage);
3488 return ret;
3489}
3490
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003491static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3492{
3493 struct nfs4_exception exception = { };
3494 ssize_t ret;
3495 do {
3496 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3497 if (ret >= 0)
3498 break;
3499 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3500 } while (exception.retry);
3501 return ret;
3502}
3503
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003504static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3505{
3506 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003507 int ret;
3508
3509 if (!nfs4_server_supports_acls(server))
3510 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003511 ret = nfs_revalidate_inode(server, inode);
3512 if (ret < 0)
3513 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003514 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3515 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003516 ret = nfs4_read_cached_acl(inode, buf, buflen);
3517 if (ret != -ENOENT)
3518 return ret;
3519 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003520}
3521
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003522static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003523{
3524 struct nfs_server *server = NFS_SERVER(inode);
3525 struct page *pages[NFS4ACL_MAXPAGES];
3526 struct nfs_setaclargs arg = {
3527 .fh = NFS_FH(inode),
3528 .acl_pages = pages,
3529 .acl_len = buflen,
3530 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003531 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003532 struct rpc_message msg = {
3533 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3534 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003535 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003536 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003537 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003538
3539 if (!nfs4_server_supports_acls(server))
3540 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003541 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3542 if (i < 0)
3543 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003544 nfs_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003545 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003546
3547 /*
3548 * Free each page after tx, so the only ref left is
3549 * held by the network stack
3550 */
3551 for (; i > 0; i--)
3552 put_page(pages[i-1]);
3553
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003554 /*
3555 * Acl update can result in inode attribute update.
3556 * so mark the attribute cache invalid.
3557 */
3558 spin_lock(&inode->i_lock);
3559 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3560 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003561 nfs_access_zap_cache(inode);
3562 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003563 return ret;
3564}
3565
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003566static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3567{
3568 struct nfs4_exception exception = { };
3569 int err;
3570 do {
3571 err = nfs4_handle_exception(NFS_SERVER(inode),
3572 __nfs4_proc_set_acl(inode, buf, buflen),
3573 &exception);
3574 } while (exception.retry);
3575 return err;
3576}
3577
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003579nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003581 struct nfs_client *clp = server->nfs_client;
3582
3583 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 return 0;
3585 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003586 case -NFS4ERR_ADMIN_REVOKED:
3587 case -NFS4ERR_BAD_STATEID:
3588 case -NFS4ERR_OPENMODE:
3589 if (state == NULL)
3590 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003591 nfs4_schedule_stateid_recovery(server, state);
3592 goto wait_on_recovery;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003594 case -NFS4ERR_STALE_CLIENTID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 case -NFS4ERR_EXPIRED:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003596 nfs4_schedule_lease_recovery(clp);
3597 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003598#if defined(CONFIG_NFS_V4_1)
3599 case -NFS4ERR_BADSESSION:
3600 case -NFS4ERR_BADSLOT:
3601 case -NFS4ERR_BAD_HIGH_SLOT:
3602 case -NFS4ERR_DEADSESSION:
3603 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3604 case -NFS4ERR_SEQ_FALSE_RETRY:
3605 case -NFS4ERR_SEQ_MISORDERED:
3606 dprintk("%s ERROR %d, Reset session\n", __func__,
3607 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003608 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -04003609 task->tk_status = 0;
3610 return -EAGAIN;
3611#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003613 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003614 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003615 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3617 task->tk_status = 0;
3618 return -EAGAIN;
3619 case -NFS4ERR_OLD_STATEID:
3620 task->tk_status = 0;
3621 return -EAGAIN;
3622 }
3623 task->tk_status = nfs4_map_errors(task->tk_status);
3624 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003625wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003626 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003627 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3628 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3629 task->tk_status = 0;
3630 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631}
3632
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003633int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3634 unsigned short port, struct rpc_cred *cred,
3635 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636{
3637 nfs4_verifier sc_verifier;
3638 struct nfs4_setclientid setclientid = {
3639 .sc_verifier = &sc_verifier,
3640 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003641 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 };
3643 struct rpc_message msg = {
3644 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3645 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003646 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003647 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 };
Al Virobc4785c2006-10-19 23:28:51 -07003649 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 int loop = 0;
3651 int status;
3652
Al Virobc4785c2006-10-19 23:28:51 -07003653 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3655 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3656
3657 for(;;) {
3658 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003659 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003660 clp->cl_ipaddr,
3661 rpc_peeraddr2str(clp->cl_rpcclient,
3662 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003663 rpc_peeraddr2str(clp->cl_rpcclient,
3664 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003665 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 clp->cl_id_uniquifier);
3667 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003668 sizeof(setclientid.sc_netid),
3669 rpc_peeraddr2str(clp->cl_rpcclient,
3670 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003672 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 clp->cl_ipaddr, port >> 8, port & 255);
3674
3675 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3676 if (status != -NFS4ERR_CLID_INUSE)
3677 break;
3678 if (signalled())
3679 break;
3680 if (loop++ & 1)
J. Bruce Fields611c96c2010-12-13 19:05:46 -05003681 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 else
3683 if (++clp->cl_id_uniquifier == 0)
3684 break;
3685 }
3686 return status;
3687}
3688
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003689static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3690 struct nfs4_setclientid_res *arg,
3691 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692{
3693 struct nfs_fsinfo fsinfo;
3694 struct rpc_message msg = {
3695 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003696 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003698 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 };
3700 unsigned long now;
3701 int status;
3702
3703 now = jiffies;
3704 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3705 if (status == 0) {
3706 spin_lock(&clp->cl_lock);
3707 clp->cl_lease_time = fsinfo.lease_time * HZ;
3708 clp->cl_last_renewal = now;
3709 spin_unlock(&clp->cl_lock);
3710 }
3711 return status;
3712}
3713
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003714int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3715 struct nfs4_setclientid_res *arg,
3716 struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003717{
Benny Halevy77e03672008-06-24 20:25:57 +03003718 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003719 int err;
3720 do {
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003721 err = _nfs4_proc_setclientid_confirm(clp, arg, cred);
Trond Myklebust51581f32006-03-20 13:44:47 -05003722 switch (err) {
3723 case 0:
3724 return err;
3725 case -NFS4ERR_RESOURCE:
3726 /* The IBM lawyers misread another document! */
3727 case -NFS4ERR_DELAY:
3728 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3729 }
3730 } while (err == 0);
3731 return err;
3732}
3733
Trond Myklebustfe650402006-01-03 09:55:18 +01003734struct nfs4_delegreturndata {
3735 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003736 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003737 struct nfs_fh fh;
3738 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003739 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003740 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003741 int rpc_status;
3742};
3743
Trond Myklebustfe650402006-01-03 09:55:18 +01003744static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3745{
3746 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003747
Trond Myklebust14516c32010-07-31 14:29:06 -04003748 if (!nfs4_sequence_done(task, &data->res.seq_res))
3749 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003750
Ricardo Labiaga79708862009-12-07 09:23:21 -05003751 switch (task->tk_status) {
3752 case -NFS4ERR_STALE_STATEID:
3753 case -NFS4ERR_EXPIRED:
3754 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003755 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003756 break;
3757 default:
3758 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3759 -EAGAIN) {
3760 nfs_restart_rpc(task, data->res.server->nfs_client);
3761 return;
3762 }
3763 }
3764 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003765}
3766
3767static void nfs4_delegreturn_release(void *calldata)
3768{
Trond Myklebustfe650402006-01-03 09:55:18 +01003769 kfree(calldata);
3770}
3771
Andy Adamson938e1012009-04-01 09:22:28 -04003772#if defined(CONFIG_NFS_V4_1)
3773static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3774{
3775 struct nfs4_delegreturndata *d_data;
3776
3777 d_data = (struct nfs4_delegreturndata *)data;
3778
Trond Myklebust035168a2010-06-16 09:52:26 -04003779 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003780 &d_data->args.seq_args,
3781 &d_data->res.seq_res, 1, task))
3782 return;
3783 rpc_call_start(task);
3784}
3785#endif /* CONFIG_NFS_V4_1 */
3786
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003787static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003788#if defined(CONFIG_NFS_V4_1)
3789 .rpc_call_prepare = nfs4_delegreturn_prepare,
3790#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003791 .rpc_call_done = nfs4_delegreturn_done,
3792 .rpc_release = nfs4_delegreturn_release,
3793};
3794
Trond Myklebuste6f81072008-01-24 18:14:34 -05003795static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003796{
3797 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003798 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003799 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003800 struct rpc_message msg = {
3801 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3802 .rpc_cred = cred,
3803 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003804 struct rpc_task_setup task_setup_data = {
3805 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003806 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003807 .callback_ops = &nfs4_delegreturn_ops,
3808 .flags = RPC_TASK_ASYNC,
3809 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003810 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003811
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003812 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003813 if (data == NULL)
3814 return -ENOMEM;
3815 data->args.fhandle = &data->fh;
3816 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003817 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003818 nfs_copy_fh(&data->fh, NFS_FH(inode));
3819 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003820 data->res.fattr = &data->fattr;
3821 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003822 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003823 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003824 data->rpc_status = 0;
3825
Trond Myklebustc970aa82007-07-14 15:39:59 -04003826 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05003827 msg.rpc_argp = &data->args;
3828 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003829 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003830 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003831 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003832 if (!issync)
3833 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003834 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003835 if (status != 0)
3836 goto out;
3837 status = data->rpc_status;
3838 if (status != 0)
3839 goto out;
3840 nfs_refresh_inode(inode, &data->fattr);
3841out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003842 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003843 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844}
3845
Trond Myklebuste6f81072008-01-24 18:14:34 -05003846int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847{
3848 struct nfs_server *server = NFS_SERVER(inode);
3849 struct nfs4_exception exception = { };
3850 int err;
3851 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003852 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 switch (err) {
3854 case -NFS4ERR_STALE_STATEID:
3855 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 case 0:
3857 return 0;
3858 }
3859 err = nfs4_handle_exception(server, err, &exception);
3860 } while (exception.retry);
3861 return err;
3862}
3863
3864#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3865#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3866
3867/*
3868 * sleep, with exponential backoff, and retry the LOCK operation.
3869 */
3870static unsigned long
3871nfs4_set_lock_task_retry(unsigned long timeout)
3872{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003873 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 timeout <<= 1;
3875 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3876 return NFS4_LOCK_MAXTIMEOUT;
3877 return timeout;
3878}
3879
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3881{
3882 struct inode *inode = state->inode;
3883 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003884 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003885 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003887 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003889 struct nfs_lockt_res res = {
3890 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 };
3892 struct rpc_message msg = {
3893 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3894 .rpc_argp = &arg,
3895 .rpc_resp = &res,
3896 .rpc_cred = state->owner->so_cred,
3897 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 struct nfs4_lock_state *lsp;
3899 int status;
3900
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003901 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003902 status = nfs4_set_lock_state(state, request);
3903 if (status != 0)
3904 goto out;
3905 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003906 arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05003907 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003908 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003909 switch (status) {
3910 case 0:
3911 request->fl_type = F_UNLCK;
3912 break;
3913 case -NFS4ERR_DENIED:
3914 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003916 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003917out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 return status;
3919}
3920
3921static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3922{
3923 struct nfs4_exception exception = { };
3924 int err;
3925
3926 do {
3927 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3928 _nfs4_proc_getlk(state, cmd, request),
3929 &exception);
3930 } while (exception.retry);
3931 return err;
3932}
3933
3934static int do_vfs_lock(struct file *file, struct file_lock *fl)
3935{
3936 int res = 0;
3937 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3938 case FL_POSIX:
3939 res = posix_lock_file_wait(file, fl);
3940 break;
3941 case FL_FLOCK:
3942 res = flock_lock_file_wait(file, fl);
3943 break;
3944 default:
3945 BUG();
3946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 return res;
3948}
3949
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003950struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003951 struct nfs_locku_args arg;
3952 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003953 struct nfs4_lock_state *lsp;
3954 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003955 struct file_lock fl;
3956 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003957 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003958};
3959
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003960static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3961 struct nfs_open_context *ctx,
3962 struct nfs4_lock_state *lsp,
3963 struct nfs_seqid *seqid)
3964{
3965 struct nfs4_unlockdata *p;
3966 struct inode *inode = lsp->ls_state->inode;
3967
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003968 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003969 if (p == NULL)
3970 return NULL;
3971 p->arg.fh = NFS_FH(inode);
3972 p->arg.fl = &p->fl;
3973 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003974 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003975 p->arg.stateid = &lsp->ls_stateid;
3976 p->lsp = lsp;
3977 atomic_inc(&lsp->ls_count);
3978 /* Ensure we don't close file until we're done freeing locks! */
3979 p->ctx = get_nfs_open_context(ctx);
3980 memcpy(&p->fl, fl, sizeof(p->fl));
3981 p->server = NFS_SERVER(inode);
3982 return p;
3983}
3984
Trond Myklebust06f814a2006-01-03 09:55:07 +01003985static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003986{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003987 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003988 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003989 nfs4_put_lock_state(calldata->lsp);
3990 put_nfs_open_context(calldata->ctx);
3991 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003992}
3993
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003994static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003995{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003996 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003997
Trond Myklebust14516c32010-07-31 14:29:06 -04003998 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3999 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004000 switch (task->tk_status) {
4001 case 0:
4002 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004003 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004004 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01004005 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004006 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004007 case -NFS4ERR_BAD_STATEID:
4008 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004009 case -NFS4ERR_STALE_STATEID:
4010 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004011 break;
4012 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004013 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebust0110ee12009-12-07 09:00:24 -05004014 nfs_restart_rpc(task,
Trond Myklebustd61e6122009-12-05 19:32:19 -05004015 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004016 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004017}
4018
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004019static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004020{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004021 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004023 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004024 return;
4025 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004026 /* Note: exit _without_ running nfs4_locku_done */
4027 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004028 return;
4029 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004030 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004031 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004032 &calldata->arg.seq_args,
4033 &calldata->res.seq_res, 1, task))
4034 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004035 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036}
4037
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004038static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004039 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004040 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004041 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004042};
4043
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004044static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4045 struct nfs_open_context *ctx,
4046 struct nfs4_lock_state *lsp,
4047 struct nfs_seqid *seqid)
4048{
4049 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004050 struct rpc_message msg = {
4051 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4052 .rpc_cred = ctx->cred,
4053 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004054 struct rpc_task_setup task_setup_data = {
4055 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004056 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004057 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004058 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004059 .flags = RPC_TASK_ASYNC,
4060 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004061
Frank Filz137d6ac2007-07-09 15:32:29 -07004062 /* Ensure this is an unlock - when canceling a lock, the
4063 * canceled lock is passed in, and it won't be an unlock.
4064 */
4065 fl->fl_type = F_UNLCK;
4066
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004067 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4068 if (data == NULL) {
4069 nfs_free_seqid(seqid);
4070 return ERR_PTR(-ENOMEM);
4071 }
4072
Trond Myklebust1174dd12010-12-21 10:52:24 -05004073 msg.rpc_argp = &data->arg;
4074 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004075 task_setup_data.callback_data = data;
4076 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004077}
4078
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4080{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004081 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004082 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004083 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004084 struct rpc_task *task;
4085 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004086 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087
Trond Myklebust9b073572006-06-29 16:38:34 -04004088 status = nfs4_set_lock_state(state, request);
4089 /* Unlock _before_ we do the RPC call */
4090 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004091 down_read(&nfsi->rwsem);
4092 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4093 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004094 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004095 }
4096 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004097 if (status != 0)
4098 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004099 /* Is this a delegated lock? */
4100 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004101 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004102 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004103 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004104 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004105 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004106 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004107 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004108 status = PTR_ERR(task);
4109 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004110 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004111 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004112 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004113out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004114 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004115 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116}
4117
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004118struct nfs4_lockdata {
4119 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004120 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004121 struct nfs4_lock_state *lsp;
4122 struct nfs_open_context *ctx;
4123 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004124 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004125 int rpc_status;
4126 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004127 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004128};
4129
4130static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004131 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4132 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004133{
4134 struct nfs4_lockdata *p;
4135 struct inode *inode = lsp->ls_state->inode;
4136 struct nfs_server *server = NFS_SERVER(inode);
4137
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004138 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004139 if (p == NULL)
4140 return NULL;
4141
4142 p->arg.fh = NFS_FH(inode);
4143 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004144 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004145 if (p->arg.open_seqid == NULL)
4146 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004147 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004148 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004149 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004150 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004151 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004152 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004153 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004154 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004155 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004156 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004157 atomic_inc(&lsp->ls_count);
4158 p->ctx = get_nfs_open_context(ctx);
4159 memcpy(&p->fl, fl, sizeof(p->fl));
4160 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004161out_free_seqid:
4162 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004163out_free:
4164 kfree(p);
4165 return NULL;
4166}
4167
4168static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4169{
4170 struct nfs4_lockdata *data = calldata;
4171 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172
Harvey Harrison3110ff82008-05-02 13:42:44 -07004173 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004174 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4175 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004176 /* Do we need to do an open_to_lock_owner? */
4177 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004178 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4179 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004180 data->arg.open_stateid = &state->stateid;
4181 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004182 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004183 } else
4184 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004185 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004186 if (nfs4_setup_sequence(data->server,
4187 &data->arg.seq_args,
Andy Adamson66179ef2009-04-01 09:22:22 -04004188 &data->res.seq_res, 1, task))
4189 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004190 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004191 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004192}
4193
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004194static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4195{
4196 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4197 nfs4_lock_prepare(task, calldata);
4198}
4199
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004200static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4201{
4202 struct nfs4_lockdata *data = calldata;
4203
Harvey Harrison3110ff82008-05-02 13:42:44 -07004204 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004205
Trond Myklebust14516c32010-07-31 14:29:06 -04004206 if (!nfs4_sequence_done(task, &data->res.seq_res))
4207 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004208
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004209 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004210 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004211 if (data->rpc_status == 0)
4212 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4213 else
4214 goto out;
4215 }
4216 if (data->rpc_status == 0) {
4217 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4218 sizeof(data->lsp->ls_stateid.data));
4219 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04004220 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004221 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004222out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004223 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004224}
4225
4226static void nfs4_lock_release(void *calldata)
4227{
4228 struct nfs4_lockdata *data = calldata;
4229
Harvey Harrison3110ff82008-05-02 13:42:44 -07004230 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004231 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004232 if (data->cancelled != 0) {
4233 struct rpc_task *task;
4234 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4235 data->arg.lock_seqid);
4236 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004237 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004238 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004239 } else
4240 nfs_free_seqid(data->arg.lock_seqid);
4241 nfs4_put_lock_state(data->lsp);
4242 put_nfs_open_context(data->ctx);
4243 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004244 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004245}
4246
4247static const struct rpc_call_ops nfs4_lock_ops = {
4248 .rpc_call_prepare = nfs4_lock_prepare,
4249 .rpc_call_done = nfs4_lock_done,
4250 .rpc_release = nfs4_lock_release,
4251};
4252
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004253static const struct rpc_call_ops nfs4_recover_lock_ops = {
4254 .rpc_call_prepare = nfs4_recover_lock_prepare,
4255 .rpc_call_done = nfs4_lock_done,
4256 .rpc_release = nfs4_lock_release,
4257};
4258
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004259static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4260{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004261 switch (error) {
4262 case -NFS4ERR_ADMIN_REVOKED:
4263 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004264 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004265 if (new_lock_owner != 0 ||
4266 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004267 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004268 break;
4269 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004270 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004271 case -NFS4ERR_EXPIRED:
4272 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004273 };
4274}
4275
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004276static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004277{
4278 struct nfs4_lockdata *data;
4279 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004280 struct rpc_message msg = {
4281 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4282 .rpc_cred = state->owner->so_cred,
4283 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004284 struct rpc_task_setup task_setup_data = {
4285 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004286 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004287 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004288 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004289 .flags = RPC_TASK_ASYNC,
4290 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004291 int ret;
4292
Harvey Harrison3110ff82008-05-02 13:42:44 -07004293 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004294 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004295 fl->fl_u.nfs4_fl.owner,
4296 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004297 if (data == NULL)
4298 return -ENOMEM;
4299 if (IS_SETLKW(cmd))
4300 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004301 if (recovery_type > NFS_LOCK_NEW) {
4302 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004303 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004304 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4305 }
Trond Myklebust1174dd12010-12-21 10:52:24 -05004306 msg.rpc_argp = &data->arg;
4307 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004308 task_setup_data.callback_data = data;
4309 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004310 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004311 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004312 ret = nfs4_wait_for_completion_rpc_task(task);
4313 if (ret == 0) {
4314 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004315 if (ret)
4316 nfs4_handle_setlk_error(data->server, data->lsp,
4317 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004318 } else
4319 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004320 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004321 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004322 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323}
4324
4325static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4326{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004327 struct nfs_server *server = NFS_SERVER(state->inode);
4328 struct nfs4_exception exception = { };
4329 int err;
4330
4331 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004332 /* Cache the lock if possible... */
4333 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4334 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004335 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004336 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004337 break;
4338 nfs4_handle_exception(server, err, &exception);
4339 } while (exception.retry);
4340 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341}
4342
4343static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4344{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004345 struct nfs_server *server = NFS_SERVER(state->inode);
4346 struct nfs4_exception exception = { };
4347 int err;
4348
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004349 err = nfs4_set_lock_state(state, request);
4350 if (err != 0)
4351 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004352 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004353 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4354 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004355 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004356 switch (err) {
4357 default:
4358 goto out;
4359 case -NFS4ERR_GRACE:
4360 case -NFS4ERR_DELAY:
4361 nfs4_handle_exception(server, err, &exception);
4362 err = 0;
4363 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004364 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004365out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004366 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367}
4368
4369static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4370{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004371 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004372 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004373 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004375 if ((fl_flags & FL_POSIX) &&
4376 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4377 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004378 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004379 status = nfs4_set_lock_state(state, request);
4380 if (status != 0)
4381 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004382 request->fl_flags |= FL_ACCESS;
4383 status = do_vfs_lock(request->fl_file, request);
4384 if (status < 0)
4385 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004386 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004387 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004388 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004389 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004390 request->fl_flags = fl_flags & ~FL_SLEEP;
4391 status = do_vfs_lock(request->fl_file, request);
4392 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004393 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004394 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004395 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004396 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004397 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004398 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004399 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004400 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004401out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004402 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004403out:
4404 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 return status;
4406}
4407
4408static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4409{
4410 struct nfs4_exception exception = { };
4411 int err;
4412
4413 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004414 err = _nfs4_proc_setlk(state, cmd, request);
4415 if (err == -NFS4ERR_DENIED)
4416 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004418 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 } while (exception.retry);
4420 return err;
4421}
4422
4423static int
4424nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4425{
4426 struct nfs_open_context *ctx;
4427 struct nfs4_state *state;
4428 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4429 int status;
4430
4431 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004432 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 state = ctx->state;
4434
4435 if (request->fl_start < 0 || request->fl_end < 0)
4436 return -EINVAL;
4437
Trond Myklebustd9531262009-07-21 19:22:38 -04004438 if (IS_GETLK(cmd)) {
4439 if (state != NULL)
4440 return nfs4_proc_getlk(state, F_GETLK, request);
4441 return 0;
4442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443
4444 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4445 return -EINVAL;
4446
Trond Myklebustd9531262009-07-21 19:22:38 -04004447 if (request->fl_type == F_UNLCK) {
4448 if (state != NULL)
4449 return nfs4_proc_unlck(state, cmd, request);
4450 return 0;
4451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452
Trond Myklebustd9531262009-07-21 19:22:38 -04004453 if (state == NULL)
4454 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 do {
4456 status = nfs4_proc_setlk(state, cmd, request);
4457 if ((status != -EAGAIN) || IS_SETLK(cmd))
4458 break;
4459 timeout = nfs4_set_lock_task_retry(timeout);
4460 status = -ERESTARTSYS;
4461 if (signalled())
4462 break;
4463 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 return status;
4465}
4466
Trond Myklebust888e6942005-11-04 15:38:11 -05004467int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4468{
4469 struct nfs_server *server = NFS_SERVER(state->inode);
4470 struct nfs4_exception exception = { };
4471 int err;
4472
4473 err = nfs4_set_lock_state(state, fl);
4474 if (err != 0)
4475 goto out;
4476 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004477 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004478 switch (err) {
4479 default:
4480 printk(KERN_ERR "%s: unhandled error %d.\n",
4481 __func__, err);
4482 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004483 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004484 goto out;
4485 case -NFS4ERR_EXPIRED:
4486 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004487 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004488 nfs4_schedule_lease_recovery(server->nfs_client);
4489 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004490 case -NFS4ERR_BADSESSION:
4491 case -NFS4ERR_BADSLOT:
4492 case -NFS4ERR_BAD_HIGH_SLOT:
4493 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4494 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004495 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004496 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004497 case -ERESTARTSYS:
4498 /*
4499 * The show must go on: exit, but mark the
4500 * stateid as needing recovery.
4501 */
4502 case -NFS4ERR_ADMIN_REVOKED:
4503 case -NFS4ERR_BAD_STATEID:
4504 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004505 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004506 err = 0;
4507 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004508 case -EKEYEXPIRED:
4509 /*
4510 * User RPCSEC_GSS context has expired.
4511 * We cannot recover this stateid now, so
4512 * skip it and allow recovery thread to
4513 * proceed.
4514 */
4515 err = 0;
4516 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004517 case -ENOMEM:
4518 case -NFS4ERR_DENIED:
4519 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4520 err = 0;
4521 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004522 case -NFS4ERR_DELAY:
4523 break;
4524 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004525 err = nfs4_handle_exception(server, err, &exception);
4526 } while (exception.retry);
4527out:
4528 return err;
4529}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004530
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004531static void nfs4_release_lockowner_release(void *calldata)
4532{
4533 kfree(calldata);
4534}
4535
4536const struct rpc_call_ops nfs4_release_lockowner_ops = {
4537 .rpc_release = nfs4_release_lockowner_release,
4538};
4539
4540void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4541{
4542 struct nfs_server *server = lsp->ls_state->owner->so_server;
4543 struct nfs_release_lockowner_args *args;
4544 struct rpc_message msg = {
4545 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4546 };
4547
4548 if (server->nfs_client->cl_mvops->minor_version != 0)
4549 return;
4550 args = kmalloc(sizeof(*args), GFP_NOFS);
4551 if (!args)
4552 return;
4553 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4554 args->lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004555 args->lock_owner.s_dev = server->s_dev;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004556 msg.rpc_argp = args;
4557 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4558}
4559
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004560#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4561
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004562static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4563 const void *buf, size_t buflen,
4564 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004565{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004566 if (strcmp(key, "") != 0)
4567 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004568
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004569 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004570}
4571
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004572static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4573 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004574{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004575 if (strcmp(key, "") != 0)
4576 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004577
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004578 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004579}
4580
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004581static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4582 size_t list_len, const char *name,
4583 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004584{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004585 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004586
J. Bruce Fields096455a2006-03-20 23:23:42 -05004587 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4588 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004589
4590 if (list && len <= list_len)
4591 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004592 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004593}
4594
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004595static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4596{
4597 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4598 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4599 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4600 return;
4601
4602 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4603 NFS_ATTR_FATTR_NLINK;
4604 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4605 fattr->nlink = 2;
4606}
4607
Trond Myklebust56659e92007-07-17 21:52:39 -04004608int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004609 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004610{
4611 struct nfs_server *server = NFS_SERVER(dir);
4612 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004613 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4614 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004615 };
4616 struct nfs4_fs_locations_arg args = {
4617 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004618 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004619 .page = page,
4620 .bitmask = bitmask,
4621 };
Benny Halevy22958462009-04-01 09:22:02 -04004622 struct nfs4_fs_locations_res res = {
4623 .fs_locations = fs_locations,
4624 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004625 struct rpc_message msg = {
4626 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4627 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004628 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004629 };
4630 int status;
4631
Harvey Harrison3110ff82008-05-02 13:42:44 -07004632 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004633 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004634 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004635 fs_locations->nlocations = 0;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004636 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004637 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004638 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004639 return status;
4640}
4641
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00004642static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4643{
4644 int status;
4645 struct nfs4_secinfo_arg args = {
4646 .dir_fh = NFS_FH(dir),
4647 .name = name,
4648 };
4649 struct nfs4_secinfo_res res = {
4650 .flavors = flavors,
4651 };
4652 struct rpc_message msg = {
4653 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
4654 .rpc_argp = &args,
4655 .rpc_resp = &res,
4656 };
4657
4658 dprintk("NFS call secinfo %s\n", name->name);
4659 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4660 dprintk("NFS reply secinfo: %d\n", status);
4661 return status;
4662}
4663
4664int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4665{
4666 struct nfs4_exception exception = { };
4667 int err;
4668 do {
4669 err = nfs4_handle_exception(NFS_SERVER(dir),
4670 _nfs4_proc_secinfo(dir, name, flavors),
4671 &exception);
4672 } while (exception.retry);
4673 return err;
4674}
4675
Andy Adamson557134a2009-04-01 09:21:53 -04004676#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004677/*
Andy Adamson357f54d2010-12-14 10:11:57 -05004678 * Check the exchange flags returned by the server for invalid flags, having
4679 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4680 * DS flags set.
4681 */
4682static int nfs4_check_cl_exchange_flags(u32 flags)
4683{
4684 if (flags & ~EXCHGID4_FLAG_MASK_R)
4685 goto out_inval;
4686 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4687 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4688 goto out_inval;
4689 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4690 goto out_inval;
4691 return NFS_OK;
4692out_inval:
4693 return -NFS4ERR_INVAL;
4694}
4695
4696/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04004697 * nfs4_proc_exchange_id()
4698 *
4699 * Since the clientid has expired, all compounds using sessions
4700 * associated with the stale clientid will be returning
4701 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4702 * be in some phase of session reset.
4703 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004704int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004705{
4706 nfs4_verifier verifier;
4707 struct nfs41_exchange_id_args args = {
4708 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05004709 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04004710 };
4711 struct nfs41_exchange_id_res res = {
4712 .client = clp,
4713 };
4714 int status;
4715 struct rpc_message msg = {
4716 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4717 .rpc_argp = &args,
4718 .rpc_resp = &res,
4719 .rpc_cred = cred,
4720 };
4721 __be32 *p;
4722
4723 dprintk("--> %s\n", __func__);
4724 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004725
Benny Halevy99fe60d2009-04-01 09:22:29 -04004726 p = (u32 *)verifier.data;
4727 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4728 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4729 args.verifier = &verifier;
4730
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004731 args.id_len = scnprintf(args.id, sizeof(args.id),
4732 "%s/%s.%s/%u",
4733 clp->cl_ipaddr,
4734 init_utsname()->nodename,
4735 init_utsname()->domainname,
4736 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004737
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004738 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4739 if (!status)
4740 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004741 dprintk("<-- %s status= %d\n", __func__, status);
4742 return status;
4743}
4744
Andy Adamson2050f0c2009-04-01 09:22:30 -04004745struct nfs4_get_lease_time_data {
4746 struct nfs4_get_lease_time_args *args;
4747 struct nfs4_get_lease_time_res *res;
4748 struct nfs_client *clp;
4749};
4750
4751static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4752 void *calldata)
4753{
4754 int ret;
4755 struct nfs4_get_lease_time_data *data =
4756 (struct nfs4_get_lease_time_data *)calldata;
4757
4758 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004759 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004760 /* just setup sequence, do not trigger session recovery
4761 since we're invoked within one */
4762 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004763 &data->args->la_seq_args,
4764 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004765
4766 BUG_ON(ret == -EAGAIN);
4767 rpc_call_start(task);
4768 dprintk("<-- %s\n", __func__);
4769}
4770
4771/*
4772 * Called from nfs4_state_manager thread for session setup, so don't recover
4773 * from sequence operation or clientid errors.
4774 */
4775static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4776{
4777 struct nfs4_get_lease_time_data *data =
4778 (struct nfs4_get_lease_time_data *)calldata;
4779
4780 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04004781 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4782 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04004783 switch (task->tk_status) {
4784 case -NFS4ERR_DELAY:
4785 case -NFS4ERR_GRACE:
4786 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4787 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4788 task->tk_status = 0;
Trond Myklebust0110ee12009-12-07 09:00:24 -05004789 nfs_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004790 return;
4791 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004792 dprintk("<-- %s\n", __func__);
4793}
4794
4795struct rpc_call_ops nfs4_get_lease_time_ops = {
4796 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4797 .rpc_call_done = nfs4_get_lease_time_done,
4798};
4799
4800int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4801{
4802 struct rpc_task *task;
4803 struct nfs4_get_lease_time_args args;
4804 struct nfs4_get_lease_time_res res = {
4805 .lr_fsinfo = fsinfo,
4806 };
4807 struct nfs4_get_lease_time_data data = {
4808 .args = &args,
4809 .res = &res,
4810 .clp = clp,
4811 };
4812 struct rpc_message msg = {
4813 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4814 .rpc_argp = &args,
4815 .rpc_resp = &res,
4816 };
4817 struct rpc_task_setup task_setup = {
4818 .rpc_client = clp->cl_rpcclient,
4819 .rpc_message = &msg,
4820 .callback_ops = &nfs4_get_lease_time_ops,
4821 .callback_data = &data
4822 };
4823 int status;
4824
Andy Adamson2050f0c2009-04-01 09:22:30 -04004825 dprintk("--> %s\n", __func__);
4826 task = rpc_run_task(&task_setup);
4827
4828 if (IS_ERR(task))
4829 status = PTR_ERR(task);
4830 else {
4831 status = task->tk_status;
4832 rpc_put_task(task);
4833 }
4834 dprintk("<-- %s return %d\n", __func__, status);
4835
4836 return status;
4837}
4838
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004839/*
4840 * Reset a slot table
4841 */
Andy Adamson104aeba2010-01-14 17:45:10 -05004842static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4843 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004844{
Andy Adamson104aeba2010-01-14 17:45:10 -05004845 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004846 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004847 int ret = 0;
4848
Andy Adamson104aeba2010-01-14 17:45:10 -05004849 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4850 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004851
Andy Adamson104aeba2010-01-14 17:45:10 -05004852 /* Does the newly negotiated max_reqs match the existing slot table? */
4853 if (max_reqs != tbl->max_slots) {
4854 ret = -ENOMEM;
4855 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004856 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05004857 if (!new)
4858 goto out;
4859 ret = 0;
4860 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004861 }
4862 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05004863 if (new) {
4864 tbl->slots = new;
4865 tbl->max_slots = max_reqs;
4866 }
4867 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004868 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004869 spin_unlock(&tbl->slot_tbl_lock);
4870 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4871 tbl, tbl->slots, tbl->max_slots);
4872out:
4873 dprintk("<-- %s: return %d\n", __func__, ret);
4874 return ret;
4875}
4876
Andy Adamsonfc931582009-04-01 09:22:31 -04004877/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004878 * Reset the forechannel and backchannel slot tables
4879 */
4880static int nfs4_reset_slot_tables(struct nfs4_session *session)
4881{
4882 int status;
4883
4884 status = nfs4_reset_slot_table(&session->fc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004885 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004886 if (status)
4887 return status;
4888
4889 status = nfs4_reset_slot_table(&session->bc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004890 session->bc_attrs.max_reqs, 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004891 return status;
4892}
4893
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004894/* Destroy the slot table */
4895static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4896{
4897 if (session->fc_slot_table.slots != NULL) {
4898 kfree(session->fc_slot_table.slots);
4899 session->fc_slot_table.slots = NULL;
4900 }
4901 if (session->bc_slot_table.slots != NULL) {
4902 kfree(session->bc_slot_table.slots);
4903 session->bc_slot_table.slots = NULL;
4904 }
4905 return;
4906}
4907
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004908/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004909 * Initialize slot table
4910 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004911static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4912 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04004913{
Andy Adamsonfc931582009-04-01 09:22:31 -04004914 struct nfs4_slot *slot;
4915 int ret = -ENOMEM;
4916
4917 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4918
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004919 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04004920
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004921 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04004922 if (!slot)
4923 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04004924 ret = 0;
4925
4926 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04004927 tbl->max_slots = max_slots;
4928 tbl->slots = slot;
4929 tbl->highest_used_slotid = -1; /* no slot is currently used */
4930 spin_unlock(&tbl->slot_tbl_lock);
4931 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4932 tbl, tbl->slots, tbl->max_slots);
4933out:
4934 dprintk("<-- %s: return %d\n", __func__, ret);
4935 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04004936}
4937
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004938/*
4939 * Initialize the forechannel and backchannel tables
4940 */
4941static int nfs4_init_slot_tables(struct nfs4_session *session)
4942{
Trond Myklebustf26468f2009-12-05 19:32:11 -05004943 struct nfs4_slot_table *tbl;
4944 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004945
Trond Myklebustf26468f2009-12-05 19:32:11 -05004946 tbl = &session->fc_slot_table;
4947 if (tbl->slots == NULL) {
4948 status = nfs4_init_slot_table(tbl,
4949 session->fc_attrs.max_reqs, 1);
4950 if (status)
4951 return status;
4952 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004953
Trond Myklebustf26468f2009-12-05 19:32:11 -05004954 tbl = &session->bc_slot_table;
4955 if (tbl->slots == NULL) {
4956 status = nfs4_init_slot_table(tbl,
4957 session->bc_attrs.max_reqs, 0);
4958 if (status)
4959 nfs4_destroy_slot_tables(session);
4960 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004961
4962 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04004963}
4964
4965struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4966{
4967 struct nfs4_session *session;
4968 struct nfs4_slot_table *tbl;
4969
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004970 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04004971 if (!session)
4972 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04004973
Andy Adamson557134a2009-04-01 09:21:53 -04004974 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004975 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04004976 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004977 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00004978 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004979
4980 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004981 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004982 spin_lock_init(&tbl->slot_tbl_lock);
4983 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00004984 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004985
Trond Myklebust1055d762010-06-16 09:52:27 -04004986 session->session_state = 1<<NFS4_SESSION_INITING;
4987
Andy Adamson557134a2009-04-01 09:21:53 -04004988 session->clp = clp;
4989 return session;
4990}
4991
4992void nfs4_destroy_session(struct nfs4_session *session)
4993{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04004994 nfs4_proc_destroy_session(session);
4995 dprintk("%s Destroy backchannel for xprt %p\n",
4996 __func__, session->clp->cl_rpcclient->cl_xprt);
4997 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4998 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004999 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005000 kfree(session);
5001}
5002
Andy Adamsonfc931582009-04-01 09:22:31 -04005003/*
5004 * Initialize the values to be used by the client in CREATE_SESSION
5005 * If nfs4_init_session set the fore channel request and response sizes,
5006 * use them.
5007 *
5008 * Set the back channel max_resp_sz_cached to zero to force the client to
5009 * always set csa_cachethis to FALSE because the current implementation
5010 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5011 */
5012static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5013{
5014 struct nfs4_session *session = args->client->cl_session;
5015 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5016 mxresp_sz = session->fc_attrs.max_resp_sz;
5017
5018 if (mxrqst_sz == 0)
5019 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5020 if (mxresp_sz == 0)
5021 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5022 /* Fore channel attributes */
5023 args->fc_attrs.headerpadsz = 0;
5024 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5025 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005026 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5027 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
5028
5029 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005030 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005031 __func__,
5032 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005033 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005034
5035 /* Back channel attributes */
5036 args->bc_attrs.headerpadsz = 0;
5037 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5038 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5039 args->bc_attrs.max_resp_sz_cached = 0;
5040 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5041 args->bc_attrs.max_reqs = 1;
5042
5043 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5044 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5045 __func__,
5046 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5047 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5048 args->bc_attrs.max_reqs);
5049}
5050
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005051static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005052{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005053 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5054 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5055
5056 if (rcvd->headerpadsz > sent->headerpadsz)
5057 return -EINVAL;
5058 if (rcvd->max_resp_sz > sent->max_resp_sz)
5059 return -EINVAL;
5060 /*
5061 * Our requested max_ops is the minimum we need; we're not
5062 * prepared to break up compounds into smaller pieces than that.
5063 * So, no point even trying to continue if the server won't
5064 * cooperate:
5065 */
5066 if (rcvd->max_ops < sent->max_ops)
5067 return -EINVAL;
5068 if (rcvd->max_reqs == 0)
5069 return -EINVAL;
5070 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005071}
5072
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005073static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5074{
5075 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5076 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005077
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005078 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5079 return -EINVAL;
5080 if (rcvd->max_resp_sz < sent->max_resp_sz)
5081 return -EINVAL;
5082 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5083 return -EINVAL;
5084 /* These would render the backchannel useless: */
5085 if (rcvd->max_ops == 0)
5086 return -EINVAL;
5087 if (rcvd->max_reqs == 0)
5088 return -EINVAL;
5089 return 0;
5090}
Andy Adamson8d353012009-04-01 09:22:32 -04005091
Andy Adamson8d353012009-04-01 09:22:32 -04005092static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5093 struct nfs4_session *session)
5094{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005095 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005096
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005097 ret = nfs4_verify_fore_channel_attrs(args, session);
5098 if (ret)
5099 return ret;
5100 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005101}
5102
Andy Adamsonfc931582009-04-01 09:22:31 -04005103static int _nfs4_proc_create_session(struct nfs_client *clp)
5104{
5105 struct nfs4_session *session = clp->cl_session;
5106 struct nfs41_create_session_args args = {
5107 .client = clp,
5108 .cb_program = NFS4_CALLBACK,
5109 };
5110 struct nfs41_create_session_res res = {
5111 .client = clp,
5112 };
5113 struct rpc_message msg = {
5114 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5115 .rpc_argp = &args,
5116 .rpc_resp = &res,
5117 };
5118 int status;
5119
5120 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005121 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005122
5123 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5124
Andy Adamson8d353012009-04-01 09:22:32 -04005125 if (!status)
5126 /* Verify the session's negotiated channel_attrs values */
5127 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005128 if (!status) {
5129 /* Increment the clientid slot sequence id */
5130 clp->cl_seqid++;
5131 }
5132
5133 return status;
5134}
5135
5136/*
5137 * Issues a CREATE_SESSION operation to the server.
5138 * It is the responsibility of the caller to verify the session is
5139 * expired before calling this routine.
5140 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005141int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005142{
5143 int status;
5144 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005145 struct nfs4_session *session = clp->cl_session;
Ricardo Labiaga7d6d63d2011-03-09 13:13:44 -05005146 long timeout = 0;
5147 int err;
Andy Adamsonfc931582009-04-01 09:22:31 -04005148
5149 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5150
Ricardo Labiaga7d6d63d2011-03-09 13:13:44 -05005151 do {
5152 status = _nfs4_proc_create_session(clp);
5153 if (status == -NFS4ERR_DELAY) {
5154 err = nfs4_delay(clp->cl_rpcclient, &timeout);
5155 if (err)
5156 status = err;
5157 }
5158 } while (status == -NFS4ERR_DELAY);
5159
Andy Adamsonfc931582009-04-01 09:22:31 -04005160 if (status)
5161 goto out;
5162
Trond Myklebustf26468f2009-12-05 19:32:11 -05005163 /* Init and reset the fore channel */
5164 status = nfs4_init_slot_tables(session);
5165 dprintk("slot table initialization returned %d\n", status);
5166 if (status)
5167 goto out;
5168 status = nfs4_reset_slot_tables(session);
5169 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005170 if (status)
5171 goto out;
5172
5173 ptr = (unsigned *)&session->sess_id.data[0];
5174 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5175 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005176out:
5177 dprintk("<-- %s\n", __func__);
5178 return status;
5179}
5180
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005181/*
5182 * Issue the over-the-wire RPC DESTROY_SESSION.
5183 * The caller must serialize access to this routine.
5184 */
5185int nfs4_proc_destroy_session(struct nfs4_session *session)
5186{
5187 int status = 0;
5188 struct rpc_message msg;
5189
5190 dprintk("--> nfs4_proc_destroy_session\n");
5191
5192 /* session is still being setup */
5193 if (session->clp->cl_cons_state != NFS_CS_READY)
5194 return status;
5195
5196 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5197 msg.rpc_argp = session;
5198 msg.rpc_resp = NULL;
5199 msg.rpc_cred = NULL;
5200 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5201
5202 if (status)
5203 printk(KERN_WARNING
5204 "Got error %d from the server on DESTROY_SESSION. "
5205 "Session has been destroyed regardless...\n", status);
5206
5207 dprintk("<-- nfs4_proc_destroy_session\n");
5208 return status;
5209}
5210
Trond Myklebustfccba802009-07-21 16:48:07 -04005211int nfs4_init_session(struct nfs_server *server)
5212{
5213 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005214 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005215 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005216 int ret;
5217
5218 if (!nfs4_has_session(clp))
5219 return 0;
5220
Trond Myklebust1055d762010-06-16 09:52:27 -04005221 session = clp->cl_session;
5222 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5223 return 0;
5224
Andy Adamson68bf05e2009-12-15 12:55:02 -05005225 rsize = server->rsize;
5226 if (rsize == 0)
5227 rsize = NFS_MAX_FILE_IO_SIZE;
5228 wsize = server->wsize;
5229 if (wsize == 0)
5230 wsize = NFS_MAX_FILE_IO_SIZE;
5231
Andy Adamson68bf05e2009-12-15 12:55:02 -05005232 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5233 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005234
Trond Myklebustfccba802009-07-21 16:48:07 -04005235 ret = nfs4_recover_expired_lease(server);
5236 if (!ret)
5237 ret = nfs4_check_client_ready(clp);
5238 return ret;
5239}
5240
Andy Adamsond83217c2011-03-01 01:34:17 +00005241int nfs4_init_ds_session(struct nfs_client *clp)
5242{
5243 struct nfs4_session *session = clp->cl_session;
5244 int ret;
5245
5246 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5247 return 0;
5248
5249 ret = nfs4_client_recover_expired_lease(clp);
5250 if (!ret)
5251 /* Test for the DS role */
5252 if (!is_ds_client(clp))
5253 ret = -ENODEV;
5254 if (!ret)
5255 ret = nfs4_check_client_ready(clp);
5256 return ret;
5257
5258}
5259EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5260
5261
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005262/*
5263 * Renew the cl_session lease.
5264 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005265struct nfs4_sequence_data {
5266 struct nfs_client *clp;
5267 struct nfs4_sequence_args args;
5268 struct nfs4_sequence_res res;
5269};
5270
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005271static void nfs41_sequence_release(void *data)
5272{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005273 struct nfs4_sequence_data *calldata = data;
5274 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005275
Alexandros Batsakis71358402010-02-05 03:45:05 -08005276 if (atomic_read(&clp->cl_count) > 1)
5277 nfs4_schedule_state_renewal(clp);
5278 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005279 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005280}
5281
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005282static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5283{
5284 switch(task->tk_status) {
5285 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005286 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5287 return -EAGAIN;
5288 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005289 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005290 }
5291 return 0;
5292}
5293
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005294static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005295{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005296 struct nfs4_sequence_data *calldata = data;
5297 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005298
Trond Myklebust14516c32010-07-31 14:29:06 -04005299 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5300 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005301
5302 if (task->tk_status < 0) {
5303 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005304 if (atomic_read(&clp->cl_count) == 1)
5305 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005306
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005307 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5308 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005309 return;
5310 }
5311 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005312 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005313out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005314 dprintk("<-- %s\n", __func__);
5315}
5316
5317static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5318{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005319 struct nfs4_sequence_data *calldata = data;
5320 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005321 struct nfs4_sequence_args *args;
5322 struct nfs4_sequence_res *res;
5323
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005324 args = task->tk_msg.rpc_argp;
5325 res = task->tk_msg.rpc_resp;
5326
Trond Myklebust035168a2010-06-16 09:52:26 -04005327 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005328 return;
5329 rpc_call_start(task);
5330}
5331
5332static const struct rpc_call_ops nfs41_sequence_ops = {
5333 .rpc_call_done = nfs41_sequence_call_done,
5334 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005335 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005336};
5337
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005338static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005339{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005340 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005341 struct rpc_message msg = {
5342 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5343 .rpc_cred = cred,
5344 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005345 struct rpc_task_setup task_setup_data = {
5346 .rpc_client = clp->cl_rpcclient,
5347 .rpc_message = &msg,
5348 .callback_ops = &nfs41_sequence_ops,
5349 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5350 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005351
Alexandros Batsakis71358402010-02-05 03:45:05 -08005352 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005353 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005354 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005355 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005356 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005357 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005358 }
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005359 msg.rpc_argp = &calldata->args;
5360 msg.rpc_resp = &calldata->res;
5361 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005362 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005363
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005364 return rpc_run_task(&task_setup_data);
5365}
5366
5367static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5368{
5369 struct rpc_task *task;
5370 int ret = 0;
5371
5372 task = _nfs41_proc_sequence(clp, cred);
5373 if (IS_ERR(task))
5374 ret = PTR_ERR(task);
5375 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005376 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005377 dprintk("<-- %s status=%d\n", __func__, ret);
5378 return ret;
5379}
5380
5381static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5382{
5383 struct rpc_task *task;
5384 int ret;
5385
5386 task = _nfs41_proc_sequence(clp, cred);
5387 if (IS_ERR(task)) {
5388 ret = PTR_ERR(task);
5389 goto out;
5390 }
5391 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005392 if (!ret) {
5393 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5394
5395 if (task->tk_status == 0)
5396 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005397 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005398 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005399 rpc_put_task(task);
5400out:
5401 dprintk("<-- %s status=%d\n", __func__, ret);
5402 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005403}
5404
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005405struct nfs4_reclaim_complete_data {
5406 struct nfs_client *clp;
5407 struct nfs41_reclaim_complete_args arg;
5408 struct nfs41_reclaim_complete_res res;
5409};
5410
5411static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5412{
5413 struct nfs4_reclaim_complete_data *calldata = data;
5414
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005415 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005416 if (nfs41_setup_sequence(calldata->clp->cl_session,
5417 &calldata->arg.seq_args,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005418 &calldata->res.seq_res, 0, task))
5419 return;
5420
5421 rpc_call_start(task);
5422}
5423
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005424static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5425{
5426 switch(task->tk_status) {
5427 case 0:
5428 case -NFS4ERR_COMPLETE_ALREADY:
5429 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5430 break;
5431 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005432 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5433 return -EAGAIN;
5434 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005435 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005436 }
5437 return 0;
5438}
5439
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005440static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5441{
5442 struct nfs4_reclaim_complete_data *calldata = data;
5443 struct nfs_client *clp = calldata->clp;
5444 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5445
5446 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005447 if (!nfs41_sequence_done(task, res))
5448 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005449
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005450 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5451 rpc_restart_call_prepare(task);
5452 return;
5453 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005454 dprintk("<-- %s\n", __func__);
5455}
5456
5457static void nfs4_free_reclaim_complete_data(void *data)
5458{
5459 struct nfs4_reclaim_complete_data *calldata = data;
5460
5461 kfree(calldata);
5462}
5463
5464static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5465 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5466 .rpc_call_done = nfs4_reclaim_complete_done,
5467 .rpc_release = nfs4_free_reclaim_complete_data,
5468};
5469
5470/*
5471 * Issue a global reclaim complete.
5472 */
5473static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5474{
5475 struct nfs4_reclaim_complete_data *calldata;
5476 struct rpc_task *task;
5477 struct rpc_message msg = {
5478 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5479 };
5480 struct rpc_task_setup task_setup_data = {
5481 .rpc_client = clp->cl_rpcclient,
5482 .rpc_message = &msg,
5483 .callback_ops = &nfs4_reclaim_complete_call_ops,
5484 .flags = RPC_TASK_ASYNC,
5485 };
5486 int status = -ENOMEM;
5487
5488 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005489 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005490 if (calldata == NULL)
5491 goto out;
5492 calldata->clp = clp;
5493 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005494
5495 msg.rpc_argp = &calldata->arg;
5496 msg.rpc_resp = &calldata->res;
5497 task_setup_data.callback_data = calldata;
5498 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005499 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005500 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005501 goto out;
5502 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005503 status = nfs4_wait_for_completion_rpc_task(task);
5504 if (status == 0)
5505 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005506 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005507 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005508out:
5509 dprintk("<-- %s status=%d\n", __func__, status);
5510 return status;
5511}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005512
5513static void
5514nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5515{
5516 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005517 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005518
5519 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005520 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5521 * right now covering the LAYOUTGET we are about to send.
5522 * However, that is not so catastrophic, and there seems
5523 * to be no way to prevent it completely.
5524 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005525 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5526 &lgp->res.seq_res, 0, task))
5527 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005528 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5529 NFS_I(lgp->args.inode)->layout,
5530 lgp->args.ctx->state)) {
5531 rpc_exit(task, NFS4_OK);
5532 return;
5533 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005534 rpc_call_start(task);
5535}
5536
5537static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5538{
5539 struct nfs4_layoutget *lgp = calldata;
5540 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5541
5542 dprintk("--> %s\n", __func__);
5543
5544 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5545 return;
5546
5547 switch (task->tk_status) {
5548 case 0:
5549 break;
5550 case -NFS4ERR_LAYOUTTRYLATER:
5551 case -NFS4ERR_RECALLCONFLICT:
5552 task->tk_status = -NFS4ERR_DELAY;
5553 /* Fall through */
5554 default:
5555 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5556 rpc_restart_call_prepare(task);
5557 return;
5558 }
5559 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005560 dprintk("<-- %s\n", __func__);
5561}
5562
5563static void nfs4_layoutget_release(void *calldata)
5564{
5565 struct nfs4_layoutget *lgp = calldata;
5566
5567 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005568 if (lgp->res.layout.buf != NULL)
5569 free_page((unsigned long) lgp->res.layout.buf);
5570 put_nfs_open_context(lgp->args.ctx);
5571 kfree(calldata);
5572 dprintk("<-- %s\n", __func__);
5573}
5574
5575static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5576 .rpc_call_prepare = nfs4_layoutget_prepare,
5577 .rpc_call_done = nfs4_layoutget_done,
5578 .rpc_release = nfs4_layoutget_release,
5579};
5580
5581int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5582{
5583 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5584 struct rpc_task *task;
5585 struct rpc_message msg = {
5586 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5587 .rpc_argp = &lgp->args,
5588 .rpc_resp = &lgp->res,
5589 };
5590 struct rpc_task_setup task_setup_data = {
5591 .rpc_client = server->client,
5592 .rpc_message = &msg,
5593 .callback_ops = &nfs4_layoutget_call_ops,
5594 .callback_data = lgp,
5595 .flags = RPC_TASK_ASYNC,
5596 };
5597 int status = 0;
5598
5599 dprintk("--> %s\n", __func__);
5600
5601 lgp->res.layout.buf = (void *)__get_free_page(GFP_NOFS);
5602 if (lgp->res.layout.buf == NULL) {
5603 nfs4_layoutget_release(lgp);
5604 return -ENOMEM;
5605 }
5606
5607 lgp->res.seq_res.sr_slot = NULL;
5608 task = rpc_run_task(&task_setup_data);
5609 if (IS_ERR(task))
5610 return PTR_ERR(task);
5611 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00005612 if (status == 0)
5613 status = task->tk_status;
5614 if (status == 0)
5615 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005616 rpc_put_task(task);
5617 dprintk("<-- %s status=%d\n", __func__, status);
5618 return status;
5619}
5620
5621static int
5622_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5623{
5624 struct nfs4_getdeviceinfo_args args = {
5625 .pdev = pdev,
5626 };
5627 struct nfs4_getdeviceinfo_res res = {
5628 .pdev = pdev,
5629 };
5630 struct rpc_message msg = {
5631 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5632 .rpc_argp = &args,
5633 .rpc_resp = &res,
5634 };
5635 int status;
5636
5637 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005638 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005639 dprintk("<-- %s status=%d\n", __func__, status);
5640
5641 return status;
5642}
5643
5644int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5645{
5646 struct nfs4_exception exception = { };
5647 int err;
5648
5649 do {
5650 err = nfs4_handle_exception(server,
5651 _nfs4_proc_getdeviceinfo(server, pdev),
5652 &exception);
5653 } while (exception.retry);
5654 return err;
5655}
5656EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5657
Andy Adamson557134a2009-04-01 09:21:53 -04005658#endif /* CONFIG_NFS_V4_1 */
5659
Andy Adamson591d71c2009-04-01 09:22:47 -04005660struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005661 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005662 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 .recover_open = nfs4_open_reclaim,
5664 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04005665 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005666 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667};
5668
Andy Adamson591d71c2009-04-01 09:22:47 -04005669#if defined(CONFIG_NFS_V4_1)
5670struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5671 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5672 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5673 .recover_open = nfs4_open_reclaim,
5674 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05005675 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005676 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005677 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04005678};
5679#endif /* CONFIG_NFS_V4_1 */
5680
5681struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005682 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005683 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684 .recover_open = nfs4_open_expired,
5685 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04005686 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005687 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688};
5689
Andy Adamson591d71c2009-04-01 09:22:47 -04005690#if defined(CONFIG_NFS_V4_1)
5691struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5692 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5693 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5694 .recover_open = nfs4_open_expired,
5695 .recover_lock = nfs4_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05005696 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005697 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04005698};
5699#endif /* CONFIG_NFS_V4_1 */
5700
Benny Halevy29fba382009-04-01 09:22:44 -04005701struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5702 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04005703 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005704 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04005705};
5706
5707#if defined(CONFIG_NFS_V4_1)
5708struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5709 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04005710 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005711 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04005712};
5713#endif
5714
Trond Myklebust97dc1352010-06-16 09:52:26 -04005715static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
5716 .minor_version = 0,
5717 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04005718 .validate_stateid = nfs4_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005719 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
5720 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
5721 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005722};
5723
5724#if defined(CONFIG_NFS_V4_1)
5725static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
5726 .minor_version = 1,
5727 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04005728 .validate_stateid = nfs41_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005729 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
5730 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
5731 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005732};
5733#endif
5734
Trond Myklebust97dc1352010-06-16 09:52:26 -04005735const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
5736 [0] = &nfs_v4_0_minor_ops,
5737#if defined(CONFIG_NFS_V4_1)
5738 [1] = &nfs_v4_1_minor_ops,
5739#endif
5740};
5741
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005742static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005743 .permission = nfs_permission,
5744 .getattr = nfs_getattr,
5745 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005746 .getxattr = generic_getxattr,
5747 .setxattr = generic_setxattr,
5748 .listxattr = generic_listxattr,
5749 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005750};
5751
David Howells509de812006-08-22 20:06:11 -04005752const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753 .version = 4, /* protocol version */
5754 .dentry_ops = &nfs4_dentry_operations,
5755 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005756 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757 .getroot = nfs4_proc_get_root,
5758 .getattr = nfs4_proc_getattr,
5759 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04005760 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761 .lookup = nfs4_proc_lookup,
5762 .access = nfs4_proc_access,
5763 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764 .create = nfs4_proc_create,
5765 .remove = nfs4_proc_remove,
5766 .unlink_setup = nfs4_proc_unlink_setup,
5767 .unlink_done = nfs4_proc_unlink_done,
5768 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04005769 .rename_setup = nfs4_proc_rename_setup,
5770 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771 .link = nfs4_proc_link,
5772 .symlink = nfs4_proc_symlink,
5773 .mkdir = nfs4_proc_mkdir,
5774 .rmdir = nfs4_proc_remove,
5775 .readdir = nfs4_proc_readdir,
5776 .mknod = nfs4_proc_mknod,
5777 .statfs = nfs4_proc_statfs,
5778 .fsinfo = nfs4_proc_fsinfo,
5779 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04005780 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781 .decode_dirent = nfs4_decode_dirent,
5782 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05005783 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005785 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005786 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005787 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005789 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04005790 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04005791 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00005792 .init_client = nfs4_init_client,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005793 .secinfo = nfs4_proc_secinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794};
5795
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005796static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
5797 .prefix = XATTR_NAME_NFSV4_ACL,
5798 .list = nfs4_xattr_list_nfs4_acl,
5799 .get = nfs4_xattr_get_nfs4_acl,
5800 .set = nfs4_xattr_set_nfs4_acl,
5801};
5802
5803const struct xattr_handler *nfs4_xattr_handlers[] = {
5804 &nfs4_xattr_nfs4_acl_handler,
5805 NULL
5806};
5807
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808/*
5809 * Local variables:
5810 * c-basic-offset: 8
5811 * End:
5812 */