blob: d44f1071930f21c3d98b6d54bf03aad14ceee19e [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 Myklebust56ae19f2005-10-27 22:12:40 -04001439 if (o_arg->open_flags & O_CREAT) {
1440 update_changeattr(dir, &o_res->cinfo);
1441 nfs_post_op_update_inode(dir, o_res->dir_attr);
1442 } else
1443 nfs_refresh_inode(dir, o_res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001445 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001447 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 }
1449 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001450 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001451 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452}
1453
Trond Myklebust10afec902007-05-14 17:16:04 -04001454static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +01001455{
David Howells7539bba2006-08-22 20:06:09 -04001456 struct nfs_client *clp = server->nfs_client;
Trond Myklebusta78cb572009-08-09 15:06:19 -04001457 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001458 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001459
Trond Myklebusta78cb572009-08-09 15:06:19 -04001460 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001461 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001462 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001463 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001464 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1465 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001466 break;
Trond Myklebust58d97142006-01-03 09:55:24 +01001467 nfs4_schedule_state_recovery(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001468 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001469 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001470 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001471}
1472
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473/*
1474 * OPEN_EXPIRED:
1475 * reclaim state on the server after a network partition.
1476 * Assumes caller holds the appropriate lock
1477 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001478static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001480 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001481 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001483 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1484 if (IS_ERR(opendata))
1485 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001486 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001487 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001488 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001489 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001490 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491}
1492
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001493static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001494{
Trond Myklebust539cd032007-06-05 11:46:42 -04001495 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001496 struct nfs4_exception exception = { };
1497 int err;
1498
1499 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001500 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001501 switch (err) {
1502 default:
1503 goto out;
1504 case -NFS4ERR_GRACE:
1505 case -NFS4ERR_DELAY:
1506 nfs4_handle_exception(server, err, &exception);
1507 err = 0;
1508 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001509 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001510out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001511 return err;
1512}
1513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1515{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001517 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518
Trond Myklebust864472e2006-01-03 09:55:15 +01001519 ctx = nfs4_state_find_open_context(state);
1520 if (IS_ERR(ctx))
1521 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001522 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001523 put_nfs_open_context(ctx);
1524 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525}
1526
1527/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001528 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1529 * fields corresponding to attributes that were used to store the verifier.
1530 * Make sure we clobber those fields in the later setattr call
1531 */
1532static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1533{
1534 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1535 !(sattr->ia_valid & ATTR_ATIME_SET))
1536 sattr->ia_valid |= ATTR_ATIME;
1537
1538 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1539 !(sattr->ia_valid & ATTR_MTIME_SET))
1540 sattr->ia_valid |= ATTR_MTIME;
1541}
1542
1543/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001544 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001546static 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 -07001547{
1548 struct nfs4_state_owner *sp;
1549 struct nfs4_state *state = NULL;
1550 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001551 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001552 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
1554 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 status = -ENOMEM;
1556 if (!(sp = nfs4_get_state_owner(server, cred))) {
1557 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1558 goto out_err;
1559 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001560 status = nfs4_recover_expired_lease(server);
1561 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001562 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001563 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001564 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001565 status = -ENOMEM;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001566 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001567 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001568 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Trond Myklebustaac00a82007-07-05 19:02:21 -04001570 if (path->dentry->d_inode != NULL)
1571 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1572
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001573 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001575 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001577 if (opendata->o_arg.open_flags & O_EXCL)
1578 nfs4_exclusive_attrset(opendata, sattr);
1579
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001580 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001581 status = PTR_ERR(state);
1582 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001583 goto err_opendata_put;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001584 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 *res = state;
1587 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001588err_opendata_put:
1589 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001590err_put_state_owner:
1591 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 *res = NULL;
1594 return status;
1595}
1596
1597
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001598static 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 -07001599{
1600 struct nfs4_exception exception = { };
1601 struct nfs4_state *res;
1602 int status;
1603
1604 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001605 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 if (status == 0)
1607 break;
1608 /* NOTE: BAD_SEQID means the server and client disagree about the
1609 * book-keeping w.r.t. state-changing operations
1610 * (OPEN/CLOSE/LOCK/LOCKU...)
1611 * It is actually a sign of a bug on the client or on the server.
1612 *
1613 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001614 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 * have unhashed the old state_owner for us, and that we can
1616 * therefore safely retry using a new one. We should still warn
1617 * the user though...
1618 */
1619 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001620 printk(KERN_WARNING "NFS: v4 server %s "
1621 " returned a bad sequence-id error!\n",
1622 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 exception.retry = 1;
1624 continue;
1625 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001626 /*
1627 * BAD_STATEID on OPEN means that the server cancelled our
1628 * state before it received the OPEN_CONFIRM.
1629 * Recover by retrying the request as per the discussion
1630 * on Page 181 of RFC3530.
1631 */
1632 if (status == -NFS4ERR_BAD_STATEID) {
1633 exception.retry = 1;
1634 continue;
1635 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001636 if (status == -EAGAIN) {
1637 /* We must have found a delegation */
1638 exception.retry = 1;
1639 continue;
1640 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1642 status, &exception));
1643 } while (exception.retry);
1644 return res;
1645}
1646
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001647static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1648 struct nfs_fattr *fattr, struct iattr *sattr,
1649 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001651 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001653 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 .iap = sattr,
1655 .server = server,
1656 .bitmask = server->attr_bitmask,
1657 };
1658 struct nfs_setattrres res = {
1659 .fattr = fattr,
1660 .server = server,
1661 };
1662 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001663 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1664 .rpc_argp = &arg,
1665 .rpc_resp = &res,
1666 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001668 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001669 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
Trond Myklebust0e574af2005-10-27 22:12:38 -04001671 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001673 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1674 /* Use that stateid */
1675 } else if (state != NULL) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001676 nfs4_copy_stateid(&arg.stateid, state, current->files);
1677 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1679
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001680 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001681 if (status == 0 && state != NULL)
1682 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001683 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684}
1685
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001686static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1687 struct nfs_fattr *fattr, struct iattr *sattr,
1688 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001690 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 struct nfs4_exception exception = { };
1692 int err;
1693 do {
1694 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001695 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 &exception);
1697 } while (exception.retry);
1698 return err;
1699}
1700
1701struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001702 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 struct inode *inode;
1704 struct nfs4_state *state;
1705 struct nfs_closeargs arg;
1706 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001707 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001708 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709};
1710
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001711static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001712{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001713 struct nfs4_closedata *calldata = data;
1714 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001715
1716 nfs4_put_open_state(calldata->state);
1717 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001718 nfs4_put_state_owner(sp);
Jan Blunck31f31db12008-05-02 13:42:45 -07001719 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001720 kfree(calldata);
1721}
1722
Trond Myklebust88069f72009-12-08 08:33:16 -05001723static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1724 fmode_t fmode)
1725{
1726 spin_lock(&state->owner->so_lock);
1727 if (!(fmode & FMODE_READ))
1728 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1729 if (!(fmode & FMODE_WRITE))
1730 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1731 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1732 spin_unlock(&state->owner->so_lock);
1733}
1734
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001735static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001737 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 struct nfs_server *server = NFS_SERVER(calldata->inode);
1740
Andy Adamson19ddab02009-04-01 09:22:20 -04001741 nfs4_sequence_done(server, &calldata->res.seq_res, task->tk_status);
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001742 if (RPC_ASSASSINATED(task))
1743 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 /* hmm. we are done with the inode, and in the process of freeing
1745 * the state_owner. we keep this around to process errors
1746 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 switch (task->tk_status) {
1748 case 0:
Trond Myklebust45328c32007-07-26 17:47:34 -04001749 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001750 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001751 nfs4_close_clear_stateid_flags(state,
1752 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 break;
1754 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001755 case -NFS4ERR_OLD_STATEID:
1756 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001758 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001759 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001761 if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05001762 nfs_restart_rpc(task, server->nfs_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 return;
1764 }
1765 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001766 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767}
1768
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001769static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001771 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001772 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001773 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001774
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001775 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001776 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001777
Trond Myklebust88069f72009-12-08 08:33:16 -05001778 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1779 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001780 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001781 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001782 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001783 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001784 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1785 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1786 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001787 }
1788 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001789 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1790 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1791 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001792 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001793 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001794 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05001795
1796 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001797 /* Note: exit _without_ calling nfs4_close_done */
1798 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001799 return;
1800 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001801
1802 if (calldata->arg.fmode == 0)
1803 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
1804
Trond Myklebust516a6af2005-10-27 22:12:41 -04001805 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001806 calldata->timestamp = jiffies;
Andy Adamson19ddab02009-04-01 09:22:20 -04001807 if (nfs4_setup_sequence((NFS_SERVER(calldata->inode))->nfs_client,
1808 &calldata->arg.seq_args, &calldata->res.seq_res,
1809 1, task))
1810 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001811 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812}
1813
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001814static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001815 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001816 .rpc_call_done = nfs4_close_done,
1817 .rpc_release = nfs4_free_closedata,
1818};
1819
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820/*
1821 * It is possible for data to be read/written from a mem-mapped file
1822 * after the sys_close call (which hits the vfs layer as a flush).
1823 * This means that we can't safely call nfsv4 close on a file until
1824 * the inode is cleared. This in turn means that we are not good
1825 * NFSv4 citizens - we do not indicate to the server to update the file's
1826 * share state even when we are done with one of the three share
1827 * stateid's in the inode.
1828 *
1829 * NOTE: Caller must be holding the sp->so_owner semaphore!
1830 */
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001831int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001833 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001835 struct nfs4_state_owner *sp = state->owner;
1836 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001837 struct rpc_message msg = {
1838 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1839 .rpc_cred = state->owner->so_cred,
1840 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001841 struct rpc_task_setup task_setup_data = {
1842 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001843 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001844 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05001845 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001846 .flags = RPC_TASK_ASYNC,
1847 };
Trond Myklebust95121352005-10-18 14:20:12 -07001848 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
Andy Adamson19ddab02009-04-01 09:22:20 -04001850 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001852 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001853 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001855 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04001856 calldata->arg.stateid = &state->open_stateid;
Benny Halevy34dc1ad2009-04-01 09:22:52 -04001857 if (nfs4_has_session(server->nfs_client))
1858 memset(calldata->arg.stateid->data, 0, 4); /* clear seqid */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 /* Serialization for the sequence id */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001860 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001861 if (calldata->arg.seqid == NULL)
1862 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001863 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001864 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001865 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04001866 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001867 calldata->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04001868 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001869 calldata->path.mnt = mntget(path->mnt);
1870 calldata->path.dentry = dget(path->dentry);
Trond Myklebust95121352005-10-18 14:20:12 -07001871
Trond Myklebust5138fde2007-07-14 15:40:01 -04001872 msg.rpc_argp = &calldata->arg,
1873 msg.rpc_resp = &calldata->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001874 task_setup_data.callback_data = calldata;
1875 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001876 if (IS_ERR(task))
1877 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001878 status = 0;
1879 if (wait)
1880 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001881 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001882 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07001883out_free_calldata:
1884 kfree(calldata);
1885out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04001886 nfs4_put_open_state(state);
1887 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07001888 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889}
1890
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001891static 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 -07001892{
1893 struct file *filp;
Trond Myklebust1b45c462007-07-03 13:04:56 -04001894 int ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001895
Trond Myklebust1b45c462007-07-03 13:04:56 -04001896 /* If the open_intent is for execute, we have an extra check to make */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001897 if (fmode & FMODE_EXEC) {
Trond Myklebustaf22f942007-08-10 17:45:10 -04001898 ret = nfs_may_open(state->inode,
Trond Myklebust1b45c462007-07-03 13:04:56 -04001899 state->owner->so_cred,
1900 nd->intent.open.flags);
1901 if (ret < 0)
1902 goto out_close;
1903 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001904 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001905 if (!IS_ERR(filp)) {
1906 struct nfs_open_context *ctx;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04001907 ctx = nfs_file_open_context(filp);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001908 ctx->state = state;
Trond Myklebust95cf9592006-04-18 13:14:06 -04001909 return 0;
1910 }
Trond Myklebust1b45c462007-07-03 13:04:56 -04001911 ret = PTR_ERR(filp);
1912out_close:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001913 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
Trond Myklebust1b45c462007-07-03 13:04:56 -04001914 return ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001915}
1916
1917struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1919{
Trond Myklebustad389da2007-06-05 12:30:00 -04001920 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08001921 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001922 .dentry = dentry,
1923 };
Jan Blunck4ac91372008-02-14 19:34:32 -08001924 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 struct iattr attr;
1926 struct rpc_cred *cred;
1927 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001928 struct dentry *res;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001929 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
1931 if (nd->flags & LOOKUP_CREATE) {
1932 attr.ia_mode = nd->intent.open.create_mode;
1933 attr.ia_valid = ATTR_MODE;
1934 if (!IS_POSIXACL(dir))
Al Viroce3b0f82009-03-29 19:08:22 -04001935 attr.ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 } else {
1937 attr.ia_valid = 0;
1938 BUG_ON(nd->intent.open.flags & O_CREAT);
1939 }
1940
Trond Myklebust98a8e322008-03-12 12:25:28 -04001941 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07001943 return (struct dentry *)cred;
Trond Myklebust565277f2007-10-15 18:17:53 -04001944 parent = dentry->d_parent;
1945 /* Protect against concurrent sillydeletes */
1946 nfs_block_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001947 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001949 if (IS_ERR(state)) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04001950 if (PTR_ERR(state) == -ENOENT) {
Trond Myklebust02a913a2005-10-18 14:20:17 -07001951 d_add(dentry, NULL);
Trond Myklebustd75340c2007-10-01 21:42:01 -04001952 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1953 }
Trond Myklebust565277f2007-10-15 18:17:53 -04001954 nfs_unblock_sillyrename(parent);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001955 return (struct dentry *)state;
1956 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001957 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07001958 if (res != NULL)
Trond Myklebustdeee9362007-08-27 11:33:00 -04001959 path.dentry = res;
Trond Myklebustd75340c2007-10-01 21:42:01 -04001960 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
Trond Myklebust565277f2007-10-15 18:17:53 -04001961 nfs_unblock_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001962 nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001963 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964}
1965
1966int
Trond Myklebust02a913a2005-10-18 14:20:17 -07001967nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968{
Trond Myklebustad389da2007-06-05 12:30:00 -04001969 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08001970 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001971 .dentry = dentry,
1972 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 struct rpc_cred *cred;
1974 struct nfs4_state *state;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001975 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Trond Myklebust98a8e322008-03-12 12:25:28 -04001977 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 if (IS_ERR(cred))
1979 return PTR_ERR(cred);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001980 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001982 if (IS_ERR(state)) {
1983 switch (PTR_ERR(state)) {
1984 case -EPERM:
1985 case -EACCES:
1986 case -EDQUOT:
1987 case -ENOSPC:
1988 case -EROFS:
1989 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1990 return 1;
Chuck Leverb87c0ad2006-10-19 23:28:42 -07001991 default:
1992 goto out_drop;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001993 }
Trond Myklebust02a913a2005-10-18 14:20:17 -07001994 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001995 if (state->inode == dentry->d_inode) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04001996 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001997 nfs4_intent_set_file(nd, &path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 return 1;
1999 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002000 nfs4_close_sync(&path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002001out_drop:
2002 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 return 0;
2004}
2005
Trond Myklebust1185a552009-12-03 15:54:02 -05002006static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002007{
2008 if (ctx->state == NULL)
2009 return;
2010 if (is_sync)
2011 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2012 else
2013 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2014}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
2016static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2017{
Benny Halevy43652ad2009-04-01 09:21:54 -04002018 struct nfs4_server_caps_arg args = {
2019 .fhandle = fhandle,
2020 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 struct nfs4_server_caps_res res = {};
2022 struct rpc_message msg = {
2023 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002024 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 .rpc_resp = &res,
2026 };
2027 int status;
2028
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002029 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 if (status == 0) {
2031 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002032 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2033 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2034 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2035 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2036 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2038 server->caps |= NFS_CAP_ACLS;
2039 if (res.has_links != 0)
2040 server->caps |= NFS_CAP_HARDLINKS;
2041 if (res.has_symlinks != 0)
2042 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002043 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2044 server->caps |= NFS_CAP_FILEID;
2045 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2046 server->caps |= NFS_CAP_MODE;
2047 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2048 server->caps |= NFS_CAP_NLINK;
2049 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2050 server->caps |= NFS_CAP_OWNER;
2051 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2052 server->caps |= NFS_CAP_OWNER_GROUP;
2053 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2054 server->caps |= NFS_CAP_ATIME;
2055 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2056 server->caps |= NFS_CAP_CTIME;
2057 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2058 server->caps |= NFS_CAP_MTIME;
2059
Trond Myklebusta65318b2009-03-11 14:10:28 -04002060 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2061 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2062 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 server->acl_bitmask = res.acl_bitmask;
2064 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002065
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 return status;
2067}
2068
Trond Myklebust55a97592006-06-09 09:34:19 -04002069int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070{
2071 struct nfs4_exception exception = { };
2072 int err;
2073 do {
2074 err = nfs4_handle_exception(server,
2075 _nfs4_server_capabilities(server, fhandle),
2076 &exception);
2077 } while (exception.retry);
2078 return err;
2079}
2080
2081static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2082 struct nfs_fsinfo *info)
2083{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 struct nfs4_lookup_root_arg args = {
2085 .bitmask = nfs4_fattr_bitmap,
2086 };
2087 struct nfs4_lookup_res res = {
2088 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002089 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 .fh = fhandle,
2091 };
2092 struct rpc_message msg = {
2093 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2094 .rpc_argp = &args,
2095 .rpc_resp = &res,
2096 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002097
Trond Myklebust0e574af2005-10-27 22:12:38 -04002098 nfs_fattr_init(info->fattr);
Trond Myklebustfccba802009-07-21 16:48:07 -04002099 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100}
2101
2102static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2103 struct nfs_fsinfo *info)
2104{
2105 struct nfs4_exception exception = { };
2106 int err;
2107 do {
2108 err = nfs4_handle_exception(server,
2109 _nfs4_lookup_root(server, fhandle, info),
2110 &exception);
2111 } while (exception.retry);
2112 return err;
2113}
2114
David Howells54ceac42006-08-22 20:06:13 -04002115/*
2116 * get the file handle for the "/" directory on the server
2117 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002119 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 int status;
2122
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 if (status == 0)
2125 status = nfs4_server_capabilities(server, fhandle);
2126 if (status == 0)
2127 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002128 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129}
2130
Manoj Naik6b97fd32006-06-09 09:34:29 -04002131/*
2132 * Get locations and (maybe) other attributes of a referral.
2133 * Note that we'll actually follow the referral later when
2134 * we detect fsid mismatch in inode revalidation
2135 */
Trond Myklebust56659e92007-07-17 21:52:39 -04002136static 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 -04002137{
2138 int status = -ENOMEM;
2139 struct page *page = NULL;
2140 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002141
2142 page = alloc_page(GFP_KERNEL);
2143 if (page == NULL)
2144 goto out;
2145 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2146 if (locations == NULL)
2147 goto out;
2148
Trond Myklebustc228fd32007-01-13 02:28:11 -05002149 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002150 if (status != 0)
2151 goto out;
2152 /* Make sure server returned a different fsid for the referral */
2153 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07002154 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 -04002155 status = -EIO;
2156 goto out;
2157 }
2158
2159 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2160 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2161 if (!fattr->mode)
2162 fattr->mode = S_IFDIR;
2163 memset(fhandle, 0, sizeof(struct nfs_fh));
2164out:
2165 if (page)
2166 __free_page(page);
2167 if (locations)
2168 kfree(locations);
2169 return status;
2170}
2171
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2173{
2174 struct nfs4_getattr_arg args = {
2175 .fh = fhandle,
2176 .bitmask = server->attr_bitmask,
2177 };
2178 struct nfs4_getattr_res res = {
2179 .fattr = fattr,
2180 .server = server,
2181 };
2182 struct rpc_message msg = {
2183 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2184 .rpc_argp = &args,
2185 .rpc_resp = &res,
2186 };
2187
Trond Myklebust0e574af2005-10-27 22:12:38 -04002188 nfs_fattr_init(fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002189 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190}
2191
2192static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2193{
2194 struct nfs4_exception exception = { };
2195 int err;
2196 do {
2197 err = nfs4_handle_exception(server,
2198 _nfs4_proc_getattr(server, fhandle, fattr),
2199 &exception);
2200 } while (exception.retry);
2201 return err;
2202}
2203
2204/*
2205 * The file is not closed if it is opened due to the a request to change
2206 * the size of the file. The open call will not be needed once the
2207 * VFS layer lookup-intents are implemented.
2208 *
2209 * Close is called when the inode is destroyed.
2210 * If we haven't opened the file for O_WRONLY, we
2211 * need to in the size_change case to obtain a stateid.
2212 *
2213 * Got race?
2214 * Because OPEN is always done by name in nfsv4, it is
2215 * possible that we opened a different file by the same
2216 * name. We can recognize this race condition, but we
2217 * can't do anything about it besides returning an error.
2218 *
2219 * This will be fixed with VFS changes (lookup-intent).
2220 */
2221static int
2222nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2223 struct iattr *sattr)
2224{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002225 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002226 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002227 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 int status;
2229
Trond Myklebust0e574af2005-10-27 22:12:38 -04002230 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
Trond Myklebustd5308382005-11-04 15:33:38 -05002232 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002233 if (sattr->ia_valid & ATTR_FILE) {
2234 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002235
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002236 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002237 if (ctx) {
2238 cred = ctx->cred;
2239 state = ctx->state;
2240 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002241 }
2242
2243 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002244 if (status == 0)
2245 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 return status;
2247}
2248
Trond Myklebust56659e92007-07-17 21:52:39 -04002249static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2250 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04002251 struct nfs_fattr *fattr)
2252{
2253 int status;
2254 struct nfs4_lookup_arg args = {
2255 .bitmask = server->attr_bitmask,
2256 .dir_fh = dirfh,
2257 .name = name,
2258 };
2259 struct nfs4_lookup_res res = {
2260 .server = server,
2261 .fattr = fattr,
2262 .fh = fhandle,
2263 };
2264 struct rpc_message msg = {
2265 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2266 .rpc_argp = &args,
2267 .rpc_resp = &res,
2268 };
2269
2270 nfs_fattr_init(fattr);
2271
2272 dprintk("NFS call lookupfh %s\n", name->name);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002273 status = nfs4_call_sync(server, &msg, &args, &res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002274 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002275 return status;
2276}
2277
2278static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2279 struct qstr *name, struct nfs_fh *fhandle,
2280 struct nfs_fattr *fattr)
2281{
2282 struct nfs4_exception exception = { };
2283 int err;
2284 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04002285 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2286 /* FIXME: !!!! */
2287 if (err == -NFS4ERR_MOVED) {
2288 err = -EREMOTE;
2289 break;
2290 }
2291 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002292 } while (exception.retry);
2293 return err;
2294}
2295
Trond Myklebust56659e92007-07-17 21:52:39 -04002296static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2298{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002299 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
2301 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002302 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002303 if (status == -NFS4ERR_MOVED)
2304 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 dprintk("NFS reply lookup: %d\n", status);
2306 return status;
2307}
2308
2309static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2310{
2311 struct nfs4_exception exception = { };
2312 int err;
2313 do {
2314 err = nfs4_handle_exception(NFS_SERVER(dir),
2315 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2316 &exception);
2317 } while (exception.retry);
2318 return err;
2319}
2320
2321static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2322{
Trond Myklebust76b32992007-08-10 17:45:11 -04002323 struct nfs_server *server = NFS_SERVER(inode);
2324 struct nfs_fattr fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 struct nfs4_accessargs args = {
2326 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002327 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002329 struct nfs4_accessres res = {
2330 .server = server,
2331 .fattr = &fattr,
2332 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 struct rpc_message msg = {
2334 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2335 .rpc_argp = &args,
2336 .rpc_resp = &res,
2337 .rpc_cred = entry->cred,
2338 };
2339 int mode = entry->mask;
2340 int status;
2341
2342 /*
2343 * Determine which access bits we want to ask for...
2344 */
2345 if (mode & MAY_READ)
2346 args.access |= NFS4_ACCESS_READ;
2347 if (S_ISDIR(inode->i_mode)) {
2348 if (mode & MAY_WRITE)
2349 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2350 if (mode & MAY_EXEC)
2351 args.access |= NFS4_ACCESS_LOOKUP;
2352 } else {
2353 if (mode & MAY_WRITE)
2354 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2355 if (mode & MAY_EXEC)
2356 args.access |= NFS4_ACCESS_EXECUTE;
2357 }
Trond Myklebust76b32992007-08-10 17:45:11 -04002358 nfs_fattr_init(&fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002359 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 if (!status) {
2361 entry->mask = 0;
2362 if (res.access & NFS4_ACCESS_READ)
2363 entry->mask |= MAY_READ;
2364 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2365 entry->mask |= MAY_WRITE;
2366 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2367 entry->mask |= MAY_EXEC;
Trond Myklebust76b32992007-08-10 17:45:11 -04002368 nfs_refresh_inode(inode, &fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 }
2370 return status;
2371}
2372
2373static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2374{
2375 struct nfs4_exception exception = { };
2376 int err;
2377 do {
2378 err = nfs4_handle_exception(NFS_SERVER(inode),
2379 _nfs4_proc_access(inode, entry),
2380 &exception);
2381 } while (exception.retry);
2382 return err;
2383}
2384
2385/*
2386 * TODO: For the time being, we don't try to get any attributes
2387 * along with any of the zero-copy operations READ, READDIR,
2388 * READLINK, WRITE.
2389 *
2390 * In the case of the first three, we want to put the GETATTR
2391 * after the read-type operation -- this is because it is hard
2392 * to predict the length of a GETATTR response in v4, and thus
2393 * align the READ data correctly. This means that the GETATTR
2394 * may end up partially falling into the page cache, and we should
2395 * shift it into the 'tail' of the xdr_buf before processing.
2396 * To do this efficiently, we need to know the total length
2397 * of data received, which doesn't seem to be available outside
2398 * of the RPC layer.
2399 *
2400 * In the case of WRITE, we also want to put the GETATTR after
2401 * the operation -- in this case because we want to make sure
2402 * we get the post-operation mtime and size. This means that
2403 * we can't use xdr_encode_pages() as written: we need a variant
2404 * of it which would leave room in the 'tail' iovec.
2405 *
2406 * Both of these changes to the XDR layer would in fact be quite
2407 * minor, but I decided to leave them for a subsequent patch.
2408 */
2409static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2410 unsigned int pgbase, unsigned int pglen)
2411{
2412 struct nfs4_readlink args = {
2413 .fh = NFS_FH(inode),
2414 .pgbase = pgbase,
2415 .pglen = pglen,
2416 .pages = &page,
2417 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002418 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 struct rpc_message msg = {
2420 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2421 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002422 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 };
2424
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002425 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426}
2427
2428static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2429 unsigned int pgbase, unsigned int pglen)
2430{
2431 struct nfs4_exception exception = { };
2432 int err;
2433 do {
2434 err = nfs4_handle_exception(NFS_SERVER(inode),
2435 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2436 &exception);
2437 } while (exception.retry);
2438 return err;
2439}
2440
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441/*
2442 * Got race?
2443 * We will need to arrange for the VFS layer to provide an atomic open.
2444 * Until then, this create/open method is prone to inefficiency and race
2445 * conditions due to the lookup, create, and open VFS calls from sys_open()
2446 * placed on the wire.
2447 *
2448 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2449 * The file will be opened again in the subsequent VFS open call
2450 * (nfs4_proc_file_open).
2451 *
2452 * The open for read will just hang around to be used by any process that
2453 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2454 */
2455
2456static int
2457nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07002458 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459{
Trond Myklebustad389da2007-06-05 12:30:00 -04002460 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002461 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002462 .dentry = dentry,
2463 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 struct nfs4_state *state;
2465 struct rpc_cred *cred;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002466 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 int status = 0;
2468
Trond Myklebust98a8e322008-03-12 12:25:28 -04002469 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 if (IS_ERR(cred)) {
2471 status = PTR_ERR(cred);
2472 goto out;
2473 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002474 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002475 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 if (IS_ERR(state)) {
2477 status = PTR_ERR(state);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002478 goto out_putcred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002480 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002481 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 if (flags & O_EXCL) {
2483 struct nfs_fattr fattr;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002484 status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002485 if (status == 0)
Trond Myklebust65e43082005-08-16 11:49:44 -04002486 nfs_setattr_update_inode(state->inode, sattr);
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002487 nfs_post_op_update_inode(state->inode, &fattr);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002488 }
Trond Myklebustad389da2007-06-05 12:30:00 -04002489 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002490 status = nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002491 else
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002492 nfs4_close_sync(&path, state, fmode);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002493out_putcred:
2494 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495out:
2496 return status;
2497}
2498
2499static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2500{
Trond Myklebust16e42952005-10-27 22:12:44 -04002501 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002502 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002504 .name.len = name->len,
2505 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002506 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002508 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002509 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002510 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002512 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2513 .rpc_argp = &args,
2514 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 };
2516 int status;
2517
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002518 nfs_fattr_init(&res.dir_attr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002519 status = nfs4_call_sync(server, &msg, &args, &res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002520 if (status == 0) {
2521 update_changeattr(dir, &res.cinfo);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002522 nfs_post_op_update_inode(dir, &res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 return status;
2525}
2526
2527static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2528{
2529 struct nfs4_exception exception = { };
2530 int err;
2531 do {
2532 err = nfs4_handle_exception(NFS_SERVER(dir),
2533 _nfs4_proc_remove(dir, name),
2534 &exception);
2535 } while (exception.retry);
2536 return err;
2537}
2538
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002539static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002541 struct nfs_server *server = NFS_SERVER(dir);
2542 struct nfs_removeargs *args = msg->rpc_argp;
2543 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544
Trond Myklebusta65318b2009-03-11 14:10:28 -04002545 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002546 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548}
2549
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002550static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002552 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2553
Andy Adamson472cfbd2009-04-01 09:22:24 -04002554 nfs4_sequence_done(res->server, &res->seq_res, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002555 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002556 return 0;
2557 update_changeattr(dir, &res->cinfo);
2558 nfs_post_op_update_inode(dir, &res->dir_attr);
2559 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560}
2561
2562static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2563 struct inode *new_dir, struct qstr *new_name)
2564{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002565 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 struct nfs4_rename_arg arg = {
2567 .old_dir = NFS_FH(old_dir),
2568 .new_dir = NFS_FH(new_dir),
2569 .old_name = old_name,
2570 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002571 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002573 struct nfs_fattr old_fattr, new_fattr;
2574 struct nfs4_rename_res res = {
2575 .server = server,
2576 .old_fattr = &old_fattr,
2577 .new_fattr = &new_fattr,
2578 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 struct rpc_message msg = {
2580 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2581 .rpc_argp = &arg,
2582 .rpc_resp = &res,
2583 };
2584 int status;
2585
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002586 nfs_fattr_init(res.old_fattr);
2587 nfs_fattr_init(res.new_fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002588 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589
2590 if (!status) {
2591 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002592 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002594 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 }
2596 return status;
2597}
2598
2599static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2600 struct inode *new_dir, struct qstr *new_name)
2601{
2602 struct nfs4_exception exception = { };
2603 int err;
2604 do {
2605 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2606 _nfs4_proc_rename(old_dir, old_name,
2607 new_dir, new_name),
2608 &exception);
2609 } while (exception.retry);
2610 return err;
2611}
2612
2613static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2614{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002615 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 struct nfs4_link_arg arg = {
2617 .fh = NFS_FH(inode),
2618 .dir_fh = NFS_FH(dir),
2619 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002620 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002622 struct nfs_fattr fattr, dir_attr;
2623 struct nfs4_link_res res = {
2624 .server = server,
2625 .fattr = &fattr,
2626 .dir_attr = &dir_attr,
2627 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 struct rpc_message msg = {
2629 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2630 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002631 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 };
2633 int status;
2634
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002635 nfs_fattr_init(res.fattr);
2636 nfs_fattr_init(res.dir_attr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002637 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002638 if (!status) {
2639 update_changeattr(dir, &res.cinfo);
2640 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002641 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
2644 return status;
2645}
2646
2647static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2648{
2649 struct nfs4_exception exception = { };
2650 int err;
2651 do {
2652 err = nfs4_handle_exception(NFS_SERVER(inode),
2653 _nfs4_proc_link(inode, dir, name),
2654 &exception);
2655 } while (exception.retry);
2656 return err;
2657}
2658
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002659struct nfs4_createdata {
2660 struct rpc_message msg;
2661 struct nfs4_create_arg arg;
2662 struct nfs4_create_res res;
2663 struct nfs_fh fh;
2664 struct nfs_fattr fattr;
2665 struct nfs_fattr dir_fattr;
2666};
2667
2668static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2669 struct qstr *name, struct iattr *sattr, u32 ftype)
2670{
2671 struct nfs4_createdata *data;
2672
2673 data = kzalloc(sizeof(*data), GFP_KERNEL);
2674 if (data != NULL) {
2675 struct nfs_server *server = NFS_SERVER(dir);
2676
2677 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2678 data->msg.rpc_argp = &data->arg;
2679 data->msg.rpc_resp = &data->res;
2680 data->arg.dir_fh = NFS_FH(dir);
2681 data->arg.server = server;
2682 data->arg.name = name;
2683 data->arg.attrs = sattr;
2684 data->arg.ftype = ftype;
2685 data->arg.bitmask = server->attr_bitmask;
2686 data->res.server = server;
2687 data->res.fh = &data->fh;
2688 data->res.fattr = &data->fattr;
2689 data->res.dir_fattr = &data->dir_fattr;
2690 nfs_fattr_init(data->res.fattr);
2691 nfs_fattr_init(data->res.dir_fattr);
2692 }
2693 return data;
2694}
2695
2696static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2697{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002698 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2699 &data->arg, &data->res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002700 if (status == 0) {
2701 update_changeattr(dir, &data->res.dir_cinfo);
2702 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2703 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2704 }
2705 return status;
2706}
2707
2708static void nfs4_free_createdata(struct nfs4_createdata *data)
2709{
2710 kfree(data);
2711}
2712
Chuck Lever4f390c12006-08-22 20:06:22 -04002713static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002714 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002716 struct nfs4_createdata *data;
2717 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718
Chuck Lever94a6d752006-08-22 20:06:23 -04002719 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002720 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002721
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002722 status = -ENOMEM;
2723 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2724 if (data == NULL)
2725 goto out;
2726
2727 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2728 data->arg.u.symlink.pages = &page;
2729 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002731 status = nfs4_do_create(dir, dentry, data);
2732
2733 nfs4_free_createdata(data);
2734out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 return status;
2736}
2737
Chuck Lever4f390c12006-08-22 20:06:22 -04002738static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002739 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740{
2741 struct nfs4_exception exception = { };
2742 int err;
2743 do {
2744 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002745 _nfs4_proc_symlink(dir, dentry, page,
2746 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 &exception);
2748 } while (exception.retry);
2749 return err;
2750}
2751
2752static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2753 struct iattr *sattr)
2754{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002755 struct nfs4_createdata *data;
2756 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002758 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2759 if (data == NULL)
2760 goto out;
2761
2762 status = nfs4_do_create(dir, dentry, data);
2763
2764 nfs4_free_createdata(data);
2765out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 return status;
2767}
2768
2769static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2770 struct iattr *sattr)
2771{
2772 struct nfs4_exception exception = { };
2773 int err;
2774 do {
2775 err = nfs4_handle_exception(NFS_SERVER(dir),
2776 _nfs4_proc_mkdir(dir, dentry, sattr),
2777 &exception);
2778 } while (exception.retry);
2779 return err;
2780}
2781
2782static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2783 u64 cookie, struct page *page, unsigned int count, int plus)
2784{
2785 struct inode *dir = dentry->d_inode;
2786 struct nfs4_readdir_arg args = {
2787 .fh = NFS_FH(dir),
2788 .pages = &page,
2789 .pgbase = 0,
2790 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002791 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 };
2793 struct nfs4_readdir_res res;
2794 struct rpc_message msg = {
2795 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2796 .rpc_argp = &args,
2797 .rpc_resp = &res,
2798 .rpc_cred = cred,
2799 };
2800 int status;
2801
Harvey Harrison3110ff82008-05-02 13:42:44 -07002802 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002803 dentry->d_parent->d_name.name,
2804 dentry->d_name.name,
2805 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2807 res.pgbase = args.pgbase;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002808 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 if (status == 0)
2810 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustc4812992007-09-28 17:11:45 -04002811
2812 nfs_invalidate_atime(dir);
2813
Harvey Harrison3110ff82008-05-02 13:42:44 -07002814 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 return status;
2816}
2817
2818static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2819 u64 cookie, struct page *page, unsigned int count, int plus)
2820{
2821 struct nfs4_exception exception = { };
2822 int err;
2823 do {
2824 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2825 _nfs4_proc_readdir(dentry, cred, cookie,
2826 page, count, plus),
2827 &exception);
2828 } while (exception.retry);
2829 return err;
2830}
2831
2832static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2833 struct iattr *sattr, dev_t rdev)
2834{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002835 struct nfs4_createdata *data;
2836 int mode = sattr->ia_mode;
2837 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
2839 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2840 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002841
2842 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2843 if (data == NULL)
2844 goto out;
2845
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002847 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002849 data->arg.ftype = NF4BLK;
2850 data->arg.u.device.specdata1 = MAJOR(rdev);
2851 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 }
2853 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002854 data->arg.ftype = NF4CHR;
2855 data->arg.u.device.specdata1 = MAJOR(rdev);
2856 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002859 status = nfs4_do_create(dir, dentry, data);
2860
2861 nfs4_free_createdata(data);
2862out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 return status;
2864}
2865
2866static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2867 struct iattr *sattr, dev_t rdev)
2868{
2869 struct nfs4_exception exception = { };
2870 int err;
2871 do {
2872 err = nfs4_handle_exception(NFS_SERVER(dir),
2873 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2874 &exception);
2875 } while (exception.retry);
2876 return err;
2877}
2878
2879static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2880 struct nfs_fsstat *fsstat)
2881{
2882 struct nfs4_statfs_arg args = {
2883 .fh = fhandle,
2884 .bitmask = server->attr_bitmask,
2885 };
Benny Halevy24ad1482009-04-01 09:21:56 -04002886 struct nfs4_statfs_res res = {
2887 .fsstat = fsstat,
2888 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 struct rpc_message msg = {
2890 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2891 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04002892 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 };
2894
Trond Myklebust0e574af2005-10-27 22:12:38 -04002895 nfs_fattr_init(fsstat->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002896 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897}
2898
2899static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2900{
2901 struct nfs4_exception exception = { };
2902 int err;
2903 do {
2904 err = nfs4_handle_exception(server,
2905 _nfs4_proc_statfs(server, fhandle, fsstat),
2906 &exception);
2907 } while (exception.retry);
2908 return err;
2909}
2910
2911static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2912 struct nfs_fsinfo *fsinfo)
2913{
2914 struct nfs4_fsinfo_arg args = {
2915 .fh = fhandle,
2916 .bitmask = server->attr_bitmask,
2917 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04002918 struct nfs4_fsinfo_res res = {
2919 .fsinfo = fsinfo,
2920 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 struct rpc_message msg = {
2922 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2923 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04002924 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 };
2926
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002927 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928}
2929
2930static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2931{
2932 struct nfs4_exception exception = { };
2933 int err;
2934
2935 do {
2936 err = nfs4_handle_exception(server,
2937 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2938 &exception);
2939 } while (exception.retry);
2940 return err;
2941}
2942
2943static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2944{
Trond Myklebust0e574af2005-10-27 22:12:38 -04002945 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2947}
2948
2949static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2950 struct nfs_pathconf *pathconf)
2951{
2952 struct nfs4_pathconf_arg args = {
2953 .fh = fhandle,
2954 .bitmask = server->attr_bitmask,
2955 };
Benny Halevyd45b2982009-04-01 09:21:58 -04002956 struct nfs4_pathconf_res res = {
2957 .pathconf = pathconf,
2958 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 struct rpc_message msg = {
2960 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2961 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04002962 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 };
2964
2965 /* None of the pathconf attributes are mandatory to implement */
2966 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2967 memset(pathconf, 0, sizeof(*pathconf));
2968 return 0;
2969 }
2970
Trond Myklebust0e574af2005-10-27 22:12:38 -04002971 nfs_fattr_init(pathconf->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002972 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973}
2974
2975static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2976 struct nfs_pathconf *pathconf)
2977{
2978 struct nfs4_exception exception = { };
2979 int err;
2980
2981 do {
2982 err = nfs4_handle_exception(server,
2983 _nfs4_proc_pathconf(server, fhandle, pathconf),
2984 &exception);
2985 } while (exception.retry);
2986 return err;
2987}
2988
Trond Myklebustec06c092006-03-20 13:44:27 -05002989static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990{
Trond Myklebustec06c092006-03-20 13:44:27 -05002991 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992
Andy Adamsonf11c88a2009-04-01 09:22:25 -04002993 dprintk("--> %s\n", __func__);
2994
Andy Adamsonf11c88a2009-04-01 09:22:25 -04002995 nfs4_sequence_done(server, &data->res.seq_res, task->tk_status);
2996
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002997 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05002998 nfs_restart_rpc(task, server->nfs_client);
Trond Myklebustec06c092006-03-20 13:44:27 -05002999 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003001
3002 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003004 renew_lease(server, data->timestamp);
3005 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006}
3007
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003008static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 data->timestamp = jiffies;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003011 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012}
3013
Trond Myklebust788e7a82006-03-20 13:44:27 -05003014static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 struct inode *inode = data->inode;
3017
Andy Adamsondef6ed72009-04-01 09:22:26 -04003018 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3019 task->tk_status);
3020
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003021 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003022 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003023 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003025 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003027 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003028 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003029 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030}
3031
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003032static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003034 struct nfs_server *server = NFS_SERVER(data->inode);
3035
Trond Myklebusta65318b2009-03-11 14:10:28 -04003036 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003037 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 data->timestamp = jiffies;
3039
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003040 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041}
3042
Trond Myklebust788e7a82006-03-20 13:44:27 -05003043static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 struct inode *inode = data->inode;
3046
Andy Adamson21d9a852009-04-01 09:22:27 -04003047 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3048 task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003049 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003050 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003051 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003053 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003054 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055}
3056
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003057static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003059 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060
Trond Myklebusta65318b2009-03-11 14:10:28 -04003061 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003062 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003063 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064}
3065
3066/*
3067 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3068 * standalone procedure for queueing an asynchronous RENEW.
3069 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003070static void nfs4_renew_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071{
David Howellsadfa6f92006-08-22 20:06:08 -04003072 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003073 unsigned long timestamp = (unsigned long)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074
3075 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003076 /* Unless we're shutting down, schedule state recovery! */
3077 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3078 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 return;
3080 }
3081 spin_lock(&clp->cl_lock);
3082 if (time_before(clp->cl_last_renewal,timestamp))
3083 clp->cl_last_renewal = timestamp;
3084 spin_unlock(&clp->cl_lock);
3085}
3086
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003087static const struct rpc_call_ops nfs4_renew_ops = {
3088 .rpc_call_done = nfs4_renew_done,
3089};
3090
David Howellsadfa6f92006-08-22 20:06:08 -04003091int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092{
3093 struct rpc_message msg = {
3094 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3095 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003096 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 };
3098
3099 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003100 &nfs4_renew_ops, (void *)jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101}
3102
David Howellsadfa6f92006-08-22 20:06:08 -04003103int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104{
3105 struct rpc_message msg = {
3106 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3107 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003108 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 };
3110 unsigned long now = jiffies;
3111 int status;
3112
3113 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3114 if (status < 0)
3115 return status;
3116 spin_lock(&clp->cl_lock);
3117 if (time_before(clp->cl_last_renewal,now))
3118 clp->cl_last_renewal = now;
3119 spin_unlock(&clp->cl_lock);
3120 return 0;
3121}
3122
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003123static inline int nfs4_server_supports_acls(struct nfs_server *server)
3124{
3125 return (server->caps & NFS_CAP_ACLS)
3126 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3127 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3128}
3129
3130/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3131 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3132 * the stack.
3133 */
3134#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3135
3136static void buf_to_pages(const void *buf, size_t buflen,
3137 struct page **pages, unsigned int *pgbase)
3138{
3139 const void *p = buf;
3140
3141 *pgbase = offset_in_page(buf);
3142 p -= *pgbase;
3143 while (p < buf + buflen) {
3144 *(pages++) = virt_to_page(p);
3145 p += PAGE_CACHE_SIZE;
3146 }
3147}
3148
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003149struct nfs4_cached_acl {
3150 int cached;
3151 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003152 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003153};
3154
3155static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003156{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003157 struct nfs_inode *nfsi = NFS_I(inode);
3158
3159 spin_lock(&inode->i_lock);
3160 kfree(nfsi->nfs4_acl);
3161 nfsi->nfs4_acl = acl;
3162 spin_unlock(&inode->i_lock);
3163}
3164
3165static void nfs4_zap_acl_attr(struct inode *inode)
3166{
3167 nfs4_set_cached_acl(inode, NULL);
3168}
3169
3170static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3171{
3172 struct nfs_inode *nfsi = NFS_I(inode);
3173 struct nfs4_cached_acl *acl;
3174 int ret = -ENOENT;
3175
3176 spin_lock(&inode->i_lock);
3177 acl = nfsi->nfs4_acl;
3178 if (acl == NULL)
3179 goto out;
3180 if (buf == NULL) /* user is just asking for length */
3181 goto out_len;
3182 if (acl->cached == 0)
3183 goto out;
3184 ret = -ERANGE; /* see getxattr(2) man page */
3185 if (acl->len > buflen)
3186 goto out;
3187 memcpy(buf, acl->data, acl->len);
3188out_len:
3189 ret = acl->len;
3190out:
3191 spin_unlock(&inode->i_lock);
3192 return ret;
3193}
3194
3195static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3196{
3197 struct nfs4_cached_acl *acl;
3198
3199 if (buf && acl_len <= PAGE_SIZE) {
3200 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3201 if (acl == NULL)
3202 goto out;
3203 acl->cached = 1;
3204 memcpy(acl->data, buf, acl_len);
3205 } else {
3206 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3207 if (acl == NULL)
3208 goto out;
3209 acl->cached = 0;
3210 }
3211 acl->len = acl_len;
3212out:
3213 nfs4_set_cached_acl(inode, acl);
3214}
3215
Trond Myklebust16b42892006-08-24 12:27:15 -04003216static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003217{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003218 struct page *pages[NFS4ACL_MAXPAGES];
3219 struct nfs_getaclargs args = {
3220 .fh = NFS_FH(inode),
3221 .acl_pages = pages,
3222 .acl_len = buflen,
3223 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003224 struct nfs_getaclres res = {
3225 .acl_len = buflen,
3226 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003227 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003228 struct rpc_message msg = {
3229 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3230 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003231 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003232 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003233 struct page *localpage = NULL;
3234 int ret;
3235
3236 if (buflen < PAGE_SIZE) {
3237 /* As long as we're doing a round trip to the server anyway,
3238 * let's be prepared for a page of acl data. */
3239 localpage = alloc_page(GFP_KERNEL);
3240 resp_buf = page_address(localpage);
3241 if (localpage == NULL)
3242 return -ENOMEM;
3243 args.acl_pages[0] = localpage;
3244 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003245 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003246 } else {
3247 resp_buf = buf;
3248 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3249 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003250 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003251 if (ret)
3252 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003253 if (res.acl_len > args.acl_len)
3254 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003255 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003256 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003257 if (buf) {
3258 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003259 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003260 goto out_free;
3261 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003262 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003263 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003264 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003265out_free:
3266 if (localpage)
3267 __free_page(localpage);
3268 return ret;
3269}
3270
Trond Myklebust16b42892006-08-24 12:27:15 -04003271static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3272{
3273 struct nfs4_exception exception = { };
3274 ssize_t ret;
3275 do {
3276 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3277 if (ret >= 0)
3278 break;
3279 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3280 } while (exception.retry);
3281 return ret;
3282}
3283
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003284static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3285{
3286 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003287 int ret;
3288
3289 if (!nfs4_server_supports_acls(server))
3290 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003291 ret = nfs_revalidate_inode(server, inode);
3292 if (ret < 0)
3293 return ret;
3294 ret = nfs4_read_cached_acl(inode, buf, buflen);
3295 if (ret != -ENOENT)
3296 return ret;
3297 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003298}
3299
Trond Myklebust16b42892006-08-24 12:27:15 -04003300static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003301{
3302 struct nfs_server *server = NFS_SERVER(inode);
3303 struct page *pages[NFS4ACL_MAXPAGES];
3304 struct nfs_setaclargs arg = {
3305 .fh = NFS_FH(inode),
3306 .acl_pages = pages,
3307 .acl_len = buflen,
3308 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003309 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003310 struct rpc_message msg = {
3311 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3312 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003313 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003314 };
3315 int ret;
3316
3317 if (!nfs4_server_supports_acls(server))
3318 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07003319 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003320 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003321 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003322 nfs_access_zap_cache(inode);
3323 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003324 return ret;
3325}
3326
Trond Myklebust16b42892006-08-24 12:27:15 -04003327static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3328{
3329 struct nfs4_exception exception = { };
3330 int err;
3331 do {
3332 err = nfs4_handle_exception(NFS_SERVER(inode),
3333 __nfs4_proc_set_acl(inode, buf, buflen),
3334 &exception);
3335 } while (exception.retry);
3336 return err;
3337}
3338
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339static int
Andy Adamson8328d592009-04-01 09:22:35 -04003340_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 -07003341{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 if (!clp || task->tk_status >= 0)
3343 return 0;
3344 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003345 case -NFS4ERR_ADMIN_REVOKED:
3346 case -NFS4ERR_BAD_STATEID:
3347 case -NFS4ERR_OPENMODE:
3348 if (state == NULL)
3349 break;
3350 nfs4_state_mark_reclaim_nograce(clp, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 case -NFS4ERR_STALE_CLIENTID:
3352 case -NFS4ERR_STALE_STATEID:
3353 case -NFS4ERR_EXPIRED:
Trond Myklebust5d008372008-02-22 16:34:17 -05003354 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 nfs4_schedule_state_recovery(clp);
Trond Myklebuste005e802008-12-23 15:21:48 -05003356 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
Trond Myklebustfda13932008-02-22 16:34:12 -05003357 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 task->tk_status = 0;
3359 return -EAGAIN;
Andy Adamson4745e312009-04-01 09:22:42 -04003360#if defined(CONFIG_NFS_V4_1)
3361 case -NFS4ERR_BADSESSION:
3362 case -NFS4ERR_BADSLOT:
3363 case -NFS4ERR_BAD_HIGH_SLOT:
3364 case -NFS4ERR_DEADSESSION:
3365 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3366 case -NFS4ERR_SEQ_FALSE_RETRY:
3367 case -NFS4ERR_SEQ_MISORDERED:
3368 dprintk("%s ERROR %d, Reset session\n", __func__,
3369 task->tk_status);
Andy Adamson0b9e2d42009-12-04 16:02:14 -05003370 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -04003371 task->tk_status = 0;
3372 return -EAGAIN;
3373#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 case -NFS4ERR_DELAY:
Andy Adamson8328d592009-04-01 09:22:35 -04003375 if (server)
3376 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003377 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3379 task->tk_status = 0;
3380 return -EAGAIN;
3381 case -NFS4ERR_OLD_STATEID:
3382 task->tk_status = 0;
3383 return -EAGAIN;
3384 }
3385 task->tk_status = nfs4_map_errors(task->tk_status);
3386 return 0;
3387}
3388
Andy Adamson8328d592009-04-01 09:22:35 -04003389static int
3390nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3391{
3392 return _nfs4_async_handle_error(task, server, server->nfs_client, state);
3393}
3394
David Howellsadfa6f92006-08-22 20:06:08 -04003395int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396{
3397 nfs4_verifier sc_verifier;
3398 struct nfs4_setclientid setclientid = {
3399 .sc_verifier = &sc_verifier,
3400 .sc_prog = program,
3401 };
3402 struct rpc_message msg = {
3403 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3404 .rpc_argp = &setclientid,
3405 .rpc_resp = clp,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003406 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 };
Al Virobc4785c2006-10-19 23:28:51 -07003408 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 int loop = 0;
3410 int status;
3411
Al Virobc4785c2006-10-19 23:28:51 -07003412 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3414 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3415
3416 for(;;) {
3417 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003418 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003419 clp->cl_ipaddr,
3420 rpc_peeraddr2str(clp->cl_rpcclient,
3421 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003422 rpc_peeraddr2str(clp->cl_rpcclient,
3423 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003424 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 clp->cl_id_uniquifier);
3426 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003427 sizeof(setclientid.sc_netid),
3428 rpc_peeraddr2str(clp->cl_rpcclient,
3429 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003431 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 clp->cl_ipaddr, port >> 8, port & 255);
3433
3434 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3435 if (status != -NFS4ERR_CLID_INUSE)
3436 break;
3437 if (signalled())
3438 break;
3439 if (loop++ & 1)
3440 ssleep(clp->cl_lease_time + 1);
3441 else
3442 if (++clp->cl_id_uniquifier == 0)
3443 break;
3444 }
3445 return status;
3446}
3447
David Howellsadfa6f92006-08-22 20:06:08 -04003448static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449{
3450 struct nfs_fsinfo fsinfo;
3451 struct rpc_message msg = {
3452 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3453 .rpc_argp = clp,
3454 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003455 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 };
3457 unsigned long now;
3458 int status;
3459
3460 now = jiffies;
3461 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3462 if (status == 0) {
3463 spin_lock(&clp->cl_lock);
3464 clp->cl_lease_time = fsinfo.lease_time * HZ;
3465 clp->cl_last_renewal = now;
3466 spin_unlock(&clp->cl_lock);
3467 }
3468 return status;
3469}
3470
David Howellsadfa6f92006-08-22 20:06:08 -04003471int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003472{
Benny Halevy77e03672008-06-24 20:25:57 +03003473 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003474 int err;
3475 do {
3476 err = _nfs4_proc_setclientid_confirm(clp, cred);
3477 switch (err) {
3478 case 0:
3479 return err;
3480 case -NFS4ERR_RESOURCE:
3481 /* The IBM lawyers misread another document! */
3482 case -NFS4ERR_DELAY:
3483 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3484 }
3485 } while (err == 0);
3486 return err;
3487}
3488
Trond Myklebustfe650402006-01-03 09:55:18 +01003489struct nfs4_delegreturndata {
3490 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003491 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003492 struct nfs_fh fh;
3493 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003494 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003495 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003496 int rpc_status;
3497};
3498
Trond Myklebustfe650402006-01-03 09:55:18 +01003499static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3500{
3501 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003502
Trond Myklebustd61e6122009-12-05 19:32:19 -05003503 nfs4_sequence_done(data->res.server, &data->res.seq_res,
3504 task->tk_status);
Andy Adamson938e1012009-04-01 09:22:28 -04003505
Ricardo Labiaga79708862009-12-07 09:23:21 -05003506 switch (task->tk_status) {
3507 case -NFS4ERR_STALE_STATEID:
3508 case -NFS4ERR_EXPIRED:
3509 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003510 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003511 break;
3512 default:
3513 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3514 -EAGAIN) {
3515 nfs_restart_rpc(task, data->res.server->nfs_client);
3516 return;
3517 }
3518 }
3519 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003520}
3521
3522static void nfs4_delegreturn_release(void *calldata)
3523{
Trond Myklebustfe650402006-01-03 09:55:18 +01003524 kfree(calldata);
3525}
3526
Andy Adamson938e1012009-04-01 09:22:28 -04003527#if defined(CONFIG_NFS_V4_1)
3528static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3529{
3530 struct nfs4_delegreturndata *d_data;
3531
3532 d_data = (struct nfs4_delegreturndata *)data;
3533
3534 if (nfs4_setup_sequence(d_data->res.server->nfs_client,
3535 &d_data->args.seq_args,
3536 &d_data->res.seq_res, 1, task))
3537 return;
3538 rpc_call_start(task);
3539}
3540#endif /* CONFIG_NFS_V4_1 */
3541
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003542static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003543#if defined(CONFIG_NFS_V4_1)
3544 .rpc_call_prepare = nfs4_delegreturn_prepare,
3545#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003546 .rpc_call_done = nfs4_delegreturn_done,
3547 .rpc_release = nfs4_delegreturn_release,
3548};
3549
Trond Myklebuste6f81072008-01-24 18:14:34 -05003550static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003551{
3552 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003553 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003554 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003555 struct rpc_message msg = {
3556 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3557 .rpc_cred = cred,
3558 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003559 struct rpc_task_setup task_setup_data = {
3560 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003561 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003562 .callback_ops = &nfs4_delegreturn_ops,
3563 .flags = RPC_TASK_ASYNC,
3564 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003565 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003566
Andy Adamson938e1012009-04-01 09:22:28 -04003567 data = kzalloc(sizeof(*data), GFP_KERNEL);
Trond Myklebustfe650402006-01-03 09:55:18 +01003568 if (data == NULL)
3569 return -ENOMEM;
3570 data->args.fhandle = &data->fh;
3571 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003572 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003573 nfs_copy_fh(&data->fh, NFS_FH(inode));
3574 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003575 data->res.fattr = &data->fattr;
3576 data->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003577 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003578 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003579 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003580 data->rpc_status = 0;
3581
Trond Myklebustc970aa82007-07-14 15:39:59 -04003582 task_setup_data.callback_data = data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003583 msg.rpc_argp = &data->args,
3584 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003585 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003586 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003587 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003588 if (!issync)
3589 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003590 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003591 if (status != 0)
3592 goto out;
3593 status = data->rpc_status;
3594 if (status != 0)
3595 goto out;
3596 nfs_refresh_inode(inode, &data->fattr);
3597out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003598 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003599 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600}
3601
Trond Myklebuste6f81072008-01-24 18:14:34 -05003602int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603{
3604 struct nfs_server *server = NFS_SERVER(inode);
3605 struct nfs4_exception exception = { };
3606 int err;
3607 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003608 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 switch (err) {
3610 case -NFS4ERR_STALE_STATEID:
3611 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 case 0:
3613 return 0;
3614 }
3615 err = nfs4_handle_exception(server, err, &exception);
3616 } while (exception.retry);
3617 return err;
3618}
3619
3620#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3621#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3622
3623/*
3624 * sleep, with exponential backoff, and retry the LOCK operation.
3625 */
3626static unsigned long
3627nfs4_set_lock_task_retry(unsigned long timeout)
3628{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003629 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 timeout <<= 1;
3631 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3632 return NFS4_LOCK_MAXTIMEOUT;
3633 return timeout;
3634}
3635
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3637{
3638 struct inode *inode = state->inode;
3639 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003640 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003641 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003643 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003645 struct nfs_lockt_res res = {
3646 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 };
3648 struct rpc_message msg = {
3649 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3650 .rpc_argp = &arg,
3651 .rpc_resp = &res,
3652 .rpc_cred = state->owner->so_cred,
3653 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 struct nfs4_lock_state *lsp;
3655 int status;
3656
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003657 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003658 status = nfs4_set_lock_state(state, request);
3659 if (status != 0)
3660 goto out;
3661 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003662 arg.lock_owner.id = lsp->ls_id.id;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003663 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003664 switch (status) {
3665 case 0:
3666 request->fl_type = F_UNLCK;
3667 break;
3668 case -NFS4ERR_DENIED:
3669 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003671 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003672out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 return status;
3674}
3675
3676static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3677{
3678 struct nfs4_exception exception = { };
3679 int err;
3680
3681 do {
3682 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3683 _nfs4_proc_getlk(state, cmd, request),
3684 &exception);
3685 } while (exception.retry);
3686 return err;
3687}
3688
3689static int do_vfs_lock(struct file *file, struct file_lock *fl)
3690{
3691 int res = 0;
3692 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3693 case FL_POSIX:
3694 res = posix_lock_file_wait(file, fl);
3695 break;
3696 case FL_FLOCK:
3697 res = flock_lock_file_wait(file, fl);
3698 break;
3699 default:
3700 BUG();
3701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 return res;
3703}
3704
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003705struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003706 struct nfs_locku_args arg;
3707 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003708 struct nfs4_lock_state *lsp;
3709 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003710 struct file_lock fl;
3711 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003712 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003713};
3714
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003715static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3716 struct nfs_open_context *ctx,
3717 struct nfs4_lock_state *lsp,
3718 struct nfs_seqid *seqid)
3719{
3720 struct nfs4_unlockdata *p;
3721 struct inode *inode = lsp->ls_state->inode;
3722
Andy Adamsona8936932009-04-01 09:22:23 -04003723 p = kzalloc(sizeof(*p), GFP_KERNEL);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003724 if (p == NULL)
3725 return NULL;
3726 p->arg.fh = NFS_FH(inode);
3727 p->arg.fl = &p->fl;
3728 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003729 p->res.seqid = seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003730 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003731 p->arg.stateid = &lsp->ls_stateid;
3732 p->lsp = lsp;
3733 atomic_inc(&lsp->ls_count);
3734 /* Ensure we don't close file until we're done freeing locks! */
3735 p->ctx = get_nfs_open_context(ctx);
3736 memcpy(&p->fl, fl, sizeof(p->fl));
3737 p->server = NFS_SERVER(inode);
3738 return p;
3739}
3740
Trond Myklebust06f814a2006-01-03 09:55:07 +01003741static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003742{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003743 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003744 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003745 nfs4_put_lock_state(calldata->lsp);
3746 put_nfs_open_context(calldata->ctx);
3747 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003748}
3749
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003750static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003751{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003752 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003753
Andy Adamsona8936932009-04-01 09:22:23 -04003754 nfs4_sequence_done(calldata->server, &calldata->res.seq_res,
3755 task->tk_status);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003756 if (RPC_ASSASSINATED(task))
3757 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003758 switch (task->tk_status) {
3759 case 0:
3760 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003761 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003762 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003763 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003764 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003765 case -NFS4ERR_BAD_STATEID:
3766 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003767 case -NFS4ERR_STALE_STATEID:
3768 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003769 break;
3770 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003771 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebust0110ee12009-12-07 09:00:24 -05003772 nfs_restart_rpc(task,
Trond Myklebustd61e6122009-12-05 19:32:19 -05003773 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003774 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003775}
3776
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003777static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003778{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003779 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003781 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003782 return;
3783 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003784 /* Note: exit _without_ running nfs4_locku_done */
3785 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003786 return;
3787 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003788 calldata->timestamp = jiffies;
Andy Adamsona8936932009-04-01 09:22:23 -04003789 if (nfs4_setup_sequence(calldata->server->nfs_client,
3790 &calldata->arg.seq_args,
3791 &calldata->res.seq_res, 1, task))
3792 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003793 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794}
3795
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003796static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003797 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003798 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003799 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003800};
3801
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003802static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3803 struct nfs_open_context *ctx,
3804 struct nfs4_lock_state *lsp,
3805 struct nfs_seqid *seqid)
3806{
3807 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003808 struct rpc_message msg = {
3809 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3810 .rpc_cred = ctx->cred,
3811 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003812 struct rpc_task_setup task_setup_data = {
3813 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003814 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003815 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003816 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003817 .flags = RPC_TASK_ASYNC,
3818 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003819
Frank Filz137d6ac2007-07-09 15:32:29 -07003820 /* Ensure this is an unlock - when canceling a lock, the
3821 * canceled lock is passed in, and it won't be an unlock.
3822 */
3823 fl->fl_type = F_UNLCK;
3824
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003825 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3826 if (data == NULL) {
3827 nfs_free_seqid(seqid);
3828 return ERR_PTR(-ENOMEM);
3829 }
3830
Trond Myklebust5138fde2007-07-14 15:40:01 -04003831 msg.rpc_argp = &data->arg,
3832 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003833 task_setup_data.callback_data = data;
3834 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003835}
3836
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3838{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003839 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003840 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003841 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003842 struct rpc_task *task;
3843 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003844 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845
Trond Myklebust9b073572006-06-29 16:38:34 -04003846 status = nfs4_set_lock_state(state, request);
3847 /* Unlock _before_ we do the RPC call */
3848 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003849 down_read(&nfsi->rwsem);
3850 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3851 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003852 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003853 }
3854 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003855 if (status != 0)
3856 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003857 /* Is this a delegated lock? */
3858 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04003859 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003860 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003861 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
Trond Myklebust9b073572006-06-29 16:38:34 -04003862 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003863 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04003864 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003865 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003866 status = PTR_ERR(task);
3867 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04003868 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003869 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003870 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04003871out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003872 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003873 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874}
3875
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003876struct nfs4_lockdata {
3877 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003878 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003879 struct nfs4_lock_state *lsp;
3880 struct nfs_open_context *ctx;
3881 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003882 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003883 int rpc_status;
3884 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04003885 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003886};
3887
3888static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3889 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3890{
3891 struct nfs4_lockdata *p;
3892 struct inode *inode = lsp->ls_state->inode;
3893 struct nfs_server *server = NFS_SERVER(inode);
3894
3895 p = kzalloc(sizeof(*p), GFP_KERNEL);
3896 if (p == NULL)
3897 return NULL;
3898
3899 p->arg.fh = NFS_FH(inode);
3900 p->arg.fl = &p->fl;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003901 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
3902 if (p->arg.open_seqid == NULL)
3903 goto out_free;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003904 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3905 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003906 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003907 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04003908 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003909 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003910 p->res.lock_seqid = p->arg.lock_seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003911 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003912 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04003913 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003914 atomic_inc(&lsp->ls_count);
3915 p->ctx = get_nfs_open_context(ctx);
3916 memcpy(&p->fl, fl, sizeof(p->fl));
3917 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003918out_free_seqid:
3919 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003920out_free:
3921 kfree(p);
3922 return NULL;
3923}
3924
3925static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3926{
3927 struct nfs4_lockdata *data = calldata;
3928 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929
Harvey Harrison3110ff82008-05-02 13:42:44 -07003930 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003931 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3932 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003933 /* Do we need to do an open_to_lock_owner? */
3934 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05003935 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
3936 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003937 data->arg.open_stateid = &state->stateid;
3938 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003939 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003940 } else
3941 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003942 data->timestamp = jiffies;
Andy Adamson66179ef2009-04-01 09:22:22 -04003943 if (nfs4_setup_sequence(data->server->nfs_client, &data->arg.seq_args,
3944 &data->res.seq_res, 1, task))
3945 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003946 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003947 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003948}
3949
3950static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3951{
3952 struct nfs4_lockdata *data = calldata;
3953
Harvey Harrison3110ff82008-05-02 13:42:44 -07003954 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003955
Trond Myklebustd61e6122009-12-05 19:32:19 -05003956 nfs4_sequence_done(data->server, &data->res.seq_res,
3957 task->tk_status);
Andy Adamson66179ef2009-04-01 09:22:22 -04003958
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003959 data->rpc_status = task->tk_status;
3960 if (RPC_ASSASSINATED(task))
3961 goto out;
3962 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003963 if (data->rpc_status == 0)
3964 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3965 else
3966 goto out;
3967 }
3968 if (data->rpc_status == 0) {
3969 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3970 sizeof(data->lsp->ls_stateid.data));
3971 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04003972 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003973 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003974out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07003975 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003976}
3977
3978static void nfs4_lock_release(void *calldata)
3979{
3980 struct nfs4_lockdata *data = calldata;
3981
Harvey Harrison3110ff82008-05-02 13:42:44 -07003982 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003983 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003984 if (data->cancelled != 0) {
3985 struct rpc_task *task;
3986 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3987 data->arg.lock_seqid);
3988 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003989 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003990 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003991 } else
3992 nfs_free_seqid(data->arg.lock_seqid);
3993 nfs4_put_lock_state(data->lsp);
3994 put_nfs_open_context(data->ctx);
3995 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003996 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003997}
3998
3999static const struct rpc_call_ops nfs4_lock_ops = {
4000 .rpc_call_prepare = nfs4_lock_prepare,
4001 .rpc_call_done = nfs4_lock_done,
4002 .rpc_release = nfs4_lock_release,
4003};
4004
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004005static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004006{
4007 struct nfs4_lockdata *data;
4008 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004009 struct rpc_message msg = {
4010 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4011 .rpc_cred = state->owner->so_cred,
4012 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004013 struct rpc_task_setup task_setup_data = {
4014 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004015 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004016 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004017 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004018 .flags = RPC_TASK_ASYNC,
4019 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004020 int ret;
4021
Harvey Harrison3110ff82008-05-02 13:42:44 -07004022 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004023 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004024 fl->fl_u.nfs4_fl.owner);
4025 if (data == NULL)
4026 return -ENOMEM;
4027 if (IS_SETLKW(cmd))
4028 data->arg.block = 1;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004029 if (recovery_type == NFS_LOCK_RECLAIM)
4030 data->arg.reclaim = NFS_LOCK_RECLAIM;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004031 msg.rpc_argp = &data->arg,
4032 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004033 task_setup_data.callback_data = data;
4034 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004035 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004036 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004037 ret = nfs4_wait_for_completion_rpc_task(task);
4038 if (ret == 0) {
4039 ret = data->rpc_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004040 } else
4041 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004042 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004043 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004044 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045}
4046
4047static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4048{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004049 struct nfs_server *server = NFS_SERVER(state->inode);
4050 struct nfs4_exception exception = { };
4051 int err;
4052
4053 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004054 /* Cache the lock if possible... */
4055 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4056 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004057 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust202b50d2005-06-22 17:16:29 +00004058 if (err != -NFS4ERR_DELAY)
4059 break;
4060 nfs4_handle_exception(server, err, &exception);
4061 } while (exception.retry);
4062 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063}
4064
4065static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4066{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004067 struct nfs_server *server = NFS_SERVER(state->inode);
4068 struct nfs4_exception exception = { };
4069 int err;
4070
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004071 err = nfs4_set_lock_state(state, request);
4072 if (err != 0)
4073 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004074 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004075 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4076 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004077 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004078 switch (err) {
4079 default:
4080 goto out;
4081 case -NFS4ERR_GRACE:
4082 case -NFS4ERR_DELAY:
4083 nfs4_handle_exception(server, err, &exception);
4084 err = 0;
4085 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004086 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004087out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004088 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089}
4090
4091static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4092{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004093 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004094 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 int status;
4096
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004097 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004098 status = nfs4_set_lock_state(state, request);
4099 if (status != 0)
4100 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004101 request->fl_flags |= FL_ACCESS;
4102 status = do_vfs_lock(request->fl_file, request);
4103 if (status < 0)
4104 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004105 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004106 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004107 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004108 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004109 request->fl_flags = fl_flags & ~FL_SLEEP;
4110 status = do_vfs_lock(request->fl_file, request);
4111 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004112 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004113 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004114 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004115 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004116 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004117 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004118 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004119 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004120out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004121 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004122out:
4123 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 return status;
4125}
4126
4127static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4128{
4129 struct nfs4_exception exception = { };
4130 int err;
4131
4132 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004133 err = _nfs4_proc_setlk(state, cmd, request);
4134 if (err == -NFS4ERR_DENIED)
4135 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004137 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138 } while (exception.retry);
4139 return err;
4140}
4141
4142static int
4143nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4144{
4145 struct nfs_open_context *ctx;
4146 struct nfs4_state *state;
4147 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4148 int status;
4149
4150 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004151 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 state = ctx->state;
4153
4154 if (request->fl_start < 0 || request->fl_end < 0)
4155 return -EINVAL;
4156
Trond Myklebustd9531262009-07-21 19:22:38 -04004157 if (IS_GETLK(cmd)) {
4158 if (state != NULL)
4159 return nfs4_proc_getlk(state, F_GETLK, request);
4160 return 0;
4161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162
4163 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4164 return -EINVAL;
4165
Trond Myklebustd9531262009-07-21 19:22:38 -04004166 if (request->fl_type == F_UNLCK) {
4167 if (state != NULL)
4168 return nfs4_proc_unlck(state, cmd, request);
4169 return 0;
4170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171
Trond Myklebustd9531262009-07-21 19:22:38 -04004172 if (state == NULL)
4173 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 do {
4175 status = nfs4_proc_setlk(state, cmd, request);
4176 if ((status != -EAGAIN) || IS_SETLK(cmd))
4177 break;
4178 timeout = nfs4_set_lock_task_retry(timeout);
4179 status = -ERESTARTSYS;
4180 if (signalled())
4181 break;
4182 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 return status;
4184}
4185
Trond Myklebust888e6942005-11-04 15:38:11 -05004186int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4187{
4188 struct nfs_server *server = NFS_SERVER(state->inode);
4189 struct nfs4_exception exception = { };
4190 int err;
4191
4192 err = nfs4_set_lock_state(state, fl);
4193 if (err != 0)
4194 goto out;
4195 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004196 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004197 switch (err) {
4198 default:
4199 printk(KERN_ERR "%s: unhandled error %d.\n",
4200 __func__, err);
4201 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004202 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004203 goto out;
4204 case -NFS4ERR_EXPIRED:
4205 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004206 case -NFS4ERR_STALE_STATEID:
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004207 case -NFS4ERR_BADSESSION:
4208 case -NFS4ERR_BADSLOT:
4209 case -NFS4ERR_BAD_HIGH_SLOT:
4210 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4211 case -NFS4ERR_DEADSESSION:
Trond Myklebustd5122202009-06-17 13:22:58 -07004212 nfs4_schedule_state_recovery(server->nfs_client);
4213 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004214 case -ERESTARTSYS:
4215 /*
4216 * The show must go on: exit, but mark the
4217 * stateid as needing recovery.
4218 */
4219 case -NFS4ERR_ADMIN_REVOKED:
4220 case -NFS4ERR_BAD_STATEID:
4221 case -NFS4ERR_OPENMODE:
4222 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4223 err = 0;
4224 goto out;
4225 case -ENOMEM:
4226 case -NFS4ERR_DENIED:
4227 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4228 err = 0;
4229 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004230 case -NFS4ERR_DELAY:
4231 break;
4232 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004233 err = nfs4_handle_exception(server, err, &exception);
4234 } while (exception.retry);
4235out:
4236 return err;
4237}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004238
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004239#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4240
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004241int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4242 size_t buflen, int flags)
4243{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004244 struct inode *inode = dentry->d_inode;
4245
4246 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4247 return -EOPNOTSUPP;
4248
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004249 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004250}
4251
4252/* The getxattr man page suggests returning -ENODATA for unknown attributes,
4253 * and that's what we'll do for e.g. user attributes that haven't been set.
4254 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4255 * attributes in kernel-managed attribute namespaces. */
4256ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4257 size_t buflen)
4258{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004259 struct inode *inode = dentry->d_inode;
4260
4261 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4262 return -EOPNOTSUPP;
4263
4264 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004265}
4266
4267ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4268{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004269 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004270
J. Bruce Fields096455a2006-03-20 23:23:42 -05004271 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4272 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004273 if (buf && buflen < len)
4274 return -ERANGE;
4275 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004276 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4277 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004278}
4279
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004280static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4281{
4282 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4283 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4284 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4285 return;
4286
4287 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4288 NFS_ATTR_FATTR_NLINK;
4289 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4290 fattr->nlink = 2;
4291}
4292
Trond Myklebust56659e92007-07-17 21:52:39 -04004293int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004294 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004295{
4296 struct nfs_server *server = NFS_SERVER(dir);
4297 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004298 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4299 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004300 };
4301 struct nfs4_fs_locations_arg args = {
4302 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004303 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004304 .page = page,
4305 .bitmask = bitmask,
4306 };
Benny Halevy22958462009-04-01 09:22:02 -04004307 struct nfs4_fs_locations_res res = {
4308 .fs_locations = fs_locations,
4309 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004310 struct rpc_message msg = {
4311 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4312 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004313 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004314 };
4315 int status;
4316
Harvey Harrison3110ff82008-05-02 13:42:44 -07004317 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004318 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004319 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004320 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04004321 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004322 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004323 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004324 return status;
4325}
4326
Andy Adamson557134a2009-04-01 09:21:53 -04004327#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004328/*
4329 * nfs4_proc_exchange_id()
4330 *
4331 * Since the clientid has expired, all compounds using sessions
4332 * associated with the stale clientid will be returning
4333 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4334 * be in some phase of session reset.
4335 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004336int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004337{
4338 nfs4_verifier verifier;
4339 struct nfs41_exchange_id_args args = {
4340 .client = clp,
4341 .flags = clp->cl_exchange_flags,
4342 };
4343 struct nfs41_exchange_id_res res = {
4344 .client = clp,
4345 };
4346 int status;
4347 struct rpc_message msg = {
4348 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4349 .rpc_argp = &args,
4350 .rpc_resp = &res,
4351 .rpc_cred = cred,
4352 };
4353 __be32 *p;
4354
4355 dprintk("--> %s\n", __func__);
4356 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004357
Alexandros Batsakis7b183d02009-12-05 13:33:25 -05004358 /* Remove server-only flags */
4359 args.flags &= ~EXCHGID4_FLAG_CONFIRMED_R;
4360
Benny Halevy99fe60d2009-04-01 09:22:29 -04004361 p = (u32 *)verifier.data;
4362 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4363 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4364 args.verifier = &verifier;
4365
4366 while (1) {
4367 args.id_len = scnprintf(args.id, sizeof(args.id),
4368 "%s/%s %u",
4369 clp->cl_ipaddr,
4370 rpc_peeraddr2str(clp->cl_rpcclient,
4371 RPC_DISPLAY_ADDR),
4372 clp->cl_id_uniquifier);
4373
4374 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4375
4376 if (status != NFS4ERR_CLID_INUSE)
4377 break;
4378
4379 if (signalled())
4380 break;
4381
4382 if (++clp->cl_id_uniquifier == 0)
4383 break;
4384 }
4385
4386 dprintk("<-- %s status= %d\n", __func__, status);
4387 return status;
4388}
4389
Andy Adamson2050f0c2009-04-01 09:22:30 -04004390struct nfs4_get_lease_time_data {
4391 struct nfs4_get_lease_time_args *args;
4392 struct nfs4_get_lease_time_res *res;
4393 struct nfs_client *clp;
4394};
4395
4396static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4397 void *calldata)
4398{
4399 int ret;
4400 struct nfs4_get_lease_time_data *data =
4401 (struct nfs4_get_lease_time_data *)calldata;
4402
4403 dprintk("--> %s\n", __func__);
4404 /* just setup sequence, do not trigger session recovery
4405 since we're invoked within one */
4406 ret = nfs41_setup_sequence(data->clp->cl_session,
4407 &data->args->la_seq_args,
4408 &data->res->lr_seq_res, 0, task);
4409
4410 BUG_ON(ret == -EAGAIN);
4411 rpc_call_start(task);
4412 dprintk("<-- %s\n", __func__);
4413}
4414
4415/*
4416 * Called from nfs4_state_manager thread for session setup, so don't recover
4417 * from sequence operation or clientid errors.
4418 */
4419static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4420{
4421 struct nfs4_get_lease_time_data *data =
4422 (struct nfs4_get_lease_time_data *)calldata;
4423
4424 dprintk("--> %s\n", __func__);
4425 nfs41_sequence_done(data->clp, &data->res->lr_seq_res, task->tk_status);
4426 switch (task->tk_status) {
4427 case -NFS4ERR_DELAY:
4428 case -NFS4ERR_GRACE:
4429 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4430 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4431 task->tk_status = 0;
Trond Myklebust0110ee12009-12-07 09:00:24 -05004432 nfs_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004433 return;
4434 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004435 dprintk("<-- %s\n", __func__);
4436}
4437
4438struct rpc_call_ops nfs4_get_lease_time_ops = {
4439 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4440 .rpc_call_done = nfs4_get_lease_time_done,
4441};
4442
4443int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4444{
4445 struct rpc_task *task;
4446 struct nfs4_get_lease_time_args args;
4447 struct nfs4_get_lease_time_res res = {
4448 .lr_fsinfo = fsinfo,
4449 };
4450 struct nfs4_get_lease_time_data data = {
4451 .args = &args,
4452 .res = &res,
4453 .clp = clp,
4454 };
4455 struct rpc_message msg = {
4456 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4457 .rpc_argp = &args,
4458 .rpc_resp = &res,
4459 };
4460 struct rpc_task_setup task_setup = {
4461 .rpc_client = clp->cl_rpcclient,
4462 .rpc_message = &msg,
4463 .callback_ops = &nfs4_get_lease_time_ops,
4464 .callback_data = &data
4465 };
4466 int status;
4467
4468 res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
4469 dprintk("--> %s\n", __func__);
4470 task = rpc_run_task(&task_setup);
4471
4472 if (IS_ERR(task))
4473 status = PTR_ERR(task);
4474 else {
4475 status = task->tk_status;
4476 rpc_put_task(task);
4477 }
4478 dprintk("<-- %s return %d\n", __func__, status);
4479
4480 return status;
4481}
4482
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004483/*
4484 * Reset a slot table
4485 */
4486static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, int max_slots,
4487 int old_max_slots, int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004488{
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004489 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004490 int ret = 0;
4491
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004492 dprintk("--> %s: max_reqs=%u, tbl %p\n", __func__, max_slots, tbl);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004493
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004494 /*
4495 * Until we have dynamic slot table adjustment, insist
4496 * upon the same slot table size
4497 */
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004498 if (max_slots != old_max_slots) {
4499 dprintk("%s reset slot table does't match old\n",
4500 __func__);
4501 ret = -EINVAL; /*XXX NFS4ERR_REQ_TOO_BIG ? */
4502 goto out;
4503 }
4504 spin_lock(&tbl->slot_tbl_lock);
4505 for (i = 0; i < max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004506 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004507 spin_unlock(&tbl->slot_tbl_lock);
4508 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4509 tbl, tbl->slots, tbl->max_slots);
4510out:
4511 dprintk("<-- %s: return %d\n", __func__, ret);
4512 return ret;
4513}
4514
Andy Adamsonfc931582009-04-01 09:22:31 -04004515/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004516 * Reset the forechannel and backchannel slot tables
4517 */
4518static int nfs4_reset_slot_tables(struct nfs4_session *session)
4519{
4520 int status;
4521
4522 status = nfs4_reset_slot_table(&session->fc_slot_table,
4523 session->fc_attrs.max_reqs,
4524 session->fc_slot_table.max_slots,
4525 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004526 if (status)
4527 return status;
4528
4529 status = nfs4_reset_slot_table(&session->bc_slot_table,
4530 session->bc_attrs.max_reqs,
4531 session->bc_slot_table.max_slots,
4532 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004533 return status;
4534}
4535
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004536/* Destroy the slot table */
4537static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4538{
4539 if (session->fc_slot_table.slots != NULL) {
4540 kfree(session->fc_slot_table.slots);
4541 session->fc_slot_table.slots = NULL;
4542 }
4543 if (session->bc_slot_table.slots != NULL) {
4544 kfree(session->bc_slot_table.slots);
4545 session->bc_slot_table.slots = NULL;
4546 }
4547 return;
4548}
4549
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004550/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004551 * Initialize slot table
4552 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004553static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4554 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04004555{
Andy Adamsonfc931582009-04-01 09:22:31 -04004556 struct nfs4_slot *slot;
4557 int ret = -ENOMEM;
4558
4559 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4560
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004561 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04004562
4563 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_KERNEL);
4564 if (!slot)
4565 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04004566 ret = 0;
4567
4568 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04004569 tbl->max_slots = max_slots;
4570 tbl->slots = slot;
4571 tbl->highest_used_slotid = -1; /* no slot is currently used */
4572 spin_unlock(&tbl->slot_tbl_lock);
4573 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4574 tbl, tbl->slots, tbl->max_slots);
4575out:
4576 dprintk("<-- %s: return %d\n", __func__, ret);
4577 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04004578}
4579
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004580/*
4581 * Initialize the forechannel and backchannel tables
4582 */
4583static int nfs4_init_slot_tables(struct nfs4_session *session)
4584{
Trond Myklebustf26468f2009-12-05 19:32:11 -05004585 struct nfs4_slot_table *tbl;
4586 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004587
Trond Myklebustf26468f2009-12-05 19:32:11 -05004588 tbl = &session->fc_slot_table;
4589 if (tbl->slots == NULL) {
4590 status = nfs4_init_slot_table(tbl,
4591 session->fc_attrs.max_reqs, 1);
4592 if (status)
4593 return status;
4594 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004595
Trond Myklebustf26468f2009-12-05 19:32:11 -05004596 tbl = &session->bc_slot_table;
4597 if (tbl->slots == NULL) {
4598 status = nfs4_init_slot_table(tbl,
4599 session->bc_attrs.max_reqs, 0);
4600 if (status)
4601 nfs4_destroy_slot_tables(session);
4602 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004603
4604 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04004605}
4606
4607struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4608{
4609 struct nfs4_session *session;
4610 struct nfs4_slot_table *tbl;
4611
4612 session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
4613 if (!session)
4614 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04004615
Andy Adamson76db6d92009-04-01 09:22:38 -04004616 /*
4617 * The create session reply races with the server back
4618 * channel probe. Mark the client NFS_CS_SESSION_INITING
4619 * so that the client back channel can find the
4620 * nfs_client struct
4621 */
4622 clp->cl_cons_state = NFS_CS_SESSION_INITING;
Andy Adamsonea028ac2009-12-04 15:55:38 -05004623 init_completion(&session->complete);
Andy Adamson76db6d92009-04-01 09:22:38 -04004624
Andy Adamson557134a2009-04-01 09:21:53 -04004625 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004626 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04004627 spin_lock_init(&tbl->slot_tbl_lock);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004628 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
4629
4630 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004631 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004632 spin_lock_init(&tbl->slot_tbl_lock);
4633 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4634
Andy Adamson557134a2009-04-01 09:21:53 -04004635 session->clp = clp;
4636 return session;
4637}
4638
4639void nfs4_destroy_session(struct nfs4_session *session)
4640{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04004641 nfs4_proc_destroy_session(session);
4642 dprintk("%s Destroy backchannel for xprt %p\n",
4643 __func__, session->clp->cl_rpcclient->cl_xprt);
4644 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4645 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004646 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04004647 kfree(session);
4648}
4649
Andy Adamsonfc931582009-04-01 09:22:31 -04004650/*
4651 * Initialize the values to be used by the client in CREATE_SESSION
4652 * If nfs4_init_session set the fore channel request and response sizes,
4653 * use them.
4654 *
4655 * Set the back channel max_resp_sz_cached to zero to force the client to
4656 * always set csa_cachethis to FALSE because the current implementation
4657 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4658 */
4659static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4660{
4661 struct nfs4_session *session = args->client->cl_session;
4662 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4663 mxresp_sz = session->fc_attrs.max_resp_sz;
4664
4665 if (mxrqst_sz == 0)
4666 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4667 if (mxresp_sz == 0)
4668 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4669 /* Fore channel attributes */
4670 args->fc_attrs.headerpadsz = 0;
4671 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4672 args->fc_attrs.max_resp_sz = mxresp_sz;
4673 args->fc_attrs.max_resp_sz_cached = mxresp_sz;
4674 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4675 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4676
4677 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
4678 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4679 __func__,
4680 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
4681 args->fc_attrs.max_resp_sz_cached, args->fc_attrs.max_ops,
4682 args->fc_attrs.max_reqs);
4683
4684 /* Back channel attributes */
4685 args->bc_attrs.headerpadsz = 0;
4686 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4687 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4688 args->bc_attrs.max_resp_sz_cached = 0;
4689 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4690 args->bc_attrs.max_reqs = 1;
4691
4692 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4693 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4694 __func__,
4695 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4696 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4697 args->bc_attrs.max_reqs);
4698}
4699
Andy Adamson8d353012009-04-01 09:22:32 -04004700static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
4701{
4702 if (rcvd <= sent)
4703 return 0;
4704 printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
4705 "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
4706 return -EINVAL;
4707}
4708
4709#define _verify_fore_channel_attr(_name_) \
4710 _verify_channel_attr("fore", #_name_, \
4711 args->fc_attrs._name_, \
4712 session->fc_attrs._name_)
4713
4714#define _verify_back_channel_attr(_name_) \
4715 _verify_channel_attr("back", #_name_, \
4716 args->bc_attrs._name_, \
4717 session->bc_attrs._name_)
4718
4719/*
4720 * The server is not allowed to increase the fore channel header pad size,
4721 * maximum response size, or maximum number of operations.
4722 *
4723 * The back channel attributes are only negotiatied down: We send what the
4724 * (back channel) server insists upon.
4725 */
4726static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4727 struct nfs4_session *session)
4728{
4729 int ret = 0;
4730
4731 ret |= _verify_fore_channel_attr(headerpadsz);
4732 ret |= _verify_fore_channel_attr(max_resp_sz);
4733 ret |= _verify_fore_channel_attr(max_ops);
4734
4735 ret |= _verify_back_channel_attr(headerpadsz);
4736 ret |= _verify_back_channel_attr(max_rqst_sz);
4737 ret |= _verify_back_channel_attr(max_resp_sz);
4738 ret |= _verify_back_channel_attr(max_resp_sz_cached);
4739 ret |= _verify_back_channel_attr(max_ops);
4740 ret |= _verify_back_channel_attr(max_reqs);
4741
4742 return ret;
4743}
4744
Andy Adamsonfc931582009-04-01 09:22:31 -04004745static int _nfs4_proc_create_session(struct nfs_client *clp)
4746{
4747 struct nfs4_session *session = clp->cl_session;
4748 struct nfs41_create_session_args args = {
4749 .client = clp,
4750 .cb_program = NFS4_CALLBACK,
4751 };
4752 struct nfs41_create_session_res res = {
4753 .client = clp,
4754 };
4755 struct rpc_message msg = {
4756 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4757 .rpc_argp = &args,
4758 .rpc_resp = &res,
4759 };
4760 int status;
4761
4762 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04004763 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04004764
4765 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4766
Andy Adamson8d353012009-04-01 09:22:32 -04004767 if (!status)
4768 /* Verify the session's negotiated channel_attrs values */
4769 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04004770 if (!status) {
4771 /* Increment the clientid slot sequence id */
4772 clp->cl_seqid++;
4773 }
4774
4775 return status;
4776}
4777
4778/*
4779 * Issues a CREATE_SESSION operation to the server.
4780 * It is the responsibility of the caller to verify the session is
4781 * expired before calling this routine.
4782 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05004783int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04004784{
4785 int status;
4786 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04004787 struct nfs4_session *session = clp->cl_session;
4788
4789 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
4790
4791 status = _nfs4_proc_create_session(clp);
4792 if (status)
4793 goto out;
4794
Trond Myklebustf26468f2009-12-05 19:32:11 -05004795 /* Init and reset the fore channel */
4796 status = nfs4_init_slot_tables(session);
4797 dprintk("slot table initialization returned %d\n", status);
4798 if (status)
4799 goto out;
4800 status = nfs4_reset_slot_tables(session);
4801 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04004802 if (status)
4803 goto out;
4804
4805 ptr = (unsigned *)&session->sess_id.data[0];
4806 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
4807 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04004808out:
4809 dprintk("<-- %s\n", __func__);
4810 return status;
4811}
4812
Andy Adamson0f3e66c2009-04-01 09:22:34 -04004813/*
4814 * Issue the over-the-wire RPC DESTROY_SESSION.
4815 * The caller must serialize access to this routine.
4816 */
4817int nfs4_proc_destroy_session(struct nfs4_session *session)
4818{
4819 int status = 0;
4820 struct rpc_message msg;
4821
4822 dprintk("--> nfs4_proc_destroy_session\n");
4823
4824 /* session is still being setup */
4825 if (session->clp->cl_cons_state != NFS_CS_READY)
4826 return status;
4827
4828 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
4829 msg.rpc_argp = session;
4830 msg.rpc_resp = NULL;
4831 msg.rpc_cred = NULL;
4832 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4833
4834 if (status)
4835 printk(KERN_WARNING
4836 "Got error %d from the server on DESTROY_SESSION. "
4837 "Session has been destroyed regardless...\n", status);
4838
4839 dprintk("<-- nfs4_proc_destroy_session\n");
4840 return status;
4841}
4842
Trond Myklebustfccba802009-07-21 16:48:07 -04004843int nfs4_init_session(struct nfs_server *server)
4844{
4845 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05004846 struct nfs4_session *session;
Trond Myklebustfccba802009-07-21 16:48:07 -04004847 int ret;
4848
4849 if (!nfs4_has_session(clp))
4850 return 0;
4851
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05004852 session = clp->cl_session;
4853 session->fc_attrs.max_rqst_sz = server->wsize + nfs41_maxwrite_overhead;
4854 session->fc_attrs.max_resp_sz = server->rsize + nfs41_maxread_overhead;
4855
Trond Myklebustfccba802009-07-21 16:48:07 -04004856 ret = nfs4_recover_expired_lease(server);
4857 if (!ret)
4858 ret = nfs4_check_client_ready(clp);
4859 return ret;
4860}
4861
Andy Adamsonfc01cea2009-04-01 09:22:36 -04004862/*
4863 * Renew the cl_session lease.
4864 */
4865static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
4866{
4867 struct nfs4_sequence_args args;
4868 struct nfs4_sequence_res res;
4869
4870 struct rpc_message msg = {
4871 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
4872 .rpc_argp = &args,
4873 .rpc_resp = &res,
4874 .rpc_cred = cred,
4875 };
4876
4877 args.sa_cache_this = 0;
4878
4879 return nfs4_call_sync_sequence(clp, clp->cl_rpcclient, &msg, &args,
4880 &res, 0);
4881}
4882
4883void nfs41_sequence_call_done(struct rpc_task *task, void *data)
4884{
4885 struct nfs_client *clp = (struct nfs_client *)data;
4886
4887 nfs41_sequence_done(clp, task->tk_msg.rpc_resp, task->tk_status);
4888
4889 if (task->tk_status < 0) {
4890 dprintk("%s ERROR %d\n", __func__, task->tk_status);
4891
4892 if (_nfs4_async_handle_error(task, NULL, clp, NULL)
4893 == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05004894 nfs_restart_rpc(task, clp);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04004895 return;
4896 }
4897 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04004898 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
4899
Andy Adamsonfc01cea2009-04-01 09:22:36 -04004900 kfree(task->tk_msg.rpc_argp);
4901 kfree(task->tk_msg.rpc_resp);
4902
4903 dprintk("<-- %s\n", __func__);
4904}
4905
4906static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
4907{
4908 struct nfs_client *clp;
4909 struct nfs4_sequence_args *args;
4910 struct nfs4_sequence_res *res;
4911
4912 clp = (struct nfs_client *)data;
4913 args = task->tk_msg.rpc_argp;
4914 res = task->tk_msg.rpc_resp;
4915
4916 if (nfs4_setup_sequence(clp, args, res, 0, task))
4917 return;
4918 rpc_call_start(task);
4919}
4920
4921static const struct rpc_call_ops nfs41_sequence_ops = {
4922 .rpc_call_done = nfs41_sequence_call_done,
4923 .rpc_call_prepare = nfs41_sequence_prepare,
4924};
4925
4926static int nfs41_proc_async_sequence(struct nfs_client *clp,
4927 struct rpc_cred *cred)
4928{
4929 struct nfs4_sequence_args *args;
4930 struct nfs4_sequence_res *res;
4931 struct rpc_message msg = {
4932 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
4933 .rpc_cred = cred,
4934 };
4935
4936 args = kzalloc(sizeof(*args), GFP_KERNEL);
4937 if (!args)
4938 return -ENOMEM;
4939 res = kzalloc(sizeof(*res), GFP_KERNEL);
4940 if (!res) {
4941 kfree(args);
4942 return -ENOMEM;
4943 }
4944 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
4945 msg.rpc_argp = args;
4946 msg.rpc_resp = res;
4947
4948 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
4949 &nfs41_sequence_ops, (void *)clp);
4950}
4951
Ricardo Labiagafce5c832009-12-05 16:08:41 -05004952struct nfs4_reclaim_complete_data {
4953 struct nfs_client *clp;
4954 struct nfs41_reclaim_complete_args arg;
4955 struct nfs41_reclaim_complete_res res;
4956};
4957
4958static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
4959{
4960 struct nfs4_reclaim_complete_data *calldata = data;
4961
4962 if (nfs4_setup_sequence(calldata->clp, &calldata->arg.seq_args,
4963 &calldata->res.seq_res, 0, task))
4964 return;
4965
4966 rpc_call_start(task);
4967}
4968
4969static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
4970{
4971 struct nfs4_reclaim_complete_data *calldata = data;
4972 struct nfs_client *clp = calldata->clp;
4973 struct nfs4_sequence_res *res = &calldata->res.seq_res;
4974
4975 dprintk("--> %s\n", __func__);
4976 nfs41_sequence_done(clp, res, task->tk_status);
4977 switch (task->tk_status) {
4978 case 0:
4979 case -NFS4ERR_COMPLETE_ALREADY:
4980 break;
4981 case -NFS4ERR_BADSESSION:
4982 case -NFS4ERR_DEADSESSION:
4983 /*
4984 * Handle the session error, but do not retry the operation, as
4985 * we have no way of telling whether the clientid had to be
4986 * reset before we got our reply. If reset, a new wave of
4987 * reclaim operations will follow, containing their own reclaim
4988 * complete. We don't want our retry to get on the way of
4989 * recovery by incorrectly indicating to the server that we're
4990 * done reclaiming state since the process had to be restarted.
4991 */
4992 _nfs4_async_handle_error(task, NULL, clp, NULL);
4993 break;
4994 default:
4995 if (_nfs4_async_handle_error(
4996 task, NULL, clp, NULL) == -EAGAIN) {
4997 rpc_restart_call_prepare(task);
4998 return;
4999 }
5000 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005001
5002 dprintk("<-- %s\n", __func__);
5003}
5004
5005static void nfs4_free_reclaim_complete_data(void *data)
5006{
5007 struct nfs4_reclaim_complete_data *calldata = data;
5008
5009 kfree(calldata);
5010}
5011
5012static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5013 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5014 .rpc_call_done = nfs4_reclaim_complete_done,
5015 .rpc_release = nfs4_free_reclaim_complete_data,
5016};
5017
5018/*
5019 * Issue a global reclaim complete.
5020 */
5021static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5022{
5023 struct nfs4_reclaim_complete_data *calldata;
5024 struct rpc_task *task;
5025 struct rpc_message msg = {
5026 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5027 };
5028 struct rpc_task_setup task_setup_data = {
5029 .rpc_client = clp->cl_rpcclient,
5030 .rpc_message = &msg,
5031 .callback_ops = &nfs4_reclaim_complete_call_ops,
5032 .flags = RPC_TASK_ASYNC,
5033 };
5034 int status = -ENOMEM;
5035
5036 dprintk("--> %s\n", __func__);
5037 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
5038 if (calldata == NULL)
5039 goto out;
5040 calldata->clp = clp;
5041 calldata->arg.one_fs = 0;
5042 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
5043
5044 msg.rpc_argp = &calldata->arg;
5045 msg.rpc_resp = &calldata->res;
5046 task_setup_data.callback_data = calldata;
5047 task = rpc_run_task(&task_setup_data);
5048 if (IS_ERR(task))
5049 status = PTR_ERR(task);
5050 rpc_put_task(task);
5051out:
5052 dprintk("<-- %s status=%d\n", __func__, status);
5053 return status;
5054}
Andy Adamson557134a2009-04-01 09:21:53 -04005055#endif /* CONFIG_NFS_V4_1 */
5056
Andy Adamson591d71c2009-04-01 09:22:47 -04005057struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005058 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005059 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 .recover_open = nfs4_open_reclaim,
5061 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04005062 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005063 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064};
5065
Andy Adamson591d71c2009-04-01 09:22:47 -04005066#if defined(CONFIG_NFS_V4_1)
5067struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5068 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5069 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5070 .recover_open = nfs4_open_reclaim,
5071 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05005072 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005073 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005074 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04005075};
5076#endif /* CONFIG_NFS_V4_1 */
5077
5078struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005079 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005080 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 .recover_open = nfs4_open_expired,
5082 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04005083 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005084 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085};
5086
Andy Adamson591d71c2009-04-01 09:22:47 -04005087#if defined(CONFIG_NFS_V4_1)
5088struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5089 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5090 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5091 .recover_open = nfs4_open_expired,
5092 .recover_lock = nfs4_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05005093 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005094 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04005095};
5096#endif /* CONFIG_NFS_V4_1 */
5097
Benny Halevy29fba382009-04-01 09:22:44 -04005098struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5099 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04005100 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005101 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04005102};
5103
5104#if defined(CONFIG_NFS_V4_1)
5105struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5106 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04005107 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005108 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04005109};
5110#endif
5111
5112/*
5113 * Per minor version reboot and network partition recovery ops
5114 */
5115
Andy Adamson591d71c2009-04-01 09:22:47 -04005116struct nfs4_state_recovery_ops *nfs4_reboot_recovery_ops[] = {
5117 &nfs40_reboot_recovery_ops,
5118#if defined(CONFIG_NFS_V4_1)
5119 &nfs41_reboot_recovery_ops,
5120#endif
5121};
5122
5123struct nfs4_state_recovery_ops *nfs4_nograce_recovery_ops[] = {
5124 &nfs40_nograce_recovery_ops,
5125#if defined(CONFIG_NFS_V4_1)
5126 &nfs41_nograce_recovery_ops,
5127#endif
5128};
5129
Benny Halevy29fba382009-04-01 09:22:44 -04005130struct nfs4_state_maintenance_ops *nfs4_state_renewal_ops[] = {
5131 &nfs40_state_renewal_ops,
5132#if defined(CONFIG_NFS_V4_1)
5133 &nfs41_state_renewal_ops,
5134#endif
5135};
5136
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005137static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005138 .permission = nfs_permission,
5139 .getattr = nfs_getattr,
5140 .setattr = nfs_setattr,
5141 .getxattr = nfs4_getxattr,
5142 .setxattr = nfs4_setxattr,
5143 .listxattr = nfs4_listxattr,
5144};
5145
David Howells509de812006-08-22 20:06:11 -04005146const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147 .version = 4, /* protocol version */
5148 .dentry_ops = &nfs4_dentry_operations,
5149 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005150 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151 .getroot = nfs4_proc_get_root,
5152 .getattr = nfs4_proc_getattr,
5153 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04005154 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 .lookup = nfs4_proc_lookup,
5156 .access = nfs4_proc_access,
5157 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 .create = nfs4_proc_create,
5159 .remove = nfs4_proc_remove,
5160 .unlink_setup = nfs4_proc_unlink_setup,
5161 .unlink_done = nfs4_proc_unlink_done,
5162 .rename = nfs4_proc_rename,
5163 .link = nfs4_proc_link,
5164 .symlink = nfs4_proc_symlink,
5165 .mkdir = nfs4_proc_mkdir,
5166 .rmdir = nfs4_proc_remove,
5167 .readdir = nfs4_proc_readdir,
5168 .mknod = nfs4_proc_mknod,
5169 .statfs = nfs4_proc_statfs,
5170 .fsinfo = nfs4_proc_fsinfo,
5171 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04005172 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173 .decode_dirent = nfs4_decode_dirent,
5174 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05005175 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005177 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005179 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005181 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04005182 .close_context = nfs4_close_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183};
5184
5185/*
5186 * Local variables:
5187 * c-basic-offset: 8
5188 * End:
5189 */