blob: e403d7a8446654ac8251e9925dd8600f1e819763 [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>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000052#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050053#include <linux/utsname.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Trond Myklebust4ce79712005-06-22 17:16:21 +000055#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050057#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050058#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040059#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040060#include "pnfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62#define NFSDBG_FACILITY NFSDBG_PROC
63
Trond Myklebust2066fe82006-09-15 08:30:46 -040064#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#define NFS4_POLL_RETRY_MAX (15*HZ)
66
Trond Myklebusta78cb572009-08-09 15:06:19 -040067#define NFS4_MAX_LOOP_ON_RECOVER (10)
68
Trond Myklebustcdd4e682006-01-03 09:55:12 +010069struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010070static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080071static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050073static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Trond Myklebust99367812007-07-17 21:52:41 -040074static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
75static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040076static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
77 struct nfs_fattr *fattr, struct iattr *sattr,
78 struct nfs4_state *state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050081static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
Trond Myklebust52567b02009-10-23 14:46:42 -040083 if (err >= -1000)
84 return err;
85 switch (err) {
86 case -NFS4ERR_RESOURCE:
87 return -EREMOTEIO;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -080088 case -NFS4ERR_BADOWNER:
89 case -NFS4ERR_BADNAME:
90 return -EINVAL;
Trond Myklebust52567b02009-10-23 14:46:42 -040091 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -070093 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -040094 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 }
Trond Myklebust52567b02009-10-23 14:46:42 -040096 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
99/*
100 * This is our standard bitmap for GETATTR requests.
101 */
102const u32 nfs4_fattr_bitmap[2] = {
103 FATTR4_WORD0_TYPE
104 | FATTR4_WORD0_CHANGE
105 | FATTR4_WORD0_SIZE
106 | FATTR4_WORD0_FSID
107 | FATTR4_WORD0_FILEID,
108 FATTR4_WORD1_MODE
109 | FATTR4_WORD1_NUMLINKS
110 | FATTR4_WORD1_OWNER
111 | FATTR4_WORD1_OWNER_GROUP
112 | FATTR4_WORD1_RAWDEV
113 | FATTR4_WORD1_SPACE_USED
114 | FATTR4_WORD1_TIME_ACCESS
115 | FATTR4_WORD1_TIME_METADATA
116 | FATTR4_WORD1_TIME_MODIFY
117};
118
119const u32 nfs4_statfs_bitmap[2] = {
120 FATTR4_WORD0_FILES_AVAIL
121 | FATTR4_WORD0_FILES_FREE
122 | FATTR4_WORD0_FILES_TOTAL,
123 FATTR4_WORD1_SPACE_AVAIL
124 | FATTR4_WORD1_SPACE_FREE
125 | FATTR4_WORD1_SPACE_TOTAL
126};
127
Trond Myklebust4ce79712005-06-22 17:16:21 +0000128const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 FATTR4_WORD0_MAXLINK
130 | FATTR4_WORD0_MAXNAME,
131 0
132};
133
134const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
135 | FATTR4_WORD0_MAXREAD
136 | FATTR4_WORD0_MAXWRITE
137 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700138 FATTR4_WORD1_TIME_DELTA
Andy Adamson504913f2010-10-20 00:17:57 -0400139 | FATTR4_WORD1_FS_LAYOUT_TYPES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140};
141
Manoj Naik830b8e32006-06-09 09:34:25 -0400142const u32 nfs4_fs_locations_bitmap[2] = {
143 FATTR4_WORD0_TYPE
144 | FATTR4_WORD0_CHANGE
145 | FATTR4_WORD0_SIZE
146 | FATTR4_WORD0_FSID
147 | FATTR4_WORD0_FILEID
148 | FATTR4_WORD0_FS_LOCATIONS,
149 FATTR4_WORD1_MODE
150 | FATTR4_WORD1_NUMLINKS
151 | FATTR4_WORD1_OWNER
152 | FATTR4_WORD1_OWNER_GROUP
153 | FATTR4_WORD1_RAWDEV
154 | FATTR4_WORD1_SPACE_USED
155 | FATTR4_WORD1_TIME_ACCESS
156 | FATTR4_WORD1_TIME_METADATA
157 | FATTR4_WORD1_TIME_MODIFY
158 | FATTR4_WORD1_MOUNTED_ON_FILEID
159};
160
Al Virobc4785c2006-10-19 23:28:51 -0700161static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 struct nfs4_readdir_arg *readdir)
163{
Al Viro0dbb4c62006-10-19 23:28:49 -0700164 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 BUG_ON(readdir->count < 80);
167 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000168 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
170 return;
171 }
172
173 readdir->cookie = 0;
174 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
175 if (cookie == 2)
176 return;
177
178 /*
179 * NFSv4 servers do not return entries for '.' and '..'
180 * Therefore, we fake these entries here. We let '.'
181 * have cookie 0 and '..' have cookie 1. Note that
182 * when talking to the server, we always send cookie 0
183 * instead of 1 or 2.
184 */
Al Viro0dbb4c62006-10-19 23:28:49 -0700185 start = p = kmap_atomic(*readdir->pages, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 if (cookie == 0) {
188 *p++ = xdr_one; /* next */
189 *p++ = xdr_zero; /* cookie, first word */
190 *p++ = xdr_one; /* cookie, second word */
191 *p++ = xdr_one; /* entry len */
192 memcpy(p, ".\0\0\0", 4); /* entry */
193 p++;
194 *p++ = xdr_one; /* bitmap length */
195 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
196 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400197 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
199
200 *p++ = xdr_one; /* next */
201 *p++ = xdr_zero; /* cookie, first word */
202 *p++ = xdr_two; /* cookie, second word */
203 *p++ = xdr_two; /* entry len */
204 memcpy(p, "..\0\0", 4); /* entry */
205 p++;
206 *p++ = xdr_one; /* bitmap length */
207 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
208 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400209 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 readdir->pgbase = (char *)p - (char *)start;
212 readdir->count -= readdir->pgbase;
213 kunmap_atomic(start, KM_USER0);
214}
215
Trond Myklebust65de8722008-12-23 15:21:44 -0500216static int nfs4_wait_clnt_recover(struct nfs_client *clp)
217{
218 int res;
219
220 might_sleep();
221
Trond Myklebuste005e802008-12-23 15:21:48 -0500222 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400223 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500224 return res;
225}
226
227static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
228{
229 int res = 0;
230
231 might_sleep();
232
233 if (*timeout <= 0)
234 *timeout = NFS4_POLL_RETRY_MIN;
235 if (*timeout > NFS4_POLL_RETRY_MAX)
236 *timeout = NFS4_POLL_RETRY_MAX;
237 schedule_timeout_killable(*timeout);
238 if (fatal_signal_pending(current))
239 res = -ERESTARTSYS;
240 *timeout <<= 1;
241 return res;
242}
243
244/* This is the error handling routine for processes that are allowed
245 * to sleep.
246 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800247static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500248{
249 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500250 struct nfs4_state *state = exception->state;
Trond Myklebust65de8722008-12-23 15:21:44 -0500251 int ret = errorcode;
252
253 exception->retry = 0;
254 switch(errorcode) {
255 case 0:
256 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500257 case -NFS4ERR_ADMIN_REVOKED:
258 case -NFS4ERR_BAD_STATEID:
259 case -NFS4ERR_OPENMODE:
260 if (state == NULL)
261 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500262 nfs4_schedule_stateid_recovery(server, state);
263 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500264 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500265 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust65de8722008-12-23 15:21:44 -0500266 case -NFS4ERR_EXPIRED:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500267 nfs4_schedule_lease_recovery(clp);
268 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500269#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400270 case -NFS4ERR_BADSESSION:
271 case -NFS4ERR_BADSLOT:
272 case -NFS4ERR_BAD_HIGH_SLOT:
273 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
274 case -NFS4ERR_DEADSESSION:
275 case -NFS4ERR_SEQ_FALSE_RETRY:
276 case -NFS4ERR_SEQ_MISORDERED:
277 dprintk("%s ERROR: %d Reset session\n", __func__,
278 errorcode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500279 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -0400280 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500281 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500282#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500283 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500284 if (exception->timeout > HZ) {
285 /* We have retried a decent amount, time to
286 * fail
287 */
288 ret = -EBUSY;
289 break;
290 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500291 case -NFS4ERR_GRACE:
292 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500293 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500294 ret = nfs4_delay(server->client, &exception->timeout);
295 if (ret != 0)
296 break;
297 case -NFS4ERR_OLD_STATEID:
298 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800299 break;
300 case -NFS4ERR_BADOWNER:
301 /* The following works around a Linux server bug! */
302 case -NFS4ERR_BADNAME:
303 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
304 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
305 exception->retry = 1;
306 printk(KERN_WARNING "NFS: v4 server %s "
307 "does not accept raw "
308 "uid/gids. "
309 "Reenabling the idmapper.\n",
310 server->nfs_client->cl_hostname);
311 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500312 }
313 /* We failed to handle the error */
314 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500315wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500316 ret = nfs4_wait_clnt_recover(clp);
317 if (ret == 0)
318 exception->retry = 1;
319 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500320}
321
322
Trond Myklebust452e9352010-07-31 14:29:06 -0400323static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 spin_lock(&clp->cl_lock);
326 if (time_before(clp->cl_last_renewal,timestamp))
327 clp->cl_last_renewal = timestamp;
328 spin_unlock(&clp->cl_lock);
329}
330
Trond Myklebust452e9352010-07-31 14:29:06 -0400331static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
332{
333 do_renew_lease(server->nfs_client, timestamp);
334}
335
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400336#if defined(CONFIG_NFS_V4_1)
337
Benny Halevy510b8172009-04-01 09:22:14 -0400338/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400339 * nfs4_free_slot - free a slot and efficiently update slot table.
340 *
341 * freeing a slot is trivially done by clearing its respective bit
342 * in the bitmap.
343 * If the freed slotid equals highest_used_slotid we want to update it
344 * so that the server would be able to size down the slot table if needed,
345 * otherwise we know that the highest_used_slotid is still in use.
346 * When updating highest_used_slotid there may be "holes" in the bitmap
347 * so we need to scan down from highest_used_slotid to 0 looking for the now
348 * highest slotid in use.
349 * If none found, highest_used_slotid is set to -1.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500350 *
351 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400352 */
353static void
Benny Halevydfb4f302010-09-24 09:17:01 -0400354nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *free_slot)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400355{
Benny Halevydfb4f302010-09-24 09:17:01 -0400356 int free_slotid = free_slot - tbl->slots;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400357 int slotid = free_slotid;
358
Benny Halevydfb4f302010-09-24 09:17:01 -0400359 BUG_ON(slotid < 0 || slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400360 /* clear used bit in bitmap */
361 __clear_bit(slotid, tbl->used_slots);
362
363 /* update highest_used_slotid when it is freed */
364 if (slotid == tbl->highest_used_slotid) {
365 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500366 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400367 tbl->highest_used_slotid = slotid;
368 else
369 tbl->highest_used_slotid = -1;
370 }
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400371 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
372 free_slotid, tbl->highest_used_slotid);
373}
374
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800375/*
Andy Adamson42acd022011-01-06 02:04:34 +0000376 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800377 */
Andy Adamson42acd022011-01-06 02:04:34 +0000378static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800379{
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800380 struct rpc_task *task;
381
Trond Myklebusta2118c32010-06-16 09:52:26 -0400382 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800383 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
384 if (task)
385 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800386 return;
387 }
388
389 if (ses->fc_slot_table.highest_used_slotid != -1)
390 return;
391
Andy Adamson42acd022011-01-06 02:04:34 +0000392 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
393 complete(&ses->fc_slot_table.complete);
394}
395
396/*
397 * Signal state manager thread if session back channel is drained
398 */
399void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
400{
401 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
402 ses->bc_slot_table.highest_used_slotid != -1)
403 return;
404 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
405 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800406}
407
Trond Myklebustd185a332010-06-16 09:52:25 -0400408static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400409{
410 struct nfs4_slot_table *tbl;
411
Trond Myklebustd185a332010-06-16 09:52:25 -0400412 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f302010-09-24 09:17:01 -0400413 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400414 /* just wake up the next guy waiting since
415 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500416 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500417 return;
Andy Adamson13615872009-04-01 09:22:17 -0400418 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500419
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500420 spin_lock(&tbl->slot_tbl_lock);
Benny Halevydfb4f302010-09-24 09:17:01 -0400421 nfs4_free_slot(tbl, res->sr_slot);
Andy Adamson42acd022011-01-06 02:04:34 +0000422 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500423 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f302010-09-24 09:17:01 -0400424 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400425}
426
Trond Myklebust14516c32010-07-31 14:29:06 -0400427static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400428{
429 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400430 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400431
432 /*
433 * sr_status remains 1 if an RPC level error occurred. The server
434 * may or may not have processed the sequence operation..
435 * Proceed as if the server received and processed the sequence
436 * operation.
437 */
438 if (res->sr_status == 1)
439 res->sr_status = NFS_OK;
440
441 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
Benny Halevydfb4f302010-09-24 09:17:01 -0400442 if (!res->sr_slot)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400443 goto out;
444
Andy Adamson691daf32009-12-04 15:55:39 -0500445 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400446 switch (res->sr_status) {
447 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400448 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f302010-09-24 09:17:01 -0400449 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400450 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400451 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400452 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500453 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500454 if (res->sr_status_flags != 0)
455 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400456 break;
457 case -NFS4ERR_DELAY:
458 /* The server detected a resend of the RPC call and
459 * returned NFS4ERR_DELAY as per Section 2.10.6.2
460 * of RFC5661.
461 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200462 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f302010-09-24 09:17:01 -0400463 __func__,
464 res->sr_slot - res->sr_session->fc_slot_table.slots,
465 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400466 goto out_retry;
467 default:
468 /* Just update the slot sequence no. */
Benny Halevydfb4f302010-09-24 09:17:01 -0400469 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400470 }
471out:
472 /* The session may be reset by one of the error handlers. */
473 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400474 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400475 return 1;
476out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400477 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400478 goto out;
479 rpc_delay(task, NFS4_POLL_RETRY_MAX);
480 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400481}
482
Trond Myklebust14516c32010-07-31 14:29:06 -0400483static int nfs4_sequence_done(struct rpc_task *task,
484 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400485{
Trond Myklebust14516c32010-07-31 14:29:06 -0400486 if (res->sr_session == NULL)
487 return 1;
488 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400489}
490
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400491/*
Benny Halevy510b8172009-04-01 09:22:14 -0400492 * nfs4_find_slot - efficiently look for a free slot
493 *
494 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
495 * If found, we mark the slot as used, update the highest_used_slotid,
496 * and respectively set up the sequence operation args.
497 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400498 *
499 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400500 */
501static u8
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800502nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400503{
504 int slotid;
505 u8 ret_id = NFS4_MAX_SLOT_TABLE;
506 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
507
Benny Halevy510b8172009-04-01 09:22:14 -0400508 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
509 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
510 tbl->max_slots);
511 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
512 if (slotid >= tbl->max_slots)
513 goto out;
514 __set_bit(slotid, tbl->used_slots);
515 if (slotid > tbl->highest_used_slotid)
516 tbl->highest_used_slotid = slotid;
517 ret_id = slotid;
518out:
519 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
520 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400521 return ret_id;
522}
523
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000524int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400525 struct nfs4_sequence_args *args,
526 struct nfs4_sequence_res *res,
527 int cache_reply,
528 struct rpc_task *task)
529{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400530 struct nfs4_slot *slot;
531 struct nfs4_slot_table *tbl;
532 u8 slotid;
533
534 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400535 /* slot already allocated? */
Benny Halevydfb4f302010-09-24 09:17:01 -0400536 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400537 return 0;
538
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400539 tbl = &session->fc_slot_table;
540
541 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400542 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800543 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamsonea028ac2009-12-04 15:55:38 -0500544 /*
545 * The state manager will wait until the slot table is empty.
546 * Schedule the reset thread
547 */
Andy Adamson05f0d232009-12-04 15:55:37 -0500548 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400549 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500550 dprintk("%s Schedule Session Reset\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500551 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400552 }
553
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800554 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
555 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
556 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
557 spin_unlock(&tbl->slot_tbl_lock);
558 dprintk("%s enforce FIFO order\n", __func__);
559 return -EAGAIN;
560 }
561
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800562 slotid = nfs4_find_slot(tbl);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400563 if (slotid == NFS4_MAX_SLOT_TABLE) {
564 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
565 spin_unlock(&tbl->slot_tbl_lock);
566 dprintk("<-- %s: no free slots\n", __func__);
567 return -EAGAIN;
568 }
569 spin_unlock(&tbl->slot_tbl_lock);
570
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800571 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400572 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400573 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400574 args->sa_slotid = slotid;
575 args->sa_cache_this = cache_reply;
576
577 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
578
Benny Halevy99fe60d2009-04-01 09:22:29 -0400579 res->sr_session = session;
Benny Halevydfb4f302010-09-24 09:17:01 -0400580 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400581 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400582 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400583 /*
584 * sr_status is only set in decode_sequence, and so will remain
585 * set to 1 if an rpc level failure occurs.
586 */
587 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400588 return 0;
589}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000590EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400591
Trond Myklebust035168a2010-06-16 09:52:26 -0400592int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400593 struct nfs4_sequence_args *args,
594 struct nfs4_sequence_res *res,
595 int cache_reply,
596 struct rpc_task *task)
597{
Trond Myklebust035168a2010-06-16 09:52:26 -0400598 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400599 int ret = 0;
600
Trond Myklebust035168a2010-06-16 09:52:26 -0400601 if (session == NULL) {
602 args->sa_session = NULL;
603 res->sr_session = NULL;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400604 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400605 }
606
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200607 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f302010-09-24 09:17:01 -0400608 __func__, session->clp, session, res->sr_slot ?
609 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400610
611 ret = nfs41_setup_sequence(session, args, res, cache_reply,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400612 task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400613out:
614 dprintk("<-- %s status=%d\n", __func__, ret);
615 return ret;
616}
617
618struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400619 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400620 struct nfs4_sequence_args *seq_args;
621 struct nfs4_sequence_res *seq_res;
622 int cache_reply;
623};
624
625static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
626{
627 struct nfs41_call_sync_data *data = calldata;
628
Trond Myklebust035168a2010-06-16 09:52:26 -0400629 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
630
631 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400632 data->seq_res, data->cache_reply, task))
633 return;
634 rpc_call_start(task);
635}
636
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800637static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
638{
639 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
640 nfs41_call_sync_prepare(task, calldata);
641}
642
Andy Adamson69ab40c2009-04-01 09:22:19 -0400643static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
644{
645 struct nfs41_call_sync_data *data = calldata;
646
Trond Myklebust14516c32010-07-31 14:29:06 -0400647 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400648}
649
Andy Adamsonce5039c2009-04-01 09:22:13 -0400650struct rpc_call_ops nfs41_call_sync_ops = {
651 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400652 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400653};
654
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800655struct rpc_call_ops nfs41_call_priv_sync_ops = {
656 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
657 .rpc_call_done = nfs41_call_sync_done,
658};
659
Trond Myklebust035168a2010-06-16 09:52:26 -0400660static int nfs4_call_sync_sequence(struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400661 struct rpc_message *msg,
662 struct nfs4_sequence_args *args,
663 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800664 int cache_reply,
665 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400666{
667 int ret;
668 struct rpc_task *task;
669 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400670 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400671 .seq_args = args,
672 .seq_res = res,
673 .cache_reply = cache_reply,
674 };
675 struct rpc_task_setup task_setup = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400676 .rpc_client = server->client,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400677 .rpc_message = msg,
678 .callback_ops = &nfs41_call_sync_ops,
679 .callback_data = &data
680 };
681
Benny Halevydfb4f302010-09-24 09:17:01 -0400682 res->sr_slot = NULL;
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800683 if (privileged)
684 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400685 task = rpc_run_task(&task_setup);
686 if (IS_ERR(task))
687 ret = PTR_ERR(task);
688 else {
689 ret = task->tk_status;
690 rpc_put_task(task);
691 }
692 return ret;
693}
694
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400695int _nfs4_call_sync_session(struct nfs_server *server,
696 struct rpc_message *msg,
697 struct nfs4_sequence_args *args,
698 struct nfs4_sequence_res *res,
699 int cache_reply)
700{
Trond Myklebust035168a2010-06-16 09:52:26 -0400701 return nfs4_call_sync_sequence(server, msg, args, res, cache_reply, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400702}
703
Trond Myklebustdf896452010-06-16 09:52:26 -0400704#else
Trond Myklebust14516c32010-07-31 14:29:06 -0400705static int nfs4_sequence_done(struct rpc_task *task,
706 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400707{
Trond Myklebust14516c32010-07-31 14:29:06 -0400708 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400709}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400710#endif /* CONFIG_NFS_V4_1 */
711
712int _nfs4_call_sync(struct nfs_server *server,
713 struct rpc_message *msg,
714 struct nfs4_sequence_args *args,
715 struct nfs4_sequence_res *res,
716 int cache_reply)
717{
Benny Halevyf3752972009-04-01 09:22:04 -0400718 args->sa_session = res->sr_session = NULL;
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400719 return rpc_call_sync(server->client, msg, 0);
720}
721
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000722static inline
723int nfs4_call_sync(struct nfs_server *server,
724 struct rpc_message *msg,
725 struct nfs4_sequence_args *args,
726 struct nfs4_sequence_res *res,
727 int cache_reply)
728{
729 return server->nfs_client->cl_mvops->call_sync(server, msg, args,
730 res, cache_reply);
731}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400732
Trond Myklebust38478b22006-05-25 01:40:57 -0400733static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
Trond Myklebust38478b22006-05-25 01:40:57 -0400735 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Trond Myklebust38478b22006-05-25 01:40:57 -0400737 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400738 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
739 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400740 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400741 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400742 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100745struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400746 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100747 struct nfs_openargs o_arg;
748 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100749 struct nfs_open_confirmargs c_arg;
750 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100751 struct nfs_fattr f_attr;
752 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400753 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100754 struct dentry *dir;
755 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400756 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100757 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100758 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400759 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100760 int rpc_status;
761 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100762};
763
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400764
765static void nfs4_init_opendata_res(struct nfs4_opendata *p)
766{
767 p->o_res.f_attr = &p->f_attr;
768 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400769 p->o_res.seqid = p->o_arg.seqid;
770 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400771 p->o_res.server = p->o_arg.server;
772 nfs_fattr_init(&p->f_attr);
773 nfs_fattr_init(&p->dir_attr);
774}
775
Trond Myklebustad389da2007-06-05 12:30:00 -0400776static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500777 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400778 const struct iattr *attrs,
779 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100780{
Trond Myklebustad389da2007-06-05 12:30:00 -0400781 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100782 struct inode *dir = parent->d_inode;
783 struct nfs_server *server = NFS_SERVER(dir);
784 struct nfs4_opendata *p;
785
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400786 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100787 if (p == NULL)
788 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400789 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100790 if (p->o_arg.seqid == NULL)
791 goto err_free;
Al Virof6948692010-01-30 13:51:04 -0500792 path_get(path);
793 p->path = *path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100794 p->dir = parent;
795 p->owner = sp;
796 atomic_inc(&sp->so_count);
797 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500798 p->o_arg.open_flags = flags;
799 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400800 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400801 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400802 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100803 p->o_arg.server = server;
804 p->o_arg.bitmask = server->attr_bitmask;
805 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400806 if (flags & O_CREAT) {
807 u32 *s;
808
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100809 p->o_arg.u.attrs = &p->attrs;
810 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400811 s = (u32 *) p->o_arg.u.verifier.data;
812 s[0] = jiffies;
813 s[1] = current->pid;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100814 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100815 p->c_arg.fh = &p->o_res.fh;
816 p->c_arg.stateid = &p->o_res.stateid;
817 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400818 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400819 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100820 return p;
821err_free:
822 kfree(p);
823err:
824 dput(parent);
825 return NULL;
826}
827
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400828static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100829{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400830 struct nfs4_opendata *p = container_of(kref,
831 struct nfs4_opendata, kref);
832
833 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400834 if (p->state != NULL)
835 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400836 nfs4_put_state_owner(p->owner);
837 dput(p->dir);
Jan Blunck31f31db2008-05-02 13:42:45 -0700838 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400839 kfree(p);
840}
841
842static void nfs4_opendata_put(struct nfs4_opendata *p)
843{
844 if (p != NULL)
845 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100846}
847
Trond Myklebust06f814a2006-01-03 09:55:07 +0100848static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
849{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100850 int ret;
851
Trond Myklebust06f814a2006-01-03 09:55:07 +0100852 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100853 return ret;
854}
855
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500856static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400857{
858 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500859
860 if (open_mode & O_EXCL)
861 goto out;
862 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400863 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500864 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
865 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400866 break;
867 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500868 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
869 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400870 break;
871 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500872 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
873 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400874 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500875out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400876 return ret;
877}
878
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500879static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400880{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500881 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400882 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500883 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400884 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500885 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400886 return 1;
887}
888
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500889static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100890{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500891 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100892 case FMODE_WRITE:
893 state->n_wronly++;
894 break;
895 case FMODE_READ:
896 state->n_rdonly++;
897 break;
898 case FMODE_READ|FMODE_WRITE:
899 state->n_rdwr++;
900 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500901 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100902}
903
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500904static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400905{
906 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
907 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
908 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500909 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400910 case FMODE_READ:
911 set_bit(NFS_O_RDONLY_STATE, &state->flags);
912 break;
913 case FMODE_WRITE:
914 set_bit(NFS_O_WRONLY_STATE, &state->flags);
915 break;
916 case FMODE_READ|FMODE_WRITE:
917 set_bit(NFS_O_RDWR_STATE, &state->flags);
918 }
919}
920
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500921static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400922{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400923 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500924 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400925 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400926}
927
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500928static 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 -0700929{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400930 /*
931 * Protect the call to nfs4_state_set_mode_locked and
932 * serialise the stateid update
933 */
934 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400935 if (deleg_stateid != NULL) {
936 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
937 set_bit(NFS_DELEGATED_STATE, &state->flags);
938 }
939 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500940 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400941 write_sequnlock(&state->seqlock);
942 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500943 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700944 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500947static 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 -0500948{
949 struct nfs_inode *nfsi = NFS_I(state->inode);
950 struct nfs_delegation *deleg_cur;
951 int ret = 0;
952
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500953 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500954
955 rcu_read_lock();
956 deleg_cur = rcu_dereference(nfsi->delegation);
957 if (deleg_cur == NULL)
958 goto no_delegation;
959
960 spin_lock(&deleg_cur->lock);
961 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500962 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500963 goto no_delegation_unlock;
964
965 if (delegation == NULL)
966 delegation = &deleg_cur->stateid;
967 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
968 goto no_delegation_unlock;
969
Trond Myklebustb7391f42008-12-23 15:21:52 -0500970 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500971 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500972 ret = 1;
973no_delegation_unlock:
974 spin_unlock(&deleg_cur->lock);
975no_delegation:
976 rcu_read_unlock();
977
978 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500979 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500980 ret = 1;
981 }
982
983 return ret;
984}
985
986
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500987static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400988{
989 struct nfs_delegation *delegation;
990
991 rcu_read_lock();
992 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500993 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400994 rcu_read_unlock();
995 return;
996 }
997 rcu_read_unlock();
998 nfs_inode_return_delegation(inode);
999}
1000
Trond Myklebust6ee41262007-07-08 14:11:36 -04001001static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001002{
1003 struct nfs4_state *state = opendata->state;
1004 struct nfs_inode *nfsi = NFS_I(state->inode);
1005 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001006 int open_mode = opendata->o_arg.open_flags & O_EXCL;
1007 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001008 nfs4_stateid stateid;
1009 int ret = -EAGAIN;
1010
Trond Myklebustaac00a82007-07-05 19:02:21 -04001011 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001012 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001013 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001014 if (can_open_cached(state, fmode, open_mode)) {
1015 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001016 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001017 goto out_return_state;
1018 }
1019 spin_unlock(&state->owner->so_lock);
1020 }
Trond Myklebust34310432008-12-23 15:21:38 -05001021 rcu_read_lock();
1022 delegation = rcu_dereference(nfsi->delegation);
1023 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001024 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001025 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001026 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001027 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001028 /* Save the delegation */
1029 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1030 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001031 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001032 if (ret != 0)
1033 goto out;
1034 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001035
1036 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001037 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001038 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001039 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001040out:
1041 return ERR_PTR(ret);
1042out_return_state:
1043 atomic_inc(&state->count);
1044 return state;
1045}
1046
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001047static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1048{
1049 struct inode *inode;
1050 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001051 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001052 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001053
Trond Myklebustaac00a82007-07-05 19:02:21 -04001054 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001055 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001056 goto out;
1057 }
1058
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001059 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001060 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001061 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001062 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001063 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001064 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001065 goto err;
1066 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001067 state = nfs4_get_open_state(inode, data->owner);
1068 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001069 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001070 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001071 int delegation_flags = 0;
1072
Trond Myklebust003707c2007-07-05 18:07:55 -04001073 rcu_read_lock();
1074 delegation = rcu_dereference(NFS_I(inode)->delegation);
1075 if (delegation)
1076 delegation_flags = delegation->flags;
1077 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -05001078 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001079 nfs_inode_set_delegation(state->inode,
1080 data->owner->so_cred,
1081 &data->o_res);
1082 else
1083 nfs_inode_reclaim_delegation(state->inode,
1084 data->owner->so_cred,
1085 &data->o_res);
1086 }
Trond Myklebust34310432008-12-23 15:21:38 -05001087
1088 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001089 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001090 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001091out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001092 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001093err_put_inode:
1094 iput(inode);
1095err:
1096 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001097}
1098
Trond Myklebust864472e2006-01-03 09:55:15 +01001099static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1100{
1101 struct nfs_inode *nfsi = NFS_I(state->inode);
1102 struct nfs_open_context *ctx;
1103
1104 spin_lock(&state->inode->i_lock);
1105 list_for_each_entry(ctx, &nfsi->open_files, list) {
1106 if (ctx->state != state)
1107 continue;
1108 get_nfs_open_context(ctx);
1109 spin_unlock(&state->inode->i_lock);
1110 return ctx;
1111 }
1112 spin_unlock(&state->inode->i_lock);
1113 return ERR_PTR(-ENOENT);
1114}
1115
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001116static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1117{
1118 struct nfs4_opendata *opendata;
1119
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001120 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001121 if (opendata == NULL)
1122 return ERR_PTR(-ENOMEM);
1123 opendata->state = state;
1124 atomic_inc(&state->count);
1125 return opendata;
1126}
1127
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001128static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001129{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001130 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001131 int ret;
1132
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001133 opendata->o_arg.open_flags = 0;
1134 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001135 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1136 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1137 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001138 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001139 if (ret != 0)
1140 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001141 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001142 if (IS_ERR(newstate))
1143 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001144 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001145 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001146 return 0;
1147}
1148
1149static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1150{
Trond Myklebust864472e2006-01-03 09:55:15 +01001151 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001152 int ret;
1153
1154 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001155 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001156 smp_rmb();
1157 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001158 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001159 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001160 if (ret != 0)
1161 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001162 if (newstate != state)
1163 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001164 }
1165 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001166 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001167 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001168 if (ret != 0)
1169 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001170 if (newstate != state)
1171 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001172 }
1173 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001174 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001175 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001176 if (ret != 0)
1177 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001178 if (newstate != state)
1179 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001180 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001181 /*
1182 * We may have performed cached opens for all three recoveries.
1183 * Check if we need to update the current stateid.
1184 */
1185 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1186 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001187 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001188 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1189 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001190 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001191 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001192 return 0;
1193}
1194
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195/*
1196 * OPEN_RECLAIM:
1197 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001199static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001201 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001202 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001203 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 int status;
1205
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001206 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1207 if (IS_ERR(opendata))
1208 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001209 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1210 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001211 rcu_read_lock();
1212 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001213 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001214 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001215 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001216 opendata->o_arg.u.delegation_type = delegation_type;
1217 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001218 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 return status;
1220}
1221
Trond Myklebust539cd032007-06-05 11:46:42 -04001222static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
1224 struct nfs_server *server = NFS_SERVER(state->inode);
1225 struct nfs4_exception exception = { };
1226 int err;
1227 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001228 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001229 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001230 break;
1231 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 } while (exception.retry);
1233 return err;
1234}
1235
Trond Myklebust864472e2006-01-03 09:55:15 +01001236static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1237{
1238 struct nfs_open_context *ctx;
1239 int ret;
1240
1241 ctx = nfs4_state_find_open_context(state);
1242 if (IS_ERR(ctx))
1243 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001244 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001245 put_nfs_open_context(ctx);
1246 return ret;
1247}
1248
Trond Myklebust13437e12007-07-06 15:10:43 -04001249static 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 -07001250{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001251 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001252 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001254 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1255 if (IS_ERR(opendata))
1256 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001257 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001258 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001259 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001260 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001261 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001262 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263}
1264
Trond Myklebust13437e12007-07-06 15:10:43 -04001265int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266{
1267 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001268 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 int err;
1270 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001271 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 switch (err) {
1273 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001274 case -ENOENT:
1275 case -ESTALE:
1276 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001277 case -NFS4ERR_BADSESSION:
1278 case -NFS4ERR_BADSLOT:
1279 case -NFS4ERR_BAD_HIGH_SLOT:
1280 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1281 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001282 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001283 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 case -NFS4ERR_STALE_CLIENTID:
1285 case -NFS4ERR_STALE_STATEID:
1286 case -NFS4ERR_EXPIRED:
1287 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001288 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001289 goto out;
1290 case -ERESTARTSYS:
1291 /*
1292 * The show must go on: exit, but mark the
1293 * stateid as needing recovery.
1294 */
1295 case -NFS4ERR_ADMIN_REVOKED:
1296 case -NFS4ERR_BAD_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001297 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001298 case -EKEYEXPIRED:
1299 /*
1300 * User RPCSEC_GSS context has expired.
1301 * We cannot recover this stateid now, so
1302 * skip it and allow recovery thread to
1303 * proceed.
1304 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001305 case -ENOMEM:
1306 err = 0;
1307 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 }
1309 err = nfs4_handle_exception(server, err, &exception);
1310 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001311out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 return err;
1313}
1314
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001315static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1316{
1317 struct nfs4_opendata *data = calldata;
1318
1319 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001320 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001321 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1322 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001323 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001324 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001325 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001326 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001327}
1328
1329static void nfs4_open_confirm_release(void *calldata)
1330{
1331 struct nfs4_opendata *data = calldata;
1332 struct nfs4_state *state = NULL;
1333
1334 /* If this request hasn't been cancelled, do nothing */
1335 if (data->cancelled == 0)
1336 goto out_free;
1337 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001338 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001339 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001340 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001341 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001342 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001343out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001344 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001345}
1346
1347static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001348 .rpc_call_done = nfs4_open_confirm_done,
1349 .rpc_release = nfs4_open_confirm_release,
1350};
1351
1352/*
1353 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1354 */
1355static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1356{
1357 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1358 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001359 struct rpc_message msg = {
1360 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1361 .rpc_argp = &data->c_arg,
1362 .rpc_resp = &data->c_res,
1363 .rpc_cred = data->owner->so_cred,
1364 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001365 struct rpc_task_setup task_setup_data = {
1366 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001367 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001368 .callback_ops = &nfs4_open_confirm_ops,
1369 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001370 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001371 .flags = RPC_TASK_ASYNC,
1372 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 int status;
1374
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001375 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001376 data->rpc_done = 0;
1377 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001378 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001379 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001380 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001381 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001382 status = nfs4_wait_for_completion_rpc_task(task);
1383 if (status != 0) {
1384 data->cancelled = 1;
1385 smp_wmb();
1386 } else
1387 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001388 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 return status;
1390}
1391
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001392static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001394 struct nfs4_opendata *data = calldata;
1395 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001396
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001397 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1398 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001399 /*
1400 * Check if we still need to send an OPEN call, or if we can use
1401 * a delegation instead.
1402 */
1403 if (data->state != NULL) {
1404 struct nfs_delegation *delegation;
1405
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001406 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001407 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001408 rcu_read_lock();
1409 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1410 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001411 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001412 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001413 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001414 }
1415 rcu_read_unlock();
1416 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001417 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001418 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001419 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001420 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001421 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001422 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1423 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001424 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001425 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001426 &data->o_arg.seq_args,
1427 &data->o_res.seq_res, 1, task))
1428 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001429 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001430 return;
1431out_no_action:
1432 task->tk_action = NULL;
1433
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001434}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001436static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1437{
1438 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1439 nfs4_open_prepare(task, calldata);
1440}
1441
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001442static void nfs4_open_done(struct rpc_task *task, void *calldata)
1443{
1444 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001446 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001447
Trond Myklebust14516c32010-07-31 14:29:06 -04001448 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1449 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001450
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001451 if (task->tk_status == 0) {
1452 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001453 case S_IFREG:
1454 break;
1455 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001456 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001457 break;
1458 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001459 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001460 break;
1461 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001462 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001463 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001464 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001465 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1466 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001467 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001468 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001469}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001470
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001471static void nfs4_open_release(void *calldata)
1472{
1473 struct nfs4_opendata *data = calldata;
1474 struct nfs4_state *state = NULL;
1475
1476 /* If this request hasn't been cancelled, do nothing */
1477 if (data->cancelled == 0)
1478 goto out_free;
1479 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001480 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001481 goto out_free;
1482 /* In case we need an open_confirm, no cleanup! */
1483 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1484 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001485 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001486 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001487 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001488out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001489 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001490}
1491
1492static const struct rpc_call_ops nfs4_open_ops = {
1493 .rpc_call_prepare = nfs4_open_prepare,
1494 .rpc_call_done = nfs4_open_done,
1495 .rpc_release = nfs4_open_release,
1496};
1497
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001498static const struct rpc_call_ops nfs4_recover_open_ops = {
1499 .rpc_call_prepare = nfs4_recover_open_prepare,
1500 .rpc_call_done = nfs4_open_done,
1501 .rpc_release = nfs4_open_release,
1502};
1503
1504static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001505{
1506 struct inode *dir = data->dir->d_inode;
1507 struct nfs_server *server = NFS_SERVER(dir);
1508 struct nfs_openargs *o_arg = &data->o_arg;
1509 struct nfs_openres *o_res = &data->o_res;
1510 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001511 struct rpc_message msg = {
1512 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1513 .rpc_argp = o_arg,
1514 .rpc_resp = o_res,
1515 .rpc_cred = data->owner->so_cred,
1516 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001517 struct rpc_task_setup task_setup_data = {
1518 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001519 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001520 .callback_ops = &nfs4_open_ops,
1521 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001522 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001523 .flags = RPC_TASK_ASYNC,
1524 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001525 int status;
1526
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001527 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001528 data->rpc_done = 0;
1529 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001530 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001531 if (isrecover)
1532 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001533 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001534 if (IS_ERR(task))
1535 return PTR_ERR(task);
1536 status = nfs4_wait_for_completion_rpc_task(task);
1537 if (status != 0) {
1538 data->cancelled = 1;
1539 smp_wmb();
1540 } else
1541 status = data->rpc_status;
1542 rpc_put_task(task);
1543
1544 return status;
1545}
1546
1547static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1548{
1549 struct inode *dir = data->dir->d_inode;
1550 struct nfs_openres *o_res = &data->o_res;
1551 int status;
1552
1553 status = nfs4_run_open_task(data, 1);
1554 if (status != 0 || !data->rpc_done)
1555 return status;
1556
1557 nfs_refresh_inode(dir, o_res->dir_attr);
1558
1559 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1560 status = _nfs4_proc_open_confirm(data);
1561 if (status != 0)
1562 return status;
1563 }
1564
1565 return status;
1566}
1567
1568/*
1569 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1570 */
1571static int _nfs4_proc_open(struct nfs4_opendata *data)
1572{
1573 struct inode *dir = data->dir->d_inode;
1574 struct nfs_server *server = NFS_SERVER(dir);
1575 struct nfs_openargs *o_arg = &data->o_arg;
1576 struct nfs_openres *o_res = &data->o_res;
1577 int status;
1578
1579 status = nfs4_run_open_task(data, 0);
Trond Myklebust3e309912007-07-07 13:19:59 -04001580 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001581 return status;
1582
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001583 if (o_arg->open_flags & O_CREAT) {
1584 update_changeattr(dir, &o_res->cinfo);
1585 nfs_post_op_update_inode(dir, o_res->dir_attr);
1586 } else
1587 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001588 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1589 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001591 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001593 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 }
1595 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001596 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001597 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598}
1599
Andy Adamsond83217c2011-03-01 01:34:17 +00001600static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001601{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001602 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001603 int ret;
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001604
Trond Myklebusta78cb572009-08-09 15:06:19 -04001605 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001606 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001607 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001608 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001609 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1610 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001611 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001612 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001613 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001614 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001615 return ret;
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001616}
1617
Andy Adamsond83217c2011-03-01 01:34:17 +00001618static int nfs4_recover_expired_lease(struct nfs_server *server)
1619{
1620 return nfs4_client_recover_expired_lease(server->nfs_client);
1621}
1622
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623/*
1624 * OPEN_EXPIRED:
1625 * reclaim state on the server after a network partition.
1626 * Assumes caller holds the appropriate lock
1627 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001628static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001630 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001631 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001633 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1634 if (IS_ERR(opendata))
1635 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001636 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001637 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001638 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001639 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001640 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641}
1642
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001643static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001644{
Trond Myklebust539cd032007-06-05 11:46:42 -04001645 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001646 struct nfs4_exception exception = { };
1647 int err;
1648
1649 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001650 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001651 switch (err) {
1652 default:
1653 goto out;
1654 case -NFS4ERR_GRACE:
1655 case -NFS4ERR_DELAY:
1656 nfs4_handle_exception(server, err, &exception);
1657 err = 0;
1658 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001659 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001660out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001661 return err;
1662}
1663
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1665{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001667 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
Trond Myklebust864472e2006-01-03 09:55:15 +01001669 ctx = nfs4_state_find_open_context(state);
1670 if (IS_ERR(ctx))
1671 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001672 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001673 put_nfs_open_context(ctx);
1674 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675}
1676
1677/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001678 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1679 * fields corresponding to attributes that were used to store the verifier.
1680 * Make sure we clobber those fields in the later setattr call
1681 */
1682static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1683{
1684 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1685 !(sattr->ia_valid & ATTR_ATIME_SET))
1686 sattr->ia_valid |= ATTR_ATIME;
1687
1688 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1689 !(sattr->ia_valid & ATTR_MTIME_SET))
1690 sattr->ia_valid |= ATTR_MTIME;
1691}
1692
1693/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001694 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001696static 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 -07001697{
1698 struct nfs4_state_owner *sp;
1699 struct nfs4_state *state = NULL;
1700 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001701 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001702 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
1704 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 status = -ENOMEM;
1706 if (!(sp = nfs4_get_state_owner(server, cred))) {
1707 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1708 goto out_err;
1709 }
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001710 status = nfs4_recover_expired_lease(server);
1711 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001712 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001713 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001714 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001715 status = -ENOMEM;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001716 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001717 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001718 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Trond Myklebustaac00a82007-07-05 19:02:21 -04001720 if (path->dentry->d_inode != NULL)
1721 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1722
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001723 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001725 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001727 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001728 status = PTR_ERR(state);
1729 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001730 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001731 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001732 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001733
1734 if (opendata->o_arg.open_flags & O_EXCL) {
1735 nfs4_exclusive_attrset(opendata, sattr);
1736
1737 nfs_fattr_init(opendata->o_res.f_attr);
1738 status = nfs4_do_setattr(state->inode, cred,
1739 opendata->o_res.f_attr, sattr,
1740 state);
1741 if (status == 0)
1742 nfs_setattr_update_inode(state->inode, sattr);
1743 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1744 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001745 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 *res = state;
1748 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001749err_opendata_put:
1750 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001751err_put_state_owner:
1752 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 *res = NULL;
1755 return status;
1756}
1757
1758
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001759static 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 -07001760{
1761 struct nfs4_exception exception = { };
1762 struct nfs4_state *res;
1763 int status;
1764
1765 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001766 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 if (status == 0)
1768 break;
1769 /* NOTE: BAD_SEQID means the server and client disagree about the
1770 * book-keeping w.r.t. state-changing operations
1771 * (OPEN/CLOSE/LOCK/LOCKU...)
1772 * It is actually a sign of a bug on the client or on the server.
1773 *
1774 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001775 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 * have unhashed the old state_owner for us, and that we can
1777 * therefore safely retry using a new one. We should still warn
1778 * the user though...
1779 */
1780 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001781 printk(KERN_WARNING "NFS: v4 server %s "
1782 " returned a bad sequence-id error!\n",
1783 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 exception.retry = 1;
1785 continue;
1786 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001787 /*
1788 * BAD_STATEID on OPEN means that the server cancelled our
1789 * state before it received the OPEN_CONFIRM.
1790 * Recover by retrying the request as per the discussion
1791 * on Page 181 of RFC3530.
1792 */
1793 if (status == -NFS4ERR_BAD_STATEID) {
1794 exception.retry = 1;
1795 continue;
1796 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001797 if (status == -EAGAIN) {
1798 /* We must have found a delegation */
1799 exception.retry = 1;
1800 continue;
1801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1803 status, &exception));
1804 } while (exception.retry);
1805 return res;
1806}
1807
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001808static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1809 struct nfs_fattr *fattr, struct iattr *sattr,
1810 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001812 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001814 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 .iap = sattr,
1816 .server = server,
1817 .bitmask = server->attr_bitmask,
1818 };
1819 struct nfs_setattrres res = {
1820 .fattr = fattr,
1821 .server = server,
1822 };
1823 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001824 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1825 .rpc_argp = &arg,
1826 .rpc_resp = &res,
1827 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001829 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001830 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Trond Myklebust0e574af2005-10-27 22:12:38 -04001832 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001834 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1835 /* Use that stateid */
1836 } else if (state != NULL) {
Trond Myklebust77041ed2010-07-01 12:49:11 -04001837 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001838 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1840
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001841 status = nfs4_call_sync(server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001842 if (status == 0 && state != NULL)
1843 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001844 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845}
1846
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001847static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1848 struct nfs_fattr *fattr, struct iattr *sattr,
1849 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001851 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 struct nfs4_exception exception = { };
1853 int err;
1854 do {
1855 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001856 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 &exception);
1858 } while (exception.retry);
1859 return err;
1860}
1861
1862struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001863 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 struct inode *inode;
1865 struct nfs4_state *state;
1866 struct nfs_closeargs arg;
1867 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001868 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001869 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00001870 bool roc;
1871 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872};
1873
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001874static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001875{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001876 struct nfs4_closedata *calldata = data;
1877 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001878
Fred Isamanf7e89172011-01-06 11:36:32 +00001879 if (calldata->roc)
1880 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001881 nfs4_put_open_state(calldata->state);
1882 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001883 nfs4_put_state_owner(sp);
Jan Blunck31f31db2008-05-02 13:42:45 -07001884 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001885 kfree(calldata);
1886}
1887
Trond Myklebust88069f72009-12-08 08:33:16 -05001888static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1889 fmode_t fmode)
1890{
1891 spin_lock(&state->owner->so_lock);
1892 if (!(fmode & FMODE_READ))
1893 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1894 if (!(fmode & FMODE_WRITE))
1895 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1896 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1897 spin_unlock(&state->owner->so_lock);
1898}
1899
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001900static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001902 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 struct nfs_server *server = NFS_SERVER(calldata->inode);
1905
Trond Myklebust14516c32010-07-31 14:29:06 -04001906 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1907 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 /* hmm. we are done with the inode, and in the process of freeing
1909 * the state_owner. we keep this around to process errors
1910 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 switch (task->tk_status) {
1912 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00001913 if (calldata->roc)
1914 pnfs_roc_set_barrier(state->inode,
1915 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04001916 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001917 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001918 nfs4_close_clear_stateid_flags(state,
1919 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 break;
1921 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001922 case -NFS4ERR_OLD_STATEID:
1923 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001925 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001926 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001928 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1929 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001931 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001932 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933}
1934
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001935static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001937 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001938 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001939 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001940
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001941 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001942 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001943
Trond Myklebust88069f72009-12-08 08:33:16 -05001944 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1945 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001946 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001947 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001948 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001949 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001950 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1951 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1952 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001953 }
1954 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001955 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1956 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1957 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001958 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001959 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001960 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05001961
1962 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001963 /* Note: exit _without_ calling nfs4_close_done */
1964 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001965 return;
1966 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001967
Fred Isamanf7e89172011-01-06 11:36:32 +00001968 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001969 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00001970 if (calldata->roc &&
1971 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
1972 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
1973 task, NULL);
1974 return;
1975 }
1976 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001977
Trond Myklebust516a6af2005-10-27 22:12:41 -04001978 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001979 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001980 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Andy Adamson19ddab02009-04-01 09:22:20 -04001981 &calldata->arg.seq_args, &calldata->res.seq_res,
1982 1, task))
1983 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001984 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985}
1986
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001987static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001988 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001989 .rpc_call_done = nfs4_close_done,
1990 .rpc_release = nfs4_free_closedata,
1991};
1992
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993/*
1994 * It is possible for data to be read/written from a mem-mapped file
1995 * after the sys_close call (which hits the vfs layer as a flush).
1996 * This means that we can't safely call nfsv4 close on a file until
1997 * the inode is cleared. This in turn means that we are not good
1998 * NFSv4 citizens - we do not indicate to the server to update the file's
1999 * share state even when we are done with one of the three share
2000 * stateid's in the inode.
2001 *
2002 * NOTE: Caller must be holding the sp->so_owner semaphore!
2003 */
Fred Isamanf7e89172011-01-06 11:36:32 +00002004int nfs4_do_close(struct path *path, struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002006 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002008 struct nfs4_state_owner *sp = state->owner;
2009 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002010 struct rpc_message msg = {
2011 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2012 .rpc_cred = state->owner->so_cred,
2013 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002014 struct rpc_task_setup task_setup_data = {
2015 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002016 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002017 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002018 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002019 .flags = RPC_TASK_ASYNC,
2020 };
Trond Myklebust95121352005-10-18 14:20:12 -07002021 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002023 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002025 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002026 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002028 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002029 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002031 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002032 if (calldata->arg.seqid == NULL)
2033 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002034 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002035 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002036 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002037 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002038 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002039 calldata->roc = roc;
Al Virof6948692010-01-30 13:51:04 -05002040 path_get(path);
2041 calldata->path = *path;
Trond Myklebust95121352005-10-18 14:20:12 -07002042
Trond Myklebust1174dd12010-12-21 10:52:24 -05002043 msg.rpc_argp = &calldata->arg;
2044 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002045 task_setup_data.callback_data = calldata;
2046 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002047 if (IS_ERR(task))
2048 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002049 status = 0;
2050 if (wait)
2051 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002052 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002053 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002054out_free_calldata:
2055 kfree(calldata);
2056out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002057 if (roc)
2058 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002059 nfs4_put_open_state(state);
2060 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002061 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062}
2063
Trond Myklebust2b484292010-09-17 10:56:51 -04002064static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002065nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 struct nfs4_state *state;
2068
Trond Myklebust565277f2007-10-15 18:17:53 -04002069 /* Protect against concurrent sillydeletes */
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002070 state = nfs4_do_open(dir, &ctx->path, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002071 if (IS_ERR(state))
2072 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002073 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002074 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075}
2076
Trond Myklebust1185a552009-12-03 15:54:02 -05002077static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002078{
2079 if (ctx->state == NULL)
2080 return;
2081 if (is_sync)
2082 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2083 else
2084 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2085}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
2087static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2088{
Benny Halevy43652ad2009-04-01 09:21:54 -04002089 struct nfs4_server_caps_arg args = {
2090 .fhandle = fhandle,
2091 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 struct nfs4_server_caps_res res = {};
2093 struct rpc_message msg = {
2094 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002095 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 .rpc_resp = &res,
2097 };
2098 int status;
2099
Bryan Schumakere73b83f2011-03-24 17:12:23 +00002100 status = nfs4_call_sync(server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 if (status == 0) {
2102 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002103 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2104 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2105 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2106 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2107 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2109 server->caps |= NFS_CAP_ACLS;
2110 if (res.has_links != 0)
2111 server->caps |= NFS_CAP_HARDLINKS;
2112 if (res.has_symlinks != 0)
2113 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002114 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2115 server->caps |= NFS_CAP_FILEID;
2116 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2117 server->caps |= NFS_CAP_MODE;
2118 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2119 server->caps |= NFS_CAP_NLINK;
2120 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2121 server->caps |= NFS_CAP_OWNER;
2122 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2123 server->caps |= NFS_CAP_OWNER_GROUP;
2124 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2125 server->caps |= NFS_CAP_ATIME;
2126 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2127 server->caps |= NFS_CAP_CTIME;
2128 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2129 server->caps |= NFS_CAP_MTIME;
2130
Trond Myklebusta65318b2009-03-11 14:10:28 -04002131 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2132 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2133 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 server->acl_bitmask = res.acl_bitmask;
2135 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002136
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 return status;
2138}
2139
Trond Myklebust55a97592006-06-09 09:34:19 -04002140int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141{
2142 struct nfs4_exception exception = { };
2143 int err;
2144 do {
2145 err = nfs4_handle_exception(server,
2146 _nfs4_server_capabilities(server, fhandle),
2147 &exception);
2148 } while (exception.retry);
2149 return err;
2150}
2151
2152static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2153 struct nfs_fsinfo *info)
2154{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 struct nfs4_lookup_root_arg args = {
2156 .bitmask = nfs4_fattr_bitmap,
2157 };
2158 struct nfs4_lookup_res res = {
2159 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002160 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 .fh = fhandle,
2162 };
2163 struct rpc_message msg = {
2164 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2165 .rpc_argp = &args,
2166 .rpc_resp = &res,
2167 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002168
Trond Myklebust0e574af2005-10-27 22:12:38 -04002169 nfs_fattr_init(info->fattr);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00002170 return nfs4_call_sync(server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171}
2172
2173static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2174 struct nfs_fsinfo *info)
2175{
2176 struct nfs4_exception exception = { };
2177 int err;
2178 do {
2179 err = nfs4_handle_exception(server,
2180 _nfs4_lookup_root(server, fhandle, info),
2181 &exception);
2182 } while (exception.retry);
2183 return err;
2184}
2185
David Howells54ceac42006-08-22 20:06:13 -04002186/*
2187 * get the file handle for the "/" directory on the server
2188 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002190 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 int status;
2193
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 if (status == 0)
2196 status = nfs4_server_capabilities(server, fhandle);
2197 if (status == 0)
2198 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002199 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200}
2201
Manoj Naik6b97fd32006-06-09 09:34:29 -04002202/*
2203 * Get locations and (maybe) other attributes of a referral.
2204 * Note that we'll actually follow the referral later when
2205 * we detect fsid mismatch in inode revalidation
2206 */
Trond Myklebust56659e92007-07-17 21:52:39 -04002207static 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 -04002208{
2209 int status = -ENOMEM;
2210 struct page *page = NULL;
2211 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002212
2213 page = alloc_page(GFP_KERNEL);
2214 if (page == NULL)
2215 goto out;
2216 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2217 if (locations == NULL)
2218 goto out;
2219
Trond Myklebustc228fd32007-01-13 02:28:11 -05002220 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002221 if (status != 0)
2222 goto out;
2223 /* Make sure server returned a different fsid for the referral */
2224 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07002225 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 -04002226 status = -EIO;
2227 goto out;
2228 }
2229
2230 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2231 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2232 if (!fattr->mode)
2233 fattr->mode = S_IFDIR;
2234 memset(fhandle, 0, sizeof(struct nfs_fh));
2235out:
2236 if (page)
2237 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002238 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002239 return status;
2240}
2241
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2243{
2244 struct nfs4_getattr_arg args = {
2245 .fh = fhandle,
2246 .bitmask = server->attr_bitmask,
2247 };
2248 struct nfs4_getattr_res res = {
2249 .fattr = fattr,
2250 .server = server,
2251 };
2252 struct rpc_message msg = {
2253 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2254 .rpc_argp = &args,
2255 .rpc_resp = &res,
2256 };
2257
Trond Myklebust0e574af2005-10-27 22:12:38 -04002258 nfs_fattr_init(fattr);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00002259 return nfs4_call_sync(server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260}
2261
2262static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2263{
2264 struct nfs4_exception exception = { };
2265 int err;
2266 do {
2267 err = nfs4_handle_exception(server,
2268 _nfs4_proc_getattr(server, fhandle, fattr),
2269 &exception);
2270 } while (exception.retry);
2271 return err;
2272}
2273
2274/*
2275 * The file is not closed if it is opened due to the a request to change
2276 * the size of the file. The open call will not be needed once the
2277 * VFS layer lookup-intents are implemented.
2278 *
2279 * Close is called when the inode is destroyed.
2280 * If we haven't opened the file for O_WRONLY, we
2281 * need to in the size_change case to obtain a stateid.
2282 *
2283 * Got race?
2284 * Because OPEN is always done by name in nfsv4, it is
2285 * possible that we opened a different file by the same
2286 * name. We can recognize this race condition, but we
2287 * can't do anything about it besides returning an error.
2288 *
2289 * This will be fixed with VFS changes (lookup-intent).
2290 */
2291static int
2292nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2293 struct iattr *sattr)
2294{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002295 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002296 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002297 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 int status;
2299
Trond Myklebust0e574af2005-10-27 22:12:38 -04002300 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
Trond Myklebustd5308382005-11-04 15:33:38 -05002302 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002303 if (sattr->ia_valid & ATTR_FILE) {
2304 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002305
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002306 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002307 if (ctx) {
2308 cred = ctx->cred;
2309 state = ctx->state;
2310 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002311 }
2312
2313 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002314 if (status == 0)
2315 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 return status;
2317}
2318
Trond Myklebust56659e92007-07-17 21:52:39 -04002319static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2320 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04002321 struct nfs_fattr *fattr)
2322{
2323 int status;
2324 struct nfs4_lookup_arg args = {
2325 .bitmask = server->attr_bitmask,
2326 .dir_fh = dirfh,
2327 .name = name,
2328 };
2329 struct nfs4_lookup_res res = {
2330 .server = server,
2331 .fattr = fattr,
2332 .fh = fhandle,
2333 };
2334 struct rpc_message msg = {
2335 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2336 .rpc_argp = &args,
2337 .rpc_resp = &res,
2338 };
2339
2340 nfs_fattr_init(fattr);
2341
2342 dprintk("NFS call lookupfh %s\n", name->name);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00002343 status = nfs4_call_sync(server, &msg, &args.seq_args, &res.seq_res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002344 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002345 return status;
2346}
2347
2348static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2349 struct qstr *name, struct nfs_fh *fhandle,
2350 struct nfs_fattr *fattr)
2351{
2352 struct nfs4_exception exception = { };
2353 int err;
2354 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04002355 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2356 /* FIXME: !!!! */
2357 if (err == -NFS4ERR_MOVED) {
2358 err = -EREMOTE;
2359 break;
2360 }
2361 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002362 } while (exception.retry);
2363 return err;
2364}
2365
Trond Myklebust56659e92007-07-17 21:52:39 -04002366static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2368{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002369 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
2371 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002372 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002373 if (status == -NFS4ERR_MOVED)
2374 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 dprintk("NFS reply lookup: %d\n", status);
2376 return status;
2377}
2378
2379static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2380{
2381 struct nfs4_exception exception = { };
2382 int err;
2383 do {
2384 err = nfs4_handle_exception(NFS_SERVER(dir),
2385 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2386 &exception);
2387 } while (exception.retry);
2388 return err;
2389}
2390
2391static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2392{
Trond Myklebust76b32992007-08-10 17:45:11 -04002393 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 struct nfs4_accessargs args = {
2395 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002396 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002398 struct nfs4_accessres res = {
2399 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002400 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 struct rpc_message msg = {
2402 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2403 .rpc_argp = &args,
2404 .rpc_resp = &res,
2405 .rpc_cred = entry->cred,
2406 };
2407 int mode = entry->mask;
2408 int status;
2409
2410 /*
2411 * Determine which access bits we want to ask for...
2412 */
2413 if (mode & MAY_READ)
2414 args.access |= NFS4_ACCESS_READ;
2415 if (S_ISDIR(inode->i_mode)) {
2416 if (mode & MAY_WRITE)
2417 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2418 if (mode & MAY_EXEC)
2419 args.access |= NFS4_ACCESS_LOOKUP;
2420 } else {
2421 if (mode & MAY_WRITE)
2422 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2423 if (mode & MAY_EXEC)
2424 args.access |= NFS4_ACCESS_EXECUTE;
2425 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002426
2427 res.fattr = nfs_alloc_fattr();
2428 if (res.fattr == NULL)
2429 return -ENOMEM;
2430
Bryan Schumakere73b83f2011-03-24 17:12:23 +00002431 status = nfs4_call_sync(server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 if (!status) {
2433 entry->mask = 0;
2434 if (res.access & NFS4_ACCESS_READ)
2435 entry->mask |= MAY_READ;
2436 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2437 entry->mask |= MAY_WRITE;
2438 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2439 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002440 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002442 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 return status;
2444}
2445
2446static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2447{
2448 struct nfs4_exception exception = { };
2449 int err;
2450 do {
2451 err = nfs4_handle_exception(NFS_SERVER(inode),
2452 _nfs4_proc_access(inode, entry),
2453 &exception);
2454 } while (exception.retry);
2455 return err;
2456}
2457
2458/*
2459 * TODO: For the time being, we don't try to get any attributes
2460 * along with any of the zero-copy operations READ, READDIR,
2461 * READLINK, WRITE.
2462 *
2463 * In the case of the first three, we want to put the GETATTR
2464 * after the read-type operation -- this is because it is hard
2465 * to predict the length of a GETATTR response in v4, and thus
2466 * align the READ data correctly. This means that the GETATTR
2467 * may end up partially falling into the page cache, and we should
2468 * shift it into the 'tail' of the xdr_buf before processing.
2469 * To do this efficiently, we need to know the total length
2470 * of data received, which doesn't seem to be available outside
2471 * of the RPC layer.
2472 *
2473 * In the case of WRITE, we also want to put the GETATTR after
2474 * the operation -- in this case because we want to make sure
2475 * we get the post-operation mtime and size. This means that
2476 * we can't use xdr_encode_pages() as written: we need a variant
2477 * of it which would leave room in the 'tail' iovec.
2478 *
2479 * Both of these changes to the XDR layer would in fact be quite
2480 * minor, but I decided to leave them for a subsequent patch.
2481 */
2482static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2483 unsigned int pgbase, unsigned int pglen)
2484{
2485 struct nfs4_readlink args = {
2486 .fh = NFS_FH(inode),
2487 .pgbase = pgbase,
2488 .pglen = pglen,
2489 .pages = &page,
2490 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002491 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 struct rpc_message msg = {
2493 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2494 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002495 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 };
2497
Bryan Schumakere73b83f2011-03-24 17:12:23 +00002498 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499}
2500
2501static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2502 unsigned int pgbase, unsigned int pglen)
2503{
2504 struct nfs4_exception exception = { };
2505 int err;
2506 do {
2507 err = nfs4_handle_exception(NFS_SERVER(inode),
2508 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2509 &exception);
2510 } while (exception.retry);
2511 return err;
2512}
2513
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514/*
2515 * Got race?
2516 * We will need to arrange for the VFS layer to provide an atomic open.
2517 * Until then, this create/open method is prone to inefficiency and race
2518 * conditions due to the lookup, create, and open VFS calls from sys_open()
2519 * placed on the wire.
2520 *
2521 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2522 * The file will be opened again in the subsequent VFS open call
2523 * (nfs4_proc_file_open).
2524 *
2525 * The open for read will just hang around to be used by any process that
2526 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2527 */
2528
2529static int
2530nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002531 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532{
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002533 struct path my_path = {
Trond Myklebustad389da2007-06-05 12:30:00 -04002534 .dentry = dentry,
2535 };
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002536 struct path *path = &my_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002538 struct rpc_cred *cred = NULL;
2539 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 int status = 0;
2541
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002542 if (ctx != NULL) {
2543 cred = ctx->cred;
2544 path = &ctx->path;
2545 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002547 sattr->ia_mode &= ~current_umask();
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002548 state = nfs4_do_open(dir, path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002549 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 if (IS_ERR(state)) {
2551 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002552 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002554 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002555 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002556 if (ctx != NULL)
2557 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002558 else
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002559 nfs4_close_sync(path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560out:
2561 return status;
2562}
2563
2564static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2565{
Trond Myklebust16e42952005-10-27 22:12:44 -04002566 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002567 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002569 .name.len = name->len,
2570 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002571 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002573 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002574 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002575 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002577 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2578 .rpc_argp = &args,
2579 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002581 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
Trond Myklebustd3468902010-04-16 16:22:50 -04002583 res.dir_attr = nfs_alloc_fattr();
2584 if (res.dir_attr == NULL)
2585 goto out;
2586
Bryan Schumakere73b83f2011-03-24 17:12:23 +00002587 status = nfs4_call_sync(server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002588 if (status == 0) {
2589 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002590 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002591 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002592 nfs_free_fattr(res.dir_attr);
2593out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 return status;
2595}
2596
2597static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2598{
2599 struct nfs4_exception exception = { };
2600 int err;
2601 do {
2602 err = nfs4_handle_exception(NFS_SERVER(dir),
2603 _nfs4_proc_remove(dir, name),
2604 &exception);
2605 } while (exception.retry);
2606 return err;
2607}
2608
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002609static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002611 struct nfs_server *server = NFS_SERVER(dir);
2612 struct nfs_removeargs *args = msg->rpc_argp;
2613 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614
Trond Myklebusta65318b2009-03-11 14:10:28 -04002615 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002616 res->server = server;
Benny Halevydfb4f302010-09-24 09:17:01 -04002617 res->seq_res.sr_slot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619}
2620
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002621static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002623 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2624
Trond Myklebust14516c32010-07-31 14:29:06 -04002625 if (!nfs4_sequence_done(task, &res->seq_res))
2626 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002627 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002628 return 0;
2629 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002630 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002631 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632}
2633
Jeff Laytond3d41522010-09-17 17:31:57 -04002634static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2635{
2636 struct nfs_server *server = NFS_SERVER(dir);
2637 struct nfs_renameargs *arg = msg->rpc_argp;
2638 struct nfs_renameres *res = msg->rpc_resp;
2639
2640 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2641 arg->bitmask = server->attr_bitmask;
2642 res->server = server;
2643}
2644
2645static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2646 struct inode *new_dir)
2647{
2648 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2649
2650 if (!nfs4_sequence_done(task, &res->seq_res))
2651 return 0;
2652 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2653 return 0;
2654
2655 update_changeattr(old_dir, &res->old_cinfo);
2656 nfs_post_op_update_inode(old_dir, res->old_fattr);
2657 update_changeattr(new_dir, &res->new_cinfo);
2658 nfs_post_op_update_inode(new_dir, res->new_fattr);
2659 return 1;
2660}
2661
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2663 struct inode *new_dir, struct qstr *new_name)
2664{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002665 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002666 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 .old_dir = NFS_FH(old_dir),
2668 .new_dir = NFS_FH(new_dir),
2669 .old_name = old_name,
2670 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002671 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002673 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002674 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002675 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 struct rpc_message msg = {
2677 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2678 .rpc_argp = &arg,
2679 .rpc_resp = &res,
2680 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002681 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682
Trond Myklebust011fff72010-04-16 16:22:49 -04002683 res.old_fattr = nfs_alloc_fattr();
2684 res.new_fattr = nfs_alloc_fattr();
2685 if (res.old_fattr == NULL || res.new_fattr == NULL)
2686 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
Bryan Schumakere73b83f2011-03-24 17:12:23 +00002688 status = nfs4_call_sync(server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 if (!status) {
2690 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002691 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002693 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002695out:
2696 nfs_free_fattr(res.new_fattr);
2697 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 return status;
2699}
2700
2701static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2702 struct inode *new_dir, struct qstr *new_name)
2703{
2704 struct nfs4_exception exception = { };
2705 int err;
2706 do {
2707 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2708 _nfs4_proc_rename(old_dir, old_name,
2709 new_dir, new_name),
2710 &exception);
2711 } while (exception.retry);
2712 return err;
2713}
2714
2715static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2716{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002717 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 struct nfs4_link_arg arg = {
2719 .fh = NFS_FH(inode),
2720 .dir_fh = NFS_FH(dir),
2721 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002722 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002724 struct nfs4_link_res res = {
2725 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002726 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 struct rpc_message msg = {
2728 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2729 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002730 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002732 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733
Trond Myklebust136f2622010-04-16 16:22:49 -04002734 res.fattr = nfs_alloc_fattr();
2735 res.dir_attr = nfs_alloc_fattr();
2736 if (res.fattr == NULL || res.dir_attr == NULL)
2737 goto out;
2738
Bryan Schumakere73b83f2011-03-24 17:12:23 +00002739 status = nfs4_call_sync(server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002740 if (!status) {
2741 update_changeattr(dir, &res.cinfo);
2742 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002743 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002744 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002745out:
2746 nfs_free_fattr(res.dir_attr);
2747 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 return status;
2749}
2750
2751static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2752{
2753 struct nfs4_exception exception = { };
2754 int err;
2755 do {
2756 err = nfs4_handle_exception(NFS_SERVER(inode),
2757 _nfs4_proc_link(inode, dir, name),
2758 &exception);
2759 } while (exception.retry);
2760 return err;
2761}
2762
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002763struct nfs4_createdata {
2764 struct rpc_message msg;
2765 struct nfs4_create_arg arg;
2766 struct nfs4_create_res res;
2767 struct nfs_fh fh;
2768 struct nfs_fattr fattr;
2769 struct nfs_fattr dir_fattr;
2770};
2771
2772static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2773 struct qstr *name, struct iattr *sattr, u32 ftype)
2774{
2775 struct nfs4_createdata *data;
2776
2777 data = kzalloc(sizeof(*data), GFP_KERNEL);
2778 if (data != NULL) {
2779 struct nfs_server *server = NFS_SERVER(dir);
2780
2781 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2782 data->msg.rpc_argp = &data->arg;
2783 data->msg.rpc_resp = &data->res;
2784 data->arg.dir_fh = NFS_FH(dir);
2785 data->arg.server = server;
2786 data->arg.name = name;
2787 data->arg.attrs = sattr;
2788 data->arg.ftype = ftype;
2789 data->arg.bitmask = server->attr_bitmask;
2790 data->res.server = server;
2791 data->res.fh = &data->fh;
2792 data->res.fattr = &data->fattr;
2793 data->res.dir_fattr = &data->dir_fattr;
2794 nfs_fattr_init(data->res.fattr);
2795 nfs_fattr_init(data->res.dir_fattr);
2796 }
2797 return data;
2798}
2799
2800static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2801{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002802 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00002803 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002804 if (status == 0) {
2805 update_changeattr(dir, &data->res.dir_cinfo);
2806 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2807 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2808 }
2809 return status;
2810}
2811
2812static void nfs4_free_createdata(struct nfs4_createdata *data)
2813{
2814 kfree(data);
2815}
2816
Chuck Lever4f390c12006-08-22 20:06:22 -04002817static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002818 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002820 struct nfs4_createdata *data;
2821 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
Chuck Lever94a6d752006-08-22 20:06:23 -04002823 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002824 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002825
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002826 status = -ENOMEM;
2827 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2828 if (data == NULL)
2829 goto out;
2830
2831 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2832 data->arg.u.symlink.pages = &page;
2833 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002835 status = nfs4_do_create(dir, dentry, data);
2836
2837 nfs4_free_createdata(data);
2838out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 return status;
2840}
2841
Chuck Lever4f390c12006-08-22 20:06:22 -04002842static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002843 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844{
2845 struct nfs4_exception exception = { };
2846 int err;
2847 do {
2848 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002849 _nfs4_proc_symlink(dir, dentry, page,
2850 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 &exception);
2852 } while (exception.retry);
2853 return err;
2854}
2855
2856static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2857 struct iattr *sattr)
2858{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002859 struct nfs4_createdata *data;
2860 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002862 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2863 if (data == NULL)
2864 goto out;
2865
2866 status = nfs4_do_create(dir, dentry, data);
2867
2868 nfs4_free_createdata(data);
2869out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 return status;
2871}
2872
2873static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2874 struct iattr *sattr)
2875{
2876 struct nfs4_exception exception = { };
2877 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002878
2879 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 do {
2881 err = nfs4_handle_exception(NFS_SERVER(dir),
2882 _nfs4_proc_mkdir(dir, dentry, sattr),
2883 &exception);
2884 } while (exception.retry);
2885 return err;
2886}
2887
2888static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002889 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890{
2891 struct inode *dir = dentry->d_inode;
2892 struct nfs4_readdir_arg args = {
2893 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002894 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 .pgbase = 0,
2896 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002897 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04002898 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 };
2900 struct nfs4_readdir_res res;
2901 struct rpc_message msg = {
2902 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2903 .rpc_argp = &args,
2904 .rpc_resp = &res,
2905 .rpc_cred = cred,
2906 };
2907 int status;
2908
Harvey Harrison3110ff82008-05-02 13:42:44 -07002909 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002910 dentry->d_parent->d_name.name,
2911 dentry->d_name.name,
2912 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2914 res.pgbase = args.pgbase;
Bryan Schumakere73b83f2011-03-24 17:12:23 +00002915 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebustac396122010-11-15 20:26:22 -05002916 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05002918 status += args.pgbase;
2919 }
Trond Myklebustc4812992007-09-28 17:11:45 -04002920
2921 nfs_invalidate_atime(dir);
2922
Harvey Harrison3110ff82008-05-02 13:42:44 -07002923 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 return status;
2925}
2926
2927static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002928 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929{
2930 struct nfs4_exception exception = { };
2931 int err;
2932 do {
2933 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2934 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002935 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 &exception);
2937 } while (exception.retry);
2938 return err;
2939}
2940
2941static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2942 struct iattr *sattr, dev_t rdev)
2943{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002944 struct nfs4_createdata *data;
2945 int mode = sattr->ia_mode;
2946 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947
2948 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2949 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002950
2951 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2952 if (data == NULL)
2953 goto out;
2954
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002956 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002958 data->arg.ftype = NF4BLK;
2959 data->arg.u.device.specdata1 = MAJOR(rdev);
2960 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 }
2962 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002963 data->arg.ftype = NF4CHR;
2964 data->arg.u.device.specdata1 = MAJOR(rdev);
2965 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002968 status = nfs4_do_create(dir, dentry, data);
2969
2970 nfs4_free_createdata(data);
2971out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 return status;
2973}
2974
2975static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2976 struct iattr *sattr, dev_t rdev)
2977{
2978 struct nfs4_exception exception = { };
2979 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002980
2981 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 do {
2983 err = nfs4_handle_exception(NFS_SERVER(dir),
2984 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2985 &exception);
2986 } while (exception.retry);
2987 return err;
2988}
2989
2990static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2991 struct nfs_fsstat *fsstat)
2992{
2993 struct nfs4_statfs_arg args = {
2994 .fh = fhandle,
2995 .bitmask = server->attr_bitmask,
2996 };
Benny Halevy24ad1482009-04-01 09:21:56 -04002997 struct nfs4_statfs_res res = {
2998 .fsstat = fsstat,
2999 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 struct rpc_message msg = {
3001 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3002 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003003 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 };
3005
Trond Myklebust0e574af2005-10-27 22:12:38 -04003006 nfs_fattr_init(fsstat->fattr);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003007 return nfs4_call_sync(server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008}
3009
3010static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3011{
3012 struct nfs4_exception exception = { };
3013 int err;
3014 do {
3015 err = nfs4_handle_exception(server,
3016 _nfs4_proc_statfs(server, fhandle, fsstat),
3017 &exception);
3018 } while (exception.retry);
3019 return err;
3020}
3021
3022static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3023 struct nfs_fsinfo *fsinfo)
3024{
3025 struct nfs4_fsinfo_arg args = {
3026 .fh = fhandle,
3027 .bitmask = server->attr_bitmask,
3028 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003029 struct nfs4_fsinfo_res res = {
3030 .fsinfo = fsinfo,
3031 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 struct rpc_message msg = {
3033 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3034 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003035 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 };
3037
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003038 return nfs4_call_sync(server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039}
3040
3041static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3042{
3043 struct nfs4_exception exception = { };
3044 int err;
3045
3046 do {
3047 err = nfs4_handle_exception(server,
3048 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3049 &exception);
3050 } while (exception.retry);
3051 return err;
3052}
3053
3054static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3055{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003056 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3058}
3059
3060static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3061 struct nfs_pathconf *pathconf)
3062{
3063 struct nfs4_pathconf_arg args = {
3064 .fh = fhandle,
3065 .bitmask = server->attr_bitmask,
3066 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003067 struct nfs4_pathconf_res res = {
3068 .pathconf = pathconf,
3069 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 struct rpc_message msg = {
3071 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3072 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003073 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 };
3075
3076 /* None of the pathconf attributes are mandatory to implement */
3077 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3078 memset(pathconf, 0, sizeof(*pathconf));
3079 return 0;
3080 }
3081
Trond Myklebust0e574af2005-10-27 22:12:38 -04003082 nfs_fattr_init(pathconf->fattr);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003083 return nfs4_call_sync(server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084}
3085
3086static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3087 struct nfs_pathconf *pathconf)
3088{
3089 struct nfs4_exception exception = { };
3090 int err;
3091
3092 do {
3093 err = nfs4_handle_exception(server,
3094 _nfs4_proc_pathconf(server, fhandle, pathconf),
3095 &exception);
3096 } while (exception.retry);
3097 return err;
3098}
3099
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003100static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101{
Trond Myklebustec06c092006-03-20 13:44:27 -05003102 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003104 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003105 nfs_restart_rpc(task, server->nfs_client);
Trond Myklebustec06c092006-03-20 13:44:27 -05003106 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003108
3109 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003111 renew_lease(server, data->timestamp);
3112 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113}
3114
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003115static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3116{
3117
3118 dprintk("--> %s\n", __func__);
3119
3120 if (!nfs4_sequence_done(task, &data->res.seq_res))
3121 return -EAGAIN;
3122
3123 return data->read_done_cb(task, data);
3124}
3125
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003126static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003129 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003130 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131}
3132
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003133/* Reset the the nfs_read_data to send the read to the MDS. */
3134void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3135{
3136 dprintk("%s Reset task for i/o through\n", __func__);
3137 put_lseg(data->lseg);
3138 data->lseg = NULL;
3139 /* offsets will differ in the dense stripe case */
3140 data->args.offset = data->mds_offset;
3141 data->ds_clp = NULL;
3142 data->args.fh = NFS_FH(data->inode);
3143 data->read_done_cb = nfs4_read_done_cb;
3144 task->tk_ops = data->mds_ops;
3145 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3146}
3147EXPORT_SYMBOL_GPL(nfs4_reset_read);
3148
Fred Isamanb029bc92011-03-03 15:13:42 +00003149static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 struct inode *inode = data->inode;
3152
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003153 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003154 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003155 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003157 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003159 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003160 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003161 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162}
3163
Fred Isamanb029bc92011-03-03 15:13:42 +00003164static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3165{
3166 if (!nfs4_sequence_done(task, &data->res.seq_res))
3167 return -EAGAIN;
3168 return data->write_done_cb(task, data);
3169}
3170
Fred Isamana69aef12011-03-03 15:13:47 +00003171/* Reset the the nfs_write_data to send the write to the MDS. */
3172void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3173{
3174 dprintk("%s Reset task for i/o through\n", __func__);
3175 put_lseg(data->lseg);
3176 data->lseg = NULL;
3177 data->ds_clp = NULL;
3178 data->write_done_cb = nfs4_write_done_cb;
3179 data->args.fh = NFS_FH(data->inode);
3180 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3181 data->args.offset = data->mds_offset;
3182 data->res.fattr = &data->fattr;
3183 task->tk_ops = data->mds_ops;
3184 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3185}
3186EXPORT_SYMBOL_GPL(nfs4_reset_write);
3187
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003188static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003190 struct nfs_server *server = NFS_SERVER(data->inode);
3191
Fred Isaman7ffd1062011-03-03 15:13:46 +00003192 if (data->lseg) {
3193 data->args.bitmask = NULL;
3194 data->res.fattr = NULL;
3195 } else
3196 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isamanb029bc92011-03-03 15:13:42 +00003197 if (!data->write_done_cb)
3198 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003199 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 data->timestamp = jiffies;
3201
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003202 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203}
3204
Trond Myklebust788e7a82006-03-20 13:44:27 -05003205static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 struct inode *inode = data->inode;
3208
Trond Myklebust14516c32010-07-31 14:29:06 -04003209 if (!nfs4_sequence_done(task, &data->res.seq_res))
3210 return -EAGAIN;
3211
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003212 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003213 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003214 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003216 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003217 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218}
3219
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003220static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003222 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223
Trond Myklebusta65318b2009-03-11 14:10:28 -04003224 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003225 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003226 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227}
3228
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003229struct nfs4_renewdata {
3230 struct nfs_client *client;
3231 unsigned long timestamp;
3232};
3233
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234/*
3235 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3236 * standalone procedure for queueing an asynchronous RENEW.
3237 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003238static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003239{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003240 struct nfs4_renewdata *data = calldata;
3241 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003242
Alexandros Batsakis0851de02010-02-05 03:45:06 -08003243 if (atomic_read(&clp->cl_count) > 1)
3244 nfs4_schedule_state_renewal(clp);
3245 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003246 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003247}
3248
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003249static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003251 struct nfs4_renewdata *data = calldata;
3252 struct nfs_client *clp = data->client;
3253 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254
3255 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003256 /* Unless we're shutting down, schedule state recovery! */
3257 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003258 nfs4_schedule_lease_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 return;
3260 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003261 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262}
3263
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003264static const struct rpc_call_ops nfs4_renew_ops = {
3265 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003266 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003267};
3268
David Howellsadfa6f92006-08-22 20:06:08 -04003269int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270{
3271 struct rpc_message msg = {
3272 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3273 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003274 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003276 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277
Alexandros Batsakis0851de02010-02-05 03:45:06 -08003278 if (!atomic_inc_not_zero(&clp->cl_count))
3279 return -EIO;
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003280 data = kmalloc(sizeof(*data), GFP_KERNEL);
3281 if (data == NULL)
3282 return -ENOMEM;
3283 data->client = clp;
3284 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003286 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287}
3288
David Howellsadfa6f92006-08-22 20:06:08 -04003289int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290{
3291 struct rpc_message msg = {
3292 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3293 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003294 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 };
3296 unsigned long now = jiffies;
3297 int status;
3298
3299 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3300 if (status < 0)
3301 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003302 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 return 0;
3304}
3305
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003306static inline int nfs4_server_supports_acls(struct nfs_server *server)
3307{
3308 return (server->caps & NFS_CAP_ACLS)
3309 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3310 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3311}
3312
3313/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3314 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3315 * the stack.
3316 */
3317#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3318
3319static void buf_to_pages(const void *buf, size_t buflen,
3320 struct page **pages, unsigned int *pgbase)
3321{
3322 const void *p = buf;
3323
3324 *pgbase = offset_in_page(buf);
3325 p -= *pgbase;
3326 while (p < buf + buflen) {
3327 *(pages++) = virt_to_page(p);
3328 p += PAGE_CACHE_SIZE;
3329 }
3330}
3331
Neil Hormane9e3d722011-03-04 19:26:03 -05003332static int buf_to_pages_noslab(const void *buf, size_t buflen,
3333 struct page **pages, unsigned int *pgbase)
3334{
3335 struct page *newpage, **spages;
3336 int rc = 0;
3337 size_t len;
3338 spages = pages;
3339
3340 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003341 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003342 newpage = alloc_page(GFP_KERNEL);
3343
3344 if (newpage == NULL)
3345 goto unwind;
3346 memcpy(page_address(newpage), buf, len);
3347 buf += len;
3348 buflen -= len;
3349 *pages++ = newpage;
3350 rc++;
3351 } while (buflen != 0);
3352
3353 return rc;
3354
3355unwind:
3356 for(; rc > 0; rc--)
3357 __free_page(spages[rc-1]);
3358 return -ENOMEM;
3359}
3360
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003361struct nfs4_cached_acl {
3362 int cached;
3363 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003364 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003365};
3366
3367static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003368{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003369 struct nfs_inode *nfsi = NFS_I(inode);
3370
3371 spin_lock(&inode->i_lock);
3372 kfree(nfsi->nfs4_acl);
3373 nfsi->nfs4_acl = acl;
3374 spin_unlock(&inode->i_lock);
3375}
3376
3377static void nfs4_zap_acl_attr(struct inode *inode)
3378{
3379 nfs4_set_cached_acl(inode, NULL);
3380}
3381
3382static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3383{
3384 struct nfs_inode *nfsi = NFS_I(inode);
3385 struct nfs4_cached_acl *acl;
3386 int ret = -ENOENT;
3387
3388 spin_lock(&inode->i_lock);
3389 acl = nfsi->nfs4_acl;
3390 if (acl == NULL)
3391 goto out;
3392 if (buf == NULL) /* user is just asking for length */
3393 goto out_len;
3394 if (acl->cached == 0)
3395 goto out;
3396 ret = -ERANGE; /* see getxattr(2) man page */
3397 if (acl->len > buflen)
3398 goto out;
3399 memcpy(buf, acl->data, acl->len);
3400out_len:
3401 ret = acl->len;
3402out:
3403 spin_unlock(&inode->i_lock);
3404 return ret;
3405}
3406
3407static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3408{
3409 struct nfs4_cached_acl *acl;
3410
3411 if (buf && acl_len <= PAGE_SIZE) {
3412 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3413 if (acl == NULL)
3414 goto out;
3415 acl->cached = 1;
3416 memcpy(acl->data, buf, acl_len);
3417 } else {
3418 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3419 if (acl == NULL)
3420 goto out;
3421 acl->cached = 0;
3422 }
3423 acl->len = acl_len;
3424out:
3425 nfs4_set_cached_acl(inode, acl);
3426}
3427
Trond Myklebust16b42892006-08-24 12:27:15 -04003428static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003429{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003430 struct page *pages[NFS4ACL_MAXPAGES];
3431 struct nfs_getaclargs args = {
3432 .fh = NFS_FH(inode),
3433 .acl_pages = pages,
3434 .acl_len = buflen,
3435 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003436 struct nfs_getaclres res = {
3437 .acl_len = buflen,
3438 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003439 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003440 struct rpc_message msg = {
3441 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3442 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003443 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003444 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003445 struct page *localpage = NULL;
3446 int ret;
3447
3448 if (buflen < PAGE_SIZE) {
3449 /* As long as we're doing a round trip to the server anyway,
3450 * let's be prepared for a page of acl data. */
3451 localpage = alloc_page(GFP_KERNEL);
3452 resp_buf = page_address(localpage);
3453 if (localpage == NULL)
3454 return -ENOMEM;
3455 args.acl_pages[0] = localpage;
3456 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003457 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003458 } else {
3459 resp_buf = buf;
3460 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3461 }
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003462 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003463 if (ret)
3464 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003465 if (res.acl_len > args.acl_len)
3466 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003467 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003468 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003469 if (buf) {
3470 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003471 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003472 goto out_free;
3473 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003474 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003475 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003476 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003477out_free:
3478 if (localpage)
3479 __free_page(localpage);
3480 return ret;
3481}
3482
Trond Myklebust16b42892006-08-24 12:27:15 -04003483static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3484{
3485 struct nfs4_exception exception = { };
3486 ssize_t ret;
3487 do {
3488 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3489 if (ret >= 0)
3490 break;
3491 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3492 } while (exception.retry);
3493 return ret;
3494}
3495
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003496static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3497{
3498 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003499 int ret;
3500
3501 if (!nfs4_server_supports_acls(server))
3502 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003503 ret = nfs_revalidate_inode(server, inode);
3504 if (ret < 0)
3505 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003506 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3507 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003508 ret = nfs4_read_cached_acl(inode, buf, buflen);
3509 if (ret != -ENOENT)
3510 return ret;
3511 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003512}
3513
Trond Myklebust16b42892006-08-24 12:27:15 -04003514static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003515{
3516 struct nfs_server *server = NFS_SERVER(inode);
3517 struct page *pages[NFS4ACL_MAXPAGES];
3518 struct nfs_setaclargs arg = {
3519 .fh = NFS_FH(inode),
3520 .acl_pages = pages,
3521 .acl_len = buflen,
3522 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003523 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003524 struct rpc_message msg = {
3525 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3526 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003527 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003528 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003529 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003530
3531 if (!nfs4_server_supports_acls(server))
3532 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003533 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3534 if (i < 0)
3535 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003536 nfs_inode_return_delegation(inode);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003537 ret = nfs4_call_sync(server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003538
3539 /*
3540 * Free each page after tx, so the only ref left is
3541 * held by the network stack
3542 */
3543 for (; i > 0; i--)
3544 put_page(pages[i-1]);
3545
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003546 /*
3547 * Acl update can result in inode attribute update.
3548 * so mark the attribute cache invalid.
3549 */
3550 spin_lock(&inode->i_lock);
3551 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3552 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003553 nfs_access_zap_cache(inode);
3554 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003555 return ret;
3556}
3557
Trond Myklebust16b42892006-08-24 12:27:15 -04003558static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3559{
3560 struct nfs4_exception exception = { };
3561 int err;
3562 do {
3563 err = nfs4_handle_exception(NFS_SERVER(inode),
3564 __nfs4_proc_set_acl(inode, buf, buflen),
3565 &exception);
3566 } while (exception.retry);
3567 return err;
3568}
3569
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003571nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003573 struct nfs_client *clp = server->nfs_client;
3574
3575 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 return 0;
3577 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003578 case -NFS4ERR_ADMIN_REVOKED:
3579 case -NFS4ERR_BAD_STATEID:
3580 case -NFS4ERR_OPENMODE:
3581 if (state == NULL)
3582 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003583 nfs4_schedule_stateid_recovery(server, state);
3584 goto wait_on_recovery;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003586 case -NFS4ERR_STALE_CLIENTID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 case -NFS4ERR_EXPIRED:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003588 nfs4_schedule_lease_recovery(clp);
3589 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003590#if defined(CONFIG_NFS_V4_1)
3591 case -NFS4ERR_BADSESSION:
3592 case -NFS4ERR_BADSLOT:
3593 case -NFS4ERR_BAD_HIGH_SLOT:
3594 case -NFS4ERR_DEADSESSION:
3595 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3596 case -NFS4ERR_SEQ_FALSE_RETRY:
3597 case -NFS4ERR_SEQ_MISORDERED:
3598 dprintk("%s ERROR %d, Reset session\n", __func__,
3599 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003600 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -04003601 task->tk_status = 0;
3602 return -EAGAIN;
3603#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003605 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003606 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003607 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3609 task->tk_status = 0;
3610 return -EAGAIN;
3611 case -NFS4ERR_OLD_STATEID:
3612 task->tk_status = 0;
3613 return -EAGAIN;
3614 }
3615 task->tk_status = nfs4_map_errors(task->tk_status);
3616 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003617wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003618 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003619 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3620 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3621 task->tk_status = 0;
3622 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623}
3624
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003625int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3626 unsigned short port, struct rpc_cred *cred,
3627 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628{
3629 nfs4_verifier sc_verifier;
3630 struct nfs4_setclientid setclientid = {
3631 .sc_verifier = &sc_verifier,
3632 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003633 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 };
3635 struct rpc_message msg = {
3636 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3637 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003638 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003639 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 };
Al Virobc4785c2006-10-19 23:28:51 -07003641 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 int loop = 0;
3643 int status;
3644
Al Virobc4785c2006-10-19 23:28:51 -07003645 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3647 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3648
3649 for(;;) {
3650 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003651 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003652 clp->cl_ipaddr,
3653 rpc_peeraddr2str(clp->cl_rpcclient,
3654 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003655 rpc_peeraddr2str(clp->cl_rpcclient,
3656 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003657 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 clp->cl_id_uniquifier);
3659 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003660 sizeof(setclientid.sc_netid),
3661 rpc_peeraddr2str(clp->cl_rpcclient,
3662 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003664 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 clp->cl_ipaddr, port >> 8, port & 255);
3666
3667 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3668 if (status != -NFS4ERR_CLID_INUSE)
3669 break;
3670 if (signalled())
3671 break;
3672 if (loop++ & 1)
J. Bruce Fields611c96c2010-12-13 19:05:46 -05003673 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 else
3675 if (++clp->cl_id_uniquifier == 0)
3676 break;
3677 }
3678 return status;
3679}
3680
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003681static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3682 struct nfs4_setclientid_res *arg,
3683 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684{
3685 struct nfs_fsinfo fsinfo;
3686 struct rpc_message msg = {
3687 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003688 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003690 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 };
3692 unsigned long now;
3693 int status;
3694
3695 now = jiffies;
3696 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3697 if (status == 0) {
3698 spin_lock(&clp->cl_lock);
3699 clp->cl_lease_time = fsinfo.lease_time * HZ;
3700 clp->cl_last_renewal = now;
3701 spin_unlock(&clp->cl_lock);
3702 }
3703 return status;
3704}
3705
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003706int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3707 struct nfs4_setclientid_res *arg,
3708 struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003709{
Benny Halevy77e03672008-06-24 20:25:57 +03003710 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003711 int err;
3712 do {
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003713 err = _nfs4_proc_setclientid_confirm(clp, arg, cred);
Trond Myklebust51581f32006-03-20 13:44:47 -05003714 switch (err) {
3715 case 0:
3716 return err;
3717 case -NFS4ERR_RESOURCE:
3718 /* The IBM lawyers misread another document! */
3719 case -NFS4ERR_DELAY:
3720 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3721 }
3722 } while (err == 0);
3723 return err;
3724}
3725
Trond Myklebustfe650402006-01-03 09:55:18 +01003726struct nfs4_delegreturndata {
3727 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003728 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003729 struct nfs_fh fh;
3730 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003731 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003732 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003733 int rpc_status;
3734};
3735
Trond Myklebustfe650402006-01-03 09:55:18 +01003736static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3737{
3738 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003739
Trond Myklebust14516c32010-07-31 14:29:06 -04003740 if (!nfs4_sequence_done(task, &data->res.seq_res))
3741 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003742
Ricardo Labiaga79708862009-12-07 09:23:21 -05003743 switch (task->tk_status) {
3744 case -NFS4ERR_STALE_STATEID:
3745 case -NFS4ERR_EXPIRED:
3746 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003747 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003748 break;
3749 default:
3750 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3751 -EAGAIN) {
3752 nfs_restart_rpc(task, data->res.server->nfs_client);
3753 return;
3754 }
3755 }
3756 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003757}
3758
3759static void nfs4_delegreturn_release(void *calldata)
3760{
Trond Myklebustfe650402006-01-03 09:55:18 +01003761 kfree(calldata);
3762}
3763
Andy Adamson938e1012009-04-01 09:22:28 -04003764#if defined(CONFIG_NFS_V4_1)
3765static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3766{
3767 struct nfs4_delegreturndata *d_data;
3768
3769 d_data = (struct nfs4_delegreturndata *)data;
3770
Trond Myklebust035168a2010-06-16 09:52:26 -04003771 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003772 &d_data->args.seq_args,
3773 &d_data->res.seq_res, 1, task))
3774 return;
3775 rpc_call_start(task);
3776}
3777#endif /* CONFIG_NFS_V4_1 */
3778
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003779static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003780#if defined(CONFIG_NFS_V4_1)
3781 .rpc_call_prepare = nfs4_delegreturn_prepare,
3782#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003783 .rpc_call_done = nfs4_delegreturn_done,
3784 .rpc_release = nfs4_delegreturn_release,
3785};
3786
Trond Myklebuste6f81072008-01-24 18:14:34 -05003787static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003788{
3789 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003790 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003791 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003792 struct rpc_message msg = {
3793 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3794 .rpc_cred = cred,
3795 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003796 struct rpc_task_setup task_setup_data = {
3797 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003798 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003799 .callback_ops = &nfs4_delegreturn_ops,
3800 .flags = RPC_TASK_ASYNC,
3801 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003802 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003803
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003804 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003805 if (data == NULL)
3806 return -ENOMEM;
3807 data->args.fhandle = &data->fh;
3808 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003809 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003810 nfs_copy_fh(&data->fh, NFS_FH(inode));
3811 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003812 data->res.fattr = &data->fattr;
3813 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003814 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003815 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003816 data->rpc_status = 0;
3817
Trond Myklebustc970aa82007-07-14 15:39:59 -04003818 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05003819 msg.rpc_argp = &data->args;
3820 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003821 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003822 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003823 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003824 if (!issync)
3825 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003826 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003827 if (status != 0)
3828 goto out;
3829 status = data->rpc_status;
3830 if (status != 0)
3831 goto out;
3832 nfs_refresh_inode(inode, &data->fattr);
3833out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003834 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003835 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836}
3837
Trond Myklebuste6f81072008-01-24 18:14:34 -05003838int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839{
3840 struct nfs_server *server = NFS_SERVER(inode);
3841 struct nfs4_exception exception = { };
3842 int err;
3843 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003844 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 switch (err) {
3846 case -NFS4ERR_STALE_STATEID:
3847 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 case 0:
3849 return 0;
3850 }
3851 err = nfs4_handle_exception(server, err, &exception);
3852 } while (exception.retry);
3853 return err;
3854}
3855
3856#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3857#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3858
3859/*
3860 * sleep, with exponential backoff, and retry the LOCK operation.
3861 */
3862static unsigned long
3863nfs4_set_lock_task_retry(unsigned long timeout)
3864{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003865 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 timeout <<= 1;
3867 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3868 return NFS4_LOCK_MAXTIMEOUT;
3869 return timeout;
3870}
3871
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3873{
3874 struct inode *inode = state->inode;
3875 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003876 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003877 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003879 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003881 struct nfs_lockt_res res = {
3882 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 };
3884 struct rpc_message msg = {
3885 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3886 .rpc_argp = &arg,
3887 .rpc_resp = &res,
3888 .rpc_cred = state->owner->so_cred,
3889 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 struct nfs4_lock_state *lsp;
3891 int status;
3892
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003893 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003894 status = nfs4_set_lock_state(state, request);
3895 if (status != 0)
3896 goto out;
3897 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003898 arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05003899 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumakere73b83f2011-03-24 17:12:23 +00003900 status = nfs4_call_sync(server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003901 switch (status) {
3902 case 0:
3903 request->fl_type = F_UNLCK;
3904 break;
3905 case -NFS4ERR_DENIED:
3906 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003908 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003909out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 return status;
3911}
3912
3913static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3914{
3915 struct nfs4_exception exception = { };
3916 int err;
3917
3918 do {
3919 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3920 _nfs4_proc_getlk(state, cmd, request),
3921 &exception);
3922 } while (exception.retry);
3923 return err;
3924}
3925
3926static int do_vfs_lock(struct file *file, struct file_lock *fl)
3927{
3928 int res = 0;
3929 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3930 case FL_POSIX:
3931 res = posix_lock_file_wait(file, fl);
3932 break;
3933 case FL_FLOCK:
3934 res = flock_lock_file_wait(file, fl);
3935 break;
3936 default:
3937 BUG();
3938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 return res;
3940}
3941
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003942struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003943 struct nfs_locku_args arg;
3944 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003945 struct nfs4_lock_state *lsp;
3946 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003947 struct file_lock fl;
3948 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003949 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003950};
3951
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003952static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3953 struct nfs_open_context *ctx,
3954 struct nfs4_lock_state *lsp,
3955 struct nfs_seqid *seqid)
3956{
3957 struct nfs4_unlockdata *p;
3958 struct inode *inode = lsp->ls_state->inode;
3959
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003960 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003961 if (p == NULL)
3962 return NULL;
3963 p->arg.fh = NFS_FH(inode);
3964 p->arg.fl = &p->fl;
3965 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003966 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003967 p->arg.stateid = &lsp->ls_stateid;
3968 p->lsp = lsp;
3969 atomic_inc(&lsp->ls_count);
3970 /* Ensure we don't close file until we're done freeing locks! */
3971 p->ctx = get_nfs_open_context(ctx);
3972 memcpy(&p->fl, fl, sizeof(p->fl));
3973 p->server = NFS_SERVER(inode);
3974 return p;
3975}
3976
Trond Myklebust06f814a2006-01-03 09:55:07 +01003977static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003978{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003979 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003980 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003981 nfs4_put_lock_state(calldata->lsp);
3982 put_nfs_open_context(calldata->ctx);
3983 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003984}
3985
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003986static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003987{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003988 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003989
Trond Myklebust14516c32010-07-31 14:29:06 -04003990 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3991 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003992 switch (task->tk_status) {
3993 case 0:
3994 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003995 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003996 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003997 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003998 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003999 case -NFS4ERR_BAD_STATEID:
4000 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004001 case -NFS4ERR_STALE_STATEID:
4002 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004003 break;
4004 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004005 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebust0110ee12009-12-07 09:00:24 -05004006 nfs_restart_rpc(task,
Trond Myklebustd61e6122009-12-05 19:32:19 -05004007 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004008 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004009}
4010
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004011static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004012{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004013 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004015 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004016 return;
4017 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004018 /* Note: exit _without_ running nfs4_locku_done */
4019 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004020 return;
4021 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004022 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004023 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004024 &calldata->arg.seq_args,
4025 &calldata->res.seq_res, 1, task))
4026 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004027 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028}
4029
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004030static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004031 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004032 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004033 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004034};
4035
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004036static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4037 struct nfs_open_context *ctx,
4038 struct nfs4_lock_state *lsp,
4039 struct nfs_seqid *seqid)
4040{
4041 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004042 struct rpc_message msg = {
4043 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4044 .rpc_cred = ctx->cred,
4045 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004046 struct rpc_task_setup task_setup_data = {
4047 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004048 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004049 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004050 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004051 .flags = RPC_TASK_ASYNC,
4052 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004053
Frank Filz137d6ac2007-07-09 15:32:29 -07004054 /* Ensure this is an unlock - when canceling a lock, the
4055 * canceled lock is passed in, and it won't be an unlock.
4056 */
4057 fl->fl_type = F_UNLCK;
4058
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004059 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4060 if (data == NULL) {
4061 nfs_free_seqid(seqid);
4062 return ERR_PTR(-ENOMEM);
4063 }
4064
Trond Myklebust1174dd12010-12-21 10:52:24 -05004065 msg.rpc_argp = &data->arg;
4066 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004067 task_setup_data.callback_data = data;
4068 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004069}
4070
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4072{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004073 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004074 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004075 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004076 struct rpc_task *task;
4077 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004078 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079
Trond Myklebust9b073572006-06-29 16:38:34 -04004080 status = nfs4_set_lock_state(state, request);
4081 /* Unlock _before_ we do the RPC call */
4082 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004083 down_read(&nfsi->rwsem);
4084 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4085 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004086 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004087 }
4088 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004089 if (status != 0)
4090 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004091 /* Is this a delegated lock? */
4092 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004093 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004094 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004095 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004096 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004097 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004098 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004099 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004100 status = PTR_ERR(task);
4101 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004102 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004103 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004104 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004105out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004106 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004107 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108}
4109
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004110struct nfs4_lockdata {
4111 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004112 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004113 struct nfs4_lock_state *lsp;
4114 struct nfs_open_context *ctx;
4115 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004116 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004117 int rpc_status;
4118 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004119 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004120};
4121
4122static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004123 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4124 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004125{
4126 struct nfs4_lockdata *p;
4127 struct inode *inode = lsp->ls_state->inode;
4128 struct nfs_server *server = NFS_SERVER(inode);
4129
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004130 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004131 if (p == NULL)
4132 return NULL;
4133
4134 p->arg.fh = NFS_FH(inode);
4135 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004136 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004137 if (p->arg.open_seqid == NULL)
4138 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004139 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004140 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004141 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004142 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004143 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004144 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004145 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004146 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004147 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004148 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004149 atomic_inc(&lsp->ls_count);
4150 p->ctx = get_nfs_open_context(ctx);
4151 memcpy(&p->fl, fl, sizeof(p->fl));
4152 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004153out_free_seqid:
4154 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004155out_free:
4156 kfree(p);
4157 return NULL;
4158}
4159
4160static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4161{
4162 struct nfs4_lockdata *data = calldata;
4163 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164
Harvey Harrison3110ff82008-05-02 13:42:44 -07004165 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004166 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4167 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004168 /* Do we need to do an open_to_lock_owner? */
4169 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004170 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4171 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004172 data->arg.open_stateid = &state->stateid;
4173 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004174 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004175 } else
4176 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004177 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004178 if (nfs4_setup_sequence(data->server,
4179 &data->arg.seq_args,
Andy Adamson66179ef2009-04-01 09:22:22 -04004180 &data->res.seq_res, 1, task))
4181 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004182 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004183 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004184}
4185
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004186static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4187{
4188 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4189 nfs4_lock_prepare(task, calldata);
4190}
4191
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004192static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4193{
4194 struct nfs4_lockdata *data = calldata;
4195
Harvey Harrison3110ff82008-05-02 13:42:44 -07004196 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004197
Trond Myklebust14516c32010-07-31 14:29:06 -04004198 if (!nfs4_sequence_done(task, &data->res.seq_res))
4199 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004200
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004201 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004202 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004203 if (data->rpc_status == 0)
4204 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4205 else
4206 goto out;
4207 }
4208 if (data->rpc_status == 0) {
4209 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4210 sizeof(data->lsp->ls_stateid.data));
4211 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04004212 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004213 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004214out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004215 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004216}
4217
4218static void nfs4_lock_release(void *calldata)
4219{
4220 struct nfs4_lockdata *data = calldata;
4221
Harvey Harrison3110ff82008-05-02 13:42:44 -07004222 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004223 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004224 if (data->cancelled != 0) {
4225 struct rpc_task *task;
4226 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4227 data->arg.lock_seqid);
4228 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004229 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004230 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004231 } else
4232 nfs_free_seqid(data->arg.lock_seqid);
4233 nfs4_put_lock_state(data->lsp);
4234 put_nfs_open_context(data->ctx);
4235 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004236 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004237}
4238
4239static const struct rpc_call_ops nfs4_lock_ops = {
4240 .rpc_call_prepare = nfs4_lock_prepare,
4241 .rpc_call_done = nfs4_lock_done,
4242 .rpc_release = nfs4_lock_release,
4243};
4244
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004245static const struct rpc_call_ops nfs4_recover_lock_ops = {
4246 .rpc_call_prepare = nfs4_recover_lock_prepare,
4247 .rpc_call_done = nfs4_lock_done,
4248 .rpc_release = nfs4_lock_release,
4249};
4250
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004251static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4252{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004253 switch (error) {
4254 case -NFS4ERR_ADMIN_REVOKED:
4255 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004256 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004257 if (new_lock_owner != 0 ||
4258 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004259 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004260 break;
4261 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004262 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004263 case -NFS4ERR_EXPIRED:
4264 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004265 };
4266}
4267
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004268static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004269{
4270 struct nfs4_lockdata *data;
4271 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004272 struct rpc_message msg = {
4273 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4274 .rpc_cred = state->owner->so_cred,
4275 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004276 struct rpc_task_setup task_setup_data = {
4277 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004278 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004279 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004280 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004281 .flags = RPC_TASK_ASYNC,
4282 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004283 int ret;
4284
Harvey Harrison3110ff82008-05-02 13:42:44 -07004285 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004286 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004287 fl->fl_u.nfs4_fl.owner,
4288 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004289 if (data == NULL)
4290 return -ENOMEM;
4291 if (IS_SETLKW(cmd))
4292 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004293 if (recovery_type > NFS_LOCK_NEW) {
4294 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004295 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004296 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4297 }
Trond Myklebust1174dd12010-12-21 10:52:24 -05004298 msg.rpc_argp = &data->arg;
4299 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004300 task_setup_data.callback_data = data;
4301 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004302 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004303 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004304 ret = nfs4_wait_for_completion_rpc_task(task);
4305 if (ret == 0) {
4306 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004307 if (ret)
4308 nfs4_handle_setlk_error(data->server, data->lsp,
4309 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004310 } else
4311 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004312 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004313 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004314 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315}
4316
4317static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4318{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004319 struct nfs_server *server = NFS_SERVER(state->inode);
4320 struct nfs4_exception exception = { };
4321 int err;
4322
4323 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004324 /* Cache the lock if possible... */
4325 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4326 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004327 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004328 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004329 break;
4330 nfs4_handle_exception(server, err, &exception);
4331 } while (exception.retry);
4332 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333}
4334
4335static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4336{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004337 struct nfs_server *server = NFS_SERVER(state->inode);
4338 struct nfs4_exception exception = { };
4339 int err;
4340
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004341 err = nfs4_set_lock_state(state, request);
4342 if (err != 0)
4343 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004344 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004345 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4346 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004347 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004348 switch (err) {
4349 default:
4350 goto out;
4351 case -NFS4ERR_GRACE:
4352 case -NFS4ERR_DELAY:
4353 nfs4_handle_exception(server, err, &exception);
4354 err = 0;
4355 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004356 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004357out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004358 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359}
4360
4361static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4362{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004363 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004364 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004365 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004367 if ((fl_flags & FL_POSIX) &&
4368 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4369 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004370 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004371 status = nfs4_set_lock_state(state, request);
4372 if (status != 0)
4373 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004374 request->fl_flags |= FL_ACCESS;
4375 status = do_vfs_lock(request->fl_file, request);
4376 if (status < 0)
4377 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004378 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004379 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004380 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004381 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004382 request->fl_flags = fl_flags & ~FL_SLEEP;
4383 status = do_vfs_lock(request->fl_file, request);
4384 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004385 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004386 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004387 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004388 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004389 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004390 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004391 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004392 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004393out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004394 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004395out:
4396 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 return status;
4398}
4399
4400static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4401{
4402 struct nfs4_exception exception = { };
4403 int err;
4404
4405 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004406 err = _nfs4_proc_setlk(state, cmd, request);
4407 if (err == -NFS4ERR_DENIED)
4408 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004410 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 } while (exception.retry);
4412 return err;
4413}
4414
4415static int
4416nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4417{
4418 struct nfs_open_context *ctx;
4419 struct nfs4_state *state;
4420 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4421 int status;
4422
4423 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004424 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 state = ctx->state;
4426
4427 if (request->fl_start < 0 || request->fl_end < 0)
4428 return -EINVAL;
4429
Trond Myklebustd9531262009-07-21 19:22:38 -04004430 if (IS_GETLK(cmd)) {
4431 if (state != NULL)
4432 return nfs4_proc_getlk(state, F_GETLK, request);
4433 return 0;
4434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435
4436 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4437 return -EINVAL;
4438
Trond Myklebustd9531262009-07-21 19:22:38 -04004439 if (request->fl_type == F_UNLCK) {
4440 if (state != NULL)
4441 return nfs4_proc_unlck(state, cmd, request);
4442 return 0;
4443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444
Trond Myklebustd9531262009-07-21 19:22:38 -04004445 if (state == NULL)
4446 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 do {
4448 status = nfs4_proc_setlk(state, cmd, request);
4449 if ((status != -EAGAIN) || IS_SETLK(cmd))
4450 break;
4451 timeout = nfs4_set_lock_task_retry(timeout);
4452 status = -ERESTARTSYS;
4453 if (signalled())
4454 break;
4455 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 return status;
4457}
4458
Trond Myklebust888e6942005-11-04 15:38:11 -05004459int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4460{
4461 struct nfs_server *server = NFS_SERVER(state->inode);
4462 struct nfs4_exception exception = { };
4463 int err;
4464
4465 err = nfs4_set_lock_state(state, fl);
4466 if (err != 0)
4467 goto out;
4468 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004469 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004470 switch (err) {
4471 default:
4472 printk(KERN_ERR "%s: unhandled error %d.\n",
4473 __func__, err);
4474 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004475 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004476 goto out;
4477 case -NFS4ERR_EXPIRED:
4478 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004479 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004480 nfs4_schedule_lease_recovery(server->nfs_client);
4481 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004482 case -NFS4ERR_BADSESSION:
4483 case -NFS4ERR_BADSLOT:
4484 case -NFS4ERR_BAD_HIGH_SLOT:
4485 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4486 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004487 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004488 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004489 case -ERESTARTSYS:
4490 /*
4491 * The show must go on: exit, but mark the
4492 * stateid as needing recovery.
4493 */
4494 case -NFS4ERR_ADMIN_REVOKED:
4495 case -NFS4ERR_BAD_STATEID:
4496 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004497 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004498 err = 0;
4499 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004500 case -EKEYEXPIRED:
4501 /*
4502 * User RPCSEC_GSS context has expired.
4503 * We cannot recover this stateid now, so
4504 * skip it and allow recovery thread to
4505 * proceed.
4506 */
4507 err = 0;
4508 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004509 case -ENOMEM:
4510 case -NFS4ERR_DENIED:
4511 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4512 err = 0;
4513 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004514 case -NFS4ERR_DELAY:
4515 break;
4516 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004517 err = nfs4_handle_exception(server, err, &exception);
4518 } while (exception.retry);
4519out:
4520 return err;
4521}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004522
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004523static void nfs4_release_lockowner_release(void *calldata)
4524{
4525 kfree(calldata);
4526}
4527
4528const struct rpc_call_ops nfs4_release_lockowner_ops = {
4529 .rpc_release = nfs4_release_lockowner_release,
4530};
4531
4532void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4533{
4534 struct nfs_server *server = lsp->ls_state->owner->so_server;
4535 struct nfs_release_lockowner_args *args;
4536 struct rpc_message msg = {
4537 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4538 };
4539
4540 if (server->nfs_client->cl_mvops->minor_version != 0)
4541 return;
4542 args = kmalloc(sizeof(*args), GFP_NOFS);
4543 if (!args)
4544 return;
4545 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4546 args->lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004547 args->lock_owner.s_dev = server->s_dev;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004548 msg.rpc_argp = args;
4549 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4550}
4551
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004552#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4553
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004554static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4555 const void *buf, size_t buflen,
4556 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004557{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004558 if (strcmp(key, "") != 0)
4559 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004560
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004561 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004562}
4563
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004564static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4565 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004566{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004567 if (strcmp(key, "") != 0)
4568 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004569
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004570 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004571}
4572
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004573static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4574 size_t list_len, const char *name,
4575 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004576{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004577 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004578
J. Bruce Fields096455a2006-03-20 23:23:42 -05004579 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4580 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004581
4582 if (list && len <= list_len)
4583 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004584 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004585}
4586
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004587static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4588{
4589 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4590 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4591 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4592 return;
4593
4594 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4595 NFS_ATTR_FATTR_NLINK;
4596 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4597 fattr->nlink = 2;
4598}
4599
Trond Myklebust56659e92007-07-17 21:52:39 -04004600int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004601 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004602{
4603 struct nfs_server *server = NFS_SERVER(dir);
4604 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004605 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4606 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004607 };
4608 struct nfs4_fs_locations_arg args = {
4609 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004610 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004611 .page = page,
4612 .bitmask = bitmask,
4613 };
Benny Halevy22958462009-04-01 09:22:02 -04004614 struct nfs4_fs_locations_res res = {
4615 .fs_locations = fs_locations,
4616 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004617 struct rpc_message msg = {
4618 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4619 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004620 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004621 };
4622 int status;
4623
Harvey Harrison3110ff82008-05-02 13:42:44 -07004624 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004625 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004626 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004627 fs_locations->nlocations = 0;
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004628 status = nfs4_call_sync(server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004629 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004630 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004631 return status;
4632}
4633
Andy Adamson557134a2009-04-01 09:21:53 -04004634#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004635/*
Andy Adamson357f54d2010-12-14 10:11:57 -05004636 * Check the exchange flags returned by the server for invalid flags, having
4637 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4638 * DS flags set.
4639 */
4640static int nfs4_check_cl_exchange_flags(u32 flags)
4641{
4642 if (flags & ~EXCHGID4_FLAG_MASK_R)
4643 goto out_inval;
4644 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4645 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4646 goto out_inval;
4647 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4648 goto out_inval;
4649 return NFS_OK;
4650out_inval:
4651 return -NFS4ERR_INVAL;
4652}
4653
4654/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04004655 * nfs4_proc_exchange_id()
4656 *
4657 * Since the clientid has expired, all compounds using sessions
4658 * associated with the stale clientid will be returning
4659 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4660 * be in some phase of session reset.
4661 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004662int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004663{
4664 nfs4_verifier verifier;
4665 struct nfs41_exchange_id_args args = {
4666 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05004667 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04004668 };
4669 struct nfs41_exchange_id_res res = {
4670 .client = clp,
4671 };
4672 int status;
4673 struct rpc_message msg = {
4674 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4675 .rpc_argp = &args,
4676 .rpc_resp = &res,
4677 .rpc_cred = cred,
4678 };
4679 __be32 *p;
4680
4681 dprintk("--> %s\n", __func__);
4682 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004683
Benny Halevy99fe60d2009-04-01 09:22:29 -04004684 p = (u32 *)verifier.data;
4685 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4686 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4687 args.verifier = &verifier;
4688
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004689 args.id_len = scnprintf(args.id, sizeof(args.id),
4690 "%s/%s.%s/%u",
4691 clp->cl_ipaddr,
4692 init_utsname()->nodename,
4693 init_utsname()->domainname,
4694 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004695
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004696 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4697 if (!status)
4698 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004699 dprintk("<-- %s status= %d\n", __func__, status);
4700 return status;
4701}
4702
Andy Adamson2050f0c2009-04-01 09:22:30 -04004703struct nfs4_get_lease_time_data {
4704 struct nfs4_get_lease_time_args *args;
4705 struct nfs4_get_lease_time_res *res;
4706 struct nfs_client *clp;
4707};
4708
4709static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4710 void *calldata)
4711{
4712 int ret;
4713 struct nfs4_get_lease_time_data *data =
4714 (struct nfs4_get_lease_time_data *)calldata;
4715
4716 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004717 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004718 /* just setup sequence, do not trigger session recovery
4719 since we're invoked within one */
4720 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004721 &data->args->la_seq_args,
4722 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004723
4724 BUG_ON(ret == -EAGAIN);
4725 rpc_call_start(task);
4726 dprintk("<-- %s\n", __func__);
4727}
4728
4729/*
4730 * Called from nfs4_state_manager thread for session setup, so don't recover
4731 * from sequence operation or clientid errors.
4732 */
4733static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4734{
4735 struct nfs4_get_lease_time_data *data =
4736 (struct nfs4_get_lease_time_data *)calldata;
4737
4738 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04004739 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4740 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04004741 switch (task->tk_status) {
4742 case -NFS4ERR_DELAY:
4743 case -NFS4ERR_GRACE:
4744 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4745 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4746 task->tk_status = 0;
Trond Myklebust0110ee12009-12-07 09:00:24 -05004747 nfs_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004748 return;
4749 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004750 dprintk("<-- %s\n", __func__);
4751}
4752
4753struct rpc_call_ops nfs4_get_lease_time_ops = {
4754 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4755 .rpc_call_done = nfs4_get_lease_time_done,
4756};
4757
4758int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4759{
4760 struct rpc_task *task;
4761 struct nfs4_get_lease_time_args args;
4762 struct nfs4_get_lease_time_res res = {
4763 .lr_fsinfo = fsinfo,
4764 };
4765 struct nfs4_get_lease_time_data data = {
4766 .args = &args,
4767 .res = &res,
4768 .clp = clp,
4769 };
4770 struct rpc_message msg = {
4771 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4772 .rpc_argp = &args,
4773 .rpc_resp = &res,
4774 };
4775 struct rpc_task_setup task_setup = {
4776 .rpc_client = clp->cl_rpcclient,
4777 .rpc_message = &msg,
4778 .callback_ops = &nfs4_get_lease_time_ops,
4779 .callback_data = &data
4780 };
4781 int status;
4782
Andy Adamson2050f0c2009-04-01 09:22:30 -04004783 dprintk("--> %s\n", __func__);
4784 task = rpc_run_task(&task_setup);
4785
4786 if (IS_ERR(task))
4787 status = PTR_ERR(task);
4788 else {
4789 status = task->tk_status;
4790 rpc_put_task(task);
4791 }
4792 dprintk("<-- %s return %d\n", __func__, status);
4793
4794 return status;
4795}
4796
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004797/*
4798 * Reset a slot table
4799 */
Andy Adamson104aeba2010-01-14 17:45:10 -05004800static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4801 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004802{
Andy Adamson104aeba2010-01-14 17:45:10 -05004803 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004804 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004805 int ret = 0;
4806
Andy Adamson104aeba2010-01-14 17:45:10 -05004807 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4808 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004809
Andy Adamson104aeba2010-01-14 17:45:10 -05004810 /* Does the newly negotiated max_reqs match the existing slot table? */
4811 if (max_reqs != tbl->max_slots) {
4812 ret = -ENOMEM;
4813 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004814 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05004815 if (!new)
4816 goto out;
4817 ret = 0;
4818 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004819 }
4820 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05004821 if (new) {
4822 tbl->slots = new;
4823 tbl->max_slots = max_reqs;
4824 }
4825 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004826 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004827 spin_unlock(&tbl->slot_tbl_lock);
4828 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4829 tbl, tbl->slots, tbl->max_slots);
4830out:
4831 dprintk("<-- %s: return %d\n", __func__, ret);
4832 return ret;
4833}
4834
Andy Adamsonfc931582009-04-01 09:22:31 -04004835/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004836 * Reset the forechannel and backchannel slot tables
4837 */
4838static int nfs4_reset_slot_tables(struct nfs4_session *session)
4839{
4840 int status;
4841
4842 status = nfs4_reset_slot_table(&session->fc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004843 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004844 if (status)
4845 return status;
4846
4847 status = nfs4_reset_slot_table(&session->bc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004848 session->bc_attrs.max_reqs, 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004849 return status;
4850}
4851
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004852/* Destroy the slot table */
4853static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4854{
4855 if (session->fc_slot_table.slots != NULL) {
4856 kfree(session->fc_slot_table.slots);
4857 session->fc_slot_table.slots = NULL;
4858 }
4859 if (session->bc_slot_table.slots != NULL) {
4860 kfree(session->bc_slot_table.slots);
4861 session->bc_slot_table.slots = NULL;
4862 }
4863 return;
4864}
4865
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004866/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004867 * Initialize slot table
4868 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004869static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4870 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04004871{
Andy Adamsonfc931582009-04-01 09:22:31 -04004872 struct nfs4_slot *slot;
4873 int ret = -ENOMEM;
4874
4875 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4876
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004877 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04004878
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004879 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04004880 if (!slot)
4881 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04004882 ret = 0;
4883
4884 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04004885 tbl->max_slots = max_slots;
4886 tbl->slots = slot;
4887 tbl->highest_used_slotid = -1; /* no slot is currently used */
4888 spin_unlock(&tbl->slot_tbl_lock);
4889 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4890 tbl, tbl->slots, tbl->max_slots);
4891out:
4892 dprintk("<-- %s: return %d\n", __func__, ret);
4893 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04004894}
4895
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004896/*
4897 * Initialize the forechannel and backchannel tables
4898 */
4899static int nfs4_init_slot_tables(struct nfs4_session *session)
4900{
Trond Myklebustf26468f2009-12-05 19:32:11 -05004901 struct nfs4_slot_table *tbl;
4902 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004903
Trond Myklebustf26468f2009-12-05 19:32:11 -05004904 tbl = &session->fc_slot_table;
4905 if (tbl->slots == NULL) {
4906 status = nfs4_init_slot_table(tbl,
4907 session->fc_attrs.max_reqs, 1);
4908 if (status)
4909 return status;
4910 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004911
Trond Myklebustf26468f2009-12-05 19:32:11 -05004912 tbl = &session->bc_slot_table;
4913 if (tbl->slots == NULL) {
4914 status = nfs4_init_slot_table(tbl,
4915 session->bc_attrs.max_reqs, 0);
4916 if (status)
4917 nfs4_destroy_slot_tables(session);
4918 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004919
4920 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04004921}
4922
4923struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4924{
4925 struct nfs4_session *session;
4926 struct nfs4_slot_table *tbl;
4927
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004928 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04004929 if (!session)
4930 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04004931
Andy Adamson557134a2009-04-01 09:21:53 -04004932 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004933 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04004934 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004935 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00004936 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004937
4938 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004939 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004940 spin_lock_init(&tbl->slot_tbl_lock);
4941 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00004942 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004943
Trond Myklebust1055d762010-06-16 09:52:27 -04004944 session->session_state = 1<<NFS4_SESSION_INITING;
4945
Andy Adamson557134a2009-04-01 09:21:53 -04004946 session->clp = clp;
4947 return session;
4948}
4949
4950void nfs4_destroy_session(struct nfs4_session *session)
4951{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04004952 nfs4_proc_destroy_session(session);
4953 dprintk("%s Destroy backchannel for xprt %p\n",
4954 __func__, session->clp->cl_rpcclient->cl_xprt);
4955 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4956 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004957 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04004958 kfree(session);
4959}
4960
Andy Adamsonfc931582009-04-01 09:22:31 -04004961/*
4962 * Initialize the values to be used by the client in CREATE_SESSION
4963 * If nfs4_init_session set the fore channel request and response sizes,
4964 * use them.
4965 *
4966 * Set the back channel max_resp_sz_cached to zero to force the client to
4967 * always set csa_cachethis to FALSE because the current implementation
4968 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4969 */
4970static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4971{
4972 struct nfs4_session *session = args->client->cl_session;
4973 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4974 mxresp_sz = session->fc_attrs.max_resp_sz;
4975
4976 if (mxrqst_sz == 0)
4977 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4978 if (mxresp_sz == 0)
4979 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4980 /* Fore channel attributes */
4981 args->fc_attrs.headerpadsz = 0;
4982 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4983 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04004984 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4985 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4986
4987 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05004988 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04004989 __func__,
4990 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05004991 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04004992
4993 /* Back channel attributes */
4994 args->bc_attrs.headerpadsz = 0;
4995 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4996 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4997 args->bc_attrs.max_resp_sz_cached = 0;
4998 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4999 args->bc_attrs.max_reqs = 1;
5000
5001 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5002 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5003 __func__,
5004 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5005 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5006 args->bc_attrs.max_reqs);
5007}
5008
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005009static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005010{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005011 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5012 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5013
5014 if (rcvd->headerpadsz > sent->headerpadsz)
5015 return -EINVAL;
5016 if (rcvd->max_resp_sz > sent->max_resp_sz)
5017 return -EINVAL;
5018 /*
5019 * Our requested max_ops is the minimum we need; we're not
5020 * prepared to break up compounds into smaller pieces than that.
5021 * So, no point even trying to continue if the server won't
5022 * cooperate:
5023 */
5024 if (rcvd->max_ops < sent->max_ops)
5025 return -EINVAL;
5026 if (rcvd->max_reqs == 0)
5027 return -EINVAL;
5028 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005029}
5030
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005031static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5032{
5033 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5034 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005035
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005036 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5037 return -EINVAL;
5038 if (rcvd->max_resp_sz < sent->max_resp_sz)
5039 return -EINVAL;
5040 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5041 return -EINVAL;
5042 /* These would render the backchannel useless: */
5043 if (rcvd->max_ops == 0)
5044 return -EINVAL;
5045 if (rcvd->max_reqs == 0)
5046 return -EINVAL;
5047 return 0;
5048}
Andy Adamson8d353012009-04-01 09:22:32 -04005049
Andy Adamson8d353012009-04-01 09:22:32 -04005050static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5051 struct nfs4_session *session)
5052{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005053 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005054
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005055 ret = nfs4_verify_fore_channel_attrs(args, session);
5056 if (ret)
5057 return ret;
5058 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005059}
5060
Andy Adamsonfc931582009-04-01 09:22:31 -04005061static int _nfs4_proc_create_session(struct nfs_client *clp)
5062{
5063 struct nfs4_session *session = clp->cl_session;
5064 struct nfs41_create_session_args args = {
5065 .client = clp,
5066 .cb_program = NFS4_CALLBACK,
5067 };
5068 struct nfs41_create_session_res res = {
5069 .client = clp,
5070 };
5071 struct rpc_message msg = {
5072 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5073 .rpc_argp = &args,
5074 .rpc_resp = &res,
5075 };
5076 int status;
5077
5078 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005079 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005080
5081 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5082
Andy Adamson8d353012009-04-01 09:22:32 -04005083 if (!status)
5084 /* Verify the session's negotiated channel_attrs values */
5085 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005086 if (!status) {
5087 /* Increment the clientid slot sequence id */
5088 clp->cl_seqid++;
5089 }
5090
5091 return status;
5092}
5093
5094/*
5095 * Issues a CREATE_SESSION operation to the server.
5096 * It is the responsibility of the caller to verify the session is
5097 * expired before calling this routine.
5098 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005099int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005100{
5101 int status;
5102 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005103 struct nfs4_session *session = clp->cl_session;
Ricardo Labiaga7d6d63d2011-03-09 13:13:44 -05005104 long timeout = 0;
5105 int err;
Andy Adamsonfc931582009-04-01 09:22:31 -04005106
5107 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5108
Ricardo Labiaga7d6d63d2011-03-09 13:13:44 -05005109 do {
5110 status = _nfs4_proc_create_session(clp);
5111 if (status == -NFS4ERR_DELAY) {
5112 err = nfs4_delay(clp->cl_rpcclient, &timeout);
5113 if (err)
5114 status = err;
5115 }
5116 } while (status == -NFS4ERR_DELAY);
5117
Andy Adamsonfc931582009-04-01 09:22:31 -04005118 if (status)
5119 goto out;
5120
Trond Myklebustf26468f2009-12-05 19:32:11 -05005121 /* Init and reset the fore channel */
5122 status = nfs4_init_slot_tables(session);
5123 dprintk("slot table initialization returned %d\n", status);
5124 if (status)
5125 goto out;
5126 status = nfs4_reset_slot_tables(session);
5127 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005128 if (status)
5129 goto out;
5130
5131 ptr = (unsigned *)&session->sess_id.data[0];
5132 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5133 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005134out:
5135 dprintk("<-- %s\n", __func__);
5136 return status;
5137}
5138
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005139/*
5140 * Issue the over-the-wire RPC DESTROY_SESSION.
5141 * The caller must serialize access to this routine.
5142 */
5143int nfs4_proc_destroy_session(struct nfs4_session *session)
5144{
5145 int status = 0;
5146 struct rpc_message msg;
5147
5148 dprintk("--> nfs4_proc_destroy_session\n");
5149
5150 /* session is still being setup */
5151 if (session->clp->cl_cons_state != NFS_CS_READY)
5152 return status;
5153
5154 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5155 msg.rpc_argp = session;
5156 msg.rpc_resp = NULL;
5157 msg.rpc_cred = NULL;
5158 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5159
5160 if (status)
5161 printk(KERN_WARNING
5162 "Got error %d from the server on DESTROY_SESSION. "
5163 "Session has been destroyed regardless...\n", status);
5164
5165 dprintk("<-- nfs4_proc_destroy_session\n");
5166 return status;
5167}
5168
Trond Myklebustfccba802009-07-21 16:48:07 -04005169int nfs4_init_session(struct nfs_server *server)
5170{
5171 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005172 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005173 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005174 int ret;
5175
5176 if (!nfs4_has_session(clp))
5177 return 0;
5178
Trond Myklebust1055d762010-06-16 09:52:27 -04005179 session = clp->cl_session;
5180 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5181 return 0;
5182
Andy Adamson68bf05e2009-12-15 12:55:02 -05005183 rsize = server->rsize;
5184 if (rsize == 0)
5185 rsize = NFS_MAX_FILE_IO_SIZE;
5186 wsize = server->wsize;
5187 if (wsize == 0)
5188 wsize = NFS_MAX_FILE_IO_SIZE;
5189
Andy Adamson68bf05e2009-12-15 12:55:02 -05005190 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5191 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005192
Trond Myklebustfccba802009-07-21 16:48:07 -04005193 ret = nfs4_recover_expired_lease(server);
5194 if (!ret)
5195 ret = nfs4_check_client_ready(clp);
5196 return ret;
5197}
5198
Andy Adamsond83217c2011-03-01 01:34:17 +00005199int nfs4_init_ds_session(struct nfs_client *clp)
5200{
5201 struct nfs4_session *session = clp->cl_session;
5202 int ret;
5203
5204 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5205 return 0;
5206
5207 ret = nfs4_client_recover_expired_lease(clp);
5208 if (!ret)
5209 /* Test for the DS role */
5210 if (!is_ds_client(clp))
5211 ret = -ENODEV;
5212 if (!ret)
5213 ret = nfs4_check_client_ready(clp);
5214 return ret;
5215
5216}
5217EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5218
5219
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005220/*
5221 * Renew the cl_session lease.
5222 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005223struct nfs4_sequence_data {
5224 struct nfs_client *clp;
5225 struct nfs4_sequence_args args;
5226 struct nfs4_sequence_res res;
5227};
5228
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005229static void nfs41_sequence_release(void *data)
5230{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005231 struct nfs4_sequence_data *calldata = data;
5232 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005233
Alexandros Batsakis71358402010-02-05 03:45:05 -08005234 if (atomic_read(&clp->cl_count) > 1)
5235 nfs4_schedule_state_renewal(clp);
5236 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005237 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005238}
5239
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005240static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5241{
5242 switch(task->tk_status) {
5243 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005244 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5245 return -EAGAIN;
5246 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005247 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005248 }
5249 return 0;
5250}
5251
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005252static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005253{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005254 struct nfs4_sequence_data *calldata = data;
5255 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005256
Trond Myklebust14516c32010-07-31 14:29:06 -04005257 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5258 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005259
5260 if (task->tk_status < 0) {
5261 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005262 if (atomic_read(&clp->cl_count) == 1)
5263 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005264
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005265 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5266 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005267 return;
5268 }
5269 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005270 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005271out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005272 dprintk("<-- %s\n", __func__);
5273}
5274
5275static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5276{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005277 struct nfs4_sequence_data *calldata = data;
5278 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005279 struct nfs4_sequence_args *args;
5280 struct nfs4_sequence_res *res;
5281
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005282 args = task->tk_msg.rpc_argp;
5283 res = task->tk_msg.rpc_resp;
5284
Trond Myklebust035168a2010-06-16 09:52:26 -04005285 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005286 return;
5287 rpc_call_start(task);
5288}
5289
5290static const struct rpc_call_ops nfs41_sequence_ops = {
5291 .rpc_call_done = nfs41_sequence_call_done,
5292 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005293 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005294};
5295
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005296static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005297{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005298 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005299 struct rpc_message msg = {
5300 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5301 .rpc_cred = cred,
5302 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005303 struct rpc_task_setup task_setup_data = {
5304 .rpc_client = clp->cl_rpcclient,
5305 .rpc_message = &msg,
5306 .callback_ops = &nfs41_sequence_ops,
5307 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5308 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005309
Alexandros Batsakis71358402010-02-05 03:45:05 -08005310 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005311 return ERR_PTR(-EIO);
Benny Halevydfb4f302010-09-24 09:17:01 -04005312 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005313 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005314 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005315 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005316 }
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005317 msg.rpc_argp = &calldata->args;
5318 msg.rpc_resp = &calldata->res;
5319 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005320 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005321
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005322 return rpc_run_task(&task_setup_data);
5323}
5324
5325static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5326{
5327 struct rpc_task *task;
5328 int ret = 0;
5329
5330 task = _nfs41_proc_sequence(clp, cred);
5331 if (IS_ERR(task))
5332 ret = PTR_ERR(task);
5333 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005334 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005335 dprintk("<-- %s status=%d\n", __func__, ret);
5336 return ret;
5337}
5338
5339static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5340{
5341 struct rpc_task *task;
5342 int ret;
5343
5344 task = _nfs41_proc_sequence(clp, cred);
5345 if (IS_ERR(task)) {
5346 ret = PTR_ERR(task);
5347 goto out;
5348 }
5349 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005350 if (!ret) {
5351 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5352
5353 if (task->tk_status == 0)
5354 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005355 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005356 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005357 rpc_put_task(task);
5358out:
5359 dprintk("<-- %s status=%d\n", __func__, ret);
5360 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005361}
5362
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005363struct nfs4_reclaim_complete_data {
5364 struct nfs_client *clp;
5365 struct nfs41_reclaim_complete_args arg;
5366 struct nfs41_reclaim_complete_res res;
5367};
5368
5369static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5370{
5371 struct nfs4_reclaim_complete_data *calldata = data;
5372
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005373 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005374 if (nfs41_setup_sequence(calldata->clp->cl_session,
5375 &calldata->arg.seq_args,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005376 &calldata->res.seq_res, 0, task))
5377 return;
5378
5379 rpc_call_start(task);
5380}
5381
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005382static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5383{
5384 switch(task->tk_status) {
5385 case 0:
5386 case -NFS4ERR_COMPLETE_ALREADY:
5387 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5388 break;
5389 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005390 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5391 return -EAGAIN;
5392 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005393 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005394 }
5395 return 0;
5396}
5397
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005398static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5399{
5400 struct nfs4_reclaim_complete_data *calldata = data;
5401 struct nfs_client *clp = calldata->clp;
5402 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5403
5404 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005405 if (!nfs41_sequence_done(task, res))
5406 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005407
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005408 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5409 rpc_restart_call_prepare(task);
5410 return;
5411 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005412 dprintk("<-- %s\n", __func__);
5413}
5414
5415static void nfs4_free_reclaim_complete_data(void *data)
5416{
5417 struct nfs4_reclaim_complete_data *calldata = data;
5418
5419 kfree(calldata);
5420}
5421
5422static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5423 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5424 .rpc_call_done = nfs4_reclaim_complete_done,
5425 .rpc_release = nfs4_free_reclaim_complete_data,
5426};
5427
5428/*
5429 * Issue a global reclaim complete.
5430 */
5431static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5432{
5433 struct nfs4_reclaim_complete_data *calldata;
5434 struct rpc_task *task;
5435 struct rpc_message msg = {
5436 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5437 };
5438 struct rpc_task_setup task_setup_data = {
5439 .rpc_client = clp->cl_rpcclient,
5440 .rpc_message = &msg,
5441 .callback_ops = &nfs4_reclaim_complete_call_ops,
5442 .flags = RPC_TASK_ASYNC,
5443 };
5444 int status = -ENOMEM;
5445
5446 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005447 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005448 if (calldata == NULL)
5449 goto out;
5450 calldata->clp = clp;
5451 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005452
5453 msg.rpc_argp = &calldata->arg;
5454 msg.rpc_resp = &calldata->res;
5455 task_setup_data.callback_data = calldata;
5456 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005457 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005458 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005459 goto out;
5460 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005461 status = nfs4_wait_for_completion_rpc_task(task);
5462 if (status == 0)
5463 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005464 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005465 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005466out:
5467 dprintk("<-- %s status=%d\n", __func__, status);
5468 return status;
5469}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005470
5471static void
5472nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5473{
5474 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005475 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005476
5477 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005478 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5479 * right now covering the LAYOUTGET we are about to send.
5480 * However, that is not so catastrophic, and there seems
5481 * to be no way to prevent it completely.
5482 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005483 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5484 &lgp->res.seq_res, 0, task))
5485 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005486 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5487 NFS_I(lgp->args.inode)->layout,
5488 lgp->args.ctx->state)) {
5489 rpc_exit(task, NFS4_OK);
5490 return;
5491 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005492 rpc_call_start(task);
5493}
5494
5495static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5496{
5497 struct nfs4_layoutget *lgp = calldata;
5498 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5499
5500 dprintk("--> %s\n", __func__);
5501
5502 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5503 return;
5504
5505 switch (task->tk_status) {
5506 case 0:
5507 break;
5508 case -NFS4ERR_LAYOUTTRYLATER:
5509 case -NFS4ERR_RECALLCONFLICT:
5510 task->tk_status = -NFS4ERR_DELAY;
5511 /* Fall through */
5512 default:
5513 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5514 rpc_restart_call_prepare(task);
5515 return;
5516 }
5517 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005518 dprintk("<-- %s\n", __func__);
5519}
5520
5521static void nfs4_layoutget_release(void *calldata)
5522{
5523 struct nfs4_layoutget *lgp = calldata;
5524
5525 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005526 if (lgp->res.layout.buf != NULL)
5527 free_page((unsigned long) lgp->res.layout.buf);
5528 put_nfs_open_context(lgp->args.ctx);
5529 kfree(calldata);
5530 dprintk("<-- %s\n", __func__);
5531}
5532
5533static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5534 .rpc_call_prepare = nfs4_layoutget_prepare,
5535 .rpc_call_done = nfs4_layoutget_done,
5536 .rpc_release = nfs4_layoutget_release,
5537};
5538
5539int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5540{
5541 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5542 struct rpc_task *task;
5543 struct rpc_message msg = {
5544 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5545 .rpc_argp = &lgp->args,
5546 .rpc_resp = &lgp->res,
5547 };
5548 struct rpc_task_setup task_setup_data = {
5549 .rpc_client = server->client,
5550 .rpc_message = &msg,
5551 .callback_ops = &nfs4_layoutget_call_ops,
5552 .callback_data = lgp,
5553 .flags = RPC_TASK_ASYNC,
5554 };
5555 int status = 0;
5556
5557 dprintk("--> %s\n", __func__);
5558
5559 lgp->res.layout.buf = (void *)__get_free_page(GFP_NOFS);
5560 if (lgp->res.layout.buf == NULL) {
5561 nfs4_layoutget_release(lgp);
5562 return -ENOMEM;
5563 }
5564
5565 lgp->res.seq_res.sr_slot = NULL;
5566 task = rpc_run_task(&task_setup_data);
5567 if (IS_ERR(task))
5568 return PTR_ERR(task);
5569 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00005570 if (status == 0)
5571 status = task->tk_status;
5572 if (status == 0)
5573 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005574 rpc_put_task(task);
5575 dprintk("<-- %s status=%d\n", __func__, status);
5576 return status;
5577}
5578
5579static int
5580_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5581{
5582 struct nfs4_getdeviceinfo_args args = {
5583 .pdev = pdev,
5584 };
5585 struct nfs4_getdeviceinfo_res res = {
5586 .pdev = pdev,
5587 };
5588 struct rpc_message msg = {
5589 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5590 .rpc_argp = &args,
5591 .rpc_resp = &res,
5592 };
5593 int status;
5594
5595 dprintk("--> %s\n", __func__);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00005596 status = nfs4_call_sync(server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005597 dprintk("<-- %s status=%d\n", __func__, status);
5598
5599 return status;
5600}
5601
5602int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5603{
5604 struct nfs4_exception exception = { };
5605 int err;
5606
5607 do {
5608 err = nfs4_handle_exception(server,
5609 _nfs4_proc_getdeviceinfo(server, pdev),
5610 &exception);
5611 } while (exception.retry);
5612 return err;
5613}
5614EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5615
Andy Adamson557134a2009-04-01 09:21:53 -04005616#endif /* CONFIG_NFS_V4_1 */
5617
Andy Adamson591d71c2009-04-01 09:22:47 -04005618struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005619 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005620 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621 .recover_open = nfs4_open_reclaim,
5622 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04005623 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005624 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625};
5626
Andy Adamson591d71c2009-04-01 09:22:47 -04005627#if defined(CONFIG_NFS_V4_1)
5628struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5629 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5630 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5631 .recover_open = nfs4_open_reclaim,
5632 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05005633 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005634 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005635 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04005636};
5637#endif /* CONFIG_NFS_V4_1 */
5638
5639struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005640 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005641 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 .recover_open = nfs4_open_expired,
5643 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04005644 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005645 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646};
5647
Andy Adamson591d71c2009-04-01 09:22:47 -04005648#if defined(CONFIG_NFS_V4_1)
5649struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5650 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5651 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5652 .recover_open = nfs4_open_expired,
5653 .recover_lock = nfs4_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05005654 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005655 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04005656};
5657#endif /* CONFIG_NFS_V4_1 */
5658
Benny Halevy29fba382009-04-01 09:22:44 -04005659struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5660 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04005661 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005662 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04005663};
5664
5665#if defined(CONFIG_NFS_V4_1)
5666struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5667 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04005668 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005669 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04005670};
5671#endif
5672
Trond Myklebust97dc1352010-06-16 09:52:26 -04005673static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
5674 .minor_version = 0,
5675 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04005676 .validate_stateid = nfs4_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005677 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
5678 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
5679 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005680};
5681
5682#if defined(CONFIG_NFS_V4_1)
5683static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
5684 .minor_version = 1,
5685 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04005686 .validate_stateid = nfs41_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005687 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
5688 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
5689 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005690};
5691#endif
5692
Trond Myklebust97dc1352010-06-16 09:52:26 -04005693const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
5694 [0] = &nfs_v4_0_minor_ops,
5695#if defined(CONFIG_NFS_V4_1)
5696 [1] = &nfs_v4_1_minor_ops,
5697#endif
5698};
5699
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005700static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005701 .permission = nfs_permission,
5702 .getattr = nfs_getattr,
5703 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005704 .getxattr = generic_getxattr,
5705 .setxattr = generic_setxattr,
5706 .listxattr = generic_listxattr,
5707 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005708};
5709
David Howells509de812006-08-22 20:06:11 -04005710const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711 .version = 4, /* protocol version */
5712 .dentry_ops = &nfs4_dentry_operations,
5713 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005714 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715 .getroot = nfs4_proc_get_root,
5716 .getattr = nfs4_proc_getattr,
5717 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04005718 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719 .lookup = nfs4_proc_lookup,
5720 .access = nfs4_proc_access,
5721 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722 .create = nfs4_proc_create,
5723 .remove = nfs4_proc_remove,
5724 .unlink_setup = nfs4_proc_unlink_setup,
5725 .unlink_done = nfs4_proc_unlink_done,
5726 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04005727 .rename_setup = nfs4_proc_rename_setup,
5728 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729 .link = nfs4_proc_link,
5730 .symlink = nfs4_proc_symlink,
5731 .mkdir = nfs4_proc_mkdir,
5732 .rmdir = nfs4_proc_remove,
5733 .readdir = nfs4_proc_readdir,
5734 .mknod = nfs4_proc_mknod,
5735 .statfs = nfs4_proc_statfs,
5736 .fsinfo = nfs4_proc_fsinfo,
5737 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04005738 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739 .decode_dirent = nfs4_decode_dirent,
5740 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05005741 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005743 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005745 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005747 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04005748 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04005749 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00005750 .init_client = nfs4_init_client,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751};
5752
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005753static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
5754 .prefix = XATTR_NAME_NFSV4_ACL,
5755 .list = nfs4_xattr_list_nfs4_acl,
5756 .get = nfs4_xattr_get_nfs4_acl,
5757 .set = nfs4_xattr_set_nfs4_acl,
5758};
5759
5760const struct xattr_handler *nfs4_xattr_handlers[] = {
5761 &nfs4_xattr_nfs4_acl_handler,
5762 NULL
5763};
5764
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765/*
5766 * Local variables:
5767 * c-basic-offset: 8
5768 * End:
5769 */