blob: 8f3ada04ea1910968344371d008d16ede44ef051 [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 */
247static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
248{
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;
299 }
300 /* We failed to handle the error */
301 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500302wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500303 ret = nfs4_wait_clnt_recover(clp);
304 if (ret == 0)
305 exception->retry = 1;
306 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500307}
308
309
Trond Myklebust452e9352010-07-31 14:29:06 -0400310static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 spin_lock(&clp->cl_lock);
313 if (time_before(clp->cl_last_renewal,timestamp))
314 clp->cl_last_renewal = timestamp;
315 spin_unlock(&clp->cl_lock);
316}
317
Trond Myklebust452e9352010-07-31 14:29:06 -0400318static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
319{
320 do_renew_lease(server->nfs_client, timestamp);
321}
322
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400323#if defined(CONFIG_NFS_V4_1)
324
Benny Halevy510b8172009-04-01 09:22:14 -0400325/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400326 * nfs4_free_slot - free a slot and efficiently update slot table.
327 *
328 * freeing a slot is trivially done by clearing its respective bit
329 * in the bitmap.
330 * If the freed slotid equals highest_used_slotid we want to update it
331 * so that the server would be able to size down the slot table if needed,
332 * otherwise we know that the highest_used_slotid is still in use.
333 * When updating highest_used_slotid there may be "holes" in the bitmap
334 * so we need to scan down from highest_used_slotid to 0 looking for the now
335 * highest slotid in use.
336 * If none found, highest_used_slotid is set to -1.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500337 *
338 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400339 */
340static void
Benny Halevydfb4f302010-09-24 09:17:01 -0400341nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *free_slot)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400342{
Benny Halevydfb4f302010-09-24 09:17:01 -0400343 int free_slotid = free_slot - tbl->slots;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400344 int slotid = free_slotid;
345
Benny Halevydfb4f302010-09-24 09:17:01 -0400346 BUG_ON(slotid < 0 || slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400347 /* clear used bit in bitmap */
348 __clear_bit(slotid, tbl->used_slots);
349
350 /* update highest_used_slotid when it is freed */
351 if (slotid == tbl->highest_used_slotid) {
352 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500353 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400354 tbl->highest_used_slotid = slotid;
355 else
356 tbl->highest_used_slotid = -1;
357 }
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400358 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
359 free_slotid, tbl->highest_used_slotid);
360}
361
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800362/*
Andy Adamson42acd022011-01-06 02:04:34 +0000363 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800364 */
Andy Adamson42acd022011-01-06 02:04:34 +0000365static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800366{
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800367 struct rpc_task *task;
368
Trond Myklebusta2118c32010-06-16 09:52:26 -0400369 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800370 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
371 if (task)
372 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800373 return;
374 }
375
376 if (ses->fc_slot_table.highest_used_slotid != -1)
377 return;
378
Andy Adamson42acd022011-01-06 02:04:34 +0000379 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
380 complete(&ses->fc_slot_table.complete);
381}
382
383/*
384 * Signal state manager thread if session back channel is drained
385 */
386void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
387{
388 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
389 ses->bc_slot_table.highest_used_slotid != -1)
390 return;
391 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
392 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800393}
394
Trond Myklebustd185a332010-06-16 09:52:25 -0400395static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400396{
397 struct nfs4_slot_table *tbl;
398
Trond Myklebustd185a332010-06-16 09:52:25 -0400399 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f302010-09-24 09:17:01 -0400400 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400401 /* just wake up the next guy waiting since
402 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500403 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500404 return;
Andy Adamson13615872009-04-01 09:22:17 -0400405 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500406
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500407 spin_lock(&tbl->slot_tbl_lock);
Benny Halevydfb4f302010-09-24 09:17:01 -0400408 nfs4_free_slot(tbl, res->sr_slot);
Andy Adamson42acd022011-01-06 02:04:34 +0000409 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500410 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f302010-09-24 09:17:01 -0400411 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400412}
413
Trond Myklebust14516c32010-07-31 14:29:06 -0400414static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400415{
416 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400417 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400418
419 /*
420 * sr_status remains 1 if an RPC level error occurred. The server
421 * may or may not have processed the sequence operation..
422 * Proceed as if the server received and processed the sequence
423 * operation.
424 */
425 if (res->sr_status == 1)
426 res->sr_status = NFS_OK;
427
428 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
Benny Halevydfb4f302010-09-24 09:17:01 -0400429 if (!res->sr_slot)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400430 goto out;
431
Andy Adamson691daf32009-12-04 15:55:39 -0500432 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400433 switch (res->sr_status) {
434 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400435 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f302010-09-24 09:17:01 -0400436 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400437 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400438 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400439 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500440 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500441 if (res->sr_status_flags != 0)
442 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400443 break;
444 case -NFS4ERR_DELAY:
445 /* The server detected a resend of the RPC call and
446 * returned NFS4ERR_DELAY as per Section 2.10.6.2
447 * of RFC5661.
448 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200449 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f302010-09-24 09:17:01 -0400450 __func__,
451 res->sr_slot - res->sr_session->fc_slot_table.slots,
452 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400453 goto out_retry;
454 default:
455 /* Just update the slot sequence no. */
Benny Halevydfb4f302010-09-24 09:17:01 -0400456 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400457 }
458out:
459 /* The session may be reset by one of the error handlers. */
460 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400461 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400462 return 1;
463out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400464 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400465 goto out;
466 rpc_delay(task, NFS4_POLL_RETRY_MAX);
467 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400468}
469
Trond Myklebust14516c32010-07-31 14:29:06 -0400470static int nfs4_sequence_done(struct rpc_task *task,
471 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400472{
Trond Myklebust14516c32010-07-31 14:29:06 -0400473 if (res->sr_session == NULL)
474 return 1;
475 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400476}
477
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400478/*
Benny Halevy510b8172009-04-01 09:22:14 -0400479 * nfs4_find_slot - efficiently look for a free slot
480 *
481 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
482 * If found, we mark the slot as used, update the highest_used_slotid,
483 * and respectively set up the sequence operation args.
484 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400485 *
486 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400487 */
488static u8
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800489nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400490{
491 int slotid;
492 u8 ret_id = NFS4_MAX_SLOT_TABLE;
493 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
494
Benny Halevy510b8172009-04-01 09:22:14 -0400495 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
496 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
497 tbl->max_slots);
498 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
499 if (slotid >= tbl->max_slots)
500 goto out;
501 __set_bit(slotid, tbl->used_slots);
502 if (slotid > tbl->highest_used_slotid)
503 tbl->highest_used_slotid = slotid;
504 ret_id = slotid;
505out:
506 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
507 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400508 return ret_id;
509}
510
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000511int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400512 struct nfs4_sequence_args *args,
513 struct nfs4_sequence_res *res,
514 int cache_reply,
515 struct rpc_task *task)
516{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400517 struct nfs4_slot *slot;
518 struct nfs4_slot_table *tbl;
519 u8 slotid;
520
521 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400522 /* slot already allocated? */
Benny Halevydfb4f302010-09-24 09:17:01 -0400523 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400524 return 0;
525
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400526 tbl = &session->fc_slot_table;
527
528 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400529 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800530 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamsonea028ac2009-12-04 15:55:38 -0500531 /*
532 * The state manager will wait until the slot table is empty.
533 * Schedule the reset thread
534 */
Andy Adamson05f0d232009-12-04 15:55:37 -0500535 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400536 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500537 dprintk("%s Schedule Session Reset\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500538 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400539 }
540
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800541 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
542 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
543 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
544 spin_unlock(&tbl->slot_tbl_lock);
545 dprintk("%s enforce FIFO order\n", __func__);
546 return -EAGAIN;
547 }
548
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800549 slotid = nfs4_find_slot(tbl);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400550 if (slotid == NFS4_MAX_SLOT_TABLE) {
551 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
552 spin_unlock(&tbl->slot_tbl_lock);
553 dprintk("<-- %s: no free slots\n", __func__);
554 return -EAGAIN;
555 }
556 spin_unlock(&tbl->slot_tbl_lock);
557
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800558 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400559 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400560 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400561 args->sa_slotid = slotid;
562 args->sa_cache_this = cache_reply;
563
564 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
565
Benny Halevy99fe60d2009-04-01 09:22:29 -0400566 res->sr_session = session;
Benny Halevydfb4f302010-09-24 09:17:01 -0400567 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400568 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400569 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400570 /*
571 * sr_status is only set in decode_sequence, and so will remain
572 * set to 1 if an rpc level failure occurs.
573 */
574 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400575 return 0;
576}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000577EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400578
Trond Myklebust035168a2010-06-16 09:52:26 -0400579int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400580 struct nfs4_sequence_args *args,
581 struct nfs4_sequence_res *res,
582 int cache_reply,
583 struct rpc_task *task)
584{
Trond Myklebust035168a2010-06-16 09:52:26 -0400585 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400586 int ret = 0;
587
Trond Myklebust035168a2010-06-16 09:52:26 -0400588 if (session == NULL) {
589 args->sa_session = NULL;
590 res->sr_session = NULL;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400591 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400592 }
593
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200594 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f302010-09-24 09:17:01 -0400595 __func__, session->clp, session, res->sr_slot ?
596 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400597
598 ret = nfs41_setup_sequence(session, args, res, cache_reply,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400599 task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400600out:
601 dprintk("<-- %s status=%d\n", __func__, ret);
602 return ret;
603}
604
605struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400606 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400607 struct nfs4_sequence_args *seq_args;
608 struct nfs4_sequence_res *seq_res;
609 int cache_reply;
610};
611
612static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
613{
614 struct nfs41_call_sync_data *data = calldata;
615
Trond Myklebust035168a2010-06-16 09:52:26 -0400616 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
617
618 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400619 data->seq_res, data->cache_reply, task))
620 return;
621 rpc_call_start(task);
622}
623
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800624static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
625{
626 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
627 nfs41_call_sync_prepare(task, calldata);
628}
629
Andy Adamson69ab40c2009-04-01 09:22:19 -0400630static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
631{
632 struct nfs41_call_sync_data *data = calldata;
633
Trond Myklebust14516c32010-07-31 14:29:06 -0400634 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400635}
636
Andy Adamsonce5039c2009-04-01 09:22:13 -0400637struct rpc_call_ops nfs41_call_sync_ops = {
638 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400639 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400640};
641
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800642struct rpc_call_ops nfs41_call_priv_sync_ops = {
643 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
644 .rpc_call_done = nfs41_call_sync_done,
645};
646
Trond Myklebust035168a2010-06-16 09:52:26 -0400647static int nfs4_call_sync_sequence(struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400648 struct rpc_message *msg,
649 struct nfs4_sequence_args *args,
650 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800651 int cache_reply,
652 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400653{
654 int ret;
655 struct rpc_task *task;
656 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400657 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400658 .seq_args = args,
659 .seq_res = res,
660 .cache_reply = cache_reply,
661 };
662 struct rpc_task_setup task_setup = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400663 .rpc_client = server->client,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400664 .rpc_message = msg,
665 .callback_ops = &nfs41_call_sync_ops,
666 .callback_data = &data
667 };
668
Benny Halevydfb4f302010-09-24 09:17:01 -0400669 res->sr_slot = NULL;
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800670 if (privileged)
671 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400672 task = rpc_run_task(&task_setup);
673 if (IS_ERR(task))
674 ret = PTR_ERR(task);
675 else {
676 ret = task->tk_status;
677 rpc_put_task(task);
678 }
679 return ret;
680}
681
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400682int _nfs4_call_sync_session(struct nfs_server *server,
683 struct rpc_message *msg,
684 struct nfs4_sequence_args *args,
685 struct nfs4_sequence_res *res,
686 int cache_reply)
687{
Trond Myklebust035168a2010-06-16 09:52:26 -0400688 return nfs4_call_sync_sequence(server, msg, args, res, cache_reply, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400689}
690
Trond Myklebustdf896452010-06-16 09:52:26 -0400691#else
Trond Myklebust14516c32010-07-31 14:29:06 -0400692static int nfs4_sequence_done(struct rpc_task *task,
693 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400694{
Trond Myklebust14516c32010-07-31 14:29:06 -0400695 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400696}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400697#endif /* CONFIG_NFS_V4_1 */
698
699int _nfs4_call_sync(struct nfs_server *server,
700 struct rpc_message *msg,
701 struct nfs4_sequence_args *args,
702 struct nfs4_sequence_res *res,
703 int cache_reply)
704{
Benny Halevyf3752972009-04-01 09:22:04 -0400705 args->sa_session = res->sr_session = NULL;
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400706 return rpc_call_sync(server->client, msg, 0);
707}
708
709#define nfs4_call_sync(server, msg, args, res, cache_reply) \
Trond Myklebust97dc1352010-06-16 09:52:26 -0400710 (server)->nfs_client->cl_mvops->call_sync((server), (msg), &(args)->seq_args, \
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400711 &(res)->seq_res, (cache_reply))
712
Trond Myklebust38478b22006-05-25 01:40:57 -0400713static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
Trond Myklebust38478b22006-05-25 01:40:57 -0400715 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Trond Myklebust38478b22006-05-25 01:40:57 -0400717 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400718 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
719 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400720 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400721 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400722 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723}
724
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100725struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400726 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100727 struct nfs_openargs o_arg;
728 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100729 struct nfs_open_confirmargs c_arg;
730 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100731 struct nfs_fattr f_attr;
732 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400733 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100734 struct dentry *dir;
735 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400736 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100737 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100738 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400739 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100740 int rpc_status;
741 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100742};
743
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400744
745static void nfs4_init_opendata_res(struct nfs4_opendata *p)
746{
747 p->o_res.f_attr = &p->f_attr;
748 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400749 p->o_res.seqid = p->o_arg.seqid;
750 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400751 p->o_res.server = p->o_arg.server;
752 nfs_fattr_init(&p->f_attr);
753 nfs_fattr_init(&p->dir_attr);
754}
755
Trond Myklebustad389da2007-06-05 12:30:00 -0400756static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500757 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400758 const struct iattr *attrs,
759 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100760{
Trond Myklebustad389da2007-06-05 12:30:00 -0400761 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100762 struct inode *dir = parent->d_inode;
763 struct nfs_server *server = NFS_SERVER(dir);
764 struct nfs4_opendata *p;
765
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400766 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100767 if (p == NULL)
768 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400769 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100770 if (p->o_arg.seqid == NULL)
771 goto err_free;
Al Virof6948692010-01-30 13:51:04 -0500772 path_get(path);
773 p->path = *path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100774 p->dir = parent;
775 p->owner = sp;
776 atomic_inc(&sp->so_count);
777 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500778 p->o_arg.open_flags = flags;
779 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400780 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400781 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400782 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100783 p->o_arg.server = server;
784 p->o_arg.bitmask = server->attr_bitmask;
785 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400786 if (flags & O_CREAT) {
787 u32 *s;
788
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100789 p->o_arg.u.attrs = &p->attrs;
790 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400791 s = (u32 *) p->o_arg.u.verifier.data;
792 s[0] = jiffies;
793 s[1] = current->pid;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100794 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100795 p->c_arg.fh = &p->o_res.fh;
796 p->c_arg.stateid = &p->o_res.stateid;
797 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400798 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400799 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100800 return p;
801err_free:
802 kfree(p);
803err:
804 dput(parent);
805 return NULL;
806}
807
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400808static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100809{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400810 struct nfs4_opendata *p = container_of(kref,
811 struct nfs4_opendata, kref);
812
813 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400814 if (p->state != NULL)
815 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400816 nfs4_put_state_owner(p->owner);
817 dput(p->dir);
Jan Blunck31f31db2008-05-02 13:42:45 -0700818 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400819 kfree(p);
820}
821
822static void nfs4_opendata_put(struct nfs4_opendata *p)
823{
824 if (p != NULL)
825 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100826}
827
Trond Myklebust06f814a2006-01-03 09:55:07 +0100828static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
829{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100830 int ret;
831
Trond Myklebust06f814a2006-01-03 09:55:07 +0100832 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100833 return ret;
834}
835
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500836static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400837{
838 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500839
840 if (open_mode & O_EXCL)
841 goto out;
842 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400843 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500844 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
845 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400846 break;
847 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500848 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
849 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400850 break;
851 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500852 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
853 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400854 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500855out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400856 return ret;
857}
858
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500859static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400860{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500861 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400862 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500863 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400864 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500865 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400866 return 1;
867}
868
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500869static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100870{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500871 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100872 case FMODE_WRITE:
873 state->n_wronly++;
874 break;
875 case FMODE_READ:
876 state->n_rdonly++;
877 break;
878 case FMODE_READ|FMODE_WRITE:
879 state->n_rdwr++;
880 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500881 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100882}
883
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500884static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400885{
886 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
887 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
888 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500889 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400890 case FMODE_READ:
891 set_bit(NFS_O_RDONLY_STATE, &state->flags);
892 break;
893 case FMODE_WRITE:
894 set_bit(NFS_O_WRONLY_STATE, &state->flags);
895 break;
896 case FMODE_READ|FMODE_WRITE:
897 set_bit(NFS_O_RDWR_STATE, &state->flags);
898 }
899}
900
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500901static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400902{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400903 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500904 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400905 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400906}
907
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500908static 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 -0700909{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400910 /*
911 * Protect the call to nfs4_state_set_mode_locked and
912 * serialise the stateid update
913 */
914 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400915 if (deleg_stateid != NULL) {
916 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
917 set_bit(NFS_DELEGATED_STATE, &state->flags);
918 }
919 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500920 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400921 write_sequnlock(&state->seqlock);
922 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500923 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700924 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925}
926
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500927static 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 -0500928{
929 struct nfs_inode *nfsi = NFS_I(state->inode);
930 struct nfs_delegation *deleg_cur;
931 int ret = 0;
932
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500933 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500934
935 rcu_read_lock();
936 deleg_cur = rcu_dereference(nfsi->delegation);
937 if (deleg_cur == NULL)
938 goto no_delegation;
939
940 spin_lock(&deleg_cur->lock);
941 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500942 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500943 goto no_delegation_unlock;
944
945 if (delegation == NULL)
946 delegation = &deleg_cur->stateid;
947 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
948 goto no_delegation_unlock;
949
Trond Myklebustb7391f42008-12-23 15:21:52 -0500950 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500951 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500952 ret = 1;
953no_delegation_unlock:
954 spin_unlock(&deleg_cur->lock);
955no_delegation:
956 rcu_read_unlock();
957
958 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500959 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500960 ret = 1;
961 }
962
963 return ret;
964}
965
966
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500967static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400968{
969 struct nfs_delegation *delegation;
970
971 rcu_read_lock();
972 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500973 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400974 rcu_read_unlock();
975 return;
976 }
977 rcu_read_unlock();
978 nfs_inode_return_delegation(inode);
979}
980
Trond Myklebust6ee41262007-07-08 14:11:36 -0400981static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400982{
983 struct nfs4_state *state = opendata->state;
984 struct nfs_inode *nfsi = NFS_I(state->inode);
985 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500986 int open_mode = opendata->o_arg.open_flags & O_EXCL;
987 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400988 nfs4_stateid stateid;
989 int ret = -EAGAIN;
990
Trond Myklebustaac00a82007-07-05 19:02:21 -0400991 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500992 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400993 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500994 if (can_open_cached(state, fmode, open_mode)) {
995 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400996 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400997 goto out_return_state;
998 }
999 spin_unlock(&state->owner->so_lock);
1000 }
Trond Myklebust34310432008-12-23 15:21:38 -05001001 rcu_read_lock();
1002 delegation = rcu_dereference(nfsi->delegation);
1003 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001004 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001005 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001006 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001007 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001008 /* Save the delegation */
1009 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1010 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001011 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001012 if (ret != 0)
1013 goto out;
1014 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001015
1016 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001017 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001018 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001019 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001020out:
1021 return ERR_PTR(ret);
1022out_return_state:
1023 atomic_inc(&state->count);
1024 return state;
1025}
1026
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001027static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1028{
1029 struct inode *inode;
1030 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001031 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001032 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001033
Trond Myklebustaac00a82007-07-05 19:02:21 -04001034 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001035 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001036 goto out;
1037 }
1038
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001039 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001040 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001041 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001042 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001043 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001044 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001045 goto err;
1046 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001047 state = nfs4_get_open_state(inode, data->owner);
1048 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001049 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001050 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001051 int delegation_flags = 0;
1052
Trond Myklebust003707c2007-07-05 18:07:55 -04001053 rcu_read_lock();
1054 delegation = rcu_dereference(NFS_I(inode)->delegation);
1055 if (delegation)
1056 delegation_flags = delegation->flags;
1057 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -05001058 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001059 nfs_inode_set_delegation(state->inode,
1060 data->owner->so_cred,
1061 &data->o_res);
1062 else
1063 nfs_inode_reclaim_delegation(state->inode,
1064 data->owner->so_cred,
1065 &data->o_res);
1066 }
Trond Myklebust34310432008-12-23 15:21:38 -05001067
1068 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001069 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001070 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001071out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001072 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001073err_put_inode:
1074 iput(inode);
1075err:
1076 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001077}
1078
Trond Myklebust864472e2006-01-03 09:55:15 +01001079static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1080{
1081 struct nfs_inode *nfsi = NFS_I(state->inode);
1082 struct nfs_open_context *ctx;
1083
1084 spin_lock(&state->inode->i_lock);
1085 list_for_each_entry(ctx, &nfsi->open_files, list) {
1086 if (ctx->state != state)
1087 continue;
1088 get_nfs_open_context(ctx);
1089 spin_unlock(&state->inode->i_lock);
1090 return ctx;
1091 }
1092 spin_unlock(&state->inode->i_lock);
1093 return ERR_PTR(-ENOENT);
1094}
1095
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001096static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1097{
1098 struct nfs4_opendata *opendata;
1099
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001100 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001101 if (opendata == NULL)
1102 return ERR_PTR(-ENOMEM);
1103 opendata->state = state;
1104 atomic_inc(&state->count);
1105 return opendata;
1106}
1107
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001108static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001109{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001110 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001111 int ret;
1112
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001113 opendata->o_arg.open_flags = 0;
1114 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001115 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1116 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1117 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001118 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001119 if (ret != 0)
1120 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001121 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001122 if (IS_ERR(newstate))
1123 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001124 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001125 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001126 return 0;
1127}
1128
1129static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1130{
Trond Myklebust864472e2006-01-03 09:55:15 +01001131 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001132 int ret;
1133
1134 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001135 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001136 smp_rmb();
1137 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001138 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001139 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001140 if (ret != 0)
1141 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001142 if (newstate != state)
1143 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001144 }
1145 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001146 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001147 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001148 if (ret != 0)
1149 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001150 if (newstate != state)
1151 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001152 }
1153 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001154 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001155 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001156 if (ret != 0)
1157 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001158 if (newstate != state)
1159 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001160 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001161 /*
1162 * We may have performed cached opens for all three recoveries.
1163 * Check if we need to update the current stateid.
1164 */
1165 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1166 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001167 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001168 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1169 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001170 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001171 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001172 return 0;
1173}
1174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175/*
1176 * OPEN_RECLAIM:
1177 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001179static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001181 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001182 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001183 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 int status;
1185
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001186 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1187 if (IS_ERR(opendata))
1188 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001189 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1190 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001191 rcu_read_lock();
1192 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001193 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001194 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001195 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001196 opendata->o_arg.u.delegation_type = delegation_type;
1197 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001198 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 return status;
1200}
1201
Trond Myklebust539cd032007-06-05 11:46:42 -04001202static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203{
1204 struct nfs_server *server = NFS_SERVER(state->inode);
1205 struct nfs4_exception exception = { };
1206 int err;
1207 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001208 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001209 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001210 break;
1211 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 } while (exception.retry);
1213 return err;
1214}
1215
Trond Myklebust864472e2006-01-03 09:55:15 +01001216static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1217{
1218 struct nfs_open_context *ctx;
1219 int ret;
1220
1221 ctx = nfs4_state_find_open_context(state);
1222 if (IS_ERR(ctx))
1223 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001224 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001225 put_nfs_open_context(ctx);
1226 return ret;
1227}
1228
Trond Myklebust13437e12007-07-06 15:10:43 -04001229static 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 -07001230{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001231 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001232 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001234 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1235 if (IS_ERR(opendata))
1236 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001237 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001238 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001239 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001240 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001241 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001242 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
1244
Trond Myklebust13437e12007-07-06 15:10:43 -04001245int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
1247 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001248 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 int err;
1250 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001251 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 switch (err) {
1253 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001254 case -ENOENT:
1255 case -ESTALE:
1256 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001257 case -NFS4ERR_BADSESSION:
1258 case -NFS4ERR_BADSLOT:
1259 case -NFS4ERR_BAD_HIGH_SLOT:
1260 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1261 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001262 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001263 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 case -NFS4ERR_STALE_CLIENTID:
1265 case -NFS4ERR_STALE_STATEID:
1266 case -NFS4ERR_EXPIRED:
1267 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001268 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001269 goto out;
1270 case -ERESTARTSYS:
1271 /*
1272 * The show must go on: exit, but mark the
1273 * stateid as needing recovery.
1274 */
1275 case -NFS4ERR_ADMIN_REVOKED:
1276 case -NFS4ERR_BAD_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001277 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001278 case -EKEYEXPIRED:
1279 /*
1280 * User RPCSEC_GSS context has expired.
1281 * We cannot recover this stateid now, so
1282 * skip it and allow recovery thread to
1283 * proceed.
1284 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001285 case -ENOMEM:
1286 err = 0;
1287 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 }
1289 err = nfs4_handle_exception(server, err, &exception);
1290 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001291out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 return err;
1293}
1294
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001295static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1296{
1297 struct nfs4_opendata *data = calldata;
1298
1299 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001300 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001301 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1302 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001303 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001304 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001305 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001306 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001307}
1308
1309static void nfs4_open_confirm_release(void *calldata)
1310{
1311 struct nfs4_opendata *data = calldata;
1312 struct nfs4_state *state = NULL;
1313
1314 /* If this request hasn't been cancelled, do nothing */
1315 if (data->cancelled == 0)
1316 goto out_free;
1317 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001318 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001319 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001320 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001321 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001322 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001323out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001324 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001325}
1326
1327static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001328 .rpc_call_done = nfs4_open_confirm_done,
1329 .rpc_release = nfs4_open_confirm_release,
1330};
1331
1332/*
1333 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1334 */
1335static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1336{
1337 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1338 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001339 struct rpc_message msg = {
1340 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1341 .rpc_argp = &data->c_arg,
1342 .rpc_resp = &data->c_res,
1343 .rpc_cred = data->owner->so_cred,
1344 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001345 struct rpc_task_setup task_setup_data = {
1346 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001347 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001348 .callback_ops = &nfs4_open_confirm_ops,
1349 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001350 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001351 .flags = RPC_TASK_ASYNC,
1352 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 int status;
1354
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001355 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001356 data->rpc_done = 0;
1357 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001358 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001359 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001360 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001361 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001362 status = nfs4_wait_for_completion_rpc_task(task);
1363 if (status != 0) {
1364 data->cancelled = 1;
1365 smp_wmb();
1366 } else
1367 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001368 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return status;
1370}
1371
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001372static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001374 struct nfs4_opendata *data = calldata;
1375 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001376
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001377 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1378 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001379 /*
1380 * Check if we still need to send an OPEN call, or if we can use
1381 * a delegation instead.
1382 */
1383 if (data->state != NULL) {
1384 struct nfs_delegation *delegation;
1385
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001386 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001387 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001388 rcu_read_lock();
1389 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1390 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001391 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001392 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001393 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001394 }
1395 rcu_read_unlock();
1396 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001397 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001398 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001399 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001400 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001401 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001402 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1403 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001404 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001405 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001406 &data->o_arg.seq_args,
1407 &data->o_res.seq_res, 1, task))
1408 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001409 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001410 return;
1411out_no_action:
1412 task->tk_action = NULL;
1413
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001414}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001416static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1417{
1418 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1419 nfs4_open_prepare(task, calldata);
1420}
1421
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001422static void nfs4_open_done(struct rpc_task *task, void *calldata)
1423{
1424 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001426 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001427
Trond Myklebust14516c32010-07-31 14:29:06 -04001428 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1429 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001430
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001431 if (task->tk_status == 0) {
1432 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001433 case S_IFREG:
1434 break;
1435 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001436 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001437 break;
1438 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001439 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001440 break;
1441 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001442 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001443 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001444 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001445 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1446 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001447 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001448 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001449}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001450
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001451static void nfs4_open_release(void *calldata)
1452{
1453 struct nfs4_opendata *data = calldata;
1454 struct nfs4_state *state = NULL;
1455
1456 /* If this request hasn't been cancelled, do nothing */
1457 if (data->cancelled == 0)
1458 goto out_free;
1459 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001460 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001461 goto out_free;
1462 /* In case we need an open_confirm, no cleanup! */
1463 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1464 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001465 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001466 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001467 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001468out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001469 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001470}
1471
1472static const struct rpc_call_ops nfs4_open_ops = {
1473 .rpc_call_prepare = nfs4_open_prepare,
1474 .rpc_call_done = nfs4_open_done,
1475 .rpc_release = nfs4_open_release,
1476};
1477
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001478static const struct rpc_call_ops nfs4_recover_open_ops = {
1479 .rpc_call_prepare = nfs4_recover_open_prepare,
1480 .rpc_call_done = nfs4_open_done,
1481 .rpc_release = nfs4_open_release,
1482};
1483
1484static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001485{
1486 struct inode *dir = data->dir->d_inode;
1487 struct nfs_server *server = NFS_SERVER(dir);
1488 struct nfs_openargs *o_arg = &data->o_arg;
1489 struct nfs_openres *o_res = &data->o_res;
1490 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001491 struct rpc_message msg = {
1492 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1493 .rpc_argp = o_arg,
1494 .rpc_resp = o_res,
1495 .rpc_cred = data->owner->so_cred,
1496 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001497 struct rpc_task_setup task_setup_data = {
1498 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001499 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001500 .callback_ops = &nfs4_open_ops,
1501 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001502 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001503 .flags = RPC_TASK_ASYNC,
1504 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001505 int status;
1506
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001507 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001508 data->rpc_done = 0;
1509 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001510 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001511 if (isrecover)
1512 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001513 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001514 if (IS_ERR(task))
1515 return PTR_ERR(task);
1516 status = nfs4_wait_for_completion_rpc_task(task);
1517 if (status != 0) {
1518 data->cancelled = 1;
1519 smp_wmb();
1520 } else
1521 status = data->rpc_status;
1522 rpc_put_task(task);
1523
1524 return status;
1525}
1526
1527static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1528{
1529 struct inode *dir = data->dir->d_inode;
1530 struct nfs_openres *o_res = &data->o_res;
1531 int status;
1532
1533 status = nfs4_run_open_task(data, 1);
1534 if (status != 0 || !data->rpc_done)
1535 return status;
1536
1537 nfs_refresh_inode(dir, o_res->dir_attr);
1538
1539 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1540 status = _nfs4_proc_open_confirm(data);
1541 if (status != 0)
1542 return status;
1543 }
1544
1545 return status;
1546}
1547
1548/*
1549 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1550 */
1551static int _nfs4_proc_open(struct nfs4_opendata *data)
1552{
1553 struct inode *dir = data->dir->d_inode;
1554 struct nfs_server *server = NFS_SERVER(dir);
1555 struct nfs_openargs *o_arg = &data->o_arg;
1556 struct nfs_openres *o_res = &data->o_res;
1557 int status;
1558
1559 status = nfs4_run_open_task(data, 0);
Trond Myklebust3e309912007-07-07 13:19:59 -04001560 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001561 return status;
1562
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001563 if (o_arg->open_flags & O_CREAT) {
1564 update_changeattr(dir, &o_res->cinfo);
1565 nfs_post_op_update_inode(dir, o_res->dir_attr);
1566 } else
1567 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001568 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1569 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001571 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001573 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 }
1575 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001576 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001577 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578}
1579
Andy Adamsond83217c2011-03-01 01:34:17 +00001580static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001581{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001582 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001583 int ret;
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001584
Trond Myklebusta78cb572009-08-09 15:06:19 -04001585 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001586 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001587 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001588 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001589 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1590 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001591 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001592 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001593 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001594 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001595 return ret;
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001596}
1597
Andy Adamsond83217c2011-03-01 01:34:17 +00001598static int nfs4_recover_expired_lease(struct nfs_server *server)
1599{
1600 return nfs4_client_recover_expired_lease(server->nfs_client);
1601}
1602
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603/*
1604 * OPEN_EXPIRED:
1605 * reclaim state on the server after a network partition.
1606 * Assumes caller holds the appropriate lock
1607 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001608static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001610 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001611 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001613 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1614 if (IS_ERR(opendata))
1615 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001616 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001617 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001618 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001619 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001620 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621}
1622
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001623static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001624{
Trond Myklebust539cd032007-06-05 11:46:42 -04001625 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001626 struct nfs4_exception exception = { };
1627 int err;
1628
1629 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001630 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001631 switch (err) {
1632 default:
1633 goto out;
1634 case -NFS4ERR_GRACE:
1635 case -NFS4ERR_DELAY:
1636 nfs4_handle_exception(server, err, &exception);
1637 err = 0;
1638 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001639 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001640out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001641 return err;
1642}
1643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1645{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001647 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Trond Myklebust864472e2006-01-03 09:55:15 +01001649 ctx = nfs4_state_find_open_context(state);
1650 if (IS_ERR(ctx))
1651 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001652 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001653 put_nfs_open_context(ctx);
1654 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655}
1656
1657/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001658 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1659 * fields corresponding to attributes that were used to store the verifier.
1660 * Make sure we clobber those fields in the later setattr call
1661 */
1662static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1663{
1664 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1665 !(sattr->ia_valid & ATTR_ATIME_SET))
1666 sattr->ia_valid |= ATTR_ATIME;
1667
1668 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1669 !(sattr->ia_valid & ATTR_MTIME_SET))
1670 sattr->ia_valid |= ATTR_MTIME;
1671}
1672
1673/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001674 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001676static 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 -07001677{
1678 struct nfs4_state_owner *sp;
1679 struct nfs4_state *state = NULL;
1680 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001681 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001682 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
1684 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 status = -ENOMEM;
1686 if (!(sp = nfs4_get_state_owner(server, cred))) {
1687 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1688 goto out_err;
1689 }
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001690 status = nfs4_recover_expired_lease(server);
1691 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001692 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001693 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001694 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001695 status = -ENOMEM;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001696 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001697 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001698 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
Trond Myklebustaac00a82007-07-05 19:02:21 -04001700 if (path->dentry->d_inode != NULL)
1701 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1702
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001703 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001705 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001707 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001708 status = PTR_ERR(state);
1709 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001710 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001711 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001712 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001713
1714 if (opendata->o_arg.open_flags & O_EXCL) {
1715 nfs4_exclusive_attrset(opendata, sattr);
1716
1717 nfs_fattr_init(opendata->o_res.f_attr);
1718 status = nfs4_do_setattr(state->inode, cred,
1719 opendata->o_res.f_attr, sattr,
1720 state);
1721 if (status == 0)
1722 nfs_setattr_update_inode(state->inode, sattr);
1723 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1724 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001725 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 *res = state;
1728 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001729err_opendata_put:
1730 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001731err_put_state_owner:
1732 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 *res = NULL;
1735 return status;
1736}
1737
1738
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001739static 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 -07001740{
1741 struct nfs4_exception exception = { };
1742 struct nfs4_state *res;
1743 int status;
1744
1745 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001746 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 if (status == 0)
1748 break;
1749 /* NOTE: BAD_SEQID means the server and client disagree about the
1750 * book-keeping w.r.t. state-changing operations
1751 * (OPEN/CLOSE/LOCK/LOCKU...)
1752 * It is actually a sign of a bug on the client or on the server.
1753 *
1754 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001755 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 * have unhashed the old state_owner for us, and that we can
1757 * therefore safely retry using a new one. We should still warn
1758 * the user though...
1759 */
1760 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001761 printk(KERN_WARNING "NFS: v4 server %s "
1762 " returned a bad sequence-id error!\n",
1763 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 exception.retry = 1;
1765 continue;
1766 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001767 /*
1768 * BAD_STATEID on OPEN means that the server cancelled our
1769 * state before it received the OPEN_CONFIRM.
1770 * Recover by retrying the request as per the discussion
1771 * on Page 181 of RFC3530.
1772 */
1773 if (status == -NFS4ERR_BAD_STATEID) {
1774 exception.retry = 1;
1775 continue;
1776 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001777 if (status == -EAGAIN) {
1778 /* We must have found a delegation */
1779 exception.retry = 1;
1780 continue;
1781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1783 status, &exception));
1784 } while (exception.retry);
1785 return res;
1786}
1787
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001788static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1789 struct nfs_fattr *fattr, struct iattr *sattr,
1790 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001792 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001794 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 .iap = sattr,
1796 .server = server,
1797 .bitmask = server->attr_bitmask,
1798 };
1799 struct nfs_setattrres res = {
1800 .fattr = fattr,
1801 .server = server,
1802 };
1803 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001804 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1805 .rpc_argp = &arg,
1806 .rpc_resp = &res,
1807 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001809 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001810 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
Trond Myklebust0e574af2005-10-27 22:12:38 -04001812 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001814 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1815 /* Use that stateid */
1816 } else if (state != NULL) {
Trond Myklebust77041ed2010-07-01 12:49:11 -04001817 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001818 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1820
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001821 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001822 if (status == 0 && state != NULL)
1823 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001824 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825}
1826
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001827static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1828 struct nfs_fattr *fattr, struct iattr *sattr,
1829 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001831 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 struct nfs4_exception exception = { };
1833 int err;
1834 do {
1835 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001836 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 &exception);
1838 } while (exception.retry);
1839 return err;
1840}
1841
1842struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001843 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 struct inode *inode;
1845 struct nfs4_state *state;
1846 struct nfs_closeargs arg;
1847 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001848 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001849 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00001850 bool roc;
1851 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852};
1853
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001854static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001855{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001856 struct nfs4_closedata *calldata = data;
1857 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001858
Fred Isamanf7e89172011-01-06 11:36:32 +00001859 if (calldata->roc)
1860 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001861 nfs4_put_open_state(calldata->state);
1862 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001863 nfs4_put_state_owner(sp);
Jan Blunck31f31db2008-05-02 13:42:45 -07001864 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001865 kfree(calldata);
1866}
1867
Trond Myklebust88069f72009-12-08 08:33:16 -05001868static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1869 fmode_t fmode)
1870{
1871 spin_lock(&state->owner->so_lock);
1872 if (!(fmode & FMODE_READ))
1873 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1874 if (!(fmode & FMODE_WRITE))
1875 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1876 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1877 spin_unlock(&state->owner->so_lock);
1878}
1879
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001880static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001882 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 struct nfs_server *server = NFS_SERVER(calldata->inode);
1885
Trond Myklebust14516c32010-07-31 14:29:06 -04001886 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1887 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 /* hmm. we are done with the inode, and in the process of freeing
1889 * the state_owner. we keep this around to process errors
1890 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 switch (task->tk_status) {
1892 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00001893 if (calldata->roc)
1894 pnfs_roc_set_barrier(state->inode,
1895 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04001896 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001897 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001898 nfs4_close_clear_stateid_flags(state,
1899 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 break;
1901 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001902 case -NFS4ERR_OLD_STATEID:
1903 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001905 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001906 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001908 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1909 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001911 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001912 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913}
1914
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001915static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001917 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001918 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001919 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001920
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001921 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001922 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001923
Trond Myklebust88069f72009-12-08 08:33:16 -05001924 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1925 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001926 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001927 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001928 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001929 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001930 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1931 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1932 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001933 }
1934 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001935 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1936 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1937 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001938 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001939 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001940 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05001941
1942 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001943 /* Note: exit _without_ calling nfs4_close_done */
1944 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001945 return;
1946 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001947
Fred Isamanf7e89172011-01-06 11:36:32 +00001948 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001949 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00001950 if (calldata->roc &&
1951 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
1952 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
1953 task, NULL);
1954 return;
1955 }
1956 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001957
Trond Myklebust516a6af2005-10-27 22:12:41 -04001958 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001959 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001960 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Andy Adamson19ddab02009-04-01 09:22:20 -04001961 &calldata->arg.seq_args, &calldata->res.seq_res,
1962 1, task))
1963 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001964 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965}
1966
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001967static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001968 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001969 .rpc_call_done = nfs4_close_done,
1970 .rpc_release = nfs4_free_closedata,
1971};
1972
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973/*
1974 * It is possible for data to be read/written from a mem-mapped file
1975 * after the sys_close call (which hits the vfs layer as a flush).
1976 * This means that we can't safely call nfsv4 close on a file until
1977 * the inode is cleared. This in turn means that we are not good
1978 * NFSv4 citizens - we do not indicate to the server to update the file's
1979 * share state even when we are done with one of the three share
1980 * stateid's in the inode.
1981 *
1982 * NOTE: Caller must be holding the sp->so_owner semaphore!
1983 */
Fred Isamanf7e89172011-01-06 11:36:32 +00001984int 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 -07001985{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001986 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001988 struct nfs4_state_owner *sp = state->owner;
1989 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001990 struct rpc_message msg = {
1991 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1992 .rpc_cred = state->owner->so_cred,
1993 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001994 struct rpc_task_setup task_setup_data = {
1995 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001996 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001997 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05001998 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001999 .flags = RPC_TASK_ASYNC,
2000 };
Trond Myklebust95121352005-10-18 14:20:12 -07002001 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002003 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002005 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002006 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002008 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002009 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002011 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002012 if (calldata->arg.seqid == NULL)
2013 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002014 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002015 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002016 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002017 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002018 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002019 calldata->roc = roc;
Al Virof6948692010-01-30 13:51:04 -05002020 path_get(path);
2021 calldata->path = *path;
Trond Myklebust95121352005-10-18 14:20:12 -07002022
Trond Myklebust1174dd12010-12-21 10:52:24 -05002023 msg.rpc_argp = &calldata->arg;
2024 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002025 task_setup_data.callback_data = calldata;
2026 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002027 if (IS_ERR(task))
2028 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002029 status = 0;
2030 if (wait)
2031 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002032 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002033 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002034out_free_calldata:
2035 kfree(calldata);
2036out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002037 if (roc)
2038 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002039 nfs4_put_open_state(state);
2040 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002041 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042}
2043
Trond Myklebust2b484292010-09-17 10:56:51 -04002044static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002045nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 struct nfs4_state *state;
2048
Trond Myklebust565277f2007-10-15 18:17:53 -04002049 /* Protect against concurrent sillydeletes */
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002050 state = nfs4_do_open(dir, &ctx->path, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002051 if (IS_ERR(state))
2052 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002053 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002054 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055}
2056
Trond Myklebust1185a552009-12-03 15:54:02 -05002057static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002058{
2059 if (ctx->state == NULL)
2060 return;
2061 if (is_sync)
2062 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2063 else
2064 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2065}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
2067static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2068{
Benny Halevy43652ad2009-04-01 09:21:54 -04002069 struct nfs4_server_caps_arg args = {
2070 .fhandle = fhandle,
2071 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 struct nfs4_server_caps_res res = {};
2073 struct rpc_message msg = {
2074 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002075 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 .rpc_resp = &res,
2077 };
2078 int status;
2079
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002080 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 if (status == 0) {
2082 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002083 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2084 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2085 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2086 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2087 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2089 server->caps |= NFS_CAP_ACLS;
2090 if (res.has_links != 0)
2091 server->caps |= NFS_CAP_HARDLINKS;
2092 if (res.has_symlinks != 0)
2093 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002094 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2095 server->caps |= NFS_CAP_FILEID;
2096 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2097 server->caps |= NFS_CAP_MODE;
2098 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2099 server->caps |= NFS_CAP_NLINK;
2100 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2101 server->caps |= NFS_CAP_OWNER;
2102 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2103 server->caps |= NFS_CAP_OWNER_GROUP;
2104 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2105 server->caps |= NFS_CAP_ATIME;
2106 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2107 server->caps |= NFS_CAP_CTIME;
2108 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2109 server->caps |= NFS_CAP_MTIME;
2110
Trond Myklebusta65318b2009-03-11 14:10:28 -04002111 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2112 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2113 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 server->acl_bitmask = res.acl_bitmask;
2115 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002116
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 return status;
2118}
2119
Trond Myklebust55a97592006-06-09 09:34:19 -04002120int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121{
2122 struct nfs4_exception exception = { };
2123 int err;
2124 do {
2125 err = nfs4_handle_exception(server,
2126 _nfs4_server_capabilities(server, fhandle),
2127 &exception);
2128 } while (exception.retry);
2129 return err;
2130}
2131
2132static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2133 struct nfs_fsinfo *info)
2134{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 struct nfs4_lookup_root_arg args = {
2136 .bitmask = nfs4_fattr_bitmap,
2137 };
2138 struct nfs4_lookup_res res = {
2139 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002140 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 .fh = fhandle,
2142 };
2143 struct rpc_message msg = {
2144 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2145 .rpc_argp = &args,
2146 .rpc_resp = &res,
2147 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002148
Trond Myklebust0e574af2005-10-27 22:12:38 -04002149 nfs_fattr_init(info->fattr);
Trond Myklebustfccba802009-07-21 16:48:07 -04002150 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151}
2152
2153static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2154 struct nfs_fsinfo *info)
2155{
2156 struct nfs4_exception exception = { };
2157 int err;
2158 do {
2159 err = nfs4_handle_exception(server,
2160 _nfs4_lookup_root(server, fhandle, info),
2161 &exception);
2162 } while (exception.retry);
2163 return err;
2164}
2165
David Howells54ceac42006-08-22 20:06:13 -04002166/*
2167 * get the file handle for the "/" directory on the server
2168 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002170 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 int status;
2173
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 if (status == 0)
2176 status = nfs4_server_capabilities(server, fhandle);
2177 if (status == 0)
2178 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002179 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180}
2181
Manoj Naik6b97fd32006-06-09 09:34:29 -04002182/*
2183 * Get locations and (maybe) other attributes of a referral.
2184 * Note that we'll actually follow the referral later when
2185 * we detect fsid mismatch in inode revalidation
2186 */
Trond Myklebust56659e92007-07-17 21:52:39 -04002187static 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 -04002188{
2189 int status = -ENOMEM;
2190 struct page *page = NULL;
2191 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002192
2193 page = alloc_page(GFP_KERNEL);
2194 if (page == NULL)
2195 goto out;
2196 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2197 if (locations == NULL)
2198 goto out;
2199
Trond Myklebustc228fd32007-01-13 02:28:11 -05002200 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002201 if (status != 0)
2202 goto out;
2203 /* Make sure server returned a different fsid for the referral */
2204 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07002205 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 -04002206 status = -EIO;
2207 goto out;
2208 }
2209
2210 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2211 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2212 if (!fattr->mode)
2213 fattr->mode = S_IFDIR;
2214 memset(fhandle, 0, sizeof(struct nfs_fh));
2215out:
2216 if (page)
2217 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002218 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002219 return status;
2220}
2221
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2223{
2224 struct nfs4_getattr_arg args = {
2225 .fh = fhandle,
2226 .bitmask = server->attr_bitmask,
2227 };
2228 struct nfs4_getattr_res res = {
2229 .fattr = fattr,
2230 .server = server,
2231 };
2232 struct rpc_message msg = {
2233 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2234 .rpc_argp = &args,
2235 .rpc_resp = &res,
2236 };
2237
Trond Myklebust0e574af2005-10-27 22:12:38 -04002238 nfs_fattr_init(fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002239 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240}
2241
2242static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2243{
2244 struct nfs4_exception exception = { };
2245 int err;
2246 do {
2247 err = nfs4_handle_exception(server,
2248 _nfs4_proc_getattr(server, fhandle, fattr),
2249 &exception);
2250 } while (exception.retry);
2251 return err;
2252}
2253
2254/*
2255 * The file is not closed if it is opened due to the a request to change
2256 * the size of the file. The open call will not be needed once the
2257 * VFS layer lookup-intents are implemented.
2258 *
2259 * Close is called when the inode is destroyed.
2260 * If we haven't opened the file for O_WRONLY, we
2261 * need to in the size_change case to obtain a stateid.
2262 *
2263 * Got race?
2264 * Because OPEN is always done by name in nfsv4, it is
2265 * possible that we opened a different file by the same
2266 * name. We can recognize this race condition, but we
2267 * can't do anything about it besides returning an error.
2268 *
2269 * This will be fixed with VFS changes (lookup-intent).
2270 */
2271static int
2272nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2273 struct iattr *sattr)
2274{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002275 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002276 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002277 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 int status;
2279
Trond Myklebust0e574af2005-10-27 22:12:38 -04002280 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
Trond Myklebustd5308382005-11-04 15:33:38 -05002282 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002283 if (sattr->ia_valid & ATTR_FILE) {
2284 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002285
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002286 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002287 if (ctx) {
2288 cred = ctx->cred;
2289 state = ctx->state;
2290 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002291 }
2292
2293 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002294 if (status == 0)
2295 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 return status;
2297}
2298
Trond Myklebust56659e92007-07-17 21:52:39 -04002299static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2300 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04002301 struct nfs_fattr *fattr)
2302{
2303 int status;
2304 struct nfs4_lookup_arg args = {
2305 .bitmask = server->attr_bitmask,
2306 .dir_fh = dirfh,
2307 .name = name,
2308 };
2309 struct nfs4_lookup_res res = {
2310 .server = server,
2311 .fattr = fattr,
2312 .fh = fhandle,
2313 };
2314 struct rpc_message msg = {
2315 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2316 .rpc_argp = &args,
2317 .rpc_resp = &res,
2318 };
2319
2320 nfs_fattr_init(fattr);
2321
2322 dprintk("NFS call lookupfh %s\n", name->name);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002323 status = nfs4_call_sync(server, &msg, &args, &res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002324 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002325 return status;
2326}
2327
2328static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2329 struct qstr *name, struct nfs_fh *fhandle,
2330 struct nfs_fattr *fattr)
2331{
2332 struct nfs4_exception exception = { };
2333 int err;
2334 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04002335 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2336 /* FIXME: !!!! */
2337 if (err == -NFS4ERR_MOVED) {
2338 err = -EREMOTE;
2339 break;
2340 }
2341 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002342 } while (exception.retry);
2343 return err;
2344}
2345
Trond Myklebust56659e92007-07-17 21:52:39 -04002346static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2348{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002349 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
2351 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002352 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002353 if (status == -NFS4ERR_MOVED)
2354 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 dprintk("NFS reply lookup: %d\n", status);
2356 return status;
2357}
2358
2359static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2360{
2361 struct nfs4_exception exception = { };
2362 int err;
2363 do {
2364 err = nfs4_handle_exception(NFS_SERVER(dir),
2365 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2366 &exception);
2367 } while (exception.retry);
2368 return err;
2369}
2370
2371static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2372{
Trond Myklebust76b32992007-08-10 17:45:11 -04002373 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 struct nfs4_accessargs args = {
2375 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002376 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002378 struct nfs4_accessres res = {
2379 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002380 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 struct rpc_message msg = {
2382 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2383 .rpc_argp = &args,
2384 .rpc_resp = &res,
2385 .rpc_cred = entry->cred,
2386 };
2387 int mode = entry->mask;
2388 int status;
2389
2390 /*
2391 * Determine which access bits we want to ask for...
2392 */
2393 if (mode & MAY_READ)
2394 args.access |= NFS4_ACCESS_READ;
2395 if (S_ISDIR(inode->i_mode)) {
2396 if (mode & MAY_WRITE)
2397 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2398 if (mode & MAY_EXEC)
2399 args.access |= NFS4_ACCESS_LOOKUP;
2400 } else {
2401 if (mode & MAY_WRITE)
2402 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2403 if (mode & MAY_EXEC)
2404 args.access |= NFS4_ACCESS_EXECUTE;
2405 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002406
2407 res.fattr = nfs_alloc_fattr();
2408 if (res.fattr == NULL)
2409 return -ENOMEM;
2410
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002411 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 if (!status) {
2413 entry->mask = 0;
2414 if (res.access & NFS4_ACCESS_READ)
2415 entry->mask |= MAY_READ;
2416 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2417 entry->mask |= MAY_WRITE;
2418 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2419 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002420 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002422 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 return status;
2424}
2425
2426static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2427{
2428 struct nfs4_exception exception = { };
2429 int err;
2430 do {
2431 err = nfs4_handle_exception(NFS_SERVER(inode),
2432 _nfs4_proc_access(inode, entry),
2433 &exception);
2434 } while (exception.retry);
2435 return err;
2436}
2437
2438/*
2439 * TODO: For the time being, we don't try to get any attributes
2440 * along with any of the zero-copy operations READ, READDIR,
2441 * READLINK, WRITE.
2442 *
2443 * In the case of the first three, we want to put the GETATTR
2444 * after the read-type operation -- this is because it is hard
2445 * to predict the length of a GETATTR response in v4, and thus
2446 * align the READ data correctly. This means that the GETATTR
2447 * may end up partially falling into the page cache, and we should
2448 * shift it into the 'tail' of the xdr_buf before processing.
2449 * To do this efficiently, we need to know the total length
2450 * of data received, which doesn't seem to be available outside
2451 * of the RPC layer.
2452 *
2453 * In the case of WRITE, we also want to put the GETATTR after
2454 * the operation -- in this case because we want to make sure
2455 * we get the post-operation mtime and size. This means that
2456 * we can't use xdr_encode_pages() as written: we need a variant
2457 * of it which would leave room in the 'tail' iovec.
2458 *
2459 * Both of these changes to the XDR layer would in fact be quite
2460 * minor, but I decided to leave them for a subsequent patch.
2461 */
2462static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2463 unsigned int pgbase, unsigned int pglen)
2464{
2465 struct nfs4_readlink args = {
2466 .fh = NFS_FH(inode),
2467 .pgbase = pgbase,
2468 .pglen = pglen,
2469 .pages = &page,
2470 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002471 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 struct rpc_message msg = {
2473 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2474 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002475 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 };
2477
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002478 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479}
2480
2481static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2482 unsigned int pgbase, unsigned int pglen)
2483{
2484 struct nfs4_exception exception = { };
2485 int err;
2486 do {
2487 err = nfs4_handle_exception(NFS_SERVER(inode),
2488 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2489 &exception);
2490 } while (exception.retry);
2491 return err;
2492}
2493
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494/*
2495 * Got race?
2496 * We will need to arrange for the VFS layer to provide an atomic open.
2497 * Until then, this create/open method is prone to inefficiency and race
2498 * conditions due to the lookup, create, and open VFS calls from sys_open()
2499 * placed on the wire.
2500 *
2501 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2502 * The file will be opened again in the subsequent VFS open call
2503 * (nfs4_proc_file_open).
2504 *
2505 * The open for read will just hang around to be used by any process that
2506 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2507 */
2508
2509static int
2510nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002511 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512{
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002513 struct path my_path = {
Trond Myklebustad389da2007-06-05 12:30:00 -04002514 .dentry = dentry,
2515 };
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002516 struct path *path = &my_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002518 struct rpc_cred *cred = NULL;
2519 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 int status = 0;
2521
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002522 if (ctx != NULL) {
2523 cred = ctx->cred;
2524 path = &ctx->path;
2525 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002527 sattr->ia_mode &= ~current_umask();
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002528 state = nfs4_do_open(dir, path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002529 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 if (IS_ERR(state)) {
2531 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002532 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002534 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002535 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002536 if (ctx != NULL)
2537 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002538 else
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002539 nfs4_close_sync(path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540out:
2541 return status;
2542}
2543
2544static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2545{
Trond Myklebust16e42952005-10-27 22:12:44 -04002546 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002547 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002549 .name.len = name->len,
2550 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002551 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002553 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002554 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002555 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002557 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2558 .rpc_argp = &args,
2559 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002561 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562
Trond Myklebustd3468902010-04-16 16:22:50 -04002563 res.dir_attr = nfs_alloc_fattr();
2564 if (res.dir_attr == NULL)
2565 goto out;
2566
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002567 status = nfs4_call_sync(server, &msg, &args, &res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002568 if (status == 0) {
2569 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002570 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002571 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002572 nfs_free_fattr(res.dir_attr);
2573out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 return status;
2575}
2576
2577static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2578{
2579 struct nfs4_exception exception = { };
2580 int err;
2581 do {
2582 err = nfs4_handle_exception(NFS_SERVER(dir),
2583 _nfs4_proc_remove(dir, name),
2584 &exception);
2585 } while (exception.retry);
2586 return err;
2587}
2588
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002589static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002591 struct nfs_server *server = NFS_SERVER(dir);
2592 struct nfs_removeargs *args = msg->rpc_argp;
2593 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
Trond Myklebusta65318b2009-03-11 14:10:28 -04002595 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002596 res->server = server;
Benny Halevydfb4f302010-09-24 09:17:01 -04002597 res->seq_res.sr_slot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599}
2600
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002601static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002603 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2604
Trond Myklebust14516c32010-07-31 14:29:06 -04002605 if (!nfs4_sequence_done(task, &res->seq_res))
2606 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002607 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002608 return 0;
2609 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002610 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002611 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612}
2613
Jeff Laytond3d41522010-09-17 17:31:57 -04002614static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2615{
2616 struct nfs_server *server = NFS_SERVER(dir);
2617 struct nfs_renameargs *arg = msg->rpc_argp;
2618 struct nfs_renameres *res = msg->rpc_resp;
2619
2620 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2621 arg->bitmask = server->attr_bitmask;
2622 res->server = server;
2623}
2624
2625static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2626 struct inode *new_dir)
2627{
2628 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2629
2630 if (!nfs4_sequence_done(task, &res->seq_res))
2631 return 0;
2632 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2633 return 0;
2634
2635 update_changeattr(old_dir, &res->old_cinfo);
2636 nfs_post_op_update_inode(old_dir, res->old_fattr);
2637 update_changeattr(new_dir, &res->new_cinfo);
2638 nfs_post_op_update_inode(new_dir, res->new_fattr);
2639 return 1;
2640}
2641
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2643 struct inode *new_dir, struct qstr *new_name)
2644{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002645 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002646 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 .old_dir = NFS_FH(old_dir),
2648 .new_dir = NFS_FH(new_dir),
2649 .old_name = old_name,
2650 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002651 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002653 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002654 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002655 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 struct rpc_message msg = {
2657 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2658 .rpc_argp = &arg,
2659 .rpc_resp = &res,
2660 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002661 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662
Trond Myklebust011fff72010-04-16 16:22:49 -04002663 res.old_fattr = nfs_alloc_fattr();
2664 res.new_fattr = nfs_alloc_fattr();
2665 if (res.old_fattr == NULL || res.new_fattr == NULL)
2666 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667
Trond Myklebust011fff72010-04-16 16:22:49 -04002668 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 if (!status) {
2670 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002671 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002673 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002675out:
2676 nfs_free_fattr(res.new_fattr);
2677 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 return status;
2679}
2680
2681static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2682 struct inode *new_dir, struct qstr *new_name)
2683{
2684 struct nfs4_exception exception = { };
2685 int err;
2686 do {
2687 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2688 _nfs4_proc_rename(old_dir, old_name,
2689 new_dir, new_name),
2690 &exception);
2691 } while (exception.retry);
2692 return err;
2693}
2694
2695static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2696{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002697 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 struct nfs4_link_arg arg = {
2699 .fh = NFS_FH(inode),
2700 .dir_fh = NFS_FH(dir),
2701 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002702 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002704 struct nfs4_link_res res = {
2705 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002706 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 struct rpc_message msg = {
2708 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2709 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002710 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002712 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
Trond Myklebust136f2622010-04-16 16:22:49 -04002714 res.fattr = nfs_alloc_fattr();
2715 res.dir_attr = nfs_alloc_fattr();
2716 if (res.fattr == NULL || res.dir_attr == NULL)
2717 goto out;
2718
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002719 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002720 if (!status) {
2721 update_changeattr(dir, &res.cinfo);
2722 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002723 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002724 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002725out:
2726 nfs_free_fattr(res.dir_attr);
2727 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 return status;
2729}
2730
2731static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2732{
2733 struct nfs4_exception exception = { };
2734 int err;
2735 do {
2736 err = nfs4_handle_exception(NFS_SERVER(inode),
2737 _nfs4_proc_link(inode, dir, name),
2738 &exception);
2739 } while (exception.retry);
2740 return err;
2741}
2742
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002743struct nfs4_createdata {
2744 struct rpc_message msg;
2745 struct nfs4_create_arg arg;
2746 struct nfs4_create_res res;
2747 struct nfs_fh fh;
2748 struct nfs_fattr fattr;
2749 struct nfs_fattr dir_fattr;
2750};
2751
2752static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2753 struct qstr *name, struct iattr *sattr, u32 ftype)
2754{
2755 struct nfs4_createdata *data;
2756
2757 data = kzalloc(sizeof(*data), GFP_KERNEL);
2758 if (data != NULL) {
2759 struct nfs_server *server = NFS_SERVER(dir);
2760
2761 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2762 data->msg.rpc_argp = &data->arg;
2763 data->msg.rpc_resp = &data->res;
2764 data->arg.dir_fh = NFS_FH(dir);
2765 data->arg.server = server;
2766 data->arg.name = name;
2767 data->arg.attrs = sattr;
2768 data->arg.ftype = ftype;
2769 data->arg.bitmask = server->attr_bitmask;
2770 data->res.server = server;
2771 data->res.fh = &data->fh;
2772 data->res.fattr = &data->fattr;
2773 data->res.dir_fattr = &data->dir_fattr;
2774 nfs_fattr_init(data->res.fattr);
2775 nfs_fattr_init(data->res.dir_fattr);
2776 }
2777 return data;
2778}
2779
2780static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2781{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002782 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2783 &data->arg, &data->res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002784 if (status == 0) {
2785 update_changeattr(dir, &data->res.dir_cinfo);
2786 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2787 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2788 }
2789 return status;
2790}
2791
2792static void nfs4_free_createdata(struct nfs4_createdata *data)
2793{
2794 kfree(data);
2795}
2796
Chuck Lever4f390c12006-08-22 20:06:22 -04002797static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002798 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002800 struct nfs4_createdata *data;
2801 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802
Chuck Lever94a6d752006-08-22 20:06:23 -04002803 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002804 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002805
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002806 status = -ENOMEM;
2807 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2808 if (data == NULL)
2809 goto out;
2810
2811 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2812 data->arg.u.symlink.pages = &page;
2813 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002815 status = nfs4_do_create(dir, dentry, data);
2816
2817 nfs4_free_createdata(data);
2818out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 return status;
2820}
2821
Chuck Lever4f390c12006-08-22 20:06:22 -04002822static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002823 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824{
2825 struct nfs4_exception exception = { };
2826 int err;
2827 do {
2828 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002829 _nfs4_proc_symlink(dir, dentry, page,
2830 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 &exception);
2832 } while (exception.retry);
2833 return err;
2834}
2835
2836static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2837 struct iattr *sattr)
2838{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002839 struct nfs4_createdata *data;
2840 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002842 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2843 if (data == NULL)
2844 goto out;
2845
2846 status = nfs4_do_create(dir, dentry, data);
2847
2848 nfs4_free_createdata(data);
2849out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 return status;
2851}
2852
2853static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2854 struct iattr *sattr)
2855{
2856 struct nfs4_exception exception = { };
2857 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002858
2859 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 do {
2861 err = nfs4_handle_exception(NFS_SERVER(dir),
2862 _nfs4_proc_mkdir(dir, dentry, sattr),
2863 &exception);
2864 } while (exception.retry);
2865 return err;
2866}
2867
2868static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002869 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870{
2871 struct inode *dir = dentry->d_inode;
2872 struct nfs4_readdir_arg args = {
2873 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002874 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 .pgbase = 0,
2876 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002877 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04002878 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 };
2880 struct nfs4_readdir_res res;
2881 struct rpc_message msg = {
2882 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2883 .rpc_argp = &args,
2884 .rpc_resp = &res,
2885 .rpc_cred = cred,
2886 };
2887 int status;
2888
Harvey Harrison3110ff82008-05-02 13:42:44 -07002889 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002890 dentry->d_parent->d_name.name,
2891 dentry->d_name.name,
2892 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2894 res.pgbase = args.pgbase;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002895 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
Trond Myklebustac396122010-11-15 20:26:22 -05002896 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05002898 status += args.pgbase;
2899 }
Trond Myklebustc4812992007-09-28 17:11:45 -04002900
2901 nfs_invalidate_atime(dir);
2902
Harvey Harrison3110ff82008-05-02 13:42:44 -07002903 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 return status;
2905}
2906
2907static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002908 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909{
2910 struct nfs4_exception exception = { };
2911 int err;
2912 do {
2913 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2914 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002915 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 &exception);
2917 } while (exception.retry);
2918 return err;
2919}
2920
2921static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2922 struct iattr *sattr, dev_t rdev)
2923{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002924 struct nfs4_createdata *data;
2925 int mode = sattr->ia_mode;
2926 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927
2928 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2929 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002930
2931 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2932 if (data == NULL)
2933 goto out;
2934
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002936 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002938 data->arg.ftype = NF4BLK;
2939 data->arg.u.device.specdata1 = MAJOR(rdev);
2940 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 }
2942 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002943 data->arg.ftype = NF4CHR;
2944 data->arg.u.device.specdata1 = MAJOR(rdev);
2945 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002948 status = nfs4_do_create(dir, dentry, data);
2949
2950 nfs4_free_createdata(data);
2951out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 return status;
2953}
2954
2955static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2956 struct iattr *sattr, dev_t rdev)
2957{
2958 struct nfs4_exception exception = { };
2959 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002960
2961 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 do {
2963 err = nfs4_handle_exception(NFS_SERVER(dir),
2964 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2965 &exception);
2966 } while (exception.retry);
2967 return err;
2968}
2969
2970static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2971 struct nfs_fsstat *fsstat)
2972{
2973 struct nfs4_statfs_arg args = {
2974 .fh = fhandle,
2975 .bitmask = server->attr_bitmask,
2976 };
Benny Halevy24ad1482009-04-01 09:21:56 -04002977 struct nfs4_statfs_res res = {
2978 .fsstat = fsstat,
2979 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 struct rpc_message msg = {
2981 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2982 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04002983 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 };
2985
Trond Myklebust0e574af2005-10-27 22:12:38 -04002986 nfs_fattr_init(fsstat->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002987 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988}
2989
2990static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2991{
2992 struct nfs4_exception exception = { };
2993 int err;
2994 do {
2995 err = nfs4_handle_exception(server,
2996 _nfs4_proc_statfs(server, fhandle, fsstat),
2997 &exception);
2998 } while (exception.retry);
2999 return err;
3000}
3001
3002static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3003 struct nfs_fsinfo *fsinfo)
3004{
3005 struct nfs4_fsinfo_arg args = {
3006 .fh = fhandle,
3007 .bitmask = server->attr_bitmask,
3008 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003009 struct nfs4_fsinfo_res res = {
3010 .fsinfo = fsinfo,
3011 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 struct rpc_message msg = {
3013 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3014 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003015 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 };
3017
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003018 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019}
3020
3021static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3022{
3023 struct nfs4_exception exception = { };
3024 int err;
3025
3026 do {
3027 err = nfs4_handle_exception(server,
3028 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3029 &exception);
3030 } while (exception.retry);
3031 return err;
3032}
3033
3034static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3035{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003036 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3038}
3039
3040static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3041 struct nfs_pathconf *pathconf)
3042{
3043 struct nfs4_pathconf_arg args = {
3044 .fh = fhandle,
3045 .bitmask = server->attr_bitmask,
3046 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003047 struct nfs4_pathconf_res res = {
3048 .pathconf = pathconf,
3049 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 struct rpc_message msg = {
3051 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3052 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003053 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 };
3055
3056 /* None of the pathconf attributes are mandatory to implement */
3057 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3058 memset(pathconf, 0, sizeof(*pathconf));
3059 return 0;
3060 }
3061
Trond Myklebust0e574af2005-10-27 22:12:38 -04003062 nfs_fattr_init(pathconf->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003063 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064}
3065
3066static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3067 struct nfs_pathconf *pathconf)
3068{
3069 struct nfs4_exception exception = { };
3070 int err;
3071
3072 do {
3073 err = nfs4_handle_exception(server,
3074 _nfs4_proc_pathconf(server, fhandle, pathconf),
3075 &exception);
3076 } while (exception.retry);
3077 return err;
3078}
3079
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003080static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081{
Trond Myklebustec06c092006-03-20 13:44:27 -05003082 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003084 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003085 nfs_restart_rpc(task, server->nfs_client);
Trond Myklebustec06c092006-03-20 13:44:27 -05003086 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003088
3089 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003091 renew_lease(server, data->timestamp);
3092 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093}
3094
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003095static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3096{
3097
3098 dprintk("--> %s\n", __func__);
3099
3100 if (!nfs4_sequence_done(task, &data->res.seq_res))
3101 return -EAGAIN;
3102
3103 return data->read_done_cb(task, data);
3104}
3105
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003106static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003109 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003110 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111}
3112
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003113/* Reset the the nfs_read_data to send the read to the MDS. */
3114void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3115{
3116 dprintk("%s Reset task for i/o through\n", __func__);
3117 put_lseg(data->lseg);
3118 data->lseg = NULL;
3119 /* offsets will differ in the dense stripe case */
3120 data->args.offset = data->mds_offset;
3121 data->ds_clp = NULL;
3122 data->args.fh = NFS_FH(data->inode);
3123 data->read_done_cb = nfs4_read_done_cb;
3124 task->tk_ops = data->mds_ops;
3125 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3126}
3127EXPORT_SYMBOL_GPL(nfs4_reset_read);
3128
Fred Isamanb029bc92011-03-03 15:13:42 +00003129static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 struct inode *inode = data->inode;
3132
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003133 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003134 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003135 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003137 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003139 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003140 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003141 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142}
3143
Fred Isamanb029bc92011-03-03 15:13:42 +00003144static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3145{
3146 if (!nfs4_sequence_done(task, &data->res.seq_res))
3147 return -EAGAIN;
3148 return data->write_done_cb(task, data);
3149}
3150
Fred Isamana69aef12011-03-03 15:13:47 +00003151/* Reset the the nfs_write_data to send the write to the MDS. */
3152void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3153{
3154 dprintk("%s Reset task for i/o through\n", __func__);
3155 put_lseg(data->lseg);
3156 data->lseg = NULL;
3157 data->ds_clp = NULL;
3158 data->write_done_cb = nfs4_write_done_cb;
3159 data->args.fh = NFS_FH(data->inode);
3160 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3161 data->args.offset = data->mds_offset;
3162 data->res.fattr = &data->fattr;
3163 task->tk_ops = data->mds_ops;
3164 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3165}
3166EXPORT_SYMBOL_GPL(nfs4_reset_write);
3167
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003168static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003170 struct nfs_server *server = NFS_SERVER(data->inode);
3171
Fred Isaman7ffd1062011-03-03 15:13:46 +00003172 if (data->lseg) {
3173 data->args.bitmask = NULL;
3174 data->res.fattr = NULL;
3175 } else
3176 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isamanb029bc92011-03-03 15:13:42 +00003177 if (!data->write_done_cb)
3178 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003179 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 data->timestamp = jiffies;
3181
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003182 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183}
3184
Trond Myklebust788e7a82006-03-20 13:44:27 -05003185static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 struct inode *inode = data->inode;
3188
Trond Myklebust14516c32010-07-31 14:29:06 -04003189 if (!nfs4_sequence_done(task, &data->res.seq_res))
3190 return -EAGAIN;
3191
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003192 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003193 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003194 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003196 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003197 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198}
3199
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003200static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003202 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203
Trond Myklebusta65318b2009-03-11 14:10:28 -04003204 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003205 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003206 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207}
3208
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003209struct nfs4_renewdata {
3210 struct nfs_client *client;
3211 unsigned long timestamp;
3212};
3213
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214/*
3215 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3216 * standalone procedure for queueing an asynchronous RENEW.
3217 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003218static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003219{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003220 struct nfs4_renewdata *data = calldata;
3221 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003222
Alexandros Batsakis0851de02010-02-05 03:45:06 -08003223 if (atomic_read(&clp->cl_count) > 1)
3224 nfs4_schedule_state_renewal(clp);
3225 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003226 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003227}
3228
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003229static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003231 struct nfs4_renewdata *data = calldata;
3232 struct nfs_client *clp = data->client;
3233 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234
3235 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003236 /* Unless we're shutting down, schedule state recovery! */
3237 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003238 nfs4_schedule_lease_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 return;
3240 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003241 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242}
3243
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003244static const struct rpc_call_ops nfs4_renew_ops = {
3245 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003246 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003247};
3248
David Howellsadfa6f92006-08-22 20:06:08 -04003249int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250{
3251 struct rpc_message msg = {
3252 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3253 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003254 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003256 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257
Alexandros Batsakis0851de02010-02-05 03:45:06 -08003258 if (!atomic_inc_not_zero(&clp->cl_count))
3259 return -EIO;
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003260 data = kmalloc(sizeof(*data), GFP_KERNEL);
3261 if (data == NULL)
3262 return -ENOMEM;
3263 data->client = clp;
3264 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003266 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267}
3268
David Howellsadfa6f92006-08-22 20:06:08 -04003269int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270{
3271 struct rpc_message msg = {
3272 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3273 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003274 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 };
3276 unsigned long now = jiffies;
3277 int status;
3278
3279 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3280 if (status < 0)
3281 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003282 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 return 0;
3284}
3285
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003286static inline int nfs4_server_supports_acls(struct nfs_server *server)
3287{
3288 return (server->caps & NFS_CAP_ACLS)
3289 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3290 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3291}
3292
3293/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3294 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3295 * the stack.
3296 */
3297#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3298
3299static void buf_to_pages(const void *buf, size_t buflen,
3300 struct page **pages, unsigned int *pgbase)
3301{
3302 const void *p = buf;
3303
3304 *pgbase = offset_in_page(buf);
3305 p -= *pgbase;
3306 while (p < buf + buflen) {
3307 *(pages++) = virt_to_page(p);
3308 p += PAGE_CACHE_SIZE;
3309 }
3310}
3311
Neil Hormane9e3d722011-03-04 19:26:03 -05003312static int buf_to_pages_noslab(const void *buf, size_t buflen,
3313 struct page **pages, unsigned int *pgbase)
3314{
3315 struct page *newpage, **spages;
3316 int rc = 0;
3317 size_t len;
3318 spages = pages;
3319
3320 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003321 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003322 newpage = alloc_page(GFP_KERNEL);
3323
3324 if (newpage == NULL)
3325 goto unwind;
3326 memcpy(page_address(newpage), buf, len);
3327 buf += len;
3328 buflen -= len;
3329 *pages++ = newpage;
3330 rc++;
3331 } while (buflen != 0);
3332
3333 return rc;
3334
3335unwind:
3336 for(; rc > 0; rc--)
3337 __free_page(spages[rc-1]);
3338 return -ENOMEM;
3339}
3340
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003341struct nfs4_cached_acl {
3342 int cached;
3343 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003344 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003345};
3346
3347static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003348{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003349 struct nfs_inode *nfsi = NFS_I(inode);
3350
3351 spin_lock(&inode->i_lock);
3352 kfree(nfsi->nfs4_acl);
3353 nfsi->nfs4_acl = acl;
3354 spin_unlock(&inode->i_lock);
3355}
3356
3357static void nfs4_zap_acl_attr(struct inode *inode)
3358{
3359 nfs4_set_cached_acl(inode, NULL);
3360}
3361
3362static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3363{
3364 struct nfs_inode *nfsi = NFS_I(inode);
3365 struct nfs4_cached_acl *acl;
3366 int ret = -ENOENT;
3367
3368 spin_lock(&inode->i_lock);
3369 acl = nfsi->nfs4_acl;
3370 if (acl == NULL)
3371 goto out;
3372 if (buf == NULL) /* user is just asking for length */
3373 goto out_len;
3374 if (acl->cached == 0)
3375 goto out;
3376 ret = -ERANGE; /* see getxattr(2) man page */
3377 if (acl->len > buflen)
3378 goto out;
3379 memcpy(buf, acl->data, acl->len);
3380out_len:
3381 ret = acl->len;
3382out:
3383 spin_unlock(&inode->i_lock);
3384 return ret;
3385}
3386
3387static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3388{
3389 struct nfs4_cached_acl *acl;
3390
3391 if (buf && acl_len <= PAGE_SIZE) {
3392 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3393 if (acl == NULL)
3394 goto out;
3395 acl->cached = 1;
3396 memcpy(acl->data, buf, acl_len);
3397 } else {
3398 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3399 if (acl == NULL)
3400 goto out;
3401 acl->cached = 0;
3402 }
3403 acl->len = acl_len;
3404out:
3405 nfs4_set_cached_acl(inode, acl);
3406}
3407
Trond Myklebust16b42892006-08-24 12:27:15 -04003408static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003409{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003410 struct page *pages[NFS4ACL_MAXPAGES];
3411 struct nfs_getaclargs args = {
3412 .fh = NFS_FH(inode),
3413 .acl_pages = pages,
3414 .acl_len = buflen,
3415 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003416 struct nfs_getaclres res = {
3417 .acl_len = buflen,
3418 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003419 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003420 struct rpc_message msg = {
3421 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3422 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003423 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003424 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003425 struct page *localpage = NULL;
3426 int ret;
3427
3428 if (buflen < PAGE_SIZE) {
3429 /* As long as we're doing a round trip to the server anyway,
3430 * let's be prepared for a page of acl data. */
3431 localpage = alloc_page(GFP_KERNEL);
3432 resp_buf = page_address(localpage);
3433 if (localpage == NULL)
3434 return -ENOMEM;
3435 args.acl_pages[0] = localpage;
3436 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003437 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003438 } else {
3439 resp_buf = buf;
3440 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3441 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003442 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003443 if (ret)
3444 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003445 if (res.acl_len > args.acl_len)
3446 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003447 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003448 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003449 if (buf) {
3450 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003451 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003452 goto out_free;
3453 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003454 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003455 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003456 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003457out_free:
3458 if (localpage)
3459 __free_page(localpage);
3460 return ret;
3461}
3462
Trond Myklebust16b42892006-08-24 12:27:15 -04003463static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3464{
3465 struct nfs4_exception exception = { };
3466 ssize_t ret;
3467 do {
3468 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3469 if (ret >= 0)
3470 break;
3471 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3472 } while (exception.retry);
3473 return ret;
3474}
3475
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003476static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3477{
3478 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003479 int ret;
3480
3481 if (!nfs4_server_supports_acls(server))
3482 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003483 ret = nfs_revalidate_inode(server, inode);
3484 if (ret < 0)
3485 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003486 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3487 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003488 ret = nfs4_read_cached_acl(inode, buf, buflen);
3489 if (ret != -ENOENT)
3490 return ret;
3491 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003492}
3493
Trond Myklebust16b42892006-08-24 12:27:15 -04003494static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003495{
3496 struct nfs_server *server = NFS_SERVER(inode);
3497 struct page *pages[NFS4ACL_MAXPAGES];
3498 struct nfs_setaclargs arg = {
3499 .fh = NFS_FH(inode),
3500 .acl_pages = pages,
3501 .acl_len = buflen,
3502 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003503 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003504 struct rpc_message msg = {
3505 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3506 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003507 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003508 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003509 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003510
3511 if (!nfs4_server_supports_acls(server))
3512 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003513 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3514 if (i < 0)
3515 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003516 nfs_inode_return_delegation(inode);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003517 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003518
3519 /*
3520 * Free each page after tx, so the only ref left is
3521 * held by the network stack
3522 */
3523 for (; i > 0; i--)
3524 put_page(pages[i-1]);
3525
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003526 /*
3527 * Acl update can result in inode attribute update.
3528 * so mark the attribute cache invalid.
3529 */
3530 spin_lock(&inode->i_lock);
3531 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3532 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003533 nfs_access_zap_cache(inode);
3534 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003535 return ret;
3536}
3537
Trond Myklebust16b42892006-08-24 12:27:15 -04003538static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3539{
3540 struct nfs4_exception exception = { };
3541 int err;
3542 do {
3543 err = nfs4_handle_exception(NFS_SERVER(inode),
3544 __nfs4_proc_set_acl(inode, buf, buflen),
3545 &exception);
3546 } while (exception.retry);
3547 return err;
3548}
3549
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003551nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003553 struct nfs_client *clp = server->nfs_client;
3554
3555 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 return 0;
3557 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003558 case -NFS4ERR_ADMIN_REVOKED:
3559 case -NFS4ERR_BAD_STATEID:
3560 case -NFS4ERR_OPENMODE:
3561 if (state == NULL)
3562 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003563 nfs4_schedule_stateid_recovery(server, state);
3564 goto wait_on_recovery;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003566 case -NFS4ERR_STALE_CLIENTID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 case -NFS4ERR_EXPIRED:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003568 nfs4_schedule_lease_recovery(clp);
3569 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003570#if defined(CONFIG_NFS_V4_1)
3571 case -NFS4ERR_BADSESSION:
3572 case -NFS4ERR_BADSLOT:
3573 case -NFS4ERR_BAD_HIGH_SLOT:
3574 case -NFS4ERR_DEADSESSION:
3575 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3576 case -NFS4ERR_SEQ_FALSE_RETRY:
3577 case -NFS4ERR_SEQ_MISORDERED:
3578 dprintk("%s ERROR %d, Reset session\n", __func__,
3579 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003580 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -04003581 task->tk_status = 0;
3582 return -EAGAIN;
3583#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003585 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003586 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003587 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3589 task->tk_status = 0;
3590 return -EAGAIN;
3591 case -NFS4ERR_OLD_STATEID:
3592 task->tk_status = 0;
3593 return -EAGAIN;
3594 }
3595 task->tk_status = nfs4_map_errors(task->tk_status);
3596 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003597wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003598 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003599 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3600 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3601 task->tk_status = 0;
3602 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603}
3604
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003605int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3606 unsigned short port, struct rpc_cred *cred,
3607 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608{
3609 nfs4_verifier sc_verifier;
3610 struct nfs4_setclientid setclientid = {
3611 .sc_verifier = &sc_verifier,
3612 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003613 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 };
3615 struct rpc_message msg = {
3616 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3617 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003618 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003619 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 };
Al Virobc4785c2006-10-19 23:28:51 -07003621 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 int loop = 0;
3623 int status;
3624
Al Virobc4785c2006-10-19 23:28:51 -07003625 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3627 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3628
3629 for(;;) {
3630 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003631 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003632 clp->cl_ipaddr,
3633 rpc_peeraddr2str(clp->cl_rpcclient,
3634 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003635 rpc_peeraddr2str(clp->cl_rpcclient,
3636 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003637 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 clp->cl_id_uniquifier);
3639 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003640 sizeof(setclientid.sc_netid),
3641 rpc_peeraddr2str(clp->cl_rpcclient,
3642 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003644 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 clp->cl_ipaddr, port >> 8, port & 255);
3646
3647 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3648 if (status != -NFS4ERR_CLID_INUSE)
3649 break;
3650 if (signalled())
3651 break;
3652 if (loop++ & 1)
J. Bruce Fields611c96c2010-12-13 19:05:46 -05003653 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 else
3655 if (++clp->cl_id_uniquifier == 0)
3656 break;
3657 }
3658 return status;
3659}
3660
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003661static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3662 struct nfs4_setclientid_res *arg,
3663 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664{
3665 struct nfs_fsinfo fsinfo;
3666 struct rpc_message msg = {
3667 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003668 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003670 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 };
3672 unsigned long now;
3673 int status;
3674
3675 now = jiffies;
3676 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3677 if (status == 0) {
3678 spin_lock(&clp->cl_lock);
3679 clp->cl_lease_time = fsinfo.lease_time * HZ;
3680 clp->cl_last_renewal = now;
3681 spin_unlock(&clp->cl_lock);
3682 }
3683 return status;
3684}
3685
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003686int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3687 struct nfs4_setclientid_res *arg,
3688 struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003689{
Benny Halevy77e03672008-06-24 20:25:57 +03003690 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003691 int err;
3692 do {
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003693 err = _nfs4_proc_setclientid_confirm(clp, arg, cred);
Trond Myklebust51581f32006-03-20 13:44:47 -05003694 switch (err) {
3695 case 0:
3696 return err;
3697 case -NFS4ERR_RESOURCE:
3698 /* The IBM lawyers misread another document! */
3699 case -NFS4ERR_DELAY:
3700 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3701 }
3702 } while (err == 0);
3703 return err;
3704}
3705
Trond Myklebustfe650402006-01-03 09:55:18 +01003706struct nfs4_delegreturndata {
3707 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003708 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003709 struct nfs_fh fh;
3710 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003711 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003712 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003713 int rpc_status;
3714};
3715
Trond Myklebustfe650402006-01-03 09:55:18 +01003716static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3717{
3718 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003719
Trond Myklebust14516c32010-07-31 14:29:06 -04003720 if (!nfs4_sequence_done(task, &data->res.seq_res))
3721 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003722
Ricardo Labiaga79708862009-12-07 09:23:21 -05003723 switch (task->tk_status) {
3724 case -NFS4ERR_STALE_STATEID:
3725 case -NFS4ERR_EXPIRED:
3726 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003727 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003728 break;
3729 default:
3730 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3731 -EAGAIN) {
3732 nfs_restart_rpc(task, data->res.server->nfs_client);
3733 return;
3734 }
3735 }
3736 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003737}
3738
3739static void nfs4_delegreturn_release(void *calldata)
3740{
Trond Myklebustfe650402006-01-03 09:55:18 +01003741 kfree(calldata);
3742}
3743
Andy Adamson938e1012009-04-01 09:22:28 -04003744#if defined(CONFIG_NFS_V4_1)
3745static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3746{
3747 struct nfs4_delegreturndata *d_data;
3748
3749 d_data = (struct nfs4_delegreturndata *)data;
3750
Trond Myklebust035168a2010-06-16 09:52:26 -04003751 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003752 &d_data->args.seq_args,
3753 &d_data->res.seq_res, 1, task))
3754 return;
3755 rpc_call_start(task);
3756}
3757#endif /* CONFIG_NFS_V4_1 */
3758
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003759static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003760#if defined(CONFIG_NFS_V4_1)
3761 .rpc_call_prepare = nfs4_delegreturn_prepare,
3762#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003763 .rpc_call_done = nfs4_delegreturn_done,
3764 .rpc_release = nfs4_delegreturn_release,
3765};
3766
Trond Myklebuste6f81072008-01-24 18:14:34 -05003767static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003768{
3769 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003770 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003771 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003772 struct rpc_message msg = {
3773 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3774 .rpc_cred = cred,
3775 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003776 struct rpc_task_setup task_setup_data = {
3777 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003778 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003779 .callback_ops = &nfs4_delegreturn_ops,
3780 .flags = RPC_TASK_ASYNC,
3781 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003782 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003783
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003784 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003785 if (data == NULL)
3786 return -ENOMEM;
3787 data->args.fhandle = &data->fh;
3788 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003789 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003790 nfs_copy_fh(&data->fh, NFS_FH(inode));
3791 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003792 data->res.fattr = &data->fattr;
3793 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003794 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003795 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003796 data->rpc_status = 0;
3797
Trond Myklebustc970aa82007-07-14 15:39:59 -04003798 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05003799 msg.rpc_argp = &data->args;
3800 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003801 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003802 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003803 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003804 if (!issync)
3805 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003806 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003807 if (status != 0)
3808 goto out;
3809 status = data->rpc_status;
3810 if (status != 0)
3811 goto out;
3812 nfs_refresh_inode(inode, &data->fattr);
3813out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003814 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003815 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816}
3817
Trond Myklebuste6f81072008-01-24 18:14:34 -05003818int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819{
3820 struct nfs_server *server = NFS_SERVER(inode);
3821 struct nfs4_exception exception = { };
3822 int err;
3823 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003824 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 switch (err) {
3826 case -NFS4ERR_STALE_STATEID:
3827 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 case 0:
3829 return 0;
3830 }
3831 err = nfs4_handle_exception(server, err, &exception);
3832 } while (exception.retry);
3833 return err;
3834}
3835
3836#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3837#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3838
3839/*
3840 * sleep, with exponential backoff, and retry the LOCK operation.
3841 */
3842static unsigned long
3843nfs4_set_lock_task_retry(unsigned long timeout)
3844{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003845 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 timeout <<= 1;
3847 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3848 return NFS4_LOCK_MAXTIMEOUT;
3849 return timeout;
3850}
3851
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3853{
3854 struct inode *inode = state->inode;
3855 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003856 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003857 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003859 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003861 struct nfs_lockt_res res = {
3862 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 };
3864 struct rpc_message msg = {
3865 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3866 .rpc_argp = &arg,
3867 .rpc_resp = &res,
3868 .rpc_cred = state->owner->so_cred,
3869 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 struct nfs4_lock_state *lsp;
3871 int status;
3872
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003873 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003874 status = nfs4_set_lock_state(state, request);
3875 if (status != 0)
3876 goto out;
3877 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003878 arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05003879 arg.lock_owner.s_dev = server->s_dev;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003880 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003881 switch (status) {
3882 case 0:
3883 request->fl_type = F_UNLCK;
3884 break;
3885 case -NFS4ERR_DENIED:
3886 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003888 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003889out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 return status;
3891}
3892
3893static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3894{
3895 struct nfs4_exception exception = { };
3896 int err;
3897
3898 do {
3899 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3900 _nfs4_proc_getlk(state, cmd, request),
3901 &exception);
3902 } while (exception.retry);
3903 return err;
3904}
3905
3906static int do_vfs_lock(struct file *file, struct file_lock *fl)
3907{
3908 int res = 0;
3909 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3910 case FL_POSIX:
3911 res = posix_lock_file_wait(file, fl);
3912 break;
3913 case FL_FLOCK:
3914 res = flock_lock_file_wait(file, fl);
3915 break;
3916 default:
3917 BUG();
3918 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 return res;
3920}
3921
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003922struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003923 struct nfs_locku_args arg;
3924 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003925 struct nfs4_lock_state *lsp;
3926 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003927 struct file_lock fl;
3928 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003929 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003930};
3931
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003932static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3933 struct nfs_open_context *ctx,
3934 struct nfs4_lock_state *lsp,
3935 struct nfs_seqid *seqid)
3936{
3937 struct nfs4_unlockdata *p;
3938 struct inode *inode = lsp->ls_state->inode;
3939
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003940 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003941 if (p == NULL)
3942 return NULL;
3943 p->arg.fh = NFS_FH(inode);
3944 p->arg.fl = &p->fl;
3945 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003946 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003947 p->arg.stateid = &lsp->ls_stateid;
3948 p->lsp = lsp;
3949 atomic_inc(&lsp->ls_count);
3950 /* Ensure we don't close file until we're done freeing locks! */
3951 p->ctx = get_nfs_open_context(ctx);
3952 memcpy(&p->fl, fl, sizeof(p->fl));
3953 p->server = NFS_SERVER(inode);
3954 return p;
3955}
3956
Trond Myklebust06f814a2006-01-03 09:55:07 +01003957static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003958{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003959 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003960 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003961 nfs4_put_lock_state(calldata->lsp);
3962 put_nfs_open_context(calldata->ctx);
3963 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003964}
3965
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003966static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003967{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003968 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003969
Trond Myklebust14516c32010-07-31 14:29:06 -04003970 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3971 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003972 switch (task->tk_status) {
3973 case 0:
3974 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003975 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003976 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003977 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003978 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003979 case -NFS4ERR_BAD_STATEID:
3980 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003981 case -NFS4ERR_STALE_STATEID:
3982 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003983 break;
3984 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003985 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebust0110ee12009-12-07 09:00:24 -05003986 nfs_restart_rpc(task,
Trond Myklebustd61e6122009-12-05 19:32:19 -05003987 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003988 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003989}
3990
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003991static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003992{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003993 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003995 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003996 return;
3997 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003998 /* Note: exit _without_ running nfs4_locku_done */
3999 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004000 return;
4001 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004002 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004003 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004004 &calldata->arg.seq_args,
4005 &calldata->res.seq_res, 1, task))
4006 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004007 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008}
4009
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004010static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004011 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004012 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004013 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004014};
4015
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004016static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4017 struct nfs_open_context *ctx,
4018 struct nfs4_lock_state *lsp,
4019 struct nfs_seqid *seqid)
4020{
4021 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004022 struct rpc_message msg = {
4023 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4024 .rpc_cred = ctx->cred,
4025 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004026 struct rpc_task_setup task_setup_data = {
4027 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004028 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004029 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004030 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004031 .flags = RPC_TASK_ASYNC,
4032 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004033
Frank Filz137d6ac2007-07-09 15:32:29 -07004034 /* Ensure this is an unlock - when canceling a lock, the
4035 * canceled lock is passed in, and it won't be an unlock.
4036 */
4037 fl->fl_type = F_UNLCK;
4038
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004039 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4040 if (data == NULL) {
4041 nfs_free_seqid(seqid);
4042 return ERR_PTR(-ENOMEM);
4043 }
4044
Trond Myklebust1174dd12010-12-21 10:52:24 -05004045 msg.rpc_argp = &data->arg;
4046 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004047 task_setup_data.callback_data = data;
4048 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004049}
4050
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4052{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004053 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004054 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004055 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004056 struct rpc_task *task;
4057 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004058 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059
Trond Myklebust9b073572006-06-29 16:38:34 -04004060 status = nfs4_set_lock_state(state, request);
4061 /* Unlock _before_ we do the RPC call */
4062 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004063 down_read(&nfsi->rwsem);
4064 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4065 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004066 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004067 }
4068 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004069 if (status != 0)
4070 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004071 /* Is this a delegated lock? */
4072 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004073 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004074 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004075 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004076 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004077 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004078 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004079 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004080 status = PTR_ERR(task);
4081 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004082 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004083 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004084 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004085out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004086 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004087 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088}
4089
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004090struct nfs4_lockdata {
4091 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004092 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004093 struct nfs4_lock_state *lsp;
4094 struct nfs_open_context *ctx;
4095 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004096 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004097 int rpc_status;
4098 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004099 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004100};
4101
4102static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004103 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4104 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004105{
4106 struct nfs4_lockdata *p;
4107 struct inode *inode = lsp->ls_state->inode;
4108 struct nfs_server *server = NFS_SERVER(inode);
4109
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004110 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004111 if (p == NULL)
4112 return NULL;
4113
4114 p->arg.fh = NFS_FH(inode);
4115 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004116 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004117 if (p->arg.open_seqid == NULL)
4118 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004119 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004120 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004121 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004122 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004123 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004124 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004125 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004126 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004127 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004128 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004129 atomic_inc(&lsp->ls_count);
4130 p->ctx = get_nfs_open_context(ctx);
4131 memcpy(&p->fl, fl, sizeof(p->fl));
4132 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004133out_free_seqid:
4134 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004135out_free:
4136 kfree(p);
4137 return NULL;
4138}
4139
4140static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4141{
4142 struct nfs4_lockdata *data = calldata;
4143 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144
Harvey Harrison3110ff82008-05-02 13:42:44 -07004145 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004146 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4147 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004148 /* Do we need to do an open_to_lock_owner? */
4149 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004150 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4151 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004152 data->arg.open_stateid = &state->stateid;
4153 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004154 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004155 } else
4156 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004157 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004158 if (nfs4_setup_sequence(data->server,
4159 &data->arg.seq_args,
Andy Adamson66179ef2009-04-01 09:22:22 -04004160 &data->res.seq_res, 1, task))
4161 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004162 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004163 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004164}
4165
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004166static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4167{
4168 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4169 nfs4_lock_prepare(task, calldata);
4170}
4171
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004172static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4173{
4174 struct nfs4_lockdata *data = calldata;
4175
Harvey Harrison3110ff82008-05-02 13:42:44 -07004176 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004177
Trond Myklebust14516c32010-07-31 14:29:06 -04004178 if (!nfs4_sequence_done(task, &data->res.seq_res))
4179 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004180
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004181 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004182 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004183 if (data->rpc_status == 0)
4184 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4185 else
4186 goto out;
4187 }
4188 if (data->rpc_status == 0) {
4189 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4190 sizeof(data->lsp->ls_stateid.data));
4191 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04004192 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004193 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004194out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004195 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004196}
4197
4198static void nfs4_lock_release(void *calldata)
4199{
4200 struct nfs4_lockdata *data = calldata;
4201
Harvey Harrison3110ff82008-05-02 13:42:44 -07004202 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004203 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004204 if (data->cancelled != 0) {
4205 struct rpc_task *task;
4206 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4207 data->arg.lock_seqid);
4208 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004209 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004210 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004211 } else
4212 nfs_free_seqid(data->arg.lock_seqid);
4213 nfs4_put_lock_state(data->lsp);
4214 put_nfs_open_context(data->ctx);
4215 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004216 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004217}
4218
4219static const struct rpc_call_ops nfs4_lock_ops = {
4220 .rpc_call_prepare = nfs4_lock_prepare,
4221 .rpc_call_done = nfs4_lock_done,
4222 .rpc_release = nfs4_lock_release,
4223};
4224
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004225static const struct rpc_call_ops nfs4_recover_lock_ops = {
4226 .rpc_call_prepare = nfs4_recover_lock_prepare,
4227 .rpc_call_done = nfs4_lock_done,
4228 .rpc_release = nfs4_lock_release,
4229};
4230
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004231static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4232{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004233 switch (error) {
4234 case -NFS4ERR_ADMIN_REVOKED:
4235 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004236 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004237 if (new_lock_owner != 0 ||
4238 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004239 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004240 break;
4241 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004242 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004243 case -NFS4ERR_EXPIRED:
4244 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004245 };
4246}
4247
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004248static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004249{
4250 struct nfs4_lockdata *data;
4251 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004252 struct rpc_message msg = {
4253 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4254 .rpc_cred = state->owner->so_cred,
4255 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004256 struct rpc_task_setup task_setup_data = {
4257 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004258 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004259 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004260 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004261 .flags = RPC_TASK_ASYNC,
4262 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004263 int ret;
4264
Harvey Harrison3110ff82008-05-02 13:42:44 -07004265 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004266 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004267 fl->fl_u.nfs4_fl.owner,
4268 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004269 if (data == NULL)
4270 return -ENOMEM;
4271 if (IS_SETLKW(cmd))
4272 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004273 if (recovery_type > NFS_LOCK_NEW) {
4274 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004275 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004276 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4277 }
Trond Myklebust1174dd12010-12-21 10:52:24 -05004278 msg.rpc_argp = &data->arg;
4279 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004280 task_setup_data.callback_data = data;
4281 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004282 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004283 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004284 ret = nfs4_wait_for_completion_rpc_task(task);
4285 if (ret == 0) {
4286 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004287 if (ret)
4288 nfs4_handle_setlk_error(data->server, data->lsp,
4289 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004290 } else
4291 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004292 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004293 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004294 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295}
4296
4297static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4298{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004299 struct nfs_server *server = NFS_SERVER(state->inode);
4300 struct nfs4_exception exception = { };
4301 int err;
4302
4303 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004304 /* Cache the lock if possible... */
4305 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4306 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004307 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004308 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004309 break;
4310 nfs4_handle_exception(server, err, &exception);
4311 } while (exception.retry);
4312 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313}
4314
4315static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4316{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004317 struct nfs_server *server = NFS_SERVER(state->inode);
4318 struct nfs4_exception exception = { };
4319 int err;
4320
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004321 err = nfs4_set_lock_state(state, request);
4322 if (err != 0)
4323 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004324 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004325 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4326 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004327 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004328 switch (err) {
4329 default:
4330 goto out;
4331 case -NFS4ERR_GRACE:
4332 case -NFS4ERR_DELAY:
4333 nfs4_handle_exception(server, err, &exception);
4334 err = 0;
4335 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004336 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004337out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004338 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339}
4340
4341static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4342{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004343 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004344 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004345 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004347 if ((fl_flags & FL_POSIX) &&
4348 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4349 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004350 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004351 status = nfs4_set_lock_state(state, request);
4352 if (status != 0)
4353 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004354 request->fl_flags |= FL_ACCESS;
4355 status = do_vfs_lock(request->fl_file, request);
4356 if (status < 0)
4357 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004358 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004359 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004360 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004361 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004362 request->fl_flags = fl_flags & ~FL_SLEEP;
4363 status = do_vfs_lock(request->fl_file, request);
4364 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004365 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004366 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004367 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004368 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004369 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004370 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004371 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004372 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004373out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004374 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004375out:
4376 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 return status;
4378}
4379
4380static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4381{
4382 struct nfs4_exception exception = { };
4383 int err;
4384
4385 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004386 err = _nfs4_proc_setlk(state, cmd, request);
4387 if (err == -NFS4ERR_DENIED)
4388 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004390 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 } while (exception.retry);
4392 return err;
4393}
4394
4395static int
4396nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4397{
4398 struct nfs_open_context *ctx;
4399 struct nfs4_state *state;
4400 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4401 int status;
4402
4403 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004404 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 state = ctx->state;
4406
4407 if (request->fl_start < 0 || request->fl_end < 0)
4408 return -EINVAL;
4409
Trond Myklebustd9531262009-07-21 19:22:38 -04004410 if (IS_GETLK(cmd)) {
4411 if (state != NULL)
4412 return nfs4_proc_getlk(state, F_GETLK, request);
4413 return 0;
4414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415
4416 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4417 return -EINVAL;
4418
Trond Myklebustd9531262009-07-21 19:22:38 -04004419 if (request->fl_type == F_UNLCK) {
4420 if (state != NULL)
4421 return nfs4_proc_unlck(state, cmd, request);
4422 return 0;
4423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424
Trond Myklebustd9531262009-07-21 19:22:38 -04004425 if (state == NULL)
4426 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 do {
4428 status = nfs4_proc_setlk(state, cmd, request);
4429 if ((status != -EAGAIN) || IS_SETLK(cmd))
4430 break;
4431 timeout = nfs4_set_lock_task_retry(timeout);
4432 status = -ERESTARTSYS;
4433 if (signalled())
4434 break;
4435 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 return status;
4437}
4438
Trond Myklebust888e6942005-11-04 15:38:11 -05004439int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4440{
4441 struct nfs_server *server = NFS_SERVER(state->inode);
4442 struct nfs4_exception exception = { };
4443 int err;
4444
4445 err = nfs4_set_lock_state(state, fl);
4446 if (err != 0)
4447 goto out;
4448 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004449 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004450 switch (err) {
4451 default:
4452 printk(KERN_ERR "%s: unhandled error %d.\n",
4453 __func__, err);
4454 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004455 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004456 goto out;
4457 case -NFS4ERR_EXPIRED:
4458 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004459 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004460 nfs4_schedule_lease_recovery(server->nfs_client);
4461 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004462 case -NFS4ERR_BADSESSION:
4463 case -NFS4ERR_BADSLOT:
4464 case -NFS4ERR_BAD_HIGH_SLOT:
4465 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4466 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004467 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004468 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004469 case -ERESTARTSYS:
4470 /*
4471 * The show must go on: exit, but mark the
4472 * stateid as needing recovery.
4473 */
4474 case -NFS4ERR_ADMIN_REVOKED:
4475 case -NFS4ERR_BAD_STATEID:
4476 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004477 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004478 err = 0;
4479 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004480 case -EKEYEXPIRED:
4481 /*
4482 * User RPCSEC_GSS context has expired.
4483 * We cannot recover this stateid now, so
4484 * skip it and allow recovery thread to
4485 * proceed.
4486 */
4487 err = 0;
4488 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004489 case -ENOMEM:
4490 case -NFS4ERR_DENIED:
4491 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4492 err = 0;
4493 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004494 case -NFS4ERR_DELAY:
4495 break;
4496 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004497 err = nfs4_handle_exception(server, err, &exception);
4498 } while (exception.retry);
4499out:
4500 return err;
4501}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004502
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004503static void nfs4_release_lockowner_release(void *calldata)
4504{
4505 kfree(calldata);
4506}
4507
4508const struct rpc_call_ops nfs4_release_lockowner_ops = {
4509 .rpc_release = nfs4_release_lockowner_release,
4510};
4511
4512void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4513{
4514 struct nfs_server *server = lsp->ls_state->owner->so_server;
4515 struct nfs_release_lockowner_args *args;
4516 struct rpc_message msg = {
4517 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4518 };
4519
4520 if (server->nfs_client->cl_mvops->minor_version != 0)
4521 return;
4522 args = kmalloc(sizeof(*args), GFP_NOFS);
4523 if (!args)
4524 return;
4525 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4526 args->lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004527 args->lock_owner.s_dev = server->s_dev;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004528 msg.rpc_argp = args;
4529 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4530}
4531
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004532#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4533
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004534static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4535 const void *buf, size_t buflen,
4536 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004537{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004538 if (strcmp(key, "") != 0)
4539 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004540
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004541 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004542}
4543
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004544static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4545 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004546{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004547 if (strcmp(key, "") != 0)
4548 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004549
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004550 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004551}
4552
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004553static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4554 size_t list_len, const char *name,
4555 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004556{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004557 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004558
J. Bruce Fields096455a2006-03-20 23:23:42 -05004559 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4560 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004561
4562 if (list && len <= list_len)
4563 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004564 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004565}
4566
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004567static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4568{
4569 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4570 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4571 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4572 return;
4573
4574 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4575 NFS_ATTR_FATTR_NLINK;
4576 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4577 fattr->nlink = 2;
4578}
4579
Trond Myklebust56659e92007-07-17 21:52:39 -04004580int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004581 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004582{
4583 struct nfs_server *server = NFS_SERVER(dir);
4584 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004585 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4586 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004587 };
4588 struct nfs4_fs_locations_arg args = {
4589 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004590 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004591 .page = page,
4592 .bitmask = bitmask,
4593 };
Benny Halevy22958462009-04-01 09:22:02 -04004594 struct nfs4_fs_locations_res res = {
4595 .fs_locations = fs_locations,
4596 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004597 struct rpc_message msg = {
4598 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4599 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004600 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004601 };
4602 int status;
4603
Harvey Harrison3110ff82008-05-02 13:42:44 -07004604 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004605 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004606 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004607 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04004608 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004609 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004610 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004611 return status;
4612}
4613
Andy Adamson557134a2009-04-01 09:21:53 -04004614#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004615/*
Andy Adamson357f54d2010-12-14 10:11:57 -05004616 * Check the exchange flags returned by the server for invalid flags, having
4617 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4618 * DS flags set.
4619 */
4620static int nfs4_check_cl_exchange_flags(u32 flags)
4621{
4622 if (flags & ~EXCHGID4_FLAG_MASK_R)
4623 goto out_inval;
4624 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4625 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4626 goto out_inval;
4627 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4628 goto out_inval;
4629 return NFS_OK;
4630out_inval:
4631 return -NFS4ERR_INVAL;
4632}
4633
4634/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04004635 * nfs4_proc_exchange_id()
4636 *
4637 * Since the clientid has expired, all compounds using sessions
4638 * associated with the stale clientid will be returning
4639 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4640 * be in some phase of session reset.
4641 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004642int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004643{
4644 nfs4_verifier verifier;
4645 struct nfs41_exchange_id_args args = {
4646 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05004647 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04004648 };
4649 struct nfs41_exchange_id_res res = {
4650 .client = clp,
4651 };
4652 int status;
4653 struct rpc_message msg = {
4654 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4655 .rpc_argp = &args,
4656 .rpc_resp = &res,
4657 .rpc_cred = cred,
4658 };
4659 __be32 *p;
4660
4661 dprintk("--> %s\n", __func__);
4662 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004663
Benny Halevy99fe60d2009-04-01 09:22:29 -04004664 p = (u32 *)verifier.data;
4665 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4666 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4667 args.verifier = &verifier;
4668
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004669 args.id_len = scnprintf(args.id, sizeof(args.id),
4670 "%s/%s.%s/%u",
4671 clp->cl_ipaddr,
4672 init_utsname()->nodename,
4673 init_utsname()->domainname,
4674 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004675
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004676 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4677 if (!status)
4678 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004679 dprintk("<-- %s status= %d\n", __func__, status);
4680 return status;
4681}
4682
Andy Adamson2050f0c2009-04-01 09:22:30 -04004683struct nfs4_get_lease_time_data {
4684 struct nfs4_get_lease_time_args *args;
4685 struct nfs4_get_lease_time_res *res;
4686 struct nfs_client *clp;
4687};
4688
4689static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4690 void *calldata)
4691{
4692 int ret;
4693 struct nfs4_get_lease_time_data *data =
4694 (struct nfs4_get_lease_time_data *)calldata;
4695
4696 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004697 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004698 /* just setup sequence, do not trigger session recovery
4699 since we're invoked within one */
4700 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004701 &data->args->la_seq_args,
4702 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004703
4704 BUG_ON(ret == -EAGAIN);
4705 rpc_call_start(task);
4706 dprintk("<-- %s\n", __func__);
4707}
4708
4709/*
4710 * Called from nfs4_state_manager thread for session setup, so don't recover
4711 * from sequence operation or clientid errors.
4712 */
4713static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4714{
4715 struct nfs4_get_lease_time_data *data =
4716 (struct nfs4_get_lease_time_data *)calldata;
4717
4718 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04004719 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4720 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04004721 switch (task->tk_status) {
4722 case -NFS4ERR_DELAY:
4723 case -NFS4ERR_GRACE:
4724 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4725 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4726 task->tk_status = 0;
Trond Myklebust0110ee12009-12-07 09:00:24 -05004727 nfs_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004728 return;
4729 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004730 dprintk("<-- %s\n", __func__);
4731}
4732
4733struct rpc_call_ops nfs4_get_lease_time_ops = {
4734 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4735 .rpc_call_done = nfs4_get_lease_time_done,
4736};
4737
4738int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4739{
4740 struct rpc_task *task;
4741 struct nfs4_get_lease_time_args args;
4742 struct nfs4_get_lease_time_res res = {
4743 .lr_fsinfo = fsinfo,
4744 };
4745 struct nfs4_get_lease_time_data data = {
4746 .args = &args,
4747 .res = &res,
4748 .clp = clp,
4749 };
4750 struct rpc_message msg = {
4751 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4752 .rpc_argp = &args,
4753 .rpc_resp = &res,
4754 };
4755 struct rpc_task_setup task_setup = {
4756 .rpc_client = clp->cl_rpcclient,
4757 .rpc_message = &msg,
4758 .callback_ops = &nfs4_get_lease_time_ops,
4759 .callback_data = &data
4760 };
4761 int status;
4762
Andy Adamson2050f0c2009-04-01 09:22:30 -04004763 dprintk("--> %s\n", __func__);
4764 task = rpc_run_task(&task_setup);
4765
4766 if (IS_ERR(task))
4767 status = PTR_ERR(task);
4768 else {
4769 status = task->tk_status;
4770 rpc_put_task(task);
4771 }
4772 dprintk("<-- %s return %d\n", __func__, status);
4773
4774 return status;
4775}
4776
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004777/*
4778 * Reset a slot table
4779 */
Andy Adamson104aeba2010-01-14 17:45:10 -05004780static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4781 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004782{
Andy Adamson104aeba2010-01-14 17:45:10 -05004783 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004784 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004785 int ret = 0;
4786
Andy Adamson104aeba2010-01-14 17:45:10 -05004787 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4788 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004789
Andy Adamson104aeba2010-01-14 17:45:10 -05004790 /* Does the newly negotiated max_reqs match the existing slot table? */
4791 if (max_reqs != tbl->max_slots) {
4792 ret = -ENOMEM;
4793 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004794 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05004795 if (!new)
4796 goto out;
4797 ret = 0;
4798 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004799 }
4800 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05004801 if (new) {
4802 tbl->slots = new;
4803 tbl->max_slots = max_reqs;
4804 }
4805 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004806 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004807 spin_unlock(&tbl->slot_tbl_lock);
4808 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4809 tbl, tbl->slots, tbl->max_slots);
4810out:
4811 dprintk("<-- %s: return %d\n", __func__, ret);
4812 return ret;
4813}
4814
Andy Adamsonfc931582009-04-01 09:22:31 -04004815/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004816 * Reset the forechannel and backchannel slot tables
4817 */
4818static int nfs4_reset_slot_tables(struct nfs4_session *session)
4819{
4820 int status;
4821
4822 status = nfs4_reset_slot_table(&session->fc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004823 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004824 if (status)
4825 return status;
4826
4827 status = nfs4_reset_slot_table(&session->bc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004828 session->bc_attrs.max_reqs, 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004829 return status;
4830}
4831
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004832/* Destroy the slot table */
4833static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4834{
4835 if (session->fc_slot_table.slots != NULL) {
4836 kfree(session->fc_slot_table.slots);
4837 session->fc_slot_table.slots = NULL;
4838 }
4839 if (session->bc_slot_table.slots != NULL) {
4840 kfree(session->bc_slot_table.slots);
4841 session->bc_slot_table.slots = NULL;
4842 }
4843 return;
4844}
4845
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004846/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004847 * Initialize slot table
4848 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004849static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4850 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04004851{
Andy Adamsonfc931582009-04-01 09:22:31 -04004852 struct nfs4_slot *slot;
4853 int ret = -ENOMEM;
4854
4855 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4856
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004857 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04004858
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004859 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04004860 if (!slot)
4861 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04004862 ret = 0;
4863
4864 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04004865 tbl->max_slots = max_slots;
4866 tbl->slots = slot;
4867 tbl->highest_used_slotid = -1; /* no slot is currently used */
4868 spin_unlock(&tbl->slot_tbl_lock);
4869 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4870 tbl, tbl->slots, tbl->max_slots);
4871out:
4872 dprintk("<-- %s: return %d\n", __func__, ret);
4873 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04004874}
4875
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004876/*
4877 * Initialize the forechannel and backchannel tables
4878 */
4879static int nfs4_init_slot_tables(struct nfs4_session *session)
4880{
Trond Myklebustf26468f2009-12-05 19:32:11 -05004881 struct nfs4_slot_table *tbl;
4882 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004883
Trond Myklebustf26468f2009-12-05 19:32:11 -05004884 tbl = &session->fc_slot_table;
4885 if (tbl->slots == NULL) {
4886 status = nfs4_init_slot_table(tbl,
4887 session->fc_attrs.max_reqs, 1);
4888 if (status)
4889 return status;
4890 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004891
Trond Myklebustf26468f2009-12-05 19:32:11 -05004892 tbl = &session->bc_slot_table;
4893 if (tbl->slots == NULL) {
4894 status = nfs4_init_slot_table(tbl,
4895 session->bc_attrs.max_reqs, 0);
4896 if (status)
4897 nfs4_destroy_slot_tables(session);
4898 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004899
4900 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04004901}
4902
4903struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4904{
4905 struct nfs4_session *session;
4906 struct nfs4_slot_table *tbl;
4907
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004908 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04004909 if (!session)
4910 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04004911
Andy Adamson557134a2009-04-01 09:21:53 -04004912 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004913 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04004914 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004915 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00004916 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004917
4918 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004919 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004920 spin_lock_init(&tbl->slot_tbl_lock);
4921 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00004922 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004923
Trond Myklebust1055d762010-06-16 09:52:27 -04004924 session->session_state = 1<<NFS4_SESSION_INITING;
4925
Andy Adamson557134a2009-04-01 09:21:53 -04004926 session->clp = clp;
4927 return session;
4928}
4929
4930void nfs4_destroy_session(struct nfs4_session *session)
4931{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04004932 nfs4_proc_destroy_session(session);
4933 dprintk("%s Destroy backchannel for xprt %p\n",
4934 __func__, session->clp->cl_rpcclient->cl_xprt);
4935 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4936 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004937 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04004938 kfree(session);
4939}
4940
Andy Adamsonfc931582009-04-01 09:22:31 -04004941/*
4942 * Initialize the values to be used by the client in CREATE_SESSION
4943 * If nfs4_init_session set the fore channel request and response sizes,
4944 * use them.
4945 *
4946 * Set the back channel max_resp_sz_cached to zero to force the client to
4947 * always set csa_cachethis to FALSE because the current implementation
4948 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4949 */
4950static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4951{
4952 struct nfs4_session *session = args->client->cl_session;
4953 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4954 mxresp_sz = session->fc_attrs.max_resp_sz;
4955
4956 if (mxrqst_sz == 0)
4957 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4958 if (mxresp_sz == 0)
4959 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4960 /* Fore channel attributes */
4961 args->fc_attrs.headerpadsz = 0;
4962 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4963 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04004964 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4965 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4966
4967 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05004968 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04004969 __func__,
4970 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05004971 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04004972
4973 /* Back channel attributes */
4974 args->bc_attrs.headerpadsz = 0;
4975 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4976 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4977 args->bc_attrs.max_resp_sz_cached = 0;
4978 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4979 args->bc_attrs.max_reqs = 1;
4980
4981 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4982 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4983 __func__,
4984 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4985 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4986 args->bc_attrs.max_reqs);
4987}
4988
J. Bruce Fields43c2e882010-10-02 15:19:01 -04004989static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04004990{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04004991 struct nfs4_channel_attrs *sent = &args->fc_attrs;
4992 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
4993
4994 if (rcvd->headerpadsz > sent->headerpadsz)
4995 return -EINVAL;
4996 if (rcvd->max_resp_sz > sent->max_resp_sz)
4997 return -EINVAL;
4998 /*
4999 * Our requested max_ops is the minimum we need; we're not
5000 * prepared to break up compounds into smaller pieces than that.
5001 * So, no point even trying to continue if the server won't
5002 * cooperate:
5003 */
5004 if (rcvd->max_ops < sent->max_ops)
5005 return -EINVAL;
5006 if (rcvd->max_reqs == 0)
5007 return -EINVAL;
5008 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005009}
5010
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005011static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5012{
5013 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5014 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005015
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005016 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5017 return -EINVAL;
5018 if (rcvd->max_resp_sz < sent->max_resp_sz)
5019 return -EINVAL;
5020 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5021 return -EINVAL;
5022 /* These would render the backchannel useless: */
5023 if (rcvd->max_ops == 0)
5024 return -EINVAL;
5025 if (rcvd->max_reqs == 0)
5026 return -EINVAL;
5027 return 0;
5028}
Andy Adamson8d353012009-04-01 09:22:32 -04005029
Andy Adamson8d353012009-04-01 09:22:32 -04005030static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5031 struct nfs4_session *session)
5032{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005033 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005034
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005035 ret = nfs4_verify_fore_channel_attrs(args, session);
5036 if (ret)
5037 return ret;
5038 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005039}
5040
Andy Adamsonfc931582009-04-01 09:22:31 -04005041static int _nfs4_proc_create_session(struct nfs_client *clp)
5042{
5043 struct nfs4_session *session = clp->cl_session;
5044 struct nfs41_create_session_args args = {
5045 .client = clp,
5046 .cb_program = NFS4_CALLBACK,
5047 };
5048 struct nfs41_create_session_res res = {
5049 .client = clp,
5050 };
5051 struct rpc_message msg = {
5052 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5053 .rpc_argp = &args,
5054 .rpc_resp = &res,
5055 };
5056 int status;
5057
5058 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005059 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005060
5061 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5062
Andy Adamson8d353012009-04-01 09:22:32 -04005063 if (!status)
5064 /* Verify the session's negotiated channel_attrs values */
5065 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005066 if (!status) {
5067 /* Increment the clientid slot sequence id */
5068 clp->cl_seqid++;
5069 }
5070
5071 return status;
5072}
5073
5074/*
5075 * Issues a CREATE_SESSION operation to the server.
5076 * It is the responsibility of the caller to verify the session is
5077 * expired before calling this routine.
5078 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005079int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005080{
5081 int status;
5082 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005083 struct nfs4_session *session = clp->cl_session;
Ricardo Labiaga7d6d63d2011-03-09 13:13:44 -05005084 long timeout = 0;
5085 int err;
Andy Adamsonfc931582009-04-01 09:22:31 -04005086
5087 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5088
Ricardo Labiaga7d6d63d2011-03-09 13:13:44 -05005089 do {
5090 status = _nfs4_proc_create_session(clp);
5091 if (status == -NFS4ERR_DELAY) {
5092 err = nfs4_delay(clp->cl_rpcclient, &timeout);
5093 if (err)
5094 status = err;
5095 }
5096 } while (status == -NFS4ERR_DELAY);
5097
Andy Adamsonfc931582009-04-01 09:22:31 -04005098 if (status)
5099 goto out;
5100
Trond Myklebustf26468f2009-12-05 19:32:11 -05005101 /* Init and reset the fore channel */
5102 status = nfs4_init_slot_tables(session);
5103 dprintk("slot table initialization returned %d\n", status);
5104 if (status)
5105 goto out;
5106 status = nfs4_reset_slot_tables(session);
5107 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005108 if (status)
5109 goto out;
5110
5111 ptr = (unsigned *)&session->sess_id.data[0];
5112 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5113 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005114out:
5115 dprintk("<-- %s\n", __func__);
5116 return status;
5117}
5118
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005119/*
5120 * Issue the over-the-wire RPC DESTROY_SESSION.
5121 * The caller must serialize access to this routine.
5122 */
5123int nfs4_proc_destroy_session(struct nfs4_session *session)
5124{
5125 int status = 0;
5126 struct rpc_message msg;
5127
5128 dprintk("--> nfs4_proc_destroy_session\n");
5129
5130 /* session is still being setup */
5131 if (session->clp->cl_cons_state != NFS_CS_READY)
5132 return status;
5133
5134 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5135 msg.rpc_argp = session;
5136 msg.rpc_resp = NULL;
5137 msg.rpc_cred = NULL;
5138 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5139
5140 if (status)
5141 printk(KERN_WARNING
5142 "Got error %d from the server on DESTROY_SESSION. "
5143 "Session has been destroyed regardless...\n", status);
5144
5145 dprintk("<-- nfs4_proc_destroy_session\n");
5146 return status;
5147}
5148
Trond Myklebustfccba802009-07-21 16:48:07 -04005149int nfs4_init_session(struct nfs_server *server)
5150{
5151 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005152 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005153 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005154 int ret;
5155
5156 if (!nfs4_has_session(clp))
5157 return 0;
5158
Trond Myklebust1055d762010-06-16 09:52:27 -04005159 session = clp->cl_session;
5160 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5161 return 0;
5162
Andy Adamson68bf05e2009-12-15 12:55:02 -05005163 rsize = server->rsize;
5164 if (rsize == 0)
5165 rsize = NFS_MAX_FILE_IO_SIZE;
5166 wsize = server->wsize;
5167 if (wsize == 0)
5168 wsize = NFS_MAX_FILE_IO_SIZE;
5169
Andy Adamson68bf05e2009-12-15 12:55:02 -05005170 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5171 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005172
Trond Myklebustfccba802009-07-21 16:48:07 -04005173 ret = nfs4_recover_expired_lease(server);
5174 if (!ret)
5175 ret = nfs4_check_client_ready(clp);
5176 return ret;
5177}
5178
Andy Adamsond83217c2011-03-01 01:34:17 +00005179int nfs4_init_ds_session(struct nfs_client *clp)
5180{
5181 struct nfs4_session *session = clp->cl_session;
5182 int ret;
5183
5184 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5185 return 0;
5186
5187 ret = nfs4_client_recover_expired_lease(clp);
5188 if (!ret)
5189 /* Test for the DS role */
5190 if (!is_ds_client(clp))
5191 ret = -ENODEV;
5192 if (!ret)
5193 ret = nfs4_check_client_ready(clp);
5194 return ret;
5195
5196}
5197EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5198
5199
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005200/*
5201 * Renew the cl_session lease.
5202 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005203struct nfs4_sequence_data {
5204 struct nfs_client *clp;
5205 struct nfs4_sequence_args args;
5206 struct nfs4_sequence_res res;
5207};
5208
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005209static void nfs41_sequence_release(void *data)
5210{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005211 struct nfs4_sequence_data *calldata = data;
5212 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005213
Alexandros Batsakis71358402010-02-05 03:45:05 -08005214 if (atomic_read(&clp->cl_count) > 1)
5215 nfs4_schedule_state_renewal(clp);
5216 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005217 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005218}
5219
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005220static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5221{
5222 switch(task->tk_status) {
5223 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005224 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5225 return -EAGAIN;
5226 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005227 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005228 }
5229 return 0;
5230}
5231
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005232static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005233{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005234 struct nfs4_sequence_data *calldata = data;
5235 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005236
Trond Myklebust14516c32010-07-31 14:29:06 -04005237 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5238 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005239
5240 if (task->tk_status < 0) {
5241 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005242 if (atomic_read(&clp->cl_count) == 1)
5243 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005244
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005245 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5246 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005247 return;
5248 }
5249 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005250 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005251out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005252 dprintk("<-- %s\n", __func__);
5253}
5254
5255static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5256{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005257 struct nfs4_sequence_data *calldata = data;
5258 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005259 struct nfs4_sequence_args *args;
5260 struct nfs4_sequence_res *res;
5261
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005262 args = task->tk_msg.rpc_argp;
5263 res = task->tk_msg.rpc_resp;
5264
Trond Myklebust035168a2010-06-16 09:52:26 -04005265 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005266 return;
5267 rpc_call_start(task);
5268}
5269
5270static const struct rpc_call_ops nfs41_sequence_ops = {
5271 .rpc_call_done = nfs41_sequence_call_done,
5272 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005273 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005274};
5275
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005276static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005277{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005278 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005279 struct rpc_message msg = {
5280 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5281 .rpc_cred = cred,
5282 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005283 struct rpc_task_setup task_setup_data = {
5284 .rpc_client = clp->cl_rpcclient,
5285 .rpc_message = &msg,
5286 .callback_ops = &nfs41_sequence_ops,
5287 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5288 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005289
Alexandros Batsakis71358402010-02-05 03:45:05 -08005290 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005291 return ERR_PTR(-EIO);
Benny Halevydfb4f302010-09-24 09:17:01 -04005292 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005293 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005294 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005295 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005296 }
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005297 msg.rpc_argp = &calldata->args;
5298 msg.rpc_resp = &calldata->res;
5299 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005300 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005301
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005302 return rpc_run_task(&task_setup_data);
5303}
5304
5305static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5306{
5307 struct rpc_task *task;
5308 int ret = 0;
5309
5310 task = _nfs41_proc_sequence(clp, cred);
5311 if (IS_ERR(task))
5312 ret = PTR_ERR(task);
5313 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005314 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005315 dprintk("<-- %s status=%d\n", __func__, ret);
5316 return ret;
5317}
5318
5319static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5320{
5321 struct rpc_task *task;
5322 int ret;
5323
5324 task = _nfs41_proc_sequence(clp, cred);
5325 if (IS_ERR(task)) {
5326 ret = PTR_ERR(task);
5327 goto out;
5328 }
5329 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005330 if (!ret) {
5331 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5332
5333 if (task->tk_status == 0)
5334 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005335 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005336 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005337 rpc_put_task(task);
5338out:
5339 dprintk("<-- %s status=%d\n", __func__, ret);
5340 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005341}
5342
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005343struct nfs4_reclaim_complete_data {
5344 struct nfs_client *clp;
5345 struct nfs41_reclaim_complete_args arg;
5346 struct nfs41_reclaim_complete_res res;
5347};
5348
5349static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5350{
5351 struct nfs4_reclaim_complete_data *calldata = data;
5352
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005353 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005354 if (nfs41_setup_sequence(calldata->clp->cl_session,
5355 &calldata->arg.seq_args,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005356 &calldata->res.seq_res, 0, task))
5357 return;
5358
5359 rpc_call_start(task);
5360}
5361
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005362static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5363{
5364 switch(task->tk_status) {
5365 case 0:
5366 case -NFS4ERR_COMPLETE_ALREADY:
5367 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5368 break;
5369 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005370 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5371 return -EAGAIN;
5372 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005373 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005374 }
5375 return 0;
5376}
5377
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005378static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5379{
5380 struct nfs4_reclaim_complete_data *calldata = data;
5381 struct nfs_client *clp = calldata->clp;
5382 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5383
5384 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005385 if (!nfs41_sequence_done(task, res))
5386 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005387
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005388 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5389 rpc_restart_call_prepare(task);
5390 return;
5391 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005392 dprintk("<-- %s\n", __func__);
5393}
5394
5395static void nfs4_free_reclaim_complete_data(void *data)
5396{
5397 struct nfs4_reclaim_complete_data *calldata = data;
5398
5399 kfree(calldata);
5400}
5401
5402static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5403 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5404 .rpc_call_done = nfs4_reclaim_complete_done,
5405 .rpc_release = nfs4_free_reclaim_complete_data,
5406};
5407
5408/*
5409 * Issue a global reclaim complete.
5410 */
5411static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5412{
5413 struct nfs4_reclaim_complete_data *calldata;
5414 struct rpc_task *task;
5415 struct rpc_message msg = {
5416 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5417 };
5418 struct rpc_task_setup task_setup_data = {
5419 .rpc_client = clp->cl_rpcclient,
5420 .rpc_message = &msg,
5421 .callback_ops = &nfs4_reclaim_complete_call_ops,
5422 .flags = RPC_TASK_ASYNC,
5423 };
5424 int status = -ENOMEM;
5425
5426 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005427 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005428 if (calldata == NULL)
5429 goto out;
5430 calldata->clp = clp;
5431 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005432
5433 msg.rpc_argp = &calldata->arg;
5434 msg.rpc_resp = &calldata->res;
5435 task_setup_data.callback_data = calldata;
5436 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005437 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005438 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005439 goto out;
5440 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005441 status = nfs4_wait_for_completion_rpc_task(task);
5442 if (status == 0)
5443 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005444 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005445 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005446out:
5447 dprintk("<-- %s status=%d\n", __func__, status);
5448 return status;
5449}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005450
5451static void
5452nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5453{
5454 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005455 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005456
5457 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005458 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5459 * right now covering the LAYOUTGET we are about to send.
5460 * However, that is not so catastrophic, and there seems
5461 * to be no way to prevent it completely.
5462 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005463 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5464 &lgp->res.seq_res, 0, task))
5465 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005466 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5467 NFS_I(lgp->args.inode)->layout,
5468 lgp->args.ctx->state)) {
5469 rpc_exit(task, NFS4_OK);
5470 return;
5471 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005472 rpc_call_start(task);
5473}
5474
5475static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5476{
5477 struct nfs4_layoutget *lgp = calldata;
5478 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5479
5480 dprintk("--> %s\n", __func__);
5481
5482 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5483 return;
5484
5485 switch (task->tk_status) {
5486 case 0:
5487 break;
5488 case -NFS4ERR_LAYOUTTRYLATER:
5489 case -NFS4ERR_RECALLCONFLICT:
5490 task->tk_status = -NFS4ERR_DELAY;
5491 /* Fall through */
5492 default:
5493 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5494 rpc_restart_call_prepare(task);
5495 return;
5496 }
5497 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005498 dprintk("<-- %s\n", __func__);
5499}
5500
5501static void nfs4_layoutget_release(void *calldata)
5502{
5503 struct nfs4_layoutget *lgp = calldata;
5504
5505 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005506 if (lgp->res.layout.buf != NULL)
5507 free_page((unsigned long) lgp->res.layout.buf);
5508 put_nfs_open_context(lgp->args.ctx);
5509 kfree(calldata);
5510 dprintk("<-- %s\n", __func__);
5511}
5512
5513static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5514 .rpc_call_prepare = nfs4_layoutget_prepare,
5515 .rpc_call_done = nfs4_layoutget_done,
5516 .rpc_release = nfs4_layoutget_release,
5517};
5518
5519int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5520{
5521 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5522 struct rpc_task *task;
5523 struct rpc_message msg = {
5524 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5525 .rpc_argp = &lgp->args,
5526 .rpc_resp = &lgp->res,
5527 };
5528 struct rpc_task_setup task_setup_data = {
5529 .rpc_client = server->client,
5530 .rpc_message = &msg,
5531 .callback_ops = &nfs4_layoutget_call_ops,
5532 .callback_data = lgp,
5533 .flags = RPC_TASK_ASYNC,
5534 };
5535 int status = 0;
5536
5537 dprintk("--> %s\n", __func__);
5538
5539 lgp->res.layout.buf = (void *)__get_free_page(GFP_NOFS);
5540 if (lgp->res.layout.buf == NULL) {
5541 nfs4_layoutget_release(lgp);
5542 return -ENOMEM;
5543 }
5544
5545 lgp->res.seq_res.sr_slot = NULL;
5546 task = rpc_run_task(&task_setup_data);
5547 if (IS_ERR(task))
5548 return PTR_ERR(task);
5549 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00005550 if (status == 0)
5551 status = task->tk_status;
5552 if (status == 0)
5553 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005554 rpc_put_task(task);
5555 dprintk("<-- %s status=%d\n", __func__, status);
5556 return status;
5557}
5558
5559static int
5560_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5561{
5562 struct nfs4_getdeviceinfo_args args = {
5563 .pdev = pdev,
5564 };
5565 struct nfs4_getdeviceinfo_res res = {
5566 .pdev = pdev,
5567 };
5568 struct rpc_message msg = {
5569 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5570 .rpc_argp = &args,
5571 .rpc_resp = &res,
5572 };
5573 int status;
5574
5575 dprintk("--> %s\n", __func__);
5576 status = nfs4_call_sync(server, &msg, &args, &res, 0);
5577 dprintk("<-- %s status=%d\n", __func__, status);
5578
5579 return status;
5580}
5581
5582int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5583{
5584 struct nfs4_exception exception = { };
5585 int err;
5586
5587 do {
5588 err = nfs4_handle_exception(server,
5589 _nfs4_proc_getdeviceinfo(server, pdev),
5590 &exception);
5591 } while (exception.retry);
5592 return err;
5593}
5594EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5595
Andy Adamson557134a2009-04-01 09:21:53 -04005596#endif /* CONFIG_NFS_V4_1 */
5597
Andy Adamson591d71c2009-04-01 09:22:47 -04005598struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005599 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005600 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601 .recover_open = nfs4_open_reclaim,
5602 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04005603 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005604 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605};
5606
Andy Adamson591d71c2009-04-01 09:22:47 -04005607#if defined(CONFIG_NFS_V4_1)
5608struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5609 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5610 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5611 .recover_open = nfs4_open_reclaim,
5612 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05005613 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005614 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005615 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04005616};
5617#endif /* CONFIG_NFS_V4_1 */
5618
5619struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005620 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005621 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622 .recover_open = nfs4_open_expired,
5623 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04005624 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005625 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626};
5627
Andy Adamson591d71c2009-04-01 09:22:47 -04005628#if defined(CONFIG_NFS_V4_1)
5629struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5630 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5631 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5632 .recover_open = nfs4_open_expired,
5633 .recover_lock = nfs4_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05005634 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005635 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04005636};
5637#endif /* CONFIG_NFS_V4_1 */
5638
Benny Halevy29fba382009-04-01 09:22:44 -04005639struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5640 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04005641 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005642 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04005643};
5644
5645#if defined(CONFIG_NFS_V4_1)
5646struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5647 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04005648 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005649 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04005650};
5651#endif
5652
Trond Myklebust97dc1352010-06-16 09:52:26 -04005653static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
5654 .minor_version = 0,
5655 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04005656 .validate_stateid = nfs4_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005657 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
5658 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
5659 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005660};
5661
5662#if defined(CONFIG_NFS_V4_1)
5663static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
5664 .minor_version = 1,
5665 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04005666 .validate_stateid = nfs41_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005667 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
5668 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
5669 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005670};
5671#endif
5672
Trond Myklebust97dc1352010-06-16 09:52:26 -04005673const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
5674 [0] = &nfs_v4_0_minor_ops,
5675#if defined(CONFIG_NFS_V4_1)
5676 [1] = &nfs_v4_1_minor_ops,
5677#endif
5678};
5679
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005680static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005681 .permission = nfs_permission,
5682 .getattr = nfs_getattr,
5683 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005684 .getxattr = generic_getxattr,
5685 .setxattr = generic_setxattr,
5686 .listxattr = generic_listxattr,
5687 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005688};
5689
David Howells509de812006-08-22 20:06:11 -04005690const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691 .version = 4, /* protocol version */
5692 .dentry_ops = &nfs4_dentry_operations,
5693 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005694 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695 .getroot = nfs4_proc_get_root,
5696 .getattr = nfs4_proc_getattr,
5697 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04005698 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699 .lookup = nfs4_proc_lookup,
5700 .access = nfs4_proc_access,
5701 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702 .create = nfs4_proc_create,
5703 .remove = nfs4_proc_remove,
5704 .unlink_setup = nfs4_proc_unlink_setup,
5705 .unlink_done = nfs4_proc_unlink_done,
5706 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04005707 .rename_setup = nfs4_proc_rename_setup,
5708 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709 .link = nfs4_proc_link,
5710 .symlink = nfs4_proc_symlink,
5711 .mkdir = nfs4_proc_mkdir,
5712 .rmdir = nfs4_proc_remove,
5713 .readdir = nfs4_proc_readdir,
5714 .mknod = nfs4_proc_mknod,
5715 .statfs = nfs4_proc_statfs,
5716 .fsinfo = nfs4_proc_fsinfo,
5717 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04005718 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719 .decode_dirent = nfs4_decode_dirent,
5720 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05005721 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005723 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005725 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005727 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04005728 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04005729 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00005730 .init_client = nfs4_init_client,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731};
5732
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005733static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
5734 .prefix = XATTR_NAME_NFSV4_ACL,
5735 .list = nfs4_xattr_list_nfs4_acl,
5736 .get = nfs4_xattr_get_nfs4_acl,
5737 .set = nfs4_xattr_set_nfs4_acl,
5738};
5739
5740const struct xattr_handler *nfs4_xattr_handlers[] = {
5741 &nfs4_xattr_nfs4_acl_handler,
5742 NULL
5743};
5744
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745/*
5746 * Local variables:
5747 * c-basic-offset: 8
5748 * End:
5749 */