blob: 101bf403f5079504d2701046bcf57f07a9bcd1bd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/sunrpc/clnt.h>
44#include <linux/nfs.h>
45#include <linux/nfs4.h>
46#include <linux/nfs_fs.h>
47#include <linux/nfs_page.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070049#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040050#include <linux/module.h>
Andy Adamson5a0ffe52009-04-01 09:23:18 -040051#include <linux/sunrpc/bc_xprt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Trond Myklebust4ce79712005-06-22 17:16:21 +000053#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050055#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050056#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040057#include "callback.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59#define NFSDBG_FACILITY NFSDBG_PROC
60
Trond Myklebust2066fe82006-09-15 08:30:46 -040061#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#define NFS4_POLL_RETRY_MAX (15*HZ)
63
Trond Myklebusta78cb572009-08-09 15:06:19 -040064#define NFS4_MAX_LOOP_ON_RECOVER (10)
65
Trond Myklebustcdd4e682006-01-03 09:55:12 +010066struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010067static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080068static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050070static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Trond Myklebust99367812007-07-17 21:52:41 -040071static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
72static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040073static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
74 struct nfs_fattr *fattr, struct iattr *sattr,
75 struct nfs4_state *state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050078static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Trond Myklebust52567b02009-10-23 14:46:42 -040080 if (err >= -1000)
81 return err;
82 switch (err) {
83 case -NFS4ERR_RESOURCE:
84 return -EREMOTEIO;
85 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -070087 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -040088 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 }
Trond Myklebust52567b02009-10-23 14:46:42 -040090 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
93/*
94 * This is our standard bitmap for GETATTR requests.
95 */
96const u32 nfs4_fattr_bitmap[2] = {
97 FATTR4_WORD0_TYPE
98 | FATTR4_WORD0_CHANGE
99 | FATTR4_WORD0_SIZE
100 | FATTR4_WORD0_FSID
101 | FATTR4_WORD0_FILEID,
102 FATTR4_WORD1_MODE
103 | FATTR4_WORD1_NUMLINKS
104 | FATTR4_WORD1_OWNER
105 | FATTR4_WORD1_OWNER_GROUP
106 | FATTR4_WORD1_RAWDEV
107 | FATTR4_WORD1_SPACE_USED
108 | FATTR4_WORD1_TIME_ACCESS
109 | FATTR4_WORD1_TIME_METADATA
110 | FATTR4_WORD1_TIME_MODIFY
111};
112
113const u32 nfs4_statfs_bitmap[2] = {
114 FATTR4_WORD0_FILES_AVAIL
115 | FATTR4_WORD0_FILES_FREE
116 | FATTR4_WORD0_FILES_TOTAL,
117 FATTR4_WORD1_SPACE_AVAIL
118 | FATTR4_WORD1_SPACE_FREE
119 | FATTR4_WORD1_SPACE_TOTAL
120};
121
Trond Myklebust4ce79712005-06-22 17:16:21 +0000122const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 FATTR4_WORD0_MAXLINK
124 | FATTR4_WORD0_MAXNAME,
125 0
126};
127
128const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
129 | FATTR4_WORD0_MAXREAD
130 | FATTR4_WORD0_MAXWRITE
131 | FATTR4_WORD0_LEASE_TIME,
132 0
133};
134
Manoj Naik830b8e32006-06-09 09:34:25 -0400135const u32 nfs4_fs_locations_bitmap[2] = {
136 FATTR4_WORD0_TYPE
137 | FATTR4_WORD0_CHANGE
138 | FATTR4_WORD0_SIZE
139 | FATTR4_WORD0_FSID
140 | FATTR4_WORD0_FILEID
141 | FATTR4_WORD0_FS_LOCATIONS,
142 FATTR4_WORD1_MODE
143 | FATTR4_WORD1_NUMLINKS
144 | FATTR4_WORD1_OWNER
145 | FATTR4_WORD1_OWNER_GROUP
146 | FATTR4_WORD1_RAWDEV
147 | FATTR4_WORD1_SPACE_USED
148 | FATTR4_WORD1_TIME_ACCESS
149 | FATTR4_WORD1_TIME_METADATA
150 | FATTR4_WORD1_TIME_MODIFY
151 | FATTR4_WORD1_MOUNTED_ON_FILEID
152};
153
Al Virobc4785c2006-10-19 23:28:51 -0700154static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 struct nfs4_readdir_arg *readdir)
156{
Al Viro0dbb4c62006-10-19 23:28:49 -0700157 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 BUG_ON(readdir->count < 80);
160 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000161 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
163 return;
164 }
165
166 readdir->cookie = 0;
167 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
168 if (cookie == 2)
169 return;
170
171 /*
172 * NFSv4 servers do not return entries for '.' and '..'
173 * Therefore, we fake these entries here. We let '.'
174 * have cookie 0 and '..' have cookie 1. Note that
175 * when talking to the server, we always send cookie 0
176 * instead of 1 or 2.
177 */
Al Viro0dbb4c62006-10-19 23:28:49 -0700178 start = p = kmap_atomic(*readdir->pages, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 if (cookie == 0) {
181 *p++ = xdr_one; /* next */
182 *p++ = xdr_zero; /* cookie, first word */
183 *p++ = xdr_one; /* cookie, second word */
184 *p++ = xdr_one; /* entry len */
185 memcpy(p, ".\0\0\0", 4); /* entry */
186 p++;
187 *p++ = xdr_one; /* bitmap length */
188 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
189 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400190 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 }
192
193 *p++ = xdr_one; /* next */
194 *p++ = xdr_zero; /* cookie, first word */
195 *p++ = xdr_two; /* cookie, second word */
196 *p++ = xdr_two; /* entry len */
197 memcpy(p, "..\0\0", 4); /* entry */
198 p++;
199 *p++ = xdr_one; /* bitmap length */
200 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
201 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400202 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 readdir->pgbase = (char *)p - (char *)start;
205 readdir->count -= readdir->pgbase;
206 kunmap_atomic(start, KM_USER0);
207}
208
Trond Myklebust65de8722008-12-23 15:21:44 -0500209static int nfs4_wait_clnt_recover(struct nfs_client *clp)
210{
211 int res;
212
213 might_sleep();
214
Trond Myklebuste005e802008-12-23 15:21:48 -0500215 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400216 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500217 return res;
218}
219
220static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
221{
222 int res = 0;
223
224 might_sleep();
225
226 if (*timeout <= 0)
227 *timeout = NFS4_POLL_RETRY_MIN;
228 if (*timeout > NFS4_POLL_RETRY_MAX)
229 *timeout = NFS4_POLL_RETRY_MAX;
230 schedule_timeout_killable(*timeout);
231 if (fatal_signal_pending(current))
232 res = -ERESTARTSYS;
233 *timeout <<= 1;
234 return res;
235}
236
237/* This is the error handling routine for processes that are allowed
238 * to sleep.
239 */
240static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
241{
242 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500243 struct nfs4_state *state = exception->state;
Trond Myklebust65de8722008-12-23 15:21:44 -0500244 int ret = errorcode;
245
246 exception->retry = 0;
247 switch(errorcode) {
248 case 0:
249 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500250 case -NFS4ERR_ADMIN_REVOKED:
251 case -NFS4ERR_BAD_STATEID:
252 case -NFS4ERR_OPENMODE:
253 if (state == NULL)
254 break;
255 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500256 goto do_state_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500257 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500258 if (state == NULL)
259 break;
260 nfs4_state_mark_reclaim_reboot(clp, state);
261 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust65de8722008-12-23 15:21:44 -0500262 case -NFS4ERR_EXPIRED:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500263 goto do_state_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500264#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400265 case -NFS4ERR_BADSESSION:
266 case -NFS4ERR_BADSLOT:
267 case -NFS4ERR_BAD_HIGH_SLOT:
268 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
269 case -NFS4ERR_DEADSESSION:
270 case -NFS4ERR_SEQ_FALSE_RETRY:
271 case -NFS4ERR_SEQ_MISORDERED:
272 dprintk("%s ERROR: %d Reset session\n", __func__,
273 errorcode);
Andy Adamson0b9e2d42009-12-04 16:02:14 -0500274 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -0400275 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500276 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500277#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500278 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500279 if (exception->timeout > HZ) {
280 /* We have retried a decent amount, time to
281 * fail
282 */
283 ret = -EBUSY;
284 break;
285 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500286 case -NFS4ERR_GRACE:
287 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500288 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500289 ret = nfs4_delay(server->client, &exception->timeout);
290 if (ret != 0)
291 break;
292 case -NFS4ERR_OLD_STATEID:
293 exception->retry = 1;
294 }
295 /* We failed to handle the error */
296 return nfs4_map_errors(ret);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500297do_state_recovery:
298 nfs4_schedule_state_recovery(clp);
299 ret = nfs4_wait_clnt_recover(clp);
300 if (ret == 0)
301 exception->retry = 1;
302 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500303}
304
305
Trond Myklebust452e9352010-07-31 14:29:06 -0400306static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 spin_lock(&clp->cl_lock);
309 if (time_before(clp->cl_last_renewal,timestamp))
310 clp->cl_last_renewal = timestamp;
311 spin_unlock(&clp->cl_lock);
312}
313
Trond Myklebust452e9352010-07-31 14:29:06 -0400314static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
315{
316 do_renew_lease(server->nfs_client, timestamp);
317}
318
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400319#if defined(CONFIG_NFS_V4_1)
320
Benny Halevy510b8172009-04-01 09:22:14 -0400321/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400322 * nfs4_free_slot - free a slot and efficiently update slot table.
323 *
324 * freeing a slot is trivially done by clearing its respective bit
325 * in the bitmap.
326 * If the freed slotid equals highest_used_slotid we want to update it
327 * so that the server would be able to size down the slot table if needed,
328 * otherwise we know that the highest_used_slotid is still in use.
329 * When updating highest_used_slotid there may be "holes" in the bitmap
330 * so we need to scan down from highest_used_slotid to 0 looking for the now
331 * highest slotid in use.
332 * If none found, highest_used_slotid is set to -1.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500333 *
334 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400335 */
336static void
337nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid)
338{
339 int slotid = free_slotid;
340
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400341 /* clear used bit in bitmap */
342 __clear_bit(slotid, tbl->used_slots);
343
344 /* update highest_used_slotid when it is freed */
345 if (slotid == tbl->highest_used_slotid) {
346 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500347 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400348 tbl->highest_used_slotid = slotid;
349 else
350 tbl->highest_used_slotid = -1;
351 }
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400352 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
353 free_slotid, tbl->highest_used_slotid);
354}
355
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800356/*
357 * Signal state manager thread if session is drained
358 */
359static void nfs41_check_drain_session_complete(struct nfs4_session *ses)
360{
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800361 struct rpc_task *task;
362
Trond Myklebusta2118c32010-06-16 09:52:26 -0400363 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800364 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
365 if (task)
366 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800367 return;
368 }
369
370 if (ses->fc_slot_table.highest_used_slotid != -1)
371 return;
372
373 dprintk("%s COMPLETE: Session Drained\n", __func__);
374 complete(&ses->complete);
375}
376
Trond Myklebustd185a332010-06-16 09:52:25 -0400377static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400378{
379 struct nfs4_slot_table *tbl;
380
Trond Myklebustd185a332010-06-16 09:52:25 -0400381 tbl = &res->sr_session->fc_slot_table;
Andy Adamson13615872009-04-01 09:22:17 -0400382 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE) {
Andy Adamson13615872009-04-01 09:22:17 -0400383 /* just wake up the next guy waiting since
384 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500385 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500386 return;
Andy Adamson13615872009-04-01 09:22:17 -0400387 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500388
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500389 spin_lock(&tbl->slot_tbl_lock);
390 nfs4_free_slot(tbl, res->sr_slotid);
Trond Myklebustd185a332010-06-16 09:52:25 -0400391 nfs41_check_drain_session_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500392 spin_unlock(&tbl->slot_tbl_lock);
393 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Andy Adamson13615872009-04-01 09:22:17 -0400394}
395
Trond Myklebust14516c32010-07-31 14:29:06 -0400396static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400397{
398 unsigned long timestamp;
399 struct nfs4_slot_table *tbl;
400 struct nfs4_slot *slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400401 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400402
403 /*
404 * sr_status remains 1 if an RPC level error occurred. The server
405 * may or may not have processed the sequence operation..
406 * Proceed as if the server received and processed the sequence
407 * operation.
408 */
409 if (res->sr_status == 1)
410 res->sr_status = NFS_OK;
411
412 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
413 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE)
414 goto out;
415
Trond Myklebust14516c32010-07-31 14:29:06 -0400416 tbl = &res->sr_session->fc_slot_table;
417 slot = tbl->slots + res->sr_slotid;
418
Andy Adamson691daf32009-12-04 15:55:39 -0500419 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400420 switch (res->sr_status) {
421 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400422 /* Update the slot's sequence and clientid lease timer */
423 ++slot->seq_nr;
424 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400425 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400426 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500427 /* Check sequence flags */
Alexandros Batsakis71358402010-02-05 03:45:05 -0800428 if (atomic_read(&clp->cl_count) > 1)
429 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust14516c32010-07-31 14:29:06 -0400430 break;
431 case -NFS4ERR_DELAY:
432 /* The server detected a resend of the RPC call and
433 * returned NFS4ERR_DELAY as per Section 2.10.6.2
434 * of RFC5661.
435 */
436 dprintk("%s: slot=%d seq=%d: Operation in progress\n",
437 __func__, res->sr_slotid, slot->seq_nr);
438 goto out_retry;
439 default:
440 /* Just update the slot sequence no. */
441 ++slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400442 }
443out:
444 /* The session may be reset by one of the error handlers. */
445 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400446 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400447 return 1;
448out_retry:
449 rpc_restart_call(task);
450 /* FIXME: rpc_restart_call() should be made to return success/fail */
451 if (task->tk_action == NULL)
452 goto out;
453 rpc_delay(task, NFS4_POLL_RETRY_MAX);
454 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400455}
456
Trond Myklebust14516c32010-07-31 14:29:06 -0400457static int nfs4_sequence_done(struct rpc_task *task,
458 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400459{
Trond Myklebust14516c32010-07-31 14:29:06 -0400460 if (res->sr_session == NULL)
461 return 1;
462 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400463}
464
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400465/*
Benny Halevy510b8172009-04-01 09:22:14 -0400466 * nfs4_find_slot - efficiently look for a free slot
467 *
468 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
469 * If found, we mark the slot as used, update the highest_used_slotid,
470 * and respectively set up the sequence operation args.
471 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400472 *
473 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400474 */
475static u8
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800476nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400477{
478 int slotid;
479 u8 ret_id = NFS4_MAX_SLOT_TABLE;
480 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
481
Benny Halevy510b8172009-04-01 09:22:14 -0400482 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
483 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
484 tbl->max_slots);
485 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
486 if (slotid >= tbl->max_slots)
487 goto out;
488 __set_bit(slotid, tbl->used_slots);
489 if (slotid > tbl->highest_used_slotid)
490 tbl->highest_used_slotid = slotid;
491 ret_id = slotid;
492out:
493 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
494 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400495 return ret_id;
496}
497
Andy Adamsonce5039c2009-04-01 09:22:13 -0400498static int nfs41_setup_sequence(struct nfs4_session *session,
499 struct nfs4_sequence_args *args,
500 struct nfs4_sequence_res *res,
501 int cache_reply,
502 struct rpc_task *task)
503{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400504 struct nfs4_slot *slot;
505 struct nfs4_slot_table *tbl;
506 u8 slotid;
507
508 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400509 /* slot already allocated? */
510 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
511 return 0;
512
Andy Adamsonce5039c2009-04-01 09:22:13 -0400513 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400514 tbl = &session->fc_slot_table;
515
516 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400517 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800518 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamsonea028ac2009-12-04 15:55:38 -0500519 /*
520 * The state manager will wait until the slot table is empty.
521 * Schedule the reset thread
522 */
Andy Adamson05f0d232009-12-04 15:55:37 -0500523 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400524 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500525 dprintk("%s Schedule Session Reset\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500526 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400527 }
528
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800529 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
530 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
531 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
532 spin_unlock(&tbl->slot_tbl_lock);
533 dprintk("%s enforce FIFO order\n", __func__);
534 return -EAGAIN;
535 }
536
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800537 slotid = nfs4_find_slot(tbl);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400538 if (slotid == NFS4_MAX_SLOT_TABLE) {
539 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
540 spin_unlock(&tbl->slot_tbl_lock);
541 dprintk("<-- %s: no free slots\n", __func__);
542 return -EAGAIN;
543 }
544 spin_unlock(&tbl->slot_tbl_lock);
545
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800546 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400547 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400548 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400549 args->sa_slotid = slotid;
550 args->sa_cache_this = cache_reply;
551
552 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
553
Benny Halevy99fe60d2009-04-01 09:22:29 -0400554 res->sr_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400555 res->sr_slotid = slotid;
556 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400557 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400558 /*
559 * sr_status is only set in decode_sequence, and so will remain
560 * set to 1 if an rpc level failure occurs.
561 */
562 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400563 return 0;
564}
565
Trond Myklebust035168a2010-06-16 09:52:26 -0400566int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400567 struct nfs4_sequence_args *args,
568 struct nfs4_sequence_res *res,
569 int cache_reply,
570 struct rpc_task *task)
571{
Trond Myklebust035168a2010-06-16 09:52:26 -0400572 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400573 int ret = 0;
574
Trond Myklebust035168a2010-06-16 09:52:26 -0400575 if (session == NULL) {
576 args->sa_session = NULL;
577 res->sr_session = NULL;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400578 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400579 }
580
581 dprintk("--> %s clp %p session %p sr_slotid %d\n",
582 __func__, session->clp, session, res->sr_slotid);
583
584 ret = nfs41_setup_sequence(session, args, res, cache_reply,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400585 task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400586out:
587 dprintk("<-- %s status=%d\n", __func__, ret);
588 return ret;
589}
590
591struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400592 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400593 struct nfs4_sequence_args *seq_args;
594 struct nfs4_sequence_res *seq_res;
595 int cache_reply;
596};
597
598static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
599{
600 struct nfs41_call_sync_data *data = calldata;
601
Trond Myklebust035168a2010-06-16 09:52:26 -0400602 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
603
604 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400605 data->seq_res, data->cache_reply, task))
606 return;
607 rpc_call_start(task);
608}
609
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800610static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
611{
612 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
613 nfs41_call_sync_prepare(task, calldata);
614}
615
Andy Adamson69ab40c2009-04-01 09:22:19 -0400616static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
617{
618 struct nfs41_call_sync_data *data = calldata;
619
Trond Myklebust14516c32010-07-31 14:29:06 -0400620 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400621}
622
Andy Adamsonce5039c2009-04-01 09:22:13 -0400623struct rpc_call_ops nfs41_call_sync_ops = {
624 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400625 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400626};
627
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800628struct rpc_call_ops nfs41_call_priv_sync_ops = {
629 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
630 .rpc_call_done = nfs41_call_sync_done,
631};
632
Trond Myklebust035168a2010-06-16 09:52:26 -0400633static int nfs4_call_sync_sequence(struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400634 struct rpc_message *msg,
635 struct nfs4_sequence_args *args,
636 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800637 int cache_reply,
638 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400639{
640 int ret;
641 struct rpc_task *task;
642 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400643 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400644 .seq_args = args,
645 .seq_res = res,
646 .cache_reply = cache_reply,
647 };
648 struct rpc_task_setup task_setup = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400649 .rpc_client = server->client,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400650 .rpc_message = msg,
651 .callback_ops = &nfs41_call_sync_ops,
652 .callback_data = &data
653 };
654
655 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800656 if (privileged)
657 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400658 task = rpc_run_task(&task_setup);
659 if (IS_ERR(task))
660 ret = PTR_ERR(task);
661 else {
662 ret = task->tk_status;
663 rpc_put_task(task);
664 }
665 return ret;
666}
667
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400668int _nfs4_call_sync_session(struct nfs_server *server,
669 struct rpc_message *msg,
670 struct nfs4_sequence_args *args,
671 struct nfs4_sequence_res *res,
672 int cache_reply)
673{
Trond Myklebust035168a2010-06-16 09:52:26 -0400674 return nfs4_call_sync_sequence(server, msg, args, res, cache_reply, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400675}
676
Trond Myklebustdf896452010-06-16 09:52:26 -0400677#else
Trond Myklebust14516c32010-07-31 14:29:06 -0400678static int nfs4_sequence_done(struct rpc_task *task,
679 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400680{
Trond Myklebust14516c32010-07-31 14:29:06 -0400681 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400682}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400683#endif /* CONFIG_NFS_V4_1 */
684
685int _nfs4_call_sync(struct nfs_server *server,
686 struct rpc_message *msg,
687 struct nfs4_sequence_args *args,
688 struct nfs4_sequence_res *res,
689 int cache_reply)
690{
Benny Halevyf3752972009-04-01 09:22:04 -0400691 args->sa_session = res->sr_session = NULL;
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400692 return rpc_call_sync(server->client, msg, 0);
693}
694
695#define nfs4_call_sync(server, msg, args, res, cache_reply) \
Trond Myklebust97dc1352010-06-16 09:52:26 -0400696 (server)->nfs_client->cl_mvops->call_sync((server), (msg), &(args)->seq_args, \
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400697 &(res)->seq_res, (cache_reply))
698
Trond Myklebust38478b22006-05-25 01:40:57 -0400699static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Trond Myklebust38478b22006-05-25 01:40:57 -0400701 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Trond Myklebust38478b22006-05-25 01:40:57 -0400703 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400704 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
705 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400706 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400707 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400708 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100711struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400712 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100713 struct nfs_openargs o_arg;
714 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100715 struct nfs_open_confirmargs c_arg;
716 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100717 struct nfs_fattr f_attr;
718 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400719 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100720 struct dentry *dir;
721 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400722 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100723 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100724 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400725 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100726 int rpc_status;
727 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100728};
729
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400730
731static void nfs4_init_opendata_res(struct nfs4_opendata *p)
732{
733 p->o_res.f_attr = &p->f_attr;
734 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400735 p->o_res.seqid = p->o_arg.seqid;
736 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400737 p->o_res.server = p->o_arg.server;
738 nfs_fattr_init(&p->f_attr);
739 nfs_fattr_init(&p->dir_attr);
Benny Halevy578e4582009-06-18 22:01:23 -0400740 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400741}
742
Trond Myklebustad389da2007-06-05 12:30:00 -0400743static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500744 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400745 const struct iattr *attrs,
746 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100747{
Trond Myklebustad389da2007-06-05 12:30:00 -0400748 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100749 struct inode *dir = parent->d_inode;
750 struct nfs_server *server = NFS_SERVER(dir);
751 struct nfs4_opendata *p;
752
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400753 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100754 if (p == NULL)
755 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400756 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100757 if (p->o_arg.seqid == NULL)
758 goto err_free;
Al Virof6948692010-01-30 13:51:04 -0500759 path_get(path);
760 p->path = *path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100761 p->dir = parent;
762 p->owner = sp;
763 atomic_inc(&sp->so_count);
764 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500765 p->o_arg.open_flags = flags;
766 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400767 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400768 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400769 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100770 p->o_arg.server = server;
771 p->o_arg.bitmask = server->attr_bitmask;
772 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400773 if (flags & O_CREAT) {
774 u32 *s;
775
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100776 p->o_arg.u.attrs = &p->attrs;
777 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400778 s = (u32 *) p->o_arg.u.verifier.data;
779 s[0] = jiffies;
780 s[1] = current->pid;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100781 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100782 p->c_arg.fh = &p->o_res.fh;
783 p->c_arg.stateid = &p->o_res.stateid;
784 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400785 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400786 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100787 return p;
788err_free:
789 kfree(p);
790err:
791 dput(parent);
792 return NULL;
793}
794
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400795static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100796{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400797 struct nfs4_opendata *p = container_of(kref,
798 struct nfs4_opendata, kref);
799
800 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400801 if (p->state != NULL)
802 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400803 nfs4_put_state_owner(p->owner);
804 dput(p->dir);
Jan Blunck31f31db12008-05-02 13:42:45 -0700805 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400806 kfree(p);
807}
808
809static void nfs4_opendata_put(struct nfs4_opendata *p)
810{
811 if (p != NULL)
812 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100813}
814
Trond Myklebust06f814a2006-01-03 09:55:07 +0100815static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
816{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100817 int ret;
818
Trond Myklebust06f814a2006-01-03 09:55:07 +0100819 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100820 return ret;
821}
822
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500823static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400824{
825 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500826
827 if (open_mode & O_EXCL)
828 goto out;
829 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400830 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500831 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
832 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400833 break;
834 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500835 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
836 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400837 break;
838 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500839 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
840 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400841 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500842out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400843 return ret;
844}
845
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500846static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400847{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500848 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400849 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500850 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400851 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500852 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400853 return 1;
854}
855
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500856static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100857{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500858 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100859 case FMODE_WRITE:
860 state->n_wronly++;
861 break;
862 case FMODE_READ:
863 state->n_rdonly++;
864 break;
865 case FMODE_READ|FMODE_WRITE:
866 state->n_rdwr++;
867 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500868 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100869}
870
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500871static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400872{
873 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
874 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
875 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500876 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400877 case FMODE_READ:
878 set_bit(NFS_O_RDONLY_STATE, &state->flags);
879 break;
880 case FMODE_WRITE:
881 set_bit(NFS_O_WRONLY_STATE, &state->flags);
882 break;
883 case FMODE_READ|FMODE_WRITE:
884 set_bit(NFS_O_RDWR_STATE, &state->flags);
885 }
886}
887
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500888static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400889{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400890 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500891 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400892 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400893}
894
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500895static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400897 /*
898 * Protect the call to nfs4_state_set_mode_locked and
899 * serialise the stateid update
900 */
901 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400902 if (deleg_stateid != NULL) {
903 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
904 set_bit(NFS_DELEGATED_STATE, &state->flags);
905 }
906 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500907 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400908 write_sequnlock(&state->seqlock);
909 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500910 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700911 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912}
913
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500914static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500915{
916 struct nfs_inode *nfsi = NFS_I(state->inode);
917 struct nfs_delegation *deleg_cur;
918 int ret = 0;
919
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500920 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500921
922 rcu_read_lock();
923 deleg_cur = rcu_dereference(nfsi->delegation);
924 if (deleg_cur == NULL)
925 goto no_delegation;
926
927 spin_lock(&deleg_cur->lock);
928 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500929 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500930 goto no_delegation_unlock;
931
932 if (delegation == NULL)
933 delegation = &deleg_cur->stateid;
934 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
935 goto no_delegation_unlock;
936
Trond Myklebustb7391f42008-12-23 15:21:52 -0500937 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500938 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500939 ret = 1;
940no_delegation_unlock:
941 spin_unlock(&deleg_cur->lock);
942no_delegation:
943 rcu_read_unlock();
944
945 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500946 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500947 ret = 1;
948 }
949
950 return ret;
951}
952
953
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500954static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400955{
956 struct nfs_delegation *delegation;
957
958 rcu_read_lock();
959 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500960 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400961 rcu_read_unlock();
962 return;
963 }
964 rcu_read_unlock();
965 nfs_inode_return_delegation(inode);
966}
967
Trond Myklebust6ee41262007-07-08 14:11:36 -0400968static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400969{
970 struct nfs4_state *state = opendata->state;
971 struct nfs_inode *nfsi = NFS_I(state->inode);
972 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500973 int open_mode = opendata->o_arg.open_flags & O_EXCL;
974 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400975 nfs4_stateid stateid;
976 int ret = -EAGAIN;
977
Trond Myklebustaac00a82007-07-05 19:02:21 -0400978 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500979 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400980 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500981 if (can_open_cached(state, fmode, open_mode)) {
982 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400983 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400984 goto out_return_state;
985 }
986 spin_unlock(&state->owner->so_lock);
987 }
Trond Myklebust34310432008-12-23 15:21:38 -0500988 rcu_read_lock();
989 delegation = rcu_dereference(nfsi->delegation);
990 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500991 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -0500992 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -0400993 break;
Trond Myklebust34310432008-12-23 15:21:38 -0500994 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400995 /* Save the delegation */
996 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
997 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -0400998 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400999 if (ret != 0)
1000 goto out;
1001 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001002
1003 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001004 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001005 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001006 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001007out:
1008 return ERR_PTR(ret);
1009out_return_state:
1010 atomic_inc(&state->count);
1011 return state;
1012}
1013
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001014static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1015{
1016 struct inode *inode;
1017 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001018 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001019 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001020
Trond Myklebustaac00a82007-07-05 19:02:21 -04001021 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001022 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001023 goto out;
1024 }
1025
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001026 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001027 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001028 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001029 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001030 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001031 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001032 goto err;
1033 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001034 state = nfs4_get_open_state(inode, data->owner);
1035 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001036 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001037 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001038 int delegation_flags = 0;
1039
Trond Myklebust003707c2007-07-05 18:07:55 -04001040 rcu_read_lock();
1041 delegation = rcu_dereference(NFS_I(inode)->delegation);
1042 if (delegation)
1043 delegation_flags = delegation->flags;
1044 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -05001045 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001046 nfs_inode_set_delegation(state->inode,
1047 data->owner->so_cred,
1048 &data->o_res);
1049 else
1050 nfs_inode_reclaim_delegation(state->inode,
1051 data->owner->so_cred,
1052 &data->o_res);
1053 }
Trond Myklebust34310432008-12-23 15:21:38 -05001054
1055 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001056 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001057 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001058out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001059 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001060err_put_inode:
1061 iput(inode);
1062err:
1063 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001064}
1065
Trond Myklebust864472e2006-01-03 09:55:15 +01001066static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1067{
1068 struct nfs_inode *nfsi = NFS_I(state->inode);
1069 struct nfs_open_context *ctx;
1070
1071 spin_lock(&state->inode->i_lock);
1072 list_for_each_entry(ctx, &nfsi->open_files, list) {
1073 if (ctx->state != state)
1074 continue;
1075 get_nfs_open_context(ctx);
1076 spin_unlock(&state->inode->i_lock);
1077 return ctx;
1078 }
1079 spin_unlock(&state->inode->i_lock);
1080 return ERR_PTR(-ENOENT);
1081}
1082
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001083static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1084{
1085 struct nfs4_opendata *opendata;
1086
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001087 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001088 if (opendata == NULL)
1089 return ERR_PTR(-ENOMEM);
1090 opendata->state = state;
1091 atomic_inc(&state->count);
1092 return opendata;
1093}
1094
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001095static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001096{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001097 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001098 int ret;
1099
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001100 opendata->o_arg.open_flags = 0;
1101 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001102 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1103 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1104 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001105 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001106 if (ret != 0)
1107 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001108 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001109 if (IS_ERR(newstate))
1110 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001111 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001112 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001113 return 0;
1114}
1115
1116static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1117{
Trond Myklebust864472e2006-01-03 09:55:15 +01001118 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001119 int ret;
1120
1121 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001122 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001123 smp_rmb();
1124 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001125 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001126 if (ret != 0)
1127 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001128 if (newstate != state)
1129 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001130 }
1131 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001132 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001133 if (ret != 0)
1134 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001135 if (newstate != state)
1136 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001137 }
1138 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001139 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001140 if (ret != 0)
1141 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001142 if (newstate != state)
1143 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001144 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001145 /*
1146 * We may have performed cached opens for all three recoveries.
1147 * Check if we need to update the current stateid.
1148 */
1149 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1150 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001151 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001152 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1153 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001154 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001155 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001156 return 0;
1157}
1158
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159/*
1160 * OPEN_RECLAIM:
1161 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001163static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001165 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001166 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001167 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 int status;
1169
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001170 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1171 if (IS_ERR(opendata))
1172 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001173 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1174 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001175 rcu_read_lock();
1176 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001177 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001178 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001179 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001180 opendata->o_arg.u.delegation_type = delegation_type;
1181 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001182 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 return status;
1184}
1185
Trond Myklebust539cd032007-06-05 11:46:42 -04001186static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
1188 struct nfs_server *server = NFS_SERVER(state->inode);
1189 struct nfs4_exception exception = { };
1190 int err;
1191 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001192 err = _nfs4_do_open_reclaim(ctx, state);
Jeff Layton2c643482010-01-07 09:42:03 -05001193 if (err != -NFS4ERR_DELAY && err != -EKEYEXPIRED)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001194 break;
1195 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 } while (exception.retry);
1197 return err;
1198}
1199
Trond Myklebust864472e2006-01-03 09:55:15 +01001200static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1201{
1202 struct nfs_open_context *ctx;
1203 int ret;
1204
1205 ctx = nfs4_state_find_open_context(state);
1206 if (IS_ERR(ctx))
1207 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001208 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001209 put_nfs_open_context(ctx);
1210 return ret;
1211}
1212
Trond Myklebust13437e12007-07-06 15:10:43 -04001213static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001215 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001216 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001218 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1219 if (IS_ERR(opendata))
1220 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001221 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001222 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001223 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001224 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001225 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001226 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227}
1228
Trond Myklebust13437e12007-07-06 15:10:43 -04001229int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
1231 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001232 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 int err;
1234 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001235 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 switch (err) {
1237 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001238 case -ENOENT:
1239 case -ESTALE:
1240 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001241 case -NFS4ERR_BADSESSION:
1242 case -NFS4ERR_BADSLOT:
1243 case -NFS4ERR_BAD_HIGH_SLOT:
1244 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1245 case -NFS4ERR_DEADSESSION:
1246 nfs4_schedule_state_recovery(
1247 server->nfs_client);
1248 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 case -NFS4ERR_STALE_CLIENTID:
1250 case -NFS4ERR_STALE_STATEID:
1251 case -NFS4ERR_EXPIRED:
1252 /* Don't recall a delegation if it was lost */
David Howells7539bba2006-08-22 20:06:09 -04001253 nfs4_schedule_state_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001254 goto out;
1255 case -ERESTARTSYS:
1256 /*
1257 * The show must go on: exit, but mark the
1258 * stateid as needing recovery.
1259 */
1260 case -NFS4ERR_ADMIN_REVOKED:
1261 case -NFS4ERR_BAD_STATEID:
1262 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
1263 case -ENOMEM:
1264 err = 0;
1265 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 }
1267 err = nfs4_handle_exception(server, err, &exception);
1268 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001269out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 return err;
1271}
1272
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001273static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1274{
1275 struct nfs4_opendata *data = calldata;
1276
1277 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001278 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001279 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1280 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001281 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001282 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001283 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001284 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001285}
1286
1287static void nfs4_open_confirm_release(void *calldata)
1288{
1289 struct nfs4_opendata *data = calldata;
1290 struct nfs4_state *state = NULL;
1291
1292 /* If this request hasn't been cancelled, do nothing */
1293 if (data->cancelled == 0)
1294 goto out_free;
1295 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001296 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001297 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001298 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001299 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001300 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001301out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001302 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001303}
1304
1305static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001306 .rpc_call_done = nfs4_open_confirm_done,
1307 .rpc_release = nfs4_open_confirm_release,
1308};
1309
1310/*
1311 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1312 */
1313static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1314{
1315 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1316 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001317 struct rpc_message msg = {
1318 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1319 .rpc_argp = &data->c_arg,
1320 .rpc_resp = &data->c_res,
1321 .rpc_cred = data->owner->so_cred,
1322 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001323 struct rpc_task_setup task_setup_data = {
1324 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001325 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001326 .callback_ops = &nfs4_open_confirm_ops,
1327 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001328 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001329 .flags = RPC_TASK_ASYNC,
1330 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 int status;
1332
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001333 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001334 data->rpc_done = 0;
1335 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001336 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001337 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001338 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001339 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001340 status = nfs4_wait_for_completion_rpc_task(task);
1341 if (status != 0) {
1342 data->cancelled = 1;
1343 smp_wmb();
1344 } else
1345 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001346 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 return status;
1348}
1349
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001350static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001352 struct nfs4_opendata *data = calldata;
1353 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001354
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001355 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1356 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001357 /*
1358 * Check if we still need to send an OPEN call, or if we can use
1359 * a delegation instead.
1360 */
1361 if (data->state != NULL) {
1362 struct nfs_delegation *delegation;
1363
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001364 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001365 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001366 rcu_read_lock();
1367 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1368 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001369 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001370 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001371 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001372 }
1373 rcu_read_unlock();
1374 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001375 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001376 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001377 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001378 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001379 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001380 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1381 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001382 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001383 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001384 &data->o_arg.seq_args,
1385 &data->o_res.seq_res, 1, task))
1386 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001387 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001388 return;
1389out_no_action:
1390 task->tk_action = NULL;
1391
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001392}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001394static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1395{
1396 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1397 nfs4_open_prepare(task, calldata);
1398}
1399
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001400static void nfs4_open_done(struct rpc_task *task, void *calldata)
1401{
1402 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001404 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001405
Trond Myklebust14516c32010-07-31 14:29:06 -04001406 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1407 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001408
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001409 if (task->tk_status == 0) {
1410 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001411 case S_IFREG:
1412 break;
1413 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001414 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001415 break;
1416 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001417 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001418 break;
1419 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001420 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001421 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001422 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001423 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1424 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001425 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001426 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001427}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001428
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001429static void nfs4_open_release(void *calldata)
1430{
1431 struct nfs4_opendata *data = calldata;
1432 struct nfs4_state *state = NULL;
1433
1434 /* If this request hasn't been cancelled, do nothing */
1435 if (data->cancelled == 0)
1436 goto out_free;
1437 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001438 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001439 goto out_free;
1440 /* In case we need an open_confirm, no cleanup! */
1441 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1442 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001443 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001444 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001445 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001446out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001447 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001448}
1449
1450static const struct rpc_call_ops nfs4_open_ops = {
1451 .rpc_call_prepare = nfs4_open_prepare,
1452 .rpc_call_done = nfs4_open_done,
1453 .rpc_release = nfs4_open_release,
1454};
1455
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001456static const struct rpc_call_ops nfs4_recover_open_ops = {
1457 .rpc_call_prepare = nfs4_recover_open_prepare,
1458 .rpc_call_done = nfs4_open_done,
1459 .rpc_release = nfs4_open_release,
1460};
1461
1462static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001463{
1464 struct inode *dir = data->dir->d_inode;
1465 struct nfs_server *server = NFS_SERVER(dir);
1466 struct nfs_openargs *o_arg = &data->o_arg;
1467 struct nfs_openres *o_res = &data->o_res;
1468 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001469 struct rpc_message msg = {
1470 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1471 .rpc_argp = o_arg,
1472 .rpc_resp = o_res,
1473 .rpc_cred = data->owner->so_cred,
1474 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001475 struct rpc_task_setup task_setup_data = {
1476 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001477 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001478 .callback_ops = &nfs4_open_ops,
1479 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001480 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001481 .flags = RPC_TASK_ASYNC,
1482 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001483 int status;
1484
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001485 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001486 data->rpc_done = 0;
1487 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001488 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001489 if (isrecover)
1490 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001491 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001492 if (IS_ERR(task))
1493 return PTR_ERR(task);
1494 status = nfs4_wait_for_completion_rpc_task(task);
1495 if (status != 0) {
1496 data->cancelled = 1;
1497 smp_wmb();
1498 } else
1499 status = data->rpc_status;
1500 rpc_put_task(task);
1501
1502 return status;
1503}
1504
1505static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1506{
1507 struct inode *dir = data->dir->d_inode;
1508 struct nfs_openres *o_res = &data->o_res;
1509 int status;
1510
1511 status = nfs4_run_open_task(data, 1);
1512 if (status != 0 || !data->rpc_done)
1513 return status;
1514
1515 nfs_refresh_inode(dir, o_res->dir_attr);
1516
1517 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1518 status = _nfs4_proc_open_confirm(data);
1519 if (status != 0)
1520 return status;
1521 }
1522
1523 return status;
1524}
1525
1526/*
1527 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1528 */
1529static int _nfs4_proc_open(struct nfs4_opendata *data)
1530{
1531 struct inode *dir = data->dir->d_inode;
1532 struct nfs_server *server = NFS_SERVER(dir);
1533 struct nfs_openargs *o_arg = &data->o_arg;
1534 struct nfs_openres *o_res = &data->o_res;
1535 int status;
1536
1537 status = nfs4_run_open_task(data, 0);
Trond Myklebust3e309912007-07-07 13:19:59 -04001538 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001539 return status;
1540
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001541 if (o_arg->open_flags & O_CREAT) {
1542 update_changeattr(dir, &o_res->cinfo);
1543 nfs_post_op_update_inode(dir, o_res->dir_attr);
1544 } else
1545 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001546 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1547 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001549 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001551 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 }
1553 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001554 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001555 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556}
1557
Trond Myklebust10afec92007-05-14 17:16:04 -04001558static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +01001559{
David Howells7539bba2006-08-22 20:06:09 -04001560 struct nfs_client *clp = server->nfs_client;
Trond Myklebusta78cb572009-08-09 15:06:19 -04001561 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001562 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001563
Trond Myklebusta78cb572009-08-09 15:06:19 -04001564 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001565 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001566 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001567 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001568 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1569 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001570 break;
Trond Myklebust58d97142006-01-03 09:55:24 +01001571 nfs4_schedule_state_recovery(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001572 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001573 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001574 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001575}
1576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577/*
1578 * OPEN_EXPIRED:
1579 * reclaim state on the server after a network partition.
1580 * Assumes caller holds the appropriate lock
1581 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001582static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001584 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001585 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001587 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1588 if (IS_ERR(opendata))
1589 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001590 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001591 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001592 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001593 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001594 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595}
1596
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001597static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001598{
Trond Myklebust539cd032007-06-05 11:46:42 -04001599 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001600 struct nfs4_exception exception = { };
1601 int err;
1602
1603 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001604 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001605 switch (err) {
1606 default:
1607 goto out;
1608 case -NFS4ERR_GRACE:
1609 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05001610 case -EKEYEXPIRED:
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001611 nfs4_handle_exception(server, err, &exception);
1612 err = 0;
1613 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001614 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001615out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001616 return err;
1617}
1618
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1620{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001622 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Trond Myklebust864472e2006-01-03 09:55:15 +01001624 ctx = nfs4_state_find_open_context(state);
1625 if (IS_ERR(ctx))
1626 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001627 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001628 put_nfs_open_context(ctx);
1629 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630}
1631
1632/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001633 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1634 * fields corresponding to attributes that were used to store the verifier.
1635 * Make sure we clobber those fields in the later setattr call
1636 */
1637static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1638{
1639 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1640 !(sattr->ia_valid & ATTR_ATIME_SET))
1641 sattr->ia_valid |= ATTR_ATIME;
1642
1643 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1644 !(sattr->ia_valid & ATTR_MTIME_SET))
1645 sattr->ia_valid |= ATTR_MTIME;
1646}
1647
1648/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001649 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001651static 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 -07001652{
1653 struct nfs4_state_owner *sp;
1654 struct nfs4_state *state = NULL;
1655 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001656 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001657 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
1659 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 status = -ENOMEM;
1661 if (!(sp = nfs4_get_state_owner(server, cred))) {
1662 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1663 goto out_err;
1664 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001665 status = nfs4_recover_expired_lease(server);
1666 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001667 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001668 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001669 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001670 status = -ENOMEM;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001671 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001672 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001673 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Trond Myklebustaac00a82007-07-05 19:02:21 -04001675 if (path->dentry->d_inode != NULL)
1676 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1677
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001678 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001680 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001682 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001683 status = PTR_ERR(state);
1684 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001685 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001686 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001687 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001688
1689 if (opendata->o_arg.open_flags & O_EXCL) {
1690 nfs4_exclusive_attrset(opendata, sattr);
1691
1692 nfs_fattr_init(opendata->o_res.f_attr);
1693 status = nfs4_do_setattr(state->inode, cred,
1694 opendata->o_res.f_attr, sattr,
1695 state);
1696 if (status == 0)
1697 nfs_setattr_update_inode(state->inode, sattr);
1698 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1699 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001700 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 *res = state;
1703 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001704err_opendata_put:
1705 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001706err_put_state_owner:
1707 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 *res = NULL;
1710 return status;
1711}
1712
1713
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001714static 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 -07001715{
1716 struct nfs4_exception exception = { };
1717 struct nfs4_state *res;
1718 int status;
1719
1720 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001721 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 if (status == 0)
1723 break;
1724 /* NOTE: BAD_SEQID means the server and client disagree about the
1725 * book-keeping w.r.t. state-changing operations
1726 * (OPEN/CLOSE/LOCK/LOCKU...)
1727 * It is actually a sign of a bug on the client or on the server.
1728 *
1729 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001730 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 * have unhashed the old state_owner for us, and that we can
1732 * therefore safely retry using a new one. We should still warn
1733 * the user though...
1734 */
1735 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001736 printk(KERN_WARNING "NFS: v4 server %s "
1737 " returned a bad sequence-id error!\n",
1738 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 exception.retry = 1;
1740 continue;
1741 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001742 /*
1743 * BAD_STATEID on OPEN means that the server cancelled our
1744 * state before it received the OPEN_CONFIRM.
1745 * Recover by retrying the request as per the discussion
1746 * on Page 181 of RFC3530.
1747 */
1748 if (status == -NFS4ERR_BAD_STATEID) {
1749 exception.retry = 1;
1750 continue;
1751 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001752 if (status == -EAGAIN) {
1753 /* We must have found a delegation */
1754 exception.retry = 1;
1755 continue;
1756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1758 status, &exception));
1759 } while (exception.retry);
1760 return res;
1761}
1762
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001763static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1764 struct nfs_fattr *fattr, struct iattr *sattr,
1765 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001767 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001769 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 .iap = sattr,
1771 .server = server,
1772 .bitmask = server->attr_bitmask,
1773 };
1774 struct nfs_setattrres res = {
1775 .fattr = fattr,
1776 .server = server,
1777 };
1778 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001779 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1780 .rpc_argp = &arg,
1781 .rpc_resp = &res,
1782 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001784 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001785 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Trond Myklebust0e574af2005-10-27 22:12:38 -04001787 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001789 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1790 /* Use that stateid */
1791 } else if (state != NULL) {
Trond Myklebust77041ed2010-07-01 12:49:11 -04001792 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001793 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1795
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001796 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001797 if (status == 0 && state != NULL)
1798 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001799 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800}
1801
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001802static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1803 struct nfs_fattr *fattr, struct iattr *sattr,
1804 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001806 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 struct nfs4_exception exception = { };
1808 int err;
1809 do {
1810 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001811 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 &exception);
1813 } while (exception.retry);
1814 return err;
1815}
1816
1817struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001818 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 struct inode *inode;
1820 struct nfs4_state *state;
1821 struct nfs_closeargs arg;
1822 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001823 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001824 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825};
1826
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001827static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001828{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001829 struct nfs4_closedata *calldata = data;
1830 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001831
1832 nfs4_put_open_state(calldata->state);
1833 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001834 nfs4_put_state_owner(sp);
Jan Blunck31f31db12008-05-02 13:42:45 -07001835 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001836 kfree(calldata);
1837}
1838
Trond Myklebust88069f72009-12-08 08:33:16 -05001839static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1840 fmode_t fmode)
1841{
1842 spin_lock(&state->owner->so_lock);
1843 if (!(fmode & FMODE_READ))
1844 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1845 if (!(fmode & FMODE_WRITE))
1846 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1847 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1848 spin_unlock(&state->owner->so_lock);
1849}
1850
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001851static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001853 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 struct nfs_server *server = NFS_SERVER(calldata->inode);
1856
Trond Myklebust14516c32010-07-31 14:29:06 -04001857 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1858 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 /* hmm. we are done with the inode, and in the process of freeing
1860 * the state_owner. we keep this around to process errors
1861 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 switch (task->tk_status) {
1863 case 0:
Trond Myklebust45328c32007-07-26 17:47:34 -04001864 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001865 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001866 nfs4_close_clear_stateid_flags(state,
1867 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 break;
1869 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001870 case -NFS4ERR_OLD_STATEID:
1871 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001873 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001874 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001876 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1877 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001879 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001880 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881}
1882
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001883static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001885 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001886 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001887 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001888
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001889 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001890 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001891
Trond Myklebust88069f72009-12-08 08:33:16 -05001892 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1893 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001894 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001895 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001896 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001897 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001898 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1899 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1900 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001901 }
1902 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001903 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1904 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1905 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001906 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001907 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001908 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05001909
1910 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001911 /* Note: exit _without_ calling nfs4_close_done */
1912 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001913 return;
1914 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001915
1916 if (calldata->arg.fmode == 0)
1917 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
1918
Trond Myklebust516a6af2005-10-27 22:12:41 -04001919 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001920 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001921 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Andy Adamson19ddab02009-04-01 09:22:20 -04001922 &calldata->arg.seq_args, &calldata->res.seq_res,
1923 1, task))
1924 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001925 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926}
1927
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001928static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001929 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001930 .rpc_call_done = nfs4_close_done,
1931 .rpc_release = nfs4_free_closedata,
1932};
1933
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934/*
1935 * It is possible for data to be read/written from a mem-mapped file
1936 * after the sys_close call (which hits the vfs layer as a flush).
1937 * This means that we can't safely call nfsv4 close on a file until
1938 * the inode is cleared. This in turn means that we are not good
1939 * NFSv4 citizens - we do not indicate to the server to update the file's
1940 * share state even when we are done with one of the three share
1941 * stateid's in the inode.
1942 *
1943 * NOTE: Caller must be holding the sp->so_owner semaphore!
1944 */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001945int nfs4_do_close(struct path *path, struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001947 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001949 struct nfs4_state_owner *sp = state->owner;
1950 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001951 struct rpc_message msg = {
1952 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1953 .rpc_cred = state->owner->so_cred,
1954 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001955 struct rpc_task_setup task_setup_data = {
1956 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001957 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001958 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05001959 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001960 .flags = RPC_TASK_ASYNC,
1961 };
Trond Myklebust95121352005-10-18 14:20:12 -07001962 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001964 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001966 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001967 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001969 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04001970 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001972 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07001973 if (calldata->arg.seqid == NULL)
1974 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001975 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001976 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001977 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04001978 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001979 calldata->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04001980 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Al Virof6948692010-01-30 13:51:04 -05001981 path_get(path);
1982 calldata->path = *path;
Trond Myklebust95121352005-10-18 14:20:12 -07001983
Trond Myklebust5138fde2007-07-14 15:40:01 -04001984 msg.rpc_argp = &calldata->arg,
1985 msg.rpc_resp = &calldata->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001986 task_setup_data.callback_data = calldata;
1987 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001988 if (IS_ERR(task))
1989 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001990 status = 0;
1991 if (wait)
1992 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001993 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001994 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07001995out_free_calldata:
1996 kfree(calldata);
1997out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04001998 nfs4_put_open_state(state);
1999 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002000 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001}
2002
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002003static 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 -07002004{
2005 struct file *filp;
Trond Myklebust1b45c462007-07-03 13:04:56 -04002006 int ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002007
Trond Myklebust1b45c462007-07-03 13:04:56 -04002008 /* If the open_intent is for execute, we have an extra check to make */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002009 if (fmode & FMODE_EXEC) {
Trond Myklebustaf22f942007-08-10 17:45:10 -04002010 ret = nfs_may_open(state->inode,
Trond Myklebust1b45c462007-07-03 13:04:56 -04002011 state->owner->so_cred,
2012 nd->intent.open.flags);
2013 if (ret < 0)
2014 goto out_close;
2015 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002016 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002017 if (!IS_ERR(filp)) {
2018 struct nfs_open_context *ctx;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04002019 ctx = nfs_file_open_context(filp);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002020 ctx->state = state;
Trond Myklebust95cf9592006-04-18 13:14:06 -04002021 return 0;
2022 }
Trond Myklebust1b45c462007-07-03 13:04:56 -04002023 ret = PTR_ERR(filp);
2024out_close:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002025 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
Trond Myklebust1b45c462007-07-03 13:04:56 -04002026 return ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002027}
2028
2029struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
2031{
Trond Myklebustad389da2007-06-05 12:30:00 -04002032 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002033 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002034 .dentry = dentry,
2035 };
Jan Blunck4ac91372008-02-14 19:34:32 -08002036 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 struct iattr attr;
2038 struct rpc_cred *cred;
2039 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002040 struct dentry *res;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002041 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
2043 if (nd->flags & LOOKUP_CREATE) {
2044 attr.ia_mode = nd->intent.open.create_mode;
2045 attr.ia_valid = ATTR_MODE;
2046 if (!IS_POSIXACL(dir))
Al Viroce3b0f82009-03-29 19:08:22 -04002047 attr.ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 } else {
2049 attr.ia_valid = 0;
2050 BUG_ON(nd->intent.open.flags & O_CREAT);
2051 }
2052
Trond Myklebust98a8e322008-03-12 12:25:28 -04002053 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07002055 return (struct dentry *)cred;
Trond Myklebust565277f2007-10-15 18:17:53 -04002056 parent = dentry->d_parent;
2057 /* Protect against concurrent sillydeletes */
2058 nfs_block_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002059 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002061 if (IS_ERR(state)) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04002062 if (PTR_ERR(state) == -ENOENT) {
Trond Myklebust02a913a2005-10-18 14:20:17 -07002063 d_add(dentry, NULL);
Trond Myklebustd75340c2007-10-01 21:42:01 -04002064 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
2065 }
Trond Myklebust565277f2007-10-15 18:17:53 -04002066 nfs_unblock_sillyrename(parent);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002067 return (struct dentry *)state;
2068 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002069 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07002070 if (res != NULL)
Trond Myklebustdeee9362007-08-27 11:33:00 -04002071 path.dentry = res;
Trond Myklebustd75340c2007-10-01 21:42:01 -04002072 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
Trond Myklebust565277f2007-10-15 18:17:53 -04002073 nfs_unblock_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002074 nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002075 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076}
2077
2078int
Trond Myklebust02a913a2005-10-18 14:20:17 -07002079nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080{
Trond Myklebustad389da2007-06-05 12:30:00 -04002081 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002082 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002083 .dentry = dentry,
2084 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 struct rpc_cred *cred;
2086 struct nfs4_state *state;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002087 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
Trond Myklebust98a8e322008-03-12 12:25:28 -04002089 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 if (IS_ERR(cred))
2091 return PTR_ERR(cred);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002092 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002094 if (IS_ERR(state)) {
2095 switch (PTR_ERR(state)) {
2096 case -EPERM:
2097 case -EACCES:
2098 case -EDQUOT:
2099 case -ENOSPC:
2100 case -EROFS:
Al Viro04287f92010-04-08 00:06:07 +01002101 return PTR_ERR(state);
Chuck Leverb87c0ad2006-10-19 23:28:42 -07002102 default:
2103 goto out_drop;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002104 }
Trond Myklebust02a913a2005-10-18 14:20:17 -07002105 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002106 if (state->inode == dentry->d_inode) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04002107 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002108 nfs4_intent_set_file(nd, &path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 return 1;
2110 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002111 nfs4_close_sync(&path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002112out_drop:
2113 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 return 0;
2115}
2116
Trond Myklebust1185a552009-12-03 15:54:02 -05002117static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002118{
2119 if (ctx->state == NULL)
2120 return;
2121 if (is_sync)
2122 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2123 else
2124 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2125}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
2127static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2128{
Benny Halevy43652ad2009-04-01 09:21:54 -04002129 struct nfs4_server_caps_arg args = {
2130 .fhandle = fhandle,
2131 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 struct nfs4_server_caps_res res = {};
2133 struct rpc_message msg = {
2134 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002135 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 .rpc_resp = &res,
2137 };
2138 int status;
2139
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002140 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 if (status == 0) {
2142 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002143 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2144 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2145 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2146 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2147 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2149 server->caps |= NFS_CAP_ACLS;
2150 if (res.has_links != 0)
2151 server->caps |= NFS_CAP_HARDLINKS;
2152 if (res.has_symlinks != 0)
2153 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002154 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2155 server->caps |= NFS_CAP_FILEID;
2156 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2157 server->caps |= NFS_CAP_MODE;
2158 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2159 server->caps |= NFS_CAP_NLINK;
2160 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2161 server->caps |= NFS_CAP_OWNER;
2162 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2163 server->caps |= NFS_CAP_OWNER_GROUP;
2164 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2165 server->caps |= NFS_CAP_ATIME;
2166 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2167 server->caps |= NFS_CAP_CTIME;
2168 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2169 server->caps |= NFS_CAP_MTIME;
2170
Trond Myklebusta65318b2009-03-11 14:10:28 -04002171 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2172 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2173 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 server->acl_bitmask = res.acl_bitmask;
2175 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002176
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 return status;
2178}
2179
Trond Myklebust55a97592006-06-09 09:34:19 -04002180int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181{
2182 struct nfs4_exception exception = { };
2183 int err;
2184 do {
2185 err = nfs4_handle_exception(server,
2186 _nfs4_server_capabilities(server, fhandle),
2187 &exception);
2188 } while (exception.retry);
2189 return err;
2190}
2191
2192static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2193 struct nfs_fsinfo *info)
2194{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 struct nfs4_lookup_root_arg args = {
2196 .bitmask = nfs4_fattr_bitmap,
2197 };
2198 struct nfs4_lookup_res res = {
2199 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002200 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 .fh = fhandle,
2202 };
2203 struct rpc_message msg = {
2204 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2205 .rpc_argp = &args,
2206 .rpc_resp = &res,
2207 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002208
Trond Myklebust0e574af2005-10-27 22:12:38 -04002209 nfs_fattr_init(info->fattr);
Trond Myklebustfccba802009-07-21 16:48:07 -04002210 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211}
2212
2213static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2214 struct nfs_fsinfo *info)
2215{
2216 struct nfs4_exception exception = { };
2217 int err;
2218 do {
2219 err = nfs4_handle_exception(server,
2220 _nfs4_lookup_root(server, fhandle, info),
2221 &exception);
2222 } while (exception.retry);
2223 return err;
2224}
2225
David Howells54ceac42006-08-22 20:06:13 -04002226/*
2227 * get the file handle for the "/" directory on the server
2228 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002230 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 int status;
2233
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 if (status == 0)
2236 status = nfs4_server_capabilities(server, fhandle);
2237 if (status == 0)
2238 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002239 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240}
2241
Manoj Naik6b97fd32006-06-09 09:34:29 -04002242/*
2243 * Get locations and (maybe) other attributes of a referral.
2244 * Note that we'll actually follow the referral later when
2245 * we detect fsid mismatch in inode revalidation
2246 */
Trond Myklebust56659e92007-07-17 21:52:39 -04002247static 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 -04002248{
2249 int status = -ENOMEM;
2250 struct page *page = NULL;
2251 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002252
2253 page = alloc_page(GFP_KERNEL);
2254 if (page == NULL)
2255 goto out;
2256 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2257 if (locations == NULL)
2258 goto out;
2259
Trond Myklebustc228fd32007-01-13 02:28:11 -05002260 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002261 if (status != 0)
2262 goto out;
2263 /* Make sure server returned a different fsid for the referral */
2264 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07002265 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 -04002266 status = -EIO;
2267 goto out;
2268 }
2269
2270 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2271 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2272 if (!fattr->mode)
2273 fattr->mode = S_IFDIR;
2274 memset(fhandle, 0, sizeof(struct nfs_fh));
2275out:
2276 if (page)
2277 __free_page(page);
2278 if (locations)
2279 kfree(locations);
2280 return status;
2281}
2282
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2284{
2285 struct nfs4_getattr_arg args = {
2286 .fh = fhandle,
2287 .bitmask = server->attr_bitmask,
2288 };
2289 struct nfs4_getattr_res res = {
2290 .fattr = fattr,
2291 .server = server,
2292 };
2293 struct rpc_message msg = {
2294 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2295 .rpc_argp = &args,
2296 .rpc_resp = &res,
2297 };
2298
Trond Myklebust0e574af2005-10-27 22:12:38 -04002299 nfs_fattr_init(fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002300 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301}
2302
2303static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2304{
2305 struct nfs4_exception exception = { };
2306 int err;
2307 do {
2308 err = nfs4_handle_exception(server,
2309 _nfs4_proc_getattr(server, fhandle, fattr),
2310 &exception);
2311 } while (exception.retry);
2312 return err;
2313}
2314
2315/*
2316 * The file is not closed if it is opened due to the a request to change
2317 * the size of the file. The open call will not be needed once the
2318 * VFS layer lookup-intents are implemented.
2319 *
2320 * Close is called when the inode is destroyed.
2321 * If we haven't opened the file for O_WRONLY, we
2322 * need to in the size_change case to obtain a stateid.
2323 *
2324 * Got race?
2325 * Because OPEN is always done by name in nfsv4, it is
2326 * possible that we opened a different file by the same
2327 * name. We can recognize this race condition, but we
2328 * can't do anything about it besides returning an error.
2329 *
2330 * This will be fixed with VFS changes (lookup-intent).
2331 */
2332static int
2333nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2334 struct iattr *sattr)
2335{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002336 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002337 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002338 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 int status;
2340
Trond Myklebust0e574af2005-10-27 22:12:38 -04002341 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
Trond Myklebustd5308382005-11-04 15:33:38 -05002343 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002344 if (sattr->ia_valid & ATTR_FILE) {
2345 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002346
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002347 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002348 if (ctx) {
2349 cred = ctx->cred;
2350 state = ctx->state;
2351 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002352 }
2353
2354 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002355 if (status == 0)
2356 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 return status;
2358}
2359
Trond Myklebust56659e92007-07-17 21:52:39 -04002360static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2361 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04002362 struct nfs_fattr *fattr)
2363{
2364 int status;
2365 struct nfs4_lookup_arg args = {
2366 .bitmask = server->attr_bitmask,
2367 .dir_fh = dirfh,
2368 .name = name,
2369 };
2370 struct nfs4_lookup_res res = {
2371 .server = server,
2372 .fattr = fattr,
2373 .fh = fhandle,
2374 };
2375 struct rpc_message msg = {
2376 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2377 .rpc_argp = &args,
2378 .rpc_resp = &res,
2379 };
2380
2381 nfs_fattr_init(fattr);
2382
2383 dprintk("NFS call lookupfh %s\n", name->name);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002384 status = nfs4_call_sync(server, &msg, &args, &res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002385 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002386 return status;
2387}
2388
2389static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2390 struct qstr *name, struct nfs_fh *fhandle,
2391 struct nfs_fattr *fattr)
2392{
2393 struct nfs4_exception exception = { };
2394 int err;
2395 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04002396 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2397 /* FIXME: !!!! */
2398 if (err == -NFS4ERR_MOVED) {
2399 err = -EREMOTE;
2400 break;
2401 }
2402 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002403 } while (exception.retry);
2404 return err;
2405}
2406
Trond Myklebust56659e92007-07-17 21:52:39 -04002407static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2409{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002410 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411
2412 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002413 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002414 if (status == -NFS4ERR_MOVED)
2415 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 dprintk("NFS reply lookup: %d\n", status);
2417 return status;
2418}
2419
2420static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2421{
2422 struct nfs4_exception exception = { };
2423 int err;
2424 do {
2425 err = nfs4_handle_exception(NFS_SERVER(dir),
2426 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2427 &exception);
2428 } while (exception.retry);
2429 return err;
2430}
2431
2432static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2433{
Trond Myklebust76b32992007-08-10 17:45:11 -04002434 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 struct nfs4_accessargs args = {
2436 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002437 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002439 struct nfs4_accessres res = {
2440 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002441 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 struct rpc_message msg = {
2443 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2444 .rpc_argp = &args,
2445 .rpc_resp = &res,
2446 .rpc_cred = entry->cred,
2447 };
2448 int mode = entry->mask;
2449 int status;
2450
2451 /*
2452 * Determine which access bits we want to ask for...
2453 */
2454 if (mode & MAY_READ)
2455 args.access |= NFS4_ACCESS_READ;
2456 if (S_ISDIR(inode->i_mode)) {
2457 if (mode & MAY_WRITE)
2458 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2459 if (mode & MAY_EXEC)
2460 args.access |= NFS4_ACCESS_LOOKUP;
2461 } else {
2462 if (mode & MAY_WRITE)
2463 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2464 if (mode & MAY_EXEC)
2465 args.access |= NFS4_ACCESS_EXECUTE;
2466 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002467
2468 res.fattr = nfs_alloc_fattr();
2469 if (res.fattr == NULL)
2470 return -ENOMEM;
2471
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002472 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 if (!status) {
2474 entry->mask = 0;
2475 if (res.access & NFS4_ACCESS_READ)
2476 entry->mask |= MAY_READ;
2477 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2478 entry->mask |= MAY_WRITE;
2479 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2480 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002481 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002483 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 return status;
2485}
2486
2487static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2488{
2489 struct nfs4_exception exception = { };
2490 int err;
2491 do {
2492 err = nfs4_handle_exception(NFS_SERVER(inode),
2493 _nfs4_proc_access(inode, entry),
2494 &exception);
2495 } while (exception.retry);
2496 return err;
2497}
2498
2499/*
2500 * TODO: For the time being, we don't try to get any attributes
2501 * along with any of the zero-copy operations READ, READDIR,
2502 * READLINK, WRITE.
2503 *
2504 * In the case of the first three, we want to put the GETATTR
2505 * after the read-type operation -- this is because it is hard
2506 * to predict the length of a GETATTR response in v4, and thus
2507 * align the READ data correctly. This means that the GETATTR
2508 * may end up partially falling into the page cache, and we should
2509 * shift it into the 'tail' of the xdr_buf before processing.
2510 * To do this efficiently, we need to know the total length
2511 * of data received, which doesn't seem to be available outside
2512 * of the RPC layer.
2513 *
2514 * In the case of WRITE, we also want to put the GETATTR after
2515 * the operation -- in this case because we want to make sure
2516 * we get the post-operation mtime and size. This means that
2517 * we can't use xdr_encode_pages() as written: we need a variant
2518 * of it which would leave room in the 'tail' iovec.
2519 *
2520 * Both of these changes to the XDR layer would in fact be quite
2521 * minor, but I decided to leave them for a subsequent patch.
2522 */
2523static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2524 unsigned int pgbase, unsigned int pglen)
2525{
2526 struct nfs4_readlink args = {
2527 .fh = NFS_FH(inode),
2528 .pgbase = pgbase,
2529 .pglen = pglen,
2530 .pages = &page,
2531 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002532 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 struct rpc_message msg = {
2534 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2535 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002536 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 };
2538
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002539 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540}
2541
2542static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2543 unsigned int pgbase, unsigned int pglen)
2544{
2545 struct nfs4_exception exception = { };
2546 int err;
2547 do {
2548 err = nfs4_handle_exception(NFS_SERVER(inode),
2549 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2550 &exception);
2551 } while (exception.retry);
2552 return err;
2553}
2554
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555/*
2556 * Got race?
2557 * We will need to arrange for the VFS layer to provide an atomic open.
2558 * Until then, this create/open method is prone to inefficiency and race
2559 * conditions due to the lookup, create, and open VFS calls from sys_open()
2560 * placed on the wire.
2561 *
2562 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2563 * The file will be opened again in the subsequent VFS open call
2564 * (nfs4_proc_file_open).
2565 *
2566 * The open for read will just hang around to be used by any process that
2567 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2568 */
2569
2570static int
2571nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07002572 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573{
Trond Myklebustad389da2007-06-05 12:30:00 -04002574 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002575 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002576 .dentry = dentry,
2577 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 struct nfs4_state *state;
2579 struct rpc_cred *cred;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002580 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 int status = 0;
2582
Trond Myklebust98a8e322008-03-12 12:25:28 -04002583 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 if (IS_ERR(cred)) {
2585 status = PTR_ERR(cred);
2586 goto out;
2587 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002588 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002589 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 if (IS_ERR(state)) {
2591 status = PTR_ERR(state);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002592 goto out_putcred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002594 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002595 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustad389da2007-06-05 12:30:00 -04002596 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002597 status = nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002598 else
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002599 nfs4_close_sync(&path, state, fmode);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002600out_putcred:
2601 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602out:
2603 return status;
2604}
2605
2606static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2607{
Trond Myklebust16e42952005-10-27 22:12:44 -04002608 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002609 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002611 .name.len = name->len,
2612 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002613 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002615 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002616 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002617 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002619 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2620 .rpc_argp = &args,
2621 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002623 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624
Trond Myklebustd3468902010-04-16 16:22:50 -04002625 res.dir_attr = nfs_alloc_fattr();
2626 if (res.dir_attr == NULL)
2627 goto out;
2628
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002629 status = nfs4_call_sync(server, &msg, &args, &res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002630 if (status == 0) {
2631 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002632 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002633 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002634 nfs_free_fattr(res.dir_attr);
2635out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 return status;
2637}
2638
2639static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2640{
2641 struct nfs4_exception exception = { };
2642 int err;
2643 do {
2644 err = nfs4_handle_exception(NFS_SERVER(dir),
2645 _nfs4_proc_remove(dir, name),
2646 &exception);
2647 } while (exception.retry);
2648 return err;
2649}
2650
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002651static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002653 struct nfs_server *server = NFS_SERVER(dir);
2654 struct nfs_removeargs *args = msg->rpc_argp;
2655 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
Trond Myklebusta65318b2009-03-11 14:10:28 -04002657 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002658 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660}
2661
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002662static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002664 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2665
Trond Myklebust14516c32010-07-31 14:29:06 -04002666 if (!nfs4_sequence_done(task, &res->seq_res))
2667 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002668 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002669 return 0;
2670 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002671 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002672 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673}
2674
2675static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2676 struct inode *new_dir, struct qstr *new_name)
2677{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002678 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 struct nfs4_rename_arg arg = {
2680 .old_dir = NFS_FH(old_dir),
2681 .new_dir = NFS_FH(new_dir),
2682 .old_name = old_name,
2683 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002684 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002686 struct nfs4_rename_res res = {
2687 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002688 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 struct rpc_message msg = {
2690 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2691 .rpc_argp = &arg,
2692 .rpc_resp = &res,
2693 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002694 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695
Trond Myklebust011fff72010-04-16 16:22:49 -04002696 res.old_fattr = nfs_alloc_fattr();
2697 res.new_fattr = nfs_alloc_fattr();
2698 if (res.old_fattr == NULL || res.new_fattr == NULL)
2699 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700
Trond Myklebust011fff72010-04-16 16:22:49 -04002701 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 if (!status) {
2703 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002704 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002706 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002708out:
2709 nfs_free_fattr(res.new_fattr);
2710 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 return status;
2712}
2713
2714static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2715 struct inode *new_dir, struct qstr *new_name)
2716{
2717 struct nfs4_exception exception = { };
2718 int err;
2719 do {
2720 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2721 _nfs4_proc_rename(old_dir, old_name,
2722 new_dir, new_name),
2723 &exception);
2724 } while (exception.retry);
2725 return err;
2726}
2727
2728static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2729{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002730 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 struct nfs4_link_arg arg = {
2732 .fh = NFS_FH(inode),
2733 .dir_fh = NFS_FH(dir),
2734 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002735 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002737 struct nfs4_link_res res = {
2738 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002739 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 struct rpc_message msg = {
2741 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2742 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002743 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002745 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746
Trond Myklebust136f2622010-04-16 16:22:49 -04002747 res.fattr = nfs_alloc_fattr();
2748 res.dir_attr = nfs_alloc_fattr();
2749 if (res.fattr == NULL || res.dir_attr == NULL)
2750 goto out;
2751
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002752 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002753 if (!status) {
2754 update_changeattr(dir, &res.cinfo);
2755 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002756 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002757 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002758out:
2759 nfs_free_fattr(res.dir_attr);
2760 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 return status;
2762}
2763
2764static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2765{
2766 struct nfs4_exception exception = { };
2767 int err;
2768 do {
2769 err = nfs4_handle_exception(NFS_SERVER(inode),
2770 _nfs4_proc_link(inode, dir, name),
2771 &exception);
2772 } while (exception.retry);
2773 return err;
2774}
2775
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002776struct nfs4_createdata {
2777 struct rpc_message msg;
2778 struct nfs4_create_arg arg;
2779 struct nfs4_create_res res;
2780 struct nfs_fh fh;
2781 struct nfs_fattr fattr;
2782 struct nfs_fattr dir_fattr;
2783};
2784
2785static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2786 struct qstr *name, struct iattr *sattr, u32 ftype)
2787{
2788 struct nfs4_createdata *data;
2789
2790 data = kzalloc(sizeof(*data), GFP_KERNEL);
2791 if (data != NULL) {
2792 struct nfs_server *server = NFS_SERVER(dir);
2793
2794 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2795 data->msg.rpc_argp = &data->arg;
2796 data->msg.rpc_resp = &data->res;
2797 data->arg.dir_fh = NFS_FH(dir);
2798 data->arg.server = server;
2799 data->arg.name = name;
2800 data->arg.attrs = sattr;
2801 data->arg.ftype = ftype;
2802 data->arg.bitmask = server->attr_bitmask;
2803 data->res.server = server;
2804 data->res.fh = &data->fh;
2805 data->res.fattr = &data->fattr;
2806 data->res.dir_fattr = &data->dir_fattr;
2807 nfs_fattr_init(data->res.fattr);
2808 nfs_fattr_init(data->res.dir_fattr);
2809 }
2810 return data;
2811}
2812
2813static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2814{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002815 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2816 &data->arg, &data->res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002817 if (status == 0) {
2818 update_changeattr(dir, &data->res.dir_cinfo);
2819 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2820 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2821 }
2822 return status;
2823}
2824
2825static void nfs4_free_createdata(struct nfs4_createdata *data)
2826{
2827 kfree(data);
2828}
2829
Chuck Lever4f390c12006-08-22 20:06:22 -04002830static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002831 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002833 struct nfs4_createdata *data;
2834 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
Chuck Lever94a6d752006-08-22 20:06:23 -04002836 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002837 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002838
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002839 status = -ENOMEM;
2840 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2841 if (data == NULL)
2842 goto out;
2843
2844 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2845 data->arg.u.symlink.pages = &page;
2846 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002848 status = nfs4_do_create(dir, dentry, data);
2849
2850 nfs4_free_createdata(data);
2851out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 return status;
2853}
2854
Chuck Lever4f390c12006-08-22 20:06:22 -04002855static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002856 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857{
2858 struct nfs4_exception exception = { };
2859 int err;
2860 do {
2861 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002862 _nfs4_proc_symlink(dir, dentry, page,
2863 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 &exception);
2865 } while (exception.retry);
2866 return err;
2867}
2868
2869static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2870 struct iattr *sattr)
2871{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002872 struct nfs4_createdata *data;
2873 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002875 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2876 if (data == NULL)
2877 goto out;
2878
2879 status = nfs4_do_create(dir, dentry, data);
2880
2881 nfs4_free_createdata(data);
2882out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 return status;
2884}
2885
2886static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2887 struct iattr *sattr)
2888{
2889 struct nfs4_exception exception = { };
2890 int err;
2891 do {
2892 err = nfs4_handle_exception(NFS_SERVER(dir),
2893 _nfs4_proc_mkdir(dir, dentry, sattr),
2894 &exception);
2895 } while (exception.retry);
2896 return err;
2897}
2898
2899static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2900 u64 cookie, struct page *page, unsigned int count, int plus)
2901{
2902 struct inode *dir = dentry->d_inode;
2903 struct nfs4_readdir_arg args = {
2904 .fh = NFS_FH(dir),
2905 .pages = &page,
2906 .pgbase = 0,
2907 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002908 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 };
2910 struct nfs4_readdir_res res;
2911 struct rpc_message msg = {
2912 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2913 .rpc_argp = &args,
2914 .rpc_resp = &res,
2915 .rpc_cred = cred,
2916 };
2917 int status;
2918
Harvey Harrison3110ff82008-05-02 13:42:44 -07002919 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002920 dentry->d_parent->d_name.name,
2921 dentry->d_name.name,
2922 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2924 res.pgbase = args.pgbase;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002925 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 if (status == 0)
2927 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustc4812992007-09-28 17:11:45 -04002928
2929 nfs_invalidate_atime(dir);
2930
Harvey Harrison3110ff82008-05-02 13:42:44 -07002931 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 return status;
2933}
2934
2935static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2936 u64 cookie, struct page *page, unsigned int count, int plus)
2937{
2938 struct nfs4_exception exception = { };
2939 int err;
2940 do {
2941 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2942 _nfs4_proc_readdir(dentry, cred, cookie,
2943 page, count, plus),
2944 &exception);
2945 } while (exception.retry);
2946 return err;
2947}
2948
2949static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2950 struct iattr *sattr, dev_t rdev)
2951{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002952 struct nfs4_createdata *data;
2953 int mode = sattr->ia_mode;
2954 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955
2956 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2957 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002958
2959 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2960 if (data == NULL)
2961 goto out;
2962
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002964 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002966 data->arg.ftype = NF4BLK;
2967 data->arg.u.device.specdata1 = MAJOR(rdev);
2968 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 }
2970 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002971 data->arg.ftype = NF4CHR;
2972 data->arg.u.device.specdata1 = MAJOR(rdev);
2973 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002976 status = nfs4_do_create(dir, dentry, data);
2977
2978 nfs4_free_createdata(data);
2979out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 return status;
2981}
2982
2983static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2984 struct iattr *sattr, dev_t rdev)
2985{
2986 struct nfs4_exception exception = { };
2987 int err;
2988 do {
2989 err = nfs4_handle_exception(NFS_SERVER(dir),
2990 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2991 &exception);
2992 } while (exception.retry);
2993 return err;
2994}
2995
2996static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2997 struct nfs_fsstat *fsstat)
2998{
2999 struct nfs4_statfs_arg args = {
3000 .fh = fhandle,
3001 .bitmask = server->attr_bitmask,
3002 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003003 struct nfs4_statfs_res res = {
3004 .fsstat = fsstat,
3005 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 struct rpc_message msg = {
3007 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3008 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003009 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 };
3011
Trond Myklebust0e574af2005-10-27 22:12:38 -04003012 nfs_fattr_init(fsstat->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003013 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014}
3015
3016static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3017{
3018 struct nfs4_exception exception = { };
3019 int err;
3020 do {
3021 err = nfs4_handle_exception(server,
3022 _nfs4_proc_statfs(server, fhandle, fsstat),
3023 &exception);
3024 } while (exception.retry);
3025 return err;
3026}
3027
3028static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3029 struct nfs_fsinfo *fsinfo)
3030{
3031 struct nfs4_fsinfo_arg args = {
3032 .fh = fhandle,
3033 .bitmask = server->attr_bitmask,
3034 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003035 struct nfs4_fsinfo_res res = {
3036 .fsinfo = fsinfo,
3037 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 struct rpc_message msg = {
3039 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3040 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003041 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 };
3043
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003044 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045}
3046
3047static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3048{
3049 struct nfs4_exception exception = { };
3050 int err;
3051
3052 do {
3053 err = nfs4_handle_exception(server,
3054 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3055 &exception);
3056 } while (exception.retry);
3057 return err;
3058}
3059
3060static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3061{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003062 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3064}
3065
3066static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3067 struct nfs_pathconf *pathconf)
3068{
3069 struct nfs4_pathconf_arg args = {
3070 .fh = fhandle,
3071 .bitmask = server->attr_bitmask,
3072 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003073 struct nfs4_pathconf_res res = {
3074 .pathconf = pathconf,
3075 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 struct rpc_message msg = {
3077 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3078 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003079 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 };
3081
3082 /* None of the pathconf attributes are mandatory to implement */
3083 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3084 memset(pathconf, 0, sizeof(*pathconf));
3085 return 0;
3086 }
3087
Trond Myklebust0e574af2005-10-27 22:12:38 -04003088 nfs_fattr_init(pathconf->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003089 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090}
3091
3092static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3093 struct nfs_pathconf *pathconf)
3094{
3095 struct nfs4_exception exception = { };
3096 int err;
3097
3098 do {
3099 err = nfs4_handle_exception(server,
3100 _nfs4_proc_pathconf(server, fhandle, pathconf),
3101 &exception);
3102 } while (exception.retry);
3103 return err;
3104}
3105
Trond Myklebustec06c092006-03-20 13:44:27 -05003106static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107{
Trond Myklebustec06c092006-03-20 13:44:27 -05003108 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003110 dprintk("--> %s\n", __func__);
3111
Trond Myklebust14516c32010-07-31 14:29:06 -04003112 if (!nfs4_sequence_done(task, &data->res.seq_res))
3113 return -EAGAIN;
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003114
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003115 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003116 nfs_restart_rpc(task, server->nfs_client);
Trond Myklebustec06c092006-03-20 13:44:27 -05003117 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003119
3120 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003122 renew_lease(server, data->timestamp);
3123 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124}
3125
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003126static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 data->timestamp = jiffies;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003129 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130}
3131
Trond Myklebust788e7a82006-03-20 13:44:27 -05003132static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 struct inode *inode = data->inode;
3135
Trond Myklebust14516c32010-07-31 14:29:06 -04003136 if (!nfs4_sequence_done(task, &data->res.seq_res))
3137 return -EAGAIN;
Andy Adamsondef6ed72009-04-01 09:22:26 -04003138
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003139 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003140 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003141 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003143 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003145 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003146 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003147 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148}
3149
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003150static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003152 struct nfs_server *server = NFS_SERVER(data->inode);
3153
Trond Myklebusta65318b2009-03-11 14:10:28 -04003154 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003155 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 data->timestamp = jiffies;
3157
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003158 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159}
3160
Trond Myklebust788e7a82006-03-20 13:44:27 -05003161static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 struct inode *inode = data->inode;
3164
Trond Myklebust14516c32010-07-31 14:29:06 -04003165 if (!nfs4_sequence_done(task, &data->res.seq_res))
3166 return -EAGAIN;
3167
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003168 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003169 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003170 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003172 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003173 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174}
3175
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003176static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003178 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
Trond Myklebusta65318b2009-03-11 14:10:28 -04003180 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003181 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003182 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183}
3184
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003185struct nfs4_renewdata {
3186 struct nfs_client *client;
3187 unsigned long timestamp;
3188};
3189
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190/*
3191 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3192 * standalone procedure for queueing an asynchronous RENEW.
3193 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003194static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003195{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003196 struct nfs4_renewdata *data = calldata;
3197 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003198
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003199 if (atomic_read(&clp->cl_count) > 1)
3200 nfs4_schedule_state_renewal(clp);
3201 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003202 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003203}
3204
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003205static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003207 struct nfs4_renewdata *data = calldata;
3208 struct nfs_client *clp = data->client;
3209 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210
3211 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003212 /* Unless we're shutting down, schedule state recovery! */
3213 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3214 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 return;
3216 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003217 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218}
3219
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003220static const struct rpc_call_ops nfs4_renew_ops = {
3221 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003222 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003223};
3224
David Howellsadfa6f92006-08-22 20:06:08 -04003225int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226{
3227 struct rpc_message msg = {
3228 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3229 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003230 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003232 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003234 if (!atomic_inc_not_zero(&clp->cl_count))
3235 return -EIO;
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003236 data = kmalloc(sizeof(*data), GFP_KERNEL);
3237 if (data == NULL)
3238 return -ENOMEM;
3239 data->client = clp;
3240 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003242 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243}
3244
David Howellsadfa6f92006-08-22 20:06:08 -04003245int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246{
3247 struct rpc_message msg = {
3248 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3249 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003250 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 };
3252 unsigned long now = jiffies;
3253 int status;
3254
3255 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3256 if (status < 0)
3257 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003258 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 return 0;
3260}
3261
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003262static inline int nfs4_server_supports_acls(struct nfs_server *server)
3263{
3264 return (server->caps & NFS_CAP_ACLS)
3265 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3266 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3267}
3268
3269/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3270 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3271 * the stack.
3272 */
3273#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3274
3275static void buf_to_pages(const void *buf, size_t buflen,
3276 struct page **pages, unsigned int *pgbase)
3277{
3278 const void *p = buf;
3279
3280 *pgbase = offset_in_page(buf);
3281 p -= *pgbase;
3282 while (p < buf + buflen) {
3283 *(pages++) = virt_to_page(p);
3284 p += PAGE_CACHE_SIZE;
3285 }
3286}
3287
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003288struct nfs4_cached_acl {
3289 int cached;
3290 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003291 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003292};
3293
3294static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003295{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003296 struct nfs_inode *nfsi = NFS_I(inode);
3297
3298 spin_lock(&inode->i_lock);
3299 kfree(nfsi->nfs4_acl);
3300 nfsi->nfs4_acl = acl;
3301 spin_unlock(&inode->i_lock);
3302}
3303
3304static void nfs4_zap_acl_attr(struct inode *inode)
3305{
3306 nfs4_set_cached_acl(inode, NULL);
3307}
3308
3309static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3310{
3311 struct nfs_inode *nfsi = NFS_I(inode);
3312 struct nfs4_cached_acl *acl;
3313 int ret = -ENOENT;
3314
3315 spin_lock(&inode->i_lock);
3316 acl = nfsi->nfs4_acl;
3317 if (acl == NULL)
3318 goto out;
3319 if (buf == NULL) /* user is just asking for length */
3320 goto out_len;
3321 if (acl->cached == 0)
3322 goto out;
3323 ret = -ERANGE; /* see getxattr(2) man page */
3324 if (acl->len > buflen)
3325 goto out;
3326 memcpy(buf, acl->data, acl->len);
3327out_len:
3328 ret = acl->len;
3329out:
3330 spin_unlock(&inode->i_lock);
3331 return ret;
3332}
3333
3334static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3335{
3336 struct nfs4_cached_acl *acl;
3337
3338 if (buf && acl_len <= PAGE_SIZE) {
3339 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3340 if (acl == NULL)
3341 goto out;
3342 acl->cached = 1;
3343 memcpy(acl->data, buf, acl_len);
3344 } else {
3345 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3346 if (acl == NULL)
3347 goto out;
3348 acl->cached = 0;
3349 }
3350 acl->len = acl_len;
3351out:
3352 nfs4_set_cached_acl(inode, acl);
3353}
3354
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003355static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003356{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003357 struct page *pages[NFS4ACL_MAXPAGES];
3358 struct nfs_getaclargs args = {
3359 .fh = NFS_FH(inode),
3360 .acl_pages = pages,
3361 .acl_len = buflen,
3362 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003363 struct nfs_getaclres res = {
3364 .acl_len = buflen,
3365 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003366 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003367 struct rpc_message msg = {
3368 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3369 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003370 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003371 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003372 struct page *localpage = NULL;
3373 int ret;
3374
3375 if (buflen < PAGE_SIZE) {
3376 /* As long as we're doing a round trip to the server anyway,
3377 * let's be prepared for a page of acl data. */
3378 localpage = alloc_page(GFP_KERNEL);
3379 resp_buf = page_address(localpage);
3380 if (localpage == NULL)
3381 return -ENOMEM;
3382 args.acl_pages[0] = localpage;
3383 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003384 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003385 } else {
3386 resp_buf = buf;
3387 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3388 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003389 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003390 if (ret)
3391 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003392 if (res.acl_len > args.acl_len)
3393 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003394 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003395 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003396 if (buf) {
3397 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003398 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003399 goto out_free;
3400 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003401 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003402 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003403 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003404out_free:
3405 if (localpage)
3406 __free_page(localpage);
3407 return ret;
3408}
3409
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003410static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3411{
3412 struct nfs4_exception exception = { };
3413 ssize_t ret;
3414 do {
3415 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3416 if (ret >= 0)
3417 break;
3418 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3419 } while (exception.retry);
3420 return ret;
3421}
3422
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003423static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3424{
3425 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003426 int ret;
3427
3428 if (!nfs4_server_supports_acls(server))
3429 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003430 ret = nfs_revalidate_inode(server, inode);
3431 if (ret < 0)
3432 return ret;
3433 ret = nfs4_read_cached_acl(inode, buf, buflen);
3434 if (ret != -ENOENT)
3435 return ret;
3436 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003437}
3438
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003439static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003440{
3441 struct nfs_server *server = NFS_SERVER(inode);
3442 struct page *pages[NFS4ACL_MAXPAGES];
3443 struct nfs_setaclargs arg = {
3444 .fh = NFS_FH(inode),
3445 .acl_pages = pages,
3446 .acl_len = buflen,
3447 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003448 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003449 struct rpc_message msg = {
3450 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3451 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003452 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003453 };
3454 int ret;
3455
3456 if (!nfs4_server_supports_acls(server))
3457 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07003458 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003459 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003460 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003461 nfs_access_zap_cache(inode);
3462 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003463 return ret;
3464}
3465
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003466static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3467{
3468 struct nfs4_exception exception = { };
3469 int err;
3470 do {
3471 err = nfs4_handle_exception(NFS_SERVER(inode),
3472 __nfs4_proc_set_acl(inode, buf, buflen),
3473 &exception);
3474 } while (exception.retry);
3475 return err;
3476}
3477
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003479nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003481 struct nfs_client *clp = server->nfs_client;
3482
3483 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 return 0;
3485 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003486 case -NFS4ERR_ADMIN_REVOKED:
3487 case -NFS4ERR_BAD_STATEID:
3488 case -NFS4ERR_OPENMODE:
3489 if (state == NULL)
3490 break;
3491 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003492 goto do_state_recovery;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003494 if (state == NULL)
3495 break;
3496 nfs4_state_mark_reclaim_reboot(clp, state);
3497 case -NFS4ERR_STALE_CLIENTID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 case -NFS4ERR_EXPIRED:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003499 goto do_state_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003500#if defined(CONFIG_NFS_V4_1)
3501 case -NFS4ERR_BADSESSION:
3502 case -NFS4ERR_BADSLOT:
3503 case -NFS4ERR_BAD_HIGH_SLOT:
3504 case -NFS4ERR_DEADSESSION:
3505 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3506 case -NFS4ERR_SEQ_FALSE_RETRY:
3507 case -NFS4ERR_SEQ_MISORDERED:
3508 dprintk("%s ERROR %d, Reset session\n", __func__,
3509 task->tk_status);
Andy Adamson0b9e2d42009-12-04 16:02:14 -05003510 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -04003511 task->tk_status = 0;
3512 return -EAGAIN;
3513#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003515 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003516 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003517 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3519 task->tk_status = 0;
3520 return -EAGAIN;
3521 case -NFS4ERR_OLD_STATEID:
3522 task->tk_status = 0;
3523 return -EAGAIN;
3524 }
3525 task->tk_status = nfs4_map_errors(task->tk_status);
3526 return 0;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003527do_state_recovery:
3528 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3529 nfs4_schedule_state_recovery(clp);
3530 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3531 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3532 task->tk_status = 0;
3533 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534}
3535
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003536int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3537 unsigned short port, struct rpc_cred *cred,
3538 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539{
3540 nfs4_verifier sc_verifier;
3541 struct nfs4_setclientid setclientid = {
3542 .sc_verifier = &sc_verifier,
3543 .sc_prog = program,
3544 };
3545 struct rpc_message msg = {
3546 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3547 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003548 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003549 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 };
Al Virobc4785c2006-10-19 23:28:51 -07003551 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 int loop = 0;
3553 int status;
3554
Al Virobc4785c2006-10-19 23:28:51 -07003555 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3557 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3558
3559 for(;;) {
3560 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003561 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003562 clp->cl_ipaddr,
3563 rpc_peeraddr2str(clp->cl_rpcclient,
3564 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003565 rpc_peeraddr2str(clp->cl_rpcclient,
3566 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003567 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 clp->cl_id_uniquifier);
3569 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003570 sizeof(setclientid.sc_netid),
3571 rpc_peeraddr2str(clp->cl_rpcclient,
3572 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003574 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 clp->cl_ipaddr, port >> 8, port & 255);
3576
3577 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3578 if (status != -NFS4ERR_CLID_INUSE)
3579 break;
3580 if (signalled())
3581 break;
3582 if (loop++ & 1)
3583 ssleep(clp->cl_lease_time + 1);
3584 else
3585 if (++clp->cl_id_uniquifier == 0)
3586 break;
3587 }
3588 return status;
3589}
3590
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003591static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3592 struct nfs4_setclientid_res *arg,
3593 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594{
3595 struct nfs_fsinfo fsinfo;
3596 struct rpc_message msg = {
3597 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003598 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003600 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 };
3602 unsigned long now;
3603 int status;
3604
3605 now = jiffies;
3606 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3607 if (status == 0) {
3608 spin_lock(&clp->cl_lock);
3609 clp->cl_lease_time = fsinfo.lease_time * HZ;
3610 clp->cl_last_renewal = now;
3611 spin_unlock(&clp->cl_lock);
3612 }
3613 return status;
3614}
3615
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003616int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3617 struct nfs4_setclientid_res *arg,
3618 struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003619{
Benny Halevy77e03672008-06-24 20:25:57 +03003620 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003621 int err;
3622 do {
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003623 err = _nfs4_proc_setclientid_confirm(clp, arg, cred);
Trond Myklebust51581f32006-03-20 13:44:47 -05003624 switch (err) {
3625 case 0:
3626 return err;
3627 case -NFS4ERR_RESOURCE:
3628 /* The IBM lawyers misread another document! */
3629 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05003630 case -EKEYEXPIRED:
Trond Myklebust51581f32006-03-20 13:44:47 -05003631 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3632 }
3633 } while (err == 0);
3634 return err;
3635}
3636
Trond Myklebustfe650402006-01-03 09:55:18 +01003637struct nfs4_delegreturndata {
3638 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003639 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003640 struct nfs_fh fh;
3641 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003642 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003643 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003644 int rpc_status;
3645};
3646
Trond Myklebustfe650402006-01-03 09:55:18 +01003647static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3648{
3649 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003650
Trond Myklebust14516c32010-07-31 14:29:06 -04003651 if (!nfs4_sequence_done(task, &data->res.seq_res))
3652 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003653
Ricardo Labiaga79708862009-12-07 09:23:21 -05003654 switch (task->tk_status) {
3655 case -NFS4ERR_STALE_STATEID:
3656 case -NFS4ERR_EXPIRED:
3657 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003658 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003659 break;
3660 default:
3661 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3662 -EAGAIN) {
3663 nfs_restart_rpc(task, data->res.server->nfs_client);
3664 return;
3665 }
3666 }
3667 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003668}
3669
3670static void nfs4_delegreturn_release(void *calldata)
3671{
Trond Myklebustfe650402006-01-03 09:55:18 +01003672 kfree(calldata);
3673}
3674
Andy Adamson938e1012009-04-01 09:22:28 -04003675#if defined(CONFIG_NFS_V4_1)
3676static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3677{
3678 struct nfs4_delegreturndata *d_data;
3679
3680 d_data = (struct nfs4_delegreturndata *)data;
3681
Trond Myklebust035168a2010-06-16 09:52:26 -04003682 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003683 &d_data->args.seq_args,
3684 &d_data->res.seq_res, 1, task))
3685 return;
3686 rpc_call_start(task);
3687}
3688#endif /* CONFIG_NFS_V4_1 */
3689
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003690static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003691#if defined(CONFIG_NFS_V4_1)
3692 .rpc_call_prepare = nfs4_delegreturn_prepare,
3693#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003694 .rpc_call_done = nfs4_delegreturn_done,
3695 .rpc_release = nfs4_delegreturn_release,
3696};
3697
Trond Myklebuste6f81072008-01-24 18:14:34 -05003698static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003699{
3700 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003701 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003702 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003703 struct rpc_message msg = {
3704 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3705 .rpc_cred = cred,
3706 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003707 struct rpc_task_setup task_setup_data = {
3708 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003709 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003710 .callback_ops = &nfs4_delegreturn_ops,
3711 .flags = RPC_TASK_ASYNC,
3712 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003713 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003714
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003715 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003716 if (data == NULL)
3717 return -ENOMEM;
3718 data->args.fhandle = &data->fh;
3719 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003720 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003721 nfs_copy_fh(&data->fh, NFS_FH(inode));
3722 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003723 data->res.fattr = &data->fattr;
3724 data->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003725 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003726 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003727 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003728 data->rpc_status = 0;
3729
Trond Myklebustc970aa82007-07-14 15:39:59 -04003730 task_setup_data.callback_data = data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003731 msg.rpc_argp = &data->args,
3732 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003733 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003734 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003735 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003736 if (!issync)
3737 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003738 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003739 if (status != 0)
3740 goto out;
3741 status = data->rpc_status;
3742 if (status != 0)
3743 goto out;
3744 nfs_refresh_inode(inode, &data->fattr);
3745out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003746 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003747 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748}
3749
Trond Myklebuste6f81072008-01-24 18:14:34 -05003750int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751{
3752 struct nfs_server *server = NFS_SERVER(inode);
3753 struct nfs4_exception exception = { };
3754 int err;
3755 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003756 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 switch (err) {
3758 case -NFS4ERR_STALE_STATEID:
3759 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 case 0:
3761 return 0;
3762 }
3763 err = nfs4_handle_exception(server, err, &exception);
3764 } while (exception.retry);
3765 return err;
3766}
3767
3768#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3769#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3770
3771/*
3772 * sleep, with exponential backoff, and retry the LOCK operation.
3773 */
3774static unsigned long
3775nfs4_set_lock_task_retry(unsigned long timeout)
3776{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003777 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 timeout <<= 1;
3779 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3780 return NFS4_LOCK_MAXTIMEOUT;
3781 return timeout;
3782}
3783
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3785{
3786 struct inode *inode = state->inode;
3787 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003788 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003789 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003791 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003793 struct nfs_lockt_res res = {
3794 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 };
3796 struct rpc_message msg = {
3797 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3798 .rpc_argp = &arg,
3799 .rpc_resp = &res,
3800 .rpc_cred = state->owner->so_cred,
3801 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 struct nfs4_lock_state *lsp;
3803 int status;
3804
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003805 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003806 status = nfs4_set_lock_state(state, request);
3807 if (status != 0)
3808 goto out;
3809 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003810 arg.lock_owner.id = lsp->ls_id.id;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003811 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003812 switch (status) {
3813 case 0:
3814 request->fl_type = F_UNLCK;
3815 break;
3816 case -NFS4ERR_DENIED:
3817 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003819 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003820out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 return status;
3822}
3823
3824static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3825{
3826 struct nfs4_exception exception = { };
3827 int err;
3828
3829 do {
3830 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3831 _nfs4_proc_getlk(state, cmd, request),
3832 &exception);
3833 } while (exception.retry);
3834 return err;
3835}
3836
3837static int do_vfs_lock(struct file *file, struct file_lock *fl)
3838{
3839 int res = 0;
3840 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3841 case FL_POSIX:
3842 res = posix_lock_file_wait(file, fl);
3843 break;
3844 case FL_FLOCK:
3845 res = flock_lock_file_wait(file, fl);
3846 break;
3847 default:
3848 BUG();
3849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 return res;
3851}
3852
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003853struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003854 struct nfs_locku_args arg;
3855 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003856 struct nfs4_lock_state *lsp;
3857 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003858 struct file_lock fl;
3859 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003860 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003861};
3862
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003863static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3864 struct nfs_open_context *ctx,
3865 struct nfs4_lock_state *lsp,
3866 struct nfs_seqid *seqid)
3867{
3868 struct nfs4_unlockdata *p;
3869 struct inode *inode = lsp->ls_state->inode;
3870
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003871 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003872 if (p == NULL)
3873 return NULL;
3874 p->arg.fh = NFS_FH(inode);
3875 p->arg.fl = &p->fl;
3876 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003877 p->res.seqid = seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003878 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003879 p->arg.stateid = &lsp->ls_stateid;
3880 p->lsp = lsp;
3881 atomic_inc(&lsp->ls_count);
3882 /* Ensure we don't close file until we're done freeing locks! */
3883 p->ctx = get_nfs_open_context(ctx);
3884 memcpy(&p->fl, fl, sizeof(p->fl));
3885 p->server = NFS_SERVER(inode);
3886 return p;
3887}
3888
Trond Myklebust06f814a2006-01-03 09:55:07 +01003889static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003890{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003891 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003892 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003893 nfs4_put_lock_state(calldata->lsp);
3894 put_nfs_open_context(calldata->ctx);
3895 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003896}
3897
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003898static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003899{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003900 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003901
Trond Myklebust14516c32010-07-31 14:29:06 -04003902 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3903 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003904 switch (task->tk_status) {
3905 case 0:
3906 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003907 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003908 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003909 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003910 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003911 case -NFS4ERR_BAD_STATEID:
3912 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003913 case -NFS4ERR_STALE_STATEID:
3914 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003915 break;
3916 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003917 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebust0110ee12009-12-07 09:00:24 -05003918 nfs_restart_rpc(task,
Trond Myklebustd61e6122009-12-05 19:32:19 -05003919 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003920 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003921}
3922
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003923static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003924{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003925 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003927 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003928 return;
3929 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003930 /* Note: exit _without_ running nfs4_locku_done */
3931 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003932 return;
3933 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003934 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04003935 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04003936 &calldata->arg.seq_args,
3937 &calldata->res.seq_res, 1, task))
3938 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003939 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940}
3941
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003942static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003943 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003944 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003945 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003946};
3947
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003948static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3949 struct nfs_open_context *ctx,
3950 struct nfs4_lock_state *lsp,
3951 struct nfs_seqid *seqid)
3952{
3953 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003954 struct rpc_message msg = {
3955 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3956 .rpc_cred = ctx->cred,
3957 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003958 struct rpc_task_setup task_setup_data = {
3959 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003960 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003961 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003962 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003963 .flags = RPC_TASK_ASYNC,
3964 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003965
Frank Filz137d6ac2007-07-09 15:32:29 -07003966 /* Ensure this is an unlock - when canceling a lock, the
3967 * canceled lock is passed in, and it won't be an unlock.
3968 */
3969 fl->fl_type = F_UNLCK;
3970
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003971 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3972 if (data == NULL) {
3973 nfs_free_seqid(seqid);
3974 return ERR_PTR(-ENOMEM);
3975 }
3976
Trond Myklebust5138fde2007-07-14 15:40:01 -04003977 msg.rpc_argp = &data->arg,
3978 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003979 task_setup_data.callback_data = data;
3980 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003981}
3982
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3984{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003985 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003986 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003987 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003988 struct rpc_task *task;
3989 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003990 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991
Trond Myklebust9b073572006-06-29 16:38:34 -04003992 status = nfs4_set_lock_state(state, request);
3993 /* Unlock _before_ we do the RPC call */
3994 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003995 down_read(&nfsi->rwsem);
3996 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3997 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003998 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003999 }
4000 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004001 if (status != 0)
4002 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004003 /* Is this a delegated lock? */
4004 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004005 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004006 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004007 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004008 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004009 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004010 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004011 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004012 status = PTR_ERR(task);
4013 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004014 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004015 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004016 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004017out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004018 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004019 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020}
4021
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004022struct nfs4_lockdata {
4023 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004024 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004025 struct nfs4_lock_state *lsp;
4026 struct nfs_open_context *ctx;
4027 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004028 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004029 int rpc_status;
4030 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004031 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004032};
4033
4034static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004035 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4036 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004037{
4038 struct nfs4_lockdata *p;
4039 struct inode *inode = lsp->ls_state->inode;
4040 struct nfs_server *server = NFS_SERVER(inode);
4041
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004042 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004043 if (p == NULL)
4044 return NULL;
4045
4046 p->arg.fh = NFS_FH(inode);
4047 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004048 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004049 if (p->arg.open_seqid == NULL)
4050 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004051 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004052 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004053 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004054 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004055 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004056 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004057 p->res.lock_seqid = p->arg.lock_seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04004058 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004059 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004060 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004061 atomic_inc(&lsp->ls_count);
4062 p->ctx = get_nfs_open_context(ctx);
4063 memcpy(&p->fl, fl, sizeof(p->fl));
4064 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004065out_free_seqid:
4066 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004067out_free:
4068 kfree(p);
4069 return NULL;
4070}
4071
4072static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4073{
4074 struct nfs4_lockdata *data = calldata;
4075 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076
Harvey Harrison3110ff82008-05-02 13:42:44 -07004077 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004078 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4079 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004080 /* Do we need to do an open_to_lock_owner? */
4081 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004082 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4083 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004084 data->arg.open_stateid = &state->stateid;
4085 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004086 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004087 } else
4088 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004089 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004090 if (nfs4_setup_sequence(data->server,
4091 &data->arg.seq_args,
Andy Adamson66179ef2009-04-01 09:22:22 -04004092 &data->res.seq_res, 1, task))
4093 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004094 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004095 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004096}
4097
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004098static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4099{
4100 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4101 nfs4_lock_prepare(task, calldata);
4102}
4103
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004104static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4105{
4106 struct nfs4_lockdata *data = calldata;
4107
Harvey Harrison3110ff82008-05-02 13:42:44 -07004108 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004109
Trond Myklebust14516c32010-07-31 14:29:06 -04004110 if (!nfs4_sequence_done(task, &data->res.seq_res))
4111 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004112
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004113 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004114 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004115 if (data->rpc_status == 0)
4116 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4117 else
4118 goto out;
4119 }
4120 if (data->rpc_status == 0) {
4121 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4122 sizeof(data->lsp->ls_stateid.data));
4123 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04004124 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004125 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004126out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004127 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004128}
4129
4130static void nfs4_lock_release(void *calldata)
4131{
4132 struct nfs4_lockdata *data = calldata;
4133
Harvey Harrison3110ff82008-05-02 13:42:44 -07004134 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004135 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004136 if (data->cancelled != 0) {
4137 struct rpc_task *task;
4138 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4139 data->arg.lock_seqid);
4140 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004141 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004142 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004143 } else
4144 nfs_free_seqid(data->arg.lock_seqid);
4145 nfs4_put_lock_state(data->lsp);
4146 put_nfs_open_context(data->ctx);
4147 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004148 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004149}
4150
4151static const struct rpc_call_ops nfs4_lock_ops = {
4152 .rpc_call_prepare = nfs4_lock_prepare,
4153 .rpc_call_done = nfs4_lock_done,
4154 .rpc_release = nfs4_lock_release,
4155};
4156
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004157static const struct rpc_call_ops nfs4_recover_lock_ops = {
4158 .rpc_call_prepare = nfs4_recover_lock_prepare,
4159 .rpc_call_done = nfs4_lock_done,
4160 .rpc_release = nfs4_lock_release,
4161};
4162
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004163static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4164{
4165 struct nfs_client *clp = server->nfs_client;
4166 struct nfs4_state *state = lsp->ls_state;
4167
4168 switch (error) {
4169 case -NFS4ERR_ADMIN_REVOKED:
4170 case -NFS4ERR_BAD_STATEID:
4171 case -NFS4ERR_EXPIRED:
4172 if (new_lock_owner != 0 ||
4173 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4174 nfs4_state_mark_reclaim_nograce(clp, state);
4175 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004176 break;
4177 case -NFS4ERR_STALE_STATEID:
4178 if (new_lock_owner != 0 ||
4179 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4180 nfs4_state_mark_reclaim_reboot(clp, state);
4181 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004182 };
4183}
4184
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004185static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004186{
4187 struct nfs4_lockdata *data;
4188 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004189 struct rpc_message msg = {
4190 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4191 .rpc_cred = state->owner->so_cred,
4192 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004193 struct rpc_task_setup task_setup_data = {
4194 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004195 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004196 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004197 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004198 .flags = RPC_TASK_ASYNC,
4199 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004200 int ret;
4201
Harvey Harrison3110ff82008-05-02 13:42:44 -07004202 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004203 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004204 fl->fl_u.nfs4_fl.owner,
4205 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004206 if (data == NULL)
4207 return -ENOMEM;
4208 if (IS_SETLKW(cmd))
4209 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004210 if (recovery_type > NFS_LOCK_NEW) {
4211 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004212 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004213 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4214 }
Trond Myklebust5138fde2007-07-14 15:40:01 -04004215 msg.rpc_argp = &data->arg,
4216 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004217 task_setup_data.callback_data = data;
4218 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004219 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004220 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004221 ret = nfs4_wait_for_completion_rpc_task(task);
4222 if (ret == 0) {
4223 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004224 if (ret)
4225 nfs4_handle_setlk_error(data->server, data->lsp,
4226 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004227 } else
4228 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004229 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004230 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004231 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232}
4233
4234static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4235{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004236 struct nfs_server *server = NFS_SERVER(state->inode);
4237 struct nfs4_exception exception = { };
4238 int err;
4239
4240 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004241 /* Cache the lock if possible... */
4242 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4243 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004244 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Jeff Layton2c643482010-01-07 09:42:03 -05004245 if (err != -NFS4ERR_DELAY && err != -EKEYEXPIRED)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004246 break;
4247 nfs4_handle_exception(server, err, &exception);
4248 } while (exception.retry);
4249 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250}
4251
4252static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4253{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004254 struct nfs_server *server = NFS_SERVER(state->inode);
4255 struct nfs4_exception exception = { };
4256 int err;
4257
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004258 err = nfs4_set_lock_state(state, request);
4259 if (err != 0)
4260 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004261 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004262 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4263 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004264 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004265 switch (err) {
4266 default:
4267 goto out;
4268 case -NFS4ERR_GRACE:
4269 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05004270 case -EKEYEXPIRED:
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004271 nfs4_handle_exception(server, err, &exception);
4272 err = 0;
4273 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004274 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004275out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004276 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277}
4278
4279static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4280{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004281 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004282 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004283 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004285 if ((fl_flags & FL_POSIX) &&
4286 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4287 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004288 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004289 status = nfs4_set_lock_state(state, request);
4290 if (status != 0)
4291 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004292 request->fl_flags |= FL_ACCESS;
4293 status = do_vfs_lock(request->fl_file, request);
4294 if (status < 0)
4295 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004296 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004297 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004298 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004299 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004300 request->fl_flags = fl_flags & ~FL_SLEEP;
4301 status = do_vfs_lock(request->fl_file, request);
4302 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004303 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004304 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004305 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004306 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004307 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004308 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004309 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004310 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004311out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004312 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004313out:
4314 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 return status;
4316}
4317
4318static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4319{
4320 struct nfs4_exception exception = { };
4321 int err;
4322
4323 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004324 err = _nfs4_proc_setlk(state, cmd, request);
4325 if (err == -NFS4ERR_DENIED)
4326 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004328 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 } while (exception.retry);
4330 return err;
4331}
4332
4333static int
4334nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4335{
4336 struct nfs_open_context *ctx;
4337 struct nfs4_state *state;
4338 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4339 int status;
4340
4341 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004342 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 state = ctx->state;
4344
4345 if (request->fl_start < 0 || request->fl_end < 0)
4346 return -EINVAL;
4347
Trond Myklebustd9531262009-07-21 19:22:38 -04004348 if (IS_GETLK(cmd)) {
4349 if (state != NULL)
4350 return nfs4_proc_getlk(state, F_GETLK, request);
4351 return 0;
4352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353
4354 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4355 return -EINVAL;
4356
Trond Myklebustd9531262009-07-21 19:22:38 -04004357 if (request->fl_type == F_UNLCK) {
4358 if (state != NULL)
4359 return nfs4_proc_unlck(state, cmd, request);
4360 return 0;
4361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362
Trond Myklebustd9531262009-07-21 19:22:38 -04004363 if (state == NULL)
4364 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 do {
4366 status = nfs4_proc_setlk(state, cmd, request);
4367 if ((status != -EAGAIN) || IS_SETLK(cmd))
4368 break;
4369 timeout = nfs4_set_lock_task_retry(timeout);
4370 status = -ERESTARTSYS;
4371 if (signalled())
4372 break;
4373 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 return status;
4375}
4376
Trond Myklebust888e6942005-11-04 15:38:11 -05004377int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4378{
4379 struct nfs_server *server = NFS_SERVER(state->inode);
4380 struct nfs4_exception exception = { };
4381 int err;
4382
4383 err = nfs4_set_lock_state(state, fl);
4384 if (err != 0)
4385 goto out;
4386 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004387 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004388 switch (err) {
4389 default:
4390 printk(KERN_ERR "%s: unhandled error %d.\n",
4391 __func__, err);
4392 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004393 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004394 goto out;
4395 case -NFS4ERR_EXPIRED:
4396 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004397 case -NFS4ERR_STALE_STATEID:
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004398 case -NFS4ERR_BADSESSION:
4399 case -NFS4ERR_BADSLOT:
4400 case -NFS4ERR_BAD_HIGH_SLOT:
4401 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4402 case -NFS4ERR_DEADSESSION:
Trond Myklebustd5122202009-06-17 13:22:58 -07004403 nfs4_schedule_state_recovery(server->nfs_client);
4404 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004405 case -ERESTARTSYS:
4406 /*
4407 * The show must go on: exit, but mark the
4408 * stateid as needing recovery.
4409 */
4410 case -NFS4ERR_ADMIN_REVOKED:
4411 case -NFS4ERR_BAD_STATEID:
4412 case -NFS4ERR_OPENMODE:
4413 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4414 err = 0;
4415 goto out;
4416 case -ENOMEM:
4417 case -NFS4ERR_DENIED:
4418 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4419 err = 0;
4420 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004421 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05004422 case -EKEYEXPIRED:
Trond Myklebustd5122202009-06-17 13:22:58 -07004423 break;
4424 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004425 err = nfs4_handle_exception(server, err, &exception);
4426 } while (exception.retry);
4427out:
4428 return err;
4429}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004430
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004431static void nfs4_release_lockowner_release(void *calldata)
4432{
4433 kfree(calldata);
4434}
4435
4436const struct rpc_call_ops nfs4_release_lockowner_ops = {
4437 .rpc_release = nfs4_release_lockowner_release,
4438};
4439
4440void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4441{
4442 struct nfs_server *server = lsp->ls_state->owner->so_server;
4443 struct nfs_release_lockowner_args *args;
4444 struct rpc_message msg = {
4445 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4446 };
4447
4448 if (server->nfs_client->cl_mvops->minor_version != 0)
4449 return;
4450 args = kmalloc(sizeof(*args), GFP_NOFS);
4451 if (!args)
4452 return;
4453 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4454 args->lock_owner.id = lsp->ls_id.id;
4455 msg.rpc_argp = args;
4456 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4457}
4458
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004459#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4460
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004461int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4462 size_t buflen, int flags)
4463{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004464 struct inode *inode = dentry->d_inode;
4465
4466 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4467 return -EOPNOTSUPP;
4468
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004469 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004470}
4471
4472/* The getxattr man page suggests returning -ENODATA for unknown attributes,
4473 * and that's what we'll do for e.g. user attributes that haven't been set.
4474 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4475 * attributes in kernel-managed attribute namespaces. */
4476ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4477 size_t buflen)
4478{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004479 struct inode *inode = dentry->d_inode;
4480
4481 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4482 return -EOPNOTSUPP;
4483
4484 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004485}
4486
4487ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4488{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004489 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004490
J. Bruce Fields096455a2006-03-20 23:23:42 -05004491 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4492 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004493 if (buf && buflen < len)
4494 return -ERANGE;
4495 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004496 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4497 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004498}
4499
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004500static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4501{
4502 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4503 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4504 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4505 return;
4506
4507 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4508 NFS_ATTR_FATTR_NLINK;
4509 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4510 fattr->nlink = 2;
4511}
4512
Trond Myklebust56659e92007-07-17 21:52:39 -04004513int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004514 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004515{
4516 struct nfs_server *server = NFS_SERVER(dir);
4517 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004518 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4519 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004520 };
4521 struct nfs4_fs_locations_arg args = {
4522 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004523 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004524 .page = page,
4525 .bitmask = bitmask,
4526 };
Benny Halevy22958462009-04-01 09:22:02 -04004527 struct nfs4_fs_locations_res res = {
4528 .fs_locations = fs_locations,
4529 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004530 struct rpc_message msg = {
4531 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4532 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004533 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004534 };
4535 int status;
4536
Harvey Harrison3110ff82008-05-02 13:42:44 -07004537 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004538 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004539 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004540 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04004541 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004542 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004543 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004544 return status;
4545}
4546
Andy Adamson557134a2009-04-01 09:21:53 -04004547#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004548/*
4549 * nfs4_proc_exchange_id()
4550 *
4551 * Since the clientid has expired, all compounds using sessions
4552 * associated with the stale clientid will be returning
4553 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4554 * be in some phase of session reset.
4555 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004556int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004557{
4558 nfs4_verifier verifier;
4559 struct nfs41_exchange_id_args args = {
4560 .client = clp,
4561 .flags = clp->cl_exchange_flags,
4562 };
4563 struct nfs41_exchange_id_res res = {
4564 .client = clp,
4565 };
4566 int status;
4567 struct rpc_message msg = {
4568 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4569 .rpc_argp = &args,
4570 .rpc_resp = &res,
4571 .rpc_cred = cred,
4572 };
4573 __be32 *p;
4574
4575 dprintk("--> %s\n", __func__);
4576 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004577
Alexandros Batsakis7b183d02009-12-05 13:33:25 -05004578 /* Remove server-only flags */
4579 args.flags &= ~EXCHGID4_FLAG_CONFIRMED_R;
4580
Benny Halevy99fe60d2009-04-01 09:22:29 -04004581 p = (u32 *)verifier.data;
4582 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4583 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4584 args.verifier = &verifier;
4585
4586 while (1) {
4587 args.id_len = scnprintf(args.id, sizeof(args.id),
4588 "%s/%s %u",
4589 clp->cl_ipaddr,
4590 rpc_peeraddr2str(clp->cl_rpcclient,
4591 RPC_DISPLAY_ADDR),
4592 clp->cl_id_uniquifier);
4593
4594 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4595
Andy Adamson180b62a2010-03-02 13:19:36 -05004596 if (status != -NFS4ERR_CLID_INUSE)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004597 break;
4598
4599 if (signalled())
4600 break;
4601
4602 if (++clp->cl_id_uniquifier == 0)
4603 break;
4604 }
4605
4606 dprintk("<-- %s status= %d\n", __func__, status);
4607 return status;
4608}
4609
Andy Adamson2050f0c2009-04-01 09:22:30 -04004610struct nfs4_get_lease_time_data {
4611 struct nfs4_get_lease_time_args *args;
4612 struct nfs4_get_lease_time_res *res;
4613 struct nfs_client *clp;
4614};
4615
4616static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4617 void *calldata)
4618{
4619 int ret;
4620 struct nfs4_get_lease_time_data *data =
4621 (struct nfs4_get_lease_time_data *)calldata;
4622
4623 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004624 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004625 /* just setup sequence, do not trigger session recovery
4626 since we're invoked within one */
4627 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004628 &data->args->la_seq_args,
4629 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004630
4631 BUG_ON(ret == -EAGAIN);
4632 rpc_call_start(task);
4633 dprintk("<-- %s\n", __func__);
4634}
4635
4636/*
4637 * Called from nfs4_state_manager thread for session setup, so don't recover
4638 * from sequence operation or clientid errors.
4639 */
4640static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4641{
4642 struct nfs4_get_lease_time_data *data =
4643 (struct nfs4_get_lease_time_data *)calldata;
4644
4645 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04004646 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4647 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04004648 switch (task->tk_status) {
4649 case -NFS4ERR_DELAY:
4650 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05004651 case -EKEYEXPIRED:
Andy Adamson2050f0c2009-04-01 09:22:30 -04004652 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4653 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4654 task->tk_status = 0;
Trond Myklebust0110ee12009-12-07 09:00:24 -05004655 nfs_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004656 return;
4657 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004658 dprintk("<-- %s\n", __func__);
4659}
4660
4661struct rpc_call_ops nfs4_get_lease_time_ops = {
4662 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4663 .rpc_call_done = nfs4_get_lease_time_done,
4664};
4665
4666int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4667{
4668 struct rpc_task *task;
4669 struct nfs4_get_lease_time_args args;
4670 struct nfs4_get_lease_time_res res = {
4671 .lr_fsinfo = fsinfo,
4672 };
4673 struct nfs4_get_lease_time_data data = {
4674 .args = &args,
4675 .res = &res,
4676 .clp = clp,
4677 };
4678 struct rpc_message msg = {
4679 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4680 .rpc_argp = &args,
4681 .rpc_resp = &res,
4682 };
4683 struct rpc_task_setup task_setup = {
4684 .rpc_client = clp->cl_rpcclient,
4685 .rpc_message = &msg,
4686 .callback_ops = &nfs4_get_lease_time_ops,
4687 .callback_data = &data
4688 };
4689 int status;
4690
4691 res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
4692 dprintk("--> %s\n", __func__);
4693 task = rpc_run_task(&task_setup);
4694
4695 if (IS_ERR(task))
4696 status = PTR_ERR(task);
4697 else {
4698 status = task->tk_status;
4699 rpc_put_task(task);
4700 }
4701 dprintk("<-- %s return %d\n", __func__, status);
4702
4703 return status;
4704}
4705
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004706/*
4707 * Reset a slot table
4708 */
Andy Adamson104aeba2010-01-14 17:45:10 -05004709static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4710 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004711{
Andy Adamson104aeba2010-01-14 17:45:10 -05004712 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004713 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004714 int ret = 0;
4715
Andy Adamson104aeba2010-01-14 17:45:10 -05004716 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4717 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004718
Andy Adamson104aeba2010-01-14 17:45:10 -05004719 /* Does the newly negotiated max_reqs match the existing slot table? */
4720 if (max_reqs != tbl->max_slots) {
4721 ret = -ENOMEM;
4722 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004723 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05004724 if (!new)
4725 goto out;
4726 ret = 0;
4727 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004728 }
4729 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05004730 if (new) {
4731 tbl->slots = new;
4732 tbl->max_slots = max_reqs;
4733 }
4734 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004735 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004736 spin_unlock(&tbl->slot_tbl_lock);
4737 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4738 tbl, tbl->slots, tbl->max_slots);
4739out:
4740 dprintk("<-- %s: return %d\n", __func__, ret);
4741 return ret;
4742}
4743
Andy Adamsonfc931582009-04-01 09:22:31 -04004744/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004745 * Reset the forechannel and backchannel slot tables
4746 */
4747static int nfs4_reset_slot_tables(struct nfs4_session *session)
4748{
4749 int status;
4750
4751 status = nfs4_reset_slot_table(&session->fc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004752 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004753 if (status)
4754 return status;
4755
4756 status = nfs4_reset_slot_table(&session->bc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004757 session->bc_attrs.max_reqs, 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004758 return status;
4759}
4760
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004761/* Destroy the slot table */
4762static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4763{
4764 if (session->fc_slot_table.slots != NULL) {
4765 kfree(session->fc_slot_table.slots);
4766 session->fc_slot_table.slots = NULL;
4767 }
4768 if (session->bc_slot_table.slots != NULL) {
4769 kfree(session->bc_slot_table.slots);
4770 session->bc_slot_table.slots = NULL;
4771 }
4772 return;
4773}
4774
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004775/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004776 * Initialize slot table
4777 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004778static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4779 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04004780{
Andy Adamsonfc931582009-04-01 09:22:31 -04004781 struct nfs4_slot *slot;
4782 int ret = -ENOMEM;
4783
4784 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4785
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004786 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04004787
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004788 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04004789 if (!slot)
4790 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04004791 ret = 0;
4792
4793 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04004794 tbl->max_slots = max_slots;
4795 tbl->slots = slot;
4796 tbl->highest_used_slotid = -1; /* no slot is currently used */
4797 spin_unlock(&tbl->slot_tbl_lock);
4798 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4799 tbl, tbl->slots, tbl->max_slots);
4800out:
4801 dprintk("<-- %s: return %d\n", __func__, ret);
4802 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04004803}
4804
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004805/*
4806 * Initialize the forechannel and backchannel tables
4807 */
4808static int nfs4_init_slot_tables(struct nfs4_session *session)
4809{
Trond Myklebustf26468f2009-12-05 19:32:11 -05004810 struct nfs4_slot_table *tbl;
4811 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004812
Trond Myklebustf26468f2009-12-05 19:32:11 -05004813 tbl = &session->fc_slot_table;
4814 if (tbl->slots == NULL) {
4815 status = nfs4_init_slot_table(tbl,
4816 session->fc_attrs.max_reqs, 1);
4817 if (status)
4818 return status;
4819 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004820
Trond Myklebustf26468f2009-12-05 19:32:11 -05004821 tbl = &session->bc_slot_table;
4822 if (tbl->slots == NULL) {
4823 status = nfs4_init_slot_table(tbl,
4824 session->bc_attrs.max_reqs, 0);
4825 if (status)
4826 nfs4_destroy_slot_tables(session);
4827 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004828
4829 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04004830}
4831
4832struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4833{
4834 struct nfs4_session *session;
4835 struct nfs4_slot_table *tbl;
4836
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004837 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04004838 if (!session)
4839 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04004840
Andy Adamsonea028ac2009-12-04 15:55:38 -05004841 init_completion(&session->complete);
Andy Adamson76db6d92009-04-01 09:22:38 -04004842
Andy Adamson557134a2009-04-01 09:21:53 -04004843 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004844 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04004845 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004846 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004847
4848 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004849 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004850 spin_lock_init(&tbl->slot_tbl_lock);
4851 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4852
Trond Myklebust1055d762010-06-16 09:52:27 -04004853 session->session_state = 1<<NFS4_SESSION_INITING;
4854
Andy Adamson557134a2009-04-01 09:21:53 -04004855 session->clp = clp;
4856 return session;
4857}
4858
4859void nfs4_destroy_session(struct nfs4_session *session)
4860{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04004861 nfs4_proc_destroy_session(session);
4862 dprintk("%s Destroy backchannel for xprt %p\n",
4863 __func__, session->clp->cl_rpcclient->cl_xprt);
4864 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4865 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004866 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04004867 kfree(session);
4868}
4869
Andy Adamsonfc931582009-04-01 09:22:31 -04004870/*
4871 * Initialize the values to be used by the client in CREATE_SESSION
4872 * If nfs4_init_session set the fore channel request and response sizes,
4873 * use them.
4874 *
4875 * Set the back channel max_resp_sz_cached to zero to force the client to
4876 * always set csa_cachethis to FALSE because the current implementation
4877 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4878 */
4879static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4880{
4881 struct nfs4_session *session = args->client->cl_session;
4882 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4883 mxresp_sz = session->fc_attrs.max_resp_sz;
4884
4885 if (mxrqst_sz == 0)
4886 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4887 if (mxresp_sz == 0)
4888 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4889 /* Fore channel attributes */
4890 args->fc_attrs.headerpadsz = 0;
4891 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4892 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04004893 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4894 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4895
4896 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05004897 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04004898 __func__,
4899 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05004900 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04004901
4902 /* Back channel attributes */
4903 args->bc_attrs.headerpadsz = 0;
4904 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4905 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4906 args->bc_attrs.max_resp_sz_cached = 0;
4907 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4908 args->bc_attrs.max_reqs = 1;
4909
4910 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4911 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4912 __func__,
4913 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4914 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4915 args->bc_attrs.max_reqs);
4916}
4917
Andy Adamson8d353012009-04-01 09:22:32 -04004918static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
4919{
4920 if (rcvd <= sent)
4921 return 0;
4922 printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
4923 "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
4924 return -EINVAL;
4925}
4926
4927#define _verify_fore_channel_attr(_name_) \
4928 _verify_channel_attr("fore", #_name_, \
4929 args->fc_attrs._name_, \
4930 session->fc_attrs._name_)
4931
4932#define _verify_back_channel_attr(_name_) \
4933 _verify_channel_attr("back", #_name_, \
4934 args->bc_attrs._name_, \
4935 session->bc_attrs._name_)
4936
4937/*
4938 * The server is not allowed to increase the fore channel header pad size,
4939 * maximum response size, or maximum number of operations.
4940 *
4941 * The back channel attributes are only negotiatied down: We send what the
4942 * (back channel) server insists upon.
4943 */
4944static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4945 struct nfs4_session *session)
4946{
4947 int ret = 0;
4948
4949 ret |= _verify_fore_channel_attr(headerpadsz);
4950 ret |= _verify_fore_channel_attr(max_resp_sz);
4951 ret |= _verify_fore_channel_attr(max_ops);
4952
4953 ret |= _verify_back_channel_attr(headerpadsz);
4954 ret |= _verify_back_channel_attr(max_rqst_sz);
4955 ret |= _verify_back_channel_attr(max_resp_sz);
4956 ret |= _verify_back_channel_attr(max_resp_sz_cached);
4957 ret |= _verify_back_channel_attr(max_ops);
4958 ret |= _verify_back_channel_attr(max_reqs);
4959
4960 return ret;
4961}
4962
Andy Adamsonfc931582009-04-01 09:22:31 -04004963static int _nfs4_proc_create_session(struct nfs_client *clp)
4964{
4965 struct nfs4_session *session = clp->cl_session;
4966 struct nfs41_create_session_args args = {
4967 .client = clp,
4968 .cb_program = NFS4_CALLBACK,
4969 };
4970 struct nfs41_create_session_res res = {
4971 .client = clp,
4972 };
4973 struct rpc_message msg = {
4974 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4975 .rpc_argp = &args,
4976 .rpc_resp = &res,
4977 };
4978 int status;
4979
4980 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04004981 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04004982
4983 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4984
Andy Adamson8d353012009-04-01 09:22:32 -04004985 if (!status)
4986 /* Verify the session's negotiated channel_attrs values */
4987 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04004988 if (!status) {
4989 /* Increment the clientid slot sequence id */
4990 clp->cl_seqid++;
4991 }
4992
4993 return status;
4994}
4995
4996/*
4997 * Issues a CREATE_SESSION operation to the server.
4998 * It is the responsibility of the caller to verify the session is
4999 * expired before calling this routine.
5000 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005001int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005002{
5003 int status;
5004 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005005 struct nfs4_session *session = clp->cl_session;
5006
5007 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5008
5009 status = _nfs4_proc_create_session(clp);
5010 if (status)
5011 goto out;
5012
Trond Myklebustf26468f2009-12-05 19:32:11 -05005013 /* Init and reset the fore channel */
5014 status = nfs4_init_slot_tables(session);
5015 dprintk("slot table initialization returned %d\n", status);
5016 if (status)
5017 goto out;
5018 status = nfs4_reset_slot_tables(session);
5019 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005020 if (status)
5021 goto out;
5022
5023 ptr = (unsigned *)&session->sess_id.data[0];
5024 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5025 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005026out:
5027 dprintk("<-- %s\n", __func__);
5028 return status;
5029}
5030
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005031/*
5032 * Issue the over-the-wire RPC DESTROY_SESSION.
5033 * The caller must serialize access to this routine.
5034 */
5035int nfs4_proc_destroy_session(struct nfs4_session *session)
5036{
5037 int status = 0;
5038 struct rpc_message msg;
5039
5040 dprintk("--> nfs4_proc_destroy_session\n");
5041
5042 /* session is still being setup */
5043 if (session->clp->cl_cons_state != NFS_CS_READY)
5044 return status;
5045
5046 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5047 msg.rpc_argp = session;
5048 msg.rpc_resp = NULL;
5049 msg.rpc_cred = NULL;
5050 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5051
5052 if (status)
5053 printk(KERN_WARNING
5054 "Got error %d from the server on DESTROY_SESSION. "
5055 "Session has been destroyed regardless...\n", status);
5056
5057 dprintk("<-- nfs4_proc_destroy_session\n");
5058 return status;
5059}
5060
Trond Myklebustfccba802009-07-21 16:48:07 -04005061int nfs4_init_session(struct nfs_server *server)
5062{
5063 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005064 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005065 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005066 int ret;
5067
5068 if (!nfs4_has_session(clp))
5069 return 0;
5070
Trond Myklebust1055d762010-06-16 09:52:27 -04005071 session = clp->cl_session;
5072 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5073 return 0;
5074
Andy Adamson68bf05e2009-12-15 12:55:02 -05005075 rsize = server->rsize;
5076 if (rsize == 0)
5077 rsize = NFS_MAX_FILE_IO_SIZE;
5078 wsize = server->wsize;
5079 if (wsize == 0)
5080 wsize = NFS_MAX_FILE_IO_SIZE;
5081
Andy Adamson68bf05e2009-12-15 12:55:02 -05005082 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5083 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005084
Trond Myklebustfccba802009-07-21 16:48:07 -04005085 ret = nfs4_recover_expired_lease(server);
5086 if (!ret)
5087 ret = nfs4_check_client_ready(clp);
5088 return ret;
5089}
5090
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005091/*
5092 * Renew the cl_session lease.
5093 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005094struct nfs4_sequence_data {
5095 struct nfs_client *clp;
5096 struct nfs4_sequence_args args;
5097 struct nfs4_sequence_res res;
5098};
5099
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005100static void nfs41_sequence_release(void *data)
5101{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005102 struct nfs4_sequence_data *calldata = data;
5103 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005104
Alexandros Batsakis71358402010-02-05 03:45:05 -08005105 if (atomic_read(&clp->cl_count) > 1)
5106 nfs4_schedule_state_renewal(clp);
5107 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005108 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005109}
5110
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005111static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5112{
5113 switch(task->tk_status) {
5114 case -NFS4ERR_DELAY:
5115 case -EKEYEXPIRED:
5116 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5117 return -EAGAIN;
5118 default:
5119 nfs4_schedule_state_recovery(clp);
5120 }
5121 return 0;
5122}
5123
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005124static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005125{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005126 struct nfs4_sequence_data *calldata = data;
5127 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005128
Trond Myklebust14516c32010-07-31 14:29:06 -04005129 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5130 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005131
5132 if (task->tk_status < 0) {
5133 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005134 if (atomic_read(&clp->cl_count) == 1)
5135 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005136
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005137 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5138 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005139 return;
5140 }
5141 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005142 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005143out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005144 dprintk("<-- %s\n", __func__);
5145}
5146
5147static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5148{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005149 struct nfs4_sequence_data *calldata = data;
5150 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005151 struct nfs4_sequence_args *args;
5152 struct nfs4_sequence_res *res;
5153
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005154 args = task->tk_msg.rpc_argp;
5155 res = task->tk_msg.rpc_resp;
5156
Trond Myklebust035168a2010-06-16 09:52:26 -04005157 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005158 return;
5159 rpc_call_start(task);
5160}
5161
5162static const struct rpc_call_ops nfs41_sequence_ops = {
5163 .rpc_call_done = nfs41_sequence_call_done,
5164 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005165 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005166};
5167
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005168static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005169{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005170 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005171 struct rpc_message msg = {
5172 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5173 .rpc_cred = cred,
5174 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005175 struct rpc_task_setup task_setup_data = {
5176 .rpc_client = clp->cl_rpcclient,
5177 .rpc_message = &msg,
5178 .callback_ops = &nfs41_sequence_ops,
5179 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5180 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005181
Alexandros Batsakis71358402010-02-05 03:45:05 -08005182 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005183 return ERR_PTR(-EIO);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005184 calldata = kmalloc(sizeof(*calldata), GFP_NOFS);
5185 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005186 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005187 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005188 }
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005189 calldata->res.sr_slotid = NFS4_MAX_SLOT_TABLE;
5190 msg.rpc_argp = &calldata->args;
5191 msg.rpc_resp = &calldata->res;
5192 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005193 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005194
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005195 return rpc_run_task(&task_setup_data);
5196}
5197
5198static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5199{
5200 struct rpc_task *task;
5201 int ret = 0;
5202
5203 task = _nfs41_proc_sequence(clp, cred);
5204 if (IS_ERR(task))
5205 ret = PTR_ERR(task);
5206 else
5207 rpc_put_task(task);
5208 dprintk("<-- %s status=%d\n", __func__, ret);
5209 return ret;
5210}
5211
5212static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5213{
5214 struct rpc_task *task;
5215 int ret;
5216
5217 task = _nfs41_proc_sequence(clp, cred);
5218 if (IS_ERR(task)) {
5219 ret = PTR_ERR(task);
5220 goto out;
5221 }
5222 ret = rpc_wait_for_completion_task(task);
5223 if (!ret)
5224 ret = task->tk_status;
5225 rpc_put_task(task);
5226out:
5227 dprintk("<-- %s status=%d\n", __func__, ret);
5228 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005229}
5230
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005231struct nfs4_reclaim_complete_data {
5232 struct nfs_client *clp;
5233 struct nfs41_reclaim_complete_args arg;
5234 struct nfs41_reclaim_complete_res res;
5235};
5236
5237static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5238{
5239 struct nfs4_reclaim_complete_data *calldata = data;
5240
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005241 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005242 if (nfs41_setup_sequence(calldata->clp->cl_session,
5243 &calldata->arg.seq_args,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005244 &calldata->res.seq_res, 0, task))
5245 return;
5246
5247 rpc_call_start(task);
5248}
5249
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005250static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5251{
5252 switch(task->tk_status) {
5253 case 0:
5254 case -NFS4ERR_COMPLETE_ALREADY:
5255 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5256 break;
5257 case -NFS4ERR_DELAY:
5258 case -EKEYEXPIRED:
5259 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5260 return -EAGAIN;
5261 default:
5262 nfs4_schedule_state_recovery(clp);
5263 }
5264 return 0;
5265}
5266
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005267static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5268{
5269 struct nfs4_reclaim_complete_data *calldata = data;
5270 struct nfs_client *clp = calldata->clp;
5271 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5272
5273 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005274 if (!nfs41_sequence_done(task, res))
5275 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005276
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005277 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5278 rpc_restart_call_prepare(task);
5279 return;
5280 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005281 dprintk("<-- %s\n", __func__);
5282}
5283
5284static void nfs4_free_reclaim_complete_data(void *data)
5285{
5286 struct nfs4_reclaim_complete_data *calldata = data;
5287
5288 kfree(calldata);
5289}
5290
5291static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5292 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5293 .rpc_call_done = nfs4_reclaim_complete_done,
5294 .rpc_release = nfs4_free_reclaim_complete_data,
5295};
5296
5297/*
5298 * Issue a global reclaim complete.
5299 */
5300static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5301{
5302 struct nfs4_reclaim_complete_data *calldata;
5303 struct rpc_task *task;
5304 struct rpc_message msg = {
5305 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5306 };
5307 struct rpc_task_setup task_setup_data = {
5308 .rpc_client = clp->cl_rpcclient,
5309 .rpc_message = &msg,
5310 .callback_ops = &nfs4_reclaim_complete_call_ops,
5311 .flags = RPC_TASK_ASYNC,
5312 };
5313 int status = -ENOMEM;
5314
5315 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005316 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005317 if (calldata == NULL)
5318 goto out;
5319 calldata->clp = clp;
5320 calldata->arg.one_fs = 0;
5321 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
5322
5323 msg.rpc_argp = &calldata->arg;
5324 msg.rpc_resp = &calldata->res;
5325 task_setup_data.callback_data = calldata;
5326 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005327 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005328 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005329 goto out;
5330 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005331 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005332 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005333out:
5334 dprintk("<-- %s status=%d\n", __func__, status);
5335 return status;
5336}
Andy Adamson557134a2009-04-01 09:21:53 -04005337#endif /* CONFIG_NFS_V4_1 */
5338
Andy Adamson591d71c2009-04-01 09:22:47 -04005339struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005340 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005341 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342 .recover_open = nfs4_open_reclaim,
5343 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04005344 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005345 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346};
5347
Andy Adamson591d71c2009-04-01 09:22:47 -04005348#if defined(CONFIG_NFS_V4_1)
5349struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5350 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5351 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5352 .recover_open = nfs4_open_reclaim,
5353 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05005354 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005355 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005356 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04005357};
5358#endif /* CONFIG_NFS_V4_1 */
5359
5360struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005361 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005362 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 .recover_open = nfs4_open_expired,
5364 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04005365 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005366 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367};
5368
Andy Adamson591d71c2009-04-01 09:22:47 -04005369#if defined(CONFIG_NFS_V4_1)
5370struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5371 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5372 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5373 .recover_open = nfs4_open_expired,
5374 .recover_lock = nfs4_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05005375 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005376 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04005377};
5378#endif /* CONFIG_NFS_V4_1 */
5379
Benny Halevy29fba382009-04-01 09:22:44 -04005380struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5381 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04005382 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005383 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04005384};
5385
5386#if defined(CONFIG_NFS_V4_1)
5387struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5388 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04005389 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005390 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04005391};
5392#endif
5393
Trond Myklebust97dc1352010-06-16 09:52:26 -04005394static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
5395 .minor_version = 0,
5396 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04005397 .validate_stateid = nfs4_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005398 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
5399 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
5400 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005401};
5402
5403#if defined(CONFIG_NFS_V4_1)
5404static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
5405 .minor_version = 1,
5406 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04005407 .validate_stateid = nfs41_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005408 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
5409 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
5410 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005411};
5412#endif
5413
Trond Myklebust97dc1352010-06-16 09:52:26 -04005414const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
5415 [0] = &nfs_v4_0_minor_ops,
5416#if defined(CONFIG_NFS_V4_1)
5417 [1] = &nfs_v4_1_minor_ops,
5418#endif
5419};
5420
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005421static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005422 .permission = nfs_permission,
5423 .getattr = nfs_getattr,
5424 .setattr = nfs_setattr,
5425 .getxattr = nfs4_getxattr,
5426 .setxattr = nfs4_setxattr,
5427 .listxattr = nfs4_listxattr,
5428};
5429
David Howells509de812006-08-22 20:06:11 -04005430const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 .version = 4, /* protocol version */
5432 .dentry_ops = &nfs4_dentry_operations,
5433 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005434 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435 .getroot = nfs4_proc_get_root,
5436 .getattr = nfs4_proc_getattr,
5437 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04005438 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 .lookup = nfs4_proc_lookup,
5440 .access = nfs4_proc_access,
5441 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 .create = nfs4_proc_create,
5443 .remove = nfs4_proc_remove,
5444 .unlink_setup = nfs4_proc_unlink_setup,
5445 .unlink_done = nfs4_proc_unlink_done,
5446 .rename = nfs4_proc_rename,
5447 .link = nfs4_proc_link,
5448 .symlink = nfs4_proc_symlink,
5449 .mkdir = nfs4_proc_mkdir,
5450 .rmdir = nfs4_proc_remove,
5451 .readdir = nfs4_proc_readdir,
5452 .mknod = nfs4_proc_mknod,
5453 .statfs = nfs4_proc_statfs,
5454 .fsinfo = nfs4_proc_fsinfo,
5455 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04005456 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 .decode_dirent = nfs4_decode_dirent,
5458 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05005459 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005461 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005463 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005465 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04005466 .close_context = nfs4_close_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467};
5468
5469/*
5470 * Local variables:
5471 * c-basic-offset: 8
5472 * End:
5473 */