blob: 6b44bbfb7d8a52932846aa86291e218ce5ed3719 [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:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400449 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400450 goto out;
451 rpc_delay(task, NFS4_POLL_RETRY_MAX);
452 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400453}
454
Trond Myklebust14516c32010-07-31 14:29:06 -0400455static int nfs4_sequence_done(struct rpc_task *task,
456 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400457{
Trond Myklebust14516c32010-07-31 14:29:06 -0400458 if (res->sr_session == NULL)
459 return 1;
460 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400461}
462
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400463/*
Benny Halevy510b8172009-04-01 09:22:14 -0400464 * nfs4_find_slot - efficiently look for a free slot
465 *
466 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
467 * If found, we mark the slot as used, update the highest_used_slotid,
468 * and respectively set up the sequence operation args.
469 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400470 *
471 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400472 */
473static u8
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800474nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400475{
476 int slotid;
477 u8 ret_id = NFS4_MAX_SLOT_TABLE;
478 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
479
Benny Halevy510b8172009-04-01 09:22:14 -0400480 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
481 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
482 tbl->max_slots);
483 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
484 if (slotid >= tbl->max_slots)
485 goto out;
486 __set_bit(slotid, tbl->used_slots);
487 if (slotid > tbl->highest_used_slotid)
488 tbl->highest_used_slotid = slotid;
489 ret_id = slotid;
490out:
491 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
492 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400493 return ret_id;
494}
495
Andy Adamsonce5039c2009-04-01 09:22:13 -0400496static int nfs41_setup_sequence(struct nfs4_session *session,
497 struct nfs4_sequence_args *args,
498 struct nfs4_sequence_res *res,
499 int cache_reply,
500 struct rpc_task *task)
501{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400502 struct nfs4_slot *slot;
503 struct nfs4_slot_table *tbl;
504 u8 slotid;
505
506 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400507 /* slot already allocated? */
508 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
509 return 0;
510
Andy Adamsonce5039c2009-04-01 09:22:13 -0400511 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400512 tbl = &session->fc_slot_table;
513
514 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400515 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800516 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamsonea028ac2009-12-04 15:55:38 -0500517 /*
518 * The state manager will wait until the slot table is empty.
519 * Schedule the reset thread
520 */
Andy Adamson05f0d232009-12-04 15:55:37 -0500521 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400522 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500523 dprintk("%s Schedule Session Reset\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500524 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400525 }
526
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800527 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
528 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
529 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
530 spin_unlock(&tbl->slot_tbl_lock);
531 dprintk("%s enforce FIFO order\n", __func__);
532 return -EAGAIN;
533 }
534
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800535 slotid = nfs4_find_slot(tbl);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400536 if (slotid == NFS4_MAX_SLOT_TABLE) {
537 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
538 spin_unlock(&tbl->slot_tbl_lock);
539 dprintk("<-- %s: no free slots\n", __func__);
540 return -EAGAIN;
541 }
542 spin_unlock(&tbl->slot_tbl_lock);
543
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800544 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400545 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400546 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400547 args->sa_slotid = slotid;
548 args->sa_cache_this = cache_reply;
549
550 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
551
Benny Halevy99fe60d2009-04-01 09:22:29 -0400552 res->sr_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400553 res->sr_slotid = slotid;
554 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400555 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400556 /*
557 * sr_status is only set in decode_sequence, and so will remain
558 * set to 1 if an rpc level failure occurs.
559 */
560 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400561 return 0;
562}
563
Trond Myklebust035168a2010-06-16 09:52:26 -0400564int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400565 struct nfs4_sequence_args *args,
566 struct nfs4_sequence_res *res,
567 int cache_reply,
568 struct rpc_task *task)
569{
Trond Myklebust035168a2010-06-16 09:52:26 -0400570 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400571 int ret = 0;
572
Trond Myklebust035168a2010-06-16 09:52:26 -0400573 if (session == NULL) {
574 args->sa_session = NULL;
575 res->sr_session = NULL;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400576 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400577 }
578
579 dprintk("--> %s clp %p session %p sr_slotid %d\n",
580 __func__, session->clp, session, res->sr_slotid);
581
582 ret = nfs41_setup_sequence(session, args, res, cache_reply,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400583 task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400584out:
585 dprintk("<-- %s status=%d\n", __func__, ret);
586 return ret;
587}
588
589struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400590 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400591 struct nfs4_sequence_args *seq_args;
592 struct nfs4_sequence_res *seq_res;
593 int cache_reply;
594};
595
596static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
597{
598 struct nfs41_call_sync_data *data = calldata;
599
Trond Myklebust035168a2010-06-16 09:52:26 -0400600 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
601
602 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400603 data->seq_res, data->cache_reply, task))
604 return;
605 rpc_call_start(task);
606}
607
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800608static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
609{
610 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
611 nfs41_call_sync_prepare(task, calldata);
612}
613
Andy Adamson69ab40c2009-04-01 09:22:19 -0400614static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
615{
616 struct nfs41_call_sync_data *data = calldata;
617
Trond Myklebust14516c32010-07-31 14:29:06 -0400618 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400619}
620
Andy Adamsonce5039c2009-04-01 09:22:13 -0400621struct rpc_call_ops nfs41_call_sync_ops = {
622 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400623 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400624};
625
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800626struct rpc_call_ops nfs41_call_priv_sync_ops = {
627 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
628 .rpc_call_done = nfs41_call_sync_done,
629};
630
Trond Myklebust035168a2010-06-16 09:52:26 -0400631static int nfs4_call_sync_sequence(struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400632 struct rpc_message *msg,
633 struct nfs4_sequence_args *args,
634 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800635 int cache_reply,
636 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400637{
638 int ret;
639 struct rpc_task *task;
640 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400641 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400642 .seq_args = args,
643 .seq_res = res,
644 .cache_reply = cache_reply,
645 };
646 struct rpc_task_setup task_setup = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400647 .rpc_client = server->client,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400648 .rpc_message = msg,
649 .callback_ops = &nfs41_call_sync_ops,
650 .callback_data = &data
651 };
652
653 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800654 if (privileged)
655 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400656 task = rpc_run_task(&task_setup);
657 if (IS_ERR(task))
658 ret = PTR_ERR(task);
659 else {
660 ret = task->tk_status;
661 rpc_put_task(task);
662 }
663 return ret;
664}
665
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400666int _nfs4_call_sync_session(struct nfs_server *server,
667 struct rpc_message *msg,
668 struct nfs4_sequence_args *args,
669 struct nfs4_sequence_res *res,
670 int cache_reply)
671{
Trond Myklebust035168a2010-06-16 09:52:26 -0400672 return nfs4_call_sync_sequence(server, msg, args, res, cache_reply, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400673}
674
Trond Myklebustdf896452010-06-16 09:52:26 -0400675#else
Trond Myklebust14516c32010-07-31 14:29:06 -0400676static int nfs4_sequence_done(struct rpc_task *task,
677 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400678{
Trond Myklebust14516c32010-07-31 14:29:06 -0400679 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400680}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400681#endif /* CONFIG_NFS_V4_1 */
682
683int _nfs4_call_sync(struct nfs_server *server,
684 struct rpc_message *msg,
685 struct nfs4_sequence_args *args,
686 struct nfs4_sequence_res *res,
687 int cache_reply)
688{
Benny Halevyf3752972009-04-01 09:22:04 -0400689 args->sa_session = res->sr_session = NULL;
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400690 return rpc_call_sync(server->client, msg, 0);
691}
692
693#define nfs4_call_sync(server, msg, args, res, cache_reply) \
Trond Myklebust97dc1352010-06-16 09:52:26 -0400694 (server)->nfs_client->cl_mvops->call_sync((server), (msg), &(args)->seq_args, \
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400695 &(res)->seq_res, (cache_reply))
696
Trond Myklebust38478b22006-05-25 01:40:57 -0400697static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
Trond Myklebust38478b22006-05-25 01:40:57 -0400699 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Trond Myklebust38478b22006-05-25 01:40:57 -0400701 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400702 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
703 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400704 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400705 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400706 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
708
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100709struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400710 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100711 struct nfs_openargs o_arg;
712 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100713 struct nfs_open_confirmargs c_arg;
714 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100715 struct nfs_fattr f_attr;
716 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400717 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100718 struct dentry *dir;
719 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400720 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100721 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100722 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400723 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100724 int rpc_status;
725 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100726};
727
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400728
729static void nfs4_init_opendata_res(struct nfs4_opendata *p)
730{
731 p->o_res.f_attr = &p->f_attr;
732 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400733 p->o_res.seqid = p->o_arg.seqid;
734 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400735 p->o_res.server = p->o_arg.server;
736 nfs_fattr_init(&p->f_attr);
737 nfs_fattr_init(&p->dir_attr);
Benny Halevy578e4582009-06-18 22:01:23 -0400738 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400739}
740
Trond Myklebustad389da2007-06-05 12:30:00 -0400741static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500742 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400743 const struct iattr *attrs,
744 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100745{
Trond Myklebustad389da2007-06-05 12:30:00 -0400746 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100747 struct inode *dir = parent->d_inode;
748 struct nfs_server *server = NFS_SERVER(dir);
749 struct nfs4_opendata *p;
750
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400751 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100752 if (p == NULL)
753 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400754 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100755 if (p->o_arg.seqid == NULL)
756 goto err_free;
Al Virof6948692010-01-30 13:51:04 -0500757 path_get(path);
758 p->path = *path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100759 p->dir = parent;
760 p->owner = sp;
761 atomic_inc(&sp->so_count);
762 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500763 p->o_arg.open_flags = flags;
764 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400765 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400766 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400767 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100768 p->o_arg.server = server;
769 p->o_arg.bitmask = server->attr_bitmask;
770 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400771 if (flags & O_CREAT) {
772 u32 *s;
773
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100774 p->o_arg.u.attrs = &p->attrs;
775 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400776 s = (u32 *) p->o_arg.u.verifier.data;
777 s[0] = jiffies;
778 s[1] = current->pid;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100779 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100780 p->c_arg.fh = &p->o_res.fh;
781 p->c_arg.stateid = &p->o_res.stateid;
782 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400783 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400784 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100785 return p;
786err_free:
787 kfree(p);
788err:
789 dput(parent);
790 return NULL;
791}
792
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400793static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100794{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400795 struct nfs4_opendata *p = container_of(kref,
796 struct nfs4_opendata, kref);
797
798 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400799 if (p->state != NULL)
800 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400801 nfs4_put_state_owner(p->owner);
802 dput(p->dir);
Jan Blunck31f31db12008-05-02 13:42:45 -0700803 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400804 kfree(p);
805}
806
807static void nfs4_opendata_put(struct nfs4_opendata *p)
808{
809 if (p != NULL)
810 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100811}
812
Trond Myklebust06f814a2006-01-03 09:55:07 +0100813static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
814{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100815 int ret;
816
Trond Myklebust06f814a2006-01-03 09:55:07 +0100817 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100818 return ret;
819}
820
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500821static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400822{
823 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500824
825 if (open_mode & O_EXCL)
826 goto out;
827 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400828 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500829 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
830 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400831 break;
832 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500833 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
834 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400835 break;
836 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500837 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
838 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400839 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500840out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400841 return ret;
842}
843
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500844static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400845{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500846 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400847 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500848 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400849 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500850 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400851 return 1;
852}
853
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500854static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100855{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500856 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100857 case FMODE_WRITE:
858 state->n_wronly++;
859 break;
860 case FMODE_READ:
861 state->n_rdonly++;
862 break;
863 case FMODE_READ|FMODE_WRITE:
864 state->n_rdwr++;
865 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500866 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100867}
868
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500869static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400870{
871 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
872 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
873 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500874 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400875 case FMODE_READ:
876 set_bit(NFS_O_RDONLY_STATE, &state->flags);
877 break;
878 case FMODE_WRITE:
879 set_bit(NFS_O_WRONLY_STATE, &state->flags);
880 break;
881 case FMODE_READ|FMODE_WRITE:
882 set_bit(NFS_O_RDWR_STATE, &state->flags);
883 }
884}
885
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500886static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400887{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400888 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500889 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400890 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400891}
892
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500893static 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 -0700894{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400895 /*
896 * Protect the call to nfs4_state_set_mode_locked and
897 * serialise the stateid update
898 */
899 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400900 if (deleg_stateid != NULL) {
901 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
902 set_bit(NFS_DELEGATED_STATE, &state->flags);
903 }
904 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500905 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400906 write_sequnlock(&state->seqlock);
907 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500908 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700909 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910}
911
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500912static 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 -0500913{
914 struct nfs_inode *nfsi = NFS_I(state->inode);
915 struct nfs_delegation *deleg_cur;
916 int ret = 0;
917
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500918 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500919
920 rcu_read_lock();
921 deleg_cur = rcu_dereference(nfsi->delegation);
922 if (deleg_cur == NULL)
923 goto no_delegation;
924
925 spin_lock(&deleg_cur->lock);
926 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500927 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500928 goto no_delegation_unlock;
929
930 if (delegation == NULL)
931 delegation = &deleg_cur->stateid;
932 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
933 goto no_delegation_unlock;
934
Trond Myklebustb7391f42008-12-23 15:21:52 -0500935 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500936 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500937 ret = 1;
938no_delegation_unlock:
939 spin_unlock(&deleg_cur->lock);
940no_delegation:
941 rcu_read_unlock();
942
943 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500944 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500945 ret = 1;
946 }
947
948 return ret;
949}
950
951
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500952static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400953{
954 struct nfs_delegation *delegation;
955
956 rcu_read_lock();
957 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500958 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400959 rcu_read_unlock();
960 return;
961 }
962 rcu_read_unlock();
963 nfs_inode_return_delegation(inode);
964}
965
Trond Myklebust6ee41262007-07-08 14:11:36 -0400966static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400967{
968 struct nfs4_state *state = opendata->state;
969 struct nfs_inode *nfsi = NFS_I(state->inode);
970 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500971 int open_mode = opendata->o_arg.open_flags & O_EXCL;
972 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400973 nfs4_stateid stateid;
974 int ret = -EAGAIN;
975
Trond Myklebustaac00a82007-07-05 19:02:21 -0400976 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500977 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400978 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500979 if (can_open_cached(state, fmode, open_mode)) {
980 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400981 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400982 goto out_return_state;
983 }
984 spin_unlock(&state->owner->so_lock);
985 }
Trond Myklebust34310432008-12-23 15:21:38 -0500986 rcu_read_lock();
987 delegation = rcu_dereference(nfsi->delegation);
988 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500989 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -0500990 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -0400991 break;
Trond Myklebust34310432008-12-23 15:21:38 -0500992 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400993 /* Save the delegation */
994 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
995 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -0400996 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400997 if (ret != 0)
998 goto out;
999 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001000
1001 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001002 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001003 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001004 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001005out:
1006 return ERR_PTR(ret);
1007out_return_state:
1008 atomic_inc(&state->count);
1009 return state;
1010}
1011
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001012static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1013{
1014 struct inode *inode;
1015 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001016 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001017 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001018
Trond Myklebustaac00a82007-07-05 19:02:21 -04001019 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001020 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001021 goto out;
1022 }
1023
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001024 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001025 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001026 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001027 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001028 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001029 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001030 goto err;
1031 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001032 state = nfs4_get_open_state(inode, data->owner);
1033 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001034 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001035 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001036 int delegation_flags = 0;
1037
Trond Myklebust003707c2007-07-05 18:07:55 -04001038 rcu_read_lock();
1039 delegation = rcu_dereference(NFS_I(inode)->delegation);
1040 if (delegation)
1041 delegation_flags = delegation->flags;
1042 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -05001043 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001044 nfs_inode_set_delegation(state->inode,
1045 data->owner->so_cred,
1046 &data->o_res);
1047 else
1048 nfs_inode_reclaim_delegation(state->inode,
1049 data->owner->so_cred,
1050 &data->o_res);
1051 }
Trond Myklebust34310432008-12-23 15:21:38 -05001052
1053 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001054 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001055 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001056out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001057 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001058err_put_inode:
1059 iput(inode);
1060err:
1061 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001062}
1063
Trond Myklebust864472e2006-01-03 09:55:15 +01001064static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1065{
1066 struct nfs_inode *nfsi = NFS_I(state->inode);
1067 struct nfs_open_context *ctx;
1068
1069 spin_lock(&state->inode->i_lock);
1070 list_for_each_entry(ctx, &nfsi->open_files, list) {
1071 if (ctx->state != state)
1072 continue;
1073 get_nfs_open_context(ctx);
1074 spin_unlock(&state->inode->i_lock);
1075 return ctx;
1076 }
1077 spin_unlock(&state->inode->i_lock);
1078 return ERR_PTR(-ENOENT);
1079}
1080
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001081static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1082{
1083 struct nfs4_opendata *opendata;
1084
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001085 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001086 if (opendata == NULL)
1087 return ERR_PTR(-ENOMEM);
1088 opendata->state = state;
1089 atomic_inc(&state->count);
1090 return opendata;
1091}
1092
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001093static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001094{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001095 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001096 int ret;
1097
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001098 opendata->o_arg.open_flags = 0;
1099 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001100 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1101 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1102 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001103 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001104 if (ret != 0)
1105 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001106 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001107 if (IS_ERR(newstate))
1108 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001109 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001110 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001111 return 0;
1112}
1113
1114static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1115{
Trond Myklebust864472e2006-01-03 09:55:15 +01001116 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001117 int ret;
1118
1119 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001120 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001121 smp_rmb();
1122 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001123 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001124 if (ret != 0)
1125 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001126 if (newstate != state)
1127 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001128 }
1129 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001130 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001131 if (ret != 0)
1132 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001133 if (newstate != state)
1134 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001135 }
1136 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001137 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001138 if (ret != 0)
1139 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001140 if (newstate != state)
1141 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001142 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001143 /*
1144 * We may have performed cached opens for all three recoveries.
1145 * Check if we need to update the current stateid.
1146 */
1147 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1148 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001149 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001150 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1151 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001152 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001153 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001154 return 0;
1155}
1156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157/*
1158 * OPEN_RECLAIM:
1159 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001161static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001163 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001164 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001165 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 int status;
1167
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001168 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1169 if (IS_ERR(opendata))
1170 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001171 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1172 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001173 rcu_read_lock();
1174 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001175 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001176 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001177 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001178 opendata->o_arg.u.delegation_type = delegation_type;
1179 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001180 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 return status;
1182}
1183
Trond Myklebust539cd032007-06-05 11:46:42 -04001184static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
1186 struct nfs_server *server = NFS_SERVER(state->inode);
1187 struct nfs4_exception exception = { };
1188 int err;
1189 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001190 err = _nfs4_do_open_reclaim(ctx, state);
Jeff Layton2c643482010-01-07 09:42:03 -05001191 if (err != -NFS4ERR_DELAY && err != -EKEYEXPIRED)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001192 break;
1193 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 } while (exception.retry);
1195 return err;
1196}
1197
Trond Myklebust864472e2006-01-03 09:55:15 +01001198static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1199{
1200 struct nfs_open_context *ctx;
1201 int ret;
1202
1203 ctx = nfs4_state_find_open_context(state);
1204 if (IS_ERR(ctx))
1205 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001206 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001207 put_nfs_open_context(ctx);
1208 return ret;
1209}
1210
Trond Myklebust13437e12007-07-06 15:10:43 -04001211static 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 -07001212{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001213 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001214 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001216 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1217 if (IS_ERR(opendata))
1218 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001219 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001220 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001221 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001222 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001223 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001224 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225}
1226
Trond Myklebust13437e12007-07-06 15:10:43 -04001227int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
1229 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001230 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 int err;
1232 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001233 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 switch (err) {
1235 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001236 case -ENOENT:
1237 case -ESTALE:
1238 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001239 case -NFS4ERR_BADSESSION:
1240 case -NFS4ERR_BADSLOT:
1241 case -NFS4ERR_BAD_HIGH_SLOT:
1242 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1243 case -NFS4ERR_DEADSESSION:
1244 nfs4_schedule_state_recovery(
1245 server->nfs_client);
1246 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 case -NFS4ERR_STALE_CLIENTID:
1248 case -NFS4ERR_STALE_STATEID:
1249 case -NFS4ERR_EXPIRED:
1250 /* Don't recall a delegation if it was lost */
David Howells7539bba2006-08-22 20:06:09 -04001251 nfs4_schedule_state_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001252 goto out;
1253 case -ERESTARTSYS:
1254 /*
1255 * The show must go on: exit, but mark the
1256 * stateid as needing recovery.
1257 */
1258 case -NFS4ERR_ADMIN_REVOKED:
1259 case -NFS4ERR_BAD_STATEID:
1260 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
1261 case -ENOMEM:
1262 err = 0;
1263 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
1265 err = nfs4_handle_exception(server, err, &exception);
1266 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001267out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 return err;
1269}
1270
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001271static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1272{
1273 struct nfs4_opendata *data = calldata;
1274
1275 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001276 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001277 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1278 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001279 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001280 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001281 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001282 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001283}
1284
1285static void nfs4_open_confirm_release(void *calldata)
1286{
1287 struct nfs4_opendata *data = calldata;
1288 struct nfs4_state *state = NULL;
1289
1290 /* If this request hasn't been cancelled, do nothing */
1291 if (data->cancelled == 0)
1292 goto out_free;
1293 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001294 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001295 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001296 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001297 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001298 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001299out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001300 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001301}
1302
1303static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001304 .rpc_call_done = nfs4_open_confirm_done,
1305 .rpc_release = nfs4_open_confirm_release,
1306};
1307
1308/*
1309 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1310 */
1311static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1312{
1313 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1314 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001315 struct rpc_message msg = {
1316 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1317 .rpc_argp = &data->c_arg,
1318 .rpc_resp = &data->c_res,
1319 .rpc_cred = data->owner->so_cred,
1320 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001321 struct rpc_task_setup task_setup_data = {
1322 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001323 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001324 .callback_ops = &nfs4_open_confirm_ops,
1325 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001326 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001327 .flags = RPC_TASK_ASYNC,
1328 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 int status;
1330
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001331 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001332 data->rpc_done = 0;
1333 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001334 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001335 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001336 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001337 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001338 status = nfs4_wait_for_completion_rpc_task(task);
1339 if (status != 0) {
1340 data->cancelled = 1;
1341 smp_wmb();
1342 } else
1343 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001344 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 return status;
1346}
1347
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001348static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001350 struct nfs4_opendata *data = calldata;
1351 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001352
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001353 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1354 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001355 /*
1356 * Check if we still need to send an OPEN call, or if we can use
1357 * a delegation instead.
1358 */
1359 if (data->state != NULL) {
1360 struct nfs_delegation *delegation;
1361
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001362 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001363 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001364 rcu_read_lock();
1365 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1366 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001367 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001368 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001369 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001370 }
1371 rcu_read_unlock();
1372 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001373 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001374 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001375 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001376 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001377 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001378 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1379 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001380 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001381 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001382 &data->o_arg.seq_args,
1383 &data->o_res.seq_res, 1, task))
1384 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001385 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001386 return;
1387out_no_action:
1388 task->tk_action = NULL;
1389
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001390}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001392static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1393{
1394 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1395 nfs4_open_prepare(task, calldata);
1396}
1397
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001398static void nfs4_open_done(struct rpc_task *task, void *calldata)
1399{
1400 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001402 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001403
Trond Myklebust14516c32010-07-31 14:29:06 -04001404 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1405 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001406
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001407 if (task->tk_status == 0) {
1408 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001409 case S_IFREG:
1410 break;
1411 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001412 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001413 break;
1414 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001415 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001416 break;
1417 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001418 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001419 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001420 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001421 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1422 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001423 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001424 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001425}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001426
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001427static void nfs4_open_release(void *calldata)
1428{
1429 struct nfs4_opendata *data = calldata;
1430 struct nfs4_state *state = NULL;
1431
1432 /* If this request hasn't been cancelled, do nothing */
1433 if (data->cancelled == 0)
1434 goto out_free;
1435 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001436 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001437 goto out_free;
1438 /* In case we need an open_confirm, no cleanup! */
1439 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1440 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001441 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001442 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001443 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001444out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001445 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001446}
1447
1448static const struct rpc_call_ops nfs4_open_ops = {
1449 .rpc_call_prepare = nfs4_open_prepare,
1450 .rpc_call_done = nfs4_open_done,
1451 .rpc_release = nfs4_open_release,
1452};
1453
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001454static const struct rpc_call_ops nfs4_recover_open_ops = {
1455 .rpc_call_prepare = nfs4_recover_open_prepare,
1456 .rpc_call_done = nfs4_open_done,
1457 .rpc_release = nfs4_open_release,
1458};
1459
1460static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001461{
1462 struct inode *dir = data->dir->d_inode;
1463 struct nfs_server *server = NFS_SERVER(dir);
1464 struct nfs_openargs *o_arg = &data->o_arg;
1465 struct nfs_openres *o_res = &data->o_res;
1466 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001467 struct rpc_message msg = {
1468 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1469 .rpc_argp = o_arg,
1470 .rpc_resp = o_res,
1471 .rpc_cred = data->owner->so_cred,
1472 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001473 struct rpc_task_setup task_setup_data = {
1474 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001475 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001476 .callback_ops = &nfs4_open_ops,
1477 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001478 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001479 .flags = RPC_TASK_ASYNC,
1480 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001481 int status;
1482
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001483 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001484 data->rpc_done = 0;
1485 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001486 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001487 if (isrecover)
1488 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001489 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001490 if (IS_ERR(task))
1491 return PTR_ERR(task);
1492 status = nfs4_wait_for_completion_rpc_task(task);
1493 if (status != 0) {
1494 data->cancelled = 1;
1495 smp_wmb();
1496 } else
1497 status = data->rpc_status;
1498 rpc_put_task(task);
1499
1500 return status;
1501}
1502
1503static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1504{
1505 struct inode *dir = data->dir->d_inode;
1506 struct nfs_openres *o_res = &data->o_res;
1507 int status;
1508
1509 status = nfs4_run_open_task(data, 1);
1510 if (status != 0 || !data->rpc_done)
1511 return status;
1512
1513 nfs_refresh_inode(dir, o_res->dir_attr);
1514
1515 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1516 status = _nfs4_proc_open_confirm(data);
1517 if (status != 0)
1518 return status;
1519 }
1520
1521 return status;
1522}
1523
1524/*
1525 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1526 */
1527static int _nfs4_proc_open(struct nfs4_opendata *data)
1528{
1529 struct inode *dir = data->dir->d_inode;
1530 struct nfs_server *server = NFS_SERVER(dir);
1531 struct nfs_openargs *o_arg = &data->o_arg;
1532 struct nfs_openres *o_res = &data->o_res;
1533 int status;
1534
1535 status = nfs4_run_open_task(data, 0);
Trond Myklebust3e309912007-07-07 13:19:59 -04001536 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001537 return status;
1538
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001539 if (o_arg->open_flags & O_CREAT) {
1540 update_changeattr(dir, &o_res->cinfo);
1541 nfs_post_op_update_inode(dir, o_res->dir_attr);
1542 } else
1543 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001544 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1545 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001547 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001549 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 }
1551 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001552 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001553 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554}
1555
Trond Myklebust10afec92007-05-14 17:16:04 -04001556static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +01001557{
David Howells7539bba2006-08-22 20:06:09 -04001558 struct nfs_client *clp = server->nfs_client;
Trond Myklebusta78cb572009-08-09 15:06:19 -04001559 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001560 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001561
Trond Myklebusta78cb572009-08-09 15:06:19 -04001562 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001563 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001564 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001565 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001566 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1567 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001568 break;
Trond Myklebust58d97142006-01-03 09:55:24 +01001569 nfs4_schedule_state_recovery(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001570 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001571 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001572 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001573}
1574
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575/*
1576 * OPEN_EXPIRED:
1577 * reclaim state on the server after a network partition.
1578 * Assumes caller holds the appropriate lock
1579 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001580static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001582 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001583 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001585 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1586 if (IS_ERR(opendata))
1587 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001588 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001589 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001590 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001591 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001592 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593}
1594
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001595static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001596{
Trond Myklebust539cd032007-06-05 11:46:42 -04001597 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001598 struct nfs4_exception exception = { };
1599 int err;
1600
1601 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001602 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001603 switch (err) {
1604 default:
1605 goto out;
1606 case -NFS4ERR_GRACE:
1607 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05001608 case -EKEYEXPIRED:
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001609 nfs4_handle_exception(server, err, &exception);
1610 err = 0;
1611 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001612 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001613out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001614 return err;
1615}
1616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1618{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001620 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
Trond Myklebust864472e2006-01-03 09:55:15 +01001622 ctx = nfs4_state_find_open_context(state);
1623 if (IS_ERR(ctx))
1624 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001625 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001626 put_nfs_open_context(ctx);
1627 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628}
1629
1630/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001631 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1632 * fields corresponding to attributes that were used to store the verifier.
1633 * Make sure we clobber those fields in the later setattr call
1634 */
1635static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1636{
1637 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1638 !(sattr->ia_valid & ATTR_ATIME_SET))
1639 sattr->ia_valid |= ATTR_ATIME;
1640
1641 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1642 !(sattr->ia_valid & ATTR_MTIME_SET))
1643 sattr->ia_valid |= ATTR_MTIME;
1644}
1645
1646/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001647 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001649static 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 -07001650{
1651 struct nfs4_state_owner *sp;
1652 struct nfs4_state *state = NULL;
1653 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001654 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001655 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
1657 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 status = -ENOMEM;
1659 if (!(sp = nfs4_get_state_owner(server, cred))) {
1660 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1661 goto out_err;
1662 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001663 status = nfs4_recover_expired_lease(server);
1664 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001665 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001666 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001667 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001668 status = -ENOMEM;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001669 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001670 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001671 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Trond Myklebustaac00a82007-07-05 19:02:21 -04001673 if (path->dentry->d_inode != NULL)
1674 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1675
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001676 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001678 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001680 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001681 status = PTR_ERR(state);
1682 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001683 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001684 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001685 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001686
1687 if (opendata->o_arg.open_flags & O_EXCL) {
1688 nfs4_exclusive_attrset(opendata, sattr);
1689
1690 nfs_fattr_init(opendata->o_res.f_attr);
1691 status = nfs4_do_setattr(state->inode, cred,
1692 opendata->o_res.f_attr, sattr,
1693 state);
1694 if (status == 0)
1695 nfs_setattr_update_inode(state->inode, sattr);
1696 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1697 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001698 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 *res = state;
1701 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001702err_opendata_put:
1703 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001704err_put_state_owner:
1705 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 *res = NULL;
1708 return status;
1709}
1710
1711
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001712static 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 -07001713{
1714 struct nfs4_exception exception = { };
1715 struct nfs4_state *res;
1716 int status;
1717
1718 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001719 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 if (status == 0)
1721 break;
1722 /* NOTE: BAD_SEQID means the server and client disagree about the
1723 * book-keeping w.r.t. state-changing operations
1724 * (OPEN/CLOSE/LOCK/LOCKU...)
1725 * It is actually a sign of a bug on the client or on the server.
1726 *
1727 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001728 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 * have unhashed the old state_owner for us, and that we can
1730 * therefore safely retry using a new one. We should still warn
1731 * the user though...
1732 */
1733 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001734 printk(KERN_WARNING "NFS: v4 server %s "
1735 " returned a bad sequence-id error!\n",
1736 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 exception.retry = 1;
1738 continue;
1739 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001740 /*
1741 * BAD_STATEID on OPEN means that the server cancelled our
1742 * state before it received the OPEN_CONFIRM.
1743 * Recover by retrying the request as per the discussion
1744 * on Page 181 of RFC3530.
1745 */
1746 if (status == -NFS4ERR_BAD_STATEID) {
1747 exception.retry = 1;
1748 continue;
1749 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001750 if (status == -EAGAIN) {
1751 /* We must have found a delegation */
1752 exception.retry = 1;
1753 continue;
1754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1756 status, &exception));
1757 } while (exception.retry);
1758 return res;
1759}
1760
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001761static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1762 struct nfs_fattr *fattr, struct iattr *sattr,
1763 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001765 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001767 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 .iap = sattr,
1769 .server = server,
1770 .bitmask = server->attr_bitmask,
1771 };
1772 struct nfs_setattrres res = {
1773 .fattr = fattr,
1774 .server = server,
1775 };
1776 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001777 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1778 .rpc_argp = &arg,
1779 .rpc_resp = &res,
1780 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001782 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001783 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Trond Myklebust0e574af2005-10-27 22:12:38 -04001785 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001787 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1788 /* Use that stateid */
1789 } else if (state != NULL) {
Trond Myklebust77041ed2010-07-01 12:49:11 -04001790 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001791 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1793
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001794 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001795 if (status == 0 && state != NULL)
1796 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001797 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798}
1799
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001800static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1801 struct nfs_fattr *fattr, struct iattr *sattr,
1802 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001804 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 struct nfs4_exception exception = { };
1806 int err;
1807 do {
1808 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001809 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 &exception);
1811 } while (exception.retry);
1812 return err;
1813}
1814
1815struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001816 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 struct inode *inode;
1818 struct nfs4_state *state;
1819 struct nfs_closeargs arg;
1820 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001821 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001822 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823};
1824
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001825static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001826{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001827 struct nfs4_closedata *calldata = data;
1828 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001829
1830 nfs4_put_open_state(calldata->state);
1831 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001832 nfs4_put_state_owner(sp);
Jan Blunck31f31db12008-05-02 13:42:45 -07001833 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001834 kfree(calldata);
1835}
1836
Trond Myklebust88069f72009-12-08 08:33:16 -05001837static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1838 fmode_t fmode)
1839{
1840 spin_lock(&state->owner->so_lock);
1841 if (!(fmode & FMODE_READ))
1842 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1843 if (!(fmode & FMODE_WRITE))
1844 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1845 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1846 spin_unlock(&state->owner->so_lock);
1847}
1848
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001849static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001851 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 struct nfs_server *server = NFS_SERVER(calldata->inode);
1854
Trond Myklebust14516c32010-07-31 14:29:06 -04001855 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1856 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 /* hmm. we are done with the inode, and in the process of freeing
1858 * the state_owner. we keep this around to process errors
1859 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 switch (task->tk_status) {
1861 case 0:
Trond Myklebust45328c32007-07-26 17:47:34 -04001862 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001863 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001864 nfs4_close_clear_stateid_flags(state,
1865 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 break;
1867 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001868 case -NFS4ERR_OLD_STATEID:
1869 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001871 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001872 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001874 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1875 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001877 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001878 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879}
1880
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001881static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001883 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001884 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001885 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001886
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001887 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001888 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001889
Trond Myklebust88069f72009-12-08 08:33:16 -05001890 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1891 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001892 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001893 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001894 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001895 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001896 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1897 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1898 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001899 }
1900 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001901 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1902 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1903 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001904 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001905 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001906 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05001907
1908 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001909 /* Note: exit _without_ calling nfs4_close_done */
1910 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001911 return;
1912 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001913
1914 if (calldata->arg.fmode == 0)
1915 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
1916
Trond Myklebust516a6af2005-10-27 22:12:41 -04001917 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001918 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001919 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Andy Adamson19ddab02009-04-01 09:22:20 -04001920 &calldata->arg.seq_args, &calldata->res.seq_res,
1921 1, task))
1922 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001923 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924}
1925
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001926static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001927 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001928 .rpc_call_done = nfs4_close_done,
1929 .rpc_release = nfs4_free_closedata,
1930};
1931
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932/*
1933 * It is possible for data to be read/written from a mem-mapped file
1934 * after the sys_close call (which hits the vfs layer as a flush).
1935 * This means that we can't safely call nfsv4 close on a file until
1936 * the inode is cleared. This in turn means that we are not good
1937 * NFSv4 citizens - we do not indicate to the server to update the file's
1938 * share state even when we are done with one of the three share
1939 * stateid's in the inode.
1940 *
1941 * NOTE: Caller must be holding the sp->so_owner semaphore!
1942 */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001943int nfs4_do_close(struct path *path, struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001945 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001947 struct nfs4_state_owner *sp = state->owner;
1948 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001949 struct rpc_message msg = {
1950 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1951 .rpc_cred = state->owner->so_cred,
1952 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001953 struct rpc_task_setup task_setup_data = {
1954 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001955 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001956 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05001957 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001958 .flags = RPC_TASK_ASYNC,
1959 };
Trond Myklebust95121352005-10-18 14:20:12 -07001960 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001962 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001964 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001965 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001967 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04001968 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001970 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07001971 if (calldata->arg.seqid == NULL)
1972 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001973 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001974 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001975 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04001976 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001977 calldata->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04001978 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Al Virof6948692010-01-30 13:51:04 -05001979 path_get(path);
1980 calldata->path = *path;
Trond Myklebust95121352005-10-18 14:20:12 -07001981
Trond Myklebust5138fde2007-07-14 15:40:01 -04001982 msg.rpc_argp = &calldata->arg,
1983 msg.rpc_resp = &calldata->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001984 task_setup_data.callback_data = calldata;
1985 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001986 if (IS_ERR(task))
1987 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001988 status = 0;
1989 if (wait)
1990 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001991 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001992 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07001993out_free_calldata:
1994 kfree(calldata);
1995out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04001996 nfs4_put_open_state(state);
1997 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07001998 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999}
2000
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002001static 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 -07002002{
2003 struct file *filp;
Trond Myklebust1b45c462007-07-03 13:04:56 -04002004 int ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002005
Trond Myklebust1b45c462007-07-03 13:04:56 -04002006 /* If the open_intent is for execute, we have an extra check to make */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002007 if (fmode & FMODE_EXEC) {
Trond Myklebustaf22f942007-08-10 17:45:10 -04002008 ret = nfs_may_open(state->inode,
Trond Myklebust1b45c462007-07-03 13:04:56 -04002009 state->owner->so_cred,
2010 nd->intent.open.flags);
2011 if (ret < 0)
2012 goto out_close;
2013 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002014 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002015 if (!IS_ERR(filp)) {
2016 struct nfs_open_context *ctx;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04002017 ctx = nfs_file_open_context(filp);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002018 ctx->state = state;
Trond Myklebust95cf9592006-04-18 13:14:06 -04002019 return 0;
2020 }
Trond Myklebust1b45c462007-07-03 13:04:56 -04002021 ret = PTR_ERR(filp);
2022out_close:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002023 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
Trond Myklebust1b45c462007-07-03 13:04:56 -04002024 return ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002025}
2026
2027struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
2029{
Trond Myklebustad389da2007-06-05 12:30:00 -04002030 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002031 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002032 .dentry = dentry,
2033 };
Jan Blunck4ac91372008-02-14 19:34:32 -08002034 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 struct iattr attr;
2036 struct rpc_cred *cred;
2037 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002038 struct dentry *res;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002039 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
2041 if (nd->flags & LOOKUP_CREATE) {
2042 attr.ia_mode = nd->intent.open.create_mode;
2043 attr.ia_valid = ATTR_MODE;
2044 if (!IS_POSIXACL(dir))
Al Viroce3b0f82009-03-29 19:08:22 -04002045 attr.ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 } else {
2047 attr.ia_valid = 0;
2048 BUG_ON(nd->intent.open.flags & O_CREAT);
2049 }
2050
Trond Myklebust98a8e322008-03-12 12:25:28 -04002051 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07002053 return (struct dentry *)cred;
Trond Myklebust565277f2007-10-15 18:17:53 -04002054 parent = dentry->d_parent;
2055 /* Protect against concurrent sillydeletes */
2056 nfs_block_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002057 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002059 if (IS_ERR(state)) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04002060 if (PTR_ERR(state) == -ENOENT) {
Trond Myklebust02a913a2005-10-18 14:20:17 -07002061 d_add(dentry, NULL);
Trond Myklebustd75340c2007-10-01 21:42:01 -04002062 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2063 }
Trond Myklebust565277f2007-10-15 18:17:53 -04002064 nfs_unblock_sillyrename(parent);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002065 return (struct dentry *)state;
2066 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002067 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07002068 if (res != NULL)
Trond Myklebustdeee9362007-08-27 11:33:00 -04002069 path.dentry = res;
Trond Myklebustd75340c2007-10-01 21:42:01 -04002070 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
Trond Myklebust565277f2007-10-15 18:17:53 -04002071 nfs_unblock_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002072 nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002073 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074}
2075
2076int
Trond Myklebust02a913a2005-10-18 14:20:17 -07002077nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
Trond Myklebustad389da2007-06-05 12:30:00 -04002079 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002080 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002081 .dentry = dentry,
2082 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 struct rpc_cred *cred;
2084 struct nfs4_state *state;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002085 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
Trond Myklebust98a8e322008-03-12 12:25:28 -04002087 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 if (IS_ERR(cred))
2089 return PTR_ERR(cred);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002090 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002092 if (IS_ERR(state)) {
2093 switch (PTR_ERR(state)) {
2094 case -EPERM:
2095 case -EACCES:
2096 case -EDQUOT:
2097 case -ENOSPC:
2098 case -EROFS:
Al Viro04287f92010-04-08 00:06:07 +01002099 return PTR_ERR(state);
Chuck Leverb87c0ad2006-10-19 23:28:42 -07002100 default:
2101 goto out_drop;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002102 }
Trond Myklebust02a913a2005-10-18 14:20:17 -07002103 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002104 if (state->inode == dentry->d_inode) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04002105 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002106 nfs4_intent_set_file(nd, &path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 return 1;
2108 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002109 nfs4_close_sync(&path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002110out_drop:
2111 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 return 0;
2113}
2114
Trond Myklebust1185a552009-12-03 15:54:02 -05002115static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002116{
2117 if (ctx->state == NULL)
2118 return;
2119 if (is_sync)
2120 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2121 else
2122 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2123}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
2125static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2126{
Benny Halevy43652ad2009-04-01 09:21:54 -04002127 struct nfs4_server_caps_arg args = {
2128 .fhandle = fhandle,
2129 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 struct nfs4_server_caps_res res = {};
2131 struct rpc_message msg = {
2132 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002133 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 .rpc_resp = &res,
2135 };
2136 int status;
2137
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002138 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 if (status == 0) {
2140 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002141 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2142 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2143 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2144 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2145 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2147 server->caps |= NFS_CAP_ACLS;
2148 if (res.has_links != 0)
2149 server->caps |= NFS_CAP_HARDLINKS;
2150 if (res.has_symlinks != 0)
2151 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002152 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2153 server->caps |= NFS_CAP_FILEID;
2154 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2155 server->caps |= NFS_CAP_MODE;
2156 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2157 server->caps |= NFS_CAP_NLINK;
2158 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2159 server->caps |= NFS_CAP_OWNER;
2160 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2161 server->caps |= NFS_CAP_OWNER_GROUP;
2162 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2163 server->caps |= NFS_CAP_ATIME;
2164 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2165 server->caps |= NFS_CAP_CTIME;
2166 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2167 server->caps |= NFS_CAP_MTIME;
2168
Trond Myklebusta65318b2009-03-11 14:10:28 -04002169 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2170 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2171 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 server->acl_bitmask = res.acl_bitmask;
2173 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002174
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 return status;
2176}
2177
Trond Myklebust55a97592006-06-09 09:34:19 -04002178int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
2180 struct nfs4_exception exception = { };
2181 int err;
2182 do {
2183 err = nfs4_handle_exception(server,
2184 _nfs4_server_capabilities(server, fhandle),
2185 &exception);
2186 } while (exception.retry);
2187 return err;
2188}
2189
2190static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2191 struct nfs_fsinfo *info)
2192{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 struct nfs4_lookup_root_arg args = {
2194 .bitmask = nfs4_fattr_bitmap,
2195 };
2196 struct nfs4_lookup_res res = {
2197 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002198 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 .fh = fhandle,
2200 };
2201 struct rpc_message msg = {
2202 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2203 .rpc_argp = &args,
2204 .rpc_resp = &res,
2205 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002206
Trond Myklebust0e574af2005-10-27 22:12:38 -04002207 nfs_fattr_init(info->fattr);
Trond Myklebustfccba802009-07-21 16:48:07 -04002208 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209}
2210
2211static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2212 struct nfs_fsinfo *info)
2213{
2214 struct nfs4_exception exception = { };
2215 int err;
2216 do {
2217 err = nfs4_handle_exception(server,
2218 _nfs4_lookup_root(server, fhandle, info),
2219 &exception);
2220 } while (exception.retry);
2221 return err;
2222}
2223
David Howells54ceac42006-08-22 20:06:13 -04002224/*
2225 * get the file handle for the "/" directory on the server
2226 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002228 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 int status;
2231
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 if (status == 0)
2234 status = nfs4_server_capabilities(server, fhandle);
2235 if (status == 0)
2236 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002237 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238}
2239
Manoj Naik6b97fd32006-06-09 09:34:29 -04002240/*
2241 * Get locations and (maybe) other attributes of a referral.
2242 * Note that we'll actually follow the referral later when
2243 * we detect fsid mismatch in inode revalidation
2244 */
Trond Myklebust56659e92007-07-17 21:52:39 -04002245static 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 -04002246{
2247 int status = -ENOMEM;
2248 struct page *page = NULL;
2249 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002250
2251 page = alloc_page(GFP_KERNEL);
2252 if (page == NULL)
2253 goto out;
2254 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2255 if (locations == NULL)
2256 goto out;
2257
Trond Myklebustc228fd32007-01-13 02:28:11 -05002258 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002259 if (status != 0)
2260 goto out;
2261 /* Make sure server returned a different fsid for the referral */
2262 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07002263 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 -04002264 status = -EIO;
2265 goto out;
2266 }
2267
2268 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2269 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2270 if (!fattr->mode)
2271 fattr->mode = S_IFDIR;
2272 memset(fhandle, 0, sizeof(struct nfs_fh));
2273out:
2274 if (page)
2275 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002276 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002277 return status;
2278}
2279
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2281{
2282 struct nfs4_getattr_arg args = {
2283 .fh = fhandle,
2284 .bitmask = server->attr_bitmask,
2285 };
2286 struct nfs4_getattr_res res = {
2287 .fattr = fattr,
2288 .server = server,
2289 };
2290 struct rpc_message msg = {
2291 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2292 .rpc_argp = &args,
2293 .rpc_resp = &res,
2294 };
2295
Trond Myklebust0e574af2005-10-27 22:12:38 -04002296 nfs_fattr_init(fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002297 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298}
2299
2300static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2301{
2302 struct nfs4_exception exception = { };
2303 int err;
2304 do {
2305 err = nfs4_handle_exception(server,
2306 _nfs4_proc_getattr(server, fhandle, fattr),
2307 &exception);
2308 } while (exception.retry);
2309 return err;
2310}
2311
2312/*
2313 * The file is not closed if it is opened due to the a request to change
2314 * the size of the file. The open call will not be needed once the
2315 * VFS layer lookup-intents are implemented.
2316 *
2317 * Close is called when the inode is destroyed.
2318 * If we haven't opened the file for O_WRONLY, we
2319 * need to in the size_change case to obtain a stateid.
2320 *
2321 * Got race?
2322 * Because OPEN is always done by name in nfsv4, it is
2323 * possible that we opened a different file by the same
2324 * name. We can recognize this race condition, but we
2325 * can't do anything about it besides returning an error.
2326 *
2327 * This will be fixed with VFS changes (lookup-intent).
2328 */
2329static int
2330nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2331 struct iattr *sattr)
2332{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002333 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002334 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002335 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 int status;
2337
Trond Myklebust0e574af2005-10-27 22:12:38 -04002338 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
Trond Myklebustd5308382005-11-04 15:33:38 -05002340 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002341 if (sattr->ia_valid & ATTR_FILE) {
2342 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002343
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002344 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002345 if (ctx) {
2346 cred = ctx->cred;
2347 state = ctx->state;
2348 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002349 }
2350
2351 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002352 if (status == 0)
2353 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 return status;
2355}
2356
Trond Myklebust56659e92007-07-17 21:52:39 -04002357static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2358 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04002359 struct nfs_fattr *fattr)
2360{
2361 int status;
2362 struct nfs4_lookup_arg args = {
2363 .bitmask = server->attr_bitmask,
2364 .dir_fh = dirfh,
2365 .name = name,
2366 };
2367 struct nfs4_lookup_res res = {
2368 .server = server,
2369 .fattr = fattr,
2370 .fh = fhandle,
2371 };
2372 struct rpc_message msg = {
2373 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2374 .rpc_argp = &args,
2375 .rpc_resp = &res,
2376 };
2377
2378 nfs_fattr_init(fattr);
2379
2380 dprintk("NFS call lookupfh %s\n", name->name);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002381 status = nfs4_call_sync(server, &msg, &args, &res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002382 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002383 return status;
2384}
2385
2386static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2387 struct qstr *name, struct nfs_fh *fhandle,
2388 struct nfs_fattr *fattr)
2389{
2390 struct nfs4_exception exception = { };
2391 int err;
2392 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04002393 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2394 /* FIXME: !!!! */
2395 if (err == -NFS4ERR_MOVED) {
2396 err = -EREMOTE;
2397 break;
2398 }
2399 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002400 } while (exception.retry);
2401 return err;
2402}
2403
Trond Myklebust56659e92007-07-17 21:52:39 -04002404static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2406{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002407 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408
2409 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002410 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002411 if (status == -NFS4ERR_MOVED)
2412 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 dprintk("NFS reply lookup: %d\n", status);
2414 return status;
2415}
2416
2417static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2418{
2419 struct nfs4_exception exception = { };
2420 int err;
2421 do {
2422 err = nfs4_handle_exception(NFS_SERVER(dir),
2423 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2424 &exception);
2425 } while (exception.retry);
2426 return err;
2427}
2428
2429static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2430{
Trond Myklebust76b32992007-08-10 17:45:11 -04002431 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 struct nfs4_accessargs args = {
2433 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002434 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002436 struct nfs4_accessres res = {
2437 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002438 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 struct rpc_message msg = {
2440 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2441 .rpc_argp = &args,
2442 .rpc_resp = &res,
2443 .rpc_cred = entry->cred,
2444 };
2445 int mode = entry->mask;
2446 int status;
2447
2448 /*
2449 * Determine which access bits we want to ask for...
2450 */
2451 if (mode & MAY_READ)
2452 args.access |= NFS4_ACCESS_READ;
2453 if (S_ISDIR(inode->i_mode)) {
2454 if (mode & MAY_WRITE)
2455 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2456 if (mode & MAY_EXEC)
2457 args.access |= NFS4_ACCESS_LOOKUP;
2458 } else {
2459 if (mode & MAY_WRITE)
2460 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2461 if (mode & MAY_EXEC)
2462 args.access |= NFS4_ACCESS_EXECUTE;
2463 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002464
2465 res.fattr = nfs_alloc_fattr();
2466 if (res.fattr == NULL)
2467 return -ENOMEM;
2468
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002469 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 if (!status) {
2471 entry->mask = 0;
2472 if (res.access & NFS4_ACCESS_READ)
2473 entry->mask |= MAY_READ;
2474 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2475 entry->mask |= MAY_WRITE;
2476 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2477 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002478 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002480 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 return status;
2482}
2483
2484static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2485{
2486 struct nfs4_exception exception = { };
2487 int err;
2488 do {
2489 err = nfs4_handle_exception(NFS_SERVER(inode),
2490 _nfs4_proc_access(inode, entry),
2491 &exception);
2492 } while (exception.retry);
2493 return err;
2494}
2495
2496/*
2497 * TODO: For the time being, we don't try to get any attributes
2498 * along with any of the zero-copy operations READ, READDIR,
2499 * READLINK, WRITE.
2500 *
2501 * In the case of the first three, we want to put the GETATTR
2502 * after the read-type operation -- this is because it is hard
2503 * to predict the length of a GETATTR response in v4, and thus
2504 * align the READ data correctly. This means that the GETATTR
2505 * may end up partially falling into the page cache, and we should
2506 * shift it into the 'tail' of the xdr_buf before processing.
2507 * To do this efficiently, we need to know the total length
2508 * of data received, which doesn't seem to be available outside
2509 * of the RPC layer.
2510 *
2511 * In the case of WRITE, we also want to put the GETATTR after
2512 * the operation -- in this case because we want to make sure
2513 * we get the post-operation mtime and size. This means that
2514 * we can't use xdr_encode_pages() as written: we need a variant
2515 * of it which would leave room in the 'tail' iovec.
2516 *
2517 * Both of these changes to the XDR layer would in fact be quite
2518 * minor, but I decided to leave them for a subsequent patch.
2519 */
2520static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2521 unsigned int pgbase, unsigned int pglen)
2522{
2523 struct nfs4_readlink args = {
2524 .fh = NFS_FH(inode),
2525 .pgbase = pgbase,
2526 .pglen = pglen,
2527 .pages = &page,
2528 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002529 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 struct rpc_message msg = {
2531 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2532 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002533 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 };
2535
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002536 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537}
2538
2539static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2540 unsigned int pgbase, unsigned int pglen)
2541{
2542 struct nfs4_exception exception = { };
2543 int err;
2544 do {
2545 err = nfs4_handle_exception(NFS_SERVER(inode),
2546 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2547 &exception);
2548 } while (exception.retry);
2549 return err;
2550}
2551
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552/*
2553 * Got race?
2554 * We will need to arrange for the VFS layer to provide an atomic open.
2555 * Until then, this create/open method is prone to inefficiency and race
2556 * conditions due to the lookup, create, and open VFS calls from sys_open()
2557 * placed on the wire.
2558 *
2559 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2560 * The file will be opened again in the subsequent VFS open call
2561 * (nfs4_proc_file_open).
2562 *
2563 * The open for read will just hang around to be used by any process that
2564 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2565 */
2566
2567static int
2568nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07002569 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570{
Trond Myklebustad389da2007-06-05 12:30:00 -04002571 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002572 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002573 .dentry = dentry,
2574 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 struct nfs4_state *state;
2576 struct rpc_cred *cred;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002577 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 int status = 0;
2579
Trond Myklebust98a8e322008-03-12 12:25:28 -04002580 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 if (IS_ERR(cred)) {
2582 status = PTR_ERR(cred);
2583 goto out;
2584 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002585 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002586 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 if (IS_ERR(state)) {
2588 status = PTR_ERR(state);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002589 goto out_putcred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002591 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002592 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustad389da2007-06-05 12:30:00 -04002593 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002594 status = nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002595 else
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002596 nfs4_close_sync(&path, state, fmode);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002597out_putcred:
2598 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599out:
2600 return status;
2601}
2602
2603static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2604{
Trond Myklebust16e42952005-10-27 22:12:44 -04002605 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002606 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002608 .name.len = name->len,
2609 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002610 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002612 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002613 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002614 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002616 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2617 .rpc_argp = &args,
2618 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002620 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
Trond Myklebustd3468902010-04-16 16:22:50 -04002622 res.dir_attr = nfs_alloc_fattr();
2623 if (res.dir_attr == NULL)
2624 goto out;
2625
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002626 status = nfs4_call_sync(server, &msg, &args, &res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002627 if (status == 0) {
2628 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002629 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002630 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002631 nfs_free_fattr(res.dir_attr);
2632out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 return status;
2634}
2635
2636static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2637{
2638 struct nfs4_exception exception = { };
2639 int err;
2640 do {
2641 err = nfs4_handle_exception(NFS_SERVER(dir),
2642 _nfs4_proc_remove(dir, name),
2643 &exception);
2644 } while (exception.retry);
2645 return err;
2646}
2647
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002648static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002650 struct nfs_server *server = NFS_SERVER(dir);
2651 struct nfs_removeargs *args = msg->rpc_argp;
2652 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
Trond Myklebusta65318b2009-03-11 14:10:28 -04002654 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002655 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657}
2658
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002659static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002661 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2662
Trond Myklebust14516c32010-07-31 14:29:06 -04002663 if (!nfs4_sequence_done(task, &res->seq_res))
2664 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002665 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002666 return 0;
2667 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002668 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002669 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670}
2671
2672static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2673 struct inode *new_dir, struct qstr *new_name)
2674{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002675 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 struct nfs4_rename_arg arg = {
2677 .old_dir = NFS_FH(old_dir),
2678 .new_dir = NFS_FH(new_dir),
2679 .old_name = old_name,
2680 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002681 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002683 struct nfs4_rename_res res = {
2684 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002685 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 struct rpc_message msg = {
2687 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2688 .rpc_argp = &arg,
2689 .rpc_resp = &res,
2690 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002691 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692
Trond Myklebust011fff72010-04-16 16:22:49 -04002693 res.old_fattr = nfs_alloc_fattr();
2694 res.new_fattr = nfs_alloc_fattr();
2695 if (res.old_fattr == NULL || res.new_fattr == NULL)
2696 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697
Trond Myklebust011fff72010-04-16 16:22:49 -04002698 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 if (!status) {
2700 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002701 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002703 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002705out:
2706 nfs_free_fattr(res.new_fattr);
2707 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 return status;
2709}
2710
2711static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2712 struct inode *new_dir, struct qstr *new_name)
2713{
2714 struct nfs4_exception exception = { };
2715 int err;
2716 do {
2717 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2718 _nfs4_proc_rename(old_dir, old_name,
2719 new_dir, new_name),
2720 &exception);
2721 } while (exception.retry);
2722 return err;
2723}
2724
2725static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2726{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002727 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 struct nfs4_link_arg arg = {
2729 .fh = NFS_FH(inode),
2730 .dir_fh = NFS_FH(dir),
2731 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002732 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002734 struct nfs4_link_res res = {
2735 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002736 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 struct rpc_message msg = {
2738 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2739 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002740 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002742 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
Trond Myklebust136f2622010-04-16 16:22:49 -04002744 res.fattr = nfs_alloc_fattr();
2745 res.dir_attr = nfs_alloc_fattr();
2746 if (res.fattr == NULL || res.dir_attr == NULL)
2747 goto out;
2748
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002749 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002750 if (!status) {
2751 update_changeattr(dir, &res.cinfo);
2752 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002753 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002754 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002755out:
2756 nfs_free_fattr(res.dir_attr);
2757 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 return status;
2759}
2760
2761static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2762{
2763 struct nfs4_exception exception = { };
2764 int err;
2765 do {
2766 err = nfs4_handle_exception(NFS_SERVER(inode),
2767 _nfs4_proc_link(inode, dir, name),
2768 &exception);
2769 } while (exception.retry);
2770 return err;
2771}
2772
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002773struct nfs4_createdata {
2774 struct rpc_message msg;
2775 struct nfs4_create_arg arg;
2776 struct nfs4_create_res res;
2777 struct nfs_fh fh;
2778 struct nfs_fattr fattr;
2779 struct nfs_fattr dir_fattr;
2780};
2781
2782static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2783 struct qstr *name, struct iattr *sattr, u32 ftype)
2784{
2785 struct nfs4_createdata *data;
2786
2787 data = kzalloc(sizeof(*data), GFP_KERNEL);
2788 if (data != NULL) {
2789 struct nfs_server *server = NFS_SERVER(dir);
2790
2791 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2792 data->msg.rpc_argp = &data->arg;
2793 data->msg.rpc_resp = &data->res;
2794 data->arg.dir_fh = NFS_FH(dir);
2795 data->arg.server = server;
2796 data->arg.name = name;
2797 data->arg.attrs = sattr;
2798 data->arg.ftype = ftype;
2799 data->arg.bitmask = server->attr_bitmask;
2800 data->res.server = server;
2801 data->res.fh = &data->fh;
2802 data->res.fattr = &data->fattr;
2803 data->res.dir_fattr = &data->dir_fattr;
2804 nfs_fattr_init(data->res.fattr);
2805 nfs_fattr_init(data->res.dir_fattr);
2806 }
2807 return data;
2808}
2809
2810static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2811{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002812 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2813 &data->arg, &data->res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002814 if (status == 0) {
2815 update_changeattr(dir, &data->res.dir_cinfo);
2816 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2817 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2818 }
2819 return status;
2820}
2821
2822static void nfs4_free_createdata(struct nfs4_createdata *data)
2823{
2824 kfree(data);
2825}
2826
Chuck Lever4f390c12006-08-22 20:06:22 -04002827static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002828 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002830 struct nfs4_createdata *data;
2831 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
Chuck Lever94a6d752006-08-22 20:06:23 -04002833 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002834 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002835
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002836 status = -ENOMEM;
2837 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2838 if (data == NULL)
2839 goto out;
2840
2841 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2842 data->arg.u.symlink.pages = &page;
2843 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002845 status = nfs4_do_create(dir, dentry, data);
2846
2847 nfs4_free_createdata(data);
2848out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 return status;
2850}
2851
Chuck Lever4f390c12006-08-22 20:06:22 -04002852static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002853 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854{
2855 struct nfs4_exception exception = { };
2856 int err;
2857 do {
2858 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002859 _nfs4_proc_symlink(dir, dentry, page,
2860 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 &exception);
2862 } while (exception.retry);
2863 return err;
2864}
2865
2866static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2867 struct iattr *sattr)
2868{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002869 struct nfs4_createdata *data;
2870 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002872 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2873 if (data == NULL)
2874 goto out;
2875
2876 status = nfs4_do_create(dir, dentry, data);
2877
2878 nfs4_free_createdata(data);
2879out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 return status;
2881}
2882
2883static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2884 struct iattr *sattr)
2885{
2886 struct nfs4_exception exception = { };
2887 int err;
2888 do {
2889 err = nfs4_handle_exception(NFS_SERVER(dir),
2890 _nfs4_proc_mkdir(dir, dentry, sattr),
2891 &exception);
2892 } while (exception.retry);
2893 return err;
2894}
2895
2896static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2897 u64 cookie, struct page *page, unsigned int count, int plus)
2898{
2899 struct inode *dir = dentry->d_inode;
2900 struct nfs4_readdir_arg args = {
2901 .fh = NFS_FH(dir),
2902 .pages = &page,
2903 .pgbase = 0,
2904 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002905 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 };
2907 struct nfs4_readdir_res res;
2908 struct rpc_message msg = {
2909 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2910 .rpc_argp = &args,
2911 .rpc_resp = &res,
2912 .rpc_cred = cred,
2913 };
2914 int status;
2915
Harvey Harrison3110ff82008-05-02 13:42:44 -07002916 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002917 dentry->d_parent->d_name.name,
2918 dentry->d_name.name,
2919 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2921 res.pgbase = args.pgbase;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002922 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 if (status == 0)
2924 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustc4812992007-09-28 17:11:45 -04002925
2926 nfs_invalidate_atime(dir);
2927
Harvey Harrison3110ff82008-05-02 13:42:44 -07002928 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 return status;
2930}
2931
2932static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2933 u64 cookie, struct page *page, unsigned int count, int plus)
2934{
2935 struct nfs4_exception exception = { };
2936 int err;
2937 do {
2938 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2939 _nfs4_proc_readdir(dentry, cred, cookie,
2940 page, count, plus),
2941 &exception);
2942 } while (exception.retry);
2943 return err;
2944}
2945
2946static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2947 struct iattr *sattr, dev_t rdev)
2948{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002949 struct nfs4_createdata *data;
2950 int mode = sattr->ia_mode;
2951 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
2953 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2954 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002955
2956 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2957 if (data == NULL)
2958 goto out;
2959
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002961 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002963 data->arg.ftype = NF4BLK;
2964 data->arg.u.device.specdata1 = MAJOR(rdev);
2965 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 }
2967 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002968 data->arg.ftype = NF4CHR;
2969 data->arg.u.device.specdata1 = MAJOR(rdev);
2970 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002973 status = nfs4_do_create(dir, dentry, data);
2974
2975 nfs4_free_createdata(data);
2976out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 return status;
2978}
2979
2980static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2981 struct iattr *sattr, dev_t rdev)
2982{
2983 struct nfs4_exception exception = { };
2984 int err;
2985 do {
2986 err = nfs4_handle_exception(NFS_SERVER(dir),
2987 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2988 &exception);
2989 } while (exception.retry);
2990 return err;
2991}
2992
2993static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2994 struct nfs_fsstat *fsstat)
2995{
2996 struct nfs4_statfs_arg args = {
2997 .fh = fhandle,
2998 .bitmask = server->attr_bitmask,
2999 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003000 struct nfs4_statfs_res res = {
3001 .fsstat = fsstat,
3002 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 struct rpc_message msg = {
3004 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3005 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003006 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 };
3008
Trond Myklebust0e574af2005-10-27 22:12:38 -04003009 nfs_fattr_init(fsstat->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003010 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011}
3012
3013static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3014{
3015 struct nfs4_exception exception = { };
3016 int err;
3017 do {
3018 err = nfs4_handle_exception(server,
3019 _nfs4_proc_statfs(server, fhandle, fsstat),
3020 &exception);
3021 } while (exception.retry);
3022 return err;
3023}
3024
3025static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3026 struct nfs_fsinfo *fsinfo)
3027{
3028 struct nfs4_fsinfo_arg args = {
3029 .fh = fhandle,
3030 .bitmask = server->attr_bitmask,
3031 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003032 struct nfs4_fsinfo_res res = {
3033 .fsinfo = fsinfo,
3034 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 struct rpc_message msg = {
3036 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3037 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003038 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 };
3040
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003041 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042}
3043
3044static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3045{
3046 struct nfs4_exception exception = { };
3047 int err;
3048
3049 do {
3050 err = nfs4_handle_exception(server,
3051 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3052 &exception);
3053 } while (exception.retry);
3054 return err;
3055}
3056
3057static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3058{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003059 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3061}
3062
3063static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3064 struct nfs_pathconf *pathconf)
3065{
3066 struct nfs4_pathconf_arg args = {
3067 .fh = fhandle,
3068 .bitmask = server->attr_bitmask,
3069 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003070 struct nfs4_pathconf_res res = {
3071 .pathconf = pathconf,
3072 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 struct rpc_message msg = {
3074 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3075 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003076 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 };
3078
3079 /* None of the pathconf attributes are mandatory to implement */
3080 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3081 memset(pathconf, 0, sizeof(*pathconf));
3082 return 0;
3083 }
3084
Trond Myklebust0e574af2005-10-27 22:12:38 -04003085 nfs_fattr_init(pathconf->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003086 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087}
3088
3089static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3090 struct nfs_pathconf *pathconf)
3091{
3092 struct nfs4_exception exception = { };
3093 int err;
3094
3095 do {
3096 err = nfs4_handle_exception(server,
3097 _nfs4_proc_pathconf(server, fhandle, pathconf),
3098 &exception);
3099 } while (exception.retry);
3100 return err;
3101}
3102
Trond Myklebustec06c092006-03-20 13:44:27 -05003103static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104{
Trond Myklebustec06c092006-03-20 13:44:27 -05003105 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003107 dprintk("--> %s\n", __func__);
3108
Trond Myklebust14516c32010-07-31 14:29:06 -04003109 if (!nfs4_sequence_done(task, &data->res.seq_res))
3110 return -EAGAIN;
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003111
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003112 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003113 nfs_restart_rpc(task, server->nfs_client);
Trond Myklebustec06c092006-03-20 13:44:27 -05003114 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003116
3117 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003119 renew_lease(server, data->timestamp);
3120 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121}
3122
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003123static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 data->timestamp = jiffies;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003126 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127}
3128
Trond Myklebust788e7a82006-03-20 13:44:27 -05003129static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 struct inode *inode = data->inode;
3132
Trond Myklebust14516c32010-07-31 14:29:06 -04003133 if (!nfs4_sequence_done(task, &data->res.seq_res))
3134 return -EAGAIN;
Andy Adamsondef6ed72009-04-01 09:22:26 -04003135
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003136 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003137 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003138 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003140 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003142 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003143 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003144 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145}
3146
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003147static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003149 struct nfs_server *server = NFS_SERVER(data->inode);
3150
Trond Myklebusta65318b2009-03-11 14:10:28 -04003151 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003152 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 data->timestamp = jiffies;
3154
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003155 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156}
3157
Trond Myklebust788e7a82006-03-20 13:44:27 -05003158static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 struct inode *inode = data->inode;
3161
Trond Myklebust14516c32010-07-31 14:29:06 -04003162 if (!nfs4_sequence_done(task, &data->res.seq_res))
3163 return -EAGAIN;
3164
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003165 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003166 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003167 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003169 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003170 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171}
3172
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003173static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003175 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176
Trond Myklebusta65318b2009-03-11 14:10:28 -04003177 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003178 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003179 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180}
3181
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003182struct nfs4_renewdata {
3183 struct nfs_client *client;
3184 unsigned long timestamp;
3185};
3186
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187/*
3188 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3189 * standalone procedure for queueing an asynchronous RENEW.
3190 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003191static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003192{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003193 struct nfs4_renewdata *data = calldata;
3194 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003195
Alexandros Batsakis0851de02010-02-05 03:45:06 -08003196 if (atomic_read(&clp->cl_count) > 1)
3197 nfs4_schedule_state_renewal(clp);
3198 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003199 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003200}
3201
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003202static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003204 struct nfs4_renewdata *data = calldata;
3205 struct nfs_client *clp = data->client;
3206 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207
3208 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003209 /* Unless we're shutting down, schedule state recovery! */
3210 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3211 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 return;
3213 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003214 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215}
3216
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003217static const struct rpc_call_ops nfs4_renew_ops = {
3218 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003219 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003220};
3221
David Howellsadfa6f92006-08-22 20:06:08 -04003222int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223{
3224 struct rpc_message msg = {
3225 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3226 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003227 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003229 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230
Alexandros Batsakis0851de02010-02-05 03:45:06 -08003231 if (!atomic_inc_not_zero(&clp->cl_count))
3232 return -EIO;
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003233 data = kmalloc(sizeof(*data), GFP_KERNEL);
3234 if (data == NULL)
3235 return -ENOMEM;
3236 data->client = clp;
3237 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003239 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240}
3241
David Howellsadfa6f92006-08-22 20:06:08 -04003242int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243{
3244 struct rpc_message msg = {
3245 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3246 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003247 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 };
3249 unsigned long now = jiffies;
3250 int status;
3251
3252 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3253 if (status < 0)
3254 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003255 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 return 0;
3257}
3258
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003259static inline int nfs4_server_supports_acls(struct nfs_server *server)
3260{
3261 return (server->caps & NFS_CAP_ACLS)
3262 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3263 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3264}
3265
3266/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3267 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3268 * the stack.
3269 */
3270#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3271
3272static void buf_to_pages(const void *buf, size_t buflen,
3273 struct page **pages, unsigned int *pgbase)
3274{
3275 const void *p = buf;
3276
3277 *pgbase = offset_in_page(buf);
3278 p -= *pgbase;
3279 while (p < buf + buflen) {
3280 *(pages++) = virt_to_page(p);
3281 p += PAGE_CACHE_SIZE;
3282 }
3283}
3284
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003285struct nfs4_cached_acl {
3286 int cached;
3287 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003288 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003289};
3290
3291static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003292{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003293 struct nfs_inode *nfsi = NFS_I(inode);
3294
3295 spin_lock(&inode->i_lock);
3296 kfree(nfsi->nfs4_acl);
3297 nfsi->nfs4_acl = acl;
3298 spin_unlock(&inode->i_lock);
3299}
3300
3301static void nfs4_zap_acl_attr(struct inode *inode)
3302{
3303 nfs4_set_cached_acl(inode, NULL);
3304}
3305
3306static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3307{
3308 struct nfs_inode *nfsi = NFS_I(inode);
3309 struct nfs4_cached_acl *acl;
3310 int ret = -ENOENT;
3311
3312 spin_lock(&inode->i_lock);
3313 acl = nfsi->nfs4_acl;
3314 if (acl == NULL)
3315 goto out;
3316 if (buf == NULL) /* user is just asking for length */
3317 goto out_len;
3318 if (acl->cached == 0)
3319 goto out;
3320 ret = -ERANGE; /* see getxattr(2) man page */
3321 if (acl->len > buflen)
3322 goto out;
3323 memcpy(buf, acl->data, acl->len);
3324out_len:
3325 ret = acl->len;
3326out:
3327 spin_unlock(&inode->i_lock);
3328 return ret;
3329}
3330
3331static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3332{
3333 struct nfs4_cached_acl *acl;
3334
3335 if (buf && acl_len <= PAGE_SIZE) {
3336 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3337 if (acl == NULL)
3338 goto out;
3339 acl->cached = 1;
3340 memcpy(acl->data, buf, acl_len);
3341 } else {
3342 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3343 if (acl == NULL)
3344 goto out;
3345 acl->cached = 0;
3346 }
3347 acl->len = acl_len;
3348out:
3349 nfs4_set_cached_acl(inode, acl);
3350}
3351
Trond Myklebust16b42892006-08-24 12:27:15 -04003352static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003353{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003354 struct page *pages[NFS4ACL_MAXPAGES];
3355 struct nfs_getaclargs args = {
3356 .fh = NFS_FH(inode),
3357 .acl_pages = pages,
3358 .acl_len = buflen,
3359 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003360 struct nfs_getaclres res = {
3361 .acl_len = buflen,
3362 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003363 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003364 struct rpc_message msg = {
3365 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3366 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003367 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003368 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003369 struct page *localpage = NULL;
3370 int ret;
3371
3372 if (buflen < PAGE_SIZE) {
3373 /* As long as we're doing a round trip to the server anyway,
3374 * let's be prepared for a page of acl data. */
3375 localpage = alloc_page(GFP_KERNEL);
3376 resp_buf = page_address(localpage);
3377 if (localpage == NULL)
3378 return -ENOMEM;
3379 args.acl_pages[0] = localpage;
3380 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003381 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003382 } else {
3383 resp_buf = buf;
3384 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3385 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003386 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003387 if (ret)
3388 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003389 if (res.acl_len > args.acl_len)
3390 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003391 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003392 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003393 if (buf) {
3394 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003395 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003396 goto out_free;
3397 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003398 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003399 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003400 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003401out_free:
3402 if (localpage)
3403 __free_page(localpage);
3404 return ret;
3405}
3406
Trond Myklebust16b42892006-08-24 12:27:15 -04003407static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3408{
3409 struct nfs4_exception exception = { };
3410 ssize_t ret;
3411 do {
3412 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3413 if (ret >= 0)
3414 break;
3415 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3416 } while (exception.retry);
3417 return ret;
3418}
3419
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003420static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3421{
3422 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003423 int ret;
3424
3425 if (!nfs4_server_supports_acls(server))
3426 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003427 ret = nfs_revalidate_inode(server, inode);
3428 if (ret < 0)
3429 return ret;
3430 ret = nfs4_read_cached_acl(inode, buf, buflen);
3431 if (ret != -ENOENT)
3432 return ret;
3433 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003434}
3435
Trond Myklebust16b42892006-08-24 12:27:15 -04003436static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003437{
3438 struct nfs_server *server = NFS_SERVER(inode);
3439 struct page *pages[NFS4ACL_MAXPAGES];
3440 struct nfs_setaclargs arg = {
3441 .fh = NFS_FH(inode),
3442 .acl_pages = pages,
3443 .acl_len = buflen,
3444 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003445 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003446 struct rpc_message msg = {
3447 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3448 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003449 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003450 };
3451 int ret;
3452
3453 if (!nfs4_server_supports_acls(server))
3454 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07003455 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003456 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003457 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003458 nfs_access_zap_cache(inode);
3459 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003460 return ret;
3461}
3462
Trond Myklebust16b42892006-08-24 12:27:15 -04003463static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3464{
3465 struct nfs4_exception exception = { };
3466 int err;
3467 do {
3468 err = nfs4_handle_exception(NFS_SERVER(inode),
3469 __nfs4_proc_set_acl(inode, buf, buflen),
3470 &exception);
3471 } while (exception.retry);
3472 return err;
3473}
3474
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003476nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003478 struct nfs_client *clp = server->nfs_client;
3479
3480 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 return 0;
3482 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003483 case -NFS4ERR_ADMIN_REVOKED:
3484 case -NFS4ERR_BAD_STATEID:
3485 case -NFS4ERR_OPENMODE:
3486 if (state == NULL)
3487 break;
3488 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003489 goto do_state_recovery;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003491 if (state == NULL)
3492 break;
3493 nfs4_state_mark_reclaim_reboot(clp, state);
3494 case -NFS4ERR_STALE_CLIENTID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 case -NFS4ERR_EXPIRED:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003496 goto do_state_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003497#if defined(CONFIG_NFS_V4_1)
3498 case -NFS4ERR_BADSESSION:
3499 case -NFS4ERR_BADSLOT:
3500 case -NFS4ERR_BAD_HIGH_SLOT:
3501 case -NFS4ERR_DEADSESSION:
3502 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3503 case -NFS4ERR_SEQ_FALSE_RETRY:
3504 case -NFS4ERR_SEQ_MISORDERED:
3505 dprintk("%s ERROR %d, Reset session\n", __func__,
3506 task->tk_status);
Andy Adamson0b9e2d42009-12-04 16:02:14 -05003507 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -04003508 task->tk_status = 0;
3509 return -EAGAIN;
3510#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003512 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003513 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003514 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3516 task->tk_status = 0;
3517 return -EAGAIN;
3518 case -NFS4ERR_OLD_STATEID:
3519 task->tk_status = 0;
3520 return -EAGAIN;
3521 }
3522 task->tk_status = nfs4_map_errors(task->tk_status);
3523 return 0;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003524do_state_recovery:
3525 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3526 nfs4_schedule_state_recovery(clp);
3527 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3528 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3529 task->tk_status = 0;
3530 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531}
3532
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003533int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3534 unsigned short port, struct rpc_cred *cred,
3535 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536{
3537 nfs4_verifier sc_verifier;
3538 struct nfs4_setclientid setclientid = {
3539 .sc_verifier = &sc_verifier,
3540 .sc_prog = program,
3541 };
3542 struct rpc_message msg = {
3543 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3544 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003545 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003546 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 };
Al Virobc4785c2006-10-19 23:28:51 -07003548 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 int loop = 0;
3550 int status;
3551
Al Virobc4785c2006-10-19 23:28:51 -07003552 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3554 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3555
3556 for(;;) {
3557 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003558 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003559 clp->cl_ipaddr,
3560 rpc_peeraddr2str(clp->cl_rpcclient,
3561 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003562 rpc_peeraddr2str(clp->cl_rpcclient,
3563 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003564 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 clp->cl_id_uniquifier);
3566 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003567 sizeof(setclientid.sc_netid),
3568 rpc_peeraddr2str(clp->cl_rpcclient,
3569 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003571 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 clp->cl_ipaddr, port >> 8, port & 255);
3573
3574 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3575 if (status != -NFS4ERR_CLID_INUSE)
3576 break;
3577 if (signalled())
3578 break;
3579 if (loop++ & 1)
3580 ssleep(clp->cl_lease_time + 1);
3581 else
3582 if (++clp->cl_id_uniquifier == 0)
3583 break;
3584 }
3585 return status;
3586}
3587
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003588static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3589 struct nfs4_setclientid_res *arg,
3590 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591{
3592 struct nfs_fsinfo fsinfo;
3593 struct rpc_message msg = {
3594 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003595 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003597 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 };
3599 unsigned long now;
3600 int status;
3601
3602 now = jiffies;
3603 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3604 if (status == 0) {
3605 spin_lock(&clp->cl_lock);
3606 clp->cl_lease_time = fsinfo.lease_time * HZ;
3607 clp->cl_last_renewal = now;
3608 spin_unlock(&clp->cl_lock);
3609 }
3610 return status;
3611}
3612
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003613int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3614 struct nfs4_setclientid_res *arg,
3615 struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003616{
Benny Halevy77e03672008-06-24 20:25:57 +03003617 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003618 int err;
3619 do {
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003620 err = _nfs4_proc_setclientid_confirm(clp, arg, cred);
Trond Myklebust51581f32006-03-20 13:44:47 -05003621 switch (err) {
3622 case 0:
3623 return err;
3624 case -NFS4ERR_RESOURCE:
3625 /* The IBM lawyers misread another document! */
3626 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05003627 case -EKEYEXPIRED:
Trond Myklebust51581f32006-03-20 13:44:47 -05003628 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3629 }
3630 } while (err == 0);
3631 return err;
3632}
3633
Trond Myklebustfe650402006-01-03 09:55:18 +01003634struct nfs4_delegreturndata {
3635 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003636 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003637 struct nfs_fh fh;
3638 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003639 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003640 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003641 int rpc_status;
3642};
3643
Trond Myklebustfe650402006-01-03 09:55:18 +01003644static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3645{
3646 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003647
Trond Myklebust14516c32010-07-31 14:29:06 -04003648 if (!nfs4_sequence_done(task, &data->res.seq_res))
3649 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003650
Ricardo Labiaga79708862009-12-07 09:23:21 -05003651 switch (task->tk_status) {
3652 case -NFS4ERR_STALE_STATEID:
3653 case -NFS4ERR_EXPIRED:
3654 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003655 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003656 break;
3657 default:
3658 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3659 -EAGAIN) {
3660 nfs_restart_rpc(task, data->res.server->nfs_client);
3661 return;
3662 }
3663 }
3664 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003665}
3666
3667static void nfs4_delegreturn_release(void *calldata)
3668{
Trond Myklebustfe650402006-01-03 09:55:18 +01003669 kfree(calldata);
3670}
3671
Andy Adamson938e1012009-04-01 09:22:28 -04003672#if defined(CONFIG_NFS_V4_1)
3673static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3674{
3675 struct nfs4_delegreturndata *d_data;
3676
3677 d_data = (struct nfs4_delegreturndata *)data;
3678
Trond Myklebust035168a2010-06-16 09:52:26 -04003679 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003680 &d_data->args.seq_args,
3681 &d_data->res.seq_res, 1, task))
3682 return;
3683 rpc_call_start(task);
3684}
3685#endif /* CONFIG_NFS_V4_1 */
3686
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003687static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003688#if defined(CONFIG_NFS_V4_1)
3689 .rpc_call_prepare = nfs4_delegreturn_prepare,
3690#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003691 .rpc_call_done = nfs4_delegreturn_done,
3692 .rpc_release = nfs4_delegreturn_release,
3693};
3694
Trond Myklebuste6f81072008-01-24 18:14:34 -05003695static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003696{
3697 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003698 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003699 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003700 struct rpc_message msg = {
3701 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3702 .rpc_cred = cred,
3703 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003704 struct rpc_task_setup task_setup_data = {
3705 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003706 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003707 .callback_ops = &nfs4_delegreturn_ops,
3708 .flags = RPC_TASK_ASYNC,
3709 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003710 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003711
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003712 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003713 if (data == NULL)
3714 return -ENOMEM;
3715 data->args.fhandle = &data->fh;
3716 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003717 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003718 nfs_copy_fh(&data->fh, NFS_FH(inode));
3719 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003720 data->res.fattr = &data->fattr;
3721 data->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003722 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003723 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003724 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003725 data->rpc_status = 0;
3726
Trond Myklebustc970aa82007-07-14 15:39:59 -04003727 task_setup_data.callback_data = data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003728 msg.rpc_argp = &data->args,
3729 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003730 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003731 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003732 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003733 if (!issync)
3734 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003735 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003736 if (status != 0)
3737 goto out;
3738 status = data->rpc_status;
3739 if (status != 0)
3740 goto out;
3741 nfs_refresh_inode(inode, &data->fattr);
3742out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003743 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003744 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745}
3746
Trond Myklebuste6f81072008-01-24 18:14:34 -05003747int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748{
3749 struct nfs_server *server = NFS_SERVER(inode);
3750 struct nfs4_exception exception = { };
3751 int err;
3752 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003753 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 switch (err) {
3755 case -NFS4ERR_STALE_STATEID:
3756 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 case 0:
3758 return 0;
3759 }
3760 err = nfs4_handle_exception(server, err, &exception);
3761 } while (exception.retry);
3762 return err;
3763}
3764
3765#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3766#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3767
3768/*
3769 * sleep, with exponential backoff, and retry the LOCK operation.
3770 */
3771static unsigned long
3772nfs4_set_lock_task_retry(unsigned long timeout)
3773{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003774 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 timeout <<= 1;
3776 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3777 return NFS4_LOCK_MAXTIMEOUT;
3778 return timeout;
3779}
3780
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3782{
3783 struct inode *inode = state->inode;
3784 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003785 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003786 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003788 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003790 struct nfs_lockt_res res = {
3791 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 };
3793 struct rpc_message msg = {
3794 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3795 .rpc_argp = &arg,
3796 .rpc_resp = &res,
3797 .rpc_cred = state->owner->so_cred,
3798 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 struct nfs4_lock_state *lsp;
3800 int status;
3801
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003802 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003803 status = nfs4_set_lock_state(state, request);
3804 if (status != 0)
3805 goto out;
3806 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003807 arg.lock_owner.id = lsp->ls_id.id;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003808 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003809 switch (status) {
3810 case 0:
3811 request->fl_type = F_UNLCK;
3812 break;
3813 case -NFS4ERR_DENIED:
3814 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003816 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003817out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 return status;
3819}
3820
3821static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3822{
3823 struct nfs4_exception exception = { };
3824 int err;
3825
3826 do {
3827 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3828 _nfs4_proc_getlk(state, cmd, request),
3829 &exception);
3830 } while (exception.retry);
3831 return err;
3832}
3833
3834static int do_vfs_lock(struct file *file, struct file_lock *fl)
3835{
3836 int res = 0;
3837 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3838 case FL_POSIX:
3839 res = posix_lock_file_wait(file, fl);
3840 break;
3841 case FL_FLOCK:
3842 res = flock_lock_file_wait(file, fl);
3843 break;
3844 default:
3845 BUG();
3846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 return res;
3848}
3849
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003850struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003851 struct nfs_locku_args arg;
3852 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003853 struct nfs4_lock_state *lsp;
3854 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003855 struct file_lock fl;
3856 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003857 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003858};
3859
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003860static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3861 struct nfs_open_context *ctx,
3862 struct nfs4_lock_state *lsp,
3863 struct nfs_seqid *seqid)
3864{
3865 struct nfs4_unlockdata *p;
3866 struct inode *inode = lsp->ls_state->inode;
3867
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003868 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003869 if (p == NULL)
3870 return NULL;
3871 p->arg.fh = NFS_FH(inode);
3872 p->arg.fl = &p->fl;
3873 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003874 p->res.seqid = seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003875 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003876 p->arg.stateid = &lsp->ls_stateid;
3877 p->lsp = lsp;
3878 atomic_inc(&lsp->ls_count);
3879 /* Ensure we don't close file until we're done freeing locks! */
3880 p->ctx = get_nfs_open_context(ctx);
3881 memcpy(&p->fl, fl, sizeof(p->fl));
3882 p->server = NFS_SERVER(inode);
3883 return p;
3884}
3885
Trond Myklebust06f814a2006-01-03 09:55:07 +01003886static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003887{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003888 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003889 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003890 nfs4_put_lock_state(calldata->lsp);
3891 put_nfs_open_context(calldata->ctx);
3892 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003893}
3894
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003895static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003896{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003897 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003898
Trond Myklebust14516c32010-07-31 14:29:06 -04003899 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3900 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003901 switch (task->tk_status) {
3902 case 0:
3903 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003904 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003905 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003906 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003907 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003908 case -NFS4ERR_BAD_STATEID:
3909 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003910 case -NFS4ERR_STALE_STATEID:
3911 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003912 break;
3913 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003914 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebust0110ee12009-12-07 09:00:24 -05003915 nfs_restart_rpc(task,
Trond Myklebustd61e6122009-12-05 19:32:19 -05003916 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003917 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003918}
3919
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003920static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003921{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003922 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003924 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003925 return;
3926 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003927 /* Note: exit _without_ running nfs4_locku_done */
3928 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003929 return;
3930 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003931 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04003932 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04003933 &calldata->arg.seq_args,
3934 &calldata->res.seq_res, 1, task))
3935 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003936 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937}
3938
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003939static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003940 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003941 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003942 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003943};
3944
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003945static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3946 struct nfs_open_context *ctx,
3947 struct nfs4_lock_state *lsp,
3948 struct nfs_seqid *seqid)
3949{
3950 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003951 struct rpc_message msg = {
3952 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3953 .rpc_cred = ctx->cred,
3954 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003955 struct rpc_task_setup task_setup_data = {
3956 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003957 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003958 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003959 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003960 .flags = RPC_TASK_ASYNC,
3961 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003962
Frank Filz137d6ac2007-07-09 15:32:29 -07003963 /* Ensure this is an unlock - when canceling a lock, the
3964 * canceled lock is passed in, and it won't be an unlock.
3965 */
3966 fl->fl_type = F_UNLCK;
3967
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003968 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3969 if (data == NULL) {
3970 nfs_free_seqid(seqid);
3971 return ERR_PTR(-ENOMEM);
3972 }
3973
Trond Myklebust5138fde2007-07-14 15:40:01 -04003974 msg.rpc_argp = &data->arg,
3975 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003976 task_setup_data.callback_data = data;
3977 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003978}
3979
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3981{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003982 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003983 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003984 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003985 struct rpc_task *task;
3986 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003987 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988
Trond Myklebust9b073572006-06-29 16:38:34 -04003989 status = nfs4_set_lock_state(state, request);
3990 /* Unlock _before_ we do the RPC call */
3991 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003992 down_read(&nfsi->rwsem);
3993 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3994 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003995 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003996 }
3997 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003998 if (status != 0)
3999 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004000 /* Is this a delegated lock? */
4001 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004002 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004003 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004004 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004005 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004006 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004007 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004008 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004009 status = PTR_ERR(task);
4010 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004011 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004012 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004013 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004014out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004015 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004016 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017}
4018
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004019struct nfs4_lockdata {
4020 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004021 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004022 struct nfs4_lock_state *lsp;
4023 struct nfs_open_context *ctx;
4024 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004025 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004026 int rpc_status;
4027 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004028 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004029};
4030
4031static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004032 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4033 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004034{
4035 struct nfs4_lockdata *p;
4036 struct inode *inode = lsp->ls_state->inode;
4037 struct nfs_server *server = NFS_SERVER(inode);
4038
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004039 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004040 if (p == NULL)
4041 return NULL;
4042
4043 p->arg.fh = NFS_FH(inode);
4044 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004045 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004046 if (p->arg.open_seqid == NULL)
4047 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004048 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004049 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004050 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004051 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004052 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004053 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004054 p->res.lock_seqid = p->arg.lock_seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04004055 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004056 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004057 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004058 atomic_inc(&lsp->ls_count);
4059 p->ctx = get_nfs_open_context(ctx);
4060 memcpy(&p->fl, fl, sizeof(p->fl));
4061 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004062out_free_seqid:
4063 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004064out_free:
4065 kfree(p);
4066 return NULL;
4067}
4068
4069static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4070{
4071 struct nfs4_lockdata *data = calldata;
4072 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073
Harvey Harrison3110ff82008-05-02 13:42:44 -07004074 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004075 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4076 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004077 /* Do we need to do an open_to_lock_owner? */
4078 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004079 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4080 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004081 data->arg.open_stateid = &state->stateid;
4082 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004083 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004084 } else
4085 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004086 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004087 if (nfs4_setup_sequence(data->server,
4088 &data->arg.seq_args,
Andy Adamson66179ef2009-04-01 09:22:22 -04004089 &data->res.seq_res, 1, task))
4090 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004091 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004092 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004093}
4094
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004095static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4096{
4097 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4098 nfs4_lock_prepare(task, calldata);
4099}
4100
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004101static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4102{
4103 struct nfs4_lockdata *data = calldata;
4104
Harvey Harrison3110ff82008-05-02 13:42:44 -07004105 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004106
Trond Myklebust14516c32010-07-31 14:29:06 -04004107 if (!nfs4_sequence_done(task, &data->res.seq_res))
4108 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004109
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004110 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004111 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004112 if (data->rpc_status == 0)
4113 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4114 else
4115 goto out;
4116 }
4117 if (data->rpc_status == 0) {
4118 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4119 sizeof(data->lsp->ls_stateid.data));
4120 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04004121 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004122 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004123out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004124 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004125}
4126
4127static void nfs4_lock_release(void *calldata)
4128{
4129 struct nfs4_lockdata *data = calldata;
4130
Harvey Harrison3110ff82008-05-02 13:42:44 -07004131 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004132 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004133 if (data->cancelled != 0) {
4134 struct rpc_task *task;
4135 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4136 data->arg.lock_seqid);
4137 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004138 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004139 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004140 } else
4141 nfs_free_seqid(data->arg.lock_seqid);
4142 nfs4_put_lock_state(data->lsp);
4143 put_nfs_open_context(data->ctx);
4144 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004145 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004146}
4147
4148static const struct rpc_call_ops nfs4_lock_ops = {
4149 .rpc_call_prepare = nfs4_lock_prepare,
4150 .rpc_call_done = nfs4_lock_done,
4151 .rpc_release = nfs4_lock_release,
4152};
4153
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004154static const struct rpc_call_ops nfs4_recover_lock_ops = {
4155 .rpc_call_prepare = nfs4_recover_lock_prepare,
4156 .rpc_call_done = nfs4_lock_done,
4157 .rpc_release = nfs4_lock_release,
4158};
4159
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004160static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4161{
4162 struct nfs_client *clp = server->nfs_client;
4163 struct nfs4_state *state = lsp->ls_state;
4164
4165 switch (error) {
4166 case -NFS4ERR_ADMIN_REVOKED:
4167 case -NFS4ERR_BAD_STATEID:
4168 case -NFS4ERR_EXPIRED:
4169 if (new_lock_owner != 0 ||
4170 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4171 nfs4_state_mark_reclaim_nograce(clp, state);
4172 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004173 break;
4174 case -NFS4ERR_STALE_STATEID:
4175 if (new_lock_owner != 0 ||
4176 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4177 nfs4_state_mark_reclaim_reboot(clp, state);
4178 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004179 };
4180}
4181
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004182static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004183{
4184 struct nfs4_lockdata *data;
4185 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004186 struct rpc_message msg = {
4187 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4188 .rpc_cred = state->owner->so_cred,
4189 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004190 struct rpc_task_setup task_setup_data = {
4191 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004192 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004193 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004194 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004195 .flags = RPC_TASK_ASYNC,
4196 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004197 int ret;
4198
Harvey Harrison3110ff82008-05-02 13:42:44 -07004199 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004200 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004201 fl->fl_u.nfs4_fl.owner,
4202 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004203 if (data == NULL)
4204 return -ENOMEM;
4205 if (IS_SETLKW(cmd))
4206 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004207 if (recovery_type > NFS_LOCK_NEW) {
4208 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004209 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004210 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4211 }
Trond Myklebust5138fde2007-07-14 15:40:01 -04004212 msg.rpc_argp = &data->arg,
4213 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004214 task_setup_data.callback_data = data;
4215 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004216 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004217 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004218 ret = nfs4_wait_for_completion_rpc_task(task);
4219 if (ret == 0) {
4220 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004221 if (ret)
4222 nfs4_handle_setlk_error(data->server, data->lsp,
4223 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004224 } else
4225 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004226 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004227 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004228 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229}
4230
4231static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4232{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004233 struct nfs_server *server = NFS_SERVER(state->inode);
4234 struct nfs4_exception exception = { };
4235 int err;
4236
4237 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004238 /* Cache the lock if possible... */
4239 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4240 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004241 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Jeff Layton2c643482010-01-07 09:42:03 -05004242 if (err != -NFS4ERR_DELAY && err != -EKEYEXPIRED)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004243 break;
4244 nfs4_handle_exception(server, err, &exception);
4245 } while (exception.retry);
4246 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247}
4248
4249static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4250{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004251 struct nfs_server *server = NFS_SERVER(state->inode);
4252 struct nfs4_exception exception = { };
4253 int err;
4254
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004255 err = nfs4_set_lock_state(state, request);
4256 if (err != 0)
4257 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004258 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004259 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4260 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004261 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004262 switch (err) {
4263 default:
4264 goto out;
4265 case -NFS4ERR_GRACE:
4266 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05004267 case -EKEYEXPIRED:
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004268 nfs4_handle_exception(server, err, &exception);
4269 err = 0;
4270 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004271 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004272out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004273 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274}
4275
4276static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4277{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004278 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004279 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004280 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004282 if ((fl_flags & FL_POSIX) &&
4283 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4284 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004285 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004286 status = nfs4_set_lock_state(state, request);
4287 if (status != 0)
4288 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004289 request->fl_flags |= FL_ACCESS;
4290 status = do_vfs_lock(request->fl_file, request);
4291 if (status < 0)
4292 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004293 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004294 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004295 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004296 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004297 request->fl_flags = fl_flags & ~FL_SLEEP;
4298 status = do_vfs_lock(request->fl_file, request);
4299 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004300 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004301 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004302 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004303 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004304 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004305 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004306 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004307 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004308out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004309 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004310out:
4311 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 return status;
4313}
4314
4315static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4316{
4317 struct nfs4_exception exception = { };
4318 int err;
4319
4320 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004321 err = _nfs4_proc_setlk(state, cmd, request);
4322 if (err == -NFS4ERR_DENIED)
4323 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004325 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 } while (exception.retry);
4327 return err;
4328}
4329
4330static int
4331nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4332{
4333 struct nfs_open_context *ctx;
4334 struct nfs4_state *state;
4335 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4336 int status;
4337
4338 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004339 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 state = ctx->state;
4341
4342 if (request->fl_start < 0 || request->fl_end < 0)
4343 return -EINVAL;
4344
Trond Myklebustd9531262009-07-21 19:22:38 -04004345 if (IS_GETLK(cmd)) {
4346 if (state != NULL)
4347 return nfs4_proc_getlk(state, F_GETLK, request);
4348 return 0;
4349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350
4351 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4352 return -EINVAL;
4353
Trond Myklebustd9531262009-07-21 19:22:38 -04004354 if (request->fl_type == F_UNLCK) {
4355 if (state != NULL)
4356 return nfs4_proc_unlck(state, cmd, request);
4357 return 0;
4358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359
Trond Myklebustd9531262009-07-21 19:22:38 -04004360 if (state == NULL)
4361 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 do {
4363 status = nfs4_proc_setlk(state, cmd, request);
4364 if ((status != -EAGAIN) || IS_SETLK(cmd))
4365 break;
4366 timeout = nfs4_set_lock_task_retry(timeout);
4367 status = -ERESTARTSYS;
4368 if (signalled())
4369 break;
4370 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 return status;
4372}
4373
Trond Myklebust888e6942005-11-04 15:38:11 -05004374int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4375{
4376 struct nfs_server *server = NFS_SERVER(state->inode);
4377 struct nfs4_exception exception = { };
4378 int err;
4379
4380 err = nfs4_set_lock_state(state, fl);
4381 if (err != 0)
4382 goto out;
4383 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004384 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004385 switch (err) {
4386 default:
4387 printk(KERN_ERR "%s: unhandled error %d.\n",
4388 __func__, err);
4389 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004390 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004391 goto out;
4392 case -NFS4ERR_EXPIRED:
4393 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004394 case -NFS4ERR_STALE_STATEID:
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004395 case -NFS4ERR_BADSESSION:
4396 case -NFS4ERR_BADSLOT:
4397 case -NFS4ERR_BAD_HIGH_SLOT:
4398 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4399 case -NFS4ERR_DEADSESSION:
Trond Myklebustd5122202009-06-17 13:22:58 -07004400 nfs4_schedule_state_recovery(server->nfs_client);
4401 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004402 case -ERESTARTSYS:
4403 /*
4404 * The show must go on: exit, but mark the
4405 * stateid as needing recovery.
4406 */
4407 case -NFS4ERR_ADMIN_REVOKED:
4408 case -NFS4ERR_BAD_STATEID:
4409 case -NFS4ERR_OPENMODE:
4410 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4411 err = 0;
4412 goto out;
4413 case -ENOMEM:
4414 case -NFS4ERR_DENIED:
4415 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4416 err = 0;
4417 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004418 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05004419 case -EKEYEXPIRED:
Trond Myklebustd5122202009-06-17 13:22:58 -07004420 break;
4421 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004422 err = nfs4_handle_exception(server, err, &exception);
4423 } while (exception.retry);
4424out:
4425 return err;
4426}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004427
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004428static void nfs4_release_lockowner_release(void *calldata)
4429{
4430 kfree(calldata);
4431}
4432
4433const struct rpc_call_ops nfs4_release_lockowner_ops = {
4434 .rpc_release = nfs4_release_lockowner_release,
4435};
4436
4437void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4438{
4439 struct nfs_server *server = lsp->ls_state->owner->so_server;
4440 struct nfs_release_lockowner_args *args;
4441 struct rpc_message msg = {
4442 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4443 };
4444
4445 if (server->nfs_client->cl_mvops->minor_version != 0)
4446 return;
4447 args = kmalloc(sizeof(*args), GFP_NOFS);
4448 if (!args)
4449 return;
4450 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4451 args->lock_owner.id = lsp->ls_id.id;
4452 msg.rpc_argp = args;
4453 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4454}
4455
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004456#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4457
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004458int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4459 size_t buflen, int flags)
4460{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004461 struct inode *inode = dentry->d_inode;
4462
4463 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4464 return -EOPNOTSUPP;
4465
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004466 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004467}
4468
4469/* The getxattr man page suggests returning -ENODATA for unknown attributes,
4470 * and that's what we'll do for e.g. user attributes that haven't been set.
4471 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4472 * attributes in kernel-managed attribute namespaces. */
4473ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4474 size_t buflen)
4475{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004476 struct inode *inode = dentry->d_inode;
4477
4478 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4479 return -EOPNOTSUPP;
4480
4481 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004482}
4483
4484ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4485{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004486 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004487
J. Bruce Fields096455a2006-03-20 23:23:42 -05004488 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4489 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004490 if (buf && buflen < len)
4491 return -ERANGE;
4492 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004493 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4494 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004495}
4496
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004497static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4498{
4499 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4500 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4501 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4502 return;
4503
4504 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4505 NFS_ATTR_FATTR_NLINK;
4506 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4507 fattr->nlink = 2;
4508}
4509
Trond Myklebust56659e92007-07-17 21:52:39 -04004510int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004511 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004512{
4513 struct nfs_server *server = NFS_SERVER(dir);
4514 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004515 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4516 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004517 };
4518 struct nfs4_fs_locations_arg args = {
4519 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004520 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004521 .page = page,
4522 .bitmask = bitmask,
4523 };
Benny Halevy22958462009-04-01 09:22:02 -04004524 struct nfs4_fs_locations_res res = {
4525 .fs_locations = fs_locations,
4526 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004527 struct rpc_message msg = {
4528 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4529 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004530 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004531 };
4532 int status;
4533
Harvey Harrison3110ff82008-05-02 13:42:44 -07004534 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004535 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004536 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004537 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04004538 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004539 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004540 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004541 return status;
4542}
4543
Andy Adamson557134a2009-04-01 09:21:53 -04004544#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004545/*
4546 * nfs4_proc_exchange_id()
4547 *
4548 * Since the clientid has expired, all compounds using sessions
4549 * associated with the stale clientid will be returning
4550 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4551 * be in some phase of session reset.
4552 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004553int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004554{
4555 nfs4_verifier verifier;
4556 struct nfs41_exchange_id_args args = {
4557 .client = clp,
4558 .flags = clp->cl_exchange_flags,
4559 };
4560 struct nfs41_exchange_id_res res = {
4561 .client = clp,
4562 };
4563 int status;
4564 struct rpc_message msg = {
4565 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4566 .rpc_argp = &args,
4567 .rpc_resp = &res,
4568 .rpc_cred = cred,
4569 };
4570 __be32 *p;
4571
4572 dprintk("--> %s\n", __func__);
4573 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004574
Alexandros Batsakis7b183d02009-12-05 13:33:25 -05004575 /* Remove server-only flags */
4576 args.flags &= ~EXCHGID4_FLAG_CONFIRMED_R;
4577
Benny Halevy99fe60d2009-04-01 09:22:29 -04004578 p = (u32 *)verifier.data;
4579 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4580 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4581 args.verifier = &verifier;
4582
4583 while (1) {
4584 args.id_len = scnprintf(args.id, sizeof(args.id),
4585 "%s/%s %u",
4586 clp->cl_ipaddr,
4587 rpc_peeraddr2str(clp->cl_rpcclient,
4588 RPC_DISPLAY_ADDR),
4589 clp->cl_id_uniquifier);
4590
4591 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4592
Andy Adamson180b62a2010-03-02 13:19:36 -05004593 if (status != -NFS4ERR_CLID_INUSE)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004594 break;
4595
4596 if (signalled())
4597 break;
4598
4599 if (++clp->cl_id_uniquifier == 0)
4600 break;
4601 }
4602
4603 dprintk("<-- %s status= %d\n", __func__, status);
4604 return status;
4605}
4606
Andy Adamson2050f0c2009-04-01 09:22:30 -04004607struct nfs4_get_lease_time_data {
4608 struct nfs4_get_lease_time_args *args;
4609 struct nfs4_get_lease_time_res *res;
4610 struct nfs_client *clp;
4611};
4612
4613static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4614 void *calldata)
4615{
4616 int ret;
4617 struct nfs4_get_lease_time_data *data =
4618 (struct nfs4_get_lease_time_data *)calldata;
4619
4620 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004621 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004622 /* just setup sequence, do not trigger session recovery
4623 since we're invoked within one */
4624 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004625 &data->args->la_seq_args,
4626 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004627
4628 BUG_ON(ret == -EAGAIN);
4629 rpc_call_start(task);
4630 dprintk("<-- %s\n", __func__);
4631}
4632
4633/*
4634 * Called from nfs4_state_manager thread for session setup, so don't recover
4635 * from sequence operation or clientid errors.
4636 */
4637static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4638{
4639 struct nfs4_get_lease_time_data *data =
4640 (struct nfs4_get_lease_time_data *)calldata;
4641
4642 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04004643 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4644 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04004645 switch (task->tk_status) {
4646 case -NFS4ERR_DELAY:
4647 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05004648 case -EKEYEXPIRED:
Andy Adamson2050f0c2009-04-01 09:22:30 -04004649 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4650 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4651 task->tk_status = 0;
Trond Myklebust0110ee12009-12-07 09:00:24 -05004652 nfs_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004653 return;
4654 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004655 dprintk("<-- %s\n", __func__);
4656}
4657
4658struct rpc_call_ops nfs4_get_lease_time_ops = {
4659 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4660 .rpc_call_done = nfs4_get_lease_time_done,
4661};
4662
4663int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4664{
4665 struct rpc_task *task;
4666 struct nfs4_get_lease_time_args args;
4667 struct nfs4_get_lease_time_res res = {
4668 .lr_fsinfo = fsinfo,
4669 };
4670 struct nfs4_get_lease_time_data data = {
4671 .args = &args,
4672 .res = &res,
4673 .clp = clp,
4674 };
4675 struct rpc_message msg = {
4676 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4677 .rpc_argp = &args,
4678 .rpc_resp = &res,
4679 };
4680 struct rpc_task_setup task_setup = {
4681 .rpc_client = clp->cl_rpcclient,
4682 .rpc_message = &msg,
4683 .callback_ops = &nfs4_get_lease_time_ops,
4684 .callback_data = &data
4685 };
4686 int status;
4687
4688 res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
4689 dprintk("--> %s\n", __func__);
4690 task = rpc_run_task(&task_setup);
4691
4692 if (IS_ERR(task))
4693 status = PTR_ERR(task);
4694 else {
4695 status = task->tk_status;
4696 rpc_put_task(task);
4697 }
4698 dprintk("<-- %s return %d\n", __func__, status);
4699
4700 return status;
4701}
4702
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004703/*
4704 * Reset a slot table
4705 */
Andy Adamson104aeba2010-01-14 17:45:10 -05004706static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4707 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004708{
Andy Adamson104aeba2010-01-14 17:45:10 -05004709 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004710 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004711 int ret = 0;
4712
Andy Adamson104aeba2010-01-14 17:45:10 -05004713 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4714 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004715
Andy Adamson104aeba2010-01-14 17:45:10 -05004716 /* Does the newly negotiated max_reqs match the existing slot table? */
4717 if (max_reqs != tbl->max_slots) {
4718 ret = -ENOMEM;
4719 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004720 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05004721 if (!new)
4722 goto out;
4723 ret = 0;
4724 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004725 }
4726 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05004727 if (new) {
4728 tbl->slots = new;
4729 tbl->max_slots = max_reqs;
4730 }
4731 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004732 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004733 spin_unlock(&tbl->slot_tbl_lock);
4734 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4735 tbl, tbl->slots, tbl->max_slots);
4736out:
4737 dprintk("<-- %s: return %d\n", __func__, ret);
4738 return ret;
4739}
4740
Andy Adamsonfc931582009-04-01 09:22:31 -04004741/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004742 * Reset the forechannel and backchannel slot tables
4743 */
4744static int nfs4_reset_slot_tables(struct nfs4_session *session)
4745{
4746 int status;
4747
4748 status = nfs4_reset_slot_table(&session->fc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004749 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004750 if (status)
4751 return status;
4752
4753 status = nfs4_reset_slot_table(&session->bc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004754 session->bc_attrs.max_reqs, 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004755 return status;
4756}
4757
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004758/* Destroy the slot table */
4759static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4760{
4761 if (session->fc_slot_table.slots != NULL) {
4762 kfree(session->fc_slot_table.slots);
4763 session->fc_slot_table.slots = NULL;
4764 }
4765 if (session->bc_slot_table.slots != NULL) {
4766 kfree(session->bc_slot_table.slots);
4767 session->bc_slot_table.slots = NULL;
4768 }
4769 return;
4770}
4771
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004772/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004773 * Initialize slot table
4774 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004775static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4776 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04004777{
Andy Adamsonfc931582009-04-01 09:22:31 -04004778 struct nfs4_slot *slot;
4779 int ret = -ENOMEM;
4780
4781 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4782
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004783 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04004784
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004785 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04004786 if (!slot)
4787 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04004788 ret = 0;
4789
4790 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04004791 tbl->max_slots = max_slots;
4792 tbl->slots = slot;
4793 tbl->highest_used_slotid = -1; /* no slot is currently used */
4794 spin_unlock(&tbl->slot_tbl_lock);
4795 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4796 tbl, tbl->slots, tbl->max_slots);
4797out:
4798 dprintk("<-- %s: return %d\n", __func__, ret);
4799 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04004800}
4801
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004802/*
4803 * Initialize the forechannel and backchannel tables
4804 */
4805static int nfs4_init_slot_tables(struct nfs4_session *session)
4806{
Trond Myklebustf26468f2009-12-05 19:32:11 -05004807 struct nfs4_slot_table *tbl;
4808 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004809
Trond Myklebustf26468f2009-12-05 19:32:11 -05004810 tbl = &session->fc_slot_table;
4811 if (tbl->slots == NULL) {
4812 status = nfs4_init_slot_table(tbl,
4813 session->fc_attrs.max_reqs, 1);
4814 if (status)
4815 return status;
4816 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004817
Trond Myklebustf26468f2009-12-05 19:32:11 -05004818 tbl = &session->bc_slot_table;
4819 if (tbl->slots == NULL) {
4820 status = nfs4_init_slot_table(tbl,
4821 session->bc_attrs.max_reqs, 0);
4822 if (status)
4823 nfs4_destroy_slot_tables(session);
4824 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004825
4826 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04004827}
4828
4829struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4830{
4831 struct nfs4_session *session;
4832 struct nfs4_slot_table *tbl;
4833
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004834 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04004835 if (!session)
4836 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04004837
Andy Adamsonea028ac2009-12-04 15:55:38 -05004838 init_completion(&session->complete);
Andy Adamson76db6d92009-04-01 09:22:38 -04004839
Andy Adamson557134a2009-04-01 09:21:53 -04004840 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004841 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04004842 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004843 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004844
4845 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004846 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004847 spin_lock_init(&tbl->slot_tbl_lock);
4848 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4849
Trond Myklebust1055d762010-06-16 09:52:27 -04004850 session->session_state = 1<<NFS4_SESSION_INITING;
4851
Andy Adamson557134a2009-04-01 09:21:53 -04004852 session->clp = clp;
4853 return session;
4854}
4855
4856void nfs4_destroy_session(struct nfs4_session *session)
4857{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04004858 nfs4_proc_destroy_session(session);
4859 dprintk("%s Destroy backchannel for xprt %p\n",
4860 __func__, session->clp->cl_rpcclient->cl_xprt);
4861 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4862 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004863 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04004864 kfree(session);
4865}
4866
Andy Adamsonfc931582009-04-01 09:22:31 -04004867/*
4868 * Initialize the values to be used by the client in CREATE_SESSION
4869 * If nfs4_init_session set the fore channel request and response sizes,
4870 * use them.
4871 *
4872 * Set the back channel max_resp_sz_cached to zero to force the client to
4873 * always set csa_cachethis to FALSE because the current implementation
4874 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4875 */
4876static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4877{
4878 struct nfs4_session *session = args->client->cl_session;
4879 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4880 mxresp_sz = session->fc_attrs.max_resp_sz;
4881
4882 if (mxrqst_sz == 0)
4883 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4884 if (mxresp_sz == 0)
4885 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4886 /* Fore channel attributes */
4887 args->fc_attrs.headerpadsz = 0;
4888 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4889 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04004890 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4891 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4892
4893 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05004894 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04004895 __func__,
4896 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05004897 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04004898
4899 /* Back channel attributes */
4900 args->bc_attrs.headerpadsz = 0;
4901 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4902 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4903 args->bc_attrs.max_resp_sz_cached = 0;
4904 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4905 args->bc_attrs.max_reqs = 1;
4906
4907 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4908 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4909 __func__,
4910 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4911 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4912 args->bc_attrs.max_reqs);
4913}
4914
Andy Adamson8d353012009-04-01 09:22:32 -04004915static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
4916{
4917 if (rcvd <= sent)
4918 return 0;
4919 printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
4920 "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
4921 return -EINVAL;
4922}
4923
4924#define _verify_fore_channel_attr(_name_) \
4925 _verify_channel_attr("fore", #_name_, \
4926 args->fc_attrs._name_, \
4927 session->fc_attrs._name_)
4928
4929#define _verify_back_channel_attr(_name_) \
4930 _verify_channel_attr("back", #_name_, \
4931 args->bc_attrs._name_, \
4932 session->bc_attrs._name_)
4933
4934/*
4935 * The server is not allowed to increase the fore channel header pad size,
4936 * maximum response size, or maximum number of operations.
4937 *
4938 * The back channel attributes are only negotiatied down: We send what the
4939 * (back channel) server insists upon.
4940 */
4941static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4942 struct nfs4_session *session)
4943{
4944 int ret = 0;
4945
4946 ret |= _verify_fore_channel_attr(headerpadsz);
4947 ret |= _verify_fore_channel_attr(max_resp_sz);
4948 ret |= _verify_fore_channel_attr(max_ops);
4949
4950 ret |= _verify_back_channel_attr(headerpadsz);
4951 ret |= _verify_back_channel_attr(max_rqst_sz);
4952 ret |= _verify_back_channel_attr(max_resp_sz);
4953 ret |= _verify_back_channel_attr(max_resp_sz_cached);
4954 ret |= _verify_back_channel_attr(max_ops);
4955 ret |= _verify_back_channel_attr(max_reqs);
4956
4957 return ret;
4958}
4959
Andy Adamsonfc931582009-04-01 09:22:31 -04004960static int _nfs4_proc_create_session(struct nfs_client *clp)
4961{
4962 struct nfs4_session *session = clp->cl_session;
4963 struct nfs41_create_session_args args = {
4964 .client = clp,
4965 .cb_program = NFS4_CALLBACK,
4966 };
4967 struct nfs41_create_session_res res = {
4968 .client = clp,
4969 };
4970 struct rpc_message msg = {
4971 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4972 .rpc_argp = &args,
4973 .rpc_resp = &res,
4974 };
4975 int status;
4976
4977 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04004978 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04004979
4980 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4981
Andy Adamson8d353012009-04-01 09:22:32 -04004982 if (!status)
4983 /* Verify the session's negotiated channel_attrs values */
4984 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04004985 if (!status) {
4986 /* Increment the clientid slot sequence id */
4987 clp->cl_seqid++;
4988 }
4989
4990 return status;
4991}
4992
4993/*
4994 * Issues a CREATE_SESSION operation to the server.
4995 * It is the responsibility of the caller to verify the session is
4996 * expired before calling this routine.
4997 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05004998int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04004999{
5000 int status;
5001 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005002 struct nfs4_session *session = clp->cl_session;
5003
5004 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5005
5006 status = _nfs4_proc_create_session(clp);
5007 if (status)
5008 goto out;
5009
Trond Myklebustf26468f2009-12-05 19:32:11 -05005010 /* Init and reset the fore channel */
5011 status = nfs4_init_slot_tables(session);
5012 dprintk("slot table initialization returned %d\n", status);
5013 if (status)
5014 goto out;
5015 status = nfs4_reset_slot_tables(session);
5016 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005017 if (status)
5018 goto out;
5019
5020 ptr = (unsigned *)&session->sess_id.data[0];
5021 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5022 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005023out:
5024 dprintk("<-- %s\n", __func__);
5025 return status;
5026}
5027
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005028/*
5029 * Issue the over-the-wire RPC DESTROY_SESSION.
5030 * The caller must serialize access to this routine.
5031 */
5032int nfs4_proc_destroy_session(struct nfs4_session *session)
5033{
5034 int status = 0;
5035 struct rpc_message msg;
5036
5037 dprintk("--> nfs4_proc_destroy_session\n");
5038
5039 /* session is still being setup */
5040 if (session->clp->cl_cons_state != NFS_CS_READY)
5041 return status;
5042
5043 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5044 msg.rpc_argp = session;
5045 msg.rpc_resp = NULL;
5046 msg.rpc_cred = NULL;
5047 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5048
5049 if (status)
5050 printk(KERN_WARNING
5051 "Got error %d from the server on DESTROY_SESSION. "
5052 "Session has been destroyed regardless...\n", status);
5053
5054 dprintk("<-- nfs4_proc_destroy_session\n");
5055 return status;
5056}
5057
Trond Myklebustfccba802009-07-21 16:48:07 -04005058int nfs4_init_session(struct nfs_server *server)
5059{
5060 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005061 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005062 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005063 int ret;
5064
5065 if (!nfs4_has_session(clp))
5066 return 0;
5067
Trond Myklebust1055d762010-06-16 09:52:27 -04005068 session = clp->cl_session;
5069 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5070 return 0;
5071
Andy Adamson68bf05e2009-12-15 12:55:02 -05005072 rsize = server->rsize;
5073 if (rsize == 0)
5074 rsize = NFS_MAX_FILE_IO_SIZE;
5075 wsize = server->wsize;
5076 if (wsize == 0)
5077 wsize = NFS_MAX_FILE_IO_SIZE;
5078
Andy Adamson68bf05e2009-12-15 12:55:02 -05005079 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5080 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005081
Trond Myklebustfccba802009-07-21 16:48:07 -04005082 ret = nfs4_recover_expired_lease(server);
5083 if (!ret)
5084 ret = nfs4_check_client_ready(clp);
5085 return ret;
5086}
5087
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005088/*
5089 * Renew the cl_session lease.
5090 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005091struct nfs4_sequence_data {
5092 struct nfs_client *clp;
5093 struct nfs4_sequence_args args;
5094 struct nfs4_sequence_res res;
5095};
5096
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005097static void nfs41_sequence_release(void *data)
5098{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005099 struct nfs4_sequence_data *calldata = data;
5100 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005101
Alexandros Batsakis71358402010-02-05 03:45:05 -08005102 if (atomic_read(&clp->cl_count) > 1)
5103 nfs4_schedule_state_renewal(clp);
5104 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005105 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005106}
5107
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005108static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5109{
5110 switch(task->tk_status) {
5111 case -NFS4ERR_DELAY:
5112 case -EKEYEXPIRED:
5113 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5114 return -EAGAIN;
5115 default:
5116 nfs4_schedule_state_recovery(clp);
5117 }
5118 return 0;
5119}
5120
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005121static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005122{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005123 struct nfs4_sequence_data *calldata = data;
5124 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005125
Trond Myklebust14516c32010-07-31 14:29:06 -04005126 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5127 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005128
5129 if (task->tk_status < 0) {
5130 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005131 if (atomic_read(&clp->cl_count) == 1)
5132 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005133
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005134 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5135 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005136 return;
5137 }
5138 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005139 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005140out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005141 dprintk("<-- %s\n", __func__);
5142}
5143
5144static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5145{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005146 struct nfs4_sequence_data *calldata = data;
5147 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005148 struct nfs4_sequence_args *args;
5149 struct nfs4_sequence_res *res;
5150
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005151 args = task->tk_msg.rpc_argp;
5152 res = task->tk_msg.rpc_resp;
5153
Trond Myklebust035168a2010-06-16 09:52:26 -04005154 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005155 return;
5156 rpc_call_start(task);
5157}
5158
5159static const struct rpc_call_ops nfs41_sequence_ops = {
5160 .rpc_call_done = nfs41_sequence_call_done,
5161 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005162 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005163};
5164
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005165static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005166{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005167 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005168 struct rpc_message msg = {
5169 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5170 .rpc_cred = cred,
5171 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005172 struct rpc_task_setup task_setup_data = {
5173 .rpc_client = clp->cl_rpcclient,
5174 .rpc_message = &msg,
5175 .callback_ops = &nfs41_sequence_ops,
5176 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5177 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005178
Alexandros Batsakis71358402010-02-05 03:45:05 -08005179 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005180 return ERR_PTR(-EIO);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005181 calldata = kmalloc(sizeof(*calldata), GFP_NOFS);
5182 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005183 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005184 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005185 }
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005186 calldata->res.sr_slotid = NFS4_MAX_SLOT_TABLE;
5187 msg.rpc_argp = &calldata->args;
5188 msg.rpc_resp = &calldata->res;
5189 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005190 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005191
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005192 return rpc_run_task(&task_setup_data);
5193}
5194
5195static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5196{
5197 struct rpc_task *task;
5198 int ret = 0;
5199
5200 task = _nfs41_proc_sequence(clp, cred);
5201 if (IS_ERR(task))
5202 ret = PTR_ERR(task);
5203 else
5204 rpc_put_task(task);
5205 dprintk("<-- %s status=%d\n", __func__, ret);
5206 return ret;
5207}
5208
5209static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5210{
5211 struct rpc_task *task;
5212 int ret;
5213
5214 task = _nfs41_proc_sequence(clp, cred);
5215 if (IS_ERR(task)) {
5216 ret = PTR_ERR(task);
5217 goto out;
5218 }
5219 ret = rpc_wait_for_completion_task(task);
5220 if (!ret)
5221 ret = task->tk_status;
5222 rpc_put_task(task);
5223out:
5224 dprintk("<-- %s status=%d\n", __func__, ret);
5225 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005226}
5227
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005228struct nfs4_reclaim_complete_data {
5229 struct nfs_client *clp;
5230 struct nfs41_reclaim_complete_args arg;
5231 struct nfs41_reclaim_complete_res res;
5232};
5233
5234static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5235{
5236 struct nfs4_reclaim_complete_data *calldata = data;
5237
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005238 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005239 if (nfs41_setup_sequence(calldata->clp->cl_session,
5240 &calldata->arg.seq_args,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005241 &calldata->res.seq_res, 0, task))
5242 return;
5243
5244 rpc_call_start(task);
5245}
5246
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005247static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5248{
5249 switch(task->tk_status) {
5250 case 0:
5251 case -NFS4ERR_COMPLETE_ALREADY:
5252 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5253 break;
5254 case -NFS4ERR_DELAY:
5255 case -EKEYEXPIRED:
5256 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5257 return -EAGAIN;
5258 default:
5259 nfs4_schedule_state_recovery(clp);
5260 }
5261 return 0;
5262}
5263
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005264static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5265{
5266 struct nfs4_reclaim_complete_data *calldata = data;
5267 struct nfs_client *clp = calldata->clp;
5268 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5269
5270 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005271 if (!nfs41_sequence_done(task, res))
5272 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005273
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005274 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5275 rpc_restart_call_prepare(task);
5276 return;
5277 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005278 dprintk("<-- %s\n", __func__);
5279}
5280
5281static void nfs4_free_reclaim_complete_data(void *data)
5282{
5283 struct nfs4_reclaim_complete_data *calldata = data;
5284
5285 kfree(calldata);
5286}
5287
5288static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5289 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5290 .rpc_call_done = nfs4_reclaim_complete_done,
5291 .rpc_release = nfs4_free_reclaim_complete_data,
5292};
5293
5294/*
5295 * Issue a global reclaim complete.
5296 */
5297static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5298{
5299 struct nfs4_reclaim_complete_data *calldata;
5300 struct rpc_task *task;
5301 struct rpc_message msg = {
5302 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5303 };
5304 struct rpc_task_setup task_setup_data = {
5305 .rpc_client = clp->cl_rpcclient,
5306 .rpc_message = &msg,
5307 .callback_ops = &nfs4_reclaim_complete_call_ops,
5308 .flags = RPC_TASK_ASYNC,
5309 };
5310 int status = -ENOMEM;
5311
5312 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005313 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005314 if (calldata == NULL)
5315 goto out;
5316 calldata->clp = clp;
5317 calldata->arg.one_fs = 0;
5318 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
5319
5320 msg.rpc_argp = &calldata->arg;
5321 msg.rpc_resp = &calldata->res;
5322 task_setup_data.callback_data = calldata;
5323 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005324 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005325 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005326 goto out;
5327 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005328 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005329 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005330out:
5331 dprintk("<-- %s status=%d\n", __func__, status);
5332 return status;
5333}
Andy Adamson557134a2009-04-01 09:21:53 -04005334#endif /* CONFIG_NFS_V4_1 */
5335
Andy Adamson591d71c2009-04-01 09:22:47 -04005336struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005337 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005338 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339 .recover_open = nfs4_open_reclaim,
5340 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04005341 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005342 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343};
5344
Andy Adamson591d71c2009-04-01 09:22:47 -04005345#if defined(CONFIG_NFS_V4_1)
5346struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5347 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5348 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5349 .recover_open = nfs4_open_reclaim,
5350 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05005351 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005352 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005353 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04005354};
5355#endif /* CONFIG_NFS_V4_1 */
5356
5357struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005358 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005359 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 .recover_open = nfs4_open_expired,
5361 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04005362 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005363 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364};
5365
Andy Adamson591d71c2009-04-01 09:22:47 -04005366#if defined(CONFIG_NFS_V4_1)
5367struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5368 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5369 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5370 .recover_open = nfs4_open_expired,
5371 .recover_lock = nfs4_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05005372 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005373 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04005374};
5375#endif /* CONFIG_NFS_V4_1 */
5376
Benny Halevy29fba382009-04-01 09:22:44 -04005377struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5378 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04005379 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005380 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04005381};
5382
5383#if defined(CONFIG_NFS_V4_1)
5384struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5385 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04005386 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005387 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04005388};
5389#endif
5390
Trond Myklebust97dc1352010-06-16 09:52:26 -04005391static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
5392 .minor_version = 0,
5393 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04005394 .validate_stateid = nfs4_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005395 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
5396 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
5397 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005398};
5399
5400#if defined(CONFIG_NFS_V4_1)
5401static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
5402 .minor_version = 1,
5403 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04005404 .validate_stateid = nfs41_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005405 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
5406 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
5407 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005408};
5409#endif
5410
Trond Myklebust97dc1352010-06-16 09:52:26 -04005411const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
5412 [0] = &nfs_v4_0_minor_ops,
5413#if defined(CONFIG_NFS_V4_1)
5414 [1] = &nfs_v4_1_minor_ops,
5415#endif
5416};
5417
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005418static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005419 .permission = nfs_permission,
5420 .getattr = nfs_getattr,
5421 .setattr = nfs_setattr,
5422 .getxattr = nfs4_getxattr,
5423 .setxattr = nfs4_setxattr,
5424 .listxattr = nfs4_listxattr,
5425};
5426
David Howells509de812006-08-22 20:06:11 -04005427const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 .version = 4, /* protocol version */
5429 .dentry_ops = &nfs4_dentry_operations,
5430 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005431 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 .getroot = nfs4_proc_get_root,
5433 .getattr = nfs4_proc_getattr,
5434 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04005435 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 .lookup = nfs4_proc_lookup,
5437 .access = nfs4_proc_access,
5438 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 .create = nfs4_proc_create,
5440 .remove = nfs4_proc_remove,
5441 .unlink_setup = nfs4_proc_unlink_setup,
5442 .unlink_done = nfs4_proc_unlink_done,
5443 .rename = nfs4_proc_rename,
5444 .link = nfs4_proc_link,
5445 .symlink = nfs4_proc_symlink,
5446 .mkdir = nfs4_proc_mkdir,
5447 .rmdir = nfs4_proc_remove,
5448 .readdir = nfs4_proc_readdir,
5449 .mknod = nfs4_proc_mknod,
5450 .statfs = nfs4_proc_statfs,
5451 .fsinfo = nfs4_proc_fsinfo,
5452 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04005453 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 .decode_dirent = nfs4_decode_dirent,
5455 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05005456 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005458 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005460 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005462 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04005463 .close_context = nfs4_close_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464};
5465
5466/*
5467 * Local variables:
5468 * c-basic-offset: 8
5469 * End:
5470 */