blob: aee1bcc1fcc950c512bac1aa695c9379b69f4222 [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 if (state == NULL)
259 break;
260 nfs4_state_mark_reclaim_reboot(clp, state);
261 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust65de8722008-12-23 15:21:44 -0500262 case -NFS4ERR_EXPIRED:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500263 goto do_state_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500264#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400265 case -NFS4ERR_BADSESSION:
266 case -NFS4ERR_BADSLOT:
267 case -NFS4ERR_BAD_HIGH_SLOT:
268 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
269 case -NFS4ERR_DEADSESSION:
270 case -NFS4ERR_SEQ_FALSE_RETRY:
271 case -NFS4ERR_SEQ_MISORDERED:
272 dprintk("%s ERROR: %d Reset session\n", __func__,
273 errorcode);
Andy Adamson0b9e2d42009-12-04 16:02:14 -0500274 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -0400275 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500276 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500277#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500278 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500279 if (exception->timeout > HZ) {
280 /* We have retried a decent amount, time to
281 * fail
282 */
283 ret = -EBUSY;
284 break;
285 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500286 case -NFS4ERR_GRACE:
287 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500288 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500289 ret = nfs4_delay(server->client, &exception->timeout);
290 if (ret != 0)
291 break;
292 case -NFS4ERR_OLD_STATEID:
293 exception->retry = 1;
294 }
295 /* We failed to handle the error */
296 return nfs4_map_errors(ret);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500297do_state_recovery:
298 nfs4_schedule_state_recovery(clp);
299 ret = nfs4_wait_clnt_recover(clp);
300 if (ret == 0)
301 exception->retry = 1;
302 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500303}
304
305
Trond Myklebust452e9352010-07-31 14:29:06 -0400306static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 spin_lock(&clp->cl_lock);
309 if (time_before(clp->cl_last_renewal,timestamp))
310 clp->cl_last_renewal = timestamp;
311 spin_unlock(&clp->cl_lock);
312}
313
Trond Myklebust452e9352010-07-31 14:29:06 -0400314static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
315{
316 do_renew_lease(server->nfs_client, timestamp);
317}
318
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400319#if defined(CONFIG_NFS_V4_1)
320
Benny Halevy510b8172009-04-01 09:22:14 -0400321/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400322 * nfs4_free_slot - free a slot and efficiently update slot table.
323 *
324 * freeing a slot is trivially done by clearing its respective bit
325 * in the bitmap.
326 * If the freed slotid equals highest_used_slotid we want to update it
327 * so that the server would be able to size down the slot table if needed,
328 * otherwise we know that the highest_used_slotid is still in use.
329 * When updating highest_used_slotid there may be "holes" in the bitmap
330 * so we need to scan down from highest_used_slotid to 0 looking for the now
331 * highest slotid in use.
332 * If none found, highest_used_slotid is set to -1.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500333 *
334 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400335 */
336static void
Benny Halevydfb4f3092010-09-24 09:17:01 -0400337nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *free_slot)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400338{
Benny Halevydfb4f3092010-09-24 09:17:01 -0400339 int free_slotid = free_slot - tbl->slots;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400340 int slotid = free_slotid;
341
Benny Halevydfb4f3092010-09-24 09:17:01 -0400342 BUG_ON(slotid < 0 || slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400343 /* clear used bit in bitmap */
344 __clear_bit(slotid, tbl->used_slots);
345
346 /* update highest_used_slotid when it is freed */
347 if (slotid == tbl->highest_used_slotid) {
348 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500349 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400350 tbl->highest_used_slotid = slotid;
351 else
352 tbl->highest_used_slotid = -1;
353 }
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400354 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
355 free_slotid, tbl->highest_used_slotid);
356}
357
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800358/*
359 * Signal state manager thread if session is drained
360 */
361static void nfs41_check_drain_session_complete(struct nfs4_session *ses)
362{
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800363 struct rpc_task *task;
364
Trond Myklebusta2118c32010-06-16 09:52:26 -0400365 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800366 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
367 if (task)
368 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800369 return;
370 }
371
372 if (ses->fc_slot_table.highest_used_slotid != -1)
373 return;
374
375 dprintk("%s COMPLETE: Session Drained\n", __func__);
376 complete(&ses->complete);
377}
378
Trond Myklebustd185a332010-06-16 09:52:25 -0400379static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400380{
381 struct nfs4_slot_table *tbl;
382
Trond Myklebustd185a332010-06-16 09:52:25 -0400383 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400384 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400385 /* just wake up the next guy waiting since
386 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500387 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500388 return;
Andy Adamson13615872009-04-01 09:22:17 -0400389 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500390
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500391 spin_lock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400392 nfs4_free_slot(tbl, res->sr_slot);
Trond Myklebustd185a332010-06-16 09:52:25 -0400393 nfs41_check_drain_session_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500394 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400395 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400396}
397
Trond Myklebust14516c32010-07-31 14:29:06 -0400398static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400399{
400 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400401 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400402
403 /*
404 * sr_status remains 1 if an RPC level error occurred. The server
405 * may or may not have processed the sequence operation..
406 * Proceed as if the server received and processed the sequence
407 * operation.
408 */
409 if (res->sr_status == 1)
410 res->sr_status = NFS_OK;
411
412 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400413 if (!res->sr_slot)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400414 goto out;
415
Andy Adamson691daf32009-12-04 15:55:39 -0500416 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400417 switch (res->sr_status) {
418 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400419 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400420 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400421 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400422 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400423 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500424 /* Check sequence flags */
Alexandros Batsakis71358402010-02-05 03:45:05 -0800425 if (atomic_read(&clp->cl_count) > 1)
426 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust14516c32010-07-31 14:29:06 -0400427 break;
428 case -NFS4ERR_DELAY:
429 /* The server detected a resend of the RPC call and
430 * returned NFS4ERR_DELAY as per Section 2.10.6.2
431 * of RFC5661.
432 */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400433 dprintk("%s: slot=%ld seq=%d: Operation in progress\n",
434 __func__,
435 res->sr_slot - res->sr_session->fc_slot_table.slots,
436 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400437 goto out_retry;
438 default:
439 /* Just update the slot sequence no. */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400440 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400441 }
442out:
443 /* The session may be reset by one of the error handlers. */
444 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400445 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400446 return 1;
447out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400448 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400449 goto out;
450 rpc_delay(task, NFS4_POLL_RETRY_MAX);
451 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400452}
453
Trond Myklebust14516c32010-07-31 14:29:06 -0400454static int nfs4_sequence_done(struct rpc_task *task,
455 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400456{
Trond Myklebust14516c32010-07-31 14:29:06 -0400457 if (res->sr_session == NULL)
458 return 1;
459 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400460}
461
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400462/*
Benny Halevy510b8172009-04-01 09:22:14 -0400463 * nfs4_find_slot - efficiently look for a free slot
464 *
465 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
466 * If found, we mark the slot as used, update the highest_used_slotid,
467 * and respectively set up the sequence operation args.
468 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400469 *
470 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400471 */
472static u8
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800473nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400474{
475 int slotid;
476 u8 ret_id = NFS4_MAX_SLOT_TABLE;
477 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
478
Benny Halevy510b8172009-04-01 09:22:14 -0400479 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
480 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
481 tbl->max_slots);
482 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
483 if (slotid >= tbl->max_slots)
484 goto out;
485 __set_bit(slotid, tbl->used_slots);
486 if (slotid > tbl->highest_used_slotid)
487 tbl->highest_used_slotid = slotid;
488 ret_id = slotid;
489out:
490 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
491 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400492 return ret_id;
493}
494
Andy Adamsonce5039c2009-04-01 09:22:13 -0400495static int nfs41_setup_sequence(struct nfs4_session *session,
496 struct nfs4_sequence_args *args,
497 struct nfs4_sequence_res *res,
498 int cache_reply,
499 struct rpc_task *task)
500{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400501 struct nfs4_slot *slot;
502 struct nfs4_slot_table *tbl;
503 u8 slotid;
504
505 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400506 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400507 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400508 return 0;
509
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400510 tbl = &session->fc_slot_table;
511
512 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400513 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800514 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamsonea028ac2009-12-04 15:55:38 -0500515 /*
516 * The state manager will wait until the slot table is empty.
517 * Schedule the reset thread
518 */
Andy Adamson05f0d232009-12-04 15:55:37 -0500519 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400520 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500521 dprintk("%s Schedule Session Reset\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500522 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400523 }
524
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800525 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
526 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
527 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
528 spin_unlock(&tbl->slot_tbl_lock);
529 dprintk("%s enforce FIFO order\n", __func__);
530 return -EAGAIN;
531 }
532
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800533 slotid = nfs4_find_slot(tbl);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400534 if (slotid == NFS4_MAX_SLOT_TABLE) {
535 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
536 spin_unlock(&tbl->slot_tbl_lock);
537 dprintk("<-- %s: no free slots\n", __func__);
538 return -EAGAIN;
539 }
540 spin_unlock(&tbl->slot_tbl_lock);
541
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800542 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400543 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400544 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400545 args->sa_slotid = slotid;
546 args->sa_cache_this = cache_reply;
547
548 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
549
Benny Halevy99fe60d2009-04-01 09:22:29 -0400550 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400551 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400552 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400553 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400554 /*
555 * sr_status is only set in decode_sequence, and so will remain
556 * set to 1 if an rpc level failure occurs.
557 */
558 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400559 return 0;
560}
561
Trond Myklebust035168ab2010-06-16 09:52:26 -0400562int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400563 struct nfs4_sequence_args *args,
564 struct nfs4_sequence_res *res,
565 int cache_reply,
566 struct rpc_task *task)
567{
Trond Myklebust035168ab2010-06-16 09:52:26 -0400568 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400569 int ret = 0;
570
Trond Myklebust035168ab2010-06-16 09:52:26 -0400571 if (session == NULL) {
572 args->sa_session = NULL;
573 res->sr_session = NULL;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400574 goto out;
Trond Myklebust035168ab2010-06-16 09:52:26 -0400575 }
576
Benny Halevydfb4f3092010-09-24 09:17:01 -0400577 dprintk("--> %s clp %p session %p sr_slot %ld\n",
578 __func__, session->clp, session, res->sr_slot ?
579 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168ab2010-06-16 09:52:26 -0400580
581 ret = nfs41_setup_sequence(session, args, res, cache_reply,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400582 task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400583out:
584 dprintk("<-- %s status=%d\n", __func__, ret);
585 return ret;
586}
587
588struct nfs41_call_sync_data {
Trond Myklebust035168ab2010-06-16 09:52:26 -0400589 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400590 struct nfs4_sequence_args *seq_args;
591 struct nfs4_sequence_res *seq_res;
592 int cache_reply;
593};
594
595static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
596{
597 struct nfs41_call_sync_data *data = calldata;
598
Trond Myklebust035168ab2010-06-16 09:52:26 -0400599 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
600
601 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400602 data->seq_res, data->cache_reply, task))
603 return;
604 rpc_call_start(task);
605}
606
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800607static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
608{
609 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
610 nfs41_call_sync_prepare(task, calldata);
611}
612
Andy Adamson69ab40c2009-04-01 09:22:19 -0400613static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
614{
615 struct nfs41_call_sync_data *data = calldata;
616
Trond Myklebust14516c32010-07-31 14:29:06 -0400617 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400618}
619
Andy Adamsonce5039c2009-04-01 09:22:13 -0400620struct rpc_call_ops nfs41_call_sync_ops = {
621 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400622 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400623};
624
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800625struct rpc_call_ops nfs41_call_priv_sync_ops = {
626 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
627 .rpc_call_done = nfs41_call_sync_done,
628};
629
Trond Myklebust035168ab2010-06-16 09:52:26 -0400630static int nfs4_call_sync_sequence(struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400631 struct rpc_message *msg,
632 struct nfs4_sequence_args *args,
633 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800634 int cache_reply,
635 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400636{
637 int ret;
638 struct rpc_task *task;
639 struct nfs41_call_sync_data data = {
Trond Myklebust035168ab2010-06-16 09:52:26 -0400640 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400641 .seq_args = args,
642 .seq_res = res,
643 .cache_reply = cache_reply,
644 };
645 struct rpc_task_setup task_setup = {
Trond Myklebust035168ab2010-06-16 09:52:26 -0400646 .rpc_client = server->client,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400647 .rpc_message = msg,
648 .callback_ops = &nfs41_call_sync_ops,
649 .callback_data = &data
650 };
651
Benny Halevydfb4f3092010-09-24 09:17:01 -0400652 res->sr_slot = NULL;
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800653 if (privileged)
654 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400655 task = rpc_run_task(&task_setup);
656 if (IS_ERR(task))
657 ret = PTR_ERR(task);
658 else {
659 ret = task->tk_status;
660 rpc_put_task(task);
661 }
662 return ret;
663}
664
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400665int _nfs4_call_sync_session(struct nfs_server *server,
666 struct rpc_message *msg,
667 struct nfs4_sequence_args *args,
668 struct nfs4_sequence_res *res,
669 int cache_reply)
670{
Trond Myklebust035168ab2010-06-16 09:52:26 -0400671 return nfs4_call_sync_sequence(server, msg, args, res, cache_reply, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400672}
673
Trond Myklebustdf896452010-06-16 09:52:26 -0400674#else
Trond Myklebust14516c32010-07-31 14:29:06 -0400675static int nfs4_sequence_done(struct rpc_task *task,
676 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400677{
Trond Myklebust14516c32010-07-31 14:29:06 -0400678 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400679}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400680#endif /* CONFIG_NFS_V4_1 */
681
682int _nfs4_call_sync(struct nfs_server *server,
683 struct rpc_message *msg,
684 struct nfs4_sequence_args *args,
685 struct nfs4_sequence_res *res,
686 int cache_reply)
687{
Benny Halevyf3752972009-04-01 09:22:04 -0400688 args->sa_session = res->sr_session = NULL;
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400689 return rpc_call_sync(server->client, msg, 0);
690}
691
692#define nfs4_call_sync(server, msg, args, res, cache_reply) \
Trond Myklebust97dc1352010-06-16 09:52:26 -0400693 (server)->nfs_client->cl_mvops->call_sync((server), (msg), &(args)->seq_args, \
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400694 &(res)->seq_res, (cache_reply))
695
Trond Myklebust38478b22006-05-25 01:40:57 -0400696static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
Trond Myklebust38478b22006-05-25 01:40:57 -0400698 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Trond Myklebust38478b22006-05-25 01:40:57 -0400700 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400701 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
702 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400703 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400704 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400705 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100708struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400709 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100710 struct nfs_openargs o_arg;
711 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100712 struct nfs_open_confirmargs c_arg;
713 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100714 struct nfs_fattr f_attr;
715 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400716 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100717 struct dentry *dir;
718 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400719 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100720 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100721 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400722 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100723 int rpc_status;
724 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100725};
726
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400727
728static void nfs4_init_opendata_res(struct nfs4_opendata *p)
729{
730 p->o_res.f_attr = &p->f_attr;
731 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400732 p->o_res.seqid = p->o_arg.seqid;
733 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400734 p->o_res.server = p->o_arg.server;
735 nfs_fattr_init(&p->f_attr);
736 nfs_fattr_init(&p->dir_attr);
737}
738
Trond Myklebustad389da2007-06-05 12:30:00 -0400739static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500740 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400741 const struct iattr *attrs,
742 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100743{
Trond Myklebustad389da2007-06-05 12:30:00 -0400744 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100745 struct inode *dir = parent->d_inode;
746 struct nfs_server *server = NFS_SERVER(dir);
747 struct nfs4_opendata *p;
748
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400749 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100750 if (p == NULL)
751 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400752 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100753 if (p->o_arg.seqid == NULL)
754 goto err_free;
Al Virof6948692010-01-30 13:51:04 -0500755 path_get(path);
756 p->path = *path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100757 p->dir = parent;
758 p->owner = sp;
759 atomic_inc(&sp->so_count);
760 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500761 p->o_arg.open_flags = flags;
762 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400763 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400764 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400765 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100766 p->o_arg.server = server;
767 p->o_arg.bitmask = server->attr_bitmask;
768 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400769 if (flags & O_CREAT) {
770 u32 *s;
771
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100772 p->o_arg.u.attrs = &p->attrs;
773 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400774 s = (u32 *) p->o_arg.u.verifier.data;
775 s[0] = jiffies;
776 s[1] = current->pid;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100777 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100778 p->c_arg.fh = &p->o_res.fh;
779 p->c_arg.stateid = &p->o_res.stateid;
780 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400781 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400782 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100783 return p;
784err_free:
785 kfree(p);
786err:
787 dput(parent);
788 return NULL;
789}
790
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400791static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100792{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400793 struct nfs4_opendata *p = container_of(kref,
794 struct nfs4_opendata, kref);
795
796 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400797 if (p->state != NULL)
798 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400799 nfs4_put_state_owner(p->owner);
800 dput(p->dir);
Jan Blunck31f31db12008-05-02 13:42:45 -0700801 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400802 kfree(p);
803}
804
805static void nfs4_opendata_put(struct nfs4_opendata *p)
806{
807 if (p != NULL)
808 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100809}
810
Trond Myklebust06f814a2006-01-03 09:55:07 +0100811static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
812{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100813 int ret;
814
Trond Myklebust06f814a2006-01-03 09:55:07 +0100815 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100816 return ret;
817}
818
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500819static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400820{
821 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500822
823 if (open_mode & O_EXCL)
824 goto out;
825 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400826 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500827 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
828 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400829 break;
830 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500831 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
832 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400833 break;
834 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500835 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
836 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400837 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500838out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400839 return ret;
840}
841
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500842static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400843{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500844 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400845 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500846 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400847 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500848 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400849 return 1;
850}
851
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500852static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100853{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500854 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100855 case FMODE_WRITE:
856 state->n_wronly++;
857 break;
858 case FMODE_READ:
859 state->n_rdonly++;
860 break;
861 case FMODE_READ|FMODE_WRITE:
862 state->n_rdwr++;
863 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500864 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100865}
866
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500867static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400868{
869 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
870 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
871 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500872 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400873 case FMODE_READ:
874 set_bit(NFS_O_RDONLY_STATE, &state->flags);
875 break;
876 case FMODE_WRITE:
877 set_bit(NFS_O_WRONLY_STATE, &state->flags);
878 break;
879 case FMODE_READ|FMODE_WRITE:
880 set_bit(NFS_O_RDWR_STATE, &state->flags);
881 }
882}
883
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500884static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400885{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400886 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500887 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400888 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400889}
890
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500891static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400893 /*
894 * Protect the call to nfs4_state_set_mode_locked and
895 * serialise the stateid update
896 */
897 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400898 if (deleg_stateid != NULL) {
899 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
900 set_bit(NFS_DELEGATED_STATE, &state->flags);
901 }
902 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500903 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400904 write_sequnlock(&state->seqlock);
905 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500906 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700907 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500910static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500911{
912 struct nfs_inode *nfsi = NFS_I(state->inode);
913 struct nfs_delegation *deleg_cur;
914 int ret = 0;
915
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500916 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500917
918 rcu_read_lock();
919 deleg_cur = rcu_dereference(nfsi->delegation);
920 if (deleg_cur == NULL)
921 goto no_delegation;
922
923 spin_lock(&deleg_cur->lock);
924 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500925 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500926 goto no_delegation_unlock;
927
928 if (delegation == NULL)
929 delegation = &deleg_cur->stateid;
930 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
931 goto no_delegation_unlock;
932
Trond Myklebustb7391f42008-12-23 15:21:52 -0500933 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500934 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500935 ret = 1;
936no_delegation_unlock:
937 spin_unlock(&deleg_cur->lock);
938no_delegation:
939 rcu_read_unlock();
940
941 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500942 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500943 ret = 1;
944 }
945
946 return ret;
947}
948
949
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500950static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400951{
952 struct nfs_delegation *delegation;
953
954 rcu_read_lock();
955 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500956 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400957 rcu_read_unlock();
958 return;
959 }
960 rcu_read_unlock();
961 nfs_inode_return_delegation(inode);
962}
963
Trond Myklebust6ee41262007-07-08 14:11:36 -0400964static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400965{
966 struct nfs4_state *state = opendata->state;
967 struct nfs_inode *nfsi = NFS_I(state->inode);
968 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500969 int open_mode = opendata->o_arg.open_flags & O_EXCL;
970 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400971 nfs4_stateid stateid;
972 int ret = -EAGAIN;
973
Trond Myklebustaac00a82007-07-05 19:02:21 -0400974 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500975 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400976 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500977 if (can_open_cached(state, fmode, open_mode)) {
978 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400979 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400980 goto out_return_state;
981 }
982 spin_unlock(&state->owner->so_lock);
983 }
Trond Myklebust34310432008-12-23 15:21:38 -0500984 rcu_read_lock();
985 delegation = rcu_dereference(nfsi->delegation);
986 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500987 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -0500988 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -0400989 break;
Trond Myklebust34310432008-12-23 15:21:38 -0500990 }
Trond Myklebustaac00a82007-07-05 19:02:21 -0400991 /* Save the delegation */
992 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
993 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -0400994 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400995 if (ret != 0)
996 goto out;
997 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -0500998
999 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001000 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001001 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001002 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001003out:
1004 return ERR_PTR(ret);
1005out_return_state:
1006 atomic_inc(&state->count);
1007 return state;
1008}
1009
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001010static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1011{
1012 struct inode *inode;
1013 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001014 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001015 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001016
Trond Myklebustaac00a82007-07-05 19:02:21 -04001017 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001018 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001019 goto out;
1020 }
1021
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001022 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001023 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001024 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001025 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001026 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001027 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001028 goto err;
1029 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001030 state = nfs4_get_open_state(inode, data->owner);
1031 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001032 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001033 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001034 int delegation_flags = 0;
1035
Trond Myklebust003707c2007-07-05 18:07:55 -04001036 rcu_read_lock();
1037 delegation = rcu_dereference(NFS_I(inode)->delegation);
1038 if (delegation)
1039 delegation_flags = delegation->flags;
1040 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -05001041 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001042 nfs_inode_set_delegation(state->inode,
1043 data->owner->so_cred,
1044 &data->o_res);
1045 else
1046 nfs_inode_reclaim_delegation(state->inode,
1047 data->owner->so_cred,
1048 &data->o_res);
1049 }
Trond Myklebust34310432008-12-23 15:21:38 -05001050
1051 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001052 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001053 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001054out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001055 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001056err_put_inode:
1057 iput(inode);
1058err:
1059 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001060}
1061
Trond Myklebust864472e2006-01-03 09:55:15 +01001062static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1063{
1064 struct nfs_inode *nfsi = NFS_I(state->inode);
1065 struct nfs_open_context *ctx;
1066
1067 spin_lock(&state->inode->i_lock);
1068 list_for_each_entry(ctx, &nfsi->open_files, list) {
1069 if (ctx->state != state)
1070 continue;
1071 get_nfs_open_context(ctx);
1072 spin_unlock(&state->inode->i_lock);
1073 return ctx;
1074 }
1075 spin_unlock(&state->inode->i_lock);
1076 return ERR_PTR(-ENOENT);
1077}
1078
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001079static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1080{
1081 struct nfs4_opendata *opendata;
1082
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001083 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001084 if (opendata == NULL)
1085 return ERR_PTR(-ENOMEM);
1086 opendata->state = state;
1087 atomic_inc(&state->count);
1088 return opendata;
1089}
1090
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001091static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001092{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001093 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001094 int ret;
1095
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001096 opendata->o_arg.open_flags = 0;
1097 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001098 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1099 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1100 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001101 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001102 if (ret != 0)
1103 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001104 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001105 if (IS_ERR(newstate))
1106 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001107 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001108 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001109 return 0;
1110}
1111
1112static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1113{
Trond Myklebust864472e2006-01-03 09:55:15 +01001114 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001115 int ret;
1116
1117 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001118 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001119 smp_rmb();
1120 if (state->n_rdwr != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001121 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001122 if (ret != 0)
1123 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001124 if (newstate != state)
1125 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001126 }
1127 if (state->n_wronly != 0) {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001128 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001129 if (ret != 0)
1130 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001131 if (newstate != state)
1132 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001133 }
1134 if (state->n_rdonly != 0) {
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 Myklebust035168ab2010-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 Myklebust10afec902007-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 Myklebust035168ab2010-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 Myklebust62ab4602009-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 Myklebust62ab4602009-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 Myklebust16b42892006-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 Myklebust16b42892006-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 Myklebust16b42892006-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 Myklebust16b42892006-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 if (state == NULL)
3415 break;
3416 nfs4_state_mark_reclaim_reboot(clp, state);
3417 case -NFS4ERR_STALE_CLIENTID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 case -NFS4ERR_EXPIRED:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003419 goto do_state_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003420#if defined(CONFIG_NFS_V4_1)
3421 case -NFS4ERR_BADSESSION:
3422 case -NFS4ERR_BADSLOT:
3423 case -NFS4ERR_BAD_HIGH_SLOT:
3424 case -NFS4ERR_DEADSESSION:
3425 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3426 case -NFS4ERR_SEQ_FALSE_RETRY:
3427 case -NFS4ERR_SEQ_MISORDERED:
3428 dprintk("%s ERROR %d, Reset session\n", __func__,
3429 task->tk_status);
Andy Adamson0b9e2d42009-12-04 16:02:14 -05003430 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -04003431 task->tk_status = 0;
3432 return -EAGAIN;
3433#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003435 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003436 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003437 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3439 task->tk_status = 0;
3440 return -EAGAIN;
3441 case -NFS4ERR_OLD_STATEID:
3442 task->tk_status = 0;
3443 return -EAGAIN;
3444 }
3445 task->tk_status = nfs4_map_errors(task->tk_status);
3446 return 0;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003447do_state_recovery:
3448 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3449 nfs4_schedule_state_recovery(clp);
3450 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3451 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3452 task->tk_status = 0;
3453 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454}
3455
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003456int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3457 unsigned short port, struct rpc_cred *cred,
3458 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459{
3460 nfs4_verifier sc_verifier;
3461 struct nfs4_setclientid setclientid = {
3462 .sc_verifier = &sc_verifier,
3463 .sc_prog = program,
3464 };
3465 struct rpc_message msg = {
3466 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3467 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003468 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003469 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 };
Al Virobc4785c2006-10-19 23:28:51 -07003471 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 int loop = 0;
3473 int status;
3474
Al Virobc4785c2006-10-19 23:28:51 -07003475 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3477 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3478
3479 for(;;) {
3480 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003481 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003482 clp->cl_ipaddr,
3483 rpc_peeraddr2str(clp->cl_rpcclient,
3484 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003485 rpc_peeraddr2str(clp->cl_rpcclient,
3486 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003487 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 clp->cl_id_uniquifier);
3489 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003490 sizeof(setclientid.sc_netid),
3491 rpc_peeraddr2str(clp->cl_rpcclient,
3492 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003494 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 clp->cl_ipaddr, port >> 8, port & 255);
3496
3497 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3498 if (status != -NFS4ERR_CLID_INUSE)
3499 break;
3500 if (signalled())
3501 break;
3502 if (loop++ & 1)
3503 ssleep(clp->cl_lease_time + 1);
3504 else
3505 if (++clp->cl_id_uniquifier == 0)
3506 break;
3507 }
3508 return status;
3509}
3510
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003511static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3512 struct nfs4_setclientid_res *arg,
3513 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514{
3515 struct nfs_fsinfo fsinfo;
3516 struct rpc_message msg = {
3517 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003518 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003520 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 };
3522 unsigned long now;
3523 int status;
3524
3525 now = jiffies;
3526 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3527 if (status == 0) {
3528 spin_lock(&clp->cl_lock);
3529 clp->cl_lease_time = fsinfo.lease_time * HZ;
3530 clp->cl_last_renewal = now;
3531 spin_unlock(&clp->cl_lock);
3532 }
3533 return status;
3534}
3535
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003536int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3537 struct nfs4_setclientid_res *arg,
3538 struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003539{
Benny Halevy77e03672008-06-24 20:25:57 +03003540 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003541 int err;
3542 do {
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003543 err = _nfs4_proc_setclientid_confirm(clp, arg, cred);
Trond Myklebust51581f32006-03-20 13:44:47 -05003544 switch (err) {
3545 case 0:
3546 return err;
3547 case -NFS4ERR_RESOURCE:
3548 /* The IBM lawyers misread another document! */
3549 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05003550 case -EKEYEXPIRED:
Trond Myklebust51581f32006-03-20 13:44:47 -05003551 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3552 }
3553 } while (err == 0);
3554 return err;
3555}
3556
Trond Myklebustfe650402006-01-03 09:55:18 +01003557struct nfs4_delegreturndata {
3558 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003559 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003560 struct nfs_fh fh;
3561 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003562 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003563 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003564 int rpc_status;
3565};
3566
Trond Myklebustfe650402006-01-03 09:55:18 +01003567static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3568{
3569 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003570
Trond Myklebust14516c32010-07-31 14:29:06 -04003571 if (!nfs4_sequence_done(task, &data->res.seq_res))
3572 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003573
Ricardo Labiaga79708862009-12-07 09:23:21 -05003574 switch (task->tk_status) {
3575 case -NFS4ERR_STALE_STATEID:
3576 case -NFS4ERR_EXPIRED:
3577 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003578 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003579 break;
3580 default:
3581 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3582 -EAGAIN) {
3583 nfs_restart_rpc(task, data->res.server->nfs_client);
3584 return;
3585 }
3586 }
3587 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003588}
3589
3590static void nfs4_delegreturn_release(void *calldata)
3591{
Trond Myklebustfe650402006-01-03 09:55:18 +01003592 kfree(calldata);
3593}
3594
Andy Adamson938e1012009-04-01 09:22:28 -04003595#if defined(CONFIG_NFS_V4_1)
3596static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3597{
3598 struct nfs4_delegreturndata *d_data;
3599
3600 d_data = (struct nfs4_delegreturndata *)data;
3601
Trond Myklebust035168ab2010-06-16 09:52:26 -04003602 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003603 &d_data->args.seq_args,
3604 &d_data->res.seq_res, 1, task))
3605 return;
3606 rpc_call_start(task);
3607}
3608#endif /* CONFIG_NFS_V4_1 */
3609
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003610static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003611#if defined(CONFIG_NFS_V4_1)
3612 .rpc_call_prepare = nfs4_delegreturn_prepare,
3613#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003614 .rpc_call_done = nfs4_delegreturn_done,
3615 .rpc_release = nfs4_delegreturn_release,
3616};
3617
Trond Myklebuste6f81072008-01-24 18:14:34 -05003618static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003619{
3620 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003621 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003622 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003623 struct rpc_message msg = {
3624 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3625 .rpc_cred = cred,
3626 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003627 struct rpc_task_setup task_setup_data = {
3628 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003629 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003630 .callback_ops = &nfs4_delegreturn_ops,
3631 .flags = RPC_TASK_ASYNC,
3632 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003633 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003634
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003635 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003636 if (data == NULL)
3637 return -ENOMEM;
3638 data->args.fhandle = &data->fh;
3639 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003640 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003641 nfs_copy_fh(&data->fh, NFS_FH(inode));
3642 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003643 data->res.fattr = &data->fattr;
3644 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003645 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003646 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003647 data->rpc_status = 0;
3648
Trond Myklebustc970aa82007-07-14 15:39:59 -04003649 task_setup_data.callback_data = data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003650 msg.rpc_argp = &data->args,
3651 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003652 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003653 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003654 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003655 if (!issync)
3656 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003657 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003658 if (status != 0)
3659 goto out;
3660 status = data->rpc_status;
3661 if (status != 0)
3662 goto out;
3663 nfs_refresh_inode(inode, &data->fattr);
3664out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003665 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003666 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667}
3668
Trond Myklebuste6f81072008-01-24 18:14:34 -05003669int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670{
3671 struct nfs_server *server = NFS_SERVER(inode);
3672 struct nfs4_exception exception = { };
3673 int err;
3674 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003675 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 switch (err) {
3677 case -NFS4ERR_STALE_STATEID:
3678 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 case 0:
3680 return 0;
3681 }
3682 err = nfs4_handle_exception(server, err, &exception);
3683 } while (exception.retry);
3684 return err;
3685}
3686
3687#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3688#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3689
3690/*
3691 * sleep, with exponential backoff, and retry the LOCK operation.
3692 */
3693static unsigned long
3694nfs4_set_lock_task_retry(unsigned long timeout)
3695{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003696 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 timeout <<= 1;
3698 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3699 return NFS4_LOCK_MAXTIMEOUT;
3700 return timeout;
3701}
3702
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3704{
3705 struct inode *inode = state->inode;
3706 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003707 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003708 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003710 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003712 struct nfs_lockt_res res = {
3713 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 };
3715 struct rpc_message msg = {
3716 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3717 .rpc_argp = &arg,
3718 .rpc_resp = &res,
3719 .rpc_cred = state->owner->so_cred,
3720 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 struct nfs4_lock_state *lsp;
3722 int status;
3723
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003724 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003725 status = nfs4_set_lock_state(state, request);
3726 if (status != 0)
3727 goto out;
3728 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003729 arg.lock_owner.id = lsp->ls_id.id;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003730 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003731 switch (status) {
3732 case 0:
3733 request->fl_type = F_UNLCK;
3734 break;
3735 case -NFS4ERR_DENIED:
3736 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003738 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003739out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 return status;
3741}
3742
3743static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3744{
3745 struct nfs4_exception exception = { };
3746 int err;
3747
3748 do {
3749 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3750 _nfs4_proc_getlk(state, cmd, request),
3751 &exception);
3752 } while (exception.retry);
3753 return err;
3754}
3755
3756static int do_vfs_lock(struct file *file, struct file_lock *fl)
3757{
3758 int res = 0;
3759 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3760 case FL_POSIX:
3761 res = posix_lock_file_wait(file, fl);
3762 break;
3763 case FL_FLOCK:
3764 res = flock_lock_file_wait(file, fl);
3765 break;
3766 default:
3767 BUG();
3768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 return res;
3770}
3771
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003772struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003773 struct nfs_locku_args arg;
3774 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003775 struct nfs4_lock_state *lsp;
3776 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003777 struct file_lock fl;
3778 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003779 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003780};
3781
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003782static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3783 struct nfs_open_context *ctx,
3784 struct nfs4_lock_state *lsp,
3785 struct nfs_seqid *seqid)
3786{
3787 struct nfs4_unlockdata *p;
3788 struct inode *inode = lsp->ls_state->inode;
3789
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003790 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003791 if (p == NULL)
3792 return NULL;
3793 p->arg.fh = NFS_FH(inode);
3794 p->arg.fl = &p->fl;
3795 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003796 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003797 p->arg.stateid = &lsp->ls_stateid;
3798 p->lsp = lsp;
3799 atomic_inc(&lsp->ls_count);
3800 /* Ensure we don't close file until we're done freeing locks! */
3801 p->ctx = get_nfs_open_context(ctx);
3802 memcpy(&p->fl, fl, sizeof(p->fl));
3803 p->server = NFS_SERVER(inode);
3804 return p;
3805}
3806
Trond Myklebust06f814a2006-01-03 09:55:07 +01003807static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003808{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003809 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003810 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003811 nfs4_put_lock_state(calldata->lsp);
3812 put_nfs_open_context(calldata->ctx);
3813 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003814}
3815
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003816static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003817{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003818 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003819
Trond Myklebust14516c32010-07-31 14:29:06 -04003820 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3821 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003822 switch (task->tk_status) {
3823 case 0:
3824 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003825 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003826 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003827 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003828 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003829 case -NFS4ERR_BAD_STATEID:
3830 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003831 case -NFS4ERR_STALE_STATEID:
3832 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003833 break;
3834 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003835 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebust0110ee12009-12-07 09:00:24 -05003836 nfs_restart_rpc(task,
Trond Myklebustd61e6122009-12-05 19:32:19 -05003837 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003838 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003839}
3840
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003841static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003842{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003843 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003845 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003846 return;
3847 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003848 /* Note: exit _without_ running nfs4_locku_done */
3849 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003850 return;
3851 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003852 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04003853 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04003854 &calldata->arg.seq_args,
3855 &calldata->res.seq_res, 1, task))
3856 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003857 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858}
3859
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003860static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003861 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003862 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003863 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003864};
3865
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003866static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3867 struct nfs_open_context *ctx,
3868 struct nfs4_lock_state *lsp,
3869 struct nfs_seqid *seqid)
3870{
3871 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003872 struct rpc_message msg = {
3873 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3874 .rpc_cred = ctx->cred,
3875 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003876 struct rpc_task_setup task_setup_data = {
3877 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003878 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003879 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003880 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003881 .flags = RPC_TASK_ASYNC,
3882 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003883
Frank Filz137d6ac2007-07-09 15:32:29 -07003884 /* Ensure this is an unlock - when canceling a lock, the
3885 * canceled lock is passed in, and it won't be an unlock.
3886 */
3887 fl->fl_type = F_UNLCK;
3888
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003889 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3890 if (data == NULL) {
3891 nfs_free_seqid(seqid);
3892 return ERR_PTR(-ENOMEM);
3893 }
3894
Trond Myklebust5138fde2007-07-14 15:40:01 -04003895 msg.rpc_argp = &data->arg,
3896 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003897 task_setup_data.callback_data = data;
3898 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003899}
3900
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3902{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003903 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003904 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003905 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003906 struct rpc_task *task;
3907 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003908 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909
Trond Myklebust9b073572006-06-29 16:38:34 -04003910 status = nfs4_set_lock_state(state, request);
3911 /* Unlock _before_ we do the RPC call */
3912 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003913 down_read(&nfsi->rwsem);
3914 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3915 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003916 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003917 }
3918 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003919 if (status != 0)
3920 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003921 /* Is this a delegated lock? */
3922 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04003923 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003924 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003925 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04003926 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003927 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04003928 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003929 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003930 status = PTR_ERR(task);
3931 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04003932 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003933 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003934 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04003935out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003936 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003937 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938}
3939
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003940struct nfs4_lockdata {
3941 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003942 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003943 struct nfs4_lock_state *lsp;
3944 struct nfs_open_context *ctx;
3945 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003946 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003947 int rpc_status;
3948 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04003949 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003950};
3951
3952static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003953 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
3954 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003955{
3956 struct nfs4_lockdata *p;
3957 struct inode *inode = lsp->ls_state->inode;
3958 struct nfs_server *server = NFS_SERVER(inode);
3959
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003960 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003961 if (p == NULL)
3962 return NULL;
3963
3964 p->arg.fh = NFS_FH(inode);
3965 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003966 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003967 if (p->arg.open_seqid == NULL)
3968 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003969 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003970 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003971 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003972 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04003973 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003974 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003975 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003976 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04003977 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003978 atomic_inc(&lsp->ls_count);
3979 p->ctx = get_nfs_open_context(ctx);
3980 memcpy(&p->fl, fl, sizeof(p->fl));
3981 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003982out_free_seqid:
3983 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003984out_free:
3985 kfree(p);
3986 return NULL;
3987}
3988
3989static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3990{
3991 struct nfs4_lockdata *data = calldata;
3992 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993
Harvey Harrison3110ff82008-05-02 13:42:44 -07003994 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003995 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3996 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003997 /* Do we need to do an open_to_lock_owner? */
3998 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05003999 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4000 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004001 data->arg.open_stateid = &state->stateid;
4002 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004003 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004004 } else
4005 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004006 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04004007 if (nfs4_setup_sequence(data->server,
4008 &data->arg.seq_args,
Andy Adamson66179ef2009-04-01 09:22:22 -04004009 &data->res.seq_res, 1, task))
4010 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004011 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004012 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004013}
4014
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004015static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4016{
4017 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4018 nfs4_lock_prepare(task, calldata);
4019}
4020
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004021static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4022{
4023 struct nfs4_lockdata *data = calldata;
4024
Harvey Harrison3110ff82008-05-02 13:42:44 -07004025 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004026
Trond Myklebust14516c32010-07-31 14:29:06 -04004027 if (!nfs4_sequence_done(task, &data->res.seq_res))
4028 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004029
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004030 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004031 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004032 if (data->rpc_status == 0)
4033 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4034 else
4035 goto out;
4036 }
4037 if (data->rpc_status == 0) {
4038 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4039 sizeof(data->lsp->ls_stateid.data));
4040 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04004041 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004042 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004043out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004044 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004045}
4046
4047static void nfs4_lock_release(void *calldata)
4048{
4049 struct nfs4_lockdata *data = calldata;
4050
Harvey Harrison3110ff82008-05-02 13:42:44 -07004051 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004052 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004053 if (data->cancelled != 0) {
4054 struct rpc_task *task;
4055 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4056 data->arg.lock_seqid);
4057 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004058 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004059 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004060 } else
4061 nfs_free_seqid(data->arg.lock_seqid);
4062 nfs4_put_lock_state(data->lsp);
4063 put_nfs_open_context(data->ctx);
4064 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004065 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004066}
4067
4068static const struct rpc_call_ops nfs4_lock_ops = {
4069 .rpc_call_prepare = nfs4_lock_prepare,
4070 .rpc_call_done = nfs4_lock_done,
4071 .rpc_release = nfs4_lock_release,
4072};
4073
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004074static const struct rpc_call_ops nfs4_recover_lock_ops = {
4075 .rpc_call_prepare = nfs4_recover_lock_prepare,
4076 .rpc_call_done = nfs4_lock_done,
4077 .rpc_release = nfs4_lock_release,
4078};
4079
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004080static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4081{
4082 struct nfs_client *clp = server->nfs_client;
4083 struct nfs4_state *state = lsp->ls_state;
4084
4085 switch (error) {
4086 case -NFS4ERR_ADMIN_REVOKED:
4087 case -NFS4ERR_BAD_STATEID:
4088 case -NFS4ERR_EXPIRED:
4089 if (new_lock_owner != 0 ||
4090 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4091 nfs4_state_mark_reclaim_nograce(clp, state);
4092 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004093 break;
4094 case -NFS4ERR_STALE_STATEID:
4095 if (new_lock_owner != 0 ||
4096 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4097 nfs4_state_mark_reclaim_reboot(clp, state);
4098 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004099 };
4100}
4101
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004102static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004103{
4104 struct nfs4_lockdata *data;
4105 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004106 struct rpc_message msg = {
4107 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4108 .rpc_cred = state->owner->so_cred,
4109 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004110 struct rpc_task_setup task_setup_data = {
4111 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004112 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004113 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004114 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004115 .flags = RPC_TASK_ASYNC,
4116 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004117 int ret;
4118
Harvey Harrison3110ff82008-05-02 13:42:44 -07004119 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004120 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004121 fl->fl_u.nfs4_fl.owner,
4122 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004123 if (data == NULL)
4124 return -ENOMEM;
4125 if (IS_SETLKW(cmd))
4126 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004127 if (recovery_type > NFS_LOCK_NEW) {
4128 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004129 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004130 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4131 }
Trond Myklebust5138fde2007-07-14 15:40:01 -04004132 msg.rpc_argp = &data->arg,
4133 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004134 task_setup_data.callback_data = data;
4135 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004136 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004137 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004138 ret = nfs4_wait_for_completion_rpc_task(task);
4139 if (ret == 0) {
4140 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004141 if (ret)
4142 nfs4_handle_setlk_error(data->server, data->lsp,
4143 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004144 } else
4145 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004146 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004147 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004148 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149}
4150
4151static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4152{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004153 struct nfs_server *server = NFS_SERVER(state->inode);
4154 struct nfs4_exception exception = { };
4155 int err;
4156
4157 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004158 /* Cache the lock if possible... */
4159 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4160 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004161 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Jeff Layton2c643482010-01-07 09:42:03 -05004162 if (err != -NFS4ERR_DELAY && err != -EKEYEXPIRED)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004163 break;
4164 nfs4_handle_exception(server, err, &exception);
4165 } while (exception.retry);
4166 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167}
4168
4169static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4170{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004171 struct nfs_server *server = NFS_SERVER(state->inode);
4172 struct nfs4_exception exception = { };
4173 int err;
4174
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004175 err = nfs4_set_lock_state(state, request);
4176 if (err != 0)
4177 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004178 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004179 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4180 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004181 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004182 switch (err) {
4183 default:
4184 goto out;
4185 case -NFS4ERR_GRACE:
4186 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05004187 case -EKEYEXPIRED:
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004188 nfs4_handle_exception(server, err, &exception);
4189 err = 0;
4190 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004191 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004192out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004193 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194}
4195
4196static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4197{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004198 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004199 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004200 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004202 if ((fl_flags & FL_POSIX) &&
4203 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4204 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004205 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004206 status = nfs4_set_lock_state(state, request);
4207 if (status != 0)
4208 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004209 request->fl_flags |= FL_ACCESS;
4210 status = do_vfs_lock(request->fl_file, request);
4211 if (status < 0)
4212 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004213 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004214 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004215 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004216 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004217 request->fl_flags = fl_flags & ~FL_SLEEP;
4218 status = do_vfs_lock(request->fl_file, request);
4219 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004220 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004221 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004222 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004223 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004224 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004225 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004226 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004227 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004228out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004229 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004230out:
4231 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 return status;
4233}
4234
4235static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4236{
4237 struct nfs4_exception exception = { };
4238 int err;
4239
4240 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004241 err = _nfs4_proc_setlk(state, cmd, request);
4242 if (err == -NFS4ERR_DENIED)
4243 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004245 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 } while (exception.retry);
4247 return err;
4248}
4249
4250static int
4251nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4252{
4253 struct nfs_open_context *ctx;
4254 struct nfs4_state *state;
4255 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4256 int status;
4257
4258 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004259 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 state = ctx->state;
4261
4262 if (request->fl_start < 0 || request->fl_end < 0)
4263 return -EINVAL;
4264
Trond Myklebustd9531262009-07-21 19:22:38 -04004265 if (IS_GETLK(cmd)) {
4266 if (state != NULL)
4267 return nfs4_proc_getlk(state, F_GETLK, request);
4268 return 0;
4269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270
4271 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4272 return -EINVAL;
4273
Trond Myklebustd9531262009-07-21 19:22:38 -04004274 if (request->fl_type == F_UNLCK) {
4275 if (state != NULL)
4276 return nfs4_proc_unlck(state, cmd, request);
4277 return 0;
4278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279
Trond Myklebustd9531262009-07-21 19:22:38 -04004280 if (state == NULL)
4281 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 do {
4283 status = nfs4_proc_setlk(state, cmd, request);
4284 if ((status != -EAGAIN) || IS_SETLK(cmd))
4285 break;
4286 timeout = nfs4_set_lock_task_retry(timeout);
4287 status = -ERESTARTSYS;
4288 if (signalled())
4289 break;
4290 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 return status;
4292}
4293
Trond Myklebust888e6942005-11-04 15:38:11 -05004294int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4295{
4296 struct nfs_server *server = NFS_SERVER(state->inode);
4297 struct nfs4_exception exception = { };
4298 int err;
4299
4300 err = nfs4_set_lock_state(state, fl);
4301 if (err != 0)
4302 goto out;
4303 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004304 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004305 switch (err) {
4306 default:
4307 printk(KERN_ERR "%s: unhandled error %d.\n",
4308 __func__, err);
4309 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004310 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004311 goto out;
4312 case -NFS4ERR_EXPIRED:
4313 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004314 case -NFS4ERR_STALE_STATEID:
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004315 case -NFS4ERR_BADSESSION:
4316 case -NFS4ERR_BADSLOT:
4317 case -NFS4ERR_BAD_HIGH_SLOT:
4318 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4319 case -NFS4ERR_DEADSESSION:
Trond Myklebustd5122202009-06-17 13:22:58 -07004320 nfs4_schedule_state_recovery(server->nfs_client);
4321 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004322 case -ERESTARTSYS:
4323 /*
4324 * The show must go on: exit, but mark the
4325 * stateid as needing recovery.
4326 */
4327 case -NFS4ERR_ADMIN_REVOKED:
4328 case -NFS4ERR_BAD_STATEID:
4329 case -NFS4ERR_OPENMODE:
4330 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4331 err = 0;
4332 goto out;
4333 case -ENOMEM:
4334 case -NFS4ERR_DENIED:
4335 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4336 err = 0;
4337 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004338 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05004339 case -EKEYEXPIRED:
Trond Myklebustd5122202009-06-17 13:22:58 -07004340 break;
4341 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004342 err = nfs4_handle_exception(server, err, &exception);
4343 } while (exception.retry);
4344out:
4345 return err;
4346}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004347
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004348static void nfs4_release_lockowner_release(void *calldata)
4349{
4350 kfree(calldata);
4351}
4352
4353const struct rpc_call_ops nfs4_release_lockowner_ops = {
4354 .rpc_release = nfs4_release_lockowner_release,
4355};
4356
4357void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4358{
4359 struct nfs_server *server = lsp->ls_state->owner->so_server;
4360 struct nfs_release_lockowner_args *args;
4361 struct rpc_message msg = {
4362 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4363 };
4364
4365 if (server->nfs_client->cl_mvops->minor_version != 0)
4366 return;
4367 args = kmalloc(sizeof(*args), GFP_NOFS);
4368 if (!args)
4369 return;
4370 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4371 args->lock_owner.id = lsp->ls_id.id;
4372 msg.rpc_argp = args;
4373 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4374}
4375
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004376#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4377
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004378int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4379 size_t buflen, int flags)
4380{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004381 struct inode *inode = dentry->d_inode;
4382
4383 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4384 return -EOPNOTSUPP;
4385
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004386 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004387}
4388
4389/* The getxattr man page suggests returning -ENODATA for unknown attributes,
4390 * and that's what we'll do for e.g. user attributes that haven't been set.
4391 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4392 * attributes in kernel-managed attribute namespaces. */
4393ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4394 size_t buflen)
4395{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004396 struct inode *inode = dentry->d_inode;
4397
4398 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4399 return -EOPNOTSUPP;
4400
4401 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004402}
4403
4404ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4405{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004406 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004407
J. Bruce Fields096455a2006-03-20 23:23:42 -05004408 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4409 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004410 if (buf && buflen < len)
4411 return -ERANGE;
4412 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004413 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4414 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004415}
4416
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004417static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4418{
4419 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4420 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4421 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4422 return;
4423
4424 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4425 NFS_ATTR_FATTR_NLINK;
4426 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4427 fattr->nlink = 2;
4428}
4429
Trond Myklebust56659e92007-07-17 21:52:39 -04004430int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004431 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004432{
4433 struct nfs_server *server = NFS_SERVER(dir);
4434 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004435 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4436 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004437 };
4438 struct nfs4_fs_locations_arg args = {
4439 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004440 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004441 .page = page,
4442 .bitmask = bitmask,
4443 };
Benny Halevy22958462009-04-01 09:22:02 -04004444 struct nfs4_fs_locations_res res = {
4445 .fs_locations = fs_locations,
4446 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004447 struct rpc_message msg = {
4448 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4449 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004450 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004451 };
4452 int status;
4453
Harvey Harrison3110ff82008-05-02 13:42:44 -07004454 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004455 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004456 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004457 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04004458 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004459 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004460 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004461 return status;
4462}
4463
Andy Adamson557134a2009-04-01 09:21:53 -04004464#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004465/*
4466 * nfs4_proc_exchange_id()
4467 *
4468 * Since the clientid has expired, all compounds using sessions
4469 * associated with the stale clientid will be returning
4470 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4471 * be in some phase of session reset.
4472 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004473int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004474{
4475 nfs4_verifier verifier;
4476 struct nfs41_exchange_id_args args = {
4477 .client = clp,
4478 .flags = clp->cl_exchange_flags,
4479 };
4480 struct nfs41_exchange_id_res res = {
4481 .client = clp,
4482 };
4483 int status;
4484 struct rpc_message msg = {
4485 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4486 .rpc_argp = &args,
4487 .rpc_resp = &res,
4488 .rpc_cred = cred,
4489 };
4490 __be32 *p;
4491
4492 dprintk("--> %s\n", __func__);
4493 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004494
Alexandros Batsakis7b183d02009-12-05 13:33:25 -05004495 /* Remove server-only flags */
4496 args.flags &= ~EXCHGID4_FLAG_CONFIRMED_R;
4497
Benny Halevy99fe60d2009-04-01 09:22:29 -04004498 p = (u32 *)verifier.data;
4499 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4500 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4501 args.verifier = &verifier;
4502
4503 while (1) {
4504 args.id_len = scnprintf(args.id, sizeof(args.id),
4505 "%s/%s %u",
4506 clp->cl_ipaddr,
4507 rpc_peeraddr2str(clp->cl_rpcclient,
4508 RPC_DISPLAY_ADDR),
4509 clp->cl_id_uniquifier);
4510
4511 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4512
Andy Adamson180b62a2010-03-02 13:19:36 -05004513 if (status != -NFS4ERR_CLID_INUSE)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004514 break;
4515
4516 if (signalled())
4517 break;
4518
4519 if (++clp->cl_id_uniquifier == 0)
4520 break;
4521 }
4522
4523 dprintk("<-- %s status= %d\n", __func__, status);
4524 return status;
4525}
4526
Andy Adamson2050f0c2009-04-01 09:22:30 -04004527struct nfs4_get_lease_time_data {
4528 struct nfs4_get_lease_time_args *args;
4529 struct nfs4_get_lease_time_res *res;
4530 struct nfs_client *clp;
4531};
4532
4533static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4534 void *calldata)
4535{
4536 int ret;
4537 struct nfs4_get_lease_time_data *data =
4538 (struct nfs4_get_lease_time_data *)calldata;
4539
4540 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004541 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004542 /* just setup sequence, do not trigger session recovery
4543 since we're invoked within one */
4544 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004545 &data->args->la_seq_args,
4546 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004547
4548 BUG_ON(ret == -EAGAIN);
4549 rpc_call_start(task);
4550 dprintk("<-- %s\n", __func__);
4551}
4552
4553/*
4554 * Called from nfs4_state_manager thread for session setup, so don't recover
4555 * from sequence operation or clientid errors.
4556 */
4557static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4558{
4559 struct nfs4_get_lease_time_data *data =
4560 (struct nfs4_get_lease_time_data *)calldata;
4561
4562 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04004563 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4564 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04004565 switch (task->tk_status) {
4566 case -NFS4ERR_DELAY:
4567 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05004568 case -EKEYEXPIRED:
Andy Adamson2050f0c2009-04-01 09:22:30 -04004569 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4570 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4571 task->tk_status = 0;
Trond Myklebust0110ee12009-12-07 09:00:24 -05004572 nfs_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004573 return;
4574 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004575 dprintk("<-- %s\n", __func__);
4576}
4577
4578struct rpc_call_ops nfs4_get_lease_time_ops = {
4579 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4580 .rpc_call_done = nfs4_get_lease_time_done,
4581};
4582
4583int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4584{
4585 struct rpc_task *task;
4586 struct nfs4_get_lease_time_args args;
4587 struct nfs4_get_lease_time_res res = {
4588 .lr_fsinfo = fsinfo,
4589 };
4590 struct nfs4_get_lease_time_data data = {
4591 .args = &args,
4592 .res = &res,
4593 .clp = clp,
4594 };
4595 struct rpc_message msg = {
4596 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4597 .rpc_argp = &args,
4598 .rpc_resp = &res,
4599 };
4600 struct rpc_task_setup task_setup = {
4601 .rpc_client = clp->cl_rpcclient,
4602 .rpc_message = &msg,
4603 .callback_ops = &nfs4_get_lease_time_ops,
4604 .callback_data = &data
4605 };
4606 int status;
4607
Andy Adamson2050f0c2009-04-01 09:22:30 -04004608 dprintk("--> %s\n", __func__);
4609 task = rpc_run_task(&task_setup);
4610
4611 if (IS_ERR(task))
4612 status = PTR_ERR(task);
4613 else {
4614 status = task->tk_status;
4615 rpc_put_task(task);
4616 }
4617 dprintk("<-- %s return %d\n", __func__, status);
4618
4619 return status;
4620}
4621
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004622/*
4623 * Reset a slot table
4624 */
Andy Adamson104aeba2010-01-14 17:45:10 -05004625static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4626 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004627{
Andy Adamson104aeba2010-01-14 17:45:10 -05004628 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004629 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004630 int ret = 0;
4631
Andy Adamson104aeba2010-01-14 17:45:10 -05004632 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4633 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004634
Andy Adamson104aeba2010-01-14 17:45:10 -05004635 /* Does the newly negotiated max_reqs match the existing slot table? */
4636 if (max_reqs != tbl->max_slots) {
4637 ret = -ENOMEM;
4638 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004639 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05004640 if (!new)
4641 goto out;
4642 ret = 0;
4643 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004644 }
4645 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05004646 if (new) {
4647 tbl->slots = new;
4648 tbl->max_slots = max_reqs;
4649 }
4650 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004651 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004652 spin_unlock(&tbl->slot_tbl_lock);
4653 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4654 tbl, tbl->slots, tbl->max_slots);
4655out:
4656 dprintk("<-- %s: return %d\n", __func__, ret);
4657 return ret;
4658}
4659
Andy Adamsonfc931582009-04-01 09:22:31 -04004660/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004661 * Reset the forechannel and backchannel slot tables
4662 */
4663static int nfs4_reset_slot_tables(struct nfs4_session *session)
4664{
4665 int status;
4666
4667 status = nfs4_reset_slot_table(&session->fc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004668 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004669 if (status)
4670 return status;
4671
4672 status = nfs4_reset_slot_table(&session->bc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004673 session->bc_attrs.max_reqs, 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004674 return status;
4675}
4676
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004677/* Destroy the slot table */
4678static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4679{
4680 if (session->fc_slot_table.slots != NULL) {
4681 kfree(session->fc_slot_table.slots);
4682 session->fc_slot_table.slots = NULL;
4683 }
4684 if (session->bc_slot_table.slots != NULL) {
4685 kfree(session->bc_slot_table.slots);
4686 session->bc_slot_table.slots = NULL;
4687 }
4688 return;
4689}
4690
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004691/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004692 * Initialize slot table
4693 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004694static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4695 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04004696{
Andy Adamsonfc931582009-04-01 09:22:31 -04004697 struct nfs4_slot *slot;
4698 int ret = -ENOMEM;
4699
4700 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4701
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004702 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04004703
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004704 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04004705 if (!slot)
4706 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04004707 ret = 0;
4708
4709 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04004710 tbl->max_slots = max_slots;
4711 tbl->slots = slot;
4712 tbl->highest_used_slotid = -1; /* no slot is currently used */
4713 spin_unlock(&tbl->slot_tbl_lock);
4714 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4715 tbl, tbl->slots, tbl->max_slots);
4716out:
4717 dprintk("<-- %s: return %d\n", __func__, ret);
4718 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04004719}
4720
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004721/*
4722 * Initialize the forechannel and backchannel tables
4723 */
4724static int nfs4_init_slot_tables(struct nfs4_session *session)
4725{
Trond Myklebustf26468f2009-12-05 19:32:11 -05004726 struct nfs4_slot_table *tbl;
4727 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004728
Trond Myklebustf26468f2009-12-05 19:32:11 -05004729 tbl = &session->fc_slot_table;
4730 if (tbl->slots == NULL) {
4731 status = nfs4_init_slot_table(tbl,
4732 session->fc_attrs.max_reqs, 1);
4733 if (status)
4734 return status;
4735 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004736
Trond Myklebustf26468f2009-12-05 19:32:11 -05004737 tbl = &session->bc_slot_table;
4738 if (tbl->slots == NULL) {
4739 status = nfs4_init_slot_table(tbl,
4740 session->bc_attrs.max_reqs, 0);
4741 if (status)
4742 nfs4_destroy_slot_tables(session);
4743 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004744
4745 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04004746}
4747
4748struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4749{
4750 struct nfs4_session *session;
4751 struct nfs4_slot_table *tbl;
4752
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004753 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04004754 if (!session)
4755 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04004756
Andy Adamsonea028ac2009-12-04 15:55:38 -05004757 init_completion(&session->complete);
Andy Adamson76db6d92009-04-01 09:22:38 -04004758
Andy Adamson557134a2009-04-01 09:21:53 -04004759 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004760 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04004761 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004762 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004763
4764 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004765 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004766 spin_lock_init(&tbl->slot_tbl_lock);
4767 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4768
Trond Myklebust1055d762010-06-16 09:52:27 -04004769 session->session_state = 1<<NFS4_SESSION_INITING;
4770
Andy Adamson557134a2009-04-01 09:21:53 -04004771 session->clp = clp;
4772 return session;
4773}
4774
4775void nfs4_destroy_session(struct nfs4_session *session)
4776{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04004777 nfs4_proc_destroy_session(session);
4778 dprintk("%s Destroy backchannel for xprt %p\n",
4779 __func__, session->clp->cl_rpcclient->cl_xprt);
4780 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4781 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004782 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04004783 kfree(session);
4784}
4785
Andy Adamsonfc931582009-04-01 09:22:31 -04004786/*
4787 * Initialize the values to be used by the client in CREATE_SESSION
4788 * If nfs4_init_session set the fore channel request and response sizes,
4789 * use them.
4790 *
4791 * Set the back channel max_resp_sz_cached to zero to force the client to
4792 * always set csa_cachethis to FALSE because the current implementation
4793 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4794 */
4795static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4796{
4797 struct nfs4_session *session = args->client->cl_session;
4798 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4799 mxresp_sz = session->fc_attrs.max_resp_sz;
4800
4801 if (mxrqst_sz == 0)
4802 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4803 if (mxresp_sz == 0)
4804 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4805 /* Fore channel attributes */
4806 args->fc_attrs.headerpadsz = 0;
4807 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4808 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04004809 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4810 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4811
4812 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05004813 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04004814 __func__,
4815 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05004816 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04004817
4818 /* Back channel attributes */
4819 args->bc_attrs.headerpadsz = 0;
4820 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4821 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4822 args->bc_attrs.max_resp_sz_cached = 0;
4823 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4824 args->bc_attrs.max_reqs = 1;
4825
4826 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4827 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4828 __func__,
4829 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4830 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4831 args->bc_attrs.max_reqs);
4832}
4833
Andy Adamson8d353012009-04-01 09:22:32 -04004834static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
4835{
4836 if (rcvd <= sent)
4837 return 0;
4838 printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
4839 "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
4840 return -EINVAL;
4841}
4842
4843#define _verify_fore_channel_attr(_name_) \
4844 _verify_channel_attr("fore", #_name_, \
4845 args->fc_attrs._name_, \
4846 session->fc_attrs._name_)
4847
4848#define _verify_back_channel_attr(_name_) \
4849 _verify_channel_attr("back", #_name_, \
4850 args->bc_attrs._name_, \
4851 session->bc_attrs._name_)
4852
4853/*
4854 * The server is not allowed to increase the fore channel header pad size,
4855 * maximum response size, or maximum number of operations.
4856 *
4857 * The back channel attributes are only negotiatied down: We send what the
4858 * (back channel) server insists upon.
4859 */
4860static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4861 struct nfs4_session *session)
4862{
4863 int ret = 0;
4864
4865 ret |= _verify_fore_channel_attr(headerpadsz);
4866 ret |= _verify_fore_channel_attr(max_resp_sz);
4867 ret |= _verify_fore_channel_attr(max_ops);
4868
4869 ret |= _verify_back_channel_attr(headerpadsz);
4870 ret |= _verify_back_channel_attr(max_rqst_sz);
4871 ret |= _verify_back_channel_attr(max_resp_sz);
4872 ret |= _verify_back_channel_attr(max_resp_sz_cached);
4873 ret |= _verify_back_channel_attr(max_ops);
4874 ret |= _verify_back_channel_attr(max_reqs);
4875
4876 return ret;
4877}
4878
Andy Adamsonfc931582009-04-01 09:22:31 -04004879static int _nfs4_proc_create_session(struct nfs_client *clp)
4880{
4881 struct nfs4_session *session = clp->cl_session;
4882 struct nfs41_create_session_args args = {
4883 .client = clp,
4884 .cb_program = NFS4_CALLBACK,
4885 };
4886 struct nfs41_create_session_res res = {
4887 .client = clp,
4888 };
4889 struct rpc_message msg = {
4890 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4891 .rpc_argp = &args,
4892 .rpc_resp = &res,
4893 };
4894 int status;
4895
4896 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04004897 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04004898
4899 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4900
Andy Adamson8d353012009-04-01 09:22:32 -04004901 if (!status)
4902 /* Verify the session's negotiated channel_attrs values */
4903 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04004904 if (!status) {
4905 /* Increment the clientid slot sequence id */
4906 clp->cl_seqid++;
4907 }
4908
4909 return status;
4910}
4911
4912/*
4913 * Issues a CREATE_SESSION operation to the server.
4914 * It is the responsibility of the caller to verify the session is
4915 * expired before calling this routine.
4916 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05004917int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04004918{
4919 int status;
4920 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04004921 struct nfs4_session *session = clp->cl_session;
4922
4923 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
4924
4925 status = _nfs4_proc_create_session(clp);
4926 if (status)
4927 goto out;
4928
Trond Myklebustf26468f2009-12-05 19:32:11 -05004929 /* Init and reset the fore channel */
4930 status = nfs4_init_slot_tables(session);
4931 dprintk("slot table initialization returned %d\n", status);
4932 if (status)
4933 goto out;
4934 status = nfs4_reset_slot_tables(session);
4935 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04004936 if (status)
4937 goto out;
4938
4939 ptr = (unsigned *)&session->sess_id.data[0];
4940 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
4941 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04004942out:
4943 dprintk("<-- %s\n", __func__);
4944 return status;
4945}
4946
Andy Adamson0f3e66c2009-04-01 09:22:34 -04004947/*
4948 * Issue the over-the-wire RPC DESTROY_SESSION.
4949 * The caller must serialize access to this routine.
4950 */
4951int nfs4_proc_destroy_session(struct nfs4_session *session)
4952{
4953 int status = 0;
4954 struct rpc_message msg;
4955
4956 dprintk("--> nfs4_proc_destroy_session\n");
4957
4958 /* session is still being setup */
4959 if (session->clp->cl_cons_state != NFS_CS_READY)
4960 return status;
4961
4962 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
4963 msg.rpc_argp = session;
4964 msg.rpc_resp = NULL;
4965 msg.rpc_cred = NULL;
4966 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4967
4968 if (status)
4969 printk(KERN_WARNING
4970 "Got error %d from the server on DESTROY_SESSION. "
4971 "Session has been destroyed regardless...\n", status);
4972
4973 dprintk("<-- nfs4_proc_destroy_session\n");
4974 return status;
4975}
4976
Trond Myklebustfccba802009-07-21 16:48:07 -04004977int nfs4_init_session(struct nfs_server *server)
4978{
4979 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05004980 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05004981 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04004982 int ret;
4983
4984 if (!nfs4_has_session(clp))
4985 return 0;
4986
Trond Myklebust1055d762010-06-16 09:52:27 -04004987 session = clp->cl_session;
4988 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
4989 return 0;
4990
Andy Adamson68bf05e2009-12-15 12:55:02 -05004991 rsize = server->rsize;
4992 if (rsize == 0)
4993 rsize = NFS_MAX_FILE_IO_SIZE;
4994 wsize = server->wsize;
4995 if (wsize == 0)
4996 wsize = NFS_MAX_FILE_IO_SIZE;
4997
Andy Adamson68bf05e2009-12-15 12:55:02 -05004998 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
4999 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005000
Trond Myklebustfccba802009-07-21 16:48:07 -04005001 ret = nfs4_recover_expired_lease(server);
5002 if (!ret)
5003 ret = nfs4_check_client_ready(clp);
5004 return ret;
5005}
5006
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005007/*
5008 * Renew the cl_session lease.
5009 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005010struct nfs4_sequence_data {
5011 struct nfs_client *clp;
5012 struct nfs4_sequence_args args;
5013 struct nfs4_sequence_res res;
5014};
5015
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005016static void nfs41_sequence_release(void *data)
5017{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005018 struct nfs4_sequence_data *calldata = data;
5019 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005020
Alexandros Batsakis71358402010-02-05 03:45:05 -08005021 if (atomic_read(&clp->cl_count) > 1)
5022 nfs4_schedule_state_renewal(clp);
5023 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005024 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005025}
5026
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005027static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5028{
5029 switch(task->tk_status) {
5030 case -NFS4ERR_DELAY:
5031 case -EKEYEXPIRED:
5032 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5033 return -EAGAIN;
5034 default:
5035 nfs4_schedule_state_recovery(clp);
5036 }
5037 return 0;
5038}
5039
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005040static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005041{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005042 struct nfs4_sequence_data *calldata = data;
5043 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005044
Trond Myklebust14516c32010-07-31 14:29:06 -04005045 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5046 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005047
5048 if (task->tk_status < 0) {
5049 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005050 if (atomic_read(&clp->cl_count) == 1)
5051 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005052
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005053 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5054 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005055 return;
5056 }
5057 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005058 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005059out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005060 dprintk("<-- %s\n", __func__);
5061}
5062
5063static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5064{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005065 struct nfs4_sequence_data *calldata = data;
5066 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005067 struct nfs4_sequence_args *args;
5068 struct nfs4_sequence_res *res;
5069
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005070 args = task->tk_msg.rpc_argp;
5071 res = task->tk_msg.rpc_resp;
5072
Trond Myklebust035168ab2010-06-16 09:52:26 -04005073 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005074 return;
5075 rpc_call_start(task);
5076}
5077
5078static const struct rpc_call_ops nfs41_sequence_ops = {
5079 .rpc_call_done = nfs41_sequence_call_done,
5080 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005081 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005082};
5083
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005084static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005085{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005086 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005087 struct rpc_message msg = {
5088 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5089 .rpc_cred = cred,
5090 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005091 struct rpc_task_setup task_setup_data = {
5092 .rpc_client = clp->cl_rpcclient,
5093 .rpc_message = &msg,
5094 .callback_ops = &nfs41_sequence_ops,
5095 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5096 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005097
Alexandros Batsakis71358402010-02-05 03:45:05 -08005098 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005099 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005100 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005101 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005102 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005103 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005104 }
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005105 msg.rpc_argp = &calldata->args;
5106 msg.rpc_resp = &calldata->res;
5107 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005108 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005109
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005110 return rpc_run_task(&task_setup_data);
5111}
5112
5113static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5114{
5115 struct rpc_task *task;
5116 int ret = 0;
5117
5118 task = _nfs41_proc_sequence(clp, cred);
5119 if (IS_ERR(task))
5120 ret = PTR_ERR(task);
5121 else
5122 rpc_put_task(task);
5123 dprintk("<-- %s status=%d\n", __func__, ret);
5124 return ret;
5125}
5126
5127static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5128{
5129 struct rpc_task *task;
5130 int ret;
5131
5132 task = _nfs41_proc_sequence(clp, cred);
5133 if (IS_ERR(task)) {
5134 ret = PTR_ERR(task);
5135 goto out;
5136 }
5137 ret = rpc_wait_for_completion_task(task);
5138 if (!ret)
5139 ret = task->tk_status;
5140 rpc_put_task(task);
5141out:
5142 dprintk("<-- %s status=%d\n", __func__, ret);
5143 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005144}
5145
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005146struct nfs4_reclaim_complete_data {
5147 struct nfs_client *clp;
5148 struct nfs41_reclaim_complete_args arg;
5149 struct nfs41_reclaim_complete_res res;
5150};
5151
5152static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5153{
5154 struct nfs4_reclaim_complete_data *calldata = data;
5155
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005156 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168ab2010-06-16 09:52:26 -04005157 if (nfs41_setup_sequence(calldata->clp->cl_session,
5158 &calldata->arg.seq_args,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005159 &calldata->res.seq_res, 0, task))
5160 return;
5161
5162 rpc_call_start(task);
5163}
5164
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005165static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5166{
5167 switch(task->tk_status) {
5168 case 0:
5169 case -NFS4ERR_COMPLETE_ALREADY:
5170 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5171 break;
5172 case -NFS4ERR_DELAY:
5173 case -EKEYEXPIRED:
5174 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5175 return -EAGAIN;
5176 default:
5177 nfs4_schedule_state_recovery(clp);
5178 }
5179 return 0;
5180}
5181
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005182static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5183{
5184 struct nfs4_reclaim_complete_data *calldata = data;
5185 struct nfs_client *clp = calldata->clp;
5186 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5187
5188 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005189 if (!nfs41_sequence_done(task, res))
5190 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005191
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005192 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5193 rpc_restart_call_prepare(task);
5194 return;
5195 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005196 dprintk("<-- %s\n", __func__);
5197}
5198
5199static void nfs4_free_reclaim_complete_data(void *data)
5200{
5201 struct nfs4_reclaim_complete_data *calldata = data;
5202
5203 kfree(calldata);
5204}
5205
5206static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5207 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5208 .rpc_call_done = nfs4_reclaim_complete_done,
5209 .rpc_release = nfs4_free_reclaim_complete_data,
5210};
5211
5212/*
5213 * Issue a global reclaim complete.
5214 */
5215static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5216{
5217 struct nfs4_reclaim_complete_data *calldata;
5218 struct rpc_task *task;
5219 struct rpc_message msg = {
5220 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5221 };
5222 struct rpc_task_setup task_setup_data = {
5223 .rpc_client = clp->cl_rpcclient,
5224 .rpc_message = &msg,
5225 .callback_ops = &nfs4_reclaim_complete_call_ops,
5226 .flags = RPC_TASK_ASYNC,
5227 };
5228 int status = -ENOMEM;
5229
5230 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005231 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005232 if (calldata == NULL)
5233 goto out;
5234 calldata->clp = clp;
5235 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005236
5237 msg.rpc_argp = &calldata->arg;
5238 msg.rpc_resp = &calldata->res;
5239 task_setup_data.callback_data = calldata;
5240 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005241 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005242 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005243 goto out;
5244 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005245 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005246 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005247out:
5248 dprintk("<-- %s status=%d\n", __func__, status);
5249 return status;
5250}
Andy Adamson557134a2009-04-01 09:21:53 -04005251#endif /* CONFIG_NFS_V4_1 */
5252
Andy Adamson591d71c2009-04-01 09:22:47 -04005253struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005254 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005255 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256 .recover_open = nfs4_open_reclaim,
5257 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04005258 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005259 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260};
5261
Andy Adamson591d71c2009-04-01 09:22:47 -04005262#if defined(CONFIG_NFS_V4_1)
5263struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5264 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5265 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5266 .recover_open = nfs4_open_reclaim,
5267 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05005268 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005269 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005270 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04005271};
5272#endif /* CONFIG_NFS_V4_1 */
5273
5274struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005275 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005276 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277 .recover_open = nfs4_open_expired,
5278 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04005279 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005280 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281};
5282
Andy Adamson591d71c2009-04-01 09:22:47 -04005283#if defined(CONFIG_NFS_V4_1)
5284struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5285 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5286 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5287 .recover_open = nfs4_open_expired,
5288 .recover_lock = nfs4_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05005289 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005290 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04005291};
5292#endif /* CONFIG_NFS_V4_1 */
5293
Benny Halevy29fba382009-04-01 09:22:44 -04005294struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5295 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04005296 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005297 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04005298};
5299
5300#if defined(CONFIG_NFS_V4_1)
5301struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5302 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04005303 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005304 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04005305};
5306#endif
5307
Trond Myklebust97dc1352010-06-16 09:52:26 -04005308static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
5309 .minor_version = 0,
5310 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04005311 .validate_stateid = nfs4_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005312 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
5313 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
5314 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005315};
5316
5317#if defined(CONFIG_NFS_V4_1)
5318static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
5319 .minor_version = 1,
5320 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04005321 .validate_stateid = nfs41_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005322 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
5323 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
5324 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005325};
5326#endif
5327
Trond Myklebust97dc1352010-06-16 09:52:26 -04005328const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
5329 [0] = &nfs_v4_0_minor_ops,
5330#if defined(CONFIG_NFS_V4_1)
5331 [1] = &nfs_v4_1_minor_ops,
5332#endif
5333};
5334
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005335static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005336 .permission = nfs_permission,
5337 .getattr = nfs_getattr,
5338 .setattr = nfs_setattr,
5339 .getxattr = nfs4_getxattr,
5340 .setxattr = nfs4_setxattr,
5341 .listxattr = nfs4_listxattr,
5342};
5343
David Howells509de812006-08-22 20:06:11 -04005344const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345 .version = 4, /* protocol version */
5346 .dentry_ops = &nfs4_dentry_operations,
5347 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005348 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 .getroot = nfs4_proc_get_root,
5350 .getattr = nfs4_proc_getattr,
5351 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04005352 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353 .lookup = nfs4_proc_lookup,
5354 .access = nfs4_proc_access,
5355 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356 .create = nfs4_proc_create,
5357 .remove = nfs4_proc_remove,
5358 .unlink_setup = nfs4_proc_unlink_setup,
5359 .unlink_done = nfs4_proc_unlink_done,
5360 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04005361 .rename_setup = nfs4_proc_rename_setup,
5362 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 .link = nfs4_proc_link,
5364 .symlink = nfs4_proc_symlink,
5365 .mkdir = nfs4_proc_mkdir,
5366 .rmdir = nfs4_proc_remove,
5367 .readdir = nfs4_proc_readdir,
5368 .mknod = nfs4_proc_mknod,
5369 .statfs = nfs4_proc_statfs,
5370 .fsinfo = nfs4_proc_fsinfo,
5371 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04005372 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 .decode_dirent = nfs4_decode_dirent,
5374 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05005375 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005377 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005379 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005381 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04005382 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04005383 .open_context = nfs4_atomic_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384};
5385
5386/*
5387 * Local variables:
5388 * c-basic-offset: 8
5389 * End:
5390 */