blob: d2d20fbeb086cc375aa659d42bcd40c24468a746 [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 Myklebust452e9352010-07-31 14:29:06 -0400306static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 spin_lock(&clp->cl_lock);
309 if (time_before(clp->cl_last_renewal,timestamp))
310 clp->cl_last_renewal = timestamp;
311 spin_unlock(&clp->cl_lock);
312}
313
Trond Myklebust452e9352010-07-31 14:29:06 -0400314static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
315{
316 do_renew_lease(server->nfs_client, timestamp);
317}
318
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400319#if defined(CONFIG_NFS_V4_1)
320
Benny Halevy510b8172009-04-01 09:22:14 -0400321/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400322 * nfs4_free_slot - free a slot and efficiently update slot table.
323 *
324 * freeing a slot is trivially done by clearing its respective bit
325 * in the bitmap.
326 * If the freed slotid equals highest_used_slotid we want to update it
327 * so that the server would be able to size down the slot table if needed,
328 * otherwise we know that the highest_used_slotid is still in use.
329 * When updating highest_used_slotid there may be "holes" in the bitmap
330 * so we need to scan down from highest_used_slotid to 0 looking for the now
331 * highest slotid in use.
332 * If none found, highest_used_slotid is set to -1.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500333 *
334 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400335 */
336static void
337nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid)
338{
339 int slotid = free_slotid;
340
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400341 /* clear used bit in bitmap */
342 __clear_bit(slotid, tbl->used_slots);
343
344 /* update highest_used_slotid when it is freed */
345 if (slotid == tbl->highest_used_slotid) {
346 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500347 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400348 tbl->highest_used_slotid = slotid;
349 else
350 tbl->highest_used_slotid = -1;
351 }
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400352 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
353 free_slotid, tbl->highest_used_slotid);
354}
355
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800356/*
357 * Signal state manager thread if session is drained
358 */
359static void nfs41_check_drain_session_complete(struct nfs4_session *ses)
360{
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800361 struct rpc_task *task;
362
Trond Myklebusta2118c32010-06-16 09:52:26 -0400363 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800364 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
365 if (task)
366 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800367 return;
368 }
369
370 if (ses->fc_slot_table.highest_used_slotid != -1)
371 return;
372
373 dprintk("%s COMPLETE: Session Drained\n", __func__);
374 complete(&ses->complete);
375}
376
Trond Myklebustd185a332010-06-16 09:52:25 -0400377static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400378{
379 struct nfs4_slot_table *tbl;
380
Trond Myklebustd185a332010-06-16 09:52:25 -0400381 tbl = &res->sr_session->fc_slot_table;
Andy Adamson13615872009-04-01 09:22:17 -0400382 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE) {
Andy Adamson13615872009-04-01 09:22:17 -0400383 /* just wake up the next guy waiting since
384 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500385 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500386 return;
Andy Adamson13615872009-04-01 09:22:17 -0400387 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500388
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500389 spin_lock(&tbl->slot_tbl_lock);
390 nfs4_free_slot(tbl, res->sr_slotid);
Trond Myklebustd185a332010-06-16 09:52:25 -0400391 nfs41_check_drain_session_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500392 spin_unlock(&tbl->slot_tbl_lock);
393 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Andy Adamson13615872009-04-01 09:22:17 -0400394}
395
Trond Myklebust14516c32010-07-31 14:29:06 -0400396static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400397{
398 unsigned long timestamp;
399 struct nfs4_slot_table *tbl;
400 struct nfs4_slot *slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400401 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400402
403 /*
404 * sr_status remains 1 if an RPC level error occurred. The server
405 * may or may not have processed the sequence operation..
406 * Proceed as if the server received and processed the sequence
407 * operation.
408 */
409 if (res->sr_status == 1)
410 res->sr_status = NFS_OK;
411
412 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
413 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE)
414 goto out;
415
Trond Myklebust14516c32010-07-31 14:29:06 -0400416 tbl = &res->sr_session->fc_slot_table;
417 slot = tbl->slots + res->sr_slotid;
418
Andy Adamson691daf32009-12-04 15:55:39 -0500419 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400420 switch (res->sr_status) {
421 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400422 /* Update the slot's sequence and clientid lease timer */
423 ++slot->seq_nr;
424 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400425 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400426 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500427 /* Check sequence flags */
Alexandros Batsakis71358402010-02-05 03:45:05 -0800428 if (atomic_read(&clp->cl_count) > 1)
429 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust14516c32010-07-31 14:29:06 -0400430 break;
431 case -NFS4ERR_DELAY:
432 /* The server detected a resend of the RPC call and
433 * returned NFS4ERR_DELAY as per Section 2.10.6.2
434 * of RFC5661.
435 */
436 dprintk("%s: slot=%d seq=%d: Operation in progress\n",
437 __func__, res->sr_slotid, slot->seq_nr);
438 goto out_retry;
439 default:
440 /* Just update the slot sequence no. */
441 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400442 }
443out:
444 /* The session may be reset by one of the error handlers. */
445 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400446 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400447 return 1;
448out_retry:
449 rpc_restart_call(task);
450 /* FIXME: rpc_restart_call() should be made to return success/fail */
451 if (task->tk_action == NULL)
452 goto out;
453 rpc_delay(task, NFS4_POLL_RETRY_MAX);
454 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400455}
456
Trond Myklebust14516c32010-07-31 14:29:06 -0400457static int nfs4_sequence_done(struct rpc_task *task,
458 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400459{
Trond Myklebust14516c32010-07-31 14:29:06 -0400460 if (res->sr_session == NULL)
461 return 1;
462 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400463}
464
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400465/*
Benny Halevy510b8172009-04-01 09:22:14 -0400466 * nfs4_find_slot - efficiently look for a free slot
467 *
468 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
469 * If found, we mark the slot as used, update the highest_used_slotid,
470 * and respectively set up the sequence operation args.
471 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400472 *
473 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400474 */
475static u8
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800476nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400477{
478 int slotid;
479 u8 ret_id = NFS4_MAX_SLOT_TABLE;
480 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
481
Benny Halevy510b8172009-04-01 09:22:14 -0400482 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
483 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
484 tbl->max_slots);
485 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
486 if (slotid >= tbl->max_slots)
487 goto out;
488 __set_bit(slotid, tbl->used_slots);
489 if (slotid > tbl->highest_used_slotid)
490 tbl->highest_used_slotid = slotid;
491 ret_id = slotid;
492out:
493 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
494 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400495 return ret_id;
496}
497
Andy Adamsonce5039c2009-04-01 09:22:13 -0400498static int nfs41_setup_sequence(struct nfs4_session *session,
499 struct nfs4_sequence_args *args,
500 struct nfs4_sequence_res *res,
501 int cache_reply,
502 struct rpc_task *task)
503{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400504 struct nfs4_slot *slot;
505 struct nfs4_slot_table *tbl;
506 u8 slotid;
507
508 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400509 /* slot already allocated? */
510 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
511 return 0;
512
Andy Adamsonce5039c2009-04-01 09:22:13 -0400513 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400514 tbl = &session->fc_slot_table;
515
516 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400517 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800518 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamsonea028ac2009-12-04 15:55:38 -0500519 /*
520 * The state manager will wait until the slot table is empty.
521 * Schedule the reset thread
522 */
Andy Adamson05f0d232009-12-04 15:55:37 -0500523 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400524 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500525 dprintk("%s Schedule Session Reset\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500526 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400527 }
528
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800529 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
530 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
531 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
532 spin_unlock(&tbl->slot_tbl_lock);
533 dprintk("%s enforce FIFO order\n", __func__);
534 return -EAGAIN;
535 }
536
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800537 slotid = nfs4_find_slot(tbl);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400538 if (slotid == NFS4_MAX_SLOT_TABLE) {
539 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
540 spin_unlock(&tbl->slot_tbl_lock);
541 dprintk("<-- %s: no free slots\n", __func__);
542 return -EAGAIN;
543 }
544 spin_unlock(&tbl->slot_tbl_lock);
545
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800546 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400547 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400548 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400549 args->sa_slotid = slotid;
550 args->sa_cache_this = cache_reply;
551
552 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
553
Benny Halevy99fe60d2009-04-01 09:22:29 -0400554 res->sr_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400555 res->sr_slotid = slotid;
556 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400557 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400558 /*
559 * sr_status is only set in decode_sequence, and so will remain
560 * set to 1 if an rpc level failure occurs.
561 */
562 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400563 return 0;
564}
565
Trond Myklebust035168a2010-06-16 09:52:26 -0400566int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400567 struct nfs4_sequence_args *args,
568 struct nfs4_sequence_res *res,
569 int cache_reply,
570 struct rpc_task *task)
571{
Trond Myklebust035168a2010-06-16 09:52:26 -0400572 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400573 int ret = 0;
574
Trond Myklebust035168a2010-06-16 09:52:26 -0400575 if (session == NULL) {
576 args->sa_session = NULL;
577 res->sr_session = NULL;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400578 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400579 }
580
581 dprintk("--> %s clp %p session %p sr_slotid %d\n",
582 __func__, session->clp, session, res->sr_slotid);
583
584 ret = nfs41_setup_sequence(session, args, res, cache_reply,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400585 task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400586out:
587 dprintk("<-- %s status=%d\n", __func__, ret);
588 return ret;
589}
590
591struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400592 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400593 struct nfs4_sequence_args *seq_args;
594 struct nfs4_sequence_res *seq_res;
595 int cache_reply;
596};
597
598static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
599{
600 struct nfs41_call_sync_data *data = calldata;
601
Trond Myklebust035168a2010-06-16 09:52:26 -0400602 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
603
604 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400605 data->seq_res, data->cache_reply, task))
606 return;
607 rpc_call_start(task);
608}
609
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800610static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
611{
612 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
613 nfs41_call_sync_prepare(task, calldata);
614}
615
Andy Adamson69ab40c2009-04-01 09:22:19 -0400616static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
617{
618 struct nfs41_call_sync_data *data = calldata;
619
Trond Myklebust14516c32010-07-31 14:29:06 -0400620 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400621}
622
Andy Adamsonce5039c2009-04-01 09:22:13 -0400623struct rpc_call_ops nfs41_call_sync_ops = {
624 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400625 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400626};
627
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800628struct rpc_call_ops nfs41_call_priv_sync_ops = {
629 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
630 .rpc_call_done = nfs41_call_sync_done,
631};
632
Trond Myklebust035168a2010-06-16 09:52:26 -0400633static int nfs4_call_sync_sequence(struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400634 struct rpc_message *msg,
635 struct nfs4_sequence_args *args,
636 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800637 int cache_reply,
638 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400639{
640 int ret;
641 struct rpc_task *task;
642 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400643 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400644 .seq_args = args,
645 .seq_res = res,
646 .cache_reply = cache_reply,
647 };
648 struct rpc_task_setup task_setup = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400649 .rpc_client = server->client,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400650 .rpc_message = msg,
651 .callback_ops = &nfs41_call_sync_ops,
652 .callback_data = &data
653 };
654
655 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800656 if (privileged)
657 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400658 task = rpc_run_task(&task_setup);
659 if (IS_ERR(task))
660 ret = PTR_ERR(task);
661 else {
662 ret = task->tk_status;
663 rpc_put_task(task);
664 }
665 return ret;
666}
667
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400668int _nfs4_call_sync_session(struct nfs_server *server,
669 struct rpc_message *msg,
670 struct nfs4_sequence_args *args,
671 struct nfs4_sequence_res *res,
672 int cache_reply)
673{
Trond Myklebust035168a2010-06-16 09:52:26 -0400674 return nfs4_call_sync_sequence(server, msg, args, res, cache_reply, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400675}
676
Trond Myklebustdf896452010-06-16 09:52:26 -0400677#else
Trond Myklebust14516c32010-07-31 14:29:06 -0400678static int nfs4_sequence_done(struct rpc_task *task,
679 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400680{
Trond Myklebust14516c32010-07-31 14:29:06 -0400681 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400682}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400683#endif /* CONFIG_NFS_V4_1 */
684
685int _nfs4_call_sync(struct nfs_server *server,
686 struct rpc_message *msg,
687 struct nfs4_sequence_args *args,
688 struct nfs4_sequence_res *res,
689 int cache_reply)
690{
Benny Halevyf3752972009-04-01 09:22:04 -0400691 args->sa_session = res->sr_session = NULL;
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400692 return rpc_call_sync(server->client, msg, 0);
693}
694
695#define nfs4_call_sync(server, msg, args, res, cache_reply) \
Trond Myklebust97dc1352010-06-16 09:52:26 -0400696 (server)->nfs_client->cl_mvops->call_sync((server), (msg), &(args)->seq_args, \
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400697 &(res)->seq_res, (cache_reply))
698
Trond Myklebust38478b22006-05-25 01:40:57 -0400699static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Trond Myklebust38478b22006-05-25 01:40:57 -0400701 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Trond Myklebust38478b22006-05-25 01:40:57 -0400703 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400704 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
705 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400706 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400707 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400708 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100711struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400712 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100713 struct nfs_openargs o_arg;
714 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100715 struct nfs_open_confirmargs c_arg;
716 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100717 struct nfs_fattr f_attr;
718 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400719 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100720 struct dentry *dir;
721 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400722 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100723 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100724 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400725 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100726 int rpc_status;
727 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100728};
729
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400730
731static void nfs4_init_opendata_res(struct nfs4_opendata *p)
732{
733 p->o_res.f_attr = &p->f_attr;
734 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400735 p->o_res.seqid = p->o_arg.seqid;
736 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400737 p->o_res.server = p->o_arg.server;
738 nfs_fattr_init(&p->f_attr);
739 nfs_fattr_init(&p->dir_attr);
Benny Halevy578e4582009-06-18 22:01:23 -0400740 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400741}
742
Trond Myklebustad389da2007-06-05 12:30:00 -0400743static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500744 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400745 const struct iattr *attrs,
746 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100747{
Trond Myklebustad389da2007-06-05 12:30:00 -0400748 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100749 struct inode *dir = parent->d_inode;
750 struct nfs_server *server = NFS_SERVER(dir);
751 struct nfs4_opendata *p;
752
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400753 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100754 if (p == NULL)
755 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400756 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100757 if (p->o_arg.seqid == NULL)
758 goto err_free;
Al Virof6948692010-01-30 13:51:04 -0500759 path_get(path);
760 p->path = *path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100761 p->dir = parent;
762 p->owner = sp;
763 atomic_inc(&sp->so_count);
764 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500765 p->o_arg.open_flags = flags;
766 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400767 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400768 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400769 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100770 p->o_arg.server = server;
771 p->o_arg.bitmask = server->attr_bitmask;
772 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400773 if (flags & O_CREAT) {
774 u32 *s;
775
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100776 p->o_arg.u.attrs = &p->attrs;
777 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400778 s = (u32 *) p->o_arg.u.verifier.data;
779 s[0] = jiffies;
780 s[1] = current->pid;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100781 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100782 p->c_arg.fh = &p->o_res.fh;
783 p->c_arg.stateid = &p->o_res.stateid;
784 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400785 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400786 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100787 return p;
788err_free:
789 kfree(p);
790err:
791 dput(parent);
792 return NULL;
793}
794
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400795static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100796{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400797 struct nfs4_opendata *p = container_of(kref,
798 struct nfs4_opendata, kref);
799
800 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400801 if (p->state != NULL)
802 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400803 nfs4_put_state_owner(p->owner);
804 dput(p->dir);
Jan Blunck31f31db12008-05-02 13:42:45 -0700805 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400806 kfree(p);
807}
808
809static void nfs4_opendata_put(struct nfs4_opendata *p)
810{
811 if (p != NULL)
812 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100813}
814
Trond Myklebust06f814a2006-01-03 09:55:07 +0100815static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
816{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100817 int ret;
818
Trond Myklebust06f814a2006-01-03 09:55:07 +0100819 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100820 return ret;
821}
822
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500823static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400824{
825 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500826
827 if (open_mode & O_EXCL)
828 goto out;
829 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400830 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500831 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
832 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400833 break;
834 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500835 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
836 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400837 break;
838 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500839 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
840 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400841 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500842out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400843 return ret;
844}
845
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500846static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400847{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500848 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400849 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500850 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400851 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500852 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400853 return 1;
854}
855
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500856static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100857{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500858 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100859 case FMODE_WRITE:
860 state->n_wronly++;
861 break;
862 case FMODE_READ:
863 state->n_rdonly++;
864 break;
865 case FMODE_READ|FMODE_WRITE:
866 state->n_rdwr++;
867 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500868 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100869}
870
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500871static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400872{
873 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
874 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
875 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500876 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400877 case FMODE_READ:
878 set_bit(NFS_O_RDONLY_STATE, &state->flags);
879 break;
880 case FMODE_WRITE:
881 set_bit(NFS_O_WRONLY_STATE, &state->flags);
882 break;
883 case FMODE_READ|FMODE_WRITE:
884 set_bit(NFS_O_RDWR_STATE, &state->flags);
885 }
886}
887
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500888static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400889{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400890 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500891 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400892 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400893}
894
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500895static 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 -0700896{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400897 /*
898 * Protect the call to nfs4_state_set_mode_locked and
899 * serialise the stateid update
900 */
901 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400902 if (deleg_stateid != NULL) {
903 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
904 set_bit(NFS_DELEGATED_STATE, &state->flags);
905 }
906 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500907 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400908 write_sequnlock(&state->seqlock);
909 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500910 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700911 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912}
913
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500914static 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 -0500915{
916 struct nfs_inode *nfsi = NFS_I(state->inode);
917 struct nfs_delegation *deleg_cur;
918 int ret = 0;
919
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500920 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500921
922 rcu_read_lock();
923 deleg_cur = rcu_dereference(nfsi->delegation);
924 if (deleg_cur == NULL)
925 goto no_delegation;
926
927 spin_lock(&deleg_cur->lock);
928 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500929 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500930 goto no_delegation_unlock;
931
932 if (delegation == NULL)
933 delegation = &deleg_cur->stateid;
934 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
935 goto no_delegation_unlock;
936
Trond Myklebustb7391f42008-12-23 15:21:52 -0500937 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500938 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500939 ret = 1;
940no_delegation_unlock:
941 spin_unlock(&deleg_cur->lock);
942no_delegation:
943 rcu_read_unlock();
944
945 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500946 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500947 ret = 1;
948 }
949
950 return ret;
951}
952
953
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500954static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400955{
956 struct nfs_delegation *delegation;
957
958 rcu_read_lock();
959 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500960 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400961 rcu_read_unlock();
962 return;
963 }
964 rcu_read_unlock();
965 nfs_inode_return_delegation(inode);
966}
967
Trond Myklebust6ee41262007-07-08 14:11:36 -0400968static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400969{
970 struct nfs4_state *state = opendata->state;
971 struct nfs_inode *nfsi = NFS_I(state->inode);
972 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500973 int open_mode = opendata->o_arg.open_flags & O_EXCL;
974 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400975 nfs4_stateid stateid;
976 int ret = -EAGAIN;
977
Trond Myklebustaac00a82007-07-05 19:02:21 -0400978 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500979 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400980 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500981 if (can_open_cached(state, fmode, open_mode)) {
982 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400983 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400984 goto out_return_state;
985 }
986 spin_unlock(&state->owner->so_lock);
987 }
Trond Myklebust34310432008-12-23 15:21:38 -0500988 rcu_read_lock();
989 delegation = rcu_dereference(nfsi->delegation);
990 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500991 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -0500992 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -0400993 break;
Trond Myklebust34310432008-12-23 15:21:38 -0500994 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400995 /* Save the delegation */
996 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
997 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -0400998 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400999 if (ret != 0)
1000 goto out;
1001 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001002
1003 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001004 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001005 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001006 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001007out:
1008 return ERR_PTR(ret);
1009out_return_state:
1010 atomic_inc(&state->count);
1011 return state;
1012}
1013
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001014static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1015{
1016 struct inode *inode;
1017 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001018 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001019 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001020
Trond Myklebustaac00a82007-07-05 19:02:21 -04001021 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001022 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001023 goto out;
1024 }
1025
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001026 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001027 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001028 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001029 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001030 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001031 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001032 goto err;
1033 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001034 state = nfs4_get_open_state(inode, data->owner);
1035 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001036 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001037 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001038 int delegation_flags = 0;
1039
Trond Myklebust003707c2007-07-05 18:07:55 -04001040 rcu_read_lock();
1041 delegation = rcu_dereference(NFS_I(inode)->delegation);
1042 if (delegation)
1043 delegation_flags = delegation->flags;
1044 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -05001045 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001046 nfs_inode_set_delegation(state->inode,
1047 data->owner->so_cred,
1048 &data->o_res);
1049 else
1050 nfs_inode_reclaim_delegation(state->inode,
1051 data->owner->so_cred,
1052 &data->o_res);
1053 }
Trond Myklebust34310432008-12-23 15:21:38 -05001054
1055 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001056 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001057 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001058out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001059 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001060err_put_inode:
1061 iput(inode);
1062err:
1063 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001064}
1065
Trond Myklebust864472e2006-01-03 09:55:15 +01001066static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1067{
1068 struct nfs_inode *nfsi = NFS_I(state->inode);
1069 struct nfs_open_context *ctx;
1070
1071 spin_lock(&state->inode->i_lock);
1072 list_for_each_entry(ctx, &nfsi->open_files, list) {
1073 if (ctx->state != state)
1074 continue;
1075 get_nfs_open_context(ctx);
1076 spin_unlock(&state->inode->i_lock);
1077 return ctx;
1078 }
1079 spin_unlock(&state->inode->i_lock);
1080 return ERR_PTR(-ENOENT);
1081}
1082
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001083static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1084{
1085 struct nfs4_opendata *opendata;
1086
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001087 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001088 if (opendata == NULL)
1089 return ERR_PTR(-ENOMEM);
1090 opendata->state = state;
1091 atomic_inc(&state->count);
1092 return opendata;
1093}
1094
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001095static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001096{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001097 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001098 int ret;
1099
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001100 opendata->o_arg.open_flags = 0;
1101 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001102 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1103 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1104 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001105 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001106 if (ret != 0)
1107 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001108 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001109 if (IS_ERR(newstate))
1110 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001111 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001112 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001113 return 0;
1114}
1115
1116static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1117{
Trond Myklebust864472e2006-01-03 09:55:15 +01001118 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001119 int ret;
1120
1121 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001122 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001123 smp_rmb();
1124 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001125 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001126 if (ret != 0)
1127 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001128 if (newstate != state)
1129 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001130 }
1131 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001132 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001133 if (ret != 0)
1134 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001135 if (newstate != state)
1136 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001137 }
1138 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001139 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001140 if (ret != 0)
1141 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001142 if (newstate != state)
1143 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001144 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001145 /*
1146 * We may have performed cached opens for all three recoveries.
1147 * Check if we need to update the current stateid.
1148 */
1149 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1150 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001151 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001152 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1153 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001154 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001155 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001156 return 0;
1157}
1158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159/*
1160 * OPEN_RECLAIM:
1161 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001163static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001165 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001166 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001167 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 int status;
1169
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001170 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1171 if (IS_ERR(opendata))
1172 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001173 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1174 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001175 rcu_read_lock();
1176 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001177 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001178 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001179 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001180 opendata->o_arg.u.delegation_type = delegation_type;
1181 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001182 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 return status;
1184}
1185
Trond Myklebust539cd032007-06-05 11:46:42 -04001186static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
1188 struct nfs_server *server = NFS_SERVER(state->inode);
1189 struct nfs4_exception exception = { };
1190 int err;
1191 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001192 err = _nfs4_do_open_reclaim(ctx, state);
Jeff Layton2c643482010-01-07 09:42:03 -05001193 if (err != -NFS4ERR_DELAY && err != -EKEYEXPIRED)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001194 break;
1195 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 } while (exception.retry);
1197 return err;
1198}
1199
Trond Myklebust864472e2006-01-03 09:55:15 +01001200static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1201{
1202 struct nfs_open_context *ctx;
1203 int ret;
1204
1205 ctx = nfs4_state_find_open_context(state);
1206 if (IS_ERR(ctx))
1207 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001208 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001209 put_nfs_open_context(ctx);
1210 return ret;
1211}
1212
Trond Myklebust13437e12007-07-06 15:10:43 -04001213static 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 -07001214{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001215 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001216 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001218 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1219 if (IS_ERR(opendata))
1220 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001221 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001222 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001223 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001224 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001225 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001226 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227}
1228
Trond Myklebust13437e12007-07-06 15:10:43 -04001229int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
1231 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001232 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 int err;
1234 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001235 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 switch (err) {
1237 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001238 case -ENOENT:
1239 case -ESTALE:
1240 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001241 case -NFS4ERR_BADSESSION:
1242 case -NFS4ERR_BADSLOT:
1243 case -NFS4ERR_BAD_HIGH_SLOT:
1244 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1245 case -NFS4ERR_DEADSESSION:
1246 nfs4_schedule_state_recovery(
1247 server->nfs_client);
1248 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 case -NFS4ERR_STALE_CLIENTID:
1250 case -NFS4ERR_STALE_STATEID:
1251 case -NFS4ERR_EXPIRED:
1252 /* Don't recall a delegation if it was lost */
David Howells7539bba2006-08-22 20:06:09 -04001253 nfs4_schedule_state_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001254 goto out;
1255 case -ERESTARTSYS:
1256 /*
1257 * The show must go on: exit, but mark the
1258 * stateid as needing recovery.
1259 */
1260 case -NFS4ERR_ADMIN_REVOKED:
1261 case -NFS4ERR_BAD_STATEID:
1262 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
1263 case -ENOMEM:
1264 err = 0;
1265 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 }
1267 err = nfs4_handle_exception(server, err, &exception);
1268 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001269out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 return err;
1271}
1272
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001273static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1274{
1275 struct nfs4_opendata *data = calldata;
1276
1277 data->rpc_status = task->tk_status;
1278 if (RPC_ASSASSINATED(task))
1279 return;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001280 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001281 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1282 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001283 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001284 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001285 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001286 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001287}
1288
1289static void nfs4_open_confirm_release(void *calldata)
1290{
1291 struct nfs4_opendata *data = calldata;
1292 struct nfs4_state *state = NULL;
1293
1294 /* If this request hasn't been cancelled, do nothing */
1295 if (data->cancelled == 0)
1296 goto out_free;
1297 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001298 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001299 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001300 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001301 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001302 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001303out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001304 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001305}
1306
1307static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001308 .rpc_call_done = nfs4_open_confirm_done,
1309 .rpc_release = nfs4_open_confirm_release,
1310};
1311
1312/*
1313 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1314 */
1315static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1316{
1317 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1318 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001319 struct rpc_message msg = {
1320 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1321 .rpc_argp = &data->c_arg,
1322 .rpc_resp = &data->c_res,
1323 .rpc_cred = data->owner->so_cred,
1324 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001325 struct rpc_task_setup task_setup_data = {
1326 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001327 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001328 .callback_ops = &nfs4_open_confirm_ops,
1329 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001330 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001331 .flags = RPC_TASK_ASYNC,
1332 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 int status;
1334
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001335 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001336 data->rpc_done = 0;
1337 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001338 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001339 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001340 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001341 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001342 status = nfs4_wait_for_completion_rpc_task(task);
1343 if (status != 0) {
1344 data->cancelled = 1;
1345 smp_wmb();
1346 } else
1347 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001348 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 return status;
1350}
1351
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001352static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001354 struct nfs4_opendata *data = calldata;
1355 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001356
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001357 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1358 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001359 /*
1360 * Check if we still need to send an OPEN call, or if we can use
1361 * a delegation instead.
1362 */
1363 if (data->state != NULL) {
1364 struct nfs_delegation *delegation;
1365
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001366 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001367 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001368 rcu_read_lock();
1369 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1370 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001371 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001372 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001373 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001374 }
1375 rcu_read_unlock();
1376 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001377 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001378 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001379 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001380 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001381 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001382 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1383 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001384 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001385 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001386 &data->o_arg.seq_args,
1387 &data->o_res.seq_res, 1, task))
1388 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001389 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001390 return;
1391out_no_action:
1392 task->tk_action = NULL;
1393
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001394}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001396static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1397{
1398 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1399 nfs4_open_prepare(task, calldata);
1400}
1401
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001402static void nfs4_open_done(struct rpc_task *task, void *calldata)
1403{
1404 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001406 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001407
Trond Myklebust14516c32010-07-31 14:29:06 -04001408 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1409 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001410
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001411 if (RPC_ASSASSINATED(task))
1412 return;
1413 if (task->tk_status == 0) {
1414 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001415 case S_IFREG:
1416 break;
1417 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001418 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001419 break;
1420 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001421 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001422 break;
1423 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001424 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001425 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001426 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001427 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1428 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001429 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001430 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001431}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001432
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001433static void nfs4_open_release(void *calldata)
1434{
1435 struct nfs4_opendata *data = calldata;
1436 struct nfs4_state *state = NULL;
1437
1438 /* If this request hasn't been cancelled, do nothing */
1439 if (data->cancelled == 0)
1440 goto out_free;
1441 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001442 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001443 goto out_free;
1444 /* In case we need an open_confirm, no cleanup! */
1445 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1446 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001447 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001448 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001449 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001450out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001451 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001452}
1453
1454static const struct rpc_call_ops nfs4_open_ops = {
1455 .rpc_call_prepare = nfs4_open_prepare,
1456 .rpc_call_done = nfs4_open_done,
1457 .rpc_release = nfs4_open_release,
1458};
1459
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001460static const struct rpc_call_ops nfs4_recover_open_ops = {
1461 .rpc_call_prepare = nfs4_recover_open_prepare,
1462 .rpc_call_done = nfs4_open_done,
1463 .rpc_release = nfs4_open_release,
1464};
1465
1466static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001467{
1468 struct inode *dir = data->dir->d_inode;
1469 struct nfs_server *server = NFS_SERVER(dir);
1470 struct nfs_openargs *o_arg = &data->o_arg;
1471 struct nfs_openres *o_res = &data->o_res;
1472 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001473 struct rpc_message msg = {
1474 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1475 .rpc_argp = o_arg,
1476 .rpc_resp = o_res,
1477 .rpc_cred = data->owner->so_cred,
1478 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001479 struct rpc_task_setup task_setup_data = {
1480 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001481 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001482 .callback_ops = &nfs4_open_ops,
1483 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001484 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001485 .flags = RPC_TASK_ASYNC,
1486 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001487 int status;
1488
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001489 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001490 data->rpc_done = 0;
1491 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001492 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001493 if (isrecover)
1494 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001495 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001496 if (IS_ERR(task))
1497 return PTR_ERR(task);
1498 status = nfs4_wait_for_completion_rpc_task(task);
1499 if (status != 0) {
1500 data->cancelled = 1;
1501 smp_wmb();
1502 } else
1503 status = data->rpc_status;
1504 rpc_put_task(task);
1505
1506 return status;
1507}
1508
1509static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1510{
1511 struct inode *dir = data->dir->d_inode;
1512 struct nfs_openres *o_res = &data->o_res;
1513 int status;
1514
1515 status = nfs4_run_open_task(data, 1);
1516 if (status != 0 || !data->rpc_done)
1517 return status;
1518
1519 nfs_refresh_inode(dir, o_res->dir_attr);
1520
1521 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1522 status = _nfs4_proc_open_confirm(data);
1523 if (status != 0)
1524 return status;
1525 }
1526
1527 return status;
1528}
1529
1530/*
1531 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1532 */
1533static int _nfs4_proc_open(struct nfs4_opendata *data)
1534{
1535 struct inode *dir = data->dir->d_inode;
1536 struct nfs_server *server = NFS_SERVER(dir);
1537 struct nfs_openargs *o_arg = &data->o_arg;
1538 struct nfs_openres *o_res = &data->o_res;
1539 int status;
1540
1541 status = nfs4_run_open_task(data, 0);
Trond Myklebust3e309912007-07-07 13:19:59 -04001542 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001543 return status;
1544
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001545 if (o_arg->open_flags & O_CREAT) {
1546 update_changeattr(dir, &o_res->cinfo);
1547 nfs_post_op_update_inode(dir, o_res->dir_attr);
1548 } else
1549 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001550 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1551 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001553 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001555 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 }
1557 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001558 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001559 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560}
1561
Trond Myklebust10afec92007-05-14 17:16:04 -04001562static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +01001563{
David Howells7539bba2006-08-22 20:06:09 -04001564 struct nfs_client *clp = server->nfs_client;
Trond Myklebusta78cb572009-08-09 15:06:19 -04001565 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001566 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001567
Trond Myklebusta78cb572009-08-09 15:06:19 -04001568 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001569 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001570 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001571 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001572 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1573 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001574 break;
Trond Myklebust58d97142006-01-03 09:55:24 +01001575 nfs4_schedule_state_recovery(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001576 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001577 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001578 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001579}
1580
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581/*
1582 * OPEN_EXPIRED:
1583 * reclaim state on the server after a network partition.
1584 * Assumes caller holds the appropriate lock
1585 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001586static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001588 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001589 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001591 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1592 if (IS_ERR(opendata))
1593 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001594 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001595 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001596 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001597 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001598 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599}
1600
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001601static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001602{
Trond Myklebust539cd032007-06-05 11:46:42 -04001603 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001604 struct nfs4_exception exception = { };
1605 int err;
1606
1607 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001608 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001609 switch (err) {
1610 default:
1611 goto out;
1612 case -NFS4ERR_GRACE:
1613 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05001614 case -EKEYEXPIRED:
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001615 nfs4_handle_exception(server, err, &exception);
1616 err = 0;
1617 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001618 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001619out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001620 return err;
1621}
1622
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1624{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001626 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Trond Myklebust864472e2006-01-03 09:55:15 +01001628 ctx = nfs4_state_find_open_context(state);
1629 if (IS_ERR(ctx))
1630 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001631 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001632 put_nfs_open_context(ctx);
1633 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634}
1635
1636/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001637 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1638 * fields corresponding to attributes that were used to store the verifier.
1639 * Make sure we clobber those fields in the later setattr call
1640 */
1641static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1642{
1643 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1644 !(sattr->ia_valid & ATTR_ATIME_SET))
1645 sattr->ia_valid |= ATTR_ATIME;
1646
1647 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1648 !(sattr->ia_valid & ATTR_MTIME_SET))
1649 sattr->ia_valid |= ATTR_MTIME;
1650}
1651
1652/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001653 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001655static 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 -07001656{
1657 struct nfs4_state_owner *sp;
1658 struct nfs4_state *state = NULL;
1659 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001660 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001661 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
1663 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 status = -ENOMEM;
1665 if (!(sp = nfs4_get_state_owner(server, cred))) {
1666 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1667 goto out_err;
1668 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001669 status = nfs4_recover_expired_lease(server);
1670 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001671 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001672 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001673 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001674 status = -ENOMEM;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001675 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001676 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001677 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Trond Myklebustaac00a82007-07-05 19:02:21 -04001679 if (path->dentry->d_inode != NULL)
1680 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1681
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001682 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001684 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001686 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001687 status = PTR_ERR(state);
1688 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001689 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001690 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001691 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001692
1693 if (opendata->o_arg.open_flags & O_EXCL) {
1694 nfs4_exclusive_attrset(opendata, sattr);
1695
1696 nfs_fattr_init(opendata->o_res.f_attr);
1697 status = nfs4_do_setattr(state->inode, cred,
1698 opendata->o_res.f_attr, sattr,
1699 state);
1700 if (status == 0)
1701 nfs_setattr_update_inode(state->inode, sattr);
1702 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1703 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001704 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 *res = state;
1707 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001708err_opendata_put:
1709 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001710err_put_state_owner:
1711 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 *res = NULL;
1714 return status;
1715}
1716
1717
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001718static 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 -07001719{
1720 struct nfs4_exception exception = { };
1721 struct nfs4_state *res;
1722 int status;
1723
1724 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001725 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 if (status == 0)
1727 break;
1728 /* NOTE: BAD_SEQID means the server and client disagree about the
1729 * book-keeping w.r.t. state-changing operations
1730 * (OPEN/CLOSE/LOCK/LOCKU...)
1731 * It is actually a sign of a bug on the client or on the server.
1732 *
1733 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001734 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 * have unhashed the old state_owner for us, and that we can
1736 * therefore safely retry using a new one. We should still warn
1737 * the user though...
1738 */
1739 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001740 printk(KERN_WARNING "NFS: v4 server %s "
1741 " returned a bad sequence-id error!\n",
1742 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 exception.retry = 1;
1744 continue;
1745 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001746 /*
1747 * BAD_STATEID on OPEN means that the server cancelled our
1748 * state before it received the OPEN_CONFIRM.
1749 * Recover by retrying the request as per the discussion
1750 * on Page 181 of RFC3530.
1751 */
1752 if (status == -NFS4ERR_BAD_STATEID) {
1753 exception.retry = 1;
1754 continue;
1755 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001756 if (status == -EAGAIN) {
1757 /* We must have found a delegation */
1758 exception.retry = 1;
1759 continue;
1760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1762 status, &exception));
1763 } while (exception.retry);
1764 return res;
1765}
1766
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001767static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1768 struct nfs_fattr *fattr, struct iattr *sattr,
1769 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001771 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001773 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 .iap = sattr,
1775 .server = server,
1776 .bitmask = server->attr_bitmask,
1777 };
1778 struct nfs_setattrres res = {
1779 .fattr = fattr,
1780 .server = server,
1781 };
1782 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001783 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1784 .rpc_argp = &arg,
1785 .rpc_resp = &res,
1786 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001788 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001789 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Trond Myklebust0e574af2005-10-27 22:12:38 -04001791 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001793 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1794 /* Use that stateid */
1795 } else if (state != NULL) {
Trond Myklebust77041ed2010-07-01 12:49:11 -04001796 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001797 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1799
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001800 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001801 if (status == 0 && state != NULL)
1802 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001803 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804}
1805
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001806static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1807 struct nfs_fattr *fattr, struct iattr *sattr,
1808 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001810 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 struct nfs4_exception exception = { };
1812 int err;
1813 do {
1814 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001815 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 &exception);
1817 } while (exception.retry);
1818 return err;
1819}
1820
1821struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001822 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 struct inode *inode;
1824 struct nfs4_state *state;
1825 struct nfs_closeargs arg;
1826 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001827 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001828 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829};
1830
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001831static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001832{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001833 struct nfs4_closedata *calldata = data;
1834 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001835
1836 nfs4_put_open_state(calldata->state);
1837 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001838 nfs4_put_state_owner(sp);
Jan Blunck31f31db12008-05-02 13:42:45 -07001839 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001840 kfree(calldata);
1841}
1842
Trond Myklebust88069f72009-12-08 08:33:16 -05001843static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1844 fmode_t fmode)
1845{
1846 spin_lock(&state->owner->so_lock);
1847 if (!(fmode & FMODE_READ))
1848 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1849 if (!(fmode & FMODE_WRITE))
1850 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1851 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1852 spin_unlock(&state->owner->so_lock);
1853}
1854
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001855static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001857 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 struct nfs_server *server = NFS_SERVER(calldata->inode);
1860
Trond Myklebust14516c32010-07-31 14:29:06 -04001861 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1862 return;
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001863 if (RPC_ASSASSINATED(task))
1864 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 /* hmm. we are done with the inode, and in the process of freeing
1866 * the state_owner. we keep this around to process errors
1867 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 switch (task->tk_status) {
1869 case 0:
Trond Myklebust45328c32007-07-26 17:47:34 -04001870 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001871 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001872 nfs4_close_clear_stateid_flags(state,
1873 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 break;
1875 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001876 case -NFS4ERR_OLD_STATEID:
1877 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001879 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001880 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001882 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1883 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001885 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001886 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887}
1888
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001889static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001891 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001892 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001893 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001894
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001895 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001896 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001897
Trond Myklebust88069f72009-12-08 08:33:16 -05001898 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1899 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001900 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001901 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001902 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001903 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001904 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1905 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1906 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001907 }
1908 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001909 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1910 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1911 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001912 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001913 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001914 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05001915
1916 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001917 /* Note: exit _without_ calling nfs4_close_done */
1918 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001919 return;
1920 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001921
1922 if (calldata->arg.fmode == 0)
1923 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
1924
Trond Myklebust516a6af2005-10-27 22:12:41 -04001925 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001926 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001927 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Andy Adamson19ddab02009-04-01 09:22:20 -04001928 &calldata->arg.seq_args, &calldata->res.seq_res,
1929 1, task))
1930 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001931 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932}
1933
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001934static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001935 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001936 .rpc_call_done = nfs4_close_done,
1937 .rpc_release = nfs4_free_closedata,
1938};
1939
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940/*
1941 * It is possible for data to be read/written from a mem-mapped file
1942 * after the sys_close call (which hits the vfs layer as a flush).
1943 * This means that we can't safely call nfsv4 close on a file until
1944 * the inode is cleared. This in turn means that we are not good
1945 * NFSv4 citizens - we do not indicate to the server to update the file's
1946 * share state even when we are done with one of the three share
1947 * stateid's in the inode.
1948 *
1949 * NOTE: Caller must be holding the sp->so_owner semaphore!
1950 */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001951int nfs4_do_close(struct path *path, struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001953 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001955 struct nfs4_state_owner *sp = state->owner;
1956 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001957 struct rpc_message msg = {
1958 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1959 .rpc_cred = state->owner->so_cred,
1960 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001961 struct rpc_task_setup task_setup_data = {
1962 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001963 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001964 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05001965 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001966 .flags = RPC_TASK_ASYNC,
1967 };
Trond Myklebust95121352005-10-18 14:20:12 -07001968 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001970 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001972 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001973 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001975 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04001976 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001978 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07001979 if (calldata->arg.seqid == NULL)
1980 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001981 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001982 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001983 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04001984 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001985 calldata->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04001986 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Al Virof6948692010-01-30 13:51:04 -05001987 path_get(path);
1988 calldata->path = *path;
Trond Myklebust95121352005-10-18 14:20:12 -07001989
Trond Myklebust5138fde2007-07-14 15:40:01 -04001990 msg.rpc_argp = &calldata->arg,
1991 msg.rpc_resp = &calldata->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001992 task_setup_data.callback_data = calldata;
1993 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001994 if (IS_ERR(task))
1995 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001996 status = 0;
1997 if (wait)
1998 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001999 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002000 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002001out_free_calldata:
2002 kfree(calldata);
2003out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002004 nfs4_put_open_state(state);
2005 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002006 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007}
2008
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002009static 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 -07002010{
2011 struct file *filp;
Trond Myklebust1b45c462007-07-03 13:04:56 -04002012 int ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002013
Trond Myklebust1b45c462007-07-03 13:04:56 -04002014 /* If the open_intent is for execute, we have an extra check to make */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002015 if (fmode & FMODE_EXEC) {
Trond Myklebustaf22f942007-08-10 17:45:10 -04002016 ret = nfs_may_open(state->inode,
Trond Myklebust1b45c462007-07-03 13:04:56 -04002017 state->owner->so_cred,
2018 nd->intent.open.flags);
2019 if (ret < 0)
2020 goto out_close;
2021 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002022 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002023 if (!IS_ERR(filp)) {
2024 struct nfs_open_context *ctx;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04002025 ctx = nfs_file_open_context(filp);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002026 ctx->state = state;
Trond Myklebust95cf9592006-04-18 13:14:06 -04002027 return 0;
2028 }
Trond Myklebust1b45c462007-07-03 13:04:56 -04002029 ret = PTR_ERR(filp);
2030out_close:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002031 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
Trond Myklebust1b45c462007-07-03 13:04:56 -04002032 return ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002033}
2034
2035struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
2037{
Trond Myklebustad389da2007-06-05 12:30:00 -04002038 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002039 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002040 .dentry = dentry,
2041 };
Jan Blunck4ac91372008-02-14 19:34:32 -08002042 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 struct iattr attr;
2044 struct rpc_cred *cred;
2045 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002046 struct dentry *res;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002047 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
2049 if (nd->flags & LOOKUP_CREATE) {
2050 attr.ia_mode = nd->intent.open.create_mode;
2051 attr.ia_valid = ATTR_MODE;
2052 if (!IS_POSIXACL(dir))
Al Viroce3b0f82009-03-29 19:08:22 -04002053 attr.ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 } else {
2055 attr.ia_valid = 0;
2056 BUG_ON(nd->intent.open.flags & O_CREAT);
2057 }
2058
Trond Myklebust98a8e322008-03-12 12:25:28 -04002059 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07002061 return (struct dentry *)cred;
Trond Myklebust565277f2007-10-15 18:17:53 -04002062 parent = dentry->d_parent;
2063 /* Protect against concurrent sillydeletes */
2064 nfs_block_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002065 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002067 if (IS_ERR(state)) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04002068 if (PTR_ERR(state) == -ENOENT) {
Trond Myklebust02a913a2005-10-18 14:20:17 -07002069 d_add(dentry, NULL);
Trond Myklebustd75340c2007-10-01 21:42:01 -04002070 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2071 }
Trond Myklebust565277f2007-10-15 18:17:53 -04002072 nfs_unblock_sillyrename(parent);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002073 return (struct dentry *)state;
2074 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002075 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07002076 if (res != NULL)
Trond Myklebustdeee9362007-08-27 11:33:00 -04002077 path.dentry = res;
Trond Myklebustd75340c2007-10-01 21:42:01 -04002078 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
Trond Myklebust565277f2007-10-15 18:17:53 -04002079 nfs_unblock_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002080 nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002081 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082}
2083
2084int
Trond Myklebust02a913a2005-10-18 14:20:17 -07002085nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086{
Trond Myklebustad389da2007-06-05 12:30:00 -04002087 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002088 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002089 .dentry = dentry,
2090 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 struct rpc_cred *cred;
2092 struct nfs4_state *state;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002093 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
Trond Myklebust98a8e322008-03-12 12:25:28 -04002095 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 if (IS_ERR(cred))
2097 return PTR_ERR(cred);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002098 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002100 if (IS_ERR(state)) {
2101 switch (PTR_ERR(state)) {
2102 case -EPERM:
2103 case -EACCES:
2104 case -EDQUOT:
2105 case -ENOSPC:
2106 case -EROFS:
Al Viro04287f92010-04-08 00:06:07 +01002107 return PTR_ERR(state);
Chuck Leverb87c0ad2006-10-19 23:28:42 -07002108 default:
2109 goto out_drop;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002110 }
Trond Myklebust02a913a2005-10-18 14:20:17 -07002111 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002112 if (state->inode == dentry->d_inode) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04002113 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002114 nfs4_intent_set_file(nd, &path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 return 1;
2116 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002117 nfs4_close_sync(&path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002118out_drop:
2119 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 return 0;
2121}
2122
Trond Myklebust1185a552009-12-03 15:54:02 -05002123static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002124{
2125 if (ctx->state == NULL)
2126 return;
2127 if (is_sync)
2128 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2129 else
2130 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2131}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
2133static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2134{
Benny Halevy43652ad2009-04-01 09:21:54 -04002135 struct nfs4_server_caps_arg args = {
2136 .fhandle = fhandle,
2137 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 struct nfs4_server_caps_res res = {};
2139 struct rpc_message msg = {
2140 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002141 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 .rpc_resp = &res,
2143 };
2144 int status;
2145
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002146 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 if (status == 0) {
2148 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002149 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2150 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2151 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2152 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2153 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2155 server->caps |= NFS_CAP_ACLS;
2156 if (res.has_links != 0)
2157 server->caps |= NFS_CAP_HARDLINKS;
2158 if (res.has_symlinks != 0)
2159 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002160 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2161 server->caps |= NFS_CAP_FILEID;
2162 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2163 server->caps |= NFS_CAP_MODE;
2164 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2165 server->caps |= NFS_CAP_NLINK;
2166 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2167 server->caps |= NFS_CAP_OWNER;
2168 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2169 server->caps |= NFS_CAP_OWNER_GROUP;
2170 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2171 server->caps |= NFS_CAP_ATIME;
2172 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2173 server->caps |= NFS_CAP_CTIME;
2174 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2175 server->caps |= NFS_CAP_MTIME;
2176
Trond Myklebusta65318b2009-03-11 14:10:28 -04002177 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2178 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2179 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 server->acl_bitmask = res.acl_bitmask;
2181 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002182
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 return status;
2184}
2185
Trond Myklebust55a97592006-06-09 09:34:19 -04002186int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187{
2188 struct nfs4_exception exception = { };
2189 int err;
2190 do {
2191 err = nfs4_handle_exception(server,
2192 _nfs4_server_capabilities(server, fhandle),
2193 &exception);
2194 } while (exception.retry);
2195 return err;
2196}
2197
2198static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2199 struct nfs_fsinfo *info)
2200{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 struct nfs4_lookup_root_arg args = {
2202 .bitmask = nfs4_fattr_bitmap,
2203 };
2204 struct nfs4_lookup_res res = {
2205 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002206 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 .fh = fhandle,
2208 };
2209 struct rpc_message msg = {
2210 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2211 .rpc_argp = &args,
2212 .rpc_resp = &res,
2213 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002214
Trond Myklebust0e574af2005-10-27 22:12:38 -04002215 nfs_fattr_init(info->fattr);
Trond Myklebustfccba802009-07-21 16:48:07 -04002216 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217}
2218
2219static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2220 struct nfs_fsinfo *info)
2221{
2222 struct nfs4_exception exception = { };
2223 int err;
2224 do {
2225 err = nfs4_handle_exception(server,
2226 _nfs4_lookup_root(server, fhandle, info),
2227 &exception);
2228 } while (exception.retry);
2229 return err;
2230}
2231
David Howells54ceac42006-08-22 20:06:13 -04002232/*
2233 * get the file handle for the "/" directory on the server
2234 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002236 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 int status;
2239
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 if (status == 0)
2242 status = nfs4_server_capabilities(server, fhandle);
2243 if (status == 0)
2244 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002245 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246}
2247
Manoj Naik6b97fd32006-06-09 09:34:29 -04002248/*
2249 * Get locations and (maybe) other attributes of a referral.
2250 * Note that we'll actually follow the referral later when
2251 * we detect fsid mismatch in inode revalidation
2252 */
Trond Myklebust56659e92007-07-17 21:52:39 -04002253static 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 -04002254{
2255 int status = -ENOMEM;
2256 struct page *page = NULL;
2257 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002258
2259 page = alloc_page(GFP_KERNEL);
2260 if (page == NULL)
2261 goto out;
2262 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2263 if (locations == NULL)
2264 goto out;
2265
Trond Myklebustc228fd32007-01-13 02:28:11 -05002266 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002267 if (status != 0)
2268 goto out;
2269 /* Make sure server returned a different fsid for the referral */
2270 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07002271 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 -04002272 status = -EIO;
2273 goto out;
2274 }
2275
2276 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2277 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2278 if (!fattr->mode)
2279 fattr->mode = S_IFDIR;
2280 memset(fhandle, 0, sizeof(struct nfs_fh));
2281out:
2282 if (page)
2283 __free_page(page);
2284 if (locations)
2285 kfree(locations);
2286 return status;
2287}
2288
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2290{
2291 struct nfs4_getattr_arg args = {
2292 .fh = fhandle,
2293 .bitmask = server->attr_bitmask,
2294 };
2295 struct nfs4_getattr_res res = {
2296 .fattr = fattr,
2297 .server = server,
2298 };
2299 struct rpc_message msg = {
2300 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2301 .rpc_argp = &args,
2302 .rpc_resp = &res,
2303 };
2304
Trond Myklebust0e574af2005-10-27 22:12:38 -04002305 nfs_fattr_init(fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002306 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307}
2308
2309static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2310{
2311 struct nfs4_exception exception = { };
2312 int err;
2313 do {
2314 err = nfs4_handle_exception(server,
2315 _nfs4_proc_getattr(server, fhandle, fattr),
2316 &exception);
2317 } while (exception.retry);
2318 return err;
2319}
2320
2321/*
2322 * The file is not closed if it is opened due to the a request to change
2323 * the size of the file. The open call will not be needed once the
2324 * VFS layer lookup-intents are implemented.
2325 *
2326 * Close is called when the inode is destroyed.
2327 * If we haven't opened the file for O_WRONLY, we
2328 * need to in the size_change case to obtain a stateid.
2329 *
2330 * Got race?
2331 * Because OPEN is always done by name in nfsv4, it is
2332 * possible that we opened a different file by the same
2333 * name. We can recognize this race condition, but we
2334 * can't do anything about it besides returning an error.
2335 *
2336 * This will be fixed with VFS changes (lookup-intent).
2337 */
2338static int
2339nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2340 struct iattr *sattr)
2341{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002342 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002343 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002344 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 int status;
2346
Trond Myklebust0e574af2005-10-27 22:12:38 -04002347 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
Trond Myklebustd5308382005-11-04 15:33:38 -05002349 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002350 if (sattr->ia_valid & ATTR_FILE) {
2351 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002352
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002353 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002354 if (ctx) {
2355 cred = ctx->cred;
2356 state = ctx->state;
2357 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002358 }
2359
2360 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002361 if (status == 0)
2362 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 return status;
2364}
2365
Trond Myklebust56659e92007-07-17 21:52:39 -04002366static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2367 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04002368 struct nfs_fattr *fattr)
2369{
2370 int status;
2371 struct nfs4_lookup_arg args = {
2372 .bitmask = server->attr_bitmask,
2373 .dir_fh = dirfh,
2374 .name = name,
2375 };
2376 struct nfs4_lookup_res res = {
2377 .server = server,
2378 .fattr = fattr,
2379 .fh = fhandle,
2380 };
2381 struct rpc_message msg = {
2382 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2383 .rpc_argp = &args,
2384 .rpc_resp = &res,
2385 };
2386
2387 nfs_fattr_init(fattr);
2388
2389 dprintk("NFS call lookupfh %s\n", name->name);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002390 status = nfs4_call_sync(server, &msg, &args, &res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002391 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002392 return status;
2393}
2394
2395static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2396 struct qstr *name, struct nfs_fh *fhandle,
2397 struct nfs_fattr *fattr)
2398{
2399 struct nfs4_exception exception = { };
2400 int err;
2401 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04002402 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2403 /* FIXME: !!!! */
2404 if (err == -NFS4ERR_MOVED) {
2405 err = -EREMOTE;
2406 break;
2407 }
2408 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002409 } while (exception.retry);
2410 return err;
2411}
2412
Trond Myklebust56659e92007-07-17 21:52:39 -04002413static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2415{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002416 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
2418 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002419 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002420 if (status == -NFS4ERR_MOVED)
2421 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 dprintk("NFS reply lookup: %d\n", status);
2423 return status;
2424}
2425
2426static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2427{
2428 struct nfs4_exception exception = { };
2429 int err;
2430 do {
2431 err = nfs4_handle_exception(NFS_SERVER(dir),
2432 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2433 &exception);
2434 } while (exception.retry);
2435 return err;
2436}
2437
2438static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2439{
Trond Myklebust76b32992007-08-10 17:45:11 -04002440 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 struct nfs4_accessargs args = {
2442 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002443 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002445 struct nfs4_accessres res = {
2446 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002447 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 struct rpc_message msg = {
2449 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2450 .rpc_argp = &args,
2451 .rpc_resp = &res,
2452 .rpc_cred = entry->cred,
2453 };
2454 int mode = entry->mask;
2455 int status;
2456
2457 /*
2458 * Determine which access bits we want to ask for...
2459 */
2460 if (mode & MAY_READ)
2461 args.access |= NFS4_ACCESS_READ;
2462 if (S_ISDIR(inode->i_mode)) {
2463 if (mode & MAY_WRITE)
2464 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2465 if (mode & MAY_EXEC)
2466 args.access |= NFS4_ACCESS_LOOKUP;
2467 } else {
2468 if (mode & MAY_WRITE)
2469 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2470 if (mode & MAY_EXEC)
2471 args.access |= NFS4_ACCESS_EXECUTE;
2472 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002473
2474 res.fattr = nfs_alloc_fattr();
2475 if (res.fattr == NULL)
2476 return -ENOMEM;
2477
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002478 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 if (!status) {
2480 entry->mask = 0;
2481 if (res.access & NFS4_ACCESS_READ)
2482 entry->mask |= MAY_READ;
2483 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2484 entry->mask |= MAY_WRITE;
2485 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2486 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002487 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002489 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 return status;
2491}
2492
2493static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2494{
2495 struct nfs4_exception exception = { };
2496 int err;
2497 do {
2498 err = nfs4_handle_exception(NFS_SERVER(inode),
2499 _nfs4_proc_access(inode, entry),
2500 &exception);
2501 } while (exception.retry);
2502 return err;
2503}
2504
2505/*
2506 * TODO: For the time being, we don't try to get any attributes
2507 * along with any of the zero-copy operations READ, READDIR,
2508 * READLINK, WRITE.
2509 *
2510 * In the case of the first three, we want to put the GETATTR
2511 * after the read-type operation -- this is because it is hard
2512 * to predict the length of a GETATTR response in v4, and thus
2513 * align the READ data correctly. This means that the GETATTR
2514 * may end up partially falling into the page cache, and we should
2515 * shift it into the 'tail' of the xdr_buf before processing.
2516 * To do this efficiently, we need to know the total length
2517 * of data received, which doesn't seem to be available outside
2518 * of the RPC layer.
2519 *
2520 * In the case of WRITE, we also want to put the GETATTR after
2521 * the operation -- in this case because we want to make sure
2522 * we get the post-operation mtime and size. This means that
2523 * we can't use xdr_encode_pages() as written: we need a variant
2524 * of it which would leave room in the 'tail' iovec.
2525 *
2526 * Both of these changes to the XDR layer would in fact be quite
2527 * minor, but I decided to leave them for a subsequent patch.
2528 */
2529static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2530 unsigned int pgbase, unsigned int pglen)
2531{
2532 struct nfs4_readlink args = {
2533 .fh = NFS_FH(inode),
2534 .pgbase = pgbase,
2535 .pglen = pglen,
2536 .pages = &page,
2537 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002538 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 struct rpc_message msg = {
2540 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2541 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002542 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 };
2544
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002545 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546}
2547
2548static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2549 unsigned int pgbase, unsigned int pglen)
2550{
2551 struct nfs4_exception exception = { };
2552 int err;
2553 do {
2554 err = nfs4_handle_exception(NFS_SERVER(inode),
2555 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2556 &exception);
2557 } while (exception.retry);
2558 return err;
2559}
2560
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561/*
2562 * Got race?
2563 * We will need to arrange for the VFS layer to provide an atomic open.
2564 * Until then, this create/open method is prone to inefficiency and race
2565 * conditions due to the lookup, create, and open VFS calls from sys_open()
2566 * placed on the wire.
2567 *
2568 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2569 * The file will be opened again in the subsequent VFS open call
2570 * (nfs4_proc_file_open).
2571 *
2572 * The open for read will just hang around to be used by any process that
2573 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2574 */
2575
2576static int
2577nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07002578 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579{
Trond Myklebustad389da2007-06-05 12:30:00 -04002580 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002581 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002582 .dentry = dentry,
2583 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 struct nfs4_state *state;
2585 struct rpc_cred *cred;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002586 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 int status = 0;
2588
Trond Myklebust98a8e322008-03-12 12:25:28 -04002589 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 if (IS_ERR(cred)) {
2591 status = PTR_ERR(cred);
2592 goto out;
2593 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002594 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002595 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 if (IS_ERR(state)) {
2597 status = PTR_ERR(state);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002598 goto out_putcred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002600 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002601 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustad389da2007-06-05 12:30:00 -04002602 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002603 status = nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002604 else
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002605 nfs4_close_sync(&path, state, fmode);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002606out_putcred:
2607 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608out:
2609 return status;
2610}
2611
2612static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2613{
Trond Myklebust16e42952005-10-27 22:12:44 -04002614 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002615 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002617 .name.len = name->len,
2618 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002619 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002621 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002622 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002623 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002625 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2626 .rpc_argp = &args,
2627 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002629 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
Trond Myklebustd3468902010-04-16 16:22:50 -04002631 res.dir_attr = nfs_alloc_fattr();
2632 if (res.dir_attr == NULL)
2633 goto out;
2634
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002635 status = nfs4_call_sync(server, &msg, &args, &res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002636 if (status == 0) {
2637 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002638 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002639 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002640 nfs_free_fattr(res.dir_attr);
2641out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 return status;
2643}
2644
2645static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2646{
2647 struct nfs4_exception exception = { };
2648 int err;
2649 do {
2650 err = nfs4_handle_exception(NFS_SERVER(dir),
2651 _nfs4_proc_remove(dir, name),
2652 &exception);
2653 } while (exception.retry);
2654 return err;
2655}
2656
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002657static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002659 struct nfs_server *server = NFS_SERVER(dir);
2660 struct nfs_removeargs *args = msg->rpc_argp;
2661 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662
Trond Myklebusta65318b2009-03-11 14:10:28 -04002663 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002664 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666}
2667
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002668static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002670 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2671
Trond Myklebust14516c32010-07-31 14:29:06 -04002672 if (!nfs4_sequence_done(task, &res->seq_res))
2673 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002674 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002675 return 0;
2676 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002677 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002678 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679}
2680
2681static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2682 struct inode *new_dir, struct qstr *new_name)
2683{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002684 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 struct nfs4_rename_arg arg = {
2686 .old_dir = NFS_FH(old_dir),
2687 .new_dir = NFS_FH(new_dir),
2688 .old_name = old_name,
2689 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002690 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002692 struct nfs4_rename_res res = {
2693 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002694 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 struct rpc_message msg = {
2696 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2697 .rpc_argp = &arg,
2698 .rpc_resp = &res,
2699 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002700 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701
Trond Myklebust011fff72010-04-16 16:22:49 -04002702 res.old_fattr = nfs_alloc_fattr();
2703 res.new_fattr = nfs_alloc_fattr();
2704 if (res.old_fattr == NULL || res.new_fattr == NULL)
2705 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706
Trond Myklebust011fff72010-04-16 16:22:49 -04002707 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 if (!status) {
2709 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002710 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002712 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002714out:
2715 nfs_free_fattr(res.new_fattr);
2716 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 return status;
2718}
2719
2720static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2721 struct inode *new_dir, struct qstr *new_name)
2722{
2723 struct nfs4_exception exception = { };
2724 int err;
2725 do {
2726 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2727 _nfs4_proc_rename(old_dir, old_name,
2728 new_dir, new_name),
2729 &exception);
2730 } while (exception.retry);
2731 return err;
2732}
2733
2734static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2735{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002736 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 struct nfs4_link_arg arg = {
2738 .fh = NFS_FH(inode),
2739 .dir_fh = NFS_FH(dir),
2740 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002741 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002743 struct nfs4_link_res res = {
2744 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002745 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 struct rpc_message msg = {
2747 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2748 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002749 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002751 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752
Trond Myklebust136f2622010-04-16 16:22:49 -04002753 res.fattr = nfs_alloc_fattr();
2754 res.dir_attr = nfs_alloc_fattr();
2755 if (res.fattr == NULL || res.dir_attr == NULL)
2756 goto out;
2757
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002758 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002759 if (!status) {
2760 update_changeattr(dir, &res.cinfo);
2761 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002762 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002763 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002764out:
2765 nfs_free_fattr(res.dir_attr);
2766 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 return status;
2768}
2769
2770static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2771{
2772 struct nfs4_exception exception = { };
2773 int err;
2774 do {
2775 err = nfs4_handle_exception(NFS_SERVER(inode),
2776 _nfs4_proc_link(inode, dir, name),
2777 &exception);
2778 } while (exception.retry);
2779 return err;
2780}
2781
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002782struct nfs4_createdata {
2783 struct rpc_message msg;
2784 struct nfs4_create_arg arg;
2785 struct nfs4_create_res res;
2786 struct nfs_fh fh;
2787 struct nfs_fattr fattr;
2788 struct nfs_fattr dir_fattr;
2789};
2790
2791static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2792 struct qstr *name, struct iattr *sattr, u32 ftype)
2793{
2794 struct nfs4_createdata *data;
2795
2796 data = kzalloc(sizeof(*data), GFP_KERNEL);
2797 if (data != NULL) {
2798 struct nfs_server *server = NFS_SERVER(dir);
2799
2800 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2801 data->msg.rpc_argp = &data->arg;
2802 data->msg.rpc_resp = &data->res;
2803 data->arg.dir_fh = NFS_FH(dir);
2804 data->arg.server = server;
2805 data->arg.name = name;
2806 data->arg.attrs = sattr;
2807 data->arg.ftype = ftype;
2808 data->arg.bitmask = server->attr_bitmask;
2809 data->res.server = server;
2810 data->res.fh = &data->fh;
2811 data->res.fattr = &data->fattr;
2812 data->res.dir_fattr = &data->dir_fattr;
2813 nfs_fattr_init(data->res.fattr);
2814 nfs_fattr_init(data->res.dir_fattr);
2815 }
2816 return data;
2817}
2818
2819static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2820{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002821 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2822 &data->arg, &data->res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002823 if (status == 0) {
2824 update_changeattr(dir, &data->res.dir_cinfo);
2825 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2826 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2827 }
2828 return status;
2829}
2830
2831static void nfs4_free_createdata(struct nfs4_createdata *data)
2832{
2833 kfree(data);
2834}
2835
Chuck Lever4f390c12006-08-22 20:06:22 -04002836static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002837 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002839 struct nfs4_createdata *data;
2840 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841
Chuck Lever94a6d752006-08-22 20:06:23 -04002842 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002843 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002844
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002845 status = -ENOMEM;
2846 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2847 if (data == NULL)
2848 goto out;
2849
2850 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2851 data->arg.u.symlink.pages = &page;
2852 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002854 status = nfs4_do_create(dir, dentry, data);
2855
2856 nfs4_free_createdata(data);
2857out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 return status;
2859}
2860
Chuck Lever4f390c12006-08-22 20:06:22 -04002861static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002862 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863{
2864 struct nfs4_exception exception = { };
2865 int err;
2866 do {
2867 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002868 _nfs4_proc_symlink(dir, dentry, page,
2869 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 &exception);
2871 } while (exception.retry);
2872 return err;
2873}
2874
2875static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2876 struct iattr *sattr)
2877{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002878 struct nfs4_createdata *data;
2879 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002881 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2882 if (data == NULL)
2883 goto out;
2884
2885 status = nfs4_do_create(dir, dentry, data);
2886
2887 nfs4_free_createdata(data);
2888out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 return status;
2890}
2891
2892static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2893 struct iattr *sattr)
2894{
2895 struct nfs4_exception exception = { };
2896 int err;
2897 do {
2898 err = nfs4_handle_exception(NFS_SERVER(dir),
2899 _nfs4_proc_mkdir(dir, dentry, sattr),
2900 &exception);
2901 } while (exception.retry);
2902 return err;
2903}
2904
2905static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2906 u64 cookie, struct page *page, unsigned int count, int plus)
2907{
2908 struct inode *dir = dentry->d_inode;
2909 struct nfs4_readdir_arg args = {
2910 .fh = NFS_FH(dir),
2911 .pages = &page,
2912 .pgbase = 0,
2913 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002914 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 };
2916 struct nfs4_readdir_res res;
2917 struct rpc_message msg = {
2918 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2919 .rpc_argp = &args,
2920 .rpc_resp = &res,
2921 .rpc_cred = cred,
2922 };
2923 int status;
2924
Harvey Harrison3110ff82008-05-02 13:42:44 -07002925 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002926 dentry->d_parent->d_name.name,
2927 dentry->d_name.name,
2928 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2930 res.pgbase = args.pgbase;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002931 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 if (status == 0)
2933 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustc4812992007-09-28 17:11:45 -04002934
2935 nfs_invalidate_atime(dir);
2936
Harvey Harrison3110ff82008-05-02 13:42:44 -07002937 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 return status;
2939}
2940
2941static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2942 u64 cookie, struct page *page, unsigned int count, int plus)
2943{
2944 struct nfs4_exception exception = { };
2945 int err;
2946 do {
2947 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2948 _nfs4_proc_readdir(dentry, cred, cookie,
2949 page, count, plus),
2950 &exception);
2951 } while (exception.retry);
2952 return err;
2953}
2954
2955static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2956 struct iattr *sattr, dev_t rdev)
2957{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002958 struct nfs4_createdata *data;
2959 int mode = sattr->ia_mode;
2960 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961
2962 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2963 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002964
2965 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2966 if (data == NULL)
2967 goto out;
2968
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002970 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002972 data->arg.ftype = NF4BLK;
2973 data->arg.u.device.specdata1 = MAJOR(rdev);
2974 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 }
2976 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002977 data->arg.ftype = NF4CHR;
2978 data->arg.u.device.specdata1 = MAJOR(rdev);
2979 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002982 status = nfs4_do_create(dir, dentry, data);
2983
2984 nfs4_free_createdata(data);
2985out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 return status;
2987}
2988
2989static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2990 struct iattr *sattr, dev_t rdev)
2991{
2992 struct nfs4_exception exception = { };
2993 int err;
2994 do {
2995 err = nfs4_handle_exception(NFS_SERVER(dir),
2996 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2997 &exception);
2998 } while (exception.retry);
2999 return err;
3000}
3001
3002static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3003 struct nfs_fsstat *fsstat)
3004{
3005 struct nfs4_statfs_arg args = {
3006 .fh = fhandle,
3007 .bitmask = server->attr_bitmask,
3008 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003009 struct nfs4_statfs_res res = {
3010 .fsstat = fsstat,
3011 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 struct rpc_message msg = {
3013 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3014 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003015 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 };
3017
Trond Myklebust0e574af2005-10-27 22:12:38 -04003018 nfs_fattr_init(fsstat->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003019 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020}
3021
3022static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3023{
3024 struct nfs4_exception exception = { };
3025 int err;
3026 do {
3027 err = nfs4_handle_exception(server,
3028 _nfs4_proc_statfs(server, fhandle, fsstat),
3029 &exception);
3030 } while (exception.retry);
3031 return err;
3032}
3033
3034static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3035 struct nfs_fsinfo *fsinfo)
3036{
3037 struct nfs4_fsinfo_arg args = {
3038 .fh = fhandle,
3039 .bitmask = server->attr_bitmask,
3040 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003041 struct nfs4_fsinfo_res res = {
3042 .fsinfo = fsinfo,
3043 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 struct rpc_message msg = {
3045 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3046 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003047 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 };
3049
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003050 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051}
3052
3053static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3054{
3055 struct nfs4_exception exception = { };
3056 int err;
3057
3058 do {
3059 err = nfs4_handle_exception(server,
3060 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3061 &exception);
3062 } while (exception.retry);
3063 return err;
3064}
3065
3066static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3067{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003068 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3070}
3071
3072static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3073 struct nfs_pathconf *pathconf)
3074{
3075 struct nfs4_pathconf_arg args = {
3076 .fh = fhandle,
3077 .bitmask = server->attr_bitmask,
3078 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003079 struct nfs4_pathconf_res res = {
3080 .pathconf = pathconf,
3081 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 struct rpc_message msg = {
3083 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3084 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003085 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 };
3087
3088 /* None of the pathconf attributes are mandatory to implement */
3089 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3090 memset(pathconf, 0, sizeof(*pathconf));
3091 return 0;
3092 }
3093
Trond Myklebust0e574af2005-10-27 22:12:38 -04003094 nfs_fattr_init(pathconf->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003095 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096}
3097
3098static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3099 struct nfs_pathconf *pathconf)
3100{
3101 struct nfs4_exception exception = { };
3102 int err;
3103
3104 do {
3105 err = nfs4_handle_exception(server,
3106 _nfs4_proc_pathconf(server, fhandle, pathconf),
3107 &exception);
3108 } while (exception.retry);
3109 return err;
3110}
3111
Trond Myklebustec06c092006-03-20 13:44:27 -05003112static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113{
Trond Myklebustec06c092006-03-20 13:44:27 -05003114 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003116 dprintk("--> %s\n", __func__);
3117
Trond Myklebust14516c32010-07-31 14:29:06 -04003118 if (!nfs4_sequence_done(task, &data->res.seq_res))
3119 return -EAGAIN;
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003120
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003121 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003122 nfs_restart_rpc(task, server->nfs_client);
Trond Myklebustec06c092006-03-20 13:44:27 -05003123 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003125
3126 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003128 renew_lease(server, data->timestamp);
3129 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130}
3131
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003132static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 data->timestamp = jiffies;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003135 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136}
3137
Trond Myklebust788e7a82006-03-20 13:44:27 -05003138static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 struct inode *inode = data->inode;
3141
Trond Myklebust14516c32010-07-31 14:29:06 -04003142 if (!nfs4_sequence_done(task, &data->res.seq_res))
3143 return -EAGAIN;
Andy Adamsondef6ed72009-04-01 09:22:26 -04003144
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003145 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003146 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003147 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003149 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003151 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003152 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003153 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154}
3155
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003156static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003158 struct nfs_server *server = NFS_SERVER(data->inode);
3159
Trond Myklebusta65318b2009-03-11 14:10:28 -04003160 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003161 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 data->timestamp = jiffies;
3163
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003164 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165}
3166
Trond Myklebust788e7a82006-03-20 13:44:27 -05003167static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 struct inode *inode = data->inode;
3170
Trond Myklebust14516c32010-07-31 14:29:06 -04003171 if (!nfs4_sequence_done(task, &data->res.seq_res))
3172 return -EAGAIN;
3173
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003174 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003175 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003176 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003178 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003179 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180}
3181
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003182static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003184 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185
Trond Myklebusta65318b2009-03-11 14:10:28 -04003186 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003187 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003188 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189}
3190
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003191struct nfs4_renewdata {
3192 struct nfs_client *client;
3193 unsigned long timestamp;
3194};
3195
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196/*
3197 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3198 * standalone procedure for queueing an asynchronous RENEW.
3199 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003200static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003201{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003202 struct nfs4_renewdata *data = calldata;
3203 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003204
Alexandros Batsakis0851de02010-02-05 03:45:06 -08003205 if (atomic_read(&clp->cl_count) > 1)
3206 nfs4_schedule_state_renewal(clp);
3207 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003208 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003209}
3210
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003211static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003213 struct nfs4_renewdata *data = calldata;
3214 struct nfs_client *clp = data->client;
3215 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216
3217 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003218 /* Unless we're shutting down, schedule state recovery! */
3219 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3220 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 return;
3222 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003223 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224}
3225
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003226static const struct rpc_call_ops nfs4_renew_ops = {
3227 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003228 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003229};
3230
David Howellsadfa6f92006-08-22 20:06:08 -04003231int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232{
3233 struct rpc_message msg = {
3234 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3235 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003236 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003238 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239
Alexandros Batsakis0851de02010-02-05 03:45:06 -08003240 if (!atomic_inc_not_zero(&clp->cl_count))
3241 return -EIO;
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003242 data = kmalloc(sizeof(*data), GFP_KERNEL);
3243 if (data == NULL)
3244 return -ENOMEM;
3245 data->client = clp;
3246 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003248 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249}
3250
David Howellsadfa6f92006-08-22 20:06:08 -04003251int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252{
3253 struct rpc_message msg = {
3254 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3255 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003256 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 };
3258 unsigned long now = jiffies;
3259 int status;
3260
3261 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3262 if (status < 0)
3263 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003264 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 return 0;
3266}
3267
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003268static inline int nfs4_server_supports_acls(struct nfs_server *server)
3269{
3270 return (server->caps & NFS_CAP_ACLS)
3271 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3272 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3273}
3274
3275/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3276 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3277 * the stack.
3278 */
3279#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3280
3281static void buf_to_pages(const void *buf, size_t buflen,
3282 struct page **pages, unsigned int *pgbase)
3283{
3284 const void *p = buf;
3285
3286 *pgbase = offset_in_page(buf);
3287 p -= *pgbase;
3288 while (p < buf + buflen) {
3289 *(pages++) = virt_to_page(p);
3290 p += PAGE_CACHE_SIZE;
3291 }
3292}
3293
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003294struct nfs4_cached_acl {
3295 int cached;
3296 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003297 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003298};
3299
3300static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003301{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003302 struct nfs_inode *nfsi = NFS_I(inode);
3303
3304 spin_lock(&inode->i_lock);
3305 kfree(nfsi->nfs4_acl);
3306 nfsi->nfs4_acl = acl;
3307 spin_unlock(&inode->i_lock);
3308}
3309
3310static void nfs4_zap_acl_attr(struct inode *inode)
3311{
3312 nfs4_set_cached_acl(inode, NULL);
3313}
3314
3315static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3316{
3317 struct nfs_inode *nfsi = NFS_I(inode);
3318 struct nfs4_cached_acl *acl;
3319 int ret = -ENOENT;
3320
3321 spin_lock(&inode->i_lock);
3322 acl = nfsi->nfs4_acl;
3323 if (acl == NULL)
3324 goto out;
3325 if (buf == NULL) /* user is just asking for length */
3326 goto out_len;
3327 if (acl->cached == 0)
3328 goto out;
3329 ret = -ERANGE; /* see getxattr(2) man page */
3330 if (acl->len > buflen)
3331 goto out;
3332 memcpy(buf, acl->data, acl->len);
3333out_len:
3334 ret = acl->len;
3335out:
3336 spin_unlock(&inode->i_lock);
3337 return ret;
3338}
3339
3340static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3341{
3342 struct nfs4_cached_acl *acl;
3343
3344 if (buf && acl_len <= PAGE_SIZE) {
3345 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3346 if (acl == NULL)
3347 goto out;
3348 acl->cached = 1;
3349 memcpy(acl->data, buf, acl_len);
3350 } else {
3351 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3352 if (acl == NULL)
3353 goto out;
3354 acl->cached = 0;
3355 }
3356 acl->len = acl_len;
3357out:
3358 nfs4_set_cached_acl(inode, acl);
3359}
3360
Trond Myklebust16b42892006-08-24 12:27:15 -04003361static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003362{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003363 struct page *pages[NFS4ACL_MAXPAGES];
3364 struct nfs_getaclargs args = {
3365 .fh = NFS_FH(inode),
3366 .acl_pages = pages,
3367 .acl_len = buflen,
3368 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003369 struct nfs_getaclres res = {
3370 .acl_len = buflen,
3371 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003372 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003373 struct rpc_message msg = {
3374 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3375 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003376 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003377 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003378 struct page *localpage = NULL;
3379 int ret;
3380
3381 if (buflen < PAGE_SIZE) {
3382 /* As long as we're doing a round trip to the server anyway,
3383 * let's be prepared for a page of acl data. */
3384 localpage = alloc_page(GFP_KERNEL);
3385 resp_buf = page_address(localpage);
3386 if (localpage == NULL)
3387 return -ENOMEM;
3388 args.acl_pages[0] = localpage;
3389 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003390 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003391 } else {
3392 resp_buf = buf;
3393 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3394 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003395 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003396 if (ret)
3397 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003398 if (res.acl_len > args.acl_len)
3399 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003400 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003401 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003402 if (buf) {
3403 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003404 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003405 goto out_free;
3406 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003407 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003408 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003409 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003410out_free:
3411 if (localpage)
3412 __free_page(localpage);
3413 return ret;
3414}
3415
Trond Myklebust16b42892006-08-24 12:27:15 -04003416static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3417{
3418 struct nfs4_exception exception = { };
3419 ssize_t ret;
3420 do {
3421 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3422 if (ret >= 0)
3423 break;
3424 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3425 } while (exception.retry);
3426 return ret;
3427}
3428
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003429static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3430{
3431 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003432 int ret;
3433
3434 if (!nfs4_server_supports_acls(server))
3435 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003436 ret = nfs_revalidate_inode(server, inode);
3437 if (ret < 0)
3438 return ret;
3439 ret = nfs4_read_cached_acl(inode, buf, buflen);
3440 if (ret != -ENOENT)
3441 return ret;
3442 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003443}
3444
Trond Myklebust16b42892006-08-24 12:27:15 -04003445static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003446{
3447 struct nfs_server *server = NFS_SERVER(inode);
3448 struct page *pages[NFS4ACL_MAXPAGES];
3449 struct nfs_setaclargs arg = {
3450 .fh = NFS_FH(inode),
3451 .acl_pages = pages,
3452 .acl_len = buflen,
3453 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003454 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003455 struct rpc_message msg = {
3456 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3457 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003458 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003459 };
3460 int ret;
3461
3462 if (!nfs4_server_supports_acls(server))
3463 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07003464 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003465 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003466 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003467 nfs_access_zap_cache(inode);
3468 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003469 return ret;
3470}
3471
Trond Myklebust16b42892006-08-24 12:27:15 -04003472static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3473{
3474 struct nfs4_exception exception = { };
3475 int err;
3476 do {
3477 err = nfs4_handle_exception(NFS_SERVER(inode),
3478 __nfs4_proc_set_acl(inode, buf, buflen),
3479 &exception);
3480 } while (exception.retry);
3481 return err;
3482}
3483
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003485nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003487 struct nfs_client *clp = server->nfs_client;
3488
3489 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 return 0;
3491 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003492 case -NFS4ERR_ADMIN_REVOKED:
3493 case -NFS4ERR_BAD_STATEID:
3494 case -NFS4ERR_OPENMODE:
3495 if (state == NULL)
3496 break;
3497 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003498 goto do_state_recovery;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003500 if (state == NULL)
3501 break;
3502 nfs4_state_mark_reclaim_reboot(clp, state);
3503 case -NFS4ERR_STALE_CLIENTID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 case -NFS4ERR_EXPIRED:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003505 goto do_state_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003506#if defined(CONFIG_NFS_V4_1)
3507 case -NFS4ERR_BADSESSION:
3508 case -NFS4ERR_BADSLOT:
3509 case -NFS4ERR_BAD_HIGH_SLOT:
3510 case -NFS4ERR_DEADSESSION:
3511 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3512 case -NFS4ERR_SEQ_FALSE_RETRY:
3513 case -NFS4ERR_SEQ_MISORDERED:
3514 dprintk("%s ERROR %d, Reset session\n", __func__,
3515 task->tk_status);
Andy Adamson0b9e2d42009-12-04 16:02:14 -05003516 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -04003517 task->tk_status = 0;
3518 return -EAGAIN;
3519#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003521 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003522 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003523 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3525 task->tk_status = 0;
3526 return -EAGAIN;
3527 case -NFS4ERR_OLD_STATEID:
3528 task->tk_status = 0;
3529 return -EAGAIN;
3530 }
3531 task->tk_status = nfs4_map_errors(task->tk_status);
3532 return 0;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003533do_state_recovery:
3534 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3535 nfs4_schedule_state_recovery(clp);
3536 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3537 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3538 task->tk_status = 0;
3539 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540}
3541
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003542int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3543 unsigned short port, struct rpc_cred *cred,
3544 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545{
3546 nfs4_verifier sc_verifier;
3547 struct nfs4_setclientid setclientid = {
3548 .sc_verifier = &sc_verifier,
3549 .sc_prog = program,
3550 };
3551 struct rpc_message msg = {
3552 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3553 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003554 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003555 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 };
Al Virobc4785c2006-10-19 23:28:51 -07003557 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 int loop = 0;
3559 int status;
3560
Al Virobc4785c2006-10-19 23:28:51 -07003561 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3563 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3564
3565 for(;;) {
3566 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003567 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003568 clp->cl_ipaddr,
3569 rpc_peeraddr2str(clp->cl_rpcclient,
3570 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003571 rpc_peeraddr2str(clp->cl_rpcclient,
3572 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003573 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 clp->cl_id_uniquifier);
3575 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003576 sizeof(setclientid.sc_netid),
3577 rpc_peeraddr2str(clp->cl_rpcclient,
3578 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003580 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 clp->cl_ipaddr, port >> 8, port & 255);
3582
3583 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3584 if (status != -NFS4ERR_CLID_INUSE)
3585 break;
3586 if (signalled())
3587 break;
3588 if (loop++ & 1)
3589 ssleep(clp->cl_lease_time + 1);
3590 else
3591 if (++clp->cl_id_uniquifier == 0)
3592 break;
3593 }
3594 return status;
3595}
3596
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003597static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3598 struct nfs4_setclientid_res *arg,
3599 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600{
3601 struct nfs_fsinfo fsinfo;
3602 struct rpc_message msg = {
3603 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003604 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003606 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 };
3608 unsigned long now;
3609 int status;
3610
3611 now = jiffies;
3612 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3613 if (status == 0) {
3614 spin_lock(&clp->cl_lock);
3615 clp->cl_lease_time = fsinfo.lease_time * HZ;
3616 clp->cl_last_renewal = now;
3617 spin_unlock(&clp->cl_lock);
3618 }
3619 return status;
3620}
3621
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003622int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3623 struct nfs4_setclientid_res *arg,
3624 struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003625{
Benny Halevy77e03672008-06-24 20:25:57 +03003626 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003627 int err;
3628 do {
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003629 err = _nfs4_proc_setclientid_confirm(clp, arg, cred);
Trond Myklebust51581f32006-03-20 13:44:47 -05003630 switch (err) {
3631 case 0:
3632 return err;
3633 case -NFS4ERR_RESOURCE:
3634 /* The IBM lawyers misread another document! */
3635 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05003636 case -EKEYEXPIRED:
Trond Myklebust51581f32006-03-20 13:44:47 -05003637 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3638 }
3639 } while (err == 0);
3640 return err;
3641}
3642
Trond Myklebustfe650402006-01-03 09:55:18 +01003643struct nfs4_delegreturndata {
3644 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003645 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003646 struct nfs_fh fh;
3647 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003648 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003649 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003650 int rpc_status;
3651};
3652
Trond Myklebustfe650402006-01-03 09:55:18 +01003653static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3654{
3655 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003656
Trond Myklebust14516c32010-07-31 14:29:06 -04003657 if (!nfs4_sequence_done(task, &data->res.seq_res))
3658 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003659
Ricardo Labiaga79708862009-12-07 09:23:21 -05003660 switch (task->tk_status) {
3661 case -NFS4ERR_STALE_STATEID:
3662 case -NFS4ERR_EXPIRED:
3663 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003664 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003665 break;
3666 default:
3667 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3668 -EAGAIN) {
3669 nfs_restart_rpc(task, data->res.server->nfs_client);
3670 return;
3671 }
3672 }
3673 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003674}
3675
3676static void nfs4_delegreturn_release(void *calldata)
3677{
Trond Myklebustfe650402006-01-03 09:55:18 +01003678 kfree(calldata);
3679}
3680
Andy Adamson938e1012009-04-01 09:22:28 -04003681#if defined(CONFIG_NFS_V4_1)
3682static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3683{
3684 struct nfs4_delegreturndata *d_data;
3685
3686 d_data = (struct nfs4_delegreturndata *)data;
3687
Trond Myklebust035168a2010-06-16 09:52:26 -04003688 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003689 &d_data->args.seq_args,
3690 &d_data->res.seq_res, 1, task))
3691 return;
3692 rpc_call_start(task);
3693}
3694#endif /* CONFIG_NFS_V4_1 */
3695
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003696static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003697#if defined(CONFIG_NFS_V4_1)
3698 .rpc_call_prepare = nfs4_delegreturn_prepare,
3699#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003700 .rpc_call_done = nfs4_delegreturn_done,
3701 .rpc_release = nfs4_delegreturn_release,
3702};
3703
Trond Myklebuste6f81072008-01-24 18:14:34 -05003704static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003705{
3706 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003707 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003708 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003709 struct rpc_message msg = {
3710 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3711 .rpc_cred = cred,
3712 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003713 struct rpc_task_setup task_setup_data = {
3714 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003715 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003716 .callback_ops = &nfs4_delegreturn_ops,
3717 .flags = RPC_TASK_ASYNC,
3718 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003719 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003720
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003721 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003722 if (data == NULL)
3723 return -ENOMEM;
3724 data->args.fhandle = &data->fh;
3725 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003726 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003727 nfs_copy_fh(&data->fh, NFS_FH(inode));
3728 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003729 data->res.fattr = &data->fattr;
3730 data->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003731 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003732 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003733 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003734 data->rpc_status = 0;
3735
Trond Myklebustc970aa82007-07-14 15:39:59 -04003736 task_setup_data.callback_data = data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003737 msg.rpc_argp = &data->args,
3738 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003739 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003740 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003741 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003742 if (!issync)
3743 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003744 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003745 if (status != 0)
3746 goto out;
3747 status = data->rpc_status;
3748 if (status != 0)
3749 goto out;
3750 nfs_refresh_inode(inode, &data->fattr);
3751out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003752 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003753 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754}
3755
Trond Myklebuste6f81072008-01-24 18:14:34 -05003756int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757{
3758 struct nfs_server *server = NFS_SERVER(inode);
3759 struct nfs4_exception exception = { };
3760 int err;
3761 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003762 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 switch (err) {
3764 case -NFS4ERR_STALE_STATEID:
3765 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 case 0:
3767 return 0;
3768 }
3769 err = nfs4_handle_exception(server, err, &exception);
3770 } while (exception.retry);
3771 return err;
3772}
3773
3774#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3775#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3776
3777/*
3778 * sleep, with exponential backoff, and retry the LOCK operation.
3779 */
3780static unsigned long
3781nfs4_set_lock_task_retry(unsigned long timeout)
3782{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003783 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 timeout <<= 1;
3785 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3786 return NFS4_LOCK_MAXTIMEOUT;
3787 return timeout;
3788}
3789
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3791{
3792 struct inode *inode = state->inode;
3793 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003794 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003795 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003797 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003799 struct nfs_lockt_res res = {
3800 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 };
3802 struct rpc_message msg = {
3803 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3804 .rpc_argp = &arg,
3805 .rpc_resp = &res,
3806 .rpc_cred = state->owner->so_cred,
3807 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 struct nfs4_lock_state *lsp;
3809 int status;
3810
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003811 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003812 status = nfs4_set_lock_state(state, request);
3813 if (status != 0)
3814 goto out;
3815 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003816 arg.lock_owner.id = lsp->ls_id.id;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003817 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003818 switch (status) {
3819 case 0:
3820 request->fl_type = F_UNLCK;
3821 break;
3822 case -NFS4ERR_DENIED:
3823 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003825 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003826out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 return status;
3828}
3829
3830static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3831{
3832 struct nfs4_exception exception = { };
3833 int err;
3834
3835 do {
3836 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3837 _nfs4_proc_getlk(state, cmd, request),
3838 &exception);
3839 } while (exception.retry);
3840 return err;
3841}
3842
3843static int do_vfs_lock(struct file *file, struct file_lock *fl)
3844{
3845 int res = 0;
3846 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3847 case FL_POSIX:
3848 res = posix_lock_file_wait(file, fl);
3849 break;
3850 case FL_FLOCK:
3851 res = flock_lock_file_wait(file, fl);
3852 break;
3853 default:
3854 BUG();
3855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 return res;
3857}
3858
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003859struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003860 struct nfs_locku_args arg;
3861 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003862 struct nfs4_lock_state *lsp;
3863 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003864 struct file_lock fl;
3865 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003866 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003867};
3868
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003869static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3870 struct nfs_open_context *ctx,
3871 struct nfs4_lock_state *lsp,
3872 struct nfs_seqid *seqid)
3873{
3874 struct nfs4_unlockdata *p;
3875 struct inode *inode = lsp->ls_state->inode;
3876
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003877 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003878 if (p == NULL)
3879 return NULL;
3880 p->arg.fh = NFS_FH(inode);
3881 p->arg.fl = &p->fl;
3882 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003883 p->res.seqid = seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003884 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003885 p->arg.stateid = &lsp->ls_stateid;
3886 p->lsp = lsp;
3887 atomic_inc(&lsp->ls_count);
3888 /* Ensure we don't close file until we're done freeing locks! */
3889 p->ctx = get_nfs_open_context(ctx);
3890 memcpy(&p->fl, fl, sizeof(p->fl));
3891 p->server = NFS_SERVER(inode);
3892 return p;
3893}
3894
Trond Myklebust06f814a2006-01-03 09:55:07 +01003895static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003896{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003897 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003898 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003899 nfs4_put_lock_state(calldata->lsp);
3900 put_nfs_open_context(calldata->ctx);
3901 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003902}
3903
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003904static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003905{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003906 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003907
Trond Myklebust14516c32010-07-31 14:29:06 -04003908 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3909 return;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003910 if (RPC_ASSASSINATED(task))
3911 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003912 switch (task->tk_status) {
3913 case 0:
3914 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003915 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003916 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003917 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003918 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003919 case -NFS4ERR_BAD_STATEID:
3920 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003921 case -NFS4ERR_STALE_STATEID:
3922 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003923 break;
3924 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003925 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebust0110ee12009-12-07 09:00:24 -05003926 nfs_restart_rpc(task,
Trond Myklebustd61e6122009-12-05 19:32:19 -05003927 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003928 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003929}
3930
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003931static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003932{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003933 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003935 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003936 return;
3937 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003938 /* Note: exit _without_ running nfs4_locku_done */
3939 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003940 return;
3941 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003942 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04003943 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04003944 &calldata->arg.seq_args,
3945 &calldata->res.seq_res, 1, task))
3946 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003947 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948}
3949
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003950static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003951 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003952 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003953 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003954};
3955
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003956static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3957 struct nfs_open_context *ctx,
3958 struct nfs4_lock_state *lsp,
3959 struct nfs_seqid *seqid)
3960{
3961 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003962 struct rpc_message msg = {
3963 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3964 .rpc_cred = ctx->cred,
3965 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003966 struct rpc_task_setup task_setup_data = {
3967 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003968 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003969 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003970 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003971 .flags = RPC_TASK_ASYNC,
3972 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003973
Frank Filz137d6ac2007-07-09 15:32:29 -07003974 /* Ensure this is an unlock - when canceling a lock, the
3975 * canceled lock is passed in, and it won't be an unlock.
3976 */
3977 fl->fl_type = F_UNLCK;
3978
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003979 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3980 if (data == NULL) {
3981 nfs_free_seqid(seqid);
3982 return ERR_PTR(-ENOMEM);
3983 }
3984
Trond Myklebust5138fde2007-07-14 15:40:01 -04003985 msg.rpc_argp = &data->arg,
3986 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003987 task_setup_data.callback_data = data;
3988 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003989}
3990
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3992{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003993 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003994 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003995 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003996 struct rpc_task *task;
3997 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003998 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999
Trond Myklebust9b073572006-06-29 16:38:34 -04004000 status = nfs4_set_lock_state(state, request);
4001 /* Unlock _before_ we do the RPC call */
4002 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004003 down_read(&nfsi->rwsem);
4004 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4005 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004006 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004007 }
4008 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004009 if (status != 0)
4010 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004011 /* Is this a delegated lock? */
4012 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004013 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004014 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004015 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004016 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004017 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004018 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004019 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004020 status = PTR_ERR(task);
4021 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004022 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004023 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004024 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004025out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004026 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004027 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028}
4029
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004030struct nfs4_lockdata {
4031 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004032 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004033 struct nfs4_lock_state *lsp;
4034 struct nfs_open_context *ctx;
4035 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004036 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004037 int rpc_status;
4038 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004039 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004040};
4041
4042static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004043 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4044 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004045{
4046 struct nfs4_lockdata *p;
4047 struct inode *inode = lsp->ls_state->inode;
4048 struct nfs_server *server = NFS_SERVER(inode);
4049
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004050 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004051 if (p == NULL)
4052 return NULL;
4053
4054 p->arg.fh = NFS_FH(inode);
4055 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004056 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004057 if (p->arg.open_seqid == NULL)
4058 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004059 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004060 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004061 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004062 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004063 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004064 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004065 p->res.lock_seqid = p->arg.lock_seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04004066 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004067 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004068 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004069 atomic_inc(&lsp->ls_count);
4070 p->ctx = get_nfs_open_context(ctx);
4071 memcpy(&p->fl, fl, sizeof(p->fl));
4072 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004073out_free_seqid:
4074 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004075out_free:
4076 kfree(p);
4077 return NULL;
4078}
4079
4080static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4081{
4082 struct nfs4_lockdata *data = calldata;
4083 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084
Harvey Harrison3110ff82008-05-02 13:42:44 -07004085 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004086 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4087 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004088 /* Do we need to do an open_to_lock_owner? */
4089 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004090 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4091 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004092 data->arg.open_stateid = &state->stateid;
4093 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004094 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004095 } else
4096 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004097 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004098 if (nfs4_setup_sequence(data->server,
4099 &data->arg.seq_args,
Andy Adamson66179ef2009-04-01 09:22:22 -04004100 &data->res.seq_res, 1, task))
4101 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004102 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004103 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004104}
4105
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004106static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4107{
4108 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4109 nfs4_lock_prepare(task, calldata);
4110}
4111
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004112static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4113{
4114 struct nfs4_lockdata *data = calldata;
4115
Harvey Harrison3110ff82008-05-02 13:42:44 -07004116 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004117
Trond Myklebust14516c32010-07-31 14:29:06 -04004118 if (!nfs4_sequence_done(task, &data->res.seq_res))
4119 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004120
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004121 data->rpc_status = task->tk_status;
4122 if (RPC_ASSASSINATED(task))
4123 goto out;
4124 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004125 if (data->rpc_status == 0)
4126 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4127 else
4128 goto out;
4129 }
4130 if (data->rpc_status == 0) {
4131 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4132 sizeof(data->lsp->ls_stateid.data));
4133 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04004134 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004135 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004136out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004137 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004138}
4139
4140static void nfs4_lock_release(void *calldata)
4141{
4142 struct nfs4_lockdata *data = calldata;
4143
Harvey Harrison3110ff82008-05-02 13:42:44 -07004144 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004145 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004146 if (data->cancelled != 0) {
4147 struct rpc_task *task;
4148 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4149 data->arg.lock_seqid);
4150 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004151 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004152 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004153 } else
4154 nfs_free_seqid(data->arg.lock_seqid);
4155 nfs4_put_lock_state(data->lsp);
4156 put_nfs_open_context(data->ctx);
4157 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004158 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004159}
4160
4161static const struct rpc_call_ops nfs4_lock_ops = {
4162 .rpc_call_prepare = nfs4_lock_prepare,
4163 .rpc_call_done = nfs4_lock_done,
4164 .rpc_release = nfs4_lock_release,
4165};
4166
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004167static const struct rpc_call_ops nfs4_recover_lock_ops = {
4168 .rpc_call_prepare = nfs4_recover_lock_prepare,
4169 .rpc_call_done = nfs4_lock_done,
4170 .rpc_release = nfs4_lock_release,
4171};
4172
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004173static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4174{
4175 struct nfs_client *clp = server->nfs_client;
4176 struct nfs4_state *state = lsp->ls_state;
4177
4178 switch (error) {
4179 case -NFS4ERR_ADMIN_REVOKED:
4180 case -NFS4ERR_BAD_STATEID:
4181 case -NFS4ERR_EXPIRED:
4182 if (new_lock_owner != 0 ||
4183 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4184 nfs4_state_mark_reclaim_nograce(clp, state);
4185 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004186 break;
4187 case -NFS4ERR_STALE_STATEID:
4188 if (new_lock_owner != 0 ||
4189 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4190 nfs4_state_mark_reclaim_reboot(clp, state);
4191 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004192 };
4193}
4194
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004195static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004196{
4197 struct nfs4_lockdata *data;
4198 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004199 struct rpc_message msg = {
4200 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4201 .rpc_cred = state->owner->so_cred,
4202 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004203 struct rpc_task_setup task_setup_data = {
4204 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004205 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004206 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004207 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004208 .flags = RPC_TASK_ASYNC,
4209 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004210 int ret;
4211
Harvey Harrison3110ff82008-05-02 13:42:44 -07004212 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004213 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004214 fl->fl_u.nfs4_fl.owner,
4215 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004216 if (data == NULL)
4217 return -ENOMEM;
4218 if (IS_SETLKW(cmd))
4219 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004220 if (recovery_type > NFS_LOCK_NEW) {
4221 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004222 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004223 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4224 }
Trond Myklebust5138fde2007-07-14 15:40:01 -04004225 msg.rpc_argp = &data->arg,
4226 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004227 task_setup_data.callback_data = data;
4228 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004229 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004230 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004231 ret = nfs4_wait_for_completion_rpc_task(task);
4232 if (ret == 0) {
4233 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004234 if (ret)
4235 nfs4_handle_setlk_error(data->server, data->lsp,
4236 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004237 } else
4238 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004239 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004240 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004241 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242}
4243
4244static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4245{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004246 struct nfs_server *server = NFS_SERVER(state->inode);
4247 struct nfs4_exception exception = { };
4248 int err;
4249
4250 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004251 /* Cache the lock if possible... */
4252 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4253 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004254 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Jeff Layton2c643482010-01-07 09:42:03 -05004255 if (err != -NFS4ERR_DELAY && err != -EKEYEXPIRED)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004256 break;
4257 nfs4_handle_exception(server, err, &exception);
4258 } while (exception.retry);
4259 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260}
4261
4262static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4263{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004264 struct nfs_server *server = NFS_SERVER(state->inode);
4265 struct nfs4_exception exception = { };
4266 int err;
4267
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004268 err = nfs4_set_lock_state(state, request);
4269 if (err != 0)
4270 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004271 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004272 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4273 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004274 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004275 switch (err) {
4276 default:
4277 goto out;
4278 case -NFS4ERR_GRACE:
4279 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05004280 case -EKEYEXPIRED:
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004281 nfs4_handle_exception(server, err, &exception);
4282 err = 0;
4283 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004284 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004285out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004286 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287}
4288
4289static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4290{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004291 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004292 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004293 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004295 if ((fl_flags & FL_POSIX) &&
4296 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4297 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004298 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004299 status = nfs4_set_lock_state(state, request);
4300 if (status != 0)
4301 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004302 request->fl_flags |= FL_ACCESS;
4303 status = do_vfs_lock(request->fl_file, request);
4304 if (status < 0)
4305 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004306 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004307 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004308 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004309 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004310 request->fl_flags = fl_flags & ~FL_SLEEP;
4311 status = do_vfs_lock(request->fl_file, request);
4312 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004313 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004314 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004315 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004316 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004317 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004318 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004319 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004320 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004321out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004322 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004323out:
4324 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 return status;
4326}
4327
4328static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4329{
4330 struct nfs4_exception exception = { };
4331 int err;
4332
4333 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004334 err = _nfs4_proc_setlk(state, cmd, request);
4335 if (err == -NFS4ERR_DENIED)
4336 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004338 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 } while (exception.retry);
4340 return err;
4341}
4342
4343static int
4344nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4345{
4346 struct nfs_open_context *ctx;
4347 struct nfs4_state *state;
4348 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4349 int status;
4350
4351 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004352 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 state = ctx->state;
4354
4355 if (request->fl_start < 0 || request->fl_end < 0)
4356 return -EINVAL;
4357
Trond Myklebustd9531262009-07-21 19:22:38 -04004358 if (IS_GETLK(cmd)) {
4359 if (state != NULL)
4360 return nfs4_proc_getlk(state, F_GETLK, request);
4361 return 0;
4362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363
4364 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4365 return -EINVAL;
4366
Trond Myklebustd9531262009-07-21 19:22:38 -04004367 if (request->fl_type == F_UNLCK) {
4368 if (state != NULL)
4369 return nfs4_proc_unlck(state, cmd, request);
4370 return 0;
4371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372
Trond Myklebustd9531262009-07-21 19:22:38 -04004373 if (state == NULL)
4374 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 do {
4376 status = nfs4_proc_setlk(state, cmd, request);
4377 if ((status != -EAGAIN) || IS_SETLK(cmd))
4378 break;
4379 timeout = nfs4_set_lock_task_retry(timeout);
4380 status = -ERESTARTSYS;
4381 if (signalled())
4382 break;
4383 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 return status;
4385}
4386
Trond Myklebust888e6942005-11-04 15:38:11 -05004387int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4388{
4389 struct nfs_server *server = NFS_SERVER(state->inode);
4390 struct nfs4_exception exception = { };
4391 int err;
4392
4393 err = nfs4_set_lock_state(state, fl);
4394 if (err != 0)
4395 goto out;
4396 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004397 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004398 switch (err) {
4399 default:
4400 printk(KERN_ERR "%s: unhandled error %d.\n",
4401 __func__, err);
4402 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004403 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004404 goto out;
4405 case -NFS4ERR_EXPIRED:
4406 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004407 case -NFS4ERR_STALE_STATEID:
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004408 case -NFS4ERR_BADSESSION:
4409 case -NFS4ERR_BADSLOT:
4410 case -NFS4ERR_BAD_HIGH_SLOT:
4411 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4412 case -NFS4ERR_DEADSESSION:
Trond Myklebustd5122202009-06-17 13:22:58 -07004413 nfs4_schedule_state_recovery(server->nfs_client);
4414 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004415 case -ERESTARTSYS:
4416 /*
4417 * The show must go on: exit, but mark the
4418 * stateid as needing recovery.
4419 */
4420 case -NFS4ERR_ADMIN_REVOKED:
4421 case -NFS4ERR_BAD_STATEID:
4422 case -NFS4ERR_OPENMODE:
4423 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4424 err = 0;
4425 goto out;
4426 case -ENOMEM:
4427 case -NFS4ERR_DENIED:
4428 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4429 err = 0;
4430 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004431 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05004432 case -EKEYEXPIRED:
Trond Myklebustd5122202009-06-17 13:22:58 -07004433 break;
4434 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004435 err = nfs4_handle_exception(server, err, &exception);
4436 } while (exception.retry);
4437out:
4438 return err;
4439}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004440
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004441static void nfs4_release_lockowner_release(void *calldata)
4442{
4443 kfree(calldata);
4444}
4445
4446const struct rpc_call_ops nfs4_release_lockowner_ops = {
4447 .rpc_release = nfs4_release_lockowner_release,
4448};
4449
4450void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4451{
4452 struct nfs_server *server = lsp->ls_state->owner->so_server;
4453 struct nfs_release_lockowner_args *args;
4454 struct rpc_message msg = {
4455 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4456 };
4457
4458 if (server->nfs_client->cl_mvops->minor_version != 0)
4459 return;
4460 args = kmalloc(sizeof(*args), GFP_NOFS);
4461 if (!args)
4462 return;
4463 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4464 args->lock_owner.id = lsp->ls_id.id;
4465 msg.rpc_argp = args;
4466 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4467}
4468
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004469#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4470
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004471int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4472 size_t buflen, int flags)
4473{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004474 struct inode *inode = dentry->d_inode;
4475
4476 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4477 return -EOPNOTSUPP;
4478
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004479 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004480}
4481
4482/* The getxattr man page suggests returning -ENODATA for unknown attributes,
4483 * and that's what we'll do for e.g. user attributes that haven't been set.
4484 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4485 * attributes in kernel-managed attribute namespaces. */
4486ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4487 size_t buflen)
4488{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004489 struct inode *inode = dentry->d_inode;
4490
4491 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4492 return -EOPNOTSUPP;
4493
4494 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004495}
4496
4497ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4498{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004499 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004500
J. Bruce Fields096455a2006-03-20 23:23:42 -05004501 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4502 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004503 if (buf && buflen < len)
4504 return -ERANGE;
4505 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004506 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4507 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004508}
4509
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004510static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4511{
4512 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4513 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4514 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4515 return;
4516
4517 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4518 NFS_ATTR_FATTR_NLINK;
4519 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4520 fattr->nlink = 2;
4521}
4522
Trond Myklebust56659e92007-07-17 21:52:39 -04004523int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004524 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004525{
4526 struct nfs_server *server = NFS_SERVER(dir);
4527 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004528 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4529 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004530 };
4531 struct nfs4_fs_locations_arg args = {
4532 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004533 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004534 .page = page,
4535 .bitmask = bitmask,
4536 };
Benny Halevy22958462009-04-01 09:22:02 -04004537 struct nfs4_fs_locations_res res = {
4538 .fs_locations = fs_locations,
4539 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004540 struct rpc_message msg = {
4541 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4542 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004543 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004544 };
4545 int status;
4546
Harvey Harrison3110ff82008-05-02 13:42:44 -07004547 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004548 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004549 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004550 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04004551 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004552 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004553 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004554 return status;
4555}
4556
Andy Adamson557134a2009-04-01 09:21:53 -04004557#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004558/*
4559 * nfs4_proc_exchange_id()
4560 *
4561 * Since the clientid has expired, all compounds using sessions
4562 * associated with the stale clientid will be returning
4563 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4564 * be in some phase of session reset.
4565 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004566int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004567{
4568 nfs4_verifier verifier;
4569 struct nfs41_exchange_id_args args = {
4570 .client = clp,
4571 .flags = clp->cl_exchange_flags,
4572 };
4573 struct nfs41_exchange_id_res res = {
4574 .client = clp,
4575 };
4576 int status;
4577 struct rpc_message msg = {
4578 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4579 .rpc_argp = &args,
4580 .rpc_resp = &res,
4581 .rpc_cred = cred,
4582 };
4583 __be32 *p;
4584
4585 dprintk("--> %s\n", __func__);
4586 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004587
Alexandros Batsakis7b183d02009-12-05 13:33:25 -05004588 /* Remove server-only flags */
4589 args.flags &= ~EXCHGID4_FLAG_CONFIRMED_R;
4590
Benny Halevy99fe60d2009-04-01 09:22:29 -04004591 p = (u32 *)verifier.data;
4592 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4593 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4594 args.verifier = &verifier;
4595
4596 while (1) {
4597 args.id_len = scnprintf(args.id, sizeof(args.id),
4598 "%s/%s %u",
4599 clp->cl_ipaddr,
4600 rpc_peeraddr2str(clp->cl_rpcclient,
4601 RPC_DISPLAY_ADDR),
4602 clp->cl_id_uniquifier);
4603
4604 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4605
Andy Adamson180b62a2010-03-02 13:19:36 -05004606 if (status != -NFS4ERR_CLID_INUSE)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004607 break;
4608
4609 if (signalled())
4610 break;
4611
4612 if (++clp->cl_id_uniquifier == 0)
4613 break;
4614 }
4615
4616 dprintk("<-- %s status= %d\n", __func__, status);
4617 return status;
4618}
4619
Andy Adamson2050f0c2009-04-01 09:22:30 -04004620struct nfs4_get_lease_time_data {
4621 struct nfs4_get_lease_time_args *args;
4622 struct nfs4_get_lease_time_res *res;
4623 struct nfs_client *clp;
4624};
4625
4626static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4627 void *calldata)
4628{
4629 int ret;
4630 struct nfs4_get_lease_time_data *data =
4631 (struct nfs4_get_lease_time_data *)calldata;
4632
4633 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004634 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004635 /* just setup sequence, do not trigger session recovery
4636 since we're invoked within one */
4637 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004638 &data->args->la_seq_args,
4639 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004640
4641 BUG_ON(ret == -EAGAIN);
4642 rpc_call_start(task);
4643 dprintk("<-- %s\n", __func__);
4644}
4645
4646/*
4647 * Called from nfs4_state_manager thread for session setup, so don't recover
4648 * from sequence operation or clientid errors.
4649 */
4650static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4651{
4652 struct nfs4_get_lease_time_data *data =
4653 (struct nfs4_get_lease_time_data *)calldata;
4654
4655 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04004656 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4657 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04004658 switch (task->tk_status) {
4659 case -NFS4ERR_DELAY:
4660 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05004661 case -EKEYEXPIRED:
Andy Adamson2050f0c2009-04-01 09:22:30 -04004662 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4663 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4664 task->tk_status = 0;
Trond Myklebust0110ee12009-12-07 09:00:24 -05004665 nfs_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004666 return;
4667 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004668 dprintk("<-- %s\n", __func__);
4669}
4670
4671struct rpc_call_ops nfs4_get_lease_time_ops = {
4672 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4673 .rpc_call_done = nfs4_get_lease_time_done,
4674};
4675
4676int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4677{
4678 struct rpc_task *task;
4679 struct nfs4_get_lease_time_args args;
4680 struct nfs4_get_lease_time_res res = {
4681 .lr_fsinfo = fsinfo,
4682 };
4683 struct nfs4_get_lease_time_data data = {
4684 .args = &args,
4685 .res = &res,
4686 .clp = clp,
4687 };
4688 struct rpc_message msg = {
4689 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4690 .rpc_argp = &args,
4691 .rpc_resp = &res,
4692 };
4693 struct rpc_task_setup task_setup = {
4694 .rpc_client = clp->cl_rpcclient,
4695 .rpc_message = &msg,
4696 .callback_ops = &nfs4_get_lease_time_ops,
4697 .callback_data = &data
4698 };
4699 int status;
4700
4701 res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
4702 dprintk("--> %s\n", __func__);
4703 task = rpc_run_task(&task_setup);
4704
4705 if (IS_ERR(task))
4706 status = PTR_ERR(task);
4707 else {
4708 status = task->tk_status;
4709 rpc_put_task(task);
4710 }
4711 dprintk("<-- %s return %d\n", __func__, status);
4712
4713 return status;
4714}
4715
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004716/*
4717 * Reset a slot table
4718 */
Andy Adamson104aeba2010-01-14 17:45:10 -05004719static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4720 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004721{
Andy Adamson104aeba2010-01-14 17:45:10 -05004722 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004723 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004724 int ret = 0;
4725
Andy Adamson104aeba2010-01-14 17:45:10 -05004726 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4727 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004728
Andy Adamson104aeba2010-01-14 17:45:10 -05004729 /* Does the newly negotiated max_reqs match the existing slot table? */
4730 if (max_reqs != tbl->max_slots) {
4731 ret = -ENOMEM;
4732 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004733 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05004734 if (!new)
4735 goto out;
4736 ret = 0;
4737 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004738 }
4739 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05004740 if (new) {
4741 tbl->slots = new;
4742 tbl->max_slots = max_reqs;
4743 }
4744 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004745 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004746 spin_unlock(&tbl->slot_tbl_lock);
4747 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4748 tbl, tbl->slots, tbl->max_slots);
4749out:
4750 dprintk("<-- %s: return %d\n", __func__, ret);
4751 return ret;
4752}
4753
Andy Adamsonfc931582009-04-01 09:22:31 -04004754/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004755 * Reset the forechannel and backchannel slot tables
4756 */
4757static int nfs4_reset_slot_tables(struct nfs4_session *session)
4758{
4759 int status;
4760
4761 status = nfs4_reset_slot_table(&session->fc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004762 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004763 if (status)
4764 return status;
4765
4766 status = nfs4_reset_slot_table(&session->bc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004767 session->bc_attrs.max_reqs, 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004768 return status;
4769}
4770
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004771/* Destroy the slot table */
4772static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4773{
4774 if (session->fc_slot_table.slots != NULL) {
4775 kfree(session->fc_slot_table.slots);
4776 session->fc_slot_table.slots = NULL;
4777 }
4778 if (session->bc_slot_table.slots != NULL) {
4779 kfree(session->bc_slot_table.slots);
4780 session->bc_slot_table.slots = NULL;
4781 }
4782 return;
4783}
4784
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004785/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004786 * Initialize slot table
4787 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004788static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4789 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04004790{
Andy Adamsonfc931582009-04-01 09:22:31 -04004791 struct nfs4_slot *slot;
4792 int ret = -ENOMEM;
4793
4794 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4795
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004796 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04004797
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004798 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04004799 if (!slot)
4800 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04004801 ret = 0;
4802
4803 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04004804 tbl->max_slots = max_slots;
4805 tbl->slots = slot;
4806 tbl->highest_used_slotid = -1; /* no slot is currently used */
4807 spin_unlock(&tbl->slot_tbl_lock);
4808 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4809 tbl, tbl->slots, tbl->max_slots);
4810out:
4811 dprintk("<-- %s: return %d\n", __func__, ret);
4812 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04004813}
4814
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004815/*
4816 * Initialize the forechannel and backchannel tables
4817 */
4818static int nfs4_init_slot_tables(struct nfs4_session *session)
4819{
Trond Myklebustf26468f2009-12-05 19:32:11 -05004820 struct nfs4_slot_table *tbl;
4821 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004822
Trond Myklebustf26468f2009-12-05 19:32:11 -05004823 tbl = &session->fc_slot_table;
4824 if (tbl->slots == NULL) {
4825 status = nfs4_init_slot_table(tbl,
4826 session->fc_attrs.max_reqs, 1);
4827 if (status)
4828 return status;
4829 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004830
Trond Myklebustf26468f2009-12-05 19:32:11 -05004831 tbl = &session->bc_slot_table;
4832 if (tbl->slots == NULL) {
4833 status = nfs4_init_slot_table(tbl,
4834 session->bc_attrs.max_reqs, 0);
4835 if (status)
4836 nfs4_destroy_slot_tables(session);
4837 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004838
4839 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04004840}
4841
4842struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4843{
4844 struct nfs4_session *session;
4845 struct nfs4_slot_table *tbl;
4846
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004847 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04004848 if (!session)
4849 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04004850
Andy Adamsonea028ac2009-12-04 15:55:38 -05004851 init_completion(&session->complete);
Andy Adamson76db6d92009-04-01 09:22:38 -04004852
Andy Adamson557134a2009-04-01 09:21:53 -04004853 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004854 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04004855 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004856 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004857
4858 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004859 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004860 spin_lock_init(&tbl->slot_tbl_lock);
4861 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4862
Trond Myklebust1055d762010-06-16 09:52:27 -04004863 session->session_state = 1<<NFS4_SESSION_INITING;
4864
Andy Adamson557134a2009-04-01 09:21:53 -04004865 session->clp = clp;
4866 return session;
4867}
4868
4869void nfs4_destroy_session(struct nfs4_session *session)
4870{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04004871 nfs4_proc_destroy_session(session);
4872 dprintk("%s Destroy backchannel for xprt %p\n",
4873 __func__, session->clp->cl_rpcclient->cl_xprt);
4874 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4875 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004876 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04004877 kfree(session);
4878}
4879
Andy Adamsonfc931582009-04-01 09:22:31 -04004880/*
4881 * Initialize the values to be used by the client in CREATE_SESSION
4882 * If nfs4_init_session set the fore channel request and response sizes,
4883 * use them.
4884 *
4885 * Set the back channel max_resp_sz_cached to zero to force the client to
4886 * always set csa_cachethis to FALSE because the current implementation
4887 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4888 */
4889static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4890{
4891 struct nfs4_session *session = args->client->cl_session;
4892 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4893 mxresp_sz = session->fc_attrs.max_resp_sz;
4894
4895 if (mxrqst_sz == 0)
4896 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4897 if (mxresp_sz == 0)
4898 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4899 /* Fore channel attributes */
4900 args->fc_attrs.headerpadsz = 0;
4901 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4902 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04004903 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4904 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4905
4906 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05004907 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04004908 __func__,
4909 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05004910 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04004911
4912 /* Back channel attributes */
4913 args->bc_attrs.headerpadsz = 0;
4914 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4915 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4916 args->bc_attrs.max_resp_sz_cached = 0;
4917 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4918 args->bc_attrs.max_reqs = 1;
4919
4920 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4921 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4922 __func__,
4923 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4924 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4925 args->bc_attrs.max_reqs);
4926}
4927
Andy Adamson8d353012009-04-01 09:22:32 -04004928static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
4929{
4930 if (rcvd <= sent)
4931 return 0;
4932 printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
4933 "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
4934 return -EINVAL;
4935}
4936
4937#define _verify_fore_channel_attr(_name_) \
4938 _verify_channel_attr("fore", #_name_, \
4939 args->fc_attrs._name_, \
4940 session->fc_attrs._name_)
4941
4942#define _verify_back_channel_attr(_name_) \
4943 _verify_channel_attr("back", #_name_, \
4944 args->bc_attrs._name_, \
4945 session->bc_attrs._name_)
4946
4947/*
4948 * The server is not allowed to increase the fore channel header pad size,
4949 * maximum response size, or maximum number of operations.
4950 *
4951 * The back channel attributes are only negotiatied down: We send what the
4952 * (back channel) server insists upon.
4953 */
4954static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4955 struct nfs4_session *session)
4956{
4957 int ret = 0;
4958
4959 ret |= _verify_fore_channel_attr(headerpadsz);
4960 ret |= _verify_fore_channel_attr(max_resp_sz);
4961 ret |= _verify_fore_channel_attr(max_ops);
4962
4963 ret |= _verify_back_channel_attr(headerpadsz);
4964 ret |= _verify_back_channel_attr(max_rqst_sz);
4965 ret |= _verify_back_channel_attr(max_resp_sz);
4966 ret |= _verify_back_channel_attr(max_resp_sz_cached);
4967 ret |= _verify_back_channel_attr(max_ops);
4968 ret |= _verify_back_channel_attr(max_reqs);
4969
4970 return ret;
4971}
4972
Andy Adamsonfc931582009-04-01 09:22:31 -04004973static int _nfs4_proc_create_session(struct nfs_client *clp)
4974{
4975 struct nfs4_session *session = clp->cl_session;
4976 struct nfs41_create_session_args args = {
4977 .client = clp,
4978 .cb_program = NFS4_CALLBACK,
4979 };
4980 struct nfs41_create_session_res res = {
4981 .client = clp,
4982 };
4983 struct rpc_message msg = {
4984 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4985 .rpc_argp = &args,
4986 .rpc_resp = &res,
4987 };
4988 int status;
4989
4990 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04004991 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04004992
4993 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4994
Andy Adamson8d353012009-04-01 09:22:32 -04004995 if (!status)
4996 /* Verify the session's negotiated channel_attrs values */
4997 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04004998 if (!status) {
4999 /* Increment the clientid slot sequence id */
5000 clp->cl_seqid++;
5001 }
5002
5003 return status;
5004}
5005
5006/*
5007 * Issues a CREATE_SESSION operation to the server.
5008 * It is the responsibility of the caller to verify the session is
5009 * expired before calling this routine.
5010 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005011int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005012{
5013 int status;
5014 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005015 struct nfs4_session *session = clp->cl_session;
5016
5017 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5018
5019 status = _nfs4_proc_create_session(clp);
5020 if (status)
5021 goto out;
5022
Trond Myklebustf26468f2009-12-05 19:32:11 -05005023 /* Init and reset the fore channel */
5024 status = nfs4_init_slot_tables(session);
5025 dprintk("slot table initialization returned %d\n", status);
5026 if (status)
5027 goto out;
5028 status = nfs4_reset_slot_tables(session);
5029 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005030 if (status)
5031 goto out;
5032
5033 ptr = (unsigned *)&session->sess_id.data[0];
5034 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5035 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005036out:
5037 dprintk("<-- %s\n", __func__);
5038 return status;
5039}
5040
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005041/*
5042 * Issue the over-the-wire RPC DESTROY_SESSION.
5043 * The caller must serialize access to this routine.
5044 */
5045int nfs4_proc_destroy_session(struct nfs4_session *session)
5046{
5047 int status = 0;
5048 struct rpc_message msg;
5049
5050 dprintk("--> nfs4_proc_destroy_session\n");
5051
5052 /* session is still being setup */
5053 if (session->clp->cl_cons_state != NFS_CS_READY)
5054 return status;
5055
5056 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5057 msg.rpc_argp = session;
5058 msg.rpc_resp = NULL;
5059 msg.rpc_cred = NULL;
5060 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5061
5062 if (status)
5063 printk(KERN_WARNING
5064 "Got error %d from the server on DESTROY_SESSION. "
5065 "Session has been destroyed regardless...\n", status);
5066
5067 dprintk("<-- nfs4_proc_destroy_session\n");
5068 return status;
5069}
5070
Trond Myklebustfccba802009-07-21 16:48:07 -04005071int nfs4_init_session(struct nfs_server *server)
5072{
5073 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005074 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005075 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005076 int ret;
5077
5078 if (!nfs4_has_session(clp))
5079 return 0;
5080
Trond Myklebust1055d762010-06-16 09:52:27 -04005081 session = clp->cl_session;
5082 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5083 return 0;
5084
Andy Adamson68bf05e2009-12-15 12:55:02 -05005085 rsize = server->rsize;
5086 if (rsize == 0)
5087 rsize = NFS_MAX_FILE_IO_SIZE;
5088 wsize = server->wsize;
5089 if (wsize == 0)
5090 wsize = NFS_MAX_FILE_IO_SIZE;
5091
Andy Adamson68bf05e2009-12-15 12:55:02 -05005092 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5093 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005094
Trond Myklebustfccba802009-07-21 16:48:07 -04005095 ret = nfs4_recover_expired_lease(server);
5096 if (!ret)
5097 ret = nfs4_check_client_ready(clp);
5098 return ret;
5099}
5100
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005101/*
5102 * Renew the cl_session lease.
5103 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005104struct nfs4_sequence_data {
5105 struct nfs_client *clp;
5106 struct nfs4_sequence_args args;
5107 struct nfs4_sequence_res res;
5108};
5109
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005110static void nfs41_sequence_release(void *data)
5111{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005112 struct nfs4_sequence_data *calldata = data;
5113 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005114
Alexandros Batsakis71358402010-02-05 03:45:05 -08005115 if (atomic_read(&clp->cl_count) > 1)
5116 nfs4_schedule_state_renewal(clp);
5117 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005118 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005119}
5120
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005121static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5122{
5123 switch(task->tk_status) {
5124 case -NFS4ERR_DELAY:
5125 case -EKEYEXPIRED:
5126 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5127 return -EAGAIN;
5128 default:
5129 nfs4_schedule_state_recovery(clp);
5130 }
5131 return 0;
5132}
5133
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005134static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005135{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005136 struct nfs4_sequence_data *calldata = data;
5137 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005138
Trond Myklebust14516c32010-07-31 14:29:06 -04005139 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5140 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005141
5142 if (task->tk_status < 0) {
5143 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005144 if (atomic_read(&clp->cl_count) == 1)
5145 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005146
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005147 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5148 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005149 return;
5150 }
5151 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005152 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005153out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005154 dprintk("<-- %s\n", __func__);
5155}
5156
5157static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5158{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005159 struct nfs4_sequence_data *calldata = data;
5160 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005161 struct nfs4_sequence_args *args;
5162 struct nfs4_sequence_res *res;
5163
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005164 args = task->tk_msg.rpc_argp;
5165 res = task->tk_msg.rpc_resp;
5166
Trond Myklebust035168a2010-06-16 09:52:26 -04005167 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005168 return;
5169 rpc_call_start(task);
5170}
5171
5172static const struct rpc_call_ops nfs41_sequence_ops = {
5173 .rpc_call_done = nfs41_sequence_call_done,
5174 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005175 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005176};
5177
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005178static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005179{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005180 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005181 struct rpc_message msg = {
5182 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5183 .rpc_cred = cred,
5184 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005185 struct rpc_task_setup task_setup_data = {
5186 .rpc_client = clp->cl_rpcclient,
5187 .rpc_message = &msg,
5188 .callback_ops = &nfs41_sequence_ops,
5189 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5190 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005191
Alexandros Batsakis71358402010-02-05 03:45:05 -08005192 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005193 return ERR_PTR(-EIO);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005194 calldata = kmalloc(sizeof(*calldata), GFP_NOFS);
5195 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005196 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005197 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005198 }
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005199 calldata->res.sr_slotid = NFS4_MAX_SLOT_TABLE;
5200 msg.rpc_argp = &calldata->args;
5201 msg.rpc_resp = &calldata->res;
5202 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005203 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005204
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005205 return rpc_run_task(&task_setup_data);
5206}
5207
5208static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5209{
5210 struct rpc_task *task;
5211 int ret = 0;
5212
5213 task = _nfs41_proc_sequence(clp, cred);
5214 if (IS_ERR(task))
5215 ret = PTR_ERR(task);
5216 else
5217 rpc_put_task(task);
5218 dprintk("<-- %s status=%d\n", __func__, ret);
5219 return ret;
5220}
5221
5222static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5223{
5224 struct rpc_task *task;
5225 int ret;
5226
5227 task = _nfs41_proc_sequence(clp, cred);
5228 if (IS_ERR(task)) {
5229 ret = PTR_ERR(task);
5230 goto out;
5231 }
5232 ret = rpc_wait_for_completion_task(task);
5233 if (!ret)
5234 ret = task->tk_status;
5235 rpc_put_task(task);
5236out:
5237 dprintk("<-- %s status=%d\n", __func__, ret);
5238 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005239}
5240
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005241struct nfs4_reclaim_complete_data {
5242 struct nfs_client *clp;
5243 struct nfs41_reclaim_complete_args arg;
5244 struct nfs41_reclaim_complete_res res;
5245};
5246
5247static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5248{
5249 struct nfs4_reclaim_complete_data *calldata = data;
5250
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005251 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005252 if (nfs41_setup_sequence(calldata->clp->cl_session,
5253 &calldata->arg.seq_args,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005254 &calldata->res.seq_res, 0, task))
5255 return;
5256
5257 rpc_call_start(task);
5258}
5259
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005260static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5261{
5262 switch(task->tk_status) {
5263 case 0:
5264 case -NFS4ERR_COMPLETE_ALREADY:
5265 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5266 break;
5267 case -NFS4ERR_DELAY:
5268 case -EKEYEXPIRED:
5269 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5270 return -EAGAIN;
5271 default:
5272 nfs4_schedule_state_recovery(clp);
5273 }
5274 return 0;
5275}
5276
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005277static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5278{
5279 struct nfs4_reclaim_complete_data *calldata = data;
5280 struct nfs_client *clp = calldata->clp;
5281 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5282
5283 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005284 if (!nfs41_sequence_done(task, res))
5285 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005286
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005287 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5288 rpc_restart_call_prepare(task);
5289 return;
5290 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005291 dprintk("<-- %s\n", __func__);
5292}
5293
5294static void nfs4_free_reclaim_complete_data(void *data)
5295{
5296 struct nfs4_reclaim_complete_data *calldata = data;
5297
5298 kfree(calldata);
5299}
5300
5301static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5302 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5303 .rpc_call_done = nfs4_reclaim_complete_done,
5304 .rpc_release = nfs4_free_reclaim_complete_data,
5305};
5306
5307/*
5308 * Issue a global reclaim complete.
5309 */
5310static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5311{
5312 struct nfs4_reclaim_complete_data *calldata;
5313 struct rpc_task *task;
5314 struct rpc_message msg = {
5315 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5316 };
5317 struct rpc_task_setup task_setup_data = {
5318 .rpc_client = clp->cl_rpcclient,
5319 .rpc_message = &msg,
5320 .callback_ops = &nfs4_reclaim_complete_call_ops,
5321 .flags = RPC_TASK_ASYNC,
5322 };
5323 int status = -ENOMEM;
5324
5325 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005326 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005327 if (calldata == NULL)
5328 goto out;
5329 calldata->clp = clp;
5330 calldata->arg.one_fs = 0;
5331 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
5332
5333 msg.rpc_argp = &calldata->arg;
5334 msg.rpc_resp = &calldata->res;
5335 task_setup_data.callback_data = calldata;
5336 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005337 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005338 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005339 goto out;
5340 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005341 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005342 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005343out:
5344 dprintk("<-- %s status=%d\n", __func__, status);
5345 return status;
5346}
Andy Adamson557134a2009-04-01 09:21:53 -04005347#endif /* CONFIG_NFS_V4_1 */
5348
Andy Adamson591d71c2009-04-01 09:22:47 -04005349struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005350 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005351 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352 .recover_open = nfs4_open_reclaim,
5353 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04005354 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005355 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356};
5357
Andy Adamson591d71c2009-04-01 09:22:47 -04005358#if defined(CONFIG_NFS_V4_1)
5359struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5360 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5361 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5362 .recover_open = nfs4_open_reclaim,
5363 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05005364 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005365 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005366 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04005367};
5368#endif /* CONFIG_NFS_V4_1 */
5369
5370struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005371 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005372 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 .recover_open = nfs4_open_expired,
5374 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04005375 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005376 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377};
5378
Andy Adamson591d71c2009-04-01 09:22:47 -04005379#if defined(CONFIG_NFS_V4_1)
5380struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5381 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5382 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5383 .recover_open = nfs4_open_expired,
5384 .recover_lock = nfs4_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05005385 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005386 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04005387};
5388#endif /* CONFIG_NFS_V4_1 */
5389
Benny Halevy29fba382009-04-01 09:22:44 -04005390struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5391 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04005392 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005393 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04005394};
5395
5396#if defined(CONFIG_NFS_V4_1)
5397struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5398 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04005399 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005400 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04005401};
5402#endif
5403
Trond Myklebust97dc1352010-06-16 09:52:26 -04005404static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
5405 .minor_version = 0,
5406 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04005407 .validate_stateid = nfs4_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005408 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
5409 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
5410 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005411};
5412
5413#if defined(CONFIG_NFS_V4_1)
5414static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
5415 .minor_version = 1,
5416 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04005417 .validate_stateid = nfs41_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005418 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
5419 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
5420 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005421};
5422#endif
5423
Trond Myklebust97dc1352010-06-16 09:52:26 -04005424const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
5425 [0] = &nfs_v4_0_minor_ops,
5426#if defined(CONFIG_NFS_V4_1)
5427 [1] = &nfs_v4_1_minor_ops,
5428#endif
5429};
5430
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005431static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005432 .permission = nfs_permission,
5433 .getattr = nfs_getattr,
5434 .setattr = nfs_setattr,
5435 .getxattr = nfs4_getxattr,
5436 .setxattr = nfs4_setxattr,
5437 .listxattr = nfs4_listxattr,
5438};
5439
David Howells509de812006-08-22 20:06:11 -04005440const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 .version = 4, /* protocol version */
5442 .dentry_ops = &nfs4_dentry_operations,
5443 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005444 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 .getroot = nfs4_proc_get_root,
5446 .getattr = nfs4_proc_getattr,
5447 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04005448 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 .lookup = nfs4_proc_lookup,
5450 .access = nfs4_proc_access,
5451 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 .create = nfs4_proc_create,
5453 .remove = nfs4_proc_remove,
5454 .unlink_setup = nfs4_proc_unlink_setup,
5455 .unlink_done = nfs4_proc_unlink_done,
5456 .rename = nfs4_proc_rename,
5457 .link = nfs4_proc_link,
5458 .symlink = nfs4_proc_symlink,
5459 .mkdir = nfs4_proc_mkdir,
5460 .rmdir = nfs4_proc_remove,
5461 .readdir = nfs4_proc_readdir,
5462 .mknod = nfs4_proc_mknod,
5463 .statfs = nfs4_proc_statfs,
5464 .fsinfo = nfs4_proc_fsinfo,
5465 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04005466 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 .decode_dirent = nfs4_decode_dirent,
5468 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05005469 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005471 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005473 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005475 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04005476 .close_context = nfs4_close_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477};
5478
5479/*
5480 * Local variables:
5481 * c-basic-offset: 8
5482 * End:
5483 */