blob: da902123ec53df31d7c72e7985823a4c3e68a837 [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 Adamsondc70d7b2011-03-01 01:34:19 +0000508int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400509 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}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000574EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400575
Trond Myklebust035168ab2010-06-16 09:52:26 -0400576int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400577 struct nfs4_sequence_args *args,
578 struct nfs4_sequence_res *res,
579 int cache_reply,
580 struct rpc_task *task)
581{
Trond Myklebust035168ab2010-06-16 09:52:26 -0400582 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400583 int ret = 0;
584
Trond Myklebust035168ab2010-06-16 09:52:26 -0400585 if (session == NULL) {
586 args->sa_session = NULL;
587 res->sr_session = NULL;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400588 goto out;
Trond Myklebust035168ab2010-06-16 09:52:26 -0400589 }
590
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200591 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400592 __func__, session->clp, session, res->sr_slot ?
593 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168ab2010-06-16 09:52:26 -0400594
595 ret = nfs41_setup_sequence(session, args, res, cache_reply,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400596 task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400597out:
598 dprintk("<-- %s status=%d\n", __func__, ret);
599 return ret;
600}
601
602struct nfs41_call_sync_data {
Trond Myklebust035168ab2010-06-16 09:52:26 -0400603 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400604 struct nfs4_sequence_args *seq_args;
605 struct nfs4_sequence_res *seq_res;
606 int cache_reply;
607};
608
609static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
610{
611 struct nfs41_call_sync_data *data = calldata;
612
Trond Myklebust035168ab2010-06-16 09:52:26 -0400613 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
614
615 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400616 data->seq_res, data->cache_reply, task))
617 return;
618 rpc_call_start(task);
619}
620
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800621static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
622{
623 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
624 nfs41_call_sync_prepare(task, calldata);
625}
626
Andy Adamson69ab40c2009-04-01 09:22:19 -0400627static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
628{
629 struct nfs41_call_sync_data *data = calldata;
630
Trond Myklebust14516c32010-07-31 14:29:06 -0400631 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400632}
633
Andy Adamsonce5039c2009-04-01 09:22:13 -0400634struct rpc_call_ops nfs41_call_sync_ops = {
635 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400636 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400637};
638
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800639struct rpc_call_ops nfs41_call_priv_sync_ops = {
640 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
641 .rpc_call_done = nfs41_call_sync_done,
642};
643
Trond Myklebust035168ab2010-06-16 09:52:26 -0400644static int nfs4_call_sync_sequence(struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400645 struct rpc_message *msg,
646 struct nfs4_sequence_args *args,
647 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800648 int cache_reply,
649 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400650{
651 int ret;
652 struct rpc_task *task;
653 struct nfs41_call_sync_data data = {
Trond Myklebust035168ab2010-06-16 09:52:26 -0400654 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400655 .seq_args = args,
656 .seq_res = res,
657 .cache_reply = cache_reply,
658 };
659 struct rpc_task_setup task_setup = {
Trond Myklebust035168ab2010-06-16 09:52:26 -0400660 .rpc_client = server->client,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400661 .rpc_message = msg,
662 .callback_ops = &nfs41_call_sync_ops,
663 .callback_data = &data
664 };
665
Benny Halevydfb4f3092010-09-24 09:17:01 -0400666 res->sr_slot = NULL;
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800667 if (privileged)
668 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400669 task = rpc_run_task(&task_setup);
670 if (IS_ERR(task))
671 ret = PTR_ERR(task);
672 else {
673 ret = task->tk_status;
674 rpc_put_task(task);
675 }
676 return ret;
677}
678
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400679int _nfs4_call_sync_session(struct nfs_server *server,
680 struct rpc_message *msg,
681 struct nfs4_sequence_args *args,
682 struct nfs4_sequence_res *res,
683 int cache_reply)
684{
Trond Myklebust035168ab2010-06-16 09:52:26 -0400685 return nfs4_call_sync_sequence(server, msg, args, res, cache_reply, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400686}
687
Trond Myklebustdf896452010-06-16 09:52:26 -0400688#else
Trond Myklebust14516c32010-07-31 14:29:06 -0400689static int nfs4_sequence_done(struct rpc_task *task,
690 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400691{
Trond Myklebust14516c32010-07-31 14:29:06 -0400692 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400693}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400694#endif /* CONFIG_NFS_V4_1 */
695
696int _nfs4_call_sync(struct nfs_server *server,
697 struct rpc_message *msg,
698 struct nfs4_sequence_args *args,
699 struct nfs4_sequence_res *res,
700 int cache_reply)
701{
Benny Halevyf3752972009-04-01 09:22:04 -0400702 args->sa_session = res->sr_session = NULL;
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400703 return rpc_call_sync(server->client, msg, 0);
704}
705
706#define nfs4_call_sync(server, msg, args, res, cache_reply) \
Trond Myklebust97dc1352010-06-16 09:52:26 -0400707 (server)->nfs_client->cl_mvops->call_sync((server), (msg), &(args)->seq_args, \
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400708 &(res)->seq_res, (cache_reply))
709
Trond Myklebust38478b22006-05-25 01:40:57 -0400710static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Trond Myklebust38478b22006-05-25 01:40:57 -0400712 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Trond Myklebust38478b22006-05-25 01:40:57 -0400714 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400715 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
716 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400717 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400718 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400719 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720}
721
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100722struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400723 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100724 struct nfs_openargs o_arg;
725 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100726 struct nfs_open_confirmargs c_arg;
727 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100728 struct nfs_fattr f_attr;
729 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400730 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100731 struct dentry *dir;
732 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400733 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100734 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100735 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400736 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100737 int rpc_status;
738 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100739};
740
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400741
742static void nfs4_init_opendata_res(struct nfs4_opendata *p)
743{
744 p->o_res.f_attr = &p->f_attr;
745 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400746 p->o_res.seqid = p->o_arg.seqid;
747 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400748 p->o_res.server = p->o_arg.server;
749 nfs_fattr_init(&p->f_attr);
750 nfs_fattr_init(&p->dir_attr);
751}
752
Trond Myklebustad389da2007-06-05 12:30:00 -0400753static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500754 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400755 const struct iattr *attrs,
756 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100757{
Trond Myklebustad389da2007-06-05 12:30:00 -0400758 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100759 struct inode *dir = parent->d_inode;
760 struct nfs_server *server = NFS_SERVER(dir);
761 struct nfs4_opendata *p;
762
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400763 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100764 if (p == NULL)
765 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400766 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100767 if (p->o_arg.seqid == NULL)
768 goto err_free;
Al Virof6948692010-01-30 13:51:04 -0500769 path_get(path);
770 p->path = *path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100771 p->dir = parent;
772 p->owner = sp;
773 atomic_inc(&sp->so_count);
774 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500775 p->o_arg.open_flags = flags;
776 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400777 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400778 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400779 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100780 p->o_arg.server = server;
781 p->o_arg.bitmask = server->attr_bitmask;
782 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400783 if (flags & O_CREAT) {
784 u32 *s;
785
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100786 p->o_arg.u.attrs = &p->attrs;
787 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400788 s = (u32 *) p->o_arg.u.verifier.data;
789 s[0] = jiffies;
790 s[1] = current->pid;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100791 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100792 p->c_arg.fh = &p->o_res.fh;
793 p->c_arg.stateid = &p->o_res.stateid;
794 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400795 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400796 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100797 return p;
798err_free:
799 kfree(p);
800err:
801 dput(parent);
802 return NULL;
803}
804
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400805static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100806{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400807 struct nfs4_opendata *p = container_of(kref,
808 struct nfs4_opendata, kref);
809
810 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400811 if (p->state != NULL)
812 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400813 nfs4_put_state_owner(p->owner);
814 dput(p->dir);
Jan Blunck31f31db12008-05-02 13:42:45 -0700815 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400816 kfree(p);
817}
818
819static void nfs4_opendata_put(struct nfs4_opendata *p)
820{
821 if (p != NULL)
822 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100823}
824
Trond Myklebust06f814a2006-01-03 09:55:07 +0100825static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
826{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100827 int ret;
828
Trond Myklebust06f814a2006-01-03 09:55:07 +0100829 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100830 return ret;
831}
832
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500833static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400834{
835 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500836
837 if (open_mode & O_EXCL)
838 goto out;
839 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400840 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500841 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
842 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400843 break;
844 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500845 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
846 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400847 break;
848 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500849 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
850 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400851 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500852out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400853 return ret;
854}
855
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500856static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400857{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500858 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400859 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500860 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400861 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500862 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400863 return 1;
864}
865
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500866static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100867{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500868 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100869 case FMODE_WRITE:
870 state->n_wronly++;
871 break;
872 case FMODE_READ:
873 state->n_rdonly++;
874 break;
875 case FMODE_READ|FMODE_WRITE:
876 state->n_rdwr++;
877 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500878 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100879}
880
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500881static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400882{
883 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
884 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
885 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500886 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400887 case FMODE_READ:
888 set_bit(NFS_O_RDONLY_STATE, &state->flags);
889 break;
890 case FMODE_WRITE:
891 set_bit(NFS_O_WRONLY_STATE, &state->flags);
892 break;
893 case FMODE_READ|FMODE_WRITE:
894 set_bit(NFS_O_RDWR_STATE, &state->flags);
895 }
896}
897
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500898static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400899{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400900 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500901 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400902 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400903}
904
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500905static 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 -0700906{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400907 /*
908 * Protect the call to nfs4_state_set_mode_locked and
909 * serialise the stateid update
910 */
911 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400912 if (deleg_stateid != NULL) {
913 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
914 set_bit(NFS_DELEGATED_STATE, &state->flags);
915 }
916 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500917 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400918 write_sequnlock(&state->seqlock);
919 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500920 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700921 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922}
923
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500924static 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 -0500925{
926 struct nfs_inode *nfsi = NFS_I(state->inode);
927 struct nfs_delegation *deleg_cur;
928 int ret = 0;
929
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500930 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500931
932 rcu_read_lock();
933 deleg_cur = rcu_dereference(nfsi->delegation);
934 if (deleg_cur == NULL)
935 goto no_delegation;
936
937 spin_lock(&deleg_cur->lock);
938 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500939 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500940 goto no_delegation_unlock;
941
942 if (delegation == NULL)
943 delegation = &deleg_cur->stateid;
944 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
945 goto no_delegation_unlock;
946
Trond Myklebustb7391f42008-12-23 15:21:52 -0500947 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500948 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500949 ret = 1;
950no_delegation_unlock:
951 spin_unlock(&deleg_cur->lock);
952no_delegation:
953 rcu_read_unlock();
954
955 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500956 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500957 ret = 1;
958 }
959
960 return ret;
961}
962
963
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500964static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400965{
966 struct nfs_delegation *delegation;
967
968 rcu_read_lock();
969 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500970 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -0400971 rcu_read_unlock();
972 return;
973 }
974 rcu_read_unlock();
975 nfs_inode_return_delegation(inode);
976}
977
Trond Myklebust6ee41262007-07-08 14:11:36 -0400978static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400979{
980 struct nfs4_state *state = opendata->state;
981 struct nfs_inode *nfsi = NFS_I(state->inode);
982 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500983 int open_mode = opendata->o_arg.open_flags & O_EXCL;
984 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400985 nfs4_stateid stateid;
986 int ret = -EAGAIN;
987
Trond Myklebustaac00a82007-07-05 19:02:21 -0400988 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500989 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400990 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500991 if (can_open_cached(state, fmode, open_mode)) {
992 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400993 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -0400994 goto out_return_state;
995 }
996 spin_unlock(&state->owner->so_lock);
997 }
Trond Myklebust34310432008-12-23 15:21:38 -0500998 rcu_read_lock();
999 delegation = rcu_dereference(nfsi->delegation);
1000 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001001 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001002 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001003 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001004 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001005 /* Save the delegation */
1006 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1007 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001008 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001009 if (ret != 0)
1010 goto out;
1011 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001012
1013 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001014 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001015 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001016 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001017out:
1018 return ERR_PTR(ret);
1019out_return_state:
1020 atomic_inc(&state->count);
1021 return state;
1022}
1023
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001024static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1025{
1026 struct inode *inode;
1027 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001028 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001029 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001030
Trond Myklebustaac00a82007-07-05 19:02:21 -04001031 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001032 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001033 goto out;
1034 }
1035
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001036 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001037 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001038 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001039 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001040 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001041 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001042 goto err;
1043 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001044 state = nfs4_get_open_state(inode, data->owner);
1045 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001046 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001047 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001048 int delegation_flags = 0;
1049
Trond Myklebust003707c2007-07-05 18:07:55 -04001050 rcu_read_lock();
1051 delegation = rcu_dereference(NFS_I(inode)->delegation);
1052 if (delegation)
1053 delegation_flags = delegation->flags;
1054 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -05001055 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001056 nfs_inode_set_delegation(state->inode,
1057 data->owner->so_cred,
1058 &data->o_res);
1059 else
1060 nfs_inode_reclaim_delegation(state->inode,
1061 data->owner->so_cred,
1062 &data->o_res);
1063 }
Trond Myklebust34310432008-12-23 15:21:38 -05001064
1065 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001066 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001067 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001068out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001069 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001070err_put_inode:
1071 iput(inode);
1072err:
1073 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001074}
1075
Trond Myklebust864472e2006-01-03 09:55:15 +01001076static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1077{
1078 struct nfs_inode *nfsi = NFS_I(state->inode);
1079 struct nfs_open_context *ctx;
1080
1081 spin_lock(&state->inode->i_lock);
1082 list_for_each_entry(ctx, &nfsi->open_files, list) {
1083 if (ctx->state != state)
1084 continue;
1085 get_nfs_open_context(ctx);
1086 spin_unlock(&state->inode->i_lock);
1087 return ctx;
1088 }
1089 spin_unlock(&state->inode->i_lock);
1090 return ERR_PTR(-ENOENT);
1091}
1092
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001093static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1094{
1095 struct nfs4_opendata *opendata;
1096
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001097 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001098 if (opendata == NULL)
1099 return ERR_PTR(-ENOMEM);
1100 opendata->state = state;
1101 atomic_inc(&state->count);
1102 return opendata;
1103}
1104
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001105static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001106{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001107 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001108 int ret;
1109
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001110 opendata->o_arg.open_flags = 0;
1111 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001112 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1113 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1114 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001115 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001116 if (ret != 0)
1117 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001118 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001119 if (IS_ERR(newstate))
1120 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001121 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001122 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001123 return 0;
1124}
1125
1126static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1127{
Trond Myklebust864472e2006-01-03 09:55:15 +01001128 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001129 int ret;
1130
1131 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001132 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001133 smp_rmb();
1134 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001135 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001136 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001137 if (ret != 0)
1138 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001139 if (newstate != state)
1140 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001141 }
1142 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001143 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001144 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001145 if (ret != 0)
1146 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001147 if (newstate != state)
1148 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001149 }
1150 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001151 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001152 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001153 if (ret != 0)
1154 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001155 if (newstate != state)
1156 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001157 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001158 /*
1159 * We may have performed cached opens for all three recoveries.
1160 * Check if we need to update the current stateid.
1161 */
1162 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1163 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001164 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001165 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1166 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001167 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001168 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001169 return 0;
1170}
1171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172/*
1173 * OPEN_RECLAIM:
1174 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001176static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001178 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001179 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001180 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 int status;
1182
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001183 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1184 if (IS_ERR(opendata))
1185 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001186 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1187 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001188 rcu_read_lock();
1189 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001190 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001191 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001192 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001193 opendata->o_arg.u.delegation_type = delegation_type;
1194 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001195 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 return status;
1197}
1198
Trond Myklebust539cd032007-06-05 11:46:42 -04001199static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
1201 struct nfs_server *server = NFS_SERVER(state->inode);
1202 struct nfs4_exception exception = { };
1203 int err;
1204 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001205 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001206 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001207 break;
1208 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 } while (exception.retry);
1210 return err;
1211}
1212
Trond Myklebust864472e2006-01-03 09:55:15 +01001213static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1214{
1215 struct nfs_open_context *ctx;
1216 int ret;
1217
1218 ctx = nfs4_state_find_open_context(state);
1219 if (IS_ERR(ctx))
1220 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001221 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001222 put_nfs_open_context(ctx);
1223 return ret;
1224}
1225
Trond Myklebust13437e12007-07-06 15:10:43 -04001226static 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 -07001227{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001228 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001229 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001231 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1232 if (IS_ERR(opendata))
1233 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001234 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001235 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001236 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001237 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001238 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001239 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240}
1241
Trond Myklebust13437e12007-07-06 15:10:43 -04001242int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
1244 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001245 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 int err;
1247 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001248 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 switch (err) {
1250 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001251 case -ENOENT:
1252 case -ESTALE:
1253 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001254 case -NFS4ERR_BADSESSION:
1255 case -NFS4ERR_BADSLOT:
1256 case -NFS4ERR_BAD_HIGH_SLOT:
1257 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1258 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001259 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001260 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 case -NFS4ERR_STALE_CLIENTID:
1262 case -NFS4ERR_STALE_STATEID:
1263 case -NFS4ERR_EXPIRED:
1264 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001265 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001266 goto out;
1267 case -ERESTARTSYS:
1268 /*
1269 * The show must go on: exit, but mark the
1270 * stateid as needing recovery.
1271 */
1272 case -NFS4ERR_ADMIN_REVOKED:
1273 case -NFS4ERR_BAD_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001274 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001275 case -EKEYEXPIRED:
1276 /*
1277 * User RPCSEC_GSS context has expired.
1278 * We cannot recover this stateid now, so
1279 * skip it and allow recovery thread to
1280 * proceed.
1281 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001282 case -ENOMEM:
1283 err = 0;
1284 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 }
1286 err = nfs4_handle_exception(server, err, &exception);
1287 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001288out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 return err;
1290}
1291
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001292static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1293{
1294 struct nfs4_opendata *data = calldata;
1295
1296 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001297 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001298 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1299 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001300 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001301 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001302 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001303 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001304}
1305
1306static void nfs4_open_confirm_release(void *calldata)
1307{
1308 struct nfs4_opendata *data = calldata;
1309 struct nfs4_state *state = NULL;
1310
1311 /* If this request hasn't been cancelled, do nothing */
1312 if (data->cancelled == 0)
1313 goto out_free;
1314 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001315 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001316 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001317 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001318 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001319 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001320out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001321 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001322}
1323
1324static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001325 .rpc_call_done = nfs4_open_confirm_done,
1326 .rpc_release = nfs4_open_confirm_release,
1327};
1328
1329/*
1330 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1331 */
1332static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1333{
1334 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1335 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001336 struct rpc_message msg = {
1337 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1338 .rpc_argp = &data->c_arg,
1339 .rpc_resp = &data->c_res,
1340 .rpc_cred = data->owner->so_cred,
1341 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001342 struct rpc_task_setup task_setup_data = {
1343 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001344 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001345 .callback_ops = &nfs4_open_confirm_ops,
1346 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001347 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001348 .flags = RPC_TASK_ASYNC,
1349 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 int status;
1351
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001352 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001353 data->rpc_done = 0;
1354 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001355 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001356 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001357 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001358 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001359 status = nfs4_wait_for_completion_rpc_task(task);
1360 if (status != 0) {
1361 data->cancelled = 1;
1362 smp_wmb();
1363 } else
1364 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001365 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 return status;
1367}
1368
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001369static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001371 struct nfs4_opendata *data = calldata;
1372 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001373
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001374 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1375 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001376 /*
1377 * Check if we still need to send an OPEN call, or if we can use
1378 * a delegation instead.
1379 */
1380 if (data->state != NULL) {
1381 struct nfs_delegation *delegation;
1382
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001383 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001384 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001385 rcu_read_lock();
1386 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1387 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001388 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001389 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001390 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001391 }
1392 rcu_read_unlock();
1393 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001394 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001395 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001396 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001397 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001398 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001399 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1400 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001401 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04001402 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001403 &data->o_arg.seq_args,
1404 &data->o_res.seq_res, 1, task))
1405 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001406 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001407 return;
1408out_no_action:
1409 task->tk_action = NULL;
1410
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001411}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001413static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1414{
1415 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1416 nfs4_open_prepare(task, calldata);
1417}
1418
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001419static void nfs4_open_done(struct rpc_task *task, void *calldata)
1420{
1421 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001423 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001424
Trond Myklebust14516c32010-07-31 14:29:06 -04001425 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1426 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001427
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001428 if (task->tk_status == 0) {
1429 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001430 case S_IFREG:
1431 break;
1432 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001433 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001434 break;
1435 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001436 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001437 break;
1438 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001439 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001440 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001441 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001442 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1443 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001444 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001445 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001446}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001447
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001448static void nfs4_open_release(void *calldata)
1449{
1450 struct nfs4_opendata *data = calldata;
1451 struct nfs4_state *state = NULL;
1452
1453 /* If this request hasn't been cancelled, do nothing */
1454 if (data->cancelled == 0)
1455 goto out_free;
1456 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001457 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001458 goto out_free;
1459 /* In case we need an open_confirm, no cleanup! */
1460 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1461 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001462 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001463 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001464 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001465out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001466 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001467}
1468
1469static const struct rpc_call_ops nfs4_open_ops = {
1470 .rpc_call_prepare = nfs4_open_prepare,
1471 .rpc_call_done = nfs4_open_done,
1472 .rpc_release = nfs4_open_release,
1473};
1474
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001475static const struct rpc_call_ops nfs4_recover_open_ops = {
1476 .rpc_call_prepare = nfs4_recover_open_prepare,
1477 .rpc_call_done = nfs4_open_done,
1478 .rpc_release = nfs4_open_release,
1479};
1480
1481static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001482{
1483 struct inode *dir = data->dir->d_inode;
1484 struct nfs_server *server = NFS_SERVER(dir);
1485 struct nfs_openargs *o_arg = &data->o_arg;
1486 struct nfs_openres *o_res = &data->o_res;
1487 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001488 struct rpc_message msg = {
1489 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1490 .rpc_argp = o_arg,
1491 .rpc_resp = o_res,
1492 .rpc_cred = data->owner->so_cred,
1493 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001494 struct rpc_task_setup task_setup_data = {
1495 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001496 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001497 .callback_ops = &nfs4_open_ops,
1498 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001499 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001500 .flags = RPC_TASK_ASYNC,
1501 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001502 int status;
1503
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001504 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001505 data->rpc_done = 0;
1506 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001507 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001508 if (isrecover)
1509 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001510 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001511 if (IS_ERR(task))
1512 return PTR_ERR(task);
1513 status = nfs4_wait_for_completion_rpc_task(task);
1514 if (status != 0) {
1515 data->cancelled = 1;
1516 smp_wmb();
1517 } else
1518 status = data->rpc_status;
1519 rpc_put_task(task);
1520
1521 return status;
1522}
1523
1524static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1525{
1526 struct inode *dir = data->dir->d_inode;
1527 struct nfs_openres *o_res = &data->o_res;
1528 int status;
1529
1530 status = nfs4_run_open_task(data, 1);
1531 if (status != 0 || !data->rpc_done)
1532 return status;
1533
1534 nfs_refresh_inode(dir, o_res->dir_attr);
1535
1536 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1537 status = _nfs4_proc_open_confirm(data);
1538 if (status != 0)
1539 return status;
1540 }
1541
1542 return status;
1543}
1544
1545/*
1546 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1547 */
1548static int _nfs4_proc_open(struct nfs4_opendata *data)
1549{
1550 struct inode *dir = data->dir->d_inode;
1551 struct nfs_server *server = NFS_SERVER(dir);
1552 struct nfs_openargs *o_arg = &data->o_arg;
1553 struct nfs_openres *o_res = &data->o_res;
1554 int status;
1555
1556 status = nfs4_run_open_task(data, 0);
Trond Myklebust3e309912007-07-07 13:19:59 -04001557 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001558 return status;
1559
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001560 if (o_arg->open_flags & O_CREAT) {
1561 update_changeattr(dir, &o_res->cinfo);
1562 nfs_post_op_update_inode(dir, o_res->dir_attr);
1563 } else
1564 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001565 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1566 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001568 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001570 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 }
1572 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001573 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001574 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575}
1576
Andy Adamsond83217c2011-03-01 01:34:17 +00001577static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001578{
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
Andy Adamsond83217c2011-03-01 01:34:17 +00001595static int nfs4_recover_expired_lease(struct nfs_server *server)
1596{
1597 return nfs4_client_recover_expired_lease(server->nfs_client);
1598}
1599
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600/*
1601 * OPEN_EXPIRED:
1602 * reclaim state on the server after a network partition.
1603 * Assumes caller holds the appropriate lock
1604 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001605static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001607 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001608 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001610 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1611 if (IS_ERR(opendata))
1612 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001613 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001614 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001615 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001616 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001617 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618}
1619
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001620static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001621{
Trond Myklebust539cd032007-06-05 11:46:42 -04001622 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001623 struct nfs4_exception exception = { };
1624 int err;
1625
1626 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001627 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001628 switch (err) {
1629 default:
1630 goto out;
1631 case -NFS4ERR_GRACE:
1632 case -NFS4ERR_DELAY:
1633 nfs4_handle_exception(server, err, &exception);
1634 err = 0;
1635 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001636 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001637out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001638 return err;
1639}
1640
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1642{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001644 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Trond Myklebust864472e2006-01-03 09:55:15 +01001646 ctx = nfs4_state_find_open_context(state);
1647 if (IS_ERR(ctx))
1648 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001649 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001650 put_nfs_open_context(ctx);
1651 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652}
1653
1654/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001655 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1656 * fields corresponding to attributes that were used to store the verifier.
1657 * Make sure we clobber those fields in the later setattr call
1658 */
1659static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1660{
1661 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1662 !(sattr->ia_valid & ATTR_ATIME_SET))
1663 sattr->ia_valid |= ATTR_ATIME;
1664
1665 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1666 !(sattr->ia_valid & ATTR_MTIME_SET))
1667 sattr->ia_valid |= ATTR_MTIME;
1668}
1669
1670/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001671 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001673static 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 -07001674{
1675 struct nfs4_state_owner *sp;
1676 struct nfs4_state *state = NULL;
1677 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001678 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001679 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
1681 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 status = -ENOMEM;
1683 if (!(sp = nfs4_get_state_owner(server, cred))) {
1684 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1685 goto out_err;
1686 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001687 status = nfs4_recover_expired_lease(server);
1688 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001689 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001690 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001691 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001692 status = -ENOMEM;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001693 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001694 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001695 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Trond Myklebustaac00a82007-07-05 19:02:21 -04001697 if (path->dentry->d_inode != NULL)
1698 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1699
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001700 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001702 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001704 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001705 status = PTR_ERR(state);
1706 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001707 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001708 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001709 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001710
1711 if (opendata->o_arg.open_flags & O_EXCL) {
1712 nfs4_exclusive_attrset(opendata, sattr);
1713
1714 nfs_fattr_init(opendata->o_res.f_attr);
1715 status = nfs4_do_setattr(state->inode, cred,
1716 opendata->o_res.f_attr, sattr,
1717 state);
1718 if (status == 0)
1719 nfs_setattr_update_inode(state->inode, sattr);
1720 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1721 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001722 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 *res = state;
1725 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001726err_opendata_put:
1727 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001728err_put_state_owner:
1729 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 *res = NULL;
1732 return status;
1733}
1734
1735
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001736static 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 -07001737{
1738 struct nfs4_exception exception = { };
1739 struct nfs4_state *res;
1740 int status;
1741
1742 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001743 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 if (status == 0)
1745 break;
1746 /* NOTE: BAD_SEQID means the server and client disagree about the
1747 * book-keeping w.r.t. state-changing operations
1748 * (OPEN/CLOSE/LOCK/LOCKU...)
1749 * It is actually a sign of a bug on the client or on the server.
1750 *
1751 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001752 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 * have unhashed the old state_owner for us, and that we can
1754 * therefore safely retry using a new one. We should still warn
1755 * the user though...
1756 */
1757 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001758 printk(KERN_WARNING "NFS: v4 server %s "
1759 " returned a bad sequence-id error!\n",
1760 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 exception.retry = 1;
1762 continue;
1763 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001764 /*
1765 * BAD_STATEID on OPEN means that the server cancelled our
1766 * state before it received the OPEN_CONFIRM.
1767 * Recover by retrying the request as per the discussion
1768 * on Page 181 of RFC3530.
1769 */
1770 if (status == -NFS4ERR_BAD_STATEID) {
1771 exception.retry = 1;
1772 continue;
1773 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001774 if (status == -EAGAIN) {
1775 /* We must have found a delegation */
1776 exception.retry = 1;
1777 continue;
1778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1780 status, &exception));
1781 } while (exception.retry);
1782 return res;
1783}
1784
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001785static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1786 struct nfs_fattr *fattr, struct iattr *sattr,
1787 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001789 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001791 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 .iap = sattr,
1793 .server = server,
1794 .bitmask = server->attr_bitmask,
1795 };
1796 struct nfs_setattrres res = {
1797 .fattr = fattr,
1798 .server = server,
1799 };
1800 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001801 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1802 .rpc_argp = &arg,
1803 .rpc_resp = &res,
1804 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001806 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001807 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
Trond Myklebust0e574af2005-10-27 22:12:38 -04001809 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001811 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1812 /* Use that stateid */
1813 } else if (state != NULL) {
Trond Myklebust77041ed2010-07-01 12:49:11 -04001814 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001815 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1817
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001818 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001819 if (status == 0 && state != NULL)
1820 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001821 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822}
1823
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001824static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1825 struct nfs_fattr *fattr, struct iattr *sattr,
1826 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001828 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 struct nfs4_exception exception = { };
1830 int err;
1831 do {
1832 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001833 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 &exception);
1835 } while (exception.retry);
1836 return err;
1837}
1838
1839struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001840 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 struct inode *inode;
1842 struct nfs4_state *state;
1843 struct nfs_closeargs arg;
1844 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001845 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001846 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00001847 bool roc;
1848 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849};
1850
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001851static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001852{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001853 struct nfs4_closedata *calldata = data;
1854 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001855
Fred Isamanf7e89172011-01-06 11:36:32 +00001856 if (calldata->roc)
1857 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001858 nfs4_put_open_state(calldata->state);
1859 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001860 nfs4_put_state_owner(sp);
Jan Blunck31f31db12008-05-02 13:42:45 -07001861 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001862 kfree(calldata);
1863}
1864
Trond Myklebust88069f72009-12-08 08:33:16 -05001865static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1866 fmode_t fmode)
1867{
1868 spin_lock(&state->owner->so_lock);
1869 if (!(fmode & FMODE_READ))
1870 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1871 if (!(fmode & FMODE_WRITE))
1872 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1873 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1874 spin_unlock(&state->owner->so_lock);
1875}
1876
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001877static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001879 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 struct nfs_server *server = NFS_SERVER(calldata->inode);
1882
Trond Myklebust14516c32010-07-31 14:29:06 -04001883 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1884 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 /* hmm. we are done with the inode, and in the process of freeing
1886 * the state_owner. we keep this around to process errors
1887 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 switch (task->tk_status) {
1889 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00001890 if (calldata->roc)
1891 pnfs_roc_set_barrier(state->inode,
1892 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04001893 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001894 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001895 nfs4_close_clear_stateid_flags(state,
1896 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 break;
1898 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001899 case -NFS4ERR_OLD_STATEID:
1900 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001902 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001903 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001905 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1906 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001908 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001909 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910}
1911
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001912static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001914 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001915 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001916 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001917
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001918 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001919 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001920
Trond Myklebust88069f72009-12-08 08:33:16 -05001921 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1922 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001923 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001924 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001925 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001926 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001927 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1928 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1929 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001930 }
1931 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001932 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1933 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1934 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001935 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001936 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001937 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05001938
1939 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001940 /* Note: exit _without_ calling nfs4_close_done */
1941 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001942 return;
1943 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001944
Fred Isamanf7e89172011-01-06 11:36:32 +00001945 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001946 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00001947 if (calldata->roc &&
1948 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
1949 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
1950 task, NULL);
1951 return;
1952 }
1953 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001954
Trond Myklebust516a6af2005-10-27 22:12:41 -04001955 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001956 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04001957 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Andy Adamson19ddab02009-04-01 09:22:20 -04001958 &calldata->arg.seq_args, &calldata->res.seq_res,
1959 1, task))
1960 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001961 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962}
1963
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001964static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001965 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001966 .rpc_call_done = nfs4_close_done,
1967 .rpc_release = nfs4_free_closedata,
1968};
1969
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970/*
1971 * It is possible for data to be read/written from a mem-mapped file
1972 * after the sys_close call (which hits the vfs layer as a flush).
1973 * This means that we can't safely call nfsv4 close on a file until
1974 * the inode is cleared. This in turn means that we are not good
1975 * NFSv4 citizens - we do not indicate to the server to update the file's
1976 * share state even when we are done with one of the three share
1977 * stateid's in the inode.
1978 *
1979 * NOTE: Caller must be holding the sp->so_owner semaphore!
1980 */
Fred Isamanf7e89172011-01-06 11:36:32 +00001981int 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 -07001982{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001983 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04001985 struct nfs4_state_owner *sp = state->owner;
1986 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001987 struct rpc_message msg = {
1988 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
1989 .rpc_cred = state->owner->so_cred,
1990 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001991 struct rpc_task_setup task_setup_data = {
1992 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001993 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001994 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05001995 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001996 .flags = RPC_TASK_ASYNC,
1997 };
Trond Myklebust95121352005-10-18 14:20:12 -07001998 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002000 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002002 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002003 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002005 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002006 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002008 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002009 if (calldata->arg.seqid == NULL)
2010 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002011 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002012 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002013 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002014 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002015 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002016 calldata->roc = roc;
Al Virof6948692010-01-30 13:51:04 -05002017 path_get(path);
2018 calldata->path = *path;
Trond Myklebust95121352005-10-18 14:20:12 -07002019
Trond Myklebust1174dd12010-12-21 10:52:24 -05002020 msg.rpc_argp = &calldata->arg;
2021 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002022 task_setup_data.callback_data = calldata;
2023 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002024 if (IS_ERR(task))
2025 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002026 status = 0;
2027 if (wait)
2028 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002029 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002030 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002031out_free_calldata:
2032 kfree(calldata);
2033out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002034 if (roc)
2035 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002036 nfs4_put_open_state(state);
2037 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002038 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039}
2040
Trond Myklebust2b484292010-09-17 10:56:51 -04002041static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002042nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 struct nfs4_state *state;
2045
Trond Myklebust565277f2007-10-15 18:17:53 -04002046 /* Protect against concurrent sillydeletes */
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002047 state = nfs4_do_open(dir, &ctx->path, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002048 if (IS_ERR(state))
2049 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002050 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002051 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052}
2053
Trond Myklebust1185a552009-12-03 15:54:02 -05002054static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002055{
2056 if (ctx->state == NULL)
2057 return;
2058 if (is_sync)
2059 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2060 else
2061 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2062}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
2064static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2065{
Benny Halevy43652ad2009-04-01 09:21:54 -04002066 struct nfs4_server_caps_arg args = {
2067 .fhandle = fhandle,
2068 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 struct nfs4_server_caps_res res = {};
2070 struct rpc_message msg = {
2071 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002072 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 .rpc_resp = &res,
2074 };
2075 int status;
2076
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002077 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 if (status == 0) {
2079 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002080 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2081 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2082 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2083 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2084 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2086 server->caps |= NFS_CAP_ACLS;
2087 if (res.has_links != 0)
2088 server->caps |= NFS_CAP_HARDLINKS;
2089 if (res.has_symlinks != 0)
2090 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002091 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2092 server->caps |= NFS_CAP_FILEID;
2093 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2094 server->caps |= NFS_CAP_MODE;
2095 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2096 server->caps |= NFS_CAP_NLINK;
2097 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2098 server->caps |= NFS_CAP_OWNER;
2099 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2100 server->caps |= NFS_CAP_OWNER_GROUP;
2101 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2102 server->caps |= NFS_CAP_ATIME;
2103 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2104 server->caps |= NFS_CAP_CTIME;
2105 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2106 server->caps |= NFS_CAP_MTIME;
2107
Trond Myklebusta65318b2009-03-11 14:10:28 -04002108 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2109 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2110 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 server->acl_bitmask = res.acl_bitmask;
2112 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002113
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 return status;
2115}
2116
Trond Myklebust55a97592006-06-09 09:34:19 -04002117int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118{
2119 struct nfs4_exception exception = { };
2120 int err;
2121 do {
2122 err = nfs4_handle_exception(server,
2123 _nfs4_server_capabilities(server, fhandle),
2124 &exception);
2125 } while (exception.retry);
2126 return err;
2127}
2128
2129static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2130 struct nfs_fsinfo *info)
2131{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 struct nfs4_lookup_root_arg args = {
2133 .bitmask = nfs4_fattr_bitmap,
2134 };
2135 struct nfs4_lookup_res res = {
2136 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002137 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 .fh = fhandle,
2139 };
2140 struct rpc_message msg = {
2141 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2142 .rpc_argp = &args,
2143 .rpc_resp = &res,
2144 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002145
Trond Myklebust0e574af2005-10-27 22:12:38 -04002146 nfs_fattr_init(info->fattr);
Trond Myklebustfccba802009-07-21 16:48:07 -04002147 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148}
2149
2150static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2151 struct nfs_fsinfo *info)
2152{
2153 struct nfs4_exception exception = { };
2154 int err;
2155 do {
2156 err = nfs4_handle_exception(server,
2157 _nfs4_lookup_root(server, fhandle, info),
2158 &exception);
2159 } while (exception.retry);
2160 return err;
2161}
2162
David Howells54ceac42006-08-22 20:06:13 -04002163/*
2164 * get the file handle for the "/" directory on the server
2165 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002167 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 int status;
2170
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 if (status == 0)
2173 status = nfs4_server_capabilities(server, fhandle);
2174 if (status == 0)
2175 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002176 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177}
2178
Manoj Naik6b97fd32006-06-09 09:34:29 -04002179/*
2180 * Get locations and (maybe) other attributes of a referral.
2181 * Note that we'll actually follow the referral later when
2182 * we detect fsid mismatch in inode revalidation
2183 */
Trond Myklebust56659e92007-07-17 21:52:39 -04002184static 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 -04002185{
2186 int status = -ENOMEM;
2187 struct page *page = NULL;
2188 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002189
2190 page = alloc_page(GFP_KERNEL);
2191 if (page == NULL)
2192 goto out;
2193 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2194 if (locations == NULL)
2195 goto out;
2196
Trond Myklebustc228fd32007-01-13 02:28:11 -05002197 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002198 if (status != 0)
2199 goto out;
2200 /* Make sure server returned a different fsid for the referral */
2201 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07002202 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 -04002203 status = -EIO;
2204 goto out;
2205 }
2206
2207 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2208 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2209 if (!fattr->mode)
2210 fattr->mode = S_IFDIR;
2211 memset(fhandle, 0, sizeof(struct nfs_fh));
2212out:
2213 if (page)
2214 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002215 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002216 return status;
2217}
2218
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2220{
2221 struct nfs4_getattr_arg args = {
2222 .fh = fhandle,
2223 .bitmask = server->attr_bitmask,
2224 };
2225 struct nfs4_getattr_res res = {
2226 .fattr = fattr,
2227 .server = server,
2228 };
2229 struct rpc_message msg = {
2230 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2231 .rpc_argp = &args,
2232 .rpc_resp = &res,
2233 };
2234
Trond Myklebust0e574af2005-10-27 22:12:38 -04002235 nfs_fattr_init(fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002236 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237}
2238
2239static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2240{
2241 struct nfs4_exception exception = { };
2242 int err;
2243 do {
2244 err = nfs4_handle_exception(server,
2245 _nfs4_proc_getattr(server, fhandle, fattr),
2246 &exception);
2247 } while (exception.retry);
2248 return err;
2249}
2250
2251/*
2252 * The file is not closed if it is opened due to the a request to change
2253 * the size of the file. The open call will not be needed once the
2254 * VFS layer lookup-intents are implemented.
2255 *
2256 * Close is called when the inode is destroyed.
2257 * If we haven't opened the file for O_WRONLY, we
2258 * need to in the size_change case to obtain a stateid.
2259 *
2260 * Got race?
2261 * Because OPEN is always done by name in nfsv4, it is
2262 * possible that we opened a different file by the same
2263 * name. We can recognize this race condition, but we
2264 * can't do anything about it besides returning an error.
2265 *
2266 * This will be fixed with VFS changes (lookup-intent).
2267 */
2268static int
2269nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2270 struct iattr *sattr)
2271{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002272 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002273 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002274 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 int status;
2276
Trond Myklebust0e574af2005-10-27 22:12:38 -04002277 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
Trond Myklebustd5308382005-11-04 15:33:38 -05002279 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002280 if (sattr->ia_valid & ATTR_FILE) {
2281 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002282
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002283 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002284 if (ctx) {
2285 cred = ctx->cred;
2286 state = ctx->state;
2287 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002288 }
2289
2290 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002291 if (status == 0)
2292 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 return status;
2294}
2295
Trond Myklebust56659e92007-07-17 21:52:39 -04002296static int _nfs4_proc_lookupfh(struct nfs_server *server, const struct nfs_fh *dirfh,
2297 const struct qstr *name, struct nfs_fh *fhandle,
David Howells2b3de442006-08-22 20:06:09 -04002298 struct nfs_fattr *fattr)
2299{
2300 int status;
2301 struct nfs4_lookup_arg args = {
2302 .bitmask = server->attr_bitmask,
2303 .dir_fh = dirfh,
2304 .name = name,
2305 };
2306 struct nfs4_lookup_res res = {
2307 .server = server,
2308 .fattr = fattr,
2309 .fh = fhandle,
2310 };
2311 struct rpc_message msg = {
2312 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2313 .rpc_argp = &args,
2314 .rpc_resp = &res,
2315 };
2316
2317 nfs_fattr_init(fattr);
2318
2319 dprintk("NFS call lookupfh %s\n", name->name);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002320 status = nfs4_call_sync(server, &msg, &args, &res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002321 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002322 return status;
2323}
2324
2325static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2326 struct qstr *name, struct nfs_fh *fhandle,
2327 struct nfs_fattr *fattr)
2328{
2329 struct nfs4_exception exception = { };
2330 int err;
2331 do {
Trond Myklebust4e56e082007-07-01 18:13:52 -04002332 err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr);
2333 /* FIXME: !!!! */
2334 if (err == -NFS4ERR_MOVED) {
2335 err = -EREMOTE;
2336 break;
2337 }
2338 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002339 } while (exception.retry);
2340 return err;
2341}
2342
Trond Myklebust56659e92007-07-17 21:52:39 -04002343static int _nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2345{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002346 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
2348 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002349 status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002350 if (status == -NFS4ERR_MOVED)
2351 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 dprintk("NFS reply lookup: %d\n", status);
2353 return status;
2354}
2355
2356static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2357{
2358 struct nfs4_exception exception = { };
2359 int err;
2360 do {
2361 err = nfs4_handle_exception(NFS_SERVER(dir),
2362 _nfs4_proc_lookup(dir, name, fhandle, fattr),
2363 &exception);
2364 } while (exception.retry);
2365 return err;
2366}
2367
2368static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2369{
Trond Myklebust76b32992007-08-10 17:45:11 -04002370 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 struct nfs4_accessargs args = {
2372 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002373 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002375 struct nfs4_accessres res = {
2376 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002377 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 struct rpc_message msg = {
2379 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2380 .rpc_argp = &args,
2381 .rpc_resp = &res,
2382 .rpc_cred = entry->cred,
2383 };
2384 int mode = entry->mask;
2385 int status;
2386
2387 /*
2388 * Determine which access bits we want to ask for...
2389 */
2390 if (mode & MAY_READ)
2391 args.access |= NFS4_ACCESS_READ;
2392 if (S_ISDIR(inode->i_mode)) {
2393 if (mode & MAY_WRITE)
2394 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2395 if (mode & MAY_EXEC)
2396 args.access |= NFS4_ACCESS_LOOKUP;
2397 } else {
2398 if (mode & MAY_WRITE)
2399 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2400 if (mode & MAY_EXEC)
2401 args.access |= NFS4_ACCESS_EXECUTE;
2402 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002403
2404 res.fattr = nfs_alloc_fattr();
2405 if (res.fattr == NULL)
2406 return -ENOMEM;
2407
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002408 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 if (!status) {
2410 entry->mask = 0;
2411 if (res.access & NFS4_ACCESS_READ)
2412 entry->mask |= MAY_READ;
2413 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2414 entry->mask |= MAY_WRITE;
2415 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2416 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002417 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002419 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 return status;
2421}
2422
2423static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2424{
2425 struct nfs4_exception exception = { };
2426 int err;
2427 do {
2428 err = nfs4_handle_exception(NFS_SERVER(inode),
2429 _nfs4_proc_access(inode, entry),
2430 &exception);
2431 } while (exception.retry);
2432 return err;
2433}
2434
2435/*
2436 * TODO: For the time being, we don't try to get any attributes
2437 * along with any of the zero-copy operations READ, READDIR,
2438 * READLINK, WRITE.
2439 *
2440 * In the case of the first three, we want to put the GETATTR
2441 * after the read-type operation -- this is because it is hard
2442 * to predict the length of a GETATTR response in v4, and thus
2443 * align the READ data correctly. This means that the GETATTR
2444 * may end up partially falling into the page cache, and we should
2445 * shift it into the 'tail' of the xdr_buf before processing.
2446 * To do this efficiently, we need to know the total length
2447 * of data received, which doesn't seem to be available outside
2448 * of the RPC layer.
2449 *
2450 * In the case of WRITE, we also want to put the GETATTR after
2451 * the operation -- in this case because we want to make sure
2452 * we get the post-operation mtime and size. This means that
2453 * we can't use xdr_encode_pages() as written: we need a variant
2454 * of it which would leave room in the 'tail' iovec.
2455 *
2456 * Both of these changes to the XDR layer would in fact be quite
2457 * minor, but I decided to leave them for a subsequent patch.
2458 */
2459static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2460 unsigned int pgbase, unsigned int pglen)
2461{
2462 struct nfs4_readlink args = {
2463 .fh = NFS_FH(inode),
2464 .pgbase = pgbase,
2465 .pglen = pglen,
2466 .pages = &page,
2467 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002468 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 struct rpc_message msg = {
2470 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2471 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002472 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 };
2474
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002475 return nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476}
2477
2478static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2479 unsigned int pgbase, unsigned int pglen)
2480{
2481 struct nfs4_exception exception = { };
2482 int err;
2483 do {
2484 err = nfs4_handle_exception(NFS_SERVER(inode),
2485 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2486 &exception);
2487 } while (exception.retry);
2488 return err;
2489}
2490
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491/*
2492 * Got race?
2493 * We will need to arrange for the VFS layer to provide an atomic open.
2494 * Until then, this create/open method is prone to inefficiency and race
2495 * conditions due to the lookup, create, and open VFS calls from sys_open()
2496 * placed on the wire.
2497 *
2498 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2499 * The file will be opened again in the subsequent VFS open call
2500 * (nfs4_proc_file_open).
2501 *
2502 * The open for read will just hang around to be used by any process that
2503 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2504 */
2505
2506static int
2507nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002508 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509{
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002510 struct path my_path = {
Trond Myklebustad389da2007-06-05 12:30:00 -04002511 .dentry = dentry,
2512 };
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002513 struct path *path = &my_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002515 struct rpc_cred *cred = NULL;
2516 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 int status = 0;
2518
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002519 if (ctx != NULL) {
2520 cred = ctx->cred;
2521 path = &ctx->path;
2522 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002524 sattr->ia_mode &= ~current_umask();
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002525 state = nfs4_do_open(dir, path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002526 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 if (IS_ERR(state)) {
2528 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002529 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002531 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002532 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002533 if (ctx != NULL)
2534 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002535 else
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002536 nfs4_close_sync(path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537out:
2538 return status;
2539}
2540
2541static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2542{
Trond Myklebust16e42952005-10-27 22:12:44 -04002543 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002544 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002546 .name.len = name->len,
2547 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002548 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002550 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002551 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002552 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002554 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2555 .rpc_argp = &args,
2556 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002558 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
Trond Myklebustd3468902010-04-16 16:22:50 -04002560 res.dir_attr = nfs_alloc_fattr();
2561 if (res.dir_attr == NULL)
2562 goto out;
2563
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002564 status = nfs4_call_sync(server, &msg, &args, &res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002565 if (status == 0) {
2566 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002567 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002568 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002569 nfs_free_fattr(res.dir_attr);
2570out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 return status;
2572}
2573
2574static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2575{
2576 struct nfs4_exception exception = { };
2577 int err;
2578 do {
2579 err = nfs4_handle_exception(NFS_SERVER(dir),
2580 _nfs4_proc_remove(dir, name),
2581 &exception);
2582 } while (exception.retry);
2583 return err;
2584}
2585
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002586static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002588 struct nfs_server *server = NFS_SERVER(dir);
2589 struct nfs_removeargs *args = msg->rpc_argp;
2590 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
Trond Myklebusta65318b2009-03-11 14:10:28 -04002592 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002593 res->server = server;
Benny Halevydfb4f3092010-09-24 09:17:01 -04002594 res->seq_res.sr_slot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596}
2597
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002598static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002600 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2601
Trond Myklebust14516c32010-07-31 14:29:06 -04002602 if (!nfs4_sequence_done(task, &res->seq_res))
2603 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002604 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002605 return 0;
2606 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002607 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002608 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609}
2610
Jeff Laytond3d41522010-09-17 17:31:57 -04002611static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2612{
2613 struct nfs_server *server = NFS_SERVER(dir);
2614 struct nfs_renameargs *arg = msg->rpc_argp;
2615 struct nfs_renameres *res = msg->rpc_resp;
2616
2617 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2618 arg->bitmask = server->attr_bitmask;
2619 res->server = server;
2620}
2621
2622static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2623 struct inode *new_dir)
2624{
2625 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2626
2627 if (!nfs4_sequence_done(task, &res->seq_res))
2628 return 0;
2629 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2630 return 0;
2631
2632 update_changeattr(old_dir, &res->old_cinfo);
2633 nfs_post_op_update_inode(old_dir, res->old_fattr);
2634 update_changeattr(new_dir, &res->new_cinfo);
2635 nfs_post_op_update_inode(new_dir, res->new_fattr);
2636 return 1;
2637}
2638
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2640 struct inode *new_dir, struct qstr *new_name)
2641{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002642 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002643 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 .old_dir = NFS_FH(old_dir),
2645 .new_dir = NFS_FH(new_dir),
2646 .old_name = old_name,
2647 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002648 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002650 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002651 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002652 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 struct rpc_message msg = {
2654 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2655 .rpc_argp = &arg,
2656 .rpc_resp = &res,
2657 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002658 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
Trond Myklebust011fff72010-04-16 16:22:49 -04002660 res.old_fattr = nfs_alloc_fattr();
2661 res.new_fattr = nfs_alloc_fattr();
2662 if (res.old_fattr == NULL || res.new_fattr == NULL)
2663 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
Trond Myklebust011fff72010-04-16 16:22:49 -04002665 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 if (!status) {
2667 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002668 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002670 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002672out:
2673 nfs_free_fattr(res.new_fattr);
2674 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 return status;
2676}
2677
2678static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2679 struct inode *new_dir, struct qstr *new_name)
2680{
2681 struct nfs4_exception exception = { };
2682 int err;
2683 do {
2684 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2685 _nfs4_proc_rename(old_dir, old_name,
2686 new_dir, new_name),
2687 &exception);
2688 } while (exception.retry);
2689 return err;
2690}
2691
2692static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2693{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002694 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 struct nfs4_link_arg arg = {
2696 .fh = NFS_FH(inode),
2697 .dir_fh = NFS_FH(dir),
2698 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002699 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002701 struct nfs4_link_res res = {
2702 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002703 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 struct rpc_message msg = {
2705 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2706 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002707 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002709 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710
Trond Myklebust136f2622010-04-16 16:22:49 -04002711 res.fattr = nfs_alloc_fattr();
2712 res.dir_attr = nfs_alloc_fattr();
2713 if (res.fattr == NULL || res.dir_attr == NULL)
2714 goto out;
2715
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002716 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002717 if (!status) {
2718 update_changeattr(dir, &res.cinfo);
2719 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002720 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002721 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002722out:
2723 nfs_free_fattr(res.dir_attr);
2724 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 return status;
2726}
2727
2728static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2729{
2730 struct nfs4_exception exception = { };
2731 int err;
2732 do {
2733 err = nfs4_handle_exception(NFS_SERVER(inode),
2734 _nfs4_proc_link(inode, dir, name),
2735 &exception);
2736 } while (exception.retry);
2737 return err;
2738}
2739
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002740struct nfs4_createdata {
2741 struct rpc_message msg;
2742 struct nfs4_create_arg arg;
2743 struct nfs4_create_res res;
2744 struct nfs_fh fh;
2745 struct nfs_fattr fattr;
2746 struct nfs_fattr dir_fattr;
2747};
2748
2749static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2750 struct qstr *name, struct iattr *sattr, u32 ftype)
2751{
2752 struct nfs4_createdata *data;
2753
2754 data = kzalloc(sizeof(*data), GFP_KERNEL);
2755 if (data != NULL) {
2756 struct nfs_server *server = NFS_SERVER(dir);
2757
2758 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2759 data->msg.rpc_argp = &data->arg;
2760 data->msg.rpc_resp = &data->res;
2761 data->arg.dir_fh = NFS_FH(dir);
2762 data->arg.server = server;
2763 data->arg.name = name;
2764 data->arg.attrs = sattr;
2765 data->arg.ftype = ftype;
2766 data->arg.bitmask = server->attr_bitmask;
2767 data->res.server = server;
2768 data->res.fh = &data->fh;
2769 data->res.fattr = &data->fattr;
2770 data->res.dir_fattr = &data->dir_fattr;
2771 nfs_fattr_init(data->res.fattr);
2772 nfs_fattr_init(data->res.dir_fattr);
2773 }
2774 return data;
2775}
2776
2777static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2778{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002779 int status = nfs4_call_sync(NFS_SERVER(dir), &data->msg,
2780 &data->arg, &data->res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002781 if (status == 0) {
2782 update_changeattr(dir, &data->res.dir_cinfo);
2783 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2784 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2785 }
2786 return status;
2787}
2788
2789static void nfs4_free_createdata(struct nfs4_createdata *data)
2790{
2791 kfree(data);
2792}
2793
Chuck Lever4f390c12006-08-22 20:06:22 -04002794static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002795 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002797 struct nfs4_createdata *data;
2798 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
Chuck Lever94a6d752006-08-22 20:06:23 -04002800 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002801 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002802
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002803 status = -ENOMEM;
2804 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2805 if (data == NULL)
2806 goto out;
2807
2808 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2809 data->arg.u.symlink.pages = &page;
2810 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002812 status = nfs4_do_create(dir, dentry, data);
2813
2814 nfs4_free_createdata(data);
2815out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 return status;
2817}
2818
Chuck Lever4f390c12006-08-22 20:06:22 -04002819static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002820 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821{
2822 struct nfs4_exception exception = { };
2823 int err;
2824 do {
2825 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002826 _nfs4_proc_symlink(dir, dentry, page,
2827 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 &exception);
2829 } while (exception.retry);
2830 return err;
2831}
2832
2833static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2834 struct iattr *sattr)
2835{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002836 struct nfs4_createdata *data;
2837 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002839 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2840 if (data == NULL)
2841 goto out;
2842
2843 status = nfs4_do_create(dir, dentry, data);
2844
2845 nfs4_free_createdata(data);
2846out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 return status;
2848}
2849
2850static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2851 struct iattr *sattr)
2852{
2853 struct nfs4_exception exception = { };
2854 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002855
2856 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 do {
2858 err = nfs4_handle_exception(NFS_SERVER(dir),
2859 _nfs4_proc_mkdir(dir, dentry, sattr),
2860 &exception);
2861 } while (exception.retry);
2862 return err;
2863}
2864
2865static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002866 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867{
2868 struct inode *dir = dentry->d_inode;
2869 struct nfs4_readdir_arg args = {
2870 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002871 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 .pgbase = 0,
2873 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002874 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04002875 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 };
2877 struct nfs4_readdir_res res;
2878 struct rpc_message msg = {
2879 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2880 .rpc_argp = &args,
2881 .rpc_resp = &res,
2882 .rpc_cred = cred,
2883 };
2884 int status;
2885
Harvey Harrison3110ff82008-05-02 13:42:44 -07002886 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002887 dentry->d_parent->d_name.name,
2888 dentry->d_name.name,
2889 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2891 res.pgbase = args.pgbase;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002892 status = nfs4_call_sync(NFS_SERVER(dir), &msg, &args, &res, 0);
Trond Myklebustac396122010-11-15 20:26:22 -05002893 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05002895 status += args.pgbase;
2896 }
Trond Myklebustc4812992007-09-28 17:11:45 -04002897
2898 nfs_invalidate_atime(dir);
2899
Harvey Harrison3110ff82008-05-02 13:42:44 -07002900 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 return status;
2902}
2903
2904static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002905 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906{
2907 struct nfs4_exception exception = { };
2908 int err;
2909 do {
2910 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2911 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002912 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 &exception);
2914 } while (exception.retry);
2915 return err;
2916}
2917
2918static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2919 struct iattr *sattr, dev_t rdev)
2920{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002921 struct nfs4_createdata *data;
2922 int mode = sattr->ia_mode;
2923 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
2925 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2926 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002927
2928 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2929 if (data == NULL)
2930 goto out;
2931
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002933 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002935 data->arg.ftype = NF4BLK;
2936 data->arg.u.device.specdata1 = MAJOR(rdev);
2937 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 }
2939 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002940 data->arg.ftype = NF4CHR;
2941 data->arg.u.device.specdata1 = MAJOR(rdev);
2942 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002945 status = nfs4_do_create(dir, dentry, data);
2946
2947 nfs4_free_createdata(data);
2948out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 return status;
2950}
2951
2952static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2953 struct iattr *sattr, dev_t rdev)
2954{
2955 struct nfs4_exception exception = { };
2956 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002957
2958 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 do {
2960 err = nfs4_handle_exception(NFS_SERVER(dir),
2961 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
2962 &exception);
2963 } while (exception.retry);
2964 return err;
2965}
2966
2967static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
2968 struct nfs_fsstat *fsstat)
2969{
2970 struct nfs4_statfs_arg args = {
2971 .fh = fhandle,
2972 .bitmask = server->attr_bitmask,
2973 };
Benny Halevy24ad1482009-04-01 09:21:56 -04002974 struct nfs4_statfs_res res = {
2975 .fsstat = fsstat,
2976 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 struct rpc_message msg = {
2978 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
2979 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04002980 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 };
2982
Trond Myklebust0e574af2005-10-27 22:12:38 -04002983 nfs_fattr_init(fsstat->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002984 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985}
2986
2987static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
2988{
2989 struct nfs4_exception exception = { };
2990 int err;
2991 do {
2992 err = nfs4_handle_exception(server,
2993 _nfs4_proc_statfs(server, fhandle, fsstat),
2994 &exception);
2995 } while (exception.retry);
2996 return err;
2997}
2998
2999static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3000 struct nfs_fsinfo *fsinfo)
3001{
3002 struct nfs4_fsinfo_arg args = {
3003 .fh = fhandle,
3004 .bitmask = server->attr_bitmask,
3005 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003006 struct nfs4_fsinfo_res res = {
3007 .fsinfo = fsinfo,
3008 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 struct rpc_message msg = {
3010 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3011 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003012 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 };
3014
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003015 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016}
3017
3018static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3019{
3020 struct nfs4_exception exception = { };
3021 int err;
3022
3023 do {
3024 err = nfs4_handle_exception(server,
3025 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3026 &exception);
3027 } while (exception.retry);
3028 return err;
3029}
3030
3031static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3032{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003033 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3035}
3036
3037static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3038 struct nfs_pathconf *pathconf)
3039{
3040 struct nfs4_pathconf_arg args = {
3041 .fh = fhandle,
3042 .bitmask = server->attr_bitmask,
3043 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003044 struct nfs4_pathconf_res res = {
3045 .pathconf = pathconf,
3046 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 struct rpc_message msg = {
3048 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3049 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003050 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 };
3052
3053 /* None of the pathconf attributes are mandatory to implement */
3054 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3055 memset(pathconf, 0, sizeof(*pathconf));
3056 return 0;
3057 }
3058
Trond Myklebust0e574af2005-10-27 22:12:38 -04003059 nfs_fattr_init(pathconf->fattr);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003060 return nfs4_call_sync(server, &msg, &args, &res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061}
3062
3063static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3064 struct nfs_pathconf *pathconf)
3065{
3066 struct nfs4_exception exception = { };
3067 int err;
3068
3069 do {
3070 err = nfs4_handle_exception(server,
3071 _nfs4_proc_pathconf(server, fhandle, pathconf),
3072 &exception);
3073 } while (exception.retry);
3074 return err;
3075}
3076
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003077static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078{
Trond Myklebustec06c092006-03-20 13:44:27 -05003079 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080
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
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003092static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3093{
3094
3095 dprintk("--> %s\n", __func__);
3096
3097 if (!nfs4_sequence_done(task, &data->res.seq_res))
3098 return -EAGAIN;
3099
3100 return data->read_done_cb(task, data);
3101}
3102
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003103static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003106 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003107 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108}
3109
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003110/* Reset the the nfs_read_data to send the read to the MDS. */
3111void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3112{
3113 dprintk("%s Reset task for i/o through\n", __func__);
3114 put_lseg(data->lseg);
3115 data->lseg = NULL;
3116 /* offsets will differ in the dense stripe case */
3117 data->args.offset = data->mds_offset;
3118 data->ds_clp = NULL;
3119 data->args.fh = NFS_FH(data->inode);
3120 data->read_done_cb = nfs4_read_done_cb;
3121 task->tk_ops = data->mds_ops;
3122 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3123}
3124EXPORT_SYMBOL_GPL(nfs4_reset_read);
3125
Fred Isamanb029bc92011-03-03 15:13:42 +00003126static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 struct inode *inode = data->inode;
3129
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003130 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003131 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003132 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003134 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003136 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003137 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003138 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139}
3140
Fred Isamanb029bc92011-03-03 15:13:42 +00003141static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3142{
3143 if (!nfs4_sequence_done(task, &data->res.seq_res))
3144 return -EAGAIN;
3145 return data->write_done_cb(task, data);
3146}
3147
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003148static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003150 struct nfs_server *server = NFS_SERVER(data->inode);
3151
Fred Isaman7ffd1062011-03-03 15:13:46 +00003152 if (data->lseg) {
3153 data->args.bitmask = NULL;
3154 data->res.fattr = NULL;
3155 } else
3156 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isamanb029bc92011-03-03 15:13:42 +00003157 if (!data->write_done_cb)
3158 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003159 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 data->timestamp = jiffies;
3161
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003162 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163}
3164
Trond Myklebust788e7a82006-03-20 13:44:27 -05003165static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 struct inode *inode = data->inode;
3168
Trond Myklebust14516c32010-07-31 14:29:06 -04003169 if (!nfs4_sequence_done(task, &data->res.seq_res))
3170 return -EAGAIN;
3171
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003172 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003173 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003174 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003176 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003177 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178}
3179
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003180static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003182 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183
Trond Myklebusta65318b2009-03-11 14:10:28 -04003184 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003185 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003186 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187}
3188
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003189struct nfs4_renewdata {
3190 struct nfs_client *client;
3191 unsigned long timestamp;
3192};
3193
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194/*
3195 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3196 * standalone procedure for queueing an asynchronous RENEW.
3197 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003198static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003199{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003200 struct nfs4_renewdata *data = calldata;
3201 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003202
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003203 if (atomic_read(&clp->cl_count) > 1)
3204 nfs4_schedule_state_renewal(clp);
3205 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003206 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003207}
3208
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003209static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003211 struct nfs4_renewdata *data = calldata;
3212 struct nfs_client *clp = data->client;
3213 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214
3215 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003216 /* Unless we're shutting down, schedule state recovery! */
3217 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003218 nfs4_schedule_lease_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 return;
3220 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003221 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222}
3223
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003224static const struct rpc_call_ops nfs4_renew_ops = {
3225 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003226 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003227};
3228
David Howellsadfa6f92006-08-22 20:06:08 -04003229int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230{
3231 struct rpc_message msg = {
3232 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3233 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003234 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003236 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003238 if (!atomic_inc_not_zero(&clp->cl_count))
3239 return -EIO;
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003240 data = kmalloc(sizeof(*data), GFP_KERNEL);
3241 if (data == NULL)
3242 return -ENOMEM;
3243 data->client = clp;
3244 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003246 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247}
3248
David Howellsadfa6f92006-08-22 20:06:08 -04003249int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250{
3251 struct rpc_message msg = {
3252 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3253 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003254 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 };
3256 unsigned long now = jiffies;
3257 int status;
3258
3259 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3260 if (status < 0)
3261 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003262 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 return 0;
3264}
3265
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003266static inline int nfs4_server_supports_acls(struct nfs_server *server)
3267{
3268 return (server->caps & NFS_CAP_ACLS)
3269 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3270 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3271}
3272
3273/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3274 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3275 * the stack.
3276 */
3277#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3278
3279static void buf_to_pages(const void *buf, size_t buflen,
3280 struct page **pages, unsigned int *pgbase)
3281{
3282 const void *p = buf;
3283
3284 *pgbase = offset_in_page(buf);
3285 p -= *pgbase;
3286 while (p < buf + buflen) {
3287 *(pages++) = virt_to_page(p);
3288 p += PAGE_CACHE_SIZE;
3289 }
3290}
3291
Neil Hormane9e3d722011-03-04 19:26:03 -05003292static int buf_to_pages_noslab(const void *buf, size_t buflen,
3293 struct page **pages, unsigned int *pgbase)
3294{
3295 struct page *newpage, **spages;
3296 int rc = 0;
3297 size_t len;
3298 spages = pages;
3299
3300 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003301 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003302 newpage = alloc_page(GFP_KERNEL);
3303
3304 if (newpage == NULL)
3305 goto unwind;
3306 memcpy(page_address(newpage), buf, len);
3307 buf += len;
3308 buflen -= len;
3309 *pages++ = newpage;
3310 rc++;
3311 } while (buflen != 0);
3312
3313 return rc;
3314
3315unwind:
3316 for(; rc > 0; rc--)
3317 __free_page(spages[rc-1]);
3318 return -ENOMEM;
3319}
3320
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003321struct nfs4_cached_acl {
3322 int cached;
3323 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003324 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003325};
3326
3327static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003328{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003329 struct nfs_inode *nfsi = NFS_I(inode);
3330
3331 spin_lock(&inode->i_lock);
3332 kfree(nfsi->nfs4_acl);
3333 nfsi->nfs4_acl = acl;
3334 spin_unlock(&inode->i_lock);
3335}
3336
3337static void nfs4_zap_acl_attr(struct inode *inode)
3338{
3339 nfs4_set_cached_acl(inode, NULL);
3340}
3341
3342static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3343{
3344 struct nfs_inode *nfsi = NFS_I(inode);
3345 struct nfs4_cached_acl *acl;
3346 int ret = -ENOENT;
3347
3348 spin_lock(&inode->i_lock);
3349 acl = nfsi->nfs4_acl;
3350 if (acl == NULL)
3351 goto out;
3352 if (buf == NULL) /* user is just asking for length */
3353 goto out_len;
3354 if (acl->cached == 0)
3355 goto out;
3356 ret = -ERANGE; /* see getxattr(2) man page */
3357 if (acl->len > buflen)
3358 goto out;
3359 memcpy(buf, acl->data, acl->len);
3360out_len:
3361 ret = acl->len;
3362out:
3363 spin_unlock(&inode->i_lock);
3364 return ret;
3365}
3366
3367static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3368{
3369 struct nfs4_cached_acl *acl;
3370
3371 if (buf && acl_len <= PAGE_SIZE) {
3372 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3373 if (acl == NULL)
3374 goto out;
3375 acl->cached = 1;
3376 memcpy(acl->data, buf, acl_len);
3377 } else {
3378 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3379 if (acl == NULL)
3380 goto out;
3381 acl->cached = 0;
3382 }
3383 acl->len = acl_len;
3384out:
3385 nfs4_set_cached_acl(inode, acl);
3386}
3387
Trond Myklebust16b42892006-08-24 12:27:15 -04003388static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003389{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003390 struct page *pages[NFS4ACL_MAXPAGES];
3391 struct nfs_getaclargs args = {
3392 .fh = NFS_FH(inode),
3393 .acl_pages = pages,
3394 .acl_len = buflen,
3395 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003396 struct nfs_getaclres res = {
3397 .acl_len = buflen,
3398 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003399 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003400 struct rpc_message msg = {
3401 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3402 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003403 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003404 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003405 struct page *localpage = NULL;
3406 int ret;
3407
3408 if (buflen < PAGE_SIZE) {
3409 /* As long as we're doing a round trip to the server anyway,
3410 * let's be prepared for a page of acl data. */
3411 localpage = alloc_page(GFP_KERNEL);
3412 resp_buf = page_address(localpage);
3413 if (localpage == NULL)
3414 return -ENOMEM;
3415 args.acl_pages[0] = localpage;
3416 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003417 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003418 } else {
3419 resp_buf = buf;
3420 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3421 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003422 ret = nfs4_call_sync(NFS_SERVER(inode), &msg, &args, &res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003423 if (ret)
3424 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003425 if (res.acl_len > args.acl_len)
3426 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003427 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003428 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003429 if (buf) {
3430 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003431 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003432 goto out_free;
3433 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003434 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003435 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003436 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003437out_free:
3438 if (localpage)
3439 __free_page(localpage);
3440 return ret;
3441}
3442
Trond Myklebust16b42892006-08-24 12:27:15 -04003443static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3444{
3445 struct nfs4_exception exception = { };
3446 ssize_t ret;
3447 do {
3448 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3449 if (ret >= 0)
3450 break;
3451 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3452 } while (exception.retry);
3453 return ret;
3454}
3455
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003456static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3457{
3458 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003459 int ret;
3460
3461 if (!nfs4_server_supports_acls(server))
3462 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003463 ret = nfs_revalidate_inode(server, inode);
3464 if (ret < 0)
3465 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003466 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3467 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003468 ret = nfs4_read_cached_acl(inode, buf, buflen);
3469 if (ret != -ENOENT)
3470 return ret;
3471 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003472}
3473
Trond Myklebust16b42892006-08-24 12:27:15 -04003474static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003475{
3476 struct nfs_server *server = NFS_SERVER(inode);
3477 struct page *pages[NFS4ACL_MAXPAGES];
3478 struct nfs_setaclargs arg = {
3479 .fh = NFS_FH(inode),
3480 .acl_pages = pages,
3481 .acl_len = buflen,
3482 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003483 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003484 struct rpc_message msg = {
3485 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3486 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003487 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003488 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003489 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003490
3491 if (!nfs4_server_supports_acls(server))
3492 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003493 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3494 if (i < 0)
3495 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003496 nfs_inode_return_delegation(inode);
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003497 ret = nfs4_call_sync(server, &msg, &arg, &res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003498
3499 /*
3500 * Free each page after tx, so the only ref left is
3501 * held by the network stack
3502 */
3503 for (; i > 0; i--)
3504 put_page(pages[i-1]);
3505
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003506 /*
3507 * Acl update can result in inode attribute update.
3508 * so mark the attribute cache invalid.
3509 */
3510 spin_lock(&inode->i_lock);
3511 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3512 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003513 nfs_access_zap_cache(inode);
3514 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003515 return ret;
3516}
3517
Trond Myklebust16b42892006-08-24 12:27:15 -04003518static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3519{
3520 struct nfs4_exception exception = { };
3521 int err;
3522 do {
3523 err = nfs4_handle_exception(NFS_SERVER(inode),
3524 __nfs4_proc_set_acl(inode, buf, buflen),
3525 &exception);
3526 } while (exception.retry);
3527 return err;
3528}
3529
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003531nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003533 struct nfs_client *clp = server->nfs_client;
3534
3535 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 return 0;
3537 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003538 case -NFS4ERR_ADMIN_REVOKED:
3539 case -NFS4ERR_BAD_STATEID:
3540 case -NFS4ERR_OPENMODE:
3541 if (state == NULL)
3542 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003543 nfs4_schedule_stateid_recovery(server, state);
3544 goto wait_on_recovery;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003546 case -NFS4ERR_STALE_CLIENTID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 case -NFS4ERR_EXPIRED:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003548 nfs4_schedule_lease_recovery(clp);
3549 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003550#if defined(CONFIG_NFS_V4_1)
3551 case -NFS4ERR_BADSESSION:
3552 case -NFS4ERR_BADSLOT:
3553 case -NFS4ERR_BAD_HIGH_SLOT:
3554 case -NFS4ERR_DEADSESSION:
3555 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3556 case -NFS4ERR_SEQ_FALSE_RETRY:
3557 case -NFS4ERR_SEQ_MISORDERED:
3558 dprintk("%s ERROR %d, Reset session\n", __func__,
3559 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003560 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -04003561 task->tk_status = 0;
3562 return -EAGAIN;
3563#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003565 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003566 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003567 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3569 task->tk_status = 0;
3570 return -EAGAIN;
3571 case -NFS4ERR_OLD_STATEID:
3572 task->tk_status = 0;
3573 return -EAGAIN;
3574 }
3575 task->tk_status = nfs4_map_errors(task->tk_status);
3576 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003577wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003578 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003579 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3580 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3581 task->tk_status = 0;
3582 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583}
3584
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003585int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3586 unsigned short port, struct rpc_cred *cred,
3587 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588{
3589 nfs4_verifier sc_verifier;
3590 struct nfs4_setclientid setclientid = {
3591 .sc_verifier = &sc_verifier,
3592 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003593 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 };
3595 struct rpc_message msg = {
3596 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3597 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003598 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003599 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 };
Al Virobc4785c2006-10-19 23:28:51 -07003601 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 int loop = 0;
3603 int status;
3604
Al Virobc4785c2006-10-19 23:28:51 -07003605 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3607 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3608
3609 for(;;) {
3610 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003611 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003612 clp->cl_ipaddr,
3613 rpc_peeraddr2str(clp->cl_rpcclient,
3614 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003615 rpc_peeraddr2str(clp->cl_rpcclient,
3616 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003617 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 clp->cl_id_uniquifier);
3619 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003620 sizeof(setclientid.sc_netid),
3621 rpc_peeraddr2str(clp->cl_rpcclient,
3622 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003624 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 clp->cl_ipaddr, port >> 8, port & 255);
3626
3627 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3628 if (status != -NFS4ERR_CLID_INUSE)
3629 break;
3630 if (signalled())
3631 break;
3632 if (loop++ & 1)
J. Bruce Fields611c96c2010-12-13 19:05:46 -05003633 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 else
3635 if (++clp->cl_id_uniquifier == 0)
3636 break;
3637 }
3638 return status;
3639}
3640
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003641static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3642 struct nfs4_setclientid_res *arg,
3643 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644{
3645 struct nfs_fsinfo fsinfo;
3646 struct rpc_message msg = {
3647 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003648 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003650 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 };
3652 unsigned long now;
3653 int status;
3654
3655 now = jiffies;
3656 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3657 if (status == 0) {
3658 spin_lock(&clp->cl_lock);
3659 clp->cl_lease_time = fsinfo.lease_time * HZ;
3660 clp->cl_last_renewal = now;
3661 spin_unlock(&clp->cl_lock);
3662 }
3663 return status;
3664}
3665
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003666int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3667 struct nfs4_setclientid_res *arg,
3668 struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003669{
Benny Halevy77e03672008-06-24 20:25:57 +03003670 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003671 int err;
3672 do {
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003673 err = _nfs4_proc_setclientid_confirm(clp, arg, cred);
Trond Myklebust51581f32006-03-20 13:44:47 -05003674 switch (err) {
3675 case 0:
3676 return err;
3677 case -NFS4ERR_RESOURCE:
3678 /* The IBM lawyers misread another document! */
3679 case -NFS4ERR_DELAY:
3680 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3681 }
3682 } while (err == 0);
3683 return err;
3684}
3685
Trond Myklebustfe650402006-01-03 09:55:18 +01003686struct nfs4_delegreturndata {
3687 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003688 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003689 struct nfs_fh fh;
3690 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003691 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003692 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003693 int rpc_status;
3694};
3695
Trond Myklebustfe650402006-01-03 09:55:18 +01003696static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3697{
3698 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003699
Trond Myklebust14516c32010-07-31 14:29:06 -04003700 if (!nfs4_sequence_done(task, &data->res.seq_res))
3701 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003702
Ricardo Labiaga79708862009-12-07 09:23:21 -05003703 switch (task->tk_status) {
3704 case -NFS4ERR_STALE_STATEID:
3705 case -NFS4ERR_EXPIRED:
3706 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003707 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003708 break;
3709 default:
3710 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3711 -EAGAIN) {
3712 nfs_restart_rpc(task, data->res.server->nfs_client);
3713 return;
3714 }
3715 }
3716 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003717}
3718
3719static void nfs4_delegreturn_release(void *calldata)
3720{
Trond Myklebustfe650402006-01-03 09:55:18 +01003721 kfree(calldata);
3722}
3723
Andy Adamson938e1012009-04-01 09:22:28 -04003724#if defined(CONFIG_NFS_V4_1)
3725static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3726{
3727 struct nfs4_delegreturndata *d_data;
3728
3729 d_data = (struct nfs4_delegreturndata *)data;
3730
Trond Myklebust035168ab2010-06-16 09:52:26 -04003731 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003732 &d_data->args.seq_args,
3733 &d_data->res.seq_res, 1, task))
3734 return;
3735 rpc_call_start(task);
3736}
3737#endif /* CONFIG_NFS_V4_1 */
3738
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003739static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003740#if defined(CONFIG_NFS_V4_1)
3741 .rpc_call_prepare = nfs4_delegreturn_prepare,
3742#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003743 .rpc_call_done = nfs4_delegreturn_done,
3744 .rpc_release = nfs4_delegreturn_release,
3745};
3746
Trond Myklebuste6f81072008-01-24 18:14:34 -05003747static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003748{
3749 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003750 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003751 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003752 struct rpc_message msg = {
3753 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3754 .rpc_cred = cred,
3755 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003756 struct rpc_task_setup task_setup_data = {
3757 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003758 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003759 .callback_ops = &nfs4_delegreturn_ops,
3760 .flags = RPC_TASK_ASYNC,
3761 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003762 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003763
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003764 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003765 if (data == NULL)
3766 return -ENOMEM;
3767 data->args.fhandle = &data->fh;
3768 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003769 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003770 nfs_copy_fh(&data->fh, NFS_FH(inode));
3771 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003772 data->res.fattr = &data->fattr;
3773 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003774 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003775 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003776 data->rpc_status = 0;
3777
Trond Myklebustc970aa82007-07-14 15:39:59 -04003778 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05003779 msg.rpc_argp = &data->args;
3780 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003781 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003782 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003783 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003784 if (!issync)
3785 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003786 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003787 if (status != 0)
3788 goto out;
3789 status = data->rpc_status;
3790 if (status != 0)
3791 goto out;
3792 nfs_refresh_inode(inode, &data->fattr);
3793out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003794 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003795 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796}
3797
Trond Myklebuste6f81072008-01-24 18:14:34 -05003798int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799{
3800 struct nfs_server *server = NFS_SERVER(inode);
3801 struct nfs4_exception exception = { };
3802 int err;
3803 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003804 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 switch (err) {
3806 case -NFS4ERR_STALE_STATEID:
3807 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 case 0:
3809 return 0;
3810 }
3811 err = nfs4_handle_exception(server, err, &exception);
3812 } while (exception.retry);
3813 return err;
3814}
3815
3816#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3817#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3818
3819/*
3820 * sleep, with exponential backoff, and retry the LOCK operation.
3821 */
3822static unsigned long
3823nfs4_set_lock_task_retry(unsigned long timeout)
3824{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003825 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 timeout <<= 1;
3827 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3828 return NFS4_LOCK_MAXTIMEOUT;
3829 return timeout;
3830}
3831
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3833{
3834 struct inode *inode = state->inode;
3835 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003836 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003837 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003839 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003841 struct nfs_lockt_res res = {
3842 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 };
3844 struct rpc_message msg = {
3845 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3846 .rpc_argp = &arg,
3847 .rpc_resp = &res,
3848 .rpc_cred = state->owner->so_cred,
3849 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 struct nfs4_lock_state *lsp;
3851 int status;
3852
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003853 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003854 status = nfs4_set_lock_state(state, request);
3855 if (status != 0)
3856 goto out;
3857 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003858 arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05003859 arg.lock_owner.s_dev = server->s_dev;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003860 status = nfs4_call_sync(server, &msg, &arg, &res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003861 switch (status) {
3862 case 0:
3863 request->fl_type = F_UNLCK;
3864 break;
3865 case -NFS4ERR_DENIED:
3866 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003868 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003869out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 return status;
3871}
3872
3873static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3874{
3875 struct nfs4_exception exception = { };
3876 int err;
3877
3878 do {
3879 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3880 _nfs4_proc_getlk(state, cmd, request),
3881 &exception);
3882 } while (exception.retry);
3883 return err;
3884}
3885
3886static int do_vfs_lock(struct file *file, struct file_lock *fl)
3887{
3888 int res = 0;
3889 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3890 case FL_POSIX:
3891 res = posix_lock_file_wait(file, fl);
3892 break;
3893 case FL_FLOCK:
3894 res = flock_lock_file_wait(file, fl);
3895 break;
3896 default:
3897 BUG();
3898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 return res;
3900}
3901
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003902struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003903 struct nfs_locku_args arg;
3904 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003905 struct nfs4_lock_state *lsp;
3906 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003907 struct file_lock fl;
3908 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003909 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003910};
3911
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003912static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3913 struct nfs_open_context *ctx,
3914 struct nfs4_lock_state *lsp,
3915 struct nfs_seqid *seqid)
3916{
3917 struct nfs4_unlockdata *p;
3918 struct inode *inode = lsp->ls_state->inode;
3919
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003920 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003921 if (p == NULL)
3922 return NULL;
3923 p->arg.fh = NFS_FH(inode);
3924 p->arg.fl = &p->fl;
3925 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003926 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003927 p->arg.stateid = &lsp->ls_stateid;
3928 p->lsp = lsp;
3929 atomic_inc(&lsp->ls_count);
3930 /* Ensure we don't close file until we're done freeing locks! */
3931 p->ctx = get_nfs_open_context(ctx);
3932 memcpy(&p->fl, fl, sizeof(p->fl));
3933 p->server = NFS_SERVER(inode);
3934 return p;
3935}
3936
Trond Myklebust06f814a2006-01-03 09:55:07 +01003937static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003938{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003939 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003940 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01003941 nfs4_put_lock_state(calldata->lsp);
3942 put_nfs_open_context(calldata->ctx);
3943 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003944}
3945
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003946static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003947{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003948 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003949
Trond Myklebust14516c32010-07-31 14:29:06 -04003950 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3951 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003952 switch (task->tk_status) {
3953 case 0:
3954 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003955 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003956 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01003957 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003958 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003959 case -NFS4ERR_BAD_STATEID:
3960 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003961 case -NFS4ERR_STALE_STATEID:
3962 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003963 break;
3964 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003965 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebust0110ee12009-12-07 09:00:24 -05003966 nfs_restart_rpc(task,
Trond Myklebustd61e6122009-12-05 19:32:19 -05003967 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003968 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003969}
3970
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003971static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003972{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003973 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003975 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003976 return;
3977 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003978 /* Note: exit _without_ running nfs4_locku_done */
3979 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003980 return;
3981 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01003982 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04003983 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04003984 &calldata->arg.seq_args,
3985 &calldata->res.seq_res, 1, task))
3986 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003987 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988}
3989
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003990static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003991 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003992 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01003993 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003994};
3995
Trond Myklebusta5d16a42006-01-03 09:55:17 +01003996static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
3997 struct nfs_open_context *ctx,
3998 struct nfs4_lock_state *lsp,
3999 struct nfs_seqid *seqid)
4000{
4001 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004002 struct rpc_message msg = {
4003 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4004 .rpc_cred = ctx->cred,
4005 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004006 struct rpc_task_setup task_setup_data = {
4007 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004008 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004009 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004010 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004011 .flags = RPC_TASK_ASYNC,
4012 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004013
Frank Filz137d6ac2007-07-09 15:32:29 -07004014 /* Ensure this is an unlock - when canceling a lock, the
4015 * canceled lock is passed in, and it won't be an unlock.
4016 */
4017 fl->fl_type = F_UNLCK;
4018
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004019 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4020 if (data == NULL) {
4021 nfs_free_seqid(seqid);
4022 return ERR_PTR(-ENOMEM);
4023 }
4024
Trond Myklebust1174dd12010-12-21 10:52:24 -05004025 msg.rpc_argp = &data->arg;
4026 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004027 task_setup_data.callback_data = data;
4028 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004029}
4030
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4032{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004033 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004034 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004035 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004036 struct rpc_task *task;
4037 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004038 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039
Trond Myklebust9b073572006-06-29 16:38:34 -04004040 status = nfs4_set_lock_state(state, request);
4041 /* Unlock _before_ we do the RPC call */
4042 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004043 down_read(&nfsi->rwsem);
4044 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4045 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004046 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004047 }
4048 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004049 if (status != 0)
4050 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004051 /* Is this a delegated lock? */
4052 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004053 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004054 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004055 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004056 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004057 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004058 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004059 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004060 status = PTR_ERR(task);
4061 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004062 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004063 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004064 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004065out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004066 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004067 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068}
4069
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004070struct nfs4_lockdata {
4071 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004072 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004073 struct nfs4_lock_state *lsp;
4074 struct nfs_open_context *ctx;
4075 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004076 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004077 int rpc_status;
4078 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004079 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004080};
4081
4082static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004083 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4084 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004085{
4086 struct nfs4_lockdata *p;
4087 struct inode *inode = lsp->ls_state->inode;
4088 struct nfs_server *server = NFS_SERVER(inode);
4089
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004090 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004091 if (p == NULL)
4092 return NULL;
4093
4094 p->arg.fh = NFS_FH(inode);
4095 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004096 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004097 if (p->arg.open_seqid == NULL)
4098 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004099 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004100 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004101 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004102 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004103 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004104 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004105 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004106 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004107 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004108 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004109 atomic_inc(&lsp->ls_count);
4110 p->ctx = get_nfs_open_context(ctx);
4111 memcpy(&p->fl, fl, sizeof(p->fl));
4112 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004113out_free_seqid:
4114 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004115out_free:
4116 kfree(p);
4117 return NULL;
4118}
4119
4120static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4121{
4122 struct nfs4_lockdata *data = calldata;
4123 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124
Harvey Harrison3110ff82008-05-02 13:42:44 -07004125 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004126 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4127 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004128 /* Do we need to do an open_to_lock_owner? */
4129 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004130 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4131 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004132 data->arg.open_stateid = &state->stateid;
4133 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004134 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004135 } else
4136 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004137 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04004138 if (nfs4_setup_sequence(data->server,
4139 &data->arg.seq_args,
Andy Adamson66179ef2009-04-01 09:22:22 -04004140 &data->res.seq_res, 1, task))
4141 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004142 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004143 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004144}
4145
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004146static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4147{
4148 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4149 nfs4_lock_prepare(task, calldata);
4150}
4151
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004152static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4153{
4154 struct nfs4_lockdata *data = calldata;
4155
Harvey Harrison3110ff82008-05-02 13:42:44 -07004156 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004157
Trond Myklebust14516c32010-07-31 14:29:06 -04004158 if (!nfs4_sequence_done(task, &data->res.seq_res))
4159 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004160
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004161 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004162 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004163 if (data->rpc_status == 0)
4164 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4165 else
4166 goto out;
4167 }
4168 if (data->rpc_status == 0) {
4169 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4170 sizeof(data->lsp->ls_stateid.data));
4171 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04004172 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004173 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004174out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004175 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004176}
4177
4178static void nfs4_lock_release(void *calldata)
4179{
4180 struct nfs4_lockdata *data = calldata;
4181
Harvey Harrison3110ff82008-05-02 13:42:44 -07004182 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004183 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004184 if (data->cancelled != 0) {
4185 struct rpc_task *task;
4186 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4187 data->arg.lock_seqid);
4188 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004189 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004190 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004191 } else
4192 nfs_free_seqid(data->arg.lock_seqid);
4193 nfs4_put_lock_state(data->lsp);
4194 put_nfs_open_context(data->ctx);
4195 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004196 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004197}
4198
4199static const struct rpc_call_ops nfs4_lock_ops = {
4200 .rpc_call_prepare = nfs4_lock_prepare,
4201 .rpc_call_done = nfs4_lock_done,
4202 .rpc_release = nfs4_lock_release,
4203};
4204
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004205static const struct rpc_call_ops nfs4_recover_lock_ops = {
4206 .rpc_call_prepare = nfs4_recover_lock_prepare,
4207 .rpc_call_done = nfs4_lock_done,
4208 .rpc_release = nfs4_lock_release,
4209};
4210
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004211static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4212{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004213 switch (error) {
4214 case -NFS4ERR_ADMIN_REVOKED:
4215 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004216 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004217 if (new_lock_owner != 0 ||
4218 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004219 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004220 break;
4221 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004222 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004223 case -NFS4ERR_EXPIRED:
4224 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004225 };
4226}
4227
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004228static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004229{
4230 struct nfs4_lockdata *data;
4231 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004232 struct rpc_message msg = {
4233 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4234 .rpc_cred = state->owner->so_cred,
4235 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004236 struct rpc_task_setup task_setup_data = {
4237 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004238 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004239 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004240 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004241 .flags = RPC_TASK_ASYNC,
4242 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004243 int ret;
4244
Harvey Harrison3110ff82008-05-02 13:42:44 -07004245 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004246 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004247 fl->fl_u.nfs4_fl.owner,
4248 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004249 if (data == NULL)
4250 return -ENOMEM;
4251 if (IS_SETLKW(cmd))
4252 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004253 if (recovery_type > NFS_LOCK_NEW) {
4254 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004255 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004256 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4257 }
Trond Myklebust1174dd12010-12-21 10:52:24 -05004258 msg.rpc_argp = &data->arg;
4259 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004260 task_setup_data.callback_data = data;
4261 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004262 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004263 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004264 ret = nfs4_wait_for_completion_rpc_task(task);
4265 if (ret == 0) {
4266 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004267 if (ret)
4268 nfs4_handle_setlk_error(data->server, data->lsp,
4269 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004270 } else
4271 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004272 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004273 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004274 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275}
4276
4277static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4278{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004279 struct nfs_server *server = NFS_SERVER(state->inode);
4280 struct nfs4_exception exception = { };
4281 int err;
4282
4283 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004284 /* Cache the lock if possible... */
4285 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4286 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004287 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004288 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004289 break;
4290 nfs4_handle_exception(server, err, &exception);
4291 } while (exception.retry);
4292 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293}
4294
4295static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4296{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004297 struct nfs_server *server = NFS_SERVER(state->inode);
4298 struct nfs4_exception exception = { };
4299 int err;
4300
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004301 err = nfs4_set_lock_state(state, request);
4302 if (err != 0)
4303 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004304 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004305 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4306 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004307 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004308 switch (err) {
4309 default:
4310 goto out;
4311 case -NFS4ERR_GRACE:
4312 case -NFS4ERR_DELAY:
4313 nfs4_handle_exception(server, err, &exception);
4314 err = 0;
4315 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004316 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004317out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004318 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319}
4320
4321static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4322{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004323 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004324 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004325 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004327 if ((fl_flags & FL_POSIX) &&
4328 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4329 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004330 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004331 status = nfs4_set_lock_state(state, request);
4332 if (status != 0)
4333 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004334 request->fl_flags |= FL_ACCESS;
4335 status = do_vfs_lock(request->fl_file, request);
4336 if (status < 0)
4337 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004338 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004339 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004340 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004341 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004342 request->fl_flags = fl_flags & ~FL_SLEEP;
4343 status = do_vfs_lock(request->fl_file, request);
4344 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004345 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004346 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004347 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004348 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004349 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004350 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004351 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004352 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004353out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004354 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004355out:
4356 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 return status;
4358}
4359
4360static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4361{
4362 struct nfs4_exception exception = { };
4363 int err;
4364
4365 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004366 err = _nfs4_proc_setlk(state, cmd, request);
4367 if (err == -NFS4ERR_DENIED)
4368 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004370 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 } while (exception.retry);
4372 return err;
4373}
4374
4375static int
4376nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4377{
4378 struct nfs_open_context *ctx;
4379 struct nfs4_state *state;
4380 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4381 int status;
4382
4383 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004384 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 state = ctx->state;
4386
4387 if (request->fl_start < 0 || request->fl_end < 0)
4388 return -EINVAL;
4389
Trond Myklebustd9531262009-07-21 19:22:38 -04004390 if (IS_GETLK(cmd)) {
4391 if (state != NULL)
4392 return nfs4_proc_getlk(state, F_GETLK, request);
4393 return 0;
4394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395
4396 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4397 return -EINVAL;
4398
Trond Myklebustd9531262009-07-21 19:22:38 -04004399 if (request->fl_type == F_UNLCK) {
4400 if (state != NULL)
4401 return nfs4_proc_unlck(state, cmd, request);
4402 return 0;
4403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404
Trond Myklebustd9531262009-07-21 19:22:38 -04004405 if (state == NULL)
4406 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 do {
4408 status = nfs4_proc_setlk(state, cmd, request);
4409 if ((status != -EAGAIN) || IS_SETLK(cmd))
4410 break;
4411 timeout = nfs4_set_lock_task_retry(timeout);
4412 status = -ERESTARTSYS;
4413 if (signalled())
4414 break;
4415 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 return status;
4417}
4418
Trond Myklebust888e6942005-11-04 15:38:11 -05004419int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4420{
4421 struct nfs_server *server = NFS_SERVER(state->inode);
4422 struct nfs4_exception exception = { };
4423 int err;
4424
4425 err = nfs4_set_lock_state(state, fl);
4426 if (err != 0)
4427 goto out;
4428 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004429 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004430 switch (err) {
4431 default:
4432 printk(KERN_ERR "%s: unhandled error %d.\n",
4433 __func__, err);
4434 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004435 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004436 goto out;
4437 case -NFS4ERR_EXPIRED:
4438 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004439 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004440 nfs4_schedule_lease_recovery(server->nfs_client);
4441 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004442 case -NFS4ERR_BADSESSION:
4443 case -NFS4ERR_BADSLOT:
4444 case -NFS4ERR_BAD_HIGH_SLOT:
4445 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4446 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004447 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004448 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004449 case -ERESTARTSYS:
4450 /*
4451 * The show must go on: exit, but mark the
4452 * stateid as needing recovery.
4453 */
4454 case -NFS4ERR_ADMIN_REVOKED:
4455 case -NFS4ERR_BAD_STATEID:
4456 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004457 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004458 err = 0;
4459 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004460 case -EKEYEXPIRED:
4461 /*
4462 * User RPCSEC_GSS context has expired.
4463 * We cannot recover this stateid now, so
4464 * skip it and allow recovery thread to
4465 * proceed.
4466 */
4467 err = 0;
4468 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004469 case -ENOMEM:
4470 case -NFS4ERR_DENIED:
4471 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4472 err = 0;
4473 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004474 case -NFS4ERR_DELAY:
4475 break;
4476 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004477 err = nfs4_handle_exception(server, err, &exception);
4478 } while (exception.retry);
4479out:
4480 return err;
4481}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004482
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004483static void nfs4_release_lockowner_release(void *calldata)
4484{
4485 kfree(calldata);
4486}
4487
4488const struct rpc_call_ops nfs4_release_lockowner_ops = {
4489 .rpc_release = nfs4_release_lockowner_release,
4490};
4491
4492void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4493{
4494 struct nfs_server *server = lsp->ls_state->owner->so_server;
4495 struct nfs_release_lockowner_args *args;
4496 struct rpc_message msg = {
4497 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4498 };
4499
4500 if (server->nfs_client->cl_mvops->minor_version != 0)
4501 return;
4502 args = kmalloc(sizeof(*args), GFP_NOFS);
4503 if (!args)
4504 return;
4505 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4506 args->lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004507 args->lock_owner.s_dev = server->s_dev;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004508 msg.rpc_argp = args;
4509 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4510}
4511
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004512#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4513
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004514static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4515 const void *buf, size_t buflen,
4516 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004517{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004518 if (strcmp(key, "") != 0)
4519 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004520
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004521 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004522}
4523
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004524static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4525 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004526{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004527 if (strcmp(key, "") != 0)
4528 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004529
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004530 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004531}
4532
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004533static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4534 size_t list_len, const char *name,
4535 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004536{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004537 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004538
J. Bruce Fields096455a2006-03-20 23:23:42 -05004539 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4540 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004541
4542 if (list && len <= list_len)
4543 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004544 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004545}
4546
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004547static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4548{
4549 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4550 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4551 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4552 return;
4553
4554 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4555 NFS_ATTR_FATTR_NLINK;
4556 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4557 fattr->nlink = 2;
4558}
4559
Trond Myklebust56659e92007-07-17 21:52:39 -04004560int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004561 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004562{
4563 struct nfs_server *server = NFS_SERVER(dir);
4564 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004565 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4566 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004567 };
4568 struct nfs4_fs_locations_arg args = {
4569 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004570 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004571 .page = page,
4572 .bitmask = bitmask,
4573 };
Benny Halevy22958462009-04-01 09:22:02 -04004574 struct nfs4_fs_locations_res res = {
4575 .fs_locations = fs_locations,
4576 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004577 struct rpc_message msg = {
4578 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4579 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004580 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004581 };
4582 int status;
4583
Harvey Harrison3110ff82008-05-02 13:42:44 -07004584 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004585 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004586 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004587 fs_locations->nlocations = 0;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04004588 status = nfs4_call_sync(server, &msg, &args, &res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004589 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004590 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004591 return status;
4592}
4593
Andy Adamson557134a2009-04-01 09:21:53 -04004594#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004595/*
Andy Adamson357f54d2010-12-14 10:11:57 -05004596 * Check the exchange flags returned by the server for invalid flags, having
4597 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4598 * DS flags set.
4599 */
4600static int nfs4_check_cl_exchange_flags(u32 flags)
4601{
4602 if (flags & ~EXCHGID4_FLAG_MASK_R)
4603 goto out_inval;
4604 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4605 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4606 goto out_inval;
4607 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4608 goto out_inval;
4609 return NFS_OK;
4610out_inval:
4611 return -NFS4ERR_INVAL;
4612}
4613
4614/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04004615 * nfs4_proc_exchange_id()
4616 *
4617 * Since the clientid has expired, all compounds using sessions
4618 * associated with the stale clientid will be returning
4619 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4620 * be in some phase of session reset.
4621 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004622int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004623{
4624 nfs4_verifier verifier;
4625 struct nfs41_exchange_id_args args = {
4626 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05004627 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04004628 };
4629 struct nfs41_exchange_id_res res = {
4630 .client = clp,
4631 };
4632 int status;
4633 struct rpc_message msg = {
4634 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4635 .rpc_argp = &args,
4636 .rpc_resp = &res,
4637 .rpc_cred = cred,
4638 };
4639 __be32 *p;
4640
4641 dprintk("--> %s\n", __func__);
4642 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004643
Benny Halevy99fe60d2009-04-01 09:22:29 -04004644 p = (u32 *)verifier.data;
4645 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4646 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4647 args.verifier = &verifier;
4648
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004649 args.id_len = scnprintf(args.id, sizeof(args.id),
4650 "%s/%s.%s/%u",
4651 clp->cl_ipaddr,
4652 init_utsname()->nodename,
4653 init_utsname()->domainname,
4654 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004655
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004656 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4657 if (!status)
4658 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004659 dprintk("<-- %s status= %d\n", __func__, status);
4660 return status;
4661}
4662
Andy Adamson2050f0c2009-04-01 09:22:30 -04004663struct nfs4_get_lease_time_data {
4664 struct nfs4_get_lease_time_args *args;
4665 struct nfs4_get_lease_time_res *res;
4666 struct nfs_client *clp;
4667};
4668
4669static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4670 void *calldata)
4671{
4672 int ret;
4673 struct nfs4_get_lease_time_data *data =
4674 (struct nfs4_get_lease_time_data *)calldata;
4675
4676 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004677 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004678 /* just setup sequence, do not trigger session recovery
4679 since we're invoked within one */
4680 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004681 &data->args->la_seq_args,
4682 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004683
4684 BUG_ON(ret == -EAGAIN);
4685 rpc_call_start(task);
4686 dprintk("<-- %s\n", __func__);
4687}
4688
4689/*
4690 * Called from nfs4_state_manager thread for session setup, so don't recover
4691 * from sequence operation or clientid errors.
4692 */
4693static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4694{
4695 struct nfs4_get_lease_time_data *data =
4696 (struct nfs4_get_lease_time_data *)calldata;
4697
4698 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04004699 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4700 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04004701 switch (task->tk_status) {
4702 case -NFS4ERR_DELAY:
4703 case -NFS4ERR_GRACE:
4704 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4705 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4706 task->tk_status = 0;
Trond Myklebust0110ee12009-12-07 09:00:24 -05004707 nfs_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004708 return;
4709 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004710 dprintk("<-- %s\n", __func__);
4711}
4712
4713struct rpc_call_ops nfs4_get_lease_time_ops = {
4714 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4715 .rpc_call_done = nfs4_get_lease_time_done,
4716};
4717
4718int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4719{
4720 struct rpc_task *task;
4721 struct nfs4_get_lease_time_args args;
4722 struct nfs4_get_lease_time_res res = {
4723 .lr_fsinfo = fsinfo,
4724 };
4725 struct nfs4_get_lease_time_data data = {
4726 .args = &args,
4727 .res = &res,
4728 .clp = clp,
4729 };
4730 struct rpc_message msg = {
4731 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4732 .rpc_argp = &args,
4733 .rpc_resp = &res,
4734 };
4735 struct rpc_task_setup task_setup = {
4736 .rpc_client = clp->cl_rpcclient,
4737 .rpc_message = &msg,
4738 .callback_ops = &nfs4_get_lease_time_ops,
4739 .callback_data = &data
4740 };
4741 int status;
4742
Andy Adamson2050f0c2009-04-01 09:22:30 -04004743 dprintk("--> %s\n", __func__);
4744 task = rpc_run_task(&task_setup);
4745
4746 if (IS_ERR(task))
4747 status = PTR_ERR(task);
4748 else {
4749 status = task->tk_status;
4750 rpc_put_task(task);
4751 }
4752 dprintk("<-- %s return %d\n", __func__, status);
4753
4754 return status;
4755}
4756
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004757/*
4758 * Reset a slot table
4759 */
Andy Adamson104aeba2010-01-14 17:45:10 -05004760static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4761 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004762{
Andy Adamson104aeba2010-01-14 17:45:10 -05004763 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004764 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004765 int ret = 0;
4766
Andy Adamson104aeba2010-01-14 17:45:10 -05004767 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4768 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004769
Andy Adamson104aeba2010-01-14 17:45:10 -05004770 /* Does the newly negotiated max_reqs match the existing slot table? */
4771 if (max_reqs != tbl->max_slots) {
4772 ret = -ENOMEM;
4773 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004774 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05004775 if (!new)
4776 goto out;
4777 ret = 0;
4778 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004779 }
4780 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05004781 if (new) {
4782 tbl->slots = new;
4783 tbl->max_slots = max_reqs;
4784 }
4785 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004786 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004787 spin_unlock(&tbl->slot_tbl_lock);
4788 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4789 tbl, tbl->slots, tbl->max_slots);
4790out:
4791 dprintk("<-- %s: return %d\n", __func__, ret);
4792 return ret;
4793}
4794
Andy Adamsonfc931582009-04-01 09:22:31 -04004795/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004796 * Reset the forechannel and backchannel slot tables
4797 */
4798static int nfs4_reset_slot_tables(struct nfs4_session *session)
4799{
4800 int status;
4801
4802 status = nfs4_reset_slot_table(&session->fc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004803 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004804 if (status)
4805 return status;
4806
4807 status = nfs4_reset_slot_table(&session->bc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004808 session->bc_attrs.max_reqs, 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004809 return status;
4810}
4811
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004812/* Destroy the slot table */
4813static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4814{
4815 if (session->fc_slot_table.slots != NULL) {
4816 kfree(session->fc_slot_table.slots);
4817 session->fc_slot_table.slots = NULL;
4818 }
4819 if (session->bc_slot_table.slots != NULL) {
4820 kfree(session->bc_slot_table.slots);
4821 session->bc_slot_table.slots = NULL;
4822 }
4823 return;
4824}
4825
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004826/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004827 * Initialize slot table
4828 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004829static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4830 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04004831{
Andy Adamsonfc931582009-04-01 09:22:31 -04004832 struct nfs4_slot *slot;
4833 int ret = -ENOMEM;
4834
4835 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4836
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004837 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04004838
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004839 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04004840 if (!slot)
4841 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04004842 ret = 0;
4843
4844 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04004845 tbl->max_slots = max_slots;
4846 tbl->slots = slot;
4847 tbl->highest_used_slotid = -1; /* no slot is currently used */
4848 spin_unlock(&tbl->slot_tbl_lock);
4849 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4850 tbl, tbl->slots, tbl->max_slots);
4851out:
4852 dprintk("<-- %s: return %d\n", __func__, ret);
4853 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04004854}
4855
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004856/*
4857 * Initialize the forechannel and backchannel tables
4858 */
4859static int nfs4_init_slot_tables(struct nfs4_session *session)
4860{
Trond Myklebustf26468f2009-12-05 19:32:11 -05004861 struct nfs4_slot_table *tbl;
4862 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004863
Trond Myklebustf26468f2009-12-05 19:32:11 -05004864 tbl = &session->fc_slot_table;
4865 if (tbl->slots == NULL) {
4866 status = nfs4_init_slot_table(tbl,
4867 session->fc_attrs.max_reqs, 1);
4868 if (status)
4869 return status;
4870 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004871
Trond Myklebustf26468f2009-12-05 19:32:11 -05004872 tbl = &session->bc_slot_table;
4873 if (tbl->slots == NULL) {
4874 status = nfs4_init_slot_table(tbl,
4875 session->bc_attrs.max_reqs, 0);
4876 if (status)
4877 nfs4_destroy_slot_tables(session);
4878 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004879
4880 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04004881}
4882
4883struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4884{
4885 struct nfs4_session *session;
4886 struct nfs4_slot_table *tbl;
4887
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004888 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04004889 if (!session)
4890 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04004891
Andy Adamson557134a2009-04-01 09:21:53 -04004892 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004893 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04004894 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004895 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00004896 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004897
4898 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004899 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004900 spin_lock_init(&tbl->slot_tbl_lock);
4901 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00004902 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004903
Trond Myklebust1055d762010-06-16 09:52:27 -04004904 session->session_state = 1<<NFS4_SESSION_INITING;
4905
Andy Adamson557134a2009-04-01 09:21:53 -04004906 session->clp = clp;
4907 return session;
4908}
4909
4910void nfs4_destroy_session(struct nfs4_session *session)
4911{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04004912 nfs4_proc_destroy_session(session);
4913 dprintk("%s Destroy backchannel for xprt %p\n",
4914 __func__, session->clp->cl_rpcclient->cl_xprt);
4915 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
4916 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004917 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04004918 kfree(session);
4919}
4920
Andy Adamsonfc931582009-04-01 09:22:31 -04004921/*
4922 * Initialize the values to be used by the client in CREATE_SESSION
4923 * If nfs4_init_session set the fore channel request and response sizes,
4924 * use them.
4925 *
4926 * Set the back channel max_resp_sz_cached to zero to force the client to
4927 * always set csa_cachethis to FALSE because the current implementation
4928 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
4929 */
4930static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
4931{
4932 struct nfs4_session *session = args->client->cl_session;
4933 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
4934 mxresp_sz = session->fc_attrs.max_resp_sz;
4935
4936 if (mxrqst_sz == 0)
4937 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
4938 if (mxresp_sz == 0)
4939 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
4940 /* Fore channel attributes */
4941 args->fc_attrs.headerpadsz = 0;
4942 args->fc_attrs.max_rqst_sz = mxrqst_sz;
4943 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04004944 args->fc_attrs.max_ops = NFS4_MAX_OPS;
4945 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
4946
4947 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05004948 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04004949 __func__,
4950 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05004951 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04004952
4953 /* Back channel attributes */
4954 args->bc_attrs.headerpadsz = 0;
4955 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
4956 args->bc_attrs.max_resp_sz = PAGE_SIZE;
4957 args->bc_attrs.max_resp_sz_cached = 0;
4958 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
4959 args->bc_attrs.max_reqs = 1;
4960
4961 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
4962 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
4963 __func__,
4964 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
4965 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
4966 args->bc_attrs.max_reqs);
4967}
4968
J. Bruce Fields43c2e882010-10-02 15:19:01 -04004969static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04004970{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04004971 struct nfs4_channel_attrs *sent = &args->fc_attrs;
4972 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
4973
4974 if (rcvd->headerpadsz > sent->headerpadsz)
4975 return -EINVAL;
4976 if (rcvd->max_resp_sz > sent->max_resp_sz)
4977 return -EINVAL;
4978 /*
4979 * Our requested max_ops is the minimum we need; we're not
4980 * prepared to break up compounds into smaller pieces than that.
4981 * So, no point even trying to continue if the server won't
4982 * cooperate:
4983 */
4984 if (rcvd->max_ops < sent->max_ops)
4985 return -EINVAL;
4986 if (rcvd->max_reqs == 0)
4987 return -EINVAL;
4988 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04004989}
4990
J. Bruce Fields43c2e882010-10-02 15:19:01 -04004991static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
4992{
4993 struct nfs4_channel_attrs *sent = &args->bc_attrs;
4994 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04004995
J. Bruce Fields43c2e882010-10-02 15:19:01 -04004996 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
4997 return -EINVAL;
4998 if (rcvd->max_resp_sz < sent->max_resp_sz)
4999 return -EINVAL;
5000 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5001 return -EINVAL;
5002 /* These would render the backchannel useless: */
5003 if (rcvd->max_ops == 0)
5004 return -EINVAL;
5005 if (rcvd->max_reqs == 0)
5006 return -EINVAL;
5007 return 0;
5008}
Andy Adamson8d353012009-04-01 09:22:32 -04005009
Andy Adamson8d353012009-04-01 09:22:32 -04005010static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5011 struct nfs4_session *session)
5012{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005013 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005014
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005015 ret = nfs4_verify_fore_channel_attrs(args, session);
5016 if (ret)
5017 return ret;
5018 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005019}
5020
Andy Adamsonfc931582009-04-01 09:22:31 -04005021static int _nfs4_proc_create_session(struct nfs_client *clp)
5022{
5023 struct nfs4_session *session = clp->cl_session;
5024 struct nfs41_create_session_args args = {
5025 .client = clp,
5026 .cb_program = NFS4_CALLBACK,
5027 };
5028 struct nfs41_create_session_res res = {
5029 .client = clp,
5030 };
5031 struct rpc_message msg = {
5032 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5033 .rpc_argp = &args,
5034 .rpc_resp = &res,
5035 };
5036 int status;
5037
5038 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005039 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005040
5041 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5042
Andy Adamson8d353012009-04-01 09:22:32 -04005043 if (!status)
5044 /* Verify the session's negotiated channel_attrs values */
5045 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005046 if (!status) {
5047 /* Increment the clientid slot sequence id */
5048 clp->cl_seqid++;
5049 }
5050
5051 return status;
5052}
5053
5054/*
5055 * Issues a CREATE_SESSION operation to the server.
5056 * It is the responsibility of the caller to verify the session is
5057 * expired before calling this routine.
5058 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005059int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005060{
5061 int status;
5062 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005063 struct nfs4_session *session = clp->cl_session;
Ricardo Labiaga7d6d63d2011-03-09 13:13:44 -05005064 long timeout = 0;
5065 int err;
Andy Adamsonfc931582009-04-01 09:22:31 -04005066
5067 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5068
Ricardo Labiaga7d6d63d2011-03-09 13:13:44 -05005069 do {
5070 status = _nfs4_proc_create_session(clp);
5071 if (status == -NFS4ERR_DELAY) {
5072 err = nfs4_delay(clp->cl_rpcclient, &timeout);
5073 if (err)
5074 status = err;
5075 }
5076 } while (status == -NFS4ERR_DELAY);
5077
Andy Adamsonfc931582009-04-01 09:22:31 -04005078 if (status)
5079 goto out;
5080
Trond Myklebustf26468f2009-12-05 19:32:11 -05005081 /* Init and reset the fore channel */
5082 status = nfs4_init_slot_tables(session);
5083 dprintk("slot table initialization returned %d\n", status);
5084 if (status)
5085 goto out;
5086 status = nfs4_reset_slot_tables(session);
5087 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005088 if (status)
5089 goto out;
5090
5091 ptr = (unsigned *)&session->sess_id.data[0];
5092 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5093 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005094out:
5095 dprintk("<-- %s\n", __func__);
5096 return status;
5097}
5098
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005099/*
5100 * Issue the over-the-wire RPC DESTROY_SESSION.
5101 * The caller must serialize access to this routine.
5102 */
5103int nfs4_proc_destroy_session(struct nfs4_session *session)
5104{
5105 int status = 0;
5106 struct rpc_message msg;
5107
5108 dprintk("--> nfs4_proc_destroy_session\n");
5109
5110 /* session is still being setup */
5111 if (session->clp->cl_cons_state != NFS_CS_READY)
5112 return status;
5113
5114 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5115 msg.rpc_argp = session;
5116 msg.rpc_resp = NULL;
5117 msg.rpc_cred = NULL;
5118 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5119
5120 if (status)
5121 printk(KERN_WARNING
5122 "Got error %d from the server on DESTROY_SESSION. "
5123 "Session has been destroyed regardless...\n", status);
5124
5125 dprintk("<-- nfs4_proc_destroy_session\n");
5126 return status;
5127}
5128
Trond Myklebustfccba802009-07-21 16:48:07 -04005129int nfs4_init_session(struct nfs_server *server)
5130{
5131 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005132 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005133 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005134 int ret;
5135
5136 if (!nfs4_has_session(clp))
5137 return 0;
5138
Trond Myklebust1055d762010-06-16 09:52:27 -04005139 session = clp->cl_session;
5140 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5141 return 0;
5142
Andy Adamson68bf05e2009-12-15 12:55:02 -05005143 rsize = server->rsize;
5144 if (rsize == 0)
5145 rsize = NFS_MAX_FILE_IO_SIZE;
5146 wsize = server->wsize;
5147 if (wsize == 0)
5148 wsize = NFS_MAX_FILE_IO_SIZE;
5149
Andy Adamson68bf05e2009-12-15 12:55:02 -05005150 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5151 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005152
Trond Myklebustfccba802009-07-21 16:48:07 -04005153 ret = nfs4_recover_expired_lease(server);
5154 if (!ret)
5155 ret = nfs4_check_client_ready(clp);
5156 return ret;
5157}
5158
Andy Adamsond83217c2011-03-01 01:34:17 +00005159int nfs4_init_ds_session(struct nfs_client *clp)
5160{
5161 struct nfs4_session *session = clp->cl_session;
5162 int ret;
5163
5164 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5165 return 0;
5166
5167 ret = nfs4_client_recover_expired_lease(clp);
5168 if (!ret)
5169 /* Test for the DS role */
5170 if (!is_ds_client(clp))
5171 ret = -ENODEV;
5172 if (!ret)
5173 ret = nfs4_check_client_ready(clp);
5174 return ret;
5175
5176}
5177EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5178
5179
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005180/*
5181 * Renew the cl_session lease.
5182 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005183struct nfs4_sequence_data {
5184 struct nfs_client *clp;
5185 struct nfs4_sequence_args args;
5186 struct nfs4_sequence_res res;
5187};
5188
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005189static void nfs41_sequence_release(void *data)
5190{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005191 struct nfs4_sequence_data *calldata = data;
5192 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005193
Alexandros Batsakis71358402010-02-05 03:45:05 -08005194 if (atomic_read(&clp->cl_count) > 1)
5195 nfs4_schedule_state_renewal(clp);
5196 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005197 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005198}
5199
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005200static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5201{
5202 switch(task->tk_status) {
5203 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005204 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5205 return -EAGAIN;
5206 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005207 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005208 }
5209 return 0;
5210}
5211
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005212static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005213{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005214 struct nfs4_sequence_data *calldata = data;
5215 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005216
Trond Myklebust14516c32010-07-31 14:29:06 -04005217 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5218 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005219
5220 if (task->tk_status < 0) {
5221 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005222 if (atomic_read(&clp->cl_count) == 1)
5223 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005224
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005225 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5226 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005227 return;
5228 }
5229 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005230 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005231out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005232 dprintk("<-- %s\n", __func__);
5233}
5234
5235static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5236{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005237 struct nfs4_sequence_data *calldata = data;
5238 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005239 struct nfs4_sequence_args *args;
5240 struct nfs4_sequence_res *res;
5241
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005242 args = task->tk_msg.rpc_argp;
5243 res = task->tk_msg.rpc_resp;
5244
Trond Myklebust035168ab2010-06-16 09:52:26 -04005245 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005246 return;
5247 rpc_call_start(task);
5248}
5249
5250static const struct rpc_call_ops nfs41_sequence_ops = {
5251 .rpc_call_done = nfs41_sequence_call_done,
5252 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005253 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005254};
5255
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005256static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005257{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005258 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005259 struct rpc_message msg = {
5260 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5261 .rpc_cred = cred,
5262 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005263 struct rpc_task_setup task_setup_data = {
5264 .rpc_client = clp->cl_rpcclient,
5265 .rpc_message = &msg,
5266 .callback_ops = &nfs41_sequence_ops,
5267 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5268 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005269
Alexandros Batsakis71358402010-02-05 03:45:05 -08005270 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005271 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005272 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005273 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005274 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005275 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005276 }
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005277 msg.rpc_argp = &calldata->args;
5278 msg.rpc_resp = &calldata->res;
5279 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005280 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005281
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005282 return rpc_run_task(&task_setup_data);
5283}
5284
5285static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5286{
5287 struct rpc_task *task;
5288 int ret = 0;
5289
5290 task = _nfs41_proc_sequence(clp, cred);
5291 if (IS_ERR(task))
5292 ret = PTR_ERR(task);
5293 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005294 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005295 dprintk("<-- %s status=%d\n", __func__, ret);
5296 return ret;
5297}
5298
5299static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5300{
5301 struct rpc_task *task;
5302 int ret;
5303
5304 task = _nfs41_proc_sequence(clp, cred);
5305 if (IS_ERR(task)) {
5306 ret = PTR_ERR(task);
5307 goto out;
5308 }
5309 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005310 if (!ret) {
5311 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5312
5313 if (task->tk_status == 0)
5314 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005315 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005316 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005317 rpc_put_task(task);
5318out:
5319 dprintk("<-- %s status=%d\n", __func__, ret);
5320 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005321}
5322
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005323struct nfs4_reclaim_complete_data {
5324 struct nfs_client *clp;
5325 struct nfs41_reclaim_complete_args arg;
5326 struct nfs41_reclaim_complete_res res;
5327};
5328
5329static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5330{
5331 struct nfs4_reclaim_complete_data *calldata = data;
5332
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005333 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168ab2010-06-16 09:52:26 -04005334 if (nfs41_setup_sequence(calldata->clp->cl_session,
5335 &calldata->arg.seq_args,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005336 &calldata->res.seq_res, 0, task))
5337 return;
5338
5339 rpc_call_start(task);
5340}
5341
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005342static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5343{
5344 switch(task->tk_status) {
5345 case 0:
5346 case -NFS4ERR_COMPLETE_ALREADY:
5347 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5348 break;
5349 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005350 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5351 return -EAGAIN;
5352 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005353 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005354 }
5355 return 0;
5356}
5357
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005358static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5359{
5360 struct nfs4_reclaim_complete_data *calldata = data;
5361 struct nfs_client *clp = calldata->clp;
5362 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5363
5364 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005365 if (!nfs41_sequence_done(task, res))
5366 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005367
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005368 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5369 rpc_restart_call_prepare(task);
5370 return;
5371 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005372 dprintk("<-- %s\n", __func__);
5373}
5374
5375static void nfs4_free_reclaim_complete_data(void *data)
5376{
5377 struct nfs4_reclaim_complete_data *calldata = data;
5378
5379 kfree(calldata);
5380}
5381
5382static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5383 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5384 .rpc_call_done = nfs4_reclaim_complete_done,
5385 .rpc_release = nfs4_free_reclaim_complete_data,
5386};
5387
5388/*
5389 * Issue a global reclaim complete.
5390 */
5391static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5392{
5393 struct nfs4_reclaim_complete_data *calldata;
5394 struct rpc_task *task;
5395 struct rpc_message msg = {
5396 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5397 };
5398 struct rpc_task_setup task_setup_data = {
5399 .rpc_client = clp->cl_rpcclient,
5400 .rpc_message = &msg,
5401 .callback_ops = &nfs4_reclaim_complete_call_ops,
5402 .flags = RPC_TASK_ASYNC,
5403 };
5404 int status = -ENOMEM;
5405
5406 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005407 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005408 if (calldata == NULL)
5409 goto out;
5410 calldata->clp = clp;
5411 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005412
5413 msg.rpc_argp = &calldata->arg;
5414 msg.rpc_resp = &calldata->res;
5415 task_setup_data.callback_data = calldata;
5416 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005417 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005418 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005419 goto out;
5420 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005421 status = nfs4_wait_for_completion_rpc_task(task);
5422 if (status == 0)
5423 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005424 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005425 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005426out:
5427 dprintk("<-- %s status=%d\n", __func__, status);
5428 return status;
5429}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005430
5431static void
5432nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5433{
5434 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005435 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005436
5437 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005438 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5439 * right now covering the LAYOUTGET we are about to send.
5440 * However, that is not so catastrophic, and there seems
5441 * to be no way to prevent it completely.
5442 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005443 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5444 &lgp->res.seq_res, 0, task))
5445 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005446 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5447 NFS_I(lgp->args.inode)->layout,
5448 lgp->args.ctx->state)) {
5449 rpc_exit(task, NFS4_OK);
5450 return;
5451 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005452 rpc_call_start(task);
5453}
5454
5455static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5456{
5457 struct nfs4_layoutget *lgp = calldata;
5458 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5459
5460 dprintk("--> %s\n", __func__);
5461
5462 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5463 return;
5464
5465 switch (task->tk_status) {
5466 case 0:
5467 break;
5468 case -NFS4ERR_LAYOUTTRYLATER:
5469 case -NFS4ERR_RECALLCONFLICT:
5470 task->tk_status = -NFS4ERR_DELAY;
5471 /* Fall through */
5472 default:
5473 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5474 rpc_restart_call_prepare(task);
5475 return;
5476 }
5477 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005478 dprintk("<-- %s\n", __func__);
5479}
5480
5481static void nfs4_layoutget_release(void *calldata)
5482{
5483 struct nfs4_layoutget *lgp = calldata;
5484
5485 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005486 if (lgp->res.layout.buf != NULL)
5487 free_page((unsigned long) lgp->res.layout.buf);
5488 put_nfs_open_context(lgp->args.ctx);
5489 kfree(calldata);
5490 dprintk("<-- %s\n", __func__);
5491}
5492
5493static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5494 .rpc_call_prepare = nfs4_layoutget_prepare,
5495 .rpc_call_done = nfs4_layoutget_done,
5496 .rpc_release = nfs4_layoutget_release,
5497};
5498
5499int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5500{
5501 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5502 struct rpc_task *task;
5503 struct rpc_message msg = {
5504 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5505 .rpc_argp = &lgp->args,
5506 .rpc_resp = &lgp->res,
5507 };
5508 struct rpc_task_setup task_setup_data = {
5509 .rpc_client = server->client,
5510 .rpc_message = &msg,
5511 .callback_ops = &nfs4_layoutget_call_ops,
5512 .callback_data = lgp,
5513 .flags = RPC_TASK_ASYNC,
5514 };
5515 int status = 0;
5516
5517 dprintk("--> %s\n", __func__);
5518
5519 lgp->res.layout.buf = (void *)__get_free_page(GFP_NOFS);
5520 if (lgp->res.layout.buf == NULL) {
5521 nfs4_layoutget_release(lgp);
5522 return -ENOMEM;
5523 }
5524
5525 lgp->res.seq_res.sr_slot = NULL;
5526 task = rpc_run_task(&task_setup_data);
5527 if (IS_ERR(task))
5528 return PTR_ERR(task);
5529 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00005530 if (status == 0)
5531 status = task->tk_status;
5532 if (status == 0)
5533 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005534 rpc_put_task(task);
5535 dprintk("<-- %s status=%d\n", __func__, status);
5536 return status;
5537}
5538
5539static int
5540_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5541{
5542 struct nfs4_getdeviceinfo_args args = {
5543 .pdev = pdev,
5544 };
5545 struct nfs4_getdeviceinfo_res res = {
5546 .pdev = pdev,
5547 };
5548 struct rpc_message msg = {
5549 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5550 .rpc_argp = &args,
5551 .rpc_resp = &res,
5552 };
5553 int status;
5554
5555 dprintk("--> %s\n", __func__);
5556 status = nfs4_call_sync(server, &msg, &args, &res, 0);
5557 dprintk("<-- %s status=%d\n", __func__, status);
5558
5559 return status;
5560}
5561
5562int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5563{
5564 struct nfs4_exception exception = { };
5565 int err;
5566
5567 do {
5568 err = nfs4_handle_exception(server,
5569 _nfs4_proc_getdeviceinfo(server, pdev),
5570 &exception);
5571 } while (exception.retry);
5572 return err;
5573}
5574EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5575
Andy Adamson557134a2009-04-01 09:21:53 -04005576#endif /* CONFIG_NFS_V4_1 */
5577
Andy Adamson591d71c2009-04-01 09:22:47 -04005578struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005579 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005580 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581 .recover_open = nfs4_open_reclaim,
5582 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04005583 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005584 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585};
5586
Andy Adamson591d71c2009-04-01 09:22:47 -04005587#if defined(CONFIG_NFS_V4_1)
5588struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5589 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5590 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5591 .recover_open = nfs4_open_reclaim,
5592 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05005593 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005594 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005595 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04005596};
5597#endif /* CONFIG_NFS_V4_1 */
5598
5599struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005600 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005601 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 .recover_open = nfs4_open_expired,
5603 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04005604 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005605 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606};
5607
Andy Adamson591d71c2009-04-01 09:22:47 -04005608#if defined(CONFIG_NFS_V4_1)
5609struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5610 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5611 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5612 .recover_open = nfs4_open_expired,
5613 .recover_lock = nfs4_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05005614 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005615 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04005616};
5617#endif /* CONFIG_NFS_V4_1 */
5618
Benny Halevy29fba382009-04-01 09:22:44 -04005619struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5620 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04005621 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005622 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04005623};
5624
5625#if defined(CONFIG_NFS_V4_1)
5626struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5627 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04005628 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005629 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04005630};
5631#endif
5632
Trond Myklebust97dc1352010-06-16 09:52:26 -04005633static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
5634 .minor_version = 0,
5635 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04005636 .validate_stateid = nfs4_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005637 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
5638 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
5639 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005640};
5641
5642#if defined(CONFIG_NFS_V4_1)
5643static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
5644 .minor_version = 1,
5645 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04005646 .validate_stateid = nfs41_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005647 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
5648 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
5649 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005650};
5651#endif
5652
Trond Myklebust97dc1352010-06-16 09:52:26 -04005653const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
5654 [0] = &nfs_v4_0_minor_ops,
5655#if defined(CONFIG_NFS_V4_1)
5656 [1] = &nfs_v4_1_minor_ops,
5657#endif
5658};
5659
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005660static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005661 .permission = nfs_permission,
5662 .getattr = nfs_getattr,
5663 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005664 .getxattr = generic_getxattr,
5665 .setxattr = generic_setxattr,
5666 .listxattr = generic_listxattr,
5667 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005668};
5669
David Howells509de812006-08-22 20:06:11 -04005670const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 .version = 4, /* protocol version */
5672 .dentry_ops = &nfs4_dentry_operations,
5673 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005674 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 .getroot = nfs4_proc_get_root,
5676 .getattr = nfs4_proc_getattr,
5677 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04005678 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679 .lookup = nfs4_proc_lookup,
5680 .access = nfs4_proc_access,
5681 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682 .create = nfs4_proc_create,
5683 .remove = nfs4_proc_remove,
5684 .unlink_setup = nfs4_proc_unlink_setup,
5685 .unlink_done = nfs4_proc_unlink_done,
5686 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04005687 .rename_setup = nfs4_proc_rename_setup,
5688 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689 .link = nfs4_proc_link,
5690 .symlink = nfs4_proc_symlink,
5691 .mkdir = nfs4_proc_mkdir,
5692 .rmdir = nfs4_proc_remove,
5693 .readdir = nfs4_proc_readdir,
5694 .mknod = nfs4_proc_mknod,
5695 .statfs = nfs4_proc_statfs,
5696 .fsinfo = nfs4_proc_fsinfo,
5697 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04005698 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699 .decode_dirent = nfs4_decode_dirent,
5700 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05005701 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005703 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005705 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005707 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04005708 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04005709 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00005710 .init_client = nfs4_init_client,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711};
5712
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005713static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
5714 .prefix = XATTR_NAME_NFSV4_ACL,
5715 .list = nfs4_xattr_list_nfs4_acl,
5716 .get = nfs4_xattr_get_nfs4_acl,
5717 .set = nfs4_xattr_set_nfs4_acl,
5718};
5719
5720const struct xattr_handler *nfs4_xattr_handlers[] = {
5721 &nfs4_xattr_nfs4_acl_handler,
5722 NULL
5723};
5724
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725/*
5726 * Local variables:
5727 * c-basic-offset: 8
5728 * End:
5729 */