blob: d117a7befd466833ff7c6561ed7399584c5c34c0 [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>
42#include <linux/sunrpc/clnt.h>
43#include <linux/nfs.h>
44#include <linux/nfs4.h>
45#include <linux/nfs_fs.h>
46#include <linux/nfs_page.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070048#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040049#include <linux/module.h>
Andy Adamson5a0ffe52009-04-01 09:23:18 -040050#include <linux/sunrpc/bc_xprt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Trond Myklebust4ce79712005-06-22 17:16:21 +000052#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050054#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050055#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040056#include "callback.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58#define NFSDBG_FACILITY NFSDBG_PROC
59
Trond Myklebust2066fe82006-09-15 08:30:46 -040060#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#define NFS4_POLL_RETRY_MAX (15*HZ)
62
Trond Myklebusta78cb572009-08-09 15:06:19 -040063#define NFS4_MAX_LOOP_ON_RECOVER (10)
64
Trond Myklebustcdd4e682006-01-03 09:55:12 +010065struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010066static int _nfs4_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050068static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Trond Myklebust99367812007-07-17 21:52:41 -040069static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
70static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050073static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Trond Myklebust52567b02009-10-23 14:46:42 -040075 if (err >= -1000)
76 return err;
77 switch (err) {
78 case -NFS4ERR_RESOURCE:
79 return -EREMOTEIO;
80 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -070082 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -040083 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 }
Trond Myklebust52567b02009-10-23 14:46:42 -040085 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87
88/*
89 * This is our standard bitmap for GETATTR requests.
90 */
91const u32 nfs4_fattr_bitmap[2] = {
92 FATTR4_WORD0_TYPE
93 | FATTR4_WORD0_CHANGE
94 | FATTR4_WORD0_SIZE
95 | FATTR4_WORD0_FSID
96 | FATTR4_WORD0_FILEID,
97 FATTR4_WORD1_MODE
98 | FATTR4_WORD1_NUMLINKS
99 | FATTR4_WORD1_OWNER
100 | FATTR4_WORD1_OWNER_GROUP
101 | FATTR4_WORD1_RAWDEV
102 | FATTR4_WORD1_SPACE_USED
103 | FATTR4_WORD1_TIME_ACCESS
104 | FATTR4_WORD1_TIME_METADATA
105 | FATTR4_WORD1_TIME_MODIFY
106};
107
108const u32 nfs4_statfs_bitmap[2] = {
109 FATTR4_WORD0_FILES_AVAIL
110 | FATTR4_WORD0_FILES_FREE
111 | FATTR4_WORD0_FILES_TOTAL,
112 FATTR4_WORD1_SPACE_AVAIL
113 | FATTR4_WORD1_SPACE_FREE
114 | FATTR4_WORD1_SPACE_TOTAL
115};
116
Trond Myklebust4ce79712005-06-22 17:16:21 +0000117const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 FATTR4_WORD0_MAXLINK
119 | FATTR4_WORD0_MAXNAME,
120 0
121};
122
123const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
124 | FATTR4_WORD0_MAXREAD
125 | FATTR4_WORD0_MAXWRITE
126 | FATTR4_WORD0_LEASE_TIME,
127 0
128};
129
Manoj Naik830b8e32006-06-09 09:34:25 -0400130const u32 nfs4_fs_locations_bitmap[2] = {
131 FATTR4_WORD0_TYPE
132 | FATTR4_WORD0_CHANGE
133 | FATTR4_WORD0_SIZE
134 | FATTR4_WORD0_FSID
135 | FATTR4_WORD0_FILEID
136 | FATTR4_WORD0_FS_LOCATIONS,
137 FATTR4_WORD1_MODE
138 | FATTR4_WORD1_NUMLINKS
139 | FATTR4_WORD1_OWNER
140 | FATTR4_WORD1_OWNER_GROUP
141 | FATTR4_WORD1_RAWDEV
142 | FATTR4_WORD1_SPACE_USED
143 | FATTR4_WORD1_TIME_ACCESS
144 | FATTR4_WORD1_TIME_METADATA
145 | FATTR4_WORD1_TIME_MODIFY
146 | FATTR4_WORD1_MOUNTED_ON_FILEID
147};
148
Al Virobc4785c2006-10-19 23:28:51 -0700149static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 struct nfs4_readdir_arg *readdir)
151{
Al Viro0dbb4c62006-10-19 23:28:49 -0700152 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 BUG_ON(readdir->count < 80);
155 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000156 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
158 return;
159 }
160
161 readdir->cookie = 0;
162 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
163 if (cookie == 2)
164 return;
165
166 /*
167 * NFSv4 servers do not return entries for '.' and '..'
168 * Therefore, we fake these entries here. We let '.'
169 * have cookie 0 and '..' have cookie 1. Note that
170 * when talking to the server, we always send cookie 0
171 * instead of 1 or 2.
172 */
Al Viro0dbb4c62006-10-19 23:28:49 -0700173 start = p = kmap_atomic(*readdir->pages, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 if (cookie == 0) {
176 *p++ = xdr_one; /* next */
177 *p++ = xdr_zero; /* cookie, first word */
178 *p++ = xdr_one; /* cookie, second word */
179 *p++ = xdr_one; /* entry len */
180 memcpy(p, ".\0\0\0", 4); /* entry */
181 p++;
182 *p++ = xdr_one; /* bitmap length */
183 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
184 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400185 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 }
187
188 *p++ = xdr_one; /* next */
189 *p++ = xdr_zero; /* cookie, first word */
190 *p++ = xdr_two; /* cookie, second word */
191 *p++ = xdr_two; /* entry len */
192 memcpy(p, "..\0\0", 4); /* entry */
193 p++;
194 *p++ = xdr_one; /* bitmap length */
195 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
196 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400197 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 readdir->pgbase = (char *)p - (char *)start;
200 readdir->count -= readdir->pgbase;
201 kunmap_atomic(start, KM_USER0);
202}
203
Trond Myklebust65de8722008-12-23 15:21:44 -0500204static int nfs4_wait_clnt_recover(struct nfs_client *clp)
205{
206 int res;
207
208 might_sleep();
209
Trond Myklebuste005e802008-12-23 15:21:48 -0500210 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400211 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500212 return res;
213}
214
215static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
216{
217 int res = 0;
218
219 might_sleep();
220
221 if (*timeout <= 0)
222 *timeout = NFS4_POLL_RETRY_MIN;
223 if (*timeout > NFS4_POLL_RETRY_MAX)
224 *timeout = NFS4_POLL_RETRY_MAX;
225 schedule_timeout_killable(*timeout);
226 if (fatal_signal_pending(current))
227 res = -ERESTARTSYS;
228 *timeout <<= 1;
229 return res;
230}
231
232/* This is the error handling routine for processes that are allowed
233 * to sleep.
234 */
235static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
236{
237 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500238 struct nfs4_state *state = exception->state;
Trond Myklebust65de8722008-12-23 15:21:44 -0500239 int ret = errorcode;
240
241 exception->retry = 0;
242 switch(errorcode) {
243 case 0:
244 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500245 case -NFS4ERR_ADMIN_REVOKED:
246 case -NFS4ERR_BAD_STATEID:
247 case -NFS4ERR_OPENMODE:
248 if (state == NULL)
249 break;
250 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500251 case -NFS4ERR_STALE_CLIENTID:
252 case -NFS4ERR_STALE_STATEID:
253 case -NFS4ERR_EXPIRED:
254 nfs4_schedule_state_recovery(clp);
255 ret = nfs4_wait_clnt_recover(clp);
256 if (ret == 0)
257 exception->retry = 1;
Andy Adamson4745e312009-04-01 09:22:42 -0400258#if !defined(CONFIG_NFS_V4_1)
Trond Myklebust65de8722008-12-23 15:21:44 -0500259 break;
Andy Adamson4745e312009-04-01 09:22:42 -0400260#else /* !defined(CONFIG_NFS_V4_1) */
261 if (!nfs4_has_session(server->nfs_client))
262 break;
263 /* FALLTHROUGH */
264 case -NFS4ERR_BADSESSION:
265 case -NFS4ERR_BADSLOT:
266 case -NFS4ERR_BAD_HIGH_SLOT:
267 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
268 case -NFS4ERR_DEADSESSION:
269 case -NFS4ERR_SEQ_FALSE_RETRY:
270 case -NFS4ERR_SEQ_MISORDERED:
271 dprintk("%s ERROR: %d Reset session\n", __func__,
272 errorcode);
Andy Adamson0b9e2d42009-12-04 16:02:14 -0500273 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -0400274 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500275 break;
Andy Adamson4745e312009-04-01 09:22:42 -0400276#endif /* !defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500277 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500278 if (exception->timeout > HZ) {
279 /* We have retried a decent amount, time to
280 * fail
281 */
282 ret = -EBUSY;
283 break;
284 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500285 case -NFS4ERR_GRACE:
286 case -NFS4ERR_DELAY:
287 ret = nfs4_delay(server->client, &exception->timeout);
288 if (ret != 0)
289 break;
290 case -NFS4ERR_OLD_STATEID:
291 exception->retry = 1;
292 }
293 /* We failed to handle the error */
294 return nfs4_map_errors(ret);
295}
296
297
Trond Myklebust26e976a2006-01-03 09:55:21 +0100298static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
David Howells7539bba2006-08-22 20:06:09 -0400300 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 spin_lock(&clp->cl_lock);
302 if (time_before(clp->cl_last_renewal,timestamp))
303 clp->cl_last_renewal = timestamp;
304 spin_unlock(&clp->cl_lock);
305}
306
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400307#if defined(CONFIG_NFS_V4_1)
308
Benny Halevy510b8172009-04-01 09:22:14 -0400309/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400310 * nfs4_free_slot - free a slot and efficiently update slot table.
311 *
312 * freeing a slot is trivially done by clearing its respective bit
313 * in the bitmap.
314 * If the freed slotid equals highest_used_slotid we want to update it
315 * so that the server would be able to size down the slot table if needed,
316 * otherwise we know that the highest_used_slotid is still in use.
317 * When updating highest_used_slotid there may be "holes" in the bitmap
318 * so we need to scan down from highest_used_slotid to 0 looking for the now
319 * highest slotid in use.
320 * If none found, highest_used_slotid is set to -1.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500321 *
322 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400323 */
324static void
325nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid)
326{
327 int slotid = free_slotid;
328
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400329 /* clear used bit in bitmap */
330 __clear_bit(slotid, tbl->used_slots);
331
332 /* update highest_used_slotid when it is freed */
333 if (slotid == tbl->highest_used_slotid) {
334 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500335 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400336 tbl->highest_used_slotid = slotid;
337 else
338 tbl->highest_used_slotid = -1;
339 }
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400340 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
341 free_slotid, tbl->highest_used_slotid);
342}
343
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800344/*
345 * Signal state manager thread if session is drained
346 */
347static void nfs41_check_drain_session_complete(struct nfs4_session *ses)
348{
349 if (!test_bit(NFS4CLNT_SESSION_DRAINING, &ses->clp->cl_state)) {
350 rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
351 return;
352 }
353
354 if (ses->fc_slot_table.highest_used_slotid != -1)
355 return;
356
357 dprintk("%s COMPLETE: Session Drained\n", __func__);
358 complete(&ses->complete);
359}
360
Trond Myklebustd61e6122009-12-05 19:32:19 -0500361static void nfs41_sequence_free_slot(const struct nfs_client *clp,
Andy Adamson13615872009-04-01 09:22:17 -0400362 struct nfs4_sequence_res *res)
363{
364 struct nfs4_slot_table *tbl;
365
Andy Adamson13615872009-04-01 09:22:17 -0400366 tbl = &clp->cl_session->fc_slot_table;
367 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE) {
Andy Adamson13615872009-04-01 09:22:17 -0400368 /* just wake up the next guy waiting since
369 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500370 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500371 return;
Andy Adamson13615872009-04-01 09:22:17 -0400372 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500373
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500374 spin_lock(&tbl->slot_tbl_lock);
375 nfs4_free_slot(tbl, res->sr_slotid);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800376 nfs41_check_drain_session_complete(clp->cl_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500377 spin_unlock(&tbl->slot_tbl_lock);
378 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Andy Adamson13615872009-04-01 09:22:17 -0400379}
380
Andy Adamsonb0df8062009-04-01 09:22:18 -0400381static void nfs41_sequence_done(struct nfs_client *clp,
382 struct nfs4_sequence_res *res,
383 int rpc_status)
384{
385 unsigned long timestamp;
386 struct nfs4_slot_table *tbl;
387 struct nfs4_slot *slot;
388
389 /*
390 * sr_status remains 1 if an RPC level error occurred. The server
391 * may or may not have processed the sequence operation..
392 * Proceed as if the server received and processed the sequence
393 * operation.
394 */
395 if (res->sr_status == 1)
396 res->sr_status = NFS_OK;
397
398 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
399 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE)
400 goto out;
401
Andy Adamson691daf32009-12-04 15:55:39 -0500402 /* Check the SEQUENCE operation status */
Andy Adamsonb0df8062009-04-01 09:22:18 -0400403 if (res->sr_status == 0) {
Andy Adamson691daf32009-12-04 15:55:39 -0500404 tbl = &clp->cl_session->fc_slot_table;
405 slot = tbl->slots + res->sr_slotid;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400406 /* Update the slot's sequence and clientid lease timer */
407 ++slot->seq_nr;
408 timestamp = res->sr_renewal_time;
409 spin_lock(&clp->cl_lock);
410 if (time_before(clp->cl_last_renewal, timestamp))
411 clp->cl_last_renewal = timestamp;
412 spin_unlock(&clp->cl_lock);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500413 /* Check sequence flags */
414 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400415 }
416out:
417 /* The session may be reset by one of the error handlers. */
418 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
419 nfs41_sequence_free_slot(clp, res);
420}
421
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400422/*
Benny Halevy510b8172009-04-01 09:22:14 -0400423 * nfs4_find_slot - efficiently look for a free slot
424 *
425 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
426 * If found, we mark the slot as used, update the highest_used_slotid,
427 * and respectively set up the sequence operation args.
428 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400429 *
430 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400431 */
432static u8
433nfs4_find_slot(struct nfs4_slot_table *tbl, struct rpc_task *task)
434{
435 int slotid;
436 u8 ret_id = NFS4_MAX_SLOT_TABLE;
437 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
438
Benny Halevy510b8172009-04-01 09:22:14 -0400439 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
440 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
441 tbl->max_slots);
442 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
443 if (slotid >= tbl->max_slots)
444 goto out;
445 __set_bit(slotid, tbl->used_slots);
446 if (slotid > tbl->highest_used_slotid)
447 tbl->highest_used_slotid = slotid;
448 ret_id = slotid;
449out:
450 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
451 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400452 return ret_id;
453}
454
Andy Adamsonce5039c2009-04-01 09:22:13 -0400455static int nfs41_setup_sequence(struct nfs4_session *session,
456 struct nfs4_sequence_args *args,
457 struct nfs4_sequence_res *res,
458 int cache_reply,
459 struct rpc_task *task)
460{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400461 struct nfs4_slot *slot;
462 struct nfs4_slot_table *tbl;
463 u8 slotid;
464
465 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400466 /* slot already allocated? */
467 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
468 return 0;
469
470 memset(res, 0, sizeof(*res));
471 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400472 tbl = &session->fc_slot_table;
473
474 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson0b9e2d42009-12-04 16:02:14 -0500475 if (test_bit(NFS4CLNT_SESSION_DRAINING, &session->clp->cl_state)) {
Andy Adamsonea028ac2009-12-04 15:55:38 -0500476 /*
477 * The state manager will wait until the slot table is empty.
478 * Schedule the reset thread
479 */
Andy Adamson05f0d232009-12-04 15:55:37 -0500480 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400481 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500482 dprintk("%s Schedule Session Reset\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500483 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400484 }
485
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400486 slotid = nfs4_find_slot(tbl, task);
487 if (slotid == NFS4_MAX_SLOT_TABLE) {
488 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
489 spin_unlock(&tbl->slot_tbl_lock);
490 dprintk("<-- %s: no free slots\n", __func__);
491 return -EAGAIN;
492 }
493 spin_unlock(&tbl->slot_tbl_lock);
494
495 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400496 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400497 args->sa_slotid = slotid;
498 args->sa_cache_this = cache_reply;
499
500 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
501
Benny Halevy99fe60d2009-04-01 09:22:29 -0400502 res->sr_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400503 res->sr_slotid = slotid;
504 res->sr_renewal_time = jiffies;
505 /*
506 * sr_status is only set in decode_sequence, and so will remain
507 * set to 1 if an rpc level failure occurs.
508 */
509 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400510 return 0;
511}
512
513int nfs4_setup_sequence(struct nfs_client *clp,
514 struct nfs4_sequence_args *args,
515 struct nfs4_sequence_res *res,
516 int cache_reply,
517 struct rpc_task *task)
518{
519 int ret = 0;
520
521 dprintk("--> %s clp %p session %p sr_slotid %d\n",
522 __func__, clp, clp->cl_session, res->sr_slotid);
523
524 if (!nfs4_has_session(clp))
525 goto out;
526 ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply,
527 task);
Andy Adamson2628edd2009-12-04 15:55:35 -0500528 if (ret && ret != -EAGAIN) {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400529 /* terminate rpc task */
530 task->tk_status = ret;
531 task->tk_action = NULL;
532 }
533out:
534 dprintk("<-- %s status=%d\n", __func__, ret);
535 return ret;
536}
537
538struct nfs41_call_sync_data {
539 struct nfs_client *clp;
540 struct nfs4_sequence_args *seq_args;
541 struct nfs4_sequence_res *seq_res;
542 int cache_reply;
543};
544
545static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
546{
547 struct nfs41_call_sync_data *data = calldata;
548
549 dprintk("--> %s data->clp->cl_session %p\n", __func__,
550 data->clp->cl_session);
551 if (nfs4_setup_sequence(data->clp, data->seq_args,
552 data->seq_res, data->cache_reply, task))
553 return;
554 rpc_call_start(task);
555}
556
Andy Adamson69ab40c2009-04-01 09:22:19 -0400557static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
558{
559 struct nfs41_call_sync_data *data = calldata;
560
561 nfs41_sequence_done(data->clp, data->seq_res, task->tk_status);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400562}
563
Andy Adamsonce5039c2009-04-01 09:22:13 -0400564struct rpc_call_ops nfs41_call_sync_ops = {
565 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400566 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400567};
568
569static int nfs4_call_sync_sequence(struct nfs_client *clp,
570 struct rpc_clnt *clnt,
571 struct rpc_message *msg,
572 struct nfs4_sequence_args *args,
573 struct nfs4_sequence_res *res,
574 int cache_reply)
575{
576 int ret;
577 struct rpc_task *task;
578 struct nfs41_call_sync_data data = {
579 .clp = clp,
580 .seq_args = args,
581 .seq_res = res,
582 .cache_reply = cache_reply,
583 };
584 struct rpc_task_setup task_setup = {
585 .rpc_client = clnt,
586 .rpc_message = msg,
587 .callback_ops = &nfs41_call_sync_ops,
588 .callback_data = &data
589 };
590
591 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
592 task = rpc_run_task(&task_setup);
593 if (IS_ERR(task))
594 ret = PTR_ERR(task);
595 else {
596 ret = task->tk_status;
597 rpc_put_task(task);
598 }
599 return ret;
600}
601
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400602int _nfs4_call_sync_session(struct nfs_server *server,
603 struct rpc_message *msg,
604 struct nfs4_sequence_args *args,
605 struct nfs4_sequence_res *res,
606 int cache_reply)
607{
Andy Adamsonce5039c2009-04-01 09:22:13 -0400608 return nfs4_call_sync_sequence(server->nfs_client, server->client,
609 msg, args, res, cache_reply);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400610}
611
612#endif /* CONFIG_NFS_V4_1 */
613
614int _nfs4_call_sync(struct nfs_server *server,
615 struct rpc_message *msg,
616 struct nfs4_sequence_args *args,
617 struct nfs4_sequence_res *res,
618 int cache_reply)
619{
Benny Halevyf3752972009-04-01 09:22:04 -0400620 args->sa_session = res->sr_session = NULL;
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400621 return rpc_call_sync(server->client, msg, 0);
622}
623
624#define nfs4_call_sync(server, msg, args, res, cache_reply) \
625 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
626 &(res)->seq_res, (cache_reply))
627
Andy Adamsonb0df8062009-04-01 09:22:18 -0400628static void nfs4_sequence_done(const struct nfs_server *server,
629 struct nfs4_sequence_res *res, int rpc_status)
630{
631#ifdef CONFIG_NFS_V4_1
632 if (nfs4_has_session(server->nfs_client))
633 nfs41_sequence_done(server->nfs_client, res, rpc_status);
634#endif /* CONFIG_NFS_V4_1 */
635}
636
Trond Myklebust38478b22006-05-25 01:40:57 -0400637static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Trond Myklebust38478b22006-05-25 01:40:57 -0400639 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Trond Myklebust38478b22006-05-25 01:40:57 -0400641 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400642 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
643 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400644 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400645 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400646 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100649struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400650 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100651 struct nfs_openargs o_arg;
652 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100653 struct nfs_open_confirmargs c_arg;
654 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100655 struct nfs_fattr f_attr;
656 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400657 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100658 struct dentry *dir;
659 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400660 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100661 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100662 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400663 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100664 int rpc_status;
665 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100666};
667
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400668
669static void nfs4_init_opendata_res(struct nfs4_opendata *p)
670{
671 p->o_res.f_attr = &p->f_attr;
672 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400673 p->o_res.seqid = p->o_arg.seqid;
674 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400675 p->o_res.server = p->o_arg.server;
676 nfs_fattr_init(&p->f_attr);
677 nfs_fattr_init(&p->dir_attr);
Benny Halevy578e4582009-06-18 22:01:23 -0400678 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400679}
680
Trond Myklebustad389da2007-06-05 12:30:00 -0400681static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500682 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100683 const struct iattr *attrs)
684{
Trond Myklebustad389da2007-06-05 12:30:00 -0400685 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100686 struct inode *dir = parent->d_inode;
687 struct nfs_server *server = NFS_SERVER(dir);
688 struct nfs4_opendata *p;
689
690 p = kzalloc(sizeof(*p), GFP_KERNEL);
691 if (p == NULL)
692 goto err;
693 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
694 if (p->o_arg.seqid == NULL)
695 goto err_free;
Trond Myklebustad389da2007-06-05 12:30:00 -0400696 p->path.mnt = mntget(path->mnt);
697 p->path.dentry = dget(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100698 p->dir = parent;
699 p->owner = sp;
700 atomic_inc(&sp->so_count);
701 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500702 p->o_arg.open_flags = flags;
703 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400704 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400705 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400706 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100707 p->o_arg.server = server;
708 p->o_arg.bitmask = server->attr_bitmask;
709 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100710 if (flags & O_EXCL) {
Alexandros Batsakis4882ef72009-12-05 13:30:21 -0500711 if (nfs4_has_persistent_session(server->nfs_client)) {
712 /* GUARDED */
713 p->o_arg.u.attrs = &p->attrs;
714 memcpy(&p->attrs, attrs, sizeof(p->attrs));
715 } else { /* EXCLUSIVE4_1 */
716 u32 *s = (u32 *) p->o_arg.u.verifier.data;
717 s[0] = jiffies;
718 s[1] = current->pid;
719 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100720 } else if (flags & O_CREAT) {
721 p->o_arg.u.attrs = &p->attrs;
722 memcpy(&p->attrs, attrs, sizeof(p->attrs));
723 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100724 p->c_arg.fh = &p->o_res.fh;
725 p->c_arg.stateid = &p->o_res.stateid;
726 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400727 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400728 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100729 return p;
730err_free:
731 kfree(p);
732err:
733 dput(parent);
734 return NULL;
735}
736
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400737static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100738{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400739 struct nfs4_opendata *p = container_of(kref,
740 struct nfs4_opendata, kref);
741
742 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400743 if (p->state != NULL)
744 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400745 nfs4_put_state_owner(p->owner);
746 dput(p->dir);
Jan Blunck31f31db12008-05-02 13:42:45 -0700747 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400748 kfree(p);
749}
750
751static void nfs4_opendata_put(struct nfs4_opendata *p)
752{
753 if (p != NULL)
754 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100755}
756
Trond Myklebust06f814a2006-01-03 09:55:07 +0100757static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
758{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100759 int ret;
760
Trond Myklebust06f814a2006-01-03 09:55:07 +0100761 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100762 return ret;
763}
764
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500765static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400766{
767 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500768
769 if (open_mode & O_EXCL)
770 goto out;
771 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400772 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500773 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
774 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400775 break;
776 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500777 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
778 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400779 break;
780 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500781 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
782 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400783 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500784out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400785 return ret;
786}
787
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500788static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400789{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500790 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400791 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500792 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400793 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500794 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400795 return 1;
796}
797
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500798static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100799{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500800 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100801 case FMODE_WRITE:
802 state->n_wronly++;
803 break;
804 case FMODE_READ:
805 state->n_rdonly++;
806 break;
807 case FMODE_READ|FMODE_WRITE:
808 state->n_rdwr++;
809 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500810 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100811}
812
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500813static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400814{
815 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
816 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
817 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500818 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400819 case FMODE_READ:
820 set_bit(NFS_O_RDONLY_STATE, &state->flags);
821 break;
822 case FMODE_WRITE:
823 set_bit(NFS_O_WRONLY_STATE, &state->flags);
824 break;
825 case FMODE_READ|FMODE_WRITE:
826 set_bit(NFS_O_RDWR_STATE, &state->flags);
827 }
828}
829
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500830static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400831{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400832 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500833 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400834 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400835}
836
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500837static 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 -0700838{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400839 /*
840 * Protect the call to nfs4_state_set_mode_locked and
841 * serialise the stateid update
842 */
843 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400844 if (deleg_stateid != NULL) {
845 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
846 set_bit(NFS_DELEGATED_STATE, &state->flags);
847 }
848 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500849 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400850 write_sequnlock(&state->seqlock);
851 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500852 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700853 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854}
855
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500856static 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 -0500857{
858 struct nfs_inode *nfsi = NFS_I(state->inode);
859 struct nfs_delegation *deleg_cur;
860 int ret = 0;
861
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500862 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500863
864 rcu_read_lock();
865 deleg_cur = rcu_dereference(nfsi->delegation);
866 if (deleg_cur == NULL)
867 goto no_delegation;
868
869 spin_lock(&deleg_cur->lock);
870 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500871 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500872 goto no_delegation_unlock;
873
874 if (delegation == NULL)
875 delegation = &deleg_cur->stateid;
876 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
877 goto no_delegation_unlock;
878
Trond Myklebustb7391f42008-12-23 15:21:52 -0500879 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500880 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500881 ret = 1;
882no_delegation_unlock:
883 spin_unlock(&deleg_cur->lock);
884no_delegation:
885 rcu_read_unlock();
886
887 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500888 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500889 ret = 1;
890 }
891
892 return ret;
893}
894
895
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500896static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400897{
898 struct nfs_delegation *delegation;
899
900 rcu_read_lock();
901 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500902 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400903 rcu_read_unlock();
904 return;
905 }
906 rcu_read_unlock();
907 nfs_inode_return_delegation(inode);
908}
909
Trond Myklebust6ee41262007-07-08 14:11:36 -0400910static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400911{
912 struct nfs4_state *state = opendata->state;
913 struct nfs_inode *nfsi = NFS_I(state->inode);
914 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500915 int open_mode = opendata->o_arg.open_flags & O_EXCL;
916 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400917 nfs4_stateid stateid;
918 int ret = -EAGAIN;
919
Trond Myklebustaac00a82007-07-05 19:02:21 -0400920 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500921 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400922 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500923 if (can_open_cached(state, fmode, open_mode)) {
924 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400925 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400926 goto out_return_state;
927 }
928 spin_unlock(&state->owner->so_lock);
929 }
Trond Myklebust34310432008-12-23 15:21:38 -0500930 rcu_read_lock();
931 delegation = rcu_dereference(nfsi->delegation);
932 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500933 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -0500934 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -0400935 break;
Trond Myklebust34310432008-12-23 15:21:38 -0500936 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400937 /* Save the delegation */
938 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
939 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -0400940 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400941 if (ret != 0)
942 goto out;
943 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -0500944
945 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500946 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -0500947 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400948 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400949out:
950 return ERR_PTR(ret);
951out_return_state:
952 atomic_inc(&state->count);
953 return state;
954}
955
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100956static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
957{
958 struct inode *inode;
959 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -0400960 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400961 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100962
Trond Myklebustaac00a82007-07-05 19:02:21 -0400963 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400964 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400965 goto out;
966 }
967
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400968 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100969 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400970 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100971 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400972 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -0500973 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400974 goto err;
975 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100976 state = nfs4_get_open_state(inode, data->owner);
977 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400978 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400979 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400980 int delegation_flags = 0;
981
Trond Myklebust003707c2007-07-05 18:07:55 -0400982 rcu_read_lock();
983 delegation = rcu_dereference(NFS_I(inode)->delegation);
984 if (delegation)
985 delegation_flags = delegation->flags;
986 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -0500987 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -0400988 nfs_inode_set_delegation(state->inode,
989 data->owner->so_cred,
990 &data->o_res);
991 else
992 nfs_inode_reclaim_delegation(state->inode,
993 data->owner->so_cred,
994 &data->o_res);
995 }
Trond Myklebust34310432008-12-23 15:21:38 -0500996
997 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500998 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100999 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001000out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001001 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001002err_put_inode:
1003 iput(inode);
1004err:
1005 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001006}
1007
Trond Myklebust864472e2006-01-03 09:55:15 +01001008static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1009{
1010 struct nfs_inode *nfsi = NFS_I(state->inode);
1011 struct nfs_open_context *ctx;
1012
1013 spin_lock(&state->inode->i_lock);
1014 list_for_each_entry(ctx, &nfsi->open_files, list) {
1015 if (ctx->state != state)
1016 continue;
1017 get_nfs_open_context(ctx);
1018 spin_unlock(&state->inode->i_lock);
1019 return ctx;
1020 }
1021 spin_unlock(&state->inode->i_lock);
1022 return ERR_PTR(-ENOENT);
1023}
1024
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001025static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1026{
1027 struct nfs4_opendata *opendata;
1028
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001029 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001030 if (opendata == NULL)
1031 return ERR_PTR(-ENOMEM);
1032 opendata->state = state;
1033 atomic_inc(&state->count);
1034 return opendata;
1035}
1036
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001037static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001038{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001039 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001040 int ret;
1041
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001042 opendata->o_arg.open_flags = 0;
1043 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001044 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1045 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1046 nfs4_init_opendata_res(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001047 ret = _nfs4_proc_open(opendata);
1048 if (ret != 0)
1049 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001050 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001051 if (IS_ERR(newstate))
1052 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001053 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001054 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001055 return 0;
1056}
1057
1058static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1059{
Trond Myklebust864472e2006-01-03 09:55:15 +01001060 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001061 int ret;
1062
1063 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001064 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001065 smp_rmb();
1066 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001067 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001068 if (ret != 0)
1069 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001070 if (newstate != state)
1071 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001072 }
1073 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001074 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001075 if (ret != 0)
1076 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001077 if (newstate != state)
1078 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001079 }
1080 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001081 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001082 if (ret != 0)
1083 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001084 if (newstate != state)
1085 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001086 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001087 /*
1088 * We may have performed cached opens for all three recoveries.
1089 * Check if we need to update the current stateid.
1090 */
1091 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1092 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001093 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001094 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1095 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001096 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001097 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001098 return 0;
1099}
1100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101/*
1102 * OPEN_RECLAIM:
1103 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001105static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001107 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001108 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001109 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 int status;
1111
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001112 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1113 if (IS_ERR(opendata))
1114 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001115 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1116 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001117 rcu_read_lock();
1118 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001119 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001120 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001121 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001122 opendata->o_arg.u.delegation_type = delegation_type;
1123 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001124 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 return status;
1126}
1127
Trond Myklebust539cd032007-06-05 11:46:42 -04001128static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129{
1130 struct nfs_server *server = NFS_SERVER(state->inode);
1131 struct nfs4_exception exception = { };
1132 int err;
1133 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001134 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001135 if (err != -NFS4ERR_DELAY)
1136 break;
1137 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 } while (exception.retry);
1139 return err;
1140}
1141
Trond Myklebust864472e2006-01-03 09:55:15 +01001142static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1143{
1144 struct nfs_open_context *ctx;
1145 int ret;
1146
1147 ctx = nfs4_state_find_open_context(state);
1148 if (IS_ERR(ctx))
1149 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001150 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001151 put_nfs_open_context(ctx);
1152 return ret;
1153}
1154
Trond Myklebust13437e12007-07-06 15:10:43 -04001155static 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 -07001156{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001157 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001158 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001160 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1161 if (IS_ERR(opendata))
1162 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001163 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001164 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001165 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001166 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001167 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001168 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169}
1170
Trond Myklebust13437e12007-07-06 15:10:43 -04001171int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
1173 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001174 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 int err;
1176 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001177 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 switch (err) {
1179 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001180 case -ENOENT:
1181 case -ESTALE:
1182 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001183 case -NFS4ERR_BADSESSION:
1184 case -NFS4ERR_BADSLOT:
1185 case -NFS4ERR_BAD_HIGH_SLOT:
1186 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1187 case -NFS4ERR_DEADSESSION:
1188 nfs4_schedule_state_recovery(
1189 server->nfs_client);
1190 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 case -NFS4ERR_STALE_CLIENTID:
1192 case -NFS4ERR_STALE_STATEID:
1193 case -NFS4ERR_EXPIRED:
1194 /* Don't recall a delegation if it was lost */
David Howells7539bba2006-08-22 20:06:09 -04001195 nfs4_schedule_state_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001196 goto out;
1197 case -ERESTARTSYS:
1198 /*
1199 * The show must go on: exit, but mark the
1200 * stateid as needing recovery.
1201 */
1202 case -NFS4ERR_ADMIN_REVOKED:
1203 case -NFS4ERR_BAD_STATEID:
1204 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
1205 case -ENOMEM:
1206 err = 0;
1207 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 }
1209 err = nfs4_handle_exception(server, err, &exception);
1210 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001211out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 return err;
1213}
1214
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001215static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1216{
1217 struct nfs4_opendata *data = calldata;
1218
1219 data->rpc_status = task->tk_status;
1220 if (RPC_ASSASSINATED(task))
1221 return;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001222 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001223 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1224 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001225 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001226 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001227 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001228 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001229}
1230
1231static void nfs4_open_confirm_release(void *calldata)
1232{
1233 struct nfs4_opendata *data = calldata;
1234 struct nfs4_state *state = NULL;
1235
1236 /* If this request hasn't been cancelled, do nothing */
1237 if (data->cancelled == 0)
1238 goto out_free;
1239 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001240 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001241 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001242 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001243 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001244 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001245out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001246 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001247}
1248
1249static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001250 .rpc_call_done = nfs4_open_confirm_done,
1251 .rpc_release = nfs4_open_confirm_release,
1252};
1253
1254/*
1255 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1256 */
1257static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1258{
1259 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1260 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001261 struct rpc_message msg = {
1262 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1263 .rpc_argp = &data->c_arg,
1264 .rpc_resp = &data->c_res,
1265 .rpc_cred = data->owner->so_cred,
1266 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001267 struct rpc_task_setup task_setup_data = {
1268 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001269 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001270 .callback_ops = &nfs4_open_confirm_ops,
1271 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001272 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001273 .flags = RPC_TASK_ASYNC,
1274 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 int status;
1276
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001277 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001278 data->rpc_done = 0;
1279 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001280 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001281 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001282 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001283 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001284 status = nfs4_wait_for_completion_rpc_task(task);
1285 if (status != 0) {
1286 data->cancelled = 1;
1287 smp_wmb();
1288 } else
1289 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001290 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 return status;
1292}
1293
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001294static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001296 struct nfs4_opendata *data = calldata;
1297 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001298
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001299 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1300 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001301 /*
1302 * Check if we still need to send an OPEN call, or if we can use
1303 * a delegation instead.
1304 */
1305 if (data->state != NULL) {
1306 struct nfs_delegation *delegation;
1307
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001308 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001309 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001310 rcu_read_lock();
1311 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1312 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001313 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001314 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001315 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001316 }
1317 rcu_read_unlock();
1318 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001319 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001320 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001321 data->o_arg.clientid = sp->so_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001322 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001323 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001324 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1325 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001326 data->timestamp = jiffies;
Andy Adamsond8985282009-04-01 09:22:21 -04001327 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
1328 &data->o_arg.seq_args,
1329 &data->o_res.seq_res, 1, task))
1330 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001331 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001332 return;
1333out_no_action:
1334 task->tk_action = NULL;
1335
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001336}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001338static void nfs4_open_done(struct rpc_task *task, void *calldata)
1339{
1340 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001342 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001343
Trond Myklebustd61e6122009-12-05 19:32:19 -05001344 nfs4_sequence_done(data->o_arg.server, &data->o_res.seq_res,
1345 task->tk_status);
Andy Adamsond8985282009-04-01 09:22:21 -04001346
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001347 if (RPC_ASSASSINATED(task))
1348 return;
1349 if (task->tk_status == 0) {
1350 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001351 case S_IFREG:
1352 break;
1353 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001354 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001355 break;
1356 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001357 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001358 break;
1359 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001360 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001361 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001362 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001363 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1364 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001365 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001366 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001367}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001368
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001369static void nfs4_open_release(void *calldata)
1370{
1371 struct nfs4_opendata *data = calldata;
1372 struct nfs4_state *state = NULL;
1373
1374 /* If this request hasn't been cancelled, do nothing */
1375 if (data->cancelled == 0)
1376 goto out_free;
1377 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001378 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001379 goto out_free;
1380 /* In case we need an open_confirm, no cleanup! */
1381 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1382 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001383 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001384 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001385 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001386out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001387 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001388}
1389
1390static const struct rpc_call_ops nfs4_open_ops = {
1391 .rpc_call_prepare = nfs4_open_prepare,
1392 .rpc_call_done = nfs4_open_done,
1393 .rpc_release = nfs4_open_release,
1394};
1395
1396/*
1397 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1398 */
1399static int _nfs4_proc_open(struct nfs4_opendata *data)
1400{
1401 struct inode *dir = data->dir->d_inode;
1402 struct nfs_server *server = NFS_SERVER(dir);
1403 struct nfs_openargs *o_arg = &data->o_arg;
1404 struct nfs_openres *o_res = &data->o_res;
1405 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001406 struct rpc_message msg = {
1407 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1408 .rpc_argp = o_arg,
1409 .rpc_resp = o_res,
1410 .rpc_cred = data->owner->so_cred,
1411 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001412 struct rpc_task_setup task_setup_data = {
1413 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001414 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001415 .callback_ops = &nfs4_open_ops,
1416 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001417 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001418 .flags = RPC_TASK_ASYNC,
1419 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001420 int status;
1421
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001422 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001423 data->rpc_done = 0;
1424 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001425 data->cancelled = 0;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001426 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001427 if (IS_ERR(task))
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001428 return PTR_ERR(task);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001429 status = nfs4_wait_for_completion_rpc_task(task);
1430 if (status != 0) {
1431 data->cancelled = 1;
1432 smp_wmb();
1433 } else
1434 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001435 rpc_put_task(task);
Trond Myklebust3e309912007-07-07 13:19:59 -04001436 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001437 return status;
1438
Trond Myklebust99367812007-07-17 21:52:41 -04001439 if (o_res->fh.size == 0)
1440 _nfs4_proc_lookup(dir, o_arg->name, &o_res->fh, o_res->f_attr);
1441
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001442 if (o_arg->open_flags & O_CREAT) {
1443 update_changeattr(dir, &o_res->cinfo);
1444 nfs_post_op_update_inode(dir, o_res->dir_attr);
1445 } else
1446 nfs_refresh_inode(dir, o_res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001448 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001450 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 }
1452 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001453 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001454 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455}
1456
Trond Myklebust10afec92007-05-14 17:16:04 -04001457static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +01001458{
David Howells7539bba2006-08-22 20:06:09 -04001459 struct nfs_client *clp = server->nfs_client;
Trond Myklebusta78cb572009-08-09 15:06:19 -04001460 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001461 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001462
Trond Myklebusta78cb572009-08-09 15:06:19 -04001463 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001464 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001465 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001466 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001467 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1468 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001469 break;
Trond Myklebust58d97142006-01-03 09:55:24 +01001470 nfs4_schedule_state_recovery(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001471 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001472 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001473 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001474}
1475
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476/*
1477 * OPEN_EXPIRED:
1478 * reclaim state on the server after a network partition.
1479 * Assumes caller holds the appropriate lock
1480 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001481static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001483 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001484 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001486 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1487 if (IS_ERR(opendata))
1488 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001489 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001490 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001491 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001492 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001493 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494}
1495
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001496static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001497{
Trond Myklebust539cd032007-06-05 11:46:42 -04001498 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001499 struct nfs4_exception exception = { };
1500 int err;
1501
1502 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001503 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001504 switch (err) {
1505 default:
1506 goto out;
1507 case -NFS4ERR_GRACE:
1508 case -NFS4ERR_DELAY:
1509 nfs4_handle_exception(server, err, &exception);
1510 err = 0;
1511 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001512 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001513out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001514 return err;
1515}
1516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1518{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001520 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Trond Myklebust864472e2006-01-03 09:55:15 +01001522 ctx = nfs4_state_find_open_context(state);
1523 if (IS_ERR(ctx))
1524 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001525 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001526 put_nfs_open_context(ctx);
1527 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528}
1529
1530/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001531 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1532 * fields corresponding to attributes that were used to store the verifier.
1533 * Make sure we clobber those fields in the later setattr call
1534 */
1535static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1536{
1537 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1538 !(sattr->ia_valid & ATTR_ATIME_SET))
1539 sattr->ia_valid |= ATTR_ATIME;
1540
1541 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1542 !(sattr->ia_valid & ATTR_MTIME_SET))
1543 sattr->ia_valid |= ATTR_MTIME;
1544}
1545
1546/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001547 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001549static 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 -07001550{
1551 struct nfs4_state_owner *sp;
1552 struct nfs4_state *state = NULL;
1553 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001554 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001555 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
1557 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 status = -ENOMEM;
1559 if (!(sp = nfs4_get_state_owner(server, cred))) {
1560 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1561 goto out_err;
1562 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001563 status = nfs4_recover_expired_lease(server);
1564 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001565 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001566 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001567 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001568 status = -ENOMEM;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001569 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001570 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001571 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Trond Myklebustaac00a82007-07-05 19:02:21 -04001573 if (path->dentry->d_inode != NULL)
1574 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1575
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001576 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001578 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001580 if (opendata->o_arg.open_flags & O_EXCL)
1581 nfs4_exclusive_attrset(opendata, sattr);
1582
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001583 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001584 status = PTR_ERR(state);
1585 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001586 goto err_opendata_put;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001587 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 *res = state;
1590 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001591err_opendata_put:
1592 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001593err_put_state_owner:
1594 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 *res = NULL;
1597 return status;
1598}
1599
1600
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001601static 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 -07001602{
1603 struct nfs4_exception exception = { };
1604 struct nfs4_state *res;
1605 int status;
1606
1607 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001608 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 if (status == 0)
1610 break;
1611 /* NOTE: BAD_SEQID means the server and client disagree about the
1612 * book-keeping w.r.t. state-changing operations
1613 * (OPEN/CLOSE/LOCK/LOCKU...)
1614 * It is actually a sign of a bug on the client or on the server.
1615 *
1616 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001617 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 * have unhashed the old state_owner for us, and that we can
1619 * therefore safely retry using a new one. We should still warn
1620 * the user though...
1621 */
1622 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001623 printk(KERN_WARNING "NFS: v4 server %s "
1624 " returned a bad sequence-id error!\n",
1625 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 exception.retry = 1;
1627 continue;
1628 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001629 /*
1630 * BAD_STATEID on OPEN means that the server cancelled our
1631 * state before it received the OPEN_CONFIRM.
1632 * Recover by retrying the request as per the discussion
1633 * on Page 181 of RFC3530.
1634 */
1635 if (status == -NFS4ERR_BAD_STATEID) {
1636 exception.retry = 1;
1637 continue;
1638 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001639 if (status == -EAGAIN) {
1640 /* We must have found a delegation */
1641 exception.retry = 1;
1642 continue;
1643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1645 status, &exception));
1646 } while (exception.retry);
1647 return res;
1648}
1649
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001650static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1651 struct nfs_fattr *fattr, struct iattr *sattr,
1652 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001654 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001656 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 .iap = sattr,
1658 .server = server,
1659 .bitmask = server->attr_bitmask,
1660 };
1661 struct nfs_setattrres res = {
1662 .fattr = fattr,
1663 .server = server,
1664 };
1665 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001666 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1667 .rpc_argp = &arg,
1668 .rpc_resp = &res,
1669 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001671 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001672 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Trond Myklebust0e574af2005-10-27 22:12:38 -04001674 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001676 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1677 /* Use that stateid */
1678 } else if (state != NULL) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001679 nfs4_copy_stateid(&arg.stateid, state, current->files);
1680 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1682
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001683 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001684 if (status == 0 && state != NULL)
1685 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001686 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687}
1688
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001689static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1690 struct nfs_fattr *fattr, struct iattr *sattr,
1691 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001693 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 struct nfs4_exception exception = { };
1695 int err;
1696 do {
1697 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001698 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 &exception);
1700 } while (exception.retry);
1701 return err;
1702}
1703
1704struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001705 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 struct inode *inode;
1707 struct nfs4_state *state;
1708 struct nfs_closeargs arg;
1709 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001710 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001711 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712};
1713
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001714static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001715{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001716 struct nfs4_closedata *calldata = data;
1717 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001718
1719 nfs4_put_open_state(calldata->state);
1720 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001721 nfs4_put_state_owner(sp);
Jan Blunck31f31db12008-05-02 13:42:45 -07001722 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001723 kfree(calldata);
1724}
1725
Trond Myklebust88069f72009-12-08 08:33:16 -05001726static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1727 fmode_t fmode)
1728{
1729 spin_lock(&state->owner->so_lock);
1730 if (!(fmode & FMODE_READ))
1731 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1732 if (!(fmode & FMODE_WRITE))
1733 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1734 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1735 spin_unlock(&state->owner->so_lock);
1736}
1737
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001738static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001740 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 struct nfs_server *server = NFS_SERVER(calldata->inode);
1743
Andy Adamson19ddab02009-04-01 09:22:20 -04001744 nfs4_sequence_done(server, &calldata->res.seq_res, task->tk_status);
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001745 if (RPC_ASSASSINATED(task))
1746 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 /* hmm. we are done with the inode, and in the process of freeing
1748 * the state_owner. we keep this around to process errors
1749 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 switch (task->tk_status) {
1751 case 0:
Trond Myklebust45328c32007-07-26 17:47:34 -04001752 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001753 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001754 nfs4_close_clear_stateid_flags(state,
1755 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 break;
1757 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001758 case -NFS4ERR_OLD_STATEID:
1759 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001761 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001762 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001764 if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05001765 nfs_restart_rpc(task, server->nfs_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 return;
1767 }
1768 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001769 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770}
1771
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001772static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001774 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001775 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001776 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001777
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001778 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001779 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001780
Trond Myklebust88069f72009-12-08 08:33:16 -05001781 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1782 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001783 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001784 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001785 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001786 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001787 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1788 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1789 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001790 }
1791 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001792 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1793 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1794 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001795 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001796 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001797 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05001798
1799 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001800 /* Note: exit _without_ calling nfs4_close_done */
1801 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001802 return;
1803 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001804
1805 if (calldata->arg.fmode == 0)
1806 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
1807
Trond Myklebust516a6af2005-10-27 22:12:41 -04001808 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001809 calldata->timestamp = jiffies;
Andy Adamson19ddab02009-04-01 09:22:20 -04001810 if (nfs4_setup_sequence((NFS_SERVER(calldata->inode))->nfs_client,
1811 &calldata->arg.seq_args, &calldata->res.seq_res,
1812 1, task))
1813 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001814 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815}
1816
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001817static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001818 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001819 .rpc_call_done = nfs4_close_done,
1820 .rpc_release = nfs4_free_closedata,
1821};
1822
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823/*
1824 * It is possible for data to be read/written from a mem-mapped file
1825 * after the sys_close call (which hits the vfs layer as a flush).
1826 * This means that we can't safely call nfsv4 close on a file until
1827 * the inode is cleared. This in turn means that we are not good
1828 * NFSv4 citizens - we do not indicate to the server to update the file's
1829 * share state even when we are done with one of the three share
1830 * stateid's in the inode.
1831 *
1832 * NOTE: Caller must be holding the sp->so_owner semaphore!
1833 */
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001834int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001836 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001838 struct nfs4_state_owner *sp = state->owner;
1839 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001840 struct rpc_message msg = {
1841 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1842 .rpc_cred = state->owner->so_cred,
1843 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001844 struct rpc_task_setup task_setup_data = {
1845 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001846 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001847 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05001848 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001849 .flags = RPC_TASK_ASYNC,
1850 };
Trond Myklebust95121352005-10-18 14:20:12 -07001851 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
Andy Adamson19ddab02009-04-01 09:22:20 -04001853 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001855 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001856 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001858 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04001859 calldata->arg.stateid = &state->open_stateid;
Benny Halevy34dc1ad2009-04-01 09:22:52 -04001860 if (nfs4_has_session(server->nfs_client))
1861 memset(calldata->arg.stateid->data, 0, 4); /* clear seqid */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 /* Serialization for the sequence id */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001863 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001864 if (calldata->arg.seqid == NULL)
1865 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001866 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001867 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001868 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04001869 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001870 calldata->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04001871 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001872 calldata->path.mnt = mntget(path->mnt);
1873 calldata->path.dentry = dget(path->dentry);
Trond Myklebust95121352005-10-18 14:20:12 -07001874
Trond Myklebust5138fde2007-07-14 15:40:01 -04001875 msg.rpc_argp = &calldata->arg,
1876 msg.rpc_resp = &calldata->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001877 task_setup_data.callback_data = calldata;
1878 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001879 if (IS_ERR(task))
1880 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001881 status = 0;
1882 if (wait)
1883 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001884 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001885 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07001886out_free_calldata:
1887 kfree(calldata);
1888out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04001889 nfs4_put_open_state(state);
1890 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07001891 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892}
1893
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001894static 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 -07001895{
1896 struct file *filp;
Trond Myklebust1b45c462007-07-03 13:04:56 -04001897 int ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001898
Trond Myklebust1b45c462007-07-03 13:04:56 -04001899 /* If the open_intent is for execute, we have an extra check to make */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001900 if (fmode & FMODE_EXEC) {
Trond Myklebustaf22f942007-08-10 17:45:10 -04001901 ret = nfs_may_open(state->inode,
Trond Myklebust1b45c462007-07-03 13:04:56 -04001902 state->owner->so_cred,
1903 nd->intent.open.flags);
1904 if (ret < 0)
1905 goto out_close;
1906 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001907 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001908 if (!IS_ERR(filp)) {
1909 struct nfs_open_context *ctx;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04001910 ctx = nfs_file_open_context(filp);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001911 ctx->state = state;
Trond Myklebust95cf9592006-04-18 13:14:06 -04001912 return 0;
1913 }
Trond Myklebust1b45c462007-07-03 13:04:56 -04001914 ret = PTR_ERR(filp);
1915out_close:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001916 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
Trond Myklebust1b45c462007-07-03 13:04:56 -04001917 return ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001918}
1919
1920struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1922{
Trond Myklebustad389da2007-06-05 12:30:00 -04001923 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08001924 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001925 .dentry = dentry,
1926 };
Jan Blunck4ac91372008-02-14 19:34:32 -08001927 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 struct iattr attr;
1929 struct rpc_cred *cred;
1930 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001931 struct dentry *res;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001932 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
1934 if (nd->flags & LOOKUP_CREATE) {
1935 attr.ia_mode = nd->intent.open.create_mode;
1936 attr.ia_valid = ATTR_MODE;
1937 if (!IS_POSIXACL(dir))
Al Viroce3b0f82009-03-29 19:08:22 -04001938 attr.ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 } else {
1940 attr.ia_valid = 0;
1941 BUG_ON(nd->intent.open.flags & O_CREAT);
1942 }
1943
Trond Myklebust98a8e322008-03-12 12:25:28 -04001944 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07001946 return (struct dentry *)cred;
Trond Myklebust565277f2007-10-15 18:17:53 -04001947 parent = dentry->d_parent;
1948 /* Protect against concurrent sillydeletes */
1949 nfs_block_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001950 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001952 if (IS_ERR(state)) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04001953 if (PTR_ERR(state) == -ENOENT) {
Trond Myklebust02a913a2005-10-18 14:20:17 -07001954 d_add(dentry, NULL);
Trond Myklebustd75340c2007-10-01 21:42:01 -04001955 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1956 }
Trond Myklebust565277f2007-10-15 18:17:53 -04001957 nfs_unblock_sillyrename(parent);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001958 return (struct dentry *)state;
1959 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001960 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07001961 if (res != NULL)
Trond Myklebustdeee9362007-08-27 11:33:00 -04001962 path.dentry = res;
Trond Myklebustd75340c2007-10-01 21:42:01 -04001963 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
Trond Myklebust565277f2007-10-15 18:17:53 -04001964 nfs_unblock_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001965 nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001966 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967}
1968
1969int
Trond Myklebust02a913a2005-10-18 14:20:17 -07001970nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971{
Trond Myklebustad389da2007-06-05 12:30:00 -04001972 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08001973 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001974 .dentry = dentry,
1975 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 struct rpc_cred *cred;
1977 struct nfs4_state *state;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001978 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
Trond Myklebust98a8e322008-03-12 12:25:28 -04001980 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 if (IS_ERR(cred))
1982 return PTR_ERR(cred);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001983 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001985 if (IS_ERR(state)) {
1986 switch (PTR_ERR(state)) {
1987 case -EPERM:
1988 case -EACCES:
1989 case -EDQUOT:
1990 case -ENOSPC:
1991 case -EROFS:
1992 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1993 return 1;
Chuck Leverb87c0ad2006-10-19 23:28:42 -07001994 default:
1995 goto out_drop;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001996 }
Trond Myklebust02a913a2005-10-18 14:20:17 -07001997 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001998 if (state->inode == dentry->d_inode) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04001999 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002000 nfs4_intent_set_file(nd, &path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 return 1;
2002 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002003 nfs4_close_sync(&path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002004out_drop:
2005 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 return 0;
2007}
2008
Trond Myklebust1185a552009-12-03 15:54:02 -05002009static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002010{
2011 if (ctx->state == NULL)
2012 return;
2013 if (is_sync)
2014 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2015 else
2016 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2017}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
2019static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2020{
Benny Halevy43652ad2009-04-01 09:21:54 -04002021 struct nfs4_server_caps_arg args = {
2022 .fhandle = fhandle,
2023 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 struct nfs4_server_caps_res res = {};
2025 struct rpc_message msg = {
2026 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002027 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 .rpc_resp = &res,
2029 };
2030 int status;
2031
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002032 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 if (status == 0) {
2034 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002035 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2036 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2037 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2038 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2039 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2041 server->caps |= NFS_CAP_ACLS;
2042 if (res.has_links != 0)
2043 server->caps |= NFS_CAP_HARDLINKS;
2044 if (res.has_symlinks != 0)
2045 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002046 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2047 server->caps |= NFS_CAP_FILEID;
2048 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2049 server->caps |= NFS_CAP_MODE;
2050 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2051 server->caps |= NFS_CAP_NLINK;
2052 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2053 server->caps |= NFS_CAP_OWNER;
2054 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2055 server->caps |= NFS_CAP_OWNER_GROUP;
2056 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2057 server->caps |= NFS_CAP_ATIME;
2058 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2059 server->caps |= NFS_CAP_CTIME;
2060 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2061 server->caps |= NFS_CAP_MTIME;
2062
Trond Myklebusta65318b2009-03-11 14:10:28 -04002063 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2064 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2065 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 server->acl_bitmask = res.acl_bitmask;
2067 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002068
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 return status;
2070}
2071
Trond Myklebust55a97592006-06-09 09:34:19 -04002072int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073{
2074 struct nfs4_exception exception = { };
2075 int err;
2076 do {
2077 err = nfs4_handle_exception(server,
2078 _nfs4_server_capabilities(server, fhandle),
2079 &exception);
2080 } while (exception.retry);
2081 return err;
2082}
2083
2084static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2085 struct nfs_fsinfo *info)
2086{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 struct nfs4_lookup_root_arg args = {
2088 .bitmask = nfs4_fattr_bitmap,
2089 };
2090 struct nfs4_lookup_res res = {
2091 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002092 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 .fh = fhandle,
2094 };
2095 struct rpc_message msg = {
2096 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2097 .rpc_argp = &args,
2098 .rpc_resp = &res,
2099 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002100
Trond Myklebust0e574af2005-10-27 22:12:38 -04002101 nfs_fattr_init(info->fattr);
Trond Myklebustfccba802009-07-21 16:48:07 -04002102 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103}
2104
2105static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2106 struct nfs_fsinfo *info)
2107{
2108 struct nfs4_exception exception = { };
2109 int err;
2110 do {
2111 err = nfs4_handle_exception(server,
2112 _nfs4_lookup_root(server, fhandle, info),
2113 &exception);
2114 } while (exception.retry);
2115 return err;
2116}
2117
David Howells54ceac42006-08-22 20:06:13 -04002118/*
2119 * get the file handle for the "/" directory on the server
2120 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002122 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 int status;
2125
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 if (status == 0)
2128 status = nfs4_server_capabilities(server, fhandle);
2129 if (status == 0)
2130 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002131 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132}
2133
Manoj Naik6b97fd32006-06-09 09:34:29 -04002134/*
2135 * Get locations and (maybe) other attributes of a referral.
2136 * Note that we'll actually follow the referral later when
2137 * we detect fsid mismatch in inode revalidation
2138 */
Trond Myklebust56659e92007-07-17 21:52:39 -04002139static 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 -04002140{
2141 int status = -ENOMEM;
2142 struct page *page = NULL;
2143 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002144
2145 page = alloc_page(GFP_KERNEL);
2146 if (page == NULL)
2147 goto out;
2148 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2149 if (locations == NULL)
2150 goto out;
2151
Trond Myklebustc228fd32007-01-13 02:28:11 -05002152 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002153 if (status != 0)
2154 goto out;
2155 /* Make sure server returned a different fsid for the referral */
2156 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07002157 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 -04002158 status = -EIO;
2159 goto out;
2160 }
2161
2162 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2163 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2164 if (!fattr->mode)
2165 fattr->mode = S_IFDIR;
2166 memset(fhandle, 0, sizeof(struct nfs_fh));
2167out:
2168 if (page)
2169 __free_page(page);
2170 if (locations)
2171 kfree(locations);
2172 return status;
2173}
2174
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2176{
2177 struct nfs4_getattr_arg args = {
2178 .fh = fhandle,
2179 .bitmask = server->attr_bitmask,
2180 };
2181 struct nfs4_getattr_res res = {
2182 .fattr = fattr,
2183 .server = server,
2184 };
2185 struct rpc_message msg = {
2186 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2187 .rpc_argp = &args,
2188 .rpc_resp = &res,
2189 };
2190
Trond Myklebust0e574af2005-10-27 22:12:38 -04002191 nfs_fattr_init(fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002192 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193}
2194
2195static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2196{
2197 struct nfs4_exception exception = { };
2198 int err;
2199 do {
2200 err = nfs4_handle_exception(server,
2201 _nfs4_proc_getattr(server, fhandle, fattr),
2202 &exception);
2203 } while (exception.retry);
2204 return err;
2205}
2206
2207/*
2208 * The file is not closed if it is opened due to the a request to change
2209 * the size of the file. The open call will not be needed once the
2210 * VFS layer lookup-intents are implemented.
2211 *
2212 * Close is called when the inode is destroyed.
2213 * If we haven't opened the file for O_WRONLY, we
2214 * need to in the size_change case to obtain a stateid.
2215 *
2216 * Got race?
2217 * Because OPEN is always done by name in nfsv4, it is
2218 * possible that we opened a different file by the same
2219 * name. We can recognize this race condition, but we
2220 * can't do anything about it besides returning an error.
2221 *
2222 * This will be fixed with VFS changes (lookup-intent).
2223 */
2224static int
2225nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2226 struct iattr *sattr)
2227{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002228 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002229 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002230 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 int status;
2232
Trond Myklebust0e574af2005-10-27 22:12:38 -04002233 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
Trond Myklebustd5308382005-11-04 15:33:38 -05002235 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002236 if (sattr->ia_valid & ATTR_FILE) {
2237 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002238
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002239 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002240 if (ctx) {
2241 cred = ctx->cred;
2242 state = ctx->state;
2243 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002244 }
2245
2246 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002247 if (status == 0)
2248 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 return status;
2250}
2251
Trond Myklebust56659e92007-07-17 21:52:39 -04002252static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2253 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04002254 struct nfs_fattr *fattr)
2255{
2256 int status;
2257 struct nfs4_lookup_arg args = {
2258 .bitmask = server->attr_bitmask,
2259 .dir_fh = dirfh,
2260 .name = name,
2261 };
2262 struct nfs4_lookup_res res = {
2263 .server = server,
2264 .fattr = fattr,
2265 .fh = fhandle,
2266 };
2267 struct rpc_message msg = {
2268 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2269 .rpc_argp = &args,
2270 .rpc_resp = &res,
2271 };
2272
2273 nfs_fattr_init(fattr);
2274
2275 dprintk("NFS call lookupfh %s\n", name->name);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002276 status = nfs4_call_sync(server, &msg, &args, &res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002277 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002278 return status;
2279}
2280
2281static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2282 struct qstr *name, struct nfs_fh *fhandle,
2283 struct nfs_fattr *fattr)
2284{
2285 struct nfs4_exception exception = { };
2286 int err;
2287 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04002288 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2289 /* FIXME: !!!! */
2290 if (err == -NFS4ERR_MOVED) {
2291 err = -EREMOTE;
2292 break;
2293 }
2294 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002295 } while (exception.retry);
2296 return err;
2297}
2298
Trond Myklebust56659e92007-07-17 21:52:39 -04002299static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2301{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002302 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
2304 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002305 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002306 if (status == -NFS4ERR_MOVED)
2307 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 dprintk("NFS reply lookup: %d\n", status);
2309 return status;
2310}
2311
2312static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2313{
2314 struct nfs4_exception exception = { };
2315 int err;
2316 do {
2317 err = nfs4_handle_exception(NFS_SERVER(dir),
2318 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2319 &exception);
2320 } while (exception.retry);
2321 return err;
2322}
2323
2324static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2325{
Trond Myklebust76b32992007-08-10 17:45:11 -04002326 struct nfs_server *server = NFS_SERVER(inode);
2327 struct nfs_fattr fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 struct nfs4_accessargs args = {
2329 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002330 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002332 struct nfs4_accessres res = {
2333 .server = server,
2334 .fattr = &fattr,
2335 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 struct rpc_message msg = {
2337 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2338 .rpc_argp = &args,
2339 .rpc_resp = &res,
2340 .rpc_cred = entry->cred,
2341 };
2342 int mode = entry->mask;
2343 int status;
2344
2345 /*
2346 * Determine which access bits we want to ask for...
2347 */
2348 if (mode & MAY_READ)
2349 args.access |= NFS4_ACCESS_READ;
2350 if (S_ISDIR(inode->i_mode)) {
2351 if (mode & MAY_WRITE)
2352 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2353 if (mode & MAY_EXEC)
2354 args.access |= NFS4_ACCESS_LOOKUP;
2355 } else {
2356 if (mode & MAY_WRITE)
2357 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2358 if (mode & MAY_EXEC)
2359 args.access |= NFS4_ACCESS_EXECUTE;
2360 }
Trond Myklebust76b32992007-08-10 17:45:11 -04002361 nfs_fattr_init(&fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002362 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 if (!status) {
2364 entry->mask = 0;
2365 if (res.access & NFS4_ACCESS_READ)
2366 entry->mask |= MAY_READ;
2367 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2368 entry->mask |= MAY_WRITE;
2369 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2370 entry->mask |= MAY_EXEC;
Trond Myklebust76b32992007-08-10 17:45:11 -04002371 nfs_refresh_inode(inode, &fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 }
2373 return status;
2374}
2375
2376static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2377{
2378 struct nfs4_exception exception = { };
2379 int err;
2380 do {
2381 err = nfs4_handle_exception(NFS_SERVER(inode),
2382 _nfs4_proc_access(inode, entry),
2383 &exception);
2384 } while (exception.retry);
2385 return err;
2386}
2387
2388/*
2389 * TODO: For the time being, we don't try to get any attributes
2390 * along with any of the zero-copy operations READ, READDIR,
2391 * READLINK, WRITE.
2392 *
2393 * In the case of the first three, we want to put the GETATTR
2394 * after the read-type operation -- this is because it is hard
2395 * to predict the length of a GETATTR response in v4, and thus
2396 * align the READ data correctly. This means that the GETATTR
2397 * may end up partially falling into the page cache, and we should
2398 * shift it into the 'tail' of the xdr_buf before processing.
2399 * To do this efficiently, we need to know the total length
2400 * of data received, which doesn't seem to be available outside
2401 * of the RPC layer.
2402 *
2403 * In the case of WRITE, we also want to put the GETATTR after
2404 * the operation -- in this case because we want to make sure
2405 * we get the post-operation mtime and size. This means that
2406 * we can't use xdr_encode_pages() as written: we need a variant
2407 * of it which would leave room in the 'tail' iovec.
2408 *
2409 * Both of these changes to the XDR layer would in fact be quite
2410 * minor, but I decided to leave them for a subsequent patch.
2411 */
2412static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2413 unsigned int pgbase, unsigned int pglen)
2414{
2415 struct nfs4_readlink args = {
2416 .fh = NFS_FH(inode),
2417 .pgbase = pgbase,
2418 .pglen = pglen,
2419 .pages = &page,
2420 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002421 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 struct rpc_message msg = {
2423 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2424 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002425 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 };
2427
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002428 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429}
2430
2431static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2432 unsigned int pgbase, unsigned int pglen)
2433{
2434 struct nfs4_exception exception = { };
2435 int err;
2436 do {
2437 err = nfs4_handle_exception(NFS_SERVER(inode),
2438 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2439 &exception);
2440 } while (exception.retry);
2441 return err;
2442}
2443
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444/*
2445 * Got race?
2446 * We will need to arrange for the VFS layer to provide an atomic open.
2447 * Until then, this create/open method is prone to inefficiency and race
2448 * conditions due to the lookup, create, and open VFS calls from sys_open()
2449 * placed on the wire.
2450 *
2451 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2452 * The file will be opened again in the subsequent VFS open call
2453 * (nfs4_proc_file_open).
2454 *
2455 * The open for read will just hang around to be used by any process that
2456 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2457 */
2458
2459static int
2460nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07002461 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462{
Trond Myklebustad389da2007-06-05 12:30:00 -04002463 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002464 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002465 .dentry = dentry,
2466 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 struct nfs4_state *state;
2468 struct rpc_cred *cred;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002469 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 int status = 0;
2471
Trond Myklebust98a8e322008-03-12 12:25:28 -04002472 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 if (IS_ERR(cred)) {
2474 status = PTR_ERR(cred);
2475 goto out;
2476 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002477 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002478 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 if (IS_ERR(state)) {
2480 status = PTR_ERR(state);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002481 goto out_putcred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002483 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002484 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 if (flags & O_EXCL) {
2486 struct nfs_fattr fattr;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002487 status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002488 if (status == 0)
Trond Myklebust65e43082005-08-16 11:49:44 -04002489 nfs_setattr_update_inode(state->inode, sattr);
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002490 nfs_post_op_update_inode(state->inode, &fattr);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002491 }
Trond Myklebustad389da2007-06-05 12:30:00 -04002492 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002493 status = nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002494 else
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002495 nfs4_close_sync(&path, state, fmode);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002496out_putcred:
2497 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498out:
2499 return status;
2500}
2501
2502static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2503{
Trond Myklebust16e42952005-10-27 22:12:44 -04002504 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002505 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002507 .name.len = name->len,
2508 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002509 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002511 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002512 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002513 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002515 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2516 .rpc_argp = &args,
2517 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 };
2519 int status;
2520
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002521 nfs_fattr_init(&res.dir_attr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002522 status = nfs4_call_sync(server, &msg, &args, &res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002523 if (status == 0) {
2524 update_changeattr(dir, &res.cinfo);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002525 nfs_post_op_update_inode(dir, &res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 return status;
2528}
2529
2530static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2531{
2532 struct nfs4_exception exception = { };
2533 int err;
2534 do {
2535 err = nfs4_handle_exception(NFS_SERVER(dir),
2536 _nfs4_proc_remove(dir, name),
2537 &exception);
2538 } while (exception.retry);
2539 return err;
2540}
2541
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002542static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002544 struct nfs_server *server = NFS_SERVER(dir);
2545 struct nfs_removeargs *args = msg->rpc_argp;
2546 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547
Trond Myklebusta65318b2009-03-11 14:10:28 -04002548 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002549 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551}
2552
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002553static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002555 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2556
Andy Adamson472cfbd2009-04-01 09:22:24 -04002557 nfs4_sequence_done(res->server, &res->seq_res, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002558 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002559 return 0;
2560 update_changeattr(dir, &res->cinfo);
2561 nfs_post_op_update_inode(dir, &res->dir_attr);
2562 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563}
2564
2565static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2566 struct inode *new_dir, struct qstr *new_name)
2567{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002568 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 struct nfs4_rename_arg arg = {
2570 .old_dir = NFS_FH(old_dir),
2571 .new_dir = NFS_FH(new_dir),
2572 .old_name = old_name,
2573 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002574 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002576 struct nfs_fattr old_fattr, new_fattr;
2577 struct nfs4_rename_res res = {
2578 .server = server,
2579 .old_fattr = &old_fattr,
2580 .new_fattr = &new_fattr,
2581 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 struct rpc_message msg = {
2583 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2584 .rpc_argp = &arg,
2585 .rpc_resp = &res,
2586 };
2587 int status;
2588
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002589 nfs_fattr_init(res.old_fattr);
2590 nfs_fattr_init(res.new_fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002591 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
2593 if (!status) {
2594 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002595 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002597 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 }
2599 return status;
2600}
2601
2602static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2603 struct inode *new_dir, struct qstr *new_name)
2604{
2605 struct nfs4_exception exception = { };
2606 int err;
2607 do {
2608 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2609 _nfs4_proc_rename(old_dir, old_name,
2610 new_dir, new_name),
2611 &exception);
2612 } while (exception.retry);
2613 return err;
2614}
2615
2616static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2617{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002618 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 struct nfs4_link_arg arg = {
2620 .fh = NFS_FH(inode),
2621 .dir_fh = NFS_FH(dir),
2622 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002623 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002625 struct nfs_fattr fattr, dir_attr;
2626 struct nfs4_link_res res = {
2627 .server = server,
2628 .fattr = &fattr,
2629 .dir_attr = &dir_attr,
2630 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 struct rpc_message msg = {
2632 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2633 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002634 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 };
2636 int status;
2637
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002638 nfs_fattr_init(res.fattr);
2639 nfs_fattr_init(res.dir_attr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002640 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002641 if (!status) {
2642 update_changeattr(dir, &res.cinfo);
2643 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002644 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646
2647 return status;
2648}
2649
2650static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2651{
2652 struct nfs4_exception exception = { };
2653 int err;
2654 do {
2655 err = nfs4_handle_exception(NFS_SERVER(inode),
2656 _nfs4_proc_link(inode, dir, name),
2657 &exception);
2658 } while (exception.retry);
2659 return err;
2660}
2661
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002662struct nfs4_createdata {
2663 struct rpc_message msg;
2664 struct nfs4_create_arg arg;
2665 struct nfs4_create_res res;
2666 struct nfs_fh fh;
2667 struct nfs_fattr fattr;
2668 struct nfs_fattr dir_fattr;
2669};
2670
2671static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2672 struct qstr *name, struct iattr *sattr, u32 ftype)
2673{
2674 struct nfs4_createdata *data;
2675
2676 data = kzalloc(sizeof(*data), GFP_KERNEL);
2677 if (data != NULL) {
2678 struct nfs_server *server = NFS_SERVER(dir);
2679
2680 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2681 data->msg.rpc_argp = &data->arg;
2682 data->msg.rpc_resp = &data->res;
2683 data->arg.dir_fh = NFS_FH(dir);
2684 data->arg.server = server;
2685 data->arg.name = name;
2686 data->arg.attrs = sattr;
2687 data->arg.ftype = ftype;
2688 data->arg.bitmask = server->attr_bitmask;
2689 data->res.server = server;
2690 data->res.fh = &data->fh;
2691 data->res.fattr = &data->fattr;
2692 data->res.dir_fattr = &data->dir_fattr;
2693 nfs_fattr_init(data->res.fattr);
2694 nfs_fattr_init(data->res.dir_fattr);
2695 }
2696 return data;
2697}
2698
2699static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2700{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002701 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2702 &data->arg, &data->res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002703 if (status == 0) {
2704 update_changeattr(dir, &data->res.dir_cinfo);
2705 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2706 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2707 }
2708 return status;
2709}
2710
2711static void nfs4_free_createdata(struct nfs4_createdata *data)
2712{
2713 kfree(data);
2714}
2715
Chuck Lever4f390c12006-08-22 20:06:22 -04002716static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002717 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002719 struct nfs4_createdata *data;
2720 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721
Chuck Lever94a6d752006-08-22 20:06:23 -04002722 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002723 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002724
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002725 status = -ENOMEM;
2726 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2727 if (data == NULL)
2728 goto out;
2729
2730 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2731 data->arg.u.symlink.pages = &page;
2732 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002734 status = nfs4_do_create(dir, dentry, data);
2735
2736 nfs4_free_createdata(data);
2737out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 return status;
2739}
2740
Chuck Lever4f390c12006-08-22 20:06:22 -04002741static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002742 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743{
2744 struct nfs4_exception exception = { };
2745 int err;
2746 do {
2747 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002748 _nfs4_proc_symlink(dir, dentry, page,
2749 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 &exception);
2751 } while (exception.retry);
2752 return err;
2753}
2754
2755static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2756 struct iattr *sattr)
2757{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002758 struct nfs4_createdata *data;
2759 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002761 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2762 if (data == NULL)
2763 goto out;
2764
2765 status = nfs4_do_create(dir, dentry, data);
2766
2767 nfs4_free_createdata(data);
2768out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 return status;
2770}
2771
2772static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2773 struct iattr *sattr)
2774{
2775 struct nfs4_exception exception = { };
2776 int err;
2777 do {
2778 err = nfs4_handle_exception(NFS_SERVER(dir),
2779 _nfs4_proc_mkdir(dir, dentry, sattr),
2780 &exception);
2781 } while (exception.retry);
2782 return err;
2783}
2784
2785static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2786 u64 cookie, struct page *page, unsigned int count, int plus)
2787{
2788 struct inode *dir = dentry->d_inode;
2789 struct nfs4_readdir_arg args = {
2790 .fh = NFS_FH(dir),
2791 .pages = &page,
2792 .pgbase = 0,
2793 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002794 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 };
2796 struct nfs4_readdir_res res;
2797 struct rpc_message msg = {
2798 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2799 .rpc_argp = &args,
2800 .rpc_resp = &res,
2801 .rpc_cred = cred,
2802 };
2803 int status;
2804
Harvey Harrison3110ff82008-05-02 13:42:44 -07002805 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002806 dentry->d_parent->d_name.name,
2807 dentry->d_name.name,
2808 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2810 res.pgbase = args.pgbase;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002811 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 if (status == 0)
2813 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustc4812992007-09-28 17:11:45 -04002814
2815 nfs_invalidate_atime(dir);
2816
Harvey Harrison3110ff82008-05-02 13:42:44 -07002817 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 return status;
2819}
2820
2821static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2822 u64 cookie, struct page *page, unsigned int count, int plus)
2823{
2824 struct nfs4_exception exception = { };
2825 int err;
2826 do {
2827 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2828 _nfs4_proc_readdir(dentry, cred, cookie,
2829 page, count, plus),
2830 &exception);
2831 } while (exception.retry);
2832 return err;
2833}
2834
2835static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2836 struct iattr *sattr, dev_t rdev)
2837{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002838 struct nfs4_createdata *data;
2839 int mode = sattr->ia_mode;
2840 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841
2842 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2843 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002844
2845 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2846 if (data == NULL)
2847 goto out;
2848
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002850 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002852 data->arg.ftype = NF4BLK;
2853 data->arg.u.device.specdata1 = MAJOR(rdev);
2854 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 }
2856 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002857 data->arg.ftype = NF4CHR;
2858 data->arg.u.device.specdata1 = MAJOR(rdev);
2859 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002862 status = nfs4_do_create(dir, dentry, data);
2863
2864 nfs4_free_createdata(data);
2865out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 return status;
2867}
2868
2869static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2870 struct iattr *sattr, dev_t rdev)
2871{
2872 struct nfs4_exception exception = { };
2873 int err;
2874 do {
2875 err = nfs4_handle_exception(NFS_SERVER(dir),
2876 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2877 &exception);
2878 } while (exception.retry);
2879 return err;
2880}
2881
2882static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2883 struct nfs_fsstat *fsstat)
2884{
2885 struct nfs4_statfs_arg args = {
2886 .fh = fhandle,
2887 .bitmask = server->attr_bitmask,
2888 };
Benny Halevy24ad1482009-04-01 09:21:56 -04002889 struct nfs4_statfs_res res = {
2890 .fsstat = fsstat,
2891 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 struct rpc_message msg = {
2893 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2894 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04002895 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 };
2897
Trond Myklebust0e574af2005-10-27 22:12:38 -04002898 nfs_fattr_init(fsstat->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002899 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900}
2901
2902static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2903{
2904 struct nfs4_exception exception = { };
2905 int err;
2906 do {
2907 err = nfs4_handle_exception(server,
2908 _nfs4_proc_statfs(server, fhandle, fsstat),
2909 &exception);
2910 } while (exception.retry);
2911 return err;
2912}
2913
2914static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2915 struct nfs_fsinfo *fsinfo)
2916{
2917 struct nfs4_fsinfo_arg args = {
2918 .fh = fhandle,
2919 .bitmask = server->attr_bitmask,
2920 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04002921 struct nfs4_fsinfo_res res = {
2922 .fsinfo = fsinfo,
2923 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 struct rpc_message msg = {
2925 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2926 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04002927 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 };
2929
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002930 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931}
2932
2933static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2934{
2935 struct nfs4_exception exception = { };
2936 int err;
2937
2938 do {
2939 err = nfs4_handle_exception(server,
2940 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2941 &exception);
2942 } while (exception.retry);
2943 return err;
2944}
2945
2946static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2947{
Trond Myklebust0e574af2005-10-27 22:12:38 -04002948 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2950}
2951
2952static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2953 struct nfs_pathconf *pathconf)
2954{
2955 struct nfs4_pathconf_arg args = {
2956 .fh = fhandle,
2957 .bitmask = server->attr_bitmask,
2958 };
Benny Halevyd45b2982009-04-01 09:21:58 -04002959 struct nfs4_pathconf_res res = {
2960 .pathconf = pathconf,
2961 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 struct rpc_message msg = {
2963 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2964 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04002965 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 };
2967
2968 /* None of the pathconf attributes are mandatory to implement */
2969 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2970 memset(pathconf, 0, sizeof(*pathconf));
2971 return 0;
2972 }
2973
Trond Myklebust0e574af2005-10-27 22:12:38 -04002974 nfs_fattr_init(pathconf->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002975 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976}
2977
2978static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2979 struct nfs_pathconf *pathconf)
2980{
2981 struct nfs4_exception exception = { };
2982 int err;
2983
2984 do {
2985 err = nfs4_handle_exception(server,
2986 _nfs4_proc_pathconf(server, fhandle, pathconf),
2987 &exception);
2988 } while (exception.retry);
2989 return err;
2990}
2991
Trond Myklebustec06c092006-03-20 13:44:27 -05002992static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993{
Trond Myklebustec06c092006-03-20 13:44:27 -05002994 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
Andy Adamsonf11c88a2009-04-01 09:22:25 -04002996 dprintk("--> %s\n", __func__);
2997
Andy Adamsonf11c88a2009-04-01 09:22:25 -04002998 nfs4_sequence_done(server, &data->res.seq_res, task->tk_status);
2999
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003000 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003001 nfs_restart_rpc(task, server->nfs_client);
Trond Myklebustec06c092006-03-20 13:44:27 -05003002 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003004
3005 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003007 renew_lease(server, data->timestamp);
3008 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009}
3010
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003011static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 data->timestamp = jiffies;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003014 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015}
3016
Trond Myklebust788e7a82006-03-20 13:44:27 -05003017static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 struct inode *inode = data->inode;
3020
Andy Adamsondef6ed72009-04-01 09:22:26 -04003021 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3022 task->tk_status);
3023
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003024 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003025 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003026 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003028 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003030 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003031 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003032 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033}
3034
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003035static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003037 struct nfs_server *server = NFS_SERVER(data->inode);
3038
Trond Myklebusta65318b2009-03-11 14:10:28 -04003039 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003040 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 data->timestamp = jiffies;
3042
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003043 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044}
3045
Trond Myklebust788e7a82006-03-20 13:44:27 -05003046static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 struct inode *inode = data->inode;
3049
Andy Adamson21d9a852009-04-01 09:22:27 -04003050 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3051 task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003052 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003053 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003054 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003056 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003057 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058}
3059
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003060static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003062 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063
Trond Myklebusta65318b2009-03-11 14:10:28 -04003064 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003065 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003066 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067}
3068
3069/*
3070 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3071 * standalone procedure for queueing an asynchronous RENEW.
3072 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003073static void nfs4_renew_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074{
David Howellsadfa6f92006-08-22 20:06:08 -04003075 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003076 unsigned long timestamp = (unsigned long)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077
3078 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003079 /* Unless we're shutting down, schedule state recovery! */
3080 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3081 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 return;
3083 }
3084 spin_lock(&clp->cl_lock);
3085 if (time_before(clp->cl_last_renewal,timestamp))
3086 clp->cl_last_renewal = timestamp;
3087 spin_unlock(&clp->cl_lock);
3088}
3089
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003090static const struct rpc_call_ops nfs4_renew_ops = {
3091 .rpc_call_done = nfs4_renew_done,
3092};
3093
David Howellsadfa6f92006-08-22 20:06:08 -04003094int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095{
3096 struct rpc_message msg = {
3097 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3098 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003099 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 };
3101
3102 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003103 &nfs4_renew_ops, (void *)jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104}
3105
David Howellsadfa6f92006-08-22 20:06:08 -04003106int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107{
3108 struct rpc_message msg = {
3109 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3110 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003111 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 };
3113 unsigned long now = jiffies;
3114 int status;
3115
3116 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3117 if (status < 0)
3118 return status;
3119 spin_lock(&clp->cl_lock);
3120 if (time_before(clp->cl_last_renewal,now))
3121 clp->cl_last_renewal = now;
3122 spin_unlock(&clp->cl_lock);
3123 return 0;
3124}
3125
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003126static inline int nfs4_server_supports_acls(struct nfs_server *server)
3127{
3128 return (server->caps & NFS_CAP_ACLS)
3129 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3130 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3131}
3132
3133/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3134 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3135 * the stack.
3136 */
3137#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3138
3139static void buf_to_pages(const void *buf, size_t buflen,
3140 struct page **pages, unsigned int *pgbase)
3141{
3142 const void *p = buf;
3143
3144 *pgbase = offset_in_page(buf);
3145 p -= *pgbase;
3146 while (p < buf + buflen) {
3147 *(pages++) = virt_to_page(p);
3148 p += PAGE_CACHE_SIZE;
3149 }
3150}
3151
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003152struct nfs4_cached_acl {
3153 int cached;
3154 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003155 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003156};
3157
3158static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003159{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003160 struct nfs_inode *nfsi = NFS_I(inode);
3161
3162 spin_lock(&inode->i_lock);
3163 kfree(nfsi->nfs4_acl);
3164 nfsi->nfs4_acl = acl;
3165 spin_unlock(&inode->i_lock);
3166}
3167
3168static void nfs4_zap_acl_attr(struct inode *inode)
3169{
3170 nfs4_set_cached_acl(inode, NULL);
3171}
3172
3173static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3174{
3175 struct nfs_inode *nfsi = NFS_I(inode);
3176 struct nfs4_cached_acl *acl;
3177 int ret = -ENOENT;
3178
3179 spin_lock(&inode->i_lock);
3180 acl = nfsi->nfs4_acl;
3181 if (acl == NULL)
3182 goto out;
3183 if (buf == NULL) /* user is just asking for length */
3184 goto out_len;
3185 if (acl->cached == 0)
3186 goto out;
3187 ret = -ERANGE; /* see getxattr(2) man page */
3188 if (acl->len > buflen)
3189 goto out;
3190 memcpy(buf, acl->data, acl->len);
3191out_len:
3192 ret = acl->len;
3193out:
3194 spin_unlock(&inode->i_lock);
3195 return ret;
3196}
3197
3198static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3199{
3200 struct nfs4_cached_acl *acl;
3201
3202 if (buf && acl_len <= PAGE_SIZE) {
3203 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3204 if (acl == NULL)
3205 goto out;
3206 acl->cached = 1;
3207 memcpy(acl->data, buf, acl_len);
3208 } else {
3209 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3210 if (acl == NULL)
3211 goto out;
3212 acl->cached = 0;
3213 }
3214 acl->len = acl_len;
3215out:
3216 nfs4_set_cached_acl(inode, acl);
3217}
3218
Trond Myklebust16b42892006-08-24 12:27:15 -04003219static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003220{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003221 struct page *pages[NFS4ACL_MAXPAGES];
3222 struct nfs_getaclargs args = {
3223 .fh = NFS_FH(inode),
3224 .acl_pages = pages,
3225 .acl_len = buflen,
3226 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003227 struct nfs_getaclres res = {
3228 .acl_len = buflen,
3229 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003230 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003231 struct rpc_message msg = {
3232 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3233 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003234 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003235 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003236 struct page *localpage = NULL;
3237 int ret;
3238
3239 if (buflen < PAGE_SIZE) {
3240 /* As long as we're doing a round trip to the server anyway,
3241 * let's be prepared for a page of acl data. */
3242 localpage = alloc_page(GFP_KERNEL);
3243 resp_buf = page_address(localpage);
3244 if (localpage == NULL)
3245 return -ENOMEM;
3246 args.acl_pages[0] = localpage;
3247 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003248 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003249 } else {
3250 resp_buf = buf;
3251 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3252 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003253 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003254 if (ret)
3255 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003256 if (res.acl_len > args.acl_len)
3257 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003258 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003259 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003260 if (buf) {
3261 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003262 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003263 goto out_free;
3264 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003265 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003266 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003267 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003268out_free:
3269 if (localpage)
3270 __free_page(localpage);
3271 return ret;
3272}
3273
Trond Myklebust16b42892006-08-24 12:27:15 -04003274static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3275{
3276 struct nfs4_exception exception = { };
3277 ssize_t ret;
3278 do {
3279 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3280 if (ret >= 0)
3281 break;
3282 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3283 } while (exception.retry);
3284 return ret;
3285}
3286
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003287static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3288{
3289 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003290 int ret;
3291
3292 if (!nfs4_server_supports_acls(server))
3293 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003294 ret = nfs_revalidate_inode(server, inode);
3295 if (ret < 0)
3296 return ret;
3297 ret = nfs4_read_cached_acl(inode, buf, buflen);
3298 if (ret != -ENOENT)
3299 return ret;
3300 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003301}
3302
Trond Myklebust16b42892006-08-24 12:27:15 -04003303static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003304{
3305 struct nfs_server *server = NFS_SERVER(inode);
3306 struct page *pages[NFS4ACL_MAXPAGES];
3307 struct nfs_setaclargs arg = {
3308 .fh = NFS_FH(inode),
3309 .acl_pages = pages,
3310 .acl_len = buflen,
3311 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003312 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003313 struct rpc_message msg = {
3314 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3315 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003316 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003317 };
3318 int ret;
3319
3320 if (!nfs4_server_supports_acls(server))
3321 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07003322 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003323 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003324 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003325 nfs_access_zap_cache(inode);
3326 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003327 return ret;
3328}
3329
Trond Myklebust16b42892006-08-24 12:27:15 -04003330static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3331{
3332 struct nfs4_exception exception = { };
3333 int err;
3334 do {
3335 err = nfs4_handle_exception(NFS_SERVER(inode),
3336 __nfs4_proc_set_acl(inode, buf, buflen),
3337 &exception);
3338 } while (exception.retry);
3339 return err;
3340}
3341
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342static int
Andy Adamson8328d592009-04-01 09:22:35 -04003343_nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs_client *clp, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 if (!clp || task->tk_status >= 0)
3346 return 0;
3347 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003348 case -NFS4ERR_ADMIN_REVOKED:
3349 case -NFS4ERR_BAD_STATEID:
3350 case -NFS4ERR_OPENMODE:
3351 if (state == NULL)
3352 break;
3353 nfs4_state_mark_reclaim_nograce(clp, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 case -NFS4ERR_STALE_CLIENTID:
3355 case -NFS4ERR_STALE_STATEID:
3356 case -NFS4ERR_EXPIRED:
Trond Myklebust5d008372008-02-22 16:34:17 -05003357 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 nfs4_schedule_state_recovery(clp);
Trond Myklebuste005e802008-12-23 15:21:48 -05003359 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
Trond Myklebustfda13932008-02-22 16:34:12 -05003360 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 task->tk_status = 0;
3362 return -EAGAIN;
Andy Adamson4745e312009-04-01 09:22:42 -04003363#if defined(CONFIG_NFS_V4_1)
3364 case -NFS4ERR_BADSESSION:
3365 case -NFS4ERR_BADSLOT:
3366 case -NFS4ERR_BAD_HIGH_SLOT:
3367 case -NFS4ERR_DEADSESSION:
3368 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3369 case -NFS4ERR_SEQ_FALSE_RETRY:
3370 case -NFS4ERR_SEQ_MISORDERED:
3371 dprintk("%s ERROR %d, Reset session\n", __func__,
3372 task->tk_status);
Andy Adamson0b9e2d42009-12-04 16:02:14 -05003373 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -04003374 task->tk_status = 0;
3375 return -EAGAIN;
3376#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 case -NFS4ERR_DELAY:
Andy Adamson8328d592009-04-01 09:22:35 -04003378 if (server)
3379 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003380 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3382 task->tk_status = 0;
3383 return -EAGAIN;
3384 case -NFS4ERR_OLD_STATEID:
3385 task->tk_status = 0;
3386 return -EAGAIN;
3387 }
3388 task->tk_status = nfs4_map_errors(task->tk_status);
3389 return 0;
3390}
3391
Andy Adamson8328d592009-04-01 09:22:35 -04003392static int
3393nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3394{
3395 return _nfs4_async_handle_error(task, server, server->nfs_client, state);
3396}
3397
David Howellsadfa6f92006-08-22 20:06:08 -04003398int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399{
3400 nfs4_verifier sc_verifier;
3401 struct nfs4_setclientid setclientid = {
3402 .sc_verifier = &sc_verifier,
3403 .sc_prog = program,
3404 };
3405 struct rpc_message msg = {
3406 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3407 .rpc_argp = &setclientid,
3408 .rpc_resp = clp,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003409 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 };
Al Virobc4785c2006-10-19 23:28:51 -07003411 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 int loop = 0;
3413 int status;
3414
Al Virobc4785c2006-10-19 23:28:51 -07003415 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3417 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3418
3419 for(;;) {
3420 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003421 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003422 clp->cl_ipaddr,
3423 rpc_peeraddr2str(clp->cl_rpcclient,
3424 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003425 rpc_peeraddr2str(clp->cl_rpcclient,
3426 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003427 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 clp->cl_id_uniquifier);
3429 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003430 sizeof(setclientid.sc_netid),
3431 rpc_peeraddr2str(clp->cl_rpcclient,
3432 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003434 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 clp->cl_ipaddr, port >> 8, port & 255);
3436
3437 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3438 if (status != -NFS4ERR_CLID_INUSE)
3439 break;
3440 if (signalled())
3441 break;
3442 if (loop++ & 1)
3443 ssleep(clp->cl_lease_time + 1);
3444 else
3445 if (++clp->cl_id_uniquifier == 0)
3446 break;
3447 }
3448 return status;
3449}
3450
David Howellsadfa6f92006-08-22 20:06:08 -04003451static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452{
3453 struct nfs_fsinfo fsinfo;
3454 struct rpc_message msg = {
3455 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3456 .rpc_argp = clp,
3457 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003458 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 };
3460 unsigned long now;
3461 int status;
3462
3463 now = jiffies;
3464 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3465 if (status == 0) {
3466 spin_lock(&clp->cl_lock);
3467 clp->cl_lease_time = fsinfo.lease_time * HZ;
3468 clp->cl_last_renewal = now;
3469 spin_unlock(&clp->cl_lock);
3470 }
3471 return status;
3472}
3473
David Howellsadfa6f92006-08-22 20:06:08 -04003474int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003475{
Benny Halevy77e03672008-06-24 20:25:57 +03003476 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003477 int err;
3478 do {
3479 err = _nfs4_proc_setclientid_confirm(clp, cred);
3480 switch (err) {
3481 case 0:
3482 return err;
3483 case -NFS4ERR_RESOURCE:
3484 /* The IBM lawyers misread another document! */
3485 case -NFS4ERR_DELAY:
3486 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3487 }
3488 } while (err == 0);
3489 return err;
3490}
3491
Trond Myklebustfe650402006-01-03 09:55:18 +01003492struct nfs4_delegreturndata {
3493 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003494 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003495 struct nfs_fh fh;
3496 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003497 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003498 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003499 int rpc_status;
3500};
3501
Trond Myklebustfe650402006-01-03 09:55:18 +01003502static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3503{
3504 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003505
Trond Myklebustd61e6122009-12-05 19:32:19 -05003506 nfs4_sequence_done(data->res.server, &data->res.seq_res,
3507 task->tk_status);
Andy Adamson938e1012009-04-01 09:22:28 -04003508
Ricardo Labiaga79708862009-12-07 09:23:21 -05003509 switch (task->tk_status) {
3510 case -NFS4ERR_STALE_STATEID:
3511 case -NFS4ERR_EXPIRED:
3512 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003513 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003514 break;
3515 default:
3516 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3517 -EAGAIN) {
3518 nfs_restart_rpc(task, data->res.server->nfs_client);
3519 return;
3520 }
3521 }
3522 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003523}
3524
3525static void nfs4_delegreturn_release(void *calldata)
3526{
Trond Myklebustfe650402006-01-03 09:55:18 +01003527 kfree(calldata);
3528}
3529
Andy Adamson938e1012009-04-01 09:22:28 -04003530#if defined(CONFIG_NFS_V4_1)
3531static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3532{
3533 struct nfs4_delegreturndata *d_data;
3534
3535 d_data = (struct nfs4_delegreturndata *)data;
3536
3537 if (nfs4_setup_sequence(d_data->res.server->nfs_client,
3538 &d_data->args.seq_args,
3539 &d_data->res.seq_res, 1, task))
3540 return;
3541 rpc_call_start(task);
3542}
3543#endif /* CONFIG_NFS_V4_1 */
3544
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003545static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003546#if defined(CONFIG_NFS_V4_1)
3547 .rpc_call_prepare = nfs4_delegreturn_prepare,
3548#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003549 .rpc_call_done = nfs4_delegreturn_done,
3550 .rpc_release = nfs4_delegreturn_release,
3551};
3552
Trond Myklebuste6f81072008-01-24 18:14:34 -05003553static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003554{
3555 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003556 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003557 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003558 struct rpc_message msg = {
3559 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3560 .rpc_cred = cred,
3561 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003562 struct rpc_task_setup task_setup_data = {
3563 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003564 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003565 .callback_ops = &nfs4_delegreturn_ops,
3566 .flags = RPC_TASK_ASYNC,
3567 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003568 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003569
Andy Adamson938e1012009-04-01 09:22:28 -04003570 data = kzalloc(sizeof(*data), GFP_KERNEL);
Trond Myklebustfe650402006-01-03 09:55:18 +01003571 if (data == NULL)
3572 return -ENOMEM;
3573 data->args.fhandle = &data->fh;
3574 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003575 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003576 nfs_copy_fh(&data->fh, NFS_FH(inode));
3577 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003578 data->res.fattr = &data->fattr;
3579 data->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003580 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003581 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003582 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003583 data->rpc_status = 0;
3584
Trond Myklebustc970aa82007-07-14 15:39:59 -04003585 task_setup_data.callback_data = data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003586 msg.rpc_argp = &data->args,
3587 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003588 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003589 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003590 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003591 if (!issync)
3592 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003593 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003594 if (status != 0)
3595 goto out;
3596 status = data->rpc_status;
3597 if (status != 0)
3598 goto out;
3599 nfs_refresh_inode(inode, &data->fattr);
3600out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003601 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003602 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603}
3604
Trond Myklebuste6f81072008-01-24 18:14:34 -05003605int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606{
3607 struct nfs_server *server = NFS_SERVER(inode);
3608 struct nfs4_exception exception = { };
3609 int err;
3610 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003611 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 switch (err) {
3613 case -NFS4ERR_STALE_STATEID:
3614 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 case 0:
3616 return 0;
3617 }
3618 err = nfs4_handle_exception(server, err, &exception);
3619 } while (exception.retry);
3620 return err;
3621}
3622
3623#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3624#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3625
3626/*
3627 * sleep, with exponential backoff, and retry the LOCK operation.
3628 */
3629static unsigned long
3630nfs4_set_lock_task_retry(unsigned long timeout)
3631{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003632 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 timeout <<= 1;
3634 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3635 return NFS4_LOCK_MAXTIMEOUT;
3636 return timeout;
3637}
3638
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3640{
3641 struct inode *inode = state->inode;
3642 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003643 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003644 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003646 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003648 struct nfs_lockt_res res = {
3649 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 };
3651 struct rpc_message msg = {
3652 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3653 .rpc_argp = &arg,
3654 .rpc_resp = &res,
3655 .rpc_cred = state->owner->so_cred,
3656 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 struct nfs4_lock_state *lsp;
3658 int status;
3659
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003660 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003661 status = nfs4_set_lock_state(state, request);
3662 if (status != 0)
3663 goto out;
3664 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003665 arg.lock_owner.id = lsp->ls_id.id;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003666 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003667 switch (status) {
3668 case 0:
3669 request->fl_type = F_UNLCK;
3670 break;
3671 case -NFS4ERR_DENIED:
3672 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003674 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003675out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 return status;
3677}
3678
3679static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3680{
3681 struct nfs4_exception exception = { };
3682 int err;
3683
3684 do {
3685 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3686 _nfs4_proc_getlk(state, cmd, request),
3687 &exception);
3688 } while (exception.retry);
3689 return err;
3690}
3691
3692static int do_vfs_lock(struct file *file, struct file_lock *fl)
3693{
3694 int res = 0;
3695 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3696 case FL_POSIX:
3697 res = posix_lock_file_wait(file, fl);
3698 break;
3699 case FL_FLOCK:
3700 res = flock_lock_file_wait(file, fl);
3701 break;
3702 default:
3703 BUG();
3704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 return res;
3706}
3707
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003708struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003709 struct nfs_locku_args arg;
3710 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003711 struct nfs4_lock_state *lsp;
3712 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003713 struct file_lock fl;
3714 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003715 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003716};
3717
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003718static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3719 struct nfs_open_context *ctx,
3720 struct nfs4_lock_state *lsp,
3721 struct nfs_seqid *seqid)
3722{
3723 struct nfs4_unlockdata *p;
3724 struct inode *inode = lsp->ls_state->inode;
3725
Andy Adamsona8936932009-04-01 09:22:23 -04003726 p = kzalloc(sizeof(*p), GFP_KERNEL);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003727 if (p == NULL)
3728 return NULL;
3729 p->arg.fh = NFS_FH(inode);
3730 p->arg.fl = &p->fl;
3731 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003732 p->res.seqid = seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003733 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003734 p->arg.stateid = &lsp->ls_stateid;
3735 p->lsp = lsp;
3736 atomic_inc(&lsp->ls_count);
3737 /* Ensure we don't close file until we're done freeing locks! */
3738 p->ctx = get_nfs_open_context(ctx);
3739 memcpy(&p->fl, fl, sizeof(p->fl));
3740 p->server = NFS_SERVER(inode);
3741 return p;
3742}
3743
Trond Myklebust06f814a2006-01-03 09:55:07 +01003744static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003745{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003746 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003747 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003748 nfs4_put_lock_state(calldata->lsp);
3749 put_nfs_open_context(calldata->ctx);
3750 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003751}
3752
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003753static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003754{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003755 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003756
Andy Adamsona8936932009-04-01 09:22:23 -04003757 nfs4_sequence_done(calldata->server, &calldata->res.seq_res,
3758 task->tk_status);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003759 if (RPC_ASSASSINATED(task))
3760 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003761 switch (task->tk_status) {
3762 case 0:
3763 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003764 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003765 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003766 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003767 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003768 case -NFS4ERR_BAD_STATEID:
3769 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003770 case -NFS4ERR_STALE_STATEID:
3771 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003772 break;
3773 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003774 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebust0110ee12009-12-07 09:00:24 -05003775 nfs_restart_rpc(task,
Trond Myklebustd61e6122009-12-05 19:32:19 -05003776 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003777 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003778}
3779
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003780static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003781{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003782 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003784 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003785 return;
3786 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003787 /* Note: exit _without_ running nfs4_locku_done */
3788 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003789 return;
3790 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003791 calldata->timestamp = jiffies;
Andy Adamsona8936932009-04-01 09:22:23 -04003792 if (nfs4_setup_sequence(calldata->server->nfs_client,
3793 &calldata->arg.seq_args,
3794 &calldata->res.seq_res, 1, task))
3795 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003796 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797}
3798
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003799static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003800 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003801 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003802 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003803};
3804
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003805static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3806 struct nfs_open_context *ctx,
3807 struct nfs4_lock_state *lsp,
3808 struct nfs_seqid *seqid)
3809{
3810 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003811 struct rpc_message msg = {
3812 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3813 .rpc_cred = ctx->cred,
3814 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003815 struct rpc_task_setup task_setup_data = {
3816 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003817 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003818 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003819 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003820 .flags = RPC_TASK_ASYNC,
3821 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003822
Frank Filz137d6ac2007-07-09 15:32:29 -07003823 /* Ensure this is an unlock - when canceling a lock, the
3824 * canceled lock is passed in, and it won't be an unlock.
3825 */
3826 fl->fl_type = F_UNLCK;
3827
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003828 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3829 if (data == NULL) {
3830 nfs_free_seqid(seqid);
3831 return ERR_PTR(-ENOMEM);
3832 }
3833
Trond Myklebust5138fde2007-07-14 15:40:01 -04003834 msg.rpc_argp = &data->arg,
3835 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003836 task_setup_data.callback_data = data;
3837 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003838}
3839
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3841{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003842 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003843 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003844 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003845 struct rpc_task *task;
3846 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003847 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848
Trond Myklebust9b073572006-06-29 16:38:34 -04003849 status = nfs4_set_lock_state(state, request);
3850 /* Unlock _before_ we do the RPC call */
3851 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003852 down_read(&nfsi->rwsem);
3853 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3854 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003855 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003856 }
3857 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003858 if (status != 0)
3859 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003860 /* Is this a delegated lock? */
3861 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04003862 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003863 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003864 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
Trond Myklebust9b073572006-06-29 16:38:34 -04003865 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003866 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04003867 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003868 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003869 status = PTR_ERR(task);
3870 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04003871 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003872 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003873 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04003874out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003875 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003876 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877}
3878
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003879struct nfs4_lockdata {
3880 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003881 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003882 struct nfs4_lock_state *lsp;
3883 struct nfs_open_context *ctx;
3884 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003885 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003886 int rpc_status;
3887 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04003888 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003889};
3890
3891static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3892 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3893{
3894 struct nfs4_lockdata *p;
3895 struct inode *inode = lsp->ls_state->inode;
3896 struct nfs_server *server = NFS_SERVER(inode);
3897
3898 p = kzalloc(sizeof(*p), GFP_KERNEL);
3899 if (p == NULL)
3900 return NULL;
3901
3902 p->arg.fh = NFS_FH(inode);
3903 p->arg.fl = &p->fl;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003904 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
3905 if (p->arg.open_seqid == NULL)
3906 goto out_free;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003907 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3908 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003909 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003910 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04003911 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003912 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003913 p->res.lock_seqid = p->arg.lock_seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003914 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003915 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04003916 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003917 atomic_inc(&lsp->ls_count);
3918 p->ctx = get_nfs_open_context(ctx);
3919 memcpy(&p->fl, fl, sizeof(p->fl));
3920 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003921out_free_seqid:
3922 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003923out_free:
3924 kfree(p);
3925 return NULL;
3926}
3927
3928static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3929{
3930 struct nfs4_lockdata *data = calldata;
3931 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932
Harvey Harrison3110ff82008-05-02 13:42:44 -07003933 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003934 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3935 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003936 /* Do we need to do an open_to_lock_owner? */
3937 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05003938 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
3939 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003940 data->arg.open_stateid = &state->stateid;
3941 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003942 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003943 } else
3944 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003945 data->timestamp = jiffies;
Andy Adamson66179ef2009-04-01 09:22:22 -04003946 if (nfs4_setup_sequence(data->server->nfs_client, &data->arg.seq_args,
3947 &data->res.seq_res, 1, task))
3948 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003949 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003950 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003951}
3952
3953static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3954{
3955 struct nfs4_lockdata *data = calldata;
3956
Harvey Harrison3110ff82008-05-02 13:42:44 -07003957 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003958
Trond Myklebustd61e6122009-12-05 19:32:19 -05003959 nfs4_sequence_done(data->server, &data->res.seq_res,
3960 task->tk_status);
Andy Adamson66179ef2009-04-01 09:22:22 -04003961
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003962 data->rpc_status = task->tk_status;
3963 if (RPC_ASSASSINATED(task))
3964 goto out;
3965 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003966 if (data->rpc_status == 0)
3967 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3968 else
3969 goto out;
3970 }
3971 if (data->rpc_status == 0) {
3972 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3973 sizeof(data->lsp->ls_stateid.data));
3974 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04003975 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003976 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003977out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07003978 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003979}
3980
3981static void nfs4_lock_release(void *calldata)
3982{
3983 struct nfs4_lockdata *data = calldata;
3984
Harvey Harrison3110ff82008-05-02 13:42:44 -07003985 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003986 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003987 if (data->cancelled != 0) {
3988 struct rpc_task *task;
3989 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3990 data->arg.lock_seqid);
3991 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003992 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003993 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003994 } else
3995 nfs_free_seqid(data->arg.lock_seqid);
3996 nfs4_put_lock_state(data->lsp);
3997 put_nfs_open_context(data->ctx);
3998 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003999 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004000}
4001
4002static const struct rpc_call_ops nfs4_lock_ops = {
4003 .rpc_call_prepare = nfs4_lock_prepare,
4004 .rpc_call_done = nfs4_lock_done,
4005 .rpc_release = nfs4_lock_release,
4006};
4007
4008static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
4009{
4010 struct nfs4_lockdata *data;
4011 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004012 struct rpc_message msg = {
4013 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4014 .rpc_cred = state->owner->so_cred,
4015 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004016 struct rpc_task_setup task_setup_data = {
4017 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004018 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004019 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004020 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004021 .flags = RPC_TASK_ASYNC,
4022 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004023 int ret;
4024
Harvey Harrison3110ff82008-05-02 13:42:44 -07004025 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004026 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004027 fl->fl_u.nfs4_fl.owner);
4028 if (data == NULL)
4029 return -ENOMEM;
4030 if (IS_SETLKW(cmd))
4031 data->arg.block = 1;
4032 if (reclaim != 0)
4033 data->arg.reclaim = 1;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004034 msg.rpc_argp = &data->arg,
4035 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004036 task_setup_data.callback_data = data;
4037 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004038 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004039 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004040 ret = nfs4_wait_for_completion_rpc_task(task);
4041 if (ret == 0) {
4042 ret = data->rpc_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004043 } else
4044 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004045 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004046 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004047 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048}
4049
4050static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4051{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004052 struct nfs_server *server = NFS_SERVER(state->inode);
4053 struct nfs4_exception exception = { };
4054 int err;
4055
4056 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004057 /* Cache the lock if possible... */
4058 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4059 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004060 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
4061 if (err != -NFS4ERR_DELAY)
4062 break;
4063 nfs4_handle_exception(server, err, &exception);
4064 } while (exception.retry);
4065 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066}
4067
4068static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4069{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004070 struct nfs_server *server = NFS_SERVER(state->inode);
4071 struct nfs4_exception exception = { };
4072 int err;
4073
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004074 err = nfs4_set_lock_state(state, request);
4075 if (err != 0)
4076 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004077 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004078 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4079 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004080 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004081 switch (err) {
4082 default:
4083 goto out;
4084 case -NFS4ERR_GRACE:
4085 case -NFS4ERR_DELAY:
4086 nfs4_handle_exception(server, err, &exception);
4087 err = 0;
4088 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004089 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004090out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004091 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092}
4093
4094static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4095{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004096 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004097 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 int status;
4099
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004100 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004101 status = nfs4_set_lock_state(state, request);
4102 if (status != 0)
4103 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004104 request->fl_flags |= FL_ACCESS;
4105 status = do_vfs_lock(request->fl_file, request);
4106 if (status < 0)
4107 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004108 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004109 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004110 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004111 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004112 request->fl_flags = fl_flags & ~FL_SLEEP;
4113 status = do_vfs_lock(request->fl_file, request);
4114 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004115 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004116 status = _nfs4_do_setlk(state, cmd, request, 0);
4117 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004118 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004119 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004120 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004121 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004122 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004123out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004124 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004125out:
4126 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 return status;
4128}
4129
4130static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4131{
4132 struct nfs4_exception exception = { };
4133 int err;
4134
4135 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004136 err = _nfs4_proc_setlk(state, cmd, request);
4137 if (err == -NFS4ERR_DENIED)
4138 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004140 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 } while (exception.retry);
4142 return err;
4143}
4144
4145static int
4146nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4147{
4148 struct nfs_open_context *ctx;
4149 struct nfs4_state *state;
4150 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4151 int status;
4152
4153 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004154 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 state = ctx->state;
4156
4157 if (request->fl_start < 0 || request->fl_end < 0)
4158 return -EINVAL;
4159
Trond Myklebustd9531262009-07-21 19:22:38 -04004160 if (IS_GETLK(cmd)) {
4161 if (state != NULL)
4162 return nfs4_proc_getlk(state, F_GETLK, request);
4163 return 0;
4164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165
4166 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4167 return -EINVAL;
4168
Trond Myklebustd9531262009-07-21 19:22:38 -04004169 if (request->fl_type == F_UNLCK) {
4170 if (state != NULL)
4171 return nfs4_proc_unlck(state, cmd, request);
4172 return 0;
4173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174
Trond Myklebustd9531262009-07-21 19:22:38 -04004175 if (state == NULL)
4176 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 do {
4178 status = nfs4_proc_setlk(state, cmd, request);
4179 if ((status != -EAGAIN) || IS_SETLK(cmd))
4180 break;
4181 timeout = nfs4_set_lock_task_retry(timeout);
4182 status = -ERESTARTSYS;
4183 if (signalled())
4184 break;
4185 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 return status;
4187}
4188
Trond Myklebust888e6942005-11-04 15:38:11 -05004189int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4190{
4191 struct nfs_server *server = NFS_SERVER(state->inode);
4192 struct nfs4_exception exception = { };
4193 int err;
4194
4195 err = nfs4_set_lock_state(state, fl);
4196 if (err != 0)
4197 goto out;
4198 do {
4199 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
Trond Myklebustd5122202009-06-17 13:22:58 -07004200 switch (err) {
4201 default:
4202 printk(KERN_ERR "%s: unhandled error %d.\n",
4203 __func__, err);
4204 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004205 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004206 goto out;
4207 case -NFS4ERR_EXPIRED:
4208 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004209 case -NFS4ERR_STALE_STATEID:
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004210 case -NFS4ERR_BADSESSION:
4211 case -NFS4ERR_BADSLOT:
4212 case -NFS4ERR_BAD_HIGH_SLOT:
4213 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4214 case -NFS4ERR_DEADSESSION:
Trond Myklebustd5122202009-06-17 13:22:58 -07004215 nfs4_schedule_state_recovery(server->nfs_client);
4216 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004217 case -ERESTARTSYS:
4218 /*
4219 * The show must go on: exit, but mark the
4220 * stateid as needing recovery.
4221 */
4222 case -NFS4ERR_ADMIN_REVOKED:
4223 case -NFS4ERR_BAD_STATEID:
4224 case -NFS4ERR_OPENMODE:
4225 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4226 err = 0;
4227 goto out;
4228 case -ENOMEM:
4229 case -NFS4ERR_DENIED:
4230 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4231 err = 0;
4232 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004233 case -NFS4ERR_DELAY:
4234 break;
4235 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004236 err = nfs4_handle_exception(server, err, &exception);
4237 } while (exception.retry);
4238out:
4239 return err;
4240}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004241
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004242#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4243
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004244int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4245 size_t buflen, int flags)
4246{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004247 struct inode *inode = dentry->d_inode;
4248
4249 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4250 return -EOPNOTSUPP;
4251
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004252 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004253}
4254
4255/* The getxattr man page suggests returning -ENODATA for unknown attributes,
4256 * and that's what we'll do for e.g. user attributes that haven't been set.
4257 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4258 * attributes in kernel-managed attribute namespaces. */
4259ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4260 size_t buflen)
4261{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004262 struct inode *inode = dentry->d_inode;
4263
4264 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4265 return -EOPNOTSUPP;
4266
4267 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004268}
4269
4270ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4271{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004272 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004273
J. Bruce Fields096455a2006-03-20 23:23:42 -05004274 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4275 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004276 if (buf && buflen < len)
4277 return -ERANGE;
4278 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004279 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4280 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004281}
4282
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004283static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4284{
4285 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4286 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4287 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4288 return;
4289
4290 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4291 NFS_ATTR_FATTR_NLINK;
4292 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4293 fattr->nlink = 2;
4294}
4295
Trond Myklebust56659e92007-07-17 21:52:39 -04004296int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004297 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004298{
4299 struct nfs_server *server = NFS_SERVER(dir);
4300 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004301 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4302 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004303 };
4304 struct nfs4_fs_locations_arg args = {
4305 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004306 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004307 .page = page,
4308 .bitmask = bitmask,
4309 };
Benny Halevy22958462009-04-01 09:22:02 -04004310 struct nfs4_fs_locations_res res = {
4311 .fs_locations = fs_locations,
4312 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004313 struct rpc_message msg = {
4314 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4315 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004316 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004317 };
4318 int status;
4319
Harvey Harrison3110ff82008-05-02 13:42:44 -07004320 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004321 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004322 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004323 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04004324 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004325 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004326 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004327 return status;
4328}
4329
Andy Adamson557134a2009-04-01 09:21:53 -04004330#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004331/*
4332 * nfs4_proc_exchange_id()
4333 *
4334 * Since the clientid has expired, all compounds using sessions
4335 * associated with the stale clientid will be returning
4336 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4337 * be in some phase of session reset.
4338 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004339int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004340{
4341 nfs4_verifier verifier;
4342 struct nfs41_exchange_id_args args = {
4343 .client = clp,
4344 .flags = clp->cl_exchange_flags,
4345 };
4346 struct nfs41_exchange_id_res res = {
4347 .client = clp,
4348 };
4349 int status;
4350 struct rpc_message msg = {
4351 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4352 .rpc_argp = &args,
4353 .rpc_resp = &res,
4354 .rpc_cred = cred,
4355 };
4356 __be32 *p;
4357
4358 dprintk("--> %s\n", __func__);
4359 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004360
Alexandros Batsakis7b183d02009-12-05 13:33:25 -05004361 /* Remove server-only flags */
4362 args.flags &= ~EXCHGID4_FLAG_CONFIRMED_R;
4363
Benny Halevy99fe60d2009-04-01 09:22:29 -04004364 p = (u32 *)verifier.data;
4365 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4366 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4367 args.verifier = &verifier;
4368
4369 while (1) {
4370 args.id_len = scnprintf(args.id, sizeof(args.id),
4371 "%s/%s %u",
4372 clp->cl_ipaddr,
4373 rpc_peeraddr2str(clp->cl_rpcclient,
4374 RPC_DISPLAY_ADDR),
4375 clp->cl_id_uniquifier);
4376
4377 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4378
4379 if (status != NFS4ERR_CLID_INUSE)
4380 break;
4381
4382 if (signalled())
4383 break;
4384
4385 if (++clp->cl_id_uniquifier == 0)
4386 break;
4387 }
4388
4389 dprintk("<-- %s status= %d\n", __func__, status);
4390 return status;
4391}
4392
Andy Adamson2050f0c2009-04-01 09:22:30 -04004393struct nfs4_get_lease_time_data {
4394 struct nfs4_get_lease_time_args *args;
4395 struct nfs4_get_lease_time_res *res;
4396 struct nfs_client *clp;
4397};
4398
4399static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4400 void *calldata)
4401{
4402 int ret;
4403 struct nfs4_get_lease_time_data *data =
4404 (struct nfs4_get_lease_time_data *)calldata;
4405
4406 dprintk("--> %s\n", __func__);
4407 /* just setup sequence, do not trigger session recovery
4408 since we're invoked within one */
4409 ret = nfs41_setup_sequence(data->clp->cl_session,
4410 &data->args->la_seq_args,
4411 &data->res->lr_seq_res, 0, task);
4412
4413 BUG_ON(ret == -EAGAIN);
4414 rpc_call_start(task);
4415 dprintk("<-- %s\n", __func__);
4416}
4417
4418/*
4419 * Called from nfs4_state_manager thread for session setup, so don't recover
4420 * from sequence operation or clientid errors.
4421 */
4422static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4423{
4424 struct nfs4_get_lease_time_data *data =
4425 (struct nfs4_get_lease_time_data *)calldata;
4426
4427 dprintk("--> %s\n", __func__);
4428 nfs41_sequence_done(data->clp, &data->res->lr_seq_res, task->tk_status);
4429 switch (task->tk_status) {
4430 case -NFS4ERR_DELAY:
4431 case -NFS4ERR_GRACE:
4432 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4433 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4434 task->tk_status = 0;
Trond Myklebust0110ee12009-12-07 09:00:24 -05004435 nfs_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004436 return;
4437 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004438 dprintk("<-- %s\n", __func__);
4439}
4440
4441struct rpc_call_ops nfs4_get_lease_time_ops = {
4442 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4443 .rpc_call_done = nfs4_get_lease_time_done,
4444};
4445
4446int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4447{
4448 struct rpc_task *task;
4449 struct nfs4_get_lease_time_args args;
4450 struct nfs4_get_lease_time_res res = {
4451 .lr_fsinfo = fsinfo,
4452 };
4453 struct nfs4_get_lease_time_data data = {
4454 .args = &args,
4455 .res = &res,
4456 .clp = clp,
4457 };
4458 struct rpc_message msg = {
4459 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4460 .rpc_argp = &args,
4461 .rpc_resp = &res,
4462 };
4463 struct rpc_task_setup task_setup = {
4464 .rpc_client = clp->cl_rpcclient,
4465 .rpc_message = &msg,
4466 .callback_ops = &nfs4_get_lease_time_ops,
4467 .callback_data = &data
4468 };
4469 int status;
4470
4471 res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
4472 dprintk("--> %s\n", __func__);
4473 task = rpc_run_task(&task_setup);
4474
4475 if (IS_ERR(task))
4476 status = PTR_ERR(task);
4477 else {
4478 status = task->tk_status;
4479 rpc_put_task(task);
4480 }
4481 dprintk("<-- %s return %d\n", __func__, status);
4482
4483 return status;
4484}
4485
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004486/*
4487 * Reset a slot table
4488 */
4489static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, int max_slots,
4490 int old_max_slots, int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004491{
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004492 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004493 int ret = 0;
4494
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004495 dprintk("--> %s: max_reqs=%u, tbl %p\n", __func__, max_slots, tbl);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004496
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004497 /*
4498 * Until we have dynamic slot table adjustment, insist
4499 * upon the same slot table size
4500 */
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004501 if (max_slots != old_max_slots) {
4502 dprintk("%s reset slot table does't match old\n",
4503 __func__);
4504 ret = -EINVAL; /*XXX NFS4ERR_REQ_TOO_BIG ? */
4505 goto out;
4506 }
4507 spin_lock(&tbl->slot_tbl_lock);
4508 for (i = 0; i < max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004509 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004510 spin_unlock(&tbl->slot_tbl_lock);
4511 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4512 tbl, tbl->slots, tbl->max_slots);
4513out:
4514 dprintk("<-- %s: return %d\n", __func__, ret);
4515 return ret;
4516}
4517
Andy Adamsonfc931582009-04-01 09:22:31 -04004518/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004519 * Reset the forechannel and backchannel slot tables
4520 */
4521static int nfs4_reset_slot_tables(struct nfs4_session *session)
4522{
4523 int status;
4524
4525 status = nfs4_reset_slot_table(&session->fc_slot_table,
4526 session->fc_attrs.max_reqs,
4527 session->fc_slot_table.max_slots,
4528 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004529 if (status)
4530 return status;
4531
4532 status = nfs4_reset_slot_table(&session->bc_slot_table,
4533 session->bc_attrs.max_reqs,
4534 session->bc_slot_table.max_slots,
4535 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004536 return status;
4537}
4538
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004539/* Destroy the slot table */
4540static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4541{
4542 if (session->fc_slot_table.slots != NULL) {
4543 kfree(session->fc_slot_table.slots);
4544 session->fc_slot_table.slots = NULL;
4545 }
4546 if (session->bc_slot_table.slots != NULL) {
4547 kfree(session->bc_slot_table.slots);
4548 session->bc_slot_table.slots = NULL;
4549 }
4550 return;
4551}
4552
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004553/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004554 * Initialize slot table
4555 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004556static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4557 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04004558{
Andy Adamsonfc931582009-04-01 09:22:31 -04004559 struct nfs4_slot *slot;
4560 int ret = -ENOMEM;
4561
4562 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4563
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004564 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04004565
4566 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_KERNEL);
4567 if (!slot)
4568 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04004569 ret = 0;
4570
4571 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04004572 tbl->max_slots = max_slots;
4573 tbl->slots = slot;
4574 tbl->highest_used_slotid = -1; /* no slot is currently used */
4575 spin_unlock(&tbl->slot_tbl_lock);
4576 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4577 tbl, tbl->slots, tbl->max_slots);
4578out:
4579 dprintk("<-- %s: return %d\n", __func__, ret);
4580 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04004581}
4582
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004583/*
4584 * Initialize the forechannel and backchannel tables
4585 */
4586static int nfs4_init_slot_tables(struct nfs4_session *session)
4587{
Trond Myklebustf26468f2009-12-05 19:32:11 -05004588 struct nfs4_slot_table *tbl;
4589 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004590
Trond Myklebustf26468f2009-12-05 19:32:11 -05004591 tbl = &session->fc_slot_table;
4592 if (tbl->slots == NULL) {
4593 status = nfs4_init_slot_table(tbl,
4594 session->fc_attrs.max_reqs, 1);
4595 if (status)
4596 return status;
4597 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004598
Trond Myklebustf26468f2009-12-05 19:32:11 -05004599 tbl = &session->bc_slot_table;
4600 if (tbl->slots == NULL) {
4601 status = nfs4_init_slot_table(tbl,
4602 session->bc_attrs.max_reqs, 0);
4603 if (status)
4604 nfs4_destroy_slot_tables(session);
4605 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004606
4607 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04004608}
4609
4610struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4611{
4612 struct nfs4_session *session;
4613 struct nfs4_slot_table *tbl;
4614
4615 session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
4616 if (!session)
4617 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04004618
Andy Adamson76db6d92009-04-01 09:22:38 -04004619 /*
4620 * The create session reply races with the server back
4621 * channel probe. Mark the client NFS_CS_SESSION_INITING
4622 * so that the client back channel can find the
4623 * nfs_client struct
4624 */
4625 clp->cl_cons_state = NFS_CS_SESSION_INITING;
Andy Adamsonea028ac2009-12-04 15:55:38 -05004626 init_completion(&session->complete);
Andy Adamson76db6d92009-04-01 09:22:38 -04004627
Andy Adamson557134a2009-04-01 09:21:53 -04004628 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004629 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04004630 spin_lock_init(&tbl->slot_tbl_lock);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004631 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
4632
4633 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004634 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004635 spin_lock_init(&tbl->slot_tbl_lock);
4636 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4637
Andy Adamson557134a2009-04-01 09:21:53 -04004638 session->clp = clp;
4639 return session;
4640}
4641
4642void nfs4_destroy_session(struct nfs4_session *session)
4643{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04004644 nfs4_proc_destroy_session(session);
4645 dprintk("%s Destroy backchannel for xprt %p\n",
4646 __func__, session->clp->cl_rpcclient->cl_xprt);
4647 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4648 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004649 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04004650 kfree(session);
4651}
4652
Andy Adamsonfc931582009-04-01 09:22:31 -04004653/*
4654 * Initialize the values to be used by the client in CREATE_SESSION
4655 * If nfs4_init_session set the fore channel request and response sizes,
4656 * use them.
4657 *
4658 * Set the back channel max_resp_sz_cached to zero to force the client to
4659 * always set csa_cachethis to FALSE because the current implementation
4660 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4661 */
4662static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4663{
4664 struct nfs4_session *session = args->client->cl_session;
4665 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4666 mxresp_sz = session->fc_attrs.max_resp_sz;
4667
4668 if (mxrqst_sz == 0)
4669 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4670 if (mxresp_sz == 0)
4671 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4672 /* Fore channel attributes */
4673 args->fc_attrs.headerpadsz = 0;
4674 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4675 args->fc_attrs.max_resp_sz = mxresp_sz;
4676 args->fc_attrs.max_resp_sz_cached = mxresp_sz;
4677 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4678 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4679
4680 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
4681 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4682 __func__,
4683 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
4684 args->fc_attrs.max_resp_sz_cached, args->fc_attrs.max_ops,
4685 args->fc_attrs.max_reqs);
4686
4687 /* Back channel attributes */
4688 args->bc_attrs.headerpadsz = 0;
4689 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4690 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4691 args->bc_attrs.max_resp_sz_cached = 0;
4692 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4693 args->bc_attrs.max_reqs = 1;
4694
4695 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4696 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4697 __func__,
4698 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4699 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4700 args->bc_attrs.max_reqs);
4701}
4702
Andy Adamson8d353012009-04-01 09:22:32 -04004703static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
4704{
4705 if (rcvd <= sent)
4706 return 0;
4707 printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
4708 "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
4709 return -EINVAL;
4710}
4711
4712#define _verify_fore_channel_attr(_name_) \
4713 _verify_channel_attr("fore", #_name_, \
4714 args->fc_attrs._name_, \
4715 session->fc_attrs._name_)
4716
4717#define _verify_back_channel_attr(_name_) \
4718 _verify_channel_attr("back", #_name_, \
4719 args->bc_attrs._name_, \
4720 session->bc_attrs._name_)
4721
4722/*
4723 * The server is not allowed to increase the fore channel header pad size,
4724 * maximum response size, or maximum number of operations.
4725 *
4726 * The back channel attributes are only negotiatied down: We send what the
4727 * (back channel) server insists upon.
4728 */
4729static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4730 struct nfs4_session *session)
4731{
4732 int ret = 0;
4733
4734 ret |= _verify_fore_channel_attr(headerpadsz);
4735 ret |= _verify_fore_channel_attr(max_resp_sz);
4736 ret |= _verify_fore_channel_attr(max_ops);
4737
4738 ret |= _verify_back_channel_attr(headerpadsz);
4739 ret |= _verify_back_channel_attr(max_rqst_sz);
4740 ret |= _verify_back_channel_attr(max_resp_sz);
4741 ret |= _verify_back_channel_attr(max_resp_sz_cached);
4742 ret |= _verify_back_channel_attr(max_ops);
4743 ret |= _verify_back_channel_attr(max_reqs);
4744
4745 return ret;
4746}
4747
Andy Adamsonfc931582009-04-01 09:22:31 -04004748static int _nfs4_proc_create_session(struct nfs_client *clp)
4749{
4750 struct nfs4_session *session = clp->cl_session;
4751 struct nfs41_create_session_args args = {
4752 .client = clp,
4753 .cb_program = NFS4_CALLBACK,
4754 };
4755 struct nfs41_create_session_res res = {
4756 .client = clp,
4757 };
4758 struct rpc_message msg = {
4759 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4760 .rpc_argp = &args,
4761 .rpc_resp = &res,
4762 };
4763 int status;
4764
4765 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04004766 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04004767
4768 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4769
Andy Adamson8d353012009-04-01 09:22:32 -04004770 if (!status)
4771 /* Verify the session's negotiated channel_attrs values */
4772 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04004773 if (!status) {
4774 /* Increment the clientid slot sequence id */
4775 clp->cl_seqid++;
4776 }
4777
4778 return status;
4779}
4780
4781/*
4782 * Issues a CREATE_SESSION operation to the server.
4783 * It is the responsibility of the caller to verify the session is
4784 * expired before calling this routine.
4785 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05004786int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04004787{
4788 int status;
4789 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04004790 struct nfs4_session *session = clp->cl_session;
4791
4792 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
4793
4794 status = _nfs4_proc_create_session(clp);
4795 if (status)
4796 goto out;
4797
Trond Myklebustf26468f2009-12-05 19:32:11 -05004798 /* Init and reset the fore channel */
4799 status = nfs4_init_slot_tables(session);
4800 dprintk("slot table initialization returned %d\n", status);
4801 if (status)
4802 goto out;
4803 status = nfs4_reset_slot_tables(session);
4804 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04004805 if (status)
4806 goto out;
4807
4808 ptr = (unsigned *)&session->sess_id.data[0];
4809 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
4810 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04004811out:
4812 dprintk("<-- %s\n", __func__);
4813 return status;
4814}
4815
Andy Adamson0f3e66c2009-04-01 09:22:34 -04004816/*
4817 * Issue the over-the-wire RPC DESTROY_SESSION.
4818 * The caller must serialize access to this routine.
4819 */
4820int nfs4_proc_destroy_session(struct nfs4_session *session)
4821{
4822 int status = 0;
4823 struct rpc_message msg;
4824
4825 dprintk("--> nfs4_proc_destroy_session\n");
4826
4827 /* session is still being setup */
4828 if (session->clp->cl_cons_state != NFS_CS_READY)
4829 return status;
4830
4831 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
4832 msg.rpc_argp = session;
4833 msg.rpc_resp = NULL;
4834 msg.rpc_cred = NULL;
4835 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4836
4837 if (status)
4838 printk(KERN_WARNING
4839 "Got error %d from the server on DESTROY_SESSION. "
4840 "Session has been destroyed regardless...\n", status);
4841
4842 dprintk("<-- nfs4_proc_destroy_session\n");
4843 return status;
4844}
4845
Trond Myklebustfccba802009-07-21 16:48:07 -04004846int nfs4_init_session(struct nfs_server *server)
4847{
4848 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05004849 struct nfs4_session *session;
Trond Myklebustfccba802009-07-21 16:48:07 -04004850 int ret;
4851
4852 if (!nfs4_has_session(clp))
4853 return 0;
4854
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05004855 session = clp->cl_session;
4856 session->fc_attrs.max_rqst_sz = server->wsize + nfs41_maxwrite_overhead;
4857 session->fc_attrs.max_resp_sz = server->rsize + nfs41_maxread_overhead;
4858
Trond Myklebustfccba802009-07-21 16:48:07 -04004859 ret = nfs4_recover_expired_lease(server);
4860 if (!ret)
4861 ret = nfs4_check_client_ready(clp);
4862 return ret;
4863}
4864
Andy Adamsonfc01cea2009-04-01 09:22:36 -04004865/*
4866 * Renew the cl_session lease.
4867 */
4868static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
4869{
4870 struct nfs4_sequence_args args;
4871 struct nfs4_sequence_res res;
4872
4873 struct rpc_message msg = {
4874 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
4875 .rpc_argp = &args,
4876 .rpc_resp = &res,
4877 .rpc_cred = cred,
4878 };
4879
4880 args.sa_cache_this = 0;
4881
4882 return nfs4_call_sync_sequence(clp, clp->cl_rpcclient, &msg, &args,
4883 &res, 0);
4884}
4885
4886void nfs41_sequence_call_done(struct rpc_task *task, void *data)
4887{
4888 struct nfs_client *clp = (struct nfs_client *)data;
4889
4890 nfs41_sequence_done(clp, task->tk_msg.rpc_resp, task->tk_status);
4891
4892 if (task->tk_status < 0) {
4893 dprintk("%s ERROR %d\n", __func__, task->tk_status);
4894
4895 if (_nfs4_async_handle_error(task, NULL, clp, NULL)
4896 == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05004897 nfs_restart_rpc(task, clp);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04004898 return;
4899 }
4900 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04004901 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
4902
Andy Adamsonfc01cea2009-04-01 09:22:36 -04004903 kfree(task->tk_msg.rpc_argp);
4904 kfree(task->tk_msg.rpc_resp);
4905
4906 dprintk("<-- %s\n", __func__);
4907}
4908
4909static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
4910{
4911 struct nfs_client *clp;
4912 struct nfs4_sequence_args *args;
4913 struct nfs4_sequence_res *res;
4914
4915 clp = (struct nfs_client *)data;
4916 args = task->tk_msg.rpc_argp;
4917 res = task->tk_msg.rpc_resp;
4918
4919 if (nfs4_setup_sequence(clp, args, res, 0, task))
4920 return;
4921 rpc_call_start(task);
4922}
4923
4924static const struct rpc_call_ops nfs41_sequence_ops = {
4925 .rpc_call_done = nfs41_sequence_call_done,
4926 .rpc_call_prepare = nfs41_sequence_prepare,
4927};
4928
4929static int nfs41_proc_async_sequence(struct nfs_client *clp,
4930 struct rpc_cred *cred)
4931{
4932 struct nfs4_sequence_args *args;
4933 struct nfs4_sequence_res *res;
4934 struct rpc_message msg = {
4935 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
4936 .rpc_cred = cred,
4937 };
4938
4939 args = kzalloc(sizeof(*args), GFP_KERNEL);
4940 if (!args)
4941 return -ENOMEM;
4942 res = kzalloc(sizeof(*res), GFP_KERNEL);
4943 if (!res) {
4944 kfree(args);
4945 return -ENOMEM;
4946 }
4947 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
4948 msg.rpc_argp = args;
4949 msg.rpc_resp = res;
4950
4951 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
4952 &nfs41_sequence_ops, (void *)clp);
4953}
4954
Ricardo Labiagafce5c832009-12-05 16:08:41 -05004955struct nfs4_reclaim_complete_data {
4956 struct nfs_client *clp;
4957 struct nfs41_reclaim_complete_args arg;
4958 struct nfs41_reclaim_complete_res res;
4959};
4960
4961static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
4962{
4963 struct nfs4_reclaim_complete_data *calldata = data;
4964
4965 if (nfs4_setup_sequence(calldata->clp, &calldata->arg.seq_args,
4966 &calldata->res.seq_res, 0, task))
4967 return;
4968
4969 rpc_call_start(task);
4970}
4971
4972static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
4973{
4974 struct nfs4_reclaim_complete_data *calldata = data;
4975 struct nfs_client *clp = calldata->clp;
4976 struct nfs4_sequence_res *res = &calldata->res.seq_res;
4977
4978 dprintk("--> %s\n", __func__);
4979 nfs41_sequence_done(clp, res, task->tk_status);
4980 switch (task->tk_status) {
4981 case 0:
4982 case -NFS4ERR_COMPLETE_ALREADY:
4983 break;
4984 case -NFS4ERR_BADSESSION:
4985 case -NFS4ERR_DEADSESSION:
4986 /*
4987 * Handle the session error, but do not retry the operation, as
4988 * we have no way of telling whether the clientid had to be
4989 * reset before we got our reply. If reset, a new wave of
4990 * reclaim operations will follow, containing their own reclaim
4991 * complete. We don't want our retry to get on the way of
4992 * recovery by incorrectly indicating to the server that we're
4993 * done reclaiming state since the process had to be restarted.
4994 */
4995 _nfs4_async_handle_error(task, NULL, clp, NULL);
4996 break;
4997 default:
4998 if (_nfs4_async_handle_error(
4999 task, NULL, clp, NULL) == -EAGAIN) {
5000 rpc_restart_call_prepare(task);
5001 return;
5002 }
5003 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005004
5005 dprintk("<-- %s\n", __func__);
5006}
5007
5008static void nfs4_free_reclaim_complete_data(void *data)
5009{
5010 struct nfs4_reclaim_complete_data *calldata = data;
5011
5012 kfree(calldata);
5013}
5014
5015static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5016 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5017 .rpc_call_done = nfs4_reclaim_complete_done,
5018 .rpc_release = nfs4_free_reclaim_complete_data,
5019};
5020
5021/*
5022 * Issue a global reclaim complete.
5023 */
5024static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5025{
5026 struct nfs4_reclaim_complete_data *calldata;
5027 struct rpc_task *task;
5028 struct rpc_message msg = {
5029 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5030 };
5031 struct rpc_task_setup task_setup_data = {
5032 .rpc_client = clp->cl_rpcclient,
5033 .rpc_message = &msg,
5034 .callback_ops = &nfs4_reclaim_complete_call_ops,
5035 .flags = RPC_TASK_ASYNC,
5036 };
5037 int status = -ENOMEM;
5038
5039 dprintk("--> %s\n", __func__);
5040 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
5041 if (calldata == NULL)
5042 goto out;
5043 calldata->clp = clp;
5044 calldata->arg.one_fs = 0;
5045 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
5046
5047 msg.rpc_argp = &calldata->arg;
5048 msg.rpc_resp = &calldata->res;
5049 task_setup_data.callback_data = calldata;
5050 task = rpc_run_task(&task_setup_data);
5051 if (IS_ERR(task))
5052 status = PTR_ERR(task);
5053 rpc_put_task(task);
5054out:
5055 dprintk("<-- %s status=%d\n", __func__, status);
5056 return status;
5057}
Andy Adamson557134a2009-04-01 09:21:53 -04005058#endif /* CONFIG_NFS_V4_1 */
5059
Andy Adamson591d71c2009-04-01 09:22:47 -04005060struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005061 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005062 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 .recover_open = nfs4_open_reclaim,
5064 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04005065 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005066 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067};
5068
Andy Adamson591d71c2009-04-01 09:22:47 -04005069#if defined(CONFIG_NFS_V4_1)
5070struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5071 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5072 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5073 .recover_open = nfs4_open_reclaim,
5074 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05005075 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005076 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005077 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04005078};
5079#endif /* CONFIG_NFS_V4_1 */
5080
5081struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005082 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005083 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 .recover_open = nfs4_open_expired,
5085 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04005086 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005087 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088};
5089
Andy Adamson591d71c2009-04-01 09:22:47 -04005090#if defined(CONFIG_NFS_V4_1)
5091struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5092 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5093 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5094 .recover_open = nfs4_open_expired,
5095 .recover_lock = nfs4_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05005096 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005097 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04005098};
5099#endif /* CONFIG_NFS_V4_1 */
5100
Benny Halevy29fba382009-04-01 09:22:44 -04005101struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5102 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04005103 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005104 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04005105};
5106
5107#if defined(CONFIG_NFS_V4_1)
5108struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5109 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04005110 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005111 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04005112};
5113#endif
5114
5115/*
5116 * Per minor version reboot and network partition recovery ops
5117 */
5118
Andy Adamson591d71c2009-04-01 09:22:47 -04005119struct nfs4_state_recovery_ops *nfs4_reboot_recovery_ops[] = {
5120 &nfs40_reboot_recovery_ops,
5121#if defined(CONFIG_NFS_V4_1)
5122 &nfs41_reboot_recovery_ops,
5123#endif
5124};
5125
5126struct nfs4_state_recovery_ops *nfs4_nograce_recovery_ops[] = {
5127 &nfs40_nograce_recovery_ops,
5128#if defined(CONFIG_NFS_V4_1)
5129 &nfs41_nograce_recovery_ops,
5130#endif
5131};
5132
Benny Halevy29fba382009-04-01 09:22:44 -04005133struct nfs4_state_maintenance_ops *nfs4_state_renewal_ops[] = {
5134 &nfs40_state_renewal_ops,
5135#if defined(CONFIG_NFS_V4_1)
5136 &nfs41_state_renewal_ops,
5137#endif
5138};
5139
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005140static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005141 .permission = nfs_permission,
5142 .getattr = nfs_getattr,
5143 .setattr = nfs_setattr,
5144 .getxattr = nfs4_getxattr,
5145 .setxattr = nfs4_setxattr,
5146 .listxattr = nfs4_listxattr,
5147};
5148
David Howells509de812006-08-22 20:06:11 -04005149const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 .version = 4, /* protocol version */
5151 .dentry_ops = &nfs4_dentry_operations,
5152 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005153 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 .getroot = nfs4_proc_get_root,
5155 .getattr = nfs4_proc_getattr,
5156 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04005157 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 .lookup = nfs4_proc_lookup,
5159 .access = nfs4_proc_access,
5160 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 .create = nfs4_proc_create,
5162 .remove = nfs4_proc_remove,
5163 .unlink_setup = nfs4_proc_unlink_setup,
5164 .unlink_done = nfs4_proc_unlink_done,
5165 .rename = nfs4_proc_rename,
5166 .link = nfs4_proc_link,
5167 .symlink = nfs4_proc_symlink,
5168 .mkdir = nfs4_proc_mkdir,
5169 .rmdir = nfs4_proc_remove,
5170 .readdir = nfs4_proc_readdir,
5171 .mknod = nfs4_proc_mknod,
5172 .statfs = nfs4_proc_statfs,
5173 .fsinfo = nfs4_proc_fsinfo,
5174 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04005175 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 .decode_dirent = nfs4_decode_dirent,
5177 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05005178 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005180 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005182 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005184 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04005185 .close_context = nfs4_close_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186};
5187
5188/*
5189 * Local variables:
5190 * c-basic-offset: 8
5191 * End:
5192 */