blob: 82f3a82b7115b438d02924dd6023bca97dce218b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/sunrpc/clnt.h>
44#include <linux/nfs.h>
45#include <linux/nfs4.h>
46#include <linux/nfs_fs.h>
47#include <linux/nfs_page.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070049#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040050#include <linux/module.h>
Andy Adamson5a0ffe52009-04-01 09:23:18 -040051#include <linux/sunrpc/bc_xprt.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000052#include <linux/xattr.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Trond Myklebust4ce79712005-06-22 17:16:21 +000054#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050056#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050057#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040058#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040059#include "pnfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61#define NFSDBG_FACILITY NFSDBG_PROC
62
Trond Myklebust2066fe82006-09-15 08:30:46 -040063#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#define NFS4_POLL_RETRY_MAX (15*HZ)
65
Trond Myklebusta78cb572009-08-09 15:06:19 -040066#define NFS4_MAX_LOOP_ON_RECOVER (10)
67
Trond Myklebustcdd4e682006-01-03 09:55:12 +010068struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010069static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080070static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050072static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Trond Myklebust99367812007-07-17 21:52:41 -040073static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
74static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040075static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
76 struct nfs_fattr *fattr, struct iattr *sattr,
77 struct nfs4_state *state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050080static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Trond Myklebust52567b02009-10-23 14:46:42 -040082 if (err >= -1000)
83 return err;
84 switch (err) {
85 case -NFS4ERR_RESOURCE:
86 return -EREMOTEIO;
87 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -070089 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -040090 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 }
Trond Myklebust52567b02009-10-23 14:46:42 -040092 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
95/*
96 * This is our standard bitmap for GETATTR requests.
97 */
98const u32 nfs4_fattr_bitmap[2] = {
99 FATTR4_WORD0_TYPE
100 | FATTR4_WORD0_CHANGE
101 | FATTR4_WORD0_SIZE
102 | FATTR4_WORD0_FSID
103 | FATTR4_WORD0_FILEID,
104 FATTR4_WORD1_MODE
105 | FATTR4_WORD1_NUMLINKS
106 | FATTR4_WORD1_OWNER
107 | FATTR4_WORD1_OWNER_GROUP
108 | FATTR4_WORD1_RAWDEV
109 | FATTR4_WORD1_SPACE_USED
110 | FATTR4_WORD1_TIME_ACCESS
111 | FATTR4_WORD1_TIME_METADATA
112 | FATTR4_WORD1_TIME_MODIFY
113};
114
115const u32 nfs4_statfs_bitmap[2] = {
116 FATTR4_WORD0_FILES_AVAIL
117 | FATTR4_WORD0_FILES_FREE
118 | FATTR4_WORD0_FILES_TOTAL,
119 FATTR4_WORD1_SPACE_AVAIL
120 | FATTR4_WORD1_SPACE_FREE
121 | FATTR4_WORD1_SPACE_TOTAL
122};
123
Trond Myklebust4ce79712005-06-22 17:16:21 +0000124const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 FATTR4_WORD0_MAXLINK
126 | FATTR4_WORD0_MAXNAME,
127 0
128};
129
130const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
131 | FATTR4_WORD0_MAXREAD
132 | FATTR4_WORD0_MAXWRITE
133 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700134 FATTR4_WORD1_TIME_DELTA
Andy Adamson504913f2010-10-20 00:17:57 -0400135 | FATTR4_WORD1_FS_LAYOUT_TYPES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136};
137
Manoj Naik830b8e32006-06-09 09:34:25 -0400138const u32 nfs4_fs_locations_bitmap[2] = {
139 FATTR4_WORD0_TYPE
140 | FATTR4_WORD0_CHANGE
141 | FATTR4_WORD0_SIZE
142 | FATTR4_WORD0_FSID
143 | FATTR4_WORD0_FILEID
144 | FATTR4_WORD0_FS_LOCATIONS,
145 FATTR4_WORD1_MODE
146 | FATTR4_WORD1_NUMLINKS
147 | FATTR4_WORD1_OWNER
148 | FATTR4_WORD1_OWNER_GROUP
149 | FATTR4_WORD1_RAWDEV
150 | FATTR4_WORD1_SPACE_USED
151 | FATTR4_WORD1_TIME_ACCESS
152 | FATTR4_WORD1_TIME_METADATA
153 | FATTR4_WORD1_TIME_MODIFY
154 | FATTR4_WORD1_MOUNTED_ON_FILEID
155};
156
Al Virobc4785c2006-10-19 23:28:51 -0700157static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 struct nfs4_readdir_arg *readdir)
159{
Al Viro0dbb4c62006-10-19 23:28:49 -0700160 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162 BUG_ON(readdir->count < 80);
163 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000164 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
166 return;
167 }
168
169 readdir->cookie = 0;
170 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
171 if (cookie == 2)
172 return;
173
174 /*
175 * NFSv4 servers do not return entries for '.' and '..'
176 * Therefore, we fake these entries here. We let '.'
177 * have cookie 0 and '..' have cookie 1. Note that
178 * when talking to the server, we always send cookie 0
179 * instead of 1 or 2.
180 */
Al Viro0dbb4c62006-10-19 23:28:49 -0700181 start = p = kmap_atomic(*readdir->pages, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 if (cookie == 0) {
184 *p++ = xdr_one; /* next */
185 *p++ = xdr_zero; /* cookie, first word */
186 *p++ = xdr_one; /* cookie, second word */
187 *p++ = xdr_one; /* entry len */
188 memcpy(p, ".\0\0\0", 4); /* entry */
189 p++;
190 *p++ = xdr_one; /* bitmap length */
191 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
192 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400193 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 }
195
196 *p++ = xdr_one; /* next */
197 *p++ = xdr_zero; /* cookie, first word */
198 *p++ = xdr_two; /* cookie, second word */
199 *p++ = xdr_two; /* entry len */
200 memcpy(p, "..\0\0", 4); /* entry */
201 p++;
202 *p++ = xdr_one; /* bitmap length */
203 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
204 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400205 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 readdir->pgbase = (char *)p - (char *)start;
208 readdir->count -= readdir->pgbase;
209 kunmap_atomic(start, KM_USER0);
210}
211
Trond Myklebust65de8722008-12-23 15:21:44 -0500212static int nfs4_wait_clnt_recover(struct nfs_client *clp)
213{
214 int res;
215
216 might_sleep();
217
Trond Myklebuste005e802008-12-23 15:21:48 -0500218 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400219 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500220 return res;
221}
222
223static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
224{
225 int res = 0;
226
227 might_sleep();
228
229 if (*timeout <= 0)
230 *timeout = NFS4_POLL_RETRY_MIN;
231 if (*timeout > NFS4_POLL_RETRY_MAX)
232 *timeout = NFS4_POLL_RETRY_MAX;
233 schedule_timeout_killable(*timeout);
234 if (fatal_signal_pending(current))
235 res = -ERESTARTSYS;
236 *timeout <<= 1;
237 return res;
238}
239
240/* This is the error handling routine for processes that are allowed
241 * to sleep.
242 */
243static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
244{
245 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500246 struct nfs4_state *state = exception->state;
Trond Myklebust65de8722008-12-23 15:21:44 -0500247 int ret = errorcode;
248
249 exception->retry = 0;
250 switch(errorcode) {
251 case 0:
252 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500253 case -NFS4ERR_ADMIN_REVOKED:
254 case -NFS4ERR_BAD_STATEID:
255 case -NFS4ERR_OPENMODE:
256 if (state == NULL)
257 break;
258 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500259 goto do_state_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500260 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500261 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust65de8722008-12-23 15:21:44 -0500262 case -NFS4ERR_EXPIRED:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500263 goto do_state_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500264#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400265 case -NFS4ERR_BADSESSION:
266 case -NFS4ERR_BADSLOT:
267 case -NFS4ERR_BAD_HIGH_SLOT:
268 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
269 case -NFS4ERR_DEADSESSION:
270 case -NFS4ERR_SEQ_FALSE_RETRY:
271 case -NFS4ERR_SEQ_MISORDERED:
272 dprintk("%s ERROR: %d Reset session\n", __func__,
273 errorcode);
Andy Adamson0b9e2d42009-12-04 16:02:14 -0500274 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -0400275 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500276 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500277#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500278 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500279 if (exception->timeout > HZ) {
280 /* We have retried a decent amount, time to
281 * fail
282 */
283 ret = -EBUSY;
284 break;
285 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500286 case -NFS4ERR_GRACE:
287 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500288 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500289 ret = nfs4_delay(server->client, &exception->timeout);
290 if (ret != 0)
291 break;
292 case -NFS4ERR_OLD_STATEID:
293 exception->retry = 1;
294 }
295 /* We failed to handle the error */
296 return nfs4_map_errors(ret);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500297do_state_recovery:
298 nfs4_schedule_state_recovery(clp);
299 ret = nfs4_wait_clnt_recover(clp);
300 if (ret == 0)
301 exception->retry = 1;
302 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500303}
304
305
Trond Myklebust452e9352010-07-31 14:29:06 -0400306static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 spin_lock(&clp->cl_lock);
309 if (time_before(clp->cl_last_renewal,timestamp))
310 clp->cl_last_renewal = timestamp;
311 spin_unlock(&clp->cl_lock);
312}
313
Trond Myklebust452e9352010-07-31 14:29:06 -0400314static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
315{
316 do_renew_lease(server->nfs_client, timestamp);
317}
318
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400319#if defined(CONFIG_NFS_V4_1)
320
Benny Halevy510b8172009-04-01 09:22:14 -0400321/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400322 * nfs4_free_slot - free a slot and efficiently update slot table.
323 *
324 * freeing a slot is trivially done by clearing its respective bit
325 * in the bitmap.
326 * If the freed slotid equals highest_used_slotid we want to update it
327 * so that the server would be able to size down the slot table if needed,
328 * otherwise we know that the highest_used_slotid is still in use.
329 * When updating highest_used_slotid there may be "holes" in the bitmap
330 * so we need to scan down from highest_used_slotid to 0 looking for the now
331 * highest slotid in use.
332 * If none found, highest_used_slotid is set to -1.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500333 *
334 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400335 */
336static void
Benny Halevydfb4f3092010-09-24 09:17:01 -0400337nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *free_slot)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400338{
Benny Halevydfb4f3092010-09-24 09:17:01 -0400339 int free_slotid = free_slot - tbl->slots;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400340 int slotid = free_slotid;
341
Benny Halevydfb4f3092010-09-24 09:17:01 -0400342 BUG_ON(slotid < 0 || slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400343 /* clear used bit in bitmap */
344 __clear_bit(slotid, tbl->used_slots);
345
346 /* update highest_used_slotid when it is freed */
347 if (slotid == tbl->highest_used_slotid) {
348 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500349 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400350 tbl->highest_used_slotid = slotid;
351 else
352 tbl->highest_used_slotid = -1;
353 }
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400354 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
355 free_slotid, tbl->highest_used_slotid);
356}
357
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800358/*
359 * Signal state manager thread if session is drained
360 */
361static void nfs41_check_drain_session_complete(struct nfs4_session *ses)
362{
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800363 struct rpc_task *task;
364
Trond Myklebusta2118c32010-06-16 09:52:26 -0400365 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800366 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
367 if (task)
368 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800369 return;
370 }
371
372 if (ses->fc_slot_table.highest_used_slotid != -1)
373 return;
374
375 dprintk("%s COMPLETE: Session Drained\n", __func__);
376 complete(&ses->complete);
377}
378
Trond Myklebustd185a332010-06-16 09:52:25 -0400379static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400380{
381 struct nfs4_slot_table *tbl;
382
Trond Myklebustd185a332010-06-16 09:52:25 -0400383 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400384 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400385 /* just wake up the next guy waiting since
386 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500387 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500388 return;
Andy Adamson13615872009-04-01 09:22:17 -0400389 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500390
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500391 spin_lock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400392 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebustd185a332010-06-16 09:52:25 -0400393 nfs41_check_drain_session_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500394 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400395 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400396}
397
Trond Myklebust14516c32010-07-31 14:29:06 -0400398static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400399{
400 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400401 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400402
403 /*
404 * sr_status remains 1 if an RPC level error occurred. The server
405 * may or may not have processed the sequence operation..
406 * Proceed as if the server received and processed the sequence
407 * operation.
408 */
409 if (res->sr_status == 1)
410 res->sr_status = NFS_OK;
411
412 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400413 if (!res->sr_slot)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400414 goto out;
415
Andy Adamson691daf32009-12-04 15:55:39 -0500416 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400417 switch (res->sr_status) {
418 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400419 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400420 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400421 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400422 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400423 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500424 /* Check sequence flags */
Alexandros Batsakis71358402010-02-05 03:45:05 -0800425 if (atomic_read(&clp->cl_count) > 1)
426 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust14516c32010-07-31 14:29:06 -0400427 break;
428 case -NFS4ERR_DELAY:
429 /* The server detected a resend of the RPC call and
430 * returned NFS4ERR_DELAY as per Section 2.10.6.2
431 * of RFC5661.
432 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200433 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400434 __func__,
435 res->sr_slot - res->sr_session->fc_slot_table.slots,
436 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400437 goto out_retry;
438 default:
439 /* Just update the slot sequence no. */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400440 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400441 }
442out:
443 /* The session may be reset by one of the error handlers. */
444 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400445 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400446 return 1;
447out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400448 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400449 goto out;
450 rpc_delay(task, NFS4_POLL_RETRY_MAX);
451 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400452}
453
Trond Myklebust14516c32010-07-31 14:29:06 -0400454static int nfs4_sequence_done(struct rpc_task *task,
455 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400456{
Trond Myklebust14516c32010-07-31 14:29:06 -0400457 if (res->sr_session == NULL)
458 return 1;
459 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400460}
461
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400462/*
Benny Halevy510b8172009-04-01 09:22:14 -0400463 * nfs4_find_slot - efficiently look for a free slot
464 *
465 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
466 * If found, we mark the slot as used, update the highest_used_slotid,
467 * and respectively set up the sequence operation args.
468 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400469 *
470 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400471 */
472static u8
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800473nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400474{
475 int slotid;
476 u8 ret_id = NFS4_MAX_SLOT_TABLE;
477 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
478
Benny Halevy510b8172009-04-01 09:22:14 -0400479 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
480 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
481 tbl->max_slots);
482 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
483 if (slotid >= tbl->max_slots)
484 goto out;
485 __set_bit(slotid, tbl->used_slots);
486 if (slotid > tbl->highest_used_slotid)
487 tbl->highest_used_slotid = slotid;
488 ret_id = slotid;
489out:
490 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
491 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400492 return ret_id;
493}
494
Andy Adamsonce5039c2009-04-01 09:22:13 -0400495static int nfs41_setup_sequence(struct nfs4_session *session,
496 struct nfs4_sequence_args *args,
497 struct nfs4_sequence_res *res,
498 int cache_reply,
499 struct rpc_task *task)
500{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400501 struct nfs4_slot *slot;
502 struct nfs4_slot_table *tbl;
503 u8 slotid;
504
505 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400506 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400507 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400508 return 0;
509
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400510 tbl = &session->fc_slot_table;
511
512 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400513 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800514 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamsonea028ac2009-12-04 15:55:38 -0500515 /*
516 * The state manager will wait until the slot table is empty.
517 * Schedule the reset thread
518 */
Andy Adamson05f0d232009-12-04 15:55:37 -0500519 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400520 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500521 dprintk("%s Schedule Session Reset\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500522 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400523 }
524
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800525 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
526 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
527 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
528 spin_unlock(&tbl->slot_tbl_lock);
529 dprintk("%s enforce FIFO order\n", __func__);
530 return -EAGAIN;
531 }
532
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800533 slotid = nfs4_find_slot(tbl);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400534 if (slotid == NFS4_MAX_SLOT_TABLE) {
535 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
536 spin_unlock(&tbl->slot_tbl_lock);
537 dprintk("<-- %s: no free slots\n", __func__);
538 return -EAGAIN;
539 }
540 spin_unlock(&tbl->slot_tbl_lock);
541
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800542 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400543 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400544 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400545 args->sa_slotid = slotid;
546 args->sa_cache_this = cache_reply;
547
548 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
549
Benny Halevy99fe60d2009-04-01 09:22:29 -0400550 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400551 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400552 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400553 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400554 /*
555 * sr_status is only set in decode_sequence, and so will remain
556 * set to 1 if an rpc level failure occurs.
557 */
558 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400559 return 0;
560}
561
Trond Myklebust035168ab2010-06-16 09:52:26 -0400562int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400563 struct nfs4_sequence_args *args,
564 struct nfs4_sequence_res *res,
565 int cache_reply,
566 struct rpc_task *task)
567{
Trond Myklebust035168ab2010-06-16 09:52:26 -0400568 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400569 int ret = 0;
570
Trond Myklebust035168ab2010-06-16 09:52:26 -0400571 if (session == NULL) {
572 args->sa_session = NULL;
573 res->sr_session = NULL;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400574 goto out;
Trond Myklebust035168ab2010-06-16 09:52:26 -0400575 }
576
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200577 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400578 __func__, session->clp, session, res->sr_slot ?
579 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168ab2010-06-16 09:52:26 -0400580
581 ret = nfs41_setup_sequence(session, args, res, cache_reply,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400582 task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400583out:
584 dprintk("<-- %s status=%d\n", __func__, ret);
585 return ret;
586}
587
588struct nfs41_call_sync_data {
Trond Myklebust035168ab2010-06-16 09:52:26 -0400589 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400590 struct nfs4_sequence_args *seq_args;
591 struct nfs4_sequence_res *seq_res;
592 int cache_reply;
593};
594
595static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
596{
597 struct nfs41_call_sync_data *data = calldata;
598
Trond Myklebust035168ab2010-06-16 09:52:26 -0400599 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
600
601 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400602 data->seq_res, data->cache_reply, task))
603 return;
604 rpc_call_start(task);
605}
606
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800607static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
608{
609 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
610 nfs41_call_sync_prepare(task, calldata);
611}
612
Andy Adamson69ab40c2009-04-01 09:22:19 -0400613static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
614{
615 struct nfs41_call_sync_data *data = calldata;
616
Trond Myklebust14516c32010-07-31 14:29:06 -0400617 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400618}
619
Andy Adamsonce5039c2009-04-01 09:22:13 -0400620struct rpc_call_ops nfs41_call_sync_ops = {
621 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400622 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400623};
624
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800625struct rpc_call_ops nfs41_call_priv_sync_ops = {
626 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
627 .rpc_call_done = nfs41_call_sync_done,
628};
629
Trond Myklebust035168ab2010-06-16 09:52:26 -0400630static int nfs4_call_sync_sequence(struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400631 struct rpc_message *msg,
632 struct nfs4_sequence_args *args,
633 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800634 int cache_reply,
635 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400636{
637 int ret;
638 struct rpc_task *task;
639 struct nfs41_call_sync_data data = {
Trond Myklebust035168ab2010-06-16 09:52:26 -0400640 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400641 .seq_args = args,
642 .seq_res = res,
643 .cache_reply = cache_reply,
644 };
645 struct rpc_task_setup task_setup = {
Trond Myklebust035168ab2010-06-16 09:52:26 -0400646 .rpc_client = server->client,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400647 .rpc_message = msg,
648 .callback_ops = &nfs41_call_sync_ops,
649 .callback_data = &data
650 };
651
Benny Halevydfb4f3092010-09-24 09:17:01 -0400652 res->sr_slot = NULL;
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800653 if (privileged)
654 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400655 task = rpc_run_task(&task_setup);
656 if (IS_ERR(task))
657 ret = PTR_ERR(task);
658 else {
659 ret = task->tk_status;
660 rpc_put_task(task);
661 }
662 return ret;
663}
664
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400665int _nfs4_call_sync_session(struct nfs_server *server,
666 struct rpc_message *msg,
667 struct nfs4_sequence_args *args,
668 struct nfs4_sequence_res *res,
669 int cache_reply)
670{
Trond Myklebust035168ab2010-06-16 09:52:26 -0400671 return nfs4_call_sync_sequence(server, msg, args, res, cache_reply, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400672}
673
Trond Myklebustdf896452010-06-16 09:52:26 -0400674#else
Trond Myklebust14516c32010-07-31 14:29:06 -0400675static int nfs4_sequence_done(struct rpc_task *task,
676 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400677{
Trond Myklebust14516c32010-07-31 14:29:06 -0400678 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400679}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400680#endif /* CONFIG_NFS_V4_1 */
681
682int _nfs4_call_sync(struct nfs_server *server,
683 struct rpc_message *msg,
684 struct nfs4_sequence_args *args,
685 struct nfs4_sequence_res *res,
686 int cache_reply)
687{
Benny Halevyf3752972009-04-01 09:22:04 -0400688 args->sa_session = res->sr_session = NULL;
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400689 return rpc_call_sync(server->client, msg, 0);
690}
691
692#define nfs4_call_sync(server, msg, args, res, cache_reply) \
Trond Myklebust97dc1352010-06-16 09:52:26 -0400693 (server)->nfs_client->cl_mvops->call_sync((server), (msg), &(args)->seq_args, \
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400694 &(res)->seq_res, (cache_reply))
695
Trond Myklebust38478b22006-05-25 01:40:57 -0400696static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
Trond Myklebust38478b22006-05-25 01:40:57 -0400698 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Trond Myklebust38478b22006-05-25 01:40:57 -0400700 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400701 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
702 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400703 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400704 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400705 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100708struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400709 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100710 struct nfs_openargs o_arg;
711 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100712 struct nfs_open_confirmargs c_arg;
713 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100714 struct nfs_fattr f_attr;
715 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400716 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100717 struct dentry *dir;
718 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400719 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100720 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100721 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400722 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100723 int rpc_status;
724 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100725};
726
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400727
728static void nfs4_init_opendata_res(struct nfs4_opendata *p)
729{
730 p->o_res.f_attr = &p->f_attr;
731 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400732 p->o_res.seqid = p->o_arg.seqid;
733 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400734 p->o_res.server = p->o_arg.server;
735 nfs_fattr_init(&p->f_attr);
736 nfs_fattr_init(&p->dir_attr);
737}
738
Trond Myklebustad389da2007-06-05 12:30:00 -0400739static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500740 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400741 const struct iattr *attrs,
742 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100743{
Trond Myklebustad389da2007-06-05 12:30:00 -0400744 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100745 struct inode *dir = parent->d_inode;
746 struct nfs_server *server = NFS_SERVER(dir);
747 struct nfs4_opendata *p;
748
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400749 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100750 if (p == NULL)
751 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400752 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100753 if (p->o_arg.seqid == NULL)
754 goto err_free;
Al Virof6948692010-01-30 13:51:04 -0500755 path_get(path);
756 p->path = *path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100757 p->dir = parent;
758 p->owner = sp;
759 atomic_inc(&sp->so_count);
760 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500761 p->o_arg.open_flags = flags;
762 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400763 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400764 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400765 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100766 p->o_arg.server = server;
767 p->o_arg.bitmask = server->attr_bitmask;
768 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400769 if (flags & O_CREAT) {
770 u32 *s;
771
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100772 p->o_arg.u.attrs = &p->attrs;
773 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400774 s = (u32 *) p->o_arg.u.verifier.data;
775 s[0] = jiffies;
776 s[1] = current->pid;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100777 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100778 p->c_arg.fh = &p->o_res.fh;
779 p->c_arg.stateid = &p->o_res.stateid;
780 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400781 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400782 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100783 return p;
784err_free:
785 kfree(p);
786err:
787 dput(parent);
788 return NULL;
789}
790
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400791static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100792{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400793 struct nfs4_opendata *p = container_of(kref,
794 struct nfs4_opendata, kref);
795
796 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400797 if (p->state != NULL)
798 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400799 nfs4_put_state_owner(p->owner);
800 dput(p->dir);
Jan Blunck31f31db12008-05-02 13:42:45 -0700801 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400802 kfree(p);
803}
804
805static void nfs4_opendata_put(struct nfs4_opendata *p)
806{
807 if (p != NULL)
808 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100809}
810
Trond Myklebust06f814a2006-01-03 09:55:07 +0100811static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
812{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100813 int ret;
814
Trond Myklebust06f814a2006-01-03 09:55:07 +0100815 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100816 return ret;
817}
818
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500819static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400820{
821 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500822
823 if (open_mode & O_EXCL)
824 goto out;
825 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400826 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500827 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
828 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400829 break;
830 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500831 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
832 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400833 break;
834 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500835 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
836 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400837 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500838out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400839 return ret;
840}
841
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500842static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400843{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500844 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400845 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500846 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400847 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500848 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400849 return 1;
850}
851
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500852static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100853{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500854 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100855 case FMODE_WRITE:
856 state->n_wronly++;
857 break;
858 case FMODE_READ:
859 state->n_rdonly++;
860 break;
861 case FMODE_READ|FMODE_WRITE:
862 state->n_rdwr++;
863 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500864 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100865}
866
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500867static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400868{
869 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
870 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
871 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500872 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400873 case FMODE_READ:
874 set_bit(NFS_O_RDONLY_STATE, &state->flags);
875 break;
876 case FMODE_WRITE:
877 set_bit(NFS_O_WRONLY_STATE, &state->flags);
878 break;
879 case FMODE_READ|FMODE_WRITE:
880 set_bit(NFS_O_RDWR_STATE, &state->flags);
881 }
882}
883
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500884static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400885{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400886 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500887 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400888 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400889}
890
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500891static 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 -0700892{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400893 /*
894 * Protect the call to nfs4_state_set_mode_locked and
895 * serialise the stateid update
896 */
897 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400898 if (deleg_stateid != NULL) {
899 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
900 set_bit(NFS_DELEGATED_STATE, &state->flags);
901 }
902 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500903 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400904 write_sequnlock(&state->seqlock);
905 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500906 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700907 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500910static 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 -0500911{
912 struct nfs_inode *nfsi = NFS_I(state->inode);
913 struct nfs_delegation *deleg_cur;
914 int ret = 0;
915
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500916 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500917
918 rcu_read_lock();
919 deleg_cur = rcu_dereference(nfsi->delegation);
920 if (deleg_cur == NULL)
921 goto no_delegation;
922
923 spin_lock(&deleg_cur->lock);
924 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500925 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500926 goto no_delegation_unlock;
927
928 if (delegation == NULL)
929 delegation = &deleg_cur->stateid;
930 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
931 goto no_delegation_unlock;
932
Trond Myklebustb7391f42008-12-23 15:21:52 -0500933 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500934 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500935 ret = 1;
936no_delegation_unlock:
937 spin_unlock(&deleg_cur->lock);
938no_delegation:
939 rcu_read_unlock();
940
941 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500942 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500943 ret = 1;
944 }
945
946 return ret;
947}
948
949
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500950static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400951{
952 struct nfs_delegation *delegation;
953
954 rcu_read_lock();
955 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500956 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400957 rcu_read_unlock();
958 return;
959 }
960 rcu_read_unlock();
961 nfs_inode_return_delegation(inode);
962}
963
Trond Myklebust6ee41262007-07-08 14:11:36 -0400964static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400965{
966 struct nfs4_state *state = opendata->state;
967 struct nfs_inode *nfsi = NFS_I(state->inode);
968 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500969 int open_mode = opendata->o_arg.open_flags & O_EXCL;
970 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400971 nfs4_stateid stateid;
972 int ret = -EAGAIN;
973
Trond Myklebustaac00a82007-07-05 19:02:21 -0400974 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500975 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400976 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500977 if (can_open_cached(state, fmode, open_mode)) {
978 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400979 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400980 goto out_return_state;
981 }
982 spin_unlock(&state->owner->so_lock);
983 }
Trond Myklebust34310432008-12-23 15:21:38 -0500984 rcu_read_lock();
985 delegation = rcu_dereference(nfsi->delegation);
986 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500987 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -0500988 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -0400989 break;
Trond Myklebust34310432008-12-23 15:21:38 -0500990 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400991 /* Save the delegation */
992 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
993 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -0400994 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400995 if (ret != 0)
996 goto out;
997 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -0500998
999 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001000 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001001 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001002 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001003out:
1004 return ERR_PTR(ret);
1005out_return_state:
1006 atomic_inc(&state->count);
1007 return state;
1008}
1009
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001010static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1011{
1012 struct inode *inode;
1013 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001014 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001015 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001016
Trond Myklebustaac00a82007-07-05 19:02:21 -04001017 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001018 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001019 goto out;
1020 }
1021
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001022 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001023 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001024 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001025 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001026 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001027 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001028 goto err;
1029 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001030 state = nfs4_get_open_state(inode, data->owner);
1031 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001032 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001033 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001034 int delegation_flags = 0;
1035
Trond Myklebust003707c2007-07-05 18:07:55 -04001036 rcu_read_lock();
1037 delegation = rcu_dereference(NFS_I(inode)->delegation);
1038 if (delegation)
1039 delegation_flags = delegation->flags;
1040 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -05001041 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001042 nfs_inode_set_delegation(state->inode,
1043 data->owner->so_cred,
1044 &data->o_res);
1045 else
1046 nfs_inode_reclaim_delegation(state->inode,
1047 data->owner->so_cred,
1048 &data->o_res);
1049 }
Trond Myklebust34310432008-12-23 15:21:38 -05001050
1051 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001052 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001053 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001054out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001055 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001056err_put_inode:
1057 iput(inode);
1058err:
1059 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001060}
1061
Trond Myklebust864472e2006-01-03 09:55:15 +01001062static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1063{
1064 struct nfs_inode *nfsi = NFS_I(state->inode);
1065 struct nfs_open_context *ctx;
1066
1067 spin_lock(&state->inode->i_lock);
1068 list_for_each_entry(ctx, &nfsi->open_files, list) {
1069 if (ctx->state != state)
1070 continue;
1071 get_nfs_open_context(ctx);
1072 spin_unlock(&state->inode->i_lock);
1073 return ctx;
1074 }
1075 spin_unlock(&state->inode->i_lock);
1076 return ERR_PTR(-ENOENT);
1077}
1078
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001079static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1080{
1081 struct nfs4_opendata *opendata;
1082
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001083 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001084 if (opendata == NULL)
1085 return ERR_PTR(-ENOMEM);
1086 opendata->state = state;
1087 atomic_inc(&state->count);
1088 return opendata;
1089}
1090
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001091static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001092{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001093 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001094 int ret;
1095
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001096 opendata->o_arg.open_flags = 0;
1097 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001098 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1099 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1100 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001101 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001102 if (ret != 0)
1103 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001104 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001105 if (IS_ERR(newstate))
1106 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001107 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001108 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001109 return 0;
1110}
1111
1112static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1113{
Trond Myklebust864472e2006-01-03 09:55:15 +01001114 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001115 int ret;
1116
1117 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001118 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001119 smp_rmb();
1120 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001121 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001122 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001123 if (ret != 0)
1124 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001125 if (newstate != state)
1126 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001127 }
1128 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001129 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001130 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001131 if (ret != 0)
1132 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001133 if (newstate != state)
1134 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001135 }
1136 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001137 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001138 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001139 if (ret != 0)
1140 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001141 if (newstate != state)
1142 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001143 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001144 /*
1145 * We may have performed cached opens for all three recoveries.
1146 * Check if we need to update the current stateid.
1147 */
1148 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1149 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001150 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001151 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1152 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001153 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001154 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001155 return 0;
1156}
1157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158/*
1159 * OPEN_RECLAIM:
1160 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001162static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001164 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001165 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001166 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 int status;
1168
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001169 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1170 if (IS_ERR(opendata))
1171 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001172 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1173 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001174 rcu_read_lock();
1175 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001176 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001177 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001178 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001179 opendata->o_arg.u.delegation_type = delegation_type;
1180 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001181 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 return status;
1183}
1184
Trond Myklebust539cd032007-06-05 11:46:42 -04001185static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
1187 struct nfs_server *server = NFS_SERVER(state->inode);
1188 struct nfs4_exception exception = { };
1189 int err;
1190 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001191 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001192 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001193 break;
1194 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 } while (exception.retry);
1196 return err;
1197}
1198
Trond Myklebust864472e2006-01-03 09:55:15 +01001199static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1200{
1201 struct nfs_open_context *ctx;
1202 int ret;
1203
1204 ctx = nfs4_state_find_open_context(state);
1205 if (IS_ERR(ctx))
1206 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001207 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001208 put_nfs_open_context(ctx);
1209 return ret;
1210}
1211
Trond Myklebust13437e12007-07-06 15:10:43 -04001212static 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 -07001213{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001214 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001215 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001217 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1218 if (IS_ERR(opendata))
1219 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001220 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001221 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001222 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001223 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001224 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001225 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226}
1227
Trond Myklebust13437e12007-07-06 15:10:43 -04001228int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
1230 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001231 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 int err;
1233 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001234 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 switch (err) {
1236 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001237 case -ENOENT:
1238 case -ESTALE:
1239 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001240 case -NFS4ERR_BADSESSION:
1241 case -NFS4ERR_BADSLOT:
1242 case -NFS4ERR_BAD_HIGH_SLOT:
1243 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1244 case -NFS4ERR_DEADSESSION:
1245 nfs4_schedule_state_recovery(
1246 server->nfs_client);
1247 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 case -NFS4ERR_STALE_CLIENTID:
1249 case -NFS4ERR_STALE_STATEID:
1250 case -NFS4ERR_EXPIRED:
1251 /* Don't recall a delegation if it was lost */
David Howells7539bba2006-08-22 20:06:09 -04001252 nfs4_schedule_state_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001253 goto out;
1254 case -ERESTARTSYS:
1255 /*
1256 * The show must go on: exit, but mark the
1257 * stateid as needing recovery.
1258 */
1259 case -NFS4ERR_ADMIN_REVOKED:
1260 case -NFS4ERR_BAD_STATEID:
1261 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001262 case -EKEYEXPIRED:
1263 /*
1264 * User RPCSEC_GSS context has expired.
1265 * We cannot recover this stateid now, so
1266 * skip it and allow recovery thread to
1267 * proceed.
1268 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001269 case -ENOMEM:
1270 err = 0;
1271 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 }
1273 err = nfs4_handle_exception(server, err, &exception);
1274 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001275out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 return err;
1277}
1278
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001279static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1280{
1281 struct nfs4_opendata *data = calldata;
1282
1283 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001284 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001285 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1286 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001287 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001288 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001289 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001290 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001291}
1292
1293static void nfs4_open_confirm_release(void *calldata)
1294{
1295 struct nfs4_opendata *data = calldata;
1296 struct nfs4_state *state = NULL;
1297
1298 /* If this request hasn't been cancelled, do nothing */
1299 if (data->cancelled == 0)
1300 goto out_free;
1301 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001302 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001303 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001304 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001305 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001306 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001307out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001308 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001309}
1310
1311static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001312 .rpc_call_done = nfs4_open_confirm_done,
1313 .rpc_release = nfs4_open_confirm_release,
1314};
1315
1316/*
1317 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1318 */
1319static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1320{
1321 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1322 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001323 struct rpc_message msg = {
1324 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1325 .rpc_argp = &data->c_arg,
1326 .rpc_resp = &data->c_res,
1327 .rpc_cred = data->owner->so_cred,
1328 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001329 struct rpc_task_setup task_setup_data = {
1330 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001331 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001332 .callback_ops = &nfs4_open_confirm_ops,
1333 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001334 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001335 .flags = RPC_TASK_ASYNC,
1336 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 int status;
1338
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001339 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001340 data->rpc_done = 0;
1341 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001342 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001343 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001344 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001345 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001346 status = nfs4_wait_for_completion_rpc_task(task);
1347 if (status != 0) {
1348 data->cancelled = 1;
1349 smp_wmb();
1350 } else
1351 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001352 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 return status;
1354}
1355
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001356static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001358 struct nfs4_opendata *data = calldata;
1359 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001360
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001361 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1362 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001363 /*
1364 * Check if we still need to send an OPEN call, or if we can use
1365 * a delegation instead.
1366 */
1367 if (data->state != NULL) {
1368 struct nfs_delegation *delegation;
1369
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001370 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001371 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001372 rcu_read_lock();
1373 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1374 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001375 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001376 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001377 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001378 }
1379 rcu_read_unlock();
1380 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001381 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001382 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001383 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001384 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001385 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001386 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1387 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001388 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04001389 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001390 &data->o_arg.seq_args,
1391 &data->o_res.seq_res, 1, task))
1392 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001393 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001394 return;
1395out_no_action:
1396 task->tk_action = NULL;
1397
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001398}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001400static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1401{
1402 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1403 nfs4_open_prepare(task, calldata);
1404}
1405
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001406static void nfs4_open_done(struct rpc_task *task, void *calldata)
1407{
1408 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001410 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001411
Trond Myklebust14516c32010-07-31 14:29:06 -04001412 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1413 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001414
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001415 if (task->tk_status == 0) {
1416 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001417 case S_IFREG:
1418 break;
1419 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001420 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001421 break;
1422 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001423 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001424 break;
1425 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001426 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001427 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001428 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001429 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1430 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001431 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001432 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001433}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001434
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001435static void nfs4_open_release(void *calldata)
1436{
1437 struct nfs4_opendata *data = calldata;
1438 struct nfs4_state *state = NULL;
1439
1440 /* If this request hasn't been cancelled, do nothing */
1441 if (data->cancelled == 0)
1442 goto out_free;
1443 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001444 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001445 goto out_free;
1446 /* In case we need an open_confirm, no cleanup! */
1447 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1448 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001449 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001450 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001451 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001452out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001453 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001454}
1455
1456static const struct rpc_call_ops nfs4_open_ops = {
1457 .rpc_call_prepare = nfs4_open_prepare,
1458 .rpc_call_done = nfs4_open_done,
1459 .rpc_release = nfs4_open_release,
1460};
1461
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001462static const struct rpc_call_ops nfs4_recover_open_ops = {
1463 .rpc_call_prepare = nfs4_recover_open_prepare,
1464 .rpc_call_done = nfs4_open_done,
1465 .rpc_release = nfs4_open_release,
1466};
1467
1468static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001469{
1470 struct inode *dir = data->dir->d_inode;
1471 struct nfs_server *server = NFS_SERVER(dir);
1472 struct nfs_openargs *o_arg = &data->o_arg;
1473 struct nfs_openres *o_res = &data->o_res;
1474 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001475 struct rpc_message msg = {
1476 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1477 .rpc_argp = o_arg,
1478 .rpc_resp = o_res,
1479 .rpc_cred = data->owner->so_cred,
1480 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001481 struct rpc_task_setup task_setup_data = {
1482 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001483 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001484 .callback_ops = &nfs4_open_ops,
1485 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001486 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001487 .flags = RPC_TASK_ASYNC,
1488 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001489 int status;
1490
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001491 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001492 data->rpc_done = 0;
1493 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001494 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001495 if (isrecover)
1496 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001497 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001498 if (IS_ERR(task))
1499 return PTR_ERR(task);
1500 status = nfs4_wait_for_completion_rpc_task(task);
1501 if (status != 0) {
1502 data->cancelled = 1;
1503 smp_wmb();
1504 } else
1505 status = data->rpc_status;
1506 rpc_put_task(task);
1507
1508 return status;
1509}
1510
1511static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1512{
1513 struct inode *dir = data->dir->d_inode;
1514 struct nfs_openres *o_res = &data->o_res;
1515 int status;
1516
1517 status = nfs4_run_open_task(data, 1);
1518 if (status != 0 || !data->rpc_done)
1519 return status;
1520
1521 nfs_refresh_inode(dir, o_res->dir_attr);
1522
1523 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1524 status = _nfs4_proc_open_confirm(data);
1525 if (status != 0)
1526 return status;
1527 }
1528
1529 return status;
1530}
1531
1532/*
1533 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1534 */
1535static int _nfs4_proc_open(struct nfs4_opendata *data)
1536{
1537 struct inode *dir = data->dir->d_inode;
1538 struct nfs_server *server = NFS_SERVER(dir);
1539 struct nfs_openargs *o_arg = &data->o_arg;
1540 struct nfs_openres *o_res = &data->o_res;
1541 int status;
1542
1543 status = nfs4_run_open_task(data, 0);
Trond Myklebust3e309912007-07-07 13:19:59 -04001544 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001545 return status;
1546
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001547 if (o_arg->open_flags & O_CREAT) {
1548 update_changeattr(dir, &o_res->cinfo);
1549 nfs_post_op_update_inode(dir, o_res->dir_attr);
1550 } else
1551 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001552 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1553 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001555 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001557 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 }
1559 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001560 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001561 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562}
1563
Trond Myklebust10afec92007-05-14 17:16:04 -04001564static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +01001565{
David Howells7539bba2006-08-22 20:06:09 -04001566 struct nfs_client *clp = server->nfs_client;
Trond Myklebusta78cb572009-08-09 15:06:19 -04001567 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001568 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001569
Trond Myklebusta78cb572009-08-09 15:06:19 -04001570 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001571 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001572 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001573 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001574 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1575 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001576 break;
Trond Myklebust58d97142006-01-03 09:55:24 +01001577 nfs4_schedule_state_recovery(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001578 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001579 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001580 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001581}
1582
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583/*
1584 * OPEN_EXPIRED:
1585 * reclaim state on the server after a network partition.
1586 * Assumes caller holds the appropriate lock
1587 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001588static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001590 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001591 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001593 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1594 if (IS_ERR(opendata))
1595 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001596 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001597 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001598 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001599 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001600 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601}
1602
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001603static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001604{
Trond Myklebust539cd032007-06-05 11:46:42 -04001605 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001606 struct nfs4_exception exception = { };
1607 int err;
1608
1609 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001610 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001611 switch (err) {
1612 default:
1613 goto out;
1614 case -NFS4ERR_GRACE:
1615 case -NFS4ERR_DELAY:
1616 nfs4_handle_exception(server, err, &exception);
1617 err = 0;
1618 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001619 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001620out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001621 return err;
1622}
1623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1625{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001627 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
Trond Myklebust864472e2006-01-03 09:55:15 +01001629 ctx = nfs4_state_find_open_context(state);
1630 if (IS_ERR(ctx))
1631 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001632 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001633 put_nfs_open_context(ctx);
1634 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635}
1636
1637/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001638 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1639 * fields corresponding to attributes that were used to store the verifier.
1640 * Make sure we clobber those fields in the later setattr call
1641 */
1642static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1643{
1644 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1645 !(sattr->ia_valid & ATTR_ATIME_SET))
1646 sattr->ia_valid |= ATTR_ATIME;
1647
1648 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1649 !(sattr->ia_valid & ATTR_MTIME_SET))
1650 sattr->ia_valid |= ATTR_MTIME;
1651}
1652
1653/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001654 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001656static 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 -07001657{
1658 struct nfs4_state_owner *sp;
1659 struct nfs4_state *state = NULL;
1660 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001661 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001662 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
1664 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 status = -ENOMEM;
1666 if (!(sp = nfs4_get_state_owner(server, cred))) {
1667 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1668 goto out_err;
1669 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001670 status = nfs4_recover_expired_lease(server);
1671 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001672 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001673 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001674 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001675 status = -ENOMEM;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001676 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001677 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001678 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Trond Myklebustaac00a82007-07-05 19:02:21 -04001680 if (path->dentry->d_inode != NULL)
1681 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1682
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001683 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001685 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001687 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001688 status = PTR_ERR(state);
1689 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001690 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001691 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001692 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001693
1694 if (opendata->o_arg.open_flags & O_EXCL) {
1695 nfs4_exclusive_attrset(opendata, sattr);
1696
1697 nfs_fattr_init(opendata->o_res.f_attr);
1698 status = nfs4_do_setattr(state->inode, cred,
1699 opendata->o_res.f_attr, sattr,
1700 state);
1701 if (status == 0)
1702 nfs_setattr_update_inode(state->inode, sattr);
1703 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1704 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001705 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 *res = state;
1708 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001709err_opendata_put:
1710 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001711err_put_state_owner:
1712 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 *res = NULL;
1715 return status;
1716}
1717
1718
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001719static 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 -07001720{
1721 struct nfs4_exception exception = { };
1722 struct nfs4_state *res;
1723 int status;
1724
1725 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001726 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 if (status == 0)
1728 break;
1729 /* NOTE: BAD_SEQID means the server and client disagree about the
1730 * book-keeping w.r.t. state-changing operations
1731 * (OPEN/CLOSE/LOCK/LOCKU...)
1732 * It is actually a sign of a bug on the client or on the server.
1733 *
1734 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001735 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 * have unhashed the old state_owner for us, and that we can
1737 * therefore safely retry using a new one. We should still warn
1738 * the user though...
1739 */
1740 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001741 printk(KERN_WARNING "NFS: v4 server %s "
1742 " returned a bad sequence-id error!\n",
1743 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 exception.retry = 1;
1745 continue;
1746 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001747 /*
1748 * BAD_STATEID on OPEN means that the server cancelled our
1749 * state before it received the OPEN_CONFIRM.
1750 * Recover by retrying the request as per the discussion
1751 * on Page 181 of RFC3530.
1752 */
1753 if (status == -NFS4ERR_BAD_STATEID) {
1754 exception.retry = 1;
1755 continue;
1756 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001757 if (status == -EAGAIN) {
1758 /* We must have found a delegation */
1759 exception.retry = 1;
1760 continue;
1761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1763 status, &exception));
1764 } while (exception.retry);
1765 return res;
1766}
1767
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001768static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1769 struct nfs_fattr *fattr, struct iattr *sattr,
1770 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001772 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001774 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 .iap = sattr,
1776 .server = server,
1777 .bitmask = server->attr_bitmask,
1778 };
1779 struct nfs_setattrres res = {
1780 .fattr = fattr,
1781 .server = server,
1782 };
1783 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001784 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1785 .rpc_argp = &arg,
1786 .rpc_resp = &res,
1787 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001789 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001790 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Trond Myklebust0e574af2005-10-27 22:12:38 -04001792 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001794 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1795 /* Use that stateid */
1796 } else if (state != NULL) {
Trond Myklebust77041ed2010-07-01 12:49:11 -04001797 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001798 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1800
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001801 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001802 if (status == 0 && state != NULL)
1803 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001804 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805}
1806
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001807static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1808 struct nfs_fattr *fattr, struct iattr *sattr,
1809 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001811 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 struct nfs4_exception exception = { };
1813 int err;
1814 do {
1815 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001816 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 &exception);
1818 } while (exception.retry);
1819 return err;
1820}
1821
1822struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001823 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 struct inode *inode;
1825 struct nfs4_state *state;
1826 struct nfs_closeargs arg;
1827 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001828 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001829 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830};
1831
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001832static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001833{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001834 struct nfs4_closedata *calldata = data;
1835 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001836
1837 nfs4_put_open_state(calldata->state);
1838 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001839 nfs4_put_state_owner(sp);
Jan Blunck31f31db12008-05-02 13:42:45 -07001840 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001841 kfree(calldata);
1842}
1843
Trond Myklebust88069f72009-12-08 08:33:16 -05001844static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1845 fmode_t fmode)
1846{
1847 spin_lock(&state->owner->so_lock);
1848 if (!(fmode & FMODE_READ))
1849 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1850 if (!(fmode & FMODE_WRITE))
1851 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1852 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1853 spin_unlock(&state->owner->so_lock);
1854}
1855
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001856static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001858 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 struct nfs_server *server = NFS_SERVER(calldata->inode);
1861
Trond Myklebust14516c32010-07-31 14:29:06 -04001862 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1863 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 /* hmm. we are done with the inode, and in the process of freeing
1865 * the state_owner. we keep this around to process errors
1866 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 switch (task->tk_status) {
1868 case 0:
Trond Myklebust45328c32007-07-26 17:47:34 -04001869 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001870 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001871 nfs4_close_clear_stateid_flags(state,
1872 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 break;
1874 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001875 case -NFS4ERR_OLD_STATEID:
1876 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001878 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001879 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001881 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1882 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001884 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001885 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886}
1887
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001888static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001890 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001891 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001892 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001893
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001894 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001895 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001896
Trond Myklebust88069f72009-12-08 08:33:16 -05001897 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1898 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001899 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001900 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001901 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001902 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001903 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1904 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1905 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001906 }
1907 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001908 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1909 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1910 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001911 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001912 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001913 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05001914
1915 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001916 /* Note: exit _without_ calling nfs4_close_done */
1917 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001918 return;
1919 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001920
1921 if (calldata->arg.fmode == 0)
1922 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
1923
Trond Myklebust516a6af2005-10-27 22:12:41 -04001924 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001925 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04001926 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Andy Adamson19ddab02009-04-01 09:22:20 -04001927 &calldata->arg.seq_args, &calldata->res.seq_res,
1928 1, task))
1929 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001930 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931}
1932
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001933static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001934 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001935 .rpc_call_done = nfs4_close_done,
1936 .rpc_release = nfs4_free_closedata,
1937};
1938
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939/*
1940 * It is possible for data to be read/written from a mem-mapped file
1941 * after the sys_close call (which hits the vfs layer as a flush).
1942 * This means that we can't safely call nfsv4 close on a file until
1943 * the inode is cleared. This in turn means that we are not good
1944 * NFSv4 citizens - we do not indicate to the server to update the file's
1945 * share state even when we are done with one of the three share
1946 * stateid's in the inode.
1947 *
1948 * NOTE: Caller must be holding the sp->so_owner semaphore!
1949 */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001950int nfs4_do_close(struct path *path, struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001952 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001954 struct nfs4_state_owner *sp = state->owner;
1955 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001956 struct rpc_message msg = {
1957 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1958 .rpc_cred = state->owner->so_cred,
1959 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001960 struct rpc_task_setup task_setup_data = {
1961 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001962 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001963 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05001964 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001965 .flags = RPC_TASK_ASYNC,
1966 };
Trond Myklebust95121352005-10-18 14:20:12 -07001967 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001969 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001971 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001972 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001974 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04001975 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001977 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07001978 if (calldata->arg.seqid == NULL)
1979 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001980 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001981 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001982 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04001983 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001984 calldata->res.server = server;
Al Virof6948692010-01-30 13:51:04 -05001985 path_get(path);
1986 calldata->path = *path;
Trond Myklebust95121352005-10-18 14:20:12 -07001987
Trond Myklebust1174dd12010-12-21 10:52:24 -05001988 msg.rpc_argp = &calldata->arg;
1989 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001990 task_setup_data.callback_data = calldata;
1991 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001992 if (IS_ERR(task))
1993 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001994 status = 0;
1995 if (wait)
1996 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001997 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001998 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07001999out_free_calldata:
2000 kfree(calldata);
2001out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002002 nfs4_put_open_state(state);
2003 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002004 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005}
2006
Trond Myklebust2b484292010-09-17 10:56:51 -04002007static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002008nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 struct nfs4_state *state;
2011
Trond Myklebust565277f2007-10-15 18:17:53 -04002012 /* Protect against concurrent sillydeletes */
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002013 state = nfs4_do_open(dir, &ctx->path, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002014 if (IS_ERR(state))
2015 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002016 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002017 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018}
2019
Trond Myklebust1185a552009-12-03 15:54:02 -05002020static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002021{
2022 if (ctx->state == NULL)
2023 return;
2024 if (is_sync)
2025 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2026 else
2027 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2028}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
2030static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2031{
Benny Halevy43652ad2009-04-01 09:21:54 -04002032 struct nfs4_server_caps_arg args = {
2033 .fhandle = fhandle,
2034 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 struct nfs4_server_caps_res res = {};
2036 struct rpc_message msg = {
2037 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002038 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 .rpc_resp = &res,
2040 };
2041 int status;
2042
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002043 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 if (status == 0) {
2045 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002046 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2047 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2048 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2049 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2050 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2052 server->caps |= NFS_CAP_ACLS;
2053 if (res.has_links != 0)
2054 server->caps |= NFS_CAP_HARDLINKS;
2055 if (res.has_symlinks != 0)
2056 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002057 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2058 server->caps |= NFS_CAP_FILEID;
2059 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2060 server->caps |= NFS_CAP_MODE;
2061 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2062 server->caps |= NFS_CAP_NLINK;
2063 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2064 server->caps |= NFS_CAP_OWNER;
2065 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2066 server->caps |= NFS_CAP_OWNER_GROUP;
2067 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2068 server->caps |= NFS_CAP_ATIME;
2069 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2070 server->caps |= NFS_CAP_CTIME;
2071 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2072 server->caps |= NFS_CAP_MTIME;
2073
Trond Myklebusta65318b2009-03-11 14:10:28 -04002074 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2075 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2076 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 server->acl_bitmask = res.acl_bitmask;
2078 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002079
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 return status;
2081}
2082
Trond Myklebust55a97592006-06-09 09:34:19 -04002083int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084{
2085 struct nfs4_exception exception = { };
2086 int err;
2087 do {
2088 err = nfs4_handle_exception(server,
2089 _nfs4_server_capabilities(server, fhandle),
2090 &exception);
2091 } while (exception.retry);
2092 return err;
2093}
2094
2095static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2096 struct nfs_fsinfo *info)
2097{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 struct nfs4_lookup_root_arg args = {
2099 .bitmask = nfs4_fattr_bitmap,
2100 };
2101 struct nfs4_lookup_res res = {
2102 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002103 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 .fh = fhandle,
2105 };
2106 struct rpc_message msg = {
2107 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2108 .rpc_argp = &args,
2109 .rpc_resp = &res,
2110 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002111
Trond Myklebust0e574af2005-10-27 22:12:38 -04002112 nfs_fattr_init(info->fattr);
Trond Myklebustfccba802009-07-21 16:48:07 -04002113 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114}
2115
2116static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2117 struct nfs_fsinfo *info)
2118{
2119 struct nfs4_exception exception = { };
2120 int err;
2121 do {
2122 err = nfs4_handle_exception(server,
2123 _nfs4_lookup_root(server, fhandle, info),
2124 &exception);
2125 } while (exception.retry);
2126 return err;
2127}
2128
David Howells54ceac42006-08-22 20:06:13 -04002129/*
2130 * get the file handle for the "/" directory on the server
2131 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002133 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 int status;
2136
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 if (status == 0)
2139 status = nfs4_server_capabilities(server, fhandle);
2140 if (status == 0)
2141 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002142 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143}
2144
Manoj Naik6b97fd32006-06-09 09:34:29 -04002145/*
2146 * Get locations and (maybe) other attributes of a referral.
2147 * Note that we'll actually follow the referral later when
2148 * we detect fsid mismatch in inode revalidation
2149 */
Trond Myklebust56659e92007-07-17 21:52:39 -04002150static 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 -04002151{
2152 int status = -ENOMEM;
2153 struct page *page = NULL;
2154 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002155
2156 page = alloc_page(GFP_KERNEL);
2157 if (page == NULL)
2158 goto out;
2159 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2160 if (locations == NULL)
2161 goto out;
2162
Trond Myklebustc228fd32007-01-13 02:28:11 -05002163 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002164 if (status != 0)
2165 goto out;
2166 /* Make sure server returned a different fsid for the referral */
2167 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07002168 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 -04002169 status = -EIO;
2170 goto out;
2171 }
2172
2173 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2174 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2175 if (!fattr->mode)
2176 fattr->mode = S_IFDIR;
2177 memset(fhandle, 0, sizeof(struct nfs_fh));
2178out:
2179 if (page)
2180 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002181 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002182 return status;
2183}
2184
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2186{
2187 struct nfs4_getattr_arg args = {
2188 .fh = fhandle,
2189 .bitmask = server->attr_bitmask,
2190 };
2191 struct nfs4_getattr_res res = {
2192 .fattr = fattr,
2193 .server = server,
2194 };
2195 struct rpc_message msg = {
2196 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2197 .rpc_argp = &args,
2198 .rpc_resp = &res,
2199 };
2200
Trond Myklebust0e574af2005-10-27 22:12:38 -04002201 nfs_fattr_init(fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002202 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203}
2204
2205static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2206{
2207 struct nfs4_exception exception = { };
2208 int err;
2209 do {
2210 err = nfs4_handle_exception(server,
2211 _nfs4_proc_getattr(server, fhandle, fattr),
2212 &exception);
2213 } while (exception.retry);
2214 return err;
2215}
2216
2217/*
2218 * The file is not closed if it is opened due to the a request to change
2219 * the size of the file. The open call will not be needed once the
2220 * VFS layer lookup-intents are implemented.
2221 *
2222 * Close is called when the inode is destroyed.
2223 * If we haven't opened the file for O_WRONLY, we
2224 * need to in the size_change case to obtain a stateid.
2225 *
2226 * Got race?
2227 * Because OPEN is always done by name in nfsv4, it is
2228 * possible that we opened a different file by the same
2229 * name. We can recognize this race condition, but we
2230 * can't do anything about it besides returning an error.
2231 *
2232 * This will be fixed with VFS changes (lookup-intent).
2233 */
2234static int
2235nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2236 struct iattr *sattr)
2237{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002238 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002239 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002240 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 int status;
2242
Trond Myklebust0e574af2005-10-27 22:12:38 -04002243 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
Trond Myklebustd5308382005-11-04 15:33:38 -05002245 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002246 if (sattr->ia_valid & ATTR_FILE) {
2247 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002248
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002249 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002250 if (ctx) {
2251 cred = ctx->cred;
2252 state = ctx->state;
2253 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002254 }
2255
2256 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002257 if (status == 0)
2258 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 return status;
2260}
2261
Trond Myklebust56659e92007-07-17 21:52:39 -04002262static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2263 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04002264 struct nfs_fattr *fattr)
2265{
2266 int status;
2267 struct nfs4_lookup_arg args = {
2268 .bitmask = server->attr_bitmask,
2269 .dir_fh = dirfh,
2270 .name = name,
2271 };
2272 struct nfs4_lookup_res res = {
2273 .server = server,
2274 .fattr = fattr,
2275 .fh = fhandle,
2276 };
2277 struct rpc_message msg = {
2278 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2279 .rpc_argp = &args,
2280 .rpc_resp = &res,
2281 };
2282
2283 nfs_fattr_init(fattr);
2284
2285 dprintk("NFS call lookupfh %s\n", name->name);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002286 status = nfs4_call_sync(server, &msg, &args, &res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002287 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002288 return status;
2289}
2290
2291static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2292 struct qstr *name, struct nfs_fh *fhandle,
2293 struct nfs_fattr *fattr)
2294{
2295 struct nfs4_exception exception = { };
2296 int err;
2297 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04002298 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2299 /* FIXME: !!!! */
2300 if (err == -NFS4ERR_MOVED) {
2301 err = -EREMOTE;
2302 break;
2303 }
2304 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002305 } while (exception.retry);
2306 return err;
2307}
2308
Trond Myklebust56659e92007-07-17 21:52:39 -04002309static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2311{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002312 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313
2314 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002315 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002316 if (status == -NFS4ERR_MOVED)
2317 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 dprintk("NFS reply lookup: %d\n", status);
2319 return status;
2320}
2321
2322static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2323{
2324 struct nfs4_exception exception = { };
2325 int err;
2326 do {
2327 err = nfs4_handle_exception(NFS_SERVER(dir),
2328 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2329 &exception);
2330 } while (exception.retry);
2331 return err;
2332}
2333
2334static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2335{
Trond Myklebust76b32992007-08-10 17:45:11 -04002336 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 struct nfs4_accessargs args = {
2338 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002339 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002341 struct nfs4_accessres res = {
2342 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002343 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 struct rpc_message msg = {
2345 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2346 .rpc_argp = &args,
2347 .rpc_resp = &res,
2348 .rpc_cred = entry->cred,
2349 };
2350 int mode = entry->mask;
2351 int status;
2352
2353 /*
2354 * Determine which access bits we want to ask for...
2355 */
2356 if (mode & MAY_READ)
2357 args.access |= NFS4_ACCESS_READ;
2358 if (S_ISDIR(inode->i_mode)) {
2359 if (mode & MAY_WRITE)
2360 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2361 if (mode & MAY_EXEC)
2362 args.access |= NFS4_ACCESS_LOOKUP;
2363 } else {
2364 if (mode & MAY_WRITE)
2365 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2366 if (mode & MAY_EXEC)
2367 args.access |= NFS4_ACCESS_EXECUTE;
2368 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002369
2370 res.fattr = nfs_alloc_fattr();
2371 if (res.fattr == NULL)
2372 return -ENOMEM;
2373
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002374 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 if (!status) {
2376 entry->mask = 0;
2377 if (res.access & NFS4_ACCESS_READ)
2378 entry->mask |= MAY_READ;
2379 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2380 entry->mask |= MAY_WRITE;
2381 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2382 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002383 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002385 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 return status;
2387}
2388
2389static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2390{
2391 struct nfs4_exception exception = { };
2392 int err;
2393 do {
2394 err = nfs4_handle_exception(NFS_SERVER(inode),
2395 _nfs4_proc_access(inode, entry),
2396 &exception);
2397 } while (exception.retry);
2398 return err;
2399}
2400
2401/*
2402 * TODO: For the time being, we don't try to get any attributes
2403 * along with any of the zero-copy operations READ, READDIR,
2404 * READLINK, WRITE.
2405 *
2406 * In the case of the first three, we want to put the GETATTR
2407 * after the read-type operation -- this is because it is hard
2408 * to predict the length of a GETATTR response in v4, and thus
2409 * align the READ data correctly. This means that the GETATTR
2410 * may end up partially falling into the page cache, and we should
2411 * shift it into the 'tail' of the xdr_buf before processing.
2412 * To do this efficiently, we need to know the total length
2413 * of data received, which doesn't seem to be available outside
2414 * of the RPC layer.
2415 *
2416 * In the case of WRITE, we also want to put the GETATTR after
2417 * the operation -- in this case because we want to make sure
2418 * we get the post-operation mtime and size. This means that
2419 * we can't use xdr_encode_pages() as written: we need a variant
2420 * of it which would leave room in the 'tail' iovec.
2421 *
2422 * Both of these changes to the XDR layer would in fact be quite
2423 * minor, but I decided to leave them for a subsequent patch.
2424 */
2425static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2426 unsigned int pgbase, unsigned int pglen)
2427{
2428 struct nfs4_readlink args = {
2429 .fh = NFS_FH(inode),
2430 .pgbase = pgbase,
2431 .pglen = pglen,
2432 .pages = &page,
2433 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002434 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 struct rpc_message msg = {
2436 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2437 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002438 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 };
2440
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002441 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442}
2443
2444static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2445 unsigned int pgbase, unsigned int pglen)
2446{
2447 struct nfs4_exception exception = { };
2448 int err;
2449 do {
2450 err = nfs4_handle_exception(NFS_SERVER(inode),
2451 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2452 &exception);
2453 } while (exception.retry);
2454 return err;
2455}
2456
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457/*
2458 * Got race?
2459 * We will need to arrange for the VFS layer to provide an atomic open.
2460 * Until then, this create/open method is prone to inefficiency and race
2461 * conditions due to the lookup, create, and open VFS calls from sys_open()
2462 * placed on the wire.
2463 *
2464 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2465 * The file will be opened again in the subsequent VFS open call
2466 * (nfs4_proc_file_open).
2467 *
2468 * The open for read will just hang around to be used by any process that
2469 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2470 */
2471
2472static int
2473nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002474 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475{
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002476 struct path my_path = {
Trond Myklebustad389da2007-06-05 12:30:00 -04002477 .dentry = dentry,
2478 };
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002479 struct path *path = &my_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002481 struct rpc_cred *cred = NULL;
2482 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 int status = 0;
2484
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002485 if (ctx != NULL) {
2486 cred = ctx->cred;
2487 path = &ctx->path;
2488 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002490 sattr->ia_mode &= ~current_umask();
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002491 state = nfs4_do_open(dir, path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002492 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 if (IS_ERR(state)) {
2494 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002495 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002497 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002498 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002499 if (ctx != NULL)
2500 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002501 else
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002502 nfs4_close_sync(path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503out:
2504 return status;
2505}
2506
2507static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2508{
Trond Myklebust16e42952005-10-27 22:12:44 -04002509 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002510 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002512 .name.len = name->len,
2513 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002514 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002516 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002517 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002518 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002520 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2521 .rpc_argp = &args,
2522 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002524 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
Trond Myklebustd3468902010-04-16 16:22:50 -04002526 res.dir_attr = nfs_alloc_fattr();
2527 if (res.dir_attr == NULL)
2528 goto out;
2529
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002530 status = nfs4_call_sync(server, &msg, &args, &res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002531 if (status == 0) {
2532 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002533 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002534 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002535 nfs_free_fattr(res.dir_attr);
2536out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 return status;
2538}
2539
2540static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2541{
2542 struct nfs4_exception exception = { };
2543 int err;
2544 do {
2545 err = nfs4_handle_exception(NFS_SERVER(dir),
2546 _nfs4_proc_remove(dir, name),
2547 &exception);
2548 } while (exception.retry);
2549 return err;
2550}
2551
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002552static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002554 struct nfs_server *server = NFS_SERVER(dir);
2555 struct nfs_removeargs *args = msg->rpc_argp;
2556 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557
Trond Myklebusta65318b2009-03-11 14:10:28 -04002558 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002559 res->server = server;
Benny Halevydfb4f3092010-09-24 09:17:01 -04002560 res->seq_res.sr_slot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562}
2563
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002564static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002566 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2567
Trond Myklebust14516c32010-07-31 14:29:06 -04002568 if (!nfs4_sequence_done(task, &res->seq_res))
2569 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002570 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002571 return 0;
2572 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002573 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002574 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575}
2576
Jeff Laytond3d41522010-09-17 17:31:57 -04002577static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2578{
2579 struct nfs_server *server = NFS_SERVER(dir);
2580 struct nfs_renameargs *arg = msg->rpc_argp;
2581 struct nfs_renameres *res = msg->rpc_resp;
2582
2583 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2584 arg->bitmask = server->attr_bitmask;
2585 res->server = server;
2586}
2587
2588static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2589 struct inode *new_dir)
2590{
2591 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2592
2593 if (!nfs4_sequence_done(task, &res->seq_res))
2594 return 0;
2595 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2596 return 0;
2597
2598 update_changeattr(old_dir, &res->old_cinfo);
2599 nfs_post_op_update_inode(old_dir, res->old_fattr);
2600 update_changeattr(new_dir, &res->new_cinfo);
2601 nfs_post_op_update_inode(new_dir, res->new_fattr);
2602 return 1;
2603}
2604
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2606 struct inode *new_dir, struct qstr *new_name)
2607{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002608 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002609 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 .old_dir = NFS_FH(old_dir),
2611 .new_dir = NFS_FH(new_dir),
2612 .old_name = old_name,
2613 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002614 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002616 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002617 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002618 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 struct rpc_message msg = {
2620 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2621 .rpc_argp = &arg,
2622 .rpc_resp = &res,
2623 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002624 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
Trond Myklebust011fff72010-04-16 16:22:49 -04002626 res.old_fattr = nfs_alloc_fattr();
2627 res.new_fattr = nfs_alloc_fattr();
2628 if (res.old_fattr == NULL || res.new_fattr == NULL)
2629 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
Trond Myklebust011fff72010-04-16 16:22:49 -04002631 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 if (!status) {
2633 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002634 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002636 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002638out:
2639 nfs_free_fattr(res.new_fattr);
2640 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 return status;
2642}
2643
2644static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2645 struct inode *new_dir, struct qstr *new_name)
2646{
2647 struct nfs4_exception exception = { };
2648 int err;
2649 do {
2650 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2651 _nfs4_proc_rename(old_dir, old_name,
2652 new_dir, new_name),
2653 &exception);
2654 } while (exception.retry);
2655 return err;
2656}
2657
2658static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2659{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002660 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 struct nfs4_link_arg arg = {
2662 .fh = NFS_FH(inode),
2663 .dir_fh = NFS_FH(dir),
2664 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002665 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002667 struct nfs4_link_res res = {
2668 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002669 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 struct rpc_message msg = {
2671 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2672 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002673 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002675 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676
Trond Myklebust136f2622010-04-16 16:22:49 -04002677 res.fattr = nfs_alloc_fattr();
2678 res.dir_attr = nfs_alloc_fattr();
2679 if (res.fattr == NULL || res.dir_attr == NULL)
2680 goto out;
2681
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002682 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002683 if (!status) {
2684 update_changeattr(dir, &res.cinfo);
2685 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002686 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002687 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002688out:
2689 nfs_free_fattr(res.dir_attr);
2690 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 return status;
2692}
2693
2694static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2695{
2696 struct nfs4_exception exception = { };
2697 int err;
2698 do {
2699 err = nfs4_handle_exception(NFS_SERVER(inode),
2700 _nfs4_proc_link(inode, dir, name),
2701 &exception);
2702 } while (exception.retry);
2703 return err;
2704}
2705
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002706struct nfs4_createdata {
2707 struct rpc_message msg;
2708 struct nfs4_create_arg arg;
2709 struct nfs4_create_res res;
2710 struct nfs_fh fh;
2711 struct nfs_fattr fattr;
2712 struct nfs_fattr dir_fattr;
2713};
2714
2715static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2716 struct qstr *name, struct iattr *sattr, u32 ftype)
2717{
2718 struct nfs4_createdata *data;
2719
2720 data = kzalloc(sizeof(*data), GFP_KERNEL);
2721 if (data != NULL) {
2722 struct nfs_server *server = NFS_SERVER(dir);
2723
2724 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2725 data->msg.rpc_argp = &data->arg;
2726 data->msg.rpc_resp = &data->res;
2727 data->arg.dir_fh = NFS_FH(dir);
2728 data->arg.server = server;
2729 data->arg.name = name;
2730 data->arg.attrs = sattr;
2731 data->arg.ftype = ftype;
2732 data->arg.bitmask = server->attr_bitmask;
2733 data->res.server = server;
2734 data->res.fh = &data->fh;
2735 data->res.fattr = &data->fattr;
2736 data->res.dir_fattr = &data->dir_fattr;
2737 nfs_fattr_init(data->res.fattr);
2738 nfs_fattr_init(data->res.dir_fattr);
2739 }
2740 return data;
2741}
2742
2743static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2744{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002745 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2746 &data->arg, &data->res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002747 if (status == 0) {
2748 update_changeattr(dir, &data->res.dir_cinfo);
2749 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2750 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2751 }
2752 return status;
2753}
2754
2755static void nfs4_free_createdata(struct nfs4_createdata *data)
2756{
2757 kfree(data);
2758}
2759
Chuck Lever4f390c12006-08-22 20:06:22 -04002760static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002761 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002763 struct nfs4_createdata *data;
2764 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765
Chuck Lever94a6d752006-08-22 20:06:23 -04002766 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002767 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002768
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002769 status = -ENOMEM;
2770 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2771 if (data == NULL)
2772 goto out;
2773
2774 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2775 data->arg.u.symlink.pages = &page;
2776 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002778 status = nfs4_do_create(dir, dentry, data);
2779
2780 nfs4_free_createdata(data);
2781out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 return status;
2783}
2784
Chuck Lever4f390c12006-08-22 20:06:22 -04002785static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002786 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787{
2788 struct nfs4_exception exception = { };
2789 int err;
2790 do {
2791 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002792 _nfs4_proc_symlink(dir, dentry, page,
2793 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 &exception);
2795 } while (exception.retry);
2796 return err;
2797}
2798
2799static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2800 struct iattr *sattr)
2801{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002802 struct nfs4_createdata *data;
2803 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002805 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2806 if (data == NULL)
2807 goto out;
2808
2809 status = nfs4_do_create(dir, dentry, data);
2810
2811 nfs4_free_createdata(data);
2812out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 return status;
2814}
2815
2816static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2817 struct iattr *sattr)
2818{
2819 struct nfs4_exception exception = { };
2820 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002821
2822 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 do {
2824 err = nfs4_handle_exception(NFS_SERVER(dir),
2825 _nfs4_proc_mkdir(dir, dentry, sattr),
2826 &exception);
2827 } while (exception.retry);
2828 return err;
2829}
2830
2831static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002832 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833{
2834 struct inode *dir = dentry->d_inode;
2835 struct nfs4_readdir_arg args = {
2836 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002837 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 .pgbase = 0,
2839 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002840 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04002841 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 };
2843 struct nfs4_readdir_res res;
2844 struct rpc_message msg = {
2845 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2846 .rpc_argp = &args,
2847 .rpc_resp = &res,
2848 .rpc_cred = cred,
2849 };
2850 int status;
2851
Harvey Harrison3110ff82008-05-02 13:42:44 -07002852 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002853 dentry->d_parent->d_name.name,
2854 dentry->d_name.name,
2855 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2857 res.pgbase = args.pgbase;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002858 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
Trond Myklebustac396122010-11-15 20:26:22 -05002859 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05002861 status += args.pgbase;
2862 }
Trond Myklebustc4812992007-09-28 17:11:45 -04002863
2864 nfs_invalidate_atime(dir);
2865
Harvey Harrison3110ff82008-05-02 13:42:44 -07002866 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 return status;
2868}
2869
2870static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002871 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872{
2873 struct nfs4_exception exception = { };
2874 int err;
2875 do {
2876 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2877 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002878 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 &exception);
2880 } while (exception.retry);
2881 return err;
2882}
2883
2884static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2885 struct iattr *sattr, dev_t rdev)
2886{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002887 struct nfs4_createdata *data;
2888 int mode = sattr->ia_mode;
2889 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
2891 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2892 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002893
2894 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2895 if (data == NULL)
2896 goto out;
2897
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002899 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002901 data->arg.ftype = NF4BLK;
2902 data->arg.u.device.specdata1 = MAJOR(rdev);
2903 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 }
2905 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002906 data->arg.ftype = NF4CHR;
2907 data->arg.u.device.specdata1 = MAJOR(rdev);
2908 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002911 status = nfs4_do_create(dir, dentry, data);
2912
2913 nfs4_free_createdata(data);
2914out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 return status;
2916}
2917
2918static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2919 struct iattr *sattr, dev_t rdev)
2920{
2921 struct nfs4_exception exception = { };
2922 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002923
2924 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 do {
2926 err = nfs4_handle_exception(NFS_SERVER(dir),
2927 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2928 &exception);
2929 } while (exception.retry);
2930 return err;
2931}
2932
2933static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2934 struct nfs_fsstat *fsstat)
2935{
2936 struct nfs4_statfs_arg args = {
2937 .fh = fhandle,
2938 .bitmask = server->attr_bitmask,
2939 };
Benny Halevy24ad1482009-04-01 09:21:56 -04002940 struct nfs4_statfs_res res = {
2941 .fsstat = fsstat,
2942 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 struct rpc_message msg = {
2944 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2945 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04002946 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 };
2948
Trond Myklebust0e574af2005-10-27 22:12:38 -04002949 nfs_fattr_init(fsstat->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002950 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951}
2952
2953static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2954{
2955 struct nfs4_exception exception = { };
2956 int err;
2957 do {
2958 err = nfs4_handle_exception(server,
2959 _nfs4_proc_statfs(server, fhandle, fsstat),
2960 &exception);
2961 } while (exception.retry);
2962 return err;
2963}
2964
2965static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2966 struct nfs_fsinfo *fsinfo)
2967{
2968 struct nfs4_fsinfo_arg args = {
2969 .fh = fhandle,
2970 .bitmask = server->attr_bitmask,
2971 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04002972 struct nfs4_fsinfo_res res = {
2973 .fsinfo = fsinfo,
2974 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 struct rpc_message msg = {
2976 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2977 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04002978 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 };
2980
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002981 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982}
2983
2984static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2985{
2986 struct nfs4_exception exception = { };
2987 int err;
2988
2989 do {
2990 err = nfs4_handle_exception(server,
2991 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2992 &exception);
2993 } while (exception.retry);
2994 return err;
2995}
2996
2997static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2998{
Trond Myklebust0e574af2005-10-27 22:12:38 -04002999 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3001}
3002
3003static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3004 struct nfs_pathconf *pathconf)
3005{
3006 struct nfs4_pathconf_arg args = {
3007 .fh = fhandle,
3008 .bitmask = server->attr_bitmask,
3009 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003010 struct nfs4_pathconf_res res = {
3011 .pathconf = pathconf,
3012 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 struct rpc_message msg = {
3014 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3015 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003016 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 };
3018
3019 /* None of the pathconf attributes are mandatory to implement */
3020 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3021 memset(pathconf, 0, sizeof(*pathconf));
3022 return 0;
3023 }
3024
Trond Myklebust0e574af2005-10-27 22:12:38 -04003025 nfs_fattr_init(pathconf->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003026 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027}
3028
3029static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3030 struct nfs_pathconf *pathconf)
3031{
3032 struct nfs4_exception exception = { };
3033 int err;
3034
3035 do {
3036 err = nfs4_handle_exception(server,
3037 _nfs4_proc_pathconf(server, fhandle, pathconf),
3038 &exception);
3039 } while (exception.retry);
3040 return err;
3041}
3042
Trond Myklebustec06c092006-03-20 13:44:27 -05003043static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044{
Trond Myklebustec06c092006-03-20 13:44:27 -05003045 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003047 dprintk("--> %s\n", __func__);
3048
Trond Myklebust14516c32010-07-31 14:29:06 -04003049 if (!nfs4_sequence_done(task, &data->res.seq_res))
3050 return -EAGAIN;
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003051
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003052 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003053 nfs_restart_rpc(task, server->nfs_client);
Trond Myklebustec06c092006-03-20 13:44:27 -05003054 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003056
3057 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003059 renew_lease(server, data->timestamp);
3060 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061}
3062
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003063static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 data->timestamp = jiffies;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003066 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067}
3068
Trond Myklebust788e7a82006-03-20 13:44:27 -05003069static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 struct inode *inode = data->inode;
3072
Trond Myklebust14516c32010-07-31 14:29:06 -04003073 if (!nfs4_sequence_done(task, &data->res.seq_res))
3074 return -EAGAIN;
Andy Adamsondef6ed72009-04-01 09:22:26 -04003075
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003076 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003077 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003078 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003080 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003082 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003083 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003084 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085}
3086
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003087static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003089 struct nfs_server *server = NFS_SERVER(data->inode);
3090
Trond Myklebusta65318b2009-03-11 14:10:28 -04003091 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003092 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 data->timestamp = jiffies;
3094
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003095 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096}
3097
Trond Myklebust788e7a82006-03-20 13:44:27 -05003098static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 struct inode *inode = data->inode;
3101
Trond Myklebust14516c32010-07-31 14:29:06 -04003102 if (!nfs4_sequence_done(task, &data->res.seq_res))
3103 return -EAGAIN;
3104
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003105 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003106 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003107 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003109 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003110 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111}
3112
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003113static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003115 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116
Trond Myklebusta65318b2009-03-11 14:10:28 -04003117 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003118 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003119 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120}
3121
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003122struct nfs4_renewdata {
3123 struct nfs_client *client;
3124 unsigned long timestamp;
3125};
3126
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127/*
3128 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3129 * standalone procedure for queueing an asynchronous RENEW.
3130 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003131static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003132{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003133 struct nfs4_renewdata *data = calldata;
3134 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003135
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003136 if (atomic_read(&clp->cl_count) > 1)
3137 nfs4_schedule_state_renewal(clp);
3138 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003139 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003140}
3141
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003142static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003144 struct nfs4_renewdata *data = calldata;
3145 struct nfs_client *clp = data->client;
3146 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147
3148 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003149 /* Unless we're shutting down, schedule state recovery! */
3150 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3151 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 return;
3153 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003154 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155}
3156
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003157static const struct rpc_call_ops nfs4_renew_ops = {
3158 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003159 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003160};
3161
David Howellsadfa6f92006-08-22 20:06:08 -04003162int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163{
3164 struct rpc_message msg = {
3165 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3166 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003167 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003169 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003171 if (!atomic_inc_not_zero(&clp->cl_count))
3172 return -EIO;
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003173 data = kmalloc(sizeof(*data), GFP_KERNEL);
3174 if (data == NULL)
3175 return -ENOMEM;
3176 data->client = clp;
3177 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003179 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180}
3181
David Howellsadfa6f92006-08-22 20:06:08 -04003182int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183{
3184 struct rpc_message msg = {
3185 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3186 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003187 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 };
3189 unsigned long now = jiffies;
3190 int status;
3191
3192 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3193 if (status < 0)
3194 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003195 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 return 0;
3197}
3198
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003199static inline int nfs4_server_supports_acls(struct nfs_server *server)
3200{
3201 return (server->caps & NFS_CAP_ACLS)
3202 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3203 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3204}
3205
3206/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3207 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3208 * the stack.
3209 */
3210#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3211
3212static void buf_to_pages(const void *buf, size_t buflen,
3213 struct page **pages, unsigned int *pgbase)
3214{
3215 const void *p = buf;
3216
3217 *pgbase = offset_in_page(buf);
3218 p -= *pgbase;
3219 while (p < buf + buflen) {
3220 *(pages++) = virt_to_page(p);
3221 p += PAGE_CACHE_SIZE;
3222 }
3223}
3224
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003225struct nfs4_cached_acl {
3226 int cached;
3227 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003228 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003229};
3230
3231static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003232{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003233 struct nfs_inode *nfsi = NFS_I(inode);
3234
3235 spin_lock(&inode->i_lock);
3236 kfree(nfsi->nfs4_acl);
3237 nfsi->nfs4_acl = acl;
3238 spin_unlock(&inode->i_lock);
3239}
3240
3241static void nfs4_zap_acl_attr(struct inode *inode)
3242{
3243 nfs4_set_cached_acl(inode, NULL);
3244}
3245
3246static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3247{
3248 struct nfs_inode *nfsi = NFS_I(inode);
3249 struct nfs4_cached_acl *acl;
3250 int ret = -ENOENT;
3251
3252 spin_lock(&inode->i_lock);
3253 acl = nfsi->nfs4_acl;
3254 if (acl == NULL)
3255 goto out;
3256 if (buf == NULL) /* user is just asking for length */
3257 goto out_len;
3258 if (acl->cached == 0)
3259 goto out;
3260 ret = -ERANGE; /* see getxattr(2) man page */
3261 if (acl->len > buflen)
3262 goto out;
3263 memcpy(buf, acl->data, acl->len);
3264out_len:
3265 ret = acl->len;
3266out:
3267 spin_unlock(&inode->i_lock);
3268 return ret;
3269}
3270
3271static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3272{
3273 struct nfs4_cached_acl *acl;
3274
3275 if (buf && acl_len <= PAGE_SIZE) {
3276 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3277 if (acl == NULL)
3278 goto out;
3279 acl->cached = 1;
3280 memcpy(acl->data, buf, acl_len);
3281 } else {
3282 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3283 if (acl == NULL)
3284 goto out;
3285 acl->cached = 0;
3286 }
3287 acl->len = acl_len;
3288out:
3289 nfs4_set_cached_acl(inode, acl);
3290}
3291
Trond Myklebust16b42892006-08-24 12:27:15 -04003292static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003293{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003294 struct page *pages[NFS4ACL_MAXPAGES];
3295 struct nfs_getaclargs args = {
3296 .fh = NFS_FH(inode),
3297 .acl_pages = pages,
3298 .acl_len = buflen,
3299 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003300 struct nfs_getaclres res = {
3301 .acl_len = buflen,
3302 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003303 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003304 struct rpc_message msg = {
3305 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3306 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003307 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003308 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003309 struct page *localpage = NULL;
3310 int ret;
3311
3312 if (buflen < PAGE_SIZE) {
3313 /* As long as we're doing a round trip to the server anyway,
3314 * let's be prepared for a page of acl data. */
3315 localpage = alloc_page(GFP_KERNEL);
3316 resp_buf = page_address(localpage);
3317 if (localpage == NULL)
3318 return -ENOMEM;
3319 args.acl_pages[0] = localpage;
3320 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003321 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003322 } else {
3323 resp_buf = buf;
3324 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3325 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003326 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003327 if (ret)
3328 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003329 if (res.acl_len > args.acl_len)
3330 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003331 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003332 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003333 if (buf) {
3334 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003335 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003336 goto out_free;
3337 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003338 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003339 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003340 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003341out_free:
3342 if (localpage)
3343 __free_page(localpage);
3344 return ret;
3345}
3346
Trond Myklebust16b42892006-08-24 12:27:15 -04003347static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3348{
3349 struct nfs4_exception exception = { };
3350 ssize_t ret;
3351 do {
3352 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3353 if (ret >= 0)
3354 break;
3355 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3356 } while (exception.retry);
3357 return ret;
3358}
3359
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003360static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3361{
3362 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003363 int ret;
3364
3365 if (!nfs4_server_supports_acls(server))
3366 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003367 ret = nfs_revalidate_inode(server, inode);
3368 if (ret < 0)
3369 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003370 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3371 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003372 ret = nfs4_read_cached_acl(inode, buf, buflen);
3373 if (ret != -ENOENT)
3374 return ret;
3375 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003376}
3377
Trond Myklebust16b42892006-08-24 12:27:15 -04003378static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003379{
3380 struct nfs_server *server = NFS_SERVER(inode);
3381 struct page *pages[NFS4ACL_MAXPAGES];
3382 struct nfs_setaclargs arg = {
3383 .fh = NFS_FH(inode),
3384 .acl_pages = pages,
3385 .acl_len = buflen,
3386 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003387 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003388 struct rpc_message msg = {
3389 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3390 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003391 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003392 };
3393 int ret;
3394
3395 if (!nfs4_server_supports_acls(server))
3396 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07003397 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003398 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003399 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003400 /*
3401 * Acl update can result in inode attribute update.
3402 * so mark the attribute cache invalid.
3403 */
3404 spin_lock(&inode->i_lock);
3405 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3406 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003407 nfs_access_zap_cache(inode);
3408 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003409 return ret;
3410}
3411
Trond Myklebust16b42892006-08-24 12:27:15 -04003412static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3413{
3414 struct nfs4_exception exception = { };
3415 int err;
3416 do {
3417 err = nfs4_handle_exception(NFS_SERVER(inode),
3418 __nfs4_proc_set_acl(inode, buf, buflen),
3419 &exception);
3420 } while (exception.retry);
3421 return err;
3422}
3423
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003425nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003427 struct nfs_client *clp = server->nfs_client;
3428
3429 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 return 0;
3431 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003432 case -NFS4ERR_ADMIN_REVOKED:
3433 case -NFS4ERR_BAD_STATEID:
3434 case -NFS4ERR_OPENMODE:
3435 if (state == NULL)
3436 break;
3437 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003438 goto do_state_recovery;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003440 case -NFS4ERR_STALE_CLIENTID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 case -NFS4ERR_EXPIRED:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003442 goto do_state_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003443#if defined(CONFIG_NFS_V4_1)
3444 case -NFS4ERR_BADSESSION:
3445 case -NFS4ERR_BADSLOT:
3446 case -NFS4ERR_BAD_HIGH_SLOT:
3447 case -NFS4ERR_DEADSESSION:
3448 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3449 case -NFS4ERR_SEQ_FALSE_RETRY:
3450 case -NFS4ERR_SEQ_MISORDERED:
3451 dprintk("%s ERROR %d, Reset session\n", __func__,
3452 task->tk_status);
Andy Adamson0b9e2d42009-12-04 16:02:14 -05003453 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -04003454 task->tk_status = 0;
3455 return -EAGAIN;
3456#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003458 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003459 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003460 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3462 task->tk_status = 0;
3463 return -EAGAIN;
3464 case -NFS4ERR_OLD_STATEID:
3465 task->tk_status = 0;
3466 return -EAGAIN;
3467 }
3468 task->tk_status = nfs4_map_errors(task->tk_status);
3469 return 0;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003470do_state_recovery:
3471 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3472 nfs4_schedule_state_recovery(clp);
3473 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3474 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3475 task->tk_status = 0;
3476 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477}
3478
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003479int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3480 unsigned short port, struct rpc_cred *cred,
3481 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482{
3483 nfs4_verifier sc_verifier;
3484 struct nfs4_setclientid setclientid = {
3485 .sc_verifier = &sc_verifier,
3486 .sc_prog = program,
3487 };
3488 struct rpc_message msg = {
3489 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3490 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003491 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003492 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 };
Al Virobc4785c2006-10-19 23:28:51 -07003494 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 int loop = 0;
3496 int status;
3497
Al Virobc4785c2006-10-19 23:28:51 -07003498 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3500 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3501
3502 for(;;) {
3503 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003504 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003505 clp->cl_ipaddr,
3506 rpc_peeraddr2str(clp->cl_rpcclient,
3507 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003508 rpc_peeraddr2str(clp->cl_rpcclient,
3509 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003510 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 clp->cl_id_uniquifier);
3512 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003513 sizeof(setclientid.sc_netid),
3514 rpc_peeraddr2str(clp->cl_rpcclient,
3515 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003517 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 clp->cl_ipaddr, port >> 8, port & 255);
3519
3520 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3521 if (status != -NFS4ERR_CLID_INUSE)
3522 break;
3523 if (signalled())
3524 break;
3525 if (loop++ & 1)
J. Bruce Fields611c96c2010-12-13 19:05:46 -05003526 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 else
3528 if (++clp->cl_id_uniquifier == 0)
3529 break;
3530 }
3531 return status;
3532}
3533
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003534static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3535 struct nfs4_setclientid_res *arg,
3536 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537{
3538 struct nfs_fsinfo fsinfo;
3539 struct rpc_message msg = {
3540 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003541 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003543 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 };
3545 unsigned long now;
3546 int status;
3547
3548 now = jiffies;
3549 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3550 if (status == 0) {
3551 spin_lock(&clp->cl_lock);
3552 clp->cl_lease_time = fsinfo.lease_time * HZ;
3553 clp->cl_last_renewal = now;
3554 spin_unlock(&clp->cl_lock);
3555 }
3556 return status;
3557}
3558
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003559int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3560 struct nfs4_setclientid_res *arg,
3561 struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003562{
Benny Halevy77e03672008-06-24 20:25:57 +03003563 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003564 int err;
3565 do {
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003566 err = _nfs4_proc_setclientid_confirm(clp, arg, cred);
Trond Myklebust51581f32006-03-20 13:44:47 -05003567 switch (err) {
3568 case 0:
3569 return err;
3570 case -NFS4ERR_RESOURCE:
3571 /* The IBM lawyers misread another document! */
3572 case -NFS4ERR_DELAY:
3573 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3574 }
3575 } while (err == 0);
3576 return err;
3577}
3578
Trond Myklebustfe650402006-01-03 09:55:18 +01003579struct nfs4_delegreturndata {
3580 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003581 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003582 struct nfs_fh fh;
3583 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003584 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003585 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003586 int rpc_status;
3587};
3588
Trond Myklebustfe650402006-01-03 09:55:18 +01003589static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3590{
3591 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003592
Trond Myklebust14516c32010-07-31 14:29:06 -04003593 if (!nfs4_sequence_done(task, &data->res.seq_res))
3594 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003595
Ricardo Labiaga79708862009-12-07 09:23:21 -05003596 switch (task->tk_status) {
3597 case -NFS4ERR_STALE_STATEID:
3598 case -NFS4ERR_EXPIRED:
3599 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003600 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003601 break;
3602 default:
3603 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3604 -EAGAIN) {
3605 nfs_restart_rpc(task, data->res.server->nfs_client);
3606 return;
3607 }
3608 }
3609 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003610}
3611
3612static void nfs4_delegreturn_release(void *calldata)
3613{
Trond Myklebustfe650402006-01-03 09:55:18 +01003614 kfree(calldata);
3615}
3616
Andy Adamson938e1012009-04-01 09:22:28 -04003617#if defined(CONFIG_NFS_V4_1)
3618static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3619{
3620 struct nfs4_delegreturndata *d_data;
3621
3622 d_data = (struct nfs4_delegreturndata *)data;
3623
Trond Myklebust035168ab2010-06-16 09:52:26 -04003624 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003625 &d_data->args.seq_args,
3626 &d_data->res.seq_res, 1, task))
3627 return;
3628 rpc_call_start(task);
3629}
3630#endif /* CONFIG_NFS_V4_1 */
3631
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003632static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003633#if defined(CONFIG_NFS_V4_1)
3634 .rpc_call_prepare = nfs4_delegreturn_prepare,
3635#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003636 .rpc_call_done = nfs4_delegreturn_done,
3637 .rpc_release = nfs4_delegreturn_release,
3638};
3639
Trond Myklebuste6f81072008-01-24 18:14:34 -05003640static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003641{
3642 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003643 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003644 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003645 struct rpc_message msg = {
3646 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3647 .rpc_cred = cred,
3648 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003649 struct rpc_task_setup task_setup_data = {
3650 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003651 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003652 .callback_ops = &nfs4_delegreturn_ops,
3653 .flags = RPC_TASK_ASYNC,
3654 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003655 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003656
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003657 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003658 if (data == NULL)
3659 return -ENOMEM;
3660 data->args.fhandle = &data->fh;
3661 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003662 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003663 nfs_copy_fh(&data->fh, NFS_FH(inode));
3664 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003665 data->res.fattr = &data->fattr;
3666 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003667 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003668 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003669 data->rpc_status = 0;
3670
Trond Myklebustc970aa82007-07-14 15:39:59 -04003671 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05003672 msg.rpc_argp = &data->args;
3673 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003674 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003675 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003676 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003677 if (!issync)
3678 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003679 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003680 if (status != 0)
3681 goto out;
3682 status = data->rpc_status;
3683 if (status != 0)
3684 goto out;
3685 nfs_refresh_inode(inode, &data->fattr);
3686out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003687 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003688 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689}
3690
Trond Myklebuste6f81072008-01-24 18:14:34 -05003691int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692{
3693 struct nfs_server *server = NFS_SERVER(inode);
3694 struct nfs4_exception exception = { };
3695 int err;
3696 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003697 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 switch (err) {
3699 case -NFS4ERR_STALE_STATEID:
3700 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 case 0:
3702 return 0;
3703 }
3704 err = nfs4_handle_exception(server, err, &exception);
3705 } while (exception.retry);
3706 return err;
3707}
3708
3709#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3710#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3711
3712/*
3713 * sleep, with exponential backoff, and retry the LOCK operation.
3714 */
3715static unsigned long
3716nfs4_set_lock_task_retry(unsigned long timeout)
3717{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003718 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 timeout <<= 1;
3720 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3721 return NFS4_LOCK_MAXTIMEOUT;
3722 return timeout;
3723}
3724
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3726{
3727 struct inode *inode = state->inode;
3728 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003729 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003730 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003732 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003734 struct nfs_lockt_res res = {
3735 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 };
3737 struct rpc_message msg = {
3738 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3739 .rpc_argp = &arg,
3740 .rpc_resp = &res,
3741 .rpc_cred = state->owner->so_cred,
3742 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 struct nfs4_lock_state *lsp;
3744 int status;
3745
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003746 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003747 status = nfs4_set_lock_state(state, request);
3748 if (status != 0)
3749 goto out;
3750 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003751 arg.lock_owner.id = lsp->ls_id.id;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003752 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003753 switch (status) {
3754 case 0:
3755 request->fl_type = F_UNLCK;
3756 break;
3757 case -NFS4ERR_DENIED:
3758 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003760 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003761out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 return status;
3763}
3764
3765static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3766{
3767 struct nfs4_exception exception = { };
3768 int err;
3769
3770 do {
3771 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3772 _nfs4_proc_getlk(state, cmd, request),
3773 &exception);
3774 } while (exception.retry);
3775 return err;
3776}
3777
3778static int do_vfs_lock(struct file *file, struct file_lock *fl)
3779{
3780 int res = 0;
3781 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3782 case FL_POSIX:
3783 res = posix_lock_file_wait(file, fl);
3784 break;
3785 case FL_FLOCK:
3786 res = flock_lock_file_wait(file, fl);
3787 break;
3788 default:
3789 BUG();
3790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 return res;
3792}
3793
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003794struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003795 struct nfs_locku_args arg;
3796 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003797 struct nfs4_lock_state *lsp;
3798 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003799 struct file_lock fl;
3800 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003801 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003802};
3803
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003804static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3805 struct nfs_open_context *ctx,
3806 struct nfs4_lock_state *lsp,
3807 struct nfs_seqid *seqid)
3808{
3809 struct nfs4_unlockdata *p;
3810 struct inode *inode = lsp->ls_state->inode;
3811
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003812 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003813 if (p == NULL)
3814 return NULL;
3815 p->arg.fh = NFS_FH(inode);
3816 p->arg.fl = &p->fl;
3817 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003818 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003819 p->arg.stateid = &lsp->ls_stateid;
3820 p->lsp = lsp;
3821 atomic_inc(&lsp->ls_count);
3822 /* Ensure we don't close file until we're done freeing locks! */
3823 p->ctx = get_nfs_open_context(ctx);
3824 memcpy(&p->fl, fl, sizeof(p->fl));
3825 p->server = NFS_SERVER(inode);
3826 return p;
3827}
3828
Trond Myklebust06f814a2006-01-03 09:55:07 +01003829static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003830{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003831 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003832 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003833 nfs4_put_lock_state(calldata->lsp);
3834 put_nfs_open_context(calldata->ctx);
3835 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003836}
3837
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003838static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003839{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003840 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003841
Trond Myklebust14516c32010-07-31 14:29:06 -04003842 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3843 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003844 switch (task->tk_status) {
3845 case 0:
3846 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003847 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003848 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003849 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003850 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003851 case -NFS4ERR_BAD_STATEID:
3852 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003853 case -NFS4ERR_STALE_STATEID:
3854 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003855 break;
3856 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003857 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebust0110ee12009-12-07 09:00:24 -05003858 nfs_restart_rpc(task,
Trond Myklebustd61e6122009-12-05 19:32:19 -05003859 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003860 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003861}
3862
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003863static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003864{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003865 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003867 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003868 return;
3869 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003870 /* Note: exit _without_ running nfs4_locku_done */
3871 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003872 return;
3873 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003874 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04003875 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04003876 &calldata->arg.seq_args,
3877 &calldata->res.seq_res, 1, task))
3878 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003879 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880}
3881
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003882static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003883 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003884 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003885 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003886};
3887
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003888static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3889 struct nfs_open_context *ctx,
3890 struct nfs4_lock_state *lsp,
3891 struct nfs_seqid *seqid)
3892{
3893 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003894 struct rpc_message msg = {
3895 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3896 .rpc_cred = ctx->cred,
3897 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003898 struct rpc_task_setup task_setup_data = {
3899 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003900 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003901 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003902 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003903 .flags = RPC_TASK_ASYNC,
3904 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003905
Frank Filz137d6ac2007-07-09 15:32:29 -07003906 /* Ensure this is an unlock - when canceling a lock, the
3907 * canceled lock is passed in, and it won't be an unlock.
3908 */
3909 fl->fl_type = F_UNLCK;
3910
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003911 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3912 if (data == NULL) {
3913 nfs_free_seqid(seqid);
3914 return ERR_PTR(-ENOMEM);
3915 }
3916
Trond Myklebust1174dd12010-12-21 10:52:24 -05003917 msg.rpc_argp = &data->arg;
3918 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003919 task_setup_data.callback_data = data;
3920 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003921}
3922
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3924{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003925 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003926 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003927 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003928 struct rpc_task *task;
3929 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003930 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931
Trond Myklebust9b073572006-06-29 16:38:34 -04003932 status = nfs4_set_lock_state(state, request);
3933 /* Unlock _before_ we do the RPC call */
3934 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003935 down_read(&nfsi->rwsem);
3936 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3937 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003938 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003939 }
3940 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003941 if (status != 0)
3942 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003943 /* Is this a delegated lock? */
3944 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04003945 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003946 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003947 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04003948 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003949 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04003950 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003951 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003952 status = PTR_ERR(task);
3953 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04003954 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003955 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003956 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04003957out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003958 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003959 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960}
3961
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003962struct nfs4_lockdata {
3963 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003964 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003965 struct nfs4_lock_state *lsp;
3966 struct nfs_open_context *ctx;
3967 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003968 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003969 int rpc_status;
3970 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04003971 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003972};
3973
3974static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003975 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
3976 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003977{
3978 struct nfs4_lockdata *p;
3979 struct inode *inode = lsp->ls_state->inode;
3980 struct nfs_server *server = NFS_SERVER(inode);
3981
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003982 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003983 if (p == NULL)
3984 return NULL;
3985
3986 p->arg.fh = NFS_FH(inode);
3987 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003988 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003989 if (p->arg.open_seqid == NULL)
3990 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003991 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003992 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003993 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003994 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04003995 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003996 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003997 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003998 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04003999 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004000 atomic_inc(&lsp->ls_count);
4001 p->ctx = get_nfs_open_context(ctx);
4002 memcpy(&p->fl, fl, sizeof(p->fl));
4003 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004004out_free_seqid:
4005 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004006out_free:
4007 kfree(p);
4008 return NULL;
4009}
4010
4011static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4012{
4013 struct nfs4_lockdata *data = calldata;
4014 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015
Harvey Harrison3110ff82008-05-02 13:42:44 -07004016 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004017 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4018 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004019 /* Do we need to do an open_to_lock_owner? */
4020 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004021 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4022 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004023 data->arg.open_stateid = &state->stateid;
4024 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004025 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004026 } else
4027 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004028 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04004029 if (nfs4_setup_sequence(data->server,
4030 &data->arg.seq_args,
Andy Adamson66179ef2009-04-01 09:22:22 -04004031 &data->res.seq_res, 1, task))
4032 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004033 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004034 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004035}
4036
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004037static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4038{
4039 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4040 nfs4_lock_prepare(task, calldata);
4041}
4042
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004043static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4044{
4045 struct nfs4_lockdata *data = calldata;
4046
Harvey Harrison3110ff82008-05-02 13:42:44 -07004047 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004048
Trond Myklebust14516c32010-07-31 14:29:06 -04004049 if (!nfs4_sequence_done(task, &data->res.seq_res))
4050 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004051
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004052 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004053 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004054 if (data->rpc_status == 0)
4055 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4056 else
4057 goto out;
4058 }
4059 if (data->rpc_status == 0) {
4060 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4061 sizeof(data->lsp->ls_stateid.data));
4062 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04004063 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004064 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004065out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004066 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004067}
4068
4069static void nfs4_lock_release(void *calldata)
4070{
4071 struct nfs4_lockdata *data = calldata;
4072
Harvey Harrison3110ff82008-05-02 13:42:44 -07004073 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004074 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004075 if (data->cancelled != 0) {
4076 struct rpc_task *task;
4077 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4078 data->arg.lock_seqid);
4079 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004080 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004081 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004082 } else
4083 nfs_free_seqid(data->arg.lock_seqid);
4084 nfs4_put_lock_state(data->lsp);
4085 put_nfs_open_context(data->ctx);
4086 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004087 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004088}
4089
4090static const struct rpc_call_ops nfs4_lock_ops = {
4091 .rpc_call_prepare = nfs4_lock_prepare,
4092 .rpc_call_done = nfs4_lock_done,
4093 .rpc_release = nfs4_lock_release,
4094};
4095
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004096static const struct rpc_call_ops nfs4_recover_lock_ops = {
4097 .rpc_call_prepare = nfs4_recover_lock_prepare,
4098 .rpc_call_done = nfs4_lock_done,
4099 .rpc_release = nfs4_lock_release,
4100};
4101
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004102static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4103{
4104 struct nfs_client *clp = server->nfs_client;
4105 struct nfs4_state *state = lsp->ls_state;
4106
4107 switch (error) {
4108 case -NFS4ERR_ADMIN_REVOKED:
4109 case -NFS4ERR_BAD_STATEID:
4110 case -NFS4ERR_EXPIRED:
4111 if (new_lock_owner != 0 ||
4112 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4113 nfs4_state_mark_reclaim_nograce(clp, state);
4114 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004115 break;
4116 case -NFS4ERR_STALE_STATEID:
4117 if (new_lock_owner != 0 ||
4118 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4119 nfs4_state_mark_reclaim_reboot(clp, state);
4120 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004121 };
4122}
4123
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004124static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004125{
4126 struct nfs4_lockdata *data;
4127 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004128 struct rpc_message msg = {
4129 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4130 .rpc_cred = state->owner->so_cred,
4131 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004132 struct rpc_task_setup task_setup_data = {
4133 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004134 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004135 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004136 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004137 .flags = RPC_TASK_ASYNC,
4138 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004139 int ret;
4140
Harvey Harrison3110ff82008-05-02 13:42:44 -07004141 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004142 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004143 fl->fl_u.nfs4_fl.owner,
4144 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004145 if (data == NULL)
4146 return -ENOMEM;
4147 if (IS_SETLKW(cmd))
4148 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004149 if (recovery_type > NFS_LOCK_NEW) {
4150 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004151 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004152 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4153 }
Trond Myklebust1174dd12010-12-21 10:52:24 -05004154 msg.rpc_argp = &data->arg;
4155 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004156 task_setup_data.callback_data = data;
4157 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004158 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004159 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004160 ret = nfs4_wait_for_completion_rpc_task(task);
4161 if (ret == 0) {
4162 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004163 if (ret)
4164 nfs4_handle_setlk_error(data->server, data->lsp,
4165 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004166 } else
4167 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004168 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004169 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004170 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171}
4172
4173static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4174{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004175 struct nfs_server *server = NFS_SERVER(state->inode);
4176 struct nfs4_exception exception = { };
4177 int err;
4178
4179 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004180 /* Cache the lock if possible... */
4181 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4182 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004183 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004184 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004185 break;
4186 nfs4_handle_exception(server, err, &exception);
4187 } while (exception.retry);
4188 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189}
4190
4191static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4192{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004193 struct nfs_server *server = NFS_SERVER(state->inode);
4194 struct nfs4_exception exception = { };
4195 int err;
4196
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004197 err = nfs4_set_lock_state(state, request);
4198 if (err != 0)
4199 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004200 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004201 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4202 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004203 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004204 switch (err) {
4205 default:
4206 goto out;
4207 case -NFS4ERR_GRACE:
4208 case -NFS4ERR_DELAY:
4209 nfs4_handle_exception(server, err, &exception);
4210 err = 0;
4211 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004212 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004213out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004214 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215}
4216
4217static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4218{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004219 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004220 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004221 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004223 if ((fl_flags & FL_POSIX) &&
4224 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4225 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004226 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004227 status = nfs4_set_lock_state(state, request);
4228 if (status != 0)
4229 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004230 request->fl_flags |= FL_ACCESS;
4231 status = do_vfs_lock(request->fl_file, request);
4232 if (status < 0)
4233 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004234 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004235 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004236 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004237 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004238 request->fl_flags = fl_flags & ~FL_SLEEP;
4239 status = do_vfs_lock(request->fl_file, request);
4240 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004241 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004242 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004243 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004244 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004245 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004246 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004247 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004248 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004249out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004250 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004251out:
4252 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 return status;
4254}
4255
4256static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4257{
4258 struct nfs4_exception exception = { };
4259 int err;
4260
4261 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004262 err = _nfs4_proc_setlk(state, cmd, request);
4263 if (err == -NFS4ERR_DENIED)
4264 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004266 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267 } while (exception.retry);
4268 return err;
4269}
4270
4271static int
4272nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4273{
4274 struct nfs_open_context *ctx;
4275 struct nfs4_state *state;
4276 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4277 int status;
4278
4279 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004280 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 state = ctx->state;
4282
4283 if (request->fl_start < 0 || request->fl_end < 0)
4284 return -EINVAL;
4285
Trond Myklebustd9531262009-07-21 19:22:38 -04004286 if (IS_GETLK(cmd)) {
4287 if (state != NULL)
4288 return nfs4_proc_getlk(state, F_GETLK, request);
4289 return 0;
4290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291
4292 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4293 return -EINVAL;
4294
Trond Myklebustd9531262009-07-21 19:22:38 -04004295 if (request->fl_type == F_UNLCK) {
4296 if (state != NULL)
4297 return nfs4_proc_unlck(state, cmd, request);
4298 return 0;
4299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300
Trond Myklebustd9531262009-07-21 19:22:38 -04004301 if (state == NULL)
4302 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 do {
4304 status = nfs4_proc_setlk(state, cmd, request);
4305 if ((status != -EAGAIN) || IS_SETLK(cmd))
4306 break;
4307 timeout = nfs4_set_lock_task_retry(timeout);
4308 status = -ERESTARTSYS;
4309 if (signalled())
4310 break;
4311 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 return status;
4313}
4314
Trond Myklebust888e6942005-11-04 15:38:11 -05004315int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4316{
4317 struct nfs_server *server = NFS_SERVER(state->inode);
4318 struct nfs4_exception exception = { };
4319 int err;
4320
4321 err = nfs4_set_lock_state(state, fl);
4322 if (err != 0)
4323 goto out;
4324 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004325 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004326 switch (err) {
4327 default:
4328 printk(KERN_ERR "%s: unhandled error %d.\n",
4329 __func__, err);
4330 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004331 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004332 goto out;
4333 case -NFS4ERR_EXPIRED:
4334 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004335 case -NFS4ERR_STALE_STATEID:
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004336 case -NFS4ERR_BADSESSION:
4337 case -NFS4ERR_BADSLOT:
4338 case -NFS4ERR_BAD_HIGH_SLOT:
4339 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4340 case -NFS4ERR_DEADSESSION:
Trond Myklebustd5122202009-06-17 13:22:58 -07004341 nfs4_schedule_state_recovery(server->nfs_client);
4342 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004343 case -ERESTARTSYS:
4344 /*
4345 * The show must go on: exit, but mark the
4346 * stateid as needing recovery.
4347 */
4348 case -NFS4ERR_ADMIN_REVOKED:
4349 case -NFS4ERR_BAD_STATEID:
4350 case -NFS4ERR_OPENMODE:
4351 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4352 err = 0;
4353 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004354 case -EKEYEXPIRED:
4355 /*
4356 * User RPCSEC_GSS context has expired.
4357 * We cannot recover this stateid now, so
4358 * skip it and allow recovery thread to
4359 * proceed.
4360 */
4361 err = 0;
4362 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004363 case -ENOMEM:
4364 case -NFS4ERR_DENIED:
4365 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4366 err = 0;
4367 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004368 case -NFS4ERR_DELAY:
4369 break;
4370 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004371 err = nfs4_handle_exception(server, err, &exception);
4372 } while (exception.retry);
4373out:
4374 return err;
4375}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004376
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004377static void nfs4_release_lockowner_release(void *calldata)
4378{
4379 kfree(calldata);
4380}
4381
4382const struct rpc_call_ops nfs4_release_lockowner_ops = {
4383 .rpc_release = nfs4_release_lockowner_release,
4384};
4385
4386void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4387{
4388 struct nfs_server *server = lsp->ls_state->owner->so_server;
4389 struct nfs_release_lockowner_args *args;
4390 struct rpc_message msg = {
4391 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4392 };
4393
4394 if (server->nfs_client->cl_mvops->minor_version != 0)
4395 return;
4396 args = kmalloc(sizeof(*args), GFP_NOFS);
4397 if (!args)
4398 return;
4399 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4400 args->lock_owner.id = lsp->ls_id.id;
4401 msg.rpc_argp = args;
4402 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4403}
4404
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004405#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4406
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004407static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4408 const void *buf, size_t buflen,
4409 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004410{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004411 if (strcmp(key, "") != 0)
4412 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004413
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004414 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004415}
4416
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004417static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4418 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004419{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004420 if (strcmp(key, "") != 0)
4421 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004422
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004423 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004424}
4425
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004426static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4427 size_t list_len, const char *name,
4428 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004429{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004430 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004431
J. Bruce Fields096455a2006-03-20 23:23:42 -05004432 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4433 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004434
4435 if (list && len <= list_len)
4436 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004437 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004438}
4439
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004440static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4441{
4442 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4443 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4444 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4445 return;
4446
4447 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4448 NFS_ATTR_FATTR_NLINK;
4449 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4450 fattr->nlink = 2;
4451}
4452
Trond Myklebust56659e92007-07-17 21:52:39 -04004453int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004454 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004455{
4456 struct nfs_server *server = NFS_SERVER(dir);
4457 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004458 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4459 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004460 };
4461 struct nfs4_fs_locations_arg args = {
4462 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004463 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004464 .page = page,
4465 .bitmask = bitmask,
4466 };
Benny Halevy22958462009-04-01 09:22:02 -04004467 struct nfs4_fs_locations_res res = {
4468 .fs_locations = fs_locations,
4469 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004470 struct rpc_message msg = {
4471 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4472 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004473 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004474 };
4475 int status;
4476
Harvey Harrison3110ff82008-05-02 13:42:44 -07004477 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004478 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004479 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004480 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04004481 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004482 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004483 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004484 return status;
4485}
4486
Andy Adamson557134a2009-04-01 09:21:53 -04004487#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004488/*
4489 * nfs4_proc_exchange_id()
4490 *
4491 * Since the clientid has expired, all compounds using sessions
4492 * associated with the stale clientid will be returning
4493 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4494 * be in some phase of session reset.
4495 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004496int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004497{
4498 nfs4_verifier verifier;
4499 struct nfs41_exchange_id_args args = {
4500 .client = clp,
4501 .flags = clp->cl_exchange_flags,
4502 };
4503 struct nfs41_exchange_id_res res = {
4504 .client = clp,
4505 };
4506 int status;
4507 struct rpc_message msg = {
4508 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4509 .rpc_argp = &args,
4510 .rpc_resp = &res,
4511 .rpc_cred = cred,
4512 };
4513 __be32 *p;
4514
4515 dprintk("--> %s\n", __func__);
4516 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004517
Alexandros Batsakis7b183d02009-12-05 13:33:25 -05004518 /* Remove server-only flags */
4519 args.flags &= ~EXCHGID4_FLAG_CONFIRMED_R;
4520
Benny Halevy99fe60d2009-04-01 09:22:29 -04004521 p = (u32 *)verifier.data;
4522 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4523 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4524 args.verifier = &verifier;
4525
4526 while (1) {
4527 args.id_len = scnprintf(args.id, sizeof(args.id),
4528 "%s/%s %u",
4529 clp->cl_ipaddr,
4530 rpc_peeraddr2str(clp->cl_rpcclient,
4531 RPC_DISPLAY_ADDR),
4532 clp->cl_id_uniquifier);
4533
4534 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4535
Andy Adamson180b62a2010-03-02 13:19:36 -05004536 if (status != -NFS4ERR_CLID_INUSE)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004537 break;
4538
4539 if (signalled())
4540 break;
4541
4542 if (++clp->cl_id_uniquifier == 0)
4543 break;
4544 }
4545
4546 dprintk("<-- %s status= %d\n", __func__, status);
4547 return status;
4548}
4549
Andy Adamson2050f0c2009-04-01 09:22:30 -04004550struct nfs4_get_lease_time_data {
4551 struct nfs4_get_lease_time_args *args;
4552 struct nfs4_get_lease_time_res *res;
4553 struct nfs_client *clp;
4554};
4555
4556static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4557 void *calldata)
4558{
4559 int ret;
4560 struct nfs4_get_lease_time_data *data =
4561 (struct nfs4_get_lease_time_data *)calldata;
4562
4563 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004564 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004565 /* just setup sequence, do not trigger session recovery
4566 since we're invoked within one */
4567 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004568 &data->args->la_seq_args,
4569 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004570
4571 BUG_ON(ret == -EAGAIN);
4572 rpc_call_start(task);
4573 dprintk("<-- %s\n", __func__);
4574}
4575
4576/*
4577 * Called from nfs4_state_manager thread for session setup, so don't recover
4578 * from sequence operation or clientid errors.
4579 */
4580static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4581{
4582 struct nfs4_get_lease_time_data *data =
4583 (struct nfs4_get_lease_time_data *)calldata;
4584
4585 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04004586 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4587 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04004588 switch (task->tk_status) {
4589 case -NFS4ERR_DELAY:
4590 case -NFS4ERR_GRACE:
4591 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4592 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4593 task->tk_status = 0;
Trond Myklebust0110ee12009-12-07 09:00:24 -05004594 nfs_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004595 return;
4596 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004597 dprintk("<-- %s\n", __func__);
4598}
4599
4600struct rpc_call_ops nfs4_get_lease_time_ops = {
4601 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4602 .rpc_call_done = nfs4_get_lease_time_done,
4603};
4604
4605int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4606{
4607 struct rpc_task *task;
4608 struct nfs4_get_lease_time_args args;
4609 struct nfs4_get_lease_time_res res = {
4610 .lr_fsinfo = fsinfo,
4611 };
4612 struct nfs4_get_lease_time_data data = {
4613 .args = &args,
4614 .res = &res,
4615 .clp = clp,
4616 };
4617 struct rpc_message msg = {
4618 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4619 .rpc_argp = &args,
4620 .rpc_resp = &res,
4621 };
4622 struct rpc_task_setup task_setup = {
4623 .rpc_client = clp->cl_rpcclient,
4624 .rpc_message = &msg,
4625 .callback_ops = &nfs4_get_lease_time_ops,
4626 .callback_data = &data
4627 };
4628 int status;
4629
Andy Adamson2050f0c2009-04-01 09:22:30 -04004630 dprintk("--> %s\n", __func__);
4631 task = rpc_run_task(&task_setup);
4632
4633 if (IS_ERR(task))
4634 status = PTR_ERR(task);
4635 else {
4636 status = task->tk_status;
4637 rpc_put_task(task);
4638 }
4639 dprintk("<-- %s return %d\n", __func__, status);
4640
4641 return status;
4642}
4643
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004644/*
4645 * Reset a slot table
4646 */
Andy Adamson104aeba2010-01-14 17:45:10 -05004647static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4648 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004649{
Andy Adamson104aeba2010-01-14 17:45:10 -05004650 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004651 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004652 int ret = 0;
4653
Andy Adamson104aeba2010-01-14 17:45:10 -05004654 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4655 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004656
Andy Adamson104aeba2010-01-14 17:45:10 -05004657 /* Does the newly negotiated max_reqs match the existing slot table? */
4658 if (max_reqs != tbl->max_slots) {
4659 ret = -ENOMEM;
4660 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004661 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05004662 if (!new)
4663 goto out;
4664 ret = 0;
4665 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004666 }
4667 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05004668 if (new) {
4669 tbl->slots = new;
4670 tbl->max_slots = max_reqs;
4671 }
4672 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004673 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004674 spin_unlock(&tbl->slot_tbl_lock);
4675 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4676 tbl, tbl->slots, tbl->max_slots);
4677out:
4678 dprintk("<-- %s: return %d\n", __func__, ret);
4679 return ret;
4680}
4681
Andy Adamsonfc931582009-04-01 09:22:31 -04004682/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004683 * Reset the forechannel and backchannel slot tables
4684 */
4685static int nfs4_reset_slot_tables(struct nfs4_session *session)
4686{
4687 int status;
4688
4689 status = nfs4_reset_slot_table(&session->fc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004690 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004691 if (status)
4692 return status;
4693
4694 status = nfs4_reset_slot_table(&session->bc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004695 session->bc_attrs.max_reqs, 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004696 return status;
4697}
4698
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004699/* Destroy the slot table */
4700static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4701{
4702 if (session->fc_slot_table.slots != NULL) {
4703 kfree(session->fc_slot_table.slots);
4704 session->fc_slot_table.slots = NULL;
4705 }
4706 if (session->bc_slot_table.slots != NULL) {
4707 kfree(session->bc_slot_table.slots);
4708 session->bc_slot_table.slots = NULL;
4709 }
4710 return;
4711}
4712
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004713/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004714 * Initialize slot table
4715 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004716static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4717 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04004718{
Andy Adamsonfc931582009-04-01 09:22:31 -04004719 struct nfs4_slot *slot;
4720 int ret = -ENOMEM;
4721
4722 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4723
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004724 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04004725
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004726 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04004727 if (!slot)
4728 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04004729 ret = 0;
4730
4731 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04004732 tbl->max_slots = max_slots;
4733 tbl->slots = slot;
4734 tbl->highest_used_slotid = -1; /* no slot is currently used */
4735 spin_unlock(&tbl->slot_tbl_lock);
4736 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4737 tbl, tbl->slots, tbl->max_slots);
4738out:
4739 dprintk("<-- %s: return %d\n", __func__, ret);
4740 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04004741}
4742
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004743/*
4744 * Initialize the forechannel and backchannel tables
4745 */
4746static int nfs4_init_slot_tables(struct nfs4_session *session)
4747{
Trond Myklebustf26468f2009-12-05 19:32:11 -05004748 struct nfs4_slot_table *tbl;
4749 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004750
Trond Myklebustf26468f2009-12-05 19:32:11 -05004751 tbl = &session->fc_slot_table;
4752 if (tbl->slots == NULL) {
4753 status = nfs4_init_slot_table(tbl,
4754 session->fc_attrs.max_reqs, 1);
4755 if (status)
4756 return status;
4757 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004758
Trond Myklebustf26468f2009-12-05 19:32:11 -05004759 tbl = &session->bc_slot_table;
4760 if (tbl->slots == NULL) {
4761 status = nfs4_init_slot_table(tbl,
4762 session->bc_attrs.max_reqs, 0);
4763 if (status)
4764 nfs4_destroy_slot_tables(session);
4765 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004766
4767 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04004768}
4769
4770struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4771{
4772 struct nfs4_session *session;
4773 struct nfs4_slot_table *tbl;
4774
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004775 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04004776 if (!session)
4777 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04004778
Andy Adamsonea028ac2009-12-04 15:55:38 -05004779 init_completion(&session->complete);
Andy Adamson76db6d92009-04-01 09:22:38 -04004780
Andy Adamson557134a2009-04-01 09:21:53 -04004781 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004782 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04004783 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004784 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004785
4786 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004787 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004788 spin_lock_init(&tbl->slot_tbl_lock);
4789 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4790
Trond Myklebust1055d762010-06-16 09:52:27 -04004791 session->session_state = 1<<NFS4_SESSION_INITING;
4792
Andy Adamson557134a2009-04-01 09:21:53 -04004793 session->clp = clp;
4794 return session;
4795}
4796
4797void nfs4_destroy_session(struct nfs4_session *session)
4798{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04004799 nfs4_proc_destroy_session(session);
4800 dprintk("%s Destroy backchannel for xprt %p\n",
4801 __func__, session->clp->cl_rpcclient->cl_xprt);
4802 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4803 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004804 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04004805 kfree(session);
4806}
4807
Andy Adamsonfc931582009-04-01 09:22:31 -04004808/*
4809 * Initialize the values to be used by the client in CREATE_SESSION
4810 * If nfs4_init_session set the fore channel request and response sizes,
4811 * use them.
4812 *
4813 * Set the back channel max_resp_sz_cached to zero to force the client to
4814 * always set csa_cachethis to FALSE because the current implementation
4815 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4816 */
4817static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4818{
4819 struct nfs4_session *session = args->client->cl_session;
4820 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4821 mxresp_sz = session->fc_attrs.max_resp_sz;
4822
4823 if (mxrqst_sz == 0)
4824 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4825 if (mxresp_sz == 0)
4826 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4827 /* Fore channel attributes */
4828 args->fc_attrs.headerpadsz = 0;
4829 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4830 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04004831 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4832 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4833
4834 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05004835 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04004836 __func__,
4837 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05004838 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04004839
4840 /* Back channel attributes */
4841 args->bc_attrs.headerpadsz = 0;
4842 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4843 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4844 args->bc_attrs.max_resp_sz_cached = 0;
4845 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4846 args->bc_attrs.max_reqs = 1;
4847
4848 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4849 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4850 __func__,
4851 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4852 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4853 args->bc_attrs.max_reqs);
4854}
4855
J. Bruce Fields43c2e882010-10-02 15:19:01 -04004856static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04004857{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04004858 struct nfs4_channel_attrs *sent = &args->fc_attrs;
4859 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
4860
4861 if (rcvd->headerpadsz > sent->headerpadsz)
4862 return -EINVAL;
4863 if (rcvd->max_resp_sz > sent->max_resp_sz)
4864 return -EINVAL;
4865 /*
4866 * Our requested max_ops is the minimum we need; we're not
4867 * prepared to break up compounds into smaller pieces than that.
4868 * So, no point even trying to continue if the server won't
4869 * cooperate:
4870 */
4871 if (rcvd->max_ops < sent->max_ops)
4872 return -EINVAL;
4873 if (rcvd->max_reqs == 0)
4874 return -EINVAL;
4875 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04004876}
4877
J. Bruce Fields43c2e882010-10-02 15:19:01 -04004878static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
4879{
4880 struct nfs4_channel_attrs *sent = &args->bc_attrs;
4881 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04004882
J. Bruce Fields43c2e882010-10-02 15:19:01 -04004883 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
4884 return -EINVAL;
4885 if (rcvd->max_resp_sz < sent->max_resp_sz)
4886 return -EINVAL;
4887 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
4888 return -EINVAL;
4889 /* These would render the backchannel useless: */
4890 if (rcvd->max_ops == 0)
4891 return -EINVAL;
4892 if (rcvd->max_reqs == 0)
4893 return -EINVAL;
4894 return 0;
4895}
Andy Adamson8d353012009-04-01 09:22:32 -04004896
Andy Adamson8d353012009-04-01 09:22:32 -04004897static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4898 struct nfs4_session *session)
4899{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04004900 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04004901
J. Bruce Fields43c2e882010-10-02 15:19:01 -04004902 ret = nfs4_verify_fore_channel_attrs(args, session);
4903 if (ret)
4904 return ret;
4905 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04004906}
4907
Andy Adamsonfc931582009-04-01 09:22:31 -04004908static int _nfs4_proc_create_session(struct nfs_client *clp)
4909{
4910 struct nfs4_session *session = clp->cl_session;
4911 struct nfs41_create_session_args args = {
4912 .client = clp,
4913 .cb_program = NFS4_CALLBACK,
4914 };
4915 struct nfs41_create_session_res res = {
4916 .client = clp,
4917 };
4918 struct rpc_message msg = {
4919 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4920 .rpc_argp = &args,
4921 .rpc_resp = &res,
4922 };
4923 int status;
4924
4925 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04004926 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04004927
4928 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4929
Andy Adamson8d353012009-04-01 09:22:32 -04004930 if (!status)
4931 /* Verify the session's negotiated channel_attrs values */
4932 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04004933 if (!status) {
4934 /* Increment the clientid slot sequence id */
4935 clp->cl_seqid++;
4936 }
4937
4938 return status;
4939}
4940
4941/*
4942 * Issues a CREATE_SESSION operation to the server.
4943 * It is the responsibility of the caller to verify the session is
4944 * expired before calling this routine.
4945 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05004946int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04004947{
4948 int status;
4949 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04004950 struct nfs4_session *session = clp->cl_session;
4951
4952 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
4953
4954 status = _nfs4_proc_create_session(clp);
4955 if (status)
4956 goto out;
4957
Trond Myklebustf26468f2009-12-05 19:32:11 -05004958 /* Init and reset the fore channel */
4959 status = nfs4_init_slot_tables(session);
4960 dprintk("slot table initialization returned %d\n", status);
4961 if (status)
4962 goto out;
4963 status = nfs4_reset_slot_tables(session);
4964 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04004965 if (status)
4966 goto out;
4967
4968 ptr = (unsigned *)&session->sess_id.data[0];
4969 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
4970 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04004971out:
4972 dprintk("<-- %s\n", __func__);
4973 return status;
4974}
4975
Andy Adamson0f3e66c2009-04-01 09:22:34 -04004976/*
4977 * Issue the over-the-wire RPC DESTROY_SESSION.
4978 * The caller must serialize access to this routine.
4979 */
4980int nfs4_proc_destroy_session(struct nfs4_session *session)
4981{
4982 int status = 0;
4983 struct rpc_message msg;
4984
4985 dprintk("--> nfs4_proc_destroy_session\n");
4986
4987 /* session is still being setup */
4988 if (session->clp->cl_cons_state != NFS_CS_READY)
4989 return status;
4990
4991 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
4992 msg.rpc_argp = session;
4993 msg.rpc_resp = NULL;
4994 msg.rpc_cred = NULL;
4995 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4996
4997 if (status)
4998 printk(KERN_WARNING
4999 "Got error %d from the server on DESTROY_SESSION. "
5000 "Session has been destroyed regardless...\n", status);
5001
5002 dprintk("<-- nfs4_proc_destroy_session\n");
5003 return status;
5004}
5005
Trond Myklebustfccba802009-07-21 16:48:07 -04005006int nfs4_init_session(struct nfs_server *server)
5007{
5008 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005009 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005010 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005011 int ret;
5012
5013 if (!nfs4_has_session(clp))
5014 return 0;
5015
Trond Myklebust1055d762010-06-16 09:52:27 -04005016 session = clp->cl_session;
5017 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5018 return 0;
5019
Andy Adamson68bf05e2009-12-15 12:55:02 -05005020 rsize = server->rsize;
5021 if (rsize == 0)
5022 rsize = NFS_MAX_FILE_IO_SIZE;
5023 wsize = server->wsize;
5024 if (wsize == 0)
5025 wsize = NFS_MAX_FILE_IO_SIZE;
5026
Andy Adamson68bf05e2009-12-15 12:55:02 -05005027 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5028 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005029
Trond Myklebustfccba802009-07-21 16:48:07 -04005030 ret = nfs4_recover_expired_lease(server);
5031 if (!ret)
5032 ret = nfs4_check_client_ready(clp);
5033 return ret;
5034}
5035
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005036/*
5037 * Renew the cl_session lease.
5038 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005039struct nfs4_sequence_data {
5040 struct nfs_client *clp;
5041 struct nfs4_sequence_args args;
5042 struct nfs4_sequence_res res;
5043};
5044
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005045static void nfs41_sequence_release(void *data)
5046{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005047 struct nfs4_sequence_data *calldata = data;
5048 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005049
Alexandros Batsakis71358402010-02-05 03:45:05 -08005050 if (atomic_read(&clp->cl_count) > 1)
5051 nfs4_schedule_state_renewal(clp);
5052 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005053 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005054}
5055
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005056static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5057{
5058 switch(task->tk_status) {
5059 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005060 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5061 return -EAGAIN;
5062 default:
5063 nfs4_schedule_state_recovery(clp);
5064 }
5065 return 0;
5066}
5067
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005068static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005069{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005070 struct nfs4_sequence_data *calldata = data;
5071 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005072
Trond Myklebust14516c32010-07-31 14:29:06 -04005073 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5074 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005075
5076 if (task->tk_status < 0) {
5077 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005078 if (atomic_read(&clp->cl_count) == 1)
5079 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005080
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005081 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5082 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005083 return;
5084 }
5085 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005086 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005087out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005088 dprintk("<-- %s\n", __func__);
5089}
5090
5091static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5092{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005093 struct nfs4_sequence_data *calldata = data;
5094 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005095 struct nfs4_sequence_args *args;
5096 struct nfs4_sequence_res *res;
5097
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005098 args = task->tk_msg.rpc_argp;
5099 res = task->tk_msg.rpc_resp;
5100
Trond Myklebust035168ab2010-06-16 09:52:26 -04005101 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005102 return;
5103 rpc_call_start(task);
5104}
5105
5106static const struct rpc_call_ops nfs41_sequence_ops = {
5107 .rpc_call_done = nfs41_sequence_call_done,
5108 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005109 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005110};
5111
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005112static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005113{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005114 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005115 struct rpc_message msg = {
5116 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5117 .rpc_cred = cred,
5118 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005119 struct rpc_task_setup task_setup_data = {
5120 .rpc_client = clp->cl_rpcclient,
5121 .rpc_message = &msg,
5122 .callback_ops = &nfs41_sequence_ops,
5123 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5124 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005125
Alexandros Batsakis71358402010-02-05 03:45:05 -08005126 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005127 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005128 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005129 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005130 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005131 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005132 }
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005133 msg.rpc_argp = &calldata->args;
5134 msg.rpc_resp = &calldata->res;
5135 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005136 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005137
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005138 return rpc_run_task(&task_setup_data);
5139}
5140
5141static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5142{
5143 struct rpc_task *task;
5144 int ret = 0;
5145
5146 task = _nfs41_proc_sequence(clp, cred);
5147 if (IS_ERR(task))
5148 ret = PTR_ERR(task);
5149 else
5150 rpc_put_task(task);
5151 dprintk("<-- %s status=%d\n", __func__, ret);
5152 return ret;
5153}
5154
5155static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5156{
5157 struct rpc_task *task;
5158 int ret;
5159
5160 task = _nfs41_proc_sequence(clp, cred);
5161 if (IS_ERR(task)) {
5162 ret = PTR_ERR(task);
5163 goto out;
5164 }
5165 ret = rpc_wait_for_completion_task(task);
5166 if (!ret)
5167 ret = task->tk_status;
5168 rpc_put_task(task);
5169out:
5170 dprintk("<-- %s status=%d\n", __func__, ret);
5171 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005172}
5173
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005174struct nfs4_reclaim_complete_data {
5175 struct nfs_client *clp;
5176 struct nfs41_reclaim_complete_args arg;
5177 struct nfs41_reclaim_complete_res res;
5178};
5179
5180static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5181{
5182 struct nfs4_reclaim_complete_data *calldata = data;
5183
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005184 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168ab2010-06-16 09:52:26 -04005185 if (nfs41_setup_sequence(calldata->clp->cl_session,
5186 &calldata->arg.seq_args,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005187 &calldata->res.seq_res, 0, task))
5188 return;
5189
5190 rpc_call_start(task);
5191}
5192
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005193static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5194{
5195 switch(task->tk_status) {
5196 case 0:
5197 case -NFS4ERR_COMPLETE_ALREADY:
5198 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5199 break;
5200 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005201 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5202 return -EAGAIN;
5203 default:
5204 nfs4_schedule_state_recovery(clp);
5205 }
5206 return 0;
5207}
5208
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005209static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5210{
5211 struct nfs4_reclaim_complete_data *calldata = data;
5212 struct nfs_client *clp = calldata->clp;
5213 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5214
5215 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005216 if (!nfs41_sequence_done(task, res))
5217 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005218
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005219 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5220 rpc_restart_call_prepare(task);
5221 return;
5222 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005223 dprintk("<-- %s\n", __func__);
5224}
5225
5226static void nfs4_free_reclaim_complete_data(void *data)
5227{
5228 struct nfs4_reclaim_complete_data *calldata = data;
5229
5230 kfree(calldata);
5231}
5232
5233static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5234 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5235 .rpc_call_done = nfs4_reclaim_complete_done,
5236 .rpc_release = nfs4_free_reclaim_complete_data,
5237};
5238
5239/*
5240 * Issue a global reclaim complete.
5241 */
5242static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5243{
5244 struct nfs4_reclaim_complete_data *calldata;
5245 struct rpc_task *task;
5246 struct rpc_message msg = {
5247 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5248 };
5249 struct rpc_task_setup task_setup_data = {
5250 .rpc_client = clp->cl_rpcclient,
5251 .rpc_message = &msg,
5252 .callback_ops = &nfs4_reclaim_complete_call_ops,
5253 .flags = RPC_TASK_ASYNC,
5254 };
5255 int status = -ENOMEM;
5256
5257 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005258 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005259 if (calldata == NULL)
5260 goto out;
5261 calldata->clp = clp;
5262 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005263
5264 msg.rpc_argp = &calldata->arg;
5265 msg.rpc_resp = &calldata->res;
5266 task_setup_data.callback_data = calldata;
5267 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005268 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005269 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005270 goto out;
5271 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005272 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005273 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005274out:
5275 dprintk("<-- %s status=%d\n", __func__, status);
5276 return status;
5277}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005278
5279static void
5280nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5281{
5282 struct nfs4_layoutget *lgp = calldata;
5283 struct inode *ino = lgp->args.inode;
5284 struct nfs_server *server = NFS_SERVER(ino);
5285
5286 dprintk("--> %s\n", __func__);
5287 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5288 &lgp->res.seq_res, 0, task))
5289 return;
5290 rpc_call_start(task);
5291}
5292
5293static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5294{
5295 struct nfs4_layoutget *lgp = calldata;
5296 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5297
5298 dprintk("--> %s\n", __func__);
5299
5300 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5301 return;
5302
5303 switch (task->tk_status) {
5304 case 0:
5305 break;
5306 case -NFS4ERR_LAYOUTTRYLATER:
5307 case -NFS4ERR_RECALLCONFLICT:
5308 task->tk_status = -NFS4ERR_DELAY;
5309 /* Fall through */
5310 default:
5311 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5312 rpc_restart_call_prepare(task);
5313 return;
5314 }
5315 }
5316 lgp->status = task->tk_status;
5317 dprintk("<-- %s\n", __func__);
5318}
5319
5320static void nfs4_layoutget_release(void *calldata)
5321{
5322 struct nfs4_layoutget *lgp = calldata;
5323
5324 dprintk("--> %s\n", __func__);
5325 put_layout_hdr(lgp->args.inode);
5326 if (lgp->res.layout.buf != NULL)
5327 free_page((unsigned long) lgp->res.layout.buf);
5328 put_nfs_open_context(lgp->args.ctx);
5329 kfree(calldata);
5330 dprintk("<-- %s\n", __func__);
5331}
5332
5333static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5334 .rpc_call_prepare = nfs4_layoutget_prepare,
5335 .rpc_call_done = nfs4_layoutget_done,
5336 .rpc_release = nfs4_layoutget_release,
5337};
5338
5339int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5340{
5341 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5342 struct rpc_task *task;
5343 struct rpc_message msg = {
5344 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5345 .rpc_argp = &lgp->args,
5346 .rpc_resp = &lgp->res,
5347 };
5348 struct rpc_task_setup task_setup_data = {
5349 .rpc_client = server->client,
5350 .rpc_message = &msg,
5351 .callback_ops = &nfs4_layoutget_call_ops,
5352 .callback_data = lgp,
5353 .flags = RPC_TASK_ASYNC,
5354 };
5355 int status = 0;
5356
5357 dprintk("--> %s\n", __func__);
5358
5359 lgp->res.layout.buf = (void *)__get_free_page(GFP_NOFS);
5360 if (lgp->res.layout.buf == NULL) {
5361 nfs4_layoutget_release(lgp);
5362 return -ENOMEM;
5363 }
5364
5365 lgp->res.seq_res.sr_slot = NULL;
5366 task = rpc_run_task(&task_setup_data);
5367 if (IS_ERR(task))
5368 return PTR_ERR(task);
5369 status = nfs4_wait_for_completion_rpc_task(task);
5370 if (status != 0)
5371 goto out;
5372 status = lgp->status;
5373 if (status != 0)
5374 goto out;
5375 status = pnfs_layout_process(lgp);
5376out:
5377 rpc_put_task(task);
5378 dprintk("<-- %s status=%d\n", __func__, status);
5379 return status;
5380}
5381
5382static int
5383_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5384{
5385 struct nfs4_getdeviceinfo_args args = {
5386 .pdev = pdev,
5387 };
5388 struct nfs4_getdeviceinfo_res res = {
5389 .pdev = pdev,
5390 };
5391 struct rpc_message msg = {
5392 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5393 .rpc_argp = &args,
5394 .rpc_resp = &res,
5395 };
5396 int status;
5397
5398 dprintk("--> %s\n", __func__);
5399 status = nfs4_call_sync(server, &msg, &args, &res, 0);
5400 dprintk("<-- %s status=%d\n", __func__, status);
5401
5402 return status;
5403}
5404
5405int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5406{
5407 struct nfs4_exception exception = { };
5408 int err;
5409
5410 do {
5411 err = nfs4_handle_exception(server,
5412 _nfs4_proc_getdeviceinfo(server, pdev),
5413 &exception);
5414 } while (exception.retry);
5415 return err;
5416}
5417EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5418
Andy Adamson557134a2009-04-01 09:21:53 -04005419#endif /* CONFIG_NFS_V4_1 */
5420
Andy Adamson591d71c2009-04-01 09:22:47 -04005421struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005422 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005423 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 .recover_open = nfs4_open_reclaim,
5425 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04005426 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005427 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428};
5429
Andy Adamson591d71c2009-04-01 09:22:47 -04005430#if defined(CONFIG_NFS_V4_1)
5431struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5432 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5433 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5434 .recover_open = nfs4_open_reclaim,
5435 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05005436 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005437 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005438 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04005439};
5440#endif /* CONFIG_NFS_V4_1 */
5441
5442struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005443 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005444 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 .recover_open = nfs4_open_expired,
5446 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04005447 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005448 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449};
5450
Andy Adamson591d71c2009-04-01 09:22:47 -04005451#if defined(CONFIG_NFS_V4_1)
5452struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5453 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5454 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5455 .recover_open = nfs4_open_expired,
5456 .recover_lock = nfs4_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05005457 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005458 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04005459};
5460#endif /* CONFIG_NFS_V4_1 */
5461
Benny Halevy29fba382009-04-01 09:22:44 -04005462struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5463 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04005464 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005465 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04005466};
5467
5468#if defined(CONFIG_NFS_V4_1)
5469struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5470 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04005471 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005472 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04005473};
5474#endif
5475
Trond Myklebust97dc1352010-06-16 09:52:26 -04005476static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
5477 .minor_version = 0,
5478 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04005479 .validate_stateid = nfs4_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005480 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
5481 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
5482 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005483};
5484
5485#if defined(CONFIG_NFS_V4_1)
5486static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
5487 .minor_version = 1,
5488 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04005489 .validate_stateid = nfs41_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005490 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
5491 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
5492 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005493};
5494#endif
5495
Trond Myklebust97dc1352010-06-16 09:52:26 -04005496const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
5497 [0] = &nfs_v4_0_minor_ops,
5498#if defined(CONFIG_NFS_V4_1)
5499 [1] = &nfs_v4_1_minor_ops,
5500#endif
5501};
5502
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005503static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005504 .permission = nfs_permission,
5505 .getattr = nfs_getattr,
5506 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005507 .getxattr = generic_getxattr,
5508 .setxattr = generic_setxattr,
5509 .listxattr = generic_listxattr,
5510 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005511};
5512
David Howells509de812006-08-22 20:06:11 -04005513const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 .version = 4, /* protocol version */
5515 .dentry_ops = &nfs4_dentry_operations,
5516 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005517 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 .getroot = nfs4_proc_get_root,
5519 .getattr = nfs4_proc_getattr,
5520 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04005521 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522 .lookup = nfs4_proc_lookup,
5523 .access = nfs4_proc_access,
5524 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525 .create = nfs4_proc_create,
5526 .remove = nfs4_proc_remove,
5527 .unlink_setup = nfs4_proc_unlink_setup,
5528 .unlink_done = nfs4_proc_unlink_done,
5529 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04005530 .rename_setup = nfs4_proc_rename_setup,
5531 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532 .link = nfs4_proc_link,
5533 .symlink = nfs4_proc_symlink,
5534 .mkdir = nfs4_proc_mkdir,
5535 .rmdir = nfs4_proc_remove,
5536 .readdir = nfs4_proc_readdir,
5537 .mknod = nfs4_proc_mknod,
5538 .statfs = nfs4_proc_statfs,
5539 .fsinfo = nfs4_proc_fsinfo,
5540 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04005541 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542 .decode_dirent = nfs4_decode_dirent,
5543 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05005544 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005546 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005548 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005550 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04005551 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04005552 .open_context = nfs4_atomic_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553};
5554
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005555static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
5556 .prefix = XATTR_NAME_NFSV4_ACL,
5557 .list = nfs4_xattr_list_nfs4_acl,
5558 .get = nfs4_xattr_get_nfs4_acl,
5559 .set = nfs4_xattr_set_nfs4_acl,
5560};
5561
5562const struct xattr_handler *nfs4_xattr_handlers[] = {
5563 &nfs4_xattr_nfs4_acl_handler,
5564 NULL
5565};
5566
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567/*
5568 * Local variables:
5569 * c-basic-offset: 8
5570 * End:
5571 */