blob: 7f25246bf38c9778fb8c32c19d1d1162468425bd [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 Myklebustb0ed9db2010-10-04 17:59:08 -04001121 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001122 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001123 if (ret != 0)
1124 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001125 if (newstate != state)
1126 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001127 }
1128 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001129 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001130 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001131 if (ret != 0)
1132 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001133 if (newstate != state)
1134 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001135 }
1136 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001137 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001138 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001139 if (ret != 0)
1140 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001141 if (newstate != state)
1142 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001143 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001144 /*
1145 * We may have performed cached opens for all three recoveries.
1146 * Check if we need to update the current stateid.
1147 */
1148 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1149 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001150 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001151 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1152 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001153 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001154 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001155 return 0;
1156}
1157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158/*
1159 * OPEN_RECLAIM:
1160 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001162static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001164 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001165 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001166 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 int status;
1168
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001169 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1170 if (IS_ERR(opendata))
1171 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001172 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1173 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001174 rcu_read_lock();
1175 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001176 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001177 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001178 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001179 opendata->o_arg.u.delegation_type = delegation_type;
1180 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001181 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 return status;
1183}
1184
Trond Myklebust539cd032007-06-05 11:46:42 -04001185static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
1187 struct nfs_server *server = NFS_SERVER(state->inode);
1188 struct nfs4_exception exception = { };
1189 int err;
1190 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001191 err = _nfs4_do_open_reclaim(ctx, state);
Jeff Layton2c643482010-01-07 09:42:03 -05001192 if (err != -NFS4ERR_DELAY && err != -EKEYEXPIRED)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001193 break;
1194 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 } while (exception.retry);
1196 return err;
1197}
1198
Trond Myklebust864472e2006-01-03 09:55:15 +01001199static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1200{
1201 struct nfs_open_context *ctx;
1202 int ret;
1203
1204 ctx = nfs4_state_find_open_context(state);
1205 if (IS_ERR(ctx))
1206 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001207 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001208 put_nfs_open_context(ctx);
1209 return ret;
1210}
1211
Trond Myklebust13437e12007-07-06 15:10:43 -04001212static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001214 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001215 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001217 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1218 if (IS_ERR(opendata))
1219 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001220 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001221 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001222 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001223 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001224 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001225 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226}
1227
Trond Myklebust13437e12007-07-06 15:10:43 -04001228int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
1230 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001231 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 int err;
1233 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001234 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 switch (err) {
1236 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001237 case -ENOENT:
1238 case -ESTALE:
1239 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001240 case -NFS4ERR_BADSESSION:
1241 case -NFS4ERR_BADSLOT:
1242 case -NFS4ERR_BAD_HIGH_SLOT:
1243 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1244 case -NFS4ERR_DEADSESSION:
1245 nfs4_schedule_state_recovery(
1246 server->nfs_client);
1247 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 case -NFS4ERR_STALE_CLIENTID:
1249 case -NFS4ERR_STALE_STATEID:
1250 case -NFS4ERR_EXPIRED:
1251 /* Don't recall a delegation if it was lost */
David Howells7539bba2006-08-22 20:06:09 -04001252 nfs4_schedule_state_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001253 goto out;
1254 case -ERESTARTSYS:
1255 /*
1256 * The show must go on: exit, but mark the
1257 * stateid as needing recovery.
1258 */
1259 case -NFS4ERR_ADMIN_REVOKED:
1260 case -NFS4ERR_BAD_STATEID:
1261 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
1262 case -ENOMEM:
1263 err = 0;
1264 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
1266 err = nfs4_handle_exception(server, err, &exception);
1267 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001268out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 return err;
1270}
1271
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001272static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1273{
1274 struct nfs4_opendata *data = calldata;
1275
1276 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001277 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001278 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1279 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001280 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001281 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001282 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001283 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001284}
1285
1286static void nfs4_open_confirm_release(void *calldata)
1287{
1288 struct nfs4_opendata *data = calldata;
1289 struct nfs4_state *state = NULL;
1290
1291 /* If this request hasn't been cancelled, do nothing */
1292 if (data->cancelled == 0)
1293 goto out_free;
1294 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001295 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001296 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001297 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001298 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001299 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001300out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001301 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001302}
1303
1304static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001305 .rpc_call_done = nfs4_open_confirm_done,
1306 .rpc_release = nfs4_open_confirm_release,
1307};
1308
1309/*
1310 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1311 */
1312static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1313{
1314 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1315 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001316 struct rpc_message msg = {
1317 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1318 .rpc_argp = &data->c_arg,
1319 .rpc_resp = &data->c_res,
1320 .rpc_cred = data->owner->so_cred,
1321 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001322 struct rpc_task_setup task_setup_data = {
1323 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001324 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001325 .callback_ops = &nfs4_open_confirm_ops,
1326 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001327 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001328 .flags = RPC_TASK_ASYNC,
1329 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 int status;
1331
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001332 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001333 data->rpc_done = 0;
1334 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001335 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001336 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001337 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001338 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001339 status = nfs4_wait_for_completion_rpc_task(task);
1340 if (status != 0) {
1341 data->cancelled = 1;
1342 smp_wmb();
1343 } else
1344 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001345 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 return status;
1347}
1348
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001349static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001351 struct nfs4_opendata *data = calldata;
1352 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001353
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001354 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1355 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001356 /*
1357 * Check if we still need to send an OPEN call, or if we can use
1358 * a delegation instead.
1359 */
1360 if (data->state != NULL) {
1361 struct nfs_delegation *delegation;
1362
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001363 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001364 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001365 rcu_read_lock();
1366 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1367 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001368 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001369 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001370 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001371 }
1372 rcu_read_unlock();
1373 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001374 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001375 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001376 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001377 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001378 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001379 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1380 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001381 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04001382 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001383 &data->o_arg.seq_args,
1384 &data->o_res.seq_res, 1, task))
1385 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001386 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001387 return;
1388out_no_action:
1389 task->tk_action = NULL;
1390
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001391}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001393static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1394{
1395 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1396 nfs4_open_prepare(task, calldata);
1397}
1398
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001399static void nfs4_open_done(struct rpc_task *task, void *calldata)
1400{
1401 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001403 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001404
Trond Myklebust14516c32010-07-31 14:29:06 -04001405 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1406 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001407
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001408 if (task->tk_status == 0) {
1409 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001410 case S_IFREG:
1411 break;
1412 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001413 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001414 break;
1415 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001416 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001417 break;
1418 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001419 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001420 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001421 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001422 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1423 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001424 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001425 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001426}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001427
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001428static void nfs4_open_release(void *calldata)
1429{
1430 struct nfs4_opendata *data = calldata;
1431 struct nfs4_state *state = NULL;
1432
1433 /* If this request hasn't been cancelled, do nothing */
1434 if (data->cancelled == 0)
1435 goto out_free;
1436 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001437 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001438 goto out_free;
1439 /* In case we need an open_confirm, no cleanup! */
1440 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1441 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001442 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001443 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001444 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001445out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001446 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001447}
1448
1449static const struct rpc_call_ops nfs4_open_ops = {
1450 .rpc_call_prepare = nfs4_open_prepare,
1451 .rpc_call_done = nfs4_open_done,
1452 .rpc_release = nfs4_open_release,
1453};
1454
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001455static const struct rpc_call_ops nfs4_recover_open_ops = {
1456 .rpc_call_prepare = nfs4_recover_open_prepare,
1457 .rpc_call_done = nfs4_open_done,
1458 .rpc_release = nfs4_open_release,
1459};
1460
1461static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001462{
1463 struct inode *dir = data->dir->d_inode;
1464 struct nfs_server *server = NFS_SERVER(dir);
1465 struct nfs_openargs *o_arg = &data->o_arg;
1466 struct nfs_openres *o_res = &data->o_res;
1467 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001468 struct rpc_message msg = {
1469 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1470 .rpc_argp = o_arg,
1471 .rpc_resp = o_res,
1472 .rpc_cred = data->owner->so_cred,
1473 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001474 struct rpc_task_setup task_setup_data = {
1475 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001476 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001477 .callback_ops = &nfs4_open_ops,
1478 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001479 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001480 .flags = RPC_TASK_ASYNC,
1481 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001482 int status;
1483
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001484 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001485 data->rpc_done = 0;
1486 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001487 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001488 if (isrecover)
1489 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001490 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001491 if (IS_ERR(task))
1492 return PTR_ERR(task);
1493 status = nfs4_wait_for_completion_rpc_task(task);
1494 if (status != 0) {
1495 data->cancelled = 1;
1496 smp_wmb();
1497 } else
1498 status = data->rpc_status;
1499 rpc_put_task(task);
1500
1501 return status;
1502}
1503
1504static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1505{
1506 struct inode *dir = data->dir->d_inode;
1507 struct nfs_openres *o_res = &data->o_res;
1508 int status;
1509
1510 status = nfs4_run_open_task(data, 1);
1511 if (status != 0 || !data->rpc_done)
1512 return status;
1513
1514 nfs_refresh_inode(dir, o_res->dir_attr);
1515
1516 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1517 status = _nfs4_proc_open_confirm(data);
1518 if (status != 0)
1519 return status;
1520 }
1521
1522 return status;
1523}
1524
1525/*
1526 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1527 */
1528static int _nfs4_proc_open(struct nfs4_opendata *data)
1529{
1530 struct inode *dir = data->dir->d_inode;
1531 struct nfs_server *server = NFS_SERVER(dir);
1532 struct nfs_openargs *o_arg = &data->o_arg;
1533 struct nfs_openres *o_res = &data->o_res;
1534 int status;
1535
1536 status = nfs4_run_open_task(data, 0);
Trond Myklebust3e309912007-07-07 13:19:59 -04001537 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001538 return status;
1539
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001540 if (o_arg->open_flags & O_CREAT) {
1541 update_changeattr(dir, &o_res->cinfo);
1542 nfs_post_op_update_inode(dir, o_res->dir_attr);
1543 } else
1544 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001545 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1546 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001548 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001550 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 }
1552 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001553 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001554 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555}
1556
Trond Myklebust10afec92007-05-14 17:16:04 -04001557static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +01001558{
David Howells7539bba2006-08-22 20:06:09 -04001559 struct nfs_client *clp = server->nfs_client;
Trond Myklebusta78cb572009-08-09 15:06:19 -04001560 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001561 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001562
Trond Myklebusta78cb572009-08-09 15:06:19 -04001563 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001564 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001565 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001566 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001567 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1568 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001569 break;
Trond Myklebust58d97142006-01-03 09:55:24 +01001570 nfs4_schedule_state_recovery(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001571 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001572 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001573 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001574}
1575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576/*
1577 * OPEN_EXPIRED:
1578 * reclaim state on the server after a network partition.
1579 * Assumes caller holds the appropriate lock
1580 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001581static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001583 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001584 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001586 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1587 if (IS_ERR(opendata))
1588 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001589 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001590 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001591 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001592 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001593 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594}
1595
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001596static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001597{
Trond Myklebust539cd032007-06-05 11:46:42 -04001598 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001599 struct nfs4_exception exception = { };
1600 int err;
1601
1602 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001603 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001604 switch (err) {
1605 default:
1606 goto out;
1607 case -NFS4ERR_GRACE:
1608 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05001609 case -EKEYEXPIRED:
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001610 nfs4_handle_exception(server, err, &exception);
1611 err = 0;
1612 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001613 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001614out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001615 return err;
1616}
1617
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1619{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001621 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
Trond Myklebust864472e2006-01-03 09:55:15 +01001623 ctx = nfs4_state_find_open_context(state);
1624 if (IS_ERR(ctx))
1625 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001626 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001627 put_nfs_open_context(ctx);
1628 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629}
1630
1631/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001632 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1633 * fields corresponding to attributes that were used to store the verifier.
1634 * Make sure we clobber those fields in the later setattr call
1635 */
1636static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1637{
1638 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1639 !(sattr->ia_valid & ATTR_ATIME_SET))
1640 sattr->ia_valid |= ATTR_ATIME;
1641
1642 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1643 !(sattr->ia_valid & ATTR_MTIME_SET))
1644 sattr->ia_valid |= ATTR_MTIME;
1645}
1646
1647/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001648 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001650static 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 -07001651{
1652 struct nfs4_state_owner *sp;
1653 struct nfs4_state *state = NULL;
1654 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001655 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001656 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
1658 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 status = -ENOMEM;
1660 if (!(sp = nfs4_get_state_owner(server, cred))) {
1661 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1662 goto out_err;
1663 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001664 status = nfs4_recover_expired_lease(server);
1665 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001666 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001667 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001668 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001669 status = -ENOMEM;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001670 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001671 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001672 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
Trond Myklebustaac00a82007-07-05 19:02:21 -04001674 if (path->dentry->d_inode != NULL)
1675 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1676
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001677 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001679 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001681 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001682 status = PTR_ERR(state);
1683 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001684 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001685 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001686 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001687
1688 if (opendata->o_arg.open_flags & O_EXCL) {
1689 nfs4_exclusive_attrset(opendata, sattr);
1690
1691 nfs_fattr_init(opendata->o_res.f_attr);
1692 status = nfs4_do_setattr(state->inode, cred,
1693 opendata->o_res.f_attr, sattr,
1694 state);
1695 if (status == 0)
1696 nfs_setattr_update_inode(state->inode, sattr);
1697 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1698 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001699 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 *res = state;
1702 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001703err_opendata_put:
1704 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001705err_put_state_owner:
1706 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 *res = NULL;
1709 return status;
1710}
1711
1712
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001713static 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 -07001714{
1715 struct nfs4_exception exception = { };
1716 struct nfs4_state *res;
1717 int status;
1718
1719 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001720 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 if (status == 0)
1722 break;
1723 /* NOTE: BAD_SEQID means the server and client disagree about the
1724 * book-keeping w.r.t. state-changing operations
1725 * (OPEN/CLOSE/LOCK/LOCKU...)
1726 * It is actually a sign of a bug on the client or on the server.
1727 *
1728 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001729 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 * have unhashed the old state_owner for us, and that we can
1731 * therefore safely retry using a new one. We should still warn
1732 * the user though...
1733 */
1734 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001735 printk(KERN_WARNING "NFS: v4 server %s "
1736 " returned a bad sequence-id error!\n",
1737 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 exception.retry = 1;
1739 continue;
1740 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001741 /*
1742 * BAD_STATEID on OPEN means that the server cancelled our
1743 * state before it received the OPEN_CONFIRM.
1744 * Recover by retrying the request as per the discussion
1745 * on Page 181 of RFC3530.
1746 */
1747 if (status == -NFS4ERR_BAD_STATEID) {
1748 exception.retry = 1;
1749 continue;
1750 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001751 if (status == -EAGAIN) {
1752 /* We must have found a delegation */
1753 exception.retry = 1;
1754 continue;
1755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1757 status, &exception));
1758 } while (exception.retry);
1759 return res;
1760}
1761
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001762static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1763 struct nfs_fattr *fattr, struct iattr *sattr,
1764 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001766 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001768 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 .iap = sattr,
1770 .server = server,
1771 .bitmask = server->attr_bitmask,
1772 };
1773 struct nfs_setattrres res = {
1774 .fattr = fattr,
1775 .server = server,
1776 };
1777 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001778 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1779 .rpc_argp = &arg,
1780 .rpc_resp = &res,
1781 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001783 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001784 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
Trond Myklebust0e574af2005-10-27 22:12:38 -04001786 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001788 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1789 /* Use that stateid */
1790 } else if (state != NULL) {
Trond Myklebust77041ed2010-07-01 12:49:11 -04001791 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001792 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1794
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001795 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001796 if (status == 0 && state != NULL)
1797 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001798 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799}
1800
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001801static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1802 struct nfs_fattr *fattr, struct iattr *sattr,
1803 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001805 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 struct nfs4_exception exception = { };
1807 int err;
1808 do {
1809 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001810 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 &exception);
1812 } while (exception.retry);
1813 return err;
1814}
1815
1816struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001817 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 struct inode *inode;
1819 struct nfs4_state *state;
1820 struct nfs_closeargs arg;
1821 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001822 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001823 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824};
1825
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001826static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001827{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001828 struct nfs4_closedata *calldata = data;
1829 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001830
1831 nfs4_put_open_state(calldata->state);
1832 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001833 nfs4_put_state_owner(sp);
Jan Blunck31f31db12008-05-02 13:42:45 -07001834 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001835 kfree(calldata);
1836}
1837
Trond Myklebust88069f72009-12-08 08:33:16 -05001838static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1839 fmode_t fmode)
1840{
1841 spin_lock(&state->owner->so_lock);
1842 if (!(fmode & FMODE_READ))
1843 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1844 if (!(fmode & FMODE_WRITE))
1845 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1846 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1847 spin_unlock(&state->owner->so_lock);
1848}
1849
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001850static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001852 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 struct nfs_server *server = NFS_SERVER(calldata->inode);
1855
Trond Myklebust14516c32010-07-31 14:29:06 -04001856 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1857 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 /* hmm. we are done with the inode, and in the process of freeing
1859 * the state_owner. we keep this around to process errors
1860 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 switch (task->tk_status) {
1862 case 0:
Trond Myklebust45328c32007-07-26 17:47:34 -04001863 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001864 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001865 nfs4_close_clear_stateid_flags(state,
1866 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 break;
1868 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001869 case -NFS4ERR_OLD_STATEID:
1870 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001872 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001873 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001875 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1876 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001878 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001879 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880}
1881
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001882static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001884 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001885 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001886 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001887
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001888 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001889 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001890
Trond Myklebust88069f72009-12-08 08:33:16 -05001891 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1892 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001893 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001894 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001895 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001896 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001897 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1898 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1899 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001900 }
1901 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001902 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1903 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1904 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001905 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001906 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001907 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05001908
1909 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001910 /* Note: exit _without_ calling nfs4_close_done */
1911 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001912 return;
1913 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001914
1915 if (calldata->arg.fmode == 0)
1916 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
1917
Trond Myklebust516a6af2005-10-27 22:12:41 -04001918 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001919 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04001920 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Andy Adamson19ddab02009-04-01 09:22:20 -04001921 &calldata->arg.seq_args, &calldata->res.seq_res,
1922 1, task))
1923 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001924 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925}
1926
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001927static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001928 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001929 .rpc_call_done = nfs4_close_done,
1930 .rpc_release = nfs4_free_closedata,
1931};
1932
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933/*
1934 * It is possible for data to be read/written from a mem-mapped file
1935 * after the sys_close call (which hits the vfs layer as a flush).
1936 * This means that we can't safely call nfsv4 close on a file until
1937 * the inode is cleared. This in turn means that we are not good
1938 * NFSv4 citizens - we do not indicate to the server to update the file's
1939 * share state even when we are done with one of the three share
1940 * stateid's in the inode.
1941 *
1942 * NOTE: Caller must be holding the sp->so_owner semaphore!
1943 */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001944int nfs4_do_close(struct path *path, struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001946 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001948 struct nfs4_state_owner *sp = state->owner;
1949 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001950 struct rpc_message msg = {
1951 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1952 .rpc_cred = state->owner->so_cred,
1953 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001954 struct rpc_task_setup task_setup_data = {
1955 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001956 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001957 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05001958 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001959 .flags = RPC_TASK_ASYNC,
1960 };
Trond Myklebust95121352005-10-18 14:20:12 -07001961 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001963 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001965 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001966 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001968 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04001969 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001971 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07001972 if (calldata->arg.seqid == NULL)
1973 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001974 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04001975 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001976 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04001977 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001978 calldata->res.server = server;
Al Virof6948692010-01-30 13:51:04 -05001979 path_get(path);
1980 calldata->path = *path;
Trond Myklebust95121352005-10-18 14:20:12 -07001981
Trond Myklebust5138fde2007-07-14 15:40:01 -04001982 msg.rpc_argp = &calldata->arg,
1983 msg.rpc_resp = &calldata->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001984 task_setup_data.callback_data = calldata;
1985 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001986 if (IS_ERR(task))
1987 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001988 status = 0;
1989 if (wait)
1990 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04001991 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04001992 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07001993out_free_calldata:
1994 kfree(calldata);
1995out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04001996 nfs4_put_open_state(state);
1997 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07001998 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999}
2000
Trond Myklebust2b484292010-09-17 10:56:51 -04002001static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002002nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 struct nfs4_state *state;
2005
Trond Myklebust565277f2007-10-15 18:17:53 -04002006 /* Protect against concurrent sillydeletes */
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002007 state = nfs4_do_open(dir, &ctx->path, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002008 if (IS_ERR(state))
2009 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002010 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002011 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012}
2013
Trond Myklebust1185a552009-12-03 15:54:02 -05002014static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002015{
2016 if (ctx->state == NULL)
2017 return;
2018 if (is_sync)
2019 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2020 else
2021 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2022}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
2024static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2025{
Benny Halevy43652ad2009-04-01 09:21:54 -04002026 struct nfs4_server_caps_arg args = {
2027 .fhandle = fhandle,
2028 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 struct nfs4_server_caps_res res = {};
2030 struct rpc_message msg = {
2031 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002032 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 .rpc_resp = &res,
2034 };
2035 int status;
2036
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002037 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 if (status == 0) {
2039 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002040 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2041 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2042 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2043 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2044 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2046 server->caps |= NFS_CAP_ACLS;
2047 if (res.has_links != 0)
2048 server->caps |= NFS_CAP_HARDLINKS;
2049 if (res.has_symlinks != 0)
2050 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002051 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2052 server->caps |= NFS_CAP_FILEID;
2053 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2054 server->caps |= NFS_CAP_MODE;
2055 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2056 server->caps |= NFS_CAP_NLINK;
2057 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2058 server->caps |= NFS_CAP_OWNER;
2059 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2060 server->caps |= NFS_CAP_OWNER_GROUP;
2061 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2062 server->caps |= NFS_CAP_ATIME;
2063 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2064 server->caps |= NFS_CAP_CTIME;
2065 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2066 server->caps |= NFS_CAP_MTIME;
2067
Trond Myklebusta65318b2009-03-11 14:10:28 -04002068 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2069 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2070 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 server->acl_bitmask = res.acl_bitmask;
2072 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002073
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 return status;
2075}
2076
Trond Myklebust55a97592006-06-09 09:34:19 -04002077int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
2079 struct nfs4_exception exception = { };
2080 int err;
2081 do {
2082 err = nfs4_handle_exception(server,
2083 _nfs4_server_capabilities(server, fhandle),
2084 &exception);
2085 } while (exception.retry);
2086 return err;
2087}
2088
2089static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2090 struct nfs_fsinfo *info)
2091{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 struct nfs4_lookup_root_arg args = {
2093 .bitmask = nfs4_fattr_bitmap,
2094 };
2095 struct nfs4_lookup_res res = {
2096 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002097 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 .fh = fhandle,
2099 };
2100 struct rpc_message msg = {
2101 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2102 .rpc_argp = &args,
2103 .rpc_resp = &res,
2104 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002105
Trond Myklebust0e574af2005-10-27 22:12:38 -04002106 nfs_fattr_init(info->fattr);
Trond Myklebustfccba802009-07-21 16:48:07 -04002107 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108}
2109
2110static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2111 struct nfs_fsinfo *info)
2112{
2113 struct nfs4_exception exception = { };
2114 int err;
2115 do {
2116 err = nfs4_handle_exception(server,
2117 _nfs4_lookup_root(server, fhandle, info),
2118 &exception);
2119 } while (exception.retry);
2120 return err;
2121}
2122
David Howells54ceac42006-08-22 20:06:13 -04002123/*
2124 * get the file handle for the "/" directory on the server
2125 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002127 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 int status;
2130
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 if (status == 0)
2133 status = nfs4_server_capabilities(server, fhandle);
2134 if (status == 0)
2135 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002136 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137}
2138
Manoj Naik6b97fd32006-06-09 09:34:29 -04002139/*
2140 * Get locations and (maybe) other attributes of a referral.
2141 * Note that we'll actually follow the referral later when
2142 * we detect fsid mismatch in inode revalidation
2143 */
Trond Myklebust56659e92007-07-17 21:52:39 -04002144static 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 -04002145{
2146 int status = -ENOMEM;
2147 struct page *page = NULL;
2148 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002149
2150 page = alloc_page(GFP_KERNEL);
2151 if (page == NULL)
2152 goto out;
2153 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2154 if (locations == NULL)
2155 goto out;
2156
Trond Myklebustc228fd32007-01-13 02:28:11 -05002157 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002158 if (status != 0)
2159 goto out;
2160 /* Make sure server returned a different fsid for the referral */
2161 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07002162 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 -04002163 status = -EIO;
2164 goto out;
2165 }
2166
2167 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2168 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2169 if (!fattr->mode)
2170 fattr->mode = S_IFDIR;
2171 memset(fhandle, 0, sizeof(struct nfs_fh));
2172out:
2173 if (page)
2174 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002175 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002176 return status;
2177}
2178
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2180{
2181 struct nfs4_getattr_arg args = {
2182 .fh = fhandle,
2183 .bitmask = server->attr_bitmask,
2184 };
2185 struct nfs4_getattr_res res = {
2186 .fattr = fattr,
2187 .server = server,
2188 };
2189 struct rpc_message msg = {
2190 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2191 .rpc_argp = &args,
2192 .rpc_resp = &res,
2193 };
2194
Trond Myklebust0e574af2005-10-27 22:12:38 -04002195 nfs_fattr_init(fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002196 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197}
2198
2199static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2200{
2201 struct nfs4_exception exception = { };
2202 int err;
2203 do {
2204 err = nfs4_handle_exception(server,
2205 _nfs4_proc_getattr(server, fhandle, fattr),
2206 &exception);
2207 } while (exception.retry);
2208 return err;
2209}
2210
2211/*
2212 * The file is not closed if it is opened due to the a request to change
2213 * the size of the file. The open call will not be needed once the
2214 * VFS layer lookup-intents are implemented.
2215 *
2216 * Close is called when the inode is destroyed.
2217 * If we haven't opened the file for O_WRONLY, we
2218 * need to in the size_change case to obtain a stateid.
2219 *
2220 * Got race?
2221 * Because OPEN is always done by name in nfsv4, it is
2222 * possible that we opened a different file by the same
2223 * name. We can recognize this race condition, but we
2224 * can't do anything about it besides returning an error.
2225 *
2226 * This will be fixed with VFS changes (lookup-intent).
2227 */
2228static int
2229nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2230 struct iattr *sattr)
2231{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002232 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002233 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002234 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 int status;
2236
Trond Myklebust0e574af2005-10-27 22:12:38 -04002237 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
Trond Myklebustd5308382005-11-04 15:33:38 -05002239 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002240 if (sattr->ia_valid & ATTR_FILE) {
2241 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002242
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002243 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002244 if (ctx) {
2245 cred = ctx->cred;
2246 state = ctx->state;
2247 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002248 }
2249
2250 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002251 if (status == 0)
2252 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 return status;
2254}
2255
Trond Myklebust56659e92007-07-17 21:52:39 -04002256static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2257 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04002258 struct nfs_fattr *fattr)
2259{
2260 int status;
2261 struct nfs4_lookup_arg args = {
2262 .bitmask = server->attr_bitmask,
2263 .dir_fh = dirfh,
2264 .name = name,
2265 };
2266 struct nfs4_lookup_res res = {
2267 .server = server,
2268 .fattr = fattr,
2269 .fh = fhandle,
2270 };
2271 struct rpc_message msg = {
2272 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2273 .rpc_argp = &args,
2274 .rpc_resp = &res,
2275 };
2276
2277 nfs_fattr_init(fattr);
2278
2279 dprintk("NFS call lookupfh %s\n", name->name);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002280 status = nfs4_call_sync(server, &msg, &args, &res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002281 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002282 return status;
2283}
2284
2285static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2286 struct qstr *name, struct nfs_fh *fhandle,
2287 struct nfs_fattr *fattr)
2288{
2289 struct nfs4_exception exception = { };
2290 int err;
2291 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04002292 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2293 /* FIXME: !!!! */
2294 if (err == -NFS4ERR_MOVED) {
2295 err = -EREMOTE;
2296 break;
2297 }
2298 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002299 } while (exception.retry);
2300 return err;
2301}
2302
Trond Myklebust56659e92007-07-17 21:52:39 -04002303static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2305{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002306 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
2308 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002309 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002310 if (status == -NFS4ERR_MOVED)
2311 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 dprintk("NFS reply lookup: %d\n", status);
2313 return status;
2314}
2315
2316static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2317{
2318 struct nfs4_exception exception = { };
2319 int err;
2320 do {
2321 err = nfs4_handle_exception(NFS_SERVER(dir),
2322 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2323 &exception);
2324 } while (exception.retry);
2325 return err;
2326}
2327
2328static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2329{
Trond Myklebust76b32992007-08-10 17:45:11 -04002330 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 struct nfs4_accessargs args = {
2332 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002333 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002335 struct nfs4_accessres res = {
2336 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002337 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 struct rpc_message msg = {
2339 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2340 .rpc_argp = &args,
2341 .rpc_resp = &res,
2342 .rpc_cred = entry->cred,
2343 };
2344 int mode = entry->mask;
2345 int status;
2346
2347 /*
2348 * Determine which access bits we want to ask for...
2349 */
2350 if (mode & MAY_READ)
2351 args.access |= NFS4_ACCESS_READ;
2352 if (S_ISDIR(inode->i_mode)) {
2353 if (mode & MAY_WRITE)
2354 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2355 if (mode & MAY_EXEC)
2356 args.access |= NFS4_ACCESS_LOOKUP;
2357 } else {
2358 if (mode & MAY_WRITE)
2359 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2360 if (mode & MAY_EXEC)
2361 args.access |= NFS4_ACCESS_EXECUTE;
2362 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002363
2364 res.fattr = nfs_alloc_fattr();
2365 if (res.fattr == NULL)
2366 return -ENOMEM;
2367
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002368 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 if (!status) {
2370 entry->mask = 0;
2371 if (res.access & NFS4_ACCESS_READ)
2372 entry->mask |= MAY_READ;
2373 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2374 entry->mask |= MAY_WRITE;
2375 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2376 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002377 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002379 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 return status;
2381}
2382
2383static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2384{
2385 struct nfs4_exception exception = { };
2386 int err;
2387 do {
2388 err = nfs4_handle_exception(NFS_SERVER(inode),
2389 _nfs4_proc_access(inode, entry),
2390 &exception);
2391 } while (exception.retry);
2392 return err;
2393}
2394
2395/*
2396 * TODO: For the time being, we don't try to get any attributes
2397 * along with any of the zero-copy operations READ, READDIR,
2398 * READLINK, WRITE.
2399 *
2400 * In the case of the first three, we want to put the GETATTR
2401 * after the read-type operation -- this is because it is hard
2402 * to predict the length of a GETATTR response in v4, and thus
2403 * align the READ data correctly. This means that the GETATTR
2404 * may end up partially falling into the page cache, and we should
2405 * shift it into the 'tail' of the xdr_buf before processing.
2406 * To do this efficiently, we need to know the total length
2407 * of data received, which doesn't seem to be available outside
2408 * of the RPC layer.
2409 *
2410 * In the case of WRITE, we also want to put the GETATTR after
2411 * the operation -- in this case because we want to make sure
2412 * we get the post-operation mtime and size. This means that
2413 * we can't use xdr_encode_pages() as written: we need a variant
2414 * of it which would leave room in the 'tail' iovec.
2415 *
2416 * Both of these changes to the XDR layer would in fact be quite
2417 * minor, but I decided to leave them for a subsequent patch.
2418 */
2419static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2420 unsigned int pgbase, unsigned int pglen)
2421{
2422 struct nfs4_readlink args = {
2423 .fh = NFS_FH(inode),
2424 .pgbase = pgbase,
2425 .pglen = pglen,
2426 .pages = &page,
2427 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002428 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 struct rpc_message msg = {
2430 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2431 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002432 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 };
2434
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002435 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436}
2437
2438static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2439 unsigned int pgbase, unsigned int pglen)
2440{
2441 struct nfs4_exception exception = { };
2442 int err;
2443 do {
2444 err = nfs4_handle_exception(NFS_SERVER(inode),
2445 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2446 &exception);
2447 } while (exception.retry);
2448 return err;
2449}
2450
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451/*
2452 * Got race?
2453 * We will need to arrange for the VFS layer to provide an atomic open.
2454 * Until then, this create/open method is prone to inefficiency and race
2455 * conditions due to the lookup, create, and open VFS calls from sys_open()
2456 * placed on the wire.
2457 *
2458 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2459 * The file will be opened again in the subsequent VFS open call
2460 * (nfs4_proc_file_open).
2461 *
2462 * The open for read will just hang around to be used by any process that
2463 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2464 */
2465
2466static int
2467nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002468 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469{
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002470 struct path my_path = {
Trond Myklebustad389da2007-06-05 12:30:00 -04002471 .dentry = dentry,
2472 };
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002473 struct path *path = &my_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002475 struct rpc_cred *cred = NULL;
2476 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 int status = 0;
2478
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002479 if (ctx != NULL) {
2480 cred = ctx->cred;
2481 path = &ctx->path;
2482 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 }
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002484 state = nfs4_do_open(dir, path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002485 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 if (IS_ERR(state)) {
2487 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002488 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002490 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002491 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002492 if (ctx != NULL)
2493 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002494 else
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002495 nfs4_close_sync(path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496out:
2497 return status;
2498}
2499
2500static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2501{
Trond Myklebust16e42952005-10-27 22:12:44 -04002502 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002503 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002505 .name.len = name->len,
2506 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002507 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002509 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002510 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002511 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002513 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2514 .rpc_argp = &args,
2515 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002517 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
Trond Myklebustd3468902010-04-16 16:22:50 -04002519 res.dir_attr = nfs_alloc_fattr();
2520 if (res.dir_attr == NULL)
2521 goto out;
2522
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002523 status = nfs4_call_sync(server, &msg, &args, &res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002524 if (status == 0) {
2525 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002526 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002527 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002528 nfs_free_fattr(res.dir_attr);
2529out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 return status;
2531}
2532
2533static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2534{
2535 struct nfs4_exception exception = { };
2536 int err;
2537 do {
2538 err = nfs4_handle_exception(NFS_SERVER(dir),
2539 _nfs4_proc_remove(dir, name),
2540 &exception);
2541 } while (exception.retry);
2542 return err;
2543}
2544
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002545static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002547 struct nfs_server *server = NFS_SERVER(dir);
2548 struct nfs_removeargs *args = msg->rpc_argp;
2549 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
Trond Myklebusta65318b2009-03-11 14:10:28 -04002551 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002552 res->server = server;
Benny Halevydfb4f3092010-09-24 09:17:01 -04002553 res->seq_res.sr_slot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555}
2556
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002557static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002559 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2560
Trond Myklebust14516c32010-07-31 14:29:06 -04002561 if (!nfs4_sequence_done(task, &res->seq_res))
2562 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002563 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002564 return 0;
2565 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002566 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002567 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568}
2569
Jeff Laytond3d41522010-09-17 17:31:57 -04002570static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2571{
2572 struct nfs_server *server = NFS_SERVER(dir);
2573 struct nfs_renameargs *arg = msg->rpc_argp;
2574 struct nfs_renameres *res = msg->rpc_resp;
2575
2576 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2577 arg->bitmask = server->attr_bitmask;
2578 res->server = server;
2579}
2580
2581static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2582 struct inode *new_dir)
2583{
2584 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2585
2586 if (!nfs4_sequence_done(task, &res->seq_res))
2587 return 0;
2588 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2589 return 0;
2590
2591 update_changeattr(old_dir, &res->old_cinfo);
2592 nfs_post_op_update_inode(old_dir, res->old_fattr);
2593 update_changeattr(new_dir, &res->new_cinfo);
2594 nfs_post_op_update_inode(new_dir, res->new_fattr);
2595 return 1;
2596}
2597
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2599 struct inode *new_dir, struct qstr *new_name)
2600{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002601 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002602 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 .old_dir = NFS_FH(old_dir),
2604 .new_dir = NFS_FH(new_dir),
2605 .old_name = old_name,
2606 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002607 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002609 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002610 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002611 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 struct rpc_message msg = {
2613 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2614 .rpc_argp = &arg,
2615 .rpc_resp = &res,
2616 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002617 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
Trond Myklebust011fff72010-04-16 16:22:49 -04002619 res.old_fattr = nfs_alloc_fattr();
2620 res.new_fattr = nfs_alloc_fattr();
2621 if (res.old_fattr == NULL || res.new_fattr == NULL)
2622 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
Trond Myklebust011fff72010-04-16 16:22:49 -04002624 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 if (!status) {
2626 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002627 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002629 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002631out:
2632 nfs_free_fattr(res.new_fattr);
2633 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 return status;
2635}
2636
2637static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2638 struct inode *new_dir, struct qstr *new_name)
2639{
2640 struct nfs4_exception exception = { };
2641 int err;
2642 do {
2643 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2644 _nfs4_proc_rename(old_dir, old_name,
2645 new_dir, new_name),
2646 &exception);
2647 } while (exception.retry);
2648 return err;
2649}
2650
2651static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2652{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002653 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 struct nfs4_link_arg arg = {
2655 .fh = NFS_FH(inode),
2656 .dir_fh = NFS_FH(dir),
2657 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002658 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002660 struct nfs4_link_res res = {
2661 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002662 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 struct rpc_message msg = {
2664 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2665 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002666 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002668 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
Trond Myklebust136f2622010-04-16 16:22:49 -04002670 res.fattr = nfs_alloc_fattr();
2671 res.dir_attr = nfs_alloc_fattr();
2672 if (res.fattr == NULL || res.dir_attr == NULL)
2673 goto out;
2674
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002675 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002676 if (!status) {
2677 update_changeattr(dir, &res.cinfo);
2678 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002679 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002680 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002681out:
2682 nfs_free_fattr(res.dir_attr);
2683 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 return status;
2685}
2686
2687static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2688{
2689 struct nfs4_exception exception = { };
2690 int err;
2691 do {
2692 err = nfs4_handle_exception(NFS_SERVER(inode),
2693 _nfs4_proc_link(inode, dir, name),
2694 &exception);
2695 } while (exception.retry);
2696 return err;
2697}
2698
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002699struct nfs4_createdata {
2700 struct rpc_message msg;
2701 struct nfs4_create_arg arg;
2702 struct nfs4_create_res res;
2703 struct nfs_fh fh;
2704 struct nfs_fattr fattr;
2705 struct nfs_fattr dir_fattr;
2706};
2707
2708static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2709 struct qstr *name, struct iattr *sattr, u32 ftype)
2710{
2711 struct nfs4_createdata *data;
2712
2713 data = kzalloc(sizeof(*data), GFP_KERNEL);
2714 if (data != NULL) {
2715 struct nfs_server *server = NFS_SERVER(dir);
2716
2717 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2718 data->msg.rpc_argp = &data->arg;
2719 data->msg.rpc_resp = &data->res;
2720 data->arg.dir_fh = NFS_FH(dir);
2721 data->arg.server = server;
2722 data->arg.name = name;
2723 data->arg.attrs = sattr;
2724 data->arg.ftype = ftype;
2725 data->arg.bitmask = server->attr_bitmask;
2726 data->res.server = server;
2727 data->res.fh = &data->fh;
2728 data->res.fattr = &data->fattr;
2729 data->res.dir_fattr = &data->dir_fattr;
2730 nfs_fattr_init(data->res.fattr);
2731 nfs_fattr_init(data->res.dir_fattr);
2732 }
2733 return data;
2734}
2735
2736static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2737{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002738 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2739 &data->arg, &data->res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002740 if (status == 0) {
2741 update_changeattr(dir, &data->res.dir_cinfo);
2742 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2743 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2744 }
2745 return status;
2746}
2747
2748static void nfs4_free_createdata(struct nfs4_createdata *data)
2749{
2750 kfree(data);
2751}
2752
Chuck Lever4f390c12006-08-22 20:06:22 -04002753static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002754 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002756 struct nfs4_createdata *data;
2757 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758
Chuck Lever94a6d752006-08-22 20:06:23 -04002759 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002760 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002761
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002762 status = -ENOMEM;
2763 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2764 if (data == NULL)
2765 goto out;
2766
2767 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2768 data->arg.u.symlink.pages = &page;
2769 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002771 status = nfs4_do_create(dir, dentry, data);
2772
2773 nfs4_free_createdata(data);
2774out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 return status;
2776}
2777
Chuck Lever4f390c12006-08-22 20:06:22 -04002778static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002779 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780{
2781 struct nfs4_exception exception = { };
2782 int err;
2783 do {
2784 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002785 _nfs4_proc_symlink(dir, dentry, page,
2786 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 &exception);
2788 } while (exception.retry);
2789 return err;
2790}
2791
2792static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2793 struct iattr *sattr)
2794{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002795 struct nfs4_createdata *data;
2796 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002798 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2799 if (data == NULL)
2800 goto out;
2801
2802 status = nfs4_do_create(dir, dentry, data);
2803
2804 nfs4_free_createdata(data);
2805out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 return status;
2807}
2808
2809static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2810 struct iattr *sattr)
2811{
2812 struct nfs4_exception exception = { };
2813 int err;
2814 do {
2815 err = nfs4_handle_exception(NFS_SERVER(dir),
2816 _nfs4_proc_mkdir(dir, dentry, sattr),
2817 &exception);
2818 } while (exception.retry);
2819 return err;
2820}
2821
2822static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2823 u64 cookie, struct page *page, unsigned int count, int plus)
2824{
2825 struct inode *dir = dentry->d_inode;
2826 struct nfs4_readdir_arg args = {
2827 .fh = NFS_FH(dir),
2828 .pages = &page,
2829 .pgbase = 0,
2830 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002831 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 };
2833 struct nfs4_readdir_res res;
2834 struct rpc_message msg = {
2835 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2836 .rpc_argp = &args,
2837 .rpc_resp = &res,
2838 .rpc_cred = cred,
2839 };
2840 int status;
2841
Harvey Harrison3110ff82008-05-02 13:42:44 -07002842 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002843 dentry->d_parent->d_name.name,
2844 dentry->d_name.name,
2845 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2847 res.pgbase = args.pgbase;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002848 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 if (status == 0)
2850 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustc4812992007-09-28 17:11:45 -04002851
2852 nfs_invalidate_atime(dir);
2853
Harvey Harrison3110ff82008-05-02 13:42:44 -07002854 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 return status;
2856}
2857
2858static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
2859 u64 cookie, struct page *page, unsigned int count, int plus)
2860{
2861 struct nfs4_exception exception = { };
2862 int err;
2863 do {
2864 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2865 _nfs4_proc_readdir(dentry, cred, cookie,
2866 page, count, plus),
2867 &exception);
2868 } while (exception.retry);
2869 return err;
2870}
2871
2872static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2873 struct iattr *sattr, dev_t rdev)
2874{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002875 struct nfs4_createdata *data;
2876 int mode = sattr->ia_mode;
2877 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878
2879 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2880 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002881
2882 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2883 if (data == NULL)
2884 goto out;
2885
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002887 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002889 data->arg.ftype = NF4BLK;
2890 data->arg.u.device.specdata1 = MAJOR(rdev);
2891 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 }
2893 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002894 data->arg.ftype = NF4CHR;
2895 data->arg.u.device.specdata1 = MAJOR(rdev);
2896 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002899 status = nfs4_do_create(dir, dentry, data);
2900
2901 nfs4_free_createdata(data);
2902out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 return status;
2904}
2905
2906static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2907 struct iattr *sattr, dev_t rdev)
2908{
2909 struct nfs4_exception exception = { };
2910 int err;
2911 do {
2912 err = nfs4_handle_exception(NFS_SERVER(dir),
2913 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2914 &exception);
2915 } while (exception.retry);
2916 return err;
2917}
2918
2919static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2920 struct nfs_fsstat *fsstat)
2921{
2922 struct nfs4_statfs_arg args = {
2923 .fh = fhandle,
2924 .bitmask = server->attr_bitmask,
2925 };
Benny Halevy24ad1482009-04-01 09:21:56 -04002926 struct nfs4_statfs_res res = {
2927 .fsstat = fsstat,
2928 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 struct rpc_message msg = {
2930 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2931 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04002932 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 };
2934
Trond Myklebust0e574af2005-10-27 22:12:38 -04002935 nfs_fattr_init(fsstat->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002936 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937}
2938
2939static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2940{
2941 struct nfs4_exception exception = { };
2942 int err;
2943 do {
2944 err = nfs4_handle_exception(server,
2945 _nfs4_proc_statfs(server, fhandle, fsstat),
2946 &exception);
2947 } while (exception.retry);
2948 return err;
2949}
2950
2951static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2952 struct nfs_fsinfo *fsinfo)
2953{
2954 struct nfs4_fsinfo_arg args = {
2955 .fh = fhandle,
2956 .bitmask = server->attr_bitmask,
2957 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04002958 struct nfs4_fsinfo_res res = {
2959 .fsinfo = fsinfo,
2960 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 struct rpc_message msg = {
2962 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
2963 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04002964 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 };
2966
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002967 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968}
2969
2970static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2971{
2972 struct nfs4_exception exception = { };
2973 int err;
2974
2975 do {
2976 err = nfs4_handle_exception(server,
2977 _nfs4_do_fsinfo(server, fhandle, fsinfo),
2978 &exception);
2979 } while (exception.retry);
2980 return err;
2981}
2982
2983static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
2984{
Trond Myklebust0e574af2005-10-27 22:12:38 -04002985 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 return nfs4_do_fsinfo(server, fhandle, fsinfo);
2987}
2988
2989static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
2990 struct nfs_pathconf *pathconf)
2991{
2992 struct nfs4_pathconf_arg args = {
2993 .fh = fhandle,
2994 .bitmask = server->attr_bitmask,
2995 };
Benny Halevyd45b2982009-04-01 09:21:58 -04002996 struct nfs4_pathconf_res res = {
2997 .pathconf = pathconf,
2998 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 struct rpc_message msg = {
3000 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3001 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003002 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 };
3004
3005 /* None of the pathconf attributes are mandatory to implement */
3006 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3007 memset(pathconf, 0, sizeof(*pathconf));
3008 return 0;
3009 }
3010
Trond Myklebust0e574af2005-10-27 22:12:38 -04003011 nfs_fattr_init(pathconf->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003012 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013}
3014
3015static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3016 struct nfs_pathconf *pathconf)
3017{
3018 struct nfs4_exception exception = { };
3019 int err;
3020
3021 do {
3022 err = nfs4_handle_exception(server,
3023 _nfs4_proc_pathconf(server, fhandle, pathconf),
3024 &exception);
3025 } while (exception.retry);
3026 return err;
3027}
3028
Trond Myklebustec06c092006-03-20 13:44:27 -05003029static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030{
Trond Myklebustec06c092006-03-20 13:44:27 -05003031 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003033 dprintk("--> %s\n", __func__);
3034
Trond Myklebust14516c32010-07-31 14:29:06 -04003035 if (!nfs4_sequence_done(task, &data->res.seq_res))
3036 return -EAGAIN;
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003037
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003038 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003039 nfs_restart_rpc(task, server->nfs_client);
Trond Myklebustec06c092006-03-20 13:44:27 -05003040 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003042
3043 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003045 renew_lease(server, data->timestamp);
3046 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047}
3048
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003049static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 data->timestamp = jiffies;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003052 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053}
3054
Trond Myklebust788e7a82006-03-20 13:44:27 -05003055static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 struct inode *inode = data->inode;
3058
Trond Myklebust14516c32010-07-31 14:29:06 -04003059 if (!nfs4_sequence_done(task, &data->res.seq_res))
3060 return -EAGAIN;
Andy Adamsondef6ed72009-04-01 09:22:26 -04003061
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003062 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003063 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003064 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003066 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003068 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003069 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003070 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071}
3072
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003073static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003075 struct nfs_server *server = NFS_SERVER(data->inode);
3076
Trond Myklebusta65318b2009-03-11 14:10:28 -04003077 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003078 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 data->timestamp = jiffies;
3080
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003081 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082}
3083
Trond Myklebust788e7a82006-03-20 13:44:27 -05003084static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 struct inode *inode = data->inode;
3087
Trond Myklebust14516c32010-07-31 14:29:06 -04003088 if (!nfs4_sequence_done(task, &data->res.seq_res))
3089 return -EAGAIN;
3090
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003091 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003092 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003093 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003095 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003096 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097}
3098
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003099static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003101 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102
Trond Myklebusta65318b2009-03-11 14:10:28 -04003103 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003104 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003105 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106}
3107
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003108struct nfs4_renewdata {
3109 struct nfs_client *client;
3110 unsigned long timestamp;
3111};
3112
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113/*
3114 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3115 * standalone procedure for queueing an asynchronous RENEW.
3116 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003117static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003118{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003119 struct nfs4_renewdata *data = calldata;
3120 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003121
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003122 if (atomic_read(&clp->cl_count) > 1)
3123 nfs4_schedule_state_renewal(clp);
3124 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003125 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003126}
3127
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003128static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003130 struct nfs4_renewdata *data = calldata;
3131 struct nfs_client *clp = data->client;
3132 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133
3134 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003135 /* Unless we're shutting down, schedule state recovery! */
3136 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
3137 nfs4_schedule_state_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 return;
3139 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003140 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141}
3142
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003143static const struct rpc_call_ops nfs4_renew_ops = {
3144 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003145 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003146};
3147
David Howellsadfa6f92006-08-22 20:06:08 -04003148int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149{
3150 struct rpc_message msg = {
3151 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3152 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003153 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003155 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003157 if (!atomic_inc_not_zero(&clp->cl_count))
3158 return -EIO;
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003159 data = kmalloc(sizeof(*data), GFP_KERNEL);
3160 if (data == NULL)
3161 return -ENOMEM;
3162 data->client = clp;
3163 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003165 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166}
3167
David Howellsadfa6f92006-08-22 20:06:08 -04003168int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169{
3170 struct rpc_message msg = {
3171 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3172 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003173 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 };
3175 unsigned long now = jiffies;
3176 int status;
3177
3178 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3179 if (status < 0)
3180 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003181 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 return 0;
3183}
3184
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003185static inline int nfs4_server_supports_acls(struct nfs_server *server)
3186{
3187 return (server->caps & NFS_CAP_ACLS)
3188 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3189 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3190}
3191
3192/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3193 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3194 * the stack.
3195 */
3196#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3197
3198static void buf_to_pages(const void *buf, size_t buflen,
3199 struct page **pages, unsigned int *pgbase)
3200{
3201 const void *p = buf;
3202
3203 *pgbase = offset_in_page(buf);
3204 p -= *pgbase;
3205 while (p < buf + buflen) {
3206 *(pages++) = virt_to_page(p);
3207 p += PAGE_CACHE_SIZE;
3208 }
3209}
3210
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003211struct nfs4_cached_acl {
3212 int cached;
3213 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003214 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003215};
3216
3217static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003218{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003219 struct nfs_inode *nfsi = NFS_I(inode);
3220
3221 spin_lock(&inode->i_lock);
3222 kfree(nfsi->nfs4_acl);
3223 nfsi->nfs4_acl = acl;
3224 spin_unlock(&inode->i_lock);
3225}
3226
3227static void nfs4_zap_acl_attr(struct inode *inode)
3228{
3229 nfs4_set_cached_acl(inode, NULL);
3230}
3231
3232static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3233{
3234 struct nfs_inode *nfsi = NFS_I(inode);
3235 struct nfs4_cached_acl *acl;
3236 int ret = -ENOENT;
3237
3238 spin_lock(&inode->i_lock);
3239 acl = nfsi->nfs4_acl;
3240 if (acl == NULL)
3241 goto out;
3242 if (buf == NULL) /* user is just asking for length */
3243 goto out_len;
3244 if (acl->cached == 0)
3245 goto out;
3246 ret = -ERANGE; /* see getxattr(2) man page */
3247 if (acl->len > buflen)
3248 goto out;
3249 memcpy(buf, acl->data, acl->len);
3250out_len:
3251 ret = acl->len;
3252out:
3253 spin_unlock(&inode->i_lock);
3254 return ret;
3255}
3256
3257static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3258{
3259 struct nfs4_cached_acl *acl;
3260
3261 if (buf && acl_len <= PAGE_SIZE) {
3262 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3263 if (acl == NULL)
3264 goto out;
3265 acl->cached = 1;
3266 memcpy(acl->data, buf, acl_len);
3267 } else {
3268 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3269 if (acl == NULL)
3270 goto out;
3271 acl->cached = 0;
3272 }
3273 acl->len = acl_len;
3274out:
3275 nfs4_set_cached_acl(inode, acl);
3276}
3277
Trond Myklebust16b42892006-08-24 12:27:15 -04003278static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003279{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003280 struct page *pages[NFS4ACL_MAXPAGES];
3281 struct nfs_getaclargs args = {
3282 .fh = NFS_FH(inode),
3283 .acl_pages = pages,
3284 .acl_len = buflen,
3285 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003286 struct nfs_getaclres res = {
3287 .acl_len = buflen,
3288 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003289 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003290 struct rpc_message msg = {
3291 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3292 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003293 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003294 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003295 struct page *localpage = NULL;
3296 int ret;
3297
3298 if (buflen < PAGE_SIZE) {
3299 /* As long as we're doing a round trip to the server anyway,
3300 * let's be prepared for a page of acl data. */
3301 localpage = alloc_page(GFP_KERNEL);
3302 resp_buf = page_address(localpage);
3303 if (localpage == NULL)
3304 return -ENOMEM;
3305 args.acl_pages[0] = localpage;
3306 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003307 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003308 } else {
3309 resp_buf = buf;
3310 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3311 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003312 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003313 if (ret)
3314 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003315 if (res.acl_len > args.acl_len)
3316 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003317 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003318 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003319 if (buf) {
3320 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003321 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003322 goto out_free;
3323 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003324 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003325 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003326 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003327out_free:
3328 if (localpage)
3329 __free_page(localpage);
3330 return ret;
3331}
3332
Trond Myklebust16b42892006-08-24 12:27:15 -04003333static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3334{
3335 struct nfs4_exception exception = { };
3336 ssize_t ret;
3337 do {
3338 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3339 if (ret >= 0)
3340 break;
3341 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3342 } while (exception.retry);
3343 return ret;
3344}
3345
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003346static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3347{
3348 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003349 int ret;
3350
3351 if (!nfs4_server_supports_acls(server))
3352 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003353 ret = nfs_revalidate_inode(server, inode);
3354 if (ret < 0)
3355 return ret;
3356 ret = nfs4_read_cached_acl(inode, buf, buflen);
3357 if (ret != -ENOENT)
3358 return ret;
3359 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003360}
3361
Trond Myklebust16b42892006-08-24 12:27:15 -04003362static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003363{
3364 struct nfs_server *server = NFS_SERVER(inode);
3365 struct page *pages[NFS4ACL_MAXPAGES];
3366 struct nfs_setaclargs arg = {
3367 .fh = NFS_FH(inode),
3368 .acl_pages = pages,
3369 .acl_len = buflen,
3370 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003371 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003372 struct rpc_message msg = {
3373 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3374 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003375 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003376 };
3377 int ret;
3378
3379 if (!nfs4_server_supports_acls(server))
3380 return -EOPNOTSUPP;
Trond Myklebust642ac542005-10-18 14:20:19 -07003381 nfs_inode_return_delegation(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003382 buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003383 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003384 nfs_access_zap_cache(inode);
3385 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003386 return ret;
3387}
3388
Trond Myklebust16b42892006-08-24 12:27:15 -04003389static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3390{
3391 struct nfs4_exception exception = { };
3392 int err;
3393 do {
3394 err = nfs4_handle_exception(NFS_SERVER(inode),
3395 __nfs4_proc_set_acl(inode, buf, buflen),
3396 &exception);
3397 } while (exception.retry);
3398 return err;
3399}
3400
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003402nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003404 struct nfs_client *clp = server->nfs_client;
3405
3406 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 return 0;
3408 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003409 case -NFS4ERR_ADMIN_REVOKED:
3410 case -NFS4ERR_BAD_STATEID:
3411 case -NFS4ERR_OPENMODE:
3412 if (state == NULL)
3413 break;
3414 nfs4_state_mark_reclaim_nograce(clp, state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003415 goto do_state_recovery;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003417 if (state == NULL)
3418 break;
3419 nfs4_state_mark_reclaim_reboot(clp, state);
3420 case -NFS4ERR_STALE_CLIENTID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 case -NFS4ERR_EXPIRED:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003422 goto do_state_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003423#if defined(CONFIG_NFS_V4_1)
3424 case -NFS4ERR_BADSESSION:
3425 case -NFS4ERR_BADSLOT:
3426 case -NFS4ERR_BAD_HIGH_SLOT:
3427 case -NFS4ERR_DEADSESSION:
3428 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3429 case -NFS4ERR_SEQ_FALSE_RETRY:
3430 case -NFS4ERR_SEQ_MISORDERED:
3431 dprintk("%s ERROR %d, Reset session\n", __func__,
3432 task->tk_status);
Andy Adamson0b9e2d42009-12-04 16:02:14 -05003433 nfs4_schedule_state_recovery(clp);
Andy Adamson4745e312009-04-01 09:22:42 -04003434 task->tk_status = 0;
3435 return -EAGAIN;
3436#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003438 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003439 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003440 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3442 task->tk_status = 0;
3443 return -EAGAIN;
3444 case -NFS4ERR_OLD_STATEID:
3445 task->tk_status = 0;
3446 return -EAGAIN;
3447 }
3448 task->tk_status = nfs4_map_errors(task->tk_status);
3449 return 0;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003450do_state_recovery:
3451 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
3452 nfs4_schedule_state_recovery(clp);
3453 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3454 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3455 task->tk_status = 0;
3456 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457}
3458
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003459int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3460 unsigned short port, struct rpc_cred *cred,
3461 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462{
3463 nfs4_verifier sc_verifier;
3464 struct nfs4_setclientid setclientid = {
3465 .sc_verifier = &sc_verifier,
3466 .sc_prog = program,
3467 };
3468 struct rpc_message msg = {
3469 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3470 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003471 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003472 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 };
Al Virobc4785c2006-10-19 23:28:51 -07003474 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 int loop = 0;
3476 int status;
3477
Al Virobc4785c2006-10-19 23:28:51 -07003478 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3480 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3481
3482 for(;;) {
3483 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003484 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003485 clp->cl_ipaddr,
3486 rpc_peeraddr2str(clp->cl_rpcclient,
3487 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003488 rpc_peeraddr2str(clp->cl_rpcclient,
3489 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003490 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 clp->cl_id_uniquifier);
3492 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003493 sizeof(setclientid.sc_netid),
3494 rpc_peeraddr2str(clp->cl_rpcclient,
3495 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003497 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 clp->cl_ipaddr, port >> 8, port & 255);
3499
3500 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3501 if (status != -NFS4ERR_CLID_INUSE)
3502 break;
3503 if (signalled())
3504 break;
3505 if (loop++ & 1)
3506 ssleep(clp->cl_lease_time + 1);
3507 else
3508 if (++clp->cl_id_uniquifier == 0)
3509 break;
3510 }
3511 return status;
3512}
3513
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003514static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3515 struct nfs4_setclientid_res *arg,
3516 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517{
3518 struct nfs_fsinfo fsinfo;
3519 struct rpc_message msg = {
3520 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003521 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003523 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 };
3525 unsigned long now;
3526 int status;
3527
3528 now = jiffies;
3529 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3530 if (status == 0) {
3531 spin_lock(&clp->cl_lock);
3532 clp->cl_lease_time = fsinfo.lease_time * HZ;
3533 clp->cl_last_renewal = now;
3534 spin_unlock(&clp->cl_lock);
3535 }
3536 return status;
3537}
3538
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003539int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3540 struct nfs4_setclientid_res *arg,
3541 struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003542{
Benny Halevy77e03672008-06-24 20:25:57 +03003543 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003544 int err;
3545 do {
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003546 err = _nfs4_proc_setclientid_confirm(clp, arg, cred);
Trond Myklebust51581f32006-03-20 13:44:47 -05003547 switch (err) {
3548 case 0:
3549 return err;
3550 case -NFS4ERR_RESOURCE:
3551 /* The IBM lawyers misread another document! */
3552 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05003553 case -EKEYEXPIRED:
Trond Myklebust51581f32006-03-20 13:44:47 -05003554 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3555 }
3556 } while (err == 0);
3557 return err;
3558}
3559
Trond Myklebustfe650402006-01-03 09:55:18 +01003560struct nfs4_delegreturndata {
3561 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003562 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003563 struct nfs_fh fh;
3564 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003565 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003566 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003567 int rpc_status;
3568};
3569
Trond Myklebustfe650402006-01-03 09:55:18 +01003570static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3571{
3572 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003573
Trond Myklebust14516c32010-07-31 14:29:06 -04003574 if (!nfs4_sequence_done(task, &data->res.seq_res))
3575 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003576
Ricardo Labiaga79708862009-12-07 09:23:21 -05003577 switch (task->tk_status) {
3578 case -NFS4ERR_STALE_STATEID:
3579 case -NFS4ERR_EXPIRED:
3580 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003581 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003582 break;
3583 default:
3584 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3585 -EAGAIN) {
3586 nfs_restart_rpc(task, data->res.server->nfs_client);
3587 return;
3588 }
3589 }
3590 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003591}
3592
3593static void nfs4_delegreturn_release(void *calldata)
3594{
Trond Myklebustfe650402006-01-03 09:55:18 +01003595 kfree(calldata);
3596}
3597
Andy Adamson938e1012009-04-01 09:22:28 -04003598#if defined(CONFIG_NFS_V4_1)
3599static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3600{
3601 struct nfs4_delegreturndata *d_data;
3602
3603 d_data = (struct nfs4_delegreturndata *)data;
3604
Trond Myklebust035168ab2010-06-16 09:52:26 -04003605 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003606 &d_data->args.seq_args,
3607 &d_data->res.seq_res, 1, task))
3608 return;
3609 rpc_call_start(task);
3610}
3611#endif /* CONFIG_NFS_V4_1 */
3612
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003613static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003614#if defined(CONFIG_NFS_V4_1)
3615 .rpc_call_prepare = nfs4_delegreturn_prepare,
3616#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003617 .rpc_call_done = nfs4_delegreturn_done,
3618 .rpc_release = nfs4_delegreturn_release,
3619};
3620
Trond Myklebuste6f81072008-01-24 18:14:34 -05003621static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003622{
3623 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003624 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003625 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003626 struct rpc_message msg = {
3627 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3628 .rpc_cred = cred,
3629 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003630 struct rpc_task_setup task_setup_data = {
3631 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003632 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003633 .callback_ops = &nfs4_delegreturn_ops,
3634 .flags = RPC_TASK_ASYNC,
3635 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003636 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003637
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003638 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003639 if (data == NULL)
3640 return -ENOMEM;
3641 data->args.fhandle = &data->fh;
3642 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003643 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003644 nfs_copy_fh(&data->fh, NFS_FH(inode));
3645 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003646 data->res.fattr = &data->fattr;
3647 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003648 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003649 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003650 data->rpc_status = 0;
3651
Trond Myklebustc970aa82007-07-14 15:39:59 -04003652 task_setup_data.callback_data = data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003653 msg.rpc_argp = &data->args,
3654 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003655 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003656 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003657 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003658 if (!issync)
3659 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003660 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003661 if (status != 0)
3662 goto out;
3663 status = data->rpc_status;
3664 if (status != 0)
3665 goto out;
3666 nfs_refresh_inode(inode, &data->fattr);
3667out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003668 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003669 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670}
3671
Trond Myklebuste6f81072008-01-24 18:14:34 -05003672int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673{
3674 struct nfs_server *server = NFS_SERVER(inode);
3675 struct nfs4_exception exception = { };
3676 int err;
3677 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003678 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 switch (err) {
3680 case -NFS4ERR_STALE_STATEID:
3681 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 case 0:
3683 return 0;
3684 }
3685 err = nfs4_handle_exception(server, err, &exception);
3686 } while (exception.retry);
3687 return err;
3688}
3689
3690#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3691#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3692
3693/*
3694 * sleep, with exponential backoff, and retry the LOCK operation.
3695 */
3696static unsigned long
3697nfs4_set_lock_task_retry(unsigned long timeout)
3698{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003699 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 timeout <<= 1;
3701 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3702 return NFS4_LOCK_MAXTIMEOUT;
3703 return timeout;
3704}
3705
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3707{
3708 struct inode *inode = state->inode;
3709 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003710 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003711 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003713 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003715 struct nfs_lockt_res res = {
3716 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 };
3718 struct rpc_message msg = {
3719 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3720 .rpc_argp = &arg,
3721 .rpc_resp = &res,
3722 .rpc_cred = state->owner->so_cred,
3723 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 struct nfs4_lock_state *lsp;
3725 int status;
3726
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003727 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003728 status = nfs4_set_lock_state(state, request);
3729 if (status != 0)
3730 goto out;
3731 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003732 arg.lock_owner.id = lsp->ls_id.id;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003733 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003734 switch (status) {
3735 case 0:
3736 request->fl_type = F_UNLCK;
3737 break;
3738 case -NFS4ERR_DENIED:
3739 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003741 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003742out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 return status;
3744}
3745
3746static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3747{
3748 struct nfs4_exception exception = { };
3749 int err;
3750
3751 do {
3752 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3753 _nfs4_proc_getlk(state, cmd, request),
3754 &exception);
3755 } while (exception.retry);
3756 return err;
3757}
3758
3759static int do_vfs_lock(struct file *file, struct file_lock *fl)
3760{
3761 int res = 0;
3762 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3763 case FL_POSIX:
3764 res = posix_lock_file_wait(file, fl);
3765 break;
3766 case FL_FLOCK:
3767 res = flock_lock_file_wait(file, fl);
3768 break;
3769 default:
3770 BUG();
3771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 return res;
3773}
3774
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003775struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003776 struct nfs_locku_args arg;
3777 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003778 struct nfs4_lock_state *lsp;
3779 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003780 struct file_lock fl;
3781 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003782 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003783};
3784
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003785static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3786 struct nfs_open_context *ctx,
3787 struct nfs4_lock_state *lsp,
3788 struct nfs_seqid *seqid)
3789{
3790 struct nfs4_unlockdata *p;
3791 struct inode *inode = lsp->ls_state->inode;
3792
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003793 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003794 if (p == NULL)
3795 return NULL;
3796 p->arg.fh = NFS_FH(inode);
3797 p->arg.fl = &p->fl;
3798 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003799 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003800 p->arg.stateid = &lsp->ls_stateid;
3801 p->lsp = lsp;
3802 atomic_inc(&lsp->ls_count);
3803 /* Ensure we don't close file until we're done freeing locks! */
3804 p->ctx = get_nfs_open_context(ctx);
3805 memcpy(&p->fl, fl, sizeof(p->fl));
3806 p->server = NFS_SERVER(inode);
3807 return p;
3808}
3809
Trond Myklebust06f814a2006-01-03 09:55:07 +01003810static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003811{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003812 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003813 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003814 nfs4_put_lock_state(calldata->lsp);
3815 put_nfs_open_context(calldata->ctx);
3816 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003817}
3818
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003819static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003820{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003821 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003822
Trond Myklebust14516c32010-07-31 14:29:06 -04003823 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3824 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003825 switch (task->tk_status) {
3826 case 0:
3827 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003828 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003829 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003830 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003831 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003832 case -NFS4ERR_BAD_STATEID:
3833 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003834 case -NFS4ERR_STALE_STATEID:
3835 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003836 break;
3837 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003838 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebust0110ee12009-12-07 09:00:24 -05003839 nfs_restart_rpc(task,
Trond Myklebustd61e6122009-12-05 19:32:19 -05003840 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003841 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003842}
3843
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003844static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003845{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003846 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003848 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003849 return;
3850 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003851 /* Note: exit _without_ running nfs4_locku_done */
3852 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003853 return;
3854 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003855 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04003856 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04003857 &calldata->arg.seq_args,
3858 &calldata->res.seq_res, 1, task))
3859 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003860 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861}
3862
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003863static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003864 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003865 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003866 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003867};
3868
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003869static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3870 struct nfs_open_context *ctx,
3871 struct nfs4_lock_state *lsp,
3872 struct nfs_seqid *seqid)
3873{
3874 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003875 struct rpc_message msg = {
3876 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3877 .rpc_cred = ctx->cred,
3878 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003879 struct rpc_task_setup task_setup_data = {
3880 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003881 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003882 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003883 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003884 .flags = RPC_TASK_ASYNC,
3885 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003886
Frank Filz137d6ac2007-07-09 15:32:29 -07003887 /* Ensure this is an unlock - when canceling a lock, the
3888 * canceled lock is passed in, and it won't be an unlock.
3889 */
3890 fl->fl_type = F_UNLCK;
3891
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003892 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3893 if (data == NULL) {
3894 nfs_free_seqid(seqid);
3895 return ERR_PTR(-ENOMEM);
3896 }
3897
Trond Myklebust5138fde2007-07-14 15:40:01 -04003898 msg.rpc_argp = &data->arg,
3899 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003900 task_setup_data.callback_data = data;
3901 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003902}
3903
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3905{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003906 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003907 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003908 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003909 struct rpc_task *task;
3910 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003911 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912
Trond Myklebust9b073572006-06-29 16:38:34 -04003913 status = nfs4_set_lock_state(state, request);
3914 /* Unlock _before_ we do the RPC call */
3915 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003916 down_read(&nfsi->rwsem);
3917 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
3918 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003919 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05003920 }
3921 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04003922 if (status != 0)
3923 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05003924 /* Is this a delegated lock? */
3925 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04003926 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003927 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003928 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04003929 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003930 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04003931 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04003932 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003933 status = PTR_ERR(task);
3934 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04003935 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003936 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003937 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04003938out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04003939 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003940 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941}
3942
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003943struct nfs4_lockdata {
3944 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003945 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003946 struct nfs4_lock_state *lsp;
3947 struct nfs_open_context *ctx;
3948 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003949 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003950 int rpc_status;
3951 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04003952 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003953};
3954
3955static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003956 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
3957 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003958{
3959 struct nfs4_lockdata *p;
3960 struct inode *inode = lsp->ls_state->inode;
3961 struct nfs_server *server = NFS_SERVER(inode);
3962
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003963 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003964 if (p == NULL)
3965 return NULL;
3966
3967 p->arg.fh = NFS_FH(inode);
3968 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003969 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003970 if (p->arg.open_seqid == NULL)
3971 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003972 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003973 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003974 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003975 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04003976 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003977 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003978 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003979 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04003980 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003981 atomic_inc(&lsp->ls_count);
3982 p->ctx = get_nfs_open_context(ctx);
3983 memcpy(&p->fl, fl, sizeof(p->fl));
3984 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003985out_free_seqid:
3986 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003987out_free:
3988 kfree(p);
3989 return NULL;
3990}
3991
3992static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
3993{
3994 struct nfs4_lockdata *data = calldata;
3995 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996
Harvey Harrison3110ff82008-05-02 13:42:44 -07003997 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05003998 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
3999 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004000 /* Do we need to do an open_to_lock_owner? */
4001 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004002 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4003 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004004 data->arg.open_stateid = &state->stateid;
4005 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004006 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004007 } else
4008 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004009 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04004010 if (nfs4_setup_sequence(data->server,
4011 &data->arg.seq_args,
Andy Adamson66179ef2009-04-01 09:22:22 -04004012 &data->res.seq_res, 1, task))
4013 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004014 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004015 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004016}
4017
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004018static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4019{
4020 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4021 nfs4_lock_prepare(task, calldata);
4022}
4023
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004024static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4025{
4026 struct nfs4_lockdata *data = calldata;
4027
Harvey Harrison3110ff82008-05-02 13:42:44 -07004028 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004029
Trond Myklebust14516c32010-07-31 14:29:06 -04004030 if (!nfs4_sequence_done(task, &data->res.seq_res))
4031 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004032
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004033 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004034 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004035 if (data->rpc_status == 0)
4036 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4037 else
4038 goto out;
4039 }
4040 if (data->rpc_status == 0) {
4041 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4042 sizeof(data->lsp->ls_stateid.data));
4043 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04004044 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004045 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004046out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004047 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004048}
4049
4050static void nfs4_lock_release(void *calldata)
4051{
4052 struct nfs4_lockdata *data = calldata;
4053
Harvey Harrison3110ff82008-05-02 13:42:44 -07004054 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004055 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004056 if (data->cancelled != 0) {
4057 struct rpc_task *task;
4058 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4059 data->arg.lock_seqid);
4060 if (!IS_ERR(task))
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004061 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004062 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004063 } else
4064 nfs_free_seqid(data->arg.lock_seqid);
4065 nfs4_put_lock_state(data->lsp);
4066 put_nfs_open_context(data->ctx);
4067 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004068 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004069}
4070
4071static const struct rpc_call_ops nfs4_lock_ops = {
4072 .rpc_call_prepare = nfs4_lock_prepare,
4073 .rpc_call_done = nfs4_lock_done,
4074 .rpc_release = nfs4_lock_release,
4075};
4076
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004077static const struct rpc_call_ops nfs4_recover_lock_ops = {
4078 .rpc_call_prepare = nfs4_recover_lock_prepare,
4079 .rpc_call_done = nfs4_lock_done,
4080 .rpc_release = nfs4_lock_release,
4081};
4082
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004083static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4084{
4085 struct nfs_client *clp = server->nfs_client;
4086 struct nfs4_state *state = lsp->ls_state;
4087
4088 switch (error) {
4089 case -NFS4ERR_ADMIN_REVOKED:
4090 case -NFS4ERR_BAD_STATEID:
4091 case -NFS4ERR_EXPIRED:
4092 if (new_lock_owner != 0 ||
4093 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4094 nfs4_state_mark_reclaim_nograce(clp, state);
4095 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004096 break;
4097 case -NFS4ERR_STALE_STATEID:
4098 if (new_lock_owner != 0 ||
4099 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
4100 nfs4_state_mark_reclaim_reboot(clp, state);
4101 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004102 };
4103}
4104
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004105static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004106{
4107 struct nfs4_lockdata *data;
4108 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004109 struct rpc_message msg = {
4110 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4111 .rpc_cred = state->owner->so_cred,
4112 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004113 struct rpc_task_setup task_setup_data = {
4114 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004115 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004116 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004117 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004118 .flags = RPC_TASK_ASYNC,
4119 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004120 int ret;
4121
Harvey Harrison3110ff82008-05-02 13:42:44 -07004122 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004123 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004124 fl->fl_u.nfs4_fl.owner,
4125 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004126 if (data == NULL)
4127 return -ENOMEM;
4128 if (IS_SETLKW(cmd))
4129 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004130 if (recovery_type > NFS_LOCK_NEW) {
4131 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004132 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004133 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4134 }
Trond Myklebust5138fde2007-07-14 15:40:01 -04004135 msg.rpc_argp = &data->arg,
4136 msg.rpc_resp = &data->res,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004137 task_setup_data.callback_data = data;
4138 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004139 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004140 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004141 ret = nfs4_wait_for_completion_rpc_task(task);
4142 if (ret == 0) {
4143 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004144 if (ret)
4145 nfs4_handle_setlk_error(data->server, data->lsp,
4146 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004147 } else
4148 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004149 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004150 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004151 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152}
4153
4154static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4155{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004156 struct nfs_server *server = NFS_SERVER(state->inode);
4157 struct nfs4_exception exception = { };
4158 int err;
4159
4160 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004161 /* Cache the lock if possible... */
4162 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4163 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004164 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Jeff Layton2c643482010-01-07 09:42:03 -05004165 if (err != -NFS4ERR_DELAY && err != -EKEYEXPIRED)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004166 break;
4167 nfs4_handle_exception(server, err, &exception);
4168 } while (exception.retry);
4169 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170}
4171
4172static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4173{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004174 struct nfs_server *server = NFS_SERVER(state->inode);
4175 struct nfs4_exception exception = { };
4176 int err;
4177
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004178 err = nfs4_set_lock_state(state, request);
4179 if (err != 0)
4180 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004181 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004182 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4183 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004184 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004185 switch (err) {
4186 default:
4187 goto out;
4188 case -NFS4ERR_GRACE:
4189 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05004190 case -EKEYEXPIRED:
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004191 nfs4_handle_exception(server, err, &exception);
4192 err = 0;
4193 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004194 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004195out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004196 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197}
4198
4199static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4200{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004201 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004202 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004203 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004205 if ((fl_flags & FL_POSIX) &&
4206 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4207 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004208 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004209 status = nfs4_set_lock_state(state, request);
4210 if (status != 0)
4211 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004212 request->fl_flags |= FL_ACCESS;
4213 status = do_vfs_lock(request->fl_file, request);
4214 if (status < 0)
4215 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004216 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004217 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004218 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004219 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004220 request->fl_flags = fl_flags & ~FL_SLEEP;
4221 status = do_vfs_lock(request->fl_file, request);
4222 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004223 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004224 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004225 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004226 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004227 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004228 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004229 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004230 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004231out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004232 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004233out:
4234 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 return status;
4236}
4237
4238static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4239{
4240 struct nfs4_exception exception = { };
4241 int err;
4242
4243 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004244 err = _nfs4_proc_setlk(state, cmd, request);
4245 if (err == -NFS4ERR_DENIED)
4246 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004248 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 } while (exception.retry);
4250 return err;
4251}
4252
4253static int
4254nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4255{
4256 struct nfs_open_context *ctx;
4257 struct nfs4_state *state;
4258 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4259 int status;
4260
4261 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004262 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 state = ctx->state;
4264
4265 if (request->fl_start < 0 || request->fl_end < 0)
4266 return -EINVAL;
4267
Trond Myklebustd9531262009-07-21 19:22:38 -04004268 if (IS_GETLK(cmd)) {
4269 if (state != NULL)
4270 return nfs4_proc_getlk(state, F_GETLK, request);
4271 return 0;
4272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273
4274 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4275 return -EINVAL;
4276
Trond Myklebustd9531262009-07-21 19:22:38 -04004277 if (request->fl_type == F_UNLCK) {
4278 if (state != NULL)
4279 return nfs4_proc_unlck(state, cmd, request);
4280 return 0;
4281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282
Trond Myklebustd9531262009-07-21 19:22:38 -04004283 if (state == NULL)
4284 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 do {
4286 status = nfs4_proc_setlk(state, cmd, request);
4287 if ((status != -EAGAIN) || IS_SETLK(cmd))
4288 break;
4289 timeout = nfs4_set_lock_task_retry(timeout);
4290 status = -ERESTARTSYS;
4291 if (signalled())
4292 break;
4293 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 return status;
4295}
4296
Trond Myklebust888e6942005-11-04 15:38:11 -05004297int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4298{
4299 struct nfs_server *server = NFS_SERVER(state->inode);
4300 struct nfs4_exception exception = { };
4301 int err;
4302
4303 err = nfs4_set_lock_state(state, fl);
4304 if (err != 0)
4305 goto out;
4306 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004307 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004308 switch (err) {
4309 default:
4310 printk(KERN_ERR "%s: unhandled error %d.\n",
4311 __func__, err);
4312 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004313 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004314 goto out;
4315 case -NFS4ERR_EXPIRED:
4316 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004317 case -NFS4ERR_STALE_STATEID:
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004318 case -NFS4ERR_BADSESSION:
4319 case -NFS4ERR_BADSLOT:
4320 case -NFS4ERR_BAD_HIGH_SLOT:
4321 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4322 case -NFS4ERR_DEADSESSION:
Trond Myklebustd5122202009-06-17 13:22:58 -07004323 nfs4_schedule_state_recovery(server->nfs_client);
4324 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004325 case -ERESTARTSYS:
4326 /*
4327 * The show must go on: exit, but mark the
4328 * stateid as needing recovery.
4329 */
4330 case -NFS4ERR_ADMIN_REVOKED:
4331 case -NFS4ERR_BAD_STATEID:
4332 case -NFS4ERR_OPENMODE:
4333 nfs4_state_mark_reclaim_nograce(server->nfs_client, state);
4334 err = 0;
4335 goto out;
4336 case -ENOMEM:
4337 case -NFS4ERR_DENIED:
4338 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4339 err = 0;
4340 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004341 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -05004342 case -EKEYEXPIRED:
Trond Myklebustd5122202009-06-17 13:22:58 -07004343 break;
4344 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004345 err = nfs4_handle_exception(server, err, &exception);
4346 } while (exception.retry);
4347out:
4348 return err;
4349}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004350
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004351static void nfs4_release_lockowner_release(void *calldata)
4352{
4353 kfree(calldata);
4354}
4355
4356const struct rpc_call_ops nfs4_release_lockowner_ops = {
4357 .rpc_release = nfs4_release_lockowner_release,
4358};
4359
4360void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4361{
4362 struct nfs_server *server = lsp->ls_state->owner->so_server;
4363 struct nfs_release_lockowner_args *args;
4364 struct rpc_message msg = {
4365 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4366 };
4367
4368 if (server->nfs_client->cl_mvops->minor_version != 0)
4369 return;
4370 args = kmalloc(sizeof(*args), GFP_NOFS);
4371 if (!args)
4372 return;
4373 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4374 args->lock_owner.id = lsp->ls_id.id;
4375 msg.rpc_argp = args;
4376 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4377}
4378
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004379#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4380
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004381int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf,
4382 size_t buflen, int flags)
4383{
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004384 struct inode *inode = dentry->d_inode;
4385
4386 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4387 return -EOPNOTSUPP;
4388
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004389 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004390}
4391
4392/* The getxattr man page suggests returning -ENODATA for unknown attributes,
4393 * and that's what we'll do for e.g. user attributes that haven't been set.
4394 * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported
4395 * attributes in kernel-managed attribute namespaces. */
4396ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf,
4397 size_t buflen)
4398{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004399 struct inode *inode = dentry->d_inode;
4400
4401 if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0)
4402 return -EOPNOTSUPP;
4403
4404 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004405}
4406
4407ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen)
4408{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004409 size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004410
J. Bruce Fields096455a2006-03-20 23:23:42 -05004411 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4412 return 0;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004413 if (buf && buflen < len)
4414 return -ERANGE;
4415 if (buf)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004416 memcpy(buf, XATTR_NAME_NFSV4_ACL, len);
4417 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004418}
4419
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004420static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4421{
4422 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4423 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4424 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4425 return;
4426
4427 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4428 NFS_ATTR_FATTR_NLINK;
4429 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4430 fattr->nlink = 2;
4431}
4432
Trond Myklebust56659e92007-07-17 21:52:39 -04004433int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004434 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004435{
4436 struct nfs_server *server = NFS_SERVER(dir);
4437 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004438 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4439 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004440 };
4441 struct nfs4_fs_locations_arg args = {
4442 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004443 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004444 .page = page,
4445 .bitmask = bitmask,
4446 };
Benny Halevy22958462009-04-01 09:22:02 -04004447 struct nfs4_fs_locations_res res = {
4448 .fs_locations = fs_locations,
4449 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004450 struct rpc_message msg = {
4451 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4452 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004453 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004454 };
4455 int status;
4456
Harvey Harrison3110ff82008-05-02 13:42:44 -07004457 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004458 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004459 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004460 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04004461 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004462 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004463 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004464 return status;
4465}
4466
Andy Adamson557134a2009-04-01 09:21:53 -04004467#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004468/*
4469 * nfs4_proc_exchange_id()
4470 *
4471 * Since the clientid has expired, all compounds using sessions
4472 * associated with the stale clientid will be returning
4473 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4474 * be in some phase of session reset.
4475 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004476int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004477{
4478 nfs4_verifier verifier;
4479 struct nfs41_exchange_id_args args = {
4480 .client = clp,
4481 .flags = clp->cl_exchange_flags,
4482 };
4483 struct nfs41_exchange_id_res res = {
4484 .client = clp,
4485 };
4486 int status;
4487 struct rpc_message msg = {
4488 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4489 .rpc_argp = &args,
4490 .rpc_resp = &res,
4491 .rpc_cred = cred,
4492 };
4493 __be32 *p;
4494
4495 dprintk("--> %s\n", __func__);
4496 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004497
Alexandros Batsakis7b183d02009-12-05 13:33:25 -05004498 /* Remove server-only flags */
4499 args.flags &= ~EXCHGID4_FLAG_CONFIRMED_R;
4500
Benny Halevy99fe60d2009-04-01 09:22:29 -04004501 p = (u32 *)verifier.data;
4502 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4503 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4504 args.verifier = &verifier;
4505
4506 while (1) {
4507 args.id_len = scnprintf(args.id, sizeof(args.id),
4508 "%s/%s %u",
4509 clp->cl_ipaddr,
4510 rpc_peeraddr2str(clp->cl_rpcclient,
4511 RPC_DISPLAY_ADDR),
4512 clp->cl_id_uniquifier);
4513
4514 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4515
Andy Adamson180b62a2010-03-02 13:19:36 -05004516 if (status != -NFS4ERR_CLID_INUSE)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004517 break;
4518
4519 if (signalled())
4520 break;
4521
4522 if (++clp->cl_id_uniquifier == 0)
4523 break;
4524 }
4525
4526 dprintk("<-- %s status= %d\n", __func__, status);
4527 return status;
4528}
4529
Andy Adamson2050f0c2009-04-01 09:22:30 -04004530struct nfs4_get_lease_time_data {
4531 struct nfs4_get_lease_time_args *args;
4532 struct nfs4_get_lease_time_res *res;
4533 struct nfs_client *clp;
4534};
4535
4536static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4537 void *calldata)
4538{
4539 int ret;
4540 struct nfs4_get_lease_time_data *data =
4541 (struct nfs4_get_lease_time_data *)calldata;
4542
4543 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004544 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004545 /* just setup sequence, do not trigger session recovery
4546 since we're invoked within one */
4547 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004548 &data->args->la_seq_args,
4549 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004550
4551 BUG_ON(ret == -EAGAIN);
4552 rpc_call_start(task);
4553 dprintk("<-- %s\n", __func__);
4554}
4555
4556/*
4557 * Called from nfs4_state_manager thread for session setup, so don't recover
4558 * from sequence operation or clientid errors.
4559 */
4560static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4561{
4562 struct nfs4_get_lease_time_data *data =
4563 (struct nfs4_get_lease_time_data *)calldata;
4564
4565 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04004566 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4567 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04004568 switch (task->tk_status) {
4569 case -NFS4ERR_DELAY:
4570 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05004571 case -EKEYEXPIRED:
Andy Adamson2050f0c2009-04-01 09:22:30 -04004572 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4573 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4574 task->tk_status = 0;
Trond Myklebust0110ee12009-12-07 09:00:24 -05004575 nfs_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004576 return;
4577 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004578 dprintk("<-- %s\n", __func__);
4579}
4580
4581struct rpc_call_ops nfs4_get_lease_time_ops = {
4582 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4583 .rpc_call_done = nfs4_get_lease_time_done,
4584};
4585
4586int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4587{
4588 struct rpc_task *task;
4589 struct nfs4_get_lease_time_args args;
4590 struct nfs4_get_lease_time_res res = {
4591 .lr_fsinfo = fsinfo,
4592 };
4593 struct nfs4_get_lease_time_data data = {
4594 .args = &args,
4595 .res = &res,
4596 .clp = clp,
4597 };
4598 struct rpc_message msg = {
4599 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4600 .rpc_argp = &args,
4601 .rpc_resp = &res,
4602 };
4603 struct rpc_task_setup task_setup = {
4604 .rpc_client = clp->cl_rpcclient,
4605 .rpc_message = &msg,
4606 .callback_ops = &nfs4_get_lease_time_ops,
4607 .callback_data = &data
4608 };
4609 int status;
4610
Andy Adamson2050f0c2009-04-01 09:22:30 -04004611 dprintk("--> %s\n", __func__);
4612 task = rpc_run_task(&task_setup);
4613
4614 if (IS_ERR(task))
4615 status = PTR_ERR(task);
4616 else {
4617 status = task->tk_status;
4618 rpc_put_task(task);
4619 }
4620 dprintk("<-- %s return %d\n", __func__, status);
4621
4622 return status;
4623}
4624
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004625/*
4626 * Reset a slot table
4627 */
Andy Adamson104aeba2010-01-14 17:45:10 -05004628static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4629 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004630{
Andy Adamson104aeba2010-01-14 17:45:10 -05004631 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004632 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004633 int ret = 0;
4634
Andy Adamson104aeba2010-01-14 17:45:10 -05004635 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4636 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004637
Andy Adamson104aeba2010-01-14 17:45:10 -05004638 /* Does the newly negotiated max_reqs match the existing slot table? */
4639 if (max_reqs != tbl->max_slots) {
4640 ret = -ENOMEM;
4641 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004642 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05004643 if (!new)
4644 goto out;
4645 ret = 0;
4646 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004647 }
4648 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05004649 if (new) {
4650 tbl->slots = new;
4651 tbl->max_slots = max_reqs;
4652 }
4653 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004654 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004655 spin_unlock(&tbl->slot_tbl_lock);
4656 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4657 tbl, tbl->slots, tbl->max_slots);
4658out:
4659 dprintk("<-- %s: return %d\n", __func__, ret);
4660 return ret;
4661}
4662
Andy Adamsonfc931582009-04-01 09:22:31 -04004663/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004664 * Reset the forechannel and backchannel slot tables
4665 */
4666static int nfs4_reset_slot_tables(struct nfs4_session *session)
4667{
4668 int status;
4669
4670 status = nfs4_reset_slot_table(&session->fc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004671 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004672 if (status)
4673 return status;
4674
4675 status = nfs4_reset_slot_table(&session->bc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004676 session->bc_attrs.max_reqs, 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004677 return status;
4678}
4679
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004680/* Destroy the slot table */
4681static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4682{
4683 if (session->fc_slot_table.slots != NULL) {
4684 kfree(session->fc_slot_table.slots);
4685 session->fc_slot_table.slots = NULL;
4686 }
4687 if (session->bc_slot_table.slots != NULL) {
4688 kfree(session->bc_slot_table.slots);
4689 session->bc_slot_table.slots = NULL;
4690 }
4691 return;
4692}
4693
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004694/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004695 * Initialize slot table
4696 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004697static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4698 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04004699{
Andy Adamsonfc931582009-04-01 09:22:31 -04004700 struct nfs4_slot *slot;
4701 int ret = -ENOMEM;
4702
4703 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4704
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004705 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04004706
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004707 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04004708 if (!slot)
4709 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04004710 ret = 0;
4711
4712 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04004713 tbl->max_slots = max_slots;
4714 tbl->slots = slot;
4715 tbl->highest_used_slotid = -1; /* no slot is currently used */
4716 spin_unlock(&tbl->slot_tbl_lock);
4717 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4718 tbl, tbl->slots, tbl->max_slots);
4719out:
4720 dprintk("<-- %s: return %d\n", __func__, ret);
4721 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04004722}
4723
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004724/*
4725 * Initialize the forechannel and backchannel tables
4726 */
4727static int nfs4_init_slot_tables(struct nfs4_session *session)
4728{
Trond Myklebustf26468f2009-12-05 19:32:11 -05004729 struct nfs4_slot_table *tbl;
4730 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004731
Trond Myklebustf26468f2009-12-05 19:32:11 -05004732 tbl = &session->fc_slot_table;
4733 if (tbl->slots == NULL) {
4734 status = nfs4_init_slot_table(tbl,
4735 session->fc_attrs.max_reqs, 1);
4736 if (status)
4737 return status;
4738 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004739
Trond Myklebustf26468f2009-12-05 19:32:11 -05004740 tbl = &session->bc_slot_table;
4741 if (tbl->slots == NULL) {
4742 status = nfs4_init_slot_table(tbl,
4743 session->bc_attrs.max_reqs, 0);
4744 if (status)
4745 nfs4_destroy_slot_tables(session);
4746 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004747
4748 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04004749}
4750
4751struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4752{
4753 struct nfs4_session *session;
4754 struct nfs4_slot_table *tbl;
4755
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004756 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04004757 if (!session)
4758 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04004759
Andy Adamsonea028ac2009-12-04 15:55:38 -05004760 init_completion(&session->complete);
Andy Adamson76db6d92009-04-01 09:22:38 -04004761
Andy Adamson557134a2009-04-01 09:21:53 -04004762 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004763 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04004764 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004765 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004766
4767 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004768 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004769 spin_lock_init(&tbl->slot_tbl_lock);
4770 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
4771
Trond Myklebust1055d762010-06-16 09:52:27 -04004772 session->session_state = 1<<NFS4_SESSION_INITING;
4773
Andy Adamson557134a2009-04-01 09:21:53 -04004774 session->clp = clp;
4775 return session;
4776}
4777
4778void nfs4_destroy_session(struct nfs4_session *session)
4779{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04004780 nfs4_proc_destroy_session(session);
4781 dprintk("%s Destroy backchannel for xprt %p\n",
4782 __func__, session->clp->cl_rpcclient->cl_xprt);
4783 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4784 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004785 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04004786 kfree(session);
4787}
4788
Andy Adamsonfc931582009-04-01 09:22:31 -04004789/*
4790 * Initialize the values to be used by the client in CREATE_SESSION
4791 * If nfs4_init_session set the fore channel request and response sizes,
4792 * use them.
4793 *
4794 * Set the back channel max_resp_sz_cached to zero to force the client to
4795 * always set csa_cachethis to FALSE because the current implementation
4796 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4797 */
4798static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4799{
4800 struct nfs4_session *session = args->client->cl_session;
4801 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4802 mxresp_sz = session->fc_attrs.max_resp_sz;
4803
4804 if (mxrqst_sz == 0)
4805 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4806 if (mxresp_sz == 0)
4807 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4808 /* Fore channel attributes */
4809 args->fc_attrs.headerpadsz = 0;
4810 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4811 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04004812 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4813 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4814
4815 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05004816 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04004817 __func__,
4818 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05004819 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04004820
4821 /* Back channel attributes */
4822 args->bc_attrs.headerpadsz = 0;
4823 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4824 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4825 args->bc_attrs.max_resp_sz_cached = 0;
4826 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4827 args->bc_attrs.max_reqs = 1;
4828
4829 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4830 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4831 __func__,
4832 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4833 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4834 args->bc_attrs.max_reqs);
4835}
4836
Andy Adamson8d353012009-04-01 09:22:32 -04004837static int _verify_channel_attr(char *chan, char *attr_name, u32 sent, u32 rcvd)
4838{
4839 if (rcvd <= sent)
4840 return 0;
4841 printk(KERN_WARNING "%s: Session INVALID: %s channel %s increased. "
4842 "sent=%u rcvd=%u\n", __func__, chan, attr_name, sent, rcvd);
4843 return -EINVAL;
4844}
4845
4846#define _verify_fore_channel_attr(_name_) \
4847 _verify_channel_attr("fore", #_name_, \
4848 args->fc_attrs._name_, \
4849 session->fc_attrs._name_)
4850
4851#define _verify_back_channel_attr(_name_) \
4852 _verify_channel_attr("back", #_name_, \
4853 args->bc_attrs._name_, \
4854 session->bc_attrs._name_)
4855
4856/*
4857 * The server is not allowed to increase the fore channel header pad size,
4858 * maximum response size, or maximum number of operations.
4859 *
4860 * The back channel attributes are only negotiatied down: We send what the
4861 * (back channel) server insists upon.
4862 */
4863static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4864 struct nfs4_session *session)
4865{
4866 int ret = 0;
4867
4868 ret |= _verify_fore_channel_attr(headerpadsz);
4869 ret |= _verify_fore_channel_attr(max_resp_sz);
4870 ret |= _verify_fore_channel_attr(max_ops);
4871
4872 ret |= _verify_back_channel_attr(headerpadsz);
4873 ret |= _verify_back_channel_attr(max_rqst_sz);
4874 ret |= _verify_back_channel_attr(max_resp_sz);
4875 ret |= _verify_back_channel_attr(max_resp_sz_cached);
4876 ret |= _verify_back_channel_attr(max_ops);
4877 ret |= _verify_back_channel_attr(max_reqs);
4878
4879 return ret;
4880}
4881
Andy Adamsonfc931582009-04-01 09:22:31 -04004882static int _nfs4_proc_create_session(struct nfs_client *clp)
4883{
4884 struct nfs4_session *session = clp->cl_session;
4885 struct nfs41_create_session_args args = {
4886 .client = clp,
4887 .cb_program = NFS4_CALLBACK,
4888 };
4889 struct nfs41_create_session_res res = {
4890 .client = clp,
4891 };
4892 struct rpc_message msg = {
4893 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4894 .rpc_argp = &args,
4895 .rpc_resp = &res,
4896 };
4897 int status;
4898
4899 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04004900 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04004901
4902 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4903
Andy Adamson8d353012009-04-01 09:22:32 -04004904 if (!status)
4905 /* Verify the session's negotiated channel_attrs values */
4906 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04004907 if (!status) {
4908 /* Increment the clientid slot sequence id */
4909 clp->cl_seqid++;
4910 }
4911
4912 return status;
4913}
4914
4915/*
4916 * Issues a CREATE_SESSION operation to the server.
4917 * It is the responsibility of the caller to verify the session is
4918 * expired before calling this routine.
4919 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05004920int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04004921{
4922 int status;
4923 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04004924 struct nfs4_session *session = clp->cl_session;
4925
4926 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
4927
4928 status = _nfs4_proc_create_session(clp);
4929 if (status)
4930 goto out;
4931
Trond Myklebustf26468f2009-12-05 19:32:11 -05004932 /* Init and reset the fore channel */
4933 status = nfs4_init_slot_tables(session);
4934 dprintk("slot table initialization returned %d\n", status);
4935 if (status)
4936 goto out;
4937 status = nfs4_reset_slot_tables(session);
4938 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04004939 if (status)
4940 goto out;
4941
4942 ptr = (unsigned *)&session->sess_id.data[0];
4943 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
4944 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04004945out:
4946 dprintk("<-- %s\n", __func__);
4947 return status;
4948}
4949
Andy Adamson0f3e66c2009-04-01 09:22:34 -04004950/*
4951 * Issue the over-the-wire RPC DESTROY_SESSION.
4952 * The caller must serialize access to this routine.
4953 */
4954int nfs4_proc_destroy_session(struct nfs4_session *session)
4955{
4956 int status = 0;
4957 struct rpc_message msg;
4958
4959 dprintk("--> nfs4_proc_destroy_session\n");
4960
4961 /* session is still being setup */
4962 if (session->clp->cl_cons_state != NFS_CS_READY)
4963 return status;
4964
4965 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
4966 msg.rpc_argp = session;
4967 msg.rpc_resp = NULL;
4968 msg.rpc_cred = NULL;
4969 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
4970
4971 if (status)
4972 printk(KERN_WARNING
4973 "Got error %d from the server on DESTROY_SESSION. "
4974 "Session has been destroyed regardless...\n", status);
4975
4976 dprintk("<-- nfs4_proc_destroy_session\n");
4977 return status;
4978}
4979
Trond Myklebustfccba802009-07-21 16:48:07 -04004980int nfs4_init_session(struct nfs_server *server)
4981{
4982 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05004983 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05004984 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04004985 int ret;
4986
4987 if (!nfs4_has_session(clp))
4988 return 0;
4989
Trond Myklebust1055d762010-06-16 09:52:27 -04004990 session = clp->cl_session;
4991 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
4992 return 0;
4993
Andy Adamson68bf05e2009-12-15 12:55:02 -05004994 rsize = server->rsize;
4995 if (rsize == 0)
4996 rsize = NFS_MAX_FILE_IO_SIZE;
4997 wsize = server->wsize;
4998 if (wsize == 0)
4999 wsize = NFS_MAX_FILE_IO_SIZE;
5000
Andy Adamson68bf05e2009-12-15 12:55:02 -05005001 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5002 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005003
Trond Myklebustfccba802009-07-21 16:48:07 -04005004 ret = nfs4_recover_expired_lease(server);
5005 if (!ret)
5006 ret = nfs4_check_client_ready(clp);
5007 return ret;
5008}
5009
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005010/*
5011 * Renew the cl_session lease.
5012 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005013struct nfs4_sequence_data {
5014 struct nfs_client *clp;
5015 struct nfs4_sequence_args args;
5016 struct nfs4_sequence_res res;
5017};
5018
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005019static void nfs41_sequence_release(void *data)
5020{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005021 struct nfs4_sequence_data *calldata = data;
5022 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005023
Alexandros Batsakis71358402010-02-05 03:45:05 -08005024 if (atomic_read(&clp->cl_count) > 1)
5025 nfs4_schedule_state_renewal(clp);
5026 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005027 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005028}
5029
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005030static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5031{
5032 switch(task->tk_status) {
5033 case -NFS4ERR_DELAY:
5034 case -EKEYEXPIRED:
5035 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5036 return -EAGAIN;
5037 default:
5038 nfs4_schedule_state_recovery(clp);
5039 }
5040 return 0;
5041}
5042
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005043static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005044{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005045 struct nfs4_sequence_data *calldata = data;
5046 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005047
Trond Myklebust14516c32010-07-31 14:29:06 -04005048 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5049 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005050
5051 if (task->tk_status < 0) {
5052 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005053 if (atomic_read(&clp->cl_count) == 1)
5054 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005055
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005056 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5057 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005058 return;
5059 }
5060 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005061 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005062out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005063 dprintk("<-- %s\n", __func__);
5064}
5065
5066static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5067{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005068 struct nfs4_sequence_data *calldata = data;
5069 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005070 struct nfs4_sequence_args *args;
5071 struct nfs4_sequence_res *res;
5072
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005073 args = task->tk_msg.rpc_argp;
5074 res = task->tk_msg.rpc_resp;
5075
Trond Myklebust035168ab2010-06-16 09:52:26 -04005076 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005077 return;
5078 rpc_call_start(task);
5079}
5080
5081static const struct rpc_call_ops nfs41_sequence_ops = {
5082 .rpc_call_done = nfs41_sequence_call_done,
5083 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005084 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005085};
5086
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005087static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005088{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005089 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005090 struct rpc_message msg = {
5091 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5092 .rpc_cred = cred,
5093 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005094 struct rpc_task_setup task_setup_data = {
5095 .rpc_client = clp->cl_rpcclient,
5096 .rpc_message = &msg,
5097 .callback_ops = &nfs41_sequence_ops,
5098 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5099 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005100
Alexandros Batsakis71358402010-02-05 03:45:05 -08005101 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005102 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005103 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005104 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005105 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005106 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005107 }
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005108 msg.rpc_argp = &calldata->args;
5109 msg.rpc_resp = &calldata->res;
5110 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005111 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005112
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005113 return rpc_run_task(&task_setup_data);
5114}
5115
5116static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5117{
5118 struct rpc_task *task;
5119 int ret = 0;
5120
5121 task = _nfs41_proc_sequence(clp, cred);
5122 if (IS_ERR(task))
5123 ret = PTR_ERR(task);
5124 else
5125 rpc_put_task(task);
5126 dprintk("<-- %s status=%d\n", __func__, ret);
5127 return ret;
5128}
5129
5130static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5131{
5132 struct rpc_task *task;
5133 int ret;
5134
5135 task = _nfs41_proc_sequence(clp, cred);
5136 if (IS_ERR(task)) {
5137 ret = PTR_ERR(task);
5138 goto out;
5139 }
5140 ret = rpc_wait_for_completion_task(task);
5141 if (!ret)
5142 ret = task->tk_status;
5143 rpc_put_task(task);
5144out:
5145 dprintk("<-- %s status=%d\n", __func__, ret);
5146 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005147}
5148
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005149struct nfs4_reclaim_complete_data {
5150 struct nfs_client *clp;
5151 struct nfs41_reclaim_complete_args arg;
5152 struct nfs41_reclaim_complete_res res;
5153};
5154
5155static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5156{
5157 struct nfs4_reclaim_complete_data *calldata = data;
5158
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005159 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168ab2010-06-16 09:52:26 -04005160 if (nfs41_setup_sequence(calldata->clp->cl_session,
5161 &calldata->arg.seq_args,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005162 &calldata->res.seq_res, 0, task))
5163 return;
5164
5165 rpc_call_start(task);
5166}
5167
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005168static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5169{
5170 switch(task->tk_status) {
5171 case 0:
5172 case -NFS4ERR_COMPLETE_ALREADY:
5173 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5174 break;
5175 case -NFS4ERR_DELAY:
5176 case -EKEYEXPIRED:
5177 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5178 return -EAGAIN;
5179 default:
5180 nfs4_schedule_state_recovery(clp);
5181 }
5182 return 0;
5183}
5184
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005185static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5186{
5187 struct nfs4_reclaim_complete_data *calldata = data;
5188 struct nfs_client *clp = calldata->clp;
5189 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5190
5191 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005192 if (!nfs41_sequence_done(task, res))
5193 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005194
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005195 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5196 rpc_restart_call_prepare(task);
5197 return;
5198 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005199 dprintk("<-- %s\n", __func__);
5200}
5201
5202static void nfs4_free_reclaim_complete_data(void *data)
5203{
5204 struct nfs4_reclaim_complete_data *calldata = data;
5205
5206 kfree(calldata);
5207}
5208
5209static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5210 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5211 .rpc_call_done = nfs4_reclaim_complete_done,
5212 .rpc_release = nfs4_free_reclaim_complete_data,
5213};
5214
5215/*
5216 * Issue a global reclaim complete.
5217 */
5218static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5219{
5220 struct nfs4_reclaim_complete_data *calldata;
5221 struct rpc_task *task;
5222 struct rpc_message msg = {
5223 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5224 };
5225 struct rpc_task_setup task_setup_data = {
5226 .rpc_client = clp->cl_rpcclient,
5227 .rpc_message = &msg,
5228 .callback_ops = &nfs4_reclaim_complete_call_ops,
5229 .flags = RPC_TASK_ASYNC,
5230 };
5231 int status = -ENOMEM;
5232
5233 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005234 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005235 if (calldata == NULL)
5236 goto out;
5237 calldata->clp = clp;
5238 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005239
5240 msg.rpc_argp = &calldata->arg;
5241 msg.rpc_resp = &calldata->res;
5242 task_setup_data.callback_data = calldata;
5243 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005244 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005245 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005246 goto out;
5247 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005248 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005249 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005250out:
5251 dprintk("<-- %s status=%d\n", __func__, status);
5252 return status;
5253}
Andy Adamson557134a2009-04-01 09:21:53 -04005254#endif /* CONFIG_NFS_V4_1 */
5255
Andy Adamson591d71c2009-04-01 09:22:47 -04005256struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005257 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005258 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 .recover_open = nfs4_open_reclaim,
5260 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04005261 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005262 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263};
5264
Andy Adamson591d71c2009-04-01 09:22:47 -04005265#if defined(CONFIG_NFS_V4_1)
5266struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5267 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5268 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5269 .recover_open = nfs4_open_reclaim,
5270 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05005271 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005272 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005273 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04005274};
5275#endif /* CONFIG_NFS_V4_1 */
5276
5277struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005278 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005279 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280 .recover_open = nfs4_open_expired,
5281 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04005282 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005283 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005284};
5285
Andy Adamson591d71c2009-04-01 09:22:47 -04005286#if defined(CONFIG_NFS_V4_1)
5287struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5288 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5289 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5290 .recover_open = nfs4_open_expired,
5291 .recover_lock = nfs4_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05005292 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005293 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04005294};
5295#endif /* CONFIG_NFS_V4_1 */
5296
Benny Halevy29fba382009-04-01 09:22:44 -04005297struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5298 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04005299 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005300 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04005301};
5302
5303#if defined(CONFIG_NFS_V4_1)
5304struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5305 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04005306 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005307 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04005308};
5309#endif
5310
Trond Myklebust97dc1352010-06-16 09:52:26 -04005311static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
5312 .minor_version = 0,
5313 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04005314 .validate_stateid = nfs4_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005315 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
5316 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
5317 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005318};
5319
5320#if defined(CONFIG_NFS_V4_1)
5321static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
5322 .minor_version = 1,
5323 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04005324 .validate_stateid = nfs41_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005325 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
5326 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
5327 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005328};
5329#endif
5330
Trond Myklebust97dc1352010-06-16 09:52:26 -04005331const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
5332 [0] = &nfs_v4_0_minor_ops,
5333#if defined(CONFIG_NFS_V4_1)
5334 [1] = &nfs_v4_1_minor_ops,
5335#endif
5336};
5337
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005338static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005339 .permission = nfs_permission,
5340 .getattr = nfs_getattr,
5341 .setattr = nfs_setattr,
5342 .getxattr = nfs4_getxattr,
5343 .setxattr = nfs4_setxattr,
5344 .listxattr = nfs4_listxattr,
5345};
5346
David Howells509de812006-08-22 20:06:11 -04005347const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348 .version = 4, /* protocol version */
5349 .dentry_ops = &nfs4_dentry_operations,
5350 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005351 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352 .getroot = nfs4_proc_get_root,
5353 .getattr = nfs4_proc_getattr,
5354 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04005355 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356 .lookup = nfs4_proc_lookup,
5357 .access = nfs4_proc_access,
5358 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359 .create = nfs4_proc_create,
5360 .remove = nfs4_proc_remove,
5361 .unlink_setup = nfs4_proc_unlink_setup,
5362 .unlink_done = nfs4_proc_unlink_done,
5363 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04005364 .rename_setup = nfs4_proc_rename_setup,
5365 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 .link = nfs4_proc_link,
5367 .symlink = nfs4_proc_symlink,
5368 .mkdir = nfs4_proc_mkdir,
5369 .rmdir = nfs4_proc_remove,
5370 .readdir = nfs4_proc_readdir,
5371 .mknod = nfs4_proc_mknod,
5372 .statfs = nfs4_proc_statfs,
5373 .fsinfo = nfs4_proc_fsinfo,
5374 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04005375 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376 .decode_dirent = nfs4_decode_dirent,
5377 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05005378 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005380 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005382 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005384 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04005385 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04005386 .open_context = nfs4_atomic_open,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387};
5388
5389/*
5390 * Local variables:
5391 * c-basic-offset: 8
5392 * End:
5393 */