blob: aa771c1af1154d4211e890c40d21cc4ed51a9ff6 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59#define NFSDBG_FACILITY NFSDBG_PROC
60
Trond Myklebust2066fe82006-09-15 08:30:46 -040061#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#define NFS4_POLL_RETRY_MAX (15*HZ)
63
Trond Myklebusta78cb572009-08-09 15:06:19 -040064#define NFS4_MAX_LOOP_ON_RECOVER (10)
65
Trond Myklebustcdd4e682006-01-03 09:55:12 +010066struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010067static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080068static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050070static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Trond Myklebust99367812007-07-17 21:52:41 -040071static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
72static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040073static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
74 struct nfs_fattr *fattr, struct iattr *sattr,
75 struct nfs4_state *state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050078static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Trond Myklebust52567b02009-10-23 14:46:42 -040080 if (err >= -1000)
81 return err;
82 switch (err) {
83 case -NFS4ERR_RESOURCE:
84 return -EREMOTEIO;
85 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -070087 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -040088 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 }
Trond Myklebust52567b02009-10-23 14:46:42 -040090 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
93/*
94 * This is our standard bitmap for GETATTR requests.
95 */
96const u32 nfs4_fattr_bitmap[2] = {
97 FATTR4_WORD0_TYPE
98 | FATTR4_WORD0_CHANGE
99 | FATTR4_WORD0_SIZE
100 | FATTR4_WORD0_FSID
101 | FATTR4_WORD0_FILEID,
102 FATTR4_WORD1_MODE
103 | FATTR4_WORD1_NUMLINKS
104 | FATTR4_WORD1_OWNER
105 | FATTR4_WORD1_OWNER_GROUP
106 | FATTR4_WORD1_RAWDEV
107 | FATTR4_WORD1_SPACE_USED
108 | FATTR4_WORD1_TIME_ACCESS
109 | FATTR4_WORD1_TIME_METADATA
110 | FATTR4_WORD1_TIME_MODIFY
111};
112
113const u32 nfs4_statfs_bitmap[2] = {
114 FATTR4_WORD0_FILES_AVAIL
115 | FATTR4_WORD0_FILES_FREE
116 | FATTR4_WORD0_FILES_TOTAL,
117 FATTR4_WORD1_SPACE_AVAIL
118 | FATTR4_WORD1_SPACE_FREE
119 | FATTR4_WORD1_SPACE_TOTAL
120};
121
Trond Myklebust4ce79712005-06-22 17:16:21 +0000122const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 FATTR4_WORD0_MAXLINK
124 | FATTR4_WORD0_MAXNAME,
125 0
126};
127
128const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
129 | FATTR4_WORD0_MAXREAD
130 | FATTR4_WORD0_MAXWRITE
131 | FATTR4_WORD0_LEASE_TIME,
132 0
133};
134
Manoj Naik830b8e32006-06-09 09:34:25 -0400135const u32 nfs4_fs_locations_bitmap[2] = {
136 FATTR4_WORD0_TYPE
137 | FATTR4_WORD0_CHANGE
138 | FATTR4_WORD0_SIZE
139 | FATTR4_WORD0_FSID
140 | FATTR4_WORD0_FILEID
141 | FATTR4_WORD0_FS_LOCATIONS,
142 FATTR4_WORD1_MODE
143 | FATTR4_WORD1_NUMLINKS
144 | FATTR4_WORD1_OWNER
145 | FATTR4_WORD1_OWNER_GROUP
146 | FATTR4_WORD1_RAWDEV
147 | FATTR4_WORD1_SPACE_USED
148 | FATTR4_WORD1_TIME_ACCESS
149 | FATTR4_WORD1_TIME_METADATA
150 | FATTR4_WORD1_TIME_MODIFY
151 | FATTR4_WORD1_MOUNTED_ON_FILEID
152};
153
Al Virobc4785c2006-10-19 23:28:51 -0700154static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 struct nfs4_readdir_arg *readdir)
156{
Al Viro0dbb4c62006-10-19 23:28:49 -0700157 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 BUG_ON(readdir->count < 80);
160 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000161 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
163 return;
164 }
165
166 readdir->cookie = 0;
167 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
168 if (cookie == 2)
169 return;
170
171 /*
172 * NFSv4 servers do not return entries for '.' and '..'
173 * Therefore, we fake these entries here. We let '.'
174 * have cookie 0 and '..' have cookie 1. Note that
175 * when talking to the server, we always send cookie 0
176 * instead of 1 or 2.
177 */
Al Viro0dbb4c62006-10-19 23:28:49 -0700178 start = p = kmap_atomic(*readdir->pages, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 if (cookie == 0) {
181 *p++ = xdr_one; /* next */
182 *p++ = xdr_zero; /* cookie, first word */
183 *p++ = xdr_one; /* cookie, second word */
184 *p++ = xdr_one; /* entry len */
185 memcpy(p, ".\0\0\0", 4); /* entry */
186 p++;
187 *p++ = xdr_one; /* bitmap length */
188 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
189 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400190 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 }
192
193 *p++ = xdr_one; /* next */
194 *p++ = xdr_zero; /* cookie, first word */
195 *p++ = xdr_two; /* cookie, second word */
196 *p++ = xdr_two; /* entry len */
197 memcpy(p, "..\0\0", 4); /* entry */
198 p++;
199 *p++ = xdr_one; /* bitmap length */
200 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
201 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400202 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 readdir->pgbase = (char *)p - (char *)start;
205 readdir->count -= readdir->pgbase;
206 kunmap_atomic(start, KM_USER0);
207}
208
Trond Myklebust65de8722008-12-23 15:21:44 -0500209static int nfs4_wait_clnt_recover(struct nfs_client *clp)
210{
211 int res;
212
213 might_sleep();
214
Trond Myklebuste005e802008-12-23 15:21:48 -0500215 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400216 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500217 return res;
218}
219
220static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
221{
222 int res = 0;
223
224 might_sleep();
225
226 if (*timeout <= 0)
227 *timeout = NFS4_POLL_RETRY_MIN;
228 if (*timeout > NFS4_POLL_RETRY_MAX)
229 *timeout = NFS4_POLL_RETRY_MAX;
230 schedule_timeout_killable(*timeout);
231 if (fatal_signal_pending(current))
232 res = -ERESTARTSYS;
233 *timeout <<= 1;
234 return res;
235}
236
237/* This is the error handling routine for processes that are allowed
238 * to sleep.
239 */
240static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
241{
242 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500243 struct nfs4_state *state = exception->state;
Trond Myklebust65de8722008-12-23 15:21:44 -0500244 int ret = errorcode;
245
246 exception->retry = 0;
247 switch(errorcode) {
248 case 0:
249 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500250 case -NFS4ERR_ADMIN_REVOKED:
251 case -NFS4ERR_BAD_STATEID:
252 case -NFS4ERR_OPENMODE:
253 if (state == NULL)
254 break;
255 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500256 goto do_state_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500257 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500258 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust65de8722008-12-23 15:21:44 -0500259 case -NFS4ERR_EXPIRED:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500260 goto do_state_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500261#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400262 case -NFS4ERR_BADSESSION:
263 case -NFS4ERR_BADSLOT:
264 case -NFS4ERR_BAD_HIGH_SLOT:
265 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
266 case -NFS4ERR_DEADSESSION:
267 case -NFS4ERR_SEQ_FALSE_RETRY:
268 case -NFS4ERR_SEQ_MISORDERED:
269 dprintk("%s ERROR: %d Reset session\n", __func__,
270 errorcode);
Andy Adamson0b9e2d42009-12-04 16:02:14 -0500271 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -0400272 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500273 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500274#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500275 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500276 if (exception->timeout > HZ) {
277 /* We have retried a decent amount, time to
278 * fail
279 */
280 ret = -EBUSY;
281 break;
282 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500283 case -NFS4ERR_GRACE:
284 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500285 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500286 ret = nfs4_delay(server->client, &exception->timeout);
287 if (ret != 0)
288 break;
289 case -NFS4ERR_OLD_STATEID:
290 exception->retry = 1;
291 }
292 /* We failed to handle the error */
293 return nfs4_map_errors(ret);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500294do_state_recovery:
295 nfs4_schedule_state_recovery(clp);
296 ret = nfs4_wait_clnt_recover(clp);
297 if (ret == 0)
298 exception->retry = 1;
299 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500300}
301
302
Trond Myklebust452e9352010-07-31 14:29:06 -0400303static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 spin_lock(&clp->cl_lock);
306 if (time_before(clp->cl_last_renewal,timestamp))
307 clp->cl_last_renewal = timestamp;
308 spin_unlock(&clp->cl_lock);
309}
310
Trond Myklebust452e9352010-07-31 14:29:06 -0400311static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
312{
313 do_renew_lease(server->nfs_client, timestamp);
314}
315
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400316#if defined(CONFIG_NFS_V4_1)
317
Benny Halevy510b8172009-04-01 09:22:14 -0400318/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400319 * nfs4_free_slot - free a slot and efficiently update slot table.
320 *
321 * freeing a slot is trivially done by clearing its respective bit
322 * in the bitmap.
323 * If the freed slotid equals highest_used_slotid we want to update it
324 * so that the server would be able to size down the slot table if needed,
325 * otherwise we know that the highest_used_slotid is still in use.
326 * When updating highest_used_slotid there may be "holes" in the bitmap
327 * so we need to scan down from highest_used_slotid to 0 looking for the now
328 * highest slotid in use.
329 * If none found, highest_used_slotid is set to -1.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500330 *
331 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400332 */
333static void
Benny Halevydfb4f3092010-09-24 09:17:01 -0400334nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *free_slot)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400335{
Benny Halevydfb4f3092010-09-24 09:17:01 -0400336 int free_slotid = free_slot - tbl->slots;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400337 int slotid = free_slotid;
338
Benny Halevydfb4f3092010-09-24 09:17:01 -0400339 BUG_ON(slotid < 0 || slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400340 /* clear used bit in bitmap */
341 __clear_bit(slotid, tbl->used_slots);
342
343 /* update highest_used_slotid when it is freed */
344 if (slotid == tbl->highest_used_slotid) {
345 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500346 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400347 tbl->highest_used_slotid = slotid;
348 else
349 tbl->highest_used_slotid = -1;
350 }
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400351 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
352 free_slotid, tbl->highest_used_slotid);
353}
354
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800355/*
356 * Signal state manager thread if session is drained
357 */
358static void nfs41_check_drain_session_complete(struct nfs4_session *ses)
359{
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800360 struct rpc_task *task;
361
Trond Myklebusta2118c32010-06-16 09:52:26 -0400362 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800363 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
364 if (task)
365 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800366 return;
367 }
368
369 if (ses->fc_slot_table.highest_used_slotid != -1)
370 return;
371
372 dprintk("%s COMPLETE: Session Drained\n", __func__);
373 complete(&ses->complete);
374}
375
Trond Myklebustd185a332010-06-16 09:52:25 -0400376static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400377{
378 struct nfs4_slot_table *tbl;
379
Trond Myklebustd185a332010-06-16 09:52:25 -0400380 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400381 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400382 /* just wake up the next guy waiting since
383 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500384 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500385 return;
Andy Adamson13615872009-04-01 09:22:17 -0400386 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500387
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500388 spin_lock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400389 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebustd185a332010-06-16 09:52:25 -0400390 nfs41_check_drain_session_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500391 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400392 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400393}
394
Trond Myklebust14516c32010-07-31 14:29:06 -0400395static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400396{
397 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400398 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400399
400 /*
401 * sr_status remains 1 if an RPC level error occurred. The server
402 * may or may not have processed the sequence operation..
403 * Proceed as if the server received and processed the sequence
404 * operation.
405 */
406 if (res->sr_status == 1)
407 res->sr_status = NFS_OK;
408
409 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400410 if (!res->sr_slot)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400411 goto out;
412
Andy Adamson691daf32009-12-04 15:55:39 -0500413 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400414 switch (res->sr_status) {
415 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400416 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400417 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400418 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400419 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400420 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500421 /* Check sequence flags */
Alexandros Batsakis71358402010-02-05 03:45:05 -0800422 if (atomic_read(&clp->cl_count) > 1)
423 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust14516c32010-07-31 14:29:06 -0400424 break;
425 case -NFS4ERR_DELAY:
426 /* The server detected a resend of the RPC call and
427 * returned NFS4ERR_DELAY as per Section 2.10.6.2
428 * of RFC5661.
429 */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400430 dprintk("%s: slot=%ld seq=%d: Operation in progress\n",
431 __func__,
432 res->sr_slot - res->sr_session->fc_slot_table.slots,
433 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400434 goto out_retry;
435 default:
436 /* Just update the slot sequence no. */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400437 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400438 }
439out:
440 /* The session may be reset by one of the error handlers. */
441 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400442 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400443 return 1;
444out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400445 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400446 goto out;
447 rpc_delay(task, NFS4_POLL_RETRY_MAX);
448 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400449}
450
Trond Myklebust14516c32010-07-31 14:29:06 -0400451static int nfs4_sequence_done(struct rpc_task *task,
452 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400453{
Trond Myklebust14516c32010-07-31 14:29:06 -0400454 if (res->sr_session == NULL)
455 return 1;
456 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400457}
458
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400459/*
Benny Halevy510b8172009-04-01 09:22:14 -0400460 * nfs4_find_slot - efficiently look for a free slot
461 *
462 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
463 * If found, we mark the slot as used, update the highest_used_slotid,
464 * and respectively set up the sequence operation args.
465 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400466 *
467 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400468 */
469static u8
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800470nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400471{
472 int slotid;
473 u8 ret_id = NFS4_MAX_SLOT_TABLE;
474 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
475
Benny Halevy510b8172009-04-01 09:22:14 -0400476 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
477 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
478 tbl->max_slots);
479 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
480 if (slotid >= tbl->max_slots)
481 goto out;
482 __set_bit(slotid, tbl->used_slots);
483 if (slotid > tbl->highest_used_slotid)
484 tbl->highest_used_slotid = slotid;
485 ret_id = slotid;
486out:
487 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
488 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400489 return ret_id;
490}
491
Andy Adamsonce5039c2009-04-01 09:22:13 -0400492static int nfs41_setup_sequence(struct nfs4_session *session,
493 struct nfs4_sequence_args *args,
494 struct nfs4_sequence_res *res,
495 int cache_reply,
496 struct rpc_task *task)
497{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400498 struct nfs4_slot *slot;
499 struct nfs4_slot_table *tbl;
500 u8 slotid;
501
502 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400503 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400504 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400505 return 0;
506
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400507 tbl = &session->fc_slot_table;
508
509 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400510 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800511 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamsonea028ac2009-12-04 15:55:38 -0500512 /*
513 * The state manager will wait until the slot table is empty.
514 * Schedule the reset thread
515 */
Andy Adamson05f0d232009-12-04 15:55:37 -0500516 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400517 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500518 dprintk("%s Schedule Session Reset\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500519 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400520 }
521
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800522 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
523 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
524 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
525 spin_unlock(&tbl->slot_tbl_lock);
526 dprintk("%s enforce FIFO order\n", __func__);
527 return -EAGAIN;
528 }
529
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800530 slotid = nfs4_find_slot(tbl);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400531 if (slotid == NFS4_MAX_SLOT_TABLE) {
532 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
533 spin_unlock(&tbl->slot_tbl_lock);
534 dprintk("<-- %s: no free slots\n", __func__);
535 return -EAGAIN;
536 }
537 spin_unlock(&tbl->slot_tbl_lock);
538
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800539 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400540 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400541 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400542 args->sa_slotid = slotid;
543 args->sa_cache_this = cache_reply;
544
545 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
546
Benny Halevy99fe60d2009-04-01 09:22:29 -0400547 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400548 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400549 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400550 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400551 /*
552 * sr_status is only set in decode_sequence, and so will remain
553 * set to 1 if an rpc level failure occurs.
554 */
555 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400556 return 0;
557}
558
Trond Myklebust035168a2010-06-16 09:52:26 -0400559int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400560 struct nfs4_sequence_args *args,
561 struct nfs4_sequence_res *res,
562 int cache_reply,
563 struct rpc_task *task)
564{
Trond Myklebust035168a2010-06-16 09:52:26 -0400565 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400566 int ret = 0;
567
Trond Myklebust035168a2010-06-16 09:52:26 -0400568 if (session == NULL) {
569 args->sa_session = NULL;
570 res->sr_session = NULL;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400571 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400572 }
573
Benny Halevydfb4f3092010-09-24 09:17:01 -0400574 dprintk("--> %s clp %p session %p sr_slot %ld\n",
575 __func__, session->clp, session, res->sr_slot ?
576 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400577
578 ret = nfs41_setup_sequence(session, args, res, cache_reply,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400579 task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400580out:
581 dprintk("<-- %s status=%d\n", __func__, ret);
582 return ret;
583}
584
585struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400586 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400587 struct nfs4_sequence_args *seq_args;
588 struct nfs4_sequence_res *seq_res;
589 int cache_reply;
590};
591
592static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
593{
594 struct nfs41_call_sync_data *data = calldata;
595
Trond Myklebust035168a2010-06-16 09:52:26 -0400596 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
597
598 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400599 data->seq_res, data->cache_reply, task))
600 return;
601 rpc_call_start(task);
602}
603
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800604static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
605{
606 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
607 nfs41_call_sync_prepare(task, calldata);
608}
609
Andy Adamson69ab40c2009-04-01 09:22:19 -0400610static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
611{
612 struct nfs41_call_sync_data *data = calldata;
613
Trond Myklebust14516c32010-07-31 14:29:06 -0400614 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400615}
616
Andy Adamsonce5039c2009-04-01 09:22:13 -0400617struct rpc_call_ops nfs41_call_sync_ops = {
618 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400619 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400620};
621
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800622struct rpc_call_ops nfs41_call_priv_sync_ops = {
623 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
624 .rpc_call_done = nfs41_call_sync_done,
625};
626
Trond Myklebust035168a2010-06-16 09:52:26 -0400627static int nfs4_call_sync_sequence(struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400628 struct rpc_message *msg,
629 struct nfs4_sequence_args *args,
630 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800631 int cache_reply,
632 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400633{
634 int ret;
635 struct rpc_task *task;
636 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400637 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400638 .seq_args = args,
639 .seq_res = res,
640 .cache_reply = cache_reply,
641 };
642 struct rpc_task_setup task_setup = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400643 .rpc_client = server->client,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400644 .rpc_message = msg,
645 .callback_ops = &nfs41_call_sync_ops,
646 .callback_data = &data
647 };
648
Benny Halevydfb4f3092010-09-24 09:17:01 -0400649 res->sr_slot = NULL;
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800650 if (privileged)
651 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400652 task = rpc_run_task(&task_setup);
653 if (IS_ERR(task))
654 ret = PTR_ERR(task);
655 else {
656 ret = task->tk_status;
657 rpc_put_task(task);
658 }
659 return ret;
660}
661
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400662int _nfs4_call_sync_session(struct nfs_server *server,
663 struct rpc_message *msg,
664 struct nfs4_sequence_args *args,
665 struct nfs4_sequence_res *res,
666 int cache_reply)
667{
Trond Myklebust035168a2010-06-16 09:52:26 -0400668 return nfs4_call_sync_sequence(server, msg, args, res, cache_reply, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400669}
670
Trond Myklebustdf896452010-06-16 09:52:26 -0400671#else
Trond Myklebust14516c32010-07-31 14:29:06 -0400672static int nfs4_sequence_done(struct rpc_task *task,
673 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400674{
Trond Myklebust14516c32010-07-31 14:29:06 -0400675 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400676}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400677#endif /* CONFIG_NFS_V4_1 */
678
679int _nfs4_call_sync(struct nfs_server *server,
680 struct rpc_message *msg,
681 struct nfs4_sequence_args *args,
682 struct nfs4_sequence_res *res,
683 int cache_reply)
684{
Benny Halevyf3752972009-04-01 09:22:04 -0400685 args->sa_session = res->sr_session = NULL;
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400686 return rpc_call_sync(server->client, msg, 0);
687}
688
689#define nfs4_call_sync(server, msg, args, res, cache_reply) \
Trond Myklebust97dc1352010-06-16 09:52:26 -0400690 (server)->nfs_client->cl_mvops->call_sync((server), (msg), &(args)->seq_args, \
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400691 &(res)->seq_res, (cache_reply))
692
Trond Myklebust38478b22006-05-25 01:40:57 -0400693static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
Trond Myklebust38478b22006-05-25 01:40:57 -0400695 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Trond Myklebust38478b22006-05-25 01:40:57 -0400697 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400698 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
699 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400700 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400701 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400702 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100705struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400706 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100707 struct nfs_openargs o_arg;
708 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100709 struct nfs_open_confirmargs c_arg;
710 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100711 struct nfs_fattr f_attr;
712 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400713 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100714 struct dentry *dir;
715 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400716 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100717 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100718 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400719 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100720 int rpc_status;
721 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100722};
723
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400724
725static void nfs4_init_opendata_res(struct nfs4_opendata *p)
726{
727 p->o_res.f_attr = &p->f_attr;
728 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400729 p->o_res.seqid = p->o_arg.seqid;
730 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400731 p->o_res.server = p->o_arg.server;
732 nfs_fattr_init(&p->f_attr);
733 nfs_fattr_init(&p->dir_attr);
734}
735
Trond Myklebustad389da2007-06-05 12:30:00 -0400736static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500737 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400738 const struct iattr *attrs,
739 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100740{
Trond Myklebustad389da2007-06-05 12:30:00 -0400741 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100742 struct inode *dir = parent->d_inode;
743 struct nfs_server *server = NFS_SERVER(dir);
744 struct nfs4_opendata *p;
745
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400746 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100747 if (p == NULL)
748 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400749 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100750 if (p->o_arg.seqid == NULL)
751 goto err_free;
Al Virof6948692010-01-30 13:51:04 -0500752 path_get(path);
753 p->path = *path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100754 p->dir = parent;
755 p->owner = sp;
756 atomic_inc(&sp->so_count);
757 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500758 p->o_arg.open_flags = flags;
759 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400760 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400761 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400762 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100763 p->o_arg.server = server;
764 p->o_arg.bitmask = server->attr_bitmask;
765 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400766 if (flags & O_CREAT) {
767 u32 *s;
768
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100769 p->o_arg.u.attrs = &p->attrs;
770 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400771 s = (u32 *) p->o_arg.u.verifier.data;
772 s[0] = jiffies;
773 s[1] = current->pid;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100774 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100775 p->c_arg.fh = &p->o_res.fh;
776 p->c_arg.stateid = &p->o_res.stateid;
777 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400778 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400779 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100780 return p;
781err_free:
782 kfree(p);
783err:
784 dput(parent);
785 return NULL;
786}
787
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400788static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100789{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400790 struct nfs4_opendata *p = container_of(kref,
791 struct nfs4_opendata, kref);
792
793 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400794 if (p->state != NULL)
795 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400796 nfs4_put_state_owner(p->owner);
797 dput(p->dir);
Jan Blunck31f31db12008-05-02 13:42:45 -0700798 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400799 kfree(p);
800}
801
802static void nfs4_opendata_put(struct nfs4_opendata *p)
803{
804 if (p != NULL)
805 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100806}
807
Trond Myklebust06f814a2006-01-03 09:55:07 +0100808static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
809{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100810 int ret;
811
Trond Myklebust06f814a2006-01-03 09:55:07 +0100812 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100813 return ret;
814}
815
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500816static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400817{
818 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500819
820 if (open_mode & O_EXCL)
821 goto out;
822 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400823 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500824 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
825 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400826 break;
827 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500828 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
829 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400830 break;
831 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500832 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
833 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400834 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500835out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400836 return ret;
837}
838
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500839static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400840{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500841 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400842 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500843 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400844 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500845 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400846 return 1;
847}
848
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500849static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100850{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500851 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100852 case FMODE_WRITE:
853 state->n_wronly++;
854 break;
855 case FMODE_READ:
856 state->n_rdonly++;
857 break;
858 case FMODE_READ|FMODE_WRITE:
859 state->n_rdwr++;
860 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500861 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100862}
863
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500864static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400865{
866 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
867 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
868 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500869 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400870 case FMODE_READ:
871 set_bit(NFS_O_RDONLY_STATE, &state->flags);
872 break;
873 case FMODE_WRITE:
874 set_bit(NFS_O_WRONLY_STATE, &state->flags);
875 break;
876 case FMODE_READ|FMODE_WRITE:
877 set_bit(NFS_O_RDWR_STATE, &state->flags);
878 }
879}
880
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500881static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400882{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400883 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500884 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400885 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400886}
887
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500888static 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 -0700889{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400890 /*
891 * Protect the call to nfs4_state_set_mode_locked and
892 * serialise the stateid update
893 */
894 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400895 if (deleg_stateid != NULL) {
896 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
897 set_bit(NFS_DELEGATED_STATE, &state->flags);
898 }
899 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500900 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400901 write_sequnlock(&state->seqlock);
902 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500903 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700904 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905}
906
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500907static 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 -0500908{
909 struct nfs_inode *nfsi = NFS_I(state->inode);
910 struct nfs_delegation *deleg_cur;
911 int ret = 0;
912
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500913 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500914
915 rcu_read_lock();
916 deleg_cur = rcu_dereference(nfsi->delegation);
917 if (deleg_cur == NULL)
918 goto no_delegation;
919
920 spin_lock(&deleg_cur->lock);
921 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500922 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500923 goto no_delegation_unlock;
924
925 if (delegation == NULL)
926 delegation = &deleg_cur->stateid;
927 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
928 goto no_delegation_unlock;
929
Trond Myklebustb7391f42008-12-23 15:21:52 -0500930 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500931 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500932 ret = 1;
933no_delegation_unlock:
934 spin_unlock(&deleg_cur->lock);
935no_delegation:
936 rcu_read_unlock();
937
938 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500939 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500940 ret = 1;
941 }
942
943 return ret;
944}
945
946
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500947static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400948{
949 struct nfs_delegation *delegation;
950
951 rcu_read_lock();
952 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500953 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400954 rcu_read_unlock();
955 return;
956 }
957 rcu_read_unlock();
958 nfs_inode_return_delegation(inode);
959}
960
Trond Myklebust6ee41262007-07-08 14:11:36 -0400961static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400962{
963 struct nfs4_state *state = opendata->state;
964 struct nfs_inode *nfsi = NFS_I(state->inode);
965 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500966 int open_mode = opendata->o_arg.open_flags & O_EXCL;
967 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400968 nfs4_stateid stateid;
969 int ret = -EAGAIN;
970
Trond Myklebustaac00a82007-07-05 19:02:21 -0400971 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500972 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400973 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500974 if (can_open_cached(state, fmode, open_mode)) {
975 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400976 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400977 goto out_return_state;
978 }
979 spin_unlock(&state->owner->so_lock);
980 }
Trond Myklebust34310432008-12-23 15:21:38 -0500981 rcu_read_lock();
982 delegation = rcu_dereference(nfsi->delegation);
983 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500984 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -0500985 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -0400986 break;
Trond Myklebust34310432008-12-23 15:21:38 -0500987 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400988 /* Save the delegation */
989 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
990 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -0400991 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400992 if (ret != 0)
993 goto out;
994 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -0500995
996 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500997 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -0500998 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400999 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001000out:
1001 return ERR_PTR(ret);
1002out_return_state:
1003 atomic_inc(&state->count);
1004 return state;
1005}
1006
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001007static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1008{
1009 struct inode *inode;
1010 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001011 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001012 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001013
Trond Myklebustaac00a82007-07-05 19:02:21 -04001014 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001015 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001016 goto out;
1017 }
1018
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001019 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001020 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001021 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001022 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001023 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001024 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001025 goto err;
1026 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001027 state = nfs4_get_open_state(inode, data->owner);
1028 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001029 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001030 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001031 int delegation_flags = 0;
1032
Trond Myklebust003707c2007-07-05 18:07:55 -04001033 rcu_read_lock();
1034 delegation = rcu_dereference(NFS_I(inode)->delegation);
1035 if (delegation)
1036 delegation_flags = delegation->flags;
1037 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -05001038 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001039 nfs_inode_set_delegation(state->inode,
1040 data->owner->so_cred,
1041 &data->o_res);
1042 else
1043 nfs_inode_reclaim_delegation(state->inode,
1044 data->owner->so_cred,
1045 &data->o_res);
1046 }
Trond Myklebust34310432008-12-23 15:21:38 -05001047
1048 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001049 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001050 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001051out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001052 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001053err_put_inode:
1054 iput(inode);
1055err:
1056 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001057}
1058
Trond Myklebust864472e2006-01-03 09:55:15 +01001059static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1060{
1061 struct nfs_inode *nfsi = NFS_I(state->inode);
1062 struct nfs_open_context *ctx;
1063
1064 spin_lock(&state->inode->i_lock);
1065 list_for_each_entry(ctx, &nfsi->open_files, list) {
1066 if (ctx->state != state)
1067 continue;
1068 get_nfs_open_context(ctx);
1069 spin_unlock(&state->inode->i_lock);
1070 return ctx;
1071 }
1072 spin_unlock(&state->inode->i_lock);
1073 return ERR_PTR(-ENOENT);
1074}
1075
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001076static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1077{
1078 struct nfs4_opendata *opendata;
1079
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001080 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001081 if (opendata == NULL)
1082 return ERR_PTR(-ENOMEM);
1083 opendata->state = state;
1084 atomic_inc(&state->count);
1085 return opendata;
1086}
1087
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001088static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001089{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001090 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001091 int ret;
1092
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001093 opendata->o_arg.open_flags = 0;
1094 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001095 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1096 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1097 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001098 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001099 if (ret != 0)
1100 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001101 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001102 if (IS_ERR(newstate))
1103 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001104 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001105 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001106 return 0;
1107}
1108
1109static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1110{
Trond Myklebust864472e2006-01-03 09:55:15 +01001111 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001112 int ret;
1113
1114 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001115 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001116 smp_rmb();
1117 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001118 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001119 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001120 if (ret != 0)
1121 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001122 if (newstate != state)
1123 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001124 }
1125 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001126 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001127 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001128 if (ret != 0)
1129 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001130 if (newstate != state)
1131 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001132 }
1133 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001134 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001135 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001136 if (ret != 0)
1137 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001138 if (newstate != state)
1139 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001140 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001141 /*
1142 * We may have performed cached opens for all three recoveries.
1143 * Check if we need to update the current stateid.
1144 */
1145 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1146 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001147 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001148 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1149 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001150 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001151 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001152 return 0;
1153}
1154
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155/*
1156 * OPEN_RECLAIM:
1157 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001159static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001161 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001162 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001163 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 int status;
1165
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001166 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1167 if (IS_ERR(opendata))
1168 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001169 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1170 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001171 rcu_read_lock();
1172 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001173 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001174 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001175 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001176 opendata->o_arg.u.delegation_type = delegation_type;
1177 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001178 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 return status;
1180}
1181
Trond Myklebust539cd032007-06-05 11:46:42 -04001182static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
1184 struct nfs_server *server = NFS_SERVER(state->inode);
1185 struct nfs4_exception exception = { };
1186 int err;
1187 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001188 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001189 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001190 break;
1191 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 } while (exception.retry);
1193 return err;
1194}
1195
Trond Myklebust864472e2006-01-03 09:55:15 +01001196static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1197{
1198 struct nfs_open_context *ctx;
1199 int ret;
1200
1201 ctx = nfs4_state_find_open_context(state);
1202 if (IS_ERR(ctx))
1203 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001204 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001205 put_nfs_open_context(ctx);
1206 return ret;
1207}
1208
Trond Myklebust13437e12007-07-06 15:10:43 -04001209static 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 -07001210{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001211 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001212 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001214 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1215 if (IS_ERR(opendata))
1216 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001217 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001218 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001219 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001220 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001221 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001222 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
1224
Trond Myklebust13437e12007-07-06 15:10:43 -04001225int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
1227 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001228 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 int err;
1230 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001231 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 switch (err) {
1233 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001234 case -ENOENT:
1235 case -ESTALE:
1236 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001237 case -NFS4ERR_BADSESSION:
1238 case -NFS4ERR_BADSLOT:
1239 case -NFS4ERR_BAD_HIGH_SLOT:
1240 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1241 case -NFS4ERR_DEADSESSION:
1242 nfs4_schedule_state_recovery(
1243 server->nfs_client);
1244 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 case -NFS4ERR_STALE_CLIENTID:
1246 case -NFS4ERR_STALE_STATEID:
1247 case -NFS4ERR_EXPIRED:
1248 /* Don't recall a delegation if it was lost */
David Howells7539bba2006-08-22 20:06:09 -04001249 nfs4_schedule_state_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001250 goto out;
1251 case -ERESTARTSYS:
1252 /*
1253 * The show must go on: exit, but mark the
1254 * stateid as needing recovery.
1255 */
1256 case -NFS4ERR_ADMIN_REVOKED:
1257 case -NFS4ERR_BAD_STATEID:
1258 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001259 case -EKEYEXPIRED:
1260 /*
1261 * User RPCSEC_GSS context has expired.
1262 * We cannot recover this stateid now, so
1263 * skip it and allow recovery thread to
1264 * proceed.
1265 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001266 case -ENOMEM:
1267 err = 0;
1268 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 }
1270 err = nfs4_handle_exception(server, err, &exception);
1271 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001272out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 return err;
1274}
1275
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001276static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1277{
1278 struct nfs4_opendata *data = calldata;
1279
1280 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001281 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001282 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1283 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001284 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001285 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001286 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001287 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001288}
1289
1290static void nfs4_open_confirm_release(void *calldata)
1291{
1292 struct nfs4_opendata *data = calldata;
1293 struct nfs4_state *state = NULL;
1294
1295 /* If this request hasn't been cancelled, do nothing */
1296 if (data->cancelled == 0)
1297 goto out_free;
1298 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001299 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001300 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001301 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001302 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001303 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001304out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001305 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001306}
1307
1308static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001309 .rpc_call_done = nfs4_open_confirm_done,
1310 .rpc_release = nfs4_open_confirm_release,
1311};
1312
1313/*
1314 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1315 */
1316static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1317{
1318 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1319 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001320 struct rpc_message msg = {
1321 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1322 .rpc_argp = &data->c_arg,
1323 .rpc_resp = &data->c_res,
1324 .rpc_cred = data->owner->so_cred,
1325 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001326 struct rpc_task_setup task_setup_data = {
1327 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001328 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001329 .callback_ops = &nfs4_open_confirm_ops,
1330 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001331 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001332 .flags = RPC_TASK_ASYNC,
1333 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 int status;
1335
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001336 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001337 data->rpc_done = 0;
1338 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001339 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001340 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001341 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001342 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001343 status = nfs4_wait_for_completion_rpc_task(task);
1344 if (status != 0) {
1345 data->cancelled = 1;
1346 smp_wmb();
1347 } else
1348 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001349 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 return status;
1351}
1352
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001353static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001355 struct nfs4_opendata *data = calldata;
1356 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001357
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001358 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1359 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001360 /*
1361 * Check if we still need to send an OPEN call, or if we can use
1362 * a delegation instead.
1363 */
1364 if (data->state != NULL) {
1365 struct nfs_delegation *delegation;
1366
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001367 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001368 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001369 rcu_read_lock();
1370 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1371 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001372 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001373 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001374 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001375 }
1376 rcu_read_unlock();
1377 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001378 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001379 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001380 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001381 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001382 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001383 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1384 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001385 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001386 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001387 &data->o_arg.seq_args,
1388 &data->o_res.seq_res, 1, task))
1389 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001390 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001391 return;
1392out_no_action:
1393 task->tk_action = NULL;
1394
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001395}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001397static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1398{
1399 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1400 nfs4_open_prepare(task, calldata);
1401}
1402
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001403static void nfs4_open_done(struct rpc_task *task, void *calldata)
1404{
1405 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001407 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001408
Trond Myklebust14516c32010-07-31 14:29:06 -04001409 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1410 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001411
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001412 if (task->tk_status == 0) {
1413 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001414 case S_IFREG:
1415 break;
1416 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001417 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001418 break;
1419 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001420 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001421 break;
1422 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001423 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001424 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001425 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001426 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1427 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001428 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001429 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001430}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001431
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001432static void nfs4_open_release(void *calldata)
1433{
1434 struct nfs4_opendata *data = calldata;
1435 struct nfs4_state *state = NULL;
1436
1437 /* If this request hasn't been cancelled, do nothing */
1438 if (data->cancelled == 0)
1439 goto out_free;
1440 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001441 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001442 goto out_free;
1443 /* In case we need an open_confirm, no cleanup! */
1444 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1445 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001446 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001447 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001448 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001449out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001450 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001451}
1452
1453static const struct rpc_call_ops nfs4_open_ops = {
1454 .rpc_call_prepare = nfs4_open_prepare,
1455 .rpc_call_done = nfs4_open_done,
1456 .rpc_release = nfs4_open_release,
1457};
1458
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001459static const struct rpc_call_ops nfs4_recover_open_ops = {
1460 .rpc_call_prepare = nfs4_recover_open_prepare,
1461 .rpc_call_done = nfs4_open_done,
1462 .rpc_release = nfs4_open_release,
1463};
1464
1465static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001466{
1467 struct inode *dir = data->dir->d_inode;
1468 struct nfs_server *server = NFS_SERVER(dir);
1469 struct nfs_openargs *o_arg = &data->o_arg;
1470 struct nfs_openres *o_res = &data->o_res;
1471 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001472 struct rpc_message msg = {
1473 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1474 .rpc_argp = o_arg,
1475 .rpc_resp = o_res,
1476 .rpc_cred = data->owner->so_cred,
1477 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001478 struct rpc_task_setup task_setup_data = {
1479 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001480 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001481 .callback_ops = &nfs4_open_ops,
1482 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001483 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001484 .flags = RPC_TASK_ASYNC,
1485 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001486 int status;
1487
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001488 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001489 data->rpc_done = 0;
1490 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001491 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001492 if (isrecover)
1493 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001494 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001495 if (IS_ERR(task))
1496 return PTR_ERR(task);
1497 status = nfs4_wait_for_completion_rpc_task(task);
1498 if (status != 0) {
1499 data->cancelled = 1;
1500 smp_wmb();
1501 } else
1502 status = data->rpc_status;
1503 rpc_put_task(task);
1504
1505 return status;
1506}
1507
1508static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1509{
1510 struct inode *dir = data->dir->d_inode;
1511 struct nfs_openres *o_res = &data->o_res;
1512 int status;
1513
1514 status = nfs4_run_open_task(data, 1);
1515 if (status != 0 || !data->rpc_done)
1516 return status;
1517
1518 nfs_refresh_inode(dir, o_res->dir_attr);
1519
1520 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1521 status = _nfs4_proc_open_confirm(data);
1522 if (status != 0)
1523 return status;
1524 }
1525
1526 return status;
1527}
1528
1529/*
1530 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1531 */
1532static int _nfs4_proc_open(struct nfs4_opendata *data)
1533{
1534 struct inode *dir = data->dir->d_inode;
1535 struct nfs_server *server = NFS_SERVER(dir);
1536 struct nfs_openargs *o_arg = &data->o_arg;
1537 struct nfs_openres *o_res = &data->o_res;
1538 int status;
1539
1540 status = nfs4_run_open_task(data, 0);
Trond Myklebust3e309912007-07-07 13:19:59 -04001541 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001542 return status;
1543
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001544 if (o_arg->open_flags & O_CREAT) {
1545 update_changeattr(dir, &o_res->cinfo);
1546 nfs_post_op_update_inode(dir, o_res->dir_attr);
1547 } else
1548 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001549 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1550 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001552 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001554 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 }
1556 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001557 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001558 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559}
1560
Trond Myklebust10afec92007-05-14 17:16:04 -04001561static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +01001562{
David Howells7539bba2006-08-22 20:06:09 -04001563 struct nfs_client *clp = server->nfs_client;
Trond Myklebusta78cb572009-08-09 15:06:19 -04001564 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001565 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001566
Trond Myklebusta78cb572009-08-09 15:06:19 -04001567 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001568 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001569 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001570 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001571 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1572 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001573 break;
Trond Myklebust58d97142006-01-03 09:55:24 +01001574 nfs4_schedule_state_recovery(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001575 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001576 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001577 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001578}
1579
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580/*
1581 * OPEN_EXPIRED:
1582 * reclaim state on the server after a network partition.
1583 * Assumes caller holds the appropriate lock
1584 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001585static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001587 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001588 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001590 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1591 if (IS_ERR(opendata))
1592 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001593 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001594 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001595 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001596 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001597 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598}
1599
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001600static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001601{
Trond Myklebust539cd032007-06-05 11:46:42 -04001602 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001603 struct nfs4_exception exception = { };
1604 int err;
1605
1606 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001607 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001608 switch (err) {
1609 default:
1610 goto out;
1611 case -NFS4ERR_GRACE:
1612 case -NFS4ERR_DELAY:
1613 nfs4_handle_exception(server, err, &exception);
1614 err = 0;
1615 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001616 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001617out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001618 return err;
1619}
1620
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1622{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001624 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Trond Myklebust864472e2006-01-03 09:55:15 +01001626 ctx = nfs4_state_find_open_context(state);
1627 if (IS_ERR(ctx))
1628 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001629 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001630 put_nfs_open_context(ctx);
1631 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632}
1633
1634/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001635 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1636 * fields corresponding to attributes that were used to store the verifier.
1637 * Make sure we clobber those fields in the later setattr call
1638 */
1639static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1640{
1641 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1642 !(sattr->ia_valid & ATTR_ATIME_SET))
1643 sattr->ia_valid |= ATTR_ATIME;
1644
1645 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1646 !(sattr->ia_valid & ATTR_MTIME_SET))
1647 sattr->ia_valid |= ATTR_MTIME;
1648}
1649
1650/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001651 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001653static 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 -07001654{
1655 struct nfs4_state_owner *sp;
1656 struct nfs4_state *state = NULL;
1657 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001658 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001659 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
1661 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 status = -ENOMEM;
1663 if (!(sp = nfs4_get_state_owner(server, cred))) {
1664 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1665 goto out_err;
1666 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001667 status = nfs4_recover_expired_lease(server);
1668 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001669 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001670 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001671 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001672 status = -ENOMEM;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001673 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001674 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001675 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Trond Myklebustaac00a82007-07-05 19:02:21 -04001677 if (path->dentry->d_inode != NULL)
1678 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1679
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001680 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001682 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001684 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001685 status = PTR_ERR(state);
1686 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001687 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001688 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001689 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001690
1691 if (opendata->o_arg.open_flags & O_EXCL) {
1692 nfs4_exclusive_attrset(opendata, sattr);
1693
1694 nfs_fattr_init(opendata->o_res.f_attr);
1695 status = nfs4_do_setattr(state->inode, cred,
1696 opendata->o_res.f_attr, sattr,
1697 state);
1698 if (status == 0)
1699 nfs_setattr_update_inode(state->inode, sattr);
1700 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1701 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001702 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 *res = state;
1705 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001706err_opendata_put:
1707 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001708err_put_state_owner:
1709 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 *res = NULL;
1712 return status;
1713}
1714
1715
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001716static 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 -07001717{
1718 struct nfs4_exception exception = { };
1719 struct nfs4_state *res;
1720 int status;
1721
1722 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001723 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 if (status == 0)
1725 break;
1726 /* NOTE: BAD_SEQID means the server and client disagree about the
1727 * book-keeping w.r.t. state-changing operations
1728 * (OPEN/CLOSE/LOCK/LOCKU...)
1729 * It is actually a sign of a bug on the client or on the server.
1730 *
1731 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001732 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 * have unhashed the old state_owner for us, and that we can
1734 * therefore safely retry using a new one. We should still warn
1735 * the user though...
1736 */
1737 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001738 printk(KERN_WARNING "NFS: v4 server %s "
1739 " returned a bad sequence-id error!\n",
1740 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 exception.retry = 1;
1742 continue;
1743 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001744 /*
1745 * BAD_STATEID on OPEN means that the server cancelled our
1746 * state before it received the OPEN_CONFIRM.
1747 * Recover by retrying the request as per the discussion
1748 * on Page 181 of RFC3530.
1749 */
1750 if (status == -NFS4ERR_BAD_STATEID) {
1751 exception.retry = 1;
1752 continue;
1753 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001754 if (status == -EAGAIN) {
1755 /* We must have found a delegation */
1756 exception.retry = 1;
1757 continue;
1758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1760 status, &exception));
1761 } while (exception.retry);
1762 return res;
1763}
1764
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001765static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1766 struct nfs_fattr *fattr, struct iattr *sattr,
1767 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001769 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001771 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 .iap = sattr,
1773 .server = server,
1774 .bitmask = server->attr_bitmask,
1775 };
1776 struct nfs_setattrres res = {
1777 .fattr = fattr,
1778 .server = server,
1779 };
1780 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001781 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1782 .rpc_argp = &arg,
1783 .rpc_resp = &res,
1784 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001786 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001787 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Trond Myklebust0e574af2005-10-27 22:12:38 -04001789 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001791 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1792 /* Use that stateid */
1793 } else if (state != NULL) {
Trond Myklebust77041ed2010-07-01 12:49:11 -04001794 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001795 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1797
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001798 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001799 if (status == 0 && state != NULL)
1800 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001801 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802}
1803
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001804static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1805 struct nfs_fattr *fattr, struct iattr *sattr,
1806 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001808 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 struct nfs4_exception exception = { };
1810 int err;
1811 do {
1812 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001813 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 &exception);
1815 } while (exception.retry);
1816 return err;
1817}
1818
1819struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001820 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 struct inode *inode;
1822 struct nfs4_state *state;
1823 struct nfs_closeargs arg;
1824 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001825 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001826 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827};
1828
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001829static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001830{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001831 struct nfs4_closedata *calldata = data;
1832 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001833
1834 nfs4_put_open_state(calldata->state);
1835 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001836 nfs4_put_state_owner(sp);
Jan Blunck31f31db12008-05-02 13:42:45 -07001837 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001838 kfree(calldata);
1839}
1840
Trond Myklebust88069f72009-12-08 08:33:16 -05001841static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1842 fmode_t fmode)
1843{
1844 spin_lock(&state->owner->so_lock);
1845 if (!(fmode & FMODE_READ))
1846 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1847 if (!(fmode & FMODE_WRITE))
1848 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1849 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1850 spin_unlock(&state->owner->so_lock);
1851}
1852
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001853static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001855 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 struct nfs_server *server = NFS_SERVER(calldata->inode);
1858
Trond Myklebust14516c32010-07-31 14:29:06 -04001859 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1860 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 /* hmm. we are done with the inode, and in the process of freeing
1862 * the state_owner. we keep this around to process errors
1863 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 switch (task->tk_status) {
1865 case 0:
Trond Myklebust45328c32007-07-26 17:47:34 -04001866 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001867 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001868 nfs4_close_clear_stateid_flags(state,
1869 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 break;
1871 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001872 case -NFS4ERR_OLD_STATEID:
1873 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001875 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001876 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001878 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1879 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001881 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001882 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883}
1884
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001885static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001887 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001888 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001889 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001890
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001891 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001892 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001893
Trond Myklebust88069f72009-12-08 08:33:16 -05001894 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1895 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001896 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001897 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001898 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001899 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001900 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1901 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1902 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001903 }
1904 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001905 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1906 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1907 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001908 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001909 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001910 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05001911
1912 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001913 /* Note: exit _without_ calling nfs4_close_done */
1914 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001915 return;
1916 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001917
1918 if (calldata->arg.fmode == 0)
1919 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
1920
Trond Myklebust516a6af2005-10-27 22:12:41 -04001921 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001922 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001923 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Andy Adamson19ddab02009-04-01 09:22:20 -04001924 &calldata->arg.seq_args, &calldata->res.seq_res,
1925 1, task))
1926 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001927 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928}
1929
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001930static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001931 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001932 .rpc_call_done = nfs4_close_done,
1933 .rpc_release = nfs4_free_closedata,
1934};
1935
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936/*
1937 * It is possible for data to be read/written from a mem-mapped file
1938 * after the sys_close call (which hits the vfs layer as a flush).
1939 * This means that we can't safely call nfsv4 close on a file until
1940 * the inode is cleared. This in turn means that we are not good
1941 * NFSv4 citizens - we do not indicate to the server to update the file's
1942 * share state even when we are done with one of the three share
1943 * stateid's in the inode.
1944 *
1945 * NOTE: Caller must be holding the sp->so_owner semaphore!
1946 */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001947int nfs4_do_close(struct path *path, struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001949 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001951 struct nfs4_state_owner *sp = state->owner;
1952 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001953 struct rpc_message msg = {
1954 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1955 .rpc_cred = state->owner->so_cred,
1956 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001957 struct rpc_task_setup task_setup_data = {
1958 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001959 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001960 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05001961 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001962 .flags = RPC_TASK_ASYNC,
1963 };
Trond Myklebust95121352005-10-18 14:20:12 -07001964 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001966 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001968 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001969 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001971 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04001972 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001974 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07001975 if (calldata->arg.seqid == NULL)
1976 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001977 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001978 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001979 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04001980 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001981 calldata->res.server = server;
Al Virof6948692010-01-30 13:51:04 -05001982 path_get(path);
1983 calldata->path = *path;
Trond Myklebust95121352005-10-18 14:20:12 -07001984
Trond Myklebust5138fde2007-07-14 15:40:01 -04001985 msg.rpc_argp = &calldata->arg,
1986 msg.rpc_resp = &calldata->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001987 task_setup_data.callback_data = calldata;
1988 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001989 if (IS_ERR(task))
1990 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001991 status = 0;
1992 if (wait)
1993 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001994 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001995 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07001996out_free_calldata:
1997 kfree(calldata);
1998out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04001999 nfs4_put_open_state(state);
2000 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002001 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002}
2003
Trond Myklebust2b484292010-09-17 10:56:51 -04002004static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002005nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 struct nfs4_state *state;
2008
Trond Myklebust565277f2007-10-15 18:17:53 -04002009 /* Protect against concurrent sillydeletes */
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002010 state = nfs4_do_open(dir, &ctx->path, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002011 if (IS_ERR(state))
2012 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002013 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002014 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015}
2016
Trond Myklebust1185a552009-12-03 15:54:02 -05002017static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002018{
2019 if (ctx->state == NULL)
2020 return;
2021 if (is_sync)
2022 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2023 else
2024 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2025}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
2027static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2028{
Benny Halevy43652ad2009-04-01 09:21:54 -04002029 struct nfs4_server_caps_arg args = {
2030 .fhandle = fhandle,
2031 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 struct nfs4_server_caps_res res = {};
2033 struct rpc_message msg = {
2034 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002035 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 .rpc_resp = &res,
2037 };
2038 int status;
2039
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002040 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 if (status == 0) {
2042 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002043 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2044 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2045 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2046 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2047 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2049 server->caps |= NFS_CAP_ACLS;
2050 if (res.has_links != 0)
2051 server->caps |= NFS_CAP_HARDLINKS;
2052 if (res.has_symlinks != 0)
2053 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002054 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2055 server->caps |= NFS_CAP_FILEID;
2056 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2057 server->caps |= NFS_CAP_MODE;
2058 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2059 server->caps |= NFS_CAP_NLINK;
2060 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2061 server->caps |= NFS_CAP_OWNER;
2062 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2063 server->caps |= NFS_CAP_OWNER_GROUP;
2064 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2065 server->caps |= NFS_CAP_ATIME;
2066 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2067 server->caps |= NFS_CAP_CTIME;
2068 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2069 server->caps |= NFS_CAP_MTIME;
2070
Trond Myklebusta65318b2009-03-11 14:10:28 -04002071 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2072 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2073 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 server->acl_bitmask = res.acl_bitmask;
2075 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002076
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 return status;
2078}
2079
Trond Myklebust55a97592006-06-09 09:34:19 -04002080int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081{
2082 struct nfs4_exception exception = { };
2083 int err;
2084 do {
2085 err = nfs4_handle_exception(server,
2086 _nfs4_server_capabilities(server, fhandle),
2087 &exception);
2088 } while (exception.retry);
2089 return err;
2090}
2091
2092static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2093 struct nfs_fsinfo *info)
2094{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 struct nfs4_lookup_root_arg args = {
2096 .bitmask = nfs4_fattr_bitmap,
2097 };
2098 struct nfs4_lookup_res res = {
2099 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002100 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 .fh = fhandle,
2102 };
2103 struct rpc_message msg = {
2104 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2105 .rpc_argp = &args,
2106 .rpc_resp = &res,
2107 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002108
Trond Myklebust0e574af2005-10-27 22:12:38 -04002109 nfs_fattr_init(info->fattr);
Trond Myklebustfccba802009-07-21 16:48:07 -04002110 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111}
2112
2113static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2114 struct nfs_fsinfo *info)
2115{
2116 struct nfs4_exception exception = { };
2117 int err;
2118 do {
2119 err = nfs4_handle_exception(server,
2120 _nfs4_lookup_root(server, fhandle, info),
2121 &exception);
2122 } while (exception.retry);
2123 return err;
2124}
2125
David Howells54ceac42006-08-22 20:06:13 -04002126/*
2127 * get the file handle for the "/" directory on the server
2128 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002130 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 int status;
2133
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 if (status == 0)
2136 status = nfs4_server_capabilities(server, fhandle);
2137 if (status == 0)
2138 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002139 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140}
2141
Manoj Naik6b97fd32006-06-09 09:34:29 -04002142/*
2143 * Get locations and (maybe) other attributes of a referral.
2144 * Note that we'll actually follow the referral later when
2145 * we detect fsid mismatch in inode revalidation
2146 */
Trond Myklebust56659e92007-07-17 21:52:39 -04002147static 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 -04002148{
2149 int status = -ENOMEM;
2150 struct page *page = NULL;
2151 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002152
2153 page = alloc_page(GFP_KERNEL);
2154 if (page == NULL)
2155 goto out;
2156 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2157 if (locations == NULL)
2158 goto out;
2159
Trond Myklebustc228fd32007-01-13 02:28:11 -05002160 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002161 if (status != 0)
2162 goto out;
2163 /* Make sure server returned a different fsid for the referral */
2164 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07002165 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 -04002166 status = -EIO;
2167 goto out;
2168 }
2169
2170 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2171 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2172 if (!fattr->mode)
2173 fattr->mode = S_IFDIR;
2174 memset(fhandle, 0, sizeof(struct nfs_fh));
2175out:
2176 if (page)
2177 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002178 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002179 return status;
2180}
2181
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2183{
2184 struct nfs4_getattr_arg args = {
2185 .fh = fhandle,
2186 .bitmask = server->attr_bitmask,
2187 };
2188 struct nfs4_getattr_res res = {
2189 .fattr = fattr,
2190 .server = server,
2191 };
2192 struct rpc_message msg = {
2193 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2194 .rpc_argp = &args,
2195 .rpc_resp = &res,
2196 };
2197
Trond Myklebust0e574af2005-10-27 22:12:38 -04002198 nfs_fattr_init(fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002199 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200}
2201
2202static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2203{
2204 struct nfs4_exception exception = { };
2205 int err;
2206 do {
2207 err = nfs4_handle_exception(server,
2208 _nfs4_proc_getattr(server, fhandle, fattr),
2209 &exception);
2210 } while (exception.retry);
2211 return err;
2212}
2213
2214/*
2215 * The file is not closed if it is opened due to the a request to change
2216 * the size of the file. The open call will not be needed once the
2217 * VFS layer lookup-intents are implemented.
2218 *
2219 * Close is called when the inode is destroyed.
2220 * If we haven't opened the file for O_WRONLY, we
2221 * need to in the size_change case to obtain a stateid.
2222 *
2223 * Got race?
2224 * Because OPEN is always done by name in nfsv4, it is
2225 * possible that we opened a different file by the same
2226 * name. We can recognize this race condition, but we
2227 * can't do anything about it besides returning an error.
2228 *
2229 * This will be fixed with VFS changes (lookup-intent).
2230 */
2231static int
2232nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2233 struct iattr *sattr)
2234{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002235 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002236 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002237 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 int status;
2239
Trond Myklebust0e574af2005-10-27 22:12:38 -04002240 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
Trond Myklebustd5308382005-11-04 15:33:38 -05002242 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002243 if (sattr->ia_valid & ATTR_FILE) {
2244 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002245
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002246 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002247 if (ctx) {
2248 cred = ctx->cred;
2249 state = ctx->state;
2250 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002251 }
2252
2253 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002254 if (status == 0)
2255 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 return status;
2257}
2258
Trond Myklebust56659e92007-07-17 21:52:39 -04002259static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2260 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04002261 struct nfs_fattr *fattr)
2262{
2263 int status;
2264 struct nfs4_lookup_arg args = {
2265 .bitmask = server->attr_bitmask,
2266 .dir_fh = dirfh,
2267 .name = name,
2268 };
2269 struct nfs4_lookup_res res = {
2270 .server = server,
2271 .fattr = fattr,
2272 .fh = fhandle,
2273 };
2274 struct rpc_message msg = {
2275 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2276 .rpc_argp = &args,
2277 .rpc_resp = &res,
2278 };
2279
2280 nfs_fattr_init(fattr);
2281
2282 dprintk("NFS call lookupfh %s\n", name->name);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002283 status = nfs4_call_sync(server, &msg, &args, &res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002284 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002285 return status;
2286}
2287
2288static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2289 struct qstr *name, struct nfs_fh *fhandle,
2290 struct nfs_fattr *fattr)
2291{
2292 struct nfs4_exception exception = { };
2293 int err;
2294 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04002295 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2296 /* FIXME: !!!! */
2297 if (err == -NFS4ERR_MOVED) {
2298 err = -EREMOTE;
2299 break;
2300 }
2301 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002302 } while (exception.retry);
2303 return err;
2304}
2305
Trond Myklebust56659e92007-07-17 21:52:39 -04002306static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2308{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002309 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
2311 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002312 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002313 if (status == -NFS4ERR_MOVED)
2314 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 dprintk("NFS reply lookup: %d\n", status);
2316 return status;
2317}
2318
2319static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2320{
2321 struct nfs4_exception exception = { };
2322 int err;
2323 do {
2324 err = nfs4_handle_exception(NFS_SERVER(dir),
2325 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2326 &exception);
2327 } while (exception.retry);
2328 return err;
2329}
2330
2331static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2332{
Trond Myklebust76b32992007-08-10 17:45:11 -04002333 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 struct nfs4_accessargs args = {
2335 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002336 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002338 struct nfs4_accessres res = {
2339 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002340 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 struct rpc_message msg = {
2342 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2343 .rpc_argp = &args,
2344 .rpc_resp = &res,
2345 .rpc_cred = entry->cred,
2346 };
2347 int mode = entry->mask;
2348 int status;
2349
2350 /*
2351 * Determine which access bits we want to ask for...
2352 */
2353 if (mode & MAY_READ)
2354 args.access |= NFS4_ACCESS_READ;
2355 if (S_ISDIR(inode->i_mode)) {
2356 if (mode & MAY_WRITE)
2357 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2358 if (mode & MAY_EXEC)
2359 args.access |= NFS4_ACCESS_LOOKUP;
2360 } else {
2361 if (mode & MAY_WRITE)
2362 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2363 if (mode & MAY_EXEC)
2364 args.access |= NFS4_ACCESS_EXECUTE;
2365 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002366
2367 res.fattr = nfs_alloc_fattr();
2368 if (res.fattr == NULL)
2369 return -ENOMEM;
2370
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002371 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 if (!status) {
2373 entry->mask = 0;
2374 if (res.access & NFS4_ACCESS_READ)
2375 entry->mask |= MAY_READ;
2376 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2377 entry->mask |= MAY_WRITE;
2378 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2379 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002380 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002382 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 return status;
2384}
2385
2386static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2387{
2388 struct nfs4_exception exception = { };
2389 int err;
2390 do {
2391 err = nfs4_handle_exception(NFS_SERVER(inode),
2392 _nfs4_proc_access(inode, entry),
2393 &exception);
2394 } while (exception.retry);
2395 return err;
2396}
2397
2398/*
2399 * TODO: For the time being, we don't try to get any attributes
2400 * along with any of the zero-copy operations READ, READDIR,
2401 * READLINK, WRITE.
2402 *
2403 * In the case of the first three, we want to put the GETATTR
2404 * after the read-type operation -- this is because it is hard
2405 * to predict the length of a GETATTR response in v4, and thus
2406 * align the READ data correctly. This means that the GETATTR
2407 * may end up partially falling into the page cache, and we should
2408 * shift it into the 'tail' of the xdr_buf before processing.
2409 * To do this efficiently, we need to know the total length
2410 * of data received, which doesn't seem to be available outside
2411 * of the RPC layer.
2412 *
2413 * In the case of WRITE, we also want to put the GETATTR after
2414 * the operation -- in this case because we want to make sure
2415 * we get the post-operation mtime and size. This means that
2416 * we can't use xdr_encode_pages() as written: we need a variant
2417 * of it which would leave room in the 'tail' iovec.
2418 *
2419 * Both of these changes to the XDR layer would in fact be quite
2420 * minor, but I decided to leave them for a subsequent patch.
2421 */
2422static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2423 unsigned int pgbase, unsigned int pglen)
2424{
2425 struct nfs4_readlink args = {
2426 .fh = NFS_FH(inode),
2427 .pgbase = pgbase,
2428 .pglen = pglen,
2429 .pages = &page,
2430 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002431 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 struct rpc_message msg = {
2433 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2434 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002435 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 };
2437
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002438 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439}
2440
2441static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2442 unsigned int pgbase, unsigned int pglen)
2443{
2444 struct nfs4_exception exception = { };
2445 int err;
2446 do {
2447 err = nfs4_handle_exception(NFS_SERVER(inode),
2448 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2449 &exception);
2450 } while (exception.retry);
2451 return err;
2452}
2453
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454/*
2455 * Got race?
2456 * We will need to arrange for the VFS layer to provide an atomic open.
2457 * Until then, this create/open method is prone to inefficiency and race
2458 * conditions due to the lookup, create, and open VFS calls from sys_open()
2459 * placed on the wire.
2460 *
2461 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2462 * The file will be opened again in the subsequent VFS open call
2463 * (nfs4_proc_file_open).
2464 *
2465 * The open for read will just hang around to be used by any process that
2466 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2467 */
2468
2469static int
2470nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002471 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472{
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002473 struct path my_path = {
Trond Myklebustad389da2007-06-05 12:30:00 -04002474 .dentry = dentry,
2475 };
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002476 struct path *path = &my_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002478 struct rpc_cred *cred = NULL;
2479 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 int status = 0;
2481
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002482 if (ctx != NULL) {
2483 cred = ctx->cred;
2484 path = &ctx->path;
2485 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 }
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002487 state = nfs4_do_open(dir, path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002488 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 if (IS_ERR(state)) {
2490 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002491 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002493 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002494 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002495 if (ctx != NULL)
2496 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002497 else
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002498 nfs4_close_sync(path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499out:
2500 return status;
2501}
2502
2503static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2504{
Trond Myklebust16e42952005-10-27 22:12:44 -04002505 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002506 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002508 .name.len = name->len,
2509 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002510 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002512 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002513 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002514 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002516 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2517 .rpc_argp = &args,
2518 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002520 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
Trond Myklebustd3468902010-04-16 16:22:50 -04002522 res.dir_attr = nfs_alloc_fattr();
2523 if (res.dir_attr == NULL)
2524 goto out;
2525
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002526 status = nfs4_call_sync(server, &msg, &args, &res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002527 if (status == 0) {
2528 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002529 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002530 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002531 nfs_free_fattr(res.dir_attr);
2532out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 return status;
2534}
2535
2536static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2537{
2538 struct nfs4_exception exception = { };
2539 int err;
2540 do {
2541 err = nfs4_handle_exception(NFS_SERVER(dir),
2542 _nfs4_proc_remove(dir, name),
2543 &exception);
2544 } while (exception.retry);
2545 return err;
2546}
2547
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002548static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002550 struct nfs_server *server = NFS_SERVER(dir);
2551 struct nfs_removeargs *args = msg->rpc_argp;
2552 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
Trond Myklebusta65318b2009-03-11 14:10:28 -04002554 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002555 res->server = server;
Benny Halevydfb4f3092010-09-24 09:17:01 -04002556 res->seq_res.sr_slot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558}
2559
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002560static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002562 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2563
Trond Myklebust14516c32010-07-31 14:29:06 -04002564 if (!nfs4_sequence_done(task, &res->seq_res))
2565 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002566 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002567 return 0;
2568 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002569 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002570 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571}
2572
Jeff Laytond3d41522010-09-17 17:31:57 -04002573static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2574{
2575 struct nfs_server *server = NFS_SERVER(dir);
2576 struct nfs_renameargs *arg = msg->rpc_argp;
2577 struct nfs_renameres *res = msg->rpc_resp;
2578
2579 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2580 arg->bitmask = server->attr_bitmask;
2581 res->server = server;
2582}
2583
2584static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2585 struct inode *new_dir)
2586{
2587 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2588
2589 if (!nfs4_sequence_done(task, &res->seq_res))
2590 return 0;
2591 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2592 return 0;
2593
2594 update_changeattr(old_dir, &res->old_cinfo);
2595 nfs_post_op_update_inode(old_dir, res->old_fattr);
2596 update_changeattr(new_dir, &res->new_cinfo);
2597 nfs_post_op_update_inode(new_dir, res->new_fattr);
2598 return 1;
2599}
2600
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2602 struct inode *new_dir, struct qstr *new_name)
2603{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002604 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002605 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 .old_dir = NFS_FH(old_dir),
2607 .new_dir = NFS_FH(new_dir),
2608 .old_name = old_name,
2609 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002610 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002612 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002613 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002614 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 struct rpc_message msg = {
2616 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2617 .rpc_argp = &arg,
2618 .rpc_resp = &res,
2619 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002620 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
Trond Myklebust011fff72010-04-16 16:22:49 -04002622 res.old_fattr = nfs_alloc_fattr();
2623 res.new_fattr = nfs_alloc_fattr();
2624 if (res.old_fattr == NULL || res.new_fattr == NULL)
2625 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626
Trond Myklebust011fff72010-04-16 16:22:49 -04002627 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 if (!status) {
2629 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002630 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002632 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002634out:
2635 nfs_free_fattr(res.new_fattr);
2636 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 return status;
2638}
2639
2640static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2641 struct inode *new_dir, struct qstr *new_name)
2642{
2643 struct nfs4_exception exception = { };
2644 int err;
2645 do {
2646 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2647 _nfs4_proc_rename(old_dir, old_name,
2648 new_dir, new_name),
2649 &exception);
2650 } while (exception.retry);
2651 return err;
2652}
2653
2654static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2655{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002656 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 struct nfs4_link_arg arg = {
2658 .fh = NFS_FH(inode),
2659 .dir_fh = NFS_FH(dir),
2660 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002661 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002663 struct nfs4_link_res res = {
2664 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002665 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 struct rpc_message msg = {
2667 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2668 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002669 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002671 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
Trond Myklebust136f2622010-04-16 16:22:49 -04002673 res.fattr = nfs_alloc_fattr();
2674 res.dir_attr = nfs_alloc_fattr();
2675 if (res.fattr == NULL || res.dir_attr == NULL)
2676 goto out;
2677
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002678 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002679 if (!status) {
2680 update_changeattr(dir, &res.cinfo);
2681 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002682 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002683 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002684out:
2685 nfs_free_fattr(res.dir_attr);
2686 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 return status;
2688}
2689
2690static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2691{
2692 struct nfs4_exception exception = { };
2693 int err;
2694 do {
2695 err = nfs4_handle_exception(NFS_SERVER(inode),
2696 _nfs4_proc_link(inode, dir, name),
2697 &exception);
2698 } while (exception.retry);
2699 return err;
2700}
2701
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002702struct nfs4_createdata {
2703 struct rpc_message msg;
2704 struct nfs4_create_arg arg;
2705 struct nfs4_create_res res;
2706 struct nfs_fh fh;
2707 struct nfs_fattr fattr;
2708 struct nfs_fattr dir_fattr;
2709};
2710
2711static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2712 struct qstr *name, struct iattr *sattr, u32 ftype)
2713{
2714 struct nfs4_createdata *data;
2715
2716 data = kzalloc(sizeof(*data), GFP_KERNEL);
2717 if (data != NULL) {
2718 struct nfs_server *server = NFS_SERVER(dir);
2719
2720 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2721 data->msg.rpc_argp = &data->arg;
2722 data->msg.rpc_resp = &data->res;
2723 data->arg.dir_fh = NFS_FH(dir);
2724 data->arg.server = server;
2725 data->arg.name = name;
2726 data->arg.attrs = sattr;
2727 data->arg.ftype = ftype;
2728 data->arg.bitmask = server->attr_bitmask;
2729 data->res.server = server;
2730 data->res.fh = &data->fh;
2731 data->res.fattr = &data->fattr;
2732 data->res.dir_fattr = &data->dir_fattr;
2733 nfs_fattr_init(data->res.fattr);
2734 nfs_fattr_init(data->res.dir_fattr);
2735 }
2736 return data;
2737}
2738
2739static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2740{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002741 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2742 &data->arg, &data->res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002743 if (status == 0) {
2744 update_changeattr(dir, &data->res.dir_cinfo);
2745 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2746 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2747 }
2748 return status;
2749}
2750
2751static void nfs4_free_createdata(struct nfs4_createdata *data)
2752{
2753 kfree(data);
2754}
2755
Chuck Lever4f390c12006-08-22 20:06:22 -04002756static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002757 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002759 struct nfs4_createdata *data;
2760 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761
Chuck Lever94a6d752006-08-22 20:06:23 -04002762 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002763 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002764
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002765 status = -ENOMEM;
2766 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2767 if (data == NULL)
2768 goto out;
2769
2770 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2771 data->arg.u.symlink.pages = &page;
2772 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002774 status = nfs4_do_create(dir, dentry, data);
2775
2776 nfs4_free_createdata(data);
2777out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 return status;
2779}
2780
Chuck Lever4f390c12006-08-22 20:06:22 -04002781static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002782 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783{
2784 struct nfs4_exception exception = { };
2785 int err;
2786 do {
2787 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002788 _nfs4_proc_symlink(dir, dentry, page,
2789 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 &exception);
2791 } while (exception.retry);
2792 return err;
2793}
2794
2795static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2796 struct iattr *sattr)
2797{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002798 struct nfs4_createdata *data;
2799 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002801 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2802 if (data == NULL)
2803 goto out;
2804
2805 status = nfs4_do_create(dir, dentry, data);
2806
2807 nfs4_free_createdata(data);
2808out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 return status;
2810}
2811
2812static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2813 struct iattr *sattr)
2814{
2815 struct nfs4_exception exception = { };
2816 int err;
2817 do {
2818 err = nfs4_handle_exception(NFS_SERVER(dir),
2819 _nfs4_proc_mkdir(dir, dentry, sattr),
2820 &exception);
2821 } while (exception.retry);
2822 return err;
2823}
2824
2825static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2826 u64 cookie, struct page *page, unsigned int count, int plus)
2827{
2828 struct inode *dir = dentry->d_inode;
2829 struct nfs4_readdir_arg args = {
2830 .fh = NFS_FH(dir),
2831 .pages = &page,
2832 .pgbase = 0,
2833 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002834 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 };
2836 struct nfs4_readdir_res res;
2837 struct rpc_message msg = {
2838 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2839 .rpc_argp = &args,
2840 .rpc_resp = &res,
2841 .rpc_cred = cred,
2842 };
2843 int status;
2844
Harvey Harrison3110ff82008-05-02 13:42:44 -07002845 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002846 dentry->d_parent->d_name.name,
2847 dentry->d_name.name,
2848 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2850 res.pgbase = args.pgbase;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002851 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 if (status == 0)
2853 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustc4812992007-09-28 17:11:45 -04002854
2855 nfs_invalidate_atime(dir);
2856
Harvey Harrison3110ff82008-05-02 13:42:44 -07002857 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 return status;
2859}
2860
2861static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2862 u64 cookie, struct page *page, unsigned int count, int plus)
2863{
2864 struct nfs4_exception exception = { };
2865 int err;
2866 do {
2867 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2868 _nfs4_proc_readdir(dentry, cred, cookie,
2869 page, count, plus),
2870 &exception);
2871 } while (exception.retry);
2872 return err;
2873}
2874
2875static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2876 struct iattr *sattr, dev_t rdev)
2877{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002878 struct nfs4_createdata *data;
2879 int mode = sattr->ia_mode;
2880 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881
2882 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2883 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002884
2885 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2886 if (data == NULL)
2887 goto out;
2888
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002890 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002892 data->arg.ftype = NF4BLK;
2893 data->arg.u.device.specdata1 = MAJOR(rdev);
2894 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 }
2896 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002897 data->arg.ftype = NF4CHR;
2898 data->arg.u.device.specdata1 = MAJOR(rdev);
2899 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002902 status = nfs4_do_create(dir, dentry, data);
2903
2904 nfs4_free_createdata(data);
2905out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 return status;
2907}
2908
2909static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2910 struct iattr *sattr, dev_t rdev)
2911{
2912 struct nfs4_exception exception = { };
2913 int err;
2914 do {
2915 err = nfs4_handle_exception(NFS_SERVER(dir),
2916 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2917 &exception);
2918 } while (exception.retry);
2919 return err;
2920}
2921
2922static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2923 struct nfs_fsstat *fsstat)
2924{
2925 struct nfs4_statfs_arg args = {
2926 .fh = fhandle,
2927 .bitmask = server->attr_bitmask,
2928 };
Benny Halevy24ad1482009-04-01 09:21:56 -04002929 struct nfs4_statfs_res res = {
2930 .fsstat = fsstat,
2931 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 struct rpc_message msg = {
2933 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2934 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04002935 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 };
2937
Trond Myklebust0e574af2005-10-27 22:12:38 -04002938 nfs_fattr_init(fsstat->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002939 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940}
2941
2942static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2943{
2944 struct nfs4_exception exception = { };
2945 int err;
2946 do {
2947 err = nfs4_handle_exception(server,
2948 _nfs4_proc_statfs(server, fhandle, fsstat),
2949 &exception);
2950 } while (exception.retry);
2951 return err;
2952}
2953
2954static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2955 struct nfs_fsinfo *fsinfo)
2956{
2957 struct nfs4_fsinfo_arg args = {
2958 .fh = fhandle,
2959 .bitmask = server->attr_bitmask,
2960 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04002961 struct nfs4_fsinfo_res res = {
2962 .fsinfo = fsinfo,
2963 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 struct rpc_message msg = {
2965 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2966 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04002967 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 };
2969
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002970 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971}
2972
2973static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2974{
2975 struct nfs4_exception exception = { };
2976 int err;
2977
2978 do {
2979 err = nfs4_handle_exception(server,
2980 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2981 &exception);
2982 } while (exception.retry);
2983 return err;
2984}
2985
2986static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2987{
Trond Myklebust0e574af2005-10-27 22:12:38 -04002988 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2990}
2991
2992static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2993 struct nfs_pathconf *pathconf)
2994{
2995 struct nfs4_pathconf_arg args = {
2996 .fh = fhandle,
2997 .bitmask = server->attr_bitmask,
2998 };
Benny Halevyd45b2982009-04-01 09:21:58 -04002999 struct nfs4_pathconf_res res = {
3000 .pathconf = pathconf,
3001 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 struct rpc_message msg = {
3003 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3004 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003005 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 };
3007
3008 /* None of the pathconf attributes are mandatory to implement */
3009 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3010 memset(pathconf, 0, sizeof(*pathconf));
3011 return 0;
3012 }
3013
Trond Myklebust0e574af2005-10-27 22:12:38 -04003014 nfs_fattr_init(pathconf->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003015 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016}
3017
3018static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3019 struct nfs_pathconf *pathconf)
3020{
3021 struct nfs4_exception exception = { };
3022 int err;
3023
3024 do {
3025 err = nfs4_handle_exception(server,
3026 _nfs4_proc_pathconf(server, fhandle, pathconf),
3027 &exception);
3028 } while (exception.retry);
3029 return err;
3030}
3031
Trond Myklebustec06c092006-03-20 13:44:27 -05003032static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033{
Trond Myklebustec06c092006-03-20 13:44:27 -05003034 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003036 dprintk("--> %s\n", __func__);
3037
Trond Myklebust14516c32010-07-31 14:29:06 -04003038 if (!nfs4_sequence_done(task, &data->res.seq_res))
3039 return -EAGAIN;
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003040
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003041 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003042 nfs_restart_rpc(task, server->nfs_client);
Trond Myklebustec06c092006-03-20 13:44:27 -05003043 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003045
3046 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003048 renew_lease(server, data->timestamp);
3049 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050}
3051
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003052static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 data->timestamp = jiffies;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003055 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056}
3057
Trond Myklebust788e7a82006-03-20 13:44:27 -05003058static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 struct inode *inode = data->inode;
3061
Trond Myklebust14516c32010-07-31 14:29:06 -04003062 if (!nfs4_sequence_done(task, &data->res.seq_res))
3063 return -EAGAIN;
Andy Adamsondef6ed72009-04-01 09:22:26 -04003064
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003065 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003066 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003067 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003069 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003071 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003072 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003073 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074}
3075
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003076static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003078 struct nfs_server *server = NFS_SERVER(data->inode);
3079
Trond Myklebusta65318b2009-03-11 14:10:28 -04003080 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003081 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 data->timestamp = jiffies;
3083
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003084 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085}
3086
Trond Myklebust788e7a82006-03-20 13:44:27 -05003087static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 struct inode *inode = data->inode;
3090
Trond Myklebust14516c32010-07-31 14:29:06 -04003091 if (!nfs4_sequence_done(task, &data->res.seq_res))
3092 return -EAGAIN;
3093
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003094 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003095 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003096 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003098 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003099 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100}
3101
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003102static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003104 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105
Trond Myklebusta65318b2009-03-11 14:10:28 -04003106 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003107 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003108 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109}
3110
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003111struct nfs4_renewdata {
3112 struct nfs_client *client;
3113 unsigned long timestamp;
3114};
3115
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116/*
3117 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3118 * standalone procedure for queueing an asynchronous RENEW.
3119 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003120static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003121{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003122 struct nfs4_renewdata *data = calldata;
3123 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003124
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003125 if (atomic_read(&clp->cl_count) > 1)
3126 nfs4_schedule_state_renewal(clp);
3127 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003128 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003129}
3130
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003131static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003133 struct nfs4_renewdata *data = calldata;
3134 struct nfs_client *clp = data->client;
3135 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
3137 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003138 /* Unless we're shutting down, schedule state recovery! */
3139 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3140 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 return;
3142 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003143 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144}
3145
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003146static const struct rpc_call_ops nfs4_renew_ops = {
3147 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003148 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003149};
3150
David Howellsadfa6f92006-08-22 20:06:08 -04003151int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152{
3153 struct rpc_message msg = {
3154 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3155 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003156 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003158 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003160 if (!atomic_inc_not_zero(&clp->cl_count))
3161 return -EIO;
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003162 data = kmalloc(sizeof(*data), GFP_KERNEL);
3163 if (data == NULL)
3164 return -ENOMEM;
3165 data->client = clp;
3166 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003168 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169}
3170
David Howellsadfa6f92006-08-22 20:06:08 -04003171int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172{
3173 struct rpc_message msg = {
3174 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3175 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003176 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 };
3178 unsigned long now = jiffies;
3179 int status;
3180
3181 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3182 if (status < 0)
3183 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003184 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 return 0;
3186}
3187
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003188static inline int nfs4_server_supports_acls(struct nfs_server *server)
3189{
3190 return (server->caps & NFS_CAP_ACLS)
3191 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3192 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3193}
3194
3195/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3196 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3197 * the stack.
3198 */
3199#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3200
3201static void buf_to_pages(const void *buf, size_t buflen,
3202 struct page **pages, unsigned int *pgbase)
3203{
3204 const void *p = buf;
3205
3206 *pgbase = offset_in_page(buf);
3207 p -= *pgbase;
3208 while (p < buf + buflen) {
3209 *(pages++) = virt_to_page(p);
3210 p += PAGE_CACHE_SIZE;
3211 }
3212}
3213
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003214struct nfs4_cached_acl {
3215 int cached;
3216 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003217 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003218};
3219
3220static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003221{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003222 struct nfs_inode *nfsi = NFS_I(inode);
3223
3224 spin_lock(&inode->i_lock);
3225 kfree(nfsi->nfs4_acl);
3226 nfsi->nfs4_acl = acl;
3227 spin_unlock(&inode->i_lock);
3228}
3229
3230static void nfs4_zap_acl_attr(struct inode *inode)
3231{
3232 nfs4_set_cached_acl(inode, NULL);
3233}
3234
3235static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3236{
3237 struct nfs_inode *nfsi = NFS_I(inode);
3238 struct nfs4_cached_acl *acl;
3239 int ret = -ENOENT;
3240
3241 spin_lock(&inode->i_lock);
3242 acl = nfsi->nfs4_acl;
3243 if (acl == NULL)
3244 goto out;
3245 if (buf == NULL) /* user is just asking for length */
3246 goto out_len;
3247 if (acl->cached == 0)
3248 goto out;
3249 ret = -ERANGE; /* see getxattr(2) man page */
3250 if (acl->len > buflen)
3251 goto out;
3252 memcpy(buf, acl->data, acl->len);
3253out_len:
3254 ret = acl->len;
3255out:
3256 spin_unlock(&inode->i_lock);
3257 return ret;
3258}
3259
3260static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3261{
3262 struct nfs4_cached_acl *acl;
3263
3264 if (buf && acl_len <= PAGE_SIZE) {
3265 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3266 if (acl == NULL)
3267 goto out;
3268 acl->cached = 1;
3269 memcpy(acl->data, buf, acl_len);
3270 } else {
3271 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3272 if (acl == NULL)
3273 goto out;
3274 acl->cached = 0;
3275 }
3276 acl->len = acl_len;
3277out:
3278 nfs4_set_cached_acl(inode, acl);
3279}
3280
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003281static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003282{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003283 struct page *pages[NFS4ACL_MAXPAGES];
3284 struct nfs_getaclargs args = {
3285 .fh = NFS_FH(inode),
3286 .acl_pages = pages,
3287 .acl_len = buflen,
3288 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003289 struct nfs_getaclres res = {
3290 .acl_len = buflen,
3291 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003292 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003293 struct rpc_message msg = {
3294 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3295 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003296 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003297 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003298 struct page *localpage = NULL;
3299 int ret;
3300
3301 if (buflen < PAGE_SIZE) {
3302 /* As long as we're doing a round trip to the server anyway,
3303 * let's be prepared for a page of acl data. */
3304 localpage = alloc_page(GFP_KERNEL);
3305 resp_buf = page_address(localpage);
3306 if (localpage == NULL)
3307 return -ENOMEM;
3308 args.acl_pages[0] = localpage;
3309 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003310 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003311 } else {
3312 resp_buf = buf;
3313 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3314 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003315 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003316 if (ret)
3317 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003318 if (res.acl_len > args.acl_len)
3319 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003320 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003321 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003322 if (buf) {
3323 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003324 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003325 goto out_free;
3326 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003327 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003328 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003329 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003330out_free:
3331 if (localpage)
3332 __free_page(localpage);
3333 return ret;
3334}
3335
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003336static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3337{
3338 struct nfs4_exception exception = { };
3339 ssize_t ret;
3340 do {
3341 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3342 if (ret >= 0)
3343 break;
3344 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3345 } while (exception.retry);
3346 return ret;
3347}
3348
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003349static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3350{
3351 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003352 int ret;
3353
3354 if (!nfs4_server_supports_acls(server))
3355 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003356 ret = nfs_revalidate_inode(server, inode);
3357 if (ret < 0)
3358 return ret;
3359 ret = nfs4_read_cached_acl(inode, buf, buflen);
3360 if (ret != -ENOENT)
3361 return ret;
3362 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003363}
3364
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003365static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003366{
3367 struct nfs_server *server = NFS_SERVER(inode);
3368 struct page *pages[NFS4ACL_MAXPAGES];
3369 struct nfs_setaclargs arg = {
3370 .fh = NFS_FH(inode),
3371 .acl_pages = pages,
3372 .acl_len = buflen,
3373 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003374 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003375 struct rpc_message msg = {
3376 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3377 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003378 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003379 };
3380 int ret;
3381
3382 if (!nfs4_server_supports_acls(server))
3383 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07003384 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003385 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003386 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003387 nfs_access_zap_cache(inode);
3388 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003389 return ret;
3390}
3391
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003392static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3393{
3394 struct nfs4_exception exception = { };
3395 int err;
3396 do {
3397 err = nfs4_handle_exception(NFS_SERVER(inode),
3398 __nfs4_proc_set_acl(inode, buf, buflen),
3399 &exception);
3400 } while (exception.retry);
3401 return err;
3402}
3403
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003405nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003407 struct nfs_client *clp = server->nfs_client;
3408
3409 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 return 0;
3411 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003412 case -NFS4ERR_ADMIN_REVOKED:
3413 case -NFS4ERR_BAD_STATEID:
3414 case -NFS4ERR_OPENMODE:
3415 if (state == NULL)
3416 break;
3417 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003418 goto do_state_recovery;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003420 case -NFS4ERR_STALE_CLIENTID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 case -NFS4ERR_EXPIRED:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003422 goto do_state_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003423#if defined(CONFIG_NFS_V4_1)
3424 case -NFS4ERR_BADSESSION:
3425 case -NFS4ERR_BADSLOT:
3426 case -NFS4ERR_BAD_HIGH_SLOT:
3427 case -NFS4ERR_DEADSESSION:
3428 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3429 case -NFS4ERR_SEQ_FALSE_RETRY:
3430 case -NFS4ERR_SEQ_MISORDERED:
3431 dprintk("%s ERROR %d, Reset session\n", __func__,
3432 task->tk_status);
Andy Adamson0b9e2d42009-12-04 16:02:14 -05003433 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -04003434 task->tk_status = 0;
3435 return -EAGAIN;
3436#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003438 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003439 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003440 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3442 task->tk_status = 0;
3443 return -EAGAIN;
3444 case -NFS4ERR_OLD_STATEID:
3445 task->tk_status = 0;
3446 return -EAGAIN;
3447 }
3448 task->tk_status = nfs4_map_errors(task->tk_status);
3449 return 0;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003450do_state_recovery:
3451 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3452 nfs4_schedule_state_recovery(clp);
3453 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3454 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3455 task->tk_status = 0;
3456 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457}
3458
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003459int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3460 unsigned short port, struct rpc_cred *cred,
3461 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462{
3463 nfs4_verifier sc_verifier;
3464 struct nfs4_setclientid setclientid = {
3465 .sc_verifier = &sc_verifier,
3466 .sc_prog = program,
3467 };
3468 struct rpc_message msg = {
3469 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3470 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003471 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003472 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 };
Al Virobc4785c2006-10-19 23:28:51 -07003474 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 int loop = 0;
3476 int status;
3477
Al Virobc4785c2006-10-19 23:28:51 -07003478 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3480 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3481
3482 for(;;) {
3483 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003484 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003485 clp->cl_ipaddr,
3486 rpc_peeraddr2str(clp->cl_rpcclient,
3487 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003488 rpc_peeraddr2str(clp->cl_rpcclient,
3489 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003490 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 clp->cl_id_uniquifier);
3492 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003493 sizeof(setclientid.sc_netid),
3494 rpc_peeraddr2str(clp->cl_rpcclient,
3495 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003497 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 clp->cl_ipaddr, port >> 8, port & 255);
3499
3500 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3501 if (status != -NFS4ERR_CLID_INUSE)
3502 break;
3503 if (signalled())
3504 break;
3505 if (loop++ & 1)
3506 ssleep(clp->cl_lease_time + 1);
3507 else
3508 if (++clp->cl_id_uniquifier == 0)
3509 break;
3510 }
3511 return status;
3512}
3513
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003514static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3515 struct nfs4_setclientid_res *arg,
3516 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517{
3518 struct nfs_fsinfo fsinfo;
3519 struct rpc_message msg = {
3520 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003521 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003523 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 };
3525 unsigned long now;
3526 int status;
3527
3528 now = jiffies;
3529 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3530 if (status == 0) {
3531 spin_lock(&clp->cl_lock);
3532 clp->cl_lease_time = fsinfo.lease_time * HZ;
3533 clp->cl_last_renewal = now;
3534 spin_unlock(&clp->cl_lock);
3535 }
3536 return status;
3537}
3538
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003539int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3540 struct nfs4_setclientid_res *arg,
3541 struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003542{
Benny Halevy77e03672008-06-24 20:25:57 +03003543 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003544 int err;
3545 do {
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003546 err = _nfs4_proc_setclientid_confirm(clp, arg, cred);
Trond Myklebust51581f32006-03-20 13:44:47 -05003547 switch (err) {
3548 case 0:
3549 return err;
3550 case -NFS4ERR_RESOURCE:
3551 /* The IBM lawyers misread another document! */
3552 case -NFS4ERR_DELAY:
3553 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3554 }
3555 } while (err == 0);
3556 return err;
3557}
3558
Trond Myklebustfe650402006-01-03 09:55:18 +01003559struct nfs4_delegreturndata {
3560 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003561 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003562 struct nfs_fh fh;
3563 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003564 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003565 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003566 int rpc_status;
3567};
3568
Trond Myklebustfe650402006-01-03 09:55:18 +01003569static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3570{
3571 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003572
Trond Myklebust14516c32010-07-31 14:29:06 -04003573 if (!nfs4_sequence_done(task, &data->res.seq_res))
3574 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003575
Ricardo Labiaga79708862009-12-07 09:23:21 -05003576 switch (task->tk_status) {
3577 case -NFS4ERR_STALE_STATEID:
3578 case -NFS4ERR_EXPIRED:
3579 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003580 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003581 break;
3582 default:
3583 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3584 -EAGAIN) {
3585 nfs_restart_rpc(task, data->res.server->nfs_client);
3586 return;
3587 }
3588 }
3589 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003590}
3591
3592static void nfs4_delegreturn_release(void *calldata)
3593{
Trond Myklebustfe650402006-01-03 09:55:18 +01003594 kfree(calldata);
3595}
3596
Andy Adamson938e1012009-04-01 09:22:28 -04003597#if defined(CONFIG_NFS_V4_1)
3598static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3599{
3600 struct nfs4_delegreturndata *d_data;
3601
3602 d_data = (struct nfs4_delegreturndata *)data;
3603
Trond Myklebust035168a2010-06-16 09:52:26 -04003604 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003605 &d_data->args.seq_args,
3606 &d_data->res.seq_res, 1, task))
3607 return;
3608 rpc_call_start(task);
3609}
3610#endif /* CONFIG_NFS_V4_1 */
3611
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003612static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003613#if defined(CONFIG_NFS_V4_1)
3614 .rpc_call_prepare = nfs4_delegreturn_prepare,
3615#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003616 .rpc_call_done = nfs4_delegreturn_done,
3617 .rpc_release = nfs4_delegreturn_release,
3618};
3619
Trond Myklebuste6f81072008-01-24 18:14:34 -05003620static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003621{
3622 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003623 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003624 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003625 struct rpc_message msg = {
3626 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3627 .rpc_cred = cred,
3628 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003629 struct rpc_task_setup task_setup_data = {
3630 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003631 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003632 .callback_ops = &nfs4_delegreturn_ops,
3633 .flags = RPC_TASK_ASYNC,
3634 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003635 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003636
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003637 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003638 if (data == NULL)
3639 return -ENOMEM;
3640 data->args.fhandle = &data->fh;
3641 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003642 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003643 nfs_copy_fh(&data->fh, NFS_FH(inode));
3644 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003645 data->res.fattr = &data->fattr;
3646 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003647 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003648 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003649 data->rpc_status = 0;
3650
Trond Myklebustc970aa82007-07-14 15:39:59 -04003651 task_setup_data.callback_data = data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003652 msg.rpc_argp = &data->args,
3653 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003654 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003655 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003656 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003657 if (!issync)
3658 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003659 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003660 if (status != 0)
3661 goto out;
3662 status = data->rpc_status;
3663 if (status != 0)
3664 goto out;
3665 nfs_refresh_inode(inode, &data->fattr);
3666out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003667 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003668 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669}
3670
Trond Myklebuste6f81072008-01-24 18:14:34 -05003671int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672{
3673 struct nfs_server *server = NFS_SERVER(inode);
3674 struct nfs4_exception exception = { };
3675 int err;
3676 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003677 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 switch (err) {
3679 case -NFS4ERR_STALE_STATEID:
3680 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 case 0:
3682 return 0;
3683 }
3684 err = nfs4_handle_exception(server, err, &exception);
3685 } while (exception.retry);
3686 return err;
3687}
3688
3689#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3690#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3691
3692/*
3693 * sleep, with exponential backoff, and retry the LOCK operation.
3694 */
3695static unsigned long
3696nfs4_set_lock_task_retry(unsigned long timeout)
3697{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003698 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 timeout <<= 1;
3700 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3701 return NFS4_LOCK_MAXTIMEOUT;
3702 return timeout;
3703}
3704
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3706{
3707 struct inode *inode = state->inode;
3708 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003709 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003710 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003712 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003714 struct nfs_lockt_res res = {
3715 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 };
3717 struct rpc_message msg = {
3718 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3719 .rpc_argp = &arg,
3720 .rpc_resp = &res,
3721 .rpc_cred = state->owner->so_cred,
3722 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 struct nfs4_lock_state *lsp;
3724 int status;
3725
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003726 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003727 status = nfs4_set_lock_state(state, request);
3728 if (status != 0)
3729 goto out;
3730 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003731 arg.lock_owner.id = lsp->ls_id.id;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003732 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003733 switch (status) {
3734 case 0:
3735 request->fl_type = F_UNLCK;
3736 break;
3737 case -NFS4ERR_DENIED:
3738 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003740 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003741out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 return status;
3743}
3744
3745static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3746{
3747 struct nfs4_exception exception = { };
3748 int err;
3749
3750 do {
3751 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3752 _nfs4_proc_getlk(state, cmd, request),
3753 &exception);
3754 } while (exception.retry);
3755 return err;
3756}
3757
3758static int do_vfs_lock(struct file *file, struct file_lock *fl)
3759{
3760 int res = 0;
3761 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3762 case FL_POSIX:
3763 res = posix_lock_file_wait(file, fl);
3764 break;
3765 case FL_FLOCK:
3766 res = flock_lock_file_wait(file, fl);
3767 break;
3768 default:
3769 BUG();
3770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 return res;
3772}
3773
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003774struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003775 struct nfs_locku_args arg;
3776 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003777 struct nfs4_lock_state *lsp;
3778 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003779 struct file_lock fl;
3780 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003781 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003782};
3783
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003784static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3785 struct nfs_open_context *ctx,
3786 struct nfs4_lock_state *lsp,
3787 struct nfs_seqid *seqid)
3788{
3789 struct nfs4_unlockdata *p;
3790 struct inode *inode = lsp->ls_state->inode;
3791
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003792 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003793 if (p == NULL)
3794 return NULL;
3795 p->arg.fh = NFS_FH(inode);
3796 p->arg.fl = &p->fl;
3797 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003798 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003799 p->arg.stateid = &lsp->ls_stateid;
3800 p->lsp = lsp;
3801 atomic_inc(&lsp->ls_count);
3802 /* Ensure we don't close file until we're done freeing locks! */
3803 p->ctx = get_nfs_open_context(ctx);
3804 memcpy(&p->fl, fl, sizeof(p->fl));
3805 p->server = NFS_SERVER(inode);
3806 return p;
3807}
3808
Trond Myklebust06f814a2006-01-03 09:55:07 +01003809static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003810{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003811 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003812 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003813 nfs4_put_lock_state(calldata->lsp);
3814 put_nfs_open_context(calldata->ctx);
3815 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003816}
3817
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003818static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003819{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003820 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003821
Trond Myklebust14516c32010-07-31 14:29:06 -04003822 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3823 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003824 switch (task->tk_status) {
3825 case 0:
3826 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003827 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003828 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003829 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003830 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003831 case -NFS4ERR_BAD_STATEID:
3832 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003833 case -NFS4ERR_STALE_STATEID:
3834 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003835 break;
3836 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003837 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebust0110ee12009-12-07 09:00:24 -05003838 nfs_restart_rpc(task,
Trond Myklebustd61e6122009-12-05 19:32:19 -05003839 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003840 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003841}
3842
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003843static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003844{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003845 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003847 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003848 return;
3849 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003850 /* Note: exit _without_ running nfs4_locku_done */
3851 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003852 return;
3853 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003854 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04003855 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04003856 &calldata->arg.seq_args,
3857 &calldata->res.seq_res, 1, task))
3858 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003859 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860}
3861
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003862static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003863 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003864 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003865 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003866};
3867
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003868static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3869 struct nfs_open_context *ctx,
3870 struct nfs4_lock_state *lsp,
3871 struct nfs_seqid *seqid)
3872{
3873 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003874 struct rpc_message msg = {
3875 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3876 .rpc_cred = ctx->cred,
3877 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003878 struct rpc_task_setup task_setup_data = {
3879 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003880 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003881 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003882 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003883 .flags = RPC_TASK_ASYNC,
3884 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003885
Frank Filz137d6ac2007-07-09 15:32:29 -07003886 /* Ensure this is an unlock - when canceling a lock, the
3887 * canceled lock is passed in, and it won't be an unlock.
3888 */
3889 fl->fl_type = F_UNLCK;
3890
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003891 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3892 if (data == NULL) {
3893 nfs_free_seqid(seqid);
3894 return ERR_PTR(-ENOMEM);
3895 }
3896
Trond Myklebust5138fde2007-07-14 15:40:01 -04003897 msg.rpc_argp = &data->arg,
3898 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003899 task_setup_data.callback_data = data;
3900 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003901}
3902
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3904{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003905 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003906 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003907 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003908 struct rpc_task *task;
3909 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003910 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911
Trond Myklebust9b073572006-06-29 16:38:34 -04003912 status = nfs4_set_lock_state(state, request);
3913 /* Unlock _before_ we do the RPC call */
3914 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003915 down_read(&nfsi->rwsem);
3916 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3917 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003918 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003919 }
3920 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003921 if (status != 0)
3922 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003923 /* Is this a delegated lock? */
3924 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04003925 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003926 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003927 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04003928 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003929 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04003930 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003931 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003932 status = PTR_ERR(task);
3933 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04003934 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003935 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003936 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04003937out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003938 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003939 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940}
3941
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003942struct nfs4_lockdata {
3943 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003944 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003945 struct nfs4_lock_state *lsp;
3946 struct nfs_open_context *ctx;
3947 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003948 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003949 int rpc_status;
3950 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04003951 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003952};
3953
3954static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003955 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
3956 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003957{
3958 struct nfs4_lockdata *p;
3959 struct inode *inode = lsp->ls_state->inode;
3960 struct nfs_server *server = NFS_SERVER(inode);
3961
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003962 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003963 if (p == NULL)
3964 return NULL;
3965
3966 p->arg.fh = NFS_FH(inode);
3967 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003968 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003969 if (p->arg.open_seqid == NULL)
3970 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003971 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003972 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003973 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003974 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04003975 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003976 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003977 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003978 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04003979 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003980 atomic_inc(&lsp->ls_count);
3981 p->ctx = get_nfs_open_context(ctx);
3982 memcpy(&p->fl, fl, sizeof(p->fl));
3983 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003984out_free_seqid:
3985 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003986out_free:
3987 kfree(p);
3988 return NULL;
3989}
3990
3991static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3992{
3993 struct nfs4_lockdata *data = calldata;
3994 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995
Harvey Harrison3110ff82008-05-02 13:42:44 -07003996 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003997 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3998 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003999 /* Do we need to do an open_to_lock_owner? */
4000 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004001 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4002 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004003 data->arg.open_stateid = &state->stateid;
4004 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004005 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004006 } else
4007 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004008 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004009 if (nfs4_setup_sequence(data->server,
4010 &data->arg.seq_args,
Andy Adamson66179ef2009-04-01 09:22:22 -04004011 &data->res.seq_res, 1, task))
4012 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004013 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004014 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004015}
4016
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004017static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4018{
4019 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4020 nfs4_lock_prepare(task, calldata);
4021}
4022
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004023static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4024{
4025 struct nfs4_lockdata *data = calldata;
4026
Harvey Harrison3110ff82008-05-02 13:42:44 -07004027 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004028
Trond Myklebust14516c32010-07-31 14:29:06 -04004029 if (!nfs4_sequence_done(task, &data->res.seq_res))
4030 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004031
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004032 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004033 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004034 if (data->rpc_status == 0)
4035 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4036 else
4037 goto out;
4038 }
4039 if (data->rpc_status == 0) {
4040 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4041 sizeof(data->lsp->ls_stateid.data));
4042 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04004043 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004044 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004045out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004046 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004047}
4048
4049static void nfs4_lock_release(void *calldata)
4050{
4051 struct nfs4_lockdata *data = calldata;
4052
Harvey Harrison3110ff82008-05-02 13:42:44 -07004053 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004054 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004055 if (data->cancelled != 0) {
4056 struct rpc_task *task;
4057 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4058 data->arg.lock_seqid);
4059 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004060 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004061 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004062 } else
4063 nfs_free_seqid(data->arg.lock_seqid);
4064 nfs4_put_lock_state(data->lsp);
4065 put_nfs_open_context(data->ctx);
4066 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004067 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004068}
4069
4070static const struct rpc_call_ops nfs4_lock_ops = {
4071 .rpc_call_prepare = nfs4_lock_prepare,
4072 .rpc_call_done = nfs4_lock_done,
4073 .rpc_release = nfs4_lock_release,
4074};
4075
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004076static const struct rpc_call_ops nfs4_recover_lock_ops = {
4077 .rpc_call_prepare = nfs4_recover_lock_prepare,
4078 .rpc_call_done = nfs4_lock_done,
4079 .rpc_release = nfs4_lock_release,
4080};
4081
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004082static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4083{
4084 struct nfs_client *clp = server->nfs_client;
4085 struct nfs4_state *state = lsp->ls_state;
4086
4087 switch (error) {
4088 case -NFS4ERR_ADMIN_REVOKED:
4089 case -NFS4ERR_BAD_STATEID:
4090 case -NFS4ERR_EXPIRED:
4091 if (new_lock_owner != 0 ||
4092 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4093 nfs4_state_mark_reclaim_nograce(clp, state);
4094 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004095 break;
4096 case -NFS4ERR_STALE_STATEID:
4097 if (new_lock_owner != 0 ||
4098 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4099 nfs4_state_mark_reclaim_reboot(clp, state);
4100 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004101 };
4102}
4103
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004104static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004105{
4106 struct nfs4_lockdata *data;
4107 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004108 struct rpc_message msg = {
4109 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4110 .rpc_cred = state->owner->so_cred,
4111 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004112 struct rpc_task_setup task_setup_data = {
4113 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004114 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004115 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004116 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004117 .flags = RPC_TASK_ASYNC,
4118 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004119 int ret;
4120
Harvey Harrison3110ff82008-05-02 13:42:44 -07004121 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004122 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004123 fl->fl_u.nfs4_fl.owner,
4124 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004125 if (data == NULL)
4126 return -ENOMEM;
4127 if (IS_SETLKW(cmd))
4128 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004129 if (recovery_type > NFS_LOCK_NEW) {
4130 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004131 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004132 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4133 }
Trond Myklebust5138fde2007-07-14 15:40:01 -04004134 msg.rpc_argp = &data->arg,
4135 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004136 task_setup_data.callback_data = data;
4137 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004138 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004139 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004140 ret = nfs4_wait_for_completion_rpc_task(task);
4141 if (ret == 0) {
4142 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004143 if (ret)
4144 nfs4_handle_setlk_error(data->server, data->lsp,
4145 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004146 } else
4147 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004148 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004149 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004150 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151}
4152
4153static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4154{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004155 struct nfs_server *server = NFS_SERVER(state->inode);
4156 struct nfs4_exception exception = { };
4157 int err;
4158
4159 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004160 /* Cache the lock if possible... */
4161 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4162 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004163 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004164 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004165 break;
4166 nfs4_handle_exception(server, err, &exception);
4167 } while (exception.retry);
4168 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169}
4170
4171static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4172{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004173 struct nfs_server *server = NFS_SERVER(state->inode);
4174 struct nfs4_exception exception = { };
4175 int err;
4176
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004177 err = nfs4_set_lock_state(state, request);
4178 if (err != 0)
4179 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004180 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004181 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4182 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004183 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004184 switch (err) {
4185 default:
4186 goto out;
4187 case -NFS4ERR_GRACE:
4188 case -NFS4ERR_DELAY:
4189 nfs4_handle_exception(server, err, &exception);
4190 err = 0;
4191 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004192 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004193out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004194 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195}
4196
4197static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4198{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004199 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004200 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004201 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004203 if ((fl_flags & FL_POSIX) &&
4204 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4205 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004206 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004207 status = nfs4_set_lock_state(state, request);
4208 if (status != 0)
4209 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004210 request->fl_flags |= FL_ACCESS;
4211 status = do_vfs_lock(request->fl_file, request);
4212 if (status < 0)
4213 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004214 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004215 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004216 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004217 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004218 request->fl_flags = fl_flags & ~FL_SLEEP;
4219 status = do_vfs_lock(request->fl_file, request);
4220 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004221 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004222 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004223 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004224 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004225 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004226 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004227 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004228 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004229out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004230 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004231out:
4232 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 return status;
4234}
4235
4236static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4237{
4238 struct nfs4_exception exception = { };
4239 int err;
4240
4241 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004242 err = _nfs4_proc_setlk(state, cmd, request);
4243 if (err == -NFS4ERR_DENIED)
4244 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004246 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 } while (exception.retry);
4248 return err;
4249}
4250
4251static int
4252nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4253{
4254 struct nfs_open_context *ctx;
4255 struct nfs4_state *state;
4256 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4257 int status;
4258
4259 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004260 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 state = ctx->state;
4262
4263 if (request->fl_start < 0 || request->fl_end < 0)
4264 return -EINVAL;
4265
Trond Myklebustd9531262009-07-21 19:22:38 -04004266 if (IS_GETLK(cmd)) {
4267 if (state != NULL)
4268 return nfs4_proc_getlk(state, F_GETLK, request);
4269 return 0;
4270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271
4272 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4273 return -EINVAL;
4274
Trond Myklebustd9531262009-07-21 19:22:38 -04004275 if (request->fl_type == F_UNLCK) {
4276 if (state != NULL)
4277 return nfs4_proc_unlck(state, cmd, request);
4278 return 0;
4279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280
Trond Myklebustd9531262009-07-21 19:22:38 -04004281 if (state == NULL)
4282 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 do {
4284 status = nfs4_proc_setlk(state, cmd, request);
4285 if ((status != -EAGAIN) || IS_SETLK(cmd))
4286 break;
4287 timeout = nfs4_set_lock_task_retry(timeout);
4288 status = -ERESTARTSYS;
4289 if (signalled())
4290 break;
4291 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 return status;
4293}
4294
Trond Myklebust888e6942005-11-04 15:38:11 -05004295int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4296{
4297 struct nfs_server *server = NFS_SERVER(state->inode);
4298 struct nfs4_exception exception = { };
4299 int err;
4300
4301 err = nfs4_set_lock_state(state, fl);
4302 if (err != 0)
4303 goto out;
4304 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004305 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004306 switch (err) {
4307 default:
4308 printk(KERN_ERR "%s: unhandled error %d.\n",
4309 __func__, err);
4310 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004311 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004312 goto out;
4313 case -NFS4ERR_EXPIRED:
4314 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004315 case -NFS4ERR_STALE_STATEID:
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004316 case -NFS4ERR_BADSESSION:
4317 case -NFS4ERR_BADSLOT:
4318 case -NFS4ERR_BAD_HIGH_SLOT:
4319 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4320 case -NFS4ERR_DEADSESSION:
Trond Myklebustd5122202009-06-17 13:22:58 -07004321 nfs4_schedule_state_recovery(server->nfs_client);
4322 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004323 case -ERESTARTSYS:
4324 /*
4325 * The show must go on: exit, but mark the
4326 * stateid as needing recovery.
4327 */
4328 case -NFS4ERR_ADMIN_REVOKED:
4329 case -NFS4ERR_BAD_STATEID:
4330 case -NFS4ERR_OPENMODE:
4331 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4332 err = 0;
4333 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004334 case -EKEYEXPIRED:
4335 /*
4336 * User RPCSEC_GSS context has expired.
4337 * We cannot recover this stateid now, so
4338 * skip it and allow recovery thread to
4339 * proceed.
4340 */
4341 err = 0;
4342 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004343 case -ENOMEM:
4344 case -NFS4ERR_DENIED:
4345 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4346 err = 0;
4347 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004348 case -NFS4ERR_DELAY:
4349 break;
4350 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004351 err = nfs4_handle_exception(server, err, &exception);
4352 } while (exception.retry);
4353out:
4354 return err;
4355}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004356
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004357static void nfs4_release_lockowner_release(void *calldata)
4358{
4359 kfree(calldata);
4360}
4361
4362const struct rpc_call_ops nfs4_release_lockowner_ops = {
4363 .rpc_release = nfs4_release_lockowner_release,
4364};
4365
4366void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4367{
4368 struct nfs_server *server = lsp->ls_state->owner->so_server;
4369 struct nfs_release_lockowner_args *args;
4370 struct rpc_message msg = {
4371 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4372 };
4373
4374 if (server->nfs_client->cl_mvops->minor_version != 0)
4375 return;
4376 args = kmalloc(sizeof(*args), GFP_NOFS);
4377 if (!args)
4378 return;
4379 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4380 args->lock_owner.id = lsp->ls_id.id;
4381 msg.rpc_argp = args;
4382 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4383}
4384
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004385#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4386
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004387int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4388 size_t buflen, int flags)
4389{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004390 struct inode *inode = dentry->d_inode;
4391
4392 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4393 return -EOPNOTSUPP;
4394
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004395 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004396}
4397
4398/* The getxattr man page suggests returning -ENODATA for unknown attributes,
4399 * and that's what we'll do for e.g. user attributes that haven't been set.
4400 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4401 * attributes in kernel-managed attribute namespaces. */
4402ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4403 size_t buflen)
4404{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004405 struct inode *inode = dentry->d_inode;
4406
4407 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4408 return -EOPNOTSUPP;
4409
4410 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004411}
4412
4413ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4414{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004415 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004416
J. Bruce Fields096455a2006-03-20 23:23:42 -05004417 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4418 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004419 if (buf && buflen < len)
4420 return -ERANGE;
4421 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004422 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4423 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004424}
4425
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004426static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4427{
4428 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4429 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4430 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4431 return;
4432
4433 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4434 NFS_ATTR_FATTR_NLINK;
4435 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4436 fattr->nlink = 2;
4437}
4438
Trond Myklebust56659e92007-07-17 21:52:39 -04004439int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004440 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004441{
4442 struct nfs_server *server = NFS_SERVER(dir);
4443 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004444 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4445 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004446 };
4447 struct nfs4_fs_locations_arg args = {
4448 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004449 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004450 .page = page,
4451 .bitmask = bitmask,
4452 };
Benny Halevy22958462009-04-01 09:22:02 -04004453 struct nfs4_fs_locations_res res = {
4454 .fs_locations = fs_locations,
4455 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004456 struct rpc_message msg = {
4457 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4458 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004459 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004460 };
4461 int status;
4462
Harvey Harrison3110ff82008-05-02 13:42:44 -07004463 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004464 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004465 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004466 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04004467 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004468 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004469 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004470 return status;
4471}
4472
Andy Adamson557134a2009-04-01 09:21:53 -04004473#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004474/*
4475 * nfs4_proc_exchange_id()
4476 *
4477 * Since the clientid has expired, all compounds using sessions
4478 * associated with the stale clientid will be returning
4479 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4480 * be in some phase of session reset.
4481 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004482int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004483{
4484 nfs4_verifier verifier;
4485 struct nfs41_exchange_id_args args = {
4486 .client = clp,
4487 .flags = clp->cl_exchange_flags,
4488 };
4489 struct nfs41_exchange_id_res res = {
4490 .client = clp,
4491 };
4492 int status;
4493 struct rpc_message msg = {
4494 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4495 .rpc_argp = &args,
4496 .rpc_resp = &res,
4497 .rpc_cred = cred,
4498 };
4499 __be32 *p;
4500
4501 dprintk("--> %s\n", __func__);
4502 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004503
Alexandros Batsakis7b183d02009-12-05 13:33:25 -05004504 /* Remove server-only flags */
4505 args.flags &= ~EXCHGID4_FLAG_CONFIRMED_R;
4506
Benny Halevy99fe60d2009-04-01 09:22:29 -04004507 p = (u32 *)verifier.data;
4508 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4509 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4510 args.verifier = &verifier;
4511
4512 while (1) {
4513 args.id_len = scnprintf(args.id, sizeof(args.id),
4514 "%s/%s %u",
4515 clp->cl_ipaddr,
4516 rpc_peeraddr2str(clp->cl_rpcclient,
4517 RPC_DISPLAY_ADDR),
4518 clp->cl_id_uniquifier);
4519
4520 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4521
Andy Adamson180b62a2010-03-02 13:19:36 -05004522 if (status != -NFS4ERR_CLID_INUSE)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004523 break;
4524
4525 if (signalled())
4526 break;
4527
4528 if (++clp->cl_id_uniquifier == 0)
4529 break;
4530 }
4531
4532 dprintk("<-- %s status= %d\n", __func__, status);
4533 return status;
4534}
4535
Andy Adamson2050f0c2009-04-01 09:22:30 -04004536struct nfs4_get_lease_time_data {
4537 struct nfs4_get_lease_time_args *args;
4538 struct nfs4_get_lease_time_res *res;
4539 struct nfs_client *clp;
4540};
4541
4542static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4543 void *calldata)
4544{
4545 int ret;
4546 struct nfs4_get_lease_time_data *data =
4547 (struct nfs4_get_lease_time_data *)calldata;
4548
4549 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004550 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004551 /* just setup sequence, do not trigger session recovery
4552 since we're invoked within one */
4553 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004554 &data->args->la_seq_args,
4555 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004556
4557 BUG_ON(ret == -EAGAIN);
4558 rpc_call_start(task);
4559 dprintk("<-- %s\n", __func__);
4560}
4561
4562/*
4563 * Called from nfs4_state_manager thread for session setup, so don't recover
4564 * from sequence operation or clientid errors.
4565 */
4566static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4567{
4568 struct nfs4_get_lease_time_data *data =
4569 (struct nfs4_get_lease_time_data *)calldata;
4570
4571 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04004572 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4573 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04004574 switch (task->tk_status) {
4575 case -NFS4ERR_DELAY:
4576 case -NFS4ERR_GRACE:
4577 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4578 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4579 task->tk_status = 0;
Trond Myklebust0110ee12009-12-07 09:00:24 -05004580 nfs_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004581 return;
4582 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004583 dprintk("<-- %s\n", __func__);
4584}
4585
4586struct rpc_call_ops nfs4_get_lease_time_ops = {
4587 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4588 .rpc_call_done = nfs4_get_lease_time_done,
4589};
4590
4591int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4592{
4593 struct rpc_task *task;
4594 struct nfs4_get_lease_time_args args;
4595 struct nfs4_get_lease_time_res res = {
4596 .lr_fsinfo = fsinfo,
4597 };
4598 struct nfs4_get_lease_time_data data = {
4599 .args = &args,
4600 .res = &res,
4601 .clp = clp,
4602 };
4603 struct rpc_message msg = {
4604 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4605 .rpc_argp = &args,
4606 .rpc_resp = &res,
4607 };
4608 struct rpc_task_setup task_setup = {
4609 .rpc_client = clp->cl_rpcclient,
4610 .rpc_message = &msg,
4611 .callback_ops = &nfs4_get_lease_time_ops,
4612 .callback_data = &data
4613 };
4614 int status;
4615
Andy Adamson2050f0c2009-04-01 09:22:30 -04004616 dprintk("--> %s\n", __func__);
4617 task = rpc_run_task(&task_setup);
4618
4619 if (IS_ERR(task))
4620 status = PTR_ERR(task);
4621 else {
4622 status = task->tk_status;
4623 rpc_put_task(task);
4624 }
4625 dprintk("<-- %s return %d\n", __func__, status);
4626
4627 return status;
4628}
4629
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004630/*
4631 * Reset a slot table
4632 */
Andy Adamson104aeba2010-01-14 17:45:10 -05004633static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4634 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004635{
Andy Adamson104aeba2010-01-14 17:45:10 -05004636 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004637 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004638 int ret = 0;
4639
Andy Adamson104aeba2010-01-14 17:45:10 -05004640 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4641 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004642
Andy Adamson104aeba2010-01-14 17:45:10 -05004643 /* Does the newly negotiated max_reqs match the existing slot table? */
4644 if (max_reqs != tbl->max_slots) {
4645 ret = -ENOMEM;
4646 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004647 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05004648 if (!new)
4649 goto out;
4650 ret = 0;
4651 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004652 }
4653 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05004654 if (new) {
4655 tbl->slots = new;
4656 tbl->max_slots = max_reqs;
4657 }
4658 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004659 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004660 spin_unlock(&tbl->slot_tbl_lock);
4661 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4662 tbl, tbl->slots, tbl->max_slots);
4663out:
4664 dprintk("<-- %s: return %d\n", __func__, ret);
4665 return ret;
4666}
4667
Andy Adamsonfc931582009-04-01 09:22:31 -04004668/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004669 * Reset the forechannel and backchannel slot tables
4670 */
4671static int nfs4_reset_slot_tables(struct nfs4_session *session)
4672{
4673 int status;
4674
4675 status = nfs4_reset_slot_table(&session->fc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004676 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004677 if (status)
4678 return status;
4679
4680 status = nfs4_reset_slot_table(&session->bc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004681 session->bc_attrs.max_reqs, 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004682 return status;
4683}
4684
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004685/* Destroy the slot table */
4686static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4687{
4688 if (session->fc_slot_table.slots != NULL) {
4689 kfree(session->fc_slot_table.slots);
4690 session->fc_slot_table.slots = NULL;
4691 }
4692 if (session->bc_slot_table.slots != NULL) {
4693 kfree(session->bc_slot_table.slots);
4694 session->bc_slot_table.slots = NULL;
4695 }
4696 return;
4697}
4698
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004699/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004700 * Initialize slot table
4701 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004702static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4703 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04004704{
Andy Adamsonfc931582009-04-01 09:22:31 -04004705 struct nfs4_slot *slot;
4706 int ret = -ENOMEM;
4707
4708 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4709
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004710 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04004711
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004712 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04004713 if (!slot)
4714 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04004715 ret = 0;
4716
4717 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04004718 tbl->max_slots = max_slots;
4719 tbl->slots = slot;
4720 tbl->highest_used_slotid = -1; /* no slot is currently used */
4721 spin_unlock(&tbl->slot_tbl_lock);
4722 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4723 tbl, tbl->slots, tbl->max_slots);
4724out:
4725 dprintk("<-- %s: return %d\n", __func__, ret);
4726 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04004727}
4728
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004729/*
4730 * Initialize the forechannel and backchannel tables
4731 */
4732static int nfs4_init_slot_tables(struct nfs4_session *session)
4733{
Trond Myklebustf26468f2009-12-05 19:32:11 -05004734 struct nfs4_slot_table *tbl;
4735 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004736
Trond Myklebustf26468f2009-12-05 19:32:11 -05004737 tbl = &session->fc_slot_table;
4738 if (tbl->slots == NULL) {
4739 status = nfs4_init_slot_table(tbl,
4740 session->fc_attrs.max_reqs, 1);
4741 if (status)
4742 return status;
4743 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004744
Trond Myklebustf26468f2009-12-05 19:32:11 -05004745 tbl = &session->bc_slot_table;
4746 if (tbl->slots == NULL) {
4747 status = nfs4_init_slot_table(tbl,
4748 session->bc_attrs.max_reqs, 0);
4749 if (status)
4750 nfs4_destroy_slot_tables(session);
4751 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004752
4753 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04004754}
4755
4756struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4757{
4758 struct nfs4_session *session;
4759 struct nfs4_slot_table *tbl;
4760
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004761 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04004762 if (!session)
4763 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04004764
Andy Adamsonea028ac2009-12-04 15:55:38 -05004765 init_completion(&session->complete);
Andy Adamson76db6d92009-04-01 09:22:38 -04004766
Andy Adamson557134a2009-04-01 09:21:53 -04004767 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004768 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04004769 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004770 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004771
4772 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004773 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004774 spin_lock_init(&tbl->slot_tbl_lock);
4775 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4776
Trond Myklebust1055d762010-06-16 09:52:27 -04004777 session->session_state = 1<<NFS4_SESSION_INITING;
4778
Andy Adamson557134a2009-04-01 09:21:53 -04004779 session->clp = clp;
4780 return session;
4781}
4782
4783void nfs4_destroy_session(struct nfs4_session *session)
4784{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04004785 nfs4_proc_destroy_session(session);
4786 dprintk("%s Destroy backchannel for xprt %p\n",
4787 __func__, session->clp->cl_rpcclient->cl_xprt);
4788 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4789 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004790 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04004791 kfree(session);
4792}
4793
Andy Adamsonfc931582009-04-01 09:22:31 -04004794/*
4795 * Initialize the values to be used by the client in CREATE_SESSION
4796 * If nfs4_init_session set the fore channel request and response sizes,
4797 * use them.
4798 *
4799 * Set the back channel max_resp_sz_cached to zero to force the client to
4800 * always set csa_cachethis to FALSE because the current implementation
4801 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4802 */
4803static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4804{
4805 struct nfs4_session *session = args->client->cl_session;
4806 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4807 mxresp_sz = session->fc_attrs.max_resp_sz;
4808
4809 if (mxrqst_sz == 0)
4810 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4811 if (mxresp_sz == 0)
4812 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4813 /* Fore channel attributes */
4814 args->fc_attrs.headerpadsz = 0;
4815 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4816 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04004817 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4818 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4819
4820 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05004821 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04004822 __func__,
4823 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05004824 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04004825
4826 /* Back channel attributes */
4827 args->bc_attrs.headerpadsz = 0;
4828 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4829 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4830 args->bc_attrs.max_resp_sz_cached = 0;
4831 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4832 args->bc_attrs.max_reqs = 1;
4833
4834 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4835 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4836 __func__,
4837 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4838 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4839 args->bc_attrs.max_reqs);
4840}
4841
Andy Adamson8d353012009-04-01 09:22:32 -04004842static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
4843{
4844 if (rcvd <= sent)
4845 return 0;
4846 printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
4847 "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
4848 return -EINVAL;
4849}
4850
4851#define _verify_fore_channel_attr(_name_) \
4852 _verify_channel_attr("fore", #_name_, \
4853 args->fc_attrs._name_, \
4854 session->fc_attrs._name_)
4855
4856#define _verify_back_channel_attr(_name_) \
4857 _verify_channel_attr("back", #_name_, \
4858 args->bc_attrs._name_, \
4859 session->bc_attrs._name_)
4860
4861/*
4862 * The server is not allowed to increase the fore channel header pad size,
4863 * maximum response size, or maximum number of operations.
4864 *
4865 * The back channel attributes are only negotiatied down: We send what the
4866 * (back channel) server insists upon.
4867 */
4868static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4869 struct nfs4_session *session)
4870{
4871 int ret = 0;
4872
4873 ret |= _verify_fore_channel_attr(headerpadsz);
4874 ret |= _verify_fore_channel_attr(max_resp_sz);
4875 ret |= _verify_fore_channel_attr(max_ops);
4876
4877 ret |= _verify_back_channel_attr(headerpadsz);
4878 ret |= _verify_back_channel_attr(max_rqst_sz);
4879 ret |= _verify_back_channel_attr(max_resp_sz);
4880 ret |= _verify_back_channel_attr(max_resp_sz_cached);
4881 ret |= _verify_back_channel_attr(max_ops);
4882 ret |= _verify_back_channel_attr(max_reqs);
4883
4884 return ret;
4885}
4886
Andy Adamsonfc931582009-04-01 09:22:31 -04004887static int _nfs4_proc_create_session(struct nfs_client *clp)
4888{
4889 struct nfs4_session *session = clp->cl_session;
4890 struct nfs41_create_session_args args = {
4891 .client = clp,
4892 .cb_program = NFS4_CALLBACK,
4893 };
4894 struct nfs41_create_session_res res = {
4895 .client = clp,
4896 };
4897 struct rpc_message msg = {
4898 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4899 .rpc_argp = &args,
4900 .rpc_resp = &res,
4901 };
4902 int status;
4903
4904 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04004905 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04004906
4907 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4908
Andy Adamson8d353012009-04-01 09:22:32 -04004909 if (!status)
4910 /* Verify the session's negotiated channel_attrs values */
4911 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04004912 if (!status) {
4913 /* Increment the clientid slot sequence id */
4914 clp->cl_seqid++;
4915 }
4916
4917 return status;
4918}
4919
4920/*
4921 * Issues a CREATE_SESSION operation to the server.
4922 * It is the responsibility of the caller to verify the session is
4923 * expired before calling this routine.
4924 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05004925int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04004926{
4927 int status;
4928 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04004929 struct nfs4_session *session = clp->cl_session;
4930
4931 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
4932
4933 status = _nfs4_proc_create_session(clp);
4934 if (status)
4935 goto out;
4936
Trond Myklebustf26468f2009-12-05 19:32:11 -05004937 /* Init and reset the fore channel */
4938 status = nfs4_init_slot_tables(session);
4939 dprintk("slot table initialization returned %d\n", status);
4940 if (status)
4941 goto out;
4942 status = nfs4_reset_slot_tables(session);
4943 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04004944 if (status)
4945 goto out;
4946
4947 ptr = (unsigned *)&session->sess_id.data[0];
4948 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
4949 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04004950out:
4951 dprintk("<-- %s\n", __func__);
4952 return status;
4953}
4954
Andy Adamson0f3e66c2009-04-01 09:22:34 -04004955/*
4956 * Issue the over-the-wire RPC DESTROY_SESSION.
4957 * The caller must serialize access to this routine.
4958 */
4959int nfs4_proc_destroy_session(struct nfs4_session *session)
4960{
4961 int status = 0;
4962 struct rpc_message msg;
4963
4964 dprintk("--> nfs4_proc_destroy_session\n");
4965
4966 /* session is still being setup */
4967 if (session->clp->cl_cons_state != NFS_CS_READY)
4968 return status;
4969
4970 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
4971 msg.rpc_argp = session;
4972 msg.rpc_resp = NULL;
4973 msg.rpc_cred = NULL;
4974 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4975
4976 if (status)
4977 printk(KERN_WARNING
4978 "Got error %d from the server on DESTROY_SESSION. "
4979 "Session has been destroyed regardless...\n", status);
4980
4981 dprintk("<-- nfs4_proc_destroy_session\n");
4982 return status;
4983}
4984
Trond Myklebustfccba802009-07-21 16:48:07 -04004985int nfs4_init_session(struct nfs_server *server)
4986{
4987 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05004988 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05004989 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04004990 int ret;
4991
4992 if (!nfs4_has_session(clp))
4993 return 0;
4994
Trond Myklebust1055d762010-06-16 09:52:27 -04004995 session = clp->cl_session;
4996 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
4997 return 0;
4998
Andy Adamson68bf05e2009-12-15 12:55:02 -05004999 rsize = server->rsize;
5000 if (rsize == 0)
5001 rsize = NFS_MAX_FILE_IO_SIZE;
5002 wsize = server->wsize;
5003 if (wsize == 0)
5004 wsize = NFS_MAX_FILE_IO_SIZE;
5005
Andy Adamson68bf05e2009-12-15 12:55:02 -05005006 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5007 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005008
Trond Myklebustfccba802009-07-21 16:48:07 -04005009 ret = nfs4_recover_expired_lease(server);
5010 if (!ret)
5011 ret = nfs4_check_client_ready(clp);
5012 return ret;
5013}
5014
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005015/*
5016 * Renew the cl_session lease.
5017 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005018struct nfs4_sequence_data {
5019 struct nfs_client *clp;
5020 struct nfs4_sequence_args args;
5021 struct nfs4_sequence_res res;
5022};
5023
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005024static void nfs41_sequence_release(void *data)
5025{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005026 struct nfs4_sequence_data *calldata = data;
5027 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005028
Alexandros Batsakis71358402010-02-05 03:45:05 -08005029 if (atomic_read(&clp->cl_count) > 1)
5030 nfs4_schedule_state_renewal(clp);
5031 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005032 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005033}
5034
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005035static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5036{
5037 switch(task->tk_status) {
5038 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005039 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5040 return -EAGAIN;
5041 default:
5042 nfs4_schedule_state_recovery(clp);
5043 }
5044 return 0;
5045}
5046
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005047static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005048{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005049 struct nfs4_sequence_data *calldata = data;
5050 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005051
Trond Myklebust14516c32010-07-31 14:29:06 -04005052 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5053 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005054
5055 if (task->tk_status < 0) {
5056 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005057 if (atomic_read(&clp->cl_count) == 1)
5058 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005059
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005060 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5061 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005062 return;
5063 }
5064 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005065 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005066out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005067 dprintk("<-- %s\n", __func__);
5068}
5069
5070static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5071{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005072 struct nfs4_sequence_data *calldata = data;
5073 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005074 struct nfs4_sequence_args *args;
5075 struct nfs4_sequence_res *res;
5076
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005077 args = task->tk_msg.rpc_argp;
5078 res = task->tk_msg.rpc_resp;
5079
Trond Myklebust035168a2010-06-16 09:52:26 -04005080 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005081 return;
5082 rpc_call_start(task);
5083}
5084
5085static const struct rpc_call_ops nfs41_sequence_ops = {
5086 .rpc_call_done = nfs41_sequence_call_done,
5087 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005088 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005089};
5090
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005091static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005092{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005093 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005094 struct rpc_message msg = {
5095 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5096 .rpc_cred = cred,
5097 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005098 struct rpc_task_setup task_setup_data = {
5099 .rpc_client = clp->cl_rpcclient,
5100 .rpc_message = &msg,
5101 .callback_ops = &nfs41_sequence_ops,
5102 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5103 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005104
Alexandros Batsakis71358402010-02-05 03:45:05 -08005105 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005106 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005107 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005108 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005109 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005110 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005111 }
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005112 msg.rpc_argp = &calldata->args;
5113 msg.rpc_resp = &calldata->res;
5114 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005115 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005116
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005117 return rpc_run_task(&task_setup_data);
5118}
5119
5120static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5121{
5122 struct rpc_task *task;
5123 int ret = 0;
5124
5125 task = _nfs41_proc_sequence(clp, cred);
5126 if (IS_ERR(task))
5127 ret = PTR_ERR(task);
5128 else
5129 rpc_put_task(task);
5130 dprintk("<-- %s status=%d\n", __func__, ret);
5131 return ret;
5132}
5133
5134static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5135{
5136 struct rpc_task *task;
5137 int ret;
5138
5139 task = _nfs41_proc_sequence(clp, cred);
5140 if (IS_ERR(task)) {
5141 ret = PTR_ERR(task);
5142 goto out;
5143 }
5144 ret = rpc_wait_for_completion_task(task);
5145 if (!ret)
5146 ret = task->tk_status;
5147 rpc_put_task(task);
5148out:
5149 dprintk("<-- %s status=%d\n", __func__, ret);
5150 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005151}
5152
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005153struct nfs4_reclaim_complete_data {
5154 struct nfs_client *clp;
5155 struct nfs41_reclaim_complete_args arg;
5156 struct nfs41_reclaim_complete_res res;
5157};
5158
5159static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5160{
5161 struct nfs4_reclaim_complete_data *calldata = data;
5162
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005163 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005164 if (nfs41_setup_sequence(calldata->clp->cl_session,
5165 &calldata->arg.seq_args,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005166 &calldata->res.seq_res, 0, task))
5167 return;
5168
5169 rpc_call_start(task);
5170}
5171
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005172static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5173{
5174 switch(task->tk_status) {
5175 case 0:
5176 case -NFS4ERR_COMPLETE_ALREADY:
5177 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5178 break;
5179 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005180 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5181 return -EAGAIN;
5182 default:
5183 nfs4_schedule_state_recovery(clp);
5184 }
5185 return 0;
5186}
5187
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005188static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5189{
5190 struct nfs4_reclaim_complete_data *calldata = data;
5191 struct nfs_client *clp = calldata->clp;
5192 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5193
5194 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005195 if (!nfs41_sequence_done(task, res))
5196 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005197
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005198 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5199 rpc_restart_call_prepare(task);
5200 return;
5201 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005202 dprintk("<-- %s\n", __func__);
5203}
5204
5205static void nfs4_free_reclaim_complete_data(void *data)
5206{
5207 struct nfs4_reclaim_complete_data *calldata = data;
5208
5209 kfree(calldata);
5210}
5211
5212static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5213 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5214 .rpc_call_done = nfs4_reclaim_complete_done,
5215 .rpc_release = nfs4_free_reclaim_complete_data,
5216};
5217
5218/*
5219 * Issue a global reclaim complete.
5220 */
5221static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5222{
5223 struct nfs4_reclaim_complete_data *calldata;
5224 struct rpc_task *task;
5225 struct rpc_message msg = {
5226 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5227 };
5228 struct rpc_task_setup task_setup_data = {
5229 .rpc_client = clp->cl_rpcclient,
5230 .rpc_message = &msg,
5231 .callback_ops = &nfs4_reclaim_complete_call_ops,
5232 .flags = RPC_TASK_ASYNC,
5233 };
5234 int status = -ENOMEM;
5235
5236 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005237 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005238 if (calldata == NULL)
5239 goto out;
5240 calldata->clp = clp;
5241 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005242
5243 msg.rpc_argp = &calldata->arg;
5244 msg.rpc_resp = &calldata->res;
5245 task_setup_data.callback_data = calldata;
5246 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005247 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005248 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005249 goto out;
5250 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005251 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005252 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005253out:
5254 dprintk("<-- %s status=%d\n", __func__, status);
5255 return status;
5256}
Andy Adamson557134a2009-04-01 09:21:53 -04005257#endif /* CONFIG_NFS_V4_1 */
5258
Andy Adamson591d71c2009-04-01 09:22:47 -04005259struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005260 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005261 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005262 .recover_open = nfs4_open_reclaim,
5263 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04005264 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005265 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266};
5267
Andy Adamson591d71c2009-04-01 09:22:47 -04005268#if defined(CONFIG_NFS_V4_1)
5269struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5270 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5271 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5272 .recover_open = nfs4_open_reclaim,
5273 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05005274 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005275 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005276 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04005277};
5278#endif /* CONFIG_NFS_V4_1 */
5279
5280struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005281 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005282 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283 .recover_open = nfs4_open_expired,
5284 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04005285 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005286 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287};
5288
Andy Adamson591d71c2009-04-01 09:22:47 -04005289#if defined(CONFIG_NFS_V4_1)
5290struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5291 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5292 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5293 .recover_open = nfs4_open_expired,
5294 .recover_lock = nfs4_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05005295 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005296 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04005297};
5298#endif /* CONFIG_NFS_V4_1 */
5299
Benny Halevy29fba382009-04-01 09:22:44 -04005300struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5301 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04005302 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005303 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04005304};
5305
5306#if defined(CONFIG_NFS_V4_1)
5307struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5308 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04005309 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005310 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04005311};
5312#endif
5313
Trond Myklebust97dc1352010-06-16 09:52:26 -04005314static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
5315 .minor_version = 0,
5316 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04005317 .validate_stateid = nfs4_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005318 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
5319 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
5320 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005321};
5322
5323#if defined(CONFIG_NFS_V4_1)
5324static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
5325 .minor_version = 1,
5326 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04005327 .validate_stateid = nfs41_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005328 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
5329 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
5330 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005331};
5332#endif
5333
Trond Myklebust97dc1352010-06-16 09:52:26 -04005334const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
5335 [0] = &nfs_v4_0_minor_ops,
5336#if defined(CONFIG_NFS_V4_1)
5337 [1] = &nfs_v4_1_minor_ops,
5338#endif
5339};
5340
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005341static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005342 .permission = nfs_permission,
5343 .getattr = nfs_getattr,
5344 .setattr = nfs_setattr,
5345 .getxattr = nfs4_getxattr,
5346 .setxattr = nfs4_setxattr,
5347 .listxattr = nfs4_listxattr,
5348};
5349
David Howells509de812006-08-22 20:06:11 -04005350const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351 .version = 4, /* protocol version */
5352 .dentry_ops = &nfs4_dentry_operations,
5353 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005354 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355 .getroot = nfs4_proc_get_root,
5356 .getattr = nfs4_proc_getattr,
5357 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04005358 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359 .lookup = nfs4_proc_lookup,
5360 .access = nfs4_proc_access,
5361 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362 .create = nfs4_proc_create,
5363 .remove = nfs4_proc_remove,
5364 .unlink_setup = nfs4_proc_unlink_setup,
5365 .unlink_done = nfs4_proc_unlink_done,
5366 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04005367 .rename_setup = nfs4_proc_rename_setup,
5368 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 .link = nfs4_proc_link,
5370 .symlink = nfs4_proc_symlink,
5371 .mkdir = nfs4_proc_mkdir,
5372 .rmdir = nfs4_proc_remove,
5373 .readdir = nfs4_proc_readdir,
5374 .mknod = nfs4_proc_mknod,
5375 .statfs = nfs4_proc_statfs,
5376 .fsinfo = nfs4_proc_fsinfo,
5377 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04005378 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 .decode_dirent = nfs4_decode_dirent,
5380 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05005381 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005383 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005385 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005387 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04005388 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04005389 .open_context = nfs4_atomic_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390};
5391
5392/*
5393 * Local variables:
5394 * c-basic-offset: 8
5395 * End:
5396 */