blob: 88a9d93ec1bd3132a956436e69bc3e6fbefe7d51 [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);
Jeff Layton2c643482010-01-07 09:42:03 -05001189 if (err != -NFS4ERR_DELAY && err != -EKEYEXPIRED)
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);
1259 case -ENOMEM:
1260 err = 0;
1261 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 }
1263 err = nfs4_handle_exception(server, err, &exception);
1264 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001265out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 return err;
1267}
1268
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001269static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1270{
1271 struct nfs4_opendata *data = calldata;
1272
1273 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001274 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001275 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1276 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001277 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001278 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001279 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001280 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001281}
1282
1283static void nfs4_open_confirm_release(void *calldata)
1284{
1285 struct nfs4_opendata *data = calldata;
1286 struct nfs4_state *state = NULL;
1287
1288 /* If this request hasn't been cancelled, do nothing */
1289 if (data->cancelled == 0)
1290 goto out_free;
1291 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001292 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001293 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001294 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001295 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001296 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001297out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001298 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001299}
1300
1301static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001302 .rpc_call_done = nfs4_open_confirm_done,
1303 .rpc_release = nfs4_open_confirm_release,
1304};
1305
1306/*
1307 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1308 */
1309static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1310{
1311 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1312 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001313 struct rpc_message msg = {
1314 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1315 .rpc_argp = &data->c_arg,
1316 .rpc_resp = &data->c_res,
1317 .rpc_cred = data->owner->so_cred,
1318 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001319 struct rpc_task_setup task_setup_data = {
1320 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001321 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001322 .callback_ops = &nfs4_open_confirm_ops,
1323 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001324 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001325 .flags = RPC_TASK_ASYNC,
1326 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 int status;
1328
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001329 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001330 data->rpc_done = 0;
1331 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001332 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001333 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001334 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001335 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001336 status = nfs4_wait_for_completion_rpc_task(task);
1337 if (status != 0) {
1338 data->cancelled = 1;
1339 smp_wmb();
1340 } else
1341 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001342 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 return status;
1344}
1345
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001346static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001348 struct nfs4_opendata *data = calldata;
1349 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001350
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001351 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1352 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001353 /*
1354 * Check if we still need to send an OPEN call, or if we can use
1355 * a delegation instead.
1356 */
1357 if (data->state != NULL) {
1358 struct nfs_delegation *delegation;
1359
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001360 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001361 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001362 rcu_read_lock();
1363 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1364 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001365 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001366 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001367 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001368 }
1369 rcu_read_unlock();
1370 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001371 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001372 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001373 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001374 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001375 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001376 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1377 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001378 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001379 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001380 &data->o_arg.seq_args,
1381 &data->o_res.seq_res, 1, task))
1382 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001383 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001384 return;
1385out_no_action:
1386 task->tk_action = NULL;
1387
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001388}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001390static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1391{
1392 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1393 nfs4_open_prepare(task, calldata);
1394}
1395
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001396static void nfs4_open_done(struct rpc_task *task, void *calldata)
1397{
1398 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001400 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001401
Trond Myklebust14516c32010-07-31 14:29:06 -04001402 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1403 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001404
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001405 if (task->tk_status == 0) {
1406 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001407 case S_IFREG:
1408 break;
1409 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001410 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001411 break;
1412 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001413 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001414 break;
1415 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001416 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001417 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001418 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001419 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1420 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001421 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001422 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001423}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001424
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001425static void nfs4_open_release(void *calldata)
1426{
1427 struct nfs4_opendata *data = calldata;
1428 struct nfs4_state *state = NULL;
1429
1430 /* If this request hasn't been cancelled, do nothing */
1431 if (data->cancelled == 0)
1432 goto out_free;
1433 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001434 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001435 goto out_free;
1436 /* In case we need an open_confirm, no cleanup! */
1437 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1438 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001439 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001440 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001441 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001442out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001443 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001444}
1445
1446static const struct rpc_call_ops nfs4_open_ops = {
1447 .rpc_call_prepare = nfs4_open_prepare,
1448 .rpc_call_done = nfs4_open_done,
1449 .rpc_release = nfs4_open_release,
1450};
1451
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001452static const struct rpc_call_ops nfs4_recover_open_ops = {
1453 .rpc_call_prepare = nfs4_recover_open_prepare,
1454 .rpc_call_done = nfs4_open_done,
1455 .rpc_release = nfs4_open_release,
1456};
1457
1458static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001459{
1460 struct inode *dir = data->dir->d_inode;
1461 struct nfs_server *server = NFS_SERVER(dir);
1462 struct nfs_openargs *o_arg = &data->o_arg;
1463 struct nfs_openres *o_res = &data->o_res;
1464 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001465 struct rpc_message msg = {
1466 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1467 .rpc_argp = o_arg,
1468 .rpc_resp = o_res,
1469 .rpc_cred = data->owner->so_cred,
1470 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001471 struct rpc_task_setup task_setup_data = {
1472 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001473 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001474 .callback_ops = &nfs4_open_ops,
1475 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001476 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001477 .flags = RPC_TASK_ASYNC,
1478 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001479 int status;
1480
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001481 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001482 data->rpc_done = 0;
1483 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001484 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001485 if (isrecover)
1486 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001487 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001488 if (IS_ERR(task))
1489 return PTR_ERR(task);
1490 status = nfs4_wait_for_completion_rpc_task(task);
1491 if (status != 0) {
1492 data->cancelled = 1;
1493 smp_wmb();
1494 } else
1495 status = data->rpc_status;
1496 rpc_put_task(task);
1497
1498 return status;
1499}
1500
1501static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1502{
1503 struct inode *dir = data->dir->d_inode;
1504 struct nfs_openres *o_res = &data->o_res;
1505 int status;
1506
1507 status = nfs4_run_open_task(data, 1);
1508 if (status != 0 || !data->rpc_done)
1509 return status;
1510
1511 nfs_refresh_inode(dir, o_res->dir_attr);
1512
1513 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1514 status = _nfs4_proc_open_confirm(data);
1515 if (status != 0)
1516 return status;
1517 }
1518
1519 return status;
1520}
1521
1522/*
1523 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1524 */
1525static int _nfs4_proc_open(struct nfs4_opendata *data)
1526{
1527 struct inode *dir = data->dir->d_inode;
1528 struct nfs_server *server = NFS_SERVER(dir);
1529 struct nfs_openargs *o_arg = &data->o_arg;
1530 struct nfs_openres *o_res = &data->o_res;
1531 int status;
1532
1533 status = nfs4_run_open_task(data, 0);
Trond Myklebust3e309912007-07-07 13:19:59 -04001534 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001535 return status;
1536
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001537 if (o_arg->open_flags & O_CREAT) {
1538 update_changeattr(dir, &o_res->cinfo);
1539 nfs_post_op_update_inode(dir, o_res->dir_attr);
1540 } else
1541 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001542 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1543 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001545 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001547 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 }
1549 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001550 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001551 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552}
1553
Trond Myklebust10afec92007-05-14 17:16:04 -04001554static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +01001555{
David Howells7539bba2006-08-22 20:06:09 -04001556 struct nfs_client *clp = server->nfs_client;
Trond Myklebusta78cb572009-08-09 15:06:19 -04001557 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001558 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001559
Trond Myklebusta78cb572009-08-09 15:06:19 -04001560 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001561 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001562 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001563 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001564 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1565 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001566 break;
Trond Myklebust58d97142006-01-03 09:55:24 +01001567 nfs4_schedule_state_recovery(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001568 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001569 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001570 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001571}
1572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573/*
1574 * OPEN_EXPIRED:
1575 * reclaim state on the server after a network partition.
1576 * Assumes caller holds the appropriate lock
1577 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001578static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001580 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001581 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001583 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1584 if (IS_ERR(opendata))
1585 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001586 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001587 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001588 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001589 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001590 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591}
1592
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001593static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001594{
Trond Myklebust539cd032007-06-05 11:46:42 -04001595 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001596 struct nfs4_exception exception = { };
1597 int err;
1598
1599 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001600 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001601 switch (err) {
1602 default:
1603 goto out;
1604 case -NFS4ERR_GRACE:
1605 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05001606 case -EKEYEXPIRED:
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001607 nfs4_handle_exception(server, err, &exception);
1608 err = 0;
1609 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001610 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001611out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001612 return err;
1613}
1614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1616{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001618 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
Trond Myklebust864472e2006-01-03 09:55:15 +01001620 ctx = nfs4_state_find_open_context(state);
1621 if (IS_ERR(ctx))
1622 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001623 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001624 put_nfs_open_context(ctx);
1625 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626}
1627
1628/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001629 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1630 * fields corresponding to attributes that were used to store the verifier.
1631 * Make sure we clobber those fields in the later setattr call
1632 */
1633static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1634{
1635 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1636 !(sattr->ia_valid & ATTR_ATIME_SET))
1637 sattr->ia_valid |= ATTR_ATIME;
1638
1639 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1640 !(sattr->ia_valid & ATTR_MTIME_SET))
1641 sattr->ia_valid |= ATTR_MTIME;
1642}
1643
1644/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001645 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001647static 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 -07001648{
1649 struct nfs4_state_owner *sp;
1650 struct nfs4_state *state = NULL;
1651 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001652 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001653 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
1655 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 status = -ENOMEM;
1657 if (!(sp = nfs4_get_state_owner(server, cred))) {
1658 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1659 goto out_err;
1660 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001661 status = nfs4_recover_expired_lease(server);
1662 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001663 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001664 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001665 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001666 status = -ENOMEM;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001667 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001668 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001669 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
Trond Myklebustaac00a82007-07-05 19:02:21 -04001671 if (path->dentry->d_inode != NULL)
1672 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1673
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001674 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001676 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001678 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001679 status = PTR_ERR(state);
1680 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001681 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001682 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001683 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001684
1685 if (opendata->o_arg.open_flags & O_EXCL) {
1686 nfs4_exclusive_attrset(opendata, sattr);
1687
1688 nfs_fattr_init(opendata->o_res.f_attr);
1689 status = nfs4_do_setattr(state->inode, cred,
1690 opendata->o_res.f_attr, sattr,
1691 state);
1692 if (status == 0)
1693 nfs_setattr_update_inode(state->inode, sattr);
1694 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1695 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001696 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 *res = state;
1699 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001700err_opendata_put:
1701 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001702err_put_state_owner:
1703 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 *res = NULL;
1706 return status;
1707}
1708
1709
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001710static 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 -07001711{
1712 struct nfs4_exception exception = { };
1713 struct nfs4_state *res;
1714 int status;
1715
1716 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001717 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 if (status == 0)
1719 break;
1720 /* NOTE: BAD_SEQID means the server and client disagree about the
1721 * book-keeping w.r.t. state-changing operations
1722 * (OPEN/CLOSE/LOCK/LOCKU...)
1723 * It is actually a sign of a bug on the client or on the server.
1724 *
1725 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001726 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 * have unhashed the old state_owner for us, and that we can
1728 * therefore safely retry using a new one. We should still warn
1729 * the user though...
1730 */
1731 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001732 printk(KERN_WARNING "NFS: v4 server %s "
1733 " returned a bad sequence-id error!\n",
1734 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 exception.retry = 1;
1736 continue;
1737 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001738 /*
1739 * BAD_STATEID on OPEN means that the server cancelled our
1740 * state before it received the OPEN_CONFIRM.
1741 * Recover by retrying the request as per the discussion
1742 * on Page 181 of RFC3530.
1743 */
1744 if (status == -NFS4ERR_BAD_STATEID) {
1745 exception.retry = 1;
1746 continue;
1747 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001748 if (status == -EAGAIN) {
1749 /* We must have found a delegation */
1750 exception.retry = 1;
1751 continue;
1752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1754 status, &exception));
1755 } while (exception.retry);
1756 return res;
1757}
1758
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001759static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1760 struct nfs_fattr *fattr, struct iattr *sattr,
1761 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001763 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001765 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 .iap = sattr,
1767 .server = server,
1768 .bitmask = server->attr_bitmask,
1769 };
1770 struct nfs_setattrres res = {
1771 .fattr = fattr,
1772 .server = server,
1773 };
1774 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001775 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1776 .rpc_argp = &arg,
1777 .rpc_resp = &res,
1778 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001780 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001781 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
Trond Myklebust0e574af2005-10-27 22:12:38 -04001783 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001785 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1786 /* Use that stateid */
1787 } else if (state != NULL) {
Trond Myklebust77041ed2010-07-01 12:49:11 -04001788 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001789 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1791
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001792 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001793 if (status == 0 && state != NULL)
1794 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001795 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796}
1797
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001798static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1799 struct nfs_fattr *fattr, struct iattr *sattr,
1800 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001802 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 struct nfs4_exception exception = { };
1804 int err;
1805 do {
1806 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001807 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 &exception);
1809 } while (exception.retry);
1810 return err;
1811}
1812
1813struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001814 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 struct inode *inode;
1816 struct nfs4_state *state;
1817 struct nfs_closeargs arg;
1818 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001819 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001820 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821};
1822
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001823static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001824{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001825 struct nfs4_closedata *calldata = data;
1826 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001827
1828 nfs4_put_open_state(calldata->state);
1829 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001830 nfs4_put_state_owner(sp);
Jan Blunck31f31db12008-05-02 13:42:45 -07001831 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001832 kfree(calldata);
1833}
1834
Trond Myklebust88069f72009-12-08 08:33:16 -05001835static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1836 fmode_t fmode)
1837{
1838 spin_lock(&state->owner->so_lock);
1839 if (!(fmode & FMODE_READ))
1840 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1841 if (!(fmode & FMODE_WRITE))
1842 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1843 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1844 spin_unlock(&state->owner->so_lock);
1845}
1846
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001847static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001849 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 struct nfs_server *server = NFS_SERVER(calldata->inode);
1852
Trond Myklebust14516c32010-07-31 14:29:06 -04001853 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1854 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 /* hmm. we are done with the inode, and in the process of freeing
1856 * the state_owner. we keep this around to process errors
1857 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 switch (task->tk_status) {
1859 case 0:
Trond Myklebust45328c32007-07-26 17:47:34 -04001860 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001861 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001862 nfs4_close_clear_stateid_flags(state,
1863 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 break;
1865 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001866 case -NFS4ERR_OLD_STATEID:
1867 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001869 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001870 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001872 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1873 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001875 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001876 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877}
1878
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001879static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001881 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001882 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001883 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001884
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001885 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001886 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001887
Trond Myklebust88069f72009-12-08 08:33:16 -05001888 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1889 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001890 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001891 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001892 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001893 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001894 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1895 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1896 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001897 }
1898 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001899 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1900 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1901 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001902 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001903 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001904 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05001905
1906 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001907 /* Note: exit _without_ calling nfs4_close_done */
1908 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001909 return;
1910 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001911
1912 if (calldata->arg.fmode == 0)
1913 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
1914
Trond Myklebust516a6af2005-10-27 22:12:41 -04001915 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001916 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001917 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Andy Adamson19ddab02009-04-01 09:22:20 -04001918 &calldata->arg.seq_args, &calldata->res.seq_res,
1919 1, task))
1920 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001921 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922}
1923
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001924static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001925 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001926 .rpc_call_done = nfs4_close_done,
1927 .rpc_release = nfs4_free_closedata,
1928};
1929
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930/*
1931 * It is possible for data to be read/written from a mem-mapped file
1932 * after the sys_close call (which hits the vfs layer as a flush).
1933 * This means that we can't safely call nfsv4 close on a file until
1934 * the inode is cleared. This in turn means that we are not good
1935 * NFSv4 citizens - we do not indicate to the server to update the file's
1936 * share state even when we are done with one of the three share
1937 * stateid's in the inode.
1938 *
1939 * NOTE: Caller must be holding the sp->so_owner semaphore!
1940 */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001941int nfs4_do_close(struct path *path, struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001943 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001945 struct nfs4_state_owner *sp = state->owner;
1946 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001947 struct rpc_message msg = {
1948 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1949 .rpc_cred = state->owner->so_cred,
1950 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001951 struct rpc_task_setup task_setup_data = {
1952 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001953 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001954 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05001955 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001956 .flags = RPC_TASK_ASYNC,
1957 };
Trond Myklebust95121352005-10-18 14:20:12 -07001958 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001960 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001962 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001963 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001965 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04001966 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001968 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07001969 if (calldata->arg.seqid == NULL)
1970 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001971 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001972 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001973 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04001974 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001975 calldata->res.server = server;
Al Virof6948692010-01-30 13:51:04 -05001976 path_get(path);
1977 calldata->path = *path;
Trond Myklebust95121352005-10-18 14:20:12 -07001978
Trond Myklebust5138fde2007-07-14 15:40:01 -04001979 msg.rpc_argp = &calldata->arg,
1980 msg.rpc_resp = &calldata->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001981 task_setup_data.callback_data = calldata;
1982 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001983 if (IS_ERR(task))
1984 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001985 status = 0;
1986 if (wait)
1987 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001988 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001989 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07001990out_free_calldata:
1991 kfree(calldata);
1992out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04001993 nfs4_put_open_state(state);
1994 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07001995 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996}
1997
Trond Myklebust2b484292010-09-17 10:56:51 -04001998static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04001999nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 struct nfs4_state *state;
2002
Trond Myklebust565277f2007-10-15 18:17:53 -04002003 /* Protect against concurrent sillydeletes */
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002004 state = nfs4_do_open(dir, &ctx->path, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002005 if (IS_ERR(state))
2006 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002007 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002008 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009}
2010
Trond Myklebust1185a552009-12-03 15:54:02 -05002011static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002012{
2013 if (ctx->state == NULL)
2014 return;
2015 if (is_sync)
2016 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2017 else
2018 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2019}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
2021static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2022{
Benny Halevy43652ad2009-04-01 09:21:54 -04002023 struct nfs4_server_caps_arg args = {
2024 .fhandle = fhandle,
2025 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 struct nfs4_server_caps_res res = {};
2027 struct rpc_message msg = {
2028 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002029 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 .rpc_resp = &res,
2031 };
2032 int status;
2033
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002034 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 if (status == 0) {
2036 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002037 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2038 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2039 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2040 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2041 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2043 server->caps |= NFS_CAP_ACLS;
2044 if (res.has_links != 0)
2045 server->caps |= NFS_CAP_HARDLINKS;
2046 if (res.has_symlinks != 0)
2047 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002048 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2049 server->caps |= NFS_CAP_FILEID;
2050 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2051 server->caps |= NFS_CAP_MODE;
2052 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2053 server->caps |= NFS_CAP_NLINK;
2054 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2055 server->caps |= NFS_CAP_OWNER;
2056 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2057 server->caps |= NFS_CAP_OWNER_GROUP;
2058 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2059 server->caps |= NFS_CAP_ATIME;
2060 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2061 server->caps |= NFS_CAP_CTIME;
2062 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2063 server->caps |= NFS_CAP_MTIME;
2064
Trond Myklebusta65318b2009-03-11 14:10:28 -04002065 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2066 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2067 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 server->acl_bitmask = res.acl_bitmask;
2069 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002070
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 return status;
2072}
2073
Trond Myklebust55a97592006-06-09 09:34:19 -04002074int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075{
2076 struct nfs4_exception exception = { };
2077 int err;
2078 do {
2079 err = nfs4_handle_exception(server,
2080 _nfs4_server_capabilities(server, fhandle),
2081 &exception);
2082 } while (exception.retry);
2083 return err;
2084}
2085
2086static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2087 struct nfs_fsinfo *info)
2088{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 struct nfs4_lookup_root_arg args = {
2090 .bitmask = nfs4_fattr_bitmap,
2091 };
2092 struct nfs4_lookup_res res = {
2093 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002094 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 .fh = fhandle,
2096 };
2097 struct rpc_message msg = {
2098 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2099 .rpc_argp = &args,
2100 .rpc_resp = &res,
2101 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002102
Trond Myklebust0e574af2005-10-27 22:12:38 -04002103 nfs_fattr_init(info->fattr);
Trond Myklebustfccba802009-07-21 16:48:07 -04002104 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105}
2106
2107static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2108 struct nfs_fsinfo *info)
2109{
2110 struct nfs4_exception exception = { };
2111 int err;
2112 do {
2113 err = nfs4_handle_exception(server,
2114 _nfs4_lookup_root(server, fhandle, info),
2115 &exception);
2116 } while (exception.retry);
2117 return err;
2118}
2119
David Howells54ceac42006-08-22 20:06:13 -04002120/*
2121 * get the file handle for the "/" directory on the server
2122 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002124 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 int status;
2127
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 if (status == 0)
2130 status = nfs4_server_capabilities(server, fhandle);
2131 if (status == 0)
2132 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002133 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134}
2135
Manoj Naik6b97fd32006-06-09 09:34:29 -04002136/*
2137 * Get locations and (maybe) other attributes of a referral.
2138 * Note that we'll actually follow the referral later when
2139 * we detect fsid mismatch in inode revalidation
2140 */
Trond Myklebust56659e92007-07-17 21:52:39 -04002141static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002142{
2143 int status = -ENOMEM;
2144 struct page *page = NULL;
2145 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002146
2147 page = alloc_page(GFP_KERNEL);
2148 if (page == NULL)
2149 goto out;
2150 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2151 if (locations == NULL)
2152 goto out;
2153
Trond Myklebustc228fd32007-01-13 02:28:11 -05002154 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002155 if (status != 0)
2156 goto out;
2157 /* Make sure server returned a different fsid for the referral */
2158 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07002159 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002160 status = -EIO;
2161 goto out;
2162 }
2163
2164 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2165 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2166 if (!fattr->mode)
2167 fattr->mode = S_IFDIR;
2168 memset(fhandle, 0, sizeof(struct nfs_fh));
2169out:
2170 if (page)
2171 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002172 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002173 return status;
2174}
2175
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2177{
2178 struct nfs4_getattr_arg args = {
2179 .fh = fhandle,
2180 .bitmask = server->attr_bitmask,
2181 };
2182 struct nfs4_getattr_res res = {
2183 .fattr = fattr,
2184 .server = server,
2185 };
2186 struct rpc_message msg = {
2187 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2188 .rpc_argp = &args,
2189 .rpc_resp = &res,
2190 };
2191
Trond Myklebust0e574af2005-10-27 22:12:38 -04002192 nfs_fattr_init(fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002193 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194}
2195
2196static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2197{
2198 struct nfs4_exception exception = { };
2199 int err;
2200 do {
2201 err = nfs4_handle_exception(server,
2202 _nfs4_proc_getattr(server, fhandle, fattr),
2203 &exception);
2204 } while (exception.retry);
2205 return err;
2206}
2207
2208/*
2209 * The file is not closed if it is opened due to the a request to change
2210 * the size of the file. The open call will not be needed once the
2211 * VFS layer lookup-intents are implemented.
2212 *
2213 * Close is called when the inode is destroyed.
2214 * If we haven't opened the file for O_WRONLY, we
2215 * need to in the size_change case to obtain a stateid.
2216 *
2217 * Got race?
2218 * Because OPEN is always done by name in nfsv4, it is
2219 * possible that we opened a different file by the same
2220 * name. We can recognize this race condition, but we
2221 * can't do anything about it besides returning an error.
2222 *
2223 * This will be fixed with VFS changes (lookup-intent).
2224 */
2225static int
2226nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2227 struct iattr *sattr)
2228{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002229 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002230 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002231 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 int status;
2233
Trond Myklebust0e574af2005-10-27 22:12:38 -04002234 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
Trond Myklebustd5308382005-11-04 15:33:38 -05002236 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002237 if (sattr->ia_valid & ATTR_FILE) {
2238 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002239
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002240 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002241 if (ctx) {
2242 cred = ctx->cred;
2243 state = ctx->state;
2244 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002245 }
2246
2247 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002248 if (status == 0)
2249 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 return status;
2251}
2252
Trond Myklebust56659e92007-07-17 21:52:39 -04002253static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2254 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04002255 struct nfs_fattr *fattr)
2256{
2257 int status;
2258 struct nfs4_lookup_arg args = {
2259 .bitmask = server->attr_bitmask,
2260 .dir_fh = dirfh,
2261 .name = name,
2262 };
2263 struct nfs4_lookup_res res = {
2264 .server = server,
2265 .fattr = fattr,
2266 .fh = fhandle,
2267 };
2268 struct rpc_message msg = {
2269 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2270 .rpc_argp = &args,
2271 .rpc_resp = &res,
2272 };
2273
2274 nfs_fattr_init(fattr);
2275
2276 dprintk("NFS call lookupfh %s\n", name->name);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002277 status = nfs4_call_sync(server, &msg, &args, &res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002278 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002279 return status;
2280}
2281
2282static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2283 struct qstr *name, struct nfs_fh *fhandle,
2284 struct nfs_fattr *fattr)
2285{
2286 struct nfs4_exception exception = { };
2287 int err;
2288 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04002289 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2290 /* FIXME: !!!! */
2291 if (err == -NFS4ERR_MOVED) {
2292 err = -EREMOTE;
2293 break;
2294 }
2295 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002296 } while (exception.retry);
2297 return err;
2298}
2299
Trond Myklebust56659e92007-07-17 21:52:39 -04002300static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2302{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002303 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
2305 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002306 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002307 if (status == -NFS4ERR_MOVED)
2308 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 dprintk("NFS reply lookup: %d\n", status);
2310 return status;
2311}
2312
2313static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2314{
2315 struct nfs4_exception exception = { };
2316 int err;
2317 do {
2318 err = nfs4_handle_exception(NFS_SERVER(dir),
2319 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2320 &exception);
2321 } while (exception.retry);
2322 return err;
2323}
2324
2325static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2326{
Trond Myklebust76b32992007-08-10 17:45:11 -04002327 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 struct nfs4_accessargs args = {
2329 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002330 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002332 struct nfs4_accessres res = {
2333 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002334 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 struct rpc_message msg = {
2336 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2337 .rpc_argp = &args,
2338 .rpc_resp = &res,
2339 .rpc_cred = entry->cred,
2340 };
2341 int mode = entry->mask;
2342 int status;
2343
2344 /*
2345 * Determine which access bits we want to ask for...
2346 */
2347 if (mode & MAY_READ)
2348 args.access |= NFS4_ACCESS_READ;
2349 if (S_ISDIR(inode->i_mode)) {
2350 if (mode & MAY_WRITE)
2351 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2352 if (mode & MAY_EXEC)
2353 args.access |= NFS4_ACCESS_LOOKUP;
2354 } else {
2355 if (mode & MAY_WRITE)
2356 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2357 if (mode & MAY_EXEC)
2358 args.access |= NFS4_ACCESS_EXECUTE;
2359 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002360
2361 res.fattr = nfs_alloc_fattr();
2362 if (res.fattr == NULL)
2363 return -ENOMEM;
2364
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002365 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 if (!status) {
2367 entry->mask = 0;
2368 if (res.access & NFS4_ACCESS_READ)
2369 entry->mask |= MAY_READ;
2370 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2371 entry->mask |= MAY_WRITE;
2372 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2373 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002374 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002376 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 return status;
2378}
2379
2380static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2381{
2382 struct nfs4_exception exception = { };
2383 int err;
2384 do {
2385 err = nfs4_handle_exception(NFS_SERVER(inode),
2386 _nfs4_proc_access(inode, entry),
2387 &exception);
2388 } while (exception.retry);
2389 return err;
2390}
2391
2392/*
2393 * TODO: For the time being, we don't try to get any attributes
2394 * along with any of the zero-copy operations READ, READDIR,
2395 * READLINK, WRITE.
2396 *
2397 * In the case of the first three, we want to put the GETATTR
2398 * after the read-type operation -- this is because it is hard
2399 * to predict the length of a GETATTR response in v4, and thus
2400 * align the READ data correctly. This means that the GETATTR
2401 * may end up partially falling into the page cache, and we should
2402 * shift it into the 'tail' of the xdr_buf before processing.
2403 * To do this efficiently, we need to know the total length
2404 * of data received, which doesn't seem to be available outside
2405 * of the RPC layer.
2406 *
2407 * In the case of WRITE, we also want to put the GETATTR after
2408 * the operation -- in this case because we want to make sure
2409 * we get the post-operation mtime and size. This means that
2410 * we can't use xdr_encode_pages() as written: we need a variant
2411 * of it which would leave room in the 'tail' iovec.
2412 *
2413 * Both of these changes to the XDR layer would in fact be quite
2414 * minor, but I decided to leave them for a subsequent patch.
2415 */
2416static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2417 unsigned int pgbase, unsigned int pglen)
2418{
2419 struct nfs4_readlink args = {
2420 .fh = NFS_FH(inode),
2421 .pgbase = pgbase,
2422 .pglen = pglen,
2423 .pages = &page,
2424 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002425 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 struct rpc_message msg = {
2427 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2428 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002429 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 };
2431
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002432 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433}
2434
2435static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2436 unsigned int pgbase, unsigned int pglen)
2437{
2438 struct nfs4_exception exception = { };
2439 int err;
2440 do {
2441 err = nfs4_handle_exception(NFS_SERVER(inode),
2442 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2443 &exception);
2444 } while (exception.retry);
2445 return err;
2446}
2447
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448/*
2449 * Got race?
2450 * We will need to arrange for the VFS layer to provide an atomic open.
2451 * Until then, this create/open method is prone to inefficiency and race
2452 * conditions due to the lookup, create, and open VFS calls from sys_open()
2453 * placed on the wire.
2454 *
2455 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2456 * The file will be opened again in the subsequent VFS open call
2457 * (nfs4_proc_file_open).
2458 *
2459 * The open for read will just hang around to be used by any process that
2460 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2461 */
2462
2463static int
2464nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002465 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466{
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002467 struct path my_path = {
Trond Myklebustad389da2007-06-05 12:30:00 -04002468 .dentry = dentry,
2469 };
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002470 struct path *path = &my_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002472 struct rpc_cred *cred = NULL;
2473 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 int status = 0;
2475
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002476 if (ctx != NULL) {
2477 cred = ctx->cred;
2478 path = &ctx->path;
2479 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 }
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002481 state = nfs4_do_open(dir, path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002482 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 if (IS_ERR(state)) {
2484 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002485 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002487 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002488 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002489 if (ctx != NULL)
2490 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002491 else
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002492 nfs4_close_sync(path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493out:
2494 return status;
2495}
2496
2497static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2498{
Trond Myklebust16e42952005-10-27 22:12:44 -04002499 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002500 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002502 .name.len = name->len,
2503 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002504 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002506 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002507 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002508 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002510 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2511 .rpc_argp = &args,
2512 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002514 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
Trond Myklebustd3468902010-04-16 16:22:50 -04002516 res.dir_attr = nfs_alloc_fattr();
2517 if (res.dir_attr == NULL)
2518 goto out;
2519
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002520 status = nfs4_call_sync(server, &msg, &args, &res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002521 if (status == 0) {
2522 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002523 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002524 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002525 nfs_free_fattr(res.dir_attr);
2526out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 return status;
2528}
2529
2530static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2531{
2532 struct nfs4_exception exception = { };
2533 int err;
2534 do {
2535 err = nfs4_handle_exception(NFS_SERVER(dir),
2536 _nfs4_proc_remove(dir, name),
2537 &exception);
2538 } while (exception.retry);
2539 return err;
2540}
2541
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002542static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002544 struct nfs_server *server = NFS_SERVER(dir);
2545 struct nfs_removeargs *args = msg->rpc_argp;
2546 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547
Trond Myklebusta65318b2009-03-11 14:10:28 -04002548 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002549 res->server = server;
Benny Halevydfb4f3092010-09-24 09:17:01 -04002550 res->seq_res.sr_slot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552}
2553
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002554static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002556 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2557
Trond Myklebust14516c32010-07-31 14:29:06 -04002558 if (!nfs4_sequence_done(task, &res->seq_res))
2559 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002560 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002561 return 0;
2562 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002563 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002564 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565}
2566
Jeff Laytond3d41522010-09-17 17:31:57 -04002567static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2568{
2569 struct nfs_server *server = NFS_SERVER(dir);
2570 struct nfs_renameargs *arg = msg->rpc_argp;
2571 struct nfs_renameres *res = msg->rpc_resp;
2572
2573 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2574 arg->bitmask = server->attr_bitmask;
2575 res->server = server;
2576}
2577
2578static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2579 struct inode *new_dir)
2580{
2581 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2582
2583 if (!nfs4_sequence_done(task, &res->seq_res))
2584 return 0;
2585 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2586 return 0;
2587
2588 update_changeattr(old_dir, &res->old_cinfo);
2589 nfs_post_op_update_inode(old_dir, res->old_fattr);
2590 update_changeattr(new_dir, &res->new_cinfo);
2591 nfs_post_op_update_inode(new_dir, res->new_fattr);
2592 return 1;
2593}
2594
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2596 struct inode *new_dir, struct qstr *new_name)
2597{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002598 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002599 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 .old_dir = NFS_FH(old_dir),
2601 .new_dir = NFS_FH(new_dir),
2602 .old_name = old_name,
2603 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002604 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002606 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002607 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002608 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 struct rpc_message msg = {
2610 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2611 .rpc_argp = &arg,
2612 .rpc_resp = &res,
2613 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002614 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
Trond Myklebust011fff72010-04-16 16:22:49 -04002616 res.old_fattr = nfs_alloc_fattr();
2617 res.new_fattr = nfs_alloc_fattr();
2618 if (res.old_fattr == NULL || res.new_fattr == NULL)
2619 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620
Trond Myklebust011fff72010-04-16 16:22:49 -04002621 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 if (!status) {
2623 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002624 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002626 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002628out:
2629 nfs_free_fattr(res.new_fattr);
2630 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 return status;
2632}
2633
2634static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2635 struct inode *new_dir, struct qstr *new_name)
2636{
2637 struct nfs4_exception exception = { };
2638 int err;
2639 do {
2640 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2641 _nfs4_proc_rename(old_dir, old_name,
2642 new_dir, new_name),
2643 &exception);
2644 } while (exception.retry);
2645 return err;
2646}
2647
2648static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2649{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002650 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 struct nfs4_link_arg arg = {
2652 .fh = NFS_FH(inode),
2653 .dir_fh = NFS_FH(dir),
2654 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002655 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002657 struct nfs4_link_res res = {
2658 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002659 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 struct rpc_message msg = {
2661 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2662 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002663 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002665 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666
Trond Myklebust136f2622010-04-16 16:22:49 -04002667 res.fattr = nfs_alloc_fattr();
2668 res.dir_attr = nfs_alloc_fattr();
2669 if (res.fattr == NULL || res.dir_attr == NULL)
2670 goto out;
2671
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002672 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002673 if (!status) {
2674 update_changeattr(dir, &res.cinfo);
2675 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002676 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002677 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002678out:
2679 nfs_free_fattr(res.dir_attr);
2680 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 return status;
2682}
2683
2684static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2685{
2686 struct nfs4_exception exception = { };
2687 int err;
2688 do {
2689 err = nfs4_handle_exception(NFS_SERVER(inode),
2690 _nfs4_proc_link(inode, dir, name),
2691 &exception);
2692 } while (exception.retry);
2693 return err;
2694}
2695
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002696struct nfs4_createdata {
2697 struct rpc_message msg;
2698 struct nfs4_create_arg arg;
2699 struct nfs4_create_res res;
2700 struct nfs_fh fh;
2701 struct nfs_fattr fattr;
2702 struct nfs_fattr dir_fattr;
2703};
2704
2705static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2706 struct qstr *name, struct iattr *sattr, u32 ftype)
2707{
2708 struct nfs4_createdata *data;
2709
2710 data = kzalloc(sizeof(*data), GFP_KERNEL);
2711 if (data != NULL) {
2712 struct nfs_server *server = NFS_SERVER(dir);
2713
2714 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2715 data->msg.rpc_argp = &data->arg;
2716 data->msg.rpc_resp = &data->res;
2717 data->arg.dir_fh = NFS_FH(dir);
2718 data->arg.server = server;
2719 data->arg.name = name;
2720 data->arg.attrs = sattr;
2721 data->arg.ftype = ftype;
2722 data->arg.bitmask = server->attr_bitmask;
2723 data->res.server = server;
2724 data->res.fh = &data->fh;
2725 data->res.fattr = &data->fattr;
2726 data->res.dir_fattr = &data->dir_fattr;
2727 nfs_fattr_init(data->res.fattr);
2728 nfs_fattr_init(data->res.dir_fattr);
2729 }
2730 return data;
2731}
2732
2733static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2734{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002735 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2736 &data->arg, &data->res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002737 if (status == 0) {
2738 update_changeattr(dir, &data->res.dir_cinfo);
2739 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2740 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2741 }
2742 return status;
2743}
2744
2745static void nfs4_free_createdata(struct nfs4_createdata *data)
2746{
2747 kfree(data);
2748}
2749
Chuck Lever4f390c12006-08-22 20:06:22 -04002750static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002751 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002753 struct nfs4_createdata *data;
2754 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755
Chuck Lever94a6d752006-08-22 20:06:23 -04002756 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002757 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002758
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002759 status = -ENOMEM;
2760 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2761 if (data == NULL)
2762 goto out;
2763
2764 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2765 data->arg.u.symlink.pages = &page;
2766 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002768 status = nfs4_do_create(dir, dentry, data);
2769
2770 nfs4_free_createdata(data);
2771out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 return status;
2773}
2774
Chuck Lever4f390c12006-08-22 20:06:22 -04002775static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002776 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777{
2778 struct nfs4_exception exception = { };
2779 int err;
2780 do {
2781 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002782 _nfs4_proc_symlink(dir, dentry, page,
2783 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 &exception);
2785 } while (exception.retry);
2786 return err;
2787}
2788
2789static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2790 struct iattr *sattr)
2791{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002792 struct nfs4_createdata *data;
2793 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002795 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2796 if (data == NULL)
2797 goto out;
2798
2799 status = nfs4_do_create(dir, dentry, data);
2800
2801 nfs4_free_createdata(data);
2802out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 return status;
2804}
2805
2806static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2807 struct iattr *sattr)
2808{
2809 struct nfs4_exception exception = { };
2810 int err;
2811 do {
2812 err = nfs4_handle_exception(NFS_SERVER(dir),
2813 _nfs4_proc_mkdir(dir, dentry, sattr),
2814 &exception);
2815 } while (exception.retry);
2816 return err;
2817}
2818
2819static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2820 u64 cookie, struct page *page, unsigned int count, int plus)
2821{
2822 struct inode *dir = dentry->d_inode;
2823 struct nfs4_readdir_arg args = {
2824 .fh = NFS_FH(dir),
2825 .pages = &page,
2826 .pgbase = 0,
2827 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002828 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 };
2830 struct nfs4_readdir_res res;
2831 struct rpc_message msg = {
2832 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2833 .rpc_argp = &args,
2834 .rpc_resp = &res,
2835 .rpc_cred = cred,
2836 };
2837 int status;
2838
Harvey Harrison3110ff82008-05-02 13:42:44 -07002839 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002840 dentry->d_parent->d_name.name,
2841 dentry->d_name.name,
2842 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2844 res.pgbase = args.pgbase;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002845 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 if (status == 0)
2847 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustc4812992007-09-28 17:11:45 -04002848
2849 nfs_invalidate_atime(dir);
2850
Harvey Harrison3110ff82008-05-02 13:42:44 -07002851 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 return status;
2853}
2854
2855static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2856 u64 cookie, struct page *page, unsigned int count, int plus)
2857{
2858 struct nfs4_exception exception = { };
2859 int err;
2860 do {
2861 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2862 _nfs4_proc_readdir(dentry, cred, cookie,
2863 page, count, plus),
2864 &exception);
2865 } while (exception.retry);
2866 return err;
2867}
2868
2869static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2870 struct iattr *sattr, dev_t rdev)
2871{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002872 struct nfs4_createdata *data;
2873 int mode = sattr->ia_mode;
2874 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875
2876 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2877 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002878
2879 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2880 if (data == NULL)
2881 goto out;
2882
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002884 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002886 data->arg.ftype = NF4BLK;
2887 data->arg.u.device.specdata1 = MAJOR(rdev);
2888 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 }
2890 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002891 data->arg.ftype = NF4CHR;
2892 data->arg.u.device.specdata1 = MAJOR(rdev);
2893 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002896 status = nfs4_do_create(dir, dentry, data);
2897
2898 nfs4_free_createdata(data);
2899out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 return status;
2901}
2902
2903static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2904 struct iattr *sattr, dev_t rdev)
2905{
2906 struct nfs4_exception exception = { };
2907 int err;
2908 do {
2909 err = nfs4_handle_exception(NFS_SERVER(dir),
2910 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2911 &exception);
2912 } while (exception.retry);
2913 return err;
2914}
2915
2916static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2917 struct nfs_fsstat *fsstat)
2918{
2919 struct nfs4_statfs_arg args = {
2920 .fh = fhandle,
2921 .bitmask = server->attr_bitmask,
2922 };
Benny Halevy24ad1482009-04-01 09:21:56 -04002923 struct nfs4_statfs_res res = {
2924 .fsstat = fsstat,
2925 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 struct rpc_message msg = {
2927 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2928 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04002929 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 };
2931
Trond Myklebust0e574af2005-10-27 22:12:38 -04002932 nfs_fattr_init(fsstat->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002933 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934}
2935
2936static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2937{
2938 struct nfs4_exception exception = { };
2939 int err;
2940 do {
2941 err = nfs4_handle_exception(server,
2942 _nfs4_proc_statfs(server, fhandle, fsstat),
2943 &exception);
2944 } while (exception.retry);
2945 return err;
2946}
2947
2948static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2949 struct nfs_fsinfo *fsinfo)
2950{
2951 struct nfs4_fsinfo_arg args = {
2952 .fh = fhandle,
2953 .bitmask = server->attr_bitmask,
2954 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04002955 struct nfs4_fsinfo_res res = {
2956 .fsinfo = fsinfo,
2957 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 struct rpc_message msg = {
2959 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2960 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04002961 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 };
2963
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002964 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965}
2966
2967static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2968{
2969 struct nfs4_exception exception = { };
2970 int err;
2971
2972 do {
2973 err = nfs4_handle_exception(server,
2974 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2975 &exception);
2976 } while (exception.retry);
2977 return err;
2978}
2979
2980static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2981{
Trond Myklebust0e574af2005-10-27 22:12:38 -04002982 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2984}
2985
2986static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2987 struct nfs_pathconf *pathconf)
2988{
2989 struct nfs4_pathconf_arg args = {
2990 .fh = fhandle,
2991 .bitmask = server->attr_bitmask,
2992 };
Benny Halevyd45b2982009-04-01 09:21:58 -04002993 struct nfs4_pathconf_res res = {
2994 .pathconf = pathconf,
2995 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 struct rpc_message msg = {
2997 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
2998 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04002999 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 };
3001
3002 /* None of the pathconf attributes are mandatory to implement */
3003 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3004 memset(pathconf, 0, sizeof(*pathconf));
3005 return 0;
3006 }
3007
Trond Myklebust0e574af2005-10-27 22:12:38 -04003008 nfs_fattr_init(pathconf->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003009 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010}
3011
3012static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3013 struct nfs_pathconf *pathconf)
3014{
3015 struct nfs4_exception exception = { };
3016 int err;
3017
3018 do {
3019 err = nfs4_handle_exception(server,
3020 _nfs4_proc_pathconf(server, fhandle, pathconf),
3021 &exception);
3022 } while (exception.retry);
3023 return err;
3024}
3025
Trond Myklebustec06c092006-03-20 13:44:27 -05003026static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027{
Trond Myklebustec06c092006-03-20 13:44:27 -05003028 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003030 dprintk("--> %s\n", __func__);
3031
Trond Myklebust14516c32010-07-31 14:29:06 -04003032 if (!nfs4_sequence_done(task, &data->res.seq_res))
3033 return -EAGAIN;
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003034
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003035 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003036 nfs_restart_rpc(task, server->nfs_client);
Trond Myklebustec06c092006-03-20 13:44:27 -05003037 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003039
3040 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003042 renew_lease(server, data->timestamp);
3043 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044}
3045
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003046static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 data->timestamp = jiffies;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003049 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050}
3051
Trond Myklebust788e7a82006-03-20 13:44:27 -05003052static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 struct inode *inode = data->inode;
3055
Trond Myklebust14516c32010-07-31 14:29:06 -04003056 if (!nfs4_sequence_done(task, &data->res.seq_res))
3057 return -EAGAIN;
Andy Adamsondef6ed72009-04-01 09:22:26 -04003058
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003059 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003060 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003061 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003063 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003065 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003066 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003067 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068}
3069
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003070static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003072 struct nfs_server *server = NFS_SERVER(data->inode);
3073
Trond Myklebusta65318b2009-03-11 14:10:28 -04003074 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003075 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 data->timestamp = jiffies;
3077
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003078 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079}
3080
Trond Myklebust788e7a82006-03-20 13:44:27 -05003081static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 struct inode *inode = data->inode;
3084
Trond Myklebust14516c32010-07-31 14:29:06 -04003085 if (!nfs4_sequence_done(task, &data->res.seq_res))
3086 return -EAGAIN;
3087
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003088 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003089 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003090 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003092 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003093 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094}
3095
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003096static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003098 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099
Trond Myklebusta65318b2009-03-11 14:10:28 -04003100 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003101 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003102 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103}
3104
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003105struct nfs4_renewdata {
3106 struct nfs_client *client;
3107 unsigned long timestamp;
3108};
3109
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110/*
3111 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3112 * standalone procedure for queueing an asynchronous RENEW.
3113 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003114static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003115{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003116 struct nfs4_renewdata *data = calldata;
3117 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003118
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003119 if (atomic_read(&clp->cl_count) > 1)
3120 nfs4_schedule_state_renewal(clp);
3121 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003122 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003123}
3124
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003125static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003127 struct nfs4_renewdata *data = calldata;
3128 struct nfs_client *clp = data->client;
3129 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130
3131 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003132 /* Unless we're shutting down, schedule state recovery! */
3133 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3134 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 return;
3136 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003137 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138}
3139
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003140static const struct rpc_call_ops nfs4_renew_ops = {
3141 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003142 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003143};
3144
David Howellsadfa6f92006-08-22 20:06:08 -04003145int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146{
3147 struct rpc_message msg = {
3148 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3149 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003150 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003152 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003154 if (!atomic_inc_not_zero(&clp->cl_count))
3155 return -EIO;
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003156 data = kmalloc(sizeof(*data), GFP_KERNEL);
3157 if (data == NULL)
3158 return -ENOMEM;
3159 data->client = clp;
3160 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003162 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163}
3164
David Howellsadfa6f92006-08-22 20:06:08 -04003165int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166{
3167 struct rpc_message msg = {
3168 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3169 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003170 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 };
3172 unsigned long now = jiffies;
3173 int status;
3174
3175 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3176 if (status < 0)
3177 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003178 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 return 0;
3180}
3181
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003182static inline int nfs4_server_supports_acls(struct nfs_server *server)
3183{
3184 return (server->caps & NFS_CAP_ACLS)
3185 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3186 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3187}
3188
3189/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3190 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3191 * the stack.
3192 */
3193#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3194
3195static void buf_to_pages(const void *buf, size_t buflen,
3196 struct page **pages, unsigned int *pgbase)
3197{
3198 const void *p = buf;
3199
3200 *pgbase = offset_in_page(buf);
3201 p -= *pgbase;
3202 while (p < buf + buflen) {
3203 *(pages++) = virt_to_page(p);
3204 p += PAGE_CACHE_SIZE;
3205 }
3206}
3207
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003208struct nfs4_cached_acl {
3209 int cached;
3210 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003211 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003212};
3213
3214static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003215{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003216 struct nfs_inode *nfsi = NFS_I(inode);
3217
3218 spin_lock(&inode->i_lock);
3219 kfree(nfsi->nfs4_acl);
3220 nfsi->nfs4_acl = acl;
3221 spin_unlock(&inode->i_lock);
3222}
3223
3224static void nfs4_zap_acl_attr(struct inode *inode)
3225{
3226 nfs4_set_cached_acl(inode, NULL);
3227}
3228
3229static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3230{
3231 struct nfs_inode *nfsi = NFS_I(inode);
3232 struct nfs4_cached_acl *acl;
3233 int ret = -ENOENT;
3234
3235 spin_lock(&inode->i_lock);
3236 acl = nfsi->nfs4_acl;
3237 if (acl == NULL)
3238 goto out;
3239 if (buf == NULL) /* user is just asking for length */
3240 goto out_len;
3241 if (acl->cached == 0)
3242 goto out;
3243 ret = -ERANGE; /* see getxattr(2) man page */
3244 if (acl->len > buflen)
3245 goto out;
3246 memcpy(buf, acl->data, acl->len);
3247out_len:
3248 ret = acl->len;
3249out:
3250 spin_unlock(&inode->i_lock);
3251 return ret;
3252}
3253
3254static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3255{
3256 struct nfs4_cached_acl *acl;
3257
3258 if (buf && acl_len <= PAGE_SIZE) {
3259 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3260 if (acl == NULL)
3261 goto out;
3262 acl->cached = 1;
3263 memcpy(acl->data, buf, acl_len);
3264 } else {
3265 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3266 if (acl == NULL)
3267 goto out;
3268 acl->cached = 0;
3269 }
3270 acl->len = acl_len;
3271out:
3272 nfs4_set_cached_acl(inode, acl);
3273}
3274
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003275static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003276{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003277 struct page *pages[NFS4ACL_MAXPAGES];
3278 struct nfs_getaclargs args = {
3279 .fh = NFS_FH(inode),
3280 .acl_pages = pages,
3281 .acl_len = buflen,
3282 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003283 struct nfs_getaclres res = {
3284 .acl_len = buflen,
3285 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003286 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003287 struct rpc_message msg = {
3288 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3289 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003290 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003291 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003292 struct page *localpage = NULL;
3293 int ret;
3294
3295 if (buflen < PAGE_SIZE) {
3296 /* As long as we're doing a round trip to the server anyway,
3297 * let's be prepared for a page of acl data. */
3298 localpage = alloc_page(GFP_KERNEL);
3299 resp_buf = page_address(localpage);
3300 if (localpage == NULL)
3301 return -ENOMEM;
3302 args.acl_pages[0] = localpage;
3303 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003304 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003305 } else {
3306 resp_buf = buf;
3307 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3308 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003309 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003310 if (ret)
3311 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003312 if (res.acl_len > args.acl_len)
3313 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003314 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003315 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003316 if (buf) {
3317 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003318 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003319 goto out_free;
3320 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003321 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003322 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003323 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003324out_free:
3325 if (localpage)
3326 __free_page(localpage);
3327 return ret;
3328}
3329
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003330static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3331{
3332 struct nfs4_exception exception = { };
3333 ssize_t ret;
3334 do {
3335 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3336 if (ret >= 0)
3337 break;
3338 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3339 } while (exception.retry);
3340 return ret;
3341}
3342
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003343static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3344{
3345 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003346 int ret;
3347
3348 if (!nfs4_server_supports_acls(server))
3349 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003350 ret = nfs_revalidate_inode(server, inode);
3351 if (ret < 0)
3352 return ret;
3353 ret = nfs4_read_cached_acl(inode, buf, buflen);
3354 if (ret != -ENOENT)
3355 return ret;
3356 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003357}
3358
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003359static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003360{
3361 struct nfs_server *server = NFS_SERVER(inode);
3362 struct page *pages[NFS4ACL_MAXPAGES];
3363 struct nfs_setaclargs arg = {
3364 .fh = NFS_FH(inode),
3365 .acl_pages = pages,
3366 .acl_len = buflen,
3367 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003368 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003369 struct rpc_message msg = {
3370 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3371 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003372 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003373 };
3374 int ret;
3375
3376 if (!nfs4_server_supports_acls(server))
3377 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07003378 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003379 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003380 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003381 nfs_access_zap_cache(inode);
3382 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003383 return ret;
3384}
3385
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003386static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3387{
3388 struct nfs4_exception exception = { };
3389 int err;
3390 do {
3391 err = nfs4_handle_exception(NFS_SERVER(inode),
3392 __nfs4_proc_set_acl(inode, buf, buflen),
3393 &exception);
3394 } while (exception.retry);
3395 return err;
3396}
3397
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003399nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003401 struct nfs_client *clp = server->nfs_client;
3402
3403 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 return 0;
3405 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003406 case -NFS4ERR_ADMIN_REVOKED:
3407 case -NFS4ERR_BAD_STATEID:
3408 case -NFS4ERR_OPENMODE:
3409 if (state == NULL)
3410 break;
3411 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003412 goto do_state_recovery;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003414 case -NFS4ERR_STALE_CLIENTID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 case -NFS4ERR_EXPIRED:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003416 goto do_state_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003417#if defined(CONFIG_NFS_V4_1)
3418 case -NFS4ERR_BADSESSION:
3419 case -NFS4ERR_BADSLOT:
3420 case -NFS4ERR_BAD_HIGH_SLOT:
3421 case -NFS4ERR_DEADSESSION:
3422 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3423 case -NFS4ERR_SEQ_FALSE_RETRY:
3424 case -NFS4ERR_SEQ_MISORDERED:
3425 dprintk("%s ERROR %d, Reset session\n", __func__,
3426 task->tk_status);
Andy Adamson0b9e2d42009-12-04 16:02:14 -05003427 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -04003428 task->tk_status = 0;
3429 return -EAGAIN;
3430#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003432 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003433 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003434 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3436 task->tk_status = 0;
3437 return -EAGAIN;
3438 case -NFS4ERR_OLD_STATEID:
3439 task->tk_status = 0;
3440 return -EAGAIN;
3441 }
3442 task->tk_status = nfs4_map_errors(task->tk_status);
3443 return 0;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003444do_state_recovery:
3445 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3446 nfs4_schedule_state_recovery(clp);
3447 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3448 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3449 task->tk_status = 0;
3450 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451}
3452
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003453int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3454 unsigned short port, struct rpc_cred *cred,
3455 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456{
3457 nfs4_verifier sc_verifier;
3458 struct nfs4_setclientid setclientid = {
3459 .sc_verifier = &sc_verifier,
3460 .sc_prog = program,
3461 };
3462 struct rpc_message msg = {
3463 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3464 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003465 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003466 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 };
Al Virobc4785c2006-10-19 23:28:51 -07003468 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 int loop = 0;
3470 int status;
3471
Al Virobc4785c2006-10-19 23:28:51 -07003472 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3474 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3475
3476 for(;;) {
3477 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003478 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003479 clp->cl_ipaddr,
3480 rpc_peeraddr2str(clp->cl_rpcclient,
3481 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003482 rpc_peeraddr2str(clp->cl_rpcclient,
3483 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003484 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 clp->cl_id_uniquifier);
3486 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003487 sizeof(setclientid.sc_netid),
3488 rpc_peeraddr2str(clp->cl_rpcclient,
3489 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003491 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 clp->cl_ipaddr, port >> 8, port & 255);
3493
3494 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3495 if (status != -NFS4ERR_CLID_INUSE)
3496 break;
3497 if (signalled())
3498 break;
3499 if (loop++ & 1)
3500 ssleep(clp->cl_lease_time + 1);
3501 else
3502 if (++clp->cl_id_uniquifier == 0)
3503 break;
3504 }
3505 return status;
3506}
3507
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003508static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3509 struct nfs4_setclientid_res *arg,
3510 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511{
3512 struct nfs_fsinfo fsinfo;
3513 struct rpc_message msg = {
3514 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003515 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003517 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 };
3519 unsigned long now;
3520 int status;
3521
3522 now = jiffies;
3523 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3524 if (status == 0) {
3525 spin_lock(&clp->cl_lock);
3526 clp->cl_lease_time = fsinfo.lease_time * HZ;
3527 clp->cl_last_renewal = now;
3528 spin_unlock(&clp->cl_lock);
3529 }
3530 return status;
3531}
3532
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003533int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3534 struct nfs4_setclientid_res *arg,
3535 struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003536{
Benny Halevy77e03672008-06-24 20:25:57 +03003537 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003538 int err;
3539 do {
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003540 err = _nfs4_proc_setclientid_confirm(clp, arg, cred);
Trond Myklebust51581f32006-03-20 13:44:47 -05003541 switch (err) {
3542 case 0:
3543 return err;
3544 case -NFS4ERR_RESOURCE:
3545 /* The IBM lawyers misread another document! */
3546 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05003547 case -EKEYEXPIRED:
Trond Myklebust51581f32006-03-20 13:44:47 -05003548 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3549 }
3550 } while (err == 0);
3551 return err;
3552}
3553
Trond Myklebustfe650402006-01-03 09:55:18 +01003554struct nfs4_delegreturndata {
3555 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003556 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003557 struct nfs_fh fh;
3558 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003559 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003560 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003561 int rpc_status;
3562};
3563
Trond Myklebustfe650402006-01-03 09:55:18 +01003564static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3565{
3566 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003567
Trond Myklebust14516c32010-07-31 14:29:06 -04003568 if (!nfs4_sequence_done(task, &data->res.seq_res))
3569 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003570
Ricardo Labiaga79708862009-12-07 09:23:21 -05003571 switch (task->tk_status) {
3572 case -NFS4ERR_STALE_STATEID:
3573 case -NFS4ERR_EXPIRED:
3574 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003575 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003576 break;
3577 default:
3578 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3579 -EAGAIN) {
3580 nfs_restart_rpc(task, data->res.server->nfs_client);
3581 return;
3582 }
3583 }
3584 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003585}
3586
3587static void nfs4_delegreturn_release(void *calldata)
3588{
Trond Myklebustfe650402006-01-03 09:55:18 +01003589 kfree(calldata);
3590}
3591
Andy Adamson938e1012009-04-01 09:22:28 -04003592#if defined(CONFIG_NFS_V4_1)
3593static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3594{
3595 struct nfs4_delegreturndata *d_data;
3596
3597 d_data = (struct nfs4_delegreturndata *)data;
3598
Trond Myklebust035168a2010-06-16 09:52:26 -04003599 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003600 &d_data->args.seq_args,
3601 &d_data->res.seq_res, 1, task))
3602 return;
3603 rpc_call_start(task);
3604}
3605#endif /* CONFIG_NFS_V4_1 */
3606
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003607static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003608#if defined(CONFIG_NFS_V4_1)
3609 .rpc_call_prepare = nfs4_delegreturn_prepare,
3610#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003611 .rpc_call_done = nfs4_delegreturn_done,
3612 .rpc_release = nfs4_delegreturn_release,
3613};
3614
Trond Myklebuste6f81072008-01-24 18:14:34 -05003615static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003616{
3617 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003618 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003619 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003620 struct rpc_message msg = {
3621 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3622 .rpc_cred = cred,
3623 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003624 struct rpc_task_setup task_setup_data = {
3625 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003626 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003627 .callback_ops = &nfs4_delegreturn_ops,
3628 .flags = RPC_TASK_ASYNC,
3629 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003630 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003631
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003632 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003633 if (data == NULL)
3634 return -ENOMEM;
3635 data->args.fhandle = &data->fh;
3636 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003637 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003638 nfs_copy_fh(&data->fh, NFS_FH(inode));
3639 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003640 data->res.fattr = &data->fattr;
3641 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003642 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003643 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003644 data->rpc_status = 0;
3645
Trond Myklebustc970aa82007-07-14 15:39:59 -04003646 task_setup_data.callback_data = data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003647 msg.rpc_argp = &data->args,
3648 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003649 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003650 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003651 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003652 if (!issync)
3653 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003654 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003655 if (status != 0)
3656 goto out;
3657 status = data->rpc_status;
3658 if (status != 0)
3659 goto out;
3660 nfs_refresh_inode(inode, &data->fattr);
3661out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003662 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003663 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664}
3665
Trond Myklebuste6f81072008-01-24 18:14:34 -05003666int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667{
3668 struct nfs_server *server = NFS_SERVER(inode);
3669 struct nfs4_exception exception = { };
3670 int err;
3671 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003672 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 switch (err) {
3674 case -NFS4ERR_STALE_STATEID:
3675 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 case 0:
3677 return 0;
3678 }
3679 err = nfs4_handle_exception(server, err, &exception);
3680 } while (exception.retry);
3681 return err;
3682}
3683
3684#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3685#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3686
3687/*
3688 * sleep, with exponential backoff, and retry the LOCK operation.
3689 */
3690static unsigned long
3691nfs4_set_lock_task_retry(unsigned long timeout)
3692{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003693 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 timeout <<= 1;
3695 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3696 return NFS4_LOCK_MAXTIMEOUT;
3697 return timeout;
3698}
3699
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3701{
3702 struct inode *inode = state->inode;
3703 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003704 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003705 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003707 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003709 struct nfs_lockt_res res = {
3710 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 };
3712 struct rpc_message msg = {
3713 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3714 .rpc_argp = &arg,
3715 .rpc_resp = &res,
3716 .rpc_cred = state->owner->so_cred,
3717 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 struct nfs4_lock_state *lsp;
3719 int status;
3720
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003721 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003722 status = nfs4_set_lock_state(state, request);
3723 if (status != 0)
3724 goto out;
3725 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003726 arg.lock_owner.id = lsp->ls_id.id;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003727 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003728 switch (status) {
3729 case 0:
3730 request->fl_type = F_UNLCK;
3731 break;
3732 case -NFS4ERR_DENIED:
3733 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003735 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003736out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737 return status;
3738}
3739
3740static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3741{
3742 struct nfs4_exception exception = { };
3743 int err;
3744
3745 do {
3746 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3747 _nfs4_proc_getlk(state, cmd, request),
3748 &exception);
3749 } while (exception.retry);
3750 return err;
3751}
3752
3753static int do_vfs_lock(struct file *file, struct file_lock *fl)
3754{
3755 int res = 0;
3756 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3757 case FL_POSIX:
3758 res = posix_lock_file_wait(file, fl);
3759 break;
3760 case FL_FLOCK:
3761 res = flock_lock_file_wait(file, fl);
3762 break;
3763 default:
3764 BUG();
3765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 return res;
3767}
3768
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003769struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003770 struct nfs_locku_args arg;
3771 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003772 struct nfs4_lock_state *lsp;
3773 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003774 struct file_lock fl;
3775 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003776 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003777};
3778
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003779static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3780 struct nfs_open_context *ctx,
3781 struct nfs4_lock_state *lsp,
3782 struct nfs_seqid *seqid)
3783{
3784 struct nfs4_unlockdata *p;
3785 struct inode *inode = lsp->ls_state->inode;
3786
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003787 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003788 if (p == NULL)
3789 return NULL;
3790 p->arg.fh = NFS_FH(inode);
3791 p->arg.fl = &p->fl;
3792 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003793 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003794 p->arg.stateid = &lsp->ls_stateid;
3795 p->lsp = lsp;
3796 atomic_inc(&lsp->ls_count);
3797 /* Ensure we don't close file until we're done freeing locks! */
3798 p->ctx = get_nfs_open_context(ctx);
3799 memcpy(&p->fl, fl, sizeof(p->fl));
3800 p->server = NFS_SERVER(inode);
3801 return p;
3802}
3803
Trond Myklebust06f814a2006-01-03 09:55:07 +01003804static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003805{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003806 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003807 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003808 nfs4_put_lock_state(calldata->lsp);
3809 put_nfs_open_context(calldata->ctx);
3810 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003811}
3812
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003813static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003814{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003815 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003816
Trond Myklebust14516c32010-07-31 14:29:06 -04003817 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3818 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003819 switch (task->tk_status) {
3820 case 0:
3821 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003822 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003823 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003824 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003825 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003826 case -NFS4ERR_BAD_STATEID:
3827 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003828 case -NFS4ERR_STALE_STATEID:
3829 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003830 break;
3831 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003832 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebust0110ee12009-12-07 09:00:24 -05003833 nfs_restart_rpc(task,
Trond Myklebustd61e6122009-12-05 19:32:19 -05003834 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003835 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003836}
3837
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003838static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003839{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003840 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003842 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003843 return;
3844 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003845 /* Note: exit _without_ running nfs4_locku_done */
3846 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003847 return;
3848 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003849 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04003850 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04003851 &calldata->arg.seq_args,
3852 &calldata->res.seq_res, 1, task))
3853 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003854 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855}
3856
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003857static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003858 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003859 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003860 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003861};
3862
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003863static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3864 struct nfs_open_context *ctx,
3865 struct nfs4_lock_state *lsp,
3866 struct nfs_seqid *seqid)
3867{
3868 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003869 struct rpc_message msg = {
3870 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3871 .rpc_cred = ctx->cred,
3872 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003873 struct rpc_task_setup task_setup_data = {
3874 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003875 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003876 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003877 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003878 .flags = RPC_TASK_ASYNC,
3879 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003880
Frank Filz137d6ac2007-07-09 15:32:29 -07003881 /* Ensure this is an unlock - when canceling a lock, the
3882 * canceled lock is passed in, and it won't be an unlock.
3883 */
3884 fl->fl_type = F_UNLCK;
3885
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003886 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3887 if (data == NULL) {
3888 nfs_free_seqid(seqid);
3889 return ERR_PTR(-ENOMEM);
3890 }
3891
Trond Myklebust5138fde2007-07-14 15:40:01 -04003892 msg.rpc_argp = &data->arg,
3893 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003894 task_setup_data.callback_data = data;
3895 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003896}
3897
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3899{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003900 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003901 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003902 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003903 struct rpc_task *task;
3904 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003905 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906
Trond Myklebust9b073572006-06-29 16:38:34 -04003907 status = nfs4_set_lock_state(state, request);
3908 /* Unlock _before_ we do the RPC call */
3909 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003910 down_read(&nfsi->rwsem);
3911 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3912 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003913 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003914 }
3915 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003916 if (status != 0)
3917 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003918 /* Is this a delegated lock? */
3919 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04003920 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003921 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003922 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04003923 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003924 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04003925 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003926 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003927 status = PTR_ERR(task);
3928 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04003929 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003930 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003931 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04003932out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003933 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003934 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935}
3936
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003937struct nfs4_lockdata {
3938 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003939 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003940 struct nfs4_lock_state *lsp;
3941 struct nfs_open_context *ctx;
3942 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003943 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003944 int rpc_status;
3945 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04003946 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003947};
3948
3949static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003950 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
3951 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003952{
3953 struct nfs4_lockdata *p;
3954 struct inode *inode = lsp->ls_state->inode;
3955 struct nfs_server *server = NFS_SERVER(inode);
3956
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003957 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003958 if (p == NULL)
3959 return NULL;
3960
3961 p->arg.fh = NFS_FH(inode);
3962 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003963 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003964 if (p->arg.open_seqid == NULL)
3965 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003966 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003967 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003968 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003969 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04003970 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003971 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003972 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003973 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04003974 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003975 atomic_inc(&lsp->ls_count);
3976 p->ctx = get_nfs_open_context(ctx);
3977 memcpy(&p->fl, fl, sizeof(p->fl));
3978 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003979out_free_seqid:
3980 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003981out_free:
3982 kfree(p);
3983 return NULL;
3984}
3985
3986static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3987{
3988 struct nfs4_lockdata *data = calldata;
3989 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990
Harvey Harrison3110ff82008-05-02 13:42:44 -07003991 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003992 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3993 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003994 /* Do we need to do an open_to_lock_owner? */
3995 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05003996 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
3997 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003998 data->arg.open_stateid = &state->stateid;
3999 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004000 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004001 } else
4002 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004003 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004004 if (nfs4_setup_sequence(data->server,
4005 &data->arg.seq_args,
Andy Adamson66179ef2009-04-01 09:22:22 -04004006 &data->res.seq_res, 1, task))
4007 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004008 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004009 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004010}
4011
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004012static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4013{
4014 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4015 nfs4_lock_prepare(task, calldata);
4016}
4017
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004018static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4019{
4020 struct nfs4_lockdata *data = calldata;
4021
Harvey Harrison3110ff82008-05-02 13:42:44 -07004022 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004023
Trond Myklebust14516c32010-07-31 14:29:06 -04004024 if (!nfs4_sequence_done(task, &data->res.seq_res))
4025 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004026
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004027 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004028 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004029 if (data->rpc_status == 0)
4030 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4031 else
4032 goto out;
4033 }
4034 if (data->rpc_status == 0) {
4035 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4036 sizeof(data->lsp->ls_stateid.data));
4037 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04004038 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004039 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004040out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004041 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004042}
4043
4044static void nfs4_lock_release(void *calldata)
4045{
4046 struct nfs4_lockdata *data = calldata;
4047
Harvey Harrison3110ff82008-05-02 13:42:44 -07004048 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004049 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004050 if (data->cancelled != 0) {
4051 struct rpc_task *task;
4052 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4053 data->arg.lock_seqid);
4054 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004055 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004056 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004057 } else
4058 nfs_free_seqid(data->arg.lock_seqid);
4059 nfs4_put_lock_state(data->lsp);
4060 put_nfs_open_context(data->ctx);
4061 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004062 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004063}
4064
4065static const struct rpc_call_ops nfs4_lock_ops = {
4066 .rpc_call_prepare = nfs4_lock_prepare,
4067 .rpc_call_done = nfs4_lock_done,
4068 .rpc_release = nfs4_lock_release,
4069};
4070
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004071static const struct rpc_call_ops nfs4_recover_lock_ops = {
4072 .rpc_call_prepare = nfs4_recover_lock_prepare,
4073 .rpc_call_done = nfs4_lock_done,
4074 .rpc_release = nfs4_lock_release,
4075};
4076
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004077static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4078{
4079 struct nfs_client *clp = server->nfs_client;
4080 struct nfs4_state *state = lsp->ls_state;
4081
4082 switch (error) {
4083 case -NFS4ERR_ADMIN_REVOKED:
4084 case -NFS4ERR_BAD_STATEID:
4085 case -NFS4ERR_EXPIRED:
4086 if (new_lock_owner != 0 ||
4087 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4088 nfs4_state_mark_reclaim_nograce(clp, state);
4089 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004090 break;
4091 case -NFS4ERR_STALE_STATEID:
4092 if (new_lock_owner != 0 ||
4093 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4094 nfs4_state_mark_reclaim_reboot(clp, state);
4095 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004096 };
4097}
4098
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004099static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004100{
4101 struct nfs4_lockdata *data;
4102 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004103 struct rpc_message msg = {
4104 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4105 .rpc_cred = state->owner->so_cred,
4106 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004107 struct rpc_task_setup task_setup_data = {
4108 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004109 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004110 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004111 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004112 .flags = RPC_TASK_ASYNC,
4113 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004114 int ret;
4115
Harvey Harrison3110ff82008-05-02 13:42:44 -07004116 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004117 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004118 fl->fl_u.nfs4_fl.owner,
4119 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004120 if (data == NULL)
4121 return -ENOMEM;
4122 if (IS_SETLKW(cmd))
4123 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004124 if (recovery_type > NFS_LOCK_NEW) {
4125 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004126 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004127 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4128 }
Trond Myklebust5138fde2007-07-14 15:40:01 -04004129 msg.rpc_argp = &data->arg,
4130 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004131 task_setup_data.callback_data = data;
4132 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004133 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004134 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004135 ret = nfs4_wait_for_completion_rpc_task(task);
4136 if (ret == 0) {
4137 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004138 if (ret)
4139 nfs4_handle_setlk_error(data->server, data->lsp,
4140 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004141 } else
4142 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004143 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004144 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004145 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146}
4147
4148static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4149{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004150 struct nfs_server *server = NFS_SERVER(state->inode);
4151 struct nfs4_exception exception = { };
4152 int err;
4153
4154 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004155 /* Cache the lock if possible... */
4156 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4157 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004158 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Jeff Layton2c643482010-01-07 09:42:03 -05004159 if (err != -NFS4ERR_DELAY && err != -EKEYEXPIRED)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004160 break;
4161 nfs4_handle_exception(server, err, &exception);
4162 } while (exception.retry);
4163 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164}
4165
4166static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4167{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004168 struct nfs_server *server = NFS_SERVER(state->inode);
4169 struct nfs4_exception exception = { };
4170 int err;
4171
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004172 err = nfs4_set_lock_state(state, request);
4173 if (err != 0)
4174 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004175 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004176 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4177 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004178 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004179 switch (err) {
4180 default:
4181 goto out;
4182 case -NFS4ERR_GRACE:
4183 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05004184 case -EKEYEXPIRED:
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004185 nfs4_handle_exception(server, err, &exception);
4186 err = 0;
4187 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004188 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004189out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004190 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191}
4192
4193static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4194{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004195 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004196 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004197 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004199 if ((fl_flags & FL_POSIX) &&
4200 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4201 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004202 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004203 status = nfs4_set_lock_state(state, request);
4204 if (status != 0)
4205 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004206 request->fl_flags |= FL_ACCESS;
4207 status = do_vfs_lock(request->fl_file, request);
4208 if (status < 0)
4209 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004210 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004211 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004212 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004213 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004214 request->fl_flags = fl_flags & ~FL_SLEEP;
4215 status = do_vfs_lock(request->fl_file, request);
4216 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004217 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004218 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004219 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004220 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004221 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004222 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004223 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004224 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004225out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004226 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004227out:
4228 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 return status;
4230}
4231
4232static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4233{
4234 struct nfs4_exception exception = { };
4235 int err;
4236
4237 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004238 err = _nfs4_proc_setlk(state, cmd, request);
4239 if (err == -NFS4ERR_DENIED)
4240 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004242 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 } while (exception.retry);
4244 return err;
4245}
4246
4247static int
4248nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4249{
4250 struct nfs_open_context *ctx;
4251 struct nfs4_state *state;
4252 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4253 int status;
4254
4255 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004256 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 state = ctx->state;
4258
4259 if (request->fl_start < 0 || request->fl_end < 0)
4260 return -EINVAL;
4261
Trond Myklebustd9531262009-07-21 19:22:38 -04004262 if (IS_GETLK(cmd)) {
4263 if (state != NULL)
4264 return nfs4_proc_getlk(state, F_GETLK, request);
4265 return 0;
4266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267
4268 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4269 return -EINVAL;
4270
Trond Myklebustd9531262009-07-21 19:22:38 -04004271 if (request->fl_type == F_UNLCK) {
4272 if (state != NULL)
4273 return nfs4_proc_unlck(state, cmd, request);
4274 return 0;
4275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276
Trond Myklebustd9531262009-07-21 19:22:38 -04004277 if (state == NULL)
4278 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 do {
4280 status = nfs4_proc_setlk(state, cmd, request);
4281 if ((status != -EAGAIN) || IS_SETLK(cmd))
4282 break;
4283 timeout = nfs4_set_lock_task_retry(timeout);
4284 status = -ERESTARTSYS;
4285 if (signalled())
4286 break;
4287 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 return status;
4289}
4290
Trond Myklebust888e6942005-11-04 15:38:11 -05004291int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4292{
4293 struct nfs_server *server = NFS_SERVER(state->inode);
4294 struct nfs4_exception exception = { };
4295 int err;
4296
4297 err = nfs4_set_lock_state(state, fl);
4298 if (err != 0)
4299 goto out;
4300 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004301 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004302 switch (err) {
4303 default:
4304 printk(KERN_ERR "%s: unhandled error %d.\n",
4305 __func__, err);
4306 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004307 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004308 goto out;
4309 case -NFS4ERR_EXPIRED:
4310 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004311 case -NFS4ERR_STALE_STATEID:
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004312 case -NFS4ERR_BADSESSION:
4313 case -NFS4ERR_BADSLOT:
4314 case -NFS4ERR_BAD_HIGH_SLOT:
4315 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4316 case -NFS4ERR_DEADSESSION:
Trond Myklebustd5122202009-06-17 13:22:58 -07004317 nfs4_schedule_state_recovery(server->nfs_client);
4318 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004319 case -ERESTARTSYS:
4320 /*
4321 * The show must go on: exit, but mark the
4322 * stateid as needing recovery.
4323 */
4324 case -NFS4ERR_ADMIN_REVOKED:
4325 case -NFS4ERR_BAD_STATEID:
4326 case -NFS4ERR_OPENMODE:
4327 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4328 err = 0;
4329 goto out;
4330 case -ENOMEM:
4331 case -NFS4ERR_DENIED:
4332 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4333 err = 0;
4334 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004335 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05004336 case -EKEYEXPIRED:
Trond Myklebustd5122202009-06-17 13:22:58 -07004337 break;
4338 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004339 err = nfs4_handle_exception(server, err, &exception);
4340 } while (exception.retry);
4341out:
4342 return err;
4343}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004344
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004345static void nfs4_release_lockowner_release(void *calldata)
4346{
4347 kfree(calldata);
4348}
4349
4350const struct rpc_call_ops nfs4_release_lockowner_ops = {
4351 .rpc_release = nfs4_release_lockowner_release,
4352};
4353
4354void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4355{
4356 struct nfs_server *server = lsp->ls_state->owner->so_server;
4357 struct nfs_release_lockowner_args *args;
4358 struct rpc_message msg = {
4359 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4360 };
4361
4362 if (server->nfs_client->cl_mvops->minor_version != 0)
4363 return;
4364 args = kmalloc(sizeof(*args), GFP_NOFS);
4365 if (!args)
4366 return;
4367 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4368 args->lock_owner.id = lsp->ls_id.id;
4369 msg.rpc_argp = args;
4370 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4371}
4372
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004373#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4374
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004375int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4376 size_t buflen, int flags)
4377{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004378 struct inode *inode = dentry->d_inode;
4379
4380 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4381 return -EOPNOTSUPP;
4382
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004383 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004384}
4385
4386/* The getxattr man page suggests returning -ENODATA for unknown attributes,
4387 * and that's what we'll do for e.g. user attributes that haven't been set.
4388 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4389 * attributes in kernel-managed attribute namespaces. */
4390ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4391 size_t buflen)
4392{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004393 struct inode *inode = dentry->d_inode;
4394
4395 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4396 return -EOPNOTSUPP;
4397
4398 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004399}
4400
4401ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4402{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004403 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004404
J. Bruce Fields096455a2006-03-20 23:23:42 -05004405 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4406 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004407 if (buf && buflen < len)
4408 return -ERANGE;
4409 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004410 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4411 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004412}
4413
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004414static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4415{
4416 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4417 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4418 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4419 return;
4420
4421 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4422 NFS_ATTR_FATTR_NLINK;
4423 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4424 fattr->nlink = 2;
4425}
4426
Trond Myklebust56659e92007-07-17 21:52:39 -04004427int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004428 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004429{
4430 struct nfs_server *server = NFS_SERVER(dir);
4431 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004432 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4433 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004434 };
4435 struct nfs4_fs_locations_arg args = {
4436 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004437 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004438 .page = page,
4439 .bitmask = bitmask,
4440 };
Benny Halevy22958462009-04-01 09:22:02 -04004441 struct nfs4_fs_locations_res res = {
4442 .fs_locations = fs_locations,
4443 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004444 struct rpc_message msg = {
4445 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4446 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004447 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004448 };
4449 int status;
4450
Harvey Harrison3110ff82008-05-02 13:42:44 -07004451 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004452 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004453 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004454 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04004455 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004456 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004457 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004458 return status;
4459}
4460
Andy Adamson557134a2009-04-01 09:21:53 -04004461#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004462/*
4463 * nfs4_proc_exchange_id()
4464 *
4465 * Since the clientid has expired, all compounds using sessions
4466 * associated with the stale clientid will be returning
4467 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4468 * be in some phase of session reset.
4469 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004470int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004471{
4472 nfs4_verifier verifier;
4473 struct nfs41_exchange_id_args args = {
4474 .client = clp,
4475 .flags = clp->cl_exchange_flags,
4476 };
4477 struct nfs41_exchange_id_res res = {
4478 .client = clp,
4479 };
4480 int status;
4481 struct rpc_message msg = {
4482 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4483 .rpc_argp = &args,
4484 .rpc_resp = &res,
4485 .rpc_cred = cred,
4486 };
4487 __be32 *p;
4488
4489 dprintk("--> %s\n", __func__);
4490 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004491
Alexandros Batsakis7b183d02009-12-05 13:33:25 -05004492 /* Remove server-only flags */
4493 args.flags &= ~EXCHGID4_FLAG_CONFIRMED_R;
4494
Benny Halevy99fe60d2009-04-01 09:22:29 -04004495 p = (u32 *)verifier.data;
4496 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4497 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4498 args.verifier = &verifier;
4499
4500 while (1) {
4501 args.id_len = scnprintf(args.id, sizeof(args.id),
4502 "%s/%s %u",
4503 clp->cl_ipaddr,
4504 rpc_peeraddr2str(clp->cl_rpcclient,
4505 RPC_DISPLAY_ADDR),
4506 clp->cl_id_uniquifier);
4507
4508 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4509
Andy Adamson180b62a2010-03-02 13:19:36 -05004510 if (status != -NFS4ERR_CLID_INUSE)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004511 break;
4512
4513 if (signalled())
4514 break;
4515
4516 if (++clp->cl_id_uniquifier == 0)
4517 break;
4518 }
4519
4520 dprintk("<-- %s status= %d\n", __func__, status);
4521 return status;
4522}
4523
Andy Adamson2050f0c2009-04-01 09:22:30 -04004524struct nfs4_get_lease_time_data {
4525 struct nfs4_get_lease_time_args *args;
4526 struct nfs4_get_lease_time_res *res;
4527 struct nfs_client *clp;
4528};
4529
4530static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4531 void *calldata)
4532{
4533 int ret;
4534 struct nfs4_get_lease_time_data *data =
4535 (struct nfs4_get_lease_time_data *)calldata;
4536
4537 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004538 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004539 /* just setup sequence, do not trigger session recovery
4540 since we're invoked within one */
4541 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004542 &data->args->la_seq_args,
4543 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004544
4545 BUG_ON(ret == -EAGAIN);
4546 rpc_call_start(task);
4547 dprintk("<-- %s\n", __func__);
4548}
4549
4550/*
4551 * Called from nfs4_state_manager thread for session setup, so don't recover
4552 * from sequence operation or clientid errors.
4553 */
4554static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4555{
4556 struct nfs4_get_lease_time_data *data =
4557 (struct nfs4_get_lease_time_data *)calldata;
4558
4559 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04004560 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4561 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04004562 switch (task->tk_status) {
4563 case -NFS4ERR_DELAY:
4564 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05004565 case -EKEYEXPIRED:
Andy Adamson2050f0c2009-04-01 09:22:30 -04004566 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4567 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4568 task->tk_status = 0;
Trond Myklebust0110ee12009-12-07 09:00:24 -05004569 nfs_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004570 return;
4571 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004572 dprintk("<-- %s\n", __func__);
4573}
4574
4575struct rpc_call_ops nfs4_get_lease_time_ops = {
4576 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4577 .rpc_call_done = nfs4_get_lease_time_done,
4578};
4579
4580int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4581{
4582 struct rpc_task *task;
4583 struct nfs4_get_lease_time_args args;
4584 struct nfs4_get_lease_time_res res = {
4585 .lr_fsinfo = fsinfo,
4586 };
4587 struct nfs4_get_lease_time_data data = {
4588 .args = &args,
4589 .res = &res,
4590 .clp = clp,
4591 };
4592 struct rpc_message msg = {
4593 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4594 .rpc_argp = &args,
4595 .rpc_resp = &res,
4596 };
4597 struct rpc_task_setup task_setup = {
4598 .rpc_client = clp->cl_rpcclient,
4599 .rpc_message = &msg,
4600 .callback_ops = &nfs4_get_lease_time_ops,
4601 .callback_data = &data
4602 };
4603 int status;
4604
Andy Adamson2050f0c2009-04-01 09:22:30 -04004605 dprintk("--> %s\n", __func__);
4606 task = rpc_run_task(&task_setup);
4607
4608 if (IS_ERR(task))
4609 status = PTR_ERR(task);
4610 else {
4611 status = task->tk_status;
4612 rpc_put_task(task);
4613 }
4614 dprintk("<-- %s return %d\n", __func__, status);
4615
4616 return status;
4617}
4618
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004619/*
4620 * Reset a slot table
4621 */
Andy Adamson104aeba2010-01-14 17:45:10 -05004622static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4623 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004624{
Andy Adamson104aeba2010-01-14 17:45:10 -05004625 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004626 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004627 int ret = 0;
4628
Andy Adamson104aeba2010-01-14 17:45:10 -05004629 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4630 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004631
Andy Adamson104aeba2010-01-14 17:45:10 -05004632 /* Does the newly negotiated max_reqs match the existing slot table? */
4633 if (max_reqs != tbl->max_slots) {
4634 ret = -ENOMEM;
4635 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004636 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05004637 if (!new)
4638 goto out;
4639 ret = 0;
4640 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004641 }
4642 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05004643 if (new) {
4644 tbl->slots = new;
4645 tbl->max_slots = max_reqs;
4646 }
4647 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004648 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004649 spin_unlock(&tbl->slot_tbl_lock);
4650 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4651 tbl, tbl->slots, tbl->max_slots);
4652out:
4653 dprintk("<-- %s: return %d\n", __func__, ret);
4654 return ret;
4655}
4656
Andy Adamsonfc931582009-04-01 09:22:31 -04004657/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004658 * Reset the forechannel and backchannel slot tables
4659 */
4660static int nfs4_reset_slot_tables(struct nfs4_session *session)
4661{
4662 int status;
4663
4664 status = nfs4_reset_slot_table(&session->fc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004665 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004666 if (status)
4667 return status;
4668
4669 status = nfs4_reset_slot_table(&session->bc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004670 session->bc_attrs.max_reqs, 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004671 return status;
4672}
4673
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004674/* Destroy the slot table */
4675static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4676{
4677 if (session->fc_slot_table.slots != NULL) {
4678 kfree(session->fc_slot_table.slots);
4679 session->fc_slot_table.slots = NULL;
4680 }
4681 if (session->bc_slot_table.slots != NULL) {
4682 kfree(session->bc_slot_table.slots);
4683 session->bc_slot_table.slots = NULL;
4684 }
4685 return;
4686}
4687
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004688/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004689 * Initialize slot table
4690 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004691static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4692 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04004693{
Andy Adamsonfc931582009-04-01 09:22:31 -04004694 struct nfs4_slot *slot;
4695 int ret = -ENOMEM;
4696
4697 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4698
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004699 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04004700
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004701 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04004702 if (!slot)
4703 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04004704 ret = 0;
4705
4706 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04004707 tbl->max_slots = max_slots;
4708 tbl->slots = slot;
4709 tbl->highest_used_slotid = -1; /* no slot is currently used */
4710 spin_unlock(&tbl->slot_tbl_lock);
4711 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4712 tbl, tbl->slots, tbl->max_slots);
4713out:
4714 dprintk("<-- %s: return %d\n", __func__, ret);
4715 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04004716}
4717
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004718/*
4719 * Initialize the forechannel and backchannel tables
4720 */
4721static int nfs4_init_slot_tables(struct nfs4_session *session)
4722{
Trond Myklebustf26468f2009-12-05 19:32:11 -05004723 struct nfs4_slot_table *tbl;
4724 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004725
Trond Myklebustf26468f2009-12-05 19:32:11 -05004726 tbl = &session->fc_slot_table;
4727 if (tbl->slots == NULL) {
4728 status = nfs4_init_slot_table(tbl,
4729 session->fc_attrs.max_reqs, 1);
4730 if (status)
4731 return status;
4732 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004733
Trond Myklebustf26468f2009-12-05 19:32:11 -05004734 tbl = &session->bc_slot_table;
4735 if (tbl->slots == NULL) {
4736 status = nfs4_init_slot_table(tbl,
4737 session->bc_attrs.max_reqs, 0);
4738 if (status)
4739 nfs4_destroy_slot_tables(session);
4740 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004741
4742 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04004743}
4744
4745struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4746{
4747 struct nfs4_session *session;
4748 struct nfs4_slot_table *tbl;
4749
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004750 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04004751 if (!session)
4752 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04004753
Andy Adamsonea028ac2009-12-04 15:55:38 -05004754 init_completion(&session->complete);
Andy Adamson76db6d92009-04-01 09:22:38 -04004755
Andy Adamson557134a2009-04-01 09:21:53 -04004756 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004757 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04004758 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004759 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004760
4761 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004762 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004763 spin_lock_init(&tbl->slot_tbl_lock);
4764 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4765
Trond Myklebust1055d762010-06-16 09:52:27 -04004766 session->session_state = 1<<NFS4_SESSION_INITING;
4767
Andy Adamson557134a2009-04-01 09:21:53 -04004768 session->clp = clp;
4769 return session;
4770}
4771
4772void nfs4_destroy_session(struct nfs4_session *session)
4773{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04004774 nfs4_proc_destroy_session(session);
4775 dprintk("%s Destroy backchannel for xprt %p\n",
4776 __func__, session->clp->cl_rpcclient->cl_xprt);
4777 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4778 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004779 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04004780 kfree(session);
4781}
4782
Andy Adamsonfc931582009-04-01 09:22:31 -04004783/*
4784 * Initialize the values to be used by the client in CREATE_SESSION
4785 * If nfs4_init_session set the fore channel request and response sizes,
4786 * use them.
4787 *
4788 * Set the back channel max_resp_sz_cached to zero to force the client to
4789 * always set csa_cachethis to FALSE because the current implementation
4790 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4791 */
4792static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4793{
4794 struct nfs4_session *session = args->client->cl_session;
4795 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4796 mxresp_sz = session->fc_attrs.max_resp_sz;
4797
4798 if (mxrqst_sz == 0)
4799 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4800 if (mxresp_sz == 0)
4801 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4802 /* Fore channel attributes */
4803 args->fc_attrs.headerpadsz = 0;
4804 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4805 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04004806 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4807 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4808
4809 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05004810 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04004811 __func__,
4812 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05004813 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04004814
4815 /* Back channel attributes */
4816 args->bc_attrs.headerpadsz = 0;
4817 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4818 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4819 args->bc_attrs.max_resp_sz_cached = 0;
4820 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4821 args->bc_attrs.max_reqs = 1;
4822
4823 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4824 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4825 __func__,
4826 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4827 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4828 args->bc_attrs.max_reqs);
4829}
4830
Andy Adamson8d353012009-04-01 09:22:32 -04004831static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
4832{
4833 if (rcvd <= sent)
4834 return 0;
4835 printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
4836 "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
4837 return -EINVAL;
4838}
4839
4840#define _verify_fore_channel_attr(_name_) \
4841 _verify_channel_attr("fore", #_name_, \
4842 args->fc_attrs._name_, \
4843 session->fc_attrs._name_)
4844
4845#define _verify_back_channel_attr(_name_) \
4846 _verify_channel_attr("back", #_name_, \
4847 args->bc_attrs._name_, \
4848 session->bc_attrs._name_)
4849
4850/*
4851 * The server is not allowed to increase the fore channel header pad size,
4852 * maximum response size, or maximum number of operations.
4853 *
4854 * The back channel attributes are only negotiatied down: We send what the
4855 * (back channel) server insists upon.
4856 */
4857static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4858 struct nfs4_session *session)
4859{
4860 int ret = 0;
4861
4862 ret |= _verify_fore_channel_attr(headerpadsz);
4863 ret |= _verify_fore_channel_attr(max_resp_sz);
4864 ret |= _verify_fore_channel_attr(max_ops);
4865
4866 ret |= _verify_back_channel_attr(headerpadsz);
4867 ret |= _verify_back_channel_attr(max_rqst_sz);
4868 ret |= _verify_back_channel_attr(max_resp_sz);
4869 ret |= _verify_back_channel_attr(max_resp_sz_cached);
4870 ret |= _verify_back_channel_attr(max_ops);
4871 ret |= _verify_back_channel_attr(max_reqs);
4872
4873 return ret;
4874}
4875
Andy Adamsonfc931582009-04-01 09:22:31 -04004876static int _nfs4_proc_create_session(struct nfs_client *clp)
4877{
4878 struct nfs4_session *session = clp->cl_session;
4879 struct nfs41_create_session_args args = {
4880 .client = clp,
4881 .cb_program = NFS4_CALLBACK,
4882 };
4883 struct nfs41_create_session_res res = {
4884 .client = clp,
4885 };
4886 struct rpc_message msg = {
4887 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4888 .rpc_argp = &args,
4889 .rpc_resp = &res,
4890 };
4891 int status;
4892
4893 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04004894 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04004895
4896 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4897
Andy Adamson8d353012009-04-01 09:22:32 -04004898 if (!status)
4899 /* Verify the session's negotiated channel_attrs values */
4900 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04004901 if (!status) {
4902 /* Increment the clientid slot sequence id */
4903 clp->cl_seqid++;
4904 }
4905
4906 return status;
4907}
4908
4909/*
4910 * Issues a CREATE_SESSION operation to the server.
4911 * It is the responsibility of the caller to verify the session is
4912 * expired before calling this routine.
4913 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05004914int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04004915{
4916 int status;
4917 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04004918 struct nfs4_session *session = clp->cl_session;
4919
4920 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
4921
4922 status = _nfs4_proc_create_session(clp);
4923 if (status)
4924 goto out;
4925
Trond Myklebustf26468f2009-12-05 19:32:11 -05004926 /* Init and reset the fore channel */
4927 status = nfs4_init_slot_tables(session);
4928 dprintk("slot table initialization returned %d\n", status);
4929 if (status)
4930 goto out;
4931 status = nfs4_reset_slot_tables(session);
4932 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04004933 if (status)
4934 goto out;
4935
4936 ptr = (unsigned *)&session->sess_id.data[0];
4937 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
4938 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04004939out:
4940 dprintk("<-- %s\n", __func__);
4941 return status;
4942}
4943
Andy Adamson0f3e66c2009-04-01 09:22:34 -04004944/*
4945 * Issue the over-the-wire RPC DESTROY_SESSION.
4946 * The caller must serialize access to this routine.
4947 */
4948int nfs4_proc_destroy_session(struct nfs4_session *session)
4949{
4950 int status = 0;
4951 struct rpc_message msg;
4952
4953 dprintk("--> nfs4_proc_destroy_session\n");
4954
4955 /* session is still being setup */
4956 if (session->clp->cl_cons_state != NFS_CS_READY)
4957 return status;
4958
4959 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
4960 msg.rpc_argp = session;
4961 msg.rpc_resp = NULL;
4962 msg.rpc_cred = NULL;
4963 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4964
4965 if (status)
4966 printk(KERN_WARNING
4967 "Got error %d from the server on DESTROY_SESSION. "
4968 "Session has been destroyed regardless...\n", status);
4969
4970 dprintk("<-- nfs4_proc_destroy_session\n");
4971 return status;
4972}
4973
Trond Myklebustfccba802009-07-21 16:48:07 -04004974int nfs4_init_session(struct nfs_server *server)
4975{
4976 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05004977 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05004978 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04004979 int ret;
4980
4981 if (!nfs4_has_session(clp))
4982 return 0;
4983
Trond Myklebust1055d762010-06-16 09:52:27 -04004984 session = clp->cl_session;
4985 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
4986 return 0;
4987
Andy Adamson68bf05e2009-12-15 12:55:02 -05004988 rsize = server->rsize;
4989 if (rsize == 0)
4990 rsize = NFS_MAX_FILE_IO_SIZE;
4991 wsize = server->wsize;
4992 if (wsize == 0)
4993 wsize = NFS_MAX_FILE_IO_SIZE;
4994
Andy Adamson68bf05e2009-12-15 12:55:02 -05004995 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
4996 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05004997
Trond Myklebustfccba802009-07-21 16:48:07 -04004998 ret = nfs4_recover_expired_lease(server);
4999 if (!ret)
5000 ret = nfs4_check_client_ready(clp);
5001 return ret;
5002}
5003
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005004/*
5005 * Renew the cl_session lease.
5006 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005007struct nfs4_sequence_data {
5008 struct nfs_client *clp;
5009 struct nfs4_sequence_args args;
5010 struct nfs4_sequence_res res;
5011};
5012
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005013static void nfs41_sequence_release(void *data)
5014{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005015 struct nfs4_sequence_data *calldata = data;
5016 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005017
Alexandros Batsakis71358402010-02-05 03:45:05 -08005018 if (atomic_read(&clp->cl_count) > 1)
5019 nfs4_schedule_state_renewal(clp);
5020 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005021 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005022}
5023
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005024static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5025{
5026 switch(task->tk_status) {
5027 case -NFS4ERR_DELAY:
5028 case -EKEYEXPIRED:
5029 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5030 return -EAGAIN;
5031 default:
5032 nfs4_schedule_state_recovery(clp);
5033 }
5034 return 0;
5035}
5036
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005037static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005038{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005039 struct nfs4_sequence_data *calldata = data;
5040 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005041
Trond Myklebust14516c32010-07-31 14:29:06 -04005042 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5043 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005044
5045 if (task->tk_status < 0) {
5046 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005047 if (atomic_read(&clp->cl_count) == 1)
5048 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005049
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005050 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5051 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005052 return;
5053 }
5054 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005055 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005056out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005057 dprintk("<-- %s\n", __func__);
5058}
5059
5060static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5061{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005062 struct nfs4_sequence_data *calldata = data;
5063 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005064 struct nfs4_sequence_args *args;
5065 struct nfs4_sequence_res *res;
5066
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005067 args = task->tk_msg.rpc_argp;
5068 res = task->tk_msg.rpc_resp;
5069
Trond Myklebust035168a2010-06-16 09:52:26 -04005070 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005071 return;
5072 rpc_call_start(task);
5073}
5074
5075static const struct rpc_call_ops nfs41_sequence_ops = {
5076 .rpc_call_done = nfs41_sequence_call_done,
5077 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005078 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005079};
5080
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005081static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005082{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005083 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005084 struct rpc_message msg = {
5085 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5086 .rpc_cred = cred,
5087 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005088 struct rpc_task_setup task_setup_data = {
5089 .rpc_client = clp->cl_rpcclient,
5090 .rpc_message = &msg,
5091 .callback_ops = &nfs41_sequence_ops,
5092 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5093 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005094
Alexandros Batsakis71358402010-02-05 03:45:05 -08005095 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005096 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005097 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005098 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005099 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005100 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005101 }
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005102 msg.rpc_argp = &calldata->args;
5103 msg.rpc_resp = &calldata->res;
5104 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005105 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005106
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005107 return rpc_run_task(&task_setup_data);
5108}
5109
5110static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5111{
5112 struct rpc_task *task;
5113 int ret = 0;
5114
5115 task = _nfs41_proc_sequence(clp, cred);
5116 if (IS_ERR(task))
5117 ret = PTR_ERR(task);
5118 else
5119 rpc_put_task(task);
5120 dprintk("<-- %s status=%d\n", __func__, ret);
5121 return ret;
5122}
5123
5124static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5125{
5126 struct rpc_task *task;
5127 int ret;
5128
5129 task = _nfs41_proc_sequence(clp, cred);
5130 if (IS_ERR(task)) {
5131 ret = PTR_ERR(task);
5132 goto out;
5133 }
5134 ret = rpc_wait_for_completion_task(task);
5135 if (!ret)
5136 ret = task->tk_status;
5137 rpc_put_task(task);
5138out:
5139 dprintk("<-- %s status=%d\n", __func__, ret);
5140 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005141}
5142
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005143struct nfs4_reclaim_complete_data {
5144 struct nfs_client *clp;
5145 struct nfs41_reclaim_complete_args arg;
5146 struct nfs41_reclaim_complete_res res;
5147};
5148
5149static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5150{
5151 struct nfs4_reclaim_complete_data *calldata = data;
5152
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005153 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005154 if (nfs41_setup_sequence(calldata->clp->cl_session,
5155 &calldata->arg.seq_args,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005156 &calldata->res.seq_res, 0, task))
5157 return;
5158
5159 rpc_call_start(task);
5160}
5161
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005162static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5163{
5164 switch(task->tk_status) {
5165 case 0:
5166 case -NFS4ERR_COMPLETE_ALREADY:
5167 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5168 break;
5169 case -NFS4ERR_DELAY:
5170 case -EKEYEXPIRED:
5171 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5172 return -EAGAIN;
5173 default:
5174 nfs4_schedule_state_recovery(clp);
5175 }
5176 return 0;
5177}
5178
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005179static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5180{
5181 struct nfs4_reclaim_complete_data *calldata = data;
5182 struct nfs_client *clp = calldata->clp;
5183 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5184
5185 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005186 if (!nfs41_sequence_done(task, res))
5187 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005188
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005189 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5190 rpc_restart_call_prepare(task);
5191 return;
5192 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005193 dprintk("<-- %s\n", __func__);
5194}
5195
5196static void nfs4_free_reclaim_complete_data(void *data)
5197{
5198 struct nfs4_reclaim_complete_data *calldata = data;
5199
5200 kfree(calldata);
5201}
5202
5203static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5204 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5205 .rpc_call_done = nfs4_reclaim_complete_done,
5206 .rpc_release = nfs4_free_reclaim_complete_data,
5207};
5208
5209/*
5210 * Issue a global reclaim complete.
5211 */
5212static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5213{
5214 struct nfs4_reclaim_complete_data *calldata;
5215 struct rpc_task *task;
5216 struct rpc_message msg = {
5217 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5218 };
5219 struct rpc_task_setup task_setup_data = {
5220 .rpc_client = clp->cl_rpcclient,
5221 .rpc_message = &msg,
5222 .callback_ops = &nfs4_reclaim_complete_call_ops,
5223 .flags = RPC_TASK_ASYNC,
5224 };
5225 int status = -ENOMEM;
5226
5227 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005228 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005229 if (calldata == NULL)
5230 goto out;
5231 calldata->clp = clp;
5232 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005233
5234 msg.rpc_argp = &calldata->arg;
5235 msg.rpc_resp = &calldata->res;
5236 task_setup_data.callback_data = calldata;
5237 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005238 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005239 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005240 goto out;
5241 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005242 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005243 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005244out:
5245 dprintk("<-- %s status=%d\n", __func__, status);
5246 return status;
5247}
Andy Adamson557134a2009-04-01 09:21:53 -04005248#endif /* CONFIG_NFS_V4_1 */
5249
Andy Adamson591d71c2009-04-01 09:22:47 -04005250struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005251 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005252 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253 .recover_open = nfs4_open_reclaim,
5254 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04005255 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005256 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257};
5258
Andy Adamson591d71c2009-04-01 09:22:47 -04005259#if defined(CONFIG_NFS_V4_1)
5260struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5261 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5262 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5263 .recover_open = nfs4_open_reclaim,
5264 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05005265 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005266 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005267 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04005268};
5269#endif /* CONFIG_NFS_V4_1 */
5270
5271struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005272 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005273 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 .recover_open = nfs4_open_expired,
5275 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04005276 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005277 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278};
5279
Andy Adamson591d71c2009-04-01 09:22:47 -04005280#if defined(CONFIG_NFS_V4_1)
5281struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5282 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5283 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5284 .recover_open = nfs4_open_expired,
5285 .recover_lock = nfs4_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05005286 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005287 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04005288};
5289#endif /* CONFIG_NFS_V4_1 */
5290
Benny Halevy29fba382009-04-01 09:22:44 -04005291struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5292 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04005293 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005294 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04005295};
5296
5297#if defined(CONFIG_NFS_V4_1)
5298struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5299 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04005300 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005301 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04005302};
5303#endif
5304
Trond Myklebust97dc1352010-06-16 09:52:26 -04005305static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
5306 .minor_version = 0,
5307 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04005308 .validate_stateid = nfs4_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005309 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
5310 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
5311 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005312};
5313
5314#if defined(CONFIG_NFS_V4_1)
5315static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
5316 .minor_version = 1,
5317 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04005318 .validate_stateid = nfs41_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005319 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
5320 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
5321 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005322};
5323#endif
5324
Trond Myklebust97dc1352010-06-16 09:52:26 -04005325const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
5326 [0] = &nfs_v4_0_minor_ops,
5327#if defined(CONFIG_NFS_V4_1)
5328 [1] = &nfs_v4_1_minor_ops,
5329#endif
5330};
5331
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005332static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005333 .permission = nfs_permission,
5334 .getattr = nfs_getattr,
5335 .setattr = nfs_setattr,
5336 .getxattr = nfs4_getxattr,
5337 .setxattr = nfs4_setxattr,
5338 .listxattr = nfs4_listxattr,
5339};
5340
David Howells509de812006-08-22 20:06:11 -04005341const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342 .version = 4, /* protocol version */
5343 .dentry_ops = &nfs4_dentry_operations,
5344 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005345 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 .getroot = nfs4_proc_get_root,
5347 .getattr = nfs4_proc_getattr,
5348 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04005349 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 .lookup = nfs4_proc_lookup,
5351 .access = nfs4_proc_access,
5352 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353 .create = nfs4_proc_create,
5354 .remove = nfs4_proc_remove,
5355 .unlink_setup = nfs4_proc_unlink_setup,
5356 .unlink_done = nfs4_proc_unlink_done,
5357 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04005358 .rename_setup = nfs4_proc_rename_setup,
5359 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 .link = nfs4_proc_link,
5361 .symlink = nfs4_proc_symlink,
5362 .mkdir = nfs4_proc_mkdir,
5363 .rmdir = nfs4_proc_remove,
5364 .readdir = nfs4_proc_readdir,
5365 .mknod = nfs4_proc_mknod,
5366 .statfs = nfs4_proc_statfs,
5367 .fsinfo = nfs4_proc_fsinfo,
5368 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04005369 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 .decode_dirent = nfs4_decode_dirent,
5371 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05005372 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005374 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005376 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005378 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04005379 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04005380 .open_context = nfs4_atomic_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381};
5382
5383/*
5384 * Local variables:
5385 * c-basic-offset: 8
5386 * End:
5387 */