blob: 55a8fc2f3df4cb591af6ff7bacc7c9b69579c745 [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 Myklebust035168ab2010-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 Myklebust035168ab2010-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 Myklebust035168ab2010-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 Myklebust035168ab2010-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 Myklebust035168ab2010-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 Myklebust035168ab2010-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 Myklebust035168ab2010-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 Myklebust035168ab2010-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 Myklebust035168ab2010-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 Myklebust035168ab2010-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 Myklebust035168ab2010-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 Myklebust035168ab2010-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
Trond Myklebust10afec902007-05-14 17:16:04 -04001576static int nfs4_recover_expired_lease(struct nfs_server *server)
Trond Myklebust58d97142006-01-03 09:55:24 +01001577{
David Howells7539bba2006-08-22 20:06:09 -04001578 struct nfs_client *clp = server->nfs_client;
Trond Myklebusta78cb572009-08-09 15:06:19 -04001579 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001580 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001581
Trond Myklebusta78cb572009-08-09 15:06:19 -04001582 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001583 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001584 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001585 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001586 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1587 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001588 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001589 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001590 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001591 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001592 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001593}
1594
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595/*
1596 * OPEN_EXPIRED:
1597 * reclaim state on the server after a network partition.
1598 * Assumes caller holds the appropriate lock
1599 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001600static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001602 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001603 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001605 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1606 if (IS_ERR(opendata))
1607 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001608 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001609 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001610 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001611 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001612 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613}
1614
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001615static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001616{
Trond Myklebust539cd032007-06-05 11:46:42 -04001617 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001618 struct nfs4_exception exception = { };
1619 int err;
1620
1621 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001622 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001623 switch (err) {
1624 default:
1625 goto out;
1626 case -NFS4ERR_GRACE:
1627 case -NFS4ERR_DELAY:
1628 nfs4_handle_exception(server, err, &exception);
1629 err = 0;
1630 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001631 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001632out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001633 return err;
1634}
1635
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1637{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001639 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
Trond Myklebust864472e2006-01-03 09:55:15 +01001641 ctx = nfs4_state_find_open_context(state);
1642 if (IS_ERR(ctx))
1643 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001644 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001645 put_nfs_open_context(ctx);
1646 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647}
1648
1649/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001650 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1651 * fields corresponding to attributes that were used to store the verifier.
1652 * Make sure we clobber those fields in the later setattr call
1653 */
1654static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1655{
1656 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1657 !(sattr->ia_valid & ATTR_ATIME_SET))
1658 sattr->ia_valid |= ATTR_ATIME;
1659
1660 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1661 !(sattr->ia_valid & ATTR_MTIME_SET))
1662 sattr->ia_valid |= ATTR_MTIME;
1663}
1664
1665/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001666 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001668static 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 -07001669{
1670 struct nfs4_state_owner *sp;
1671 struct nfs4_state *state = NULL;
1672 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001673 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001674 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
1676 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 status = -ENOMEM;
1678 if (!(sp = nfs4_get_state_owner(server, cred))) {
1679 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1680 goto out_err;
1681 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001682 status = nfs4_recover_expired_lease(server);
1683 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001684 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001685 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001686 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001687 status = -ENOMEM;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001688 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001689 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001690 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Trond Myklebustaac00a82007-07-05 19:02:21 -04001692 if (path->dentry->d_inode != NULL)
1693 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1694
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001695 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001697 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001699 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001700 status = PTR_ERR(state);
1701 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001702 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001703 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001704 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001705
1706 if (opendata->o_arg.open_flags & O_EXCL) {
1707 nfs4_exclusive_attrset(opendata, sattr);
1708
1709 nfs_fattr_init(opendata->o_res.f_attr);
1710 status = nfs4_do_setattr(state->inode, cred,
1711 opendata->o_res.f_attr, sattr,
1712 state);
1713 if (status == 0)
1714 nfs_setattr_update_inode(state->inode, sattr);
1715 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1716 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001717 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 *res = state;
1720 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001721err_opendata_put:
1722 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001723err_put_state_owner:
1724 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 *res = NULL;
1727 return status;
1728}
1729
1730
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001731static 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 -07001732{
1733 struct nfs4_exception exception = { };
1734 struct nfs4_state *res;
1735 int status;
1736
1737 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001738 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 if (status == 0)
1740 break;
1741 /* NOTE: BAD_SEQID means the server and client disagree about the
1742 * book-keeping w.r.t. state-changing operations
1743 * (OPEN/CLOSE/LOCK/LOCKU...)
1744 * It is actually a sign of a bug on the client or on the server.
1745 *
1746 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001747 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 * have unhashed the old state_owner for us, and that we can
1749 * therefore safely retry using a new one. We should still warn
1750 * the user though...
1751 */
1752 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001753 printk(KERN_WARNING "NFS: v4 server %s "
1754 " returned a bad sequence-id error!\n",
1755 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 exception.retry = 1;
1757 continue;
1758 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001759 /*
1760 * BAD_STATEID on OPEN means that the server cancelled our
1761 * state before it received the OPEN_CONFIRM.
1762 * Recover by retrying the request as per the discussion
1763 * on Page 181 of RFC3530.
1764 */
1765 if (status == -NFS4ERR_BAD_STATEID) {
1766 exception.retry = 1;
1767 continue;
1768 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001769 if (status == -EAGAIN) {
1770 /* We must have found a delegation */
1771 exception.retry = 1;
1772 continue;
1773 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1775 status, &exception));
1776 } while (exception.retry);
1777 return res;
1778}
1779
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001780static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1781 struct nfs_fattr *fattr, struct iattr *sattr,
1782 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001784 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001786 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 .iap = sattr,
1788 .server = server,
1789 .bitmask = server->attr_bitmask,
1790 };
1791 struct nfs_setattrres res = {
1792 .fattr = fattr,
1793 .server = server,
1794 };
1795 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001796 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1797 .rpc_argp = &arg,
1798 .rpc_resp = &res,
1799 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001801 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001802 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
Trond Myklebust0e574af2005-10-27 22:12:38 -04001804 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001806 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1807 /* Use that stateid */
1808 } else if (state != NULL) {
Trond Myklebust77041ed2010-07-01 12:49:11 -04001809 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001810 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1812
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001813 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001814 if (status == 0 && state != NULL)
1815 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001816 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817}
1818
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001819static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1820 struct nfs_fattr *fattr, struct iattr *sattr,
1821 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001823 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 struct nfs4_exception exception = { };
1825 int err;
1826 do {
1827 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001828 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 &exception);
1830 } while (exception.retry);
1831 return err;
1832}
1833
1834struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001835 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 struct inode *inode;
1837 struct nfs4_state *state;
1838 struct nfs_closeargs arg;
1839 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001840 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001841 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00001842 bool roc;
1843 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844};
1845
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001846static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001847{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001848 struct nfs4_closedata *calldata = data;
1849 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001850
Fred Isamanf7e89172011-01-06 11:36:32 +00001851 if (calldata->roc)
1852 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001853 nfs4_put_open_state(calldata->state);
1854 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001855 nfs4_put_state_owner(sp);
Jan Blunck31f31db12008-05-02 13:42:45 -07001856 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001857 kfree(calldata);
1858}
1859
Trond Myklebust88069f72009-12-08 08:33:16 -05001860static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1861 fmode_t fmode)
1862{
1863 spin_lock(&state->owner->so_lock);
1864 if (!(fmode & FMODE_READ))
1865 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1866 if (!(fmode & FMODE_WRITE))
1867 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1868 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1869 spin_unlock(&state->owner->so_lock);
1870}
1871
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001872static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001874 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 struct nfs_server *server = NFS_SERVER(calldata->inode);
1877
Trond Myklebust14516c32010-07-31 14:29:06 -04001878 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1879 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 /* hmm. we are done with the inode, and in the process of freeing
1881 * the state_owner. we keep this around to process errors
1882 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 switch (task->tk_status) {
1884 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00001885 if (calldata->roc)
1886 pnfs_roc_set_barrier(state->inode,
1887 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04001888 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001889 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001890 nfs4_close_clear_stateid_flags(state,
1891 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 break;
1893 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001894 case -NFS4ERR_OLD_STATEID:
1895 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001897 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001898 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001900 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1901 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001903 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001904 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905}
1906
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001907static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001909 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001910 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001911 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001912
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001913 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001914 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001915
Trond Myklebust88069f72009-12-08 08:33:16 -05001916 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1917 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001918 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001919 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001920 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001921 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001922 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1923 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1924 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001925 }
1926 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001927 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1928 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1929 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001930 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001931 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001932 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05001933
1934 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001935 /* Note: exit _without_ calling nfs4_close_done */
1936 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001937 return;
1938 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001939
Fred Isamanf7e89172011-01-06 11:36:32 +00001940 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001941 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00001942 if (calldata->roc &&
1943 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
1944 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
1945 task, NULL);
1946 return;
1947 }
1948 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001949
Trond Myklebust516a6af2005-10-27 22:12:41 -04001950 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001951 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04001952 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Andy Adamson19ddab02009-04-01 09:22:20 -04001953 &calldata->arg.seq_args, &calldata->res.seq_res,
1954 1, task))
1955 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001956 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957}
1958
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001959static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001960 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001961 .rpc_call_done = nfs4_close_done,
1962 .rpc_release = nfs4_free_closedata,
1963};
1964
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965/*
1966 * It is possible for data to be read/written from a mem-mapped file
1967 * after the sys_close call (which hits the vfs layer as a flush).
1968 * This means that we can't safely call nfsv4 close on a file until
1969 * the inode is cleared. This in turn means that we are not good
1970 * NFSv4 citizens - we do not indicate to the server to update the file's
1971 * share state even when we are done with one of the three share
1972 * stateid's in the inode.
1973 *
1974 * NOTE: Caller must be holding the sp->so_owner semaphore!
1975 */
Fred Isamanf7e89172011-01-06 11:36:32 +00001976int 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 -07001977{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001978 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001980 struct nfs4_state_owner *sp = state->owner;
1981 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001982 struct rpc_message msg = {
1983 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1984 .rpc_cred = state->owner->so_cred,
1985 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001986 struct rpc_task_setup task_setup_data = {
1987 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001988 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001989 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05001990 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001991 .flags = RPC_TASK_ASYNC,
1992 };
Trond Myklebust95121352005-10-18 14:20:12 -07001993 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001995 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07001997 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001998 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002000 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002001 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002003 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002004 if (calldata->arg.seqid == NULL)
2005 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002006 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002007 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002008 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002009 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002010 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002011 calldata->roc = roc;
Al Virof6948692010-01-30 13:51:04 -05002012 path_get(path);
2013 calldata->path = *path;
Trond Myklebust95121352005-10-18 14:20:12 -07002014
Trond Myklebust1174dd12010-12-21 10:52:24 -05002015 msg.rpc_argp = &calldata->arg;
2016 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002017 task_setup_data.callback_data = calldata;
2018 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002019 if (IS_ERR(task))
2020 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002021 status = 0;
2022 if (wait)
2023 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002024 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002025 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002026out_free_calldata:
2027 kfree(calldata);
2028out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002029 if (roc)
2030 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002031 nfs4_put_open_state(state);
2032 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002033 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034}
2035
Trond Myklebust2b484292010-09-17 10:56:51 -04002036static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002037nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 struct nfs4_state *state;
2040
Trond Myklebust565277f2007-10-15 18:17:53 -04002041 /* Protect against concurrent sillydeletes */
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002042 state = nfs4_do_open(dir, &ctx->path, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002043 if (IS_ERR(state))
2044 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002045 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002046 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047}
2048
Trond Myklebust1185a552009-12-03 15:54:02 -05002049static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002050{
2051 if (ctx->state == NULL)
2052 return;
2053 if (is_sync)
2054 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2055 else
2056 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2057}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
2059static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2060{
Benny Halevy43652ad2009-04-01 09:21:54 -04002061 struct nfs4_server_caps_arg args = {
2062 .fhandle = fhandle,
2063 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 struct nfs4_server_caps_res res = {};
2065 struct rpc_message msg = {
2066 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002067 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 .rpc_resp = &res,
2069 };
2070 int status;
2071
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002072 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 if (status == 0) {
2074 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002075 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2076 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2077 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2078 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2079 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2081 server->caps |= NFS_CAP_ACLS;
2082 if (res.has_links != 0)
2083 server->caps |= NFS_CAP_HARDLINKS;
2084 if (res.has_symlinks != 0)
2085 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002086 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2087 server->caps |= NFS_CAP_FILEID;
2088 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2089 server->caps |= NFS_CAP_MODE;
2090 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2091 server->caps |= NFS_CAP_NLINK;
2092 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2093 server->caps |= NFS_CAP_OWNER;
2094 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2095 server->caps |= NFS_CAP_OWNER_GROUP;
2096 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2097 server->caps |= NFS_CAP_ATIME;
2098 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2099 server->caps |= NFS_CAP_CTIME;
2100 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2101 server->caps |= NFS_CAP_MTIME;
2102
Trond Myklebusta65318b2009-03-11 14:10:28 -04002103 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2104 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2105 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 server->acl_bitmask = res.acl_bitmask;
2107 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002108
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 return status;
2110}
2111
Trond Myklebust55a97592006-06-09 09:34:19 -04002112int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113{
2114 struct nfs4_exception exception = { };
2115 int err;
2116 do {
2117 err = nfs4_handle_exception(server,
2118 _nfs4_server_capabilities(server, fhandle),
2119 &exception);
2120 } while (exception.retry);
2121 return err;
2122}
2123
2124static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2125 struct nfs_fsinfo *info)
2126{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 struct nfs4_lookup_root_arg args = {
2128 .bitmask = nfs4_fattr_bitmap,
2129 };
2130 struct nfs4_lookup_res res = {
2131 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002132 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 .fh = fhandle,
2134 };
2135 struct rpc_message msg = {
2136 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2137 .rpc_argp = &args,
2138 .rpc_resp = &res,
2139 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002140
Trond Myklebust0e574af2005-10-27 22:12:38 -04002141 nfs_fattr_init(info->fattr);
Trond Myklebustfccba802009-07-21 16:48:07 -04002142 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143}
2144
2145static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2146 struct nfs_fsinfo *info)
2147{
2148 struct nfs4_exception exception = { };
2149 int err;
2150 do {
2151 err = nfs4_handle_exception(server,
2152 _nfs4_lookup_root(server, fhandle, info),
2153 &exception);
2154 } while (exception.retry);
2155 return err;
2156}
2157
David Howells54ceac42006-08-22 20:06:13 -04002158/*
2159 * get the file handle for the "/" directory on the server
2160 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002162 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 int status;
2165
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 if (status == 0)
2168 status = nfs4_server_capabilities(server, fhandle);
2169 if (status == 0)
2170 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002171 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172}
2173
Manoj Naik6b97fd32006-06-09 09:34:29 -04002174/*
2175 * Get locations and (maybe) other attributes of a referral.
2176 * Note that we'll actually follow the referral later when
2177 * we detect fsid mismatch in inode revalidation
2178 */
Trond Myklebust56659e92007-07-17 21:52:39 -04002179static 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 -04002180{
2181 int status = -ENOMEM;
2182 struct page *page = NULL;
2183 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002184
2185 page = alloc_page(GFP_KERNEL);
2186 if (page == NULL)
2187 goto out;
2188 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2189 if (locations == NULL)
2190 goto out;
2191
Trond Myklebustc228fd32007-01-13 02:28:11 -05002192 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002193 if (status != 0)
2194 goto out;
2195 /* Make sure server returned a different fsid for the referral */
2196 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07002197 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 -04002198 status = -EIO;
2199 goto out;
2200 }
2201
2202 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2203 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2204 if (!fattr->mode)
2205 fattr->mode = S_IFDIR;
2206 memset(fhandle, 0, sizeof(struct nfs_fh));
2207out:
2208 if (page)
2209 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002210 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002211 return status;
2212}
2213
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2215{
2216 struct nfs4_getattr_arg args = {
2217 .fh = fhandle,
2218 .bitmask = server->attr_bitmask,
2219 };
2220 struct nfs4_getattr_res res = {
2221 .fattr = fattr,
2222 .server = server,
2223 };
2224 struct rpc_message msg = {
2225 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2226 .rpc_argp = &args,
2227 .rpc_resp = &res,
2228 };
2229
Trond Myklebust0e574af2005-10-27 22:12:38 -04002230 nfs_fattr_init(fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002231 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232}
2233
2234static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2235{
2236 struct nfs4_exception exception = { };
2237 int err;
2238 do {
2239 err = nfs4_handle_exception(server,
2240 _nfs4_proc_getattr(server, fhandle, fattr),
2241 &exception);
2242 } while (exception.retry);
2243 return err;
2244}
2245
2246/*
2247 * The file is not closed if it is opened due to the a request to change
2248 * the size of the file. The open call will not be needed once the
2249 * VFS layer lookup-intents are implemented.
2250 *
2251 * Close is called when the inode is destroyed.
2252 * If we haven't opened the file for O_WRONLY, we
2253 * need to in the size_change case to obtain a stateid.
2254 *
2255 * Got race?
2256 * Because OPEN is always done by name in nfsv4, it is
2257 * possible that we opened a different file by the same
2258 * name. We can recognize this race condition, but we
2259 * can't do anything about it besides returning an error.
2260 *
2261 * This will be fixed with VFS changes (lookup-intent).
2262 */
2263static int
2264nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2265 struct iattr *sattr)
2266{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002267 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002268 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002269 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 int status;
2271
Trond Myklebust0e574af2005-10-27 22:12:38 -04002272 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
Trond Myklebustd5308382005-11-04 15:33:38 -05002274 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002275 if (sattr->ia_valid & ATTR_FILE) {
2276 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002277
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002278 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002279 if (ctx) {
2280 cred = ctx->cred;
2281 state = ctx->state;
2282 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002283 }
2284
2285 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002286 if (status == 0)
2287 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 return status;
2289}
2290
Trond Myklebust56659e92007-07-17 21:52:39 -04002291static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2292 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04002293 struct nfs_fattr *fattr)
2294{
2295 int status;
2296 struct nfs4_lookup_arg args = {
2297 .bitmask = server->attr_bitmask,
2298 .dir_fh = dirfh,
2299 .name = name,
2300 };
2301 struct nfs4_lookup_res res = {
2302 .server = server,
2303 .fattr = fattr,
2304 .fh = fhandle,
2305 };
2306 struct rpc_message msg = {
2307 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2308 .rpc_argp = &args,
2309 .rpc_resp = &res,
2310 };
2311
2312 nfs_fattr_init(fattr);
2313
2314 dprintk("NFS call lookupfh %s\n", name->name);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002315 status = nfs4_call_sync(server, &msg, &args, &res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002316 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002317 return status;
2318}
2319
2320static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2321 struct qstr *name, struct nfs_fh *fhandle,
2322 struct nfs_fattr *fattr)
2323{
2324 struct nfs4_exception exception = { };
2325 int err;
2326 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04002327 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2328 /* FIXME: !!!! */
2329 if (err == -NFS4ERR_MOVED) {
2330 err = -EREMOTE;
2331 break;
2332 }
2333 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002334 } while (exception.retry);
2335 return err;
2336}
2337
Trond Myklebust56659e92007-07-17 21:52:39 -04002338static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2340{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002341 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342
2343 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002344 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002345 if (status == -NFS4ERR_MOVED)
2346 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 dprintk("NFS reply lookup: %d\n", status);
2348 return status;
2349}
2350
2351static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2352{
2353 struct nfs4_exception exception = { };
2354 int err;
2355 do {
2356 err = nfs4_handle_exception(NFS_SERVER(dir),
2357 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2358 &exception);
2359 } while (exception.retry);
2360 return err;
2361}
2362
2363static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2364{
Trond Myklebust76b32992007-08-10 17:45:11 -04002365 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 struct nfs4_accessargs args = {
2367 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002368 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002370 struct nfs4_accessres res = {
2371 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002372 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 struct rpc_message msg = {
2374 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2375 .rpc_argp = &args,
2376 .rpc_resp = &res,
2377 .rpc_cred = entry->cred,
2378 };
2379 int mode = entry->mask;
2380 int status;
2381
2382 /*
2383 * Determine which access bits we want to ask for...
2384 */
2385 if (mode & MAY_READ)
2386 args.access |= NFS4_ACCESS_READ;
2387 if (S_ISDIR(inode->i_mode)) {
2388 if (mode & MAY_WRITE)
2389 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2390 if (mode & MAY_EXEC)
2391 args.access |= NFS4_ACCESS_LOOKUP;
2392 } else {
2393 if (mode & MAY_WRITE)
2394 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2395 if (mode & MAY_EXEC)
2396 args.access |= NFS4_ACCESS_EXECUTE;
2397 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002398
2399 res.fattr = nfs_alloc_fattr();
2400 if (res.fattr == NULL)
2401 return -ENOMEM;
2402
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002403 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 if (!status) {
2405 entry->mask = 0;
2406 if (res.access & NFS4_ACCESS_READ)
2407 entry->mask |= MAY_READ;
2408 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2409 entry->mask |= MAY_WRITE;
2410 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2411 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002412 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002414 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 return status;
2416}
2417
2418static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2419{
2420 struct nfs4_exception exception = { };
2421 int err;
2422 do {
2423 err = nfs4_handle_exception(NFS_SERVER(inode),
2424 _nfs4_proc_access(inode, entry),
2425 &exception);
2426 } while (exception.retry);
2427 return err;
2428}
2429
2430/*
2431 * TODO: For the time being, we don't try to get any attributes
2432 * along with any of the zero-copy operations READ, READDIR,
2433 * READLINK, WRITE.
2434 *
2435 * In the case of the first three, we want to put the GETATTR
2436 * after the read-type operation -- this is because it is hard
2437 * to predict the length of a GETATTR response in v4, and thus
2438 * align the READ data correctly. This means that the GETATTR
2439 * may end up partially falling into the page cache, and we should
2440 * shift it into the 'tail' of the xdr_buf before processing.
2441 * To do this efficiently, we need to know the total length
2442 * of data received, which doesn't seem to be available outside
2443 * of the RPC layer.
2444 *
2445 * In the case of WRITE, we also want to put the GETATTR after
2446 * the operation -- in this case because we want to make sure
2447 * we get the post-operation mtime and size. This means that
2448 * we can't use xdr_encode_pages() as written: we need a variant
2449 * of it which would leave room in the 'tail' iovec.
2450 *
2451 * Both of these changes to the XDR layer would in fact be quite
2452 * minor, but I decided to leave them for a subsequent patch.
2453 */
2454static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2455 unsigned int pgbase, unsigned int pglen)
2456{
2457 struct nfs4_readlink args = {
2458 .fh = NFS_FH(inode),
2459 .pgbase = pgbase,
2460 .pglen = pglen,
2461 .pages = &page,
2462 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002463 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 struct rpc_message msg = {
2465 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2466 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002467 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 };
2469
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002470 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471}
2472
2473static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2474 unsigned int pgbase, unsigned int pglen)
2475{
2476 struct nfs4_exception exception = { };
2477 int err;
2478 do {
2479 err = nfs4_handle_exception(NFS_SERVER(inode),
2480 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2481 &exception);
2482 } while (exception.retry);
2483 return err;
2484}
2485
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486/*
2487 * Got race?
2488 * We will need to arrange for the VFS layer to provide an atomic open.
2489 * Until then, this create/open method is prone to inefficiency and race
2490 * conditions due to the lookup, create, and open VFS calls from sys_open()
2491 * placed on the wire.
2492 *
2493 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2494 * The file will be opened again in the subsequent VFS open call
2495 * (nfs4_proc_file_open).
2496 *
2497 * The open for read will just hang around to be used by any process that
2498 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2499 */
2500
2501static int
2502nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002503 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504{
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002505 struct path my_path = {
Trond Myklebustad389da2007-06-05 12:30:00 -04002506 .dentry = dentry,
2507 };
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002508 struct path *path = &my_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002510 struct rpc_cred *cred = NULL;
2511 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 int status = 0;
2513
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002514 if (ctx != NULL) {
2515 cred = ctx->cred;
2516 path = &ctx->path;
2517 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002519 sattr->ia_mode &= ~current_umask();
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002520 state = nfs4_do_open(dir, path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002521 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 if (IS_ERR(state)) {
2523 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002524 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002526 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002527 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002528 if (ctx != NULL)
2529 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002530 else
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002531 nfs4_close_sync(path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532out:
2533 return status;
2534}
2535
2536static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2537{
Trond Myklebust16e42952005-10-27 22:12:44 -04002538 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002539 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002541 .name.len = name->len,
2542 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002543 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002545 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002546 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002547 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002549 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2550 .rpc_argp = &args,
2551 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002553 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554
Trond Myklebustd3468902010-04-16 16:22:50 -04002555 res.dir_attr = nfs_alloc_fattr();
2556 if (res.dir_attr == NULL)
2557 goto out;
2558
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002559 status = nfs4_call_sync(server, &msg, &args, &res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002560 if (status == 0) {
2561 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002562 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002563 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002564 nfs_free_fattr(res.dir_attr);
2565out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 return status;
2567}
2568
2569static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2570{
2571 struct nfs4_exception exception = { };
2572 int err;
2573 do {
2574 err = nfs4_handle_exception(NFS_SERVER(dir),
2575 _nfs4_proc_remove(dir, name),
2576 &exception);
2577 } while (exception.retry);
2578 return err;
2579}
2580
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002581static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002583 struct nfs_server *server = NFS_SERVER(dir);
2584 struct nfs_removeargs *args = msg->rpc_argp;
2585 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
Trond Myklebusta65318b2009-03-11 14:10:28 -04002587 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002588 res->server = server;
Benny Halevydfb4f3092010-09-24 09:17:01 -04002589 res->seq_res.sr_slot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591}
2592
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002593static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002595 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2596
Trond Myklebust14516c32010-07-31 14:29:06 -04002597 if (!nfs4_sequence_done(task, &res->seq_res))
2598 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002599 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002600 return 0;
2601 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002602 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002603 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604}
2605
Jeff Laytond3d41522010-09-17 17:31:57 -04002606static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2607{
2608 struct nfs_server *server = NFS_SERVER(dir);
2609 struct nfs_renameargs *arg = msg->rpc_argp;
2610 struct nfs_renameres *res = msg->rpc_resp;
2611
2612 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2613 arg->bitmask = server->attr_bitmask;
2614 res->server = server;
2615}
2616
2617static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2618 struct inode *new_dir)
2619{
2620 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2621
2622 if (!nfs4_sequence_done(task, &res->seq_res))
2623 return 0;
2624 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2625 return 0;
2626
2627 update_changeattr(old_dir, &res->old_cinfo);
2628 nfs_post_op_update_inode(old_dir, res->old_fattr);
2629 update_changeattr(new_dir, &res->new_cinfo);
2630 nfs_post_op_update_inode(new_dir, res->new_fattr);
2631 return 1;
2632}
2633
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2635 struct inode *new_dir, struct qstr *new_name)
2636{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002637 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002638 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 .old_dir = NFS_FH(old_dir),
2640 .new_dir = NFS_FH(new_dir),
2641 .old_name = old_name,
2642 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002643 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002645 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002646 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002647 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 struct rpc_message msg = {
2649 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2650 .rpc_argp = &arg,
2651 .rpc_resp = &res,
2652 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002653 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654
Trond Myklebust011fff72010-04-16 16:22:49 -04002655 res.old_fattr = nfs_alloc_fattr();
2656 res.new_fattr = nfs_alloc_fattr();
2657 if (res.old_fattr == NULL || res.new_fattr == NULL)
2658 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
Trond Myklebust011fff72010-04-16 16:22:49 -04002660 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 if (!status) {
2662 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002663 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002665 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002667out:
2668 nfs_free_fattr(res.new_fattr);
2669 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 return status;
2671}
2672
2673static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2674 struct inode *new_dir, struct qstr *new_name)
2675{
2676 struct nfs4_exception exception = { };
2677 int err;
2678 do {
2679 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2680 _nfs4_proc_rename(old_dir, old_name,
2681 new_dir, new_name),
2682 &exception);
2683 } while (exception.retry);
2684 return err;
2685}
2686
2687static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2688{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002689 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 struct nfs4_link_arg arg = {
2691 .fh = NFS_FH(inode),
2692 .dir_fh = NFS_FH(dir),
2693 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002694 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002696 struct nfs4_link_res res = {
2697 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002698 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 struct rpc_message msg = {
2700 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2701 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002702 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002704 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705
Trond Myklebust136f2622010-04-16 16:22:49 -04002706 res.fattr = nfs_alloc_fattr();
2707 res.dir_attr = nfs_alloc_fattr();
2708 if (res.fattr == NULL || res.dir_attr == NULL)
2709 goto out;
2710
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002711 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002712 if (!status) {
2713 update_changeattr(dir, &res.cinfo);
2714 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002715 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002716 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002717out:
2718 nfs_free_fattr(res.dir_attr);
2719 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 return status;
2721}
2722
2723static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2724{
2725 struct nfs4_exception exception = { };
2726 int err;
2727 do {
2728 err = nfs4_handle_exception(NFS_SERVER(inode),
2729 _nfs4_proc_link(inode, dir, name),
2730 &exception);
2731 } while (exception.retry);
2732 return err;
2733}
2734
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002735struct nfs4_createdata {
2736 struct rpc_message msg;
2737 struct nfs4_create_arg arg;
2738 struct nfs4_create_res res;
2739 struct nfs_fh fh;
2740 struct nfs_fattr fattr;
2741 struct nfs_fattr dir_fattr;
2742};
2743
2744static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2745 struct qstr *name, struct iattr *sattr, u32 ftype)
2746{
2747 struct nfs4_createdata *data;
2748
2749 data = kzalloc(sizeof(*data), GFP_KERNEL);
2750 if (data != NULL) {
2751 struct nfs_server *server = NFS_SERVER(dir);
2752
2753 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2754 data->msg.rpc_argp = &data->arg;
2755 data->msg.rpc_resp = &data->res;
2756 data->arg.dir_fh = NFS_FH(dir);
2757 data->arg.server = server;
2758 data->arg.name = name;
2759 data->arg.attrs = sattr;
2760 data->arg.ftype = ftype;
2761 data->arg.bitmask = server->attr_bitmask;
2762 data->res.server = server;
2763 data->res.fh = &data->fh;
2764 data->res.fattr = &data->fattr;
2765 data->res.dir_fattr = &data->dir_fattr;
2766 nfs_fattr_init(data->res.fattr);
2767 nfs_fattr_init(data->res.dir_fattr);
2768 }
2769 return data;
2770}
2771
2772static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2773{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002774 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2775 &data->arg, &data->res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002776 if (status == 0) {
2777 update_changeattr(dir, &data->res.dir_cinfo);
2778 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2779 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2780 }
2781 return status;
2782}
2783
2784static void nfs4_free_createdata(struct nfs4_createdata *data)
2785{
2786 kfree(data);
2787}
2788
Chuck Lever4f390c12006-08-22 20:06:22 -04002789static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002790 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002792 struct nfs4_createdata *data;
2793 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
Chuck Lever94a6d752006-08-22 20:06:23 -04002795 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002796 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002797
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002798 status = -ENOMEM;
2799 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2800 if (data == NULL)
2801 goto out;
2802
2803 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2804 data->arg.u.symlink.pages = &page;
2805 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002807 status = nfs4_do_create(dir, dentry, data);
2808
2809 nfs4_free_createdata(data);
2810out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 return status;
2812}
2813
Chuck Lever4f390c12006-08-22 20:06:22 -04002814static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002815 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816{
2817 struct nfs4_exception exception = { };
2818 int err;
2819 do {
2820 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002821 _nfs4_proc_symlink(dir, dentry, page,
2822 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 &exception);
2824 } while (exception.retry);
2825 return err;
2826}
2827
2828static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2829 struct iattr *sattr)
2830{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002831 struct nfs4_createdata *data;
2832 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002834 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2835 if (data == NULL)
2836 goto out;
2837
2838 status = nfs4_do_create(dir, dentry, data);
2839
2840 nfs4_free_createdata(data);
2841out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 return status;
2843}
2844
2845static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2846 struct iattr *sattr)
2847{
2848 struct nfs4_exception exception = { };
2849 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002850
2851 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 do {
2853 err = nfs4_handle_exception(NFS_SERVER(dir),
2854 _nfs4_proc_mkdir(dir, dentry, sattr),
2855 &exception);
2856 } while (exception.retry);
2857 return err;
2858}
2859
2860static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002861 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862{
2863 struct inode *dir = dentry->d_inode;
2864 struct nfs4_readdir_arg args = {
2865 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002866 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 .pgbase = 0,
2868 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002869 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04002870 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 };
2872 struct nfs4_readdir_res res;
2873 struct rpc_message msg = {
2874 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2875 .rpc_argp = &args,
2876 .rpc_resp = &res,
2877 .rpc_cred = cred,
2878 };
2879 int status;
2880
Harvey Harrison3110ff82008-05-02 13:42:44 -07002881 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002882 dentry->d_parent->d_name.name,
2883 dentry->d_name.name,
2884 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2886 res.pgbase = args.pgbase;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002887 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
Trond Myklebustac396122010-11-15 20:26:22 -05002888 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05002890 status += args.pgbase;
2891 }
Trond Myklebustc4812992007-09-28 17:11:45 -04002892
2893 nfs_invalidate_atime(dir);
2894
Harvey Harrison3110ff82008-05-02 13:42:44 -07002895 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 return status;
2897}
2898
2899static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002900 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901{
2902 struct nfs4_exception exception = { };
2903 int err;
2904 do {
2905 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2906 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002907 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 &exception);
2909 } while (exception.retry);
2910 return err;
2911}
2912
2913static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2914 struct iattr *sattr, dev_t rdev)
2915{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002916 struct nfs4_createdata *data;
2917 int mode = sattr->ia_mode;
2918 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919
2920 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2921 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002922
2923 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2924 if (data == NULL)
2925 goto out;
2926
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002928 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002930 data->arg.ftype = NF4BLK;
2931 data->arg.u.device.specdata1 = MAJOR(rdev);
2932 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 }
2934 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002935 data->arg.ftype = NF4CHR;
2936 data->arg.u.device.specdata1 = MAJOR(rdev);
2937 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002940 status = nfs4_do_create(dir, dentry, data);
2941
2942 nfs4_free_createdata(data);
2943out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 return status;
2945}
2946
2947static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2948 struct iattr *sattr, dev_t rdev)
2949{
2950 struct nfs4_exception exception = { };
2951 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002952
2953 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 do {
2955 err = nfs4_handle_exception(NFS_SERVER(dir),
2956 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2957 &exception);
2958 } while (exception.retry);
2959 return err;
2960}
2961
2962static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2963 struct nfs_fsstat *fsstat)
2964{
2965 struct nfs4_statfs_arg args = {
2966 .fh = fhandle,
2967 .bitmask = server->attr_bitmask,
2968 };
Benny Halevy24ad1482009-04-01 09:21:56 -04002969 struct nfs4_statfs_res res = {
2970 .fsstat = fsstat,
2971 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 struct rpc_message msg = {
2973 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2974 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04002975 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 };
2977
Trond Myklebust0e574af2005-10-27 22:12:38 -04002978 nfs_fattr_init(fsstat->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002979 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980}
2981
2982static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2983{
2984 struct nfs4_exception exception = { };
2985 int err;
2986 do {
2987 err = nfs4_handle_exception(server,
2988 _nfs4_proc_statfs(server, fhandle, fsstat),
2989 &exception);
2990 } while (exception.retry);
2991 return err;
2992}
2993
2994static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
2995 struct nfs_fsinfo *fsinfo)
2996{
2997 struct nfs4_fsinfo_arg args = {
2998 .fh = fhandle,
2999 .bitmask = server->attr_bitmask,
3000 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003001 struct nfs4_fsinfo_res res = {
3002 .fsinfo = fsinfo,
3003 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 struct rpc_message msg = {
3005 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3006 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003007 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 };
3009
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003010 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011}
3012
3013static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3014{
3015 struct nfs4_exception exception = { };
3016 int err;
3017
3018 do {
3019 err = nfs4_handle_exception(server,
3020 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3021 &exception);
3022 } while (exception.retry);
3023 return err;
3024}
3025
3026static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3027{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003028 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3030}
3031
3032static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3033 struct nfs_pathconf *pathconf)
3034{
3035 struct nfs4_pathconf_arg args = {
3036 .fh = fhandle,
3037 .bitmask = server->attr_bitmask,
3038 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003039 struct nfs4_pathconf_res res = {
3040 .pathconf = pathconf,
3041 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 struct rpc_message msg = {
3043 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3044 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003045 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 };
3047
3048 /* None of the pathconf attributes are mandatory to implement */
3049 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3050 memset(pathconf, 0, sizeof(*pathconf));
3051 return 0;
3052 }
3053
Trond Myklebust0e574af2005-10-27 22:12:38 -04003054 nfs_fattr_init(pathconf->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003055 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056}
3057
3058static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3059 struct nfs_pathconf *pathconf)
3060{
3061 struct nfs4_exception exception = { };
3062 int err;
3063
3064 do {
3065 err = nfs4_handle_exception(server,
3066 _nfs4_proc_pathconf(server, fhandle, pathconf),
3067 &exception);
3068 } while (exception.retry);
3069 return err;
3070}
3071
Trond Myklebustec06c092006-03-20 13:44:27 -05003072static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073{
Trond Myklebustec06c092006-03-20 13:44:27 -05003074 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003076 dprintk("--> %s\n", __func__);
3077
Trond Myklebust14516c32010-07-31 14:29:06 -04003078 if (!nfs4_sequence_done(task, &data->res.seq_res))
3079 return -EAGAIN;
Andy Adamsonf11c88a2009-04-01 09:22:25 -04003080
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003081 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003082 nfs_restart_rpc(task, server->nfs_client);
Trond Myklebustec06c092006-03-20 13:44:27 -05003083 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003085
3086 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003088 renew_lease(server, data->timestamp);
3089 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090}
3091
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003092static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 data->timestamp = jiffies;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003095 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096}
3097
Trond Myklebust788e7a82006-03-20 13:44:27 -05003098static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 struct inode *inode = data->inode;
3101
Trond Myklebust14516c32010-07-31 14:29:06 -04003102 if (!nfs4_sequence_done(task, &data->res.seq_res))
3103 return -EAGAIN;
Andy Adamsondef6ed72009-04-01 09:22:26 -04003104
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003105 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003106 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003107 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003109 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003111 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003112 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003113 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114}
3115
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003116static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003118 struct nfs_server *server = NFS_SERVER(data->inode);
3119
Trond Myklebusta65318b2009-03-11 14:10:28 -04003120 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003121 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 data->timestamp = jiffies;
3123
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003124 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125}
3126
Trond Myklebust788e7a82006-03-20 13:44:27 -05003127static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 struct inode *inode = data->inode;
3130
Trond Myklebust14516c32010-07-31 14:29:06 -04003131 if (!nfs4_sequence_done(task, &data->res.seq_res))
3132 return -EAGAIN;
3133
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003134 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003135 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003136 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003138 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003139 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140}
3141
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003142static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003144 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145
Trond Myklebusta65318b2009-03-11 14:10:28 -04003146 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003147 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003148 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149}
3150
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003151struct nfs4_renewdata {
3152 struct nfs_client *client;
3153 unsigned long timestamp;
3154};
3155
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156/*
3157 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3158 * standalone procedure for queueing an asynchronous RENEW.
3159 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003160static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003161{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003162 struct nfs4_renewdata *data = calldata;
3163 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003164
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003165 if (atomic_read(&clp->cl_count) > 1)
3166 nfs4_schedule_state_renewal(clp);
3167 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003168 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003169}
3170
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003171static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003173 struct nfs4_renewdata *data = calldata;
3174 struct nfs_client *clp = data->client;
3175 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176
3177 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003178 /* Unless we're shutting down, schedule state recovery! */
3179 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003180 nfs4_schedule_lease_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 return;
3182 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003183 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184}
3185
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003186static const struct rpc_call_ops nfs4_renew_ops = {
3187 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003188 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003189};
3190
David Howellsadfa6f92006-08-22 20:06:08 -04003191int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192{
3193 struct rpc_message msg = {
3194 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3195 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003196 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003198 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003200 if (!atomic_inc_not_zero(&clp->cl_count))
3201 return -EIO;
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003202 data = kmalloc(sizeof(*data), GFP_KERNEL);
3203 if (data == NULL)
3204 return -ENOMEM;
3205 data->client = clp;
3206 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003208 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209}
3210
David Howellsadfa6f92006-08-22 20:06:08 -04003211int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212{
3213 struct rpc_message msg = {
3214 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3215 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003216 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 };
3218 unsigned long now = jiffies;
3219 int status;
3220
3221 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3222 if (status < 0)
3223 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003224 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 return 0;
3226}
3227
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003228static inline int nfs4_server_supports_acls(struct nfs_server *server)
3229{
3230 return (server->caps & NFS_CAP_ACLS)
3231 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3232 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3233}
3234
3235/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3236 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3237 * the stack.
3238 */
3239#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3240
3241static void buf_to_pages(const void *buf, size_t buflen,
3242 struct page **pages, unsigned int *pgbase)
3243{
3244 const void *p = buf;
3245
3246 *pgbase = offset_in_page(buf);
3247 p -= *pgbase;
3248 while (p < buf + buflen) {
3249 *(pages++) = virt_to_page(p);
3250 p += PAGE_CACHE_SIZE;
3251 }
3252}
3253
Neil Hormane9e3d722011-03-04 19:26:03 -05003254static int buf_to_pages_noslab(const void *buf, size_t buflen,
3255 struct page **pages, unsigned int *pgbase)
3256{
3257 struct page *newpage, **spages;
3258 int rc = 0;
3259 size_t len;
3260 spages = pages;
3261
3262 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003263 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003264 newpage = alloc_page(GFP_KERNEL);
3265
3266 if (newpage == NULL)
3267 goto unwind;
3268 memcpy(page_address(newpage), buf, len);
3269 buf += len;
3270 buflen -= len;
3271 *pages++ = newpage;
3272 rc++;
3273 } while (buflen != 0);
3274
3275 return rc;
3276
3277unwind:
3278 for(; rc > 0; rc--)
3279 __free_page(spages[rc-1]);
3280 return -ENOMEM;
3281}
3282
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003283struct nfs4_cached_acl {
3284 int cached;
3285 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003286 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003287};
3288
3289static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003290{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003291 struct nfs_inode *nfsi = NFS_I(inode);
3292
3293 spin_lock(&inode->i_lock);
3294 kfree(nfsi->nfs4_acl);
3295 nfsi->nfs4_acl = acl;
3296 spin_unlock(&inode->i_lock);
3297}
3298
3299static void nfs4_zap_acl_attr(struct inode *inode)
3300{
3301 nfs4_set_cached_acl(inode, NULL);
3302}
3303
3304static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3305{
3306 struct nfs_inode *nfsi = NFS_I(inode);
3307 struct nfs4_cached_acl *acl;
3308 int ret = -ENOENT;
3309
3310 spin_lock(&inode->i_lock);
3311 acl = nfsi->nfs4_acl;
3312 if (acl == NULL)
3313 goto out;
3314 if (buf == NULL) /* user is just asking for length */
3315 goto out_len;
3316 if (acl->cached == 0)
3317 goto out;
3318 ret = -ERANGE; /* see getxattr(2) man page */
3319 if (acl->len > buflen)
3320 goto out;
3321 memcpy(buf, acl->data, acl->len);
3322out_len:
3323 ret = acl->len;
3324out:
3325 spin_unlock(&inode->i_lock);
3326 return ret;
3327}
3328
3329static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3330{
3331 struct nfs4_cached_acl *acl;
3332
3333 if (buf && acl_len <= PAGE_SIZE) {
3334 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3335 if (acl == NULL)
3336 goto out;
3337 acl->cached = 1;
3338 memcpy(acl->data, buf, acl_len);
3339 } else {
3340 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3341 if (acl == NULL)
3342 goto out;
3343 acl->cached = 0;
3344 }
3345 acl->len = acl_len;
3346out:
3347 nfs4_set_cached_acl(inode, acl);
3348}
3349
Trond Myklebust16b42892006-08-24 12:27:15 -04003350static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003351{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003352 struct page *pages[NFS4ACL_MAXPAGES];
3353 struct nfs_getaclargs args = {
3354 .fh = NFS_FH(inode),
3355 .acl_pages = pages,
3356 .acl_len = buflen,
3357 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003358 struct nfs_getaclres res = {
3359 .acl_len = buflen,
3360 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003361 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003362 struct rpc_message msg = {
3363 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3364 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003365 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003366 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003367 struct page *localpage = NULL;
3368 int ret;
3369
3370 if (buflen < PAGE_SIZE) {
3371 /* As long as we're doing a round trip to the server anyway,
3372 * let's be prepared for a page of acl data. */
3373 localpage = alloc_page(GFP_KERNEL);
3374 resp_buf = page_address(localpage);
3375 if (localpage == NULL)
3376 return -ENOMEM;
3377 args.acl_pages[0] = localpage;
3378 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003379 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003380 } else {
3381 resp_buf = buf;
3382 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3383 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003384 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003385 if (ret)
3386 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003387 if (res.acl_len > args.acl_len)
3388 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003389 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003390 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003391 if (buf) {
3392 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003393 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003394 goto out_free;
3395 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003396 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003397 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003398 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003399out_free:
3400 if (localpage)
3401 __free_page(localpage);
3402 return ret;
3403}
3404
Trond Myklebust16b42892006-08-24 12:27:15 -04003405static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3406{
3407 struct nfs4_exception exception = { };
3408 ssize_t ret;
3409 do {
3410 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3411 if (ret >= 0)
3412 break;
3413 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3414 } while (exception.retry);
3415 return ret;
3416}
3417
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003418static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3419{
3420 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003421 int ret;
3422
3423 if (!nfs4_server_supports_acls(server))
3424 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003425 ret = nfs_revalidate_inode(server, inode);
3426 if (ret < 0)
3427 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003428 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3429 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003430 ret = nfs4_read_cached_acl(inode, buf, buflen);
3431 if (ret != -ENOENT)
3432 return ret;
3433 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003434}
3435
Trond Myklebust16b42892006-08-24 12:27:15 -04003436static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003437{
3438 struct nfs_server *server = NFS_SERVER(inode);
3439 struct page *pages[NFS4ACL_MAXPAGES];
3440 struct nfs_setaclargs arg = {
3441 .fh = NFS_FH(inode),
3442 .acl_pages = pages,
3443 .acl_len = buflen,
3444 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003445 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003446 struct rpc_message msg = {
3447 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3448 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003449 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003450 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003451 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003452
3453 if (!nfs4_server_supports_acls(server))
3454 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003455 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3456 if (i < 0)
3457 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003458 nfs_inode_return_delegation(inode);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003459 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003460
3461 /*
3462 * Free each page after tx, so the only ref left is
3463 * held by the network stack
3464 */
3465 for (; i > 0; i--)
3466 put_page(pages[i-1]);
3467
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003468 /*
3469 * Acl update can result in inode attribute update.
3470 * so mark the attribute cache invalid.
3471 */
3472 spin_lock(&inode->i_lock);
3473 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3474 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003475 nfs_access_zap_cache(inode);
3476 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003477 return ret;
3478}
3479
Trond Myklebust16b42892006-08-24 12:27:15 -04003480static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3481{
3482 struct nfs4_exception exception = { };
3483 int err;
3484 do {
3485 err = nfs4_handle_exception(NFS_SERVER(inode),
3486 __nfs4_proc_set_acl(inode, buf, buflen),
3487 &exception);
3488 } while (exception.retry);
3489 return err;
3490}
3491
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003493nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003495 struct nfs_client *clp = server->nfs_client;
3496
3497 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 return 0;
3499 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003500 case -NFS4ERR_ADMIN_REVOKED:
3501 case -NFS4ERR_BAD_STATEID:
3502 case -NFS4ERR_OPENMODE:
3503 if (state == NULL)
3504 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003505 nfs4_schedule_stateid_recovery(server, state);
3506 goto wait_on_recovery;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003508 case -NFS4ERR_STALE_CLIENTID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 case -NFS4ERR_EXPIRED:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003510 nfs4_schedule_lease_recovery(clp);
3511 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003512#if defined(CONFIG_NFS_V4_1)
3513 case -NFS4ERR_BADSESSION:
3514 case -NFS4ERR_BADSLOT:
3515 case -NFS4ERR_BAD_HIGH_SLOT:
3516 case -NFS4ERR_DEADSESSION:
3517 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3518 case -NFS4ERR_SEQ_FALSE_RETRY:
3519 case -NFS4ERR_SEQ_MISORDERED:
3520 dprintk("%s ERROR %d, Reset session\n", __func__,
3521 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003522 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -04003523 task->tk_status = 0;
3524 return -EAGAIN;
3525#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003527 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003528 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003529 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3531 task->tk_status = 0;
3532 return -EAGAIN;
3533 case -NFS4ERR_OLD_STATEID:
3534 task->tk_status = 0;
3535 return -EAGAIN;
3536 }
3537 task->tk_status = nfs4_map_errors(task->tk_status);
3538 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003539wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003540 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003541 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3542 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3543 task->tk_status = 0;
3544 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545}
3546
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003547int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3548 unsigned short port, struct rpc_cred *cred,
3549 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550{
3551 nfs4_verifier sc_verifier;
3552 struct nfs4_setclientid setclientid = {
3553 .sc_verifier = &sc_verifier,
3554 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003555 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 };
3557 struct rpc_message msg = {
3558 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3559 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003560 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003561 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 };
Al Virobc4785c2006-10-19 23:28:51 -07003563 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 int loop = 0;
3565 int status;
3566
Al Virobc4785c2006-10-19 23:28:51 -07003567 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3569 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3570
3571 for(;;) {
3572 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003573 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003574 clp->cl_ipaddr,
3575 rpc_peeraddr2str(clp->cl_rpcclient,
3576 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003577 rpc_peeraddr2str(clp->cl_rpcclient,
3578 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003579 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 clp->cl_id_uniquifier);
3581 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003582 sizeof(setclientid.sc_netid),
3583 rpc_peeraddr2str(clp->cl_rpcclient,
3584 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003586 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 clp->cl_ipaddr, port >> 8, port & 255);
3588
3589 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3590 if (status != -NFS4ERR_CLID_INUSE)
3591 break;
3592 if (signalled())
3593 break;
3594 if (loop++ & 1)
J. Bruce Fields611c96c2010-12-13 19:05:46 -05003595 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 else
3597 if (++clp->cl_id_uniquifier == 0)
3598 break;
3599 }
3600 return status;
3601}
3602
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003603static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3604 struct nfs4_setclientid_res *arg,
3605 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606{
3607 struct nfs_fsinfo fsinfo;
3608 struct rpc_message msg = {
3609 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003610 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003612 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 };
3614 unsigned long now;
3615 int status;
3616
3617 now = jiffies;
3618 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3619 if (status == 0) {
3620 spin_lock(&clp->cl_lock);
3621 clp->cl_lease_time = fsinfo.lease_time * HZ;
3622 clp->cl_last_renewal = now;
3623 spin_unlock(&clp->cl_lock);
3624 }
3625 return status;
3626}
3627
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003628int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3629 struct nfs4_setclientid_res *arg,
3630 struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003631{
Benny Halevy77e03672008-06-24 20:25:57 +03003632 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003633 int err;
3634 do {
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003635 err = _nfs4_proc_setclientid_confirm(clp, arg, cred);
Trond Myklebust51581f32006-03-20 13:44:47 -05003636 switch (err) {
3637 case 0:
3638 return err;
3639 case -NFS4ERR_RESOURCE:
3640 /* The IBM lawyers misread another document! */
3641 case -NFS4ERR_DELAY:
3642 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3643 }
3644 } while (err == 0);
3645 return err;
3646}
3647
Trond Myklebustfe650402006-01-03 09:55:18 +01003648struct nfs4_delegreturndata {
3649 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003650 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003651 struct nfs_fh fh;
3652 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003653 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003654 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003655 int rpc_status;
3656};
3657
Trond Myklebustfe650402006-01-03 09:55:18 +01003658static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3659{
3660 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003661
Trond Myklebust14516c32010-07-31 14:29:06 -04003662 if (!nfs4_sequence_done(task, &data->res.seq_res))
3663 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003664
Ricardo Labiaga79708862009-12-07 09:23:21 -05003665 switch (task->tk_status) {
3666 case -NFS4ERR_STALE_STATEID:
3667 case -NFS4ERR_EXPIRED:
3668 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003669 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003670 break;
3671 default:
3672 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3673 -EAGAIN) {
3674 nfs_restart_rpc(task, data->res.server->nfs_client);
3675 return;
3676 }
3677 }
3678 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003679}
3680
3681static void nfs4_delegreturn_release(void *calldata)
3682{
Trond Myklebustfe650402006-01-03 09:55:18 +01003683 kfree(calldata);
3684}
3685
Andy Adamson938e1012009-04-01 09:22:28 -04003686#if defined(CONFIG_NFS_V4_1)
3687static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3688{
3689 struct nfs4_delegreturndata *d_data;
3690
3691 d_data = (struct nfs4_delegreturndata *)data;
3692
Trond Myklebust035168ab2010-06-16 09:52:26 -04003693 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003694 &d_data->args.seq_args,
3695 &d_data->res.seq_res, 1, task))
3696 return;
3697 rpc_call_start(task);
3698}
3699#endif /* CONFIG_NFS_V4_1 */
3700
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003701static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003702#if defined(CONFIG_NFS_V4_1)
3703 .rpc_call_prepare = nfs4_delegreturn_prepare,
3704#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003705 .rpc_call_done = nfs4_delegreturn_done,
3706 .rpc_release = nfs4_delegreturn_release,
3707};
3708
Trond Myklebuste6f81072008-01-24 18:14:34 -05003709static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003710{
3711 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003712 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003713 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003714 struct rpc_message msg = {
3715 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3716 .rpc_cred = cred,
3717 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003718 struct rpc_task_setup task_setup_data = {
3719 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003720 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003721 .callback_ops = &nfs4_delegreturn_ops,
3722 .flags = RPC_TASK_ASYNC,
3723 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003724 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003725
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003726 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003727 if (data == NULL)
3728 return -ENOMEM;
3729 data->args.fhandle = &data->fh;
3730 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003731 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003732 nfs_copy_fh(&data->fh, NFS_FH(inode));
3733 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003734 data->res.fattr = &data->fattr;
3735 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003736 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003737 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003738 data->rpc_status = 0;
3739
Trond Myklebustc970aa82007-07-14 15:39:59 -04003740 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05003741 msg.rpc_argp = &data->args;
3742 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003743 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003744 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003745 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003746 if (!issync)
3747 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003748 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003749 if (status != 0)
3750 goto out;
3751 status = data->rpc_status;
3752 if (status != 0)
3753 goto out;
3754 nfs_refresh_inode(inode, &data->fattr);
3755out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003756 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003757 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758}
3759
Trond Myklebuste6f81072008-01-24 18:14:34 -05003760int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761{
3762 struct nfs_server *server = NFS_SERVER(inode);
3763 struct nfs4_exception exception = { };
3764 int err;
3765 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003766 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 switch (err) {
3768 case -NFS4ERR_STALE_STATEID:
3769 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 case 0:
3771 return 0;
3772 }
3773 err = nfs4_handle_exception(server, err, &exception);
3774 } while (exception.retry);
3775 return err;
3776}
3777
3778#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3779#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3780
3781/*
3782 * sleep, with exponential backoff, and retry the LOCK operation.
3783 */
3784static unsigned long
3785nfs4_set_lock_task_retry(unsigned long timeout)
3786{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003787 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 timeout <<= 1;
3789 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3790 return NFS4_LOCK_MAXTIMEOUT;
3791 return timeout;
3792}
3793
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3795{
3796 struct inode *inode = state->inode;
3797 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003798 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003799 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003801 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003803 struct nfs_lockt_res res = {
3804 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 };
3806 struct rpc_message msg = {
3807 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3808 .rpc_argp = &arg,
3809 .rpc_resp = &res,
3810 .rpc_cred = state->owner->so_cred,
3811 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 struct nfs4_lock_state *lsp;
3813 int status;
3814
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003815 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003816 status = nfs4_set_lock_state(state, request);
3817 if (status != 0)
3818 goto out;
3819 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003820 arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05003821 arg.lock_owner.s_dev = server->s_dev;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003822 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003823 switch (status) {
3824 case 0:
3825 request->fl_type = F_UNLCK;
3826 break;
3827 case -NFS4ERR_DENIED:
3828 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003830 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003831out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 return status;
3833}
3834
3835static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3836{
3837 struct nfs4_exception exception = { };
3838 int err;
3839
3840 do {
3841 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3842 _nfs4_proc_getlk(state, cmd, request),
3843 &exception);
3844 } while (exception.retry);
3845 return err;
3846}
3847
3848static int do_vfs_lock(struct file *file, struct file_lock *fl)
3849{
3850 int res = 0;
3851 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3852 case FL_POSIX:
3853 res = posix_lock_file_wait(file, fl);
3854 break;
3855 case FL_FLOCK:
3856 res = flock_lock_file_wait(file, fl);
3857 break;
3858 default:
3859 BUG();
3860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 return res;
3862}
3863
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003864struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003865 struct nfs_locku_args arg;
3866 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003867 struct nfs4_lock_state *lsp;
3868 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003869 struct file_lock fl;
3870 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003871 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003872};
3873
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003874static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3875 struct nfs_open_context *ctx,
3876 struct nfs4_lock_state *lsp,
3877 struct nfs_seqid *seqid)
3878{
3879 struct nfs4_unlockdata *p;
3880 struct inode *inode = lsp->ls_state->inode;
3881
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003882 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003883 if (p == NULL)
3884 return NULL;
3885 p->arg.fh = NFS_FH(inode);
3886 p->arg.fl = &p->fl;
3887 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003888 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003889 p->arg.stateid = &lsp->ls_stateid;
3890 p->lsp = lsp;
3891 atomic_inc(&lsp->ls_count);
3892 /* Ensure we don't close file until we're done freeing locks! */
3893 p->ctx = get_nfs_open_context(ctx);
3894 memcpy(&p->fl, fl, sizeof(p->fl));
3895 p->server = NFS_SERVER(inode);
3896 return p;
3897}
3898
Trond Myklebust06f814a2006-01-03 09:55:07 +01003899static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003900{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003901 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003902 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003903 nfs4_put_lock_state(calldata->lsp);
3904 put_nfs_open_context(calldata->ctx);
3905 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003906}
3907
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003908static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003909{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003910 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003911
Trond Myklebust14516c32010-07-31 14:29:06 -04003912 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3913 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003914 switch (task->tk_status) {
3915 case 0:
3916 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003917 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003918 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003919 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003920 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003921 case -NFS4ERR_BAD_STATEID:
3922 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003923 case -NFS4ERR_STALE_STATEID:
3924 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003925 break;
3926 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003927 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebust0110ee12009-12-07 09:00:24 -05003928 nfs_restart_rpc(task,
Trond Myklebustd61e6122009-12-05 19:32:19 -05003929 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003930 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003931}
3932
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003933static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003934{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003935 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003937 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003938 return;
3939 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003940 /* Note: exit _without_ running nfs4_locku_done */
3941 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003942 return;
3943 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003944 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04003945 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04003946 &calldata->arg.seq_args,
3947 &calldata->res.seq_res, 1, task))
3948 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003949 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950}
3951
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003952static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003953 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003954 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003955 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003956};
3957
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003958static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3959 struct nfs_open_context *ctx,
3960 struct nfs4_lock_state *lsp,
3961 struct nfs_seqid *seqid)
3962{
3963 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003964 struct rpc_message msg = {
3965 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
3966 .rpc_cred = ctx->cred,
3967 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003968 struct rpc_task_setup task_setup_data = {
3969 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04003970 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003971 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003972 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003973 .flags = RPC_TASK_ASYNC,
3974 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003975
Frank Filz137d6ac2007-07-09 15:32:29 -07003976 /* Ensure this is an unlock - when canceling a lock, the
3977 * canceled lock is passed in, and it won't be an unlock.
3978 */
3979 fl->fl_type = F_UNLCK;
3980
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003981 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
3982 if (data == NULL) {
3983 nfs_free_seqid(seqid);
3984 return ERR_PTR(-ENOMEM);
3985 }
3986
Trond Myklebust1174dd12010-12-21 10:52:24 -05003987 msg.rpc_argp = &data->arg;
3988 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003989 task_setup_data.callback_data = data;
3990 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003991}
3992
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
3994{
Trond Myklebust19e03c52008-12-23 15:21:44 -05003995 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003996 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003997 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01003998 struct rpc_task *task;
3999 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004000 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001
Trond Myklebust9b073572006-06-29 16:38:34 -04004002 status = nfs4_set_lock_state(state, request);
4003 /* Unlock _before_ we do the RPC call */
4004 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004005 down_read(&nfsi->rwsem);
4006 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4007 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004008 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004009 }
4010 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004011 if (status != 0)
4012 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004013 /* Is this a delegated lock? */
4014 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004015 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004016 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004017 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004018 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004019 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004020 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004021 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004022 status = PTR_ERR(task);
4023 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004024 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004025 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004026 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004027out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004028 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004029 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030}
4031
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004032struct nfs4_lockdata {
4033 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004034 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004035 struct nfs4_lock_state *lsp;
4036 struct nfs_open_context *ctx;
4037 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004038 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004039 int rpc_status;
4040 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004041 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004042};
4043
4044static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004045 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4046 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004047{
4048 struct nfs4_lockdata *p;
4049 struct inode *inode = lsp->ls_state->inode;
4050 struct nfs_server *server = NFS_SERVER(inode);
4051
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004052 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004053 if (p == NULL)
4054 return NULL;
4055
4056 p->arg.fh = NFS_FH(inode);
4057 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004058 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004059 if (p->arg.open_seqid == NULL)
4060 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004061 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004062 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004063 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004064 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004065 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004066 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004067 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004068 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004069 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004070 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004071 atomic_inc(&lsp->ls_count);
4072 p->ctx = get_nfs_open_context(ctx);
4073 memcpy(&p->fl, fl, sizeof(p->fl));
4074 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004075out_free_seqid:
4076 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004077out_free:
4078 kfree(p);
4079 return NULL;
4080}
4081
4082static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4083{
4084 struct nfs4_lockdata *data = calldata;
4085 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086
Harvey Harrison3110ff82008-05-02 13:42:44 -07004087 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004088 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4089 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004090 /* Do we need to do an open_to_lock_owner? */
4091 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004092 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4093 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004094 data->arg.open_stateid = &state->stateid;
4095 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004096 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004097 } else
4098 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004099 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04004100 if (nfs4_setup_sequence(data->server,
4101 &data->arg.seq_args,
Andy Adamson66179ef2009-04-01 09:22:22 -04004102 &data->res.seq_res, 1, task))
4103 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004104 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004105 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004106}
4107
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004108static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4109{
4110 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4111 nfs4_lock_prepare(task, calldata);
4112}
4113
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004114static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4115{
4116 struct nfs4_lockdata *data = calldata;
4117
Harvey Harrison3110ff82008-05-02 13:42:44 -07004118 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004119
Trond Myklebust14516c32010-07-31 14:29:06 -04004120 if (!nfs4_sequence_done(task, &data->res.seq_res))
4121 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004122
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004123 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004124 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004125 if (data->rpc_status == 0)
4126 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4127 else
4128 goto out;
4129 }
4130 if (data->rpc_status == 0) {
4131 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4132 sizeof(data->lsp->ls_stateid.data));
4133 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04004134 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004135 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004136out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004137 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004138}
4139
4140static void nfs4_lock_release(void *calldata)
4141{
4142 struct nfs4_lockdata *data = calldata;
4143
Harvey Harrison3110ff82008-05-02 13:42:44 -07004144 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004145 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004146 if (data->cancelled != 0) {
4147 struct rpc_task *task;
4148 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4149 data->arg.lock_seqid);
4150 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004151 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004152 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004153 } else
4154 nfs_free_seqid(data->arg.lock_seqid);
4155 nfs4_put_lock_state(data->lsp);
4156 put_nfs_open_context(data->ctx);
4157 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004158 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004159}
4160
4161static const struct rpc_call_ops nfs4_lock_ops = {
4162 .rpc_call_prepare = nfs4_lock_prepare,
4163 .rpc_call_done = nfs4_lock_done,
4164 .rpc_release = nfs4_lock_release,
4165};
4166
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004167static const struct rpc_call_ops nfs4_recover_lock_ops = {
4168 .rpc_call_prepare = nfs4_recover_lock_prepare,
4169 .rpc_call_done = nfs4_lock_done,
4170 .rpc_release = nfs4_lock_release,
4171};
4172
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004173static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4174{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004175 switch (error) {
4176 case -NFS4ERR_ADMIN_REVOKED:
4177 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004178 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004179 if (new_lock_owner != 0 ||
4180 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004181 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004182 break;
4183 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004184 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004185 case -NFS4ERR_EXPIRED:
4186 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004187 };
4188}
4189
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004190static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004191{
4192 struct nfs4_lockdata *data;
4193 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004194 struct rpc_message msg = {
4195 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4196 .rpc_cred = state->owner->so_cred,
4197 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004198 struct rpc_task_setup task_setup_data = {
4199 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004200 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004201 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004202 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004203 .flags = RPC_TASK_ASYNC,
4204 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004205 int ret;
4206
Harvey Harrison3110ff82008-05-02 13:42:44 -07004207 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004208 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004209 fl->fl_u.nfs4_fl.owner,
4210 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004211 if (data == NULL)
4212 return -ENOMEM;
4213 if (IS_SETLKW(cmd))
4214 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004215 if (recovery_type > NFS_LOCK_NEW) {
4216 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004217 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004218 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4219 }
Trond Myklebust1174dd12010-12-21 10:52:24 -05004220 msg.rpc_argp = &data->arg;
4221 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004222 task_setup_data.callback_data = data;
4223 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004224 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004225 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004226 ret = nfs4_wait_for_completion_rpc_task(task);
4227 if (ret == 0) {
4228 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004229 if (ret)
4230 nfs4_handle_setlk_error(data->server, data->lsp,
4231 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004232 } else
4233 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004234 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004235 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004236 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237}
4238
4239static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4240{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004241 struct nfs_server *server = NFS_SERVER(state->inode);
4242 struct nfs4_exception exception = { };
4243 int err;
4244
4245 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004246 /* Cache the lock if possible... */
4247 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4248 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004249 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004250 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004251 break;
4252 nfs4_handle_exception(server, err, &exception);
4253 } while (exception.retry);
4254 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255}
4256
4257static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4258{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004259 struct nfs_server *server = NFS_SERVER(state->inode);
4260 struct nfs4_exception exception = { };
4261 int err;
4262
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004263 err = nfs4_set_lock_state(state, request);
4264 if (err != 0)
4265 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004266 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004267 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4268 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004269 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004270 switch (err) {
4271 default:
4272 goto out;
4273 case -NFS4ERR_GRACE:
4274 case -NFS4ERR_DELAY:
4275 nfs4_handle_exception(server, err, &exception);
4276 err = 0;
4277 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004278 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004279out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004280 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281}
4282
4283static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4284{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004285 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004286 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004287 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004289 if ((fl_flags & FL_POSIX) &&
4290 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4291 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004292 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004293 status = nfs4_set_lock_state(state, request);
4294 if (status != 0)
4295 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004296 request->fl_flags |= FL_ACCESS;
4297 status = do_vfs_lock(request->fl_file, request);
4298 if (status < 0)
4299 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004300 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004301 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004302 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004303 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004304 request->fl_flags = fl_flags & ~FL_SLEEP;
4305 status = do_vfs_lock(request->fl_file, request);
4306 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004307 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004308 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004309 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004310 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004311 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004312 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004313 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004314 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004315out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004316 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004317out:
4318 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 return status;
4320}
4321
4322static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4323{
4324 struct nfs4_exception exception = { };
4325 int err;
4326
4327 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004328 err = _nfs4_proc_setlk(state, cmd, request);
4329 if (err == -NFS4ERR_DENIED)
4330 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004332 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 } while (exception.retry);
4334 return err;
4335}
4336
4337static int
4338nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4339{
4340 struct nfs_open_context *ctx;
4341 struct nfs4_state *state;
4342 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4343 int status;
4344
4345 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004346 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 state = ctx->state;
4348
4349 if (request->fl_start < 0 || request->fl_end < 0)
4350 return -EINVAL;
4351
Trond Myklebustd9531262009-07-21 19:22:38 -04004352 if (IS_GETLK(cmd)) {
4353 if (state != NULL)
4354 return nfs4_proc_getlk(state, F_GETLK, request);
4355 return 0;
4356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357
4358 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4359 return -EINVAL;
4360
Trond Myklebustd9531262009-07-21 19:22:38 -04004361 if (request->fl_type == F_UNLCK) {
4362 if (state != NULL)
4363 return nfs4_proc_unlck(state, cmd, request);
4364 return 0;
4365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366
Trond Myklebustd9531262009-07-21 19:22:38 -04004367 if (state == NULL)
4368 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 do {
4370 status = nfs4_proc_setlk(state, cmd, request);
4371 if ((status != -EAGAIN) || IS_SETLK(cmd))
4372 break;
4373 timeout = nfs4_set_lock_task_retry(timeout);
4374 status = -ERESTARTSYS;
4375 if (signalled())
4376 break;
4377 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 return status;
4379}
4380
Trond Myklebust888e6942005-11-04 15:38:11 -05004381int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4382{
4383 struct nfs_server *server = NFS_SERVER(state->inode);
4384 struct nfs4_exception exception = { };
4385 int err;
4386
4387 err = nfs4_set_lock_state(state, fl);
4388 if (err != 0)
4389 goto out;
4390 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004391 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004392 switch (err) {
4393 default:
4394 printk(KERN_ERR "%s: unhandled error %d.\n",
4395 __func__, err);
4396 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004397 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004398 goto out;
4399 case -NFS4ERR_EXPIRED:
4400 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004401 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004402 nfs4_schedule_lease_recovery(server->nfs_client);
4403 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004404 case -NFS4ERR_BADSESSION:
4405 case -NFS4ERR_BADSLOT:
4406 case -NFS4ERR_BAD_HIGH_SLOT:
4407 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4408 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004409 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004410 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004411 case -ERESTARTSYS:
4412 /*
4413 * The show must go on: exit, but mark the
4414 * stateid as needing recovery.
4415 */
4416 case -NFS4ERR_ADMIN_REVOKED:
4417 case -NFS4ERR_BAD_STATEID:
4418 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004419 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004420 err = 0;
4421 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004422 case -EKEYEXPIRED:
4423 /*
4424 * User RPCSEC_GSS context has expired.
4425 * We cannot recover this stateid now, so
4426 * skip it and allow recovery thread to
4427 * proceed.
4428 */
4429 err = 0;
4430 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004431 case -ENOMEM:
4432 case -NFS4ERR_DENIED:
4433 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4434 err = 0;
4435 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004436 case -NFS4ERR_DELAY:
4437 break;
4438 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004439 err = nfs4_handle_exception(server, err, &exception);
4440 } while (exception.retry);
4441out:
4442 return err;
4443}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004444
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004445static void nfs4_release_lockowner_release(void *calldata)
4446{
4447 kfree(calldata);
4448}
4449
4450const struct rpc_call_ops nfs4_release_lockowner_ops = {
4451 .rpc_release = nfs4_release_lockowner_release,
4452};
4453
4454void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4455{
4456 struct nfs_server *server = lsp->ls_state->owner->so_server;
4457 struct nfs_release_lockowner_args *args;
4458 struct rpc_message msg = {
4459 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4460 };
4461
4462 if (server->nfs_client->cl_mvops->minor_version != 0)
4463 return;
4464 args = kmalloc(sizeof(*args), GFP_NOFS);
4465 if (!args)
4466 return;
4467 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4468 args->lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004469 args->lock_owner.s_dev = server->s_dev;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004470 msg.rpc_argp = args;
4471 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4472}
4473
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004474#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4475
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004476static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4477 const void *buf, size_t buflen,
4478 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004479{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004480 if (strcmp(key, "") != 0)
4481 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004482
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004483 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004484}
4485
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004486static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4487 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004488{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004489 if (strcmp(key, "") != 0)
4490 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004491
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004492 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004493}
4494
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004495static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4496 size_t list_len, const char *name,
4497 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004498{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004499 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004500
J. Bruce Fields096455a2006-03-20 23:23:42 -05004501 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4502 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004503
4504 if (list && len <= list_len)
4505 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004506 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004507}
4508
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004509static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4510{
4511 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4512 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4513 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4514 return;
4515
4516 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4517 NFS_ATTR_FATTR_NLINK;
4518 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4519 fattr->nlink = 2;
4520}
4521
Trond Myklebust56659e92007-07-17 21:52:39 -04004522int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004523 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004524{
4525 struct nfs_server *server = NFS_SERVER(dir);
4526 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004527 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4528 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004529 };
4530 struct nfs4_fs_locations_arg args = {
4531 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004532 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004533 .page = page,
4534 .bitmask = bitmask,
4535 };
Benny Halevy22958462009-04-01 09:22:02 -04004536 struct nfs4_fs_locations_res res = {
4537 .fs_locations = fs_locations,
4538 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004539 struct rpc_message msg = {
4540 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4541 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004542 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004543 };
4544 int status;
4545
Harvey Harrison3110ff82008-05-02 13:42:44 -07004546 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004547 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004548 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004549 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04004550 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004551 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004552 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004553 return status;
4554}
4555
Andy Adamson557134a2009-04-01 09:21:53 -04004556#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004557/*
Andy Adamson357f54d2010-12-14 10:11:57 -05004558 * Check the exchange flags returned by the server for invalid flags, having
4559 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4560 * DS flags set.
4561 */
4562static int nfs4_check_cl_exchange_flags(u32 flags)
4563{
4564 if (flags & ~EXCHGID4_FLAG_MASK_R)
4565 goto out_inval;
4566 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4567 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4568 goto out_inval;
4569 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4570 goto out_inval;
4571 return NFS_OK;
4572out_inval:
4573 return -NFS4ERR_INVAL;
4574}
4575
4576/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04004577 * nfs4_proc_exchange_id()
4578 *
4579 * Since the clientid has expired, all compounds using sessions
4580 * associated with the stale clientid will be returning
4581 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4582 * be in some phase of session reset.
4583 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004584int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004585{
4586 nfs4_verifier verifier;
4587 struct nfs41_exchange_id_args args = {
4588 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05004589 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04004590 };
4591 struct nfs41_exchange_id_res res = {
4592 .client = clp,
4593 };
4594 int status;
4595 struct rpc_message msg = {
4596 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4597 .rpc_argp = &args,
4598 .rpc_resp = &res,
4599 .rpc_cred = cred,
4600 };
4601 __be32 *p;
4602
4603 dprintk("--> %s\n", __func__);
4604 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004605
Benny Halevy99fe60d2009-04-01 09:22:29 -04004606 p = (u32 *)verifier.data;
4607 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4608 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4609 args.verifier = &verifier;
4610
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004611 args.id_len = scnprintf(args.id, sizeof(args.id),
4612 "%s/%s.%s/%u",
4613 clp->cl_ipaddr,
4614 init_utsname()->nodename,
4615 init_utsname()->domainname,
4616 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004617
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004618 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4619 if (!status)
4620 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004621 dprintk("<-- %s status= %d\n", __func__, status);
4622 return status;
4623}
4624
Andy Adamson2050f0c2009-04-01 09:22:30 -04004625struct nfs4_get_lease_time_data {
4626 struct nfs4_get_lease_time_args *args;
4627 struct nfs4_get_lease_time_res *res;
4628 struct nfs_client *clp;
4629};
4630
4631static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4632 void *calldata)
4633{
4634 int ret;
4635 struct nfs4_get_lease_time_data *data =
4636 (struct nfs4_get_lease_time_data *)calldata;
4637
4638 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004639 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004640 /* just setup sequence, do not trigger session recovery
4641 since we're invoked within one */
4642 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004643 &data->args->la_seq_args,
4644 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004645
4646 BUG_ON(ret == -EAGAIN);
4647 rpc_call_start(task);
4648 dprintk("<-- %s\n", __func__);
4649}
4650
4651/*
4652 * Called from nfs4_state_manager thread for session setup, so don't recover
4653 * from sequence operation or clientid errors.
4654 */
4655static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4656{
4657 struct nfs4_get_lease_time_data *data =
4658 (struct nfs4_get_lease_time_data *)calldata;
4659
4660 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04004661 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4662 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04004663 switch (task->tk_status) {
4664 case -NFS4ERR_DELAY:
4665 case -NFS4ERR_GRACE:
4666 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4667 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4668 task->tk_status = 0;
Trond Myklebust0110ee12009-12-07 09:00:24 -05004669 nfs_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004670 return;
4671 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004672 dprintk("<-- %s\n", __func__);
4673}
4674
4675struct rpc_call_ops nfs4_get_lease_time_ops = {
4676 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4677 .rpc_call_done = nfs4_get_lease_time_done,
4678};
4679
4680int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4681{
4682 struct rpc_task *task;
4683 struct nfs4_get_lease_time_args args;
4684 struct nfs4_get_lease_time_res res = {
4685 .lr_fsinfo = fsinfo,
4686 };
4687 struct nfs4_get_lease_time_data data = {
4688 .args = &args,
4689 .res = &res,
4690 .clp = clp,
4691 };
4692 struct rpc_message msg = {
4693 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4694 .rpc_argp = &args,
4695 .rpc_resp = &res,
4696 };
4697 struct rpc_task_setup task_setup = {
4698 .rpc_client = clp->cl_rpcclient,
4699 .rpc_message = &msg,
4700 .callback_ops = &nfs4_get_lease_time_ops,
4701 .callback_data = &data
4702 };
4703 int status;
4704
Andy Adamson2050f0c2009-04-01 09:22:30 -04004705 dprintk("--> %s\n", __func__);
4706 task = rpc_run_task(&task_setup);
4707
4708 if (IS_ERR(task))
4709 status = PTR_ERR(task);
4710 else {
4711 status = task->tk_status;
4712 rpc_put_task(task);
4713 }
4714 dprintk("<-- %s return %d\n", __func__, status);
4715
4716 return status;
4717}
4718
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004719/*
4720 * Reset a slot table
4721 */
Andy Adamson104aeba2010-01-14 17:45:10 -05004722static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4723 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004724{
Andy Adamson104aeba2010-01-14 17:45:10 -05004725 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004726 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004727 int ret = 0;
4728
Andy Adamson104aeba2010-01-14 17:45:10 -05004729 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4730 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004731
Andy Adamson104aeba2010-01-14 17:45:10 -05004732 /* Does the newly negotiated max_reqs match the existing slot table? */
4733 if (max_reqs != tbl->max_slots) {
4734 ret = -ENOMEM;
4735 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004736 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05004737 if (!new)
4738 goto out;
4739 ret = 0;
4740 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004741 }
4742 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05004743 if (new) {
4744 tbl->slots = new;
4745 tbl->max_slots = max_reqs;
4746 }
4747 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004748 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004749 spin_unlock(&tbl->slot_tbl_lock);
4750 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4751 tbl, tbl->slots, tbl->max_slots);
4752out:
4753 dprintk("<-- %s: return %d\n", __func__, ret);
4754 return ret;
4755}
4756
Andy Adamsonfc931582009-04-01 09:22:31 -04004757/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004758 * Reset the forechannel and backchannel slot tables
4759 */
4760static int nfs4_reset_slot_tables(struct nfs4_session *session)
4761{
4762 int status;
4763
4764 status = nfs4_reset_slot_table(&session->fc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004765 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004766 if (status)
4767 return status;
4768
4769 status = nfs4_reset_slot_table(&session->bc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004770 session->bc_attrs.max_reqs, 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004771 return status;
4772}
4773
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004774/* Destroy the slot table */
4775static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4776{
4777 if (session->fc_slot_table.slots != NULL) {
4778 kfree(session->fc_slot_table.slots);
4779 session->fc_slot_table.slots = NULL;
4780 }
4781 if (session->bc_slot_table.slots != NULL) {
4782 kfree(session->bc_slot_table.slots);
4783 session->bc_slot_table.slots = NULL;
4784 }
4785 return;
4786}
4787
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004788/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004789 * Initialize slot table
4790 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004791static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4792 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04004793{
Andy Adamsonfc931582009-04-01 09:22:31 -04004794 struct nfs4_slot *slot;
4795 int ret = -ENOMEM;
4796
4797 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4798
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004799 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04004800
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004801 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04004802 if (!slot)
4803 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04004804 ret = 0;
4805
4806 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04004807 tbl->max_slots = max_slots;
4808 tbl->slots = slot;
4809 tbl->highest_used_slotid = -1; /* no slot is currently used */
4810 spin_unlock(&tbl->slot_tbl_lock);
4811 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4812 tbl, tbl->slots, tbl->max_slots);
4813out:
4814 dprintk("<-- %s: return %d\n", __func__, ret);
4815 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04004816}
4817
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004818/*
4819 * Initialize the forechannel and backchannel tables
4820 */
4821static int nfs4_init_slot_tables(struct nfs4_session *session)
4822{
Trond Myklebustf26468f2009-12-05 19:32:11 -05004823 struct nfs4_slot_table *tbl;
4824 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004825
Trond Myklebustf26468f2009-12-05 19:32:11 -05004826 tbl = &session->fc_slot_table;
4827 if (tbl->slots == NULL) {
4828 status = nfs4_init_slot_table(tbl,
4829 session->fc_attrs.max_reqs, 1);
4830 if (status)
4831 return status;
4832 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004833
Trond Myklebustf26468f2009-12-05 19:32:11 -05004834 tbl = &session->bc_slot_table;
4835 if (tbl->slots == NULL) {
4836 status = nfs4_init_slot_table(tbl,
4837 session->bc_attrs.max_reqs, 0);
4838 if (status)
4839 nfs4_destroy_slot_tables(session);
4840 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004841
4842 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04004843}
4844
4845struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4846{
4847 struct nfs4_session *session;
4848 struct nfs4_slot_table *tbl;
4849
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004850 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04004851 if (!session)
4852 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04004853
Andy Adamson557134a2009-04-01 09:21:53 -04004854 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004855 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04004856 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004857 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00004858 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004859
4860 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004861 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004862 spin_lock_init(&tbl->slot_tbl_lock);
4863 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00004864 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004865
Trond Myklebust1055d762010-06-16 09:52:27 -04004866 session->session_state = 1<<NFS4_SESSION_INITING;
4867
Andy Adamson557134a2009-04-01 09:21:53 -04004868 session->clp = clp;
4869 return session;
4870}
4871
4872void nfs4_destroy_session(struct nfs4_session *session)
4873{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04004874 nfs4_proc_destroy_session(session);
4875 dprintk("%s Destroy backchannel for xprt %p\n",
4876 __func__, session->clp->cl_rpcclient->cl_xprt);
4877 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4878 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004879 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04004880 kfree(session);
4881}
4882
Andy Adamsonfc931582009-04-01 09:22:31 -04004883/*
4884 * Initialize the values to be used by the client in CREATE_SESSION
4885 * If nfs4_init_session set the fore channel request and response sizes,
4886 * use them.
4887 *
4888 * Set the back channel max_resp_sz_cached to zero to force the client to
4889 * always set csa_cachethis to FALSE because the current implementation
4890 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4891 */
4892static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4893{
4894 struct nfs4_session *session = args->client->cl_session;
4895 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4896 mxresp_sz = session->fc_attrs.max_resp_sz;
4897
4898 if (mxrqst_sz == 0)
4899 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4900 if (mxresp_sz == 0)
4901 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4902 /* Fore channel attributes */
4903 args->fc_attrs.headerpadsz = 0;
4904 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4905 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04004906 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4907 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4908
4909 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05004910 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04004911 __func__,
4912 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05004913 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04004914
4915 /* Back channel attributes */
4916 args->bc_attrs.headerpadsz = 0;
4917 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4918 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4919 args->bc_attrs.max_resp_sz_cached = 0;
4920 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4921 args->bc_attrs.max_reqs = 1;
4922
4923 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4924 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4925 __func__,
4926 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4927 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4928 args->bc_attrs.max_reqs);
4929}
4930
J. Bruce Fields43c2e882010-10-02 15:19:01 -04004931static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04004932{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04004933 struct nfs4_channel_attrs *sent = &args->fc_attrs;
4934 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
4935
4936 if (rcvd->headerpadsz > sent->headerpadsz)
4937 return -EINVAL;
4938 if (rcvd->max_resp_sz > sent->max_resp_sz)
4939 return -EINVAL;
4940 /*
4941 * Our requested max_ops is the minimum we need; we're not
4942 * prepared to break up compounds into smaller pieces than that.
4943 * So, no point even trying to continue if the server won't
4944 * cooperate:
4945 */
4946 if (rcvd->max_ops < sent->max_ops)
4947 return -EINVAL;
4948 if (rcvd->max_reqs == 0)
4949 return -EINVAL;
4950 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04004951}
4952
J. Bruce Fields43c2e882010-10-02 15:19:01 -04004953static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
4954{
4955 struct nfs4_channel_attrs *sent = &args->bc_attrs;
4956 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04004957
J. Bruce Fields43c2e882010-10-02 15:19:01 -04004958 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
4959 return -EINVAL;
4960 if (rcvd->max_resp_sz < sent->max_resp_sz)
4961 return -EINVAL;
4962 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
4963 return -EINVAL;
4964 /* These would render the backchannel useless: */
4965 if (rcvd->max_ops == 0)
4966 return -EINVAL;
4967 if (rcvd->max_reqs == 0)
4968 return -EINVAL;
4969 return 0;
4970}
Andy Adamson8d353012009-04-01 09:22:32 -04004971
Andy Adamson8d353012009-04-01 09:22:32 -04004972static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
4973 struct nfs4_session *session)
4974{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04004975 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04004976
J. Bruce Fields43c2e882010-10-02 15:19:01 -04004977 ret = nfs4_verify_fore_channel_attrs(args, session);
4978 if (ret)
4979 return ret;
4980 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04004981}
4982
Andy Adamsonfc931582009-04-01 09:22:31 -04004983static int _nfs4_proc_create_session(struct nfs_client *clp)
4984{
4985 struct nfs4_session *session = clp->cl_session;
4986 struct nfs41_create_session_args args = {
4987 .client = clp,
4988 .cb_program = NFS4_CALLBACK,
4989 };
4990 struct nfs41_create_session_res res = {
4991 .client = clp,
4992 };
4993 struct rpc_message msg = {
4994 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
4995 .rpc_argp = &args,
4996 .rpc_resp = &res,
4997 };
4998 int status;
4999
5000 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005001 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005002
5003 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5004
Andy Adamson8d353012009-04-01 09:22:32 -04005005 if (!status)
5006 /* Verify the session's negotiated channel_attrs values */
5007 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005008 if (!status) {
5009 /* Increment the clientid slot sequence id */
5010 clp->cl_seqid++;
5011 }
5012
5013 return status;
5014}
5015
5016/*
5017 * Issues a CREATE_SESSION operation to the server.
5018 * It is the responsibility of the caller to verify the session is
5019 * expired before calling this routine.
5020 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005021int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005022{
5023 int status;
5024 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005025 struct nfs4_session *session = clp->cl_session;
Ricardo Labiaga7d6d63d2011-03-09 13:13:44 -05005026 long timeout = 0;
5027 int err;
Andy Adamsonfc931582009-04-01 09:22:31 -04005028
5029 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5030
Ricardo Labiaga7d6d63d2011-03-09 13:13:44 -05005031 do {
5032 status = _nfs4_proc_create_session(clp);
5033 if (status == -NFS4ERR_DELAY) {
5034 err = nfs4_delay(clp->cl_rpcclient, &timeout);
5035 if (err)
5036 status = err;
5037 }
5038 } while (status == -NFS4ERR_DELAY);
5039
Andy Adamsonfc931582009-04-01 09:22:31 -04005040 if (status)
5041 goto out;
5042
Trond Myklebustf26468f2009-12-05 19:32:11 -05005043 /* Init and reset the fore channel */
5044 status = nfs4_init_slot_tables(session);
5045 dprintk("slot table initialization returned %d\n", status);
5046 if (status)
5047 goto out;
5048 status = nfs4_reset_slot_tables(session);
5049 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005050 if (status)
5051 goto out;
5052
5053 ptr = (unsigned *)&session->sess_id.data[0];
5054 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5055 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005056out:
5057 dprintk("<-- %s\n", __func__);
5058 return status;
5059}
5060
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005061/*
5062 * Issue the over-the-wire RPC DESTROY_SESSION.
5063 * The caller must serialize access to this routine.
5064 */
5065int nfs4_proc_destroy_session(struct nfs4_session *session)
5066{
5067 int status = 0;
5068 struct rpc_message msg;
5069
5070 dprintk("--> nfs4_proc_destroy_session\n");
5071
5072 /* session is still being setup */
5073 if (session->clp->cl_cons_state != NFS_CS_READY)
5074 return status;
5075
5076 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5077 msg.rpc_argp = session;
5078 msg.rpc_resp = NULL;
5079 msg.rpc_cred = NULL;
5080 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5081
5082 if (status)
5083 printk(KERN_WARNING
5084 "Got error %d from the server on DESTROY_SESSION. "
5085 "Session has been destroyed regardless...\n", status);
5086
5087 dprintk("<-- nfs4_proc_destroy_session\n");
5088 return status;
5089}
5090
Trond Myklebustfccba802009-07-21 16:48:07 -04005091int nfs4_init_session(struct nfs_server *server)
5092{
5093 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005094 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005095 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005096 int ret;
5097
5098 if (!nfs4_has_session(clp))
5099 return 0;
5100
Trond Myklebust1055d762010-06-16 09:52:27 -04005101 session = clp->cl_session;
5102 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5103 return 0;
5104
Andy Adamson68bf05e2009-12-15 12:55:02 -05005105 rsize = server->rsize;
5106 if (rsize == 0)
5107 rsize = NFS_MAX_FILE_IO_SIZE;
5108 wsize = server->wsize;
5109 if (wsize == 0)
5110 wsize = NFS_MAX_FILE_IO_SIZE;
5111
Andy Adamson68bf05e2009-12-15 12:55:02 -05005112 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5113 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005114
Trond Myklebustfccba802009-07-21 16:48:07 -04005115 ret = nfs4_recover_expired_lease(server);
5116 if (!ret)
5117 ret = nfs4_check_client_ready(clp);
5118 return ret;
5119}
5120
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005121/*
5122 * Renew the cl_session lease.
5123 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005124struct nfs4_sequence_data {
5125 struct nfs_client *clp;
5126 struct nfs4_sequence_args args;
5127 struct nfs4_sequence_res res;
5128};
5129
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005130static void nfs41_sequence_release(void *data)
5131{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005132 struct nfs4_sequence_data *calldata = data;
5133 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005134
Alexandros Batsakis71358402010-02-05 03:45:05 -08005135 if (atomic_read(&clp->cl_count) > 1)
5136 nfs4_schedule_state_renewal(clp);
5137 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005138 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005139}
5140
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005141static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5142{
5143 switch(task->tk_status) {
5144 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005145 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5146 return -EAGAIN;
5147 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005148 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005149 }
5150 return 0;
5151}
5152
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005153static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005154{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005155 struct nfs4_sequence_data *calldata = data;
5156 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005157
Trond Myklebust14516c32010-07-31 14:29:06 -04005158 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5159 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005160
5161 if (task->tk_status < 0) {
5162 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005163 if (atomic_read(&clp->cl_count) == 1)
5164 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005165
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005166 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5167 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005168 return;
5169 }
5170 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005171 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005172out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005173 dprintk("<-- %s\n", __func__);
5174}
5175
5176static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5177{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005178 struct nfs4_sequence_data *calldata = data;
5179 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005180 struct nfs4_sequence_args *args;
5181 struct nfs4_sequence_res *res;
5182
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005183 args = task->tk_msg.rpc_argp;
5184 res = task->tk_msg.rpc_resp;
5185
Trond Myklebust035168ab2010-06-16 09:52:26 -04005186 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005187 return;
5188 rpc_call_start(task);
5189}
5190
5191static const struct rpc_call_ops nfs41_sequence_ops = {
5192 .rpc_call_done = nfs41_sequence_call_done,
5193 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005194 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005195};
5196
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005197static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005198{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005199 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005200 struct rpc_message msg = {
5201 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5202 .rpc_cred = cred,
5203 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005204 struct rpc_task_setup task_setup_data = {
5205 .rpc_client = clp->cl_rpcclient,
5206 .rpc_message = &msg,
5207 .callback_ops = &nfs41_sequence_ops,
5208 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5209 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005210
Alexandros Batsakis71358402010-02-05 03:45:05 -08005211 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005212 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005213 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005214 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005215 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005216 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005217 }
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005218 msg.rpc_argp = &calldata->args;
5219 msg.rpc_resp = &calldata->res;
5220 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005221 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005222
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005223 return rpc_run_task(&task_setup_data);
5224}
5225
5226static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5227{
5228 struct rpc_task *task;
5229 int ret = 0;
5230
5231 task = _nfs41_proc_sequence(clp, cred);
5232 if (IS_ERR(task))
5233 ret = PTR_ERR(task);
5234 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005235 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005236 dprintk("<-- %s status=%d\n", __func__, ret);
5237 return ret;
5238}
5239
5240static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5241{
5242 struct rpc_task *task;
5243 int ret;
5244
5245 task = _nfs41_proc_sequence(clp, cred);
5246 if (IS_ERR(task)) {
5247 ret = PTR_ERR(task);
5248 goto out;
5249 }
5250 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005251 if (!ret) {
5252 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5253
5254 if (task->tk_status == 0)
5255 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005256 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005257 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005258 rpc_put_task(task);
5259out:
5260 dprintk("<-- %s status=%d\n", __func__, ret);
5261 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005262}
5263
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005264struct nfs4_reclaim_complete_data {
5265 struct nfs_client *clp;
5266 struct nfs41_reclaim_complete_args arg;
5267 struct nfs41_reclaim_complete_res res;
5268};
5269
5270static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5271{
5272 struct nfs4_reclaim_complete_data *calldata = data;
5273
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005274 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168ab2010-06-16 09:52:26 -04005275 if (nfs41_setup_sequence(calldata->clp->cl_session,
5276 &calldata->arg.seq_args,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005277 &calldata->res.seq_res, 0, task))
5278 return;
5279
5280 rpc_call_start(task);
5281}
5282
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005283static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5284{
5285 switch(task->tk_status) {
5286 case 0:
5287 case -NFS4ERR_COMPLETE_ALREADY:
5288 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5289 break;
5290 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005291 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5292 return -EAGAIN;
5293 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005294 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005295 }
5296 return 0;
5297}
5298
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005299static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5300{
5301 struct nfs4_reclaim_complete_data *calldata = data;
5302 struct nfs_client *clp = calldata->clp;
5303 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5304
5305 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005306 if (!nfs41_sequence_done(task, res))
5307 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005308
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005309 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5310 rpc_restart_call_prepare(task);
5311 return;
5312 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005313 dprintk("<-- %s\n", __func__);
5314}
5315
5316static void nfs4_free_reclaim_complete_data(void *data)
5317{
5318 struct nfs4_reclaim_complete_data *calldata = data;
5319
5320 kfree(calldata);
5321}
5322
5323static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5324 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5325 .rpc_call_done = nfs4_reclaim_complete_done,
5326 .rpc_release = nfs4_free_reclaim_complete_data,
5327};
5328
5329/*
5330 * Issue a global reclaim complete.
5331 */
5332static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5333{
5334 struct nfs4_reclaim_complete_data *calldata;
5335 struct rpc_task *task;
5336 struct rpc_message msg = {
5337 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5338 };
5339 struct rpc_task_setup task_setup_data = {
5340 .rpc_client = clp->cl_rpcclient,
5341 .rpc_message = &msg,
5342 .callback_ops = &nfs4_reclaim_complete_call_ops,
5343 .flags = RPC_TASK_ASYNC,
5344 };
5345 int status = -ENOMEM;
5346
5347 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005348 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005349 if (calldata == NULL)
5350 goto out;
5351 calldata->clp = clp;
5352 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005353
5354 msg.rpc_argp = &calldata->arg;
5355 msg.rpc_resp = &calldata->res;
5356 task_setup_data.callback_data = calldata;
5357 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005358 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005359 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005360 goto out;
5361 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005362 status = nfs4_wait_for_completion_rpc_task(task);
5363 if (status == 0)
5364 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005365 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005366 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005367out:
5368 dprintk("<-- %s status=%d\n", __func__, status);
5369 return status;
5370}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005371
5372static void
5373nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5374{
5375 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005376 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005377
5378 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005379 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5380 * right now covering the LAYOUTGET we are about to send.
5381 * However, that is not so catastrophic, and there seems
5382 * to be no way to prevent it completely.
5383 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005384 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5385 &lgp->res.seq_res, 0, task))
5386 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005387 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5388 NFS_I(lgp->args.inode)->layout,
5389 lgp->args.ctx->state)) {
5390 rpc_exit(task, NFS4_OK);
5391 return;
5392 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005393 rpc_call_start(task);
5394}
5395
5396static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5397{
5398 struct nfs4_layoutget *lgp = calldata;
5399 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5400
5401 dprintk("--> %s\n", __func__);
5402
5403 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5404 return;
5405
5406 switch (task->tk_status) {
5407 case 0:
5408 break;
5409 case -NFS4ERR_LAYOUTTRYLATER:
5410 case -NFS4ERR_RECALLCONFLICT:
5411 task->tk_status = -NFS4ERR_DELAY;
5412 /* Fall through */
5413 default:
5414 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5415 rpc_restart_call_prepare(task);
5416 return;
5417 }
5418 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005419 dprintk("<-- %s\n", __func__);
5420}
5421
5422static void nfs4_layoutget_release(void *calldata)
5423{
5424 struct nfs4_layoutget *lgp = calldata;
5425
5426 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005427 if (lgp->res.layout.buf != NULL)
5428 free_page((unsigned long) lgp->res.layout.buf);
5429 put_nfs_open_context(lgp->args.ctx);
5430 kfree(calldata);
5431 dprintk("<-- %s\n", __func__);
5432}
5433
5434static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5435 .rpc_call_prepare = nfs4_layoutget_prepare,
5436 .rpc_call_done = nfs4_layoutget_done,
5437 .rpc_release = nfs4_layoutget_release,
5438};
5439
5440int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5441{
5442 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5443 struct rpc_task *task;
5444 struct rpc_message msg = {
5445 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5446 .rpc_argp = &lgp->args,
5447 .rpc_resp = &lgp->res,
5448 };
5449 struct rpc_task_setup task_setup_data = {
5450 .rpc_client = server->client,
5451 .rpc_message = &msg,
5452 .callback_ops = &nfs4_layoutget_call_ops,
5453 .callback_data = lgp,
5454 .flags = RPC_TASK_ASYNC,
5455 };
5456 int status = 0;
5457
5458 dprintk("--> %s\n", __func__);
5459
5460 lgp->res.layout.buf = (void *)__get_free_page(GFP_NOFS);
5461 if (lgp->res.layout.buf == NULL) {
5462 nfs4_layoutget_release(lgp);
5463 return -ENOMEM;
5464 }
5465
5466 lgp->res.seq_res.sr_slot = NULL;
5467 task = rpc_run_task(&task_setup_data);
5468 if (IS_ERR(task))
5469 return PTR_ERR(task);
5470 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00005471 if (status == 0)
5472 status = task->tk_status;
5473 if (status == 0)
5474 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005475 rpc_put_task(task);
5476 dprintk("<-- %s status=%d\n", __func__, status);
5477 return status;
5478}
5479
5480static int
5481_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5482{
5483 struct nfs4_getdeviceinfo_args args = {
5484 .pdev = pdev,
5485 };
5486 struct nfs4_getdeviceinfo_res res = {
5487 .pdev = pdev,
5488 };
5489 struct rpc_message msg = {
5490 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5491 .rpc_argp = &args,
5492 .rpc_resp = &res,
5493 };
5494 int status;
5495
5496 dprintk("--> %s\n", __func__);
5497 status = nfs4_call_sync(server, &msg, &args, &res, 0);
5498 dprintk("<-- %s status=%d\n", __func__, status);
5499
5500 return status;
5501}
5502
5503int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5504{
5505 struct nfs4_exception exception = { };
5506 int err;
5507
5508 do {
5509 err = nfs4_handle_exception(server,
5510 _nfs4_proc_getdeviceinfo(server, pdev),
5511 &exception);
5512 } while (exception.retry);
5513 return err;
5514}
5515EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5516
Andy Adamson557134a2009-04-01 09:21:53 -04005517#endif /* CONFIG_NFS_V4_1 */
5518
Andy Adamson591d71c2009-04-01 09:22:47 -04005519struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005520 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005521 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522 .recover_open = nfs4_open_reclaim,
5523 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04005524 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005525 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526};
5527
Andy Adamson591d71c2009-04-01 09:22:47 -04005528#if defined(CONFIG_NFS_V4_1)
5529struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5530 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5531 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5532 .recover_open = nfs4_open_reclaim,
5533 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05005534 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005535 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005536 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04005537};
5538#endif /* CONFIG_NFS_V4_1 */
5539
5540struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005541 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005542 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 .recover_open = nfs4_open_expired,
5544 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04005545 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005546 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547};
5548
Andy Adamson591d71c2009-04-01 09:22:47 -04005549#if defined(CONFIG_NFS_V4_1)
5550struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5551 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5552 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5553 .recover_open = nfs4_open_expired,
5554 .recover_lock = nfs4_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05005555 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005556 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04005557};
5558#endif /* CONFIG_NFS_V4_1 */
5559
Benny Halevy29fba382009-04-01 09:22:44 -04005560struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5561 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04005562 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005563 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04005564};
5565
5566#if defined(CONFIG_NFS_V4_1)
5567struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5568 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04005569 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005570 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04005571};
5572#endif
5573
Trond Myklebust97dc1352010-06-16 09:52:26 -04005574static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
5575 .minor_version = 0,
5576 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04005577 .validate_stateid = nfs4_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005578 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
5579 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
5580 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005581};
5582
5583#if defined(CONFIG_NFS_V4_1)
5584static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
5585 .minor_version = 1,
5586 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04005587 .validate_stateid = nfs41_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005588 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
5589 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
5590 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005591};
5592#endif
5593
Trond Myklebust97dc1352010-06-16 09:52:26 -04005594const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
5595 [0] = &nfs_v4_0_minor_ops,
5596#if defined(CONFIG_NFS_V4_1)
5597 [1] = &nfs_v4_1_minor_ops,
5598#endif
5599};
5600
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005601static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005602 .permission = nfs_permission,
5603 .getattr = nfs_getattr,
5604 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005605 .getxattr = generic_getxattr,
5606 .setxattr = generic_setxattr,
5607 .listxattr = generic_listxattr,
5608 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005609};
5610
David Howells509de812006-08-22 20:06:11 -04005611const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612 .version = 4, /* protocol version */
5613 .dentry_ops = &nfs4_dentry_operations,
5614 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005615 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 .getroot = nfs4_proc_get_root,
5617 .getattr = nfs4_proc_getattr,
5618 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04005619 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620 .lookup = nfs4_proc_lookup,
5621 .access = nfs4_proc_access,
5622 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623 .create = nfs4_proc_create,
5624 .remove = nfs4_proc_remove,
5625 .unlink_setup = nfs4_proc_unlink_setup,
5626 .unlink_done = nfs4_proc_unlink_done,
5627 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04005628 .rename_setup = nfs4_proc_rename_setup,
5629 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630 .link = nfs4_proc_link,
5631 .symlink = nfs4_proc_symlink,
5632 .mkdir = nfs4_proc_mkdir,
5633 .rmdir = nfs4_proc_remove,
5634 .readdir = nfs4_proc_readdir,
5635 .mknod = nfs4_proc_mknod,
5636 .statfs = nfs4_proc_statfs,
5637 .fsinfo = nfs4_proc_fsinfo,
5638 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04005639 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 .decode_dirent = nfs4_decode_dirent,
5641 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05005642 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005644 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005646 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005648 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04005649 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04005650 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00005651 .init_client = nfs4_init_client,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652};
5653
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005654static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
5655 .prefix = XATTR_NAME_NFSV4_ACL,
5656 .list = nfs4_xattr_list_nfs4_acl,
5657 .get = nfs4_xattr_get_nfs4_acl,
5658 .set = nfs4_xattr_set_nfs4_acl,
5659};
5660
5661const struct xattr_handler *nfs4_xattr_handlers[] = {
5662 &nfs4_xattr_nfs4_acl_handler,
5663 NULL
5664};
5665
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666/*
5667 * Local variables:
5668 * c-basic-offset: 8
5669 * End:
5670 */