blob: 07d1a43f40f59383ce8602bfaba8c112221f09c9 [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;
88 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -070090 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -040091 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 }
Trond Myklebust52567b02009-10-23 14:46:42 -040093 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
96/*
97 * This is our standard bitmap for GETATTR requests.
98 */
99const u32 nfs4_fattr_bitmap[2] = {
100 FATTR4_WORD0_TYPE
101 | FATTR4_WORD0_CHANGE
102 | FATTR4_WORD0_SIZE
103 | FATTR4_WORD0_FSID
104 | FATTR4_WORD0_FILEID,
105 FATTR4_WORD1_MODE
106 | FATTR4_WORD1_NUMLINKS
107 | FATTR4_WORD1_OWNER
108 | FATTR4_WORD1_OWNER_GROUP
109 | FATTR4_WORD1_RAWDEV
110 | FATTR4_WORD1_SPACE_USED
111 | FATTR4_WORD1_TIME_ACCESS
112 | FATTR4_WORD1_TIME_METADATA
113 | FATTR4_WORD1_TIME_MODIFY
114};
115
116const u32 nfs4_statfs_bitmap[2] = {
117 FATTR4_WORD0_FILES_AVAIL
118 | FATTR4_WORD0_FILES_FREE
119 | FATTR4_WORD0_FILES_TOTAL,
120 FATTR4_WORD1_SPACE_AVAIL
121 | FATTR4_WORD1_SPACE_FREE
122 | FATTR4_WORD1_SPACE_TOTAL
123};
124
Trond Myklebust4ce79712005-06-22 17:16:21 +0000125const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 FATTR4_WORD0_MAXLINK
127 | FATTR4_WORD0_MAXNAME,
128 0
129};
130
131const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
132 | FATTR4_WORD0_MAXREAD
133 | FATTR4_WORD0_MAXWRITE
134 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700135 FATTR4_WORD1_TIME_DELTA
Andy Adamson504913f2010-10-20 00:17:57 -0400136 | FATTR4_WORD1_FS_LAYOUT_TYPES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137};
138
Manoj Naik830b8e32006-06-09 09:34:25 -0400139const u32 nfs4_fs_locations_bitmap[2] = {
140 FATTR4_WORD0_TYPE
141 | FATTR4_WORD0_CHANGE
142 | FATTR4_WORD0_SIZE
143 | FATTR4_WORD0_FSID
144 | FATTR4_WORD0_FILEID
145 | FATTR4_WORD0_FS_LOCATIONS,
146 FATTR4_WORD1_MODE
147 | FATTR4_WORD1_NUMLINKS
148 | FATTR4_WORD1_OWNER
149 | FATTR4_WORD1_OWNER_GROUP
150 | FATTR4_WORD1_RAWDEV
151 | FATTR4_WORD1_SPACE_USED
152 | FATTR4_WORD1_TIME_ACCESS
153 | FATTR4_WORD1_TIME_METADATA
154 | FATTR4_WORD1_TIME_MODIFY
155 | FATTR4_WORD1_MOUNTED_ON_FILEID
156};
157
Al Virobc4785c2006-10-19 23:28:51 -0700158static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 struct nfs4_readdir_arg *readdir)
160{
Al Viro0dbb4c62006-10-19 23:28:49 -0700161 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 BUG_ON(readdir->count < 80);
164 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000165 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
167 return;
168 }
169
170 readdir->cookie = 0;
171 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
172 if (cookie == 2)
173 return;
174
175 /*
176 * NFSv4 servers do not return entries for '.' and '..'
177 * Therefore, we fake these entries here. We let '.'
178 * have cookie 0 and '..' have cookie 1. Note that
179 * when talking to the server, we always send cookie 0
180 * instead of 1 or 2.
181 */
Al Viro0dbb4c62006-10-19 23:28:49 -0700182 start = p = kmap_atomic(*readdir->pages, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 if (cookie == 0) {
185 *p++ = xdr_one; /* next */
186 *p++ = xdr_zero; /* cookie, first word */
187 *p++ = xdr_one; /* cookie, second word */
188 *p++ = xdr_one; /* entry len */
189 memcpy(p, ".\0\0\0", 4); /* entry */
190 p++;
191 *p++ = xdr_one; /* bitmap length */
192 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
193 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400194 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 }
196
197 *p++ = xdr_one; /* next */
198 *p++ = xdr_zero; /* cookie, first word */
199 *p++ = xdr_two; /* cookie, second word */
200 *p++ = xdr_two; /* entry len */
201 memcpy(p, "..\0\0", 4); /* entry */
202 p++;
203 *p++ = xdr_one; /* bitmap length */
204 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
205 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400206 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 readdir->pgbase = (char *)p - (char *)start;
209 readdir->count -= readdir->pgbase;
210 kunmap_atomic(start, KM_USER0);
211}
212
Trond Myklebust65de8722008-12-23 15:21:44 -0500213static int nfs4_wait_clnt_recover(struct nfs_client *clp)
214{
215 int res;
216
217 might_sleep();
218
Trond Myklebuste005e802008-12-23 15:21:48 -0500219 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400220 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500221 return res;
222}
223
224static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
225{
226 int res = 0;
227
228 might_sleep();
229
230 if (*timeout <= 0)
231 *timeout = NFS4_POLL_RETRY_MIN;
232 if (*timeout > NFS4_POLL_RETRY_MAX)
233 *timeout = NFS4_POLL_RETRY_MAX;
234 schedule_timeout_killable(*timeout);
235 if (fatal_signal_pending(current))
236 res = -ERESTARTSYS;
237 *timeout <<= 1;
238 return res;
239}
240
241/* This is the error handling routine for processes that are allowed
242 * to sleep.
243 */
244static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
245{
246 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500247 struct nfs4_state *state = exception->state;
Trond Myklebust65de8722008-12-23 15:21:44 -0500248 int ret = errorcode;
249
250 exception->retry = 0;
251 switch(errorcode) {
252 case 0:
253 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500254 case -NFS4ERR_ADMIN_REVOKED:
255 case -NFS4ERR_BAD_STATEID:
256 case -NFS4ERR_OPENMODE:
257 if (state == NULL)
258 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500259 nfs4_schedule_stateid_recovery(server, state);
260 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500261 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500262 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust65de8722008-12-23 15:21:44 -0500263 case -NFS4ERR_EXPIRED:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500264 nfs4_schedule_lease_recovery(clp);
265 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500266#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400267 case -NFS4ERR_BADSESSION:
268 case -NFS4ERR_BADSLOT:
269 case -NFS4ERR_BAD_HIGH_SLOT:
270 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
271 case -NFS4ERR_DEADSESSION:
272 case -NFS4ERR_SEQ_FALSE_RETRY:
273 case -NFS4ERR_SEQ_MISORDERED:
274 dprintk("%s ERROR: %d Reset session\n", __func__,
275 errorcode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500276 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -0400277 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500278 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500279#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500280 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500281 if (exception->timeout > HZ) {
282 /* We have retried a decent amount, time to
283 * fail
284 */
285 ret = -EBUSY;
286 break;
287 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500288 case -NFS4ERR_GRACE:
289 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500290 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500291 ret = nfs4_delay(server->client, &exception->timeout);
292 if (ret != 0)
293 break;
294 case -NFS4ERR_OLD_STATEID:
295 exception->retry = 1;
296 }
297 /* We failed to handle the error */
298 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500299wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500300 ret = nfs4_wait_clnt_recover(clp);
301 if (ret == 0)
302 exception->retry = 1;
303 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500304}
305
306
Trond Myklebust452e9352010-07-31 14:29:06 -0400307static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 spin_lock(&clp->cl_lock);
310 if (time_before(clp->cl_last_renewal,timestamp))
311 clp->cl_last_renewal = timestamp;
312 spin_unlock(&clp->cl_lock);
313}
314
Trond Myklebust452e9352010-07-31 14:29:06 -0400315static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
316{
317 do_renew_lease(server->nfs_client, timestamp);
318}
319
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400320#if defined(CONFIG_NFS_V4_1)
321
Benny Halevy510b8172009-04-01 09:22:14 -0400322/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400323 * nfs4_free_slot - free a slot and efficiently update slot table.
324 *
325 * freeing a slot is trivially done by clearing its respective bit
326 * in the bitmap.
327 * If the freed slotid equals highest_used_slotid we want to update it
328 * so that the server would be able to size down the slot table if needed,
329 * otherwise we know that the highest_used_slotid is still in use.
330 * When updating highest_used_slotid there may be "holes" in the bitmap
331 * so we need to scan down from highest_used_slotid to 0 looking for the now
332 * highest slotid in use.
333 * If none found, highest_used_slotid is set to -1.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500334 *
335 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400336 */
337static void
Benny Halevydfb4f3092010-09-24 09:17:01 -0400338nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *free_slot)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400339{
Benny Halevydfb4f3092010-09-24 09:17:01 -0400340 int free_slotid = free_slot - tbl->slots;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400341 int slotid = free_slotid;
342
Benny Halevydfb4f3092010-09-24 09:17:01 -0400343 BUG_ON(slotid < 0 || slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400344 /* clear used bit in bitmap */
345 __clear_bit(slotid, tbl->used_slots);
346
347 /* update highest_used_slotid when it is freed */
348 if (slotid == tbl->highest_used_slotid) {
349 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500350 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400351 tbl->highest_used_slotid = slotid;
352 else
353 tbl->highest_used_slotid = -1;
354 }
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400355 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
356 free_slotid, tbl->highest_used_slotid);
357}
358
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800359/*
Andy Adamson42acd022011-01-06 02:04:34 +0000360 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800361 */
Andy Adamson42acd022011-01-06 02:04:34 +0000362static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800363{
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800364 struct rpc_task *task;
365
Trond Myklebusta2118c32010-06-16 09:52:26 -0400366 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800367 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
368 if (task)
369 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800370 return;
371 }
372
373 if (ses->fc_slot_table.highest_used_slotid != -1)
374 return;
375
Andy Adamson42acd022011-01-06 02:04:34 +0000376 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
377 complete(&ses->fc_slot_table.complete);
378}
379
380/*
381 * Signal state manager thread if session back channel is drained
382 */
383void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
384{
385 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
386 ses->bc_slot_table.highest_used_slotid != -1)
387 return;
388 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
389 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800390}
391
Trond Myklebustd185a332010-06-16 09:52:25 -0400392static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400393{
394 struct nfs4_slot_table *tbl;
395
Trond Myklebustd185a332010-06-16 09:52:25 -0400396 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400397 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400398 /* just wake up the next guy waiting since
399 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500400 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500401 return;
Andy Adamson13615872009-04-01 09:22:17 -0400402 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500403
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500404 spin_lock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400405 nfs4_free_slot(tbl, res->sr_slot);
Andy Adamson42acd022011-01-06 02:04:34 +0000406 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500407 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400408 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400409}
410
Trond Myklebust14516c32010-07-31 14:29:06 -0400411static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400412{
413 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400414 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400415
416 /*
417 * sr_status remains 1 if an RPC level error occurred. The server
418 * may or may not have processed the sequence operation..
419 * Proceed as if the server received and processed the sequence
420 * operation.
421 */
422 if (res->sr_status == 1)
423 res->sr_status = NFS_OK;
424
425 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400426 if (!res->sr_slot)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400427 goto out;
428
Andy Adamson691daf32009-12-04 15:55:39 -0500429 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400430 switch (res->sr_status) {
431 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400432 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400433 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400434 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400435 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400436 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500437 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500438 if (res->sr_status_flags != 0)
439 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400440 break;
441 case -NFS4ERR_DELAY:
442 /* The server detected a resend of the RPC call and
443 * returned NFS4ERR_DELAY as per Section 2.10.6.2
444 * of RFC5661.
445 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200446 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400447 __func__,
448 res->sr_slot - res->sr_session->fc_slot_table.slots,
449 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400450 goto out_retry;
451 default:
452 /* Just update the slot sequence no. */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400453 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400454 }
455out:
456 /* The session may be reset by one of the error handlers. */
457 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400458 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400459 return 1;
460out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400461 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400462 goto out;
463 rpc_delay(task, NFS4_POLL_RETRY_MAX);
464 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400465}
466
Trond Myklebust14516c32010-07-31 14:29:06 -0400467static int nfs4_sequence_done(struct rpc_task *task,
468 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400469{
Trond Myklebust14516c32010-07-31 14:29:06 -0400470 if (res->sr_session == NULL)
471 return 1;
472 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400473}
474
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400475/*
Benny Halevy510b8172009-04-01 09:22:14 -0400476 * nfs4_find_slot - efficiently look for a free slot
477 *
478 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
479 * If found, we mark the slot as used, update the highest_used_slotid,
480 * and respectively set up the sequence operation args.
481 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400482 *
483 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400484 */
485static u8
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800486nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400487{
488 int slotid;
489 u8 ret_id = NFS4_MAX_SLOT_TABLE;
490 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
491
Benny Halevy510b8172009-04-01 09:22:14 -0400492 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
493 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
494 tbl->max_slots);
495 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
496 if (slotid >= tbl->max_slots)
497 goto out;
498 __set_bit(slotid, tbl->used_slots);
499 if (slotid > tbl->highest_used_slotid)
500 tbl->highest_used_slotid = slotid;
501 ret_id = slotid;
502out:
503 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
504 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400505 return ret_id;
506}
507
Andy Adamsonce5039c2009-04-01 09:22:13 -0400508static int nfs41_setup_sequence(struct nfs4_session *session,
509 struct nfs4_sequence_args *args,
510 struct nfs4_sequence_res *res,
511 int cache_reply,
512 struct rpc_task *task)
513{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400514 struct nfs4_slot *slot;
515 struct nfs4_slot_table *tbl;
516 u8 slotid;
517
518 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400519 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400520 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400521 return 0;
522
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400523 tbl = &session->fc_slot_table;
524
525 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400526 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800527 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamsonea028ac2009-12-04 15:55:38 -0500528 /*
529 * The state manager will wait until the slot table is empty.
530 * Schedule the reset thread
531 */
Andy Adamson05f0d232009-12-04 15:55:37 -0500532 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400533 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500534 dprintk("%s Schedule Session Reset\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500535 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400536 }
537
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800538 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
539 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
540 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
541 spin_unlock(&tbl->slot_tbl_lock);
542 dprintk("%s enforce FIFO order\n", __func__);
543 return -EAGAIN;
544 }
545
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800546 slotid = nfs4_find_slot(tbl);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400547 if (slotid == NFS4_MAX_SLOT_TABLE) {
548 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
549 spin_unlock(&tbl->slot_tbl_lock);
550 dprintk("<-- %s: no free slots\n", __func__);
551 return -EAGAIN;
552 }
553 spin_unlock(&tbl->slot_tbl_lock);
554
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800555 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400556 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400557 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400558 args->sa_slotid = slotid;
559 args->sa_cache_this = cache_reply;
560
561 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
562
Benny Halevy99fe60d2009-04-01 09:22:29 -0400563 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400564 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400565 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400566 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400567 /*
568 * sr_status is only set in decode_sequence, and so will remain
569 * set to 1 if an rpc level failure occurs.
570 */
571 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400572 return 0;
573}
574
Trond Myklebust035168a2010-06-16 09:52:26 -0400575int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400576 struct nfs4_sequence_args *args,
577 struct nfs4_sequence_res *res,
578 int cache_reply,
579 struct rpc_task *task)
580{
Trond Myklebust035168a2010-06-16 09:52:26 -0400581 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400582 int ret = 0;
583
Trond Myklebust035168a2010-06-16 09:52:26 -0400584 if (session == NULL) {
585 args->sa_session = NULL;
586 res->sr_session = NULL;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400587 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400588 }
589
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200590 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400591 __func__, session->clp, session, res->sr_slot ?
592 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400593
594 ret = nfs41_setup_sequence(session, args, res, cache_reply,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400595 task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400596out:
597 dprintk("<-- %s status=%d\n", __func__, ret);
598 return ret;
599}
600
601struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400602 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400603 struct nfs4_sequence_args *seq_args;
604 struct nfs4_sequence_res *seq_res;
605 int cache_reply;
606};
607
608static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
609{
610 struct nfs41_call_sync_data *data = calldata;
611
Trond Myklebust035168a2010-06-16 09:52:26 -0400612 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
613
614 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400615 data->seq_res, data->cache_reply, task))
616 return;
617 rpc_call_start(task);
618}
619
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800620static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
621{
622 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
623 nfs41_call_sync_prepare(task, calldata);
624}
625
Andy Adamson69ab40c2009-04-01 09:22:19 -0400626static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
627{
628 struct nfs41_call_sync_data *data = calldata;
629
Trond Myklebust14516c32010-07-31 14:29:06 -0400630 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400631}
632
Andy Adamsonce5039c2009-04-01 09:22:13 -0400633struct rpc_call_ops nfs41_call_sync_ops = {
634 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400635 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400636};
637
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800638struct rpc_call_ops nfs41_call_priv_sync_ops = {
639 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
640 .rpc_call_done = nfs41_call_sync_done,
641};
642
Trond Myklebust035168a2010-06-16 09:52:26 -0400643static int nfs4_call_sync_sequence(struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400644 struct rpc_message *msg,
645 struct nfs4_sequence_args *args,
646 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800647 int cache_reply,
648 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400649{
650 int ret;
651 struct rpc_task *task;
652 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400653 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400654 .seq_args = args,
655 .seq_res = res,
656 .cache_reply = cache_reply,
657 };
658 struct rpc_task_setup task_setup = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400659 .rpc_client = server->client,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400660 .rpc_message = msg,
661 .callback_ops = &nfs41_call_sync_ops,
662 .callback_data = &data
663 };
664
Benny Halevydfb4f3092010-09-24 09:17:01 -0400665 res->sr_slot = NULL;
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800666 if (privileged)
667 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400668 task = rpc_run_task(&task_setup);
669 if (IS_ERR(task))
670 ret = PTR_ERR(task);
671 else {
672 ret = task->tk_status;
673 rpc_put_task(task);
674 }
675 return ret;
676}
677
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400678int _nfs4_call_sync_session(struct nfs_server *server,
679 struct rpc_message *msg,
680 struct nfs4_sequence_args *args,
681 struct nfs4_sequence_res *res,
682 int cache_reply)
683{
Trond Myklebust035168a2010-06-16 09:52:26 -0400684 return nfs4_call_sync_sequence(server, msg, args, res, cache_reply, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400685}
686
Trond Myklebustdf896452010-06-16 09:52:26 -0400687#else
Trond Myklebust14516c32010-07-31 14:29:06 -0400688static int nfs4_sequence_done(struct rpc_task *task,
689 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400690{
Trond Myklebust14516c32010-07-31 14:29:06 -0400691 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400692}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400693#endif /* CONFIG_NFS_V4_1 */
694
695int _nfs4_call_sync(struct nfs_server *server,
696 struct rpc_message *msg,
697 struct nfs4_sequence_args *args,
698 struct nfs4_sequence_res *res,
699 int cache_reply)
700{
Benny Halevyf3752972009-04-01 09:22:04 -0400701 args->sa_session = res->sr_session = NULL;
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400702 return rpc_call_sync(server->client, msg, 0);
703}
704
705#define nfs4_call_sync(server, msg, args, res, cache_reply) \
Trond Myklebust97dc1352010-06-16 09:52:26 -0400706 (server)->nfs_client->cl_mvops->call_sync((server), (msg), &(args)->seq_args, \
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400707 &(res)->seq_res, (cache_reply))
708
Trond Myklebust38478b22006-05-25 01:40:57 -0400709static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Trond Myklebust38478b22006-05-25 01:40:57 -0400711 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Trond Myklebust38478b22006-05-25 01:40:57 -0400713 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400714 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
715 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400716 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400717 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400718 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100721struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400722 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100723 struct nfs_openargs o_arg;
724 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100725 struct nfs_open_confirmargs c_arg;
726 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100727 struct nfs_fattr f_attr;
728 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400729 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100730 struct dentry *dir;
731 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400732 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100733 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100734 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400735 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100736 int rpc_status;
737 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100738};
739
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400740
741static void nfs4_init_opendata_res(struct nfs4_opendata *p)
742{
743 p->o_res.f_attr = &p->f_attr;
744 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400745 p->o_res.seqid = p->o_arg.seqid;
746 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400747 p->o_res.server = p->o_arg.server;
748 nfs_fattr_init(&p->f_attr);
749 nfs_fattr_init(&p->dir_attr);
750}
751
Trond Myklebustad389da2007-06-05 12:30:00 -0400752static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500753 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400754 const struct iattr *attrs,
755 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100756{
Trond Myklebustad389da2007-06-05 12:30:00 -0400757 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100758 struct inode *dir = parent->d_inode;
759 struct nfs_server *server = NFS_SERVER(dir);
760 struct nfs4_opendata *p;
761
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400762 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100763 if (p == NULL)
764 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400765 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100766 if (p->o_arg.seqid == NULL)
767 goto err_free;
Al Virof6948692010-01-30 13:51:04 -0500768 path_get(path);
769 p->path = *path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100770 p->dir = parent;
771 p->owner = sp;
772 atomic_inc(&sp->so_count);
773 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500774 p->o_arg.open_flags = flags;
775 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400776 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400777 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400778 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100779 p->o_arg.server = server;
780 p->o_arg.bitmask = server->attr_bitmask;
781 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400782 if (flags & O_CREAT) {
783 u32 *s;
784
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100785 p->o_arg.u.attrs = &p->attrs;
786 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400787 s = (u32 *) p->o_arg.u.verifier.data;
788 s[0] = jiffies;
789 s[1] = current->pid;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100790 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100791 p->c_arg.fh = &p->o_res.fh;
792 p->c_arg.stateid = &p->o_res.stateid;
793 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400794 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400795 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100796 return p;
797err_free:
798 kfree(p);
799err:
800 dput(parent);
801 return NULL;
802}
803
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400804static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100805{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400806 struct nfs4_opendata *p = container_of(kref,
807 struct nfs4_opendata, kref);
808
809 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400810 if (p->state != NULL)
811 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400812 nfs4_put_state_owner(p->owner);
813 dput(p->dir);
Jan Blunck31f31db12008-05-02 13:42:45 -0700814 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400815 kfree(p);
816}
817
818static void nfs4_opendata_put(struct nfs4_opendata *p)
819{
820 if (p != NULL)
821 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100822}
823
Trond Myklebust06f814a2006-01-03 09:55:07 +0100824static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
825{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100826 int ret;
827
Trond Myklebust06f814a2006-01-03 09:55:07 +0100828 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100829 return ret;
830}
831
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500832static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400833{
834 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500835
836 if (open_mode & O_EXCL)
837 goto out;
838 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400839 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500840 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
841 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400842 break;
843 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500844 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
845 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400846 break;
847 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500848 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
849 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400850 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500851out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400852 return ret;
853}
854
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500855static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400856{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500857 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400858 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500859 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400860 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500861 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400862 return 1;
863}
864
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500865static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100866{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500867 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100868 case FMODE_WRITE:
869 state->n_wronly++;
870 break;
871 case FMODE_READ:
872 state->n_rdonly++;
873 break;
874 case FMODE_READ|FMODE_WRITE:
875 state->n_rdwr++;
876 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500877 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100878}
879
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500880static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400881{
882 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
883 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
884 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500885 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400886 case FMODE_READ:
887 set_bit(NFS_O_RDONLY_STATE, &state->flags);
888 break;
889 case FMODE_WRITE:
890 set_bit(NFS_O_WRONLY_STATE, &state->flags);
891 break;
892 case FMODE_READ|FMODE_WRITE:
893 set_bit(NFS_O_RDWR_STATE, &state->flags);
894 }
895}
896
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500897static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400898{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400899 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500900 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400901 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400902}
903
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500904static 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 -0700905{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400906 /*
907 * Protect the call to nfs4_state_set_mode_locked and
908 * serialise the stateid update
909 */
910 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400911 if (deleg_stateid != NULL) {
912 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
913 set_bit(NFS_DELEGATED_STATE, &state->flags);
914 }
915 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500916 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400917 write_sequnlock(&state->seqlock);
918 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500919 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700920 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921}
922
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500923static 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 -0500924{
925 struct nfs_inode *nfsi = NFS_I(state->inode);
926 struct nfs_delegation *deleg_cur;
927 int ret = 0;
928
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500929 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500930
931 rcu_read_lock();
932 deleg_cur = rcu_dereference(nfsi->delegation);
933 if (deleg_cur == NULL)
934 goto no_delegation;
935
936 spin_lock(&deleg_cur->lock);
937 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500938 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500939 goto no_delegation_unlock;
940
941 if (delegation == NULL)
942 delegation = &deleg_cur->stateid;
943 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
944 goto no_delegation_unlock;
945
Trond Myklebustb7391f42008-12-23 15:21:52 -0500946 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500947 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500948 ret = 1;
949no_delegation_unlock:
950 spin_unlock(&deleg_cur->lock);
951no_delegation:
952 rcu_read_unlock();
953
954 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500955 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500956 ret = 1;
957 }
958
959 return ret;
960}
961
962
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500963static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400964{
965 struct nfs_delegation *delegation;
966
967 rcu_read_lock();
968 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500969 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400970 rcu_read_unlock();
971 return;
972 }
973 rcu_read_unlock();
974 nfs_inode_return_delegation(inode);
975}
976
Trond Myklebust6ee41262007-07-08 14:11:36 -0400977static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400978{
979 struct nfs4_state *state = opendata->state;
980 struct nfs_inode *nfsi = NFS_I(state->inode);
981 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500982 int open_mode = opendata->o_arg.open_flags & O_EXCL;
983 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400984 nfs4_stateid stateid;
985 int ret = -EAGAIN;
986
Trond Myklebustaac00a82007-07-05 19:02:21 -0400987 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500988 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400989 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500990 if (can_open_cached(state, fmode, open_mode)) {
991 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400992 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400993 goto out_return_state;
994 }
995 spin_unlock(&state->owner->so_lock);
996 }
Trond Myklebust34310432008-12-23 15:21:38 -0500997 rcu_read_lock();
998 delegation = rcu_dereference(nfsi->delegation);
999 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001000 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001001 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001002 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001003 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001004 /* Save the delegation */
1005 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1006 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001007 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001008 if (ret != 0)
1009 goto out;
1010 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001011
1012 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001013 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001014 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001015 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001016out:
1017 return ERR_PTR(ret);
1018out_return_state:
1019 atomic_inc(&state->count);
1020 return state;
1021}
1022
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001023static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1024{
1025 struct inode *inode;
1026 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001027 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001028 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001029
Trond Myklebustaac00a82007-07-05 19:02:21 -04001030 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001031 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001032 goto out;
1033 }
1034
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001035 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001036 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001037 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001038 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001039 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001040 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001041 goto err;
1042 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001043 state = nfs4_get_open_state(inode, data->owner);
1044 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001045 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001046 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001047 int delegation_flags = 0;
1048
Trond Myklebust003707c2007-07-05 18:07:55 -04001049 rcu_read_lock();
1050 delegation = rcu_dereference(NFS_I(inode)->delegation);
1051 if (delegation)
1052 delegation_flags = delegation->flags;
1053 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -05001054 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001055 nfs_inode_set_delegation(state->inode,
1056 data->owner->so_cred,
1057 &data->o_res);
1058 else
1059 nfs_inode_reclaim_delegation(state->inode,
1060 data->owner->so_cred,
1061 &data->o_res);
1062 }
Trond Myklebust34310432008-12-23 15:21:38 -05001063
1064 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001065 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001066 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001067out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001068 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001069err_put_inode:
1070 iput(inode);
1071err:
1072 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001073}
1074
Trond Myklebust864472e2006-01-03 09:55:15 +01001075static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1076{
1077 struct nfs_inode *nfsi = NFS_I(state->inode);
1078 struct nfs_open_context *ctx;
1079
1080 spin_lock(&state->inode->i_lock);
1081 list_for_each_entry(ctx, &nfsi->open_files, list) {
1082 if (ctx->state != state)
1083 continue;
1084 get_nfs_open_context(ctx);
1085 spin_unlock(&state->inode->i_lock);
1086 return ctx;
1087 }
1088 spin_unlock(&state->inode->i_lock);
1089 return ERR_PTR(-ENOENT);
1090}
1091
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001092static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1093{
1094 struct nfs4_opendata *opendata;
1095
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001096 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001097 if (opendata == NULL)
1098 return ERR_PTR(-ENOMEM);
1099 opendata->state = state;
1100 atomic_inc(&state->count);
1101 return opendata;
1102}
1103
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001104static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001105{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001106 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001107 int ret;
1108
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001109 opendata->o_arg.open_flags = 0;
1110 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001111 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1112 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1113 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001114 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001115 if (ret != 0)
1116 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001117 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001118 if (IS_ERR(newstate))
1119 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001120 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001121 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001122 return 0;
1123}
1124
1125static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1126{
Trond Myklebust864472e2006-01-03 09:55:15 +01001127 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001128 int ret;
1129
1130 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001131 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001132 smp_rmb();
1133 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001134 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001135 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001136 if (ret != 0)
1137 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001138 if (newstate != state)
1139 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001140 }
1141 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001142 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001143 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001144 if (ret != 0)
1145 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001146 if (newstate != state)
1147 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001148 }
1149 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001150 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001151 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001152 if (ret != 0)
1153 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001154 if (newstate != state)
1155 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001156 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001157 /*
1158 * We may have performed cached opens for all three recoveries.
1159 * Check if we need to update the current stateid.
1160 */
1161 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1162 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001163 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001164 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1165 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001166 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001167 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001168 return 0;
1169}
1170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171/*
1172 * OPEN_RECLAIM:
1173 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001175static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001177 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001178 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001179 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 int status;
1181
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001182 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1183 if (IS_ERR(opendata))
1184 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001185 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1186 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001187 rcu_read_lock();
1188 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001189 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001190 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001191 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001192 opendata->o_arg.u.delegation_type = delegation_type;
1193 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001194 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 return status;
1196}
1197
Trond Myklebust539cd032007-06-05 11:46:42 -04001198static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
1200 struct nfs_server *server = NFS_SERVER(state->inode);
1201 struct nfs4_exception exception = { };
1202 int err;
1203 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001204 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001205 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001206 break;
1207 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 } while (exception.retry);
1209 return err;
1210}
1211
Trond Myklebust864472e2006-01-03 09:55:15 +01001212static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1213{
1214 struct nfs_open_context *ctx;
1215 int ret;
1216
1217 ctx = nfs4_state_find_open_context(state);
1218 if (IS_ERR(ctx))
1219 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001220 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001221 put_nfs_open_context(ctx);
1222 return ret;
1223}
1224
Trond Myklebust13437e12007-07-06 15:10:43 -04001225static 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 -07001226{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001227 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001228 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001230 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1231 if (IS_ERR(opendata))
1232 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001233 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001234 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001235 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001236 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001237 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001238 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239}
1240
Trond Myklebust13437e12007-07-06 15:10:43 -04001241int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
1243 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001244 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 int err;
1246 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001247 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 switch (err) {
1249 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001250 case -ENOENT:
1251 case -ESTALE:
1252 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001253 case -NFS4ERR_BADSESSION:
1254 case -NFS4ERR_BADSLOT:
1255 case -NFS4ERR_BAD_HIGH_SLOT:
1256 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1257 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001258 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001259 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 case -NFS4ERR_STALE_CLIENTID:
1261 case -NFS4ERR_STALE_STATEID:
1262 case -NFS4ERR_EXPIRED:
1263 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001264 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001265 goto out;
1266 case -ERESTARTSYS:
1267 /*
1268 * The show must go on: exit, but mark the
1269 * stateid as needing recovery.
1270 */
1271 case -NFS4ERR_ADMIN_REVOKED:
1272 case -NFS4ERR_BAD_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001273 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001274 case -EKEYEXPIRED:
1275 /*
1276 * User RPCSEC_GSS context has expired.
1277 * We cannot recover this stateid now, so
1278 * skip it and allow recovery thread to
1279 * proceed.
1280 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001281 case -ENOMEM:
1282 err = 0;
1283 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 }
1285 err = nfs4_handle_exception(server, err, &exception);
1286 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001287out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 return err;
1289}
1290
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001291static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1292{
1293 struct nfs4_opendata *data = calldata;
1294
1295 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001296 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001297 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1298 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001299 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001300 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001301 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001302 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001303}
1304
1305static void nfs4_open_confirm_release(void *calldata)
1306{
1307 struct nfs4_opendata *data = calldata;
1308 struct nfs4_state *state = NULL;
1309
1310 /* If this request hasn't been cancelled, do nothing */
1311 if (data->cancelled == 0)
1312 goto out_free;
1313 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001314 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001315 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001316 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001317 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001318 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001319out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001320 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001321}
1322
1323static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001324 .rpc_call_done = nfs4_open_confirm_done,
1325 .rpc_release = nfs4_open_confirm_release,
1326};
1327
1328/*
1329 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1330 */
1331static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1332{
1333 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1334 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001335 struct rpc_message msg = {
1336 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1337 .rpc_argp = &data->c_arg,
1338 .rpc_resp = &data->c_res,
1339 .rpc_cred = data->owner->so_cred,
1340 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001341 struct rpc_task_setup task_setup_data = {
1342 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001343 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001344 .callback_ops = &nfs4_open_confirm_ops,
1345 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001346 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001347 .flags = RPC_TASK_ASYNC,
1348 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 int status;
1350
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001351 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001352 data->rpc_done = 0;
1353 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001354 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001355 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001356 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001357 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001358 status = nfs4_wait_for_completion_rpc_task(task);
1359 if (status != 0) {
1360 data->cancelled = 1;
1361 smp_wmb();
1362 } else
1363 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001364 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 return status;
1366}
1367
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001368static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001370 struct nfs4_opendata *data = calldata;
1371 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001372
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001373 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1374 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001375 /*
1376 * Check if we still need to send an OPEN call, or if we can use
1377 * a delegation instead.
1378 */
1379 if (data->state != NULL) {
1380 struct nfs_delegation *delegation;
1381
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001382 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001383 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001384 rcu_read_lock();
1385 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1386 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001387 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001388 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001389 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001390 }
1391 rcu_read_unlock();
1392 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001393 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001394 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001395 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001396 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001397 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001398 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1399 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001400 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001401 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001402 &data->o_arg.seq_args,
1403 &data->o_res.seq_res, 1, task))
1404 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001405 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001406 return;
1407out_no_action:
1408 task->tk_action = NULL;
1409
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001410}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001412static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1413{
1414 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1415 nfs4_open_prepare(task, calldata);
1416}
1417
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001418static void nfs4_open_done(struct rpc_task *task, void *calldata)
1419{
1420 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001422 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001423
Trond Myklebust14516c32010-07-31 14:29:06 -04001424 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1425 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001426
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001427 if (task->tk_status == 0) {
1428 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001429 case S_IFREG:
1430 break;
1431 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001432 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001433 break;
1434 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001435 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001436 break;
1437 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001438 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001439 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001440 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001441 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1442 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001443 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001444 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001445}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001446
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001447static void nfs4_open_release(void *calldata)
1448{
1449 struct nfs4_opendata *data = calldata;
1450 struct nfs4_state *state = NULL;
1451
1452 /* If this request hasn't been cancelled, do nothing */
1453 if (data->cancelled == 0)
1454 goto out_free;
1455 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001456 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001457 goto out_free;
1458 /* In case we need an open_confirm, no cleanup! */
1459 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1460 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001461 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001462 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001463 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001464out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001465 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001466}
1467
1468static const struct rpc_call_ops nfs4_open_ops = {
1469 .rpc_call_prepare = nfs4_open_prepare,
1470 .rpc_call_done = nfs4_open_done,
1471 .rpc_release = nfs4_open_release,
1472};
1473
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001474static const struct rpc_call_ops nfs4_recover_open_ops = {
1475 .rpc_call_prepare = nfs4_recover_open_prepare,
1476 .rpc_call_done = nfs4_open_done,
1477 .rpc_release = nfs4_open_release,
1478};
1479
1480static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001481{
1482 struct inode *dir = data->dir->d_inode;
1483 struct nfs_server *server = NFS_SERVER(dir);
1484 struct nfs_openargs *o_arg = &data->o_arg;
1485 struct nfs_openres *o_res = &data->o_res;
1486 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001487 struct rpc_message msg = {
1488 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1489 .rpc_argp = o_arg,
1490 .rpc_resp = o_res,
1491 .rpc_cred = data->owner->so_cred,
1492 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001493 struct rpc_task_setup task_setup_data = {
1494 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001495 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001496 .callback_ops = &nfs4_open_ops,
1497 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001498 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001499 .flags = RPC_TASK_ASYNC,
1500 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001501 int status;
1502
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001503 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001504 data->rpc_done = 0;
1505 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001506 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001507 if (isrecover)
1508 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001509 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001510 if (IS_ERR(task))
1511 return PTR_ERR(task);
1512 status = nfs4_wait_for_completion_rpc_task(task);
1513 if (status != 0) {
1514 data->cancelled = 1;
1515 smp_wmb();
1516 } else
1517 status = data->rpc_status;
1518 rpc_put_task(task);
1519
1520 return status;
1521}
1522
1523static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1524{
1525 struct inode *dir = data->dir->d_inode;
1526 struct nfs_openres *o_res = &data->o_res;
1527 int status;
1528
1529 status = nfs4_run_open_task(data, 1);
1530 if (status != 0 || !data->rpc_done)
1531 return status;
1532
1533 nfs_refresh_inode(dir, o_res->dir_attr);
1534
1535 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1536 status = _nfs4_proc_open_confirm(data);
1537 if (status != 0)
1538 return status;
1539 }
1540
1541 return status;
1542}
1543
1544/*
1545 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1546 */
1547static int _nfs4_proc_open(struct nfs4_opendata *data)
1548{
1549 struct inode *dir = data->dir->d_inode;
1550 struct nfs_server *server = NFS_SERVER(dir);
1551 struct nfs_openargs *o_arg = &data->o_arg;
1552 struct nfs_openres *o_res = &data->o_res;
1553 int status;
1554
1555 status = nfs4_run_open_task(data, 0);
Trond Myklebust3e309912007-07-07 13:19:59 -04001556 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001557 return status;
1558
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001559 if (o_arg->open_flags & O_CREAT) {
1560 update_changeattr(dir, &o_res->cinfo);
1561 nfs_post_op_update_inode(dir, o_res->dir_attr);
1562 } else
1563 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001564 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1565 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001567 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001569 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 }
1571 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001572 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001573 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574}
1575
Andy Adamsond83217c2011-03-01 01:34:17 +00001576static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001577{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001578 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001579 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001580
Trond Myklebusta78cb572009-08-09 15:06:19 -04001581 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001582 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001583 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001584 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001585 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1586 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001587 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001588 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001589 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001590 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001591 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001592}
1593
Andy Adamsond83217c2011-03-01 01:34:17 +00001594static int nfs4_recover_expired_lease(struct nfs_server *server)
1595{
1596 return nfs4_client_recover_expired_lease(server->nfs_client);
1597}
1598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599/*
1600 * OPEN_EXPIRED:
1601 * reclaim state on the server after a network partition.
1602 * Assumes caller holds the appropriate lock
1603 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001604static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001606 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001607 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001609 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1610 if (IS_ERR(opendata))
1611 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001612 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001613 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001614 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001615 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001616 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617}
1618
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001619static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001620{
Trond Myklebust539cd032007-06-05 11:46:42 -04001621 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001622 struct nfs4_exception exception = { };
1623 int err;
1624
1625 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001626 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001627 switch (err) {
1628 default:
1629 goto out;
1630 case -NFS4ERR_GRACE:
1631 case -NFS4ERR_DELAY:
1632 nfs4_handle_exception(server, err, &exception);
1633 err = 0;
1634 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001635 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001636out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001637 return err;
1638}
1639
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1641{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001643 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Trond Myklebust864472e2006-01-03 09:55:15 +01001645 ctx = nfs4_state_find_open_context(state);
1646 if (IS_ERR(ctx))
1647 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001648 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001649 put_nfs_open_context(ctx);
1650 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651}
1652
1653/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001654 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1655 * fields corresponding to attributes that were used to store the verifier.
1656 * Make sure we clobber those fields in the later setattr call
1657 */
1658static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1659{
1660 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1661 !(sattr->ia_valid & ATTR_ATIME_SET))
1662 sattr->ia_valid |= ATTR_ATIME;
1663
1664 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1665 !(sattr->ia_valid & ATTR_MTIME_SET))
1666 sattr->ia_valid |= ATTR_MTIME;
1667}
1668
1669/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001670 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001672static 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 -07001673{
1674 struct nfs4_state_owner *sp;
1675 struct nfs4_state *state = NULL;
1676 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001677 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001678 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
1680 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 status = -ENOMEM;
1682 if (!(sp = nfs4_get_state_owner(server, cred))) {
1683 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1684 goto out_err;
1685 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001686 status = nfs4_recover_expired_lease(server);
1687 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001688 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001689 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001690 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001691 status = -ENOMEM;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001692 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001693 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001694 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
Trond Myklebustaac00a82007-07-05 19:02:21 -04001696 if (path->dentry->d_inode != NULL)
1697 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1698
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001699 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001701 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001703 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001704 status = PTR_ERR(state);
1705 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001706 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001707 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001708 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001709
1710 if (opendata->o_arg.open_flags & O_EXCL) {
1711 nfs4_exclusive_attrset(opendata, sattr);
1712
1713 nfs_fattr_init(opendata->o_res.f_attr);
1714 status = nfs4_do_setattr(state->inode, cred,
1715 opendata->o_res.f_attr, sattr,
1716 state);
1717 if (status == 0)
1718 nfs_setattr_update_inode(state->inode, sattr);
1719 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1720 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001721 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 *res = state;
1724 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001725err_opendata_put:
1726 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001727err_put_state_owner:
1728 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 *res = NULL;
1731 return status;
1732}
1733
1734
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001735static 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 -07001736{
1737 struct nfs4_exception exception = { };
1738 struct nfs4_state *res;
1739 int status;
1740
1741 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001742 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 if (status == 0)
1744 break;
1745 /* NOTE: BAD_SEQID means the server and client disagree about the
1746 * book-keeping w.r.t. state-changing operations
1747 * (OPEN/CLOSE/LOCK/LOCKU...)
1748 * It is actually a sign of a bug on the client or on the server.
1749 *
1750 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001751 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 * have unhashed the old state_owner for us, and that we can
1753 * therefore safely retry using a new one. We should still warn
1754 * the user though...
1755 */
1756 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001757 printk(KERN_WARNING "NFS: v4 server %s "
1758 " returned a bad sequence-id error!\n",
1759 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 exception.retry = 1;
1761 continue;
1762 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001763 /*
1764 * BAD_STATEID on OPEN means that the server cancelled our
1765 * state before it received the OPEN_CONFIRM.
1766 * Recover by retrying the request as per the discussion
1767 * on Page 181 of RFC3530.
1768 */
1769 if (status == -NFS4ERR_BAD_STATEID) {
1770 exception.retry = 1;
1771 continue;
1772 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001773 if (status == -EAGAIN) {
1774 /* We must have found a delegation */
1775 exception.retry = 1;
1776 continue;
1777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1779 status, &exception));
1780 } while (exception.retry);
1781 return res;
1782}
1783
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001784static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1785 struct nfs_fattr *fattr, struct iattr *sattr,
1786 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001788 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001790 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 .iap = sattr,
1792 .server = server,
1793 .bitmask = server->attr_bitmask,
1794 };
1795 struct nfs_setattrres res = {
1796 .fattr = fattr,
1797 .server = server,
1798 };
1799 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001800 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1801 .rpc_argp = &arg,
1802 .rpc_resp = &res,
1803 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001805 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001806 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Trond Myklebust0e574af2005-10-27 22:12:38 -04001808 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001810 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1811 /* Use that stateid */
1812 } else if (state != NULL) {
Trond Myklebust77041ed2010-07-01 12:49:11 -04001813 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001814 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1816
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001817 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001818 if (status == 0 && state != NULL)
1819 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001820 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821}
1822
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001823static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1824 struct nfs_fattr *fattr, struct iattr *sattr,
1825 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001827 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 struct nfs4_exception exception = { };
1829 int err;
1830 do {
1831 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001832 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 &exception);
1834 } while (exception.retry);
1835 return err;
1836}
1837
1838struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001839 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 struct inode *inode;
1841 struct nfs4_state *state;
1842 struct nfs_closeargs arg;
1843 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001844 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001845 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00001846 bool roc;
1847 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848};
1849
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001850static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001851{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001852 struct nfs4_closedata *calldata = data;
1853 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001854
Fred Isamanf7e89172011-01-06 11:36:32 +00001855 if (calldata->roc)
1856 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001857 nfs4_put_open_state(calldata->state);
1858 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001859 nfs4_put_state_owner(sp);
Jan Blunck31f31db12008-05-02 13:42:45 -07001860 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001861 kfree(calldata);
1862}
1863
Trond Myklebust88069f72009-12-08 08:33:16 -05001864static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1865 fmode_t fmode)
1866{
1867 spin_lock(&state->owner->so_lock);
1868 if (!(fmode & FMODE_READ))
1869 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1870 if (!(fmode & FMODE_WRITE))
1871 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1872 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1873 spin_unlock(&state->owner->so_lock);
1874}
1875
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001876static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001878 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 struct nfs_server *server = NFS_SERVER(calldata->inode);
1881
Trond Myklebust14516c32010-07-31 14:29:06 -04001882 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1883 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 /* hmm. we are done with the inode, and in the process of freeing
1885 * the state_owner. we keep this around to process errors
1886 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 switch (task->tk_status) {
1888 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00001889 if (calldata->roc)
1890 pnfs_roc_set_barrier(state->inode,
1891 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04001892 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001893 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001894 nfs4_close_clear_stateid_flags(state,
1895 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 break;
1897 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001898 case -NFS4ERR_OLD_STATEID:
1899 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001901 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001902 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001904 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1905 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001907 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001908 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909}
1910
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001911static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001913 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001914 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001915 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001916
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001917 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001918 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001919
Trond Myklebust88069f72009-12-08 08:33:16 -05001920 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1921 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001922 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001923 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001924 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001925 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001926 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1927 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1928 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001929 }
1930 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001931 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1932 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1933 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001934 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001935 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001936 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05001937
1938 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001939 /* Note: exit _without_ calling nfs4_close_done */
1940 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001941 return;
1942 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001943
Fred Isamanf7e89172011-01-06 11:36:32 +00001944 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001945 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00001946 if (calldata->roc &&
1947 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
1948 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
1949 task, NULL);
1950 return;
1951 }
1952 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001953
Trond Myklebust516a6af2005-10-27 22:12:41 -04001954 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001955 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001956 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Andy Adamson19ddab02009-04-01 09:22:20 -04001957 &calldata->arg.seq_args, &calldata->res.seq_res,
1958 1, task))
1959 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001960 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961}
1962
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001963static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001964 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001965 .rpc_call_done = nfs4_close_done,
1966 .rpc_release = nfs4_free_closedata,
1967};
1968
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969/*
1970 * It is possible for data to be read/written from a mem-mapped file
1971 * after the sys_close call (which hits the vfs layer as a flush).
1972 * This means that we can't safely call nfsv4 close on a file until
1973 * the inode is cleared. This in turn means that we are not good
1974 * NFSv4 citizens - we do not indicate to the server to update the file's
1975 * share state even when we are done with one of the three share
1976 * stateid's in the inode.
1977 *
1978 * NOTE: Caller must be holding the sp->so_owner semaphore!
1979 */
Fred Isamanf7e89172011-01-06 11:36:32 +00001980int 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 -07001981{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001982 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001984 struct nfs4_state_owner *sp = state->owner;
1985 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001986 struct rpc_message msg = {
1987 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1988 .rpc_cred = state->owner->so_cred,
1989 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001990 struct rpc_task_setup task_setup_data = {
1991 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001992 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001993 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05001994 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001995 .flags = RPC_TASK_ASYNC,
1996 };
Trond Myklebust95121352005-10-18 14:20:12 -07001997 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001999 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002001 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002002 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002004 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002005 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002007 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002008 if (calldata->arg.seqid == NULL)
2009 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002010 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002011 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002012 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002013 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002014 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002015 calldata->roc = roc;
Al Virof6948692010-01-30 13:51:04 -05002016 path_get(path);
2017 calldata->path = *path;
Trond Myklebust95121352005-10-18 14:20:12 -07002018
Trond Myklebust1174dd12010-12-21 10:52:24 -05002019 msg.rpc_argp = &calldata->arg;
2020 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002021 task_setup_data.callback_data = calldata;
2022 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002023 if (IS_ERR(task))
2024 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002025 status = 0;
2026 if (wait)
2027 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002028 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002029 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002030out_free_calldata:
2031 kfree(calldata);
2032out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002033 if (roc)
2034 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002035 nfs4_put_open_state(state);
2036 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002037 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038}
2039
Trond Myklebust2b484292010-09-17 10:56:51 -04002040static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002041nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 struct nfs4_state *state;
2044
Trond Myklebust565277f2007-10-15 18:17:53 -04002045 /* Protect against concurrent sillydeletes */
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002046 state = nfs4_do_open(dir, &ctx->path, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002047 if (IS_ERR(state))
2048 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002049 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002050 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051}
2052
Trond Myklebust1185a552009-12-03 15:54:02 -05002053static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002054{
2055 if (ctx->state == NULL)
2056 return;
2057 if (is_sync)
2058 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2059 else
2060 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2061}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
2063static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2064{
Benny Halevy43652ad2009-04-01 09:21:54 -04002065 struct nfs4_server_caps_arg args = {
2066 .fhandle = fhandle,
2067 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 struct nfs4_server_caps_res res = {};
2069 struct rpc_message msg = {
2070 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002071 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 .rpc_resp = &res,
2073 };
2074 int status;
2075
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002076 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 if (status == 0) {
2078 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002079 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2080 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2081 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2082 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2083 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2085 server->caps |= NFS_CAP_ACLS;
2086 if (res.has_links != 0)
2087 server->caps |= NFS_CAP_HARDLINKS;
2088 if (res.has_symlinks != 0)
2089 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002090 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2091 server->caps |= NFS_CAP_FILEID;
2092 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2093 server->caps |= NFS_CAP_MODE;
2094 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2095 server->caps |= NFS_CAP_NLINK;
2096 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2097 server->caps |= NFS_CAP_OWNER;
2098 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2099 server->caps |= NFS_CAP_OWNER_GROUP;
2100 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2101 server->caps |= NFS_CAP_ATIME;
2102 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2103 server->caps |= NFS_CAP_CTIME;
2104 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2105 server->caps |= NFS_CAP_MTIME;
2106
Trond Myklebusta65318b2009-03-11 14:10:28 -04002107 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2108 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2109 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 server->acl_bitmask = res.acl_bitmask;
2111 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002112
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 return status;
2114}
2115
Trond Myklebust55a97592006-06-09 09:34:19 -04002116int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117{
2118 struct nfs4_exception exception = { };
2119 int err;
2120 do {
2121 err = nfs4_handle_exception(server,
2122 _nfs4_server_capabilities(server, fhandle),
2123 &exception);
2124 } while (exception.retry);
2125 return err;
2126}
2127
2128static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2129 struct nfs_fsinfo *info)
2130{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 struct nfs4_lookup_root_arg args = {
2132 .bitmask = nfs4_fattr_bitmap,
2133 };
2134 struct nfs4_lookup_res res = {
2135 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002136 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 .fh = fhandle,
2138 };
2139 struct rpc_message msg = {
2140 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2141 .rpc_argp = &args,
2142 .rpc_resp = &res,
2143 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002144
Trond Myklebust0e574af2005-10-27 22:12:38 -04002145 nfs_fattr_init(info->fattr);
Trond Myklebustfccba802009-07-21 16:48:07 -04002146 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147}
2148
2149static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2150 struct nfs_fsinfo *info)
2151{
2152 struct nfs4_exception exception = { };
2153 int err;
2154 do {
2155 err = nfs4_handle_exception(server,
2156 _nfs4_lookup_root(server, fhandle, info),
2157 &exception);
2158 } while (exception.retry);
2159 return err;
2160}
2161
David Howells54ceac42006-08-22 20:06:13 -04002162/*
2163 * get the file handle for the "/" directory on the server
2164 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002166 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 int status;
2169
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 if (status == 0)
2172 status = nfs4_server_capabilities(server, fhandle);
2173 if (status == 0)
2174 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002175 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176}
2177
Manoj Naik6b97fd32006-06-09 09:34:29 -04002178/*
2179 * Get locations and (maybe) other attributes of a referral.
2180 * Note that we'll actually follow the referral later when
2181 * we detect fsid mismatch in inode revalidation
2182 */
Trond Myklebust56659e92007-07-17 21:52:39 -04002183static 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 -04002184{
2185 int status = -ENOMEM;
2186 struct page *page = NULL;
2187 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002188
2189 page = alloc_page(GFP_KERNEL);
2190 if (page == NULL)
2191 goto out;
2192 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2193 if (locations == NULL)
2194 goto out;
2195
Trond Myklebustc228fd32007-01-13 02:28:11 -05002196 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002197 if (status != 0)
2198 goto out;
2199 /* Make sure server returned a different fsid for the referral */
2200 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07002201 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 -04002202 status = -EIO;
2203 goto out;
2204 }
2205
2206 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2207 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2208 if (!fattr->mode)
2209 fattr->mode = S_IFDIR;
2210 memset(fhandle, 0, sizeof(struct nfs_fh));
2211out:
2212 if (page)
2213 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002214 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002215 return status;
2216}
2217
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2219{
2220 struct nfs4_getattr_arg args = {
2221 .fh = fhandle,
2222 .bitmask = server->attr_bitmask,
2223 };
2224 struct nfs4_getattr_res res = {
2225 .fattr = fattr,
2226 .server = server,
2227 };
2228 struct rpc_message msg = {
2229 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2230 .rpc_argp = &args,
2231 .rpc_resp = &res,
2232 };
2233
Trond Myklebust0e574af2005-10-27 22:12:38 -04002234 nfs_fattr_init(fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002235 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236}
2237
2238static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2239{
2240 struct nfs4_exception exception = { };
2241 int err;
2242 do {
2243 err = nfs4_handle_exception(server,
2244 _nfs4_proc_getattr(server, fhandle, fattr),
2245 &exception);
2246 } while (exception.retry);
2247 return err;
2248}
2249
2250/*
2251 * The file is not closed if it is opened due to the a request to change
2252 * the size of the file. The open call will not be needed once the
2253 * VFS layer lookup-intents are implemented.
2254 *
2255 * Close is called when the inode is destroyed.
2256 * If we haven't opened the file for O_WRONLY, we
2257 * need to in the size_change case to obtain a stateid.
2258 *
2259 * Got race?
2260 * Because OPEN is always done by name in nfsv4, it is
2261 * possible that we opened a different file by the same
2262 * name. We can recognize this race condition, but we
2263 * can't do anything about it besides returning an error.
2264 *
2265 * This will be fixed with VFS changes (lookup-intent).
2266 */
2267static int
2268nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2269 struct iattr *sattr)
2270{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002271 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002272 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002273 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 int status;
2275
Trond Myklebust0e574af2005-10-27 22:12:38 -04002276 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
Trond Myklebustd5308382005-11-04 15:33:38 -05002278 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002279 if (sattr->ia_valid & ATTR_FILE) {
2280 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002281
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002282 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002283 if (ctx) {
2284 cred = ctx->cred;
2285 state = ctx->state;
2286 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002287 }
2288
2289 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002290 if (status == 0)
2291 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 return status;
2293}
2294
Trond Myklebust56659e92007-07-17 21:52:39 -04002295static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2296 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04002297 struct nfs_fattr *fattr)
2298{
2299 int status;
2300 struct nfs4_lookup_arg args = {
2301 .bitmask = server->attr_bitmask,
2302 .dir_fh = dirfh,
2303 .name = name,
2304 };
2305 struct nfs4_lookup_res res = {
2306 .server = server,
2307 .fattr = fattr,
2308 .fh = fhandle,
2309 };
2310 struct rpc_message msg = {
2311 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2312 .rpc_argp = &args,
2313 .rpc_resp = &res,
2314 };
2315
2316 nfs_fattr_init(fattr);
2317
2318 dprintk("NFS call lookupfh %s\n", name->name);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002319 status = nfs4_call_sync(server, &msg, &args, &res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002320 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002321 return status;
2322}
2323
2324static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2325 struct qstr *name, struct nfs_fh *fhandle,
2326 struct nfs_fattr *fattr)
2327{
2328 struct nfs4_exception exception = { };
2329 int err;
2330 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04002331 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2332 /* FIXME: !!!! */
2333 if (err == -NFS4ERR_MOVED) {
2334 err = -EREMOTE;
2335 break;
2336 }
2337 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002338 } while (exception.retry);
2339 return err;
2340}
2341
Trond Myklebust56659e92007-07-17 21:52:39 -04002342static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2344{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002345 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
2347 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002348 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002349 if (status == -NFS4ERR_MOVED)
2350 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 dprintk("NFS reply lookup: %d\n", status);
2352 return status;
2353}
2354
2355static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2356{
2357 struct nfs4_exception exception = { };
2358 int err;
2359 do {
2360 err = nfs4_handle_exception(NFS_SERVER(dir),
2361 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2362 &exception);
2363 } while (exception.retry);
2364 return err;
2365}
2366
2367static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2368{
Trond Myklebust76b32992007-08-10 17:45:11 -04002369 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 struct nfs4_accessargs args = {
2371 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002372 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002374 struct nfs4_accessres res = {
2375 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002376 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 struct rpc_message msg = {
2378 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2379 .rpc_argp = &args,
2380 .rpc_resp = &res,
2381 .rpc_cred = entry->cred,
2382 };
2383 int mode = entry->mask;
2384 int status;
2385
2386 /*
2387 * Determine which access bits we want to ask for...
2388 */
2389 if (mode & MAY_READ)
2390 args.access |= NFS4_ACCESS_READ;
2391 if (S_ISDIR(inode->i_mode)) {
2392 if (mode & MAY_WRITE)
2393 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2394 if (mode & MAY_EXEC)
2395 args.access |= NFS4_ACCESS_LOOKUP;
2396 } else {
2397 if (mode & MAY_WRITE)
2398 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2399 if (mode & MAY_EXEC)
2400 args.access |= NFS4_ACCESS_EXECUTE;
2401 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002402
2403 res.fattr = nfs_alloc_fattr();
2404 if (res.fattr == NULL)
2405 return -ENOMEM;
2406
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002407 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 if (!status) {
2409 entry->mask = 0;
2410 if (res.access & NFS4_ACCESS_READ)
2411 entry->mask |= MAY_READ;
2412 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2413 entry->mask |= MAY_WRITE;
2414 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2415 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002416 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002418 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 return status;
2420}
2421
2422static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2423{
2424 struct nfs4_exception exception = { };
2425 int err;
2426 do {
2427 err = nfs4_handle_exception(NFS_SERVER(inode),
2428 _nfs4_proc_access(inode, entry),
2429 &exception);
2430 } while (exception.retry);
2431 return err;
2432}
2433
2434/*
2435 * TODO: For the time being, we don't try to get any attributes
2436 * along with any of the zero-copy operations READ, READDIR,
2437 * READLINK, WRITE.
2438 *
2439 * In the case of the first three, we want to put the GETATTR
2440 * after the read-type operation -- this is because it is hard
2441 * to predict the length of a GETATTR response in v4, and thus
2442 * align the READ data correctly. This means that the GETATTR
2443 * may end up partially falling into the page cache, and we should
2444 * shift it into the 'tail' of the xdr_buf before processing.
2445 * To do this efficiently, we need to know the total length
2446 * of data received, which doesn't seem to be available outside
2447 * of the RPC layer.
2448 *
2449 * In the case of WRITE, we also want to put the GETATTR after
2450 * the operation -- in this case because we want to make sure
2451 * we get the post-operation mtime and size. This means that
2452 * we can't use xdr_encode_pages() as written: we need a variant
2453 * of it which would leave room in the 'tail' iovec.
2454 *
2455 * Both of these changes to the XDR layer would in fact be quite
2456 * minor, but I decided to leave them for a subsequent patch.
2457 */
2458static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2459 unsigned int pgbase, unsigned int pglen)
2460{
2461 struct nfs4_readlink args = {
2462 .fh = NFS_FH(inode),
2463 .pgbase = pgbase,
2464 .pglen = pglen,
2465 .pages = &page,
2466 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002467 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 struct rpc_message msg = {
2469 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2470 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002471 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 };
2473
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002474 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475}
2476
2477static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2478 unsigned int pgbase, unsigned int pglen)
2479{
2480 struct nfs4_exception exception = { };
2481 int err;
2482 do {
2483 err = nfs4_handle_exception(NFS_SERVER(inode),
2484 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2485 &exception);
2486 } while (exception.retry);
2487 return err;
2488}
2489
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490/*
2491 * Got race?
2492 * We will need to arrange for the VFS layer to provide an atomic open.
2493 * Until then, this create/open method is prone to inefficiency and race
2494 * conditions due to the lookup, create, and open VFS calls from sys_open()
2495 * placed on the wire.
2496 *
2497 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2498 * The file will be opened again in the subsequent VFS open call
2499 * (nfs4_proc_file_open).
2500 *
2501 * The open for read will just hang around to be used by any process that
2502 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2503 */
2504
2505static int
2506nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002507 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508{
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002509 struct path my_path = {
Trond Myklebustad389da2007-06-05 12:30:00 -04002510 .dentry = dentry,
2511 };
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002512 struct path *path = &my_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002514 struct rpc_cred *cred = NULL;
2515 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 int status = 0;
2517
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002518 if (ctx != NULL) {
2519 cred = ctx->cred;
2520 path = &ctx->path;
2521 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002523 sattr->ia_mode &= ~current_umask();
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002524 state = nfs4_do_open(dir, path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002525 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 if (IS_ERR(state)) {
2527 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002528 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002530 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002531 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002532 if (ctx != NULL)
2533 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002534 else
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002535 nfs4_close_sync(path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536out:
2537 return status;
2538}
2539
2540static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2541{
Trond Myklebust16e42952005-10-27 22:12:44 -04002542 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002543 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002545 .name.len = name->len,
2546 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002547 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002549 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002550 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002551 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002553 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2554 .rpc_argp = &args,
2555 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002557 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
Trond Myklebustd3468902010-04-16 16:22:50 -04002559 res.dir_attr = nfs_alloc_fattr();
2560 if (res.dir_attr == NULL)
2561 goto out;
2562
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002563 status = nfs4_call_sync(server, &msg, &args, &res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002564 if (status == 0) {
2565 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002566 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002567 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002568 nfs_free_fattr(res.dir_attr);
2569out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 return status;
2571}
2572
2573static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2574{
2575 struct nfs4_exception exception = { };
2576 int err;
2577 do {
2578 err = nfs4_handle_exception(NFS_SERVER(dir),
2579 _nfs4_proc_remove(dir, name),
2580 &exception);
2581 } while (exception.retry);
2582 return err;
2583}
2584
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002585static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002587 struct nfs_server *server = NFS_SERVER(dir);
2588 struct nfs_removeargs *args = msg->rpc_argp;
2589 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590
Trond Myklebusta65318b2009-03-11 14:10:28 -04002591 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002592 res->server = server;
Benny Halevydfb4f3092010-09-24 09:17:01 -04002593 res->seq_res.sr_slot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595}
2596
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002597static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002599 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2600
Trond Myklebust14516c32010-07-31 14:29:06 -04002601 if (!nfs4_sequence_done(task, &res->seq_res))
2602 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002603 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002604 return 0;
2605 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002606 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002607 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608}
2609
Jeff Laytond3d41522010-09-17 17:31:57 -04002610static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2611{
2612 struct nfs_server *server = NFS_SERVER(dir);
2613 struct nfs_renameargs *arg = msg->rpc_argp;
2614 struct nfs_renameres *res = msg->rpc_resp;
2615
2616 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2617 arg->bitmask = server->attr_bitmask;
2618 res->server = server;
2619}
2620
2621static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2622 struct inode *new_dir)
2623{
2624 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2625
2626 if (!nfs4_sequence_done(task, &res->seq_res))
2627 return 0;
2628 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2629 return 0;
2630
2631 update_changeattr(old_dir, &res->old_cinfo);
2632 nfs_post_op_update_inode(old_dir, res->old_fattr);
2633 update_changeattr(new_dir, &res->new_cinfo);
2634 nfs_post_op_update_inode(new_dir, res->new_fattr);
2635 return 1;
2636}
2637
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2639 struct inode *new_dir, struct qstr *new_name)
2640{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002641 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002642 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 .old_dir = NFS_FH(old_dir),
2644 .new_dir = NFS_FH(new_dir),
2645 .old_name = old_name,
2646 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002647 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002649 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002650 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002651 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 struct rpc_message msg = {
2653 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2654 .rpc_argp = &arg,
2655 .rpc_resp = &res,
2656 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002657 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658
Trond Myklebust011fff72010-04-16 16:22:49 -04002659 res.old_fattr = nfs_alloc_fattr();
2660 res.new_fattr = nfs_alloc_fattr();
2661 if (res.old_fattr == NULL || res.new_fattr == NULL)
2662 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663
Trond Myklebust011fff72010-04-16 16:22:49 -04002664 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 if (!status) {
2666 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002667 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002669 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002671out:
2672 nfs_free_fattr(res.new_fattr);
2673 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 return status;
2675}
2676
2677static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2678 struct inode *new_dir, struct qstr *new_name)
2679{
2680 struct nfs4_exception exception = { };
2681 int err;
2682 do {
2683 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2684 _nfs4_proc_rename(old_dir, old_name,
2685 new_dir, new_name),
2686 &exception);
2687 } while (exception.retry);
2688 return err;
2689}
2690
2691static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2692{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002693 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 struct nfs4_link_arg arg = {
2695 .fh = NFS_FH(inode),
2696 .dir_fh = NFS_FH(dir),
2697 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002698 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002700 struct nfs4_link_res res = {
2701 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002702 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 struct rpc_message msg = {
2704 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2705 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002706 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002708 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709
Trond Myklebust136f2622010-04-16 16:22:49 -04002710 res.fattr = nfs_alloc_fattr();
2711 res.dir_attr = nfs_alloc_fattr();
2712 if (res.fattr == NULL || res.dir_attr == NULL)
2713 goto out;
2714
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002715 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002716 if (!status) {
2717 update_changeattr(dir, &res.cinfo);
2718 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002719 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002720 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002721out:
2722 nfs_free_fattr(res.dir_attr);
2723 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 return status;
2725}
2726
2727static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2728{
2729 struct nfs4_exception exception = { };
2730 int err;
2731 do {
2732 err = nfs4_handle_exception(NFS_SERVER(inode),
2733 _nfs4_proc_link(inode, dir, name),
2734 &exception);
2735 } while (exception.retry);
2736 return err;
2737}
2738
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002739struct nfs4_createdata {
2740 struct rpc_message msg;
2741 struct nfs4_create_arg arg;
2742 struct nfs4_create_res res;
2743 struct nfs_fh fh;
2744 struct nfs_fattr fattr;
2745 struct nfs_fattr dir_fattr;
2746};
2747
2748static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2749 struct qstr *name, struct iattr *sattr, u32 ftype)
2750{
2751 struct nfs4_createdata *data;
2752
2753 data = kzalloc(sizeof(*data), GFP_KERNEL);
2754 if (data != NULL) {
2755 struct nfs_server *server = NFS_SERVER(dir);
2756
2757 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2758 data->msg.rpc_argp = &data->arg;
2759 data->msg.rpc_resp = &data->res;
2760 data->arg.dir_fh = NFS_FH(dir);
2761 data->arg.server = server;
2762 data->arg.name = name;
2763 data->arg.attrs = sattr;
2764 data->arg.ftype = ftype;
2765 data->arg.bitmask = server->attr_bitmask;
2766 data->res.server = server;
2767 data->res.fh = &data->fh;
2768 data->res.fattr = &data->fattr;
2769 data->res.dir_fattr = &data->dir_fattr;
2770 nfs_fattr_init(data->res.fattr);
2771 nfs_fattr_init(data->res.dir_fattr);
2772 }
2773 return data;
2774}
2775
2776static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2777{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002778 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2779 &data->arg, &data->res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002780 if (status == 0) {
2781 update_changeattr(dir, &data->res.dir_cinfo);
2782 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2783 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2784 }
2785 return status;
2786}
2787
2788static void nfs4_free_createdata(struct nfs4_createdata *data)
2789{
2790 kfree(data);
2791}
2792
Chuck Lever4f390c12006-08-22 20:06:22 -04002793static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002794 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002796 struct nfs4_createdata *data;
2797 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798
Chuck Lever94a6d752006-08-22 20:06:23 -04002799 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002800 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002801
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002802 status = -ENOMEM;
2803 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2804 if (data == NULL)
2805 goto out;
2806
2807 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2808 data->arg.u.symlink.pages = &page;
2809 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002811 status = nfs4_do_create(dir, dentry, data);
2812
2813 nfs4_free_createdata(data);
2814out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 return status;
2816}
2817
Chuck Lever4f390c12006-08-22 20:06:22 -04002818static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002819 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820{
2821 struct nfs4_exception exception = { };
2822 int err;
2823 do {
2824 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002825 _nfs4_proc_symlink(dir, dentry, page,
2826 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 &exception);
2828 } while (exception.retry);
2829 return err;
2830}
2831
2832static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2833 struct iattr *sattr)
2834{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002835 struct nfs4_createdata *data;
2836 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002838 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2839 if (data == NULL)
2840 goto out;
2841
2842 status = nfs4_do_create(dir, dentry, data);
2843
2844 nfs4_free_createdata(data);
2845out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 return status;
2847}
2848
2849static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2850 struct iattr *sattr)
2851{
2852 struct nfs4_exception exception = { };
2853 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002854
2855 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 do {
2857 err = nfs4_handle_exception(NFS_SERVER(dir),
2858 _nfs4_proc_mkdir(dir, dentry, sattr),
2859 &exception);
2860 } while (exception.retry);
2861 return err;
2862}
2863
2864static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002865 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866{
2867 struct inode *dir = dentry->d_inode;
2868 struct nfs4_readdir_arg args = {
2869 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002870 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 .pgbase = 0,
2872 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002873 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04002874 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 };
2876 struct nfs4_readdir_res res;
2877 struct rpc_message msg = {
2878 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2879 .rpc_argp = &args,
2880 .rpc_resp = &res,
2881 .rpc_cred = cred,
2882 };
2883 int status;
2884
Harvey Harrison3110ff82008-05-02 13:42:44 -07002885 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002886 dentry->d_parent->d_name.name,
2887 dentry->d_name.name,
2888 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2890 res.pgbase = args.pgbase;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002891 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
Trond Myklebustac396122010-11-15 20:26:22 -05002892 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05002894 status += args.pgbase;
2895 }
Trond Myklebustc4812992007-09-28 17:11:45 -04002896
2897 nfs_invalidate_atime(dir);
2898
Harvey Harrison3110ff82008-05-02 13:42:44 -07002899 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 return status;
2901}
2902
2903static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002904 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905{
2906 struct nfs4_exception exception = { };
2907 int err;
2908 do {
2909 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2910 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002911 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 &exception);
2913 } while (exception.retry);
2914 return err;
2915}
2916
2917static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2918 struct iattr *sattr, dev_t rdev)
2919{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002920 struct nfs4_createdata *data;
2921 int mode = sattr->ia_mode;
2922 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923
2924 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2925 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002926
2927 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2928 if (data == NULL)
2929 goto out;
2930
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002932 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002934 data->arg.ftype = NF4BLK;
2935 data->arg.u.device.specdata1 = MAJOR(rdev);
2936 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 }
2938 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002939 data->arg.ftype = NF4CHR;
2940 data->arg.u.device.specdata1 = MAJOR(rdev);
2941 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002944 status = nfs4_do_create(dir, dentry, data);
2945
2946 nfs4_free_createdata(data);
2947out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 return status;
2949}
2950
2951static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2952 struct iattr *sattr, dev_t rdev)
2953{
2954 struct nfs4_exception exception = { };
2955 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002956
2957 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 do {
2959 err = nfs4_handle_exception(NFS_SERVER(dir),
2960 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2961 &exception);
2962 } while (exception.retry);
2963 return err;
2964}
2965
2966static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2967 struct nfs_fsstat *fsstat)
2968{
2969 struct nfs4_statfs_arg args = {
2970 .fh = fhandle,
2971 .bitmask = server->attr_bitmask,
2972 };
Benny Halevy24ad1482009-04-01 09:21:56 -04002973 struct nfs4_statfs_res res = {
2974 .fsstat = fsstat,
2975 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 struct rpc_message msg = {
2977 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2978 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04002979 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 };
2981
Trond Myklebust0e574af2005-10-27 22:12:38 -04002982 nfs_fattr_init(fsstat->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002983 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984}
2985
2986static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2987{
2988 struct nfs4_exception exception = { };
2989 int err;
2990 do {
2991 err = nfs4_handle_exception(server,
2992 _nfs4_proc_statfs(server, fhandle, fsstat),
2993 &exception);
2994 } while (exception.retry);
2995 return err;
2996}
2997
2998static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2999 struct nfs_fsinfo *fsinfo)
3000{
3001 struct nfs4_fsinfo_arg args = {
3002 .fh = fhandle,
3003 .bitmask = server->attr_bitmask,
3004 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003005 struct nfs4_fsinfo_res res = {
3006 .fsinfo = fsinfo,
3007 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 struct rpc_message msg = {
3009 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3010 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003011 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 };
3013
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003014 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015}
3016
3017static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3018{
3019 struct nfs4_exception exception = { };
3020 int err;
3021
3022 do {
3023 err = nfs4_handle_exception(server,
3024 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3025 &exception);
3026 } while (exception.retry);
3027 return err;
3028}
3029
3030static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3031{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003032 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3034}
3035
3036static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3037 struct nfs_pathconf *pathconf)
3038{
3039 struct nfs4_pathconf_arg args = {
3040 .fh = fhandle,
3041 .bitmask = server->attr_bitmask,
3042 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003043 struct nfs4_pathconf_res res = {
3044 .pathconf = pathconf,
3045 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 struct rpc_message msg = {
3047 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3048 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003049 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 };
3051
3052 /* None of the pathconf attributes are mandatory to implement */
3053 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3054 memset(pathconf, 0, sizeof(*pathconf));
3055 return 0;
3056 }
3057
Trond Myklebust0e574af2005-10-27 22:12:38 -04003058 nfs_fattr_init(pathconf->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003059 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060}
3061
3062static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3063 struct nfs_pathconf *pathconf)
3064{
3065 struct nfs4_exception exception = { };
3066 int err;
3067
3068 do {
3069 err = nfs4_handle_exception(server,
3070 _nfs4_proc_pathconf(server, fhandle, pathconf),
3071 &exception);
3072 } while (exception.retry);
3073 return err;
3074}
3075
Trond Myklebustec06c092006-03-20 13:44:27 -05003076static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077{
Trond Myklebustec06c092006-03-20 13:44:27 -05003078 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003080 dprintk("--> %s\n", __func__);
3081
Trond Myklebust14516c32010-07-31 14:29:06 -04003082 if (!nfs4_sequence_done(task, &data->res.seq_res))
3083 return -EAGAIN;
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003084
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003085 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003086 nfs_restart_rpc(task, server->nfs_client);
Trond Myklebustec06c092006-03-20 13:44:27 -05003087 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003089
3090 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003092 renew_lease(server, data->timestamp);
3093 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094}
3095
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003096static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 data->timestamp = jiffies;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003099 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100}
3101
Trond Myklebust788e7a82006-03-20 13:44:27 -05003102static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 struct inode *inode = data->inode;
3105
Trond Myklebust14516c32010-07-31 14:29:06 -04003106 if (!nfs4_sequence_done(task, &data->res.seq_res))
3107 return -EAGAIN;
Andy Adamsondef6ed72009-04-01 09:22:26 -04003108
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003109 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003110 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003111 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003113 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003115 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003116 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003117 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118}
3119
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003120static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003122 struct nfs_server *server = NFS_SERVER(data->inode);
3123
Trond Myklebusta65318b2009-03-11 14:10:28 -04003124 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003125 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 data->timestamp = jiffies;
3127
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003128 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129}
3130
Trond Myklebust788e7a82006-03-20 13:44:27 -05003131static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 struct inode *inode = data->inode;
3134
Trond Myklebust14516c32010-07-31 14:29:06 -04003135 if (!nfs4_sequence_done(task, &data->res.seq_res))
3136 return -EAGAIN;
3137
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003138 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003139 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003140 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003142 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003143 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144}
3145
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003146static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003148 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149
Trond Myklebusta65318b2009-03-11 14:10:28 -04003150 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003151 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003152 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153}
3154
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003155struct nfs4_renewdata {
3156 struct nfs_client *client;
3157 unsigned long timestamp;
3158};
3159
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160/*
3161 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3162 * standalone procedure for queueing an asynchronous RENEW.
3163 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003164static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003165{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003166 struct nfs4_renewdata *data = calldata;
3167 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003168
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003169 if (atomic_read(&clp->cl_count) > 1)
3170 nfs4_schedule_state_renewal(clp);
3171 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003172 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003173}
3174
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003175static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003177 struct nfs4_renewdata *data = calldata;
3178 struct nfs_client *clp = data->client;
3179 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180
3181 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003182 /* Unless we're shutting down, schedule state recovery! */
3183 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003184 nfs4_schedule_lease_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 return;
3186 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003187 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188}
3189
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003190static const struct rpc_call_ops nfs4_renew_ops = {
3191 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003192 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003193};
3194
David Howellsadfa6f92006-08-22 20:06:08 -04003195int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196{
3197 struct rpc_message msg = {
3198 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3199 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003200 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003202 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003204 if (!atomic_inc_not_zero(&clp->cl_count))
3205 return -EIO;
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003206 data = kmalloc(sizeof(*data), GFP_KERNEL);
3207 if (data == NULL)
3208 return -ENOMEM;
3209 data->client = clp;
3210 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003212 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213}
3214
David Howellsadfa6f92006-08-22 20:06:08 -04003215int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216{
3217 struct rpc_message msg = {
3218 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3219 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003220 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 };
3222 unsigned long now = jiffies;
3223 int status;
3224
3225 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3226 if (status < 0)
3227 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003228 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 return 0;
3230}
3231
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003232static inline int nfs4_server_supports_acls(struct nfs_server *server)
3233{
3234 return (server->caps & NFS_CAP_ACLS)
3235 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3236 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3237}
3238
3239/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3240 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3241 * the stack.
3242 */
3243#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3244
3245static void buf_to_pages(const void *buf, size_t buflen,
3246 struct page **pages, unsigned int *pgbase)
3247{
3248 const void *p = buf;
3249
3250 *pgbase = offset_in_page(buf);
3251 p -= *pgbase;
3252 while (p < buf + buflen) {
3253 *(pages++) = virt_to_page(p);
3254 p += PAGE_CACHE_SIZE;
3255 }
3256}
3257
Neil Hormane9e3d722011-03-04 19:26:03 -05003258static int buf_to_pages_noslab(const void *buf, size_t buflen,
3259 struct page **pages, unsigned int *pgbase)
3260{
3261 struct page *newpage, **spages;
3262 int rc = 0;
3263 size_t len;
3264 spages = pages;
3265
3266 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003267 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003268 newpage = alloc_page(GFP_KERNEL);
3269
3270 if (newpage == NULL)
3271 goto unwind;
3272 memcpy(page_address(newpage), buf, len);
3273 buf += len;
3274 buflen -= len;
3275 *pages++ = newpage;
3276 rc++;
3277 } while (buflen != 0);
3278
3279 return rc;
3280
3281unwind:
3282 for(; rc > 0; rc--)
3283 __free_page(spages[rc-1]);
3284 return -ENOMEM;
3285}
3286
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003287struct nfs4_cached_acl {
3288 int cached;
3289 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003290 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003291};
3292
3293static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003294{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003295 struct nfs_inode *nfsi = NFS_I(inode);
3296
3297 spin_lock(&inode->i_lock);
3298 kfree(nfsi->nfs4_acl);
3299 nfsi->nfs4_acl = acl;
3300 spin_unlock(&inode->i_lock);
3301}
3302
3303static void nfs4_zap_acl_attr(struct inode *inode)
3304{
3305 nfs4_set_cached_acl(inode, NULL);
3306}
3307
3308static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3309{
3310 struct nfs_inode *nfsi = NFS_I(inode);
3311 struct nfs4_cached_acl *acl;
3312 int ret = -ENOENT;
3313
3314 spin_lock(&inode->i_lock);
3315 acl = nfsi->nfs4_acl;
3316 if (acl == NULL)
3317 goto out;
3318 if (buf == NULL) /* user is just asking for length */
3319 goto out_len;
3320 if (acl->cached == 0)
3321 goto out;
3322 ret = -ERANGE; /* see getxattr(2) man page */
3323 if (acl->len > buflen)
3324 goto out;
3325 memcpy(buf, acl->data, acl->len);
3326out_len:
3327 ret = acl->len;
3328out:
3329 spin_unlock(&inode->i_lock);
3330 return ret;
3331}
3332
3333static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3334{
3335 struct nfs4_cached_acl *acl;
3336
3337 if (buf && acl_len <= PAGE_SIZE) {
3338 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3339 if (acl == NULL)
3340 goto out;
3341 acl->cached = 1;
3342 memcpy(acl->data, buf, acl_len);
3343 } else {
3344 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3345 if (acl == NULL)
3346 goto out;
3347 acl->cached = 0;
3348 }
3349 acl->len = acl_len;
3350out:
3351 nfs4_set_cached_acl(inode, acl);
3352}
3353
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003354static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003355{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003356 struct page *pages[NFS4ACL_MAXPAGES];
3357 struct nfs_getaclargs args = {
3358 .fh = NFS_FH(inode),
3359 .acl_pages = pages,
3360 .acl_len = buflen,
3361 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003362 struct nfs_getaclres res = {
3363 .acl_len = buflen,
3364 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003365 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003366 struct rpc_message msg = {
3367 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3368 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003369 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003370 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003371 struct page *localpage = NULL;
3372 int ret;
3373
3374 if (buflen < PAGE_SIZE) {
3375 /* As long as we're doing a round trip to the server anyway,
3376 * let's be prepared for a page of acl data. */
3377 localpage = alloc_page(GFP_KERNEL);
3378 resp_buf = page_address(localpage);
3379 if (localpage == NULL)
3380 return -ENOMEM;
3381 args.acl_pages[0] = localpage;
3382 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003383 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003384 } else {
3385 resp_buf = buf;
3386 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3387 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003388 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003389 if (ret)
3390 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003391 if (res.acl_len > args.acl_len)
3392 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003393 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003394 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003395 if (buf) {
3396 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003397 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003398 goto out_free;
3399 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003400 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003401 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003402 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003403out_free:
3404 if (localpage)
3405 __free_page(localpage);
3406 return ret;
3407}
3408
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003409static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3410{
3411 struct nfs4_exception exception = { };
3412 ssize_t ret;
3413 do {
3414 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3415 if (ret >= 0)
3416 break;
3417 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3418 } while (exception.retry);
3419 return ret;
3420}
3421
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003422static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3423{
3424 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003425 int ret;
3426
3427 if (!nfs4_server_supports_acls(server))
3428 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003429 ret = nfs_revalidate_inode(server, inode);
3430 if (ret < 0)
3431 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003432 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3433 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003434 ret = nfs4_read_cached_acl(inode, buf, buflen);
3435 if (ret != -ENOENT)
3436 return ret;
3437 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003438}
3439
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003440static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003441{
3442 struct nfs_server *server = NFS_SERVER(inode);
3443 struct page *pages[NFS4ACL_MAXPAGES];
3444 struct nfs_setaclargs arg = {
3445 .fh = NFS_FH(inode),
3446 .acl_pages = pages,
3447 .acl_len = buflen,
3448 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003449 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003450 struct rpc_message msg = {
3451 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3452 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003453 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003454 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003455 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003456
3457 if (!nfs4_server_supports_acls(server))
3458 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003459 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3460 if (i < 0)
3461 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003462 nfs_inode_return_delegation(inode);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003463 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003464
3465 /*
3466 * Free each page after tx, so the only ref left is
3467 * held by the network stack
3468 */
3469 for (; i > 0; i--)
3470 put_page(pages[i-1]);
3471
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003472 /*
3473 * Acl update can result in inode attribute update.
3474 * so mark the attribute cache invalid.
3475 */
3476 spin_lock(&inode->i_lock);
3477 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3478 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003479 nfs_access_zap_cache(inode);
3480 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003481 return ret;
3482}
3483
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003484static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3485{
3486 struct nfs4_exception exception = { };
3487 int err;
3488 do {
3489 err = nfs4_handle_exception(NFS_SERVER(inode),
3490 __nfs4_proc_set_acl(inode, buf, buflen),
3491 &exception);
3492 } while (exception.retry);
3493 return err;
3494}
3495
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003497nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003499 struct nfs_client *clp = server->nfs_client;
3500
3501 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 return 0;
3503 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003504 case -NFS4ERR_ADMIN_REVOKED:
3505 case -NFS4ERR_BAD_STATEID:
3506 case -NFS4ERR_OPENMODE:
3507 if (state == NULL)
3508 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003509 nfs4_schedule_stateid_recovery(server, state);
3510 goto wait_on_recovery;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003512 case -NFS4ERR_STALE_CLIENTID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 case -NFS4ERR_EXPIRED:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003514 nfs4_schedule_lease_recovery(clp);
3515 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003516#if defined(CONFIG_NFS_V4_1)
3517 case -NFS4ERR_BADSESSION:
3518 case -NFS4ERR_BADSLOT:
3519 case -NFS4ERR_BAD_HIGH_SLOT:
3520 case -NFS4ERR_DEADSESSION:
3521 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3522 case -NFS4ERR_SEQ_FALSE_RETRY:
3523 case -NFS4ERR_SEQ_MISORDERED:
3524 dprintk("%s ERROR %d, Reset session\n", __func__,
3525 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003526 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -04003527 task->tk_status = 0;
3528 return -EAGAIN;
3529#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003531 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003532 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003533 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3535 task->tk_status = 0;
3536 return -EAGAIN;
3537 case -NFS4ERR_OLD_STATEID:
3538 task->tk_status = 0;
3539 return -EAGAIN;
3540 }
3541 task->tk_status = nfs4_map_errors(task->tk_status);
3542 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003543wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003544 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003545 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3546 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3547 task->tk_status = 0;
3548 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549}
3550
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003551int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3552 unsigned short port, struct rpc_cred *cred,
3553 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554{
3555 nfs4_verifier sc_verifier;
3556 struct nfs4_setclientid setclientid = {
3557 .sc_verifier = &sc_verifier,
3558 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003559 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 };
3561 struct rpc_message msg = {
3562 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3563 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003564 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003565 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 };
Al Virobc4785c2006-10-19 23:28:51 -07003567 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 int loop = 0;
3569 int status;
3570
Al Virobc4785c2006-10-19 23:28:51 -07003571 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3573 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3574
3575 for(;;) {
3576 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003577 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003578 clp->cl_ipaddr,
3579 rpc_peeraddr2str(clp->cl_rpcclient,
3580 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003581 rpc_peeraddr2str(clp->cl_rpcclient,
3582 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003583 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 clp->cl_id_uniquifier);
3585 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003586 sizeof(setclientid.sc_netid),
3587 rpc_peeraddr2str(clp->cl_rpcclient,
3588 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003590 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 clp->cl_ipaddr, port >> 8, port & 255);
3592
3593 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3594 if (status != -NFS4ERR_CLID_INUSE)
3595 break;
3596 if (signalled())
3597 break;
3598 if (loop++ & 1)
J. Bruce Fields611c96c2010-12-13 19:05:46 -05003599 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 else
3601 if (++clp->cl_id_uniquifier == 0)
3602 break;
3603 }
3604 return status;
3605}
3606
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003607static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3608 struct nfs4_setclientid_res *arg,
3609 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610{
3611 struct nfs_fsinfo fsinfo;
3612 struct rpc_message msg = {
3613 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003614 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003616 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 };
3618 unsigned long now;
3619 int status;
3620
3621 now = jiffies;
3622 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3623 if (status == 0) {
3624 spin_lock(&clp->cl_lock);
3625 clp->cl_lease_time = fsinfo.lease_time * HZ;
3626 clp->cl_last_renewal = now;
3627 spin_unlock(&clp->cl_lock);
3628 }
3629 return status;
3630}
3631
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003632int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3633 struct nfs4_setclientid_res *arg,
3634 struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003635{
Benny Halevy77e03672008-06-24 20:25:57 +03003636 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003637 int err;
3638 do {
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003639 err = _nfs4_proc_setclientid_confirm(clp, arg, cred);
Trond Myklebust51581f32006-03-20 13:44:47 -05003640 switch (err) {
3641 case 0:
3642 return err;
3643 case -NFS4ERR_RESOURCE:
3644 /* The IBM lawyers misread another document! */
3645 case -NFS4ERR_DELAY:
3646 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3647 }
3648 } while (err == 0);
3649 return err;
3650}
3651
Trond Myklebustfe650402006-01-03 09:55:18 +01003652struct nfs4_delegreturndata {
3653 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003654 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003655 struct nfs_fh fh;
3656 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003657 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003658 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003659 int rpc_status;
3660};
3661
Trond Myklebustfe650402006-01-03 09:55:18 +01003662static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3663{
3664 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003665
Trond Myklebust14516c32010-07-31 14:29:06 -04003666 if (!nfs4_sequence_done(task, &data->res.seq_res))
3667 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003668
Ricardo Labiaga79708862009-12-07 09:23:21 -05003669 switch (task->tk_status) {
3670 case -NFS4ERR_STALE_STATEID:
3671 case -NFS4ERR_EXPIRED:
3672 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003673 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003674 break;
3675 default:
3676 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3677 -EAGAIN) {
3678 nfs_restart_rpc(task, data->res.server->nfs_client);
3679 return;
3680 }
3681 }
3682 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003683}
3684
3685static void nfs4_delegreturn_release(void *calldata)
3686{
Trond Myklebustfe650402006-01-03 09:55:18 +01003687 kfree(calldata);
3688}
3689
Andy Adamson938e1012009-04-01 09:22:28 -04003690#if defined(CONFIG_NFS_V4_1)
3691static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3692{
3693 struct nfs4_delegreturndata *d_data;
3694
3695 d_data = (struct nfs4_delegreturndata *)data;
3696
Trond Myklebust035168a2010-06-16 09:52:26 -04003697 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003698 &d_data->args.seq_args,
3699 &d_data->res.seq_res, 1, task))
3700 return;
3701 rpc_call_start(task);
3702}
3703#endif /* CONFIG_NFS_V4_1 */
3704
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003705static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003706#if defined(CONFIG_NFS_V4_1)
3707 .rpc_call_prepare = nfs4_delegreturn_prepare,
3708#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003709 .rpc_call_done = nfs4_delegreturn_done,
3710 .rpc_release = nfs4_delegreturn_release,
3711};
3712
Trond Myklebuste6f81072008-01-24 18:14:34 -05003713static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003714{
3715 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003716 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003717 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003718 struct rpc_message msg = {
3719 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3720 .rpc_cred = cred,
3721 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003722 struct rpc_task_setup task_setup_data = {
3723 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003724 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003725 .callback_ops = &nfs4_delegreturn_ops,
3726 .flags = RPC_TASK_ASYNC,
3727 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003728 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003729
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003730 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003731 if (data == NULL)
3732 return -ENOMEM;
3733 data->args.fhandle = &data->fh;
3734 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003735 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003736 nfs_copy_fh(&data->fh, NFS_FH(inode));
3737 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003738 data->res.fattr = &data->fattr;
3739 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003740 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003741 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003742 data->rpc_status = 0;
3743
Trond Myklebustc970aa82007-07-14 15:39:59 -04003744 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05003745 msg.rpc_argp = &data->args;
3746 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003747 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003748 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003749 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003750 if (!issync)
3751 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003752 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003753 if (status != 0)
3754 goto out;
3755 status = data->rpc_status;
3756 if (status != 0)
3757 goto out;
3758 nfs_refresh_inode(inode, &data->fattr);
3759out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003760 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003761 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762}
3763
Trond Myklebuste6f81072008-01-24 18:14:34 -05003764int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765{
3766 struct nfs_server *server = NFS_SERVER(inode);
3767 struct nfs4_exception exception = { };
3768 int err;
3769 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003770 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 switch (err) {
3772 case -NFS4ERR_STALE_STATEID:
3773 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 case 0:
3775 return 0;
3776 }
3777 err = nfs4_handle_exception(server, err, &exception);
3778 } while (exception.retry);
3779 return err;
3780}
3781
3782#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3783#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3784
3785/*
3786 * sleep, with exponential backoff, and retry the LOCK operation.
3787 */
3788static unsigned long
3789nfs4_set_lock_task_retry(unsigned long timeout)
3790{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003791 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 timeout <<= 1;
3793 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3794 return NFS4_LOCK_MAXTIMEOUT;
3795 return timeout;
3796}
3797
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3799{
3800 struct inode *inode = state->inode;
3801 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003802 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003803 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003805 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003807 struct nfs_lockt_res res = {
3808 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 };
3810 struct rpc_message msg = {
3811 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3812 .rpc_argp = &arg,
3813 .rpc_resp = &res,
3814 .rpc_cred = state->owner->so_cred,
3815 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 struct nfs4_lock_state *lsp;
3817 int status;
3818
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003819 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003820 status = nfs4_set_lock_state(state, request);
3821 if (status != 0)
3822 goto out;
3823 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003824 arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05003825 arg.lock_owner.s_dev = server->s_dev;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003826 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003827 switch (status) {
3828 case 0:
3829 request->fl_type = F_UNLCK;
3830 break;
3831 case -NFS4ERR_DENIED:
3832 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003834 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003835out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 return status;
3837}
3838
3839static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3840{
3841 struct nfs4_exception exception = { };
3842 int err;
3843
3844 do {
3845 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3846 _nfs4_proc_getlk(state, cmd, request),
3847 &exception);
3848 } while (exception.retry);
3849 return err;
3850}
3851
3852static int do_vfs_lock(struct file *file, struct file_lock *fl)
3853{
3854 int res = 0;
3855 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3856 case FL_POSIX:
3857 res = posix_lock_file_wait(file, fl);
3858 break;
3859 case FL_FLOCK:
3860 res = flock_lock_file_wait(file, fl);
3861 break;
3862 default:
3863 BUG();
3864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 return res;
3866}
3867
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003868struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003869 struct nfs_locku_args arg;
3870 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003871 struct nfs4_lock_state *lsp;
3872 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003873 struct file_lock fl;
3874 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003875 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003876};
3877
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003878static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3879 struct nfs_open_context *ctx,
3880 struct nfs4_lock_state *lsp,
3881 struct nfs_seqid *seqid)
3882{
3883 struct nfs4_unlockdata *p;
3884 struct inode *inode = lsp->ls_state->inode;
3885
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003886 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003887 if (p == NULL)
3888 return NULL;
3889 p->arg.fh = NFS_FH(inode);
3890 p->arg.fl = &p->fl;
3891 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003892 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003893 p->arg.stateid = &lsp->ls_stateid;
3894 p->lsp = lsp;
3895 atomic_inc(&lsp->ls_count);
3896 /* Ensure we don't close file until we're done freeing locks! */
3897 p->ctx = get_nfs_open_context(ctx);
3898 memcpy(&p->fl, fl, sizeof(p->fl));
3899 p->server = NFS_SERVER(inode);
3900 return p;
3901}
3902
Trond Myklebust06f814a2006-01-03 09:55:07 +01003903static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003904{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003905 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003906 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003907 nfs4_put_lock_state(calldata->lsp);
3908 put_nfs_open_context(calldata->ctx);
3909 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003910}
3911
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003912static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003913{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003914 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003915
Trond Myklebust14516c32010-07-31 14:29:06 -04003916 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3917 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003918 switch (task->tk_status) {
3919 case 0:
3920 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003921 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003922 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003923 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003924 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003925 case -NFS4ERR_BAD_STATEID:
3926 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003927 case -NFS4ERR_STALE_STATEID:
3928 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003929 break;
3930 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003931 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebust0110ee12009-12-07 09:00:24 -05003932 nfs_restart_rpc(task,
Trond Myklebustd61e6122009-12-05 19:32:19 -05003933 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003934 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003935}
3936
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003937static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003938{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003939 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003941 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003942 return;
3943 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003944 /* Note: exit _without_ running nfs4_locku_done */
3945 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003946 return;
3947 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003948 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04003949 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04003950 &calldata->arg.seq_args,
3951 &calldata->res.seq_res, 1, task))
3952 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003953 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954}
3955
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003956static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003957 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003958 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003959 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003960};
3961
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003962static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3963 struct nfs_open_context *ctx,
3964 struct nfs4_lock_state *lsp,
3965 struct nfs_seqid *seqid)
3966{
3967 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003968 struct rpc_message msg = {
3969 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3970 .rpc_cred = ctx->cred,
3971 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003972 struct rpc_task_setup task_setup_data = {
3973 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003974 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003975 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003976 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003977 .flags = RPC_TASK_ASYNC,
3978 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003979
Frank Filz137d6ac2007-07-09 15:32:29 -07003980 /* Ensure this is an unlock - when canceling a lock, the
3981 * canceled lock is passed in, and it won't be an unlock.
3982 */
3983 fl->fl_type = F_UNLCK;
3984
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003985 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3986 if (data == NULL) {
3987 nfs_free_seqid(seqid);
3988 return ERR_PTR(-ENOMEM);
3989 }
3990
Trond Myklebust1174dd12010-12-21 10:52:24 -05003991 msg.rpc_argp = &data->arg;
3992 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003993 task_setup_data.callback_data = data;
3994 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003995}
3996
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3998{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003999 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004000 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004001 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004002 struct rpc_task *task;
4003 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004004 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005
Trond Myklebust9b073572006-06-29 16:38:34 -04004006 status = nfs4_set_lock_state(state, request);
4007 /* Unlock _before_ we do the RPC call */
4008 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004009 down_read(&nfsi->rwsem);
4010 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4011 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004012 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004013 }
4014 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004015 if (status != 0)
4016 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004017 /* Is this a delegated lock? */
4018 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004019 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004020 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004021 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004022 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004023 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004024 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004025 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004026 status = PTR_ERR(task);
4027 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004028 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004029 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004030 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004031out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004032 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004033 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034}
4035
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004036struct nfs4_lockdata {
4037 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004038 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004039 struct nfs4_lock_state *lsp;
4040 struct nfs_open_context *ctx;
4041 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004042 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004043 int rpc_status;
4044 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004045 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004046};
4047
4048static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004049 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4050 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004051{
4052 struct nfs4_lockdata *p;
4053 struct inode *inode = lsp->ls_state->inode;
4054 struct nfs_server *server = NFS_SERVER(inode);
4055
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004056 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004057 if (p == NULL)
4058 return NULL;
4059
4060 p->arg.fh = NFS_FH(inode);
4061 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004062 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004063 if (p->arg.open_seqid == NULL)
4064 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004065 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004066 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004067 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004068 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004069 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004070 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004071 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004072 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004073 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004074 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004075 atomic_inc(&lsp->ls_count);
4076 p->ctx = get_nfs_open_context(ctx);
4077 memcpy(&p->fl, fl, sizeof(p->fl));
4078 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004079out_free_seqid:
4080 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004081out_free:
4082 kfree(p);
4083 return NULL;
4084}
4085
4086static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4087{
4088 struct nfs4_lockdata *data = calldata;
4089 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090
Harvey Harrison3110ff82008-05-02 13:42:44 -07004091 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004092 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4093 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004094 /* Do we need to do an open_to_lock_owner? */
4095 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004096 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4097 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004098 data->arg.open_stateid = &state->stateid;
4099 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004100 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004101 } else
4102 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004103 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004104 if (nfs4_setup_sequence(data->server,
4105 &data->arg.seq_args,
Andy Adamson66179ef2009-04-01 09:22:22 -04004106 &data->res.seq_res, 1, task))
4107 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004108 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004109 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004110}
4111
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004112static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4113{
4114 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4115 nfs4_lock_prepare(task, calldata);
4116}
4117
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004118static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4119{
4120 struct nfs4_lockdata *data = calldata;
4121
Harvey Harrison3110ff82008-05-02 13:42:44 -07004122 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004123
Trond Myklebust14516c32010-07-31 14:29:06 -04004124 if (!nfs4_sequence_done(task, &data->res.seq_res))
4125 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004126
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004127 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004128 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004129 if (data->rpc_status == 0)
4130 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4131 else
4132 goto out;
4133 }
4134 if (data->rpc_status == 0) {
4135 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4136 sizeof(data->lsp->ls_stateid.data));
4137 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04004138 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004139 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004140out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004141 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004142}
4143
4144static void nfs4_lock_release(void *calldata)
4145{
4146 struct nfs4_lockdata *data = calldata;
4147
Harvey Harrison3110ff82008-05-02 13:42:44 -07004148 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004149 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004150 if (data->cancelled != 0) {
4151 struct rpc_task *task;
4152 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4153 data->arg.lock_seqid);
4154 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004155 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004156 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004157 } else
4158 nfs_free_seqid(data->arg.lock_seqid);
4159 nfs4_put_lock_state(data->lsp);
4160 put_nfs_open_context(data->ctx);
4161 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004162 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004163}
4164
4165static const struct rpc_call_ops nfs4_lock_ops = {
4166 .rpc_call_prepare = nfs4_lock_prepare,
4167 .rpc_call_done = nfs4_lock_done,
4168 .rpc_release = nfs4_lock_release,
4169};
4170
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004171static const struct rpc_call_ops nfs4_recover_lock_ops = {
4172 .rpc_call_prepare = nfs4_recover_lock_prepare,
4173 .rpc_call_done = nfs4_lock_done,
4174 .rpc_release = nfs4_lock_release,
4175};
4176
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004177static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4178{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004179 switch (error) {
4180 case -NFS4ERR_ADMIN_REVOKED:
4181 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004182 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004183 if (new_lock_owner != 0 ||
4184 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004185 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004186 break;
4187 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004188 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004189 case -NFS4ERR_EXPIRED:
4190 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004191 };
4192}
4193
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004194static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004195{
4196 struct nfs4_lockdata *data;
4197 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004198 struct rpc_message msg = {
4199 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4200 .rpc_cred = state->owner->so_cred,
4201 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004202 struct rpc_task_setup task_setup_data = {
4203 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004204 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004205 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004206 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004207 .flags = RPC_TASK_ASYNC,
4208 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004209 int ret;
4210
Harvey Harrison3110ff82008-05-02 13:42:44 -07004211 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004212 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004213 fl->fl_u.nfs4_fl.owner,
4214 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004215 if (data == NULL)
4216 return -ENOMEM;
4217 if (IS_SETLKW(cmd))
4218 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004219 if (recovery_type > NFS_LOCK_NEW) {
4220 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004221 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004222 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4223 }
Trond Myklebust1174dd12010-12-21 10:52:24 -05004224 msg.rpc_argp = &data->arg;
4225 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004226 task_setup_data.callback_data = data;
4227 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004228 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004229 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004230 ret = nfs4_wait_for_completion_rpc_task(task);
4231 if (ret == 0) {
4232 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004233 if (ret)
4234 nfs4_handle_setlk_error(data->server, data->lsp,
4235 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004236 } else
4237 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004238 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004239 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004240 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241}
4242
4243static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4244{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004245 struct nfs_server *server = NFS_SERVER(state->inode);
4246 struct nfs4_exception exception = { };
4247 int err;
4248
4249 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004250 /* Cache the lock if possible... */
4251 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4252 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004253 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004254 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004255 break;
4256 nfs4_handle_exception(server, err, &exception);
4257 } while (exception.retry);
4258 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259}
4260
4261static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4262{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004263 struct nfs_server *server = NFS_SERVER(state->inode);
4264 struct nfs4_exception exception = { };
4265 int err;
4266
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004267 err = nfs4_set_lock_state(state, request);
4268 if (err != 0)
4269 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004270 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004271 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4272 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004273 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004274 switch (err) {
4275 default:
4276 goto out;
4277 case -NFS4ERR_GRACE:
4278 case -NFS4ERR_DELAY:
4279 nfs4_handle_exception(server, err, &exception);
4280 err = 0;
4281 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004282 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004283out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004284 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285}
4286
4287static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4288{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004289 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004290 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004291 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004293 if ((fl_flags & FL_POSIX) &&
4294 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4295 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004296 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004297 status = nfs4_set_lock_state(state, request);
4298 if (status != 0)
4299 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004300 request->fl_flags |= FL_ACCESS;
4301 status = do_vfs_lock(request->fl_file, request);
4302 if (status < 0)
4303 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004304 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004305 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004306 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004307 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004308 request->fl_flags = fl_flags & ~FL_SLEEP;
4309 status = do_vfs_lock(request->fl_file, request);
4310 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004311 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004312 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004313 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004314 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004315 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004316 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004317 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004318 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004319out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004320 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004321out:
4322 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 return status;
4324}
4325
4326static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4327{
4328 struct nfs4_exception exception = { };
4329 int err;
4330
4331 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004332 err = _nfs4_proc_setlk(state, cmd, request);
4333 if (err == -NFS4ERR_DENIED)
4334 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004336 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 } while (exception.retry);
4338 return err;
4339}
4340
4341static int
4342nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4343{
4344 struct nfs_open_context *ctx;
4345 struct nfs4_state *state;
4346 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4347 int status;
4348
4349 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004350 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 state = ctx->state;
4352
4353 if (request->fl_start < 0 || request->fl_end < 0)
4354 return -EINVAL;
4355
Trond Myklebustd9531262009-07-21 19:22:38 -04004356 if (IS_GETLK(cmd)) {
4357 if (state != NULL)
4358 return nfs4_proc_getlk(state, F_GETLK, request);
4359 return 0;
4360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361
4362 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4363 return -EINVAL;
4364
Trond Myklebustd9531262009-07-21 19:22:38 -04004365 if (request->fl_type == F_UNLCK) {
4366 if (state != NULL)
4367 return nfs4_proc_unlck(state, cmd, request);
4368 return 0;
4369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370
Trond Myklebustd9531262009-07-21 19:22:38 -04004371 if (state == NULL)
4372 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 do {
4374 status = nfs4_proc_setlk(state, cmd, request);
4375 if ((status != -EAGAIN) || IS_SETLK(cmd))
4376 break;
4377 timeout = nfs4_set_lock_task_retry(timeout);
4378 status = -ERESTARTSYS;
4379 if (signalled())
4380 break;
4381 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 return status;
4383}
4384
Trond Myklebust888e6942005-11-04 15:38:11 -05004385int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4386{
4387 struct nfs_server *server = NFS_SERVER(state->inode);
4388 struct nfs4_exception exception = { };
4389 int err;
4390
4391 err = nfs4_set_lock_state(state, fl);
4392 if (err != 0)
4393 goto out;
4394 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004395 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004396 switch (err) {
4397 default:
4398 printk(KERN_ERR "%s: unhandled error %d.\n",
4399 __func__, err);
4400 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004401 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004402 goto out;
4403 case -NFS4ERR_EXPIRED:
4404 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004405 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004406 nfs4_schedule_lease_recovery(server->nfs_client);
4407 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004408 case -NFS4ERR_BADSESSION:
4409 case -NFS4ERR_BADSLOT:
4410 case -NFS4ERR_BAD_HIGH_SLOT:
4411 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4412 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004413 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004414 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004415 case -ERESTARTSYS:
4416 /*
4417 * The show must go on: exit, but mark the
4418 * stateid as needing recovery.
4419 */
4420 case -NFS4ERR_ADMIN_REVOKED:
4421 case -NFS4ERR_BAD_STATEID:
4422 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004423 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004424 err = 0;
4425 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004426 case -EKEYEXPIRED:
4427 /*
4428 * User RPCSEC_GSS context has expired.
4429 * We cannot recover this stateid now, so
4430 * skip it and allow recovery thread to
4431 * proceed.
4432 */
4433 err = 0;
4434 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004435 case -ENOMEM:
4436 case -NFS4ERR_DENIED:
4437 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4438 err = 0;
4439 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004440 case -NFS4ERR_DELAY:
4441 break;
4442 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004443 err = nfs4_handle_exception(server, err, &exception);
4444 } while (exception.retry);
4445out:
4446 return err;
4447}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004448
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004449static void nfs4_release_lockowner_release(void *calldata)
4450{
4451 kfree(calldata);
4452}
4453
4454const struct rpc_call_ops nfs4_release_lockowner_ops = {
4455 .rpc_release = nfs4_release_lockowner_release,
4456};
4457
4458void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4459{
4460 struct nfs_server *server = lsp->ls_state->owner->so_server;
4461 struct nfs_release_lockowner_args *args;
4462 struct rpc_message msg = {
4463 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4464 };
4465
4466 if (server->nfs_client->cl_mvops->minor_version != 0)
4467 return;
4468 args = kmalloc(sizeof(*args), GFP_NOFS);
4469 if (!args)
4470 return;
4471 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4472 args->lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004473 args->lock_owner.s_dev = server->s_dev;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004474 msg.rpc_argp = args;
4475 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4476}
4477
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004478#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4479
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004480static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4481 const void *buf, size_t buflen,
4482 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004483{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004484 if (strcmp(key, "") != 0)
4485 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004486
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004487 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004488}
4489
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004490static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4491 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004492{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004493 if (strcmp(key, "") != 0)
4494 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004495
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004496 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004497}
4498
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004499static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4500 size_t list_len, const char *name,
4501 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004502{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004503 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004504
J. Bruce Fields096455a2006-03-20 23:23:42 -05004505 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4506 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004507
4508 if (list && len <= list_len)
4509 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004510 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004511}
4512
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004513static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4514{
4515 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4516 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4517 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4518 return;
4519
4520 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4521 NFS_ATTR_FATTR_NLINK;
4522 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4523 fattr->nlink = 2;
4524}
4525
Trond Myklebust56659e92007-07-17 21:52:39 -04004526int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004527 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004528{
4529 struct nfs_server *server = NFS_SERVER(dir);
4530 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004531 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4532 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004533 };
4534 struct nfs4_fs_locations_arg args = {
4535 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004536 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004537 .page = page,
4538 .bitmask = bitmask,
4539 };
Benny Halevy22958462009-04-01 09:22:02 -04004540 struct nfs4_fs_locations_res res = {
4541 .fs_locations = fs_locations,
4542 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004543 struct rpc_message msg = {
4544 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4545 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004546 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004547 };
4548 int status;
4549
Harvey Harrison3110ff82008-05-02 13:42:44 -07004550 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004551 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004552 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004553 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04004554 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004555 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004556 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004557 return status;
4558}
4559
Andy Adamson557134a2009-04-01 09:21:53 -04004560#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004561/*
Andy Adamson357f54d2010-12-14 10:11:57 -05004562 * Check the exchange flags returned by the server for invalid flags, having
4563 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4564 * DS flags set.
4565 */
4566static int nfs4_check_cl_exchange_flags(u32 flags)
4567{
4568 if (flags & ~EXCHGID4_FLAG_MASK_R)
4569 goto out_inval;
4570 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4571 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4572 goto out_inval;
4573 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4574 goto out_inval;
4575 return NFS_OK;
4576out_inval:
4577 return -NFS4ERR_INVAL;
4578}
4579
4580/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04004581 * nfs4_proc_exchange_id()
4582 *
4583 * Since the clientid has expired, all compounds using sessions
4584 * associated with the stale clientid will be returning
4585 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4586 * be in some phase of session reset.
4587 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004588int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004589{
4590 nfs4_verifier verifier;
4591 struct nfs41_exchange_id_args args = {
4592 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05004593 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04004594 };
4595 struct nfs41_exchange_id_res res = {
4596 .client = clp,
4597 };
4598 int status;
4599 struct rpc_message msg = {
4600 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4601 .rpc_argp = &args,
4602 .rpc_resp = &res,
4603 .rpc_cred = cred,
4604 };
4605 __be32 *p;
4606
4607 dprintk("--> %s\n", __func__);
4608 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004609
Benny Halevy99fe60d2009-04-01 09:22:29 -04004610 p = (u32 *)verifier.data;
4611 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4612 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4613 args.verifier = &verifier;
4614
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004615 args.id_len = scnprintf(args.id, sizeof(args.id),
4616 "%s/%s.%s/%u",
4617 clp->cl_ipaddr,
4618 init_utsname()->nodename,
4619 init_utsname()->domainname,
4620 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004621
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004622 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4623 if (!status)
4624 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004625 dprintk("<-- %s status= %d\n", __func__, status);
4626 return status;
4627}
4628
Andy Adamson2050f0c2009-04-01 09:22:30 -04004629struct nfs4_get_lease_time_data {
4630 struct nfs4_get_lease_time_args *args;
4631 struct nfs4_get_lease_time_res *res;
4632 struct nfs_client *clp;
4633};
4634
4635static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4636 void *calldata)
4637{
4638 int ret;
4639 struct nfs4_get_lease_time_data *data =
4640 (struct nfs4_get_lease_time_data *)calldata;
4641
4642 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004643 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004644 /* just setup sequence, do not trigger session recovery
4645 since we're invoked within one */
4646 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004647 &data->args->la_seq_args,
4648 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004649
4650 BUG_ON(ret == -EAGAIN);
4651 rpc_call_start(task);
4652 dprintk("<-- %s\n", __func__);
4653}
4654
4655/*
4656 * Called from nfs4_state_manager thread for session setup, so don't recover
4657 * from sequence operation or clientid errors.
4658 */
4659static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4660{
4661 struct nfs4_get_lease_time_data *data =
4662 (struct nfs4_get_lease_time_data *)calldata;
4663
4664 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04004665 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4666 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04004667 switch (task->tk_status) {
4668 case -NFS4ERR_DELAY:
4669 case -NFS4ERR_GRACE:
4670 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4671 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4672 task->tk_status = 0;
Trond Myklebust0110ee12009-12-07 09:00:24 -05004673 nfs_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004674 return;
4675 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004676 dprintk("<-- %s\n", __func__);
4677}
4678
4679struct rpc_call_ops nfs4_get_lease_time_ops = {
4680 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4681 .rpc_call_done = nfs4_get_lease_time_done,
4682};
4683
4684int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4685{
4686 struct rpc_task *task;
4687 struct nfs4_get_lease_time_args args;
4688 struct nfs4_get_lease_time_res res = {
4689 .lr_fsinfo = fsinfo,
4690 };
4691 struct nfs4_get_lease_time_data data = {
4692 .args = &args,
4693 .res = &res,
4694 .clp = clp,
4695 };
4696 struct rpc_message msg = {
4697 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4698 .rpc_argp = &args,
4699 .rpc_resp = &res,
4700 };
4701 struct rpc_task_setup task_setup = {
4702 .rpc_client = clp->cl_rpcclient,
4703 .rpc_message = &msg,
4704 .callback_ops = &nfs4_get_lease_time_ops,
4705 .callback_data = &data
4706 };
4707 int status;
4708
Andy Adamson2050f0c2009-04-01 09:22:30 -04004709 dprintk("--> %s\n", __func__);
4710 task = rpc_run_task(&task_setup);
4711
4712 if (IS_ERR(task))
4713 status = PTR_ERR(task);
4714 else {
4715 status = task->tk_status;
4716 rpc_put_task(task);
4717 }
4718 dprintk("<-- %s return %d\n", __func__, status);
4719
4720 return status;
4721}
4722
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004723/*
4724 * Reset a slot table
4725 */
Andy Adamson104aeba2010-01-14 17:45:10 -05004726static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4727 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004728{
Andy Adamson104aeba2010-01-14 17:45:10 -05004729 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004730 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004731 int ret = 0;
4732
Andy Adamson104aeba2010-01-14 17:45:10 -05004733 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4734 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004735
Andy Adamson104aeba2010-01-14 17:45:10 -05004736 /* Does the newly negotiated max_reqs match the existing slot table? */
4737 if (max_reqs != tbl->max_slots) {
4738 ret = -ENOMEM;
4739 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004740 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05004741 if (!new)
4742 goto out;
4743 ret = 0;
4744 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004745 }
4746 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05004747 if (new) {
4748 tbl->slots = new;
4749 tbl->max_slots = max_reqs;
4750 }
4751 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004752 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004753 spin_unlock(&tbl->slot_tbl_lock);
4754 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4755 tbl, tbl->slots, tbl->max_slots);
4756out:
4757 dprintk("<-- %s: return %d\n", __func__, ret);
4758 return ret;
4759}
4760
Andy Adamsonfc931582009-04-01 09:22:31 -04004761/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004762 * Reset the forechannel and backchannel slot tables
4763 */
4764static int nfs4_reset_slot_tables(struct nfs4_session *session)
4765{
4766 int status;
4767
4768 status = nfs4_reset_slot_table(&session->fc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004769 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004770 if (status)
4771 return status;
4772
4773 status = nfs4_reset_slot_table(&session->bc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004774 session->bc_attrs.max_reqs, 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004775 return status;
4776}
4777
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004778/* Destroy the slot table */
4779static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4780{
4781 if (session->fc_slot_table.slots != NULL) {
4782 kfree(session->fc_slot_table.slots);
4783 session->fc_slot_table.slots = NULL;
4784 }
4785 if (session->bc_slot_table.slots != NULL) {
4786 kfree(session->bc_slot_table.slots);
4787 session->bc_slot_table.slots = NULL;
4788 }
4789 return;
4790}
4791
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004792/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004793 * Initialize slot table
4794 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004795static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4796 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04004797{
Andy Adamsonfc931582009-04-01 09:22:31 -04004798 struct nfs4_slot *slot;
4799 int ret = -ENOMEM;
4800
4801 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4802
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004803 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04004804
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004805 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04004806 if (!slot)
4807 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04004808 ret = 0;
4809
4810 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04004811 tbl->max_slots = max_slots;
4812 tbl->slots = slot;
4813 tbl->highest_used_slotid = -1; /* no slot is currently used */
4814 spin_unlock(&tbl->slot_tbl_lock);
4815 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4816 tbl, tbl->slots, tbl->max_slots);
4817out:
4818 dprintk("<-- %s: return %d\n", __func__, ret);
4819 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04004820}
4821
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004822/*
4823 * Initialize the forechannel and backchannel tables
4824 */
4825static int nfs4_init_slot_tables(struct nfs4_session *session)
4826{
Trond Myklebustf26468f2009-12-05 19:32:11 -05004827 struct nfs4_slot_table *tbl;
4828 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004829
Trond Myklebustf26468f2009-12-05 19:32:11 -05004830 tbl = &session->fc_slot_table;
4831 if (tbl->slots == NULL) {
4832 status = nfs4_init_slot_table(tbl,
4833 session->fc_attrs.max_reqs, 1);
4834 if (status)
4835 return status;
4836 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004837
Trond Myklebustf26468f2009-12-05 19:32:11 -05004838 tbl = &session->bc_slot_table;
4839 if (tbl->slots == NULL) {
4840 status = nfs4_init_slot_table(tbl,
4841 session->bc_attrs.max_reqs, 0);
4842 if (status)
4843 nfs4_destroy_slot_tables(session);
4844 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004845
4846 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04004847}
4848
4849struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4850{
4851 struct nfs4_session *session;
4852 struct nfs4_slot_table *tbl;
4853
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004854 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04004855 if (!session)
4856 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04004857
Andy Adamson557134a2009-04-01 09:21:53 -04004858 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004859 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04004860 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004861 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00004862 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004863
4864 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004865 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004866 spin_lock_init(&tbl->slot_tbl_lock);
4867 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00004868 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004869
Trond Myklebust1055d762010-06-16 09:52:27 -04004870 session->session_state = 1<<NFS4_SESSION_INITING;
4871
Andy Adamson557134a2009-04-01 09:21:53 -04004872 session->clp = clp;
4873 return session;
4874}
4875
4876void nfs4_destroy_session(struct nfs4_session *session)
4877{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04004878 nfs4_proc_destroy_session(session);
4879 dprintk("%s Destroy backchannel for xprt %p\n",
4880 __func__, session->clp->cl_rpcclient->cl_xprt);
4881 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4882 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004883 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04004884 kfree(session);
4885}
4886
Andy Adamsonfc931582009-04-01 09:22:31 -04004887/*
4888 * Initialize the values to be used by the client in CREATE_SESSION
4889 * If nfs4_init_session set the fore channel request and response sizes,
4890 * use them.
4891 *
4892 * Set the back channel max_resp_sz_cached to zero to force the client to
4893 * always set csa_cachethis to FALSE because the current implementation
4894 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4895 */
4896static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4897{
4898 struct nfs4_session *session = args->client->cl_session;
4899 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4900 mxresp_sz = session->fc_attrs.max_resp_sz;
4901
4902 if (mxrqst_sz == 0)
4903 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4904 if (mxresp_sz == 0)
4905 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4906 /* Fore channel attributes */
4907 args->fc_attrs.headerpadsz = 0;
4908 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4909 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04004910 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4911 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4912
4913 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05004914 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04004915 __func__,
4916 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05004917 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04004918
4919 /* Back channel attributes */
4920 args->bc_attrs.headerpadsz = 0;
4921 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4922 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4923 args->bc_attrs.max_resp_sz_cached = 0;
4924 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4925 args->bc_attrs.max_reqs = 1;
4926
4927 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4928 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4929 __func__,
4930 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4931 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4932 args->bc_attrs.max_reqs);
4933}
4934
J. Bruce Fields43c2e882010-10-02 15:19:01 -04004935static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04004936{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04004937 struct nfs4_channel_attrs *sent = &args->fc_attrs;
4938 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
4939
4940 if (rcvd->headerpadsz > sent->headerpadsz)
4941 return -EINVAL;
4942 if (rcvd->max_resp_sz > sent->max_resp_sz)
4943 return -EINVAL;
4944 /*
4945 * Our requested max_ops is the minimum we need; we're not
4946 * prepared to break up compounds into smaller pieces than that.
4947 * So, no point even trying to continue if the server won't
4948 * cooperate:
4949 */
4950 if (rcvd->max_ops < sent->max_ops)
4951 return -EINVAL;
4952 if (rcvd->max_reqs == 0)
4953 return -EINVAL;
4954 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04004955}
4956
J. Bruce Fields43c2e882010-10-02 15:19:01 -04004957static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
4958{
4959 struct nfs4_channel_attrs *sent = &args->bc_attrs;
4960 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04004961
J. Bruce Fields43c2e882010-10-02 15:19:01 -04004962 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
4963 return -EINVAL;
4964 if (rcvd->max_resp_sz < sent->max_resp_sz)
4965 return -EINVAL;
4966 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
4967 return -EINVAL;
4968 /* These would render the backchannel useless: */
4969 if (rcvd->max_ops == 0)
4970 return -EINVAL;
4971 if (rcvd->max_reqs == 0)
4972 return -EINVAL;
4973 return 0;
4974}
Andy Adamson8d353012009-04-01 09:22:32 -04004975
Andy Adamson8d353012009-04-01 09:22:32 -04004976static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4977 struct nfs4_session *session)
4978{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04004979 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04004980
J. Bruce Fields43c2e882010-10-02 15:19:01 -04004981 ret = nfs4_verify_fore_channel_attrs(args, session);
4982 if (ret)
4983 return ret;
4984 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04004985}
4986
Andy Adamsonfc931582009-04-01 09:22:31 -04004987static int _nfs4_proc_create_session(struct nfs_client *clp)
4988{
4989 struct nfs4_session *session = clp->cl_session;
4990 struct nfs41_create_session_args args = {
4991 .client = clp,
4992 .cb_program = NFS4_CALLBACK,
4993 };
4994 struct nfs41_create_session_res res = {
4995 .client = clp,
4996 };
4997 struct rpc_message msg = {
4998 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4999 .rpc_argp = &args,
5000 .rpc_resp = &res,
5001 };
5002 int status;
5003
5004 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005005 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005006
5007 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5008
Andy Adamson8d353012009-04-01 09:22:32 -04005009 if (!status)
5010 /* Verify the session's negotiated channel_attrs values */
5011 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005012 if (!status) {
5013 /* Increment the clientid slot sequence id */
5014 clp->cl_seqid++;
5015 }
5016
5017 return status;
5018}
5019
5020/*
5021 * Issues a CREATE_SESSION operation to the server.
5022 * It is the responsibility of the caller to verify the session is
5023 * expired before calling this routine.
5024 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005025int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005026{
5027 int status;
5028 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005029 struct nfs4_session *session = clp->cl_session;
Ricardo Labiaga7d6d63d2011-03-09 13:13:44 -05005030 long timeout = 0;
5031 int err;
Andy Adamsonfc931582009-04-01 09:22:31 -04005032
5033 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5034
Ricardo Labiaga7d6d63d2011-03-09 13:13:44 -05005035 do {
5036 status = _nfs4_proc_create_session(clp);
5037 if (status == -NFS4ERR_DELAY) {
5038 err = nfs4_delay(clp->cl_rpcclient, &timeout);
5039 if (err)
5040 status = err;
5041 }
5042 } while (status == -NFS4ERR_DELAY);
5043
Andy Adamsonfc931582009-04-01 09:22:31 -04005044 if (status)
5045 goto out;
5046
Trond Myklebustf26468f2009-12-05 19:32:11 -05005047 /* Init and reset the fore channel */
5048 status = nfs4_init_slot_tables(session);
5049 dprintk("slot table initialization returned %d\n", status);
5050 if (status)
5051 goto out;
5052 status = nfs4_reset_slot_tables(session);
5053 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005054 if (status)
5055 goto out;
5056
5057 ptr = (unsigned *)&session->sess_id.data[0];
5058 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5059 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005060out:
5061 dprintk("<-- %s\n", __func__);
5062 return status;
5063}
5064
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005065/*
5066 * Issue the over-the-wire RPC DESTROY_SESSION.
5067 * The caller must serialize access to this routine.
5068 */
5069int nfs4_proc_destroy_session(struct nfs4_session *session)
5070{
5071 int status = 0;
5072 struct rpc_message msg;
5073
5074 dprintk("--> nfs4_proc_destroy_session\n");
5075
5076 /* session is still being setup */
5077 if (session->clp->cl_cons_state != NFS_CS_READY)
5078 return status;
5079
5080 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5081 msg.rpc_argp = session;
5082 msg.rpc_resp = NULL;
5083 msg.rpc_cred = NULL;
5084 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5085
5086 if (status)
5087 printk(KERN_WARNING
5088 "Got error %d from the server on DESTROY_SESSION. "
5089 "Session has been destroyed regardless...\n", status);
5090
5091 dprintk("<-- nfs4_proc_destroy_session\n");
5092 return status;
5093}
5094
Trond Myklebustfccba802009-07-21 16:48:07 -04005095int nfs4_init_session(struct nfs_server *server)
5096{
5097 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005098 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005099 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005100 int ret;
5101
5102 if (!nfs4_has_session(clp))
5103 return 0;
5104
Trond Myklebust1055d762010-06-16 09:52:27 -04005105 session = clp->cl_session;
5106 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5107 return 0;
5108
Andy Adamson68bf05e2009-12-15 12:55:02 -05005109 rsize = server->rsize;
5110 if (rsize == 0)
5111 rsize = NFS_MAX_FILE_IO_SIZE;
5112 wsize = server->wsize;
5113 if (wsize == 0)
5114 wsize = NFS_MAX_FILE_IO_SIZE;
5115
Andy Adamson68bf05e2009-12-15 12:55:02 -05005116 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5117 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005118
Trond Myklebustfccba802009-07-21 16:48:07 -04005119 ret = nfs4_recover_expired_lease(server);
5120 if (!ret)
5121 ret = nfs4_check_client_ready(clp);
5122 return ret;
5123}
5124
Andy Adamsond83217c2011-03-01 01:34:17 +00005125int nfs4_init_ds_session(struct nfs_client *clp)
5126{
5127 struct nfs4_session *session = clp->cl_session;
5128 int ret;
5129
5130 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5131 return 0;
5132
5133 ret = nfs4_client_recover_expired_lease(clp);
5134 if (!ret)
5135 /* Test for the DS role */
5136 if (!is_ds_client(clp))
5137 ret = -ENODEV;
5138 if (!ret)
5139 ret = nfs4_check_client_ready(clp);
5140 return ret;
5141
5142}
5143EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5144
5145
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005146/*
5147 * Renew the cl_session lease.
5148 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005149struct nfs4_sequence_data {
5150 struct nfs_client *clp;
5151 struct nfs4_sequence_args args;
5152 struct nfs4_sequence_res res;
5153};
5154
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005155static void nfs41_sequence_release(void *data)
5156{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005157 struct nfs4_sequence_data *calldata = data;
5158 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005159
Alexandros Batsakis71358402010-02-05 03:45:05 -08005160 if (atomic_read(&clp->cl_count) > 1)
5161 nfs4_schedule_state_renewal(clp);
5162 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005163 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005164}
5165
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005166static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5167{
5168 switch(task->tk_status) {
5169 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005170 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5171 return -EAGAIN;
5172 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005173 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005174 }
5175 return 0;
5176}
5177
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005178static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005179{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005180 struct nfs4_sequence_data *calldata = data;
5181 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005182
Trond Myklebust14516c32010-07-31 14:29:06 -04005183 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5184 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005185
5186 if (task->tk_status < 0) {
5187 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005188 if (atomic_read(&clp->cl_count) == 1)
5189 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005190
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005191 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5192 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005193 return;
5194 }
5195 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005196 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005197out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005198 dprintk("<-- %s\n", __func__);
5199}
5200
5201static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5202{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005203 struct nfs4_sequence_data *calldata = data;
5204 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005205 struct nfs4_sequence_args *args;
5206 struct nfs4_sequence_res *res;
5207
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005208 args = task->tk_msg.rpc_argp;
5209 res = task->tk_msg.rpc_resp;
5210
Trond Myklebust035168a2010-06-16 09:52:26 -04005211 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005212 return;
5213 rpc_call_start(task);
5214}
5215
5216static const struct rpc_call_ops nfs41_sequence_ops = {
5217 .rpc_call_done = nfs41_sequence_call_done,
5218 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005219 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005220};
5221
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005222static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005223{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005224 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005225 struct rpc_message msg = {
5226 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5227 .rpc_cred = cred,
5228 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005229 struct rpc_task_setup task_setup_data = {
5230 .rpc_client = clp->cl_rpcclient,
5231 .rpc_message = &msg,
5232 .callback_ops = &nfs41_sequence_ops,
5233 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5234 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005235
Alexandros Batsakis71358402010-02-05 03:45:05 -08005236 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005237 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005238 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005239 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005240 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005241 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005242 }
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005243 msg.rpc_argp = &calldata->args;
5244 msg.rpc_resp = &calldata->res;
5245 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005246 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005247
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005248 return rpc_run_task(&task_setup_data);
5249}
5250
5251static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5252{
5253 struct rpc_task *task;
5254 int ret = 0;
5255
5256 task = _nfs41_proc_sequence(clp, cred);
5257 if (IS_ERR(task))
5258 ret = PTR_ERR(task);
5259 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005260 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005261 dprintk("<-- %s status=%d\n", __func__, ret);
5262 return ret;
5263}
5264
5265static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5266{
5267 struct rpc_task *task;
5268 int ret;
5269
5270 task = _nfs41_proc_sequence(clp, cred);
5271 if (IS_ERR(task)) {
5272 ret = PTR_ERR(task);
5273 goto out;
5274 }
5275 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005276 if (!ret) {
5277 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5278
5279 if (task->tk_status == 0)
5280 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005281 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005282 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005283 rpc_put_task(task);
5284out:
5285 dprintk("<-- %s status=%d\n", __func__, ret);
5286 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005287}
5288
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005289struct nfs4_reclaim_complete_data {
5290 struct nfs_client *clp;
5291 struct nfs41_reclaim_complete_args arg;
5292 struct nfs41_reclaim_complete_res res;
5293};
5294
5295static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5296{
5297 struct nfs4_reclaim_complete_data *calldata = data;
5298
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005299 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005300 if (nfs41_setup_sequence(calldata->clp->cl_session,
5301 &calldata->arg.seq_args,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005302 &calldata->res.seq_res, 0, task))
5303 return;
5304
5305 rpc_call_start(task);
5306}
5307
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005308static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5309{
5310 switch(task->tk_status) {
5311 case 0:
5312 case -NFS4ERR_COMPLETE_ALREADY:
5313 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5314 break;
5315 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005316 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5317 return -EAGAIN;
5318 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005319 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005320 }
5321 return 0;
5322}
5323
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005324static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5325{
5326 struct nfs4_reclaim_complete_data *calldata = data;
5327 struct nfs_client *clp = calldata->clp;
5328 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5329
5330 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005331 if (!nfs41_sequence_done(task, res))
5332 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005333
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005334 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5335 rpc_restart_call_prepare(task);
5336 return;
5337 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005338 dprintk("<-- %s\n", __func__);
5339}
5340
5341static void nfs4_free_reclaim_complete_data(void *data)
5342{
5343 struct nfs4_reclaim_complete_data *calldata = data;
5344
5345 kfree(calldata);
5346}
5347
5348static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5349 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5350 .rpc_call_done = nfs4_reclaim_complete_done,
5351 .rpc_release = nfs4_free_reclaim_complete_data,
5352};
5353
5354/*
5355 * Issue a global reclaim complete.
5356 */
5357static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5358{
5359 struct nfs4_reclaim_complete_data *calldata;
5360 struct rpc_task *task;
5361 struct rpc_message msg = {
5362 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5363 };
5364 struct rpc_task_setup task_setup_data = {
5365 .rpc_client = clp->cl_rpcclient,
5366 .rpc_message = &msg,
5367 .callback_ops = &nfs4_reclaim_complete_call_ops,
5368 .flags = RPC_TASK_ASYNC,
5369 };
5370 int status = -ENOMEM;
5371
5372 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005373 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005374 if (calldata == NULL)
5375 goto out;
5376 calldata->clp = clp;
5377 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005378
5379 msg.rpc_argp = &calldata->arg;
5380 msg.rpc_resp = &calldata->res;
5381 task_setup_data.callback_data = calldata;
5382 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005383 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005384 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005385 goto out;
5386 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005387 status = nfs4_wait_for_completion_rpc_task(task);
5388 if (status == 0)
5389 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005390 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005391 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005392out:
5393 dprintk("<-- %s status=%d\n", __func__, status);
5394 return status;
5395}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005396
5397static void
5398nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5399{
5400 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005401 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005402
5403 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005404 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5405 * right now covering the LAYOUTGET we are about to send.
5406 * However, that is not so catastrophic, and there seems
5407 * to be no way to prevent it completely.
5408 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005409 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5410 &lgp->res.seq_res, 0, task))
5411 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005412 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5413 NFS_I(lgp->args.inode)->layout,
5414 lgp->args.ctx->state)) {
5415 rpc_exit(task, NFS4_OK);
5416 return;
5417 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005418 rpc_call_start(task);
5419}
5420
5421static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5422{
5423 struct nfs4_layoutget *lgp = calldata;
5424 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5425
5426 dprintk("--> %s\n", __func__);
5427
5428 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5429 return;
5430
5431 switch (task->tk_status) {
5432 case 0:
5433 break;
5434 case -NFS4ERR_LAYOUTTRYLATER:
5435 case -NFS4ERR_RECALLCONFLICT:
5436 task->tk_status = -NFS4ERR_DELAY;
5437 /* Fall through */
5438 default:
5439 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5440 rpc_restart_call_prepare(task);
5441 return;
5442 }
5443 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005444 dprintk("<-- %s\n", __func__);
5445}
5446
5447static void nfs4_layoutget_release(void *calldata)
5448{
5449 struct nfs4_layoutget *lgp = calldata;
5450
5451 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005452 if (lgp->res.layout.buf != NULL)
5453 free_page((unsigned long) lgp->res.layout.buf);
5454 put_nfs_open_context(lgp->args.ctx);
5455 kfree(calldata);
5456 dprintk("<-- %s\n", __func__);
5457}
5458
5459static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5460 .rpc_call_prepare = nfs4_layoutget_prepare,
5461 .rpc_call_done = nfs4_layoutget_done,
5462 .rpc_release = nfs4_layoutget_release,
5463};
5464
5465int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5466{
5467 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5468 struct rpc_task *task;
5469 struct rpc_message msg = {
5470 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5471 .rpc_argp = &lgp->args,
5472 .rpc_resp = &lgp->res,
5473 };
5474 struct rpc_task_setup task_setup_data = {
5475 .rpc_client = server->client,
5476 .rpc_message = &msg,
5477 .callback_ops = &nfs4_layoutget_call_ops,
5478 .callback_data = lgp,
5479 .flags = RPC_TASK_ASYNC,
5480 };
5481 int status = 0;
5482
5483 dprintk("--> %s\n", __func__);
5484
5485 lgp->res.layout.buf = (void *)__get_free_page(GFP_NOFS);
5486 if (lgp->res.layout.buf == NULL) {
5487 nfs4_layoutget_release(lgp);
5488 return -ENOMEM;
5489 }
5490
5491 lgp->res.seq_res.sr_slot = NULL;
5492 task = rpc_run_task(&task_setup_data);
5493 if (IS_ERR(task))
5494 return PTR_ERR(task);
5495 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00005496 if (status == 0)
5497 status = task->tk_status;
5498 if (status == 0)
5499 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005500 rpc_put_task(task);
5501 dprintk("<-- %s status=%d\n", __func__, status);
5502 return status;
5503}
5504
5505static int
5506_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5507{
5508 struct nfs4_getdeviceinfo_args args = {
5509 .pdev = pdev,
5510 };
5511 struct nfs4_getdeviceinfo_res res = {
5512 .pdev = pdev,
5513 };
5514 struct rpc_message msg = {
5515 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5516 .rpc_argp = &args,
5517 .rpc_resp = &res,
5518 };
5519 int status;
5520
5521 dprintk("--> %s\n", __func__);
5522 status = nfs4_call_sync(server, &msg, &args, &res, 0);
5523 dprintk("<-- %s status=%d\n", __func__, status);
5524
5525 return status;
5526}
5527
5528int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5529{
5530 struct nfs4_exception exception = { };
5531 int err;
5532
5533 do {
5534 err = nfs4_handle_exception(server,
5535 _nfs4_proc_getdeviceinfo(server, pdev),
5536 &exception);
5537 } while (exception.retry);
5538 return err;
5539}
5540EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5541
Andy Adamson557134a2009-04-01 09:21:53 -04005542#endif /* CONFIG_NFS_V4_1 */
5543
Andy Adamson591d71c2009-04-01 09:22:47 -04005544struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005545 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005546 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 .recover_open = nfs4_open_reclaim,
5548 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04005549 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005550 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551};
5552
Andy Adamson591d71c2009-04-01 09:22:47 -04005553#if defined(CONFIG_NFS_V4_1)
5554struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5555 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5556 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5557 .recover_open = nfs4_open_reclaim,
5558 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05005559 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005560 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005561 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04005562};
5563#endif /* CONFIG_NFS_V4_1 */
5564
5565struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005566 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005567 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 .recover_open = nfs4_open_expired,
5569 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04005570 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005571 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572};
5573
Andy Adamson591d71c2009-04-01 09:22:47 -04005574#if defined(CONFIG_NFS_V4_1)
5575struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5576 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5577 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5578 .recover_open = nfs4_open_expired,
5579 .recover_lock = nfs4_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05005580 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005581 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04005582};
5583#endif /* CONFIG_NFS_V4_1 */
5584
Benny Halevy29fba382009-04-01 09:22:44 -04005585struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5586 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04005587 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005588 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04005589};
5590
5591#if defined(CONFIG_NFS_V4_1)
5592struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5593 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04005594 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005595 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04005596};
5597#endif
5598
Trond Myklebust97dc1352010-06-16 09:52:26 -04005599static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
5600 .minor_version = 0,
5601 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04005602 .validate_stateid = nfs4_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005603 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
5604 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
5605 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005606};
5607
5608#if defined(CONFIG_NFS_V4_1)
5609static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
5610 .minor_version = 1,
5611 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04005612 .validate_stateid = nfs41_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005613 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
5614 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
5615 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005616};
5617#endif
5618
Trond Myklebust97dc1352010-06-16 09:52:26 -04005619const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
5620 [0] = &nfs_v4_0_minor_ops,
5621#if defined(CONFIG_NFS_V4_1)
5622 [1] = &nfs_v4_1_minor_ops,
5623#endif
5624};
5625
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005626static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005627 .permission = nfs_permission,
5628 .getattr = nfs_getattr,
5629 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005630 .getxattr = generic_getxattr,
5631 .setxattr = generic_setxattr,
5632 .listxattr = generic_listxattr,
5633 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005634};
5635
David Howells509de812006-08-22 20:06:11 -04005636const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637 .version = 4, /* protocol version */
5638 .dentry_ops = &nfs4_dentry_operations,
5639 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005640 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641 .getroot = nfs4_proc_get_root,
5642 .getattr = nfs4_proc_getattr,
5643 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04005644 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645 .lookup = nfs4_proc_lookup,
5646 .access = nfs4_proc_access,
5647 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 .create = nfs4_proc_create,
5649 .remove = nfs4_proc_remove,
5650 .unlink_setup = nfs4_proc_unlink_setup,
5651 .unlink_done = nfs4_proc_unlink_done,
5652 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04005653 .rename_setup = nfs4_proc_rename_setup,
5654 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 .link = nfs4_proc_link,
5656 .symlink = nfs4_proc_symlink,
5657 .mkdir = nfs4_proc_mkdir,
5658 .rmdir = nfs4_proc_remove,
5659 .readdir = nfs4_proc_readdir,
5660 .mknod = nfs4_proc_mknod,
5661 .statfs = nfs4_proc_statfs,
5662 .fsinfo = nfs4_proc_fsinfo,
5663 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04005664 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 .decode_dirent = nfs4_decode_dirent,
5666 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05005667 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005669 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005671 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005673 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04005674 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04005675 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00005676 .init_client = nfs4_init_client,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677};
5678
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005679static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
5680 .prefix = XATTR_NAME_NFSV4_ACL,
5681 .list = nfs4_xattr_list_nfs4_acl,
5682 .get = nfs4_xattr_get_nfs4_acl,
5683 .set = nfs4_xattr_set_nfs4_acl,
5684};
5685
5686const struct xattr_handler *nfs4_xattr_handlers[] = {
5687 &nfs4_xattr_nfs4_acl_handler,
5688 NULL
5689};
5690
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691/*
5692 * Local variables:
5693 * c-basic-offset: 8
5694 * End:
5695 */