blob: cab0eb9151450ed460446c2fdbcdbd01484e9f61 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Trond Myklebust4ce79712005-06-22 17:16:21 +000053#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050055#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050056#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040057#include "callback.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59#define NFSDBG_FACILITY NFSDBG_PROC
60
Trond Myklebust2066fe82006-09-15 08:30:46 -040061#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#define NFS4_POLL_RETRY_MAX (15*HZ)
63
Trond Myklebusta78cb572009-08-09 15:06:19 -040064#define NFS4_MAX_LOOP_ON_RECOVER (10)
65
Trond Myklebustcdd4e682006-01-03 09:55:12 +010066struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010067static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080068static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050070static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Trond Myklebust99367812007-07-17 21:52:41 -040071static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
72static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040073static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
74 struct nfs_fattr *fattr, struct iattr *sattr,
75 struct nfs4_state *state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050078static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Trond Myklebust52567b02009-10-23 14:46:42 -040080 if (err >= -1000)
81 return err;
82 switch (err) {
83 case -NFS4ERR_RESOURCE:
84 return -EREMOTEIO;
85 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -070087 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -040088 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 }
Trond Myklebust52567b02009-10-23 14:46:42 -040090 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
93/*
94 * This is our standard bitmap for GETATTR requests.
95 */
96const u32 nfs4_fattr_bitmap[2] = {
97 FATTR4_WORD0_TYPE
98 | FATTR4_WORD0_CHANGE
99 | FATTR4_WORD0_SIZE
100 | FATTR4_WORD0_FSID
101 | FATTR4_WORD0_FILEID,
102 FATTR4_WORD1_MODE
103 | FATTR4_WORD1_NUMLINKS
104 | FATTR4_WORD1_OWNER
105 | FATTR4_WORD1_OWNER_GROUP
106 | FATTR4_WORD1_RAWDEV
107 | FATTR4_WORD1_SPACE_USED
108 | FATTR4_WORD1_TIME_ACCESS
109 | FATTR4_WORD1_TIME_METADATA
110 | FATTR4_WORD1_TIME_MODIFY
111};
112
113const u32 nfs4_statfs_bitmap[2] = {
114 FATTR4_WORD0_FILES_AVAIL
115 | FATTR4_WORD0_FILES_FREE
116 | FATTR4_WORD0_FILES_TOTAL,
117 FATTR4_WORD1_SPACE_AVAIL
118 | FATTR4_WORD1_SPACE_FREE
119 | FATTR4_WORD1_SPACE_TOTAL
120};
121
Trond Myklebust4ce79712005-06-22 17:16:21 +0000122const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 FATTR4_WORD0_MAXLINK
124 | FATTR4_WORD0_MAXNAME,
125 0
126};
127
128const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
129 | FATTR4_WORD0_MAXREAD
130 | FATTR4_WORD0_MAXWRITE
131 | FATTR4_WORD0_LEASE_TIME,
132 0
133};
134
Manoj Naik830b8e32006-06-09 09:34:25 -0400135const u32 nfs4_fs_locations_bitmap[2] = {
136 FATTR4_WORD0_TYPE
137 | FATTR4_WORD0_CHANGE
138 | FATTR4_WORD0_SIZE
139 | FATTR4_WORD0_FSID
140 | FATTR4_WORD0_FILEID
141 | FATTR4_WORD0_FS_LOCATIONS,
142 FATTR4_WORD1_MODE
143 | FATTR4_WORD1_NUMLINKS
144 | FATTR4_WORD1_OWNER
145 | FATTR4_WORD1_OWNER_GROUP
146 | FATTR4_WORD1_RAWDEV
147 | FATTR4_WORD1_SPACE_USED
148 | FATTR4_WORD1_TIME_ACCESS
149 | FATTR4_WORD1_TIME_METADATA
150 | FATTR4_WORD1_TIME_MODIFY
151 | FATTR4_WORD1_MOUNTED_ON_FILEID
152};
153
Al Virobc4785c2006-10-19 23:28:51 -0700154static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 struct nfs4_readdir_arg *readdir)
156{
Al Viro0dbb4c62006-10-19 23:28:49 -0700157 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 BUG_ON(readdir->count < 80);
160 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000161 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
163 return;
164 }
165
166 readdir->cookie = 0;
167 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
168 if (cookie == 2)
169 return;
170
171 /*
172 * NFSv4 servers do not return entries for '.' and '..'
173 * Therefore, we fake these entries here. We let '.'
174 * have cookie 0 and '..' have cookie 1. Note that
175 * when talking to the server, we always send cookie 0
176 * instead of 1 or 2.
177 */
Al Viro0dbb4c62006-10-19 23:28:49 -0700178 start = p = kmap_atomic(*readdir->pages, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 if (cookie == 0) {
181 *p++ = xdr_one; /* next */
182 *p++ = xdr_zero; /* cookie, first word */
183 *p++ = xdr_one; /* cookie, second word */
184 *p++ = xdr_one; /* entry len */
185 memcpy(p, ".\0\0\0", 4); /* entry */
186 p++;
187 *p++ = xdr_one; /* bitmap length */
188 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
189 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400190 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 }
192
193 *p++ = xdr_one; /* next */
194 *p++ = xdr_zero; /* cookie, first word */
195 *p++ = xdr_two; /* cookie, second word */
196 *p++ = xdr_two; /* entry len */
197 memcpy(p, "..\0\0", 4); /* entry */
198 p++;
199 *p++ = xdr_one; /* bitmap length */
200 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
201 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400202 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 readdir->pgbase = (char *)p - (char *)start;
205 readdir->count -= readdir->pgbase;
206 kunmap_atomic(start, KM_USER0);
207}
208
Trond Myklebust65de8722008-12-23 15:21:44 -0500209static int nfs4_wait_clnt_recover(struct nfs_client *clp)
210{
211 int res;
212
213 might_sleep();
214
Trond Myklebuste005e802008-12-23 15:21:48 -0500215 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400216 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500217 return res;
218}
219
220static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
221{
222 int res = 0;
223
224 might_sleep();
225
226 if (*timeout <= 0)
227 *timeout = NFS4_POLL_RETRY_MIN;
228 if (*timeout > NFS4_POLL_RETRY_MAX)
229 *timeout = NFS4_POLL_RETRY_MAX;
230 schedule_timeout_killable(*timeout);
231 if (fatal_signal_pending(current))
232 res = -ERESTARTSYS;
233 *timeout <<= 1;
234 return res;
235}
236
237/* This is the error handling routine for processes that are allowed
238 * to sleep.
239 */
240static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
241{
242 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500243 struct nfs4_state *state = exception->state;
Trond Myklebust65de8722008-12-23 15:21:44 -0500244 int ret = errorcode;
245
246 exception->retry = 0;
247 switch(errorcode) {
248 case 0:
249 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500250 case -NFS4ERR_ADMIN_REVOKED:
251 case -NFS4ERR_BAD_STATEID:
252 case -NFS4ERR_OPENMODE:
253 if (state == NULL)
254 break;
255 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500256 goto do_state_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500257 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500258 if (state == NULL)
259 break;
260 nfs4_state_mark_reclaim_reboot(clp, state);
261 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust65de8722008-12-23 15:21:44 -0500262 case -NFS4ERR_EXPIRED:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500263 goto do_state_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500264#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400265 case -NFS4ERR_BADSESSION:
266 case -NFS4ERR_BADSLOT:
267 case -NFS4ERR_BAD_HIGH_SLOT:
268 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
269 case -NFS4ERR_DEADSESSION:
270 case -NFS4ERR_SEQ_FALSE_RETRY:
271 case -NFS4ERR_SEQ_MISORDERED:
272 dprintk("%s ERROR: %d Reset session\n", __func__,
273 errorcode);
Andy Adamson0b9e2d42009-12-04 16:02:14 -0500274 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -0400275 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500276 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500277#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500278 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500279 if (exception->timeout > HZ) {
280 /* We have retried a decent amount, time to
281 * fail
282 */
283 ret = -EBUSY;
284 break;
285 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500286 case -NFS4ERR_GRACE:
287 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500288 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500289 ret = nfs4_delay(server->client, &exception->timeout);
290 if (ret != 0)
291 break;
292 case -NFS4ERR_OLD_STATEID:
293 exception->retry = 1;
294 }
295 /* We failed to handle the error */
296 return nfs4_map_errors(ret);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500297do_state_recovery:
298 nfs4_schedule_state_recovery(clp);
299 ret = nfs4_wait_clnt_recover(clp);
300 if (ret == 0)
301 exception->retry = 1;
302 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500303}
304
305
Trond Myklebust26e976a2006-01-03 09:55:21 +0100306static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
David Howells7539bba2006-08-22 20:06:09 -0400308 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 spin_lock(&clp->cl_lock);
310 if (time_before(clp->cl_last_renewal,timestamp))
311 clp->cl_last_renewal = timestamp;
312 spin_unlock(&clp->cl_lock);
313}
314
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400315#if defined(CONFIG_NFS_V4_1)
316
Benny Halevy510b8172009-04-01 09:22:14 -0400317/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400318 * nfs4_free_slot - free a slot and efficiently update slot table.
319 *
320 * freeing a slot is trivially done by clearing its respective bit
321 * in the bitmap.
322 * If the freed slotid equals highest_used_slotid we want to update it
323 * so that the server would be able to size down the slot table if needed,
324 * otherwise we know that the highest_used_slotid is still in use.
325 * When updating highest_used_slotid there may be "holes" in the bitmap
326 * so we need to scan down from highest_used_slotid to 0 looking for the now
327 * highest slotid in use.
328 * If none found, highest_used_slotid is set to -1.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500329 *
330 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400331 */
332static void
333nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid)
334{
335 int slotid = free_slotid;
336
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400337 /* clear used bit in bitmap */
338 __clear_bit(slotid, tbl->used_slots);
339
340 /* update highest_used_slotid when it is freed */
341 if (slotid == tbl->highest_used_slotid) {
342 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500343 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400344 tbl->highest_used_slotid = slotid;
345 else
346 tbl->highest_used_slotid = -1;
347 }
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400348 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
349 free_slotid, tbl->highest_used_slotid);
350}
351
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800352/*
353 * Signal state manager thread if session is drained
354 */
355static void nfs41_check_drain_session_complete(struct nfs4_session *ses)
356{
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800357 struct rpc_task *task;
358
Trond Myklebusta2118c32010-06-16 09:52:26 -0400359 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800360 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
361 if (task)
362 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800363 return;
364 }
365
366 if (ses->fc_slot_table.highest_used_slotid != -1)
367 return;
368
369 dprintk("%s COMPLETE: Session Drained\n", __func__);
370 complete(&ses->complete);
371}
372
Trond Myklebustd185a332010-06-16 09:52:25 -0400373static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400374{
375 struct nfs4_slot_table *tbl;
376
Trond Myklebustd185a332010-06-16 09:52:25 -0400377 tbl = &res->sr_session->fc_slot_table;
Andy Adamson13615872009-04-01 09:22:17 -0400378 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE) {
Andy Adamson13615872009-04-01 09:22:17 -0400379 /* just wake up the next guy waiting since
380 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500381 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500382 return;
Andy Adamson13615872009-04-01 09:22:17 -0400383 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500384
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500385 spin_lock(&tbl->slot_tbl_lock);
386 nfs4_free_slot(tbl, res->sr_slotid);
Trond Myklebustd185a332010-06-16 09:52:25 -0400387 nfs41_check_drain_session_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500388 spin_unlock(&tbl->slot_tbl_lock);
389 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Andy Adamson13615872009-04-01 09:22:17 -0400390}
391
Trond Myklebust14516c32010-07-31 14:29:06 -0400392static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400393{
394 unsigned long timestamp;
395 struct nfs4_slot_table *tbl;
396 struct nfs4_slot *slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400397 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400398
399 /*
400 * sr_status remains 1 if an RPC level error occurred. The server
401 * may or may not have processed the sequence operation..
402 * Proceed as if the server received and processed the sequence
403 * operation.
404 */
405 if (res->sr_status == 1)
406 res->sr_status = NFS_OK;
407
408 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
409 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE)
410 goto out;
411
Trond Myklebust14516c32010-07-31 14:29:06 -0400412 tbl = &res->sr_session->fc_slot_table;
413 slot = tbl->slots + res->sr_slotid;
414
Andy Adamson691daf32009-12-04 15:55:39 -0500415 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400416 switch (res->sr_status) {
417 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400418 /* Update the slot's sequence and clientid lease timer */
419 ++slot->seq_nr;
420 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400421 clp = res->sr_session->clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400422 spin_lock(&clp->cl_lock);
423 if (time_before(clp->cl_last_renewal, timestamp))
424 clp->cl_last_renewal = timestamp;
425 spin_unlock(&clp->cl_lock);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500426 /* Check sequence flags */
Alexandros Batsakis71358402010-02-05 03:45:05 -0800427 if (atomic_read(&clp->cl_count) > 1)
428 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust14516c32010-07-31 14:29:06 -0400429 break;
430 case -NFS4ERR_DELAY:
431 /* The server detected a resend of the RPC call and
432 * returned NFS4ERR_DELAY as per Section 2.10.6.2
433 * of RFC5661.
434 */
435 dprintk("%s: slot=%d seq=%d: Operation in progress\n",
436 __func__, res->sr_slotid, slot->seq_nr);
437 goto out_retry;
438 default:
439 /* Just update the slot sequence no. */
440 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400441 }
442out:
443 /* The session may be reset by one of the error handlers. */
444 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400445 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400446 return 1;
447out_retry:
448 rpc_restart_call(task);
449 /* FIXME: rpc_restart_call() should be made to return success/fail */
450 if (task->tk_action == NULL)
451 goto out;
452 rpc_delay(task, NFS4_POLL_RETRY_MAX);
453 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400454}
455
Trond Myklebust14516c32010-07-31 14:29:06 -0400456static int nfs4_sequence_done(struct rpc_task *task,
457 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400458{
Trond Myklebust14516c32010-07-31 14:29:06 -0400459 if (res->sr_session == NULL)
460 return 1;
461 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400462}
463
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400464/*
Benny Halevy510b8172009-04-01 09:22:14 -0400465 * nfs4_find_slot - efficiently look for a free slot
466 *
467 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
468 * If found, we mark the slot as used, update the highest_used_slotid,
469 * and respectively set up the sequence operation args.
470 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400471 *
472 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400473 */
474static u8
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800475nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400476{
477 int slotid;
478 u8 ret_id = NFS4_MAX_SLOT_TABLE;
479 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
480
Benny Halevy510b8172009-04-01 09:22:14 -0400481 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
482 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
483 tbl->max_slots);
484 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
485 if (slotid >= tbl->max_slots)
486 goto out;
487 __set_bit(slotid, tbl->used_slots);
488 if (slotid > tbl->highest_used_slotid)
489 tbl->highest_used_slotid = slotid;
490 ret_id = slotid;
491out:
492 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
493 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400494 return ret_id;
495}
496
Andy Adamsonce5039c2009-04-01 09:22:13 -0400497static int nfs41_setup_sequence(struct nfs4_session *session,
498 struct nfs4_sequence_args *args,
499 struct nfs4_sequence_res *res,
500 int cache_reply,
501 struct rpc_task *task)
502{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400503 struct nfs4_slot *slot;
504 struct nfs4_slot_table *tbl;
505 u8 slotid;
506
507 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400508 /* slot already allocated? */
509 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
510 return 0;
511
Andy Adamsonce5039c2009-04-01 09:22:13 -0400512 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400513 tbl = &session->fc_slot_table;
514
515 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400516 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800517 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamsonea028ac2009-12-04 15:55:38 -0500518 /*
519 * The state manager will wait until the slot table is empty.
520 * Schedule the reset thread
521 */
Andy Adamson05f0d232009-12-04 15:55:37 -0500522 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400523 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500524 dprintk("%s Schedule Session Reset\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500525 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400526 }
527
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800528 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
529 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
530 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
531 spin_unlock(&tbl->slot_tbl_lock);
532 dprintk("%s enforce FIFO order\n", __func__);
533 return -EAGAIN;
534 }
535
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800536 slotid = nfs4_find_slot(tbl);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400537 if (slotid == NFS4_MAX_SLOT_TABLE) {
538 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
539 spin_unlock(&tbl->slot_tbl_lock);
540 dprintk("<-- %s: no free slots\n", __func__);
541 return -EAGAIN;
542 }
543 spin_unlock(&tbl->slot_tbl_lock);
544
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800545 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400546 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400547 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400548 args->sa_slotid = slotid;
549 args->sa_cache_this = cache_reply;
550
551 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
552
Benny Halevy99fe60d2009-04-01 09:22:29 -0400553 res->sr_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400554 res->sr_slotid = slotid;
555 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400556 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400557 /*
558 * sr_status is only set in decode_sequence, and so will remain
559 * set to 1 if an rpc level failure occurs.
560 */
561 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400562 return 0;
563}
564
Trond Myklebust035168a2010-06-16 09:52:26 -0400565int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400566 struct nfs4_sequence_args *args,
567 struct nfs4_sequence_res *res,
568 int cache_reply,
569 struct rpc_task *task)
570{
Trond Myklebust035168a2010-06-16 09:52:26 -0400571 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400572 int ret = 0;
573
Trond Myklebust035168a2010-06-16 09:52:26 -0400574 if (session == NULL) {
575 args->sa_session = NULL;
576 res->sr_session = NULL;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400577 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400578 }
579
580 dprintk("--> %s clp %p session %p sr_slotid %d\n",
581 __func__, session->clp, session, res->sr_slotid);
582
583 ret = nfs41_setup_sequence(session, args, res, cache_reply,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400584 task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400585out:
586 dprintk("<-- %s status=%d\n", __func__, ret);
587 return ret;
588}
589
590struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400591 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400592 struct nfs4_sequence_args *seq_args;
593 struct nfs4_sequence_res *seq_res;
594 int cache_reply;
595};
596
597static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
598{
599 struct nfs41_call_sync_data *data = calldata;
600
Trond Myklebust035168a2010-06-16 09:52:26 -0400601 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
602
603 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400604 data->seq_res, data->cache_reply, task))
605 return;
606 rpc_call_start(task);
607}
608
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800609static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
610{
611 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
612 nfs41_call_sync_prepare(task, calldata);
613}
614
Andy Adamson69ab40c2009-04-01 09:22:19 -0400615static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
616{
617 struct nfs41_call_sync_data *data = calldata;
618
Trond Myklebust14516c32010-07-31 14:29:06 -0400619 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400620}
621
Andy Adamsonce5039c2009-04-01 09:22:13 -0400622struct rpc_call_ops nfs41_call_sync_ops = {
623 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400624 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400625};
626
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800627struct rpc_call_ops nfs41_call_priv_sync_ops = {
628 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
629 .rpc_call_done = nfs41_call_sync_done,
630};
631
Trond Myklebust035168a2010-06-16 09:52:26 -0400632static int nfs4_call_sync_sequence(struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400633 struct rpc_message *msg,
634 struct nfs4_sequence_args *args,
635 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800636 int cache_reply,
637 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400638{
639 int ret;
640 struct rpc_task *task;
641 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400642 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400643 .seq_args = args,
644 .seq_res = res,
645 .cache_reply = cache_reply,
646 };
647 struct rpc_task_setup task_setup = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400648 .rpc_client = server->client,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400649 .rpc_message = msg,
650 .callback_ops = &nfs41_call_sync_ops,
651 .callback_data = &data
652 };
653
654 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800655 if (privileged)
656 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400657 task = rpc_run_task(&task_setup);
658 if (IS_ERR(task))
659 ret = PTR_ERR(task);
660 else {
661 ret = task->tk_status;
662 rpc_put_task(task);
663 }
664 return ret;
665}
666
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400667int _nfs4_call_sync_session(struct nfs_server *server,
668 struct rpc_message *msg,
669 struct nfs4_sequence_args *args,
670 struct nfs4_sequence_res *res,
671 int cache_reply)
672{
Trond Myklebust035168a2010-06-16 09:52:26 -0400673 return nfs4_call_sync_sequence(server, msg, args, res, cache_reply, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400674}
675
Trond Myklebustdf896452010-06-16 09:52:26 -0400676#else
Trond Myklebust14516c32010-07-31 14:29:06 -0400677static int nfs4_sequence_done(struct rpc_task *task,
678 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400679{
Trond Myklebust14516c32010-07-31 14:29:06 -0400680 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400681}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400682#endif /* CONFIG_NFS_V4_1 */
683
684int _nfs4_call_sync(struct nfs_server *server,
685 struct rpc_message *msg,
686 struct nfs4_sequence_args *args,
687 struct nfs4_sequence_res *res,
688 int cache_reply)
689{
Benny Halevyf3752972009-04-01 09:22:04 -0400690 args->sa_session = res->sr_session = NULL;
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400691 return rpc_call_sync(server->client, msg, 0);
692}
693
694#define nfs4_call_sync(server, msg, args, res, cache_reply) \
Trond Myklebust97dc1352010-06-16 09:52:26 -0400695 (server)->nfs_client->cl_mvops->call_sync((server), (msg), &(args)->seq_args, \
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400696 &(res)->seq_res, (cache_reply))
697
Trond Myklebust38478b22006-05-25 01:40:57 -0400698static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
Trond Myklebust38478b22006-05-25 01:40:57 -0400700 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Trond Myklebust38478b22006-05-25 01:40:57 -0400702 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400703 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
704 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400705 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400706 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400707 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100710struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400711 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100712 struct nfs_openargs o_arg;
713 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100714 struct nfs_open_confirmargs c_arg;
715 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100716 struct nfs_fattr f_attr;
717 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400718 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100719 struct dentry *dir;
720 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400721 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100722 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100723 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400724 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100725 int rpc_status;
726 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100727};
728
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400729
730static void nfs4_init_opendata_res(struct nfs4_opendata *p)
731{
732 p->o_res.f_attr = &p->f_attr;
733 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400734 p->o_res.seqid = p->o_arg.seqid;
735 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400736 p->o_res.server = p->o_arg.server;
737 nfs_fattr_init(&p->f_attr);
738 nfs_fattr_init(&p->dir_attr);
Benny Halevy578e4582009-06-18 22:01:23 -0400739 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400740}
741
Trond Myklebustad389da2007-06-05 12:30:00 -0400742static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500743 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400744 const struct iattr *attrs,
745 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100746{
Trond Myklebustad389da2007-06-05 12:30:00 -0400747 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100748 struct inode *dir = parent->d_inode;
749 struct nfs_server *server = NFS_SERVER(dir);
750 struct nfs4_opendata *p;
751
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400752 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100753 if (p == NULL)
754 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400755 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100756 if (p->o_arg.seqid == NULL)
757 goto err_free;
Al Virof6948692010-01-30 13:51:04 -0500758 path_get(path);
759 p->path = *path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100760 p->dir = parent;
761 p->owner = sp;
762 atomic_inc(&sp->so_count);
763 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500764 p->o_arg.open_flags = flags;
765 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400766 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400767 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400768 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100769 p->o_arg.server = server;
770 p->o_arg.bitmask = server->attr_bitmask;
771 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400772 if (flags & O_CREAT) {
773 u32 *s;
774
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100775 p->o_arg.u.attrs = &p->attrs;
776 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400777 s = (u32 *) p->o_arg.u.verifier.data;
778 s[0] = jiffies;
779 s[1] = current->pid;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100780 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100781 p->c_arg.fh = &p->o_res.fh;
782 p->c_arg.stateid = &p->o_res.stateid;
783 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400784 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400785 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100786 return p;
787err_free:
788 kfree(p);
789err:
790 dput(parent);
791 return NULL;
792}
793
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400794static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100795{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400796 struct nfs4_opendata *p = container_of(kref,
797 struct nfs4_opendata, kref);
798
799 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400800 if (p->state != NULL)
801 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400802 nfs4_put_state_owner(p->owner);
803 dput(p->dir);
Jan Blunck31f31db12008-05-02 13:42:45 -0700804 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400805 kfree(p);
806}
807
808static void nfs4_opendata_put(struct nfs4_opendata *p)
809{
810 if (p != NULL)
811 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100812}
813
Trond Myklebust06f814a2006-01-03 09:55:07 +0100814static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
815{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100816 int ret;
817
Trond Myklebust06f814a2006-01-03 09:55:07 +0100818 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100819 return ret;
820}
821
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500822static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400823{
824 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500825
826 if (open_mode & O_EXCL)
827 goto out;
828 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400829 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500830 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
831 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400832 break;
833 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500834 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
835 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400836 break;
837 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500838 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
839 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400840 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500841out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400842 return ret;
843}
844
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500845static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400846{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500847 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400848 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500849 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400850 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500851 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400852 return 1;
853}
854
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500855static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100856{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500857 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100858 case FMODE_WRITE:
859 state->n_wronly++;
860 break;
861 case FMODE_READ:
862 state->n_rdonly++;
863 break;
864 case FMODE_READ|FMODE_WRITE:
865 state->n_rdwr++;
866 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500867 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100868}
869
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500870static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400871{
872 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
873 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
874 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500875 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400876 case FMODE_READ:
877 set_bit(NFS_O_RDONLY_STATE, &state->flags);
878 break;
879 case FMODE_WRITE:
880 set_bit(NFS_O_WRONLY_STATE, &state->flags);
881 break;
882 case FMODE_READ|FMODE_WRITE:
883 set_bit(NFS_O_RDWR_STATE, &state->flags);
884 }
885}
886
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500887static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400888{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400889 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500890 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400891 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400892}
893
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500894static 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 -0700895{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400896 /*
897 * Protect the call to nfs4_state_set_mode_locked and
898 * serialise the stateid update
899 */
900 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400901 if (deleg_stateid != NULL) {
902 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
903 set_bit(NFS_DELEGATED_STATE, &state->flags);
904 }
905 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500906 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400907 write_sequnlock(&state->seqlock);
908 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500909 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700910 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}
912
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500913static 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 -0500914{
915 struct nfs_inode *nfsi = NFS_I(state->inode);
916 struct nfs_delegation *deleg_cur;
917 int ret = 0;
918
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500919 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500920
921 rcu_read_lock();
922 deleg_cur = rcu_dereference(nfsi->delegation);
923 if (deleg_cur == NULL)
924 goto no_delegation;
925
926 spin_lock(&deleg_cur->lock);
927 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500928 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500929 goto no_delegation_unlock;
930
931 if (delegation == NULL)
932 delegation = &deleg_cur->stateid;
933 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
934 goto no_delegation_unlock;
935
Trond Myklebustb7391f42008-12-23 15:21:52 -0500936 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500937 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500938 ret = 1;
939no_delegation_unlock:
940 spin_unlock(&deleg_cur->lock);
941no_delegation:
942 rcu_read_unlock();
943
944 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500945 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500946 ret = 1;
947 }
948
949 return ret;
950}
951
952
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500953static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400954{
955 struct nfs_delegation *delegation;
956
957 rcu_read_lock();
958 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500959 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400960 rcu_read_unlock();
961 return;
962 }
963 rcu_read_unlock();
964 nfs_inode_return_delegation(inode);
965}
966
Trond Myklebust6ee41262007-07-08 14:11:36 -0400967static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400968{
969 struct nfs4_state *state = opendata->state;
970 struct nfs_inode *nfsi = NFS_I(state->inode);
971 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500972 int open_mode = opendata->o_arg.open_flags & O_EXCL;
973 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400974 nfs4_stateid stateid;
975 int ret = -EAGAIN;
976
Trond Myklebustaac00a82007-07-05 19:02:21 -0400977 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500978 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400979 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500980 if (can_open_cached(state, fmode, open_mode)) {
981 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400982 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400983 goto out_return_state;
984 }
985 spin_unlock(&state->owner->so_lock);
986 }
Trond Myklebust34310432008-12-23 15:21:38 -0500987 rcu_read_lock();
988 delegation = rcu_dereference(nfsi->delegation);
989 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500990 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -0500991 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -0400992 break;
Trond Myklebust34310432008-12-23 15:21:38 -0500993 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400994 /* Save the delegation */
995 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
996 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -0400997 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400998 if (ret != 0)
999 goto out;
1000 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001001
1002 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001003 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001004 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001005 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001006out:
1007 return ERR_PTR(ret);
1008out_return_state:
1009 atomic_inc(&state->count);
1010 return state;
1011}
1012
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001013static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1014{
1015 struct inode *inode;
1016 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001017 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001018 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001019
Trond Myklebustaac00a82007-07-05 19:02:21 -04001020 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001021 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001022 goto out;
1023 }
1024
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001025 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001026 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001027 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001028 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001029 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001030 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001031 goto err;
1032 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001033 state = nfs4_get_open_state(inode, data->owner);
1034 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001035 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001036 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001037 int delegation_flags = 0;
1038
Trond Myklebust003707c2007-07-05 18:07:55 -04001039 rcu_read_lock();
1040 delegation = rcu_dereference(NFS_I(inode)->delegation);
1041 if (delegation)
1042 delegation_flags = delegation->flags;
1043 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -05001044 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001045 nfs_inode_set_delegation(state->inode,
1046 data->owner->so_cred,
1047 &data->o_res);
1048 else
1049 nfs_inode_reclaim_delegation(state->inode,
1050 data->owner->so_cred,
1051 &data->o_res);
1052 }
Trond Myklebust34310432008-12-23 15:21:38 -05001053
1054 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001055 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001056 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001057out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001058 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001059err_put_inode:
1060 iput(inode);
1061err:
1062 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001063}
1064
Trond Myklebust864472e2006-01-03 09:55:15 +01001065static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1066{
1067 struct nfs_inode *nfsi = NFS_I(state->inode);
1068 struct nfs_open_context *ctx;
1069
1070 spin_lock(&state->inode->i_lock);
1071 list_for_each_entry(ctx, &nfsi->open_files, list) {
1072 if (ctx->state != state)
1073 continue;
1074 get_nfs_open_context(ctx);
1075 spin_unlock(&state->inode->i_lock);
1076 return ctx;
1077 }
1078 spin_unlock(&state->inode->i_lock);
1079 return ERR_PTR(-ENOENT);
1080}
1081
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001082static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1083{
1084 struct nfs4_opendata *opendata;
1085
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001086 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001087 if (opendata == NULL)
1088 return ERR_PTR(-ENOMEM);
1089 opendata->state = state;
1090 atomic_inc(&state->count);
1091 return opendata;
1092}
1093
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001094static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001095{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001096 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001097 int ret;
1098
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001099 opendata->o_arg.open_flags = 0;
1100 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001101 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1102 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1103 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001104 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001105 if (ret != 0)
1106 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001107 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001108 if (IS_ERR(newstate))
1109 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001110 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001111 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001112 return 0;
1113}
1114
1115static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1116{
Trond Myklebust864472e2006-01-03 09:55:15 +01001117 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001118 int ret;
1119
1120 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001121 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001122 smp_rmb();
1123 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001124 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001125 if (ret != 0)
1126 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001127 if (newstate != state)
1128 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001129 }
1130 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001131 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001132 if (ret != 0)
1133 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001134 if (newstate != state)
1135 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001136 }
1137 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001138 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001139 if (ret != 0)
1140 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001141 if (newstate != state)
1142 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001143 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001144 /*
1145 * We may have performed cached opens for all three recoveries.
1146 * Check if we need to update the current stateid.
1147 */
1148 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1149 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001150 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001151 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1152 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001153 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001154 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001155 return 0;
1156}
1157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158/*
1159 * OPEN_RECLAIM:
1160 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001162static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001164 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001165 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001166 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 int status;
1168
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001169 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1170 if (IS_ERR(opendata))
1171 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001172 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1173 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001174 rcu_read_lock();
1175 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001176 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001177 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001178 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001179 opendata->o_arg.u.delegation_type = delegation_type;
1180 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001181 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 return status;
1183}
1184
Trond Myklebust539cd032007-06-05 11:46:42 -04001185static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
1187 struct nfs_server *server = NFS_SERVER(state->inode);
1188 struct nfs4_exception exception = { };
1189 int err;
1190 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001191 err = _nfs4_do_open_reclaim(ctx, state);
Jeff Layton2c643482010-01-07 09:42:03 -05001192 if (err != -NFS4ERR_DELAY && err != -EKEYEXPIRED)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001193 break;
1194 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 } while (exception.retry);
1196 return err;
1197}
1198
Trond Myklebust864472e2006-01-03 09:55:15 +01001199static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1200{
1201 struct nfs_open_context *ctx;
1202 int ret;
1203
1204 ctx = nfs4_state_find_open_context(state);
1205 if (IS_ERR(ctx))
1206 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001207 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001208 put_nfs_open_context(ctx);
1209 return ret;
1210}
1211
Trond Myklebust13437e12007-07-06 15:10:43 -04001212static 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 -07001213{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001214 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001215 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001217 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1218 if (IS_ERR(opendata))
1219 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001220 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001221 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001222 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001223 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001224 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001225 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226}
1227
Trond Myklebust13437e12007-07-06 15:10:43 -04001228int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
1230 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001231 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 int err;
1233 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001234 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 switch (err) {
1236 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001237 case -ENOENT:
1238 case -ESTALE:
1239 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001240 case -NFS4ERR_BADSESSION:
1241 case -NFS4ERR_BADSLOT:
1242 case -NFS4ERR_BAD_HIGH_SLOT:
1243 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1244 case -NFS4ERR_DEADSESSION:
1245 nfs4_schedule_state_recovery(
1246 server->nfs_client);
1247 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 case -NFS4ERR_STALE_CLIENTID:
1249 case -NFS4ERR_STALE_STATEID:
1250 case -NFS4ERR_EXPIRED:
1251 /* Don't recall a delegation if it was lost */
David Howells7539bba2006-08-22 20:06:09 -04001252 nfs4_schedule_state_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001253 goto out;
1254 case -ERESTARTSYS:
1255 /*
1256 * The show must go on: exit, but mark the
1257 * stateid as needing recovery.
1258 */
1259 case -NFS4ERR_ADMIN_REVOKED:
1260 case -NFS4ERR_BAD_STATEID:
1261 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
1262 case -ENOMEM:
1263 err = 0;
1264 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
1266 err = nfs4_handle_exception(server, err, &exception);
1267 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001268out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 return err;
1270}
1271
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001272static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1273{
1274 struct nfs4_opendata *data = calldata;
1275
1276 data->rpc_status = task->tk_status;
1277 if (RPC_ASSASSINATED(task))
1278 return;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001279 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001280 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1281 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001282 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001283 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001284 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001285 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001286}
1287
1288static void nfs4_open_confirm_release(void *calldata)
1289{
1290 struct nfs4_opendata *data = calldata;
1291 struct nfs4_state *state = NULL;
1292
1293 /* If this request hasn't been cancelled, do nothing */
1294 if (data->cancelled == 0)
1295 goto out_free;
1296 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001297 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001298 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001299 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001300 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001301 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001302out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001303 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001304}
1305
1306static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001307 .rpc_call_done = nfs4_open_confirm_done,
1308 .rpc_release = nfs4_open_confirm_release,
1309};
1310
1311/*
1312 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1313 */
1314static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1315{
1316 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1317 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001318 struct rpc_message msg = {
1319 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1320 .rpc_argp = &data->c_arg,
1321 .rpc_resp = &data->c_res,
1322 .rpc_cred = data->owner->so_cred,
1323 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001324 struct rpc_task_setup task_setup_data = {
1325 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001326 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001327 .callback_ops = &nfs4_open_confirm_ops,
1328 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001329 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001330 .flags = RPC_TASK_ASYNC,
1331 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 int status;
1333
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001334 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001335 data->rpc_done = 0;
1336 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001337 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001338 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001339 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001340 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001341 status = nfs4_wait_for_completion_rpc_task(task);
1342 if (status != 0) {
1343 data->cancelled = 1;
1344 smp_wmb();
1345 } else
1346 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001347 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 return status;
1349}
1350
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001351static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001353 struct nfs4_opendata *data = calldata;
1354 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001355
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001356 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1357 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001358 /*
1359 * Check if we still need to send an OPEN call, or if we can use
1360 * a delegation instead.
1361 */
1362 if (data->state != NULL) {
1363 struct nfs_delegation *delegation;
1364
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001365 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001366 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001367 rcu_read_lock();
1368 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1369 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001370 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001371 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001372 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001373 }
1374 rcu_read_unlock();
1375 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001376 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001377 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001378 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001379 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001380 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001381 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1382 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001383 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001384 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001385 &data->o_arg.seq_args,
1386 &data->o_res.seq_res, 1, task))
1387 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001388 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001389 return;
1390out_no_action:
1391 task->tk_action = NULL;
1392
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001393}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001395static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1396{
1397 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1398 nfs4_open_prepare(task, calldata);
1399}
1400
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001401static void nfs4_open_done(struct rpc_task *task, void *calldata)
1402{
1403 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001405 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001406
Trond Myklebust14516c32010-07-31 14:29:06 -04001407 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1408 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001409
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001410 if (RPC_ASSASSINATED(task))
1411 return;
1412 if (task->tk_status == 0) {
1413 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001414 case S_IFREG:
1415 break;
1416 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001417 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001418 break;
1419 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001420 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001421 break;
1422 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001423 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001424 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001425 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001426 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1427 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001428 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001429 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001430}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001431
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001432static void nfs4_open_release(void *calldata)
1433{
1434 struct nfs4_opendata *data = calldata;
1435 struct nfs4_state *state = NULL;
1436
1437 /* If this request hasn't been cancelled, do nothing */
1438 if (data->cancelled == 0)
1439 goto out_free;
1440 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001441 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001442 goto out_free;
1443 /* In case we need an open_confirm, no cleanup! */
1444 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1445 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001446 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001447 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001448 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001449out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001450 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001451}
1452
1453static const struct rpc_call_ops nfs4_open_ops = {
1454 .rpc_call_prepare = nfs4_open_prepare,
1455 .rpc_call_done = nfs4_open_done,
1456 .rpc_release = nfs4_open_release,
1457};
1458
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001459static const struct rpc_call_ops nfs4_recover_open_ops = {
1460 .rpc_call_prepare = nfs4_recover_open_prepare,
1461 .rpc_call_done = nfs4_open_done,
1462 .rpc_release = nfs4_open_release,
1463};
1464
1465static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001466{
1467 struct inode *dir = data->dir->d_inode;
1468 struct nfs_server *server = NFS_SERVER(dir);
1469 struct nfs_openargs *o_arg = &data->o_arg;
1470 struct nfs_openres *o_res = &data->o_res;
1471 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001472 struct rpc_message msg = {
1473 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1474 .rpc_argp = o_arg,
1475 .rpc_resp = o_res,
1476 .rpc_cred = data->owner->so_cred,
1477 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001478 struct rpc_task_setup task_setup_data = {
1479 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001480 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001481 .callback_ops = &nfs4_open_ops,
1482 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001483 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001484 .flags = RPC_TASK_ASYNC,
1485 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001486 int status;
1487
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001488 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001489 data->rpc_done = 0;
1490 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001491 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001492 if (isrecover)
1493 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001494 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001495 if (IS_ERR(task))
1496 return PTR_ERR(task);
1497 status = nfs4_wait_for_completion_rpc_task(task);
1498 if (status != 0) {
1499 data->cancelled = 1;
1500 smp_wmb();
1501 } else
1502 status = data->rpc_status;
1503 rpc_put_task(task);
1504
1505 return status;
1506}
1507
1508static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1509{
1510 struct inode *dir = data->dir->d_inode;
1511 struct nfs_openres *o_res = &data->o_res;
1512 int status;
1513
1514 status = nfs4_run_open_task(data, 1);
1515 if (status != 0 || !data->rpc_done)
1516 return status;
1517
1518 nfs_refresh_inode(dir, o_res->dir_attr);
1519
1520 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1521 status = _nfs4_proc_open_confirm(data);
1522 if (status != 0)
1523 return status;
1524 }
1525
1526 return status;
1527}
1528
1529/*
1530 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1531 */
1532static int _nfs4_proc_open(struct nfs4_opendata *data)
1533{
1534 struct inode *dir = data->dir->d_inode;
1535 struct nfs_server *server = NFS_SERVER(dir);
1536 struct nfs_openargs *o_arg = &data->o_arg;
1537 struct nfs_openres *o_res = &data->o_res;
1538 int status;
1539
1540 status = nfs4_run_open_task(data, 0);
Trond Myklebust3e309912007-07-07 13:19:59 -04001541 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001542 return status;
1543
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001544 if (o_arg->open_flags & O_CREAT) {
1545 update_changeattr(dir, &o_res->cinfo);
1546 nfs_post_op_update_inode(dir, o_res->dir_attr);
1547 } else
1548 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001549 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1550 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001552 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001554 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 }
1556 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001557 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001558 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559}
1560
Trond Myklebust10afec92007-05-14 17:16:04 -04001561static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +01001562{
David Howells7539bba2006-08-22 20:06:09 -04001563 struct nfs_client *clp = server->nfs_client;
Trond Myklebusta78cb572009-08-09 15:06:19 -04001564 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001565 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001566
Trond Myklebusta78cb572009-08-09 15:06:19 -04001567 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001568 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001569 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001570 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001571 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1572 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001573 break;
Trond Myklebust58d97142006-01-03 09:55:24 +01001574 nfs4_schedule_state_recovery(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001575 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001576 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001577 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001578}
1579
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580/*
1581 * OPEN_EXPIRED:
1582 * reclaim state on the server after a network partition.
1583 * Assumes caller holds the appropriate lock
1584 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001585static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001587 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001588 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001590 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1591 if (IS_ERR(opendata))
1592 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001593 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001594 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001595 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001596 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001597 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598}
1599
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001600static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001601{
Trond Myklebust539cd032007-06-05 11:46:42 -04001602 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001603 struct nfs4_exception exception = { };
1604 int err;
1605
1606 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001607 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001608 switch (err) {
1609 default:
1610 goto out;
1611 case -NFS4ERR_GRACE:
1612 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05001613 case -EKEYEXPIRED:
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001614 nfs4_handle_exception(server, err, &exception);
1615 err = 0;
1616 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001617 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001618out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001619 return err;
1620}
1621
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1623{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001625 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Trond Myklebust864472e2006-01-03 09:55:15 +01001627 ctx = nfs4_state_find_open_context(state);
1628 if (IS_ERR(ctx))
1629 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001630 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001631 put_nfs_open_context(ctx);
1632 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633}
1634
1635/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001636 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1637 * fields corresponding to attributes that were used to store the verifier.
1638 * Make sure we clobber those fields in the later setattr call
1639 */
1640static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1641{
1642 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1643 !(sattr->ia_valid & ATTR_ATIME_SET))
1644 sattr->ia_valid |= ATTR_ATIME;
1645
1646 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1647 !(sattr->ia_valid & ATTR_MTIME_SET))
1648 sattr->ia_valid |= ATTR_MTIME;
1649}
1650
1651/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001652 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001654static 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 -07001655{
1656 struct nfs4_state_owner *sp;
1657 struct nfs4_state *state = NULL;
1658 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001659 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001660 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
1662 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 status = -ENOMEM;
1664 if (!(sp = nfs4_get_state_owner(server, cred))) {
1665 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1666 goto out_err;
1667 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001668 status = nfs4_recover_expired_lease(server);
1669 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001670 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001671 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001672 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001673 status = -ENOMEM;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001674 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001675 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001676 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Trond Myklebustaac00a82007-07-05 19:02:21 -04001678 if (path->dentry->d_inode != NULL)
1679 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1680
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001681 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001683 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001685 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001686 status = PTR_ERR(state);
1687 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001688 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001689 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001690 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001691
1692 if (opendata->o_arg.open_flags & O_EXCL) {
1693 nfs4_exclusive_attrset(opendata, sattr);
1694
1695 nfs_fattr_init(opendata->o_res.f_attr);
1696 status = nfs4_do_setattr(state->inode, cred,
1697 opendata->o_res.f_attr, sattr,
1698 state);
1699 if (status == 0)
1700 nfs_setattr_update_inode(state->inode, sattr);
1701 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1702 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001703 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 *res = state;
1706 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001707err_opendata_put:
1708 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001709err_put_state_owner:
1710 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 *res = NULL;
1713 return status;
1714}
1715
1716
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001717static 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 -07001718{
1719 struct nfs4_exception exception = { };
1720 struct nfs4_state *res;
1721 int status;
1722
1723 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001724 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 if (status == 0)
1726 break;
1727 /* NOTE: BAD_SEQID means the server and client disagree about the
1728 * book-keeping w.r.t. state-changing operations
1729 * (OPEN/CLOSE/LOCK/LOCKU...)
1730 * It is actually a sign of a bug on the client or on the server.
1731 *
1732 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001733 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 * have unhashed the old state_owner for us, and that we can
1735 * therefore safely retry using a new one. We should still warn
1736 * the user though...
1737 */
1738 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001739 printk(KERN_WARNING "NFS: v4 server %s "
1740 " returned a bad sequence-id error!\n",
1741 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 exception.retry = 1;
1743 continue;
1744 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001745 /*
1746 * BAD_STATEID on OPEN means that the server cancelled our
1747 * state before it received the OPEN_CONFIRM.
1748 * Recover by retrying the request as per the discussion
1749 * on Page 181 of RFC3530.
1750 */
1751 if (status == -NFS4ERR_BAD_STATEID) {
1752 exception.retry = 1;
1753 continue;
1754 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001755 if (status == -EAGAIN) {
1756 /* We must have found a delegation */
1757 exception.retry = 1;
1758 continue;
1759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1761 status, &exception));
1762 } while (exception.retry);
1763 return res;
1764}
1765
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001766static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1767 struct nfs_fattr *fattr, struct iattr *sattr,
1768 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001770 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001772 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 .iap = sattr,
1774 .server = server,
1775 .bitmask = server->attr_bitmask,
1776 };
1777 struct nfs_setattrres res = {
1778 .fattr = fattr,
1779 .server = server,
1780 };
1781 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001782 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1783 .rpc_argp = &arg,
1784 .rpc_resp = &res,
1785 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001787 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001788 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Trond Myklebust0e574af2005-10-27 22:12:38 -04001790 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001792 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1793 /* Use that stateid */
1794 } else if (state != NULL) {
Trond Myklebust77041ed2010-07-01 12:49:11 -04001795 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001796 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1798
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001799 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001800 if (status == 0 && state != NULL)
1801 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001802 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803}
1804
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001805static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1806 struct nfs_fattr *fattr, struct iattr *sattr,
1807 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001809 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 struct nfs4_exception exception = { };
1811 int err;
1812 do {
1813 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001814 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 &exception);
1816 } while (exception.retry);
1817 return err;
1818}
1819
1820struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001821 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 struct inode *inode;
1823 struct nfs4_state *state;
1824 struct nfs_closeargs arg;
1825 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001826 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001827 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828};
1829
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001830static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001831{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001832 struct nfs4_closedata *calldata = data;
1833 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001834
1835 nfs4_put_open_state(calldata->state);
1836 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001837 nfs4_put_state_owner(sp);
Jan Blunck31f31db12008-05-02 13:42:45 -07001838 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001839 kfree(calldata);
1840}
1841
Trond Myklebust88069f72009-12-08 08:33:16 -05001842static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1843 fmode_t fmode)
1844{
1845 spin_lock(&state->owner->so_lock);
1846 if (!(fmode & FMODE_READ))
1847 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1848 if (!(fmode & FMODE_WRITE))
1849 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1850 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1851 spin_unlock(&state->owner->so_lock);
1852}
1853
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001854static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001856 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 struct nfs_server *server = NFS_SERVER(calldata->inode);
1859
Trond Myklebust14516c32010-07-31 14:29:06 -04001860 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1861 return;
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001862 if (RPC_ASSASSINATED(task))
1863 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 /* hmm. we are done with the inode, and in the process of freeing
1865 * the state_owner. we keep this around to process errors
1866 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 switch (task->tk_status) {
1868 case 0:
Trond Myklebust45328c32007-07-26 17:47:34 -04001869 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001870 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001871 nfs4_close_clear_stateid_flags(state,
1872 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 break;
1874 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001875 case -NFS4ERR_OLD_STATEID:
1876 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001878 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001879 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001881 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1882 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001884 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001885 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886}
1887
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001888static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001890 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001891 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001892 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001893
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001894 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001895 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001896
Trond Myklebust88069f72009-12-08 08:33:16 -05001897 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1898 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001899 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001900 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001901 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001902 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001903 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1904 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1905 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001906 }
1907 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001908 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1909 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1910 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001911 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001912 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001913 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05001914
1915 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001916 /* Note: exit _without_ calling nfs4_close_done */
1917 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001918 return;
1919 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001920
1921 if (calldata->arg.fmode == 0)
1922 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
1923
Trond Myklebust516a6af2005-10-27 22:12:41 -04001924 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001925 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001926 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Andy Adamson19ddab02009-04-01 09:22:20 -04001927 &calldata->arg.seq_args, &calldata->res.seq_res,
1928 1, task))
1929 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001930 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931}
1932
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001933static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001934 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001935 .rpc_call_done = nfs4_close_done,
1936 .rpc_release = nfs4_free_closedata,
1937};
1938
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939/*
1940 * It is possible for data to be read/written from a mem-mapped file
1941 * after the sys_close call (which hits the vfs layer as a flush).
1942 * This means that we can't safely call nfsv4 close on a file until
1943 * the inode is cleared. This in turn means that we are not good
1944 * NFSv4 citizens - we do not indicate to the server to update the file's
1945 * share state even when we are done with one of the three share
1946 * stateid's in the inode.
1947 *
1948 * NOTE: Caller must be holding the sp->so_owner semaphore!
1949 */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001950int nfs4_do_close(struct path *path, struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001952 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001954 struct nfs4_state_owner *sp = state->owner;
1955 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001956 struct rpc_message msg = {
1957 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1958 .rpc_cred = state->owner->so_cred,
1959 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001960 struct rpc_task_setup task_setup_data = {
1961 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001962 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001963 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05001964 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001965 .flags = RPC_TASK_ASYNC,
1966 };
Trond Myklebust95121352005-10-18 14:20:12 -07001967 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001969 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001971 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001972 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001974 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04001975 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001977 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07001978 if (calldata->arg.seqid == NULL)
1979 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001980 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001981 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001982 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04001983 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001984 calldata->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04001985 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Al Virof6948692010-01-30 13:51:04 -05001986 path_get(path);
1987 calldata->path = *path;
Trond Myklebust95121352005-10-18 14:20:12 -07001988
Trond Myklebust5138fde2007-07-14 15:40:01 -04001989 msg.rpc_argp = &calldata->arg,
1990 msg.rpc_resp = &calldata->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001991 task_setup_data.callback_data = calldata;
1992 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001993 if (IS_ERR(task))
1994 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001995 status = 0;
1996 if (wait)
1997 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001998 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001999 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002000out_free_calldata:
2001 kfree(calldata);
2002out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002003 nfs4_put_open_state(state);
2004 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002005 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006}
2007
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002008static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state, fmode_t fmode)
Trond Myklebust02a913a2005-10-18 14:20:17 -07002009{
2010 struct file *filp;
Trond Myklebust1b45c462007-07-03 13:04:56 -04002011 int ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002012
Trond Myklebust1b45c462007-07-03 13:04:56 -04002013 /* If the open_intent is for execute, we have an extra check to make */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002014 if (fmode & FMODE_EXEC) {
Trond Myklebustaf22f942007-08-10 17:45:10 -04002015 ret = nfs_may_open(state->inode,
Trond Myklebust1b45c462007-07-03 13:04:56 -04002016 state->owner->so_cred,
2017 nd->intent.open.flags);
2018 if (ret < 0)
2019 goto out_close;
2020 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002021 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002022 if (!IS_ERR(filp)) {
2023 struct nfs_open_context *ctx;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04002024 ctx = nfs_file_open_context(filp);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002025 ctx->state = state;
Trond Myklebust95cf9592006-04-18 13:14:06 -04002026 return 0;
2027 }
Trond Myklebust1b45c462007-07-03 13:04:56 -04002028 ret = PTR_ERR(filp);
2029out_close:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002030 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
Trond Myklebust1b45c462007-07-03 13:04:56 -04002031 return ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002032}
2033
2034struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
2036{
Trond Myklebustad389da2007-06-05 12:30:00 -04002037 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002038 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002039 .dentry = dentry,
2040 };
Jan Blunck4ac91372008-02-14 19:34:32 -08002041 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 struct iattr attr;
2043 struct rpc_cred *cred;
2044 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002045 struct dentry *res;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002046 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
2048 if (nd->flags & LOOKUP_CREATE) {
2049 attr.ia_mode = nd->intent.open.create_mode;
2050 attr.ia_valid = ATTR_MODE;
2051 if (!IS_POSIXACL(dir))
Al Viroce3b0f82009-03-29 19:08:22 -04002052 attr.ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 } else {
2054 attr.ia_valid = 0;
2055 BUG_ON(nd->intent.open.flags & O_CREAT);
2056 }
2057
Trond Myklebust98a8e322008-03-12 12:25:28 -04002058 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07002060 return (struct dentry *)cred;
Trond Myklebust565277f2007-10-15 18:17:53 -04002061 parent = dentry->d_parent;
2062 /* Protect against concurrent sillydeletes */
2063 nfs_block_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002064 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002066 if (IS_ERR(state)) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04002067 if (PTR_ERR(state) == -ENOENT) {
Trond Myklebust02a913a2005-10-18 14:20:17 -07002068 d_add(dentry, NULL);
Trond Myklebustd75340c2007-10-01 21:42:01 -04002069 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2070 }
Trond Myklebust565277f2007-10-15 18:17:53 -04002071 nfs_unblock_sillyrename(parent);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002072 return (struct dentry *)state;
2073 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002074 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07002075 if (res != NULL)
Trond Myklebustdeee9362007-08-27 11:33:00 -04002076 path.dentry = res;
Trond Myklebustd75340c2007-10-01 21:42:01 -04002077 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
Trond Myklebust565277f2007-10-15 18:17:53 -04002078 nfs_unblock_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002079 nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002080 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081}
2082
2083int
Trond Myklebust02a913a2005-10-18 14:20:17 -07002084nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085{
Trond Myklebustad389da2007-06-05 12:30:00 -04002086 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002087 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002088 .dentry = dentry,
2089 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 struct rpc_cred *cred;
2091 struct nfs4_state *state;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002092 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
Trond Myklebust98a8e322008-03-12 12:25:28 -04002094 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 if (IS_ERR(cred))
2096 return PTR_ERR(cred);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002097 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002099 if (IS_ERR(state)) {
2100 switch (PTR_ERR(state)) {
2101 case -EPERM:
2102 case -EACCES:
2103 case -EDQUOT:
2104 case -ENOSPC:
2105 case -EROFS:
Al Viro04287f92010-04-08 00:06:07 +01002106 return PTR_ERR(state);
Chuck Leverb87c0ad2006-10-19 23:28:42 -07002107 default:
2108 goto out_drop;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002109 }
Trond Myklebust02a913a2005-10-18 14:20:17 -07002110 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002111 if (state->inode == dentry->d_inode) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04002112 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002113 nfs4_intent_set_file(nd, &path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 return 1;
2115 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002116 nfs4_close_sync(&path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002117out_drop:
2118 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 return 0;
2120}
2121
Trond Myklebust1185a552009-12-03 15:54:02 -05002122static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002123{
2124 if (ctx->state == NULL)
2125 return;
2126 if (is_sync)
2127 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2128 else
2129 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2130}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
2132static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2133{
Benny Halevy43652ad2009-04-01 09:21:54 -04002134 struct nfs4_server_caps_arg args = {
2135 .fhandle = fhandle,
2136 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 struct nfs4_server_caps_res res = {};
2138 struct rpc_message msg = {
2139 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002140 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 .rpc_resp = &res,
2142 };
2143 int status;
2144
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002145 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 if (status == 0) {
2147 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002148 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2149 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2150 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2151 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2152 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2154 server->caps |= NFS_CAP_ACLS;
2155 if (res.has_links != 0)
2156 server->caps |= NFS_CAP_HARDLINKS;
2157 if (res.has_symlinks != 0)
2158 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002159 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2160 server->caps |= NFS_CAP_FILEID;
2161 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2162 server->caps |= NFS_CAP_MODE;
2163 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2164 server->caps |= NFS_CAP_NLINK;
2165 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2166 server->caps |= NFS_CAP_OWNER;
2167 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2168 server->caps |= NFS_CAP_OWNER_GROUP;
2169 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2170 server->caps |= NFS_CAP_ATIME;
2171 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2172 server->caps |= NFS_CAP_CTIME;
2173 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2174 server->caps |= NFS_CAP_MTIME;
2175
Trond Myklebusta65318b2009-03-11 14:10:28 -04002176 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2177 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2178 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 server->acl_bitmask = res.acl_bitmask;
2180 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002181
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 return status;
2183}
2184
Trond Myklebust55a97592006-06-09 09:34:19 -04002185int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186{
2187 struct nfs4_exception exception = { };
2188 int err;
2189 do {
2190 err = nfs4_handle_exception(server,
2191 _nfs4_server_capabilities(server, fhandle),
2192 &exception);
2193 } while (exception.retry);
2194 return err;
2195}
2196
2197static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2198 struct nfs_fsinfo *info)
2199{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 struct nfs4_lookup_root_arg args = {
2201 .bitmask = nfs4_fattr_bitmap,
2202 };
2203 struct nfs4_lookup_res res = {
2204 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002205 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 .fh = fhandle,
2207 };
2208 struct rpc_message msg = {
2209 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2210 .rpc_argp = &args,
2211 .rpc_resp = &res,
2212 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002213
Trond Myklebust0e574af2005-10-27 22:12:38 -04002214 nfs_fattr_init(info->fattr);
Trond Myklebustfccba802009-07-21 16:48:07 -04002215 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216}
2217
2218static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2219 struct nfs_fsinfo *info)
2220{
2221 struct nfs4_exception exception = { };
2222 int err;
2223 do {
2224 err = nfs4_handle_exception(server,
2225 _nfs4_lookup_root(server, fhandle, info),
2226 &exception);
2227 } while (exception.retry);
2228 return err;
2229}
2230
David Howells54ceac42006-08-22 20:06:13 -04002231/*
2232 * get the file handle for the "/" directory on the server
2233 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002235 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 int status;
2238
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 if (status == 0)
2241 status = nfs4_server_capabilities(server, fhandle);
2242 if (status == 0)
2243 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002244 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245}
2246
Manoj Naik6b97fd32006-06-09 09:34:29 -04002247/*
2248 * Get locations and (maybe) other attributes of a referral.
2249 * Note that we'll actually follow the referral later when
2250 * we detect fsid mismatch in inode revalidation
2251 */
Trond Myklebust56659e92007-07-17 21:52:39 -04002252static 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 -04002253{
2254 int status = -ENOMEM;
2255 struct page *page = NULL;
2256 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002257
2258 page = alloc_page(GFP_KERNEL);
2259 if (page == NULL)
2260 goto out;
2261 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2262 if (locations == NULL)
2263 goto out;
2264
Trond Myklebustc228fd32007-01-13 02:28:11 -05002265 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002266 if (status != 0)
2267 goto out;
2268 /* Make sure server returned a different fsid for the referral */
2269 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07002270 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 -04002271 status = -EIO;
2272 goto out;
2273 }
2274
2275 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2276 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2277 if (!fattr->mode)
2278 fattr->mode = S_IFDIR;
2279 memset(fhandle, 0, sizeof(struct nfs_fh));
2280out:
2281 if (page)
2282 __free_page(page);
2283 if (locations)
2284 kfree(locations);
2285 return status;
2286}
2287
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2289{
2290 struct nfs4_getattr_arg args = {
2291 .fh = fhandle,
2292 .bitmask = server->attr_bitmask,
2293 };
2294 struct nfs4_getattr_res res = {
2295 .fattr = fattr,
2296 .server = server,
2297 };
2298 struct rpc_message msg = {
2299 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2300 .rpc_argp = &args,
2301 .rpc_resp = &res,
2302 };
2303
Trond Myklebust0e574af2005-10-27 22:12:38 -04002304 nfs_fattr_init(fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002305 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306}
2307
2308static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2309{
2310 struct nfs4_exception exception = { };
2311 int err;
2312 do {
2313 err = nfs4_handle_exception(server,
2314 _nfs4_proc_getattr(server, fhandle, fattr),
2315 &exception);
2316 } while (exception.retry);
2317 return err;
2318}
2319
2320/*
2321 * The file is not closed if it is opened due to the a request to change
2322 * the size of the file. The open call will not be needed once the
2323 * VFS layer lookup-intents are implemented.
2324 *
2325 * Close is called when the inode is destroyed.
2326 * If we haven't opened the file for O_WRONLY, we
2327 * need to in the size_change case to obtain a stateid.
2328 *
2329 * Got race?
2330 * Because OPEN is always done by name in nfsv4, it is
2331 * possible that we opened a different file by the same
2332 * name. We can recognize this race condition, but we
2333 * can't do anything about it besides returning an error.
2334 *
2335 * This will be fixed with VFS changes (lookup-intent).
2336 */
2337static int
2338nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2339 struct iattr *sattr)
2340{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002341 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002342 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002343 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 int status;
2345
Trond Myklebust0e574af2005-10-27 22:12:38 -04002346 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
Trond Myklebustd5308382005-11-04 15:33:38 -05002348 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002349 if (sattr->ia_valid & ATTR_FILE) {
2350 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002351
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002352 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002353 if (ctx) {
2354 cred = ctx->cred;
2355 state = ctx->state;
2356 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002357 }
2358
2359 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002360 if (status == 0)
2361 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 return status;
2363}
2364
Trond Myklebust56659e92007-07-17 21:52:39 -04002365static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2366 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04002367 struct nfs_fattr *fattr)
2368{
2369 int status;
2370 struct nfs4_lookup_arg args = {
2371 .bitmask = server->attr_bitmask,
2372 .dir_fh = dirfh,
2373 .name = name,
2374 };
2375 struct nfs4_lookup_res res = {
2376 .server = server,
2377 .fattr = fattr,
2378 .fh = fhandle,
2379 };
2380 struct rpc_message msg = {
2381 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2382 .rpc_argp = &args,
2383 .rpc_resp = &res,
2384 };
2385
2386 nfs_fattr_init(fattr);
2387
2388 dprintk("NFS call lookupfh %s\n", name->name);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002389 status = nfs4_call_sync(server, &msg, &args, &res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002390 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002391 return status;
2392}
2393
2394static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2395 struct qstr *name, struct nfs_fh *fhandle,
2396 struct nfs_fattr *fattr)
2397{
2398 struct nfs4_exception exception = { };
2399 int err;
2400 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04002401 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2402 /* FIXME: !!!! */
2403 if (err == -NFS4ERR_MOVED) {
2404 err = -EREMOTE;
2405 break;
2406 }
2407 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002408 } while (exception.retry);
2409 return err;
2410}
2411
Trond Myklebust56659e92007-07-17 21:52:39 -04002412static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2414{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002415 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
2417 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002418 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002419 if (status == -NFS4ERR_MOVED)
2420 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 dprintk("NFS reply lookup: %d\n", status);
2422 return status;
2423}
2424
2425static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2426{
2427 struct nfs4_exception exception = { };
2428 int err;
2429 do {
2430 err = nfs4_handle_exception(NFS_SERVER(dir),
2431 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2432 &exception);
2433 } while (exception.retry);
2434 return err;
2435}
2436
2437static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2438{
Trond Myklebust76b32992007-08-10 17:45:11 -04002439 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 struct nfs4_accessargs args = {
2441 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002442 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002444 struct nfs4_accessres res = {
2445 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002446 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 struct rpc_message msg = {
2448 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2449 .rpc_argp = &args,
2450 .rpc_resp = &res,
2451 .rpc_cred = entry->cred,
2452 };
2453 int mode = entry->mask;
2454 int status;
2455
2456 /*
2457 * Determine which access bits we want to ask for...
2458 */
2459 if (mode & MAY_READ)
2460 args.access |= NFS4_ACCESS_READ;
2461 if (S_ISDIR(inode->i_mode)) {
2462 if (mode & MAY_WRITE)
2463 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2464 if (mode & MAY_EXEC)
2465 args.access |= NFS4_ACCESS_LOOKUP;
2466 } else {
2467 if (mode & MAY_WRITE)
2468 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2469 if (mode & MAY_EXEC)
2470 args.access |= NFS4_ACCESS_EXECUTE;
2471 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002472
2473 res.fattr = nfs_alloc_fattr();
2474 if (res.fattr == NULL)
2475 return -ENOMEM;
2476
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002477 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 if (!status) {
2479 entry->mask = 0;
2480 if (res.access & NFS4_ACCESS_READ)
2481 entry->mask |= MAY_READ;
2482 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2483 entry->mask |= MAY_WRITE;
2484 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2485 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002486 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002488 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 return status;
2490}
2491
2492static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2493{
2494 struct nfs4_exception exception = { };
2495 int err;
2496 do {
2497 err = nfs4_handle_exception(NFS_SERVER(inode),
2498 _nfs4_proc_access(inode, entry),
2499 &exception);
2500 } while (exception.retry);
2501 return err;
2502}
2503
2504/*
2505 * TODO: For the time being, we don't try to get any attributes
2506 * along with any of the zero-copy operations READ, READDIR,
2507 * READLINK, WRITE.
2508 *
2509 * In the case of the first three, we want to put the GETATTR
2510 * after the read-type operation -- this is because it is hard
2511 * to predict the length of a GETATTR response in v4, and thus
2512 * align the READ data correctly. This means that the GETATTR
2513 * may end up partially falling into the page cache, and we should
2514 * shift it into the 'tail' of the xdr_buf before processing.
2515 * To do this efficiently, we need to know the total length
2516 * of data received, which doesn't seem to be available outside
2517 * of the RPC layer.
2518 *
2519 * In the case of WRITE, we also want to put the GETATTR after
2520 * the operation -- in this case because we want to make sure
2521 * we get the post-operation mtime and size. This means that
2522 * we can't use xdr_encode_pages() as written: we need a variant
2523 * of it which would leave room in the 'tail' iovec.
2524 *
2525 * Both of these changes to the XDR layer would in fact be quite
2526 * minor, but I decided to leave them for a subsequent patch.
2527 */
2528static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2529 unsigned int pgbase, unsigned int pglen)
2530{
2531 struct nfs4_readlink args = {
2532 .fh = NFS_FH(inode),
2533 .pgbase = pgbase,
2534 .pglen = pglen,
2535 .pages = &page,
2536 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002537 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 struct rpc_message msg = {
2539 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2540 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002541 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 };
2543
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002544 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545}
2546
2547static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2548 unsigned int pgbase, unsigned int pglen)
2549{
2550 struct nfs4_exception exception = { };
2551 int err;
2552 do {
2553 err = nfs4_handle_exception(NFS_SERVER(inode),
2554 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2555 &exception);
2556 } while (exception.retry);
2557 return err;
2558}
2559
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560/*
2561 * Got race?
2562 * We will need to arrange for the VFS layer to provide an atomic open.
2563 * Until then, this create/open method is prone to inefficiency and race
2564 * conditions due to the lookup, create, and open VFS calls from sys_open()
2565 * placed on the wire.
2566 *
2567 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2568 * The file will be opened again in the subsequent VFS open call
2569 * (nfs4_proc_file_open).
2570 *
2571 * The open for read will just hang around to be used by any process that
2572 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2573 */
2574
2575static int
2576nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07002577 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578{
Trond Myklebustad389da2007-06-05 12:30:00 -04002579 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002580 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002581 .dentry = dentry,
2582 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 struct nfs4_state *state;
2584 struct rpc_cred *cred;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002585 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 int status = 0;
2587
Trond Myklebust98a8e322008-03-12 12:25:28 -04002588 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 if (IS_ERR(cred)) {
2590 status = PTR_ERR(cred);
2591 goto out;
2592 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002593 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002594 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 if (IS_ERR(state)) {
2596 status = PTR_ERR(state);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002597 goto out_putcred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002599 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002600 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustad389da2007-06-05 12:30:00 -04002601 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002602 status = nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002603 else
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002604 nfs4_close_sync(&path, state, fmode);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002605out_putcred:
2606 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607out:
2608 return status;
2609}
2610
2611static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2612{
Trond Myklebust16e42952005-10-27 22:12:44 -04002613 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002614 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002616 .name.len = name->len,
2617 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002618 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002620 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002621 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002622 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002624 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2625 .rpc_argp = &args,
2626 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002628 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
Trond Myklebustd3468902010-04-16 16:22:50 -04002630 res.dir_attr = nfs_alloc_fattr();
2631 if (res.dir_attr == NULL)
2632 goto out;
2633
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002634 status = nfs4_call_sync(server, &msg, &args, &res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002635 if (status == 0) {
2636 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002637 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002638 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002639 nfs_free_fattr(res.dir_attr);
2640out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 return status;
2642}
2643
2644static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2645{
2646 struct nfs4_exception exception = { };
2647 int err;
2648 do {
2649 err = nfs4_handle_exception(NFS_SERVER(dir),
2650 _nfs4_proc_remove(dir, name),
2651 &exception);
2652 } while (exception.retry);
2653 return err;
2654}
2655
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002656static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002658 struct nfs_server *server = NFS_SERVER(dir);
2659 struct nfs_removeargs *args = msg->rpc_argp;
2660 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661
Trond Myklebusta65318b2009-03-11 14:10:28 -04002662 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002663 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665}
2666
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002667static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002669 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2670
Trond Myklebust14516c32010-07-31 14:29:06 -04002671 if (!nfs4_sequence_done(task, &res->seq_res))
2672 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002673 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002674 return 0;
2675 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002676 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002677 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678}
2679
2680static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2681 struct inode *new_dir, struct qstr *new_name)
2682{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002683 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 struct nfs4_rename_arg arg = {
2685 .old_dir = NFS_FH(old_dir),
2686 .new_dir = NFS_FH(new_dir),
2687 .old_name = old_name,
2688 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002689 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002691 struct nfs4_rename_res res = {
2692 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002693 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 struct rpc_message msg = {
2695 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2696 .rpc_argp = &arg,
2697 .rpc_resp = &res,
2698 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002699 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700
Trond Myklebust011fff72010-04-16 16:22:49 -04002701 res.old_fattr = nfs_alloc_fattr();
2702 res.new_fattr = nfs_alloc_fattr();
2703 if (res.old_fattr == NULL || res.new_fattr == NULL)
2704 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705
Trond Myklebust011fff72010-04-16 16:22:49 -04002706 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 if (!status) {
2708 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002709 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002711 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002713out:
2714 nfs_free_fattr(res.new_fattr);
2715 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 return status;
2717}
2718
2719static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2720 struct inode *new_dir, struct qstr *new_name)
2721{
2722 struct nfs4_exception exception = { };
2723 int err;
2724 do {
2725 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2726 _nfs4_proc_rename(old_dir, old_name,
2727 new_dir, new_name),
2728 &exception);
2729 } while (exception.retry);
2730 return err;
2731}
2732
2733static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2734{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002735 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 struct nfs4_link_arg arg = {
2737 .fh = NFS_FH(inode),
2738 .dir_fh = NFS_FH(dir),
2739 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002740 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002742 struct nfs4_link_res res = {
2743 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002744 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 struct rpc_message msg = {
2746 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2747 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002748 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002750 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751
Trond Myklebust136f2622010-04-16 16:22:49 -04002752 res.fattr = nfs_alloc_fattr();
2753 res.dir_attr = nfs_alloc_fattr();
2754 if (res.fattr == NULL || res.dir_attr == NULL)
2755 goto out;
2756
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002757 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002758 if (!status) {
2759 update_changeattr(dir, &res.cinfo);
2760 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002761 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002762 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002763out:
2764 nfs_free_fattr(res.dir_attr);
2765 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 return status;
2767}
2768
2769static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2770{
2771 struct nfs4_exception exception = { };
2772 int err;
2773 do {
2774 err = nfs4_handle_exception(NFS_SERVER(inode),
2775 _nfs4_proc_link(inode, dir, name),
2776 &exception);
2777 } while (exception.retry);
2778 return err;
2779}
2780
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002781struct nfs4_createdata {
2782 struct rpc_message msg;
2783 struct nfs4_create_arg arg;
2784 struct nfs4_create_res res;
2785 struct nfs_fh fh;
2786 struct nfs_fattr fattr;
2787 struct nfs_fattr dir_fattr;
2788};
2789
2790static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2791 struct qstr *name, struct iattr *sattr, u32 ftype)
2792{
2793 struct nfs4_createdata *data;
2794
2795 data = kzalloc(sizeof(*data), GFP_KERNEL);
2796 if (data != NULL) {
2797 struct nfs_server *server = NFS_SERVER(dir);
2798
2799 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2800 data->msg.rpc_argp = &data->arg;
2801 data->msg.rpc_resp = &data->res;
2802 data->arg.dir_fh = NFS_FH(dir);
2803 data->arg.server = server;
2804 data->arg.name = name;
2805 data->arg.attrs = sattr;
2806 data->arg.ftype = ftype;
2807 data->arg.bitmask = server->attr_bitmask;
2808 data->res.server = server;
2809 data->res.fh = &data->fh;
2810 data->res.fattr = &data->fattr;
2811 data->res.dir_fattr = &data->dir_fattr;
2812 nfs_fattr_init(data->res.fattr);
2813 nfs_fattr_init(data->res.dir_fattr);
2814 }
2815 return data;
2816}
2817
2818static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2819{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002820 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2821 &data->arg, &data->res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002822 if (status == 0) {
2823 update_changeattr(dir, &data->res.dir_cinfo);
2824 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2825 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2826 }
2827 return status;
2828}
2829
2830static void nfs4_free_createdata(struct nfs4_createdata *data)
2831{
2832 kfree(data);
2833}
2834
Chuck Lever4f390c12006-08-22 20:06:22 -04002835static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002836 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002838 struct nfs4_createdata *data;
2839 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840
Chuck Lever94a6d752006-08-22 20:06:23 -04002841 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002842 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002843
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002844 status = -ENOMEM;
2845 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2846 if (data == NULL)
2847 goto out;
2848
2849 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2850 data->arg.u.symlink.pages = &page;
2851 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002853 status = nfs4_do_create(dir, dentry, data);
2854
2855 nfs4_free_createdata(data);
2856out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 return status;
2858}
2859
Chuck Lever4f390c12006-08-22 20:06:22 -04002860static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002861 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862{
2863 struct nfs4_exception exception = { };
2864 int err;
2865 do {
2866 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002867 _nfs4_proc_symlink(dir, dentry, page,
2868 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 &exception);
2870 } while (exception.retry);
2871 return err;
2872}
2873
2874static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2875 struct iattr *sattr)
2876{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002877 struct nfs4_createdata *data;
2878 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002880 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2881 if (data == NULL)
2882 goto out;
2883
2884 status = nfs4_do_create(dir, dentry, data);
2885
2886 nfs4_free_createdata(data);
2887out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 return status;
2889}
2890
2891static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2892 struct iattr *sattr)
2893{
2894 struct nfs4_exception exception = { };
2895 int err;
2896 do {
2897 err = nfs4_handle_exception(NFS_SERVER(dir),
2898 _nfs4_proc_mkdir(dir, dentry, sattr),
2899 &exception);
2900 } while (exception.retry);
2901 return err;
2902}
2903
2904static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2905 u64 cookie, struct page *page, unsigned int count, int plus)
2906{
2907 struct inode *dir = dentry->d_inode;
2908 struct nfs4_readdir_arg args = {
2909 .fh = NFS_FH(dir),
2910 .pages = &page,
2911 .pgbase = 0,
2912 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002913 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 };
2915 struct nfs4_readdir_res res;
2916 struct rpc_message msg = {
2917 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2918 .rpc_argp = &args,
2919 .rpc_resp = &res,
2920 .rpc_cred = cred,
2921 };
2922 int status;
2923
Harvey Harrison3110ff82008-05-02 13:42:44 -07002924 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002925 dentry->d_parent->d_name.name,
2926 dentry->d_name.name,
2927 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2929 res.pgbase = args.pgbase;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002930 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 if (status == 0)
2932 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustc4812992007-09-28 17:11:45 -04002933
2934 nfs_invalidate_atime(dir);
2935
Harvey Harrison3110ff82008-05-02 13:42:44 -07002936 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 return status;
2938}
2939
2940static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2941 u64 cookie, struct page *page, unsigned int count, int plus)
2942{
2943 struct nfs4_exception exception = { };
2944 int err;
2945 do {
2946 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2947 _nfs4_proc_readdir(dentry, cred, cookie,
2948 page, count, plus),
2949 &exception);
2950 } while (exception.retry);
2951 return err;
2952}
2953
2954static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2955 struct iattr *sattr, dev_t rdev)
2956{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002957 struct nfs4_createdata *data;
2958 int mode = sattr->ia_mode;
2959 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960
2961 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2962 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002963
2964 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2965 if (data == NULL)
2966 goto out;
2967
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002969 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002971 data->arg.ftype = NF4BLK;
2972 data->arg.u.device.specdata1 = MAJOR(rdev);
2973 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 }
2975 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002976 data->arg.ftype = NF4CHR;
2977 data->arg.u.device.specdata1 = MAJOR(rdev);
2978 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002981 status = nfs4_do_create(dir, dentry, data);
2982
2983 nfs4_free_createdata(data);
2984out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 return status;
2986}
2987
2988static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2989 struct iattr *sattr, dev_t rdev)
2990{
2991 struct nfs4_exception exception = { };
2992 int err;
2993 do {
2994 err = nfs4_handle_exception(NFS_SERVER(dir),
2995 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2996 &exception);
2997 } while (exception.retry);
2998 return err;
2999}
3000
3001static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3002 struct nfs_fsstat *fsstat)
3003{
3004 struct nfs4_statfs_arg args = {
3005 .fh = fhandle,
3006 .bitmask = server->attr_bitmask,
3007 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003008 struct nfs4_statfs_res res = {
3009 .fsstat = fsstat,
3010 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 struct rpc_message msg = {
3012 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3013 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003014 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 };
3016
Trond Myklebust0e574af2005-10-27 22:12:38 -04003017 nfs_fattr_init(fsstat->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003018 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019}
3020
3021static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3022{
3023 struct nfs4_exception exception = { };
3024 int err;
3025 do {
3026 err = nfs4_handle_exception(server,
3027 _nfs4_proc_statfs(server, fhandle, fsstat),
3028 &exception);
3029 } while (exception.retry);
3030 return err;
3031}
3032
3033static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3034 struct nfs_fsinfo *fsinfo)
3035{
3036 struct nfs4_fsinfo_arg args = {
3037 .fh = fhandle,
3038 .bitmask = server->attr_bitmask,
3039 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003040 struct nfs4_fsinfo_res res = {
3041 .fsinfo = fsinfo,
3042 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 struct rpc_message msg = {
3044 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3045 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003046 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 };
3048
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003049 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050}
3051
3052static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3053{
3054 struct nfs4_exception exception = { };
3055 int err;
3056
3057 do {
3058 err = nfs4_handle_exception(server,
3059 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3060 &exception);
3061 } while (exception.retry);
3062 return err;
3063}
3064
3065static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3066{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003067 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3069}
3070
3071static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3072 struct nfs_pathconf *pathconf)
3073{
3074 struct nfs4_pathconf_arg args = {
3075 .fh = fhandle,
3076 .bitmask = server->attr_bitmask,
3077 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003078 struct nfs4_pathconf_res res = {
3079 .pathconf = pathconf,
3080 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 struct rpc_message msg = {
3082 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3083 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003084 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 };
3086
3087 /* None of the pathconf attributes are mandatory to implement */
3088 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3089 memset(pathconf, 0, sizeof(*pathconf));
3090 return 0;
3091 }
3092
Trond Myklebust0e574af2005-10-27 22:12:38 -04003093 nfs_fattr_init(pathconf->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003094 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095}
3096
3097static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3098 struct nfs_pathconf *pathconf)
3099{
3100 struct nfs4_exception exception = { };
3101 int err;
3102
3103 do {
3104 err = nfs4_handle_exception(server,
3105 _nfs4_proc_pathconf(server, fhandle, pathconf),
3106 &exception);
3107 } while (exception.retry);
3108 return err;
3109}
3110
Trond Myklebustec06c092006-03-20 13:44:27 -05003111static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112{
Trond Myklebustec06c092006-03-20 13:44:27 -05003113 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003115 dprintk("--> %s\n", __func__);
3116
Trond Myklebust14516c32010-07-31 14:29:06 -04003117 if (!nfs4_sequence_done(task, &data->res.seq_res))
3118 return -EAGAIN;
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003119
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003120 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003121 nfs_restart_rpc(task, server->nfs_client);
Trond Myklebustec06c092006-03-20 13:44:27 -05003122 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003124
3125 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003127 renew_lease(server, data->timestamp);
3128 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129}
3130
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003131static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 data->timestamp = jiffies;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003134 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135}
3136
Trond Myklebust788e7a82006-03-20 13:44:27 -05003137static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 struct inode *inode = data->inode;
3140
Trond Myklebust14516c32010-07-31 14:29:06 -04003141 if (!nfs4_sequence_done(task, &data->res.seq_res))
3142 return -EAGAIN;
Andy Adamsondef6ed72009-04-01 09:22:26 -04003143
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003144 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003145 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003146 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003148 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003150 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003151 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003152 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153}
3154
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003155static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003157 struct nfs_server *server = NFS_SERVER(data->inode);
3158
Trond Myklebusta65318b2009-03-11 14:10:28 -04003159 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003160 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 data->timestamp = jiffies;
3162
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003163 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164}
3165
Trond Myklebust788e7a82006-03-20 13:44:27 -05003166static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 struct inode *inode = data->inode;
3169
Trond Myklebust14516c32010-07-31 14:29:06 -04003170 if (!nfs4_sequence_done(task, &data->res.seq_res))
3171 return -EAGAIN;
3172
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003173 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003174 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003175 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003177 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003178 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179}
3180
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003181static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003183 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184
Trond Myklebusta65318b2009-03-11 14:10:28 -04003185 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003186 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003187 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188}
3189
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003190struct nfs4_renewdata {
3191 struct nfs_client *client;
3192 unsigned long timestamp;
3193};
3194
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195/*
3196 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3197 * standalone procedure for queueing an asynchronous RENEW.
3198 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003199static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003200{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003201 struct nfs4_renewdata *data = calldata;
3202 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003203
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003204 if (atomic_read(&clp->cl_count) > 1)
3205 nfs4_schedule_state_renewal(clp);
3206 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003207 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003208}
3209
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003210static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003212 struct nfs4_renewdata *data = calldata;
3213 struct nfs_client *clp = data->client;
3214 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215
3216 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003217 /* Unless we're shutting down, schedule state recovery! */
3218 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3219 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 return;
3221 }
3222 spin_lock(&clp->cl_lock);
3223 if (time_before(clp->cl_last_renewal,timestamp))
3224 clp->cl_last_renewal = timestamp;
3225 spin_unlock(&clp->cl_lock);
3226}
3227
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003228static const struct rpc_call_ops nfs4_renew_ops = {
3229 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003230 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003231};
3232
David Howellsadfa6f92006-08-22 20:06:08 -04003233int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234{
3235 struct rpc_message msg = {
3236 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3237 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003238 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003240 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003242 if (!atomic_inc_not_zero(&clp->cl_count))
3243 return -EIO;
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003244 data = kmalloc(sizeof(*data), GFP_KERNEL);
3245 if (data == NULL)
3246 return -ENOMEM;
3247 data->client = clp;
3248 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003250 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251}
3252
David Howellsadfa6f92006-08-22 20:06:08 -04003253int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254{
3255 struct rpc_message msg = {
3256 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3257 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003258 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 };
3260 unsigned long now = jiffies;
3261 int status;
3262
3263 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3264 if (status < 0)
3265 return status;
3266 spin_lock(&clp->cl_lock);
3267 if (time_before(clp->cl_last_renewal,now))
3268 clp->cl_last_renewal = now;
3269 spin_unlock(&clp->cl_lock);
3270 return 0;
3271}
3272
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003273static inline int nfs4_server_supports_acls(struct nfs_server *server)
3274{
3275 return (server->caps & NFS_CAP_ACLS)
3276 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3277 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3278}
3279
3280/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3281 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3282 * the stack.
3283 */
3284#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3285
3286static void buf_to_pages(const void *buf, size_t buflen,
3287 struct page **pages, unsigned int *pgbase)
3288{
3289 const void *p = buf;
3290
3291 *pgbase = offset_in_page(buf);
3292 p -= *pgbase;
3293 while (p < buf + buflen) {
3294 *(pages++) = virt_to_page(p);
3295 p += PAGE_CACHE_SIZE;
3296 }
3297}
3298
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003299struct nfs4_cached_acl {
3300 int cached;
3301 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003302 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003303};
3304
3305static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003306{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003307 struct nfs_inode *nfsi = NFS_I(inode);
3308
3309 spin_lock(&inode->i_lock);
3310 kfree(nfsi->nfs4_acl);
3311 nfsi->nfs4_acl = acl;
3312 spin_unlock(&inode->i_lock);
3313}
3314
3315static void nfs4_zap_acl_attr(struct inode *inode)
3316{
3317 nfs4_set_cached_acl(inode, NULL);
3318}
3319
3320static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3321{
3322 struct nfs_inode *nfsi = NFS_I(inode);
3323 struct nfs4_cached_acl *acl;
3324 int ret = -ENOENT;
3325
3326 spin_lock(&inode->i_lock);
3327 acl = nfsi->nfs4_acl;
3328 if (acl == NULL)
3329 goto out;
3330 if (buf == NULL) /* user is just asking for length */
3331 goto out_len;
3332 if (acl->cached == 0)
3333 goto out;
3334 ret = -ERANGE; /* see getxattr(2) man page */
3335 if (acl->len > buflen)
3336 goto out;
3337 memcpy(buf, acl->data, acl->len);
3338out_len:
3339 ret = acl->len;
3340out:
3341 spin_unlock(&inode->i_lock);
3342 return ret;
3343}
3344
3345static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3346{
3347 struct nfs4_cached_acl *acl;
3348
3349 if (buf && acl_len <= PAGE_SIZE) {
3350 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3351 if (acl == NULL)
3352 goto out;
3353 acl->cached = 1;
3354 memcpy(acl->data, buf, acl_len);
3355 } else {
3356 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3357 if (acl == NULL)
3358 goto out;
3359 acl->cached = 0;
3360 }
3361 acl->len = acl_len;
3362out:
3363 nfs4_set_cached_acl(inode, acl);
3364}
3365
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003366static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003367{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003368 struct page *pages[NFS4ACL_MAXPAGES];
3369 struct nfs_getaclargs args = {
3370 .fh = NFS_FH(inode),
3371 .acl_pages = pages,
3372 .acl_len = buflen,
3373 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003374 struct nfs_getaclres res = {
3375 .acl_len = buflen,
3376 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003377 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003378 struct rpc_message msg = {
3379 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3380 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003381 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003382 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003383 struct page *localpage = NULL;
3384 int ret;
3385
3386 if (buflen < PAGE_SIZE) {
3387 /* As long as we're doing a round trip to the server anyway,
3388 * let's be prepared for a page of acl data. */
3389 localpage = alloc_page(GFP_KERNEL);
3390 resp_buf = page_address(localpage);
3391 if (localpage == NULL)
3392 return -ENOMEM;
3393 args.acl_pages[0] = localpage;
3394 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003395 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003396 } else {
3397 resp_buf = buf;
3398 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3399 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003400 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003401 if (ret)
3402 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003403 if (res.acl_len > args.acl_len)
3404 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003405 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003406 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003407 if (buf) {
3408 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003409 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003410 goto out_free;
3411 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003412 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003413 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003414 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003415out_free:
3416 if (localpage)
3417 __free_page(localpage);
3418 return ret;
3419}
3420
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003421static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3422{
3423 struct nfs4_exception exception = { };
3424 ssize_t ret;
3425 do {
3426 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3427 if (ret >= 0)
3428 break;
3429 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3430 } while (exception.retry);
3431 return ret;
3432}
3433
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003434static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3435{
3436 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003437 int ret;
3438
3439 if (!nfs4_server_supports_acls(server))
3440 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003441 ret = nfs_revalidate_inode(server, inode);
3442 if (ret < 0)
3443 return ret;
3444 ret = nfs4_read_cached_acl(inode, buf, buflen);
3445 if (ret != -ENOENT)
3446 return ret;
3447 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003448}
3449
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003450static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003451{
3452 struct nfs_server *server = NFS_SERVER(inode);
3453 struct page *pages[NFS4ACL_MAXPAGES];
3454 struct nfs_setaclargs arg = {
3455 .fh = NFS_FH(inode),
3456 .acl_pages = pages,
3457 .acl_len = buflen,
3458 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003459 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003460 struct rpc_message msg = {
3461 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3462 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003463 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003464 };
3465 int ret;
3466
3467 if (!nfs4_server_supports_acls(server))
3468 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07003469 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003470 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003471 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003472 nfs_access_zap_cache(inode);
3473 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003474 return ret;
3475}
3476
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003477static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3478{
3479 struct nfs4_exception exception = { };
3480 int err;
3481 do {
3482 err = nfs4_handle_exception(NFS_SERVER(inode),
3483 __nfs4_proc_set_acl(inode, buf, buflen),
3484 &exception);
3485 } while (exception.retry);
3486 return err;
3487}
3488
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003490nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003492 struct nfs_client *clp = server->nfs_client;
3493
3494 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 return 0;
3496 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003497 case -NFS4ERR_ADMIN_REVOKED:
3498 case -NFS4ERR_BAD_STATEID:
3499 case -NFS4ERR_OPENMODE:
3500 if (state == NULL)
3501 break;
3502 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003503 goto do_state_recovery;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003505 if (state == NULL)
3506 break;
3507 nfs4_state_mark_reclaim_reboot(clp, state);
3508 case -NFS4ERR_STALE_CLIENTID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 case -NFS4ERR_EXPIRED:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003510 goto do_state_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003511#if defined(CONFIG_NFS_V4_1)
3512 case -NFS4ERR_BADSESSION:
3513 case -NFS4ERR_BADSLOT:
3514 case -NFS4ERR_BAD_HIGH_SLOT:
3515 case -NFS4ERR_DEADSESSION:
3516 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3517 case -NFS4ERR_SEQ_FALSE_RETRY:
3518 case -NFS4ERR_SEQ_MISORDERED:
3519 dprintk("%s ERROR %d, Reset session\n", __func__,
3520 task->tk_status);
Andy Adamson0b9e2d42009-12-04 16:02:14 -05003521 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -04003522 task->tk_status = 0;
3523 return -EAGAIN;
3524#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003526 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003527 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003528 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3530 task->tk_status = 0;
3531 return -EAGAIN;
3532 case -NFS4ERR_OLD_STATEID:
3533 task->tk_status = 0;
3534 return -EAGAIN;
3535 }
3536 task->tk_status = nfs4_map_errors(task->tk_status);
3537 return 0;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003538do_state_recovery:
3539 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3540 nfs4_schedule_state_recovery(clp);
3541 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3542 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3543 task->tk_status = 0;
3544 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545}
3546
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003547int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3548 unsigned short port, struct rpc_cred *cred,
3549 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550{
3551 nfs4_verifier sc_verifier;
3552 struct nfs4_setclientid setclientid = {
3553 .sc_verifier = &sc_verifier,
3554 .sc_prog = program,
3555 };
3556 struct rpc_message msg = {
3557 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3558 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003559 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003560 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 };
Al Virobc4785c2006-10-19 23:28:51 -07003562 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 int loop = 0;
3564 int status;
3565
Al Virobc4785c2006-10-19 23:28:51 -07003566 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3568 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3569
3570 for(;;) {
3571 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003572 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003573 clp->cl_ipaddr,
3574 rpc_peeraddr2str(clp->cl_rpcclient,
3575 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003576 rpc_peeraddr2str(clp->cl_rpcclient,
3577 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003578 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 clp->cl_id_uniquifier);
3580 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003581 sizeof(setclientid.sc_netid),
3582 rpc_peeraddr2str(clp->cl_rpcclient,
3583 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003585 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 clp->cl_ipaddr, port >> 8, port & 255);
3587
3588 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3589 if (status != -NFS4ERR_CLID_INUSE)
3590 break;
3591 if (signalled())
3592 break;
3593 if (loop++ & 1)
3594 ssleep(clp->cl_lease_time + 1);
3595 else
3596 if (++clp->cl_id_uniquifier == 0)
3597 break;
3598 }
3599 return status;
3600}
3601
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003602static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3603 struct nfs4_setclientid_res *arg,
3604 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605{
3606 struct nfs_fsinfo fsinfo;
3607 struct rpc_message msg = {
3608 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003609 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003611 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 };
3613 unsigned long now;
3614 int status;
3615
3616 now = jiffies;
3617 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3618 if (status == 0) {
3619 spin_lock(&clp->cl_lock);
3620 clp->cl_lease_time = fsinfo.lease_time * HZ;
3621 clp->cl_last_renewal = now;
3622 spin_unlock(&clp->cl_lock);
3623 }
3624 return status;
3625}
3626
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003627int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3628 struct nfs4_setclientid_res *arg,
3629 struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003630{
Benny Halevy77e03672008-06-24 20:25:57 +03003631 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003632 int err;
3633 do {
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003634 err = _nfs4_proc_setclientid_confirm(clp, arg, cred);
Trond Myklebust51581f32006-03-20 13:44:47 -05003635 switch (err) {
3636 case 0:
3637 return err;
3638 case -NFS4ERR_RESOURCE:
3639 /* The IBM lawyers misread another document! */
3640 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05003641 case -EKEYEXPIRED:
Trond Myklebust51581f32006-03-20 13:44:47 -05003642 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3643 }
3644 } while (err == 0);
3645 return err;
3646}
3647
Trond Myklebustfe650402006-01-03 09:55:18 +01003648struct nfs4_delegreturndata {
3649 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003650 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003651 struct nfs_fh fh;
3652 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003653 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003654 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003655 int rpc_status;
3656};
3657
Trond Myklebustfe650402006-01-03 09:55:18 +01003658static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3659{
3660 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003661
Trond Myklebust14516c32010-07-31 14:29:06 -04003662 if (!nfs4_sequence_done(task, &data->res.seq_res))
3663 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003664
Ricardo Labiaga79708862009-12-07 09:23:21 -05003665 switch (task->tk_status) {
3666 case -NFS4ERR_STALE_STATEID:
3667 case -NFS4ERR_EXPIRED:
3668 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003669 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003670 break;
3671 default:
3672 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3673 -EAGAIN) {
3674 nfs_restart_rpc(task, data->res.server->nfs_client);
3675 return;
3676 }
3677 }
3678 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003679}
3680
3681static void nfs4_delegreturn_release(void *calldata)
3682{
Trond Myklebustfe650402006-01-03 09:55:18 +01003683 kfree(calldata);
3684}
3685
Andy Adamson938e1012009-04-01 09:22:28 -04003686#if defined(CONFIG_NFS_V4_1)
3687static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3688{
3689 struct nfs4_delegreturndata *d_data;
3690
3691 d_data = (struct nfs4_delegreturndata *)data;
3692
Trond Myklebust035168a2010-06-16 09:52:26 -04003693 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003694 &d_data->args.seq_args,
3695 &d_data->res.seq_res, 1, task))
3696 return;
3697 rpc_call_start(task);
3698}
3699#endif /* CONFIG_NFS_V4_1 */
3700
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003701static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003702#if defined(CONFIG_NFS_V4_1)
3703 .rpc_call_prepare = nfs4_delegreturn_prepare,
3704#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003705 .rpc_call_done = nfs4_delegreturn_done,
3706 .rpc_release = nfs4_delegreturn_release,
3707};
3708
Trond Myklebuste6f81072008-01-24 18:14:34 -05003709static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003710{
3711 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003712 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003713 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003714 struct rpc_message msg = {
3715 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3716 .rpc_cred = cred,
3717 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003718 struct rpc_task_setup task_setup_data = {
3719 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003720 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003721 .callback_ops = &nfs4_delegreturn_ops,
3722 .flags = RPC_TASK_ASYNC,
3723 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003724 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003725
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003726 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003727 if (data == NULL)
3728 return -ENOMEM;
3729 data->args.fhandle = &data->fh;
3730 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003731 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003732 nfs_copy_fh(&data->fh, NFS_FH(inode));
3733 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003734 data->res.fattr = &data->fattr;
3735 data->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003736 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003737 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003738 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003739 data->rpc_status = 0;
3740
Trond Myklebustc970aa82007-07-14 15:39:59 -04003741 task_setup_data.callback_data = data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003742 msg.rpc_argp = &data->args,
3743 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003744 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003745 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003746 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003747 if (!issync)
3748 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003749 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003750 if (status != 0)
3751 goto out;
3752 status = data->rpc_status;
3753 if (status != 0)
3754 goto out;
3755 nfs_refresh_inode(inode, &data->fattr);
3756out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003757 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003758 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759}
3760
Trond Myklebuste6f81072008-01-24 18:14:34 -05003761int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762{
3763 struct nfs_server *server = NFS_SERVER(inode);
3764 struct nfs4_exception exception = { };
3765 int err;
3766 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003767 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 switch (err) {
3769 case -NFS4ERR_STALE_STATEID:
3770 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 case 0:
3772 return 0;
3773 }
3774 err = nfs4_handle_exception(server, err, &exception);
3775 } while (exception.retry);
3776 return err;
3777}
3778
3779#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3780#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3781
3782/*
3783 * sleep, with exponential backoff, and retry the LOCK operation.
3784 */
3785static unsigned long
3786nfs4_set_lock_task_retry(unsigned long timeout)
3787{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003788 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 timeout <<= 1;
3790 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3791 return NFS4_LOCK_MAXTIMEOUT;
3792 return timeout;
3793}
3794
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3796{
3797 struct inode *inode = state->inode;
3798 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003799 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003800 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003802 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003804 struct nfs_lockt_res res = {
3805 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 };
3807 struct rpc_message msg = {
3808 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3809 .rpc_argp = &arg,
3810 .rpc_resp = &res,
3811 .rpc_cred = state->owner->so_cred,
3812 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 struct nfs4_lock_state *lsp;
3814 int status;
3815
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003816 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003817 status = nfs4_set_lock_state(state, request);
3818 if (status != 0)
3819 goto out;
3820 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003821 arg.lock_owner.id = lsp->ls_id.id;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003822 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003823 switch (status) {
3824 case 0:
3825 request->fl_type = F_UNLCK;
3826 break;
3827 case -NFS4ERR_DENIED:
3828 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003830 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003831out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 return status;
3833}
3834
3835static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3836{
3837 struct nfs4_exception exception = { };
3838 int err;
3839
3840 do {
3841 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3842 _nfs4_proc_getlk(state, cmd, request),
3843 &exception);
3844 } while (exception.retry);
3845 return err;
3846}
3847
3848static int do_vfs_lock(struct file *file, struct file_lock *fl)
3849{
3850 int res = 0;
3851 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3852 case FL_POSIX:
3853 res = posix_lock_file_wait(file, fl);
3854 break;
3855 case FL_FLOCK:
3856 res = flock_lock_file_wait(file, fl);
3857 break;
3858 default:
3859 BUG();
3860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 return res;
3862}
3863
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003864struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003865 struct nfs_locku_args arg;
3866 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003867 struct nfs4_lock_state *lsp;
3868 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003869 struct file_lock fl;
3870 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003871 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003872};
3873
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003874static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3875 struct nfs_open_context *ctx,
3876 struct nfs4_lock_state *lsp,
3877 struct nfs_seqid *seqid)
3878{
3879 struct nfs4_unlockdata *p;
3880 struct inode *inode = lsp->ls_state->inode;
3881
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003882 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003883 if (p == NULL)
3884 return NULL;
3885 p->arg.fh = NFS_FH(inode);
3886 p->arg.fl = &p->fl;
3887 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003888 p->res.seqid = seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003889 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003890 p->arg.stateid = &lsp->ls_stateid;
3891 p->lsp = lsp;
3892 atomic_inc(&lsp->ls_count);
3893 /* Ensure we don't close file until we're done freeing locks! */
3894 p->ctx = get_nfs_open_context(ctx);
3895 memcpy(&p->fl, fl, sizeof(p->fl));
3896 p->server = NFS_SERVER(inode);
3897 return p;
3898}
3899
Trond Myklebust06f814a2006-01-03 09:55:07 +01003900static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003901{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003902 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003903 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003904 nfs4_put_lock_state(calldata->lsp);
3905 put_nfs_open_context(calldata->ctx);
3906 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003907}
3908
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003909static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003910{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003911 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003912
Trond Myklebust14516c32010-07-31 14:29:06 -04003913 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3914 return;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003915 if (RPC_ASSASSINATED(task))
3916 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003917 switch (task->tk_status) {
3918 case 0:
3919 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003920 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003921 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003922 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003923 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003924 case -NFS4ERR_BAD_STATEID:
3925 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003926 case -NFS4ERR_STALE_STATEID:
3927 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003928 break;
3929 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003930 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebust0110ee12009-12-07 09:00:24 -05003931 nfs_restart_rpc(task,
Trond Myklebustd61e6122009-12-05 19:32:19 -05003932 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003933 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003934}
3935
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003936static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003937{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003938 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003940 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003941 return;
3942 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003943 /* Note: exit _without_ running nfs4_locku_done */
3944 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003945 return;
3946 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003947 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04003948 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04003949 &calldata->arg.seq_args,
3950 &calldata->res.seq_res, 1, task))
3951 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003952 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953}
3954
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003955static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003956 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003957 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003958 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003959};
3960
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003961static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3962 struct nfs_open_context *ctx,
3963 struct nfs4_lock_state *lsp,
3964 struct nfs_seqid *seqid)
3965{
3966 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003967 struct rpc_message msg = {
3968 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3969 .rpc_cred = ctx->cred,
3970 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003971 struct rpc_task_setup task_setup_data = {
3972 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003973 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003974 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003975 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003976 .flags = RPC_TASK_ASYNC,
3977 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003978
Frank Filz137d6ac2007-07-09 15:32:29 -07003979 /* Ensure this is an unlock - when canceling a lock, the
3980 * canceled lock is passed in, and it won't be an unlock.
3981 */
3982 fl->fl_type = F_UNLCK;
3983
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003984 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3985 if (data == NULL) {
3986 nfs_free_seqid(seqid);
3987 return ERR_PTR(-ENOMEM);
3988 }
3989
Trond Myklebust5138fde2007-07-14 15:40:01 -04003990 msg.rpc_argp = &data->arg,
3991 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003992 task_setup_data.callback_data = data;
3993 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003994}
3995
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3997{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003998 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003999 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004000 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004001 struct rpc_task *task;
4002 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004003 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004
Trond Myklebust9b073572006-06-29 16:38:34 -04004005 status = nfs4_set_lock_state(state, request);
4006 /* Unlock _before_ we do the RPC call */
4007 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004008 down_read(&nfsi->rwsem);
4009 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4010 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004011 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004012 }
4013 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004014 if (status != 0)
4015 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004016 /* Is this a delegated lock? */
4017 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004018 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004019 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004020 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004021 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004022 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004023 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004024 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004025 status = PTR_ERR(task);
4026 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004027 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004028 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004029 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004030out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004031 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004032 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033}
4034
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004035struct nfs4_lockdata {
4036 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004037 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004038 struct nfs4_lock_state *lsp;
4039 struct nfs_open_context *ctx;
4040 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004041 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004042 int rpc_status;
4043 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004044 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004045};
4046
4047static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004048 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4049 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004050{
4051 struct nfs4_lockdata *p;
4052 struct inode *inode = lsp->ls_state->inode;
4053 struct nfs_server *server = NFS_SERVER(inode);
4054
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004055 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004056 if (p == NULL)
4057 return NULL;
4058
4059 p->arg.fh = NFS_FH(inode);
4060 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004061 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004062 if (p->arg.open_seqid == NULL)
4063 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004064 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004065 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004066 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004067 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004068 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004069 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004070 p->res.lock_seqid = p->arg.lock_seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04004071 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004072 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004073 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004074 atomic_inc(&lsp->ls_count);
4075 p->ctx = get_nfs_open_context(ctx);
4076 memcpy(&p->fl, fl, sizeof(p->fl));
4077 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004078out_free_seqid:
4079 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004080out_free:
4081 kfree(p);
4082 return NULL;
4083}
4084
4085static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4086{
4087 struct nfs4_lockdata *data = calldata;
4088 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089
Harvey Harrison3110ff82008-05-02 13:42:44 -07004090 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004091 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4092 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004093 /* Do we need to do an open_to_lock_owner? */
4094 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004095 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4096 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004097 data->arg.open_stateid = &state->stateid;
4098 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004099 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004100 } else
4101 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004102 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004103 if (nfs4_setup_sequence(data->server,
4104 &data->arg.seq_args,
Andy Adamson66179ef2009-04-01 09:22:22 -04004105 &data->res.seq_res, 1, task))
4106 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004107 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004108 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004109}
4110
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004111static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4112{
4113 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4114 nfs4_lock_prepare(task, calldata);
4115}
4116
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004117static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4118{
4119 struct nfs4_lockdata *data = calldata;
4120
Harvey Harrison3110ff82008-05-02 13:42:44 -07004121 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004122
Trond Myklebust14516c32010-07-31 14:29:06 -04004123 if (!nfs4_sequence_done(task, &data->res.seq_res))
4124 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004125
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004126 data->rpc_status = task->tk_status;
4127 if (RPC_ASSASSINATED(task))
4128 goto out;
4129 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004130 if (data->rpc_status == 0)
4131 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4132 else
4133 goto out;
4134 }
4135 if (data->rpc_status == 0) {
4136 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4137 sizeof(data->lsp->ls_stateid.data));
4138 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04004139 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004140 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004141out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004142 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004143}
4144
4145static void nfs4_lock_release(void *calldata)
4146{
4147 struct nfs4_lockdata *data = calldata;
4148
Harvey Harrison3110ff82008-05-02 13:42:44 -07004149 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004150 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004151 if (data->cancelled != 0) {
4152 struct rpc_task *task;
4153 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4154 data->arg.lock_seqid);
4155 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004156 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004157 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004158 } else
4159 nfs_free_seqid(data->arg.lock_seqid);
4160 nfs4_put_lock_state(data->lsp);
4161 put_nfs_open_context(data->ctx);
4162 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004163 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004164}
4165
4166static const struct rpc_call_ops nfs4_lock_ops = {
4167 .rpc_call_prepare = nfs4_lock_prepare,
4168 .rpc_call_done = nfs4_lock_done,
4169 .rpc_release = nfs4_lock_release,
4170};
4171
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004172static const struct rpc_call_ops nfs4_recover_lock_ops = {
4173 .rpc_call_prepare = nfs4_recover_lock_prepare,
4174 .rpc_call_done = nfs4_lock_done,
4175 .rpc_release = nfs4_lock_release,
4176};
4177
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004178static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4179{
4180 struct nfs_client *clp = server->nfs_client;
4181 struct nfs4_state *state = lsp->ls_state;
4182
4183 switch (error) {
4184 case -NFS4ERR_ADMIN_REVOKED:
4185 case -NFS4ERR_BAD_STATEID:
4186 case -NFS4ERR_EXPIRED:
4187 if (new_lock_owner != 0 ||
4188 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4189 nfs4_state_mark_reclaim_nograce(clp, state);
4190 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004191 break;
4192 case -NFS4ERR_STALE_STATEID:
4193 if (new_lock_owner != 0 ||
4194 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4195 nfs4_state_mark_reclaim_reboot(clp, state);
4196 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004197 };
4198}
4199
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004200static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004201{
4202 struct nfs4_lockdata *data;
4203 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004204 struct rpc_message msg = {
4205 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4206 .rpc_cred = state->owner->so_cred,
4207 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004208 struct rpc_task_setup task_setup_data = {
4209 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004210 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004211 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004212 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004213 .flags = RPC_TASK_ASYNC,
4214 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004215 int ret;
4216
Harvey Harrison3110ff82008-05-02 13:42:44 -07004217 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004218 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004219 fl->fl_u.nfs4_fl.owner,
4220 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004221 if (data == NULL)
4222 return -ENOMEM;
4223 if (IS_SETLKW(cmd))
4224 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004225 if (recovery_type > NFS_LOCK_NEW) {
4226 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004227 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004228 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4229 }
Trond Myklebust5138fde2007-07-14 15:40:01 -04004230 msg.rpc_argp = &data->arg,
4231 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004232 task_setup_data.callback_data = data;
4233 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004234 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004235 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004236 ret = nfs4_wait_for_completion_rpc_task(task);
4237 if (ret == 0) {
4238 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004239 if (ret)
4240 nfs4_handle_setlk_error(data->server, data->lsp,
4241 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004242 } else
4243 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004244 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004245 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004246 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247}
4248
4249static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4250{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004251 struct nfs_server *server = NFS_SERVER(state->inode);
4252 struct nfs4_exception exception = { };
4253 int err;
4254
4255 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004256 /* Cache the lock if possible... */
4257 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4258 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004259 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Jeff Layton2c643482010-01-07 09:42:03 -05004260 if (err != -NFS4ERR_DELAY && err != -EKEYEXPIRED)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004261 break;
4262 nfs4_handle_exception(server, err, &exception);
4263 } while (exception.retry);
4264 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265}
4266
4267static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4268{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004269 struct nfs_server *server = NFS_SERVER(state->inode);
4270 struct nfs4_exception exception = { };
4271 int err;
4272
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004273 err = nfs4_set_lock_state(state, request);
4274 if (err != 0)
4275 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004276 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004277 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4278 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004279 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004280 switch (err) {
4281 default:
4282 goto out;
4283 case -NFS4ERR_GRACE:
4284 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05004285 case -EKEYEXPIRED:
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004286 nfs4_handle_exception(server, err, &exception);
4287 err = 0;
4288 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004289 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004290out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004291 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292}
4293
4294static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4295{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004296 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004297 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004298 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004300 if ((fl_flags & FL_POSIX) &&
4301 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4302 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004303 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004304 status = nfs4_set_lock_state(state, request);
4305 if (status != 0)
4306 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004307 request->fl_flags |= FL_ACCESS;
4308 status = do_vfs_lock(request->fl_file, request);
4309 if (status < 0)
4310 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004311 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004312 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004313 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004314 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004315 request->fl_flags = fl_flags & ~FL_SLEEP;
4316 status = do_vfs_lock(request->fl_file, request);
4317 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004318 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004319 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004320 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004321 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004322 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004323 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004324 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004325 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004326out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004327 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004328out:
4329 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 return status;
4331}
4332
4333static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4334{
4335 struct nfs4_exception exception = { };
4336 int err;
4337
4338 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004339 err = _nfs4_proc_setlk(state, cmd, request);
4340 if (err == -NFS4ERR_DENIED)
4341 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004343 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 } while (exception.retry);
4345 return err;
4346}
4347
4348static int
4349nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4350{
4351 struct nfs_open_context *ctx;
4352 struct nfs4_state *state;
4353 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4354 int status;
4355
4356 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004357 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 state = ctx->state;
4359
4360 if (request->fl_start < 0 || request->fl_end < 0)
4361 return -EINVAL;
4362
Trond Myklebustd9531262009-07-21 19:22:38 -04004363 if (IS_GETLK(cmd)) {
4364 if (state != NULL)
4365 return nfs4_proc_getlk(state, F_GETLK, request);
4366 return 0;
4367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368
4369 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4370 return -EINVAL;
4371
Trond Myklebustd9531262009-07-21 19:22:38 -04004372 if (request->fl_type == F_UNLCK) {
4373 if (state != NULL)
4374 return nfs4_proc_unlck(state, cmd, request);
4375 return 0;
4376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377
Trond Myklebustd9531262009-07-21 19:22:38 -04004378 if (state == NULL)
4379 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 do {
4381 status = nfs4_proc_setlk(state, cmd, request);
4382 if ((status != -EAGAIN) || IS_SETLK(cmd))
4383 break;
4384 timeout = nfs4_set_lock_task_retry(timeout);
4385 status = -ERESTARTSYS;
4386 if (signalled())
4387 break;
4388 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 return status;
4390}
4391
Trond Myklebust888e6942005-11-04 15:38:11 -05004392int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4393{
4394 struct nfs_server *server = NFS_SERVER(state->inode);
4395 struct nfs4_exception exception = { };
4396 int err;
4397
4398 err = nfs4_set_lock_state(state, fl);
4399 if (err != 0)
4400 goto out;
4401 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004402 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004403 switch (err) {
4404 default:
4405 printk(KERN_ERR "%s: unhandled error %d.\n",
4406 __func__, err);
4407 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004408 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004409 goto out;
4410 case -NFS4ERR_EXPIRED:
4411 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004412 case -NFS4ERR_STALE_STATEID:
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004413 case -NFS4ERR_BADSESSION:
4414 case -NFS4ERR_BADSLOT:
4415 case -NFS4ERR_BAD_HIGH_SLOT:
4416 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4417 case -NFS4ERR_DEADSESSION:
Trond Myklebustd5122202009-06-17 13:22:58 -07004418 nfs4_schedule_state_recovery(server->nfs_client);
4419 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004420 case -ERESTARTSYS:
4421 /*
4422 * The show must go on: exit, but mark the
4423 * stateid as needing recovery.
4424 */
4425 case -NFS4ERR_ADMIN_REVOKED:
4426 case -NFS4ERR_BAD_STATEID:
4427 case -NFS4ERR_OPENMODE:
4428 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4429 err = 0;
4430 goto out;
4431 case -ENOMEM:
4432 case -NFS4ERR_DENIED:
4433 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4434 err = 0;
4435 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004436 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05004437 case -EKEYEXPIRED:
Trond Myklebustd5122202009-06-17 13:22:58 -07004438 break;
4439 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004440 err = nfs4_handle_exception(server, err, &exception);
4441 } while (exception.retry);
4442out:
4443 return err;
4444}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004445
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004446static void nfs4_release_lockowner_release(void *calldata)
4447{
4448 kfree(calldata);
4449}
4450
4451const struct rpc_call_ops nfs4_release_lockowner_ops = {
4452 .rpc_release = nfs4_release_lockowner_release,
4453};
4454
4455void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4456{
4457 struct nfs_server *server = lsp->ls_state->owner->so_server;
4458 struct nfs_release_lockowner_args *args;
4459 struct rpc_message msg = {
4460 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4461 };
4462
4463 if (server->nfs_client->cl_mvops->minor_version != 0)
4464 return;
4465 args = kmalloc(sizeof(*args), GFP_NOFS);
4466 if (!args)
4467 return;
4468 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4469 args->lock_owner.id = lsp->ls_id.id;
4470 msg.rpc_argp = args;
4471 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4472}
4473
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004474#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4475
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004476int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4477 size_t buflen, int flags)
4478{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004479 struct inode *inode = dentry->d_inode;
4480
4481 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4482 return -EOPNOTSUPP;
4483
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004484 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004485}
4486
4487/* The getxattr man page suggests returning -ENODATA for unknown attributes,
4488 * and that's what we'll do for e.g. user attributes that haven't been set.
4489 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4490 * attributes in kernel-managed attribute namespaces. */
4491ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4492 size_t buflen)
4493{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004494 struct inode *inode = dentry->d_inode;
4495
4496 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4497 return -EOPNOTSUPP;
4498
4499 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004500}
4501
4502ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4503{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004504 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004505
J. Bruce Fields096455a2006-03-20 23:23:42 -05004506 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4507 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004508 if (buf && buflen < len)
4509 return -ERANGE;
4510 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004511 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4512 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004513}
4514
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004515static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4516{
4517 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4518 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4519 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4520 return;
4521
4522 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4523 NFS_ATTR_FATTR_NLINK;
4524 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4525 fattr->nlink = 2;
4526}
4527
Trond Myklebust56659e92007-07-17 21:52:39 -04004528int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004529 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004530{
4531 struct nfs_server *server = NFS_SERVER(dir);
4532 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004533 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4534 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004535 };
4536 struct nfs4_fs_locations_arg args = {
4537 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004538 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004539 .page = page,
4540 .bitmask = bitmask,
4541 };
Benny Halevy22958462009-04-01 09:22:02 -04004542 struct nfs4_fs_locations_res res = {
4543 .fs_locations = fs_locations,
4544 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004545 struct rpc_message msg = {
4546 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4547 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004548 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004549 };
4550 int status;
4551
Harvey Harrison3110ff82008-05-02 13:42:44 -07004552 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004553 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004554 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004555 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04004556 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004557 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004558 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004559 return status;
4560}
4561
Andy Adamson557134a2009-04-01 09:21:53 -04004562#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004563/*
4564 * nfs4_proc_exchange_id()
4565 *
4566 * Since the clientid has expired, all compounds using sessions
4567 * associated with the stale clientid will be returning
4568 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4569 * be in some phase of session reset.
4570 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004571int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004572{
4573 nfs4_verifier verifier;
4574 struct nfs41_exchange_id_args args = {
4575 .client = clp,
4576 .flags = clp->cl_exchange_flags,
4577 };
4578 struct nfs41_exchange_id_res res = {
4579 .client = clp,
4580 };
4581 int status;
4582 struct rpc_message msg = {
4583 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4584 .rpc_argp = &args,
4585 .rpc_resp = &res,
4586 .rpc_cred = cred,
4587 };
4588 __be32 *p;
4589
4590 dprintk("--> %s\n", __func__);
4591 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004592
Alexandros Batsakis7b183d02009-12-05 13:33:25 -05004593 /* Remove server-only flags */
4594 args.flags &= ~EXCHGID4_FLAG_CONFIRMED_R;
4595
Benny Halevy99fe60d2009-04-01 09:22:29 -04004596 p = (u32 *)verifier.data;
4597 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4598 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4599 args.verifier = &verifier;
4600
4601 while (1) {
4602 args.id_len = scnprintf(args.id, sizeof(args.id),
4603 "%s/%s %u",
4604 clp->cl_ipaddr,
4605 rpc_peeraddr2str(clp->cl_rpcclient,
4606 RPC_DISPLAY_ADDR),
4607 clp->cl_id_uniquifier);
4608
4609 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4610
Andy Adamson180b62a2010-03-02 13:19:36 -05004611 if (status != -NFS4ERR_CLID_INUSE)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004612 break;
4613
4614 if (signalled())
4615 break;
4616
4617 if (++clp->cl_id_uniquifier == 0)
4618 break;
4619 }
4620
4621 dprintk("<-- %s status= %d\n", __func__, status);
4622 return status;
4623}
4624
Andy Adamson2050f0c2009-04-01 09:22:30 -04004625struct nfs4_get_lease_time_data {
4626 struct nfs4_get_lease_time_args *args;
4627 struct nfs4_get_lease_time_res *res;
4628 struct nfs_client *clp;
4629};
4630
4631static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4632 void *calldata)
4633{
4634 int ret;
4635 struct nfs4_get_lease_time_data *data =
4636 (struct nfs4_get_lease_time_data *)calldata;
4637
4638 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004639 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004640 /* just setup sequence, do not trigger session recovery
4641 since we're invoked within one */
4642 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004643 &data->args->la_seq_args,
4644 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004645
4646 BUG_ON(ret == -EAGAIN);
4647 rpc_call_start(task);
4648 dprintk("<-- %s\n", __func__);
4649}
4650
4651/*
4652 * Called from nfs4_state_manager thread for session setup, so don't recover
4653 * from sequence operation or clientid errors.
4654 */
4655static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4656{
4657 struct nfs4_get_lease_time_data *data =
4658 (struct nfs4_get_lease_time_data *)calldata;
4659
4660 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04004661 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4662 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04004663 switch (task->tk_status) {
4664 case -NFS4ERR_DELAY:
4665 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05004666 case -EKEYEXPIRED:
Andy Adamson2050f0c2009-04-01 09:22:30 -04004667 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4668 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4669 task->tk_status = 0;
Trond Myklebust0110ee12009-12-07 09:00:24 -05004670 nfs_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004671 return;
4672 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004673 dprintk("<-- %s\n", __func__);
4674}
4675
4676struct rpc_call_ops nfs4_get_lease_time_ops = {
4677 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4678 .rpc_call_done = nfs4_get_lease_time_done,
4679};
4680
4681int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4682{
4683 struct rpc_task *task;
4684 struct nfs4_get_lease_time_args args;
4685 struct nfs4_get_lease_time_res res = {
4686 .lr_fsinfo = fsinfo,
4687 };
4688 struct nfs4_get_lease_time_data data = {
4689 .args = &args,
4690 .res = &res,
4691 .clp = clp,
4692 };
4693 struct rpc_message msg = {
4694 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4695 .rpc_argp = &args,
4696 .rpc_resp = &res,
4697 };
4698 struct rpc_task_setup task_setup = {
4699 .rpc_client = clp->cl_rpcclient,
4700 .rpc_message = &msg,
4701 .callback_ops = &nfs4_get_lease_time_ops,
4702 .callback_data = &data
4703 };
4704 int status;
4705
4706 res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
4707 dprintk("--> %s\n", __func__);
4708 task = rpc_run_task(&task_setup);
4709
4710 if (IS_ERR(task))
4711 status = PTR_ERR(task);
4712 else {
4713 status = task->tk_status;
4714 rpc_put_task(task);
4715 }
4716 dprintk("<-- %s return %d\n", __func__, status);
4717
4718 return status;
4719}
4720
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004721/*
4722 * Reset a slot table
4723 */
Andy Adamson104aeba2010-01-14 17:45:10 -05004724static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4725 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004726{
Andy Adamson104aeba2010-01-14 17:45:10 -05004727 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004728 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004729 int ret = 0;
4730
Andy Adamson104aeba2010-01-14 17:45:10 -05004731 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4732 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004733
Andy Adamson104aeba2010-01-14 17:45:10 -05004734 /* Does the newly negotiated max_reqs match the existing slot table? */
4735 if (max_reqs != tbl->max_slots) {
4736 ret = -ENOMEM;
4737 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004738 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05004739 if (!new)
4740 goto out;
4741 ret = 0;
4742 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004743 }
4744 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05004745 if (new) {
4746 tbl->slots = new;
4747 tbl->max_slots = max_reqs;
4748 }
4749 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004750 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004751 spin_unlock(&tbl->slot_tbl_lock);
4752 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4753 tbl, tbl->slots, tbl->max_slots);
4754out:
4755 dprintk("<-- %s: return %d\n", __func__, ret);
4756 return ret;
4757}
4758
Andy Adamsonfc931582009-04-01 09:22:31 -04004759/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004760 * Reset the forechannel and backchannel slot tables
4761 */
4762static int nfs4_reset_slot_tables(struct nfs4_session *session)
4763{
4764 int status;
4765
4766 status = nfs4_reset_slot_table(&session->fc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004767 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004768 if (status)
4769 return status;
4770
4771 status = nfs4_reset_slot_table(&session->bc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004772 session->bc_attrs.max_reqs, 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004773 return status;
4774}
4775
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004776/* Destroy the slot table */
4777static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4778{
4779 if (session->fc_slot_table.slots != NULL) {
4780 kfree(session->fc_slot_table.slots);
4781 session->fc_slot_table.slots = NULL;
4782 }
4783 if (session->bc_slot_table.slots != NULL) {
4784 kfree(session->bc_slot_table.slots);
4785 session->bc_slot_table.slots = NULL;
4786 }
4787 return;
4788}
4789
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004790/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004791 * Initialize slot table
4792 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004793static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4794 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04004795{
Andy Adamsonfc931582009-04-01 09:22:31 -04004796 struct nfs4_slot *slot;
4797 int ret = -ENOMEM;
4798
4799 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4800
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004801 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04004802
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004803 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04004804 if (!slot)
4805 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04004806 ret = 0;
4807
4808 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04004809 tbl->max_slots = max_slots;
4810 tbl->slots = slot;
4811 tbl->highest_used_slotid = -1; /* no slot is currently used */
4812 spin_unlock(&tbl->slot_tbl_lock);
4813 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4814 tbl, tbl->slots, tbl->max_slots);
4815out:
4816 dprintk("<-- %s: return %d\n", __func__, ret);
4817 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04004818}
4819
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004820/*
4821 * Initialize the forechannel and backchannel tables
4822 */
4823static int nfs4_init_slot_tables(struct nfs4_session *session)
4824{
Trond Myklebustf26468f2009-12-05 19:32:11 -05004825 struct nfs4_slot_table *tbl;
4826 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004827
Trond Myklebustf26468f2009-12-05 19:32:11 -05004828 tbl = &session->fc_slot_table;
4829 if (tbl->slots == NULL) {
4830 status = nfs4_init_slot_table(tbl,
4831 session->fc_attrs.max_reqs, 1);
4832 if (status)
4833 return status;
4834 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004835
Trond Myklebustf26468f2009-12-05 19:32:11 -05004836 tbl = &session->bc_slot_table;
4837 if (tbl->slots == NULL) {
4838 status = nfs4_init_slot_table(tbl,
4839 session->bc_attrs.max_reqs, 0);
4840 if (status)
4841 nfs4_destroy_slot_tables(session);
4842 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004843
4844 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04004845}
4846
4847struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4848{
4849 struct nfs4_session *session;
4850 struct nfs4_slot_table *tbl;
4851
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004852 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04004853 if (!session)
4854 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04004855
Andy Adamsonea028ac2009-12-04 15:55:38 -05004856 init_completion(&session->complete);
Andy Adamson76db6d92009-04-01 09:22:38 -04004857
Andy Adamson557134a2009-04-01 09:21:53 -04004858 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004859 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04004860 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004861 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004862
4863 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004864 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004865 spin_lock_init(&tbl->slot_tbl_lock);
4866 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4867
Trond Myklebust1055d762010-06-16 09:52:27 -04004868 session->session_state = 1<<NFS4_SESSION_INITING;
4869
Andy Adamson557134a2009-04-01 09:21:53 -04004870 session->clp = clp;
4871 return session;
4872}
4873
4874void nfs4_destroy_session(struct nfs4_session *session)
4875{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04004876 nfs4_proc_destroy_session(session);
4877 dprintk("%s Destroy backchannel for xprt %p\n",
4878 __func__, session->clp->cl_rpcclient->cl_xprt);
4879 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4880 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004881 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04004882 kfree(session);
4883}
4884
Andy Adamsonfc931582009-04-01 09:22:31 -04004885/*
4886 * Initialize the values to be used by the client in CREATE_SESSION
4887 * If nfs4_init_session set the fore channel request and response sizes,
4888 * use them.
4889 *
4890 * Set the back channel max_resp_sz_cached to zero to force the client to
4891 * always set csa_cachethis to FALSE because the current implementation
4892 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4893 */
4894static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4895{
4896 struct nfs4_session *session = args->client->cl_session;
4897 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4898 mxresp_sz = session->fc_attrs.max_resp_sz;
4899
4900 if (mxrqst_sz == 0)
4901 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4902 if (mxresp_sz == 0)
4903 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4904 /* Fore channel attributes */
4905 args->fc_attrs.headerpadsz = 0;
4906 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4907 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04004908 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4909 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4910
4911 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05004912 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04004913 __func__,
4914 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05004915 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04004916
4917 /* Back channel attributes */
4918 args->bc_attrs.headerpadsz = 0;
4919 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4920 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4921 args->bc_attrs.max_resp_sz_cached = 0;
4922 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4923 args->bc_attrs.max_reqs = 1;
4924
4925 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4926 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4927 __func__,
4928 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4929 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4930 args->bc_attrs.max_reqs);
4931}
4932
Andy Adamson8d353012009-04-01 09:22:32 -04004933static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
4934{
4935 if (rcvd <= sent)
4936 return 0;
4937 printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
4938 "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
4939 return -EINVAL;
4940}
4941
4942#define _verify_fore_channel_attr(_name_) \
4943 _verify_channel_attr("fore", #_name_, \
4944 args->fc_attrs._name_, \
4945 session->fc_attrs._name_)
4946
4947#define _verify_back_channel_attr(_name_) \
4948 _verify_channel_attr("back", #_name_, \
4949 args->bc_attrs._name_, \
4950 session->bc_attrs._name_)
4951
4952/*
4953 * The server is not allowed to increase the fore channel header pad size,
4954 * maximum response size, or maximum number of operations.
4955 *
4956 * The back channel attributes are only negotiatied down: We send what the
4957 * (back channel) server insists upon.
4958 */
4959static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4960 struct nfs4_session *session)
4961{
4962 int ret = 0;
4963
4964 ret |= _verify_fore_channel_attr(headerpadsz);
4965 ret |= _verify_fore_channel_attr(max_resp_sz);
4966 ret |= _verify_fore_channel_attr(max_ops);
4967
4968 ret |= _verify_back_channel_attr(headerpadsz);
4969 ret |= _verify_back_channel_attr(max_rqst_sz);
4970 ret |= _verify_back_channel_attr(max_resp_sz);
4971 ret |= _verify_back_channel_attr(max_resp_sz_cached);
4972 ret |= _verify_back_channel_attr(max_ops);
4973 ret |= _verify_back_channel_attr(max_reqs);
4974
4975 return ret;
4976}
4977
Andy Adamsonfc931582009-04-01 09:22:31 -04004978static int _nfs4_proc_create_session(struct nfs_client *clp)
4979{
4980 struct nfs4_session *session = clp->cl_session;
4981 struct nfs41_create_session_args args = {
4982 .client = clp,
4983 .cb_program = NFS4_CALLBACK,
4984 };
4985 struct nfs41_create_session_res res = {
4986 .client = clp,
4987 };
4988 struct rpc_message msg = {
4989 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4990 .rpc_argp = &args,
4991 .rpc_resp = &res,
4992 };
4993 int status;
4994
4995 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04004996 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04004997
4998 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4999
Andy Adamson8d353012009-04-01 09:22:32 -04005000 if (!status)
5001 /* Verify the session's negotiated channel_attrs values */
5002 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005003 if (!status) {
5004 /* Increment the clientid slot sequence id */
5005 clp->cl_seqid++;
5006 }
5007
5008 return status;
5009}
5010
5011/*
5012 * Issues a CREATE_SESSION operation to the server.
5013 * It is the responsibility of the caller to verify the session is
5014 * expired before calling this routine.
5015 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005016int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005017{
5018 int status;
5019 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005020 struct nfs4_session *session = clp->cl_session;
5021
5022 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5023
5024 status = _nfs4_proc_create_session(clp);
5025 if (status)
5026 goto out;
5027
Trond Myklebustf26468f2009-12-05 19:32:11 -05005028 /* Init and reset the fore channel */
5029 status = nfs4_init_slot_tables(session);
5030 dprintk("slot table initialization returned %d\n", status);
5031 if (status)
5032 goto out;
5033 status = nfs4_reset_slot_tables(session);
5034 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005035 if (status)
5036 goto out;
5037
5038 ptr = (unsigned *)&session->sess_id.data[0];
5039 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5040 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005041out:
5042 dprintk("<-- %s\n", __func__);
5043 return status;
5044}
5045
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005046/*
5047 * Issue the over-the-wire RPC DESTROY_SESSION.
5048 * The caller must serialize access to this routine.
5049 */
5050int nfs4_proc_destroy_session(struct nfs4_session *session)
5051{
5052 int status = 0;
5053 struct rpc_message msg;
5054
5055 dprintk("--> nfs4_proc_destroy_session\n");
5056
5057 /* session is still being setup */
5058 if (session->clp->cl_cons_state != NFS_CS_READY)
5059 return status;
5060
5061 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5062 msg.rpc_argp = session;
5063 msg.rpc_resp = NULL;
5064 msg.rpc_cred = NULL;
5065 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5066
5067 if (status)
5068 printk(KERN_WARNING
5069 "Got error %d from the server on DESTROY_SESSION. "
5070 "Session has been destroyed regardless...\n", status);
5071
5072 dprintk("<-- nfs4_proc_destroy_session\n");
5073 return status;
5074}
5075
Trond Myklebustfccba802009-07-21 16:48:07 -04005076int nfs4_init_session(struct nfs_server *server)
5077{
5078 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005079 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005080 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005081 int ret;
5082
5083 if (!nfs4_has_session(clp))
5084 return 0;
5085
Trond Myklebust1055d762010-06-16 09:52:27 -04005086 session = clp->cl_session;
5087 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5088 return 0;
5089
Andy Adamson68bf05e2009-12-15 12:55:02 -05005090 rsize = server->rsize;
5091 if (rsize == 0)
5092 rsize = NFS_MAX_FILE_IO_SIZE;
5093 wsize = server->wsize;
5094 if (wsize == 0)
5095 wsize = NFS_MAX_FILE_IO_SIZE;
5096
Andy Adamson68bf05e2009-12-15 12:55:02 -05005097 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5098 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005099
Trond Myklebustfccba802009-07-21 16:48:07 -04005100 ret = nfs4_recover_expired_lease(server);
5101 if (!ret)
5102 ret = nfs4_check_client_ready(clp);
5103 return ret;
5104}
5105
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005106/*
5107 * Renew the cl_session lease.
5108 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005109struct nfs4_sequence_data {
5110 struct nfs_client *clp;
5111 struct nfs4_sequence_args args;
5112 struct nfs4_sequence_res res;
5113};
5114
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005115static void nfs41_sequence_release(void *data)
5116{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005117 struct nfs4_sequence_data *calldata = data;
5118 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005119
Alexandros Batsakis71358402010-02-05 03:45:05 -08005120 if (atomic_read(&clp->cl_count) > 1)
5121 nfs4_schedule_state_renewal(clp);
5122 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005123 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005124}
5125
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005126static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5127{
5128 switch(task->tk_status) {
5129 case -NFS4ERR_DELAY:
5130 case -EKEYEXPIRED:
5131 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5132 return -EAGAIN;
5133 default:
5134 nfs4_schedule_state_recovery(clp);
5135 }
5136 return 0;
5137}
5138
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005139static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005140{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005141 struct nfs4_sequence_data *calldata = data;
5142 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005143
Trond Myklebust14516c32010-07-31 14:29:06 -04005144 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5145 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005146
5147 if (task->tk_status < 0) {
5148 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005149 if (atomic_read(&clp->cl_count) == 1)
5150 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005151
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005152 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5153 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005154 return;
5155 }
5156 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005157 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005158out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005159 dprintk("<-- %s\n", __func__);
5160}
5161
5162static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5163{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005164 struct nfs4_sequence_data *calldata = data;
5165 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005166 struct nfs4_sequence_args *args;
5167 struct nfs4_sequence_res *res;
5168
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005169 args = task->tk_msg.rpc_argp;
5170 res = task->tk_msg.rpc_resp;
5171
Trond Myklebust035168a2010-06-16 09:52:26 -04005172 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005173 return;
5174 rpc_call_start(task);
5175}
5176
5177static const struct rpc_call_ops nfs41_sequence_ops = {
5178 .rpc_call_done = nfs41_sequence_call_done,
5179 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005180 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005181};
5182
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005183static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005184{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005185 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005186 struct rpc_message msg = {
5187 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5188 .rpc_cred = cred,
5189 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005190 struct rpc_task_setup task_setup_data = {
5191 .rpc_client = clp->cl_rpcclient,
5192 .rpc_message = &msg,
5193 .callback_ops = &nfs41_sequence_ops,
5194 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5195 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005196
Alexandros Batsakis71358402010-02-05 03:45:05 -08005197 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005198 return ERR_PTR(-EIO);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005199 calldata = kmalloc(sizeof(*calldata), GFP_NOFS);
5200 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005201 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005202 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005203 }
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005204 calldata->res.sr_slotid = NFS4_MAX_SLOT_TABLE;
5205 msg.rpc_argp = &calldata->args;
5206 msg.rpc_resp = &calldata->res;
5207 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005208 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005209
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005210 return rpc_run_task(&task_setup_data);
5211}
5212
5213static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5214{
5215 struct rpc_task *task;
5216 int ret = 0;
5217
5218 task = _nfs41_proc_sequence(clp, cred);
5219 if (IS_ERR(task))
5220 ret = PTR_ERR(task);
5221 else
5222 rpc_put_task(task);
5223 dprintk("<-- %s status=%d\n", __func__, ret);
5224 return ret;
5225}
5226
5227static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5228{
5229 struct rpc_task *task;
5230 int ret;
5231
5232 task = _nfs41_proc_sequence(clp, cred);
5233 if (IS_ERR(task)) {
5234 ret = PTR_ERR(task);
5235 goto out;
5236 }
5237 ret = rpc_wait_for_completion_task(task);
5238 if (!ret)
5239 ret = task->tk_status;
5240 rpc_put_task(task);
5241out:
5242 dprintk("<-- %s status=%d\n", __func__, ret);
5243 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005244}
5245
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005246struct nfs4_reclaim_complete_data {
5247 struct nfs_client *clp;
5248 struct nfs41_reclaim_complete_args arg;
5249 struct nfs41_reclaim_complete_res res;
5250};
5251
5252static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5253{
5254 struct nfs4_reclaim_complete_data *calldata = data;
5255
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005256 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005257 if (nfs41_setup_sequence(calldata->clp->cl_session,
5258 &calldata->arg.seq_args,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005259 &calldata->res.seq_res, 0, task))
5260 return;
5261
5262 rpc_call_start(task);
5263}
5264
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005265static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5266{
5267 switch(task->tk_status) {
5268 case 0:
5269 case -NFS4ERR_COMPLETE_ALREADY:
5270 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5271 break;
5272 case -NFS4ERR_DELAY:
5273 case -EKEYEXPIRED:
5274 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5275 return -EAGAIN;
5276 default:
5277 nfs4_schedule_state_recovery(clp);
5278 }
5279 return 0;
5280}
5281
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005282static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5283{
5284 struct nfs4_reclaim_complete_data *calldata = data;
5285 struct nfs_client *clp = calldata->clp;
5286 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5287
5288 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005289 if (!nfs41_sequence_done(task, res))
5290 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005291
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005292 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5293 rpc_restart_call_prepare(task);
5294 return;
5295 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005296 dprintk("<-- %s\n", __func__);
5297}
5298
5299static void nfs4_free_reclaim_complete_data(void *data)
5300{
5301 struct nfs4_reclaim_complete_data *calldata = data;
5302
5303 kfree(calldata);
5304}
5305
5306static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5307 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5308 .rpc_call_done = nfs4_reclaim_complete_done,
5309 .rpc_release = nfs4_free_reclaim_complete_data,
5310};
5311
5312/*
5313 * Issue a global reclaim complete.
5314 */
5315static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5316{
5317 struct nfs4_reclaim_complete_data *calldata;
5318 struct rpc_task *task;
5319 struct rpc_message msg = {
5320 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5321 };
5322 struct rpc_task_setup task_setup_data = {
5323 .rpc_client = clp->cl_rpcclient,
5324 .rpc_message = &msg,
5325 .callback_ops = &nfs4_reclaim_complete_call_ops,
5326 .flags = RPC_TASK_ASYNC,
5327 };
5328 int status = -ENOMEM;
5329
5330 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005331 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005332 if (calldata == NULL)
5333 goto out;
5334 calldata->clp = clp;
5335 calldata->arg.one_fs = 0;
5336 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
5337
5338 msg.rpc_argp = &calldata->arg;
5339 msg.rpc_resp = &calldata->res;
5340 task_setup_data.callback_data = calldata;
5341 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005342 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005343 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005344 goto out;
5345 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005346 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005347 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005348out:
5349 dprintk("<-- %s status=%d\n", __func__, status);
5350 return status;
5351}
Andy Adamson557134a2009-04-01 09:21:53 -04005352#endif /* CONFIG_NFS_V4_1 */
5353
Andy Adamson591d71c2009-04-01 09:22:47 -04005354struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005355 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005356 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 .recover_open = nfs4_open_reclaim,
5358 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04005359 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005360 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361};
5362
Andy Adamson591d71c2009-04-01 09:22:47 -04005363#if defined(CONFIG_NFS_V4_1)
5364struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5365 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5366 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5367 .recover_open = nfs4_open_reclaim,
5368 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05005369 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005370 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005371 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04005372};
5373#endif /* CONFIG_NFS_V4_1 */
5374
5375struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005376 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005377 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378 .recover_open = nfs4_open_expired,
5379 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04005380 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005381 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382};
5383
Andy Adamson591d71c2009-04-01 09:22:47 -04005384#if defined(CONFIG_NFS_V4_1)
5385struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5386 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5387 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5388 .recover_open = nfs4_open_expired,
5389 .recover_lock = nfs4_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05005390 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005391 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04005392};
5393#endif /* CONFIG_NFS_V4_1 */
5394
Benny Halevy29fba382009-04-01 09:22:44 -04005395struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5396 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04005397 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005398 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04005399};
5400
5401#if defined(CONFIG_NFS_V4_1)
5402struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5403 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04005404 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005405 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04005406};
5407#endif
5408
Trond Myklebust97dc1352010-06-16 09:52:26 -04005409static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
5410 .minor_version = 0,
5411 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04005412 .validate_stateid = nfs4_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005413 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
5414 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
5415 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005416};
5417
5418#if defined(CONFIG_NFS_V4_1)
5419static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
5420 .minor_version = 1,
5421 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04005422 .validate_stateid = nfs41_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005423 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
5424 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
5425 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005426};
5427#endif
5428
Trond Myklebust97dc1352010-06-16 09:52:26 -04005429const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
5430 [0] = &nfs_v4_0_minor_ops,
5431#if defined(CONFIG_NFS_V4_1)
5432 [1] = &nfs_v4_1_minor_ops,
5433#endif
5434};
5435
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005436static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005437 .permission = nfs_permission,
5438 .getattr = nfs_getattr,
5439 .setattr = nfs_setattr,
5440 .getxattr = nfs4_getxattr,
5441 .setxattr = nfs4_setxattr,
5442 .listxattr = nfs4_listxattr,
5443};
5444
David Howells509de812006-08-22 20:06:11 -04005445const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446 .version = 4, /* protocol version */
5447 .dentry_ops = &nfs4_dentry_operations,
5448 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005449 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 .getroot = nfs4_proc_get_root,
5451 .getattr = nfs4_proc_getattr,
5452 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04005453 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 .lookup = nfs4_proc_lookup,
5455 .access = nfs4_proc_access,
5456 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 .create = nfs4_proc_create,
5458 .remove = nfs4_proc_remove,
5459 .unlink_setup = nfs4_proc_unlink_setup,
5460 .unlink_done = nfs4_proc_unlink_done,
5461 .rename = nfs4_proc_rename,
5462 .link = nfs4_proc_link,
5463 .symlink = nfs4_proc_symlink,
5464 .mkdir = nfs4_proc_mkdir,
5465 .rmdir = nfs4_proc_remove,
5466 .readdir = nfs4_proc_readdir,
5467 .mknod = nfs4_proc_mknod,
5468 .statfs = nfs4_proc_statfs,
5469 .fsinfo = nfs4_proc_fsinfo,
5470 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04005471 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 .decode_dirent = nfs4_decode_dirent,
5473 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05005474 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005476 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005478 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005480 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04005481 .close_context = nfs4_close_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482};
5483
5484/*
5485 * Local variables:
5486 * c-basic-offset: 8
5487 * End:
5488 */