blob: ca88f294f0af3e7b7d7799334ae4f6e0059ecd67 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/sunrpc/clnt.h>
44#include <linux/nfs.h>
45#include <linux/nfs4.h>
46#include <linux/nfs_fs.h>
47#include <linux/nfs_page.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070049#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040050#include <linux/module.h>
Andy Adamson5a0ffe52009-04-01 09:23:18 -040051#include <linux/sunrpc/bc_xprt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Trond Myklebust4ce79712005-06-22 17:16:21 +000053#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050055#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050056#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040057#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040058#include "pnfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60#define NFSDBG_FACILITY NFSDBG_PROC
61
Trond Myklebust2066fe82006-09-15 08:30:46 -040062#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#define NFS4_POLL_RETRY_MAX (15*HZ)
64
Trond Myklebusta78cb572009-08-09 15:06:19 -040065#define NFS4_MAX_LOOP_ON_RECOVER (10)
66
Trond Myklebustcdd4e682006-01-03 09:55:12 +010067struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010068static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080069static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050071static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Trond Myklebust99367812007-07-17 21:52:41 -040072static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
73static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040074static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
75 struct nfs_fattr *fattr, struct iattr *sattr,
76 struct nfs4_state *state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Linus Torvalds1da177e2005-04-16 15:20:36 -070078/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050079static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
Trond Myklebust52567b02009-10-23 14:46:42 -040081 if (err >= -1000)
82 return err;
83 switch (err) {
84 case -NFS4ERR_RESOURCE:
85 return -EREMOTEIO;
86 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -070088 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -040089 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 }
Trond Myklebust52567b02009-10-23 14:46:42 -040091 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
93
94/*
95 * This is our standard bitmap for GETATTR requests.
96 */
97const u32 nfs4_fattr_bitmap[2] = {
98 FATTR4_WORD0_TYPE
99 | FATTR4_WORD0_CHANGE
100 | FATTR4_WORD0_SIZE
101 | FATTR4_WORD0_FSID
102 | FATTR4_WORD0_FILEID,
103 FATTR4_WORD1_MODE
104 | FATTR4_WORD1_NUMLINKS
105 | FATTR4_WORD1_OWNER
106 | FATTR4_WORD1_OWNER_GROUP
107 | FATTR4_WORD1_RAWDEV
108 | FATTR4_WORD1_SPACE_USED
109 | FATTR4_WORD1_TIME_ACCESS
110 | FATTR4_WORD1_TIME_METADATA
111 | FATTR4_WORD1_TIME_MODIFY
112};
113
114const u32 nfs4_statfs_bitmap[2] = {
115 FATTR4_WORD0_FILES_AVAIL
116 | FATTR4_WORD0_FILES_FREE
117 | FATTR4_WORD0_FILES_TOTAL,
118 FATTR4_WORD1_SPACE_AVAIL
119 | FATTR4_WORD1_SPACE_FREE
120 | FATTR4_WORD1_SPACE_TOTAL
121};
122
Trond Myklebust4ce79712005-06-22 17:16:21 +0000123const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 FATTR4_WORD0_MAXLINK
125 | FATTR4_WORD0_MAXNAME,
126 0
127};
128
129const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
130 | FATTR4_WORD0_MAXREAD
131 | FATTR4_WORD0_MAXWRITE
132 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700133 FATTR4_WORD1_TIME_DELTA
Andy Adamson504913f2010-10-20 00:17:57 -0400134 | FATTR4_WORD1_FS_LAYOUT_TYPES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135};
136
Manoj Naik830b8e32006-06-09 09:34:25 -0400137const u32 nfs4_fs_locations_bitmap[2] = {
138 FATTR4_WORD0_TYPE
139 | FATTR4_WORD0_CHANGE
140 | FATTR4_WORD0_SIZE
141 | FATTR4_WORD0_FSID
142 | FATTR4_WORD0_FILEID
143 | FATTR4_WORD0_FS_LOCATIONS,
144 FATTR4_WORD1_MODE
145 | FATTR4_WORD1_NUMLINKS
146 | FATTR4_WORD1_OWNER
147 | FATTR4_WORD1_OWNER_GROUP
148 | FATTR4_WORD1_RAWDEV
149 | FATTR4_WORD1_SPACE_USED
150 | FATTR4_WORD1_TIME_ACCESS
151 | FATTR4_WORD1_TIME_METADATA
152 | FATTR4_WORD1_TIME_MODIFY
153 | FATTR4_WORD1_MOUNTED_ON_FILEID
154};
155
Al Virobc4785c2006-10-19 23:28:51 -0700156static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 struct nfs4_readdir_arg *readdir)
158{
Al Viro0dbb4c62006-10-19 23:28:49 -0700159 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 BUG_ON(readdir->count < 80);
162 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000163 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
165 return;
166 }
167
168 readdir->cookie = 0;
169 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
170 if (cookie == 2)
171 return;
172
173 /*
174 * NFSv4 servers do not return entries for '.' and '..'
175 * Therefore, we fake these entries here. We let '.'
176 * have cookie 0 and '..' have cookie 1. Note that
177 * when talking to the server, we always send cookie 0
178 * instead of 1 or 2.
179 */
Al Viro0dbb4c62006-10-19 23:28:49 -0700180 start = p = kmap_atomic(*readdir->pages, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 if (cookie == 0) {
183 *p++ = xdr_one; /* next */
184 *p++ = xdr_zero; /* cookie, first word */
185 *p++ = xdr_one; /* cookie, second word */
186 *p++ = xdr_one; /* entry len */
187 memcpy(p, ".\0\0\0", 4); /* entry */
188 p++;
189 *p++ = xdr_one; /* bitmap length */
190 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
191 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400192 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 }
194
195 *p++ = xdr_one; /* next */
196 *p++ = xdr_zero; /* cookie, first word */
197 *p++ = xdr_two; /* cookie, second word */
198 *p++ = xdr_two; /* entry len */
199 memcpy(p, "..\0\0", 4); /* entry */
200 p++;
201 *p++ = xdr_one; /* bitmap length */
202 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
203 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400204 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 readdir->pgbase = (char *)p - (char *)start;
207 readdir->count -= readdir->pgbase;
208 kunmap_atomic(start, KM_USER0);
209}
210
Trond Myklebust65de8722008-12-23 15:21:44 -0500211static int nfs4_wait_clnt_recover(struct nfs_client *clp)
212{
213 int res;
214
215 might_sleep();
216
Trond Myklebuste005e802008-12-23 15:21:48 -0500217 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400218 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500219 return res;
220}
221
222static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
223{
224 int res = 0;
225
226 might_sleep();
227
228 if (*timeout <= 0)
229 *timeout = NFS4_POLL_RETRY_MIN;
230 if (*timeout > NFS4_POLL_RETRY_MAX)
231 *timeout = NFS4_POLL_RETRY_MAX;
232 schedule_timeout_killable(*timeout);
233 if (fatal_signal_pending(current))
234 res = -ERESTARTSYS;
235 *timeout <<= 1;
236 return res;
237}
238
239/* This is the error handling routine for processes that are allowed
240 * to sleep.
241 */
242static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
243{
244 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500245 struct nfs4_state *state = exception->state;
Trond Myklebust65de8722008-12-23 15:21:44 -0500246 int ret = errorcode;
247
248 exception->retry = 0;
249 switch(errorcode) {
250 case 0:
251 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500252 case -NFS4ERR_ADMIN_REVOKED:
253 case -NFS4ERR_BAD_STATEID:
254 case -NFS4ERR_OPENMODE:
255 if (state == NULL)
256 break;
257 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500258 goto do_state_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500259 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500260 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust65de8722008-12-23 15:21:44 -0500261 case -NFS4ERR_EXPIRED:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500262 goto do_state_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500263#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400264 case -NFS4ERR_BADSESSION:
265 case -NFS4ERR_BADSLOT:
266 case -NFS4ERR_BAD_HIGH_SLOT:
267 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
268 case -NFS4ERR_DEADSESSION:
269 case -NFS4ERR_SEQ_FALSE_RETRY:
270 case -NFS4ERR_SEQ_MISORDERED:
271 dprintk("%s ERROR: %d Reset session\n", __func__,
272 errorcode);
Andy Adamson0b9e2d42009-12-04 16:02:14 -0500273 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -0400274 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500275 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500276#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500277 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500278 if (exception->timeout > HZ) {
279 /* We have retried a decent amount, time to
280 * fail
281 */
282 ret = -EBUSY;
283 break;
284 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500285 case -NFS4ERR_GRACE:
286 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500287 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500288 ret = nfs4_delay(server->client, &exception->timeout);
289 if (ret != 0)
290 break;
291 case -NFS4ERR_OLD_STATEID:
292 exception->retry = 1;
293 }
294 /* We failed to handle the error */
295 return nfs4_map_errors(ret);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500296do_state_recovery:
297 nfs4_schedule_state_recovery(clp);
298 ret = nfs4_wait_clnt_recover(clp);
299 if (ret == 0)
300 exception->retry = 1;
301 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500302}
303
304
Trond Myklebust452e9352010-07-31 14:29:06 -0400305static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 spin_lock(&clp->cl_lock);
308 if (time_before(clp->cl_last_renewal,timestamp))
309 clp->cl_last_renewal = timestamp;
310 spin_unlock(&clp->cl_lock);
311}
312
Trond Myklebust452e9352010-07-31 14:29:06 -0400313static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
314{
315 do_renew_lease(server->nfs_client, timestamp);
316}
317
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400318#if defined(CONFIG_NFS_V4_1)
319
Benny Halevy510b8172009-04-01 09:22:14 -0400320/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400321 * nfs4_free_slot - free a slot and efficiently update slot table.
322 *
323 * freeing a slot is trivially done by clearing its respective bit
324 * in the bitmap.
325 * If the freed slotid equals highest_used_slotid we want to update it
326 * so that the server would be able to size down the slot table if needed,
327 * otherwise we know that the highest_used_slotid is still in use.
328 * When updating highest_used_slotid there may be "holes" in the bitmap
329 * so we need to scan down from highest_used_slotid to 0 looking for the now
330 * highest slotid in use.
331 * If none found, highest_used_slotid is set to -1.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500332 *
333 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400334 */
335static void
Benny Halevydfb4f3092010-09-24 09:17:01 -0400336nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *free_slot)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400337{
Benny Halevydfb4f3092010-09-24 09:17:01 -0400338 int free_slotid = free_slot - tbl->slots;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400339 int slotid = free_slotid;
340
Benny Halevydfb4f3092010-09-24 09:17:01 -0400341 BUG_ON(slotid < 0 || slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400342 /* clear used bit in bitmap */
343 __clear_bit(slotid, tbl->used_slots);
344
345 /* update highest_used_slotid when it is freed */
346 if (slotid == tbl->highest_used_slotid) {
347 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500348 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400349 tbl->highest_used_slotid = slotid;
350 else
351 tbl->highest_used_slotid = -1;
352 }
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400353 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
354 free_slotid, tbl->highest_used_slotid);
355}
356
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800357/*
358 * Signal state manager thread if session is drained
359 */
360static void nfs41_check_drain_session_complete(struct nfs4_session *ses)
361{
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800362 struct rpc_task *task;
363
Trond Myklebusta2118c32010-06-16 09:52:26 -0400364 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800365 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
366 if (task)
367 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800368 return;
369 }
370
371 if (ses->fc_slot_table.highest_used_slotid != -1)
372 return;
373
374 dprintk("%s COMPLETE: Session Drained\n", __func__);
375 complete(&ses->complete);
376}
377
Trond Myklebustd185a332010-06-16 09:52:25 -0400378static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400379{
380 struct nfs4_slot_table *tbl;
381
Trond Myklebustd185a332010-06-16 09:52:25 -0400382 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400383 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400384 /* just wake up the next guy waiting since
385 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500386 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500387 return;
Andy Adamson13615872009-04-01 09:22:17 -0400388 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500389
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500390 spin_lock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400391 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebustd185a332010-06-16 09:52:25 -0400392 nfs41_check_drain_session_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500393 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400394 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400395}
396
Trond Myklebust14516c32010-07-31 14:29:06 -0400397static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400398{
399 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400400 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400401
402 /*
403 * sr_status remains 1 if an RPC level error occurred. The server
404 * may or may not have processed the sequence operation..
405 * Proceed as if the server received and processed the sequence
406 * operation.
407 */
408 if (res->sr_status == 1)
409 res->sr_status = NFS_OK;
410
411 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400412 if (!res->sr_slot)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400413 goto out;
414
Andy Adamson691daf32009-12-04 15:55:39 -0500415 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400416 switch (res->sr_status) {
417 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400418 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400419 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400420 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400421 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400422 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500423 /* Check sequence flags */
Alexandros Batsakis71358402010-02-05 03:45:05 -0800424 if (atomic_read(&clp->cl_count) > 1)
425 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust14516c32010-07-31 14:29:06 -0400426 break;
427 case -NFS4ERR_DELAY:
428 /* The server detected a resend of the RPC call and
429 * returned NFS4ERR_DELAY as per Section 2.10.6.2
430 * of RFC5661.
431 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200432 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400433 __func__,
434 res->sr_slot - res->sr_session->fc_slot_table.slots,
435 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400436 goto out_retry;
437 default:
438 /* Just update the slot sequence no. */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400439 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400440 }
441out:
442 /* The session may be reset by one of the error handlers. */
443 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400444 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400445 return 1;
446out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400447 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400448 goto out;
449 rpc_delay(task, NFS4_POLL_RETRY_MAX);
450 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400451}
452
Trond Myklebust14516c32010-07-31 14:29:06 -0400453static int nfs4_sequence_done(struct rpc_task *task,
454 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400455{
Trond Myklebust14516c32010-07-31 14:29:06 -0400456 if (res->sr_session == NULL)
457 return 1;
458 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400459}
460
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400461/*
Benny Halevy510b8172009-04-01 09:22:14 -0400462 * nfs4_find_slot - efficiently look for a free slot
463 *
464 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
465 * If found, we mark the slot as used, update the highest_used_slotid,
466 * and respectively set up the sequence operation args.
467 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400468 *
469 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400470 */
471static u8
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800472nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400473{
474 int slotid;
475 u8 ret_id = NFS4_MAX_SLOT_TABLE;
476 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
477
Benny Halevy510b8172009-04-01 09:22:14 -0400478 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
479 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
480 tbl->max_slots);
481 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
482 if (slotid >= tbl->max_slots)
483 goto out;
484 __set_bit(slotid, tbl->used_slots);
485 if (slotid > tbl->highest_used_slotid)
486 tbl->highest_used_slotid = slotid;
487 ret_id = slotid;
488out:
489 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
490 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400491 return ret_id;
492}
493
Andy Adamsonce5039c2009-04-01 09:22:13 -0400494static int nfs41_setup_sequence(struct nfs4_session *session,
495 struct nfs4_sequence_args *args,
496 struct nfs4_sequence_res *res,
497 int cache_reply,
498 struct rpc_task *task)
499{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400500 struct nfs4_slot *slot;
501 struct nfs4_slot_table *tbl;
502 u8 slotid;
503
504 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400505 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400506 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400507 return 0;
508
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400509 tbl = &session->fc_slot_table;
510
511 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400512 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800513 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamsonea028ac2009-12-04 15:55:38 -0500514 /*
515 * The state manager will wait until the slot table is empty.
516 * Schedule the reset thread
517 */
Andy Adamson05f0d232009-12-04 15:55:37 -0500518 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400519 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500520 dprintk("%s Schedule Session Reset\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500521 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400522 }
523
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800524 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
525 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
526 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
527 spin_unlock(&tbl->slot_tbl_lock);
528 dprintk("%s enforce FIFO order\n", __func__);
529 return -EAGAIN;
530 }
531
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800532 slotid = nfs4_find_slot(tbl);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400533 if (slotid == NFS4_MAX_SLOT_TABLE) {
534 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
535 spin_unlock(&tbl->slot_tbl_lock);
536 dprintk("<-- %s: no free slots\n", __func__);
537 return -EAGAIN;
538 }
539 spin_unlock(&tbl->slot_tbl_lock);
540
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800541 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400542 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400543 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400544 args->sa_slotid = slotid;
545 args->sa_cache_this = cache_reply;
546
547 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
548
Benny Halevy99fe60d2009-04-01 09:22:29 -0400549 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400550 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400551 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400552 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400553 /*
554 * sr_status is only set in decode_sequence, and so will remain
555 * set to 1 if an rpc level failure occurs.
556 */
557 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400558 return 0;
559}
560
Trond Myklebust035168ab2010-06-16 09:52:26 -0400561int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400562 struct nfs4_sequence_args *args,
563 struct nfs4_sequence_res *res,
564 int cache_reply,
565 struct rpc_task *task)
566{
Trond Myklebust035168ab2010-06-16 09:52:26 -0400567 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400568 int ret = 0;
569
Trond Myklebust035168ab2010-06-16 09:52:26 -0400570 if (session == NULL) {
571 args->sa_session = NULL;
572 res->sr_session = NULL;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400573 goto out;
Trond Myklebust035168ab2010-06-16 09:52:26 -0400574 }
575
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200576 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400577 __func__, session->clp, session, res->sr_slot ?
578 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168ab2010-06-16 09:52:26 -0400579
580 ret = nfs41_setup_sequence(session, args, res, cache_reply,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400581 task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400582out:
583 dprintk("<-- %s status=%d\n", __func__, ret);
584 return ret;
585}
586
587struct nfs41_call_sync_data {
Trond Myklebust035168ab2010-06-16 09:52:26 -0400588 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400589 struct nfs4_sequence_args *seq_args;
590 struct nfs4_sequence_res *seq_res;
591 int cache_reply;
592};
593
594static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
595{
596 struct nfs41_call_sync_data *data = calldata;
597
Trond Myklebust035168ab2010-06-16 09:52:26 -0400598 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
599
600 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400601 data->seq_res, data->cache_reply, task))
602 return;
603 rpc_call_start(task);
604}
605
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800606static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
607{
608 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
609 nfs41_call_sync_prepare(task, calldata);
610}
611
Andy Adamson69ab40c2009-04-01 09:22:19 -0400612static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
613{
614 struct nfs41_call_sync_data *data = calldata;
615
Trond Myklebust14516c32010-07-31 14:29:06 -0400616 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400617}
618
Andy Adamsonce5039c2009-04-01 09:22:13 -0400619struct rpc_call_ops nfs41_call_sync_ops = {
620 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400621 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400622};
623
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800624struct rpc_call_ops nfs41_call_priv_sync_ops = {
625 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
626 .rpc_call_done = nfs41_call_sync_done,
627};
628
Trond Myklebust035168ab2010-06-16 09:52:26 -0400629static int nfs4_call_sync_sequence(struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400630 struct rpc_message *msg,
631 struct nfs4_sequence_args *args,
632 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800633 int cache_reply,
634 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400635{
636 int ret;
637 struct rpc_task *task;
638 struct nfs41_call_sync_data data = {
Trond Myklebust035168ab2010-06-16 09:52:26 -0400639 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400640 .seq_args = args,
641 .seq_res = res,
642 .cache_reply = cache_reply,
643 };
644 struct rpc_task_setup task_setup = {
Trond Myklebust035168ab2010-06-16 09:52:26 -0400645 .rpc_client = server->client,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400646 .rpc_message = msg,
647 .callback_ops = &nfs41_call_sync_ops,
648 .callback_data = &data
649 };
650
Benny Halevydfb4f3092010-09-24 09:17:01 -0400651 res->sr_slot = NULL;
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800652 if (privileged)
653 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400654 task = rpc_run_task(&task_setup);
655 if (IS_ERR(task))
656 ret = PTR_ERR(task);
657 else {
658 ret = task->tk_status;
659 rpc_put_task(task);
660 }
661 return ret;
662}
663
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400664int _nfs4_call_sync_session(struct nfs_server *server,
665 struct rpc_message *msg,
666 struct nfs4_sequence_args *args,
667 struct nfs4_sequence_res *res,
668 int cache_reply)
669{
Trond Myklebust035168ab2010-06-16 09:52:26 -0400670 return nfs4_call_sync_sequence(server, msg, args, res, cache_reply, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400671}
672
Trond Myklebustdf896452010-06-16 09:52:26 -0400673#else
Trond Myklebust14516c32010-07-31 14:29:06 -0400674static int nfs4_sequence_done(struct rpc_task *task,
675 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400676{
Trond Myklebust14516c32010-07-31 14:29:06 -0400677 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400678}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400679#endif /* CONFIG_NFS_V4_1 */
680
681int _nfs4_call_sync(struct nfs_server *server,
682 struct rpc_message *msg,
683 struct nfs4_sequence_args *args,
684 struct nfs4_sequence_res *res,
685 int cache_reply)
686{
Benny Halevyf3752972009-04-01 09:22:04 -0400687 args->sa_session = res->sr_session = NULL;
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400688 return rpc_call_sync(server->client, msg, 0);
689}
690
691#define nfs4_call_sync(server, msg, args, res, cache_reply) \
Trond Myklebust97dc1352010-06-16 09:52:26 -0400692 (server)->nfs_client->cl_mvops->call_sync((server), (msg), &(args)->seq_args, \
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400693 &(res)->seq_res, (cache_reply))
694
Trond Myklebust38478b22006-05-25 01:40:57 -0400695static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
Trond Myklebust38478b22006-05-25 01:40:57 -0400697 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Trond Myklebust38478b22006-05-25 01:40:57 -0400699 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400700 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
701 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400702 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400703 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400704 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
706
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100707struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400708 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100709 struct nfs_openargs o_arg;
710 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100711 struct nfs_open_confirmargs c_arg;
712 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100713 struct nfs_fattr f_attr;
714 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400715 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100716 struct dentry *dir;
717 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400718 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100719 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100720 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400721 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100722 int rpc_status;
723 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100724};
725
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400726
727static void nfs4_init_opendata_res(struct nfs4_opendata *p)
728{
729 p->o_res.f_attr = &p->f_attr;
730 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400731 p->o_res.seqid = p->o_arg.seqid;
732 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400733 p->o_res.server = p->o_arg.server;
734 nfs_fattr_init(&p->f_attr);
735 nfs_fattr_init(&p->dir_attr);
736}
737
Trond Myklebustad389da2007-06-05 12:30:00 -0400738static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500739 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400740 const struct iattr *attrs,
741 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100742{
Trond Myklebustad389da2007-06-05 12:30:00 -0400743 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100744 struct inode *dir = parent->d_inode;
745 struct nfs_server *server = NFS_SERVER(dir);
746 struct nfs4_opendata *p;
747
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400748 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100749 if (p == NULL)
750 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400751 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100752 if (p->o_arg.seqid == NULL)
753 goto err_free;
Al Virof6948692010-01-30 13:51:04 -0500754 path_get(path);
755 p->path = *path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100756 p->dir = parent;
757 p->owner = sp;
758 atomic_inc(&sp->so_count);
759 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500760 p->o_arg.open_flags = flags;
761 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400762 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400763 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400764 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100765 p->o_arg.server = server;
766 p->o_arg.bitmask = server->attr_bitmask;
767 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400768 if (flags & O_CREAT) {
769 u32 *s;
770
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100771 p->o_arg.u.attrs = &p->attrs;
772 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400773 s = (u32 *) p->o_arg.u.verifier.data;
774 s[0] = jiffies;
775 s[1] = current->pid;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100776 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100777 p->c_arg.fh = &p->o_res.fh;
778 p->c_arg.stateid = &p->o_res.stateid;
779 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400780 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400781 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100782 return p;
783err_free:
784 kfree(p);
785err:
786 dput(parent);
787 return NULL;
788}
789
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400790static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100791{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400792 struct nfs4_opendata *p = container_of(kref,
793 struct nfs4_opendata, kref);
794
795 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400796 if (p->state != NULL)
797 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400798 nfs4_put_state_owner(p->owner);
799 dput(p->dir);
Jan Blunck31f31db12008-05-02 13:42:45 -0700800 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400801 kfree(p);
802}
803
804static void nfs4_opendata_put(struct nfs4_opendata *p)
805{
806 if (p != NULL)
807 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100808}
809
Trond Myklebust06f814a2006-01-03 09:55:07 +0100810static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
811{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100812 int ret;
813
Trond Myklebust06f814a2006-01-03 09:55:07 +0100814 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100815 return ret;
816}
817
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500818static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400819{
820 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500821
822 if (open_mode & O_EXCL)
823 goto out;
824 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400825 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500826 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
827 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400828 break;
829 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500830 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
831 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400832 break;
833 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500834 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
835 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400836 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500837out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400838 return ret;
839}
840
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500841static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400842{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500843 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400844 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500845 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400846 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500847 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400848 return 1;
849}
850
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500851static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100852{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500853 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100854 case FMODE_WRITE:
855 state->n_wronly++;
856 break;
857 case FMODE_READ:
858 state->n_rdonly++;
859 break;
860 case FMODE_READ|FMODE_WRITE:
861 state->n_rdwr++;
862 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500863 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100864}
865
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500866static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400867{
868 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
869 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
870 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500871 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400872 case FMODE_READ:
873 set_bit(NFS_O_RDONLY_STATE, &state->flags);
874 break;
875 case FMODE_WRITE:
876 set_bit(NFS_O_WRONLY_STATE, &state->flags);
877 break;
878 case FMODE_READ|FMODE_WRITE:
879 set_bit(NFS_O_RDWR_STATE, &state->flags);
880 }
881}
882
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500883static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400884{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400885 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500886 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400887 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400888}
889
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500890static 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 -0700891{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400892 /*
893 * Protect the call to nfs4_state_set_mode_locked and
894 * serialise the stateid update
895 */
896 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400897 if (deleg_stateid != NULL) {
898 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
899 set_bit(NFS_DELEGATED_STATE, &state->flags);
900 }
901 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500902 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400903 write_sequnlock(&state->seqlock);
904 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500905 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700906 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
908
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500909static 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 -0500910{
911 struct nfs_inode *nfsi = NFS_I(state->inode);
912 struct nfs_delegation *deleg_cur;
913 int ret = 0;
914
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500915 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500916
917 rcu_read_lock();
918 deleg_cur = rcu_dereference(nfsi->delegation);
919 if (deleg_cur == NULL)
920 goto no_delegation;
921
922 spin_lock(&deleg_cur->lock);
923 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500924 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500925 goto no_delegation_unlock;
926
927 if (delegation == NULL)
928 delegation = &deleg_cur->stateid;
929 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
930 goto no_delegation_unlock;
931
Trond Myklebustb7391f42008-12-23 15:21:52 -0500932 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500933 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500934 ret = 1;
935no_delegation_unlock:
936 spin_unlock(&deleg_cur->lock);
937no_delegation:
938 rcu_read_unlock();
939
940 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500941 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500942 ret = 1;
943 }
944
945 return ret;
946}
947
948
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500949static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400950{
951 struct nfs_delegation *delegation;
952
953 rcu_read_lock();
954 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500955 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400956 rcu_read_unlock();
957 return;
958 }
959 rcu_read_unlock();
960 nfs_inode_return_delegation(inode);
961}
962
Trond Myklebust6ee41262007-07-08 14:11:36 -0400963static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400964{
965 struct nfs4_state *state = opendata->state;
966 struct nfs_inode *nfsi = NFS_I(state->inode);
967 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500968 int open_mode = opendata->o_arg.open_flags & O_EXCL;
969 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400970 nfs4_stateid stateid;
971 int ret = -EAGAIN;
972
Trond Myklebustaac00a82007-07-05 19:02:21 -0400973 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500974 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400975 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500976 if (can_open_cached(state, fmode, open_mode)) {
977 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400978 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400979 goto out_return_state;
980 }
981 spin_unlock(&state->owner->so_lock);
982 }
Trond Myklebust34310432008-12-23 15:21:38 -0500983 rcu_read_lock();
984 delegation = rcu_dereference(nfsi->delegation);
985 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500986 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -0500987 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -0400988 break;
Trond Myklebust34310432008-12-23 15:21:38 -0500989 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400990 /* Save the delegation */
991 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
992 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -0400993 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400994 if (ret != 0)
995 goto out;
996 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -0500997
998 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500999 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001000 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001001 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001002out:
1003 return ERR_PTR(ret);
1004out_return_state:
1005 atomic_inc(&state->count);
1006 return state;
1007}
1008
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001009static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1010{
1011 struct inode *inode;
1012 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001013 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001014 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001015
Trond Myklebustaac00a82007-07-05 19:02:21 -04001016 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001017 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001018 goto out;
1019 }
1020
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001021 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001022 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001023 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001024 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001025 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001026 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001027 goto err;
1028 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001029 state = nfs4_get_open_state(inode, data->owner);
1030 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001031 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001032 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001033 int delegation_flags = 0;
1034
Trond Myklebust003707c2007-07-05 18:07:55 -04001035 rcu_read_lock();
1036 delegation = rcu_dereference(NFS_I(inode)->delegation);
1037 if (delegation)
1038 delegation_flags = delegation->flags;
1039 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -05001040 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001041 nfs_inode_set_delegation(state->inode,
1042 data->owner->so_cred,
1043 &data->o_res);
1044 else
1045 nfs_inode_reclaim_delegation(state->inode,
1046 data->owner->so_cred,
1047 &data->o_res);
1048 }
Trond Myklebust34310432008-12-23 15:21:38 -05001049
1050 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001051 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001052 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001053out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001054 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001055err_put_inode:
1056 iput(inode);
1057err:
1058 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001059}
1060
Trond Myklebust864472e2006-01-03 09:55:15 +01001061static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1062{
1063 struct nfs_inode *nfsi = NFS_I(state->inode);
1064 struct nfs_open_context *ctx;
1065
1066 spin_lock(&state->inode->i_lock);
1067 list_for_each_entry(ctx, &nfsi->open_files, list) {
1068 if (ctx->state != state)
1069 continue;
1070 get_nfs_open_context(ctx);
1071 spin_unlock(&state->inode->i_lock);
1072 return ctx;
1073 }
1074 spin_unlock(&state->inode->i_lock);
1075 return ERR_PTR(-ENOENT);
1076}
1077
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001078static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1079{
1080 struct nfs4_opendata *opendata;
1081
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001082 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001083 if (opendata == NULL)
1084 return ERR_PTR(-ENOMEM);
1085 opendata->state = state;
1086 atomic_inc(&state->count);
1087 return opendata;
1088}
1089
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001090static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001091{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001092 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001093 int ret;
1094
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001095 opendata->o_arg.open_flags = 0;
1096 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001097 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1098 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1099 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001100 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001101 if (ret != 0)
1102 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001103 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001104 if (IS_ERR(newstate))
1105 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001106 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001107 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001108 return 0;
1109}
1110
1111static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1112{
Trond Myklebust864472e2006-01-03 09:55:15 +01001113 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001114 int ret;
1115
1116 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001117 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001118 smp_rmb();
1119 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001120 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001121 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001122 if (ret != 0)
1123 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001124 if (newstate != state)
1125 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001126 }
1127 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001128 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001129 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001130 if (ret != 0)
1131 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001132 if (newstate != state)
1133 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001134 }
1135 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001136 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001137 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001138 if (ret != 0)
1139 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001140 if (newstate != state)
1141 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001142 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001143 /*
1144 * We may have performed cached opens for all three recoveries.
1145 * Check if we need to update the current stateid.
1146 */
1147 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1148 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001149 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001150 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1151 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001152 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001153 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001154 return 0;
1155}
1156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157/*
1158 * OPEN_RECLAIM:
1159 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001161static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001163 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001164 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001165 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 int status;
1167
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001168 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1169 if (IS_ERR(opendata))
1170 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001171 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1172 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001173 rcu_read_lock();
1174 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001175 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001176 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001177 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001178 opendata->o_arg.u.delegation_type = delegation_type;
1179 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001180 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 return status;
1182}
1183
Trond Myklebust539cd032007-06-05 11:46:42 -04001184static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
1186 struct nfs_server *server = NFS_SERVER(state->inode);
1187 struct nfs4_exception exception = { };
1188 int err;
1189 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001190 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001191 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001192 break;
1193 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 } while (exception.retry);
1195 return err;
1196}
1197
Trond Myklebust864472e2006-01-03 09:55:15 +01001198static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1199{
1200 struct nfs_open_context *ctx;
1201 int ret;
1202
1203 ctx = nfs4_state_find_open_context(state);
1204 if (IS_ERR(ctx))
1205 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001206 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001207 put_nfs_open_context(ctx);
1208 return ret;
1209}
1210
Trond Myklebust13437e12007-07-06 15:10:43 -04001211static 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 -07001212{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001213 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001214 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001216 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1217 if (IS_ERR(opendata))
1218 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001219 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001220 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001221 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001222 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001223 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001224 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225}
1226
Trond Myklebust13437e12007-07-06 15:10:43 -04001227int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
1229 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001230 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 int err;
1232 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001233 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 switch (err) {
1235 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001236 case -ENOENT:
1237 case -ESTALE:
1238 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001239 case -NFS4ERR_BADSESSION:
1240 case -NFS4ERR_BADSLOT:
1241 case -NFS4ERR_BAD_HIGH_SLOT:
1242 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1243 case -NFS4ERR_DEADSESSION:
1244 nfs4_schedule_state_recovery(
1245 server->nfs_client);
1246 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 case -NFS4ERR_STALE_CLIENTID:
1248 case -NFS4ERR_STALE_STATEID:
1249 case -NFS4ERR_EXPIRED:
1250 /* Don't recall a delegation if it was lost */
David Howells7539bba2006-08-22 20:06:09 -04001251 nfs4_schedule_state_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001252 goto out;
1253 case -ERESTARTSYS:
1254 /*
1255 * The show must go on: exit, but mark the
1256 * stateid as needing recovery.
1257 */
1258 case -NFS4ERR_ADMIN_REVOKED:
1259 case -NFS4ERR_BAD_STATEID:
1260 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001261 case -EKEYEXPIRED:
1262 /*
1263 * User RPCSEC_GSS context has expired.
1264 * We cannot recover this stateid now, so
1265 * skip it and allow recovery thread to
1266 * proceed.
1267 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001268 case -ENOMEM:
1269 err = 0;
1270 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 }
1272 err = nfs4_handle_exception(server, err, &exception);
1273 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001274out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 return err;
1276}
1277
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001278static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1279{
1280 struct nfs4_opendata *data = calldata;
1281
1282 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001283 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001284 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1285 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001286 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001287 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001288 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001289 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001290}
1291
1292static void nfs4_open_confirm_release(void *calldata)
1293{
1294 struct nfs4_opendata *data = calldata;
1295 struct nfs4_state *state = NULL;
1296
1297 /* If this request hasn't been cancelled, do nothing */
1298 if (data->cancelled == 0)
1299 goto out_free;
1300 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001301 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001302 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001303 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001304 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001305 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001306out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001307 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001308}
1309
1310static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001311 .rpc_call_done = nfs4_open_confirm_done,
1312 .rpc_release = nfs4_open_confirm_release,
1313};
1314
1315/*
1316 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1317 */
1318static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1319{
1320 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1321 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001322 struct rpc_message msg = {
1323 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1324 .rpc_argp = &data->c_arg,
1325 .rpc_resp = &data->c_res,
1326 .rpc_cred = data->owner->so_cred,
1327 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001328 struct rpc_task_setup task_setup_data = {
1329 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001330 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001331 .callback_ops = &nfs4_open_confirm_ops,
1332 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001333 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001334 .flags = RPC_TASK_ASYNC,
1335 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 int status;
1337
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001338 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001339 data->rpc_done = 0;
1340 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001341 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001342 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001343 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001344 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001345 status = nfs4_wait_for_completion_rpc_task(task);
1346 if (status != 0) {
1347 data->cancelled = 1;
1348 smp_wmb();
1349 } else
1350 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001351 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 return status;
1353}
1354
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001355static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001357 struct nfs4_opendata *data = calldata;
1358 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001359
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001360 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1361 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001362 /*
1363 * Check if we still need to send an OPEN call, or if we can use
1364 * a delegation instead.
1365 */
1366 if (data->state != NULL) {
1367 struct nfs_delegation *delegation;
1368
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001369 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001370 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001371 rcu_read_lock();
1372 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1373 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001374 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001375 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001376 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001377 }
1378 rcu_read_unlock();
1379 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001380 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001381 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001382 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001383 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001384 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001385 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1386 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001387 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04001388 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001389 &data->o_arg.seq_args,
1390 &data->o_res.seq_res, 1, task))
1391 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001392 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001393 return;
1394out_no_action:
1395 task->tk_action = NULL;
1396
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001397}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001399static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1400{
1401 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1402 nfs4_open_prepare(task, calldata);
1403}
1404
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001405static void nfs4_open_done(struct rpc_task *task, void *calldata)
1406{
1407 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001409 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001410
Trond Myklebust14516c32010-07-31 14:29:06 -04001411 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1412 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001413
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001414 if (task->tk_status == 0) {
1415 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001416 case S_IFREG:
1417 break;
1418 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001419 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001420 break;
1421 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001422 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001423 break;
1424 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001425 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001426 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001427 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001428 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1429 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001430 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001431 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001432}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001433
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001434static void nfs4_open_release(void *calldata)
1435{
1436 struct nfs4_opendata *data = calldata;
1437 struct nfs4_state *state = NULL;
1438
1439 /* If this request hasn't been cancelled, do nothing */
1440 if (data->cancelled == 0)
1441 goto out_free;
1442 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001443 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001444 goto out_free;
1445 /* In case we need an open_confirm, no cleanup! */
1446 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1447 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001448 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001449 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001450 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001451out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001452 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001453}
1454
1455static const struct rpc_call_ops nfs4_open_ops = {
1456 .rpc_call_prepare = nfs4_open_prepare,
1457 .rpc_call_done = nfs4_open_done,
1458 .rpc_release = nfs4_open_release,
1459};
1460
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001461static const struct rpc_call_ops nfs4_recover_open_ops = {
1462 .rpc_call_prepare = nfs4_recover_open_prepare,
1463 .rpc_call_done = nfs4_open_done,
1464 .rpc_release = nfs4_open_release,
1465};
1466
1467static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001468{
1469 struct inode *dir = data->dir->d_inode;
1470 struct nfs_server *server = NFS_SERVER(dir);
1471 struct nfs_openargs *o_arg = &data->o_arg;
1472 struct nfs_openres *o_res = &data->o_res;
1473 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001474 struct rpc_message msg = {
1475 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1476 .rpc_argp = o_arg,
1477 .rpc_resp = o_res,
1478 .rpc_cred = data->owner->so_cred,
1479 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001480 struct rpc_task_setup task_setup_data = {
1481 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001482 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001483 .callback_ops = &nfs4_open_ops,
1484 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001485 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001486 .flags = RPC_TASK_ASYNC,
1487 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001488 int status;
1489
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001490 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001491 data->rpc_done = 0;
1492 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001493 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001494 if (isrecover)
1495 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001496 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001497 if (IS_ERR(task))
1498 return PTR_ERR(task);
1499 status = nfs4_wait_for_completion_rpc_task(task);
1500 if (status != 0) {
1501 data->cancelled = 1;
1502 smp_wmb();
1503 } else
1504 status = data->rpc_status;
1505 rpc_put_task(task);
1506
1507 return status;
1508}
1509
1510static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1511{
1512 struct inode *dir = data->dir->d_inode;
1513 struct nfs_openres *o_res = &data->o_res;
1514 int status;
1515
1516 status = nfs4_run_open_task(data, 1);
1517 if (status != 0 || !data->rpc_done)
1518 return status;
1519
1520 nfs_refresh_inode(dir, o_res->dir_attr);
1521
1522 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1523 status = _nfs4_proc_open_confirm(data);
1524 if (status != 0)
1525 return status;
1526 }
1527
1528 return status;
1529}
1530
1531/*
1532 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1533 */
1534static int _nfs4_proc_open(struct nfs4_opendata *data)
1535{
1536 struct inode *dir = data->dir->d_inode;
1537 struct nfs_server *server = NFS_SERVER(dir);
1538 struct nfs_openargs *o_arg = &data->o_arg;
1539 struct nfs_openres *o_res = &data->o_res;
1540 int status;
1541
1542 status = nfs4_run_open_task(data, 0);
Trond Myklebust3e309912007-07-07 13:19:59 -04001543 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001544 return status;
1545
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001546 if (o_arg->open_flags & O_CREAT) {
1547 update_changeattr(dir, &o_res->cinfo);
1548 nfs_post_op_update_inode(dir, o_res->dir_attr);
1549 } else
1550 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001551 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1552 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001554 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001556 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 }
1558 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001559 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001560 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561}
1562
Trond Myklebust10afec92007-05-14 17:16:04 -04001563static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +01001564{
David Howells7539bba2006-08-22 20:06:09 -04001565 struct nfs_client *clp = server->nfs_client;
Trond Myklebusta78cb572009-08-09 15:06:19 -04001566 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001567 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001568
Trond Myklebusta78cb572009-08-09 15:06:19 -04001569 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001570 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001571 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001572 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001573 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1574 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001575 break;
Trond Myklebust58d97142006-01-03 09:55:24 +01001576 nfs4_schedule_state_recovery(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001577 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001578 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001579 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001580}
1581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582/*
1583 * OPEN_EXPIRED:
1584 * reclaim state on the server after a network partition.
1585 * Assumes caller holds the appropriate lock
1586 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001587static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001589 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001590 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001592 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1593 if (IS_ERR(opendata))
1594 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001595 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001596 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001597 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001598 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001599 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600}
1601
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001602static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001603{
Trond Myklebust539cd032007-06-05 11:46:42 -04001604 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001605 struct nfs4_exception exception = { };
1606 int err;
1607
1608 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001609 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001610 switch (err) {
1611 default:
1612 goto out;
1613 case -NFS4ERR_GRACE:
1614 case -NFS4ERR_DELAY:
1615 nfs4_handle_exception(server, err, &exception);
1616 err = 0;
1617 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001618 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001619out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001620 return err;
1621}
1622
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1624{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001626 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Trond Myklebust864472e2006-01-03 09:55:15 +01001628 ctx = nfs4_state_find_open_context(state);
1629 if (IS_ERR(ctx))
1630 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001631 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001632 put_nfs_open_context(ctx);
1633 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634}
1635
1636/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001637 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1638 * fields corresponding to attributes that were used to store the verifier.
1639 * Make sure we clobber those fields in the later setattr call
1640 */
1641static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1642{
1643 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1644 !(sattr->ia_valid & ATTR_ATIME_SET))
1645 sattr->ia_valid |= ATTR_ATIME;
1646
1647 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1648 !(sattr->ia_valid & ATTR_MTIME_SET))
1649 sattr->ia_valid |= ATTR_MTIME;
1650}
1651
1652/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001653 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001655static 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 -07001656{
1657 struct nfs4_state_owner *sp;
1658 struct nfs4_state *state = NULL;
1659 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001660 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001661 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
1663 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 status = -ENOMEM;
1665 if (!(sp = nfs4_get_state_owner(server, cred))) {
1666 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1667 goto out_err;
1668 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001669 status = nfs4_recover_expired_lease(server);
1670 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001671 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001672 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001673 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001674 status = -ENOMEM;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001675 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001676 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001677 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Trond Myklebustaac00a82007-07-05 19:02:21 -04001679 if (path->dentry->d_inode != NULL)
1680 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1681
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001682 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001684 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001686 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001687 status = PTR_ERR(state);
1688 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001689 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001690 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001691 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001692
1693 if (opendata->o_arg.open_flags & O_EXCL) {
1694 nfs4_exclusive_attrset(opendata, sattr);
1695
1696 nfs_fattr_init(opendata->o_res.f_attr);
1697 status = nfs4_do_setattr(state->inode, cred,
1698 opendata->o_res.f_attr, sattr,
1699 state);
1700 if (status == 0)
1701 nfs_setattr_update_inode(state->inode, sattr);
1702 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1703 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001704 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 *res = state;
1707 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001708err_opendata_put:
1709 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001710err_put_state_owner:
1711 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 *res = NULL;
1714 return status;
1715}
1716
1717
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001718static 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 -07001719{
1720 struct nfs4_exception exception = { };
1721 struct nfs4_state *res;
1722 int status;
1723
1724 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001725 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 if (status == 0)
1727 break;
1728 /* NOTE: BAD_SEQID means the server and client disagree about the
1729 * book-keeping w.r.t. state-changing operations
1730 * (OPEN/CLOSE/LOCK/LOCKU...)
1731 * It is actually a sign of a bug on the client or on the server.
1732 *
1733 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001734 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 * have unhashed the old state_owner for us, and that we can
1736 * therefore safely retry using a new one. We should still warn
1737 * the user though...
1738 */
1739 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001740 printk(KERN_WARNING "NFS: v4 server %s "
1741 " returned a bad sequence-id error!\n",
1742 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 exception.retry = 1;
1744 continue;
1745 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001746 /*
1747 * BAD_STATEID on OPEN means that the server cancelled our
1748 * state before it received the OPEN_CONFIRM.
1749 * Recover by retrying the request as per the discussion
1750 * on Page 181 of RFC3530.
1751 */
1752 if (status == -NFS4ERR_BAD_STATEID) {
1753 exception.retry = 1;
1754 continue;
1755 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001756 if (status == -EAGAIN) {
1757 /* We must have found a delegation */
1758 exception.retry = 1;
1759 continue;
1760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1762 status, &exception));
1763 } while (exception.retry);
1764 return res;
1765}
1766
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001767static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1768 struct nfs_fattr *fattr, struct iattr *sattr,
1769 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001771 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001773 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 .iap = sattr,
1775 .server = server,
1776 .bitmask = server->attr_bitmask,
1777 };
1778 struct nfs_setattrres res = {
1779 .fattr = fattr,
1780 .server = server,
1781 };
1782 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001783 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1784 .rpc_argp = &arg,
1785 .rpc_resp = &res,
1786 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001788 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001789 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Trond Myklebust0e574af2005-10-27 22:12:38 -04001791 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001793 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1794 /* Use that stateid */
1795 } else if (state != NULL) {
Trond Myklebust77041ed2010-07-01 12:49:11 -04001796 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001797 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1799
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001800 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001801 if (status == 0 && state != NULL)
1802 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001803 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804}
1805
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001806static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1807 struct nfs_fattr *fattr, struct iattr *sattr,
1808 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001810 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 struct nfs4_exception exception = { };
1812 int err;
1813 do {
1814 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001815 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 &exception);
1817 } while (exception.retry);
1818 return err;
1819}
1820
1821struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001822 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 struct inode *inode;
1824 struct nfs4_state *state;
1825 struct nfs_closeargs arg;
1826 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001827 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001828 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829};
1830
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001831static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001832{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001833 struct nfs4_closedata *calldata = data;
1834 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001835
1836 nfs4_put_open_state(calldata->state);
1837 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001838 nfs4_put_state_owner(sp);
Jan Blunck31f31db12008-05-02 13:42:45 -07001839 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001840 kfree(calldata);
1841}
1842
Trond Myklebust88069f72009-12-08 08:33:16 -05001843static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1844 fmode_t fmode)
1845{
1846 spin_lock(&state->owner->so_lock);
1847 if (!(fmode & FMODE_READ))
1848 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1849 if (!(fmode & FMODE_WRITE))
1850 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1851 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1852 spin_unlock(&state->owner->so_lock);
1853}
1854
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001855static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001857 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 struct nfs_server *server = NFS_SERVER(calldata->inode);
1860
Trond Myklebust14516c32010-07-31 14:29:06 -04001861 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1862 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 /* hmm. we are done with the inode, and in the process of freeing
1864 * the state_owner. we keep this around to process errors
1865 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 switch (task->tk_status) {
1867 case 0:
Trond Myklebust45328c32007-07-26 17:47:34 -04001868 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001869 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001870 nfs4_close_clear_stateid_flags(state,
1871 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 break;
1873 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001874 case -NFS4ERR_OLD_STATEID:
1875 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001877 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001878 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001880 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1881 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001883 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001884 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885}
1886
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001887static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001889 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001890 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001891 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001892
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001893 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001894 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001895
Trond Myklebust88069f72009-12-08 08:33:16 -05001896 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1897 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001898 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001899 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001900 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001901 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001902 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1903 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1904 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001905 }
1906 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001907 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1908 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1909 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001910 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001911 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001912 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05001913
1914 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001915 /* Note: exit _without_ calling nfs4_close_done */
1916 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001917 return;
1918 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001919
1920 if (calldata->arg.fmode == 0)
1921 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
1922
Trond Myklebust516a6af2005-10-27 22:12:41 -04001923 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001924 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04001925 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Andy Adamson19ddab02009-04-01 09:22:20 -04001926 &calldata->arg.seq_args, &calldata->res.seq_res,
1927 1, task))
1928 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001929 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930}
1931
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001932static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001933 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001934 .rpc_call_done = nfs4_close_done,
1935 .rpc_release = nfs4_free_closedata,
1936};
1937
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938/*
1939 * It is possible for data to be read/written from a mem-mapped file
1940 * after the sys_close call (which hits the vfs layer as a flush).
1941 * This means that we can't safely call nfsv4 close on a file until
1942 * the inode is cleared. This in turn means that we are not good
1943 * NFSv4 citizens - we do not indicate to the server to update the file's
1944 * share state even when we are done with one of the three share
1945 * stateid's in the inode.
1946 *
1947 * NOTE: Caller must be holding the sp->so_owner semaphore!
1948 */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001949int nfs4_do_close(struct path *path, struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001951 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001953 struct nfs4_state_owner *sp = state->owner;
1954 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001955 struct rpc_message msg = {
1956 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1957 .rpc_cred = state->owner->so_cred,
1958 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001959 struct rpc_task_setup task_setup_data = {
1960 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001961 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001962 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05001963 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001964 .flags = RPC_TASK_ASYNC,
1965 };
Trond Myklebust95121352005-10-18 14:20:12 -07001966 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001968 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001970 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001971 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001973 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04001974 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001976 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07001977 if (calldata->arg.seqid == NULL)
1978 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001979 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001980 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001981 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04001982 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001983 calldata->res.server = server;
Al Virof6948692010-01-30 13:51:04 -05001984 path_get(path);
1985 calldata->path = *path;
Trond Myklebust95121352005-10-18 14:20:12 -07001986
Trond Myklebust1174dd12010-12-21 10:52:24 -05001987 msg.rpc_argp = &calldata->arg;
1988 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001989 task_setup_data.callback_data = calldata;
1990 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001991 if (IS_ERR(task))
1992 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001993 status = 0;
1994 if (wait)
1995 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001996 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001997 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07001998out_free_calldata:
1999 kfree(calldata);
2000out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04002001 nfs4_put_open_state(state);
2002 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002003 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004}
2005
Trond Myklebust2b484292010-09-17 10:56:51 -04002006static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002007nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 struct nfs4_state *state;
2010
Trond Myklebust565277f2007-10-15 18:17:53 -04002011 /* Protect against concurrent sillydeletes */
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002012 state = nfs4_do_open(dir, &ctx->path, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002013 if (IS_ERR(state))
2014 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002015 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002016 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017}
2018
Trond Myklebust1185a552009-12-03 15:54:02 -05002019static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002020{
2021 if (ctx->state == NULL)
2022 return;
2023 if (is_sync)
2024 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2025 else
2026 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2027}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
2029static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2030{
Benny Halevy43652ad2009-04-01 09:21:54 -04002031 struct nfs4_server_caps_arg args = {
2032 .fhandle = fhandle,
2033 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 struct nfs4_server_caps_res res = {};
2035 struct rpc_message msg = {
2036 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002037 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 .rpc_resp = &res,
2039 };
2040 int status;
2041
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002042 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 if (status == 0) {
2044 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002045 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2046 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2047 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2048 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2049 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2051 server->caps |= NFS_CAP_ACLS;
2052 if (res.has_links != 0)
2053 server->caps |= NFS_CAP_HARDLINKS;
2054 if (res.has_symlinks != 0)
2055 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002056 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2057 server->caps |= NFS_CAP_FILEID;
2058 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2059 server->caps |= NFS_CAP_MODE;
2060 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2061 server->caps |= NFS_CAP_NLINK;
2062 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2063 server->caps |= NFS_CAP_OWNER;
2064 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2065 server->caps |= NFS_CAP_OWNER_GROUP;
2066 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2067 server->caps |= NFS_CAP_ATIME;
2068 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2069 server->caps |= NFS_CAP_CTIME;
2070 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2071 server->caps |= NFS_CAP_MTIME;
2072
Trond Myklebusta65318b2009-03-11 14:10:28 -04002073 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2074 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2075 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 server->acl_bitmask = res.acl_bitmask;
2077 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 return status;
2080}
2081
Trond Myklebust55a97592006-06-09 09:34:19 -04002082int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083{
2084 struct nfs4_exception exception = { };
2085 int err;
2086 do {
2087 err = nfs4_handle_exception(server,
2088 _nfs4_server_capabilities(server, fhandle),
2089 &exception);
2090 } while (exception.retry);
2091 return err;
2092}
2093
2094static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2095 struct nfs_fsinfo *info)
2096{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 struct nfs4_lookup_root_arg args = {
2098 .bitmask = nfs4_fattr_bitmap,
2099 };
2100 struct nfs4_lookup_res res = {
2101 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002102 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 .fh = fhandle,
2104 };
2105 struct rpc_message msg = {
2106 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2107 .rpc_argp = &args,
2108 .rpc_resp = &res,
2109 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002110
Trond Myklebust0e574af2005-10-27 22:12:38 -04002111 nfs_fattr_init(info->fattr);
Trond Myklebustfccba802009-07-21 16:48:07 -04002112 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113}
2114
2115static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2116 struct nfs_fsinfo *info)
2117{
2118 struct nfs4_exception exception = { };
2119 int err;
2120 do {
2121 err = nfs4_handle_exception(server,
2122 _nfs4_lookup_root(server, fhandle, info),
2123 &exception);
2124 } while (exception.retry);
2125 return err;
2126}
2127
David Howells54ceac42006-08-22 20:06:13 -04002128/*
2129 * get the file handle for the "/" directory on the server
2130 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002132 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 int status;
2135
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 if (status == 0)
2138 status = nfs4_server_capabilities(server, fhandle);
2139 if (status == 0)
2140 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002141 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142}
2143
Manoj Naik6b97fd32006-06-09 09:34:29 -04002144/*
2145 * Get locations and (maybe) other attributes of a referral.
2146 * Note that we'll actually follow the referral later when
2147 * we detect fsid mismatch in inode revalidation
2148 */
Trond Myklebust56659e92007-07-17 21:52:39 -04002149static 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 -04002150{
2151 int status = -ENOMEM;
2152 struct page *page = NULL;
2153 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002154
2155 page = alloc_page(GFP_KERNEL);
2156 if (page == NULL)
2157 goto out;
2158 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2159 if (locations == NULL)
2160 goto out;
2161
Trond Myklebustc228fd32007-01-13 02:28:11 -05002162 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002163 if (status != 0)
2164 goto out;
2165 /* Make sure server returned a different fsid for the referral */
2166 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07002167 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 -04002168 status = -EIO;
2169 goto out;
2170 }
2171
2172 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2173 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2174 if (!fattr->mode)
2175 fattr->mode = S_IFDIR;
2176 memset(fhandle, 0, sizeof(struct nfs_fh));
2177out:
2178 if (page)
2179 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002180 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002181 return status;
2182}
2183
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2185{
2186 struct nfs4_getattr_arg args = {
2187 .fh = fhandle,
2188 .bitmask = server->attr_bitmask,
2189 };
2190 struct nfs4_getattr_res res = {
2191 .fattr = fattr,
2192 .server = server,
2193 };
2194 struct rpc_message msg = {
2195 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2196 .rpc_argp = &args,
2197 .rpc_resp = &res,
2198 };
2199
Trond Myklebust0e574af2005-10-27 22:12:38 -04002200 nfs_fattr_init(fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002201 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202}
2203
2204static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2205{
2206 struct nfs4_exception exception = { };
2207 int err;
2208 do {
2209 err = nfs4_handle_exception(server,
2210 _nfs4_proc_getattr(server, fhandle, fattr),
2211 &exception);
2212 } while (exception.retry);
2213 return err;
2214}
2215
2216/*
2217 * The file is not closed if it is opened due to the a request to change
2218 * the size of the file. The open call will not be needed once the
2219 * VFS layer lookup-intents are implemented.
2220 *
2221 * Close is called when the inode is destroyed.
2222 * If we haven't opened the file for O_WRONLY, we
2223 * need to in the size_change case to obtain a stateid.
2224 *
2225 * Got race?
2226 * Because OPEN is always done by name in nfsv4, it is
2227 * possible that we opened a different file by the same
2228 * name. We can recognize this race condition, but we
2229 * can't do anything about it besides returning an error.
2230 *
2231 * This will be fixed with VFS changes (lookup-intent).
2232 */
2233static int
2234nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2235 struct iattr *sattr)
2236{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002237 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002238 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002239 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 int status;
2241
Trond Myklebust0e574af2005-10-27 22:12:38 -04002242 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
Trond Myklebustd5308382005-11-04 15:33:38 -05002244 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002245 if (sattr->ia_valid & ATTR_FILE) {
2246 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002247
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002248 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002249 if (ctx) {
2250 cred = ctx->cred;
2251 state = ctx->state;
2252 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002253 }
2254
2255 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002256 if (status == 0)
2257 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 return status;
2259}
2260
Trond Myklebust56659e92007-07-17 21:52:39 -04002261static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2262 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04002263 struct nfs_fattr *fattr)
2264{
2265 int status;
2266 struct nfs4_lookup_arg args = {
2267 .bitmask = server->attr_bitmask,
2268 .dir_fh = dirfh,
2269 .name = name,
2270 };
2271 struct nfs4_lookup_res res = {
2272 .server = server,
2273 .fattr = fattr,
2274 .fh = fhandle,
2275 };
2276 struct rpc_message msg = {
2277 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2278 .rpc_argp = &args,
2279 .rpc_resp = &res,
2280 };
2281
2282 nfs_fattr_init(fattr);
2283
2284 dprintk("NFS call lookupfh %s\n", name->name);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002285 status = nfs4_call_sync(server, &msg, &args, &res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002286 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002287 return status;
2288}
2289
2290static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2291 struct qstr *name, struct nfs_fh *fhandle,
2292 struct nfs_fattr *fattr)
2293{
2294 struct nfs4_exception exception = { };
2295 int err;
2296 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04002297 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2298 /* FIXME: !!!! */
2299 if (err == -NFS4ERR_MOVED) {
2300 err = -EREMOTE;
2301 break;
2302 }
2303 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002304 } while (exception.retry);
2305 return err;
2306}
2307
Trond Myklebust56659e92007-07-17 21:52:39 -04002308static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2310{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002311 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
2313 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002314 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002315 if (status == -NFS4ERR_MOVED)
2316 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 dprintk("NFS reply lookup: %d\n", status);
2318 return status;
2319}
2320
2321static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2322{
2323 struct nfs4_exception exception = { };
2324 int err;
2325 do {
2326 err = nfs4_handle_exception(NFS_SERVER(dir),
2327 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2328 &exception);
2329 } while (exception.retry);
2330 return err;
2331}
2332
2333static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2334{
Trond Myklebust76b32992007-08-10 17:45:11 -04002335 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 struct nfs4_accessargs args = {
2337 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002338 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002340 struct nfs4_accessres res = {
2341 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002342 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 struct rpc_message msg = {
2344 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2345 .rpc_argp = &args,
2346 .rpc_resp = &res,
2347 .rpc_cred = entry->cred,
2348 };
2349 int mode = entry->mask;
2350 int status;
2351
2352 /*
2353 * Determine which access bits we want to ask for...
2354 */
2355 if (mode & MAY_READ)
2356 args.access |= NFS4_ACCESS_READ;
2357 if (S_ISDIR(inode->i_mode)) {
2358 if (mode & MAY_WRITE)
2359 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2360 if (mode & MAY_EXEC)
2361 args.access |= NFS4_ACCESS_LOOKUP;
2362 } else {
2363 if (mode & MAY_WRITE)
2364 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2365 if (mode & MAY_EXEC)
2366 args.access |= NFS4_ACCESS_EXECUTE;
2367 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002368
2369 res.fattr = nfs_alloc_fattr();
2370 if (res.fattr == NULL)
2371 return -ENOMEM;
2372
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002373 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 if (!status) {
2375 entry->mask = 0;
2376 if (res.access & NFS4_ACCESS_READ)
2377 entry->mask |= MAY_READ;
2378 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2379 entry->mask |= MAY_WRITE;
2380 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2381 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002382 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002384 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 return status;
2386}
2387
2388static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2389{
2390 struct nfs4_exception exception = { };
2391 int err;
2392 do {
2393 err = nfs4_handle_exception(NFS_SERVER(inode),
2394 _nfs4_proc_access(inode, entry),
2395 &exception);
2396 } while (exception.retry);
2397 return err;
2398}
2399
2400/*
2401 * TODO: For the time being, we don't try to get any attributes
2402 * along with any of the zero-copy operations READ, READDIR,
2403 * READLINK, WRITE.
2404 *
2405 * In the case of the first three, we want to put the GETATTR
2406 * after the read-type operation -- this is because it is hard
2407 * to predict the length of a GETATTR response in v4, and thus
2408 * align the READ data correctly. This means that the GETATTR
2409 * may end up partially falling into the page cache, and we should
2410 * shift it into the 'tail' of the xdr_buf before processing.
2411 * To do this efficiently, we need to know the total length
2412 * of data received, which doesn't seem to be available outside
2413 * of the RPC layer.
2414 *
2415 * In the case of WRITE, we also want to put the GETATTR after
2416 * the operation -- in this case because we want to make sure
2417 * we get the post-operation mtime and size. This means that
2418 * we can't use xdr_encode_pages() as written: we need a variant
2419 * of it which would leave room in the 'tail' iovec.
2420 *
2421 * Both of these changes to the XDR layer would in fact be quite
2422 * minor, but I decided to leave them for a subsequent patch.
2423 */
2424static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2425 unsigned int pgbase, unsigned int pglen)
2426{
2427 struct nfs4_readlink args = {
2428 .fh = NFS_FH(inode),
2429 .pgbase = pgbase,
2430 .pglen = pglen,
2431 .pages = &page,
2432 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002433 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 struct rpc_message msg = {
2435 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2436 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002437 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 };
2439
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002440 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441}
2442
2443static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2444 unsigned int pgbase, unsigned int pglen)
2445{
2446 struct nfs4_exception exception = { };
2447 int err;
2448 do {
2449 err = nfs4_handle_exception(NFS_SERVER(inode),
2450 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2451 &exception);
2452 } while (exception.retry);
2453 return err;
2454}
2455
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456/*
2457 * Got race?
2458 * We will need to arrange for the VFS layer to provide an atomic open.
2459 * Until then, this create/open method is prone to inefficiency and race
2460 * conditions due to the lookup, create, and open VFS calls from sys_open()
2461 * placed on the wire.
2462 *
2463 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2464 * The file will be opened again in the subsequent VFS open call
2465 * (nfs4_proc_file_open).
2466 *
2467 * The open for read will just hang around to be used by any process that
2468 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2469 */
2470
2471static int
2472nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002473 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474{
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002475 struct path my_path = {
Trond Myklebustad389da2007-06-05 12:30:00 -04002476 .dentry = dentry,
2477 };
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002478 struct path *path = &my_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002480 struct rpc_cred *cred = NULL;
2481 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 int status = 0;
2483
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002484 if (ctx != NULL) {
2485 cred = ctx->cred;
2486 path = &ctx->path;
2487 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002489 sattr->ia_mode &= ~current_umask();
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002490 state = nfs4_do_open(dir, path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002491 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 if (IS_ERR(state)) {
2493 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002494 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002496 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002497 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002498 if (ctx != NULL)
2499 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002500 else
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002501 nfs4_close_sync(path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502out:
2503 return status;
2504}
2505
2506static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2507{
Trond Myklebust16e42952005-10-27 22:12:44 -04002508 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002509 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002511 .name.len = name->len,
2512 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002513 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002515 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002516 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002517 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002519 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2520 .rpc_argp = &args,
2521 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002523 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
Trond Myklebustd3468902010-04-16 16:22:50 -04002525 res.dir_attr = nfs_alloc_fattr();
2526 if (res.dir_attr == NULL)
2527 goto out;
2528
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002529 status = nfs4_call_sync(server, &msg, &args, &res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002530 if (status == 0) {
2531 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002532 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002533 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002534 nfs_free_fattr(res.dir_attr);
2535out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 return status;
2537}
2538
2539static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2540{
2541 struct nfs4_exception exception = { };
2542 int err;
2543 do {
2544 err = nfs4_handle_exception(NFS_SERVER(dir),
2545 _nfs4_proc_remove(dir, name),
2546 &exception);
2547 } while (exception.retry);
2548 return err;
2549}
2550
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002551static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002553 struct nfs_server *server = NFS_SERVER(dir);
2554 struct nfs_removeargs *args = msg->rpc_argp;
2555 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
Trond Myklebusta65318b2009-03-11 14:10:28 -04002557 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002558 res->server = server;
Benny Halevydfb4f3092010-09-24 09:17:01 -04002559 res->seq_res.sr_slot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561}
2562
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002563static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002565 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2566
Trond Myklebust14516c32010-07-31 14:29:06 -04002567 if (!nfs4_sequence_done(task, &res->seq_res))
2568 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002569 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002570 return 0;
2571 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002572 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002573 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574}
2575
Jeff Laytond3d41522010-09-17 17:31:57 -04002576static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2577{
2578 struct nfs_server *server = NFS_SERVER(dir);
2579 struct nfs_renameargs *arg = msg->rpc_argp;
2580 struct nfs_renameres *res = msg->rpc_resp;
2581
2582 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2583 arg->bitmask = server->attr_bitmask;
2584 res->server = server;
2585}
2586
2587static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2588 struct inode *new_dir)
2589{
2590 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2591
2592 if (!nfs4_sequence_done(task, &res->seq_res))
2593 return 0;
2594 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2595 return 0;
2596
2597 update_changeattr(old_dir, &res->old_cinfo);
2598 nfs_post_op_update_inode(old_dir, res->old_fattr);
2599 update_changeattr(new_dir, &res->new_cinfo);
2600 nfs_post_op_update_inode(new_dir, res->new_fattr);
2601 return 1;
2602}
2603
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2605 struct inode *new_dir, struct qstr *new_name)
2606{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002607 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002608 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 .old_dir = NFS_FH(old_dir),
2610 .new_dir = NFS_FH(new_dir),
2611 .old_name = old_name,
2612 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002613 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002615 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002616 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002617 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 struct rpc_message msg = {
2619 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2620 .rpc_argp = &arg,
2621 .rpc_resp = &res,
2622 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002623 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624
Trond Myklebust011fff72010-04-16 16:22:49 -04002625 res.old_fattr = nfs_alloc_fattr();
2626 res.new_fattr = nfs_alloc_fattr();
2627 if (res.old_fattr == NULL || res.new_fattr == NULL)
2628 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
Trond Myklebust011fff72010-04-16 16:22:49 -04002630 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 if (!status) {
2632 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002633 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002635 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002637out:
2638 nfs_free_fattr(res.new_fattr);
2639 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 return status;
2641}
2642
2643static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2644 struct inode *new_dir, struct qstr *new_name)
2645{
2646 struct nfs4_exception exception = { };
2647 int err;
2648 do {
2649 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2650 _nfs4_proc_rename(old_dir, old_name,
2651 new_dir, new_name),
2652 &exception);
2653 } while (exception.retry);
2654 return err;
2655}
2656
2657static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2658{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002659 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 struct nfs4_link_arg arg = {
2661 .fh = NFS_FH(inode),
2662 .dir_fh = NFS_FH(dir),
2663 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002664 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002666 struct nfs4_link_res res = {
2667 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002668 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 struct rpc_message msg = {
2670 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2671 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002672 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002674 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
Trond Myklebust136f2622010-04-16 16:22:49 -04002676 res.fattr = nfs_alloc_fattr();
2677 res.dir_attr = nfs_alloc_fattr();
2678 if (res.fattr == NULL || res.dir_attr == NULL)
2679 goto out;
2680
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002681 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002682 if (!status) {
2683 update_changeattr(dir, &res.cinfo);
2684 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002685 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002686 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002687out:
2688 nfs_free_fattr(res.dir_attr);
2689 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 return status;
2691}
2692
2693static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2694{
2695 struct nfs4_exception exception = { };
2696 int err;
2697 do {
2698 err = nfs4_handle_exception(NFS_SERVER(inode),
2699 _nfs4_proc_link(inode, dir, name),
2700 &exception);
2701 } while (exception.retry);
2702 return err;
2703}
2704
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002705struct nfs4_createdata {
2706 struct rpc_message msg;
2707 struct nfs4_create_arg arg;
2708 struct nfs4_create_res res;
2709 struct nfs_fh fh;
2710 struct nfs_fattr fattr;
2711 struct nfs_fattr dir_fattr;
2712};
2713
2714static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2715 struct qstr *name, struct iattr *sattr, u32 ftype)
2716{
2717 struct nfs4_createdata *data;
2718
2719 data = kzalloc(sizeof(*data), GFP_KERNEL);
2720 if (data != NULL) {
2721 struct nfs_server *server = NFS_SERVER(dir);
2722
2723 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2724 data->msg.rpc_argp = &data->arg;
2725 data->msg.rpc_resp = &data->res;
2726 data->arg.dir_fh = NFS_FH(dir);
2727 data->arg.server = server;
2728 data->arg.name = name;
2729 data->arg.attrs = sattr;
2730 data->arg.ftype = ftype;
2731 data->arg.bitmask = server->attr_bitmask;
2732 data->res.server = server;
2733 data->res.fh = &data->fh;
2734 data->res.fattr = &data->fattr;
2735 data->res.dir_fattr = &data->dir_fattr;
2736 nfs_fattr_init(data->res.fattr);
2737 nfs_fattr_init(data->res.dir_fattr);
2738 }
2739 return data;
2740}
2741
2742static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2743{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002744 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2745 &data->arg, &data->res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002746 if (status == 0) {
2747 update_changeattr(dir, &data->res.dir_cinfo);
2748 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2749 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2750 }
2751 return status;
2752}
2753
2754static void nfs4_free_createdata(struct nfs4_createdata *data)
2755{
2756 kfree(data);
2757}
2758
Chuck Lever4f390c12006-08-22 20:06:22 -04002759static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002760 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002762 struct nfs4_createdata *data;
2763 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764
Chuck Lever94a6d752006-08-22 20:06:23 -04002765 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002766 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002767
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002768 status = -ENOMEM;
2769 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2770 if (data == NULL)
2771 goto out;
2772
2773 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2774 data->arg.u.symlink.pages = &page;
2775 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002777 status = nfs4_do_create(dir, dentry, data);
2778
2779 nfs4_free_createdata(data);
2780out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 return status;
2782}
2783
Chuck Lever4f390c12006-08-22 20:06:22 -04002784static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002785 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786{
2787 struct nfs4_exception exception = { };
2788 int err;
2789 do {
2790 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002791 _nfs4_proc_symlink(dir, dentry, page,
2792 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 &exception);
2794 } while (exception.retry);
2795 return err;
2796}
2797
2798static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2799 struct iattr *sattr)
2800{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002801 struct nfs4_createdata *data;
2802 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002804 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2805 if (data == NULL)
2806 goto out;
2807
2808 status = nfs4_do_create(dir, dentry, data);
2809
2810 nfs4_free_createdata(data);
2811out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 return status;
2813}
2814
2815static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2816 struct iattr *sattr)
2817{
2818 struct nfs4_exception exception = { };
2819 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002820
2821 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 do {
2823 err = nfs4_handle_exception(NFS_SERVER(dir),
2824 _nfs4_proc_mkdir(dir, dentry, sattr),
2825 &exception);
2826 } while (exception.retry);
2827 return err;
2828}
2829
2830static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002831 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832{
2833 struct inode *dir = dentry->d_inode;
2834 struct nfs4_readdir_arg args = {
2835 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002836 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 .pgbase = 0,
2838 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002839 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04002840 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 };
2842 struct nfs4_readdir_res res;
2843 struct rpc_message msg = {
2844 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2845 .rpc_argp = &args,
2846 .rpc_resp = &res,
2847 .rpc_cred = cred,
2848 };
2849 int status;
2850
Harvey Harrison3110ff82008-05-02 13:42:44 -07002851 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002852 dentry->d_parent->d_name.name,
2853 dentry->d_name.name,
2854 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2856 res.pgbase = args.pgbase;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002857 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
Trond Myklebustac396122010-11-15 20:26:22 -05002858 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05002860 status += args.pgbase;
2861 }
Trond Myklebustc4812992007-09-28 17:11:45 -04002862
2863 nfs_invalidate_atime(dir);
2864
Harvey Harrison3110ff82008-05-02 13:42:44 -07002865 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 return status;
2867}
2868
2869static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002870 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871{
2872 struct nfs4_exception exception = { };
2873 int err;
2874 do {
2875 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2876 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002877 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 &exception);
2879 } while (exception.retry);
2880 return err;
2881}
2882
2883static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2884 struct iattr *sattr, dev_t rdev)
2885{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002886 struct nfs4_createdata *data;
2887 int mode = sattr->ia_mode;
2888 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889
2890 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2891 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002892
2893 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2894 if (data == NULL)
2895 goto out;
2896
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002898 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002900 data->arg.ftype = NF4BLK;
2901 data->arg.u.device.specdata1 = MAJOR(rdev);
2902 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 }
2904 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002905 data->arg.ftype = NF4CHR;
2906 data->arg.u.device.specdata1 = MAJOR(rdev);
2907 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002910 status = nfs4_do_create(dir, dentry, data);
2911
2912 nfs4_free_createdata(data);
2913out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 return status;
2915}
2916
2917static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2918 struct iattr *sattr, dev_t rdev)
2919{
2920 struct nfs4_exception exception = { };
2921 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002922
2923 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 do {
2925 err = nfs4_handle_exception(NFS_SERVER(dir),
2926 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2927 &exception);
2928 } while (exception.retry);
2929 return err;
2930}
2931
2932static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2933 struct nfs_fsstat *fsstat)
2934{
2935 struct nfs4_statfs_arg args = {
2936 .fh = fhandle,
2937 .bitmask = server->attr_bitmask,
2938 };
Benny Halevy24ad1482009-04-01 09:21:56 -04002939 struct nfs4_statfs_res res = {
2940 .fsstat = fsstat,
2941 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 struct rpc_message msg = {
2943 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2944 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04002945 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 };
2947
Trond Myklebust0e574af2005-10-27 22:12:38 -04002948 nfs_fattr_init(fsstat->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002949 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950}
2951
2952static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2953{
2954 struct nfs4_exception exception = { };
2955 int err;
2956 do {
2957 err = nfs4_handle_exception(server,
2958 _nfs4_proc_statfs(server, fhandle, fsstat),
2959 &exception);
2960 } while (exception.retry);
2961 return err;
2962}
2963
2964static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2965 struct nfs_fsinfo *fsinfo)
2966{
2967 struct nfs4_fsinfo_arg args = {
2968 .fh = fhandle,
2969 .bitmask = server->attr_bitmask,
2970 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04002971 struct nfs4_fsinfo_res res = {
2972 .fsinfo = fsinfo,
2973 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 struct rpc_message msg = {
2975 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2976 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04002977 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 };
2979
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002980 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981}
2982
2983static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2984{
2985 struct nfs4_exception exception = { };
2986 int err;
2987
2988 do {
2989 err = nfs4_handle_exception(server,
2990 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2991 &exception);
2992 } while (exception.retry);
2993 return err;
2994}
2995
2996static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2997{
Trond Myklebust0e574af2005-10-27 22:12:38 -04002998 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3000}
3001
3002static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3003 struct nfs_pathconf *pathconf)
3004{
3005 struct nfs4_pathconf_arg args = {
3006 .fh = fhandle,
3007 .bitmask = server->attr_bitmask,
3008 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003009 struct nfs4_pathconf_res res = {
3010 .pathconf = pathconf,
3011 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 struct rpc_message msg = {
3013 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3014 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003015 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 };
3017
3018 /* None of the pathconf attributes are mandatory to implement */
3019 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3020 memset(pathconf, 0, sizeof(*pathconf));
3021 return 0;
3022 }
3023
Trond Myklebust0e574af2005-10-27 22:12:38 -04003024 nfs_fattr_init(pathconf->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003025 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026}
3027
3028static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3029 struct nfs_pathconf *pathconf)
3030{
3031 struct nfs4_exception exception = { };
3032 int err;
3033
3034 do {
3035 err = nfs4_handle_exception(server,
3036 _nfs4_proc_pathconf(server, fhandle, pathconf),
3037 &exception);
3038 } while (exception.retry);
3039 return err;
3040}
3041
Trond Myklebustec06c092006-03-20 13:44:27 -05003042static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043{
Trond Myklebustec06c092006-03-20 13:44:27 -05003044 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003046 dprintk("--> %s\n", __func__);
3047
Trond Myklebust14516c32010-07-31 14:29:06 -04003048 if (!nfs4_sequence_done(task, &data->res.seq_res))
3049 return -EAGAIN;
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003050
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003051 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003052 nfs_restart_rpc(task, server->nfs_client);
Trond Myklebustec06c092006-03-20 13:44:27 -05003053 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003055
3056 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003058 renew_lease(server, data->timestamp);
3059 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060}
3061
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003062static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 data->timestamp = jiffies;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003065 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066}
3067
Trond Myklebust788e7a82006-03-20 13:44:27 -05003068static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 struct inode *inode = data->inode;
3071
Trond Myklebust14516c32010-07-31 14:29:06 -04003072 if (!nfs4_sequence_done(task, &data->res.seq_res))
3073 return -EAGAIN;
Andy Adamsondef6ed72009-04-01 09:22:26 -04003074
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003075 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003076 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003077 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003079 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003081 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003082 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003083 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084}
3085
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003086static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003088 struct nfs_server *server = NFS_SERVER(data->inode);
3089
Trond Myklebusta65318b2009-03-11 14:10:28 -04003090 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003091 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 data->timestamp = jiffies;
3093
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003094 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095}
3096
Trond Myklebust788e7a82006-03-20 13:44:27 -05003097static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 struct inode *inode = data->inode;
3100
Trond Myklebust14516c32010-07-31 14:29:06 -04003101 if (!nfs4_sequence_done(task, &data->res.seq_res))
3102 return -EAGAIN;
3103
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003104 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003105 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003106 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003108 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003109 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110}
3111
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003112static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003114 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115
Trond Myklebusta65318b2009-03-11 14:10:28 -04003116 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003117 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003118 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119}
3120
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003121struct nfs4_renewdata {
3122 struct nfs_client *client;
3123 unsigned long timestamp;
3124};
3125
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126/*
3127 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3128 * standalone procedure for queueing an asynchronous RENEW.
3129 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003130static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003131{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003132 struct nfs4_renewdata *data = calldata;
3133 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003134
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003135 if (atomic_read(&clp->cl_count) > 1)
3136 nfs4_schedule_state_renewal(clp);
3137 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003138 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003139}
3140
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003141static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003143 struct nfs4_renewdata *data = calldata;
3144 struct nfs_client *clp = data->client;
3145 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146
3147 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003148 /* Unless we're shutting down, schedule state recovery! */
3149 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3150 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 return;
3152 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003153 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154}
3155
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003156static const struct rpc_call_ops nfs4_renew_ops = {
3157 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003158 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003159};
3160
David Howellsadfa6f92006-08-22 20:06:08 -04003161int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162{
3163 struct rpc_message msg = {
3164 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3165 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003166 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003168 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003170 if (!atomic_inc_not_zero(&clp->cl_count))
3171 return -EIO;
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003172 data = kmalloc(sizeof(*data), GFP_KERNEL);
3173 if (data == NULL)
3174 return -ENOMEM;
3175 data->client = clp;
3176 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003178 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179}
3180
David Howellsadfa6f92006-08-22 20:06:08 -04003181int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182{
3183 struct rpc_message msg = {
3184 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3185 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003186 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 };
3188 unsigned long now = jiffies;
3189 int status;
3190
3191 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3192 if (status < 0)
3193 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003194 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 return 0;
3196}
3197
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003198static inline int nfs4_server_supports_acls(struct nfs_server *server)
3199{
3200 return (server->caps & NFS_CAP_ACLS)
3201 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3202 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3203}
3204
3205/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3206 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3207 * the stack.
3208 */
3209#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3210
3211static void buf_to_pages(const void *buf, size_t buflen,
3212 struct page **pages, unsigned int *pgbase)
3213{
3214 const void *p = buf;
3215
3216 *pgbase = offset_in_page(buf);
3217 p -= *pgbase;
3218 while (p < buf + buflen) {
3219 *(pages++) = virt_to_page(p);
3220 p += PAGE_CACHE_SIZE;
3221 }
3222}
3223
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003224struct nfs4_cached_acl {
3225 int cached;
3226 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003227 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003228};
3229
3230static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003231{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003232 struct nfs_inode *nfsi = NFS_I(inode);
3233
3234 spin_lock(&inode->i_lock);
3235 kfree(nfsi->nfs4_acl);
3236 nfsi->nfs4_acl = acl;
3237 spin_unlock(&inode->i_lock);
3238}
3239
3240static void nfs4_zap_acl_attr(struct inode *inode)
3241{
3242 nfs4_set_cached_acl(inode, NULL);
3243}
3244
3245static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3246{
3247 struct nfs_inode *nfsi = NFS_I(inode);
3248 struct nfs4_cached_acl *acl;
3249 int ret = -ENOENT;
3250
3251 spin_lock(&inode->i_lock);
3252 acl = nfsi->nfs4_acl;
3253 if (acl == NULL)
3254 goto out;
3255 if (buf == NULL) /* user is just asking for length */
3256 goto out_len;
3257 if (acl->cached == 0)
3258 goto out;
3259 ret = -ERANGE; /* see getxattr(2) man page */
3260 if (acl->len > buflen)
3261 goto out;
3262 memcpy(buf, acl->data, acl->len);
3263out_len:
3264 ret = acl->len;
3265out:
3266 spin_unlock(&inode->i_lock);
3267 return ret;
3268}
3269
3270static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3271{
3272 struct nfs4_cached_acl *acl;
3273
3274 if (buf && acl_len <= PAGE_SIZE) {
3275 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3276 if (acl == NULL)
3277 goto out;
3278 acl->cached = 1;
3279 memcpy(acl->data, buf, acl_len);
3280 } else {
3281 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3282 if (acl == NULL)
3283 goto out;
3284 acl->cached = 0;
3285 }
3286 acl->len = acl_len;
3287out:
3288 nfs4_set_cached_acl(inode, acl);
3289}
3290
Trond Myklebust16b42892006-08-24 12:27:15 -04003291static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003292{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003293 struct page *pages[NFS4ACL_MAXPAGES];
3294 struct nfs_getaclargs args = {
3295 .fh = NFS_FH(inode),
3296 .acl_pages = pages,
3297 .acl_len = buflen,
3298 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003299 struct nfs_getaclres res = {
3300 .acl_len = buflen,
3301 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003302 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003303 struct rpc_message msg = {
3304 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3305 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003306 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003307 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003308 struct page *localpage = NULL;
3309 int ret;
3310
3311 if (buflen < PAGE_SIZE) {
3312 /* As long as we're doing a round trip to the server anyway,
3313 * let's be prepared for a page of acl data. */
3314 localpage = alloc_page(GFP_KERNEL);
3315 resp_buf = page_address(localpage);
3316 if (localpage == NULL)
3317 return -ENOMEM;
3318 args.acl_pages[0] = localpage;
3319 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003320 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003321 } else {
3322 resp_buf = buf;
3323 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3324 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003325 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003326 if (ret)
3327 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003328 if (res.acl_len > args.acl_len)
3329 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003330 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003331 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003332 if (buf) {
3333 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003334 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003335 goto out_free;
3336 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003337 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003338 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003339 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003340out_free:
3341 if (localpage)
3342 __free_page(localpage);
3343 return ret;
3344}
3345
Trond Myklebust16b42892006-08-24 12:27:15 -04003346static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3347{
3348 struct nfs4_exception exception = { };
3349 ssize_t ret;
3350 do {
3351 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3352 if (ret >= 0)
3353 break;
3354 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3355 } while (exception.retry);
3356 return ret;
3357}
3358
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003359static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3360{
3361 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003362 int ret;
3363
3364 if (!nfs4_server_supports_acls(server))
3365 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003366 ret = nfs_revalidate_inode(server, inode);
3367 if (ret < 0)
3368 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003369 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3370 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003371 ret = nfs4_read_cached_acl(inode, buf, buflen);
3372 if (ret != -ENOENT)
3373 return ret;
3374 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003375}
3376
Trond Myklebust16b42892006-08-24 12:27:15 -04003377static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003378{
3379 struct nfs_server *server = NFS_SERVER(inode);
3380 struct page *pages[NFS4ACL_MAXPAGES];
3381 struct nfs_setaclargs arg = {
3382 .fh = NFS_FH(inode),
3383 .acl_pages = pages,
3384 .acl_len = buflen,
3385 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003386 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003387 struct rpc_message msg = {
3388 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3389 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003390 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003391 };
3392 int ret;
3393
3394 if (!nfs4_server_supports_acls(server))
3395 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07003396 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003397 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003398 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003399 /*
3400 * Acl update can result in inode attribute update.
3401 * so mark the attribute cache invalid.
3402 */
3403 spin_lock(&inode->i_lock);
3404 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3405 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003406 nfs_access_zap_cache(inode);
3407 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003408 return ret;
3409}
3410
Trond Myklebust16b42892006-08-24 12:27:15 -04003411static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3412{
3413 struct nfs4_exception exception = { };
3414 int err;
3415 do {
3416 err = nfs4_handle_exception(NFS_SERVER(inode),
3417 __nfs4_proc_set_acl(inode, buf, buflen),
3418 &exception);
3419 } while (exception.retry);
3420 return err;
3421}
3422
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003424nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003426 struct nfs_client *clp = server->nfs_client;
3427
3428 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 return 0;
3430 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003431 case -NFS4ERR_ADMIN_REVOKED:
3432 case -NFS4ERR_BAD_STATEID:
3433 case -NFS4ERR_OPENMODE:
3434 if (state == NULL)
3435 break;
3436 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003437 goto do_state_recovery;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003439 case -NFS4ERR_STALE_CLIENTID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 case -NFS4ERR_EXPIRED:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003441 goto do_state_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003442#if defined(CONFIG_NFS_V4_1)
3443 case -NFS4ERR_BADSESSION:
3444 case -NFS4ERR_BADSLOT:
3445 case -NFS4ERR_BAD_HIGH_SLOT:
3446 case -NFS4ERR_DEADSESSION:
3447 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3448 case -NFS4ERR_SEQ_FALSE_RETRY:
3449 case -NFS4ERR_SEQ_MISORDERED:
3450 dprintk("%s ERROR %d, Reset session\n", __func__,
3451 task->tk_status);
Andy Adamson0b9e2d42009-12-04 16:02:14 -05003452 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -04003453 task->tk_status = 0;
3454 return -EAGAIN;
3455#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003457 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003458 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003459 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3461 task->tk_status = 0;
3462 return -EAGAIN;
3463 case -NFS4ERR_OLD_STATEID:
3464 task->tk_status = 0;
3465 return -EAGAIN;
3466 }
3467 task->tk_status = nfs4_map_errors(task->tk_status);
3468 return 0;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003469do_state_recovery:
3470 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3471 nfs4_schedule_state_recovery(clp);
3472 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3473 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3474 task->tk_status = 0;
3475 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476}
3477
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003478int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3479 unsigned short port, struct rpc_cred *cred,
3480 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481{
3482 nfs4_verifier sc_verifier;
3483 struct nfs4_setclientid setclientid = {
3484 .sc_verifier = &sc_verifier,
3485 .sc_prog = program,
3486 };
3487 struct rpc_message msg = {
3488 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3489 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003490 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003491 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 };
Al Virobc4785c2006-10-19 23:28:51 -07003493 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 int loop = 0;
3495 int status;
3496
Al Virobc4785c2006-10-19 23:28:51 -07003497 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3499 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3500
3501 for(;;) {
3502 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003503 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003504 clp->cl_ipaddr,
3505 rpc_peeraddr2str(clp->cl_rpcclient,
3506 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003507 rpc_peeraddr2str(clp->cl_rpcclient,
3508 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003509 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510 clp->cl_id_uniquifier);
3511 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003512 sizeof(setclientid.sc_netid),
3513 rpc_peeraddr2str(clp->cl_rpcclient,
3514 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003516 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 clp->cl_ipaddr, port >> 8, port & 255);
3518
3519 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3520 if (status != -NFS4ERR_CLID_INUSE)
3521 break;
3522 if (signalled())
3523 break;
3524 if (loop++ & 1)
J. Bruce Fields611c96c2010-12-13 19:05:46 -05003525 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 else
3527 if (++clp->cl_id_uniquifier == 0)
3528 break;
3529 }
3530 return status;
3531}
3532
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003533static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3534 struct nfs4_setclientid_res *arg,
3535 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536{
3537 struct nfs_fsinfo fsinfo;
3538 struct rpc_message msg = {
3539 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003540 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003542 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 };
3544 unsigned long now;
3545 int status;
3546
3547 now = jiffies;
3548 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3549 if (status == 0) {
3550 spin_lock(&clp->cl_lock);
3551 clp->cl_lease_time = fsinfo.lease_time * HZ;
3552 clp->cl_last_renewal = now;
3553 spin_unlock(&clp->cl_lock);
3554 }
3555 return status;
3556}
3557
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003558int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3559 struct nfs4_setclientid_res *arg,
3560 struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003561{
Benny Halevy77e03672008-06-24 20:25:57 +03003562 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003563 int err;
3564 do {
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003565 err = _nfs4_proc_setclientid_confirm(clp, arg, cred);
Trond Myklebust51581f32006-03-20 13:44:47 -05003566 switch (err) {
3567 case 0:
3568 return err;
3569 case -NFS4ERR_RESOURCE:
3570 /* The IBM lawyers misread another document! */
3571 case -NFS4ERR_DELAY:
3572 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3573 }
3574 } while (err == 0);
3575 return err;
3576}
3577
Trond Myklebustfe650402006-01-03 09:55:18 +01003578struct nfs4_delegreturndata {
3579 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003580 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003581 struct nfs_fh fh;
3582 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003583 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003584 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003585 int rpc_status;
3586};
3587
Trond Myklebustfe650402006-01-03 09:55:18 +01003588static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3589{
3590 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003591
Trond Myklebust14516c32010-07-31 14:29:06 -04003592 if (!nfs4_sequence_done(task, &data->res.seq_res))
3593 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003594
Ricardo Labiaga79708862009-12-07 09:23:21 -05003595 switch (task->tk_status) {
3596 case -NFS4ERR_STALE_STATEID:
3597 case -NFS4ERR_EXPIRED:
3598 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003599 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003600 break;
3601 default:
3602 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3603 -EAGAIN) {
3604 nfs_restart_rpc(task, data->res.server->nfs_client);
3605 return;
3606 }
3607 }
3608 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003609}
3610
3611static void nfs4_delegreturn_release(void *calldata)
3612{
Trond Myklebustfe650402006-01-03 09:55:18 +01003613 kfree(calldata);
3614}
3615
Andy Adamson938e1012009-04-01 09:22:28 -04003616#if defined(CONFIG_NFS_V4_1)
3617static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3618{
3619 struct nfs4_delegreturndata *d_data;
3620
3621 d_data = (struct nfs4_delegreturndata *)data;
3622
Trond Myklebust035168ab2010-06-16 09:52:26 -04003623 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003624 &d_data->args.seq_args,
3625 &d_data->res.seq_res, 1, task))
3626 return;
3627 rpc_call_start(task);
3628}
3629#endif /* CONFIG_NFS_V4_1 */
3630
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003631static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003632#if defined(CONFIG_NFS_V4_1)
3633 .rpc_call_prepare = nfs4_delegreturn_prepare,
3634#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003635 .rpc_call_done = nfs4_delegreturn_done,
3636 .rpc_release = nfs4_delegreturn_release,
3637};
3638
Trond Myklebuste6f81072008-01-24 18:14:34 -05003639static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003640{
3641 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003642 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003643 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003644 struct rpc_message msg = {
3645 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3646 .rpc_cred = cred,
3647 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003648 struct rpc_task_setup task_setup_data = {
3649 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003650 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003651 .callback_ops = &nfs4_delegreturn_ops,
3652 .flags = RPC_TASK_ASYNC,
3653 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003654 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003655
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003656 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003657 if (data == NULL)
3658 return -ENOMEM;
3659 data->args.fhandle = &data->fh;
3660 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003661 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003662 nfs_copy_fh(&data->fh, NFS_FH(inode));
3663 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003664 data->res.fattr = &data->fattr;
3665 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003666 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003667 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003668 data->rpc_status = 0;
3669
Trond Myklebustc970aa82007-07-14 15:39:59 -04003670 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05003671 msg.rpc_argp = &data->args;
3672 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003673 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003674 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003675 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003676 if (!issync)
3677 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003678 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003679 if (status != 0)
3680 goto out;
3681 status = data->rpc_status;
3682 if (status != 0)
3683 goto out;
3684 nfs_refresh_inode(inode, &data->fattr);
3685out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003686 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003687 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688}
3689
Trond Myklebuste6f81072008-01-24 18:14:34 -05003690int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691{
3692 struct nfs_server *server = NFS_SERVER(inode);
3693 struct nfs4_exception exception = { };
3694 int err;
3695 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003696 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 switch (err) {
3698 case -NFS4ERR_STALE_STATEID:
3699 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 case 0:
3701 return 0;
3702 }
3703 err = nfs4_handle_exception(server, err, &exception);
3704 } while (exception.retry);
3705 return err;
3706}
3707
3708#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3709#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3710
3711/*
3712 * sleep, with exponential backoff, and retry the LOCK operation.
3713 */
3714static unsigned long
3715nfs4_set_lock_task_retry(unsigned long timeout)
3716{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003717 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 timeout <<= 1;
3719 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3720 return NFS4_LOCK_MAXTIMEOUT;
3721 return timeout;
3722}
3723
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3725{
3726 struct inode *inode = state->inode;
3727 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003728 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003729 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003731 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003733 struct nfs_lockt_res res = {
3734 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 };
3736 struct rpc_message msg = {
3737 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3738 .rpc_argp = &arg,
3739 .rpc_resp = &res,
3740 .rpc_cred = state->owner->so_cred,
3741 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 struct nfs4_lock_state *lsp;
3743 int status;
3744
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003745 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003746 status = nfs4_set_lock_state(state, request);
3747 if (status != 0)
3748 goto out;
3749 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003750 arg.lock_owner.id = lsp->ls_id.id;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003751 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003752 switch (status) {
3753 case 0:
3754 request->fl_type = F_UNLCK;
3755 break;
3756 case -NFS4ERR_DENIED:
3757 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003759 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003760out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 return status;
3762}
3763
3764static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3765{
3766 struct nfs4_exception exception = { };
3767 int err;
3768
3769 do {
3770 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3771 _nfs4_proc_getlk(state, cmd, request),
3772 &exception);
3773 } while (exception.retry);
3774 return err;
3775}
3776
3777static int do_vfs_lock(struct file *file, struct file_lock *fl)
3778{
3779 int res = 0;
3780 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3781 case FL_POSIX:
3782 res = posix_lock_file_wait(file, fl);
3783 break;
3784 case FL_FLOCK:
3785 res = flock_lock_file_wait(file, fl);
3786 break;
3787 default:
3788 BUG();
3789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 return res;
3791}
3792
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003793struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003794 struct nfs_locku_args arg;
3795 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003796 struct nfs4_lock_state *lsp;
3797 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003798 struct file_lock fl;
3799 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003800 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003801};
3802
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003803static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3804 struct nfs_open_context *ctx,
3805 struct nfs4_lock_state *lsp,
3806 struct nfs_seqid *seqid)
3807{
3808 struct nfs4_unlockdata *p;
3809 struct inode *inode = lsp->ls_state->inode;
3810
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003811 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003812 if (p == NULL)
3813 return NULL;
3814 p->arg.fh = NFS_FH(inode);
3815 p->arg.fl = &p->fl;
3816 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003817 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003818 p->arg.stateid = &lsp->ls_stateid;
3819 p->lsp = lsp;
3820 atomic_inc(&lsp->ls_count);
3821 /* Ensure we don't close file until we're done freeing locks! */
3822 p->ctx = get_nfs_open_context(ctx);
3823 memcpy(&p->fl, fl, sizeof(p->fl));
3824 p->server = NFS_SERVER(inode);
3825 return p;
3826}
3827
Trond Myklebust06f814a2006-01-03 09:55:07 +01003828static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003829{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003830 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003831 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003832 nfs4_put_lock_state(calldata->lsp);
3833 put_nfs_open_context(calldata->ctx);
3834 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003835}
3836
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003837static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003838{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003839 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003840
Trond Myklebust14516c32010-07-31 14:29:06 -04003841 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3842 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003843 switch (task->tk_status) {
3844 case 0:
3845 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003846 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003847 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003848 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003849 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003850 case -NFS4ERR_BAD_STATEID:
3851 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003852 case -NFS4ERR_STALE_STATEID:
3853 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003854 break;
3855 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003856 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebust0110ee12009-12-07 09:00:24 -05003857 nfs_restart_rpc(task,
Trond Myklebustd61e6122009-12-05 19:32:19 -05003858 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003859 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003860}
3861
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003862static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003863{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003864 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003866 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003867 return;
3868 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003869 /* Note: exit _without_ running nfs4_locku_done */
3870 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003871 return;
3872 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003873 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04003874 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04003875 &calldata->arg.seq_args,
3876 &calldata->res.seq_res, 1, task))
3877 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003878 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879}
3880
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003881static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003882 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003883 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003884 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003885};
3886
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003887static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3888 struct nfs_open_context *ctx,
3889 struct nfs4_lock_state *lsp,
3890 struct nfs_seqid *seqid)
3891{
3892 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003893 struct rpc_message msg = {
3894 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3895 .rpc_cred = ctx->cred,
3896 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003897 struct rpc_task_setup task_setup_data = {
3898 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003899 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003900 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003901 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003902 .flags = RPC_TASK_ASYNC,
3903 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003904
Frank Filz137d6ac2007-07-09 15:32:29 -07003905 /* Ensure this is an unlock - when canceling a lock, the
3906 * canceled lock is passed in, and it won't be an unlock.
3907 */
3908 fl->fl_type = F_UNLCK;
3909
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003910 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3911 if (data == NULL) {
3912 nfs_free_seqid(seqid);
3913 return ERR_PTR(-ENOMEM);
3914 }
3915
Trond Myklebust1174dd12010-12-21 10:52:24 -05003916 msg.rpc_argp = &data->arg;
3917 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003918 task_setup_data.callback_data = data;
3919 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003920}
3921
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3923{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003924 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003925 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003926 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003927 struct rpc_task *task;
3928 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003929 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930
Trond Myklebust9b073572006-06-29 16:38:34 -04003931 status = nfs4_set_lock_state(state, request);
3932 /* Unlock _before_ we do the RPC call */
3933 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003934 down_read(&nfsi->rwsem);
3935 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3936 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003937 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003938 }
3939 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003940 if (status != 0)
3941 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003942 /* Is this a delegated lock? */
3943 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04003944 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003945 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003946 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04003947 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003948 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04003949 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003950 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003951 status = PTR_ERR(task);
3952 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04003953 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003954 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003955 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04003956out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003957 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003958 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959}
3960
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003961struct nfs4_lockdata {
3962 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003963 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003964 struct nfs4_lock_state *lsp;
3965 struct nfs_open_context *ctx;
3966 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003967 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003968 int rpc_status;
3969 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04003970 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003971};
3972
3973static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003974 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
3975 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003976{
3977 struct nfs4_lockdata *p;
3978 struct inode *inode = lsp->ls_state->inode;
3979 struct nfs_server *server = NFS_SERVER(inode);
3980
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003981 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003982 if (p == NULL)
3983 return NULL;
3984
3985 p->arg.fh = NFS_FH(inode);
3986 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003987 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003988 if (p->arg.open_seqid == NULL)
3989 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003990 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003991 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003992 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003993 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04003994 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003995 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003996 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003997 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04003998 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003999 atomic_inc(&lsp->ls_count);
4000 p->ctx = get_nfs_open_context(ctx);
4001 memcpy(&p->fl, fl, sizeof(p->fl));
4002 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004003out_free_seqid:
4004 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004005out_free:
4006 kfree(p);
4007 return NULL;
4008}
4009
4010static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4011{
4012 struct nfs4_lockdata *data = calldata;
4013 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014
Harvey Harrison3110ff82008-05-02 13:42:44 -07004015 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004016 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4017 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004018 /* Do we need to do an open_to_lock_owner? */
4019 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004020 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4021 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004022 data->arg.open_stateid = &state->stateid;
4023 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004024 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004025 } else
4026 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004027 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04004028 if (nfs4_setup_sequence(data->server,
4029 &data->arg.seq_args,
Andy Adamson66179ef2009-04-01 09:22:22 -04004030 &data->res.seq_res, 1, task))
4031 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004032 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004033 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004034}
4035
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004036static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4037{
4038 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4039 nfs4_lock_prepare(task, calldata);
4040}
4041
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004042static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4043{
4044 struct nfs4_lockdata *data = calldata;
4045
Harvey Harrison3110ff82008-05-02 13:42:44 -07004046 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004047
Trond Myklebust14516c32010-07-31 14:29:06 -04004048 if (!nfs4_sequence_done(task, &data->res.seq_res))
4049 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004050
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004051 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004052 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004053 if (data->rpc_status == 0)
4054 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4055 else
4056 goto out;
4057 }
4058 if (data->rpc_status == 0) {
4059 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4060 sizeof(data->lsp->ls_stateid.data));
4061 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04004062 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004063 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004064out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004065 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004066}
4067
4068static void nfs4_lock_release(void *calldata)
4069{
4070 struct nfs4_lockdata *data = calldata;
4071
Harvey Harrison3110ff82008-05-02 13:42:44 -07004072 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004073 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004074 if (data->cancelled != 0) {
4075 struct rpc_task *task;
4076 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4077 data->arg.lock_seqid);
4078 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004079 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004080 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004081 } else
4082 nfs_free_seqid(data->arg.lock_seqid);
4083 nfs4_put_lock_state(data->lsp);
4084 put_nfs_open_context(data->ctx);
4085 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004086 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004087}
4088
4089static const struct rpc_call_ops nfs4_lock_ops = {
4090 .rpc_call_prepare = nfs4_lock_prepare,
4091 .rpc_call_done = nfs4_lock_done,
4092 .rpc_release = nfs4_lock_release,
4093};
4094
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004095static const struct rpc_call_ops nfs4_recover_lock_ops = {
4096 .rpc_call_prepare = nfs4_recover_lock_prepare,
4097 .rpc_call_done = nfs4_lock_done,
4098 .rpc_release = nfs4_lock_release,
4099};
4100
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004101static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4102{
4103 struct nfs_client *clp = server->nfs_client;
4104 struct nfs4_state *state = lsp->ls_state;
4105
4106 switch (error) {
4107 case -NFS4ERR_ADMIN_REVOKED:
4108 case -NFS4ERR_BAD_STATEID:
4109 case -NFS4ERR_EXPIRED:
4110 if (new_lock_owner != 0 ||
4111 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4112 nfs4_state_mark_reclaim_nograce(clp, state);
4113 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004114 break;
4115 case -NFS4ERR_STALE_STATEID:
4116 if (new_lock_owner != 0 ||
4117 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4118 nfs4_state_mark_reclaim_reboot(clp, state);
4119 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004120 };
4121}
4122
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004123static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004124{
4125 struct nfs4_lockdata *data;
4126 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004127 struct rpc_message msg = {
4128 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4129 .rpc_cred = state->owner->so_cred,
4130 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004131 struct rpc_task_setup task_setup_data = {
4132 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004133 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004134 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004135 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004136 .flags = RPC_TASK_ASYNC,
4137 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004138 int ret;
4139
Harvey Harrison3110ff82008-05-02 13:42:44 -07004140 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004141 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004142 fl->fl_u.nfs4_fl.owner,
4143 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004144 if (data == NULL)
4145 return -ENOMEM;
4146 if (IS_SETLKW(cmd))
4147 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004148 if (recovery_type > NFS_LOCK_NEW) {
4149 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004150 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004151 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4152 }
Trond Myklebust1174dd12010-12-21 10:52:24 -05004153 msg.rpc_argp = &data->arg;
4154 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004155 task_setup_data.callback_data = data;
4156 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004157 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004158 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004159 ret = nfs4_wait_for_completion_rpc_task(task);
4160 if (ret == 0) {
4161 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004162 if (ret)
4163 nfs4_handle_setlk_error(data->server, data->lsp,
4164 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004165 } else
4166 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004167 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004168 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004169 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170}
4171
4172static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4173{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004174 struct nfs_server *server = NFS_SERVER(state->inode);
4175 struct nfs4_exception exception = { };
4176 int err;
4177
4178 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004179 /* Cache the lock if possible... */
4180 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4181 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004182 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004183 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004184 break;
4185 nfs4_handle_exception(server, err, &exception);
4186 } while (exception.retry);
4187 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188}
4189
4190static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4191{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004192 struct nfs_server *server = NFS_SERVER(state->inode);
4193 struct nfs4_exception exception = { };
4194 int err;
4195
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004196 err = nfs4_set_lock_state(state, request);
4197 if (err != 0)
4198 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004199 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004200 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4201 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004202 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004203 switch (err) {
4204 default:
4205 goto out;
4206 case -NFS4ERR_GRACE:
4207 case -NFS4ERR_DELAY:
4208 nfs4_handle_exception(server, err, &exception);
4209 err = 0;
4210 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004211 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004212out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004213 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214}
4215
4216static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4217{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004218 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004219 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004220 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004222 if ((fl_flags & FL_POSIX) &&
4223 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4224 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004225 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004226 status = nfs4_set_lock_state(state, request);
4227 if (status != 0)
4228 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004229 request->fl_flags |= FL_ACCESS;
4230 status = do_vfs_lock(request->fl_file, request);
4231 if (status < 0)
4232 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004233 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004234 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004235 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004236 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004237 request->fl_flags = fl_flags & ~FL_SLEEP;
4238 status = do_vfs_lock(request->fl_file, request);
4239 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004240 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004241 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004242 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004243 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004244 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004245 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004246 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004247 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004248out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004249 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004250out:
4251 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 return status;
4253}
4254
4255static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4256{
4257 struct nfs4_exception exception = { };
4258 int err;
4259
4260 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004261 err = _nfs4_proc_setlk(state, cmd, request);
4262 if (err == -NFS4ERR_DENIED)
4263 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004265 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 } while (exception.retry);
4267 return err;
4268}
4269
4270static int
4271nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4272{
4273 struct nfs_open_context *ctx;
4274 struct nfs4_state *state;
4275 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4276 int status;
4277
4278 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004279 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 state = ctx->state;
4281
4282 if (request->fl_start < 0 || request->fl_end < 0)
4283 return -EINVAL;
4284
Trond Myklebustd9531262009-07-21 19:22:38 -04004285 if (IS_GETLK(cmd)) {
4286 if (state != NULL)
4287 return nfs4_proc_getlk(state, F_GETLK, request);
4288 return 0;
4289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290
4291 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4292 return -EINVAL;
4293
Trond Myklebustd9531262009-07-21 19:22:38 -04004294 if (request->fl_type == F_UNLCK) {
4295 if (state != NULL)
4296 return nfs4_proc_unlck(state, cmd, request);
4297 return 0;
4298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299
Trond Myklebustd9531262009-07-21 19:22:38 -04004300 if (state == NULL)
4301 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 do {
4303 status = nfs4_proc_setlk(state, cmd, request);
4304 if ((status != -EAGAIN) || IS_SETLK(cmd))
4305 break;
4306 timeout = nfs4_set_lock_task_retry(timeout);
4307 status = -ERESTARTSYS;
4308 if (signalled())
4309 break;
4310 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 return status;
4312}
4313
Trond Myklebust888e6942005-11-04 15:38:11 -05004314int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4315{
4316 struct nfs_server *server = NFS_SERVER(state->inode);
4317 struct nfs4_exception exception = { };
4318 int err;
4319
4320 err = nfs4_set_lock_state(state, fl);
4321 if (err != 0)
4322 goto out;
4323 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004324 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004325 switch (err) {
4326 default:
4327 printk(KERN_ERR "%s: unhandled error %d.\n",
4328 __func__, err);
4329 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004330 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004331 goto out;
4332 case -NFS4ERR_EXPIRED:
4333 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004334 case -NFS4ERR_STALE_STATEID:
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004335 case -NFS4ERR_BADSESSION:
4336 case -NFS4ERR_BADSLOT:
4337 case -NFS4ERR_BAD_HIGH_SLOT:
4338 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4339 case -NFS4ERR_DEADSESSION:
Trond Myklebustd5122202009-06-17 13:22:58 -07004340 nfs4_schedule_state_recovery(server->nfs_client);
4341 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004342 case -ERESTARTSYS:
4343 /*
4344 * The show must go on: exit, but mark the
4345 * stateid as needing recovery.
4346 */
4347 case -NFS4ERR_ADMIN_REVOKED:
4348 case -NFS4ERR_BAD_STATEID:
4349 case -NFS4ERR_OPENMODE:
4350 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4351 err = 0;
4352 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004353 case -EKEYEXPIRED:
4354 /*
4355 * User RPCSEC_GSS context has expired.
4356 * We cannot recover this stateid now, so
4357 * skip it and allow recovery thread to
4358 * proceed.
4359 */
4360 err = 0;
4361 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004362 case -ENOMEM:
4363 case -NFS4ERR_DENIED:
4364 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4365 err = 0;
4366 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004367 case -NFS4ERR_DELAY:
4368 break;
4369 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004370 err = nfs4_handle_exception(server, err, &exception);
4371 } while (exception.retry);
4372out:
4373 return err;
4374}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004375
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004376static void nfs4_release_lockowner_release(void *calldata)
4377{
4378 kfree(calldata);
4379}
4380
4381const struct rpc_call_ops nfs4_release_lockowner_ops = {
4382 .rpc_release = nfs4_release_lockowner_release,
4383};
4384
4385void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4386{
4387 struct nfs_server *server = lsp->ls_state->owner->so_server;
4388 struct nfs_release_lockowner_args *args;
4389 struct rpc_message msg = {
4390 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4391 };
4392
4393 if (server->nfs_client->cl_mvops->minor_version != 0)
4394 return;
4395 args = kmalloc(sizeof(*args), GFP_NOFS);
4396 if (!args)
4397 return;
4398 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4399 args->lock_owner.id = lsp->ls_id.id;
4400 msg.rpc_argp = args;
4401 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4402}
4403
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004404#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4405
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004406int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4407 size_t buflen, int flags)
4408{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004409 struct inode *inode = dentry->d_inode;
4410
4411 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4412 return -EOPNOTSUPP;
4413
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004414 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004415}
4416
4417/* The getxattr man page suggests returning -ENODATA for unknown attributes,
4418 * and that's what we'll do for e.g. user attributes that haven't been set.
4419 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4420 * attributes in kernel-managed attribute namespaces. */
4421ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4422 size_t buflen)
4423{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004424 struct inode *inode = dentry->d_inode;
4425
4426 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4427 return -EOPNOTSUPP;
4428
4429 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004430}
4431
4432ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4433{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004434 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004435
J. Bruce Fields096455a2006-03-20 23:23:42 -05004436 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4437 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004438 if (buf && buflen < len)
4439 return -ERANGE;
4440 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004441 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4442 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004443}
4444
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004445static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4446{
4447 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4448 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4449 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4450 return;
4451
4452 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4453 NFS_ATTR_FATTR_NLINK;
4454 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4455 fattr->nlink = 2;
4456}
4457
Trond Myklebust56659e92007-07-17 21:52:39 -04004458int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004459 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004460{
4461 struct nfs_server *server = NFS_SERVER(dir);
4462 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004463 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4464 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004465 };
4466 struct nfs4_fs_locations_arg args = {
4467 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004468 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004469 .page = page,
4470 .bitmask = bitmask,
4471 };
Benny Halevy22958462009-04-01 09:22:02 -04004472 struct nfs4_fs_locations_res res = {
4473 .fs_locations = fs_locations,
4474 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004475 struct rpc_message msg = {
4476 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4477 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004478 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004479 };
4480 int status;
4481
Harvey Harrison3110ff82008-05-02 13:42:44 -07004482 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004483 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004484 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004485 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04004486 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004487 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004488 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004489 return status;
4490}
4491
Andy Adamson557134a2009-04-01 09:21:53 -04004492#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004493/*
4494 * nfs4_proc_exchange_id()
4495 *
4496 * Since the clientid has expired, all compounds using sessions
4497 * associated with the stale clientid will be returning
4498 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4499 * be in some phase of session reset.
4500 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004501int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004502{
4503 nfs4_verifier verifier;
4504 struct nfs41_exchange_id_args args = {
4505 .client = clp,
4506 .flags = clp->cl_exchange_flags,
4507 };
4508 struct nfs41_exchange_id_res res = {
4509 .client = clp,
4510 };
4511 int status;
4512 struct rpc_message msg = {
4513 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4514 .rpc_argp = &args,
4515 .rpc_resp = &res,
4516 .rpc_cred = cred,
4517 };
4518 __be32 *p;
4519
4520 dprintk("--> %s\n", __func__);
4521 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004522
Alexandros Batsakis7b183d02009-12-05 13:33:25 -05004523 /* Remove server-only flags */
4524 args.flags &= ~EXCHGID4_FLAG_CONFIRMED_R;
4525
Benny Halevy99fe60d2009-04-01 09:22:29 -04004526 p = (u32 *)verifier.data;
4527 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4528 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4529 args.verifier = &verifier;
4530
4531 while (1) {
4532 args.id_len = scnprintf(args.id, sizeof(args.id),
4533 "%s/%s %u",
4534 clp->cl_ipaddr,
4535 rpc_peeraddr2str(clp->cl_rpcclient,
4536 RPC_DISPLAY_ADDR),
4537 clp->cl_id_uniquifier);
4538
4539 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4540
Andy Adamson180b62a2010-03-02 13:19:36 -05004541 if (status != -NFS4ERR_CLID_INUSE)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004542 break;
4543
4544 if (signalled())
4545 break;
4546
4547 if (++clp->cl_id_uniquifier == 0)
4548 break;
4549 }
4550
4551 dprintk("<-- %s status= %d\n", __func__, status);
4552 return status;
4553}
4554
Andy Adamson2050f0c2009-04-01 09:22:30 -04004555struct nfs4_get_lease_time_data {
4556 struct nfs4_get_lease_time_args *args;
4557 struct nfs4_get_lease_time_res *res;
4558 struct nfs_client *clp;
4559};
4560
4561static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4562 void *calldata)
4563{
4564 int ret;
4565 struct nfs4_get_lease_time_data *data =
4566 (struct nfs4_get_lease_time_data *)calldata;
4567
4568 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004569 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004570 /* just setup sequence, do not trigger session recovery
4571 since we're invoked within one */
4572 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004573 &data->args->la_seq_args,
4574 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004575
4576 BUG_ON(ret == -EAGAIN);
4577 rpc_call_start(task);
4578 dprintk("<-- %s\n", __func__);
4579}
4580
4581/*
4582 * Called from nfs4_state_manager thread for session setup, so don't recover
4583 * from sequence operation or clientid errors.
4584 */
4585static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4586{
4587 struct nfs4_get_lease_time_data *data =
4588 (struct nfs4_get_lease_time_data *)calldata;
4589
4590 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04004591 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4592 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04004593 switch (task->tk_status) {
4594 case -NFS4ERR_DELAY:
4595 case -NFS4ERR_GRACE:
4596 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4597 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4598 task->tk_status = 0;
Trond Myklebust0110ee12009-12-07 09:00:24 -05004599 nfs_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004600 return;
4601 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004602 dprintk("<-- %s\n", __func__);
4603}
4604
4605struct rpc_call_ops nfs4_get_lease_time_ops = {
4606 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4607 .rpc_call_done = nfs4_get_lease_time_done,
4608};
4609
4610int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4611{
4612 struct rpc_task *task;
4613 struct nfs4_get_lease_time_args args;
4614 struct nfs4_get_lease_time_res res = {
4615 .lr_fsinfo = fsinfo,
4616 };
4617 struct nfs4_get_lease_time_data data = {
4618 .args = &args,
4619 .res = &res,
4620 .clp = clp,
4621 };
4622 struct rpc_message msg = {
4623 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4624 .rpc_argp = &args,
4625 .rpc_resp = &res,
4626 };
4627 struct rpc_task_setup task_setup = {
4628 .rpc_client = clp->cl_rpcclient,
4629 .rpc_message = &msg,
4630 .callback_ops = &nfs4_get_lease_time_ops,
4631 .callback_data = &data
4632 };
4633 int status;
4634
Andy Adamson2050f0c2009-04-01 09:22:30 -04004635 dprintk("--> %s\n", __func__);
4636 task = rpc_run_task(&task_setup);
4637
4638 if (IS_ERR(task))
4639 status = PTR_ERR(task);
4640 else {
4641 status = task->tk_status;
4642 rpc_put_task(task);
4643 }
4644 dprintk("<-- %s return %d\n", __func__, status);
4645
4646 return status;
4647}
4648
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004649/*
4650 * Reset a slot table
4651 */
Andy Adamson104aeba2010-01-14 17:45:10 -05004652static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4653 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004654{
Andy Adamson104aeba2010-01-14 17:45:10 -05004655 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004656 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004657 int ret = 0;
4658
Andy Adamson104aeba2010-01-14 17:45:10 -05004659 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4660 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004661
Andy Adamson104aeba2010-01-14 17:45:10 -05004662 /* Does the newly negotiated max_reqs match the existing slot table? */
4663 if (max_reqs != tbl->max_slots) {
4664 ret = -ENOMEM;
4665 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004666 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05004667 if (!new)
4668 goto out;
4669 ret = 0;
4670 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004671 }
4672 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05004673 if (new) {
4674 tbl->slots = new;
4675 tbl->max_slots = max_reqs;
4676 }
4677 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004678 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004679 spin_unlock(&tbl->slot_tbl_lock);
4680 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4681 tbl, tbl->slots, tbl->max_slots);
4682out:
4683 dprintk("<-- %s: return %d\n", __func__, ret);
4684 return ret;
4685}
4686
Andy Adamsonfc931582009-04-01 09:22:31 -04004687/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004688 * Reset the forechannel and backchannel slot tables
4689 */
4690static int nfs4_reset_slot_tables(struct nfs4_session *session)
4691{
4692 int status;
4693
4694 status = nfs4_reset_slot_table(&session->fc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004695 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004696 if (status)
4697 return status;
4698
4699 status = nfs4_reset_slot_table(&session->bc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004700 session->bc_attrs.max_reqs, 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004701 return status;
4702}
4703
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004704/* Destroy the slot table */
4705static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4706{
4707 if (session->fc_slot_table.slots != NULL) {
4708 kfree(session->fc_slot_table.slots);
4709 session->fc_slot_table.slots = NULL;
4710 }
4711 if (session->bc_slot_table.slots != NULL) {
4712 kfree(session->bc_slot_table.slots);
4713 session->bc_slot_table.slots = NULL;
4714 }
4715 return;
4716}
4717
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004718/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004719 * Initialize slot table
4720 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004721static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4722 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04004723{
Andy Adamsonfc931582009-04-01 09:22:31 -04004724 struct nfs4_slot *slot;
4725 int ret = -ENOMEM;
4726
4727 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4728
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004729 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04004730
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004731 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04004732 if (!slot)
4733 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04004734 ret = 0;
4735
4736 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04004737 tbl->max_slots = max_slots;
4738 tbl->slots = slot;
4739 tbl->highest_used_slotid = -1; /* no slot is currently used */
4740 spin_unlock(&tbl->slot_tbl_lock);
4741 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4742 tbl, tbl->slots, tbl->max_slots);
4743out:
4744 dprintk("<-- %s: return %d\n", __func__, ret);
4745 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04004746}
4747
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004748/*
4749 * Initialize the forechannel and backchannel tables
4750 */
4751static int nfs4_init_slot_tables(struct nfs4_session *session)
4752{
Trond Myklebustf26468f2009-12-05 19:32:11 -05004753 struct nfs4_slot_table *tbl;
4754 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004755
Trond Myklebustf26468f2009-12-05 19:32:11 -05004756 tbl = &session->fc_slot_table;
4757 if (tbl->slots == NULL) {
4758 status = nfs4_init_slot_table(tbl,
4759 session->fc_attrs.max_reqs, 1);
4760 if (status)
4761 return status;
4762 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004763
Trond Myklebustf26468f2009-12-05 19:32:11 -05004764 tbl = &session->bc_slot_table;
4765 if (tbl->slots == NULL) {
4766 status = nfs4_init_slot_table(tbl,
4767 session->bc_attrs.max_reqs, 0);
4768 if (status)
4769 nfs4_destroy_slot_tables(session);
4770 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004771
4772 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04004773}
4774
4775struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4776{
4777 struct nfs4_session *session;
4778 struct nfs4_slot_table *tbl;
4779
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004780 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04004781 if (!session)
4782 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04004783
Andy Adamsonea028ac2009-12-04 15:55:38 -05004784 init_completion(&session->complete);
Andy Adamson76db6d92009-04-01 09:22:38 -04004785
Andy Adamson557134a2009-04-01 09:21:53 -04004786 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004787 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04004788 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004789 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004790
4791 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004792 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004793 spin_lock_init(&tbl->slot_tbl_lock);
4794 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4795
Trond Myklebust1055d762010-06-16 09:52:27 -04004796 session->session_state = 1<<NFS4_SESSION_INITING;
4797
Andy Adamson557134a2009-04-01 09:21:53 -04004798 session->clp = clp;
4799 return session;
4800}
4801
4802void nfs4_destroy_session(struct nfs4_session *session)
4803{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04004804 nfs4_proc_destroy_session(session);
4805 dprintk("%s Destroy backchannel for xprt %p\n",
4806 __func__, session->clp->cl_rpcclient->cl_xprt);
4807 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4808 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004809 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04004810 kfree(session);
4811}
4812
Andy Adamsonfc931582009-04-01 09:22:31 -04004813/*
4814 * Initialize the values to be used by the client in CREATE_SESSION
4815 * If nfs4_init_session set the fore channel request and response sizes,
4816 * use them.
4817 *
4818 * Set the back channel max_resp_sz_cached to zero to force the client to
4819 * always set csa_cachethis to FALSE because the current implementation
4820 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4821 */
4822static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4823{
4824 struct nfs4_session *session = args->client->cl_session;
4825 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4826 mxresp_sz = session->fc_attrs.max_resp_sz;
4827
4828 if (mxrqst_sz == 0)
4829 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4830 if (mxresp_sz == 0)
4831 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4832 /* Fore channel attributes */
4833 args->fc_attrs.headerpadsz = 0;
4834 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4835 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04004836 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4837 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4838
4839 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05004840 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04004841 __func__,
4842 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05004843 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04004844
4845 /* Back channel attributes */
4846 args->bc_attrs.headerpadsz = 0;
4847 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4848 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4849 args->bc_attrs.max_resp_sz_cached = 0;
4850 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4851 args->bc_attrs.max_reqs = 1;
4852
4853 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4854 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4855 __func__,
4856 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4857 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4858 args->bc_attrs.max_reqs);
4859}
4860
J. Bruce Fields43c2e882010-10-02 15:19:01 -04004861static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04004862{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04004863 struct nfs4_channel_attrs *sent = &args->fc_attrs;
4864 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
4865
4866 if (rcvd->headerpadsz > sent->headerpadsz)
4867 return -EINVAL;
4868 if (rcvd->max_resp_sz > sent->max_resp_sz)
4869 return -EINVAL;
4870 /*
4871 * Our requested max_ops is the minimum we need; we're not
4872 * prepared to break up compounds into smaller pieces than that.
4873 * So, no point even trying to continue if the server won't
4874 * cooperate:
4875 */
4876 if (rcvd->max_ops < sent->max_ops)
4877 return -EINVAL;
4878 if (rcvd->max_reqs == 0)
4879 return -EINVAL;
4880 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04004881}
4882
J. Bruce Fields43c2e882010-10-02 15:19:01 -04004883static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
4884{
4885 struct nfs4_channel_attrs *sent = &args->bc_attrs;
4886 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04004887
J. Bruce Fields43c2e882010-10-02 15:19:01 -04004888 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
4889 return -EINVAL;
4890 if (rcvd->max_resp_sz < sent->max_resp_sz)
4891 return -EINVAL;
4892 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
4893 return -EINVAL;
4894 /* These would render the backchannel useless: */
4895 if (rcvd->max_ops == 0)
4896 return -EINVAL;
4897 if (rcvd->max_reqs == 0)
4898 return -EINVAL;
4899 return 0;
4900}
Andy Adamson8d353012009-04-01 09:22:32 -04004901
Andy Adamson8d353012009-04-01 09:22:32 -04004902static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4903 struct nfs4_session *session)
4904{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04004905 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04004906
J. Bruce Fields43c2e882010-10-02 15:19:01 -04004907 ret = nfs4_verify_fore_channel_attrs(args, session);
4908 if (ret)
4909 return ret;
4910 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04004911}
4912
Andy Adamsonfc931582009-04-01 09:22:31 -04004913static int _nfs4_proc_create_session(struct nfs_client *clp)
4914{
4915 struct nfs4_session *session = clp->cl_session;
4916 struct nfs41_create_session_args args = {
4917 .client = clp,
4918 .cb_program = NFS4_CALLBACK,
4919 };
4920 struct nfs41_create_session_res res = {
4921 .client = clp,
4922 };
4923 struct rpc_message msg = {
4924 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4925 .rpc_argp = &args,
4926 .rpc_resp = &res,
4927 };
4928 int status;
4929
4930 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04004931 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04004932
4933 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4934
Andy Adamson8d353012009-04-01 09:22:32 -04004935 if (!status)
4936 /* Verify the session's negotiated channel_attrs values */
4937 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04004938 if (!status) {
4939 /* Increment the clientid slot sequence id */
4940 clp->cl_seqid++;
4941 }
4942
4943 return status;
4944}
4945
4946/*
4947 * Issues a CREATE_SESSION operation to the server.
4948 * It is the responsibility of the caller to verify the session is
4949 * expired before calling this routine.
4950 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05004951int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04004952{
4953 int status;
4954 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04004955 struct nfs4_session *session = clp->cl_session;
4956
4957 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
4958
4959 status = _nfs4_proc_create_session(clp);
4960 if (status)
4961 goto out;
4962
Trond Myklebustf26468f2009-12-05 19:32:11 -05004963 /* Init and reset the fore channel */
4964 status = nfs4_init_slot_tables(session);
4965 dprintk("slot table initialization returned %d\n", status);
4966 if (status)
4967 goto out;
4968 status = nfs4_reset_slot_tables(session);
4969 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04004970 if (status)
4971 goto out;
4972
4973 ptr = (unsigned *)&session->sess_id.data[0];
4974 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
4975 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04004976out:
4977 dprintk("<-- %s\n", __func__);
4978 return status;
4979}
4980
Andy Adamson0f3e66c2009-04-01 09:22:34 -04004981/*
4982 * Issue the over-the-wire RPC DESTROY_SESSION.
4983 * The caller must serialize access to this routine.
4984 */
4985int nfs4_proc_destroy_session(struct nfs4_session *session)
4986{
4987 int status = 0;
4988 struct rpc_message msg;
4989
4990 dprintk("--> nfs4_proc_destroy_session\n");
4991
4992 /* session is still being setup */
4993 if (session->clp->cl_cons_state != NFS_CS_READY)
4994 return status;
4995
4996 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
4997 msg.rpc_argp = session;
4998 msg.rpc_resp = NULL;
4999 msg.rpc_cred = NULL;
5000 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5001
5002 if (status)
5003 printk(KERN_WARNING
5004 "Got error %d from the server on DESTROY_SESSION. "
5005 "Session has been destroyed regardless...\n", status);
5006
5007 dprintk("<-- nfs4_proc_destroy_session\n");
5008 return status;
5009}
5010
Trond Myklebustfccba802009-07-21 16:48:07 -04005011int nfs4_init_session(struct nfs_server *server)
5012{
5013 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005014 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005015 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005016 int ret;
5017
5018 if (!nfs4_has_session(clp))
5019 return 0;
5020
Trond Myklebust1055d762010-06-16 09:52:27 -04005021 session = clp->cl_session;
5022 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5023 return 0;
5024
Andy Adamson68bf05e2009-12-15 12:55:02 -05005025 rsize = server->rsize;
5026 if (rsize == 0)
5027 rsize = NFS_MAX_FILE_IO_SIZE;
5028 wsize = server->wsize;
5029 if (wsize == 0)
5030 wsize = NFS_MAX_FILE_IO_SIZE;
5031
Andy Adamson68bf05e2009-12-15 12:55:02 -05005032 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5033 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005034
Trond Myklebustfccba802009-07-21 16:48:07 -04005035 ret = nfs4_recover_expired_lease(server);
5036 if (!ret)
5037 ret = nfs4_check_client_ready(clp);
5038 return ret;
5039}
5040
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005041/*
5042 * Renew the cl_session lease.
5043 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005044struct nfs4_sequence_data {
5045 struct nfs_client *clp;
5046 struct nfs4_sequence_args args;
5047 struct nfs4_sequence_res res;
5048};
5049
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005050static void nfs41_sequence_release(void *data)
5051{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005052 struct nfs4_sequence_data *calldata = data;
5053 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005054
Alexandros Batsakis71358402010-02-05 03:45:05 -08005055 if (atomic_read(&clp->cl_count) > 1)
5056 nfs4_schedule_state_renewal(clp);
5057 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005058 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005059}
5060
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005061static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5062{
5063 switch(task->tk_status) {
5064 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005065 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5066 return -EAGAIN;
5067 default:
5068 nfs4_schedule_state_recovery(clp);
5069 }
5070 return 0;
5071}
5072
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005073static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005074{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005075 struct nfs4_sequence_data *calldata = data;
5076 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005077
Trond Myklebust14516c32010-07-31 14:29:06 -04005078 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5079 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005080
5081 if (task->tk_status < 0) {
5082 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005083 if (atomic_read(&clp->cl_count) == 1)
5084 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005085
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005086 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5087 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005088 return;
5089 }
5090 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005091 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005092out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005093 dprintk("<-- %s\n", __func__);
5094}
5095
5096static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5097{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005098 struct nfs4_sequence_data *calldata = data;
5099 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005100 struct nfs4_sequence_args *args;
5101 struct nfs4_sequence_res *res;
5102
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005103 args = task->tk_msg.rpc_argp;
5104 res = task->tk_msg.rpc_resp;
5105
Trond Myklebust035168ab2010-06-16 09:52:26 -04005106 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005107 return;
5108 rpc_call_start(task);
5109}
5110
5111static const struct rpc_call_ops nfs41_sequence_ops = {
5112 .rpc_call_done = nfs41_sequence_call_done,
5113 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005114 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005115};
5116
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005117static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005118{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005119 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005120 struct rpc_message msg = {
5121 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5122 .rpc_cred = cred,
5123 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005124 struct rpc_task_setup task_setup_data = {
5125 .rpc_client = clp->cl_rpcclient,
5126 .rpc_message = &msg,
5127 .callback_ops = &nfs41_sequence_ops,
5128 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5129 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005130
Alexandros Batsakis71358402010-02-05 03:45:05 -08005131 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005132 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005133 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005134 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005135 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005136 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005137 }
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005138 msg.rpc_argp = &calldata->args;
5139 msg.rpc_resp = &calldata->res;
5140 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005141 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005142
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005143 return rpc_run_task(&task_setup_data);
5144}
5145
5146static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5147{
5148 struct rpc_task *task;
5149 int ret = 0;
5150
5151 task = _nfs41_proc_sequence(clp, cred);
5152 if (IS_ERR(task))
5153 ret = PTR_ERR(task);
5154 else
5155 rpc_put_task(task);
5156 dprintk("<-- %s status=%d\n", __func__, ret);
5157 return ret;
5158}
5159
5160static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5161{
5162 struct rpc_task *task;
5163 int ret;
5164
5165 task = _nfs41_proc_sequence(clp, cred);
5166 if (IS_ERR(task)) {
5167 ret = PTR_ERR(task);
5168 goto out;
5169 }
5170 ret = rpc_wait_for_completion_task(task);
5171 if (!ret)
5172 ret = task->tk_status;
5173 rpc_put_task(task);
5174out:
5175 dprintk("<-- %s status=%d\n", __func__, ret);
5176 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005177}
5178
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005179struct nfs4_reclaim_complete_data {
5180 struct nfs_client *clp;
5181 struct nfs41_reclaim_complete_args arg;
5182 struct nfs41_reclaim_complete_res res;
5183};
5184
5185static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5186{
5187 struct nfs4_reclaim_complete_data *calldata = data;
5188
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005189 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168ab2010-06-16 09:52:26 -04005190 if (nfs41_setup_sequence(calldata->clp->cl_session,
5191 &calldata->arg.seq_args,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005192 &calldata->res.seq_res, 0, task))
5193 return;
5194
5195 rpc_call_start(task);
5196}
5197
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005198static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5199{
5200 switch(task->tk_status) {
5201 case 0:
5202 case -NFS4ERR_COMPLETE_ALREADY:
5203 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5204 break;
5205 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005206 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5207 return -EAGAIN;
5208 default:
5209 nfs4_schedule_state_recovery(clp);
5210 }
5211 return 0;
5212}
5213
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005214static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5215{
5216 struct nfs4_reclaim_complete_data *calldata = data;
5217 struct nfs_client *clp = calldata->clp;
5218 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5219
5220 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005221 if (!nfs41_sequence_done(task, res))
5222 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005223
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005224 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5225 rpc_restart_call_prepare(task);
5226 return;
5227 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005228 dprintk("<-- %s\n", __func__);
5229}
5230
5231static void nfs4_free_reclaim_complete_data(void *data)
5232{
5233 struct nfs4_reclaim_complete_data *calldata = data;
5234
5235 kfree(calldata);
5236}
5237
5238static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5239 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5240 .rpc_call_done = nfs4_reclaim_complete_done,
5241 .rpc_release = nfs4_free_reclaim_complete_data,
5242};
5243
5244/*
5245 * Issue a global reclaim complete.
5246 */
5247static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5248{
5249 struct nfs4_reclaim_complete_data *calldata;
5250 struct rpc_task *task;
5251 struct rpc_message msg = {
5252 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5253 };
5254 struct rpc_task_setup task_setup_data = {
5255 .rpc_client = clp->cl_rpcclient,
5256 .rpc_message = &msg,
5257 .callback_ops = &nfs4_reclaim_complete_call_ops,
5258 .flags = RPC_TASK_ASYNC,
5259 };
5260 int status = -ENOMEM;
5261
5262 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005263 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005264 if (calldata == NULL)
5265 goto out;
5266 calldata->clp = clp;
5267 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005268
5269 msg.rpc_argp = &calldata->arg;
5270 msg.rpc_resp = &calldata->res;
5271 task_setup_data.callback_data = calldata;
5272 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005273 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005274 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005275 goto out;
5276 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005277 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005278 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005279out:
5280 dprintk("<-- %s status=%d\n", __func__, status);
5281 return status;
5282}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005283
5284static void
5285nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5286{
5287 struct nfs4_layoutget *lgp = calldata;
5288 struct inode *ino = lgp->args.inode;
5289 struct nfs_server *server = NFS_SERVER(ino);
5290
5291 dprintk("--> %s\n", __func__);
5292 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5293 &lgp->res.seq_res, 0, task))
5294 return;
5295 rpc_call_start(task);
5296}
5297
5298static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5299{
5300 struct nfs4_layoutget *lgp = calldata;
5301 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5302
5303 dprintk("--> %s\n", __func__);
5304
5305 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5306 return;
5307
5308 switch (task->tk_status) {
5309 case 0:
5310 break;
5311 case -NFS4ERR_LAYOUTTRYLATER:
5312 case -NFS4ERR_RECALLCONFLICT:
5313 task->tk_status = -NFS4ERR_DELAY;
5314 /* Fall through */
5315 default:
5316 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5317 rpc_restart_call_prepare(task);
5318 return;
5319 }
5320 }
5321 lgp->status = task->tk_status;
5322 dprintk("<-- %s\n", __func__);
5323}
5324
5325static void nfs4_layoutget_release(void *calldata)
5326{
5327 struct nfs4_layoutget *lgp = calldata;
5328
5329 dprintk("--> %s\n", __func__);
5330 put_layout_hdr(lgp->args.inode);
5331 if (lgp->res.layout.buf != NULL)
5332 free_page((unsigned long) lgp->res.layout.buf);
5333 put_nfs_open_context(lgp->args.ctx);
5334 kfree(calldata);
5335 dprintk("<-- %s\n", __func__);
5336}
5337
5338static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5339 .rpc_call_prepare = nfs4_layoutget_prepare,
5340 .rpc_call_done = nfs4_layoutget_done,
5341 .rpc_release = nfs4_layoutget_release,
5342};
5343
5344int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5345{
5346 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5347 struct rpc_task *task;
5348 struct rpc_message msg = {
5349 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5350 .rpc_argp = &lgp->args,
5351 .rpc_resp = &lgp->res,
5352 };
5353 struct rpc_task_setup task_setup_data = {
5354 .rpc_client = server->client,
5355 .rpc_message = &msg,
5356 .callback_ops = &nfs4_layoutget_call_ops,
5357 .callback_data = lgp,
5358 .flags = RPC_TASK_ASYNC,
5359 };
5360 int status = 0;
5361
5362 dprintk("--> %s\n", __func__);
5363
5364 lgp->res.layout.buf = (void *)__get_free_page(GFP_NOFS);
5365 if (lgp->res.layout.buf == NULL) {
5366 nfs4_layoutget_release(lgp);
5367 return -ENOMEM;
5368 }
5369
5370 lgp->res.seq_res.sr_slot = NULL;
5371 task = rpc_run_task(&task_setup_data);
5372 if (IS_ERR(task))
5373 return PTR_ERR(task);
5374 status = nfs4_wait_for_completion_rpc_task(task);
5375 if (status != 0)
5376 goto out;
5377 status = lgp->status;
5378 if (status != 0)
5379 goto out;
5380 status = pnfs_layout_process(lgp);
5381out:
5382 rpc_put_task(task);
5383 dprintk("<-- %s status=%d\n", __func__, status);
5384 return status;
5385}
5386
5387static int
5388_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5389{
5390 struct nfs4_getdeviceinfo_args args = {
5391 .pdev = pdev,
5392 };
5393 struct nfs4_getdeviceinfo_res res = {
5394 .pdev = pdev,
5395 };
5396 struct rpc_message msg = {
5397 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5398 .rpc_argp = &args,
5399 .rpc_resp = &res,
5400 };
5401 int status;
5402
5403 dprintk("--> %s\n", __func__);
5404 status = nfs4_call_sync(server, &msg, &args, &res, 0);
5405 dprintk("<-- %s status=%d\n", __func__, status);
5406
5407 return status;
5408}
5409
5410int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5411{
5412 struct nfs4_exception exception = { };
5413 int err;
5414
5415 do {
5416 err = nfs4_handle_exception(server,
5417 _nfs4_proc_getdeviceinfo(server, pdev),
5418 &exception);
5419 } while (exception.retry);
5420 return err;
5421}
5422EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5423
Andy Adamson557134a2009-04-01 09:21:53 -04005424#endif /* CONFIG_NFS_V4_1 */
5425
Andy Adamson591d71c2009-04-01 09:22:47 -04005426struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005427 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005428 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 .recover_open = nfs4_open_reclaim,
5430 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04005431 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005432 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433};
5434
Andy Adamson591d71c2009-04-01 09:22:47 -04005435#if defined(CONFIG_NFS_V4_1)
5436struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5437 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5438 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5439 .recover_open = nfs4_open_reclaim,
5440 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05005441 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005442 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005443 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04005444};
5445#endif /* CONFIG_NFS_V4_1 */
5446
5447struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005448 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005449 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 .recover_open = nfs4_open_expired,
5451 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04005452 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005453 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454};
5455
Andy Adamson591d71c2009-04-01 09:22:47 -04005456#if defined(CONFIG_NFS_V4_1)
5457struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5458 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5459 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5460 .recover_open = nfs4_open_expired,
5461 .recover_lock = nfs4_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05005462 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005463 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04005464};
5465#endif /* CONFIG_NFS_V4_1 */
5466
Benny Halevy29fba382009-04-01 09:22:44 -04005467struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5468 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04005469 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005470 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04005471};
5472
5473#if defined(CONFIG_NFS_V4_1)
5474struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5475 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04005476 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005477 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04005478};
5479#endif
5480
Trond Myklebust97dc1352010-06-16 09:52:26 -04005481static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
5482 .minor_version = 0,
5483 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04005484 .validate_stateid = nfs4_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005485 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
5486 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
5487 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005488};
5489
5490#if defined(CONFIG_NFS_V4_1)
5491static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
5492 .minor_version = 1,
5493 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04005494 .validate_stateid = nfs41_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005495 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
5496 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
5497 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005498};
5499#endif
5500
Trond Myklebust97dc1352010-06-16 09:52:26 -04005501const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
5502 [0] = &nfs_v4_0_minor_ops,
5503#if defined(CONFIG_NFS_V4_1)
5504 [1] = &nfs_v4_1_minor_ops,
5505#endif
5506};
5507
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005508static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005509 .permission = nfs_permission,
5510 .getattr = nfs_getattr,
5511 .setattr = nfs_setattr,
5512 .getxattr = nfs4_getxattr,
5513 .setxattr = nfs4_setxattr,
5514 .listxattr = nfs4_listxattr,
5515};
5516
David Howells509de812006-08-22 20:06:11 -04005517const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 .version = 4, /* protocol version */
5519 .dentry_ops = &nfs4_dentry_operations,
5520 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005521 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522 .getroot = nfs4_proc_get_root,
5523 .getattr = nfs4_proc_getattr,
5524 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04005525 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 .lookup = nfs4_proc_lookup,
5527 .access = nfs4_proc_access,
5528 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529 .create = nfs4_proc_create,
5530 .remove = nfs4_proc_remove,
5531 .unlink_setup = nfs4_proc_unlink_setup,
5532 .unlink_done = nfs4_proc_unlink_done,
5533 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04005534 .rename_setup = nfs4_proc_rename_setup,
5535 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536 .link = nfs4_proc_link,
5537 .symlink = nfs4_proc_symlink,
5538 .mkdir = nfs4_proc_mkdir,
5539 .rmdir = nfs4_proc_remove,
5540 .readdir = nfs4_proc_readdir,
5541 .mknod = nfs4_proc_mknod,
5542 .statfs = nfs4_proc_statfs,
5543 .fsinfo = nfs4_proc_fsinfo,
5544 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04005545 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546 .decode_dirent = nfs4_decode_dirent,
5547 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05005548 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005550 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005552 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005554 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04005555 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04005556 .open_context = nfs4_atomic_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557};
5558
5559/*
5560 * Local variables:
5561 * c-basic-offset: 8
5562 * End:
5563 */