blob: 6ef50aa785d599701b030aafc5b33b5fde65990a [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 Adamson6df08182009-12-04 15:55:05 -0500273 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
Andy Adamson4745e312009-04-01 09:22:42 -0400274 exception->retry = 1;
275 /* FALLTHROUGH */
276#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.
321 */
322static void
323nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid)
324{
325 int slotid = free_slotid;
326
327 spin_lock(&tbl->slot_tbl_lock);
328 /* clear used bit in bitmap */
329 __clear_bit(slotid, tbl->used_slots);
330
331 /* update highest_used_slotid when it is freed */
332 if (slotid == tbl->highest_used_slotid) {
333 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
334 if (slotid >= 0 && slotid < tbl->max_slots)
335 tbl->highest_used_slotid = slotid;
336 else
337 tbl->highest_used_slotid = -1;
338 }
339 rpc_wake_up_next(&tbl->slot_tbl_waitq);
340 spin_unlock(&tbl->slot_tbl_lock);
341 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
342 free_slotid, tbl->highest_used_slotid);
343}
344
Andy Adamson13615872009-04-01 09:22:17 -0400345void nfs41_sequence_free_slot(const struct nfs_client *clp,
346 struct nfs4_sequence_res *res)
347{
348 struct nfs4_slot_table *tbl;
349
350 if (!nfs4_has_session(clp)) {
351 dprintk("%s: No session\n", __func__);
352 return;
353 }
354 tbl = &clp->cl_session->fc_slot_table;
355 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE) {
356 dprintk("%s: No slot\n", __func__);
357 /* just wake up the next guy waiting since
358 * we may have not consumed a slot after all */
359 rpc_wake_up_next(&tbl->slot_tbl_waitq);
360 return;
361 }
362 nfs4_free_slot(tbl, res->sr_slotid);
363 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
364}
365
Andy Adamsonb0df8062009-04-01 09:22:18 -0400366static void nfs41_sequence_done(struct nfs_client *clp,
367 struct nfs4_sequence_res *res,
368 int rpc_status)
369{
370 unsigned long timestamp;
371 struct nfs4_slot_table *tbl;
372 struct nfs4_slot *slot;
373
374 /*
375 * sr_status remains 1 if an RPC level error occurred. The server
376 * may or may not have processed the sequence operation..
377 * Proceed as if the server received and processed the sequence
378 * operation.
379 */
380 if (res->sr_status == 1)
381 res->sr_status = NFS_OK;
382
383 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
384 if (res->sr_slotid == NFS4_MAX_SLOT_TABLE)
385 goto out;
386
387 tbl = &clp->cl_session->fc_slot_table;
388 slot = tbl->slots + res->sr_slotid;
389
390 if (res->sr_status == 0) {
391 /* Update the slot's sequence and clientid lease timer */
392 ++slot->seq_nr;
393 timestamp = res->sr_renewal_time;
394 spin_lock(&clp->cl_lock);
395 if (time_before(clp->cl_last_renewal, timestamp))
396 clp->cl_last_renewal = timestamp;
397 spin_unlock(&clp->cl_lock);
398 return;
399 }
400out:
401 /* The session may be reset by one of the error handlers. */
402 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
403 nfs41_sequence_free_slot(clp, res);
404}
405
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400406/*
Benny Halevy510b8172009-04-01 09:22:14 -0400407 * nfs4_find_slot - efficiently look for a free slot
408 *
409 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
410 * If found, we mark the slot as used, update the highest_used_slotid,
411 * and respectively set up the sequence operation args.
412 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400413 *
414 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400415 */
416static u8
417nfs4_find_slot(struct nfs4_slot_table *tbl, struct rpc_task *task)
418{
419 int slotid;
420 u8 ret_id = NFS4_MAX_SLOT_TABLE;
421 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
422
Benny Halevy510b8172009-04-01 09:22:14 -0400423 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
424 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
425 tbl->max_slots);
426 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
427 if (slotid >= tbl->max_slots)
428 goto out;
429 __set_bit(slotid, tbl->used_slots);
430 if (slotid > tbl->highest_used_slotid)
431 tbl->highest_used_slotid = slotid;
432 ret_id = slotid;
433out:
434 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
435 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400436 return ret_id;
437}
438
Andy Adamsonb069d942009-04-01 09:22:43 -0400439static int nfs4_recover_session(struct nfs4_session *session)
440{
441 struct nfs_client *clp = session->clp;
Trond Myklebusta78cb572009-08-09 15:06:19 -0400442 unsigned int loop;
Andy Adamsonb069d942009-04-01 09:22:43 -0400443 int ret;
444
Trond Myklebusta78cb572009-08-09 15:06:19 -0400445 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Andy Adamsonb069d942009-04-01 09:22:43 -0400446 ret = nfs4_wait_clnt_recover(clp);
447 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -0400448 break;
Andy Adamson6df08182009-12-04 15:55:05 -0500449 if (!test_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state))
Andy Adamsonb069d942009-04-01 09:22:43 -0400450 break;
451 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -0400452 ret = -EIO;
Andy Adamsonb069d942009-04-01 09:22:43 -0400453 }
Trond Myklebusta78cb572009-08-09 15:06:19 -0400454 return ret;
Andy Adamsonb069d942009-04-01 09:22:43 -0400455}
456
Andy Adamsonce5039c2009-04-01 09:22:13 -0400457static int nfs41_setup_sequence(struct nfs4_session *session,
458 struct nfs4_sequence_args *args,
459 struct nfs4_sequence_res *res,
460 int cache_reply,
461 struct rpc_task *task)
462{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400463 struct nfs4_slot *slot;
464 struct nfs4_slot_table *tbl;
Andy Adamsonb069d942009-04-01 09:22:43 -0400465 int status = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400466 u8 slotid;
467
468 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400469 /* slot already allocated? */
470 if (res->sr_slotid != NFS4_MAX_SLOT_TABLE)
471 return 0;
472
473 memset(res, 0, sizeof(*res));
474 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400475 tbl = &session->fc_slot_table;
476
477 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson6df08182009-12-04 15:55:05 -0500478 if (test_bit(NFS4CLNT_SESSION_RESET, &session->clp->cl_state)) {
Andy Adamsonb069d942009-04-01 09:22:43 -0400479 if (tbl->highest_used_slotid != -1) {
480 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
481 spin_unlock(&tbl->slot_tbl_lock);
482 dprintk("<-- %s: Session reset: draining\n", __func__);
483 return -EAGAIN;
484 }
485
486 /* The slot table is empty; start the reset thread */
487 dprintk("%s Session Reset\n", __func__);
488 spin_unlock(&tbl->slot_tbl_lock);
489 status = nfs4_recover_session(session);
490 if (status)
491 return status;
492 spin_lock(&tbl->slot_tbl_lock);
493 }
494
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400495 slotid = nfs4_find_slot(tbl, task);
496 if (slotid == NFS4_MAX_SLOT_TABLE) {
497 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
498 spin_unlock(&tbl->slot_tbl_lock);
499 dprintk("<-- %s: no free slots\n", __func__);
500 return -EAGAIN;
501 }
502 spin_unlock(&tbl->slot_tbl_lock);
503
504 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400505 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400506 args->sa_slotid = slotid;
507 args->sa_cache_this = cache_reply;
508
509 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
510
Benny Halevy99fe60d2009-04-01 09:22:29 -0400511 res->sr_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400512 res->sr_slotid = slotid;
513 res->sr_renewal_time = jiffies;
514 /*
515 * sr_status is only set in decode_sequence, and so will remain
516 * set to 1 if an rpc level failure occurs.
517 */
518 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400519 return 0;
520}
521
522int nfs4_setup_sequence(struct nfs_client *clp,
523 struct nfs4_sequence_args *args,
524 struct nfs4_sequence_res *res,
525 int cache_reply,
526 struct rpc_task *task)
527{
528 int ret = 0;
529
530 dprintk("--> %s clp %p session %p sr_slotid %d\n",
531 __func__, clp, clp->cl_session, res->sr_slotid);
532
533 if (!nfs4_has_session(clp))
534 goto out;
535 ret = nfs41_setup_sequence(clp->cl_session, args, res, cache_reply,
536 task);
537 if (ret != -EAGAIN) {
538 /* terminate rpc task */
539 task->tk_status = ret;
540 task->tk_action = NULL;
541 }
542out:
543 dprintk("<-- %s status=%d\n", __func__, ret);
544 return ret;
545}
546
547struct nfs41_call_sync_data {
548 struct nfs_client *clp;
549 struct nfs4_sequence_args *seq_args;
550 struct nfs4_sequence_res *seq_res;
551 int cache_reply;
552};
553
554static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
555{
556 struct nfs41_call_sync_data *data = calldata;
557
558 dprintk("--> %s data->clp->cl_session %p\n", __func__,
559 data->clp->cl_session);
560 if (nfs4_setup_sequence(data->clp, data->seq_args,
561 data->seq_res, data->cache_reply, task))
562 return;
563 rpc_call_start(task);
564}
565
Andy Adamson69ab40c2009-04-01 09:22:19 -0400566static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
567{
568 struct nfs41_call_sync_data *data = calldata;
569
570 nfs41_sequence_done(data->clp, data->seq_res, task->tk_status);
571 nfs41_sequence_free_slot(data->clp, data->seq_res);
572}
573
Andy Adamsonce5039c2009-04-01 09:22:13 -0400574struct rpc_call_ops nfs41_call_sync_ops = {
575 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400576 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400577};
578
579static int nfs4_call_sync_sequence(struct nfs_client *clp,
580 struct rpc_clnt *clnt,
581 struct rpc_message *msg,
582 struct nfs4_sequence_args *args,
583 struct nfs4_sequence_res *res,
584 int cache_reply)
585{
586 int ret;
587 struct rpc_task *task;
588 struct nfs41_call_sync_data data = {
589 .clp = clp,
590 .seq_args = args,
591 .seq_res = res,
592 .cache_reply = cache_reply,
593 };
594 struct rpc_task_setup task_setup = {
595 .rpc_client = clnt,
596 .rpc_message = msg,
597 .callback_ops = &nfs41_call_sync_ops,
598 .callback_data = &data
599 };
600
601 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
602 task = rpc_run_task(&task_setup);
603 if (IS_ERR(task))
604 ret = PTR_ERR(task);
605 else {
606 ret = task->tk_status;
607 rpc_put_task(task);
608 }
609 return ret;
610}
611
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400612int _nfs4_call_sync_session(struct nfs_server *server,
613 struct rpc_message *msg,
614 struct nfs4_sequence_args *args,
615 struct nfs4_sequence_res *res,
616 int cache_reply)
617{
Andy Adamsonce5039c2009-04-01 09:22:13 -0400618 return nfs4_call_sync_sequence(server->nfs_client, server->client,
619 msg, args, res, cache_reply);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400620}
621
622#endif /* CONFIG_NFS_V4_1 */
623
624int _nfs4_call_sync(struct nfs_server *server,
625 struct rpc_message *msg,
626 struct nfs4_sequence_args *args,
627 struct nfs4_sequence_res *res,
628 int cache_reply)
629{
Benny Halevyf3752972009-04-01 09:22:04 -0400630 args->sa_session = res->sr_session = NULL;
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400631 return rpc_call_sync(server->client, msg, 0);
632}
633
634#define nfs4_call_sync(server, msg, args, res, cache_reply) \
635 (server)->nfs_client->cl_call_sync((server), (msg), &(args)->seq_args, \
636 &(res)->seq_res, (cache_reply))
637
Andy Adamsonb0df8062009-04-01 09:22:18 -0400638static void nfs4_sequence_done(const struct nfs_server *server,
639 struct nfs4_sequence_res *res, int rpc_status)
640{
641#ifdef CONFIG_NFS_V4_1
642 if (nfs4_has_session(server->nfs_client))
643 nfs41_sequence_done(server->nfs_client, res, rpc_status);
644#endif /* CONFIG_NFS_V4_1 */
645}
646
Andy Adamsone608e792009-12-04 15:55:29 -0500647void nfs4_restart_rpc(struct rpc_task *task, const struct nfs_client *clp,
648 struct nfs4_sequence_res *res)
649{
650#ifdef CONFIG_NFS_V4_1
651 if (nfs4_has_session(clp)) {
652 nfs41_sequence_free_slot(clp, res);
653 rpc_restart_call_prepare(task);
654 return;
655 }
656#endif /* CONFIG_NFS_V4_1 */
657 rpc_restart_call(task);
658}
659
Andy Adamsonb0df8062009-04-01 09:22:18 -0400660/* no restart, therefore free slot here */
661static void nfs4_sequence_done_free_slot(const struct nfs_server *server,
662 struct nfs4_sequence_res *res,
663 int rpc_status)
664{
665 nfs4_sequence_done(server, res, rpc_status);
666 nfs4_sequence_free_slot(server->nfs_client, res);
667}
668
Trond Myklebust38478b22006-05-25 01:40:57 -0400669static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Trond Myklebust38478b22006-05-25 01:40:57 -0400671 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Trond Myklebust38478b22006-05-25 01:40:57 -0400673 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400674 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
675 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400676 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400677 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400678 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679}
680
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100681struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400682 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100683 struct nfs_openargs o_arg;
684 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100685 struct nfs_open_confirmargs c_arg;
686 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100687 struct nfs_fattr f_attr;
688 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400689 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100690 struct dentry *dir;
691 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400692 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100693 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100694 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400695 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100696 int rpc_status;
697 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100698};
699
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400700
701static void nfs4_init_opendata_res(struct nfs4_opendata *p)
702{
703 p->o_res.f_attr = &p->f_attr;
704 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400705 p->o_res.seqid = p->o_arg.seqid;
706 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400707 p->o_res.server = p->o_arg.server;
708 nfs_fattr_init(&p->f_attr);
709 nfs_fattr_init(&p->dir_attr);
Benny Halevy578e4582009-06-18 22:01:23 -0400710 p->o_res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400711}
712
Trond Myklebustad389da2007-06-05 12:30:00 -0400713static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500714 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100715 const struct iattr *attrs)
716{
Trond Myklebustad389da2007-06-05 12:30:00 -0400717 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100718 struct inode *dir = parent->d_inode;
719 struct nfs_server *server = NFS_SERVER(dir);
720 struct nfs4_opendata *p;
721
722 p = kzalloc(sizeof(*p), GFP_KERNEL);
723 if (p == NULL)
724 goto err;
725 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid);
726 if (p->o_arg.seqid == NULL)
727 goto err_free;
Trond Myklebustad389da2007-06-05 12:30:00 -0400728 p->path.mnt = mntget(path->mnt);
729 p->path.dentry = dget(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100730 p->dir = parent;
731 p->owner = sp;
732 atomic_inc(&sp->so_count);
733 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500734 p->o_arg.open_flags = flags;
735 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400736 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400737 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400738 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100739 p->o_arg.server = server;
740 p->o_arg.bitmask = server->attr_bitmask;
741 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100742 if (flags & O_EXCL) {
743 u32 *s = (u32 *) p->o_arg.u.verifier.data;
744 s[0] = jiffies;
745 s[1] = current->pid;
746 } else if (flags & O_CREAT) {
747 p->o_arg.u.attrs = &p->attrs;
748 memcpy(&p->attrs, attrs, sizeof(p->attrs));
749 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100750 p->c_arg.fh = &p->o_res.fh;
751 p->c_arg.stateid = &p->o_res.stateid;
752 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400753 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400754 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100755 return p;
756err_free:
757 kfree(p);
758err:
759 dput(parent);
760 return NULL;
761}
762
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400763static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100764{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400765 struct nfs4_opendata *p = container_of(kref,
766 struct nfs4_opendata, kref);
767
768 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400769 if (p->state != NULL)
770 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400771 nfs4_put_state_owner(p->owner);
772 dput(p->dir);
Jan Blunck31f31db12008-05-02 13:42:45 -0700773 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400774 kfree(p);
775}
776
777static void nfs4_opendata_put(struct nfs4_opendata *p)
778{
779 if (p != NULL)
780 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100781}
782
Trond Myklebust06f814a2006-01-03 09:55:07 +0100783static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
784{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100785 int ret;
786
Trond Myklebust06f814a2006-01-03 09:55:07 +0100787 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100788 return ret;
789}
790
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500791static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400792{
793 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500794
795 if (open_mode & O_EXCL)
796 goto out;
797 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400798 case FMODE_READ:
799 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400800 break;
801 case FMODE_WRITE:
802 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400803 break;
804 case FMODE_READ|FMODE_WRITE:
805 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0;
806 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500807out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400808 return ret;
809}
810
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500811static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400812{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500813 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400814 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500815 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400816 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500817 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400818 return 1;
819}
820
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500821static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100822{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500823 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100824 case FMODE_WRITE:
825 state->n_wronly++;
826 break;
827 case FMODE_READ:
828 state->n_rdonly++;
829 break;
830 case FMODE_READ|FMODE_WRITE:
831 state->n_rdwr++;
832 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500833 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100834}
835
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500836static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400837{
838 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
839 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
840 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500841 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400842 case FMODE_READ:
843 set_bit(NFS_O_RDONLY_STATE, &state->flags);
844 break;
845 case FMODE_WRITE:
846 set_bit(NFS_O_WRONLY_STATE, &state->flags);
847 break;
848 case FMODE_READ|FMODE_WRITE:
849 set_bit(NFS_O_RDWR_STATE, &state->flags);
850 }
851}
852
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500853static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400854{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400855 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500856 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400857 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400858}
859
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500860static 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 -0700861{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400862 /*
863 * Protect the call to nfs4_state_set_mode_locked and
864 * serialise the stateid update
865 */
866 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400867 if (deleg_stateid != NULL) {
868 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
869 set_bit(NFS_DELEGATED_STATE, &state->flags);
870 }
871 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500872 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400873 write_sequnlock(&state->seqlock);
874 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500875 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700876 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877}
878
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500879static 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 -0500880{
881 struct nfs_inode *nfsi = NFS_I(state->inode);
882 struct nfs_delegation *deleg_cur;
883 int ret = 0;
884
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500885 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500886
887 rcu_read_lock();
888 deleg_cur = rcu_dereference(nfsi->delegation);
889 if (deleg_cur == NULL)
890 goto no_delegation;
891
892 spin_lock(&deleg_cur->lock);
893 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500894 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500895 goto no_delegation_unlock;
896
897 if (delegation == NULL)
898 delegation = &deleg_cur->stateid;
899 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
900 goto no_delegation_unlock;
901
Trond Myklebustb7391f42008-12-23 15:21:52 -0500902 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500903 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500904 ret = 1;
905no_delegation_unlock:
906 spin_unlock(&deleg_cur->lock);
907no_delegation:
908 rcu_read_unlock();
909
910 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500911 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500912 ret = 1;
913 }
914
915 return ret;
916}
917
918
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500919static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400920{
921 struct nfs_delegation *delegation;
922
923 rcu_read_lock();
924 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500925 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400926 rcu_read_unlock();
927 return;
928 }
929 rcu_read_unlock();
930 nfs_inode_return_delegation(inode);
931}
932
Trond Myklebust6ee41262007-07-08 14:11:36 -0400933static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400934{
935 struct nfs4_state *state = opendata->state;
936 struct nfs_inode *nfsi = NFS_I(state->inode);
937 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500938 int open_mode = opendata->o_arg.open_flags & O_EXCL;
939 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400940 nfs4_stateid stateid;
941 int ret = -EAGAIN;
942
Trond Myklebustaac00a82007-07-05 19:02:21 -0400943 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500944 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400945 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500946 if (can_open_cached(state, fmode, open_mode)) {
947 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400948 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400949 goto out_return_state;
950 }
951 spin_unlock(&state->owner->so_lock);
952 }
Trond Myklebust34310432008-12-23 15:21:38 -0500953 rcu_read_lock();
954 delegation = rcu_dereference(nfsi->delegation);
955 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500956 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -0500957 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -0400958 break;
Trond Myklebust34310432008-12-23 15:21:38 -0500959 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400960 /* Save the delegation */
961 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
962 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -0400963 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400964 if (ret != 0)
965 goto out;
966 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -0500967
968 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500969 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -0500970 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400971 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400972out:
973 return ERR_PTR(ret);
974out_return_state:
975 atomic_inc(&state->count);
976 return state;
977}
978
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100979static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
980{
981 struct inode *inode;
982 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -0400983 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400984 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100985
Trond Myklebustaac00a82007-07-05 19:02:21 -0400986 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400987 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400988 goto out;
989 }
990
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400991 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100992 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400993 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100994 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400995 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -0500996 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -0400997 goto err;
998 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100999 state = nfs4_get_open_state(inode, data->owner);
1000 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001001 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001002 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001003 int delegation_flags = 0;
1004
Trond Myklebust003707c2007-07-05 18:07:55 -04001005 rcu_read_lock();
1006 delegation = rcu_dereference(NFS_I(inode)->delegation);
1007 if (delegation)
1008 delegation_flags = delegation->flags;
1009 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -05001010 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001011 nfs_inode_set_delegation(state->inode,
1012 data->owner->so_cred,
1013 &data->o_res);
1014 else
1015 nfs_inode_reclaim_delegation(state->inode,
1016 data->owner->so_cred,
1017 &data->o_res);
1018 }
Trond Myklebust34310432008-12-23 15:21:38 -05001019
1020 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001021 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001022 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001023out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001024 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001025err_put_inode:
1026 iput(inode);
1027err:
1028 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001029}
1030
Trond Myklebust864472e2006-01-03 09:55:15 +01001031static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1032{
1033 struct nfs_inode *nfsi = NFS_I(state->inode);
1034 struct nfs_open_context *ctx;
1035
1036 spin_lock(&state->inode->i_lock);
1037 list_for_each_entry(ctx, &nfsi->open_files, list) {
1038 if (ctx->state != state)
1039 continue;
1040 get_nfs_open_context(ctx);
1041 spin_unlock(&state->inode->i_lock);
1042 return ctx;
1043 }
1044 spin_unlock(&state->inode->i_lock);
1045 return ERR_PTR(-ENOENT);
1046}
1047
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001048static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1049{
1050 struct nfs4_opendata *opendata;
1051
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001052 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001053 if (opendata == NULL)
1054 return ERR_PTR(-ENOMEM);
1055 opendata->state = state;
1056 atomic_inc(&state->count);
1057 return opendata;
1058}
1059
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001060static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001061{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001062 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001063 int ret;
1064
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001065 opendata->o_arg.open_flags = 0;
1066 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001067 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1068 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1069 nfs4_init_opendata_res(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001070 ret = _nfs4_proc_open(opendata);
1071 if (ret != 0)
1072 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001073 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001074 if (IS_ERR(newstate))
1075 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001076 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001077 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001078 return 0;
1079}
1080
1081static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1082{
Trond Myklebust864472e2006-01-03 09:55:15 +01001083 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001084 int ret;
1085
1086 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001087 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001088 smp_rmb();
1089 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001090 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001091 if (ret != 0)
1092 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001093 if (newstate != state)
1094 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001095 }
1096 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001097 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001098 if (ret != 0)
1099 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001100 if (newstate != state)
1101 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001102 }
1103 if (state->n_rdonly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001104 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001105 if (ret != 0)
1106 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001107 if (newstate != state)
1108 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001109 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001110 /*
1111 * We may have performed cached opens for all three recoveries.
1112 * Check if we need to update the current stateid.
1113 */
1114 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1115 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001116 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001117 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1118 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001119 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001120 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001121 return 0;
1122}
1123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124/*
1125 * OPEN_RECLAIM:
1126 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 */
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{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001130 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001131 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001132 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 int status;
1134
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001135 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1136 if (IS_ERR(opendata))
1137 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001138 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1139 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001140 rcu_read_lock();
1141 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001142 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001143 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001144 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001145 opendata->o_arg.u.delegation_type = delegation_type;
1146 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001147 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 return status;
1149}
1150
Trond Myklebust539cd032007-06-05 11:46:42 -04001151static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152{
1153 struct nfs_server *server = NFS_SERVER(state->inode);
1154 struct nfs4_exception exception = { };
1155 int err;
1156 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001157 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001158 if (err != -NFS4ERR_DELAY)
1159 break;
1160 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 } while (exception.retry);
1162 return err;
1163}
1164
Trond Myklebust864472e2006-01-03 09:55:15 +01001165static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1166{
1167 struct nfs_open_context *ctx;
1168 int ret;
1169
1170 ctx = nfs4_state_find_open_context(state);
1171 if (IS_ERR(ctx))
1172 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001173 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001174 put_nfs_open_context(ctx);
1175 return ret;
1176}
1177
Trond Myklebust13437e12007-07-06 15:10:43 -04001178static 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 -07001179{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001180 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001181 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001183 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1184 if (IS_ERR(opendata))
1185 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001186 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001187 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001188 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001189 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001190 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001191 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192}
1193
Trond Myklebust13437e12007-07-06 15:10:43 -04001194int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
1196 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001197 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 int err;
1199 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001200 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 switch (err) {
1202 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001203 case -ENOENT:
1204 case -ESTALE:
1205 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 case -NFS4ERR_STALE_CLIENTID:
1207 case -NFS4ERR_STALE_STATEID:
1208 case -NFS4ERR_EXPIRED:
1209 /* Don't recall a delegation if it was lost */
David Howells7539bba2006-08-22 20:06:09 -04001210 nfs4_schedule_state_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001211 goto out;
1212 case -ERESTARTSYS:
1213 /*
1214 * The show must go on: exit, but mark the
1215 * stateid as needing recovery.
1216 */
1217 case -NFS4ERR_ADMIN_REVOKED:
1218 case -NFS4ERR_BAD_STATEID:
1219 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
1220 case -ENOMEM:
1221 err = 0;
1222 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 }
1224 err = nfs4_handle_exception(server, err, &exception);
1225 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001226out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 return err;
1228}
1229
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001230static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1231{
1232 struct nfs4_opendata *data = calldata;
1233
1234 data->rpc_status = task->tk_status;
1235 if (RPC_ASSASSINATED(task))
1236 return;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001237 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001238 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1239 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001240 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001241 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001242 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001243 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001244}
1245
1246static void nfs4_open_confirm_release(void *calldata)
1247{
1248 struct nfs4_opendata *data = calldata;
1249 struct nfs4_state *state = NULL;
1250
1251 /* If this request hasn't been cancelled, do nothing */
1252 if (data->cancelled == 0)
1253 goto out_free;
1254 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001255 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001256 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001257 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001258 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001259 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001260out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001261 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001262}
1263
1264static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001265 .rpc_call_done = nfs4_open_confirm_done,
1266 .rpc_release = nfs4_open_confirm_release,
1267};
1268
1269/*
1270 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1271 */
1272static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1273{
1274 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1275 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001276 struct rpc_message msg = {
1277 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1278 .rpc_argp = &data->c_arg,
1279 .rpc_resp = &data->c_res,
1280 .rpc_cred = data->owner->so_cred,
1281 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001282 struct rpc_task_setup task_setup_data = {
1283 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001284 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001285 .callback_ops = &nfs4_open_confirm_ops,
1286 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001287 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001288 .flags = RPC_TASK_ASYNC,
1289 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 int status;
1291
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001292 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001293 data->rpc_done = 0;
1294 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001295 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001296 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001297 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001298 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001299 status = nfs4_wait_for_completion_rpc_task(task);
1300 if (status != 0) {
1301 data->cancelled = 1;
1302 smp_wmb();
1303 } else
1304 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001305 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 return status;
1307}
1308
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001309static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001311 struct nfs4_opendata *data = calldata;
1312 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001313
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001314 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1315 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001316 /*
1317 * Check if we still need to send an OPEN call, or if we can use
1318 * a delegation instead.
1319 */
1320 if (data->state != NULL) {
1321 struct nfs_delegation *delegation;
1322
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001323 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001324 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001325 rcu_read_lock();
1326 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1327 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001328 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001329 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001330 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001331 }
1332 rcu_read_unlock();
1333 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001334 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001335 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001336 data->o_arg.clientid = sp->so_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001337 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001338 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001339 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1340 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001341 data->timestamp = jiffies;
Andy Adamsond8985282009-04-01 09:22:21 -04001342 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
1343 &data->o_arg.seq_args,
1344 &data->o_res.seq_res, 1, task))
1345 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001346 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001347 return;
1348out_no_action:
1349 task->tk_action = NULL;
1350
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001351}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001353static void nfs4_open_done(struct rpc_task *task, void *calldata)
1354{
1355 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001357 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001358
1359 nfs4_sequence_done_free_slot(data->o_arg.server, &data->o_res.seq_res,
1360 task->tk_status);
1361
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001362 if (RPC_ASSASSINATED(task))
1363 return;
1364 if (task->tk_status == 0) {
1365 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001366 case S_IFREG:
1367 break;
1368 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001369 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001370 break;
1371 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001372 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001373 break;
1374 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001375 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001376 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001377 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001378 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1379 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001380 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001381 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001382}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001383
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001384static void nfs4_open_release(void *calldata)
1385{
1386 struct nfs4_opendata *data = calldata;
1387 struct nfs4_state *state = NULL;
1388
1389 /* If this request hasn't been cancelled, do nothing */
1390 if (data->cancelled == 0)
1391 goto out_free;
1392 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001393 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001394 goto out_free;
1395 /* In case we need an open_confirm, no cleanup! */
1396 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1397 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001398 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001399 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001400 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001401out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001402 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001403}
1404
1405static const struct rpc_call_ops nfs4_open_ops = {
1406 .rpc_call_prepare = nfs4_open_prepare,
1407 .rpc_call_done = nfs4_open_done,
1408 .rpc_release = nfs4_open_release,
1409};
1410
1411/*
1412 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1413 */
1414static int _nfs4_proc_open(struct nfs4_opendata *data)
1415{
1416 struct inode *dir = data->dir->d_inode;
1417 struct nfs_server *server = NFS_SERVER(dir);
1418 struct nfs_openargs *o_arg = &data->o_arg;
1419 struct nfs_openres *o_res = &data->o_res;
1420 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001421 struct rpc_message msg = {
1422 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1423 .rpc_argp = o_arg,
1424 .rpc_resp = o_res,
1425 .rpc_cred = data->owner->so_cred,
1426 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001427 struct rpc_task_setup task_setup_data = {
1428 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001429 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001430 .callback_ops = &nfs4_open_ops,
1431 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001432 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001433 .flags = RPC_TASK_ASYNC,
1434 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001435 int status;
1436
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001437 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001438 data->rpc_done = 0;
1439 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001440 data->cancelled = 0;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001441 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001442 if (IS_ERR(task))
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001443 return PTR_ERR(task);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001444 status = nfs4_wait_for_completion_rpc_task(task);
1445 if (status != 0) {
1446 data->cancelled = 1;
1447 smp_wmb();
1448 } else
1449 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001450 rpc_put_task(task);
Trond Myklebust3e309912007-07-07 13:19:59 -04001451 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001452 return status;
1453
Trond Myklebust99367812007-07-17 21:52:41 -04001454 if (o_res->fh.size == 0)
1455 _nfs4_proc_lookup(dir, o_arg->name, &o_res->fh, o_res->f_attr);
1456
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001457 if (o_arg->open_flags & O_CREAT) {
1458 update_changeattr(dir, &o_res->cinfo);
1459 nfs_post_op_update_inode(dir, o_res->dir_attr);
1460 } else
1461 nfs_refresh_inode(dir, o_res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001463 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001465 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 }
1467 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001468 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001469 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470}
1471
Trond Myklebust10afec92007-05-14 17:16:04 -04001472static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +01001473{
David Howells7539bba2006-08-22 20:06:09 -04001474 struct nfs_client *clp = server->nfs_client;
Trond Myklebusta78cb572009-08-09 15:06:19 -04001475 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001476 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001477
Trond Myklebusta78cb572009-08-09 15:06:19 -04001478 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001479 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001480 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001481 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001482 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1483 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001484 break;
Trond Myklebust58d97142006-01-03 09:55:24 +01001485 nfs4_schedule_state_recovery(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001486 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001487 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001488 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001489}
1490
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491/*
1492 * OPEN_EXPIRED:
1493 * reclaim state on the server after a network partition.
1494 * Assumes caller holds the appropriate lock
1495 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001496static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001498 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001499 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001501 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1502 if (IS_ERR(opendata))
1503 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001504 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001505 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001506 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001507 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001508 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509}
1510
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001511static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001512{
Trond Myklebust539cd032007-06-05 11:46:42 -04001513 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001514 struct nfs4_exception exception = { };
1515 int err;
1516
1517 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001518 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001519 switch (err) {
1520 default:
1521 goto out;
1522 case -NFS4ERR_GRACE:
1523 case -NFS4ERR_DELAY:
1524 nfs4_handle_exception(server, err, &exception);
1525 err = 0;
1526 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001527 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001528out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001529 return err;
1530}
1531
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1533{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001535 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
Trond Myklebust864472e2006-01-03 09:55:15 +01001537 ctx = nfs4_state_find_open_context(state);
1538 if (IS_ERR(ctx))
1539 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001540 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001541 put_nfs_open_context(ctx);
1542 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543}
1544
1545/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001546 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1547 * fields corresponding to attributes that were used to store the verifier.
1548 * Make sure we clobber those fields in the later setattr call
1549 */
1550static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1551{
1552 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1553 !(sattr->ia_valid & ATTR_ATIME_SET))
1554 sattr->ia_valid |= ATTR_ATIME;
1555
1556 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1557 !(sattr->ia_valid & ATTR_MTIME_SET))
1558 sattr->ia_valid |= ATTR_MTIME;
1559}
1560
1561/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001562 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001564static 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 -07001565{
1566 struct nfs4_state_owner *sp;
1567 struct nfs4_state *state = NULL;
1568 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001569 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001570 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
1572 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 status = -ENOMEM;
1574 if (!(sp = nfs4_get_state_owner(server, cred))) {
1575 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1576 goto out_err;
1577 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001578 status = nfs4_recover_expired_lease(server);
1579 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001580 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001581 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001582 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001583 status = -ENOMEM;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001584 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001585 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001586 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Trond Myklebustaac00a82007-07-05 19:02:21 -04001588 if (path->dentry->d_inode != NULL)
1589 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1590
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001591 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001593 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001595 if (opendata->o_arg.open_flags & O_EXCL)
1596 nfs4_exclusive_attrset(opendata, sattr);
1597
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001598 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001599 status = PTR_ERR(state);
1600 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001601 goto err_opendata_put;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001602 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 *res = state;
1605 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001606err_opendata_put:
1607 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001608err_put_state_owner:
1609 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 *res = NULL;
1612 return status;
1613}
1614
1615
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001616static 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 -07001617{
1618 struct nfs4_exception exception = { };
1619 struct nfs4_state *res;
1620 int status;
1621
1622 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001623 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 if (status == 0)
1625 break;
1626 /* NOTE: BAD_SEQID means the server and client disagree about the
1627 * book-keeping w.r.t. state-changing operations
1628 * (OPEN/CLOSE/LOCK/LOCKU...)
1629 * It is actually a sign of a bug on the client or on the server.
1630 *
1631 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001632 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 * have unhashed the old state_owner for us, and that we can
1634 * therefore safely retry using a new one. We should still warn
1635 * the user though...
1636 */
1637 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001638 printk(KERN_WARNING "NFS: v4 server %s "
1639 " returned a bad sequence-id error!\n",
1640 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 exception.retry = 1;
1642 continue;
1643 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001644 /*
1645 * BAD_STATEID on OPEN means that the server cancelled our
1646 * state before it received the OPEN_CONFIRM.
1647 * Recover by retrying the request as per the discussion
1648 * on Page 181 of RFC3530.
1649 */
1650 if (status == -NFS4ERR_BAD_STATEID) {
1651 exception.retry = 1;
1652 continue;
1653 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001654 if (status == -EAGAIN) {
1655 /* We must have found a delegation */
1656 exception.retry = 1;
1657 continue;
1658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1660 status, &exception));
1661 } while (exception.retry);
1662 return res;
1663}
1664
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001665static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1666 struct nfs_fattr *fattr, struct iattr *sattr,
1667 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001669 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001671 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 .iap = sattr,
1673 .server = server,
1674 .bitmask = server->attr_bitmask,
1675 };
1676 struct nfs_setattrres res = {
1677 .fattr = fattr,
1678 .server = server,
1679 };
1680 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001681 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1682 .rpc_argp = &arg,
1683 .rpc_resp = &res,
1684 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001686 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001687 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Trond Myklebust0e574af2005-10-27 22:12:38 -04001689 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001691 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1692 /* Use that stateid */
1693 } else if (state != NULL) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001694 nfs4_copy_stateid(&arg.stateid, state, current->files);
1695 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1697
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001698 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001699 if (status == 0 && state != NULL)
1700 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001701 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702}
1703
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001704static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1705 struct nfs_fattr *fattr, struct iattr *sattr,
1706 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001708 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 struct nfs4_exception exception = { };
1710 int err;
1711 do {
1712 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001713 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 &exception);
1715 } while (exception.retry);
1716 return err;
1717}
1718
1719struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001720 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 struct inode *inode;
1722 struct nfs4_state *state;
1723 struct nfs_closeargs arg;
1724 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001725 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001726 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727};
1728
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001729static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001730{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001731 struct nfs4_closedata *calldata = data;
1732 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001733
1734 nfs4_put_open_state(calldata->state);
1735 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001736 nfs4_put_state_owner(sp);
Jan Blunck31f31db12008-05-02 13:42:45 -07001737 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001738 kfree(calldata);
1739}
1740
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001741static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001743 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 struct nfs_server *server = NFS_SERVER(calldata->inode);
1746
Andy Adamson19ddab02009-04-01 09:22:20 -04001747 nfs4_sequence_done(server, &calldata->res.seq_res, task->tk_status);
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001748 if (RPC_ASSASSINATED(task))
1749 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 /* hmm. we are done with the inode, and in the process of freeing
1751 * the state_owner. we keep this around to process errors
1752 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 switch (task->tk_status) {
1754 case 0:
Trond Myklebust45328c32007-07-26 17:47:34 -04001755 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001756 renew_lease(server, calldata->timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 break;
1758 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001759 case -NFS4ERR_OLD_STATEID:
1760 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001762 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001763 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001765 if (nfs4_async_handle_error(task, server, state) == -EAGAIN) {
Andy Adamsone608e792009-12-04 15:55:29 -05001766 nfs4_restart_rpc(task, server->nfs_client,
1767 &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 return;
1769 }
1770 }
Andy Adamson19ddab02009-04-01 09:22:20 -04001771 nfs4_sequence_free_slot(server->nfs_client, &calldata->res.seq_res);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001772 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773}
1774
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001775static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001777 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001778 struct nfs4_state *state = calldata->state;
Trond Myklebust003707c2007-07-05 18:07:55 -04001779 int clear_rd, clear_wr, clear_rdwr;
Trond Myklebust95121352005-10-18 14:20:12 -07001780
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001781 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001782 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001783
Trond Myklebust003707c2007-07-05 18:07:55 -04001784 clear_rd = clear_wr = clear_rdwr = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001785 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001786 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001787 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001788 if (state->n_rdonly == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001789 clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1790 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1791 }
1792 if (state->n_wronly == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001793 clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1794 clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags);
1795 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001796 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001797 spin_unlock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001798 if (!clear_rd && !clear_wr && !clear_rdwr) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001799 /* Note: exit _without_ calling nfs4_close_done */
1800 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001801 return;
1802 }
Trond Myklebust516a6af2005-10-27 22:12:41 -04001803 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust45328c32007-07-26 17:47:34 -04001804 if (test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001805 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001806 calldata->arg.fmode = FMODE_READ;
Trond Myklebust45328c32007-07-26 17:47:34 -04001807 } else if (test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001808 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001809 calldata->arg.fmode = FMODE_WRITE;
Trond Myklebust45328c32007-07-26 17:47:34 -04001810 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001811 calldata->timestamp = jiffies;
Andy Adamson19ddab02009-04-01 09:22:20 -04001812 if (nfs4_setup_sequence((NFS_SERVER(calldata->inode))->nfs_client,
1813 &calldata->arg.seq_args, &calldata->res.seq_res,
1814 1, task))
1815 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001816 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817}
1818
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001819static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001820 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001821 .rpc_call_done = nfs4_close_done,
1822 .rpc_release = nfs4_free_closedata,
1823};
1824
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825/*
1826 * It is possible for data to be read/written from a mem-mapped file
1827 * after the sys_close call (which hits the vfs layer as a flush).
1828 * This means that we can't safely call nfsv4 close on a file until
1829 * the inode is cleared. This in turn means that we are not good
1830 * NFSv4 citizens - we do not indicate to the server to update the file's
1831 * share state even when we are done with one of the three share
1832 * stateid's in the inode.
1833 *
1834 * NOTE: Caller must be holding the sp->so_owner semaphore!
1835 */
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001836int nfs4_do_close(struct path *path, struct nfs4_state *state, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001838 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001840 struct nfs4_state_owner *sp = state->owner;
1841 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001842 struct rpc_message msg = {
1843 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1844 .rpc_cred = state->owner->so_cred,
1845 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001846 struct rpc_task_setup task_setup_data = {
1847 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001848 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001849 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05001850 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001851 .flags = RPC_TASK_ASYNC,
1852 };
Trond Myklebust95121352005-10-18 14:20:12 -07001853 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
Andy Adamson19ddab02009-04-01 09:22:20 -04001855 calldata = kzalloc(sizeof(*calldata), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001857 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001858 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001860 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04001861 calldata->arg.stateid = &state->open_stateid;
Benny Halevy34dc1ad2009-04-01 09:22:52 -04001862 if (nfs4_has_session(server->nfs_client))
1863 memset(calldata->arg.stateid->data, 0, 4); /* clear seqid */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 /* Serialization for the sequence id */
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001865 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001866 if (calldata->arg.seqid == NULL)
1867 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001868 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001869 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001870 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04001871 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001872 calldata->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04001873 calldata->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001874 calldata->path.mnt = mntget(path->mnt);
1875 calldata->path.dentry = dget(path->dentry);
Trond Myklebust95121352005-10-18 14:20:12 -07001876
Trond Myklebust5138fde2007-07-14 15:40:01 -04001877 msg.rpc_argp = &calldata->arg,
1878 msg.rpc_resp = &calldata->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001879 task_setup_data.callback_data = calldata;
1880 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001881 if (IS_ERR(task))
1882 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001883 status = 0;
1884 if (wait)
1885 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001886 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001887 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07001888out_free_calldata:
1889 kfree(calldata);
1890out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04001891 nfs4_put_open_state(state);
1892 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07001893 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894}
1895
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001896static 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 -07001897{
1898 struct file *filp;
Trond Myklebust1b45c462007-07-03 13:04:56 -04001899 int ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001900
Trond Myklebust1b45c462007-07-03 13:04:56 -04001901 /* If the open_intent is for execute, we have an extra check to make */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001902 if (fmode & FMODE_EXEC) {
Trond Myklebustaf22f942007-08-10 17:45:10 -04001903 ret = nfs_may_open(state->inode,
Trond Myklebust1b45c462007-07-03 13:04:56 -04001904 state->owner->so_cred,
1905 nd->intent.open.flags);
1906 if (ret < 0)
1907 goto out_close;
1908 }
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001909 filp = lookup_instantiate_filp(nd, path->dentry, NULL);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001910 if (!IS_ERR(filp)) {
1911 struct nfs_open_context *ctx;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04001912 ctx = nfs_file_open_context(filp);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001913 ctx->state = state;
Trond Myklebust95cf9592006-04-18 13:14:06 -04001914 return 0;
1915 }
Trond Myklebust1b45c462007-07-03 13:04:56 -04001916 ret = PTR_ERR(filp);
1917out_close:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001918 nfs4_close_sync(path, state, fmode & (FMODE_READ|FMODE_WRITE));
Trond Myklebust1b45c462007-07-03 13:04:56 -04001919 return ret;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001920}
1921
1922struct dentry *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
1924{
Trond Myklebustad389da2007-06-05 12:30:00 -04001925 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08001926 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001927 .dentry = dentry,
1928 };
Jan Blunck4ac91372008-02-14 19:34:32 -08001929 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 struct iattr attr;
1931 struct rpc_cred *cred;
1932 struct nfs4_state *state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001933 struct dentry *res;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001934 fmode_t fmode = nd->intent.open.flags & (FMODE_READ | FMODE_WRITE | FMODE_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
1936 if (nd->flags & LOOKUP_CREATE) {
1937 attr.ia_mode = nd->intent.open.create_mode;
1938 attr.ia_valid = ATTR_MODE;
1939 if (!IS_POSIXACL(dir))
Al Viroce3b0f82009-03-29 19:08:22 -04001940 attr.ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 } else {
1942 attr.ia_valid = 0;
1943 BUG_ON(nd->intent.open.flags & O_CREAT);
1944 }
1945
Trond Myklebust98a8e322008-03-12 12:25:28 -04001946 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 if (IS_ERR(cred))
Trond Myklebust02a913a2005-10-18 14:20:17 -07001948 return (struct dentry *)cred;
Trond Myklebust565277f2007-10-15 18:17:53 -04001949 parent = dentry->d_parent;
1950 /* Protect against concurrent sillydeletes */
1951 nfs_block_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001952 state = nfs4_do_open(dir, &path, fmode, nd->intent.open.flags, &attr, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001954 if (IS_ERR(state)) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04001955 if (PTR_ERR(state) == -ENOENT) {
Trond Myklebust02a913a2005-10-18 14:20:17 -07001956 d_add(dentry, NULL);
Trond Myklebustd75340c2007-10-01 21:42:01 -04001957 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
1958 }
Trond Myklebust565277f2007-10-15 18:17:53 -04001959 nfs_unblock_sillyrename(parent);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001960 return (struct dentry *)state;
1961 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001962 res = d_add_unique(dentry, igrab(state->inode));
Trond Myklebust02a913a2005-10-18 14:20:17 -07001963 if (res != NULL)
Trond Myklebustdeee9362007-08-27 11:33:00 -04001964 path.dentry = res;
Trond Myklebustd75340c2007-10-01 21:42:01 -04001965 nfs_set_verifier(path.dentry, nfs_save_change_attribute(dir));
Trond Myklebust565277f2007-10-15 18:17:53 -04001966 nfs_unblock_sillyrename(parent);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001967 nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001968 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969}
1970
1971int
Trond Myklebust02a913a2005-10-18 14:20:17 -07001972nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973{
Trond Myklebustad389da2007-06-05 12:30:00 -04001974 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08001975 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001976 .dentry = dentry,
1977 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 struct rpc_cred *cred;
1979 struct nfs4_state *state;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001980 fmode_t fmode = openflags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Trond Myklebust98a8e322008-03-12 12:25:28 -04001982 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 if (IS_ERR(cred))
1984 return PTR_ERR(cred);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001985 state = nfs4_do_open(dir, &path, fmode, openflags, NULL, cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 put_rpccred(cred);
Trond Myklebust02a913a2005-10-18 14:20:17 -07001987 if (IS_ERR(state)) {
1988 switch (PTR_ERR(state)) {
1989 case -EPERM:
1990 case -EACCES:
1991 case -EDQUOT:
1992 case -ENOSPC:
1993 case -EROFS:
1994 lookup_instantiate_filp(nd, (struct dentry *)state, NULL);
1995 return 1;
Chuck Leverb87c0ad2006-10-19 23:28:42 -07001996 default:
1997 goto out_drop;
Trond Myklebust02a913a2005-10-18 14:20:17 -07001998 }
Trond Myklebust02a913a2005-10-18 14:20:17 -07001999 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002000 if (state->inode == dentry->d_inode) {
Trond Myklebustd75340c2007-10-01 21:42:01 -04002001 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002002 nfs4_intent_set_file(nd, &path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 return 1;
2004 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002005 nfs4_close_sync(&path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002006out_drop:
2007 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 return 0;
2009}
2010
Trond Myklebust1185a552009-12-03 15:54:02 -05002011static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002012{
2013 if (ctx->state == NULL)
2014 return;
2015 if (is_sync)
2016 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2017 else
2018 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2019}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
2021static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2022{
Benny Halevy43652ad2009-04-01 09:21:54 -04002023 struct nfs4_server_caps_arg args = {
2024 .fhandle = fhandle,
2025 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 struct nfs4_server_caps_res res = {};
2027 struct rpc_message msg = {
2028 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002029 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 .rpc_resp = &res,
2031 };
2032 int status;
2033
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002034 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 if (status == 0) {
2036 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002037 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2038 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2039 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2040 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2041 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2043 server->caps |= NFS_CAP_ACLS;
2044 if (res.has_links != 0)
2045 server->caps |= NFS_CAP_HARDLINKS;
2046 if (res.has_symlinks != 0)
2047 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002048 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2049 server->caps |= NFS_CAP_FILEID;
2050 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2051 server->caps |= NFS_CAP_MODE;
2052 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2053 server->caps |= NFS_CAP_NLINK;
2054 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2055 server->caps |= NFS_CAP_OWNER;
2056 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2057 server->caps |= NFS_CAP_OWNER_GROUP;
2058 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2059 server->caps |= NFS_CAP_ATIME;
2060 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2061 server->caps |= NFS_CAP_CTIME;
2062 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2063 server->caps |= NFS_CAP_MTIME;
2064
Trond Myklebusta65318b2009-03-11 14:10:28 -04002065 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2066 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2067 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 server->acl_bitmask = res.acl_bitmask;
2069 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002070
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 return status;
2072}
2073
Trond Myklebust55a97592006-06-09 09:34:19 -04002074int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075{
2076 struct nfs4_exception exception = { };
2077 int err;
2078 do {
2079 err = nfs4_handle_exception(server,
2080 _nfs4_server_capabilities(server, fhandle),
2081 &exception);
2082 } while (exception.retry);
2083 return err;
2084}
2085
2086static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2087 struct nfs_fsinfo *info)
2088{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 struct nfs4_lookup_root_arg args = {
2090 .bitmask = nfs4_fattr_bitmap,
2091 };
2092 struct nfs4_lookup_res res = {
2093 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002094 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 .fh = fhandle,
2096 };
2097 struct rpc_message msg = {
2098 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2099 .rpc_argp = &args,
2100 .rpc_resp = &res,
2101 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002102
Trond Myklebust0e574af2005-10-27 22:12:38 -04002103 nfs_fattr_init(info->fattr);
Trond Myklebustfccba802009-07-21 16:48:07 -04002104 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105}
2106
2107static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2108 struct nfs_fsinfo *info)
2109{
2110 struct nfs4_exception exception = { };
2111 int err;
2112 do {
2113 err = nfs4_handle_exception(server,
2114 _nfs4_lookup_root(server, fhandle, info),
2115 &exception);
2116 } while (exception.retry);
2117 return err;
2118}
2119
David Howells54ceac42006-08-22 20:06:13 -04002120/*
2121 * get the file handle for the "/" directory on the server
2122 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002124 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 int status;
2127
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 if (status == 0)
2130 status = nfs4_server_capabilities(server, fhandle);
2131 if (status == 0)
2132 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002133 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134}
2135
Manoj Naik6b97fd32006-06-09 09:34:29 -04002136/*
2137 * Get locations and (maybe) other attributes of a referral.
2138 * Note that we'll actually follow the referral later when
2139 * we detect fsid mismatch in inode revalidation
2140 */
Trond Myklebust56659e92007-07-17 21:52:39 -04002141static 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 -04002142{
2143 int status = -ENOMEM;
2144 struct page *page = NULL;
2145 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002146
2147 page = alloc_page(GFP_KERNEL);
2148 if (page == NULL)
2149 goto out;
2150 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2151 if (locations == NULL)
2152 goto out;
2153
Trond Myklebustc228fd32007-01-13 02:28:11 -05002154 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002155 if (status != 0)
2156 goto out;
2157 /* Make sure server returned a different fsid for the referral */
2158 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07002159 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 -04002160 status = -EIO;
2161 goto out;
2162 }
2163
2164 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2165 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2166 if (!fattr->mode)
2167 fattr->mode = S_IFDIR;
2168 memset(fhandle, 0, sizeof(struct nfs_fh));
2169out:
2170 if (page)
2171 __free_page(page);
2172 if (locations)
2173 kfree(locations);
2174 return status;
2175}
2176
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2178{
2179 struct nfs4_getattr_arg args = {
2180 .fh = fhandle,
2181 .bitmask = server->attr_bitmask,
2182 };
2183 struct nfs4_getattr_res res = {
2184 .fattr = fattr,
2185 .server = server,
2186 };
2187 struct rpc_message msg = {
2188 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2189 .rpc_argp = &args,
2190 .rpc_resp = &res,
2191 };
2192
Trond Myklebust0e574af2005-10-27 22:12:38 -04002193 nfs_fattr_init(fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002194 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195}
2196
2197static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2198{
2199 struct nfs4_exception exception = { };
2200 int err;
2201 do {
2202 err = nfs4_handle_exception(server,
2203 _nfs4_proc_getattr(server, fhandle, fattr),
2204 &exception);
2205 } while (exception.retry);
2206 return err;
2207}
2208
2209/*
2210 * The file is not closed if it is opened due to the a request to change
2211 * the size of the file. The open call will not be needed once the
2212 * VFS layer lookup-intents are implemented.
2213 *
2214 * Close is called when the inode is destroyed.
2215 * If we haven't opened the file for O_WRONLY, we
2216 * need to in the size_change case to obtain a stateid.
2217 *
2218 * Got race?
2219 * Because OPEN is always done by name in nfsv4, it is
2220 * possible that we opened a different file by the same
2221 * name. We can recognize this race condition, but we
2222 * can't do anything about it besides returning an error.
2223 *
2224 * This will be fixed with VFS changes (lookup-intent).
2225 */
2226static int
2227nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2228 struct iattr *sattr)
2229{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002230 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002231 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002232 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 int status;
2234
Trond Myklebust0e574af2005-10-27 22:12:38 -04002235 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
Trond Myklebustd5308382005-11-04 15:33:38 -05002237 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002238 if (sattr->ia_valid & ATTR_FILE) {
2239 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002240
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002241 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002242 if (ctx) {
2243 cred = ctx->cred;
2244 state = ctx->state;
2245 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002246 }
2247
2248 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002249 if (status == 0)
2250 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 return status;
2252}
2253
Trond Myklebust56659e92007-07-17 21:52:39 -04002254static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2255 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04002256 struct nfs_fattr *fattr)
2257{
2258 int status;
2259 struct nfs4_lookup_arg args = {
2260 .bitmask = server->attr_bitmask,
2261 .dir_fh = dirfh,
2262 .name = name,
2263 };
2264 struct nfs4_lookup_res res = {
2265 .server = server,
2266 .fattr = fattr,
2267 .fh = fhandle,
2268 };
2269 struct rpc_message msg = {
2270 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2271 .rpc_argp = &args,
2272 .rpc_resp = &res,
2273 };
2274
2275 nfs_fattr_init(fattr);
2276
2277 dprintk("NFS call lookupfh %s\n", name->name);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002278 status = nfs4_call_sync(server, &msg, &args, &res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002279 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002280 return status;
2281}
2282
2283static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2284 struct qstr *name, struct nfs_fh *fhandle,
2285 struct nfs_fattr *fattr)
2286{
2287 struct nfs4_exception exception = { };
2288 int err;
2289 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04002290 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2291 /* FIXME: !!!! */
2292 if (err == -NFS4ERR_MOVED) {
2293 err = -EREMOTE;
2294 break;
2295 }
2296 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002297 } while (exception.retry);
2298 return err;
2299}
2300
Trond Myklebust56659e92007-07-17 21:52:39 -04002301static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2303{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002304 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
2306 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002307 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002308 if (status == -NFS4ERR_MOVED)
2309 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 dprintk("NFS reply lookup: %d\n", status);
2311 return status;
2312}
2313
2314static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2315{
2316 struct nfs4_exception exception = { };
2317 int err;
2318 do {
2319 err = nfs4_handle_exception(NFS_SERVER(dir),
2320 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2321 &exception);
2322 } while (exception.retry);
2323 return err;
2324}
2325
2326static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2327{
Trond Myklebust76b32992007-08-10 17:45:11 -04002328 struct nfs_server *server = NFS_SERVER(inode);
2329 struct nfs_fattr fattr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 struct nfs4_accessargs args = {
2331 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002332 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002334 struct nfs4_accessres res = {
2335 .server = server,
2336 .fattr = &fattr,
2337 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 struct rpc_message msg = {
2339 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2340 .rpc_argp = &args,
2341 .rpc_resp = &res,
2342 .rpc_cred = entry->cred,
2343 };
2344 int mode = entry->mask;
2345 int status;
2346
2347 /*
2348 * Determine which access bits we want to ask for...
2349 */
2350 if (mode & MAY_READ)
2351 args.access |= NFS4_ACCESS_READ;
2352 if (S_ISDIR(inode->i_mode)) {
2353 if (mode & MAY_WRITE)
2354 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2355 if (mode & MAY_EXEC)
2356 args.access |= NFS4_ACCESS_LOOKUP;
2357 } else {
2358 if (mode & MAY_WRITE)
2359 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2360 if (mode & MAY_EXEC)
2361 args.access |= NFS4_ACCESS_EXECUTE;
2362 }
Trond Myklebust76b32992007-08-10 17:45:11 -04002363 nfs_fattr_init(&fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002364 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 if (!status) {
2366 entry->mask = 0;
2367 if (res.access & NFS4_ACCESS_READ)
2368 entry->mask |= MAY_READ;
2369 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2370 entry->mask |= MAY_WRITE;
2371 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2372 entry->mask |= MAY_EXEC;
Trond Myklebust76b32992007-08-10 17:45:11 -04002373 nfs_refresh_inode(inode, &fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 }
2375 return status;
2376}
2377
2378static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2379{
2380 struct nfs4_exception exception = { };
2381 int err;
2382 do {
2383 err = nfs4_handle_exception(NFS_SERVER(inode),
2384 _nfs4_proc_access(inode, entry),
2385 &exception);
2386 } while (exception.retry);
2387 return err;
2388}
2389
2390/*
2391 * TODO: For the time being, we don't try to get any attributes
2392 * along with any of the zero-copy operations READ, READDIR,
2393 * READLINK, WRITE.
2394 *
2395 * In the case of the first three, we want to put the GETATTR
2396 * after the read-type operation -- this is because it is hard
2397 * to predict the length of a GETATTR response in v4, and thus
2398 * align the READ data correctly. This means that the GETATTR
2399 * may end up partially falling into the page cache, and we should
2400 * shift it into the 'tail' of the xdr_buf before processing.
2401 * To do this efficiently, we need to know the total length
2402 * of data received, which doesn't seem to be available outside
2403 * of the RPC layer.
2404 *
2405 * In the case of WRITE, we also want to put the GETATTR after
2406 * the operation -- in this case because we want to make sure
2407 * we get the post-operation mtime and size. This means that
2408 * we can't use xdr_encode_pages() as written: we need a variant
2409 * of it which would leave room in the 'tail' iovec.
2410 *
2411 * Both of these changes to the XDR layer would in fact be quite
2412 * minor, but I decided to leave them for a subsequent patch.
2413 */
2414static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2415 unsigned int pgbase, unsigned int pglen)
2416{
2417 struct nfs4_readlink args = {
2418 .fh = NFS_FH(inode),
2419 .pgbase = pgbase,
2420 .pglen = pglen,
2421 .pages = &page,
2422 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002423 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 struct rpc_message msg = {
2425 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2426 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002427 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 };
2429
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002430 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431}
2432
2433static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2434 unsigned int pgbase, unsigned int pglen)
2435{
2436 struct nfs4_exception exception = { };
2437 int err;
2438 do {
2439 err = nfs4_handle_exception(NFS_SERVER(inode),
2440 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2441 &exception);
2442 } while (exception.retry);
2443 return err;
2444}
2445
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446/*
2447 * Got race?
2448 * We will need to arrange for the VFS layer to provide an atomic open.
2449 * Until then, this create/open method is prone to inefficiency and race
2450 * conditions due to the lookup, create, and open VFS calls from sys_open()
2451 * placed on the wire.
2452 *
2453 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2454 * The file will be opened again in the subsequent VFS open call
2455 * (nfs4_proc_file_open).
2456 *
2457 * The open for read will just hang around to be used by any process that
2458 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2459 */
2460
2461static int
2462nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebust02a913a2005-10-18 14:20:17 -07002463 int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464{
Trond Myklebustad389da2007-06-05 12:30:00 -04002465 struct path path = {
Jan Blunck4ac91372008-02-14 19:34:32 -08002466 .mnt = nd->path.mnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04002467 .dentry = dentry,
2468 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 struct nfs4_state *state;
2470 struct rpc_cred *cred;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002471 fmode_t fmode = flags & (FMODE_READ | FMODE_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 int status = 0;
2473
Trond Myklebust98a8e322008-03-12 12:25:28 -04002474 cred = rpc_lookup_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 if (IS_ERR(cred)) {
2476 status = PTR_ERR(cred);
2477 goto out;
2478 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002479 state = nfs4_do_open(dir, &path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002480 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 if (IS_ERR(state)) {
2482 status = PTR_ERR(state);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002483 goto out_putcred;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002485 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002486 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 if (flags & O_EXCL) {
2488 struct nfs_fattr fattr;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002489 status = nfs4_do_setattr(state->inode, cred, &fattr, sattr, state);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002490 if (status == 0)
Trond Myklebust65e43082005-08-16 11:49:44 -04002491 nfs_setattr_update_inode(state->inode, sattr);
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002492 nfs_post_op_update_inode(state->inode, &fattr);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002493 }
Trond Myklebustad389da2007-06-05 12:30:00 -04002494 if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002495 status = nfs4_intent_set_file(nd, &path, state, fmode);
Trond Myklebust02a913a2005-10-18 14:20:17 -07002496 else
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002497 nfs4_close_sync(&path, state, fmode);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002498out_putcred:
2499 put_rpccred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500out:
2501 return status;
2502}
2503
2504static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2505{
Trond Myklebust16e42952005-10-27 22:12:44 -04002506 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002507 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002509 .name.len = name->len,
2510 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002511 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002513 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002514 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002515 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002517 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2518 .rpc_argp = &args,
2519 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 };
2521 int status;
2522
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002523 nfs_fattr_init(&res.dir_attr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002524 status = nfs4_call_sync(server, &msg, &args, &res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002525 if (status == 0) {
2526 update_changeattr(dir, &res.cinfo);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002527 nfs_post_op_update_inode(dir, &res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 return status;
2530}
2531
2532static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2533{
2534 struct nfs4_exception exception = { };
2535 int err;
2536 do {
2537 err = nfs4_handle_exception(NFS_SERVER(dir),
2538 _nfs4_proc_remove(dir, name),
2539 &exception);
2540 } while (exception.retry);
2541 return err;
2542}
2543
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002544static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002546 struct nfs_server *server = NFS_SERVER(dir);
2547 struct nfs_removeargs *args = msg->rpc_argp;
2548 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549
Trond Myklebusta65318b2009-03-11 14:10:28 -04002550 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002551 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553}
2554
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002555static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002557 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2558
Andy Adamson472cfbd2009-04-01 09:22:24 -04002559 nfs4_sequence_done(res->server, &res->seq_res, task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002560 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002561 return 0;
Andy Adamson472cfbd2009-04-01 09:22:24 -04002562 nfs4_sequence_free_slot(res->server->nfs_client, &res->seq_res);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002563 update_changeattr(dir, &res->cinfo);
2564 nfs_post_op_update_inode(dir, &res->dir_attr);
2565 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566}
2567
2568static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2569 struct inode *new_dir, struct qstr *new_name)
2570{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002571 struct nfs_server *server = NFS_SERVER(old_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 struct nfs4_rename_arg arg = {
2573 .old_dir = NFS_FH(old_dir),
2574 .new_dir = NFS_FH(new_dir),
2575 .old_name = old_name,
2576 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002577 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 };
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002579 struct nfs_fattr old_fattr, new_fattr;
2580 struct nfs4_rename_res res = {
2581 .server = server,
2582 .old_fattr = &old_fattr,
2583 .new_fattr = &new_fattr,
2584 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 struct rpc_message msg = {
2586 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2587 .rpc_argp = &arg,
2588 .rpc_resp = &res,
2589 };
2590 int status;
2591
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002592 nfs_fattr_init(res.old_fattr);
2593 nfs_fattr_init(res.new_fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002594 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
2596 if (!status) {
2597 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002598 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002600 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 }
2602 return status;
2603}
2604
2605static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2606 struct inode *new_dir, struct qstr *new_name)
2607{
2608 struct nfs4_exception exception = { };
2609 int err;
2610 do {
2611 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2612 _nfs4_proc_rename(old_dir, old_name,
2613 new_dir, new_name),
2614 &exception);
2615 } while (exception.retry);
2616 return err;
2617}
2618
2619static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2620{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002621 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 struct nfs4_link_arg arg = {
2623 .fh = NFS_FH(inode),
2624 .dir_fh = NFS_FH(dir),
2625 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002626 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002628 struct nfs_fattr fattr, dir_attr;
2629 struct nfs4_link_res res = {
2630 .server = server,
2631 .fattr = &fattr,
2632 .dir_attr = &dir_attr,
2633 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 struct rpc_message msg = {
2635 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2636 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002637 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 };
2639 int status;
2640
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002641 nfs_fattr_init(res.fattr);
2642 nfs_fattr_init(res.dir_attr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002643 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002644 if (!status) {
2645 update_changeattr(dir, &res.cinfo);
2646 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002647 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649
2650 return status;
2651}
2652
2653static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2654{
2655 struct nfs4_exception exception = { };
2656 int err;
2657 do {
2658 err = nfs4_handle_exception(NFS_SERVER(inode),
2659 _nfs4_proc_link(inode, dir, name),
2660 &exception);
2661 } while (exception.retry);
2662 return err;
2663}
2664
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002665struct nfs4_createdata {
2666 struct rpc_message msg;
2667 struct nfs4_create_arg arg;
2668 struct nfs4_create_res res;
2669 struct nfs_fh fh;
2670 struct nfs_fattr fattr;
2671 struct nfs_fattr dir_fattr;
2672};
2673
2674static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2675 struct qstr *name, struct iattr *sattr, u32 ftype)
2676{
2677 struct nfs4_createdata *data;
2678
2679 data = kzalloc(sizeof(*data), GFP_KERNEL);
2680 if (data != NULL) {
2681 struct nfs_server *server = NFS_SERVER(dir);
2682
2683 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2684 data->msg.rpc_argp = &data->arg;
2685 data->msg.rpc_resp = &data->res;
2686 data->arg.dir_fh = NFS_FH(dir);
2687 data->arg.server = server;
2688 data->arg.name = name;
2689 data->arg.attrs = sattr;
2690 data->arg.ftype = ftype;
2691 data->arg.bitmask = server->attr_bitmask;
2692 data->res.server = server;
2693 data->res.fh = &data->fh;
2694 data->res.fattr = &data->fattr;
2695 data->res.dir_fattr = &data->dir_fattr;
2696 nfs_fattr_init(data->res.fattr);
2697 nfs_fattr_init(data->res.dir_fattr);
2698 }
2699 return data;
2700}
2701
2702static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2703{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002704 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2705 &data->arg, &data->res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002706 if (status == 0) {
2707 update_changeattr(dir, &data->res.dir_cinfo);
2708 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2709 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2710 }
2711 return status;
2712}
2713
2714static void nfs4_free_createdata(struct nfs4_createdata *data)
2715{
2716 kfree(data);
2717}
2718
Chuck Lever4f390c12006-08-22 20:06:22 -04002719static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002720 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002722 struct nfs4_createdata *data;
2723 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724
Chuck Lever94a6d752006-08-22 20:06:23 -04002725 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002726 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002727
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002728 status = -ENOMEM;
2729 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2730 if (data == NULL)
2731 goto out;
2732
2733 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2734 data->arg.u.symlink.pages = &page;
2735 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002737 status = nfs4_do_create(dir, dentry, data);
2738
2739 nfs4_free_createdata(data);
2740out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 return status;
2742}
2743
Chuck Lever4f390c12006-08-22 20:06:22 -04002744static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002745 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746{
2747 struct nfs4_exception exception = { };
2748 int err;
2749 do {
2750 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002751 _nfs4_proc_symlink(dir, dentry, page,
2752 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 &exception);
2754 } while (exception.retry);
2755 return err;
2756}
2757
2758static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2759 struct iattr *sattr)
2760{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002761 struct nfs4_createdata *data;
2762 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002764 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2765 if (data == NULL)
2766 goto out;
2767
2768 status = nfs4_do_create(dir, dentry, data);
2769
2770 nfs4_free_createdata(data);
2771out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 return status;
2773}
2774
2775static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2776 struct iattr *sattr)
2777{
2778 struct nfs4_exception exception = { };
2779 int err;
2780 do {
2781 err = nfs4_handle_exception(NFS_SERVER(dir),
2782 _nfs4_proc_mkdir(dir, dentry, sattr),
2783 &exception);
2784 } while (exception.retry);
2785 return err;
2786}
2787
2788static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2789 u64 cookie, struct page *page, unsigned int count, int plus)
2790{
2791 struct inode *dir = dentry->d_inode;
2792 struct nfs4_readdir_arg args = {
2793 .fh = NFS_FH(dir),
2794 .pages = &page,
2795 .pgbase = 0,
2796 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002797 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 };
2799 struct nfs4_readdir_res res;
2800 struct rpc_message msg = {
2801 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2802 .rpc_argp = &args,
2803 .rpc_resp = &res,
2804 .rpc_cred = cred,
2805 };
2806 int status;
2807
Harvey Harrison3110ff82008-05-02 13:42:44 -07002808 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002809 dentry->d_parent->d_name.name,
2810 dentry->d_name.name,
2811 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2813 res.pgbase = args.pgbase;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002814 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 if (status == 0)
2816 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustc4812992007-09-28 17:11:45 -04002817
2818 nfs_invalidate_atime(dir);
2819
Harvey Harrison3110ff82008-05-02 13:42:44 -07002820 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 return status;
2822}
2823
2824static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2825 u64 cookie, struct page *page, unsigned int count, int plus)
2826{
2827 struct nfs4_exception exception = { };
2828 int err;
2829 do {
2830 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2831 _nfs4_proc_readdir(dentry, cred, cookie,
2832 page, count, plus),
2833 &exception);
2834 } while (exception.retry);
2835 return err;
2836}
2837
2838static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2839 struct iattr *sattr, dev_t rdev)
2840{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002841 struct nfs4_createdata *data;
2842 int mode = sattr->ia_mode;
2843 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844
2845 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2846 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002847
2848 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2849 if (data == NULL)
2850 goto out;
2851
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002853 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002855 data->arg.ftype = NF4BLK;
2856 data->arg.u.device.specdata1 = MAJOR(rdev);
2857 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 }
2859 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002860 data->arg.ftype = NF4CHR;
2861 data->arg.u.device.specdata1 = MAJOR(rdev);
2862 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002865 status = nfs4_do_create(dir, dentry, data);
2866
2867 nfs4_free_createdata(data);
2868out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 return status;
2870}
2871
2872static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2873 struct iattr *sattr, dev_t rdev)
2874{
2875 struct nfs4_exception exception = { };
2876 int err;
2877 do {
2878 err = nfs4_handle_exception(NFS_SERVER(dir),
2879 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2880 &exception);
2881 } while (exception.retry);
2882 return err;
2883}
2884
2885static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2886 struct nfs_fsstat *fsstat)
2887{
2888 struct nfs4_statfs_arg args = {
2889 .fh = fhandle,
2890 .bitmask = server->attr_bitmask,
2891 };
Benny Halevy24ad1482009-04-01 09:21:56 -04002892 struct nfs4_statfs_res res = {
2893 .fsstat = fsstat,
2894 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 struct rpc_message msg = {
2896 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2897 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04002898 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 };
2900
Trond Myklebust0e574af2005-10-27 22:12:38 -04002901 nfs_fattr_init(fsstat->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002902 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903}
2904
2905static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2906{
2907 struct nfs4_exception exception = { };
2908 int err;
2909 do {
2910 err = nfs4_handle_exception(server,
2911 _nfs4_proc_statfs(server, fhandle, fsstat),
2912 &exception);
2913 } while (exception.retry);
2914 return err;
2915}
2916
2917static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2918 struct nfs_fsinfo *fsinfo)
2919{
2920 struct nfs4_fsinfo_arg args = {
2921 .fh = fhandle,
2922 .bitmask = server->attr_bitmask,
2923 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04002924 struct nfs4_fsinfo_res res = {
2925 .fsinfo = fsinfo,
2926 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 struct rpc_message msg = {
2928 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2929 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04002930 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 };
2932
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002933 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934}
2935
2936static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2937{
2938 struct nfs4_exception exception = { };
2939 int err;
2940
2941 do {
2942 err = nfs4_handle_exception(server,
2943 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2944 &exception);
2945 } while (exception.retry);
2946 return err;
2947}
2948
2949static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2950{
Trond Myklebust0e574af2005-10-27 22:12:38 -04002951 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2953}
2954
2955static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2956 struct nfs_pathconf *pathconf)
2957{
2958 struct nfs4_pathconf_arg args = {
2959 .fh = fhandle,
2960 .bitmask = server->attr_bitmask,
2961 };
Benny Halevyd45b2982009-04-01 09:21:58 -04002962 struct nfs4_pathconf_res res = {
2963 .pathconf = pathconf,
2964 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 struct rpc_message msg = {
2966 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2967 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04002968 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 };
2970
2971 /* None of the pathconf attributes are mandatory to implement */
2972 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
2973 memset(pathconf, 0, sizeof(*pathconf));
2974 return 0;
2975 }
2976
Trond Myklebust0e574af2005-10-27 22:12:38 -04002977 nfs_fattr_init(pathconf->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002978 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979}
2980
2981static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2982 struct nfs_pathconf *pathconf)
2983{
2984 struct nfs4_exception exception = { };
2985 int err;
2986
2987 do {
2988 err = nfs4_handle_exception(server,
2989 _nfs4_proc_pathconf(server, fhandle, pathconf),
2990 &exception);
2991 } while (exception.retry);
2992 return err;
2993}
2994
Trond Myklebustec06c092006-03-20 13:44:27 -05002995static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996{
Trond Myklebustec06c092006-03-20 13:44:27 -05002997 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
Andy Adamsonf11c88a2009-04-01 09:22:25 -04002999 dprintk("--> %s\n", __func__);
3000
3001 /* nfs4_sequence_free_slot called in the read rpc_call_done */
3002 nfs4_sequence_done(server, &data->res.seq_res, task->tk_status);
3003
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003004 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Andy Adamsone608e792009-12-04 15:55:29 -05003005 nfs4_restart_rpc(task, server->nfs_client, &data->res.seq_res);
Trond Myklebustec06c092006-03-20 13:44:27 -05003006 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003008
3009 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003011 renew_lease(server, data->timestamp);
3012 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013}
3014
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003015static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 data->timestamp = jiffies;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003018 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019}
3020
Trond Myklebust788e7a82006-03-20 13:44:27 -05003021static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 struct inode *inode = data->inode;
3024
Andy Adamsondef6ed72009-04-01 09:22:26 -04003025 /* slot is freed in nfs_writeback_done */
3026 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3027 task->tk_status);
3028
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003029 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Andy Adamsone608e792009-12-04 15:55:29 -05003030 nfs4_restart_rpc(task, NFS_SERVER(inode)->nfs_client,
3031 &data->res.seq_res);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003032 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003034 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003036 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003037 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003038 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039}
3040
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003041static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003043 struct nfs_server *server = NFS_SERVER(data->inode);
3044
Trond Myklebusta65318b2009-03-11 14:10:28 -04003045 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003046 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 data->timestamp = jiffies;
3048
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003049 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050}
3051
Trond Myklebust788e7a82006-03-20 13:44:27 -05003052static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 struct inode *inode = data->inode;
3055
Andy Adamson21d9a852009-04-01 09:22:27 -04003056 nfs4_sequence_done(NFS_SERVER(inode), &data->res.seq_res,
3057 task->tk_status);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003058 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Andy Adamsone608e792009-12-04 15:55:29 -05003059 nfs4_restart_rpc(task, NFS_SERVER(inode)->nfs_client,
3060 &data->res.seq_res);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003061 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 }
Andy Adamson21d9a852009-04-01 09:22:27 -04003063 nfs4_sequence_free_slot(NFS_SERVER(inode)->nfs_client,
3064 &data->res.seq_res);
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003065 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003066 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067}
3068
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003069static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003071 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
Trond Myklebusta65318b2009-03-11 14:10:28 -04003073 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003074 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003075 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076}
3077
3078/*
3079 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3080 * standalone procedure for queueing an asynchronous RENEW.
3081 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003082static void nfs4_renew_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083{
David Howellsadfa6f92006-08-22 20:06:08 -04003084 struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp;
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003085 unsigned long timestamp = (unsigned long)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086
3087 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003088 /* Unless we're shutting down, schedule state recovery! */
3089 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3090 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 return;
3092 }
3093 spin_lock(&clp->cl_lock);
3094 if (time_before(clp->cl_last_renewal,timestamp))
3095 clp->cl_last_renewal = timestamp;
3096 spin_unlock(&clp->cl_lock);
3097}
3098
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003099static const struct rpc_call_ops nfs4_renew_ops = {
3100 .rpc_call_done = nfs4_renew_done,
3101};
3102
David Howellsadfa6f92006-08-22 20:06:08 -04003103int nfs4_proc_async_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
3111 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003112 &nfs4_renew_ops, (void *)jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113}
3114
David Howellsadfa6f92006-08-22 20:06:08 -04003115int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116{
3117 struct rpc_message msg = {
3118 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3119 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003120 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 };
3122 unsigned long now = jiffies;
3123 int status;
3124
3125 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3126 if (status < 0)
3127 return status;
3128 spin_lock(&clp->cl_lock);
3129 if (time_before(clp->cl_last_renewal,now))
3130 clp->cl_last_renewal = now;
3131 spin_unlock(&clp->cl_lock);
3132 return 0;
3133}
3134
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003135static inline int nfs4_server_supports_acls(struct nfs_server *server)
3136{
3137 return (server->caps & NFS_CAP_ACLS)
3138 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3139 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3140}
3141
3142/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3143 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3144 * the stack.
3145 */
3146#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3147
3148static void buf_to_pages(const void *buf, size_t buflen,
3149 struct page **pages, unsigned int *pgbase)
3150{
3151 const void *p = buf;
3152
3153 *pgbase = offset_in_page(buf);
3154 p -= *pgbase;
3155 while (p < buf + buflen) {
3156 *(pages++) = virt_to_page(p);
3157 p += PAGE_CACHE_SIZE;
3158 }
3159}
3160
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003161struct nfs4_cached_acl {
3162 int cached;
3163 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003164 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003165};
3166
3167static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003168{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003169 struct nfs_inode *nfsi = NFS_I(inode);
3170
3171 spin_lock(&inode->i_lock);
3172 kfree(nfsi->nfs4_acl);
3173 nfsi->nfs4_acl = acl;
3174 spin_unlock(&inode->i_lock);
3175}
3176
3177static void nfs4_zap_acl_attr(struct inode *inode)
3178{
3179 nfs4_set_cached_acl(inode, NULL);
3180}
3181
3182static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3183{
3184 struct nfs_inode *nfsi = NFS_I(inode);
3185 struct nfs4_cached_acl *acl;
3186 int ret = -ENOENT;
3187
3188 spin_lock(&inode->i_lock);
3189 acl = nfsi->nfs4_acl;
3190 if (acl == NULL)
3191 goto out;
3192 if (buf == NULL) /* user is just asking for length */
3193 goto out_len;
3194 if (acl->cached == 0)
3195 goto out;
3196 ret = -ERANGE; /* see getxattr(2) man page */
3197 if (acl->len > buflen)
3198 goto out;
3199 memcpy(buf, acl->data, acl->len);
3200out_len:
3201 ret = acl->len;
3202out:
3203 spin_unlock(&inode->i_lock);
3204 return ret;
3205}
3206
3207static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3208{
3209 struct nfs4_cached_acl *acl;
3210
3211 if (buf && acl_len <= PAGE_SIZE) {
3212 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3213 if (acl == NULL)
3214 goto out;
3215 acl->cached = 1;
3216 memcpy(acl->data, buf, acl_len);
3217 } else {
3218 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3219 if (acl == NULL)
3220 goto out;
3221 acl->cached = 0;
3222 }
3223 acl->len = acl_len;
3224out:
3225 nfs4_set_cached_acl(inode, acl);
3226}
3227
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003228static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003229{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003230 struct page *pages[NFS4ACL_MAXPAGES];
3231 struct nfs_getaclargs args = {
3232 .fh = NFS_FH(inode),
3233 .acl_pages = pages,
3234 .acl_len = buflen,
3235 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003236 struct nfs_getaclres res = {
3237 .acl_len = buflen,
3238 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003239 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003240 struct rpc_message msg = {
3241 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3242 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003243 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003244 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003245 struct page *localpage = NULL;
3246 int ret;
3247
3248 if (buflen < PAGE_SIZE) {
3249 /* As long as we're doing a round trip to the server anyway,
3250 * let's be prepared for a page of acl data. */
3251 localpage = alloc_page(GFP_KERNEL);
3252 resp_buf = page_address(localpage);
3253 if (localpage == NULL)
3254 return -ENOMEM;
3255 args.acl_pages[0] = localpage;
3256 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003257 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003258 } else {
3259 resp_buf = buf;
3260 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3261 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003262 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003263 if (ret)
3264 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003265 if (res.acl_len > args.acl_len)
3266 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003267 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003268 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003269 if (buf) {
3270 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003271 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003272 goto out_free;
3273 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003274 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003275 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003276 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003277out_free:
3278 if (localpage)
3279 __free_page(localpage);
3280 return ret;
3281}
3282
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003283static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3284{
3285 struct nfs4_exception exception = { };
3286 ssize_t ret;
3287 do {
3288 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3289 if (ret >= 0)
3290 break;
3291 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3292 } while (exception.retry);
3293 return ret;
3294}
3295
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003296static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3297{
3298 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003299 int ret;
3300
3301 if (!nfs4_server_supports_acls(server))
3302 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003303 ret = nfs_revalidate_inode(server, inode);
3304 if (ret < 0)
3305 return ret;
3306 ret = nfs4_read_cached_acl(inode, buf, buflen);
3307 if (ret != -ENOENT)
3308 return ret;
3309 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003310}
3311
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003312static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003313{
3314 struct nfs_server *server = NFS_SERVER(inode);
3315 struct page *pages[NFS4ACL_MAXPAGES];
3316 struct nfs_setaclargs arg = {
3317 .fh = NFS_FH(inode),
3318 .acl_pages = pages,
3319 .acl_len = buflen,
3320 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003321 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003322 struct rpc_message msg = {
3323 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3324 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003325 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003326 };
3327 int ret;
3328
3329 if (!nfs4_server_supports_acls(server))
3330 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07003331 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003332 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003333 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003334 nfs_access_zap_cache(inode);
3335 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003336 return ret;
3337}
3338
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003339static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3340{
3341 struct nfs4_exception exception = { };
3342 int err;
3343 do {
3344 err = nfs4_handle_exception(NFS_SERVER(inode),
3345 __nfs4_proc_set_acl(inode, buf, buflen),
3346 &exception);
3347 } while (exception.retry);
3348 return err;
3349}
3350
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351static int
Andy Adamson8328d592009-04-01 09:22:35 -04003352_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 -07003353{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 if (!clp || task->tk_status >= 0)
3355 return 0;
3356 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003357 case -NFS4ERR_ADMIN_REVOKED:
3358 case -NFS4ERR_BAD_STATEID:
3359 case -NFS4ERR_OPENMODE:
3360 if (state == NULL)
3361 break;
3362 nfs4_state_mark_reclaim_nograce(clp, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 case -NFS4ERR_STALE_CLIENTID:
3364 case -NFS4ERR_STALE_STATEID:
3365 case -NFS4ERR_EXPIRED:
Trond Myklebust5d008372008-02-22 16:34:17 -05003366 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 nfs4_schedule_state_recovery(clp);
Trond Myklebuste005e802008-12-23 15:21:48 -05003368 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
Trond Myklebustfda13932008-02-22 16:34:12 -05003369 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 task->tk_status = 0;
3371 return -EAGAIN;
Andy Adamson4745e312009-04-01 09:22:42 -04003372#if defined(CONFIG_NFS_V4_1)
3373 case -NFS4ERR_BADSESSION:
3374 case -NFS4ERR_BADSLOT:
3375 case -NFS4ERR_BAD_HIGH_SLOT:
3376 case -NFS4ERR_DEADSESSION:
3377 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3378 case -NFS4ERR_SEQ_FALSE_RETRY:
3379 case -NFS4ERR_SEQ_MISORDERED:
3380 dprintk("%s ERROR %d, Reset session\n", __func__,
3381 task->tk_status);
Andy Adamson6df08182009-12-04 15:55:05 -05003382 set_bit(NFS4CLNT_SESSION_RESET, &clp->cl_state);
Andy Adamson4745e312009-04-01 09:22:42 -04003383 task->tk_status = 0;
3384 return -EAGAIN;
3385#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 case -NFS4ERR_DELAY:
Andy Adamson8328d592009-04-01 09:22:35 -04003387 if (server)
3388 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003389 case -NFS4ERR_GRACE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3391 task->tk_status = 0;
3392 return -EAGAIN;
3393 case -NFS4ERR_OLD_STATEID:
3394 task->tk_status = 0;
3395 return -EAGAIN;
3396 }
3397 task->tk_status = nfs4_map_errors(task->tk_status);
3398 return 0;
3399}
3400
Andy Adamson8328d592009-04-01 09:22:35 -04003401static int
3402nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
3403{
3404 return _nfs4_async_handle_error(task, server, server->nfs_client, state);
3405}
3406
David Howellsadfa6f92006-08-22 20:06:08 -04003407int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408{
3409 nfs4_verifier sc_verifier;
3410 struct nfs4_setclientid setclientid = {
3411 .sc_verifier = &sc_verifier,
3412 .sc_prog = program,
3413 };
3414 struct rpc_message msg = {
3415 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3416 .rpc_argp = &setclientid,
3417 .rpc_resp = clp,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003418 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 };
Al Virobc4785c2006-10-19 23:28:51 -07003420 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 int loop = 0;
3422 int status;
3423
Al Virobc4785c2006-10-19 23:28:51 -07003424 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3426 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3427
3428 for(;;) {
3429 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003430 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003431 clp->cl_ipaddr,
3432 rpc_peeraddr2str(clp->cl_rpcclient,
3433 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003434 rpc_peeraddr2str(clp->cl_rpcclient,
3435 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003436 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 clp->cl_id_uniquifier);
3438 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003439 sizeof(setclientid.sc_netid),
3440 rpc_peeraddr2str(clp->cl_rpcclient,
3441 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003443 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 clp->cl_ipaddr, port >> 8, port & 255);
3445
3446 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3447 if (status != -NFS4ERR_CLID_INUSE)
3448 break;
3449 if (signalled())
3450 break;
3451 if (loop++ & 1)
3452 ssleep(clp->cl_lease_time + 1);
3453 else
3454 if (++clp->cl_id_uniquifier == 0)
3455 break;
3456 }
3457 return status;
3458}
3459
David Howellsadfa6f92006-08-22 20:06:08 -04003460static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461{
3462 struct nfs_fsinfo fsinfo;
3463 struct rpc_message msg = {
3464 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
3465 .rpc_argp = clp,
3466 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003467 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 };
3469 unsigned long now;
3470 int status;
3471
3472 now = jiffies;
3473 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3474 if (status == 0) {
3475 spin_lock(&clp->cl_lock);
3476 clp->cl_lease_time = fsinfo.lease_time * HZ;
3477 clp->cl_last_renewal = now;
3478 spin_unlock(&clp->cl_lock);
3479 }
3480 return status;
3481}
3482
David Howellsadfa6f92006-08-22 20:06:08 -04003483int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003484{
Benny Halevy77e03672008-06-24 20:25:57 +03003485 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003486 int err;
3487 do {
3488 err = _nfs4_proc_setclientid_confirm(clp, cred);
3489 switch (err) {
3490 case 0:
3491 return err;
3492 case -NFS4ERR_RESOURCE:
3493 /* The IBM lawyers misread another document! */
3494 case -NFS4ERR_DELAY:
3495 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3496 }
3497 } while (err == 0);
3498 return err;
3499}
3500
Trond Myklebustfe650402006-01-03 09:55:18 +01003501struct nfs4_delegreturndata {
3502 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003503 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003504 struct nfs_fh fh;
3505 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003506 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003507 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003508 int rpc_status;
3509};
3510
Trond Myklebustfe650402006-01-03 09:55:18 +01003511static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3512{
3513 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003514
3515 nfs4_sequence_done_free_slot(data->res.server, &data->res.seq_res,
3516 task->tk_status);
3517
Trond Myklebustfe650402006-01-03 09:55:18 +01003518 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003519 if (data->rpc_status == 0)
Trond Myklebustfa178f22006-01-03 09:55:38 +01003520 renew_lease(data->res.server, data->timestamp);
Trond Myklebustfe650402006-01-03 09:55:18 +01003521}
3522
3523static void nfs4_delegreturn_release(void *calldata)
3524{
Trond Myklebustfe650402006-01-03 09:55:18 +01003525 kfree(calldata);
3526}
3527
Andy Adamson938e1012009-04-01 09:22:28 -04003528#if defined(CONFIG_NFS_V4_1)
3529static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3530{
3531 struct nfs4_delegreturndata *d_data;
3532
3533 d_data = (struct nfs4_delegreturndata *)data;
3534
3535 if (nfs4_setup_sequence(d_data->res.server->nfs_client,
3536 &d_data->args.seq_args,
3537 &d_data->res.seq_res, 1, task))
3538 return;
3539 rpc_call_start(task);
3540}
3541#endif /* CONFIG_NFS_V4_1 */
3542
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003543static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003544#if defined(CONFIG_NFS_V4_1)
3545 .rpc_call_prepare = nfs4_delegreturn_prepare,
3546#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003547 .rpc_call_done = nfs4_delegreturn_done,
3548 .rpc_release = nfs4_delegreturn_release,
3549};
3550
Trond Myklebuste6f81072008-01-24 18:14:34 -05003551static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003552{
3553 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003554 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003555 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003556 struct rpc_message msg = {
3557 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3558 .rpc_cred = cred,
3559 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003560 struct rpc_task_setup task_setup_data = {
3561 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003562 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003563 .callback_ops = &nfs4_delegreturn_ops,
3564 .flags = RPC_TASK_ASYNC,
3565 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003566 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003567
Andy Adamson938e1012009-04-01 09:22:28 -04003568 data = kzalloc(sizeof(*data), GFP_KERNEL);
Trond Myklebustfe650402006-01-03 09:55:18 +01003569 if (data == NULL)
3570 return -ENOMEM;
3571 data->args.fhandle = &data->fh;
3572 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003573 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003574 nfs_copy_fh(&data->fh, NFS_FH(inode));
3575 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003576 data->res.fattr = &data->fattr;
3577 data->res.server = server;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003578 data->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003579 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003580 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003581 data->rpc_status = 0;
3582
Trond Myklebustc970aa82007-07-14 15:39:59 -04003583 task_setup_data.callback_data = data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003584 msg.rpc_argp = &data->args,
3585 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003586 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003587 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003588 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003589 if (!issync)
3590 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003591 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003592 if (status != 0)
3593 goto out;
3594 status = data->rpc_status;
3595 if (status != 0)
3596 goto out;
3597 nfs_refresh_inode(inode, &data->fattr);
3598out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003599 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003600 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601}
3602
Trond Myklebuste6f81072008-01-24 18:14:34 -05003603int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604{
3605 struct nfs_server *server = NFS_SERVER(inode);
3606 struct nfs4_exception exception = { };
3607 int err;
3608 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003609 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 switch (err) {
3611 case -NFS4ERR_STALE_STATEID:
3612 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 case 0:
3614 return 0;
3615 }
3616 err = nfs4_handle_exception(server, err, &exception);
3617 } while (exception.retry);
3618 return err;
3619}
3620
3621#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3622#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3623
3624/*
3625 * sleep, with exponential backoff, and retry the LOCK operation.
3626 */
3627static unsigned long
3628nfs4_set_lock_task_retry(unsigned long timeout)
3629{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003630 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 timeout <<= 1;
3632 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3633 return NFS4_LOCK_MAXTIMEOUT;
3634 return timeout;
3635}
3636
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3638{
3639 struct inode *inode = state->inode;
3640 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003641 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003642 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003644 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003646 struct nfs_lockt_res res = {
3647 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 };
3649 struct rpc_message msg = {
3650 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3651 .rpc_argp = &arg,
3652 .rpc_resp = &res,
3653 .rpc_cred = state->owner->so_cred,
3654 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 struct nfs4_lock_state *lsp;
3656 int status;
3657
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003658 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003659 status = nfs4_set_lock_state(state, request);
3660 if (status != 0)
3661 goto out;
3662 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003663 arg.lock_owner.id = lsp->ls_id.id;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003664 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003665 switch (status) {
3666 case 0:
3667 request->fl_type = F_UNLCK;
3668 break;
3669 case -NFS4ERR_DENIED:
3670 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003672 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003673out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 return status;
3675}
3676
3677static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3678{
3679 struct nfs4_exception exception = { };
3680 int err;
3681
3682 do {
3683 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3684 _nfs4_proc_getlk(state, cmd, request),
3685 &exception);
3686 } while (exception.retry);
3687 return err;
3688}
3689
3690static int do_vfs_lock(struct file *file, struct file_lock *fl)
3691{
3692 int res = 0;
3693 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3694 case FL_POSIX:
3695 res = posix_lock_file_wait(file, fl);
3696 break;
3697 case FL_FLOCK:
3698 res = flock_lock_file_wait(file, fl);
3699 break;
3700 default:
3701 BUG();
3702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 return res;
3704}
3705
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003706struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003707 struct nfs_locku_args arg;
3708 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003709 struct nfs4_lock_state *lsp;
3710 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003711 struct file_lock fl;
3712 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003713 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003714};
3715
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003716static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3717 struct nfs_open_context *ctx,
3718 struct nfs4_lock_state *lsp,
3719 struct nfs_seqid *seqid)
3720{
3721 struct nfs4_unlockdata *p;
3722 struct inode *inode = lsp->ls_state->inode;
3723
Andy Adamsona8936932009-04-01 09:22:23 -04003724 p = kzalloc(sizeof(*p), GFP_KERNEL);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003725 if (p == NULL)
3726 return NULL;
3727 p->arg.fh = NFS_FH(inode);
3728 p->arg.fl = &p->fl;
3729 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003730 p->res.seqid = seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003731 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003732 p->arg.stateid = &lsp->ls_stateid;
3733 p->lsp = lsp;
3734 atomic_inc(&lsp->ls_count);
3735 /* Ensure we don't close file until we're done freeing locks! */
3736 p->ctx = get_nfs_open_context(ctx);
3737 memcpy(&p->fl, fl, sizeof(p->fl));
3738 p->server = NFS_SERVER(inode);
3739 return p;
3740}
3741
Trond Myklebust06f814a2006-01-03 09:55:07 +01003742static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003743{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003744 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003745 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003746 nfs4_put_lock_state(calldata->lsp);
3747 put_nfs_open_context(calldata->ctx);
3748 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003749}
3750
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003751static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003752{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003753 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003754
Andy Adamsona8936932009-04-01 09:22:23 -04003755 nfs4_sequence_done(calldata->server, &calldata->res.seq_res,
3756 task->tk_status);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003757 if (RPC_ASSASSINATED(task))
3758 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003759 switch (task->tk_status) {
3760 case 0:
3761 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003762 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003763 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003764 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003765 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003766 case -NFS4ERR_BAD_STATEID:
3767 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003768 case -NFS4ERR_STALE_STATEID:
3769 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003770 break;
3771 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003772 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Andy Adamsoneedc0202009-04-01 09:22:41 -04003773 nfs4_restart_rpc(task,
Andy Adamsone608e792009-12-04 15:55:29 -05003774 calldata->server->nfs_client,
3775 &calldata->res.seq_res);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003776 }
Andy Adamsona8936932009-04-01 09:22:23 -04003777 nfs4_sequence_free_slot(calldata->server->nfs_client,
3778 &calldata->res.seq_res);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003779}
3780
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003781static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003782{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003783 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003785 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003786 return;
3787 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003788 /* Note: exit _without_ running nfs4_locku_done */
3789 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003790 return;
3791 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003792 calldata->timestamp = jiffies;
Andy Adamsona8936932009-04-01 09:22:23 -04003793 if (nfs4_setup_sequence(calldata->server->nfs_client,
3794 &calldata->arg.seq_args,
3795 &calldata->res.seq_res, 1, task))
3796 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003797 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798}
3799
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003800static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003801 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003802 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003803 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003804};
3805
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003806static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3807 struct nfs_open_context *ctx,
3808 struct nfs4_lock_state *lsp,
3809 struct nfs_seqid *seqid)
3810{
3811 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003812 struct rpc_message msg = {
3813 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3814 .rpc_cred = ctx->cred,
3815 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003816 struct rpc_task_setup task_setup_data = {
3817 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003818 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003819 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003820 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003821 .flags = RPC_TASK_ASYNC,
3822 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003823
Frank Filz137d6ac2007-07-09 15:32:29 -07003824 /* Ensure this is an unlock - when canceling a lock, the
3825 * canceled lock is passed in, and it won't be an unlock.
3826 */
3827 fl->fl_type = F_UNLCK;
3828
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003829 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3830 if (data == NULL) {
3831 nfs_free_seqid(seqid);
3832 return ERR_PTR(-ENOMEM);
3833 }
3834
Trond Myklebust5138fde2007-07-14 15:40:01 -04003835 msg.rpc_argp = &data->arg,
3836 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003837 task_setup_data.callback_data = data;
3838 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003839}
3840
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3842{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003843 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003844 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003845 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003846 struct rpc_task *task;
3847 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003848 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849
Trond Myklebust9b073572006-06-29 16:38:34 -04003850 status = nfs4_set_lock_state(state, request);
3851 /* Unlock _before_ we do the RPC call */
3852 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003853 down_read(&nfsi->rwsem);
3854 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3855 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003856 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003857 }
3858 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003859 if (status != 0)
3860 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003861 /* Is this a delegated lock? */
3862 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04003863 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003864 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003865 seqid = nfs_alloc_seqid(&lsp->ls_seqid);
Trond Myklebust9b073572006-06-29 16:38:34 -04003866 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003867 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04003868 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003869 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003870 status = PTR_ERR(task);
3871 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04003872 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003873 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003874 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04003875out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003876 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003877 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878}
3879
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003880struct nfs4_lockdata {
3881 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003882 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003883 struct nfs4_lock_state *lsp;
3884 struct nfs_open_context *ctx;
3885 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003886 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003887 int rpc_status;
3888 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04003889 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003890};
3891
3892static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
3893 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp)
3894{
3895 struct nfs4_lockdata *p;
3896 struct inode *inode = lsp->ls_state->inode;
3897 struct nfs_server *server = NFS_SERVER(inode);
3898
3899 p = kzalloc(sizeof(*p), GFP_KERNEL);
3900 if (p == NULL)
3901 return NULL;
3902
3903 p->arg.fh = NFS_FH(inode);
3904 p->arg.fl = &p->fl;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003905 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid);
3906 if (p->arg.open_seqid == NULL)
3907 goto out_free;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003908 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid);
3909 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003910 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003911 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04003912 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003913 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003914 p->res.lock_seqid = p->arg.lock_seqid;
Andy Adamson5f7dbd52009-04-01 09:22:05 -04003915 p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003916 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04003917 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003918 atomic_inc(&lsp->ls_count);
3919 p->ctx = get_nfs_open_context(ctx);
3920 memcpy(&p->fl, fl, sizeof(p->fl));
3921 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003922out_free_seqid:
3923 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003924out_free:
3925 kfree(p);
3926 return NULL;
3927}
3928
3929static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3930{
3931 struct nfs4_lockdata *data = calldata;
3932 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933
Harvey Harrison3110ff82008-05-02 13:42:44 -07003934 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003935 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3936 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003937 /* Do we need to do an open_to_lock_owner? */
3938 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05003939 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
3940 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003941 data->arg.open_stateid = &state->stateid;
3942 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003943 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003944 } else
3945 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003946 data->timestamp = jiffies;
Andy Adamson66179ef2009-04-01 09:22:22 -04003947 if (nfs4_setup_sequence(data->server->nfs_client, &data->arg.seq_args,
3948 &data->res.seq_res, 1, task))
3949 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003950 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003951 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003952}
3953
3954static void nfs4_lock_done(struct rpc_task *task, void *calldata)
3955{
3956 struct nfs4_lockdata *data = calldata;
3957
Harvey Harrison3110ff82008-05-02 13:42:44 -07003958 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003959
Andy Adamson66179ef2009-04-01 09:22:22 -04003960 nfs4_sequence_done_free_slot(data->server, &data->res.seq_res,
3961 task->tk_status);
3962
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003963 data->rpc_status = task->tk_status;
3964 if (RPC_ASSASSINATED(task))
3965 goto out;
3966 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003967 if (data->rpc_status == 0)
3968 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
3969 else
3970 goto out;
3971 }
3972 if (data->rpc_status == 0) {
3973 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
3974 sizeof(data->lsp->ls_stateid.data));
3975 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04003976 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003977 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003978out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07003979 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003980}
3981
3982static void nfs4_lock_release(void *calldata)
3983{
3984 struct nfs4_lockdata *data = calldata;
3985
Harvey Harrison3110ff82008-05-02 13:42:44 -07003986 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003987 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003988 if (data->cancelled != 0) {
3989 struct rpc_task *task;
3990 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
3991 data->arg.lock_seqid);
3992 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003993 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07003994 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003995 } else
3996 nfs_free_seqid(data->arg.lock_seqid);
3997 nfs4_put_lock_state(data->lsp);
3998 put_nfs_open_context(data->ctx);
3999 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004000 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004001}
4002
4003static const struct rpc_call_ops nfs4_lock_ops = {
4004 .rpc_call_prepare = nfs4_lock_prepare,
4005 .rpc_call_done = nfs4_lock_done,
4006 .rpc_release = nfs4_lock_release,
4007};
4008
4009static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim)
4010{
4011 struct nfs4_lockdata *data;
4012 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004013 struct rpc_message msg = {
4014 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4015 .rpc_cred = state->owner->so_cred,
4016 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004017 struct rpc_task_setup task_setup_data = {
4018 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004019 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004020 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004021 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004022 .flags = RPC_TASK_ASYNC,
4023 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004024 int ret;
4025
Harvey Harrison3110ff82008-05-02 13:42:44 -07004026 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004027 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004028 fl->fl_u.nfs4_fl.owner);
4029 if (data == NULL)
4030 return -ENOMEM;
4031 if (IS_SETLKW(cmd))
4032 data->arg.block = 1;
4033 if (reclaim != 0)
4034 data->arg.reclaim = 1;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004035 msg.rpc_argp = &data->arg,
4036 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004037 task_setup_data.callback_data = data;
4038 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004039 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004040 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004041 ret = nfs4_wait_for_completion_rpc_task(task);
4042 if (ret == 0) {
4043 ret = data->rpc_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004044 } else
4045 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004046 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004047 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004048 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049}
4050
4051static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4052{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004053 struct nfs_server *server = NFS_SERVER(state->inode);
4054 struct nfs4_exception exception = { };
4055 int err;
4056
4057 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004058 /* Cache the lock if possible... */
4059 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4060 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004061 err = _nfs4_do_setlk(state, F_SETLK, request, 1);
4062 if (err != -NFS4ERR_DELAY)
4063 break;
4064 nfs4_handle_exception(server, err, &exception);
4065 } while (exception.retry);
4066 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067}
4068
4069static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4070{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004071 struct nfs_server *server = NFS_SERVER(state->inode);
4072 struct nfs4_exception exception = { };
4073 int err;
4074
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004075 err = nfs4_set_lock_state(state, request);
4076 if (err != 0)
4077 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004078 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004079 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4080 return 0;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004081 err = _nfs4_do_setlk(state, F_SETLK, request, 0);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004082 switch (err) {
4083 default:
4084 goto out;
4085 case -NFS4ERR_GRACE:
4086 case -NFS4ERR_DELAY:
4087 nfs4_handle_exception(server, err, &exception);
4088 err = 0;
4089 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004090 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004091out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004092 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093}
4094
4095static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4096{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004097 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004098 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 int status;
4100
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004101 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004102 status = nfs4_set_lock_state(state, request);
4103 if (status != 0)
4104 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004105 request->fl_flags |= FL_ACCESS;
4106 status = do_vfs_lock(request->fl_file, request);
4107 if (status < 0)
4108 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004109 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004110 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004111 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004112 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004113 request->fl_flags = fl_flags & ~FL_SLEEP;
4114 status = do_vfs_lock(request->fl_file, request);
4115 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004116 }
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004117 status = _nfs4_do_setlk(state, cmd, request, 0);
4118 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004119 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004120 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004121 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004122 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004123 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004124out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004125 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004126out:
4127 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 return status;
4129}
4130
4131static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4132{
4133 struct nfs4_exception exception = { };
4134 int err;
4135
4136 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004137 err = _nfs4_proc_setlk(state, cmd, request);
4138 if (err == -NFS4ERR_DENIED)
4139 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004141 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 } while (exception.retry);
4143 return err;
4144}
4145
4146static int
4147nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4148{
4149 struct nfs_open_context *ctx;
4150 struct nfs4_state *state;
4151 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4152 int status;
4153
4154 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004155 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 state = ctx->state;
4157
4158 if (request->fl_start < 0 || request->fl_end < 0)
4159 return -EINVAL;
4160
Trond Myklebustd9531262009-07-21 19:22:38 -04004161 if (IS_GETLK(cmd)) {
4162 if (state != NULL)
4163 return nfs4_proc_getlk(state, F_GETLK, request);
4164 return 0;
4165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166
4167 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4168 return -EINVAL;
4169
Trond Myklebustd9531262009-07-21 19:22:38 -04004170 if (request->fl_type == F_UNLCK) {
4171 if (state != NULL)
4172 return nfs4_proc_unlck(state, cmd, request);
4173 return 0;
4174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175
Trond Myklebustd9531262009-07-21 19:22:38 -04004176 if (state == NULL)
4177 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 do {
4179 status = nfs4_proc_setlk(state, cmd, request);
4180 if ((status != -EAGAIN) || IS_SETLK(cmd))
4181 break;
4182 timeout = nfs4_set_lock_task_retry(timeout);
4183 status = -ERESTARTSYS;
4184 if (signalled())
4185 break;
4186 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 return status;
4188}
4189
Trond Myklebust888e6942005-11-04 15:38:11 -05004190int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4191{
4192 struct nfs_server *server = NFS_SERVER(state->inode);
4193 struct nfs4_exception exception = { };
4194 int err;
4195
4196 err = nfs4_set_lock_state(state, fl);
4197 if (err != 0)
4198 goto out;
4199 do {
4200 err = _nfs4_do_setlk(state, F_SETLK, fl, 0);
Trond Myklebustd5122202009-06-17 13:22:58 -07004201 switch (err) {
4202 default:
4203 printk(KERN_ERR "%s: unhandled error %d.\n",
4204 __func__, err);
4205 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004206 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004207 goto out;
4208 case -NFS4ERR_EXPIRED:
4209 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004210 case -NFS4ERR_STALE_STATEID:
Trond Myklebustd5122202009-06-17 13:22:58 -07004211 nfs4_schedule_state_recovery(server->nfs_client);
4212 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004213 case -ERESTARTSYS:
4214 /*
4215 * The show must go on: exit, but mark the
4216 * stateid as needing recovery.
4217 */
4218 case -NFS4ERR_ADMIN_REVOKED:
4219 case -NFS4ERR_BAD_STATEID:
4220 case -NFS4ERR_OPENMODE:
4221 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4222 err = 0;
4223 goto out;
4224 case -ENOMEM:
4225 case -NFS4ERR_DENIED:
4226 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4227 err = 0;
4228 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004229 case -NFS4ERR_DELAY:
4230 break;
4231 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004232 err = nfs4_handle_exception(server, err, &exception);
4233 } while (exception.retry);
4234out:
4235 return err;
4236}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004237
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004238#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4239
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004240int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4241 size_t buflen, int flags)
4242{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004243 struct inode *inode = dentry->d_inode;
4244
4245 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4246 return -EOPNOTSUPP;
4247
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004248 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004249}
4250
4251/* The getxattr man page suggests returning -ENODATA for unknown attributes,
4252 * and that's what we'll do for e.g. user attributes that haven't been set.
4253 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4254 * attributes in kernel-managed attribute namespaces. */
4255ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4256 size_t buflen)
4257{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004258 struct inode *inode = dentry->d_inode;
4259
4260 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4261 return -EOPNOTSUPP;
4262
4263 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004264}
4265
4266ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4267{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004268 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004269
J. Bruce Fields096455a2006-03-20 23:23:42 -05004270 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4271 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004272 if (buf && buflen < len)
4273 return -ERANGE;
4274 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004275 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4276 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004277}
4278
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004279static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4280{
4281 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4282 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4283 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4284 return;
4285
4286 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4287 NFS_ATTR_FATTR_NLINK;
4288 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4289 fattr->nlink = 2;
4290}
4291
Trond Myklebust56659e92007-07-17 21:52:39 -04004292int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004293 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004294{
4295 struct nfs_server *server = NFS_SERVER(dir);
4296 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004297 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4298 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004299 };
4300 struct nfs4_fs_locations_arg args = {
4301 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004302 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004303 .page = page,
4304 .bitmask = bitmask,
4305 };
Benny Halevy22958462009-04-01 09:22:02 -04004306 struct nfs4_fs_locations_res res = {
4307 .fs_locations = fs_locations,
4308 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004309 struct rpc_message msg = {
4310 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4311 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004312 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004313 };
4314 int status;
4315
Harvey Harrison3110ff82008-05-02 13:42:44 -07004316 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004317 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004318 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004319 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04004320 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004321 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004322 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004323 return status;
4324}
4325
Andy Adamson557134a2009-04-01 09:21:53 -04004326#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004327/*
4328 * nfs4_proc_exchange_id()
4329 *
4330 * Since the clientid has expired, all compounds using sessions
4331 * associated with the stale clientid will be returning
4332 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4333 * be in some phase of session reset.
4334 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004335int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004336{
4337 nfs4_verifier verifier;
4338 struct nfs41_exchange_id_args args = {
4339 .client = clp,
4340 .flags = clp->cl_exchange_flags,
4341 };
4342 struct nfs41_exchange_id_res res = {
4343 .client = clp,
4344 };
4345 int status;
4346 struct rpc_message msg = {
4347 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4348 .rpc_argp = &args,
4349 .rpc_resp = &res,
4350 .rpc_cred = cred,
4351 };
4352 __be32 *p;
4353
4354 dprintk("--> %s\n", __func__);
4355 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004356
Benny Halevy99fe60d2009-04-01 09:22:29 -04004357 p = (u32 *)verifier.data;
4358 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4359 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4360 args.verifier = &verifier;
4361
4362 while (1) {
4363 args.id_len = scnprintf(args.id, sizeof(args.id),
4364 "%s/%s %u",
4365 clp->cl_ipaddr,
4366 rpc_peeraddr2str(clp->cl_rpcclient,
4367 RPC_DISPLAY_ADDR),
4368 clp->cl_id_uniquifier);
4369
4370 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4371
4372 if (status != NFS4ERR_CLID_INUSE)
4373 break;
4374
4375 if (signalled())
4376 break;
4377
4378 if (++clp->cl_id_uniquifier == 0)
4379 break;
4380 }
4381
4382 dprintk("<-- %s status= %d\n", __func__, status);
4383 return status;
4384}
4385
Andy Adamson2050f0c2009-04-01 09:22:30 -04004386struct nfs4_get_lease_time_data {
4387 struct nfs4_get_lease_time_args *args;
4388 struct nfs4_get_lease_time_res *res;
4389 struct nfs_client *clp;
4390};
4391
4392static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4393 void *calldata)
4394{
4395 int ret;
4396 struct nfs4_get_lease_time_data *data =
4397 (struct nfs4_get_lease_time_data *)calldata;
4398
4399 dprintk("--> %s\n", __func__);
4400 /* just setup sequence, do not trigger session recovery
4401 since we're invoked within one */
4402 ret = nfs41_setup_sequence(data->clp->cl_session,
4403 &data->args->la_seq_args,
4404 &data->res->lr_seq_res, 0, task);
4405
4406 BUG_ON(ret == -EAGAIN);
4407 rpc_call_start(task);
4408 dprintk("<-- %s\n", __func__);
4409}
4410
4411/*
4412 * Called from nfs4_state_manager thread for session setup, so don't recover
4413 * from sequence operation or clientid errors.
4414 */
4415static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4416{
4417 struct nfs4_get_lease_time_data *data =
4418 (struct nfs4_get_lease_time_data *)calldata;
4419
4420 dprintk("--> %s\n", __func__);
4421 nfs41_sequence_done(data->clp, &data->res->lr_seq_res, task->tk_status);
4422 switch (task->tk_status) {
4423 case -NFS4ERR_DELAY:
4424 case -NFS4ERR_GRACE:
4425 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4426 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4427 task->tk_status = 0;
Andy Adamson1d9ddde2009-12-04 15:55:27 -05004428 rpc_restart_call(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004429 return;
4430 }
4431 nfs41_sequence_free_slot(data->clp, &data->res->lr_seq_res);
4432 dprintk("<-- %s\n", __func__);
4433}
4434
4435struct rpc_call_ops nfs4_get_lease_time_ops = {
4436 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4437 .rpc_call_done = nfs4_get_lease_time_done,
4438};
4439
4440int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4441{
4442 struct rpc_task *task;
4443 struct nfs4_get_lease_time_args args;
4444 struct nfs4_get_lease_time_res res = {
4445 .lr_fsinfo = fsinfo,
4446 };
4447 struct nfs4_get_lease_time_data data = {
4448 .args = &args,
4449 .res = &res,
4450 .clp = clp,
4451 };
4452 struct rpc_message msg = {
4453 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4454 .rpc_argp = &args,
4455 .rpc_resp = &res,
4456 };
4457 struct rpc_task_setup task_setup = {
4458 .rpc_client = clp->cl_rpcclient,
4459 .rpc_message = &msg,
4460 .callback_ops = &nfs4_get_lease_time_ops,
4461 .callback_data = &data
4462 };
4463 int status;
4464
4465 res.lr_seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE;
4466 dprintk("--> %s\n", __func__);
4467 task = rpc_run_task(&task_setup);
4468
4469 if (IS_ERR(task))
4470 status = PTR_ERR(task);
4471 else {
4472 status = task->tk_status;
4473 rpc_put_task(task);
4474 }
4475 dprintk("<-- %s return %d\n", __func__, status);
4476
4477 return status;
4478}
4479
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004480/*
4481 * Reset a slot table
4482 */
4483static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, int max_slots,
4484 int old_max_slots, int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004485{
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004486 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004487 int ret = 0;
4488
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004489 dprintk("--> %s: max_reqs=%u, tbl %p\n", __func__, max_slots, tbl);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004490
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004491 /*
4492 * Until we have dynamic slot table adjustment, insist
4493 * upon the same slot table size
4494 */
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004495 if (max_slots != old_max_slots) {
4496 dprintk("%s reset slot table does't match old\n",
4497 __func__);
4498 ret = -EINVAL; /*XXX NFS4ERR_REQ_TOO_BIG ? */
4499 goto out;
4500 }
4501 spin_lock(&tbl->slot_tbl_lock);
4502 for (i = 0; i < max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004503 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004504 tbl->highest_used_slotid = -1;
4505 spin_unlock(&tbl->slot_tbl_lock);
4506 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4507 tbl, tbl->slots, tbl->max_slots);
4508out:
4509 dprintk("<-- %s: return %d\n", __func__, ret);
4510 return ret;
4511}
4512
Andy Adamsonfc931582009-04-01 09:22:31 -04004513/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004514 * Reset the forechannel and backchannel slot tables
4515 */
4516static int nfs4_reset_slot_tables(struct nfs4_session *session)
4517{
4518 int status;
4519
4520 status = nfs4_reset_slot_table(&session->fc_slot_table,
4521 session->fc_attrs.max_reqs,
4522 session->fc_slot_table.max_slots,
4523 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004524 if (status)
4525 return status;
4526
4527 status = nfs4_reset_slot_table(&session->bc_slot_table,
4528 session->bc_attrs.max_reqs,
4529 session->bc_slot_table.max_slots,
4530 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004531 return status;
4532}
4533
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004534/* Destroy the slot table */
4535static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4536{
4537 if (session->fc_slot_table.slots != NULL) {
4538 kfree(session->fc_slot_table.slots);
4539 session->fc_slot_table.slots = NULL;
4540 }
4541 if (session->bc_slot_table.slots != NULL) {
4542 kfree(session->bc_slot_table.slots);
4543 session->bc_slot_table.slots = NULL;
4544 }
4545 return;
4546}
4547
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004548/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004549 * Initialize slot table
4550 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004551static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4552 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04004553{
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004554 int i;
Andy Adamsonfc931582009-04-01 09:22:31 -04004555 struct nfs4_slot *slot;
4556 int ret = -ENOMEM;
4557
4558 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4559
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004560 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04004561
4562 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_KERNEL);
4563 if (!slot)
4564 goto out;
4565 for (i = 0; i < max_slots; ++i)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004566 slot[i].seq_nr = ivalue;
Andy Adamsonfc931582009-04-01 09:22:31 -04004567 ret = 0;
4568
4569 spin_lock(&tbl->slot_tbl_lock);
4570 if (tbl->slots != NULL) {
4571 spin_unlock(&tbl->slot_tbl_lock);
4572 dprintk("%s: slot table already initialized. tbl=%p slots=%p\n",
4573 __func__, tbl, tbl->slots);
4574 WARN_ON(1);
4575 goto out_free;
4576 }
4577 tbl->max_slots = max_slots;
4578 tbl->slots = slot;
4579 tbl->highest_used_slotid = -1; /* no slot is currently used */
4580 spin_unlock(&tbl->slot_tbl_lock);
4581 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4582 tbl, tbl->slots, tbl->max_slots);
4583out:
4584 dprintk("<-- %s: return %d\n", __func__, ret);
4585 return ret;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004586
Andy Adamsonfc931582009-04-01 09:22:31 -04004587out_free:
4588 kfree(slot);
4589 goto out;
4590}
4591
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004592/*
4593 * Initialize the forechannel and backchannel tables
4594 */
4595static int nfs4_init_slot_tables(struct nfs4_session *session)
4596{
4597 int status;
4598
4599 status = nfs4_init_slot_table(&session->fc_slot_table,
4600 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004601 if (status)
4602 return status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004603
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004604 status = nfs4_init_slot_table(&session->bc_slot_table,
4605 session->bc_attrs.max_reqs, 0);
4606 if (status)
4607 nfs4_destroy_slot_tables(session);
4608
4609 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04004610}
4611
4612struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4613{
4614 struct nfs4_session *session;
4615 struct nfs4_slot_table *tbl;
4616
4617 session = kzalloc(sizeof(struct nfs4_session), GFP_KERNEL);
4618 if (!session)
4619 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04004620
Andy Adamson76db6d92009-04-01 09:22:38 -04004621 /*
4622 * The create session reply races with the server back
4623 * channel probe. Mark the client NFS_CS_SESSION_INITING
4624 * so that the client back channel can find the
4625 * nfs_client struct
4626 */
4627 clp->cl_cons_state = NFS_CS_SESSION_INITING;
4628
Andy Adamson557134a2009-04-01 09:21:53 -04004629 tbl = &session->fc_slot_table;
4630 spin_lock_init(&tbl->slot_tbl_lock);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004631 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
4632
4633 tbl = &session->bc_slot_table;
4634 spin_lock_init(&tbl->slot_tbl_lock);
4635 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4636
Andy Adamson557134a2009-04-01 09:21:53 -04004637 session->clp = clp;
4638 return session;
4639}
4640
4641void nfs4_destroy_session(struct nfs4_session *session)
4642{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04004643 nfs4_proc_destroy_session(session);
4644 dprintk("%s Destroy backchannel for xprt %p\n",
4645 __func__, session->clp->cl_rpcclient->cl_xprt);
4646 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4647 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004648 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04004649 kfree(session);
4650}
4651
Andy Adamsonfc931582009-04-01 09:22:31 -04004652/*
4653 * Initialize the values to be used by the client in CREATE_SESSION
4654 * If nfs4_init_session set the fore channel request and response sizes,
4655 * use them.
4656 *
4657 * Set the back channel max_resp_sz_cached to zero to force the client to
4658 * always set csa_cachethis to FALSE because the current implementation
4659 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4660 */
4661static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4662{
4663 struct nfs4_session *session = args->client->cl_session;
4664 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4665 mxresp_sz = session->fc_attrs.max_resp_sz;
4666
4667 if (mxrqst_sz == 0)
4668 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4669 if (mxresp_sz == 0)
4670 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4671 /* Fore channel attributes */
4672 args->fc_attrs.headerpadsz = 0;
4673 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4674 args->fc_attrs.max_resp_sz = mxresp_sz;
4675 args->fc_attrs.max_resp_sz_cached = mxresp_sz;
4676 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4677 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4678
4679 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
4680 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4681 __func__,
4682 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
4683 args->fc_attrs.max_resp_sz_cached, args->fc_attrs.max_ops,
4684 args->fc_attrs.max_reqs);
4685
4686 /* Back channel attributes */
4687 args->bc_attrs.headerpadsz = 0;
4688 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4689 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4690 args->bc_attrs.max_resp_sz_cached = 0;
4691 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4692 args->bc_attrs.max_reqs = 1;
4693
4694 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4695 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4696 __func__,
4697 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4698 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4699 args->bc_attrs.max_reqs);
4700}
4701
Andy Adamson8d353012009-04-01 09:22:32 -04004702static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
4703{
4704 if (rcvd <= sent)
4705 return 0;
4706 printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
4707 "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
4708 return -EINVAL;
4709}
4710
4711#define _verify_fore_channel_attr(_name_) \
4712 _verify_channel_attr("fore", #_name_, \
4713 args->fc_attrs._name_, \
4714 session->fc_attrs._name_)
4715
4716#define _verify_back_channel_attr(_name_) \
4717 _verify_channel_attr("back", #_name_, \
4718 args->bc_attrs._name_, \
4719 session->bc_attrs._name_)
4720
4721/*
4722 * The server is not allowed to increase the fore channel header pad size,
4723 * maximum response size, or maximum number of operations.
4724 *
4725 * The back channel attributes are only negotiatied down: We send what the
4726 * (back channel) server insists upon.
4727 */
4728static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4729 struct nfs4_session *session)
4730{
4731 int ret = 0;
4732
4733 ret |= _verify_fore_channel_attr(headerpadsz);
4734 ret |= _verify_fore_channel_attr(max_resp_sz);
4735 ret |= _verify_fore_channel_attr(max_ops);
4736
4737 ret |= _verify_back_channel_attr(headerpadsz);
4738 ret |= _verify_back_channel_attr(max_rqst_sz);
4739 ret |= _verify_back_channel_attr(max_resp_sz);
4740 ret |= _verify_back_channel_attr(max_resp_sz_cached);
4741 ret |= _verify_back_channel_attr(max_ops);
4742 ret |= _verify_back_channel_attr(max_reqs);
4743
4744 return ret;
4745}
4746
Andy Adamsonfc931582009-04-01 09:22:31 -04004747static int _nfs4_proc_create_session(struct nfs_client *clp)
4748{
4749 struct nfs4_session *session = clp->cl_session;
4750 struct nfs41_create_session_args args = {
4751 .client = clp,
4752 .cb_program = NFS4_CALLBACK,
4753 };
4754 struct nfs41_create_session_res res = {
4755 .client = clp,
4756 };
4757 struct rpc_message msg = {
4758 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4759 .rpc_argp = &args,
4760 .rpc_resp = &res,
4761 };
4762 int status;
4763
4764 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04004765 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04004766
4767 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4768
Andy Adamson8d353012009-04-01 09:22:32 -04004769 if (!status)
4770 /* Verify the session's negotiated channel_attrs values */
4771 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04004772 if (!status) {
4773 /* Increment the clientid slot sequence id */
4774 clp->cl_seqid++;
4775 }
4776
4777 return status;
4778}
4779
4780/*
4781 * Issues a CREATE_SESSION operation to the server.
4782 * It is the responsibility of the caller to verify the session is
4783 * expired before calling this routine.
4784 */
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004785int nfs4_proc_create_session(struct nfs_client *clp, int reset)
Andy Adamsonfc931582009-04-01 09:22:31 -04004786{
4787 int status;
4788 unsigned *ptr;
4789 struct nfs_fsinfo fsinfo;
4790 struct nfs4_session *session = clp->cl_session;
4791
4792 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
4793
4794 status = _nfs4_proc_create_session(clp);
4795 if (status)
4796 goto out;
4797
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004798 /* Init or reset the fore channel */
4799 if (reset)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004800 status = nfs4_reset_slot_tables(session);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004801 else
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004802 status = nfs4_init_slot_tables(session);
Andy Adamsonfc931582009-04-01 09:22:31 -04004803 dprintk("fore channel slot table initialization returned %d\n", status);
4804 if (status)
4805 goto out;
4806
4807 ptr = (unsigned *)&session->sess_id.data[0];
4808 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
4809 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
4810
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004811 if (reset)
4812 /* Lease time is aleady set */
4813 goto out;
4814
Andy Adamsonfc931582009-04-01 09:22:31 -04004815 /* Get the lease time */
4816 status = nfs4_proc_get_lease_time(clp, &fsinfo);
4817 if (status == 0) {
4818 /* Update lease time and schedule renewal */
4819 spin_lock(&clp->cl_lock);
4820 clp->cl_lease_time = fsinfo.lease_time * HZ;
4821 clp->cl_last_renewal = jiffies;
4822 clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state);
4823 spin_unlock(&clp->cl_lock);
4824
4825 nfs4_schedule_state_renewal(clp);
4826 }
4827out:
4828 dprintk("<-- %s\n", __func__);
4829 return status;
4830}
4831
Andy Adamson0f3e66c2009-04-01 09:22:34 -04004832/*
4833 * Issue the over-the-wire RPC DESTROY_SESSION.
4834 * The caller must serialize access to this routine.
4835 */
4836int nfs4_proc_destroy_session(struct nfs4_session *session)
4837{
4838 int status = 0;
4839 struct rpc_message msg;
4840
4841 dprintk("--> nfs4_proc_destroy_session\n");
4842
4843 /* session is still being setup */
4844 if (session->clp->cl_cons_state != NFS_CS_READY)
4845 return status;
4846
4847 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
4848 msg.rpc_argp = session;
4849 msg.rpc_resp = NULL;
4850 msg.rpc_cred = NULL;
4851 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4852
4853 if (status)
4854 printk(KERN_WARNING
4855 "Got error %d from the server on DESTROY_SESSION. "
4856 "Session has been destroyed regardless...\n", status);
4857
4858 dprintk("<-- nfs4_proc_destroy_session\n");
4859 return status;
4860}
4861
Trond Myklebustfccba802009-07-21 16:48:07 -04004862int nfs4_init_session(struct nfs_server *server)
4863{
4864 struct nfs_client *clp = server->nfs_client;
4865 int ret;
4866
4867 if (!nfs4_has_session(clp))
4868 return 0;
4869
4870 clp->cl_session->fc_attrs.max_rqst_sz = server->wsize;
4871 clp->cl_session->fc_attrs.max_resp_sz = server->rsize;
4872 ret = nfs4_recover_expired_lease(server);
4873 if (!ret)
4874 ret = nfs4_check_client_ready(clp);
4875 return ret;
4876}
4877
Andy Adamsonfc01cea2009-04-01 09:22:36 -04004878/*
4879 * Renew the cl_session lease.
4880 */
4881static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
4882{
4883 struct nfs4_sequence_args args;
4884 struct nfs4_sequence_res res;
4885
4886 struct rpc_message msg = {
4887 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
4888 .rpc_argp = &args,
4889 .rpc_resp = &res,
4890 .rpc_cred = cred,
4891 };
4892
4893 args.sa_cache_this = 0;
4894
4895 return nfs4_call_sync_sequence(clp, clp->cl_rpcclient, &msg, &args,
4896 &res, 0);
4897}
4898
4899void nfs41_sequence_call_done(struct rpc_task *task, void *data)
4900{
4901 struct nfs_client *clp = (struct nfs_client *)data;
4902
4903 nfs41_sequence_done(clp, task->tk_msg.rpc_resp, task->tk_status);
4904
4905 if (task->tk_status < 0) {
4906 dprintk("%s ERROR %d\n", __func__, task->tk_status);
4907
4908 if (_nfs4_async_handle_error(task, NULL, clp, NULL)
4909 == -EAGAIN) {
Andy Adamsone608e792009-12-04 15:55:29 -05004910 nfs4_restart_rpc(task, clp, task->tk_msg.rpc_resp);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04004911 return;
4912 }
4913 }
4914 nfs41_sequence_free_slot(clp, task->tk_msg.rpc_resp);
4915 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
4916
Andy Adamsonfc01cea2009-04-01 09:22:36 -04004917 kfree(task->tk_msg.rpc_argp);
4918 kfree(task->tk_msg.rpc_resp);
4919
4920 dprintk("<-- %s\n", __func__);
4921}
4922
4923static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
4924{
4925 struct nfs_client *clp;
4926 struct nfs4_sequence_args *args;
4927 struct nfs4_sequence_res *res;
4928
4929 clp = (struct nfs_client *)data;
4930 args = task->tk_msg.rpc_argp;
4931 res = task->tk_msg.rpc_resp;
4932
4933 if (nfs4_setup_sequence(clp, args, res, 0, task))
4934 return;
4935 rpc_call_start(task);
4936}
4937
4938static const struct rpc_call_ops nfs41_sequence_ops = {
4939 .rpc_call_done = nfs41_sequence_call_done,
4940 .rpc_call_prepare = nfs41_sequence_prepare,
4941};
4942
4943static int nfs41_proc_async_sequence(struct nfs_client *clp,
4944 struct rpc_cred *cred)
4945{
4946 struct nfs4_sequence_args *args;
4947 struct nfs4_sequence_res *res;
4948 struct rpc_message msg = {
4949 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
4950 .rpc_cred = cred,
4951 };
4952
4953 args = kzalloc(sizeof(*args), GFP_KERNEL);
4954 if (!args)
4955 return -ENOMEM;
4956 res = kzalloc(sizeof(*res), GFP_KERNEL);
4957 if (!res) {
4958 kfree(args);
4959 return -ENOMEM;
4960 }
4961 res->sr_slotid = NFS4_MAX_SLOT_TABLE;
4962 msg.rpc_argp = args;
4963 msg.rpc_resp = res;
4964
4965 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
4966 &nfs41_sequence_ops, (void *)clp);
4967}
4968
Andy Adamson557134a2009-04-01 09:21:53 -04004969#endif /* CONFIG_NFS_V4_1 */
4970
Andy Adamson591d71c2009-04-01 09:22:47 -04004971struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05004972 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05004973 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 .recover_open = nfs4_open_reclaim,
4975 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04004976 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04004977 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978};
4979
Andy Adamson591d71c2009-04-01 09:22:47 -04004980#if defined(CONFIG_NFS_V4_1)
4981struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
4982 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
4983 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
4984 .recover_open = nfs4_open_reclaim,
4985 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05004986 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04004987 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04004988};
4989#endif /* CONFIG_NFS_V4_1 */
4990
4991struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05004992 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05004993 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994 .recover_open = nfs4_open_expired,
4995 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04004996 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04004997 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998};
4999
Andy Adamson591d71c2009-04-01 09:22:47 -04005000#if defined(CONFIG_NFS_V4_1)
5001struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5002 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5003 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5004 .recover_open = nfs4_open_expired,
5005 .recover_lock = nfs4_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05005006 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005007 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04005008};
5009#endif /* CONFIG_NFS_V4_1 */
5010
Benny Halevy29fba382009-04-01 09:22:44 -04005011struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5012 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04005013 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005014 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04005015};
5016
5017#if defined(CONFIG_NFS_V4_1)
5018struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5019 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04005020 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005021 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04005022};
5023#endif
5024
5025/*
5026 * Per minor version reboot and network partition recovery ops
5027 */
5028
Andy Adamson591d71c2009-04-01 09:22:47 -04005029struct nfs4_state_recovery_ops *nfs4_reboot_recovery_ops[] = {
5030 &nfs40_reboot_recovery_ops,
5031#if defined(CONFIG_NFS_V4_1)
5032 &nfs41_reboot_recovery_ops,
5033#endif
5034};
5035
5036struct nfs4_state_recovery_ops *nfs4_nograce_recovery_ops[] = {
5037 &nfs40_nograce_recovery_ops,
5038#if defined(CONFIG_NFS_V4_1)
5039 &nfs41_nograce_recovery_ops,
5040#endif
5041};
5042
Benny Halevy29fba382009-04-01 09:22:44 -04005043struct nfs4_state_maintenance_ops *nfs4_state_renewal_ops[] = {
5044 &nfs40_state_renewal_ops,
5045#if defined(CONFIG_NFS_V4_1)
5046 &nfs41_state_renewal_ops,
5047#endif
5048};
5049
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005050static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005051 .permission = nfs_permission,
5052 .getattr = nfs_getattr,
5053 .setattr = nfs_setattr,
5054 .getxattr = nfs4_getxattr,
5055 .setxattr = nfs4_setxattr,
5056 .listxattr = nfs4_listxattr,
5057};
5058
David Howells509de812006-08-22 20:06:11 -04005059const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 .version = 4, /* protocol version */
5061 .dentry_ops = &nfs4_dentry_operations,
5062 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005063 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 .getroot = nfs4_proc_get_root,
5065 .getattr = nfs4_proc_getattr,
5066 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04005067 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 .lookup = nfs4_proc_lookup,
5069 .access = nfs4_proc_access,
5070 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 .create = nfs4_proc_create,
5072 .remove = nfs4_proc_remove,
5073 .unlink_setup = nfs4_proc_unlink_setup,
5074 .unlink_done = nfs4_proc_unlink_done,
5075 .rename = nfs4_proc_rename,
5076 .link = nfs4_proc_link,
5077 .symlink = nfs4_proc_symlink,
5078 .mkdir = nfs4_proc_mkdir,
5079 .rmdir = nfs4_proc_remove,
5080 .readdir = nfs4_proc_readdir,
5081 .mknod = nfs4_proc_mknod,
5082 .statfs = nfs4_proc_statfs,
5083 .fsinfo = nfs4_proc_fsinfo,
5084 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04005085 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 .decode_dirent = nfs4_decode_dirent,
5087 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05005088 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005090 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005092 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005094 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04005095 .close_context = nfs4_close_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096};
5097
5098/*
5099 * Local variables:
5100 * c-basic-offset: 8
5101 * End:
5102 */