blob: bcc29bdc9bee993b4a6017654e352d5255e1d9a5 [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 Halevydfb4f3092010-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 Halevydfb4f3092010-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 Halevydfb4f3092010-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 Halevydfb4f3092010-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 Halevydfb4f3092010-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 Halevydfb4f3092010-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 Halevydfb4f3092010-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 Halevydfb4f3092010-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 Halevydfb4f3092010-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 Halevydfb4f3092010-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 Halevydfb4f3092010-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 Halevydfb4f3092010-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 Halevydfb4f3092010-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 Halevydfb4f3092010-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
722#define nfs4_call_sync(server, msg, args, res, cache_reply) \
Trond Myklebust97dc1352010-06-16 09:52:26 -0400723 (server)->nfs_client->cl_mvops->call_sync((server), (msg), &(args)->seq_args, \
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400724 &(res)->seq_res, (cache_reply))
725
Trond Myklebust38478b22006-05-25 01:40:57 -0400726static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
Trond Myklebust38478b22006-05-25 01:40:57 -0400728 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Trond Myklebust38478b22006-05-25 01:40:57 -0400730 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400731 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
732 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400733 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400734 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400735 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736}
737
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100738struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400739 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100740 struct nfs_openargs o_arg;
741 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100742 struct nfs_open_confirmargs c_arg;
743 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100744 struct nfs_fattr f_attr;
745 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400746 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100747 struct dentry *dir;
748 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400749 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100750 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100751 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400752 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100753 int rpc_status;
754 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100755};
756
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400757
758static void nfs4_init_opendata_res(struct nfs4_opendata *p)
759{
760 p->o_res.f_attr = &p->f_attr;
761 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400762 p->o_res.seqid = p->o_arg.seqid;
763 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400764 p->o_res.server = p->o_arg.server;
765 nfs_fattr_init(&p->f_attr);
766 nfs_fattr_init(&p->dir_attr);
767}
768
Trond Myklebustad389da2007-06-05 12:30:00 -0400769static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500770 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400771 const struct iattr *attrs,
772 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100773{
Trond Myklebustad389da2007-06-05 12:30:00 -0400774 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100775 struct inode *dir = parent->d_inode;
776 struct nfs_server *server = NFS_SERVER(dir);
777 struct nfs4_opendata *p;
778
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400779 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100780 if (p == NULL)
781 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400782 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100783 if (p->o_arg.seqid == NULL)
784 goto err_free;
Al Virof6948692010-01-30 13:51:04 -0500785 path_get(path);
786 p->path = *path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100787 p->dir = parent;
788 p->owner = sp;
789 atomic_inc(&sp->so_count);
790 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500791 p->o_arg.open_flags = flags;
792 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400793 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400794 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400795 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100796 p->o_arg.server = server;
797 p->o_arg.bitmask = server->attr_bitmask;
798 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400799 if (flags & O_CREAT) {
800 u32 *s;
801
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100802 p->o_arg.u.attrs = &p->attrs;
803 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400804 s = (u32 *) p->o_arg.u.verifier.data;
805 s[0] = jiffies;
806 s[1] = current->pid;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100807 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100808 p->c_arg.fh = &p->o_res.fh;
809 p->c_arg.stateid = &p->o_res.stateid;
810 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400811 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400812 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100813 return p;
814err_free:
815 kfree(p);
816err:
817 dput(parent);
818 return NULL;
819}
820
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400821static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100822{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400823 struct nfs4_opendata *p = container_of(kref,
824 struct nfs4_opendata, kref);
825
826 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400827 if (p->state != NULL)
828 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400829 nfs4_put_state_owner(p->owner);
830 dput(p->dir);
Jan Blunck31f31db12008-05-02 13:42:45 -0700831 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400832 kfree(p);
833}
834
835static void nfs4_opendata_put(struct nfs4_opendata *p)
836{
837 if (p != NULL)
838 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100839}
840
Trond Myklebust06f814a2006-01-03 09:55:07 +0100841static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
842{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100843 int ret;
844
Trond Myklebust06f814a2006-01-03 09:55:07 +0100845 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100846 return ret;
847}
848
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500849static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400850{
851 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500852
853 if (open_mode & O_EXCL)
854 goto out;
855 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400856 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500857 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
858 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400859 break;
860 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500861 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
862 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400863 break;
864 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500865 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
866 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400867 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500868out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400869 return ret;
870}
871
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500872static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400873{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500874 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400875 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500876 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400877 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500878 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400879 return 1;
880}
881
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500882static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100883{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500884 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100885 case FMODE_WRITE:
886 state->n_wronly++;
887 break;
888 case FMODE_READ:
889 state->n_rdonly++;
890 break;
891 case FMODE_READ|FMODE_WRITE:
892 state->n_rdwr++;
893 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500894 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100895}
896
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500897static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400898{
899 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
900 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
901 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500902 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400903 case FMODE_READ:
904 set_bit(NFS_O_RDONLY_STATE, &state->flags);
905 break;
906 case FMODE_WRITE:
907 set_bit(NFS_O_WRONLY_STATE, &state->flags);
908 break;
909 case FMODE_READ|FMODE_WRITE:
910 set_bit(NFS_O_RDWR_STATE, &state->flags);
911 }
912}
913
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500914static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400915{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400916 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500917 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400918 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400919}
920
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500921static 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 -0700922{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400923 /*
924 * Protect the call to nfs4_state_set_mode_locked and
925 * serialise the stateid update
926 */
927 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400928 if (deleg_stateid != NULL) {
929 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
930 set_bit(NFS_DELEGATED_STATE, &state->flags);
931 }
932 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500933 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400934 write_sequnlock(&state->seqlock);
935 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500936 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700937 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500940static 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 -0500941{
942 struct nfs_inode *nfsi = NFS_I(state->inode);
943 struct nfs_delegation *deleg_cur;
944 int ret = 0;
945
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500946 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500947
948 rcu_read_lock();
949 deleg_cur = rcu_dereference(nfsi->delegation);
950 if (deleg_cur == NULL)
951 goto no_delegation;
952
953 spin_lock(&deleg_cur->lock);
954 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500955 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500956 goto no_delegation_unlock;
957
958 if (delegation == NULL)
959 delegation = &deleg_cur->stateid;
960 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
961 goto no_delegation_unlock;
962
Trond Myklebustb7391f42008-12-23 15:21:52 -0500963 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500964 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500965 ret = 1;
966no_delegation_unlock:
967 spin_unlock(&deleg_cur->lock);
968no_delegation:
969 rcu_read_unlock();
970
971 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500972 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500973 ret = 1;
974 }
975
976 return ret;
977}
978
979
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500980static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400981{
982 struct nfs_delegation *delegation;
983
984 rcu_read_lock();
985 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500986 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400987 rcu_read_unlock();
988 return;
989 }
990 rcu_read_unlock();
991 nfs_inode_return_delegation(inode);
992}
993
Trond Myklebust6ee41262007-07-08 14:11:36 -0400994static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400995{
996 struct nfs4_state *state = opendata->state;
997 struct nfs_inode *nfsi = NFS_I(state->inode);
998 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500999 int open_mode = opendata->o_arg.open_flags & O_EXCL;
1000 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001001 nfs4_stateid stateid;
1002 int ret = -EAGAIN;
1003
Trond Myklebustaac00a82007-07-05 19:02:21 -04001004 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001005 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001006 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001007 if (can_open_cached(state, fmode, open_mode)) {
1008 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001009 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001010 goto out_return_state;
1011 }
1012 spin_unlock(&state->owner->so_lock);
1013 }
Trond Myklebust34310432008-12-23 15:21:38 -05001014 rcu_read_lock();
1015 delegation = rcu_dereference(nfsi->delegation);
1016 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001017 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001018 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001019 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001020 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001021 /* Save the delegation */
1022 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1023 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001024 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001025 if (ret != 0)
1026 goto out;
1027 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001028
1029 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001030 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001031 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001032 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001033out:
1034 return ERR_PTR(ret);
1035out_return_state:
1036 atomic_inc(&state->count);
1037 return state;
1038}
1039
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001040static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1041{
1042 struct inode *inode;
1043 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001044 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001045 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001046
Trond Myklebustaac00a82007-07-05 19:02:21 -04001047 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001048 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001049 goto out;
1050 }
1051
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001052 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001053 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001054 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001055 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001056 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001057 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001058 goto err;
1059 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001060 state = nfs4_get_open_state(inode, data->owner);
1061 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001062 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001063 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001064 int delegation_flags = 0;
1065
Trond Myklebust003707c2007-07-05 18:07:55 -04001066 rcu_read_lock();
1067 delegation = rcu_dereference(NFS_I(inode)->delegation);
1068 if (delegation)
1069 delegation_flags = delegation->flags;
1070 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -05001071 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001072 nfs_inode_set_delegation(state->inode,
1073 data->owner->so_cred,
1074 &data->o_res);
1075 else
1076 nfs_inode_reclaim_delegation(state->inode,
1077 data->owner->so_cred,
1078 &data->o_res);
1079 }
Trond Myklebust34310432008-12-23 15:21:38 -05001080
1081 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001082 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001083 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001084out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001085 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001086err_put_inode:
1087 iput(inode);
1088err:
1089 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001090}
1091
Trond Myklebust864472e2006-01-03 09:55:15 +01001092static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1093{
1094 struct nfs_inode *nfsi = NFS_I(state->inode);
1095 struct nfs_open_context *ctx;
1096
1097 spin_lock(&state->inode->i_lock);
1098 list_for_each_entry(ctx, &nfsi->open_files, list) {
1099 if (ctx->state != state)
1100 continue;
1101 get_nfs_open_context(ctx);
1102 spin_unlock(&state->inode->i_lock);
1103 return ctx;
1104 }
1105 spin_unlock(&state->inode->i_lock);
1106 return ERR_PTR(-ENOENT);
1107}
1108
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001109static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1110{
1111 struct nfs4_opendata *opendata;
1112
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001113 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001114 if (opendata == NULL)
1115 return ERR_PTR(-ENOMEM);
1116 opendata->state = state;
1117 atomic_inc(&state->count);
1118 return opendata;
1119}
1120
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001121static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001122{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001123 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001124 int ret;
1125
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001126 opendata->o_arg.open_flags = 0;
1127 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001128 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1129 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1130 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001131 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001132 if (ret != 0)
1133 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001134 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001135 if (IS_ERR(newstate))
1136 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001137 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001138 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001139 return 0;
1140}
1141
1142static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1143{
Trond Myklebust864472e2006-01-03 09:55:15 +01001144 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001145 int ret;
1146
1147 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001148 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001149 smp_rmb();
1150 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001151 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001152 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001153 if (ret != 0)
1154 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001155 if (newstate != state)
1156 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001157 }
1158 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001159 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001160 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001161 if (ret != 0)
1162 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001163 if (newstate != state)
1164 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001165 }
1166 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001167 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001168 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001169 if (ret != 0)
1170 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001171 if (newstate != state)
1172 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001173 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001174 /*
1175 * We may have performed cached opens for all three recoveries.
1176 * Check if we need to update the current stateid.
1177 */
1178 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1179 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001180 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001181 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1182 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001183 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001184 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001185 return 0;
1186}
1187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188/*
1189 * OPEN_RECLAIM:
1190 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001192static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001194 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001195 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001196 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 int status;
1198
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001199 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1200 if (IS_ERR(opendata))
1201 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001202 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1203 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001204 rcu_read_lock();
1205 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001206 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001207 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001208 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001209 opendata->o_arg.u.delegation_type = delegation_type;
1210 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001211 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 return status;
1213}
1214
Trond Myklebust539cd032007-06-05 11:46:42 -04001215static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
1217 struct nfs_server *server = NFS_SERVER(state->inode);
1218 struct nfs4_exception exception = { };
1219 int err;
1220 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001221 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001222 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001223 break;
1224 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 } while (exception.retry);
1226 return err;
1227}
1228
Trond Myklebust864472e2006-01-03 09:55:15 +01001229static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1230{
1231 struct nfs_open_context *ctx;
1232 int ret;
1233
1234 ctx = nfs4_state_find_open_context(state);
1235 if (IS_ERR(ctx))
1236 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001237 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001238 put_nfs_open_context(ctx);
1239 return ret;
1240}
1241
Trond Myklebust13437e12007-07-06 15:10:43 -04001242static 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 -07001243{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001244 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001245 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001247 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1248 if (IS_ERR(opendata))
1249 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001250 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001251 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001252 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001253 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001254 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001255 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256}
1257
Trond Myklebust13437e12007-07-06 15:10:43 -04001258int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259{
1260 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001261 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 int err;
1263 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001264 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 switch (err) {
1266 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001267 case -ENOENT:
1268 case -ESTALE:
1269 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001270 case -NFS4ERR_BADSESSION:
1271 case -NFS4ERR_BADSLOT:
1272 case -NFS4ERR_BAD_HIGH_SLOT:
1273 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1274 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001275 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001276 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 case -NFS4ERR_STALE_CLIENTID:
1278 case -NFS4ERR_STALE_STATEID:
1279 case -NFS4ERR_EXPIRED:
1280 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001281 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001282 goto out;
1283 case -ERESTARTSYS:
1284 /*
1285 * The show must go on: exit, but mark the
1286 * stateid as needing recovery.
1287 */
1288 case -NFS4ERR_ADMIN_REVOKED:
1289 case -NFS4ERR_BAD_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001290 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001291 case -EKEYEXPIRED:
1292 /*
1293 * User RPCSEC_GSS context has expired.
1294 * We cannot recover this stateid now, so
1295 * skip it and allow recovery thread to
1296 * proceed.
1297 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001298 case -ENOMEM:
1299 err = 0;
1300 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 }
1302 err = nfs4_handle_exception(server, err, &exception);
1303 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001304out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 return err;
1306}
1307
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001308static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1309{
1310 struct nfs4_opendata *data = calldata;
1311
1312 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001313 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001314 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1315 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001316 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001317 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001318 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001319 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001320}
1321
1322static void nfs4_open_confirm_release(void *calldata)
1323{
1324 struct nfs4_opendata *data = calldata;
1325 struct nfs4_state *state = NULL;
1326
1327 /* If this request hasn't been cancelled, do nothing */
1328 if (data->cancelled == 0)
1329 goto out_free;
1330 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001331 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001332 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001333 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001334 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001335 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001336out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001337 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001338}
1339
1340static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001341 .rpc_call_done = nfs4_open_confirm_done,
1342 .rpc_release = nfs4_open_confirm_release,
1343};
1344
1345/*
1346 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1347 */
1348static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1349{
1350 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1351 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001352 struct rpc_message msg = {
1353 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1354 .rpc_argp = &data->c_arg,
1355 .rpc_resp = &data->c_res,
1356 .rpc_cred = data->owner->so_cred,
1357 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001358 struct rpc_task_setup task_setup_data = {
1359 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001360 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001361 .callback_ops = &nfs4_open_confirm_ops,
1362 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001363 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001364 .flags = RPC_TASK_ASYNC,
1365 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 int status;
1367
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001368 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001369 data->rpc_done = 0;
1370 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001371 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001372 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001373 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001374 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001375 status = nfs4_wait_for_completion_rpc_task(task);
1376 if (status != 0) {
1377 data->cancelled = 1;
1378 smp_wmb();
1379 } else
1380 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001381 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 return status;
1383}
1384
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001385static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001387 struct nfs4_opendata *data = calldata;
1388 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001389
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001390 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1391 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001392 /*
1393 * Check if we still need to send an OPEN call, or if we can use
1394 * a delegation instead.
1395 */
1396 if (data->state != NULL) {
1397 struct nfs_delegation *delegation;
1398
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001399 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001400 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001401 rcu_read_lock();
1402 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1403 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001404 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001405 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001406 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001407 }
1408 rcu_read_unlock();
1409 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001410 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001411 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001412 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001413 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001414 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001415 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1416 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001417 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001418 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001419 &data->o_arg.seq_args,
1420 &data->o_res.seq_res, 1, task))
1421 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001422 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001423 return;
1424out_no_action:
1425 task->tk_action = NULL;
1426
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001427}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001429static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1430{
1431 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1432 nfs4_open_prepare(task, calldata);
1433}
1434
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001435static void nfs4_open_done(struct rpc_task *task, void *calldata)
1436{
1437 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001439 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001440
Trond Myklebust14516c32010-07-31 14:29:06 -04001441 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1442 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001443
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001444 if (task->tk_status == 0) {
1445 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001446 case S_IFREG:
1447 break;
1448 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001449 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001450 break;
1451 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001452 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001453 break;
1454 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001455 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001456 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001457 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001458 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1459 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001460 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001461 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001462}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001463
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001464static void nfs4_open_release(void *calldata)
1465{
1466 struct nfs4_opendata *data = calldata;
1467 struct nfs4_state *state = NULL;
1468
1469 /* If this request hasn't been cancelled, do nothing */
1470 if (data->cancelled == 0)
1471 goto out_free;
1472 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001473 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001474 goto out_free;
1475 /* In case we need an open_confirm, no cleanup! */
1476 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1477 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001478 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001479 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001480 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001481out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001482 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001483}
1484
1485static const struct rpc_call_ops nfs4_open_ops = {
1486 .rpc_call_prepare = nfs4_open_prepare,
1487 .rpc_call_done = nfs4_open_done,
1488 .rpc_release = nfs4_open_release,
1489};
1490
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001491static const struct rpc_call_ops nfs4_recover_open_ops = {
1492 .rpc_call_prepare = nfs4_recover_open_prepare,
1493 .rpc_call_done = nfs4_open_done,
1494 .rpc_release = nfs4_open_release,
1495};
1496
1497static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001498{
1499 struct inode *dir = data->dir->d_inode;
1500 struct nfs_server *server = NFS_SERVER(dir);
1501 struct nfs_openargs *o_arg = &data->o_arg;
1502 struct nfs_openres *o_res = &data->o_res;
1503 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001504 struct rpc_message msg = {
1505 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1506 .rpc_argp = o_arg,
1507 .rpc_resp = o_res,
1508 .rpc_cred = data->owner->so_cred,
1509 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001510 struct rpc_task_setup task_setup_data = {
1511 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001512 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001513 .callback_ops = &nfs4_open_ops,
1514 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001515 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001516 .flags = RPC_TASK_ASYNC,
1517 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001518 int status;
1519
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001520 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001521 data->rpc_done = 0;
1522 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001523 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001524 if (isrecover)
1525 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001526 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001527 if (IS_ERR(task))
1528 return PTR_ERR(task);
1529 status = nfs4_wait_for_completion_rpc_task(task);
1530 if (status != 0) {
1531 data->cancelled = 1;
1532 smp_wmb();
1533 } else
1534 status = data->rpc_status;
1535 rpc_put_task(task);
1536
1537 return status;
1538}
1539
1540static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1541{
1542 struct inode *dir = data->dir->d_inode;
1543 struct nfs_openres *o_res = &data->o_res;
1544 int status;
1545
1546 status = nfs4_run_open_task(data, 1);
1547 if (status != 0 || !data->rpc_done)
1548 return status;
1549
1550 nfs_refresh_inode(dir, o_res->dir_attr);
1551
1552 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1553 status = _nfs4_proc_open_confirm(data);
1554 if (status != 0)
1555 return status;
1556 }
1557
1558 return status;
1559}
1560
1561/*
1562 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1563 */
1564static int _nfs4_proc_open(struct nfs4_opendata *data)
1565{
1566 struct inode *dir = data->dir->d_inode;
1567 struct nfs_server *server = NFS_SERVER(dir);
1568 struct nfs_openargs *o_arg = &data->o_arg;
1569 struct nfs_openres *o_res = &data->o_res;
1570 int status;
1571
1572 status = nfs4_run_open_task(data, 0);
Trond Myklebust3e309912007-07-07 13:19:59 -04001573 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001574 return status;
1575
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001576 if (o_arg->open_flags & O_CREAT) {
1577 update_changeattr(dir, &o_res->cinfo);
1578 nfs_post_op_update_inode(dir, o_res->dir_attr);
1579 } else
1580 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001581 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1582 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001584 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001586 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 }
1588 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001589 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001590 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591}
1592
Andy Adamsond83217c2011-03-01 01:34:17 +00001593static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001594{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001595 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001596 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001597
Trond Myklebusta78cb572009-08-09 15:06:19 -04001598 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001599 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001600 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001601 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001602 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1603 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001604 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001605 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001606 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001607 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001608 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001609}
1610
Andy Adamsond83217c2011-03-01 01:34:17 +00001611static int nfs4_recover_expired_lease(struct nfs_server *server)
1612{
1613 return nfs4_client_recover_expired_lease(server->nfs_client);
1614}
1615
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616/*
1617 * OPEN_EXPIRED:
1618 * reclaim state on the server after a network partition.
1619 * Assumes caller holds the appropriate lock
1620 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001621static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001623 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001624 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001626 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1627 if (IS_ERR(opendata))
1628 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001629 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001630 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001631 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001632 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001633 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634}
1635
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001636static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001637{
Trond Myklebust539cd032007-06-05 11:46:42 -04001638 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001639 struct nfs4_exception exception = { };
1640 int err;
1641
1642 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001643 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001644 switch (err) {
1645 default:
1646 goto out;
1647 case -NFS4ERR_GRACE:
1648 case -NFS4ERR_DELAY:
1649 nfs4_handle_exception(server, err, &exception);
1650 err = 0;
1651 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001652 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001653out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001654 return err;
1655}
1656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1658{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001660 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Trond Myklebust864472e2006-01-03 09:55:15 +01001662 ctx = nfs4_state_find_open_context(state);
1663 if (IS_ERR(ctx))
1664 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001665 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001666 put_nfs_open_context(ctx);
1667 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668}
1669
1670/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001671 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1672 * fields corresponding to attributes that were used to store the verifier.
1673 * Make sure we clobber those fields in the later setattr call
1674 */
1675static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1676{
1677 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1678 !(sattr->ia_valid & ATTR_ATIME_SET))
1679 sattr->ia_valid |= ATTR_ATIME;
1680
1681 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1682 !(sattr->ia_valid & ATTR_MTIME_SET))
1683 sattr->ia_valid |= ATTR_MTIME;
1684}
1685
1686/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001687 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001689static 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 -07001690{
1691 struct nfs4_state_owner *sp;
1692 struct nfs4_state *state = NULL;
1693 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001694 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001695 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
1697 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 status = -ENOMEM;
1699 if (!(sp = nfs4_get_state_owner(server, cred))) {
1700 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1701 goto out_err;
1702 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001703 status = nfs4_recover_expired_lease(server);
1704 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001705 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001706 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001707 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001708 status = -ENOMEM;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001709 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001710 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001711 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
Trond Myklebustaac00a82007-07-05 19:02:21 -04001713 if (path->dentry->d_inode != NULL)
1714 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1715
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001716 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001718 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001720 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001721 status = PTR_ERR(state);
1722 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001723 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001724 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001725 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001726
1727 if (opendata->o_arg.open_flags & O_EXCL) {
1728 nfs4_exclusive_attrset(opendata, sattr);
1729
1730 nfs_fattr_init(opendata->o_res.f_attr);
1731 status = nfs4_do_setattr(state->inode, cred,
1732 opendata->o_res.f_attr, sattr,
1733 state);
1734 if (status == 0)
1735 nfs_setattr_update_inode(state->inode, sattr);
1736 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1737 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001738 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 *res = state;
1741 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001742err_opendata_put:
1743 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001744err_put_state_owner:
1745 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 *res = NULL;
1748 return status;
1749}
1750
1751
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001752static 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 -07001753{
1754 struct nfs4_exception exception = { };
1755 struct nfs4_state *res;
1756 int status;
1757
1758 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001759 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 if (status == 0)
1761 break;
1762 /* NOTE: BAD_SEQID means the server and client disagree about the
1763 * book-keeping w.r.t. state-changing operations
1764 * (OPEN/CLOSE/LOCK/LOCKU...)
1765 * It is actually a sign of a bug on the client or on the server.
1766 *
1767 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001768 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 * have unhashed the old state_owner for us, and that we can
1770 * therefore safely retry using a new one. We should still warn
1771 * the user though...
1772 */
1773 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001774 printk(KERN_WARNING "NFS: v4 server %s "
1775 " returned a bad sequence-id error!\n",
1776 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 exception.retry = 1;
1778 continue;
1779 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001780 /*
1781 * BAD_STATEID on OPEN means that the server cancelled our
1782 * state before it received the OPEN_CONFIRM.
1783 * Recover by retrying the request as per the discussion
1784 * on Page 181 of RFC3530.
1785 */
1786 if (status == -NFS4ERR_BAD_STATEID) {
1787 exception.retry = 1;
1788 continue;
1789 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001790 if (status == -EAGAIN) {
1791 /* We must have found a delegation */
1792 exception.retry = 1;
1793 continue;
1794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1796 status, &exception));
1797 } while (exception.retry);
1798 return res;
1799}
1800
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001801static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1802 struct nfs_fattr *fattr, struct iattr *sattr,
1803 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001805 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001807 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 .iap = sattr,
1809 .server = server,
1810 .bitmask = server->attr_bitmask,
1811 };
1812 struct nfs_setattrres res = {
1813 .fattr = fattr,
1814 .server = server,
1815 };
1816 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001817 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1818 .rpc_argp = &arg,
1819 .rpc_resp = &res,
1820 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001822 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001823 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
Trond Myklebust0e574af2005-10-27 22:12:38 -04001825 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001827 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1828 /* Use that stateid */
1829 } else if (state != NULL) {
Trond Myklebust77041ed2010-07-01 12:49:11 -04001830 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001831 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1833
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001834 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001835 if (status == 0 && state != NULL)
1836 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001837 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838}
1839
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001840static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1841 struct nfs_fattr *fattr, struct iattr *sattr,
1842 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001844 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 struct nfs4_exception exception = { };
1846 int err;
1847 do {
1848 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001849 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 &exception);
1851 } while (exception.retry);
1852 return err;
1853}
1854
1855struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001856 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 struct inode *inode;
1858 struct nfs4_state *state;
1859 struct nfs_closeargs arg;
1860 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001861 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001862 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00001863 bool roc;
1864 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865};
1866
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001867static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001868{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001869 struct nfs4_closedata *calldata = data;
1870 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001871
Fred Isamanf7e89172011-01-06 11:36:32 +00001872 if (calldata->roc)
1873 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001874 nfs4_put_open_state(calldata->state);
1875 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001876 nfs4_put_state_owner(sp);
Jan Blunck31f31db12008-05-02 13:42:45 -07001877 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001878 kfree(calldata);
1879}
1880
Trond Myklebust88069f72009-12-08 08:33:16 -05001881static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1882 fmode_t fmode)
1883{
1884 spin_lock(&state->owner->so_lock);
1885 if (!(fmode & FMODE_READ))
1886 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1887 if (!(fmode & FMODE_WRITE))
1888 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1889 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1890 spin_unlock(&state->owner->so_lock);
1891}
1892
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001893static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001895 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 struct nfs_server *server = NFS_SERVER(calldata->inode);
1898
Trond Myklebust14516c32010-07-31 14:29:06 -04001899 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1900 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 /* hmm. we are done with the inode, and in the process of freeing
1902 * the state_owner. we keep this around to process errors
1903 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 switch (task->tk_status) {
1905 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00001906 if (calldata->roc)
1907 pnfs_roc_set_barrier(state->inode,
1908 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04001909 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001910 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001911 nfs4_close_clear_stateid_flags(state,
1912 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 break;
1914 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001915 case -NFS4ERR_OLD_STATEID:
1916 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001918 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001919 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001921 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1922 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001924 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001925 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926}
1927
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001928static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001930 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001931 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001932 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001933
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001934 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001935 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001936
Trond Myklebust88069f72009-12-08 08:33:16 -05001937 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1938 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001939 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001940 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001941 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001942 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001943 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1944 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1945 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001946 }
1947 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001948 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1949 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1950 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001951 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001952 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001953 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05001954
1955 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001956 /* Note: exit _without_ calling nfs4_close_done */
1957 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001958 return;
1959 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001960
Fred Isamanf7e89172011-01-06 11:36:32 +00001961 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001962 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00001963 if (calldata->roc &&
1964 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
1965 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
1966 task, NULL);
1967 return;
1968 }
1969 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001970
Trond Myklebust516a6af2005-10-27 22:12:41 -04001971 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001972 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001973 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Andy Adamson19ddab02009-04-01 09:22:20 -04001974 &calldata->arg.seq_args, &calldata->res.seq_res,
1975 1, task))
1976 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001977 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978}
1979
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001980static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001981 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001982 .rpc_call_done = nfs4_close_done,
1983 .rpc_release = nfs4_free_closedata,
1984};
1985
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986/*
1987 * It is possible for data to be read/written from a mem-mapped file
1988 * after the sys_close call (which hits the vfs layer as a flush).
1989 * This means that we can't safely call nfsv4 close on a file until
1990 * the inode is cleared. This in turn means that we are not good
1991 * NFSv4 citizens - we do not indicate to the server to update the file's
1992 * share state even when we are done with one of the three share
1993 * stateid's in the inode.
1994 *
1995 * NOTE: Caller must be holding the sp->so_owner semaphore!
1996 */
Fred Isamanf7e89172011-01-06 11:36:32 +00001997int 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 -07001998{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001999 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002001 struct nfs4_state_owner *sp = state->owner;
2002 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002003 struct rpc_message msg = {
2004 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2005 .rpc_cred = state->owner->so_cred,
2006 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002007 struct rpc_task_setup task_setup_data = {
2008 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002009 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002010 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002011 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002012 .flags = RPC_TASK_ASYNC,
2013 };
Trond Myklebust95121352005-10-18 14:20:12 -07002014 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002016 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002018 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002019 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002021 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002022 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002024 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002025 if (calldata->arg.seqid == NULL)
2026 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002027 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002028 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002029 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002030 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002031 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002032 calldata->roc = roc;
Al Virof6948692010-01-30 13:51:04 -05002033 path_get(path);
2034 calldata->path = *path;
Trond Myklebust95121352005-10-18 14:20:12 -07002035
Trond Myklebust1174dd12010-12-21 10:52:24 -05002036 msg.rpc_argp = &calldata->arg;
2037 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002038 task_setup_data.callback_data = calldata;
2039 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002040 if (IS_ERR(task))
2041 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002042 status = 0;
2043 if (wait)
2044 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002045 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002046 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002047out_free_calldata:
2048 kfree(calldata);
2049out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002050 if (roc)
2051 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002052 nfs4_put_open_state(state);
2053 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002054 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055}
2056
Trond Myklebust2b484292010-09-17 10:56:51 -04002057static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002058nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 struct nfs4_state *state;
2061
Trond Myklebust565277f2007-10-15 18:17:53 -04002062 /* Protect against concurrent sillydeletes */
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002063 state = nfs4_do_open(dir, &ctx->path, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002064 if (IS_ERR(state))
2065 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002066 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002067 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068}
2069
Trond Myklebust1185a552009-12-03 15:54:02 -05002070static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002071{
2072 if (ctx->state == NULL)
2073 return;
2074 if (is_sync)
2075 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2076 else
2077 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2078}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
2080static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2081{
Benny Halevy43652ad2009-04-01 09:21:54 -04002082 struct nfs4_server_caps_arg args = {
2083 .fhandle = fhandle,
2084 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 struct nfs4_server_caps_res res = {};
2086 struct rpc_message msg = {
2087 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002088 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 .rpc_resp = &res,
2090 };
2091 int status;
2092
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002093 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 if (status == 0) {
2095 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002096 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2097 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2098 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2099 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2100 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2102 server->caps |= NFS_CAP_ACLS;
2103 if (res.has_links != 0)
2104 server->caps |= NFS_CAP_HARDLINKS;
2105 if (res.has_symlinks != 0)
2106 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002107 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2108 server->caps |= NFS_CAP_FILEID;
2109 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2110 server->caps |= NFS_CAP_MODE;
2111 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2112 server->caps |= NFS_CAP_NLINK;
2113 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2114 server->caps |= NFS_CAP_OWNER;
2115 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2116 server->caps |= NFS_CAP_OWNER_GROUP;
2117 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2118 server->caps |= NFS_CAP_ATIME;
2119 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2120 server->caps |= NFS_CAP_CTIME;
2121 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2122 server->caps |= NFS_CAP_MTIME;
2123
Trond Myklebusta65318b2009-03-11 14:10:28 -04002124 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2125 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2126 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 server->acl_bitmask = res.acl_bitmask;
2128 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002129
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 return status;
2131}
2132
Trond Myklebust55a97592006-06-09 09:34:19 -04002133int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134{
2135 struct nfs4_exception exception = { };
2136 int err;
2137 do {
2138 err = nfs4_handle_exception(server,
2139 _nfs4_server_capabilities(server, fhandle),
2140 &exception);
2141 } while (exception.retry);
2142 return err;
2143}
2144
2145static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2146 struct nfs_fsinfo *info)
2147{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 struct nfs4_lookup_root_arg args = {
2149 .bitmask = nfs4_fattr_bitmap,
2150 };
2151 struct nfs4_lookup_res res = {
2152 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002153 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 .fh = fhandle,
2155 };
2156 struct rpc_message msg = {
2157 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2158 .rpc_argp = &args,
2159 .rpc_resp = &res,
2160 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002161
Trond Myklebust0e574af2005-10-27 22:12:38 -04002162 nfs_fattr_init(info->fattr);
Trond Myklebustfccba802009-07-21 16:48:07 -04002163 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164}
2165
2166static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2167 struct nfs_fsinfo *info)
2168{
2169 struct nfs4_exception exception = { };
2170 int err;
2171 do {
2172 err = nfs4_handle_exception(server,
2173 _nfs4_lookup_root(server, fhandle, info),
2174 &exception);
2175 } while (exception.retry);
2176 return err;
2177}
2178
David Howells54ceac42006-08-22 20:06:13 -04002179/*
2180 * get the file handle for the "/" directory on the server
2181 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002183 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 int status;
2186
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 if (status == 0)
2189 status = nfs4_server_capabilities(server, fhandle);
2190 if (status == 0)
2191 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002192 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193}
2194
Manoj Naik6b97fd32006-06-09 09:34:29 -04002195/*
2196 * Get locations and (maybe) other attributes of a referral.
2197 * Note that we'll actually follow the referral later when
2198 * we detect fsid mismatch in inode revalidation
2199 */
Trond Myklebust56659e92007-07-17 21:52:39 -04002200static 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 -04002201{
2202 int status = -ENOMEM;
2203 struct page *page = NULL;
2204 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002205
2206 page = alloc_page(GFP_KERNEL);
2207 if (page == NULL)
2208 goto out;
2209 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2210 if (locations == NULL)
2211 goto out;
2212
Trond Myklebustc228fd32007-01-13 02:28:11 -05002213 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002214 if (status != 0)
2215 goto out;
2216 /* Make sure server returned a different fsid for the referral */
2217 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07002218 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 -04002219 status = -EIO;
2220 goto out;
2221 }
2222
2223 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2224 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2225 if (!fattr->mode)
2226 fattr->mode = S_IFDIR;
2227 memset(fhandle, 0, sizeof(struct nfs_fh));
2228out:
2229 if (page)
2230 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002231 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002232 return status;
2233}
2234
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2236{
2237 struct nfs4_getattr_arg args = {
2238 .fh = fhandle,
2239 .bitmask = server->attr_bitmask,
2240 };
2241 struct nfs4_getattr_res res = {
2242 .fattr = fattr,
2243 .server = server,
2244 };
2245 struct rpc_message msg = {
2246 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2247 .rpc_argp = &args,
2248 .rpc_resp = &res,
2249 };
2250
Trond Myklebust0e574af2005-10-27 22:12:38 -04002251 nfs_fattr_init(fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002252 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253}
2254
2255static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2256{
2257 struct nfs4_exception exception = { };
2258 int err;
2259 do {
2260 err = nfs4_handle_exception(server,
2261 _nfs4_proc_getattr(server, fhandle, fattr),
2262 &exception);
2263 } while (exception.retry);
2264 return err;
2265}
2266
2267/*
2268 * The file is not closed if it is opened due to the a request to change
2269 * the size of the file. The open call will not be needed once the
2270 * VFS layer lookup-intents are implemented.
2271 *
2272 * Close is called when the inode is destroyed.
2273 * If we haven't opened the file for O_WRONLY, we
2274 * need to in the size_change case to obtain a stateid.
2275 *
2276 * Got race?
2277 * Because OPEN is always done by name in nfsv4, it is
2278 * possible that we opened a different file by the same
2279 * name. We can recognize this race condition, but we
2280 * can't do anything about it besides returning an error.
2281 *
2282 * This will be fixed with VFS changes (lookup-intent).
2283 */
2284static int
2285nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2286 struct iattr *sattr)
2287{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002288 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002289 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002290 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 int status;
2292
Trond Myklebust0e574af2005-10-27 22:12:38 -04002293 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
Trond Myklebustd5308382005-11-04 15:33:38 -05002295 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002296 if (sattr->ia_valid & ATTR_FILE) {
2297 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002298
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002299 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002300 if (ctx) {
2301 cred = ctx->cred;
2302 state = ctx->state;
2303 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002304 }
2305
2306 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002307 if (status == 0)
2308 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 return status;
2310}
2311
Trond Myklebust56659e92007-07-17 21:52:39 -04002312static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2313 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04002314 struct nfs_fattr *fattr)
2315{
2316 int status;
2317 struct nfs4_lookup_arg args = {
2318 .bitmask = server->attr_bitmask,
2319 .dir_fh = dirfh,
2320 .name = name,
2321 };
2322 struct nfs4_lookup_res res = {
2323 .server = server,
2324 .fattr = fattr,
2325 .fh = fhandle,
2326 };
2327 struct rpc_message msg = {
2328 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2329 .rpc_argp = &args,
2330 .rpc_resp = &res,
2331 };
2332
2333 nfs_fattr_init(fattr);
2334
2335 dprintk("NFS call lookupfh %s\n", name->name);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002336 status = nfs4_call_sync(server, &msg, &args, &res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002337 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002338 return status;
2339}
2340
2341static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2342 struct qstr *name, struct nfs_fh *fhandle,
2343 struct nfs_fattr *fattr)
2344{
2345 struct nfs4_exception exception = { };
2346 int err;
2347 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04002348 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2349 /* FIXME: !!!! */
2350 if (err == -NFS4ERR_MOVED) {
2351 err = -EREMOTE;
2352 break;
2353 }
2354 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002355 } while (exception.retry);
2356 return err;
2357}
2358
Trond Myklebust56659e92007-07-17 21:52:39 -04002359static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2361{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002362 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
2364 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002365 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002366 if (status == -NFS4ERR_MOVED)
2367 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 dprintk("NFS reply lookup: %d\n", status);
2369 return status;
2370}
2371
2372static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2373{
2374 struct nfs4_exception exception = { };
2375 int err;
2376 do {
2377 err = nfs4_handle_exception(NFS_SERVER(dir),
2378 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2379 &exception);
2380 } while (exception.retry);
2381 return err;
2382}
2383
2384static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2385{
Trond Myklebust76b32992007-08-10 17:45:11 -04002386 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 struct nfs4_accessargs args = {
2388 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002389 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002391 struct nfs4_accessres res = {
2392 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002393 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 struct rpc_message msg = {
2395 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2396 .rpc_argp = &args,
2397 .rpc_resp = &res,
2398 .rpc_cred = entry->cred,
2399 };
2400 int mode = entry->mask;
2401 int status;
2402
2403 /*
2404 * Determine which access bits we want to ask for...
2405 */
2406 if (mode & MAY_READ)
2407 args.access |= NFS4_ACCESS_READ;
2408 if (S_ISDIR(inode->i_mode)) {
2409 if (mode & MAY_WRITE)
2410 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2411 if (mode & MAY_EXEC)
2412 args.access |= NFS4_ACCESS_LOOKUP;
2413 } else {
2414 if (mode & MAY_WRITE)
2415 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2416 if (mode & MAY_EXEC)
2417 args.access |= NFS4_ACCESS_EXECUTE;
2418 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002419
2420 res.fattr = nfs_alloc_fattr();
2421 if (res.fattr == NULL)
2422 return -ENOMEM;
2423
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002424 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 if (!status) {
2426 entry->mask = 0;
2427 if (res.access & NFS4_ACCESS_READ)
2428 entry->mask |= MAY_READ;
2429 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2430 entry->mask |= MAY_WRITE;
2431 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2432 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002433 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002435 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 return status;
2437}
2438
2439static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2440{
2441 struct nfs4_exception exception = { };
2442 int err;
2443 do {
2444 err = nfs4_handle_exception(NFS_SERVER(inode),
2445 _nfs4_proc_access(inode, entry),
2446 &exception);
2447 } while (exception.retry);
2448 return err;
2449}
2450
2451/*
2452 * TODO: For the time being, we don't try to get any attributes
2453 * along with any of the zero-copy operations READ, READDIR,
2454 * READLINK, WRITE.
2455 *
2456 * In the case of the first three, we want to put the GETATTR
2457 * after the read-type operation -- this is because it is hard
2458 * to predict the length of a GETATTR response in v4, and thus
2459 * align the READ data correctly. This means that the GETATTR
2460 * may end up partially falling into the page cache, and we should
2461 * shift it into the 'tail' of the xdr_buf before processing.
2462 * To do this efficiently, we need to know the total length
2463 * of data received, which doesn't seem to be available outside
2464 * of the RPC layer.
2465 *
2466 * In the case of WRITE, we also want to put the GETATTR after
2467 * the operation -- in this case because we want to make sure
2468 * we get the post-operation mtime and size. This means that
2469 * we can't use xdr_encode_pages() as written: we need a variant
2470 * of it which would leave room in the 'tail' iovec.
2471 *
2472 * Both of these changes to the XDR layer would in fact be quite
2473 * minor, but I decided to leave them for a subsequent patch.
2474 */
2475static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2476 unsigned int pgbase, unsigned int pglen)
2477{
2478 struct nfs4_readlink args = {
2479 .fh = NFS_FH(inode),
2480 .pgbase = pgbase,
2481 .pglen = pglen,
2482 .pages = &page,
2483 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002484 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 struct rpc_message msg = {
2486 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2487 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002488 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 };
2490
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002491 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492}
2493
2494static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2495 unsigned int pgbase, unsigned int pglen)
2496{
2497 struct nfs4_exception exception = { };
2498 int err;
2499 do {
2500 err = nfs4_handle_exception(NFS_SERVER(inode),
2501 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2502 &exception);
2503 } while (exception.retry);
2504 return err;
2505}
2506
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507/*
2508 * Got race?
2509 * We will need to arrange for the VFS layer to provide an atomic open.
2510 * Until then, this create/open method is prone to inefficiency and race
2511 * conditions due to the lookup, create, and open VFS calls from sys_open()
2512 * placed on the wire.
2513 *
2514 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2515 * The file will be opened again in the subsequent VFS open call
2516 * (nfs4_proc_file_open).
2517 *
2518 * The open for read will just hang around to be used by any process that
2519 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2520 */
2521
2522static int
2523nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002524 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525{
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002526 struct path my_path = {
Trond Myklebustad389da2007-06-05 12:30:00 -04002527 .dentry = dentry,
2528 };
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002529 struct path *path = &my_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002531 struct rpc_cred *cred = NULL;
2532 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 int status = 0;
2534
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002535 if (ctx != NULL) {
2536 cred = ctx->cred;
2537 path = &ctx->path;
2538 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002540 sattr->ia_mode &= ~current_umask();
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002541 state = nfs4_do_open(dir, path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002542 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 if (IS_ERR(state)) {
2544 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002545 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002547 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002548 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002549 if (ctx != NULL)
2550 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002551 else
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002552 nfs4_close_sync(path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553out:
2554 return status;
2555}
2556
2557static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2558{
Trond Myklebust16e42952005-10-27 22:12:44 -04002559 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002560 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002562 .name.len = name->len,
2563 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002564 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002566 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002567 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002568 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002570 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2571 .rpc_argp = &args,
2572 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002574 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
Trond Myklebustd3468902010-04-16 16:22:50 -04002576 res.dir_attr = nfs_alloc_fattr();
2577 if (res.dir_attr == NULL)
2578 goto out;
2579
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002580 status = nfs4_call_sync(server, &msg, &args, &res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002581 if (status == 0) {
2582 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002583 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002584 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002585 nfs_free_fattr(res.dir_attr);
2586out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 return status;
2588}
2589
2590static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2591{
2592 struct nfs4_exception exception = { };
2593 int err;
2594 do {
2595 err = nfs4_handle_exception(NFS_SERVER(dir),
2596 _nfs4_proc_remove(dir, name),
2597 &exception);
2598 } while (exception.retry);
2599 return err;
2600}
2601
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002602static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002604 struct nfs_server *server = NFS_SERVER(dir);
2605 struct nfs_removeargs *args = msg->rpc_argp;
2606 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607
Trond Myklebusta65318b2009-03-11 14:10:28 -04002608 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002609 res->server = server;
Benny Halevydfb4f3092010-09-24 09:17:01 -04002610 res->seq_res.sr_slot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612}
2613
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002614static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002616 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2617
Trond Myklebust14516c32010-07-31 14:29:06 -04002618 if (!nfs4_sequence_done(task, &res->seq_res))
2619 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002620 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002621 return 0;
2622 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002623 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002624 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625}
2626
Jeff Laytond3d41522010-09-17 17:31:57 -04002627static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2628{
2629 struct nfs_server *server = NFS_SERVER(dir);
2630 struct nfs_renameargs *arg = msg->rpc_argp;
2631 struct nfs_renameres *res = msg->rpc_resp;
2632
2633 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2634 arg->bitmask = server->attr_bitmask;
2635 res->server = server;
2636}
2637
2638static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2639 struct inode *new_dir)
2640{
2641 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2642
2643 if (!nfs4_sequence_done(task, &res->seq_res))
2644 return 0;
2645 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2646 return 0;
2647
2648 update_changeattr(old_dir, &res->old_cinfo);
2649 nfs_post_op_update_inode(old_dir, res->old_fattr);
2650 update_changeattr(new_dir, &res->new_cinfo);
2651 nfs_post_op_update_inode(new_dir, res->new_fattr);
2652 return 1;
2653}
2654
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2656 struct inode *new_dir, struct qstr *new_name)
2657{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002658 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002659 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 .old_dir = NFS_FH(old_dir),
2661 .new_dir = NFS_FH(new_dir),
2662 .old_name = old_name,
2663 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002664 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002666 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002667 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002668 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 struct rpc_message msg = {
2670 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2671 .rpc_argp = &arg,
2672 .rpc_resp = &res,
2673 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002674 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
Trond Myklebust011fff72010-04-16 16:22:49 -04002676 res.old_fattr = nfs_alloc_fattr();
2677 res.new_fattr = nfs_alloc_fattr();
2678 if (res.old_fattr == NULL || res.new_fattr == NULL)
2679 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680
Trond Myklebust011fff72010-04-16 16:22:49 -04002681 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 if (!status) {
2683 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002684 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002686 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002688out:
2689 nfs_free_fattr(res.new_fattr);
2690 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 return status;
2692}
2693
2694static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2695 struct inode *new_dir, struct qstr *new_name)
2696{
2697 struct nfs4_exception exception = { };
2698 int err;
2699 do {
2700 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2701 _nfs4_proc_rename(old_dir, old_name,
2702 new_dir, new_name),
2703 &exception);
2704 } while (exception.retry);
2705 return err;
2706}
2707
2708static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2709{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002710 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 struct nfs4_link_arg arg = {
2712 .fh = NFS_FH(inode),
2713 .dir_fh = NFS_FH(dir),
2714 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002715 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002717 struct nfs4_link_res res = {
2718 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002719 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 struct rpc_message msg = {
2721 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2722 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002723 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002725 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726
Trond Myklebust136f2622010-04-16 16:22:49 -04002727 res.fattr = nfs_alloc_fattr();
2728 res.dir_attr = nfs_alloc_fattr();
2729 if (res.fattr == NULL || res.dir_attr == NULL)
2730 goto out;
2731
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002732 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002733 if (!status) {
2734 update_changeattr(dir, &res.cinfo);
2735 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002736 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002737 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002738out:
2739 nfs_free_fattr(res.dir_attr);
2740 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 return status;
2742}
2743
2744static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2745{
2746 struct nfs4_exception exception = { };
2747 int err;
2748 do {
2749 err = nfs4_handle_exception(NFS_SERVER(inode),
2750 _nfs4_proc_link(inode, dir, name),
2751 &exception);
2752 } while (exception.retry);
2753 return err;
2754}
2755
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002756struct nfs4_createdata {
2757 struct rpc_message msg;
2758 struct nfs4_create_arg arg;
2759 struct nfs4_create_res res;
2760 struct nfs_fh fh;
2761 struct nfs_fattr fattr;
2762 struct nfs_fattr dir_fattr;
2763};
2764
2765static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2766 struct qstr *name, struct iattr *sattr, u32 ftype)
2767{
2768 struct nfs4_createdata *data;
2769
2770 data = kzalloc(sizeof(*data), GFP_KERNEL);
2771 if (data != NULL) {
2772 struct nfs_server *server = NFS_SERVER(dir);
2773
2774 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2775 data->msg.rpc_argp = &data->arg;
2776 data->msg.rpc_resp = &data->res;
2777 data->arg.dir_fh = NFS_FH(dir);
2778 data->arg.server = server;
2779 data->arg.name = name;
2780 data->arg.attrs = sattr;
2781 data->arg.ftype = ftype;
2782 data->arg.bitmask = server->attr_bitmask;
2783 data->res.server = server;
2784 data->res.fh = &data->fh;
2785 data->res.fattr = &data->fattr;
2786 data->res.dir_fattr = &data->dir_fattr;
2787 nfs_fattr_init(data->res.fattr);
2788 nfs_fattr_init(data->res.dir_fattr);
2789 }
2790 return data;
2791}
2792
2793static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2794{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002795 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2796 &data->arg, &data->res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002797 if (status == 0) {
2798 update_changeattr(dir, &data->res.dir_cinfo);
2799 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2800 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2801 }
2802 return status;
2803}
2804
2805static void nfs4_free_createdata(struct nfs4_createdata *data)
2806{
2807 kfree(data);
2808}
2809
Chuck Lever4f390c12006-08-22 20:06:22 -04002810static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002811 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002813 struct nfs4_createdata *data;
2814 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815
Chuck Lever94a6d752006-08-22 20:06:23 -04002816 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002817 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002818
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002819 status = -ENOMEM;
2820 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2821 if (data == NULL)
2822 goto out;
2823
2824 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2825 data->arg.u.symlink.pages = &page;
2826 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002828 status = nfs4_do_create(dir, dentry, data);
2829
2830 nfs4_free_createdata(data);
2831out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 return status;
2833}
2834
Chuck Lever4f390c12006-08-22 20:06:22 -04002835static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002836 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837{
2838 struct nfs4_exception exception = { };
2839 int err;
2840 do {
2841 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002842 _nfs4_proc_symlink(dir, dentry, page,
2843 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 &exception);
2845 } while (exception.retry);
2846 return err;
2847}
2848
2849static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2850 struct iattr *sattr)
2851{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002852 struct nfs4_createdata *data;
2853 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002855 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2856 if (data == NULL)
2857 goto out;
2858
2859 status = nfs4_do_create(dir, dentry, data);
2860
2861 nfs4_free_createdata(data);
2862out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 return status;
2864}
2865
2866static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2867 struct iattr *sattr)
2868{
2869 struct nfs4_exception exception = { };
2870 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002871
2872 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 do {
2874 err = nfs4_handle_exception(NFS_SERVER(dir),
2875 _nfs4_proc_mkdir(dir, dentry, sattr),
2876 &exception);
2877 } while (exception.retry);
2878 return err;
2879}
2880
2881static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002882 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883{
2884 struct inode *dir = dentry->d_inode;
2885 struct nfs4_readdir_arg args = {
2886 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002887 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 .pgbase = 0,
2889 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002890 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04002891 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 };
2893 struct nfs4_readdir_res res;
2894 struct rpc_message msg = {
2895 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2896 .rpc_argp = &args,
2897 .rpc_resp = &res,
2898 .rpc_cred = cred,
2899 };
2900 int status;
2901
Harvey Harrison3110ff82008-05-02 13:42:44 -07002902 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002903 dentry->d_parent->d_name.name,
2904 dentry->d_name.name,
2905 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2907 res.pgbase = args.pgbase;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002908 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
Trond Myklebustac396122010-11-15 20:26:22 -05002909 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05002911 status += args.pgbase;
2912 }
Trond Myklebustc4812992007-09-28 17:11:45 -04002913
2914 nfs_invalidate_atime(dir);
2915
Harvey Harrison3110ff82008-05-02 13:42:44 -07002916 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 return status;
2918}
2919
2920static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002921 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922{
2923 struct nfs4_exception exception = { };
2924 int err;
2925 do {
2926 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2927 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002928 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 &exception);
2930 } while (exception.retry);
2931 return err;
2932}
2933
2934static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2935 struct iattr *sattr, dev_t rdev)
2936{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002937 struct nfs4_createdata *data;
2938 int mode = sattr->ia_mode;
2939 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940
2941 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2942 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002943
2944 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2945 if (data == NULL)
2946 goto out;
2947
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002949 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002951 data->arg.ftype = NF4BLK;
2952 data->arg.u.device.specdata1 = MAJOR(rdev);
2953 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 }
2955 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002956 data->arg.ftype = NF4CHR;
2957 data->arg.u.device.specdata1 = MAJOR(rdev);
2958 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002961 status = nfs4_do_create(dir, dentry, data);
2962
2963 nfs4_free_createdata(data);
2964out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 return status;
2966}
2967
2968static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2969 struct iattr *sattr, dev_t rdev)
2970{
2971 struct nfs4_exception exception = { };
2972 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002973
2974 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 do {
2976 err = nfs4_handle_exception(NFS_SERVER(dir),
2977 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2978 &exception);
2979 } while (exception.retry);
2980 return err;
2981}
2982
2983static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2984 struct nfs_fsstat *fsstat)
2985{
2986 struct nfs4_statfs_arg args = {
2987 .fh = fhandle,
2988 .bitmask = server->attr_bitmask,
2989 };
Benny Halevy24ad1482009-04-01 09:21:56 -04002990 struct nfs4_statfs_res res = {
2991 .fsstat = fsstat,
2992 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 struct rpc_message msg = {
2994 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2995 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04002996 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 };
2998
Trond Myklebust0e574af2005-10-27 22:12:38 -04002999 nfs_fattr_init(fsstat->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003000 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001}
3002
3003static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3004{
3005 struct nfs4_exception exception = { };
3006 int err;
3007 do {
3008 err = nfs4_handle_exception(server,
3009 _nfs4_proc_statfs(server, fhandle, fsstat),
3010 &exception);
3011 } while (exception.retry);
3012 return err;
3013}
3014
3015static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3016 struct nfs_fsinfo *fsinfo)
3017{
3018 struct nfs4_fsinfo_arg args = {
3019 .fh = fhandle,
3020 .bitmask = server->attr_bitmask,
3021 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003022 struct nfs4_fsinfo_res res = {
3023 .fsinfo = fsinfo,
3024 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 struct rpc_message msg = {
3026 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3027 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003028 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 };
3030
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003031 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032}
3033
3034static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3035{
3036 struct nfs4_exception exception = { };
3037 int err;
3038
3039 do {
3040 err = nfs4_handle_exception(server,
3041 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3042 &exception);
3043 } while (exception.retry);
3044 return err;
3045}
3046
3047static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3048{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003049 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3051}
3052
3053static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3054 struct nfs_pathconf *pathconf)
3055{
3056 struct nfs4_pathconf_arg args = {
3057 .fh = fhandle,
3058 .bitmask = server->attr_bitmask,
3059 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003060 struct nfs4_pathconf_res res = {
3061 .pathconf = pathconf,
3062 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 struct rpc_message msg = {
3064 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3065 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003066 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 };
3068
3069 /* None of the pathconf attributes are mandatory to implement */
3070 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3071 memset(pathconf, 0, sizeof(*pathconf));
3072 return 0;
3073 }
3074
Trond Myklebust0e574af2005-10-27 22:12:38 -04003075 nfs_fattr_init(pathconf->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003076 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077}
3078
3079static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3080 struct nfs_pathconf *pathconf)
3081{
3082 struct nfs4_exception exception = { };
3083 int err;
3084
3085 do {
3086 err = nfs4_handle_exception(server,
3087 _nfs4_proc_pathconf(server, fhandle, pathconf),
3088 &exception);
3089 } while (exception.retry);
3090 return err;
3091}
3092
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003093static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094{
Trond Myklebustec06c092006-03-20 13:44:27 -05003095 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003097 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003098 nfs_restart_rpc(task, server->nfs_client);
Trond Myklebustec06c092006-03-20 13:44:27 -05003099 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003101
3102 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003104 renew_lease(server, data->timestamp);
3105 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106}
3107
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003108static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3109{
3110
3111 dprintk("--> %s\n", __func__);
3112
3113 if (!nfs4_sequence_done(task, &data->res.seq_res))
3114 return -EAGAIN;
3115
3116 return data->read_done_cb(task, data);
3117}
3118
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003119static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003122 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003123 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124}
3125
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003126/* Reset the the nfs_read_data to send the read to the MDS. */
3127void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3128{
3129 dprintk("%s Reset task for i/o through\n", __func__);
3130 put_lseg(data->lseg);
3131 data->lseg = NULL;
3132 /* offsets will differ in the dense stripe case */
3133 data->args.offset = data->mds_offset;
3134 data->ds_clp = NULL;
3135 data->args.fh = NFS_FH(data->inode);
3136 data->read_done_cb = nfs4_read_done_cb;
3137 task->tk_ops = data->mds_ops;
3138 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3139}
3140EXPORT_SYMBOL_GPL(nfs4_reset_read);
3141
Fred Isamanb029bc92011-03-03 15:13:42 +00003142static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 struct inode *inode = data->inode;
3145
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003146 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003147 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003148 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003150 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003152 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003153 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003154 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155}
3156
Fred Isamanb029bc92011-03-03 15:13:42 +00003157static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3158{
3159 if (!nfs4_sequence_done(task, &data->res.seq_res))
3160 return -EAGAIN;
3161 return data->write_done_cb(task, data);
3162}
3163
Fred Isamana69aef12011-03-03 15:13:47 +00003164/* Reset the the nfs_write_data to send the write to the MDS. */
3165void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3166{
3167 dprintk("%s Reset task for i/o through\n", __func__);
3168 put_lseg(data->lseg);
3169 data->lseg = NULL;
3170 data->ds_clp = NULL;
3171 data->write_done_cb = nfs4_write_done_cb;
3172 data->args.fh = NFS_FH(data->inode);
3173 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3174 data->args.offset = data->mds_offset;
3175 data->res.fattr = &data->fattr;
3176 task->tk_ops = data->mds_ops;
3177 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3178}
3179EXPORT_SYMBOL_GPL(nfs4_reset_write);
3180
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003181static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003183 struct nfs_server *server = NFS_SERVER(data->inode);
3184
Fred Isaman7ffd1062011-03-03 15:13:46 +00003185 if (data->lseg) {
3186 data->args.bitmask = NULL;
3187 data->res.fattr = NULL;
3188 } else
3189 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isamanb029bc92011-03-03 15:13:42 +00003190 if (!data->write_done_cb)
3191 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003192 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 data->timestamp = jiffies;
3194
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003195 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196}
3197
Fred Isaman5f452432011-03-23 13:27:46 +00003198static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003201
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003202 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003203 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003204 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003206 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003207 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208}
3209
Fred Isaman5f452432011-03-23 13:27:46 +00003210static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3211{
3212 if (!nfs4_sequence_done(task, &data->res.seq_res))
3213 return -EAGAIN;
3214 return data->write_done_cb(task, data);
3215}
3216
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003217static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003219 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220
Trond Myklebusta65318b2009-03-11 14:10:28 -04003221 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isaman5f452432011-03-23 13:27:46 +00003222 if (!data->write_done_cb)
3223 data->write_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003224 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003225 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226}
3227
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003228struct nfs4_renewdata {
3229 struct nfs_client *client;
3230 unsigned long timestamp;
3231};
3232
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233/*
3234 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3235 * standalone procedure for queueing an asynchronous RENEW.
3236 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003237static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003238{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003239 struct nfs4_renewdata *data = calldata;
3240 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003241
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003242 if (atomic_read(&clp->cl_count) > 1)
3243 nfs4_schedule_state_renewal(clp);
3244 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003245 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003246}
3247
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003248static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003250 struct nfs4_renewdata *data = calldata;
3251 struct nfs_client *clp = data->client;
3252 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253
3254 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003255 /* Unless we're shutting down, schedule state recovery! */
3256 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003257 nfs4_schedule_lease_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 return;
3259 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003260 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261}
3262
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003263static const struct rpc_call_ops nfs4_renew_ops = {
3264 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003265 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003266};
3267
David Howellsadfa6f92006-08-22 20:06:08 -04003268int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269{
3270 struct rpc_message msg = {
3271 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3272 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003273 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003275 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003277 if (!atomic_inc_not_zero(&clp->cl_count))
3278 return -EIO;
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003279 data = kmalloc(sizeof(*data), GFP_KERNEL);
3280 if (data == NULL)
3281 return -ENOMEM;
3282 data->client = clp;
3283 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003285 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286}
3287
David Howellsadfa6f92006-08-22 20:06:08 -04003288int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289{
3290 struct rpc_message msg = {
3291 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3292 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003293 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 };
3295 unsigned long now = jiffies;
3296 int status;
3297
3298 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3299 if (status < 0)
3300 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003301 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 return 0;
3303}
3304
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003305static inline int nfs4_server_supports_acls(struct nfs_server *server)
3306{
3307 return (server->caps & NFS_CAP_ACLS)
3308 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3309 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3310}
3311
3312/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3313 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3314 * the stack.
3315 */
3316#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3317
3318static void buf_to_pages(const void *buf, size_t buflen,
3319 struct page **pages, unsigned int *pgbase)
3320{
3321 const void *p = buf;
3322
3323 *pgbase = offset_in_page(buf);
3324 p -= *pgbase;
3325 while (p < buf + buflen) {
3326 *(pages++) = virt_to_page(p);
3327 p += PAGE_CACHE_SIZE;
3328 }
3329}
3330
Neil Hormane9e3d722011-03-04 19:26:03 -05003331static int buf_to_pages_noslab(const void *buf, size_t buflen,
3332 struct page **pages, unsigned int *pgbase)
3333{
3334 struct page *newpage, **spages;
3335 int rc = 0;
3336 size_t len;
3337 spages = pages;
3338
3339 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003340 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003341 newpage = alloc_page(GFP_KERNEL);
3342
3343 if (newpage == NULL)
3344 goto unwind;
3345 memcpy(page_address(newpage), buf, len);
3346 buf += len;
3347 buflen -= len;
3348 *pages++ = newpage;
3349 rc++;
3350 } while (buflen != 0);
3351
3352 return rc;
3353
3354unwind:
3355 for(; rc > 0; rc--)
3356 __free_page(spages[rc-1]);
3357 return -ENOMEM;
3358}
3359
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003360struct nfs4_cached_acl {
3361 int cached;
3362 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003363 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003364};
3365
3366static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003367{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003368 struct nfs_inode *nfsi = NFS_I(inode);
3369
3370 spin_lock(&inode->i_lock);
3371 kfree(nfsi->nfs4_acl);
3372 nfsi->nfs4_acl = acl;
3373 spin_unlock(&inode->i_lock);
3374}
3375
3376static void nfs4_zap_acl_attr(struct inode *inode)
3377{
3378 nfs4_set_cached_acl(inode, NULL);
3379}
3380
3381static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3382{
3383 struct nfs_inode *nfsi = NFS_I(inode);
3384 struct nfs4_cached_acl *acl;
3385 int ret = -ENOENT;
3386
3387 spin_lock(&inode->i_lock);
3388 acl = nfsi->nfs4_acl;
3389 if (acl == NULL)
3390 goto out;
3391 if (buf == NULL) /* user is just asking for length */
3392 goto out_len;
3393 if (acl->cached == 0)
3394 goto out;
3395 ret = -ERANGE; /* see getxattr(2) man page */
3396 if (acl->len > buflen)
3397 goto out;
3398 memcpy(buf, acl->data, acl->len);
3399out_len:
3400 ret = acl->len;
3401out:
3402 spin_unlock(&inode->i_lock);
3403 return ret;
3404}
3405
3406static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3407{
3408 struct nfs4_cached_acl *acl;
3409
3410 if (buf && acl_len <= PAGE_SIZE) {
3411 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3412 if (acl == NULL)
3413 goto out;
3414 acl->cached = 1;
3415 memcpy(acl->data, buf, acl_len);
3416 } else {
3417 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3418 if (acl == NULL)
3419 goto out;
3420 acl->cached = 0;
3421 }
3422 acl->len = acl_len;
3423out:
3424 nfs4_set_cached_acl(inode, acl);
3425}
3426
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003427static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003428{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003429 struct page *pages[NFS4ACL_MAXPAGES];
3430 struct nfs_getaclargs args = {
3431 .fh = NFS_FH(inode),
3432 .acl_pages = pages,
3433 .acl_len = buflen,
3434 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003435 struct nfs_getaclres res = {
3436 .acl_len = buflen,
3437 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003438 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003439 struct rpc_message msg = {
3440 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3441 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003442 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003443 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003444 struct page *localpage = NULL;
3445 int ret;
3446
3447 if (buflen < PAGE_SIZE) {
3448 /* As long as we're doing a round trip to the server anyway,
3449 * let's be prepared for a page of acl data. */
3450 localpage = alloc_page(GFP_KERNEL);
3451 resp_buf = page_address(localpage);
3452 if (localpage == NULL)
3453 return -ENOMEM;
3454 args.acl_pages[0] = localpage;
3455 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003456 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003457 } else {
3458 resp_buf = buf;
3459 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3460 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003461 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003462 if (ret)
3463 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003464 if (res.acl_len > args.acl_len)
3465 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003466 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003467 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003468 if (buf) {
3469 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003470 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003471 goto out_free;
3472 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003473 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003474 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003475 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003476out_free:
3477 if (localpage)
3478 __free_page(localpage);
3479 return ret;
3480}
3481
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003482static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3483{
3484 struct nfs4_exception exception = { };
3485 ssize_t ret;
3486 do {
3487 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3488 if (ret >= 0)
3489 break;
3490 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3491 } while (exception.retry);
3492 return ret;
3493}
3494
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003495static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3496{
3497 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003498 int ret;
3499
3500 if (!nfs4_server_supports_acls(server))
3501 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003502 ret = nfs_revalidate_inode(server, inode);
3503 if (ret < 0)
3504 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003505 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3506 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003507 ret = nfs4_read_cached_acl(inode, buf, buflen);
3508 if (ret != -ENOENT)
3509 return ret;
3510 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003511}
3512
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003513static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003514{
3515 struct nfs_server *server = NFS_SERVER(inode);
3516 struct page *pages[NFS4ACL_MAXPAGES];
3517 struct nfs_setaclargs arg = {
3518 .fh = NFS_FH(inode),
3519 .acl_pages = pages,
3520 .acl_len = buflen,
3521 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003522 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003523 struct rpc_message msg = {
3524 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3525 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003526 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003527 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003528 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003529
3530 if (!nfs4_server_supports_acls(server))
3531 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003532 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3533 if (i < 0)
3534 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003535 nfs_inode_return_delegation(inode);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003536 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003537
3538 /*
3539 * Free each page after tx, so the only ref left is
3540 * held by the network stack
3541 */
3542 for (; i > 0; i--)
3543 put_page(pages[i-1]);
3544
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003545 /*
3546 * Acl update can result in inode attribute update.
3547 * so mark the attribute cache invalid.
3548 */
3549 spin_lock(&inode->i_lock);
3550 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3551 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003552 nfs_access_zap_cache(inode);
3553 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003554 return ret;
3555}
3556
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003557static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3558{
3559 struct nfs4_exception exception = { };
3560 int err;
3561 do {
3562 err = nfs4_handle_exception(NFS_SERVER(inode),
3563 __nfs4_proc_set_acl(inode, buf, buflen),
3564 &exception);
3565 } while (exception.retry);
3566 return err;
3567}
3568
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003570nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003572 struct nfs_client *clp = server->nfs_client;
3573
3574 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 return 0;
3576 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003577 case -NFS4ERR_ADMIN_REVOKED:
3578 case -NFS4ERR_BAD_STATEID:
3579 case -NFS4ERR_OPENMODE:
3580 if (state == NULL)
3581 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003582 nfs4_schedule_stateid_recovery(server, state);
3583 goto wait_on_recovery;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003585 case -NFS4ERR_STALE_CLIENTID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 case -NFS4ERR_EXPIRED:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003587 nfs4_schedule_lease_recovery(clp);
3588 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003589#if defined(CONFIG_NFS_V4_1)
3590 case -NFS4ERR_BADSESSION:
3591 case -NFS4ERR_BADSLOT:
3592 case -NFS4ERR_BAD_HIGH_SLOT:
3593 case -NFS4ERR_DEADSESSION:
3594 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3595 case -NFS4ERR_SEQ_FALSE_RETRY:
3596 case -NFS4ERR_SEQ_MISORDERED:
3597 dprintk("%s ERROR %d, Reset session\n", __func__,
3598 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003599 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -04003600 task->tk_status = 0;
3601 return -EAGAIN;
3602#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003604 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003605 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003606 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3608 task->tk_status = 0;
3609 return -EAGAIN;
3610 case -NFS4ERR_OLD_STATEID:
3611 task->tk_status = 0;
3612 return -EAGAIN;
3613 }
3614 task->tk_status = nfs4_map_errors(task->tk_status);
3615 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003616wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003617 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003618 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3619 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3620 task->tk_status = 0;
3621 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622}
3623
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003624int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3625 unsigned short port, struct rpc_cred *cred,
3626 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627{
3628 nfs4_verifier sc_verifier;
3629 struct nfs4_setclientid setclientid = {
3630 .sc_verifier = &sc_verifier,
3631 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003632 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 };
3634 struct rpc_message msg = {
3635 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3636 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003637 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003638 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639 };
Al Virobc4785c2006-10-19 23:28:51 -07003640 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 int loop = 0;
3642 int status;
3643
Al Virobc4785c2006-10-19 23:28:51 -07003644 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3646 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3647
3648 for(;;) {
3649 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003650 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003651 clp->cl_ipaddr,
3652 rpc_peeraddr2str(clp->cl_rpcclient,
3653 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003654 rpc_peeraddr2str(clp->cl_rpcclient,
3655 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003656 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 clp->cl_id_uniquifier);
3658 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003659 sizeof(setclientid.sc_netid),
3660 rpc_peeraddr2str(clp->cl_rpcclient,
3661 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003663 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 clp->cl_ipaddr, port >> 8, port & 255);
3665
3666 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3667 if (status != -NFS4ERR_CLID_INUSE)
3668 break;
3669 if (signalled())
3670 break;
3671 if (loop++ & 1)
J. Bruce Fields611c96c2010-12-13 19:05:46 -05003672 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 else
3674 if (++clp->cl_id_uniquifier == 0)
3675 break;
3676 }
3677 return status;
3678}
3679
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003680static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3681 struct nfs4_setclientid_res *arg,
3682 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683{
3684 struct nfs_fsinfo fsinfo;
3685 struct rpc_message msg = {
3686 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003687 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003689 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 };
3691 unsigned long now;
3692 int status;
3693
3694 now = jiffies;
3695 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3696 if (status == 0) {
3697 spin_lock(&clp->cl_lock);
3698 clp->cl_lease_time = fsinfo.lease_time * HZ;
3699 clp->cl_last_renewal = now;
3700 spin_unlock(&clp->cl_lock);
3701 }
3702 return status;
3703}
3704
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003705int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3706 struct nfs4_setclientid_res *arg,
3707 struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003708{
Benny Halevy77e03672008-06-24 20:25:57 +03003709 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003710 int err;
3711 do {
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003712 err = _nfs4_proc_setclientid_confirm(clp, arg, cred);
Trond Myklebust51581f32006-03-20 13:44:47 -05003713 switch (err) {
3714 case 0:
3715 return err;
3716 case -NFS4ERR_RESOURCE:
3717 /* The IBM lawyers misread another document! */
3718 case -NFS4ERR_DELAY:
3719 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3720 }
3721 } while (err == 0);
3722 return err;
3723}
3724
Trond Myklebustfe650402006-01-03 09:55:18 +01003725struct nfs4_delegreturndata {
3726 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003727 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003728 struct nfs_fh fh;
3729 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003730 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003731 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003732 int rpc_status;
3733};
3734
Trond Myklebustfe650402006-01-03 09:55:18 +01003735static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3736{
3737 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003738
Trond Myklebust14516c32010-07-31 14:29:06 -04003739 if (!nfs4_sequence_done(task, &data->res.seq_res))
3740 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003741
Ricardo Labiaga79708862009-12-07 09:23:21 -05003742 switch (task->tk_status) {
3743 case -NFS4ERR_STALE_STATEID:
3744 case -NFS4ERR_EXPIRED:
3745 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003746 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003747 break;
3748 default:
3749 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3750 -EAGAIN) {
3751 nfs_restart_rpc(task, data->res.server->nfs_client);
3752 return;
3753 }
3754 }
3755 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003756}
3757
3758static void nfs4_delegreturn_release(void *calldata)
3759{
Trond Myklebustfe650402006-01-03 09:55:18 +01003760 kfree(calldata);
3761}
3762
Andy Adamson938e1012009-04-01 09:22:28 -04003763#if defined(CONFIG_NFS_V4_1)
3764static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3765{
3766 struct nfs4_delegreturndata *d_data;
3767
3768 d_data = (struct nfs4_delegreturndata *)data;
3769
Trond Myklebust035168a2010-06-16 09:52:26 -04003770 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003771 &d_data->args.seq_args,
3772 &d_data->res.seq_res, 1, task))
3773 return;
3774 rpc_call_start(task);
3775}
3776#endif /* CONFIG_NFS_V4_1 */
3777
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003778static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003779#if defined(CONFIG_NFS_V4_1)
3780 .rpc_call_prepare = nfs4_delegreturn_prepare,
3781#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003782 .rpc_call_done = nfs4_delegreturn_done,
3783 .rpc_release = nfs4_delegreturn_release,
3784};
3785
Trond Myklebuste6f81072008-01-24 18:14:34 -05003786static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003787{
3788 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003789 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003790 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003791 struct rpc_message msg = {
3792 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3793 .rpc_cred = cred,
3794 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003795 struct rpc_task_setup task_setup_data = {
3796 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003797 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003798 .callback_ops = &nfs4_delegreturn_ops,
3799 .flags = RPC_TASK_ASYNC,
3800 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003801 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003802
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003803 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003804 if (data == NULL)
3805 return -ENOMEM;
3806 data->args.fhandle = &data->fh;
3807 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003808 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003809 nfs_copy_fh(&data->fh, NFS_FH(inode));
3810 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003811 data->res.fattr = &data->fattr;
3812 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003813 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003814 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003815 data->rpc_status = 0;
3816
Trond Myklebustc970aa82007-07-14 15:39:59 -04003817 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05003818 msg.rpc_argp = &data->args;
3819 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003820 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003821 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003822 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003823 if (!issync)
3824 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003825 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003826 if (status != 0)
3827 goto out;
3828 status = data->rpc_status;
3829 if (status != 0)
3830 goto out;
3831 nfs_refresh_inode(inode, &data->fattr);
3832out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003833 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003834 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835}
3836
Trond Myklebuste6f81072008-01-24 18:14:34 -05003837int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838{
3839 struct nfs_server *server = NFS_SERVER(inode);
3840 struct nfs4_exception exception = { };
3841 int err;
3842 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003843 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 switch (err) {
3845 case -NFS4ERR_STALE_STATEID:
3846 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 case 0:
3848 return 0;
3849 }
3850 err = nfs4_handle_exception(server, err, &exception);
3851 } while (exception.retry);
3852 return err;
3853}
3854
3855#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3856#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3857
3858/*
3859 * sleep, with exponential backoff, and retry the LOCK operation.
3860 */
3861static unsigned long
3862nfs4_set_lock_task_retry(unsigned long timeout)
3863{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003864 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 timeout <<= 1;
3866 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3867 return NFS4_LOCK_MAXTIMEOUT;
3868 return timeout;
3869}
3870
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3872{
3873 struct inode *inode = state->inode;
3874 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003875 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003876 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003878 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003880 struct nfs_lockt_res res = {
3881 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 };
3883 struct rpc_message msg = {
3884 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3885 .rpc_argp = &arg,
3886 .rpc_resp = &res,
3887 .rpc_cred = state->owner->so_cred,
3888 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 struct nfs4_lock_state *lsp;
3890 int status;
3891
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003892 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003893 status = nfs4_set_lock_state(state, request);
3894 if (status != 0)
3895 goto out;
3896 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003897 arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05003898 arg.lock_owner.s_dev = server->s_dev;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003899 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003900 switch (status) {
3901 case 0:
3902 request->fl_type = F_UNLCK;
3903 break;
3904 case -NFS4ERR_DENIED:
3905 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003907 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003908out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 return status;
3910}
3911
3912static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3913{
3914 struct nfs4_exception exception = { };
3915 int err;
3916
3917 do {
3918 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3919 _nfs4_proc_getlk(state, cmd, request),
3920 &exception);
3921 } while (exception.retry);
3922 return err;
3923}
3924
3925static int do_vfs_lock(struct file *file, struct file_lock *fl)
3926{
3927 int res = 0;
3928 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3929 case FL_POSIX:
3930 res = posix_lock_file_wait(file, fl);
3931 break;
3932 case FL_FLOCK:
3933 res = flock_lock_file_wait(file, fl);
3934 break;
3935 default:
3936 BUG();
3937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 return res;
3939}
3940
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003941struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003942 struct nfs_locku_args arg;
3943 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003944 struct nfs4_lock_state *lsp;
3945 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003946 struct file_lock fl;
3947 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003948 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003949};
3950
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003951static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3952 struct nfs_open_context *ctx,
3953 struct nfs4_lock_state *lsp,
3954 struct nfs_seqid *seqid)
3955{
3956 struct nfs4_unlockdata *p;
3957 struct inode *inode = lsp->ls_state->inode;
3958
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003959 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003960 if (p == NULL)
3961 return NULL;
3962 p->arg.fh = NFS_FH(inode);
3963 p->arg.fl = &p->fl;
3964 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003965 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003966 p->arg.stateid = &lsp->ls_stateid;
3967 p->lsp = lsp;
3968 atomic_inc(&lsp->ls_count);
3969 /* Ensure we don't close file until we're done freeing locks! */
3970 p->ctx = get_nfs_open_context(ctx);
3971 memcpy(&p->fl, fl, sizeof(p->fl));
3972 p->server = NFS_SERVER(inode);
3973 return p;
3974}
3975
Trond Myklebust06f814a2006-01-03 09:55:07 +01003976static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003977{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003978 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003979 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003980 nfs4_put_lock_state(calldata->lsp);
3981 put_nfs_open_context(calldata->ctx);
3982 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003983}
3984
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003985static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003986{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003987 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003988
Trond Myklebust14516c32010-07-31 14:29:06 -04003989 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3990 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003991 switch (task->tk_status) {
3992 case 0:
3993 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003994 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003995 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003996 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003997 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003998 case -NFS4ERR_BAD_STATEID:
3999 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004000 case -NFS4ERR_STALE_STATEID:
4001 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004002 break;
4003 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004004 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebust0110ee12009-12-07 09:00:24 -05004005 nfs_restart_rpc(task,
Trond Myklebustd61e6122009-12-05 19:32:19 -05004006 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004007 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004008}
4009
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004010static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004011{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004012 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004014 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004015 return;
4016 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004017 /* Note: exit _without_ running nfs4_locku_done */
4018 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004019 return;
4020 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004021 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004022 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004023 &calldata->arg.seq_args,
4024 &calldata->res.seq_res, 1, task))
4025 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004026 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027}
4028
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004029static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004030 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004031 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004032 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004033};
4034
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004035static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4036 struct nfs_open_context *ctx,
4037 struct nfs4_lock_state *lsp,
4038 struct nfs_seqid *seqid)
4039{
4040 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004041 struct rpc_message msg = {
4042 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4043 .rpc_cred = ctx->cred,
4044 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004045 struct rpc_task_setup task_setup_data = {
4046 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004047 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004048 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004049 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004050 .flags = RPC_TASK_ASYNC,
4051 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004052
Frank Filz137d6ac2007-07-09 15:32:29 -07004053 /* Ensure this is an unlock - when canceling a lock, the
4054 * canceled lock is passed in, and it won't be an unlock.
4055 */
4056 fl->fl_type = F_UNLCK;
4057
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004058 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4059 if (data == NULL) {
4060 nfs_free_seqid(seqid);
4061 return ERR_PTR(-ENOMEM);
4062 }
4063
Trond Myklebust1174dd12010-12-21 10:52:24 -05004064 msg.rpc_argp = &data->arg;
4065 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004066 task_setup_data.callback_data = data;
4067 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004068}
4069
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4071{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004072 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004073 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004074 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004075 struct rpc_task *task;
4076 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004077 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078
Trond Myklebust9b073572006-06-29 16:38:34 -04004079 status = nfs4_set_lock_state(state, request);
4080 /* Unlock _before_ we do the RPC call */
4081 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004082 down_read(&nfsi->rwsem);
4083 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4084 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004085 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004086 }
4087 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004088 if (status != 0)
4089 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004090 /* Is this a delegated lock? */
4091 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004092 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004093 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004094 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004095 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004096 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004097 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004098 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004099 status = PTR_ERR(task);
4100 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004101 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004102 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004103 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004104out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004105 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004106 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107}
4108
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004109struct nfs4_lockdata {
4110 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004111 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004112 struct nfs4_lock_state *lsp;
4113 struct nfs_open_context *ctx;
4114 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004115 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004116 int rpc_status;
4117 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004118 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004119};
4120
4121static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004122 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4123 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004124{
4125 struct nfs4_lockdata *p;
4126 struct inode *inode = lsp->ls_state->inode;
4127 struct nfs_server *server = NFS_SERVER(inode);
4128
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004129 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004130 if (p == NULL)
4131 return NULL;
4132
4133 p->arg.fh = NFS_FH(inode);
4134 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004135 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004136 if (p->arg.open_seqid == NULL)
4137 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004138 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004139 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004140 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004141 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004142 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004143 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004144 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004145 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004146 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004147 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004148 atomic_inc(&lsp->ls_count);
4149 p->ctx = get_nfs_open_context(ctx);
4150 memcpy(&p->fl, fl, sizeof(p->fl));
4151 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004152out_free_seqid:
4153 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004154out_free:
4155 kfree(p);
4156 return NULL;
4157}
4158
4159static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4160{
4161 struct nfs4_lockdata *data = calldata;
4162 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163
Harvey Harrison3110ff82008-05-02 13:42:44 -07004164 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004165 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4166 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004167 /* Do we need to do an open_to_lock_owner? */
4168 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004169 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4170 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004171 data->arg.open_stateid = &state->stateid;
4172 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004173 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004174 } else
4175 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004176 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004177 if (nfs4_setup_sequence(data->server,
4178 &data->arg.seq_args,
Andy Adamson66179ef2009-04-01 09:22:22 -04004179 &data->res.seq_res, 1, task))
4180 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004181 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004182 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004183}
4184
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004185static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4186{
4187 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4188 nfs4_lock_prepare(task, calldata);
4189}
4190
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004191static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4192{
4193 struct nfs4_lockdata *data = calldata;
4194
Harvey Harrison3110ff82008-05-02 13:42:44 -07004195 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004196
Trond Myklebust14516c32010-07-31 14:29:06 -04004197 if (!nfs4_sequence_done(task, &data->res.seq_res))
4198 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004199
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004200 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004201 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004202 if (data->rpc_status == 0)
4203 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4204 else
4205 goto out;
4206 }
4207 if (data->rpc_status == 0) {
4208 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4209 sizeof(data->lsp->ls_stateid.data));
4210 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04004211 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004212 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004213out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004214 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004215}
4216
4217static void nfs4_lock_release(void *calldata)
4218{
4219 struct nfs4_lockdata *data = calldata;
4220
Harvey Harrison3110ff82008-05-02 13:42:44 -07004221 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004222 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004223 if (data->cancelled != 0) {
4224 struct rpc_task *task;
4225 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4226 data->arg.lock_seqid);
4227 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004228 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004229 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004230 } else
4231 nfs_free_seqid(data->arg.lock_seqid);
4232 nfs4_put_lock_state(data->lsp);
4233 put_nfs_open_context(data->ctx);
4234 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004235 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004236}
4237
4238static const struct rpc_call_ops nfs4_lock_ops = {
4239 .rpc_call_prepare = nfs4_lock_prepare,
4240 .rpc_call_done = nfs4_lock_done,
4241 .rpc_release = nfs4_lock_release,
4242};
4243
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004244static const struct rpc_call_ops nfs4_recover_lock_ops = {
4245 .rpc_call_prepare = nfs4_recover_lock_prepare,
4246 .rpc_call_done = nfs4_lock_done,
4247 .rpc_release = nfs4_lock_release,
4248};
4249
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004250static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4251{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004252 switch (error) {
4253 case -NFS4ERR_ADMIN_REVOKED:
4254 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004255 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004256 if (new_lock_owner != 0 ||
4257 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004258 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004259 break;
4260 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004261 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004262 case -NFS4ERR_EXPIRED:
4263 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004264 };
4265}
4266
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004267static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004268{
4269 struct nfs4_lockdata *data;
4270 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004271 struct rpc_message msg = {
4272 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4273 .rpc_cred = state->owner->so_cred,
4274 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004275 struct rpc_task_setup task_setup_data = {
4276 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004277 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004278 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004279 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004280 .flags = RPC_TASK_ASYNC,
4281 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004282 int ret;
4283
Harvey Harrison3110ff82008-05-02 13:42:44 -07004284 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004285 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004286 fl->fl_u.nfs4_fl.owner,
4287 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004288 if (data == NULL)
4289 return -ENOMEM;
4290 if (IS_SETLKW(cmd))
4291 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004292 if (recovery_type > NFS_LOCK_NEW) {
4293 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004294 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004295 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4296 }
Trond Myklebust1174dd12010-12-21 10:52:24 -05004297 msg.rpc_argp = &data->arg;
4298 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004299 task_setup_data.callback_data = data;
4300 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004301 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004302 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004303 ret = nfs4_wait_for_completion_rpc_task(task);
4304 if (ret == 0) {
4305 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004306 if (ret)
4307 nfs4_handle_setlk_error(data->server, data->lsp,
4308 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004309 } else
4310 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004311 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004312 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004313 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314}
4315
4316static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4317{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004318 struct nfs_server *server = NFS_SERVER(state->inode);
4319 struct nfs4_exception exception = { };
4320 int err;
4321
4322 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004323 /* Cache the lock if possible... */
4324 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4325 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004326 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004327 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004328 break;
4329 nfs4_handle_exception(server, err, &exception);
4330 } while (exception.retry);
4331 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332}
4333
4334static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4335{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004336 struct nfs_server *server = NFS_SERVER(state->inode);
4337 struct nfs4_exception exception = { };
4338 int err;
4339
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004340 err = nfs4_set_lock_state(state, request);
4341 if (err != 0)
4342 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004343 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004344 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4345 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004346 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004347 switch (err) {
4348 default:
4349 goto out;
4350 case -NFS4ERR_GRACE:
4351 case -NFS4ERR_DELAY:
4352 nfs4_handle_exception(server, err, &exception);
4353 err = 0;
4354 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004355 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004356out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004357 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358}
4359
4360static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4361{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004362 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004363 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004364 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004366 if ((fl_flags & FL_POSIX) &&
4367 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4368 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004369 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004370 status = nfs4_set_lock_state(state, request);
4371 if (status != 0)
4372 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004373 request->fl_flags |= FL_ACCESS;
4374 status = do_vfs_lock(request->fl_file, request);
4375 if (status < 0)
4376 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004377 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004378 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004379 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004380 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004381 request->fl_flags = fl_flags & ~FL_SLEEP;
4382 status = do_vfs_lock(request->fl_file, request);
4383 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004384 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004385 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004386 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004387 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004388 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004389 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004390 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004391 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004392out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004393 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004394out:
4395 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 return status;
4397}
4398
4399static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4400{
4401 struct nfs4_exception exception = { };
4402 int err;
4403
4404 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004405 err = _nfs4_proc_setlk(state, cmd, request);
4406 if (err == -NFS4ERR_DENIED)
4407 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004409 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 } while (exception.retry);
4411 return err;
4412}
4413
4414static int
4415nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4416{
4417 struct nfs_open_context *ctx;
4418 struct nfs4_state *state;
4419 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4420 int status;
4421
4422 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004423 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 state = ctx->state;
4425
4426 if (request->fl_start < 0 || request->fl_end < 0)
4427 return -EINVAL;
4428
Trond Myklebustd9531262009-07-21 19:22:38 -04004429 if (IS_GETLK(cmd)) {
4430 if (state != NULL)
4431 return nfs4_proc_getlk(state, F_GETLK, request);
4432 return 0;
4433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434
4435 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4436 return -EINVAL;
4437
Trond Myklebustd9531262009-07-21 19:22:38 -04004438 if (request->fl_type == F_UNLCK) {
4439 if (state != NULL)
4440 return nfs4_proc_unlck(state, cmd, request);
4441 return 0;
4442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443
Trond Myklebustd9531262009-07-21 19:22:38 -04004444 if (state == NULL)
4445 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 do {
4447 status = nfs4_proc_setlk(state, cmd, request);
4448 if ((status != -EAGAIN) || IS_SETLK(cmd))
4449 break;
4450 timeout = nfs4_set_lock_task_retry(timeout);
4451 status = -ERESTARTSYS;
4452 if (signalled())
4453 break;
4454 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 return status;
4456}
4457
Trond Myklebust888e6942005-11-04 15:38:11 -05004458int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4459{
4460 struct nfs_server *server = NFS_SERVER(state->inode);
4461 struct nfs4_exception exception = { };
4462 int err;
4463
4464 err = nfs4_set_lock_state(state, fl);
4465 if (err != 0)
4466 goto out;
4467 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004468 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004469 switch (err) {
4470 default:
4471 printk(KERN_ERR "%s: unhandled error %d.\n",
4472 __func__, err);
4473 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004474 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004475 goto out;
4476 case -NFS4ERR_EXPIRED:
4477 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004478 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004479 nfs4_schedule_lease_recovery(server->nfs_client);
4480 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004481 case -NFS4ERR_BADSESSION:
4482 case -NFS4ERR_BADSLOT:
4483 case -NFS4ERR_BAD_HIGH_SLOT:
4484 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4485 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004486 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004487 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004488 case -ERESTARTSYS:
4489 /*
4490 * The show must go on: exit, but mark the
4491 * stateid as needing recovery.
4492 */
4493 case -NFS4ERR_ADMIN_REVOKED:
4494 case -NFS4ERR_BAD_STATEID:
4495 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004496 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004497 err = 0;
4498 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004499 case -EKEYEXPIRED:
4500 /*
4501 * User RPCSEC_GSS context has expired.
4502 * We cannot recover this stateid now, so
4503 * skip it and allow recovery thread to
4504 * proceed.
4505 */
4506 err = 0;
4507 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004508 case -ENOMEM:
4509 case -NFS4ERR_DENIED:
4510 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4511 err = 0;
4512 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004513 case -NFS4ERR_DELAY:
4514 break;
4515 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004516 err = nfs4_handle_exception(server, err, &exception);
4517 } while (exception.retry);
4518out:
4519 return err;
4520}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004521
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004522static void nfs4_release_lockowner_release(void *calldata)
4523{
4524 kfree(calldata);
4525}
4526
4527const struct rpc_call_ops nfs4_release_lockowner_ops = {
4528 .rpc_release = nfs4_release_lockowner_release,
4529};
4530
4531void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4532{
4533 struct nfs_server *server = lsp->ls_state->owner->so_server;
4534 struct nfs_release_lockowner_args *args;
4535 struct rpc_message msg = {
4536 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4537 };
4538
4539 if (server->nfs_client->cl_mvops->minor_version != 0)
4540 return;
4541 args = kmalloc(sizeof(*args), GFP_NOFS);
4542 if (!args)
4543 return;
4544 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4545 args->lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004546 args->lock_owner.s_dev = server->s_dev;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004547 msg.rpc_argp = args;
4548 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4549}
4550
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004551#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4552
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004553static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4554 const void *buf, size_t buflen,
4555 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004556{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004557 if (strcmp(key, "") != 0)
4558 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004559
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004560 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004561}
4562
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004563static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4564 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004565{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004566 if (strcmp(key, "") != 0)
4567 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004568
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004569 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004570}
4571
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004572static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4573 size_t list_len, const char *name,
4574 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004575{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004576 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004577
J. Bruce Fields096455a2006-03-20 23:23:42 -05004578 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4579 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004580
4581 if (list && len <= list_len)
4582 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004583 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004584}
4585
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004586static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4587{
4588 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4589 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4590 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4591 return;
4592
4593 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4594 NFS_ATTR_FATTR_NLINK;
4595 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4596 fattr->nlink = 2;
4597}
4598
Trond Myklebust56659e92007-07-17 21:52:39 -04004599int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004600 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004601{
4602 struct nfs_server *server = NFS_SERVER(dir);
4603 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004604 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4605 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004606 };
4607 struct nfs4_fs_locations_arg args = {
4608 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004609 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004610 .page = page,
4611 .bitmask = bitmask,
4612 };
Benny Halevy22958462009-04-01 09:22:02 -04004613 struct nfs4_fs_locations_res res = {
4614 .fs_locations = fs_locations,
4615 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004616 struct rpc_message msg = {
4617 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4618 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004619 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004620 };
4621 int status;
4622
Harvey Harrison3110ff82008-05-02 13:42:44 -07004623 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004624 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004625 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004626 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04004627 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004628 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004629 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004630 return status;
4631}
4632
Andy Adamson557134a2009-04-01 09:21:53 -04004633#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004634/*
Andy Adamson357f54d2010-12-14 10:11:57 -05004635 * Check the exchange flags returned by the server for invalid flags, having
4636 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4637 * DS flags set.
4638 */
4639static int nfs4_check_cl_exchange_flags(u32 flags)
4640{
4641 if (flags & ~EXCHGID4_FLAG_MASK_R)
4642 goto out_inval;
4643 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4644 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4645 goto out_inval;
4646 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4647 goto out_inval;
4648 return NFS_OK;
4649out_inval:
4650 return -NFS4ERR_INVAL;
4651}
4652
4653/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04004654 * nfs4_proc_exchange_id()
4655 *
4656 * Since the clientid has expired, all compounds using sessions
4657 * associated with the stale clientid will be returning
4658 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4659 * be in some phase of session reset.
4660 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004661int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004662{
4663 nfs4_verifier verifier;
4664 struct nfs41_exchange_id_args args = {
4665 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05004666 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04004667 };
4668 struct nfs41_exchange_id_res res = {
4669 .client = clp,
4670 };
4671 int status;
4672 struct rpc_message msg = {
4673 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4674 .rpc_argp = &args,
4675 .rpc_resp = &res,
4676 .rpc_cred = cred,
4677 };
4678 __be32 *p;
4679
4680 dprintk("--> %s\n", __func__);
4681 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004682
Benny Halevy99fe60d2009-04-01 09:22:29 -04004683 p = (u32 *)verifier.data;
4684 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4685 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4686 args.verifier = &verifier;
4687
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004688 args.id_len = scnprintf(args.id, sizeof(args.id),
4689 "%s/%s.%s/%u",
4690 clp->cl_ipaddr,
4691 init_utsname()->nodename,
4692 init_utsname()->domainname,
4693 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004694
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004695 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4696 if (!status)
4697 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004698 dprintk("<-- %s status= %d\n", __func__, status);
4699 return status;
4700}
4701
Andy Adamson2050f0c2009-04-01 09:22:30 -04004702struct nfs4_get_lease_time_data {
4703 struct nfs4_get_lease_time_args *args;
4704 struct nfs4_get_lease_time_res *res;
4705 struct nfs_client *clp;
4706};
4707
4708static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4709 void *calldata)
4710{
4711 int ret;
4712 struct nfs4_get_lease_time_data *data =
4713 (struct nfs4_get_lease_time_data *)calldata;
4714
4715 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004716 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004717 /* just setup sequence, do not trigger session recovery
4718 since we're invoked within one */
4719 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004720 &data->args->la_seq_args,
4721 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004722
4723 BUG_ON(ret == -EAGAIN);
4724 rpc_call_start(task);
4725 dprintk("<-- %s\n", __func__);
4726}
4727
4728/*
4729 * Called from nfs4_state_manager thread for session setup, so don't recover
4730 * from sequence operation or clientid errors.
4731 */
4732static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4733{
4734 struct nfs4_get_lease_time_data *data =
4735 (struct nfs4_get_lease_time_data *)calldata;
4736
4737 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04004738 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4739 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04004740 switch (task->tk_status) {
4741 case -NFS4ERR_DELAY:
4742 case -NFS4ERR_GRACE:
4743 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4744 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4745 task->tk_status = 0;
Trond Myklebust0110ee12009-12-07 09:00:24 -05004746 nfs_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004747 return;
4748 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004749 dprintk("<-- %s\n", __func__);
4750}
4751
4752struct rpc_call_ops nfs4_get_lease_time_ops = {
4753 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4754 .rpc_call_done = nfs4_get_lease_time_done,
4755};
4756
4757int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4758{
4759 struct rpc_task *task;
4760 struct nfs4_get_lease_time_args args;
4761 struct nfs4_get_lease_time_res res = {
4762 .lr_fsinfo = fsinfo,
4763 };
4764 struct nfs4_get_lease_time_data data = {
4765 .args = &args,
4766 .res = &res,
4767 .clp = clp,
4768 };
4769 struct rpc_message msg = {
4770 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4771 .rpc_argp = &args,
4772 .rpc_resp = &res,
4773 };
4774 struct rpc_task_setup task_setup = {
4775 .rpc_client = clp->cl_rpcclient,
4776 .rpc_message = &msg,
4777 .callback_ops = &nfs4_get_lease_time_ops,
4778 .callback_data = &data
4779 };
4780 int status;
4781
Andy Adamson2050f0c2009-04-01 09:22:30 -04004782 dprintk("--> %s\n", __func__);
4783 task = rpc_run_task(&task_setup);
4784
4785 if (IS_ERR(task))
4786 status = PTR_ERR(task);
4787 else {
4788 status = task->tk_status;
4789 rpc_put_task(task);
4790 }
4791 dprintk("<-- %s return %d\n", __func__, status);
4792
4793 return status;
4794}
4795
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004796/*
4797 * Reset a slot table
4798 */
Andy Adamson104aeba2010-01-14 17:45:10 -05004799static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4800 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004801{
Andy Adamson104aeba2010-01-14 17:45:10 -05004802 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004803 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004804 int ret = 0;
4805
Andy Adamson104aeba2010-01-14 17:45:10 -05004806 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4807 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004808
Andy Adamson104aeba2010-01-14 17:45:10 -05004809 /* Does the newly negotiated max_reqs match the existing slot table? */
4810 if (max_reqs != tbl->max_slots) {
4811 ret = -ENOMEM;
4812 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004813 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05004814 if (!new)
4815 goto out;
4816 ret = 0;
4817 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004818 }
4819 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05004820 if (new) {
4821 tbl->slots = new;
4822 tbl->max_slots = max_reqs;
4823 }
4824 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004825 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004826 spin_unlock(&tbl->slot_tbl_lock);
4827 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4828 tbl, tbl->slots, tbl->max_slots);
4829out:
4830 dprintk("<-- %s: return %d\n", __func__, ret);
4831 return ret;
4832}
4833
Andy Adamsonfc931582009-04-01 09:22:31 -04004834/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004835 * Reset the forechannel and backchannel slot tables
4836 */
4837static int nfs4_reset_slot_tables(struct nfs4_session *session)
4838{
4839 int status;
4840
4841 status = nfs4_reset_slot_table(&session->fc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004842 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004843 if (status)
4844 return status;
4845
4846 status = nfs4_reset_slot_table(&session->bc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004847 session->bc_attrs.max_reqs, 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004848 return status;
4849}
4850
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004851/* Destroy the slot table */
4852static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4853{
4854 if (session->fc_slot_table.slots != NULL) {
4855 kfree(session->fc_slot_table.slots);
4856 session->fc_slot_table.slots = NULL;
4857 }
4858 if (session->bc_slot_table.slots != NULL) {
4859 kfree(session->bc_slot_table.slots);
4860 session->bc_slot_table.slots = NULL;
4861 }
4862 return;
4863}
4864
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004865/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004866 * Initialize slot table
4867 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004868static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4869 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04004870{
Andy Adamsonfc931582009-04-01 09:22:31 -04004871 struct nfs4_slot *slot;
4872 int ret = -ENOMEM;
4873
4874 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4875
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004876 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04004877
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004878 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04004879 if (!slot)
4880 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04004881 ret = 0;
4882
4883 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04004884 tbl->max_slots = max_slots;
4885 tbl->slots = slot;
4886 tbl->highest_used_slotid = -1; /* no slot is currently used */
4887 spin_unlock(&tbl->slot_tbl_lock);
4888 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4889 tbl, tbl->slots, tbl->max_slots);
4890out:
4891 dprintk("<-- %s: return %d\n", __func__, ret);
4892 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04004893}
4894
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004895/*
4896 * Initialize the forechannel and backchannel tables
4897 */
4898static int nfs4_init_slot_tables(struct nfs4_session *session)
4899{
Trond Myklebustf26468f2009-12-05 19:32:11 -05004900 struct nfs4_slot_table *tbl;
4901 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004902
Trond Myklebustf26468f2009-12-05 19:32:11 -05004903 tbl = &session->fc_slot_table;
4904 if (tbl->slots == NULL) {
4905 status = nfs4_init_slot_table(tbl,
4906 session->fc_attrs.max_reqs, 1);
4907 if (status)
4908 return status;
4909 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004910
Trond Myklebustf26468f2009-12-05 19:32:11 -05004911 tbl = &session->bc_slot_table;
4912 if (tbl->slots == NULL) {
4913 status = nfs4_init_slot_table(tbl,
4914 session->bc_attrs.max_reqs, 0);
4915 if (status)
4916 nfs4_destroy_slot_tables(session);
4917 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004918
4919 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04004920}
4921
4922struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4923{
4924 struct nfs4_session *session;
4925 struct nfs4_slot_table *tbl;
4926
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004927 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04004928 if (!session)
4929 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04004930
Andy Adamson557134a2009-04-01 09:21:53 -04004931 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004932 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04004933 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004934 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00004935 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004936
4937 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004938 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004939 spin_lock_init(&tbl->slot_tbl_lock);
4940 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00004941 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004942
Trond Myklebust1055d762010-06-16 09:52:27 -04004943 session->session_state = 1<<NFS4_SESSION_INITING;
4944
Andy Adamson557134a2009-04-01 09:21:53 -04004945 session->clp = clp;
4946 return session;
4947}
4948
4949void nfs4_destroy_session(struct nfs4_session *session)
4950{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04004951 nfs4_proc_destroy_session(session);
4952 dprintk("%s Destroy backchannel for xprt %p\n",
4953 __func__, session->clp->cl_rpcclient->cl_xprt);
4954 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4955 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004956 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04004957 kfree(session);
4958}
4959
Andy Adamsonfc931582009-04-01 09:22:31 -04004960/*
4961 * Initialize the values to be used by the client in CREATE_SESSION
4962 * If nfs4_init_session set the fore channel request and response sizes,
4963 * use them.
4964 *
4965 * Set the back channel max_resp_sz_cached to zero to force the client to
4966 * always set csa_cachethis to FALSE because the current implementation
4967 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4968 */
4969static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4970{
4971 struct nfs4_session *session = args->client->cl_session;
4972 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4973 mxresp_sz = session->fc_attrs.max_resp_sz;
4974
4975 if (mxrqst_sz == 0)
4976 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4977 if (mxresp_sz == 0)
4978 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4979 /* Fore channel attributes */
4980 args->fc_attrs.headerpadsz = 0;
4981 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4982 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04004983 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4984 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4985
4986 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05004987 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04004988 __func__,
4989 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05004990 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04004991
4992 /* Back channel attributes */
4993 args->bc_attrs.headerpadsz = 0;
4994 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4995 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4996 args->bc_attrs.max_resp_sz_cached = 0;
4997 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4998 args->bc_attrs.max_reqs = 1;
4999
5000 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5001 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5002 __func__,
5003 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5004 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5005 args->bc_attrs.max_reqs);
5006}
5007
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005008static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005009{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005010 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5011 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5012
5013 if (rcvd->headerpadsz > sent->headerpadsz)
5014 return -EINVAL;
5015 if (rcvd->max_resp_sz > sent->max_resp_sz)
5016 return -EINVAL;
5017 /*
5018 * Our requested max_ops is the minimum we need; we're not
5019 * prepared to break up compounds into smaller pieces than that.
5020 * So, no point even trying to continue if the server won't
5021 * cooperate:
5022 */
5023 if (rcvd->max_ops < sent->max_ops)
5024 return -EINVAL;
5025 if (rcvd->max_reqs == 0)
5026 return -EINVAL;
5027 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005028}
5029
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005030static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5031{
5032 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5033 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005034
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005035 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5036 return -EINVAL;
5037 if (rcvd->max_resp_sz < sent->max_resp_sz)
5038 return -EINVAL;
5039 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5040 return -EINVAL;
5041 /* These would render the backchannel useless: */
5042 if (rcvd->max_ops == 0)
5043 return -EINVAL;
5044 if (rcvd->max_reqs == 0)
5045 return -EINVAL;
5046 return 0;
5047}
Andy Adamson8d353012009-04-01 09:22:32 -04005048
Andy Adamson8d353012009-04-01 09:22:32 -04005049static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5050 struct nfs4_session *session)
5051{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005052 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005053
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005054 ret = nfs4_verify_fore_channel_attrs(args, session);
5055 if (ret)
5056 return ret;
5057 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005058}
5059
Andy Adamsonfc931582009-04-01 09:22:31 -04005060static int _nfs4_proc_create_session(struct nfs_client *clp)
5061{
5062 struct nfs4_session *session = clp->cl_session;
5063 struct nfs41_create_session_args args = {
5064 .client = clp,
5065 .cb_program = NFS4_CALLBACK,
5066 };
5067 struct nfs41_create_session_res res = {
5068 .client = clp,
5069 };
5070 struct rpc_message msg = {
5071 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5072 .rpc_argp = &args,
5073 .rpc_resp = &res,
5074 };
5075 int status;
5076
5077 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005078 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005079
5080 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5081
Andy Adamson8d353012009-04-01 09:22:32 -04005082 if (!status)
5083 /* Verify the session's negotiated channel_attrs values */
5084 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005085 if (!status) {
5086 /* Increment the clientid slot sequence id */
5087 clp->cl_seqid++;
5088 }
5089
5090 return status;
5091}
5092
5093/*
5094 * Issues a CREATE_SESSION operation to the server.
5095 * It is the responsibility of the caller to verify the session is
5096 * expired before calling this routine.
5097 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005098int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005099{
5100 int status;
5101 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005102 struct nfs4_session *session = clp->cl_session;
Ricardo Labiaga7d6d63d2011-03-09 13:13:44 -05005103 long timeout = 0;
5104 int err;
Andy Adamsonfc931582009-04-01 09:22:31 -04005105
5106 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5107
Ricardo Labiaga7d6d63d2011-03-09 13:13:44 -05005108 do {
5109 status = _nfs4_proc_create_session(clp);
5110 if (status == -NFS4ERR_DELAY) {
5111 err = nfs4_delay(clp->cl_rpcclient, &timeout);
5112 if (err)
5113 status = err;
5114 }
5115 } while (status == -NFS4ERR_DELAY);
5116
Andy Adamsonfc931582009-04-01 09:22:31 -04005117 if (status)
5118 goto out;
5119
Trond Myklebustf26468f2009-12-05 19:32:11 -05005120 /* Init and reset the fore channel */
5121 status = nfs4_init_slot_tables(session);
5122 dprintk("slot table initialization returned %d\n", status);
5123 if (status)
5124 goto out;
5125 status = nfs4_reset_slot_tables(session);
5126 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005127 if (status)
5128 goto out;
5129
5130 ptr = (unsigned *)&session->sess_id.data[0];
5131 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5132 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005133out:
5134 dprintk("<-- %s\n", __func__);
5135 return status;
5136}
5137
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005138/*
5139 * Issue the over-the-wire RPC DESTROY_SESSION.
5140 * The caller must serialize access to this routine.
5141 */
5142int nfs4_proc_destroy_session(struct nfs4_session *session)
5143{
5144 int status = 0;
5145 struct rpc_message msg;
5146
5147 dprintk("--> nfs4_proc_destroy_session\n");
5148
5149 /* session is still being setup */
5150 if (session->clp->cl_cons_state != NFS_CS_READY)
5151 return status;
5152
5153 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5154 msg.rpc_argp = session;
5155 msg.rpc_resp = NULL;
5156 msg.rpc_cred = NULL;
5157 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5158
5159 if (status)
5160 printk(KERN_WARNING
5161 "Got error %d from the server on DESTROY_SESSION. "
5162 "Session has been destroyed regardless...\n", status);
5163
5164 dprintk("<-- nfs4_proc_destroy_session\n");
5165 return status;
5166}
5167
Trond Myklebustfccba802009-07-21 16:48:07 -04005168int nfs4_init_session(struct nfs_server *server)
5169{
5170 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005171 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005172 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005173 int ret;
5174
5175 if (!nfs4_has_session(clp))
5176 return 0;
5177
Trond Myklebust1055d762010-06-16 09:52:27 -04005178 session = clp->cl_session;
5179 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5180 return 0;
5181
Andy Adamson68bf05e2009-12-15 12:55:02 -05005182 rsize = server->rsize;
5183 if (rsize == 0)
5184 rsize = NFS_MAX_FILE_IO_SIZE;
5185 wsize = server->wsize;
5186 if (wsize == 0)
5187 wsize = NFS_MAX_FILE_IO_SIZE;
5188
Andy Adamson68bf05e2009-12-15 12:55:02 -05005189 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5190 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005191
Trond Myklebustfccba802009-07-21 16:48:07 -04005192 ret = nfs4_recover_expired_lease(server);
5193 if (!ret)
5194 ret = nfs4_check_client_ready(clp);
5195 return ret;
5196}
5197
Andy Adamsond83217c2011-03-01 01:34:17 +00005198int nfs4_init_ds_session(struct nfs_client *clp)
5199{
5200 struct nfs4_session *session = clp->cl_session;
5201 int ret;
5202
5203 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5204 return 0;
5205
5206 ret = nfs4_client_recover_expired_lease(clp);
5207 if (!ret)
5208 /* Test for the DS role */
5209 if (!is_ds_client(clp))
5210 ret = -ENODEV;
5211 if (!ret)
5212 ret = nfs4_check_client_ready(clp);
5213 return ret;
5214
5215}
5216EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5217
5218
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005219/*
5220 * Renew the cl_session lease.
5221 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005222struct nfs4_sequence_data {
5223 struct nfs_client *clp;
5224 struct nfs4_sequence_args args;
5225 struct nfs4_sequence_res res;
5226};
5227
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005228static void nfs41_sequence_release(void *data)
5229{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005230 struct nfs4_sequence_data *calldata = data;
5231 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005232
Alexandros Batsakis71358402010-02-05 03:45:05 -08005233 if (atomic_read(&clp->cl_count) > 1)
5234 nfs4_schedule_state_renewal(clp);
5235 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005236 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005237}
5238
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005239static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5240{
5241 switch(task->tk_status) {
5242 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005243 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5244 return -EAGAIN;
5245 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005246 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005247 }
5248 return 0;
5249}
5250
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005251static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005252{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005253 struct nfs4_sequence_data *calldata = data;
5254 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005255
Trond Myklebust14516c32010-07-31 14:29:06 -04005256 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5257 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005258
5259 if (task->tk_status < 0) {
5260 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005261 if (atomic_read(&clp->cl_count) == 1)
5262 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005263
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005264 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5265 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005266 return;
5267 }
5268 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005269 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005270out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005271 dprintk("<-- %s\n", __func__);
5272}
5273
5274static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5275{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005276 struct nfs4_sequence_data *calldata = data;
5277 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005278 struct nfs4_sequence_args *args;
5279 struct nfs4_sequence_res *res;
5280
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005281 args = task->tk_msg.rpc_argp;
5282 res = task->tk_msg.rpc_resp;
5283
Trond Myklebust035168a2010-06-16 09:52:26 -04005284 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005285 return;
5286 rpc_call_start(task);
5287}
5288
5289static const struct rpc_call_ops nfs41_sequence_ops = {
5290 .rpc_call_done = nfs41_sequence_call_done,
5291 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005292 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005293};
5294
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005295static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005296{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005297 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005298 struct rpc_message msg = {
5299 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5300 .rpc_cred = cred,
5301 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005302 struct rpc_task_setup task_setup_data = {
5303 .rpc_client = clp->cl_rpcclient,
5304 .rpc_message = &msg,
5305 .callback_ops = &nfs41_sequence_ops,
5306 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5307 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005308
Alexandros Batsakis71358402010-02-05 03:45:05 -08005309 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005310 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005311 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005312 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005313 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005314 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005315 }
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005316 msg.rpc_argp = &calldata->args;
5317 msg.rpc_resp = &calldata->res;
5318 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005319 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005320
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005321 return rpc_run_task(&task_setup_data);
5322}
5323
5324static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5325{
5326 struct rpc_task *task;
5327 int ret = 0;
5328
5329 task = _nfs41_proc_sequence(clp, cred);
5330 if (IS_ERR(task))
5331 ret = PTR_ERR(task);
5332 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005333 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005334 dprintk("<-- %s status=%d\n", __func__, ret);
5335 return ret;
5336}
5337
5338static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5339{
5340 struct rpc_task *task;
5341 int ret;
5342
5343 task = _nfs41_proc_sequence(clp, cred);
5344 if (IS_ERR(task)) {
5345 ret = PTR_ERR(task);
5346 goto out;
5347 }
5348 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005349 if (!ret) {
5350 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5351
5352 if (task->tk_status == 0)
5353 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005354 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005355 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005356 rpc_put_task(task);
5357out:
5358 dprintk("<-- %s status=%d\n", __func__, ret);
5359 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005360}
5361
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005362struct nfs4_reclaim_complete_data {
5363 struct nfs_client *clp;
5364 struct nfs41_reclaim_complete_args arg;
5365 struct nfs41_reclaim_complete_res res;
5366};
5367
5368static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5369{
5370 struct nfs4_reclaim_complete_data *calldata = data;
5371
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005372 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005373 if (nfs41_setup_sequence(calldata->clp->cl_session,
5374 &calldata->arg.seq_args,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005375 &calldata->res.seq_res, 0, task))
5376 return;
5377
5378 rpc_call_start(task);
5379}
5380
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005381static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5382{
5383 switch(task->tk_status) {
5384 case 0:
5385 case -NFS4ERR_COMPLETE_ALREADY:
5386 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5387 break;
5388 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005389 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5390 return -EAGAIN;
5391 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005392 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005393 }
5394 return 0;
5395}
5396
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005397static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5398{
5399 struct nfs4_reclaim_complete_data *calldata = data;
5400 struct nfs_client *clp = calldata->clp;
5401 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5402
5403 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005404 if (!nfs41_sequence_done(task, res))
5405 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005406
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005407 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5408 rpc_restart_call_prepare(task);
5409 return;
5410 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005411 dprintk("<-- %s\n", __func__);
5412}
5413
5414static void nfs4_free_reclaim_complete_data(void *data)
5415{
5416 struct nfs4_reclaim_complete_data *calldata = data;
5417
5418 kfree(calldata);
5419}
5420
5421static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5422 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5423 .rpc_call_done = nfs4_reclaim_complete_done,
5424 .rpc_release = nfs4_free_reclaim_complete_data,
5425};
5426
5427/*
5428 * Issue a global reclaim complete.
5429 */
5430static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5431{
5432 struct nfs4_reclaim_complete_data *calldata;
5433 struct rpc_task *task;
5434 struct rpc_message msg = {
5435 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5436 };
5437 struct rpc_task_setup task_setup_data = {
5438 .rpc_client = clp->cl_rpcclient,
5439 .rpc_message = &msg,
5440 .callback_ops = &nfs4_reclaim_complete_call_ops,
5441 .flags = RPC_TASK_ASYNC,
5442 };
5443 int status = -ENOMEM;
5444
5445 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005446 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005447 if (calldata == NULL)
5448 goto out;
5449 calldata->clp = clp;
5450 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005451
5452 msg.rpc_argp = &calldata->arg;
5453 msg.rpc_resp = &calldata->res;
5454 task_setup_data.callback_data = calldata;
5455 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005456 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005457 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005458 goto out;
5459 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005460 status = nfs4_wait_for_completion_rpc_task(task);
5461 if (status == 0)
5462 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005463 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005464 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005465out:
5466 dprintk("<-- %s status=%d\n", __func__, status);
5467 return status;
5468}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005469
5470static void
5471nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5472{
5473 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005474 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005475
5476 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005477 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5478 * right now covering the LAYOUTGET we are about to send.
5479 * However, that is not so catastrophic, and there seems
5480 * to be no way to prevent it completely.
5481 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005482 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5483 &lgp->res.seq_res, 0, task))
5484 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005485 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5486 NFS_I(lgp->args.inode)->layout,
5487 lgp->args.ctx->state)) {
5488 rpc_exit(task, NFS4_OK);
5489 return;
5490 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005491 rpc_call_start(task);
5492}
5493
5494static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5495{
5496 struct nfs4_layoutget *lgp = calldata;
5497 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5498
5499 dprintk("--> %s\n", __func__);
5500
5501 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5502 return;
5503
5504 switch (task->tk_status) {
5505 case 0:
5506 break;
5507 case -NFS4ERR_LAYOUTTRYLATER:
5508 case -NFS4ERR_RECALLCONFLICT:
5509 task->tk_status = -NFS4ERR_DELAY;
5510 /* Fall through */
5511 default:
5512 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5513 rpc_restart_call_prepare(task);
5514 return;
5515 }
5516 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005517 dprintk("<-- %s\n", __func__);
5518}
5519
5520static void nfs4_layoutget_release(void *calldata)
5521{
5522 struct nfs4_layoutget *lgp = calldata;
5523
5524 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005525 if (lgp->res.layout.buf != NULL)
5526 free_page((unsigned long) lgp->res.layout.buf);
5527 put_nfs_open_context(lgp->args.ctx);
5528 kfree(calldata);
5529 dprintk("<-- %s\n", __func__);
5530}
5531
5532static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5533 .rpc_call_prepare = nfs4_layoutget_prepare,
5534 .rpc_call_done = nfs4_layoutget_done,
5535 .rpc_release = nfs4_layoutget_release,
5536};
5537
5538int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5539{
5540 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5541 struct rpc_task *task;
5542 struct rpc_message msg = {
5543 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5544 .rpc_argp = &lgp->args,
5545 .rpc_resp = &lgp->res,
5546 };
5547 struct rpc_task_setup task_setup_data = {
5548 .rpc_client = server->client,
5549 .rpc_message = &msg,
5550 .callback_ops = &nfs4_layoutget_call_ops,
5551 .callback_data = lgp,
5552 .flags = RPC_TASK_ASYNC,
5553 };
5554 int status = 0;
5555
5556 dprintk("--> %s\n", __func__);
5557
5558 lgp->res.layout.buf = (void *)__get_free_page(GFP_NOFS);
5559 if (lgp->res.layout.buf == NULL) {
5560 nfs4_layoutget_release(lgp);
5561 return -ENOMEM;
5562 }
5563
5564 lgp->res.seq_res.sr_slot = NULL;
5565 task = rpc_run_task(&task_setup_data);
5566 if (IS_ERR(task))
5567 return PTR_ERR(task);
5568 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00005569 if (status == 0)
5570 status = task->tk_status;
5571 if (status == 0)
5572 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005573 rpc_put_task(task);
5574 dprintk("<-- %s status=%d\n", __func__, status);
5575 return status;
5576}
5577
5578static int
5579_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5580{
5581 struct nfs4_getdeviceinfo_args args = {
5582 .pdev = pdev,
5583 };
5584 struct nfs4_getdeviceinfo_res res = {
5585 .pdev = pdev,
5586 };
5587 struct rpc_message msg = {
5588 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5589 .rpc_argp = &args,
5590 .rpc_resp = &res,
5591 };
5592 int status;
5593
5594 dprintk("--> %s\n", __func__);
5595 status = nfs4_call_sync(server, &msg, &args, &res, 0);
5596 dprintk("<-- %s status=%d\n", __func__, status);
5597
5598 return status;
5599}
5600
5601int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5602{
5603 struct nfs4_exception exception = { };
5604 int err;
5605
5606 do {
5607 err = nfs4_handle_exception(server,
5608 _nfs4_proc_getdeviceinfo(server, pdev),
5609 &exception);
5610 } while (exception.retry);
5611 return err;
5612}
5613EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5614
Andy Adamson557134a2009-04-01 09:21:53 -04005615#endif /* CONFIG_NFS_V4_1 */
5616
Andy Adamson591d71c2009-04-01 09:22:47 -04005617struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005618 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005619 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620 .recover_open = nfs4_open_reclaim,
5621 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04005622 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005623 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624};
5625
Andy Adamson591d71c2009-04-01 09:22:47 -04005626#if defined(CONFIG_NFS_V4_1)
5627struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5628 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5629 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5630 .recover_open = nfs4_open_reclaim,
5631 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05005632 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005633 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005634 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04005635};
5636#endif /* CONFIG_NFS_V4_1 */
5637
5638struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005639 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005640 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641 .recover_open = nfs4_open_expired,
5642 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04005643 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005644 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645};
5646
Andy Adamson591d71c2009-04-01 09:22:47 -04005647#if defined(CONFIG_NFS_V4_1)
5648struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5649 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5650 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5651 .recover_open = nfs4_open_expired,
5652 .recover_lock = nfs4_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05005653 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005654 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04005655};
5656#endif /* CONFIG_NFS_V4_1 */
5657
Benny Halevy29fba382009-04-01 09:22:44 -04005658struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5659 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04005660 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005661 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04005662};
5663
5664#if defined(CONFIG_NFS_V4_1)
5665struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5666 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04005667 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005668 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04005669};
5670#endif
5671
Trond Myklebust97dc1352010-06-16 09:52:26 -04005672static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
5673 .minor_version = 0,
5674 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04005675 .validate_stateid = nfs4_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005676 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
5677 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
5678 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005679};
5680
5681#if defined(CONFIG_NFS_V4_1)
5682static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
5683 .minor_version = 1,
5684 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04005685 .validate_stateid = nfs41_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005686 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
5687 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
5688 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005689};
5690#endif
5691
Trond Myklebust97dc1352010-06-16 09:52:26 -04005692const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
5693 [0] = &nfs_v4_0_minor_ops,
5694#if defined(CONFIG_NFS_V4_1)
5695 [1] = &nfs_v4_1_minor_ops,
5696#endif
5697};
5698
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005699static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005700 .permission = nfs_permission,
5701 .getattr = nfs_getattr,
5702 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005703 .getxattr = generic_getxattr,
5704 .setxattr = generic_setxattr,
5705 .listxattr = generic_listxattr,
5706 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005707};
5708
David Howells509de812006-08-22 20:06:11 -04005709const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710 .version = 4, /* protocol version */
5711 .dentry_ops = &nfs4_dentry_operations,
5712 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005713 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714 .getroot = nfs4_proc_get_root,
5715 .getattr = nfs4_proc_getattr,
5716 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04005717 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718 .lookup = nfs4_proc_lookup,
5719 .access = nfs4_proc_access,
5720 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 .create = nfs4_proc_create,
5722 .remove = nfs4_proc_remove,
5723 .unlink_setup = nfs4_proc_unlink_setup,
5724 .unlink_done = nfs4_proc_unlink_done,
5725 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04005726 .rename_setup = nfs4_proc_rename_setup,
5727 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728 .link = nfs4_proc_link,
5729 .symlink = nfs4_proc_symlink,
5730 .mkdir = nfs4_proc_mkdir,
5731 .rmdir = nfs4_proc_remove,
5732 .readdir = nfs4_proc_readdir,
5733 .mknod = nfs4_proc_mknod,
5734 .statfs = nfs4_proc_statfs,
5735 .fsinfo = nfs4_proc_fsinfo,
5736 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04005737 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738 .decode_dirent = nfs4_decode_dirent,
5739 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05005740 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005742 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005744 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005746 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04005747 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04005748 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00005749 .init_client = nfs4_init_client,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750};
5751
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005752static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
5753 .prefix = XATTR_NAME_NFSV4_ACL,
5754 .list = nfs4_xattr_list_nfs4_acl,
5755 .get = nfs4_xattr_get_nfs4_acl,
5756 .set = nfs4_xattr_set_nfs4_acl,
5757};
5758
5759const struct xattr_handler *nfs4_xattr_handlers[] = {
5760 &nfs4_xattr_nfs4_acl_handler,
5761 NULL
5762};
5763
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764/*
5765 * Local variables:
5766 * c-basic-offset: 8
5767 * End:
5768 */