blob: b0c01b23422d97f74a0e102e03822b5013644d33 [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>
Bryan Schumaker8f70e952011-03-24 17:12:31 +000044#include <linux/sunrpc/gss_api.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/nfs.h>
46#include <linux/nfs4.h>
47#include <linux/nfs_fs.h>
48#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040049#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070051#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040052#include <linux/module.h>
Andy Adamson5a0ffe52009-04-01 09:23:18 -040053#include <linux/sunrpc/bc_xprt.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000054#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050055#include <linux/utsname.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Trond Myklebust4ce79712005-06-22 17:16:21 +000057#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050059#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050060#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040061#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040062#include "pnfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64#define NFSDBG_FACILITY NFSDBG_PROC
65
Trond Myklebust2066fe82006-09-15 08:30:46 -040066#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#define NFS4_POLL_RETRY_MAX (15*HZ)
68
Trond Myklebusta78cb572009-08-09 15:06:19 -040069#define NFS4_MAX_LOOP_ON_RECOVER (10)
70
Trond Myklebustcdd4e682006-01-03 09:55:12 +010071struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010072static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080073static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050075static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Trond Myklebust99367812007-07-17 21:52:41 -040076static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040077static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
78 struct nfs_fattr *fattr, struct iattr *sattr,
79 struct nfs4_state *state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040080#ifdef CONFIG_NFS_V4_1
81static int nfs41_test_stateid(struct nfs_server *, struct nfs4_state *);
82static int nfs41_free_stateid(struct nfs_server *, struct nfs4_state *);
83#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070084/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050085static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Trond Myklebust52567b02009-10-23 14:46:42 -040087 if (err >= -1000)
88 return err;
89 switch (err) {
90 case -NFS4ERR_RESOURCE:
91 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +000092 case -NFS4ERR_WRONGSEC:
93 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -080094 case -NFS4ERR_BADOWNER:
95 case -NFS4ERR_BADNAME:
96 return -EINVAL;
Trond Myklebust52567b02009-10-23 14:46:42 -040097 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -070099 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400100 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400102 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
105/*
106 * This is our standard bitmap for GETATTR requests.
107 */
108const u32 nfs4_fattr_bitmap[2] = {
109 FATTR4_WORD0_TYPE
110 | FATTR4_WORD0_CHANGE
111 | FATTR4_WORD0_SIZE
112 | FATTR4_WORD0_FSID
113 | FATTR4_WORD0_FILEID,
114 FATTR4_WORD1_MODE
115 | FATTR4_WORD1_NUMLINKS
116 | FATTR4_WORD1_OWNER
117 | FATTR4_WORD1_OWNER_GROUP
118 | FATTR4_WORD1_RAWDEV
119 | FATTR4_WORD1_SPACE_USED
120 | FATTR4_WORD1_TIME_ACCESS
121 | FATTR4_WORD1_TIME_METADATA
122 | FATTR4_WORD1_TIME_MODIFY
123};
124
125const u32 nfs4_statfs_bitmap[2] = {
126 FATTR4_WORD0_FILES_AVAIL
127 | FATTR4_WORD0_FILES_FREE
128 | FATTR4_WORD0_FILES_TOTAL,
129 FATTR4_WORD1_SPACE_AVAIL
130 | FATTR4_WORD1_SPACE_FREE
131 | FATTR4_WORD1_SPACE_TOTAL
132};
133
Trond Myklebust4ce79712005-06-22 17:16:21 +0000134const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 FATTR4_WORD0_MAXLINK
136 | FATTR4_WORD0_MAXNAME,
137 0
138};
139
Fred Isamandae100c2011-07-30 20:52:37 -0400140const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 | FATTR4_WORD0_MAXREAD
142 | FATTR4_WORD0_MAXWRITE
143 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700144 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400145 | FATTR4_WORD1_FS_LAYOUT_TYPES,
146 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147};
148
Manoj Naik830b8e32006-06-09 09:34:25 -0400149const u32 nfs4_fs_locations_bitmap[2] = {
150 FATTR4_WORD0_TYPE
151 | FATTR4_WORD0_CHANGE
152 | FATTR4_WORD0_SIZE
153 | FATTR4_WORD0_FSID
154 | FATTR4_WORD0_FILEID
155 | FATTR4_WORD0_FS_LOCATIONS,
156 FATTR4_WORD1_MODE
157 | FATTR4_WORD1_NUMLINKS
158 | FATTR4_WORD1_OWNER
159 | FATTR4_WORD1_OWNER_GROUP
160 | FATTR4_WORD1_RAWDEV
161 | FATTR4_WORD1_SPACE_USED
162 | FATTR4_WORD1_TIME_ACCESS
163 | FATTR4_WORD1_TIME_METADATA
164 | FATTR4_WORD1_TIME_MODIFY
165 | FATTR4_WORD1_MOUNTED_ON_FILEID
166};
167
Al Virobc4785c2006-10-19 23:28:51 -0700168static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 struct nfs4_readdir_arg *readdir)
170{
Al Viro0dbb4c62006-10-19 23:28:49 -0700171 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 BUG_ON(readdir->count < 80);
174 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000175 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
177 return;
178 }
179
180 readdir->cookie = 0;
181 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
182 if (cookie == 2)
183 return;
184
185 /*
186 * NFSv4 servers do not return entries for '.' and '..'
187 * Therefore, we fake these entries here. We let '.'
188 * have cookie 0 and '..' have cookie 1. Note that
189 * when talking to the server, we always send cookie 0
190 * instead of 1 or 2.
191 */
Al Viro0dbb4c62006-10-19 23:28:49 -0700192 start = p = kmap_atomic(*readdir->pages, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 if (cookie == 0) {
195 *p++ = xdr_one; /* next */
196 *p++ = xdr_zero; /* cookie, first word */
197 *p++ = xdr_one; /* cookie, second word */
198 *p++ = xdr_one; /* entry len */
199 memcpy(p, ".\0\0\0", 4); /* entry */
200 p++;
201 *p++ = xdr_one; /* bitmap length */
202 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
203 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400204 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
206
207 *p++ = xdr_one; /* next */
208 *p++ = xdr_zero; /* cookie, first word */
209 *p++ = xdr_two; /* cookie, second word */
210 *p++ = xdr_two; /* entry len */
211 memcpy(p, "..\0\0", 4); /* entry */
212 p++;
213 *p++ = xdr_one; /* bitmap length */
214 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
215 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400216 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 readdir->pgbase = (char *)p - (char *)start;
219 readdir->count -= readdir->pgbase;
220 kunmap_atomic(start, KM_USER0);
221}
222
Trond Myklebust65de8722008-12-23 15:21:44 -0500223static int nfs4_wait_clnt_recover(struct nfs_client *clp)
224{
225 int res;
226
227 might_sleep();
228
Trond Myklebuste005e802008-12-23 15:21:48 -0500229 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400230 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500231 return res;
232}
233
234static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
235{
236 int res = 0;
237
238 might_sleep();
239
240 if (*timeout <= 0)
241 *timeout = NFS4_POLL_RETRY_MIN;
242 if (*timeout > NFS4_POLL_RETRY_MAX)
243 *timeout = NFS4_POLL_RETRY_MAX;
244 schedule_timeout_killable(*timeout);
245 if (fatal_signal_pending(current))
246 res = -ERESTARTSYS;
247 *timeout <<= 1;
248 return res;
249}
250
251/* This is the error handling routine for processes that are allowed
252 * to sleep.
253 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800254static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500255{
256 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500257 struct nfs4_state *state = exception->state;
Trond Myklebust65de8722008-12-23 15:21:44 -0500258 int ret = errorcode;
259
260 exception->retry = 0;
261 switch(errorcode) {
262 case 0:
263 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500264 case -NFS4ERR_ADMIN_REVOKED:
265 case -NFS4ERR_BAD_STATEID:
266 case -NFS4ERR_OPENMODE:
267 if (state == NULL)
268 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500269 nfs4_schedule_stateid_recovery(server, state);
270 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400271 case -NFS4ERR_EXPIRED:
272 if (state != NULL)
273 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500274 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500275 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500276 nfs4_schedule_lease_recovery(clp);
277 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500278#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400279 case -NFS4ERR_BADSESSION:
280 case -NFS4ERR_BADSLOT:
281 case -NFS4ERR_BAD_HIGH_SLOT:
282 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
283 case -NFS4ERR_DEADSESSION:
284 case -NFS4ERR_SEQ_FALSE_RETRY:
285 case -NFS4ERR_SEQ_MISORDERED:
286 dprintk("%s ERROR: %d Reset session\n", __func__,
287 errorcode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500288 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -0400289 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500290 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500291#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500292 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500293 if (exception->timeout > HZ) {
294 /* We have retried a decent amount, time to
295 * fail
296 */
297 ret = -EBUSY;
298 break;
299 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500300 case -NFS4ERR_GRACE:
301 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500302 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500303 ret = nfs4_delay(server->client, &exception->timeout);
304 if (ret != 0)
305 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400306 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500307 case -NFS4ERR_OLD_STATEID:
308 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800309 break;
310 case -NFS4ERR_BADOWNER:
311 /* The following works around a Linux server bug! */
312 case -NFS4ERR_BADNAME:
313 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
314 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
315 exception->retry = 1;
316 printk(KERN_WARNING "NFS: v4 server %s "
317 "does not accept raw "
318 "uid/gids. "
319 "Reenabling the idmapper.\n",
320 server->nfs_client->cl_hostname);
321 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500322 }
323 /* We failed to handle the error */
324 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500325wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500326 ret = nfs4_wait_clnt_recover(clp);
327 if (ret == 0)
328 exception->retry = 1;
329 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500330}
331
332
Trond Myklebust452e9352010-07-31 14:29:06 -0400333static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 spin_lock(&clp->cl_lock);
336 if (time_before(clp->cl_last_renewal,timestamp))
337 clp->cl_last_renewal = timestamp;
338 spin_unlock(&clp->cl_lock);
339}
340
Trond Myklebust452e9352010-07-31 14:29:06 -0400341static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
342{
343 do_renew_lease(server->nfs_client, timestamp);
344}
345
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400346#if defined(CONFIG_NFS_V4_1)
347
Benny Halevy510b8172009-04-01 09:22:14 -0400348/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400349 * nfs4_free_slot - free a slot and efficiently update slot table.
350 *
351 * freeing a slot is trivially done by clearing its respective bit
352 * in the bitmap.
353 * If the freed slotid equals highest_used_slotid we want to update it
354 * so that the server would be able to size down the slot table if needed,
355 * otherwise we know that the highest_used_slotid is still in use.
356 * When updating highest_used_slotid there may be "holes" in the bitmap
357 * so we need to scan down from highest_used_slotid to 0 looking for the now
358 * highest slotid in use.
359 * If none found, highest_used_slotid is set to -1.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500360 *
361 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400362 */
363static void
Benny Halevydfb4f3092010-09-24 09:17:01 -0400364nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *free_slot)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400365{
Benny Halevydfb4f3092010-09-24 09:17:01 -0400366 int free_slotid = free_slot - tbl->slots;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400367 int slotid = free_slotid;
368
Benny Halevydfb4f3092010-09-24 09:17:01 -0400369 BUG_ON(slotid < 0 || slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400370 /* clear used bit in bitmap */
371 __clear_bit(slotid, tbl->used_slots);
372
373 /* update highest_used_slotid when it is freed */
374 if (slotid == tbl->highest_used_slotid) {
375 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500376 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400377 tbl->highest_used_slotid = slotid;
378 else
379 tbl->highest_used_slotid = -1;
380 }
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400381 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
382 free_slotid, tbl->highest_used_slotid);
383}
384
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800385/*
Andy Adamson42acd022011-01-06 02:04:34 +0000386 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800387 */
Andy Adamson42acd022011-01-06 02:04:34 +0000388static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800389{
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800390 struct rpc_task *task;
391
Trond Myklebusta2118c32010-06-16 09:52:26 -0400392 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800393 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
394 if (task)
395 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800396 return;
397 }
398
399 if (ses->fc_slot_table.highest_used_slotid != -1)
400 return;
401
Andy Adamson42acd022011-01-06 02:04:34 +0000402 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
403 complete(&ses->fc_slot_table.complete);
404}
405
406/*
407 * Signal state manager thread if session back channel is drained
408 */
409void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
410{
411 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
412 ses->bc_slot_table.highest_used_slotid != -1)
413 return;
414 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
415 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800416}
417
Trond Myklebustd185a332010-06-16 09:52:25 -0400418static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400419{
420 struct nfs4_slot_table *tbl;
421
Trond Myklebustd185a332010-06-16 09:52:25 -0400422 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400423 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400424 /* just wake up the next guy waiting since
425 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500426 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500427 return;
Andy Adamson13615872009-04-01 09:22:17 -0400428 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500429
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500430 spin_lock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400431 nfs4_free_slot(tbl, res->sr_slot);
Andy Adamson42acd022011-01-06 02:04:34 +0000432 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500433 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400434 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400435}
436
Trond Myklebust14516c32010-07-31 14:29:06 -0400437static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400438{
439 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400440 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400441
442 /*
443 * sr_status remains 1 if an RPC level error occurred. The server
444 * may or may not have processed the sequence operation..
445 * Proceed as if the server received and processed the sequence
446 * operation.
447 */
448 if (res->sr_status == 1)
449 res->sr_status = NFS_OK;
450
Bryan Schumaker468f8612011-04-18 15:57:32 -0400451 /* don't increment the sequence number if the task wasn't sent */
452 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400453 goto out;
454
Andy Adamson691daf32009-12-04 15:55:39 -0500455 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400456 switch (res->sr_status) {
457 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400458 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400459 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400460 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400461 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400462 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500463 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500464 if (res->sr_status_flags != 0)
465 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400466 break;
467 case -NFS4ERR_DELAY:
468 /* The server detected a resend of the RPC call and
469 * returned NFS4ERR_DELAY as per Section 2.10.6.2
470 * of RFC5661.
471 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200472 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400473 __func__,
474 res->sr_slot - res->sr_session->fc_slot_table.slots,
475 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400476 goto out_retry;
477 default:
478 /* Just update the slot sequence no. */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400479 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400480 }
481out:
482 /* The session may be reset by one of the error handlers. */
483 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400484 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400485 return 1;
486out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400487 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400488 goto out;
489 rpc_delay(task, NFS4_POLL_RETRY_MAX);
490 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400491}
492
Trond Myklebust14516c32010-07-31 14:29:06 -0400493static int nfs4_sequence_done(struct rpc_task *task,
494 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400495{
Trond Myklebust14516c32010-07-31 14:29:06 -0400496 if (res->sr_session == NULL)
497 return 1;
498 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400499}
500
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400501/*
Benny Halevy510b8172009-04-01 09:22:14 -0400502 * nfs4_find_slot - efficiently look for a free slot
503 *
504 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
505 * If found, we mark the slot as used, update the highest_used_slotid,
506 * and respectively set up the sequence operation args.
507 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400508 *
509 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400510 */
511static u8
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800512nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400513{
514 int slotid;
515 u8 ret_id = NFS4_MAX_SLOT_TABLE;
516 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
517
Benny Halevy510b8172009-04-01 09:22:14 -0400518 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
519 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
520 tbl->max_slots);
521 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
522 if (slotid >= tbl->max_slots)
523 goto out;
524 __set_bit(slotid, tbl->used_slots);
525 if (slotid > tbl->highest_used_slotid)
526 tbl->highest_used_slotid = slotid;
527 ret_id = slotid;
528out:
529 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
530 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400531 return ret_id;
532}
533
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000534int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400535 struct nfs4_sequence_args *args,
536 struct nfs4_sequence_res *res,
537 int cache_reply,
538 struct rpc_task *task)
539{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400540 struct nfs4_slot *slot;
541 struct nfs4_slot_table *tbl;
542 u8 slotid;
543
544 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400545 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400546 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400547 return 0;
548
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400549 tbl = &session->fc_slot_table;
550
551 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400552 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800553 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamsonea028ac2009-12-04 15:55:38 -0500554 /*
555 * The state manager will wait until the slot table is empty.
556 * Schedule the reset thread
557 */
Andy Adamson05f0d232009-12-04 15:55:37 -0500558 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400559 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500560 dprintk("%s Schedule Session Reset\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500561 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400562 }
563
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800564 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
565 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
566 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
567 spin_unlock(&tbl->slot_tbl_lock);
568 dprintk("%s enforce FIFO order\n", __func__);
569 return -EAGAIN;
570 }
571
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800572 slotid = nfs4_find_slot(tbl);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400573 if (slotid == NFS4_MAX_SLOT_TABLE) {
574 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
575 spin_unlock(&tbl->slot_tbl_lock);
576 dprintk("<-- %s: no free slots\n", __func__);
577 return -EAGAIN;
578 }
579 spin_unlock(&tbl->slot_tbl_lock);
580
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800581 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400582 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400583 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400584 args->sa_slotid = slotid;
585 args->sa_cache_this = cache_reply;
586
587 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
588
Benny Halevy99fe60d2009-04-01 09:22:29 -0400589 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400590 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400591 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400592 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400593 /*
594 * sr_status is only set in decode_sequence, and so will remain
595 * set to 1 if an rpc level failure occurs.
596 */
597 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400598 return 0;
599}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000600EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400601
Trond Myklebust035168a2010-06-16 09:52:26 -0400602int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400603 struct nfs4_sequence_args *args,
604 struct nfs4_sequence_res *res,
605 int cache_reply,
606 struct rpc_task *task)
607{
Trond Myklebust035168a2010-06-16 09:52:26 -0400608 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400609 int ret = 0;
610
Trond Myklebust035168a2010-06-16 09:52:26 -0400611 if (session == NULL) {
612 args->sa_session = NULL;
613 res->sr_session = NULL;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400614 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400615 }
616
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200617 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400618 __func__, session->clp, session, res->sr_slot ?
619 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400620
621 ret = nfs41_setup_sequence(session, args, res, cache_reply,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400622 task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400623out:
624 dprintk("<-- %s status=%d\n", __func__, ret);
625 return ret;
626}
627
628struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400629 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400630 struct nfs4_sequence_args *seq_args;
631 struct nfs4_sequence_res *seq_res;
632 int cache_reply;
633};
634
635static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
636{
637 struct nfs41_call_sync_data *data = calldata;
638
Trond Myklebust035168a2010-06-16 09:52:26 -0400639 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
640
641 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400642 data->seq_res, data->cache_reply, task))
643 return;
644 rpc_call_start(task);
645}
646
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800647static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
648{
649 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
650 nfs41_call_sync_prepare(task, calldata);
651}
652
Andy Adamson69ab40c2009-04-01 09:22:19 -0400653static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
654{
655 struct nfs41_call_sync_data *data = calldata;
656
Trond Myklebust14516c32010-07-31 14:29:06 -0400657 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400658}
659
Andy Adamsonce5039c2009-04-01 09:22:13 -0400660struct rpc_call_ops nfs41_call_sync_ops = {
661 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400662 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400663};
664
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800665struct rpc_call_ops nfs41_call_priv_sync_ops = {
666 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
667 .rpc_call_done = nfs41_call_sync_done,
668};
669
Bryan Schumaker7c513052011-03-24 17:12:24 +0000670static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
671 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400672 struct rpc_message *msg,
673 struct nfs4_sequence_args *args,
674 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800675 int cache_reply,
676 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400677{
678 int ret;
679 struct rpc_task *task;
680 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400681 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400682 .seq_args = args,
683 .seq_res = res,
684 .cache_reply = cache_reply,
685 };
686 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000687 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400688 .rpc_message = msg,
689 .callback_ops = &nfs41_call_sync_ops,
690 .callback_data = &data
691 };
692
Benny Halevydfb4f3092010-09-24 09:17:01 -0400693 res->sr_slot = NULL;
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800694 if (privileged)
695 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400696 task = rpc_run_task(&task_setup);
697 if (IS_ERR(task))
698 ret = PTR_ERR(task);
699 else {
700 ret = task->tk_status;
701 rpc_put_task(task);
702 }
703 return ret;
704}
705
Bryan Schumaker7c513052011-03-24 17:12:24 +0000706int _nfs4_call_sync_session(struct rpc_clnt *clnt,
707 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400708 struct rpc_message *msg,
709 struct nfs4_sequence_args *args,
710 struct nfs4_sequence_res *res,
711 int cache_reply)
712{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000713 return nfs4_call_sync_sequence(clnt, server, msg, args, res, cache_reply, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400714}
715
Trond Myklebustdf896452010-06-16 09:52:26 -0400716#else
Trond Myklebust14516c32010-07-31 14:29:06 -0400717static int nfs4_sequence_done(struct rpc_task *task,
718 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400719{
Trond Myklebust14516c32010-07-31 14:29:06 -0400720 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400721}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400722#endif /* CONFIG_NFS_V4_1 */
723
Bryan Schumaker7c513052011-03-24 17:12:24 +0000724int _nfs4_call_sync(struct rpc_clnt *clnt,
725 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400726 struct rpc_message *msg,
727 struct nfs4_sequence_args *args,
728 struct nfs4_sequence_res *res,
729 int cache_reply)
730{
Benny Halevyf3752972009-04-01 09:22:04 -0400731 args->sa_session = res->sr_session = NULL;
Bryan Schumaker7c513052011-03-24 17:12:24 +0000732 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400733}
734
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000735static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000736int nfs4_call_sync(struct rpc_clnt *clnt,
737 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000738 struct rpc_message *msg,
739 struct nfs4_sequence_args *args,
740 struct nfs4_sequence_res *res,
741 int cache_reply)
742{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000743 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
744 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000745}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400746
Trond Myklebust38478b22006-05-25 01:40:57 -0400747static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
Trond Myklebust38478b22006-05-25 01:40:57 -0400749 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Trond Myklebust38478b22006-05-25 01:40:57 -0400751 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400752 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700753 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400754 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700755 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400756 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100759struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400760 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100761 struct nfs_openargs o_arg;
762 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100763 struct nfs_open_confirmargs c_arg;
764 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100765 struct nfs_fattr f_attr;
766 struct nfs_fattr dir_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100767 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400768 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100769 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400770 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100771 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100772 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400773 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100774 int rpc_status;
775 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100776};
777
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400778
779static void nfs4_init_opendata_res(struct nfs4_opendata *p)
780{
781 p->o_res.f_attr = &p->f_attr;
782 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400783 p->o_res.seqid = p->o_arg.seqid;
784 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400785 p->o_res.server = p->o_arg.server;
786 nfs_fattr_init(&p->f_attr);
787 nfs_fattr_init(&p->dir_attr);
788}
789
Al Viro82a2c1b2011-06-22 18:30:55 -0400790static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500791 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400792 const struct iattr *attrs,
793 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100794{
Al Viro82a2c1b2011-06-22 18:30:55 -0400795 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100796 struct inode *dir = parent->d_inode;
797 struct nfs_server *server = NFS_SERVER(dir);
798 struct nfs4_opendata *p;
799
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400800 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100801 if (p == NULL)
802 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400803 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100804 if (p->o_arg.seqid == NULL)
805 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400806 nfs_sb_active(dentry->d_sb);
807 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100808 p->dir = parent;
809 p->owner = sp;
810 atomic_inc(&sp->so_count);
811 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500812 p->o_arg.open_flags = flags;
813 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400814 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400815 p->o_arg.id = sp->so_owner_id.id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400816 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100817 p->o_arg.server = server;
818 p->o_arg.bitmask = server->attr_bitmask;
819 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400820 if (flags & O_CREAT) {
821 u32 *s;
822
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100823 p->o_arg.u.attrs = &p->attrs;
824 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400825 s = (u32 *) p->o_arg.u.verifier.data;
826 s[0] = jiffies;
827 s[1] = current->pid;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100828 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100829 p->c_arg.fh = &p->o_res.fh;
830 p->c_arg.stateid = &p->o_res.stateid;
831 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400832 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400833 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100834 return p;
835err_free:
836 kfree(p);
837err:
838 dput(parent);
839 return NULL;
840}
841
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400842static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100843{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400844 struct nfs4_opendata *p = container_of(kref,
845 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400846 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400847
848 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400849 if (p->state != NULL)
850 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400851 nfs4_put_state_owner(p->owner);
852 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400853 dput(p->dentry);
854 nfs_sb_deactive(sb);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400855 kfree(p);
856}
857
858static void nfs4_opendata_put(struct nfs4_opendata *p)
859{
860 if (p != NULL)
861 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100862}
863
Trond Myklebust06f814a2006-01-03 09:55:07 +0100864static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
865{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100866 int ret;
867
Trond Myklebust06f814a2006-01-03 09:55:07 +0100868 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100869 return ret;
870}
871
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500872static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400873{
874 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500875
876 if (open_mode & O_EXCL)
877 goto out;
878 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400879 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500880 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
881 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400882 break;
883 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500884 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
885 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400886 break;
887 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500888 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
889 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400890 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500891out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400892 return ret;
893}
894
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500895static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400896{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500897 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400898 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500899 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400900 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500901 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400902 return 1;
903}
904
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500905static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100906{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500907 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100908 case FMODE_WRITE:
909 state->n_wronly++;
910 break;
911 case FMODE_READ:
912 state->n_rdonly++;
913 break;
914 case FMODE_READ|FMODE_WRITE:
915 state->n_rdwr++;
916 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500917 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100918}
919
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500920static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400921{
922 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
923 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
924 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500925 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400926 case FMODE_READ:
927 set_bit(NFS_O_RDONLY_STATE, &state->flags);
928 break;
929 case FMODE_WRITE:
930 set_bit(NFS_O_WRONLY_STATE, &state->flags);
931 break;
932 case FMODE_READ|FMODE_WRITE:
933 set_bit(NFS_O_RDWR_STATE, &state->flags);
934 }
935}
936
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500937static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400938{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400939 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500940 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400941 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400942}
943
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500944static 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 -0700945{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400946 /*
947 * Protect the call to nfs4_state_set_mode_locked and
948 * serialise the stateid update
949 */
950 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400951 if (deleg_stateid != NULL) {
952 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
953 set_bit(NFS_DELEGATED_STATE, &state->flags);
954 }
955 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500956 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400957 write_sequnlock(&state->seqlock);
958 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500959 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700960 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961}
962
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500963static 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 -0500964{
965 struct nfs_inode *nfsi = NFS_I(state->inode);
966 struct nfs_delegation *deleg_cur;
967 int ret = 0;
968
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500969 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500970
971 rcu_read_lock();
972 deleg_cur = rcu_dereference(nfsi->delegation);
973 if (deleg_cur == NULL)
974 goto no_delegation;
975
976 spin_lock(&deleg_cur->lock);
977 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500978 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500979 goto no_delegation_unlock;
980
981 if (delegation == NULL)
982 delegation = &deleg_cur->stateid;
983 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
984 goto no_delegation_unlock;
985
Trond Myklebustb7391f42008-12-23 15:21:52 -0500986 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500987 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500988 ret = 1;
989no_delegation_unlock:
990 spin_unlock(&deleg_cur->lock);
991no_delegation:
992 rcu_read_unlock();
993
994 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500995 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500996 ret = 1;
997 }
998
999 return ret;
1000}
1001
1002
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001003static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001004{
1005 struct nfs_delegation *delegation;
1006
1007 rcu_read_lock();
1008 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001009 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001010 rcu_read_unlock();
1011 return;
1012 }
1013 rcu_read_unlock();
1014 nfs_inode_return_delegation(inode);
1015}
1016
Trond Myklebust6ee41262007-07-08 14:11:36 -04001017static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001018{
1019 struct nfs4_state *state = opendata->state;
1020 struct nfs_inode *nfsi = NFS_I(state->inode);
1021 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001022 int open_mode = opendata->o_arg.open_flags & O_EXCL;
1023 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001024 nfs4_stateid stateid;
1025 int ret = -EAGAIN;
1026
Trond Myklebustaac00a82007-07-05 19:02:21 -04001027 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001028 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001029 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001030 if (can_open_cached(state, fmode, open_mode)) {
1031 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001032 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001033 goto out_return_state;
1034 }
1035 spin_unlock(&state->owner->so_lock);
1036 }
Trond Myklebust34310432008-12-23 15:21:38 -05001037 rcu_read_lock();
1038 delegation = rcu_dereference(nfsi->delegation);
1039 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001040 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001041 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001042 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001043 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001044 /* Save the delegation */
1045 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1046 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001047 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001048 if (ret != 0)
1049 goto out;
1050 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001051
1052 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001053 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001054 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001055 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001056out:
1057 return ERR_PTR(ret);
1058out_return_state:
1059 atomic_inc(&state->count);
1060 return state;
1061}
1062
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001063static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1064{
1065 struct inode *inode;
1066 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001067 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001068 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001069
Trond Myklebustaac00a82007-07-05 19:02:21 -04001070 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001071 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001072 goto out;
1073 }
1074
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001075 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001076 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001077 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001078 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001079 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001080 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001081 goto err;
1082 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001083 state = nfs4_get_open_state(inode, data->owner);
1084 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001085 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001086 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001087 int delegation_flags = 0;
1088
Trond Myklebust003707c2007-07-05 18:07:55 -04001089 rcu_read_lock();
1090 delegation = rcu_dereference(NFS_I(inode)->delegation);
1091 if (delegation)
1092 delegation_flags = delegation->flags;
1093 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -05001094 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001095 nfs_inode_set_delegation(state->inode,
1096 data->owner->so_cred,
1097 &data->o_res);
1098 else
1099 nfs_inode_reclaim_delegation(state->inode,
1100 data->owner->so_cred,
1101 &data->o_res);
1102 }
Trond Myklebust34310432008-12-23 15:21:38 -05001103
1104 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001105 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001106 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001107out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001108 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001109err_put_inode:
1110 iput(inode);
1111err:
1112 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001113}
1114
Trond Myklebust864472e2006-01-03 09:55:15 +01001115static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1116{
1117 struct nfs_inode *nfsi = NFS_I(state->inode);
1118 struct nfs_open_context *ctx;
1119
1120 spin_lock(&state->inode->i_lock);
1121 list_for_each_entry(ctx, &nfsi->open_files, list) {
1122 if (ctx->state != state)
1123 continue;
1124 get_nfs_open_context(ctx);
1125 spin_unlock(&state->inode->i_lock);
1126 return ctx;
1127 }
1128 spin_unlock(&state->inode->i_lock);
1129 return ERR_PTR(-ENOENT);
1130}
1131
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001132static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1133{
1134 struct nfs4_opendata *opendata;
1135
Al Viro3d4ff432011-06-22 18:40:12 -04001136 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001137 if (opendata == NULL)
1138 return ERR_PTR(-ENOMEM);
1139 opendata->state = state;
1140 atomic_inc(&state->count);
1141 return opendata;
1142}
1143
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001144static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001145{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001146 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001147 int ret;
1148
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001149 opendata->o_arg.open_flags = 0;
1150 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001151 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1152 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1153 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001154 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001155 if (ret != 0)
1156 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001157 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001158 if (IS_ERR(newstate))
1159 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001160 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001161 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001162 return 0;
1163}
1164
1165static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1166{
Trond Myklebust864472e2006-01-03 09:55:15 +01001167 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001168 int ret;
1169
1170 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001171 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001172 smp_rmb();
1173 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001174 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001175 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001176 if (ret != 0)
1177 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001178 if (newstate != state)
1179 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001180 }
1181 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001182 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001183 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001184 if (ret != 0)
1185 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001186 if (newstate != state)
1187 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001188 }
1189 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001190 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001191 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001192 if (ret != 0)
1193 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001194 if (newstate != state)
1195 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001196 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001197 /*
1198 * We may have performed cached opens for all three recoveries.
1199 * Check if we need to update the current stateid.
1200 */
1201 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1202 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001203 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001204 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1205 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001206 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001207 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001208 return 0;
1209}
1210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211/*
1212 * OPEN_RECLAIM:
1213 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001215static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001217 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001218 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001219 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 int status;
1221
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001222 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1223 if (IS_ERR(opendata))
1224 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001225 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1226 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001227 rcu_read_lock();
1228 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001229 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001230 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001231 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001232 opendata->o_arg.u.delegation_type = delegation_type;
1233 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001234 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 return status;
1236}
1237
Trond Myklebust539cd032007-06-05 11:46:42 -04001238static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
1240 struct nfs_server *server = NFS_SERVER(state->inode);
1241 struct nfs4_exception exception = { };
1242 int err;
1243 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001244 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001245 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001246 break;
1247 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 } while (exception.retry);
1249 return err;
1250}
1251
Trond Myklebust864472e2006-01-03 09:55:15 +01001252static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1253{
1254 struct nfs_open_context *ctx;
1255 int ret;
1256
1257 ctx = nfs4_state_find_open_context(state);
1258 if (IS_ERR(ctx))
1259 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001260 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001261 put_nfs_open_context(ctx);
1262 return ret;
1263}
1264
Trond Myklebust13437e12007-07-06 15:10:43 -04001265static 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 -07001266{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001267 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001268 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001270 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1271 if (IS_ERR(opendata))
1272 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001273 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001274 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001275 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001276 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001277 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001278 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279}
1280
Trond Myklebust13437e12007-07-06 15:10:43 -04001281int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
1283 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001284 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 int err;
1286 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001287 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 switch (err) {
1289 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001290 case -ENOENT:
1291 case -ESTALE:
1292 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001293 case -NFS4ERR_BADSESSION:
1294 case -NFS4ERR_BADSLOT:
1295 case -NFS4ERR_BAD_HIGH_SLOT:
1296 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1297 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001298 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001299 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 case -NFS4ERR_STALE_CLIENTID:
1301 case -NFS4ERR_STALE_STATEID:
1302 case -NFS4ERR_EXPIRED:
1303 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001304 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001305 goto out;
1306 case -ERESTARTSYS:
1307 /*
1308 * The show must go on: exit, but mark the
1309 * stateid as needing recovery.
1310 */
1311 case -NFS4ERR_ADMIN_REVOKED:
1312 case -NFS4ERR_BAD_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001313 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001314 case -EKEYEXPIRED:
1315 /*
1316 * User RPCSEC_GSS context has expired.
1317 * We cannot recover this stateid now, so
1318 * skip it and allow recovery thread to
1319 * proceed.
1320 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001321 case -ENOMEM:
1322 err = 0;
1323 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 }
1325 err = nfs4_handle_exception(server, err, &exception);
1326 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001327out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 return err;
1329}
1330
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001331static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1332{
1333 struct nfs4_opendata *data = calldata;
1334
1335 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001336 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001337 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1338 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001339 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001340 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001341 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001342 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001343}
1344
1345static void nfs4_open_confirm_release(void *calldata)
1346{
1347 struct nfs4_opendata *data = calldata;
1348 struct nfs4_state *state = NULL;
1349
1350 /* If this request hasn't been cancelled, do nothing */
1351 if (data->cancelled == 0)
1352 goto out_free;
1353 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001354 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001355 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001356 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001357 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001358 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001359out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001360 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001361}
1362
1363static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001364 .rpc_call_done = nfs4_open_confirm_done,
1365 .rpc_release = nfs4_open_confirm_release,
1366};
1367
1368/*
1369 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1370 */
1371static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1372{
1373 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1374 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001375 struct rpc_message msg = {
1376 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1377 .rpc_argp = &data->c_arg,
1378 .rpc_resp = &data->c_res,
1379 .rpc_cred = data->owner->so_cred,
1380 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001381 struct rpc_task_setup task_setup_data = {
1382 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001383 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001384 .callback_ops = &nfs4_open_confirm_ops,
1385 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001386 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001387 .flags = RPC_TASK_ASYNC,
1388 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 int status;
1390
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001391 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001392 data->rpc_done = 0;
1393 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001394 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001395 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001396 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001397 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001398 status = nfs4_wait_for_completion_rpc_task(task);
1399 if (status != 0) {
1400 data->cancelled = 1;
1401 smp_wmb();
1402 } else
1403 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001404 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 return status;
1406}
1407
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001408static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001410 struct nfs4_opendata *data = calldata;
1411 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001412
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001413 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1414 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001415 /*
1416 * Check if we still need to send an OPEN call, or if we can use
1417 * a delegation instead.
1418 */
1419 if (data->state != NULL) {
1420 struct nfs_delegation *delegation;
1421
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001422 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001423 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001424 rcu_read_lock();
1425 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1426 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001427 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001428 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001429 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001430 }
1431 rcu_read_unlock();
1432 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001433 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001434 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001435 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001436 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001437 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001438 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1439 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001440 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001441 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001442 &data->o_arg.seq_args,
1443 &data->o_res.seq_res, 1, task))
1444 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001445 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001446 return;
1447out_no_action:
1448 task->tk_action = NULL;
1449
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001450}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001452static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1453{
1454 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1455 nfs4_open_prepare(task, calldata);
1456}
1457
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001458static void nfs4_open_done(struct rpc_task *task, void *calldata)
1459{
1460 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001462 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001463
Trond Myklebust14516c32010-07-31 14:29:06 -04001464 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1465 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001466
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001467 if (task->tk_status == 0) {
1468 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001469 case S_IFREG:
1470 break;
1471 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001472 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001473 break;
1474 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001475 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001476 break;
1477 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001478 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001479 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001480 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001481 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1482 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001483 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001484 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001485}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001486
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001487static void nfs4_open_release(void *calldata)
1488{
1489 struct nfs4_opendata *data = calldata;
1490 struct nfs4_state *state = NULL;
1491
1492 /* If this request hasn't been cancelled, do nothing */
1493 if (data->cancelled == 0)
1494 goto out_free;
1495 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001496 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001497 goto out_free;
1498 /* In case we need an open_confirm, no cleanup! */
1499 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1500 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001501 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001502 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001503 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001504out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001505 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001506}
1507
1508static const struct rpc_call_ops nfs4_open_ops = {
1509 .rpc_call_prepare = nfs4_open_prepare,
1510 .rpc_call_done = nfs4_open_done,
1511 .rpc_release = nfs4_open_release,
1512};
1513
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001514static const struct rpc_call_ops nfs4_recover_open_ops = {
1515 .rpc_call_prepare = nfs4_recover_open_prepare,
1516 .rpc_call_done = nfs4_open_done,
1517 .rpc_release = nfs4_open_release,
1518};
1519
1520static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001521{
1522 struct inode *dir = data->dir->d_inode;
1523 struct nfs_server *server = NFS_SERVER(dir);
1524 struct nfs_openargs *o_arg = &data->o_arg;
1525 struct nfs_openres *o_res = &data->o_res;
1526 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001527 struct rpc_message msg = {
1528 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1529 .rpc_argp = o_arg,
1530 .rpc_resp = o_res,
1531 .rpc_cred = data->owner->so_cred,
1532 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001533 struct rpc_task_setup task_setup_data = {
1534 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001535 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001536 .callback_ops = &nfs4_open_ops,
1537 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001538 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001539 .flags = RPC_TASK_ASYNC,
1540 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001541 int status;
1542
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001543 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001544 data->rpc_done = 0;
1545 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001546 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001547 if (isrecover)
1548 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001549 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001550 if (IS_ERR(task))
1551 return PTR_ERR(task);
1552 status = nfs4_wait_for_completion_rpc_task(task);
1553 if (status != 0) {
1554 data->cancelled = 1;
1555 smp_wmb();
1556 } else
1557 status = data->rpc_status;
1558 rpc_put_task(task);
1559
1560 return status;
1561}
1562
1563static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1564{
1565 struct inode *dir = data->dir->d_inode;
1566 struct nfs_openres *o_res = &data->o_res;
1567 int status;
1568
1569 status = nfs4_run_open_task(data, 1);
1570 if (status != 0 || !data->rpc_done)
1571 return status;
1572
1573 nfs_refresh_inode(dir, o_res->dir_attr);
1574
1575 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1576 status = _nfs4_proc_open_confirm(data);
1577 if (status != 0)
1578 return status;
1579 }
1580
1581 return status;
1582}
1583
1584/*
1585 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1586 */
1587static int _nfs4_proc_open(struct nfs4_opendata *data)
1588{
1589 struct inode *dir = data->dir->d_inode;
1590 struct nfs_server *server = NFS_SERVER(dir);
1591 struct nfs_openargs *o_arg = &data->o_arg;
1592 struct nfs_openres *o_res = &data->o_res;
1593 int status;
1594
1595 status = nfs4_run_open_task(data, 0);
Trond Myklebust3e309912007-07-07 13:19:59 -04001596 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001597 return status;
1598
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001599 if (o_arg->open_flags & O_CREAT) {
1600 update_changeattr(dir, &o_res->cinfo);
1601 nfs_post_op_update_inode(dir, o_res->dir_attr);
1602 } else
1603 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001604 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1605 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001607 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001609 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 }
1611 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001612 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001613 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614}
1615
Andy Adamsond83217c2011-03-01 01:34:17 +00001616static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001617{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001618 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001619 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001620
Trond Myklebusta78cb572009-08-09 15:06:19 -04001621 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001622 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001623 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001624 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001625 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1626 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001627 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001628 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001629 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001630 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001631 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001632}
1633
Andy Adamsond83217c2011-03-01 01:34:17 +00001634static int nfs4_recover_expired_lease(struct nfs_server *server)
1635{
1636 return nfs4_client_recover_expired_lease(server->nfs_client);
1637}
1638
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639/*
1640 * OPEN_EXPIRED:
1641 * reclaim state on the server after a network partition.
1642 * Assumes caller holds the appropriate lock
1643 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001644static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001646 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001647 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001649 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1650 if (IS_ERR(opendata))
1651 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001652 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001653 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001654 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001655 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001656 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657}
1658
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001659static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001660{
Trond Myklebust539cd032007-06-05 11:46:42 -04001661 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001662 struct nfs4_exception exception = { };
1663 int err;
1664
1665 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001666 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001667 switch (err) {
1668 default:
1669 goto out;
1670 case -NFS4ERR_GRACE:
1671 case -NFS4ERR_DELAY:
1672 nfs4_handle_exception(server, err, &exception);
1673 err = 0;
1674 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001675 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001676out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001677 return err;
1678}
1679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1681{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001683 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
Trond Myklebust864472e2006-01-03 09:55:15 +01001685 ctx = nfs4_state_find_open_context(state);
1686 if (IS_ERR(ctx))
1687 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001688 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001689 put_nfs_open_context(ctx);
1690 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691}
1692
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001693#if defined(CONFIG_NFS_V4_1)
1694static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1695{
1696 int status;
1697 struct nfs_server *server = NFS_SERVER(state->inode);
1698
1699 status = nfs41_test_stateid(server, state);
1700 if (status == NFS_OK)
1701 return 0;
1702 nfs41_free_stateid(server, state);
1703 return nfs4_open_expired(sp, state);
1704}
1705#endif
1706
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001708 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1709 * fields corresponding to attributes that were used to store the verifier.
1710 * Make sure we clobber those fields in the later setattr call
1711 */
1712static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1713{
1714 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1715 !(sattr->ia_valid & ATTR_ATIME_SET))
1716 sattr->ia_valid |= ATTR_ATIME;
1717
1718 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1719 !(sattr->ia_valid & ATTR_MTIME_SET))
1720 sattr->ia_valid |= ATTR_MTIME;
1721}
1722
1723/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001724 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 */
Al Viro82a2c1b2011-06-22 18:30:55 -04001726static int _nfs4_do_open(struct inode *dir, struct dentry *dentry, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727{
1728 struct nfs4_state_owner *sp;
1729 struct nfs4_state *state = NULL;
1730 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001731 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001732 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
1734 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 status = -ENOMEM;
1736 if (!(sp = nfs4_get_state_owner(server, cred))) {
1737 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1738 goto out_err;
1739 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001740 status = nfs4_recover_expired_lease(server);
1741 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001742 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001743 if (dentry->d_inode != NULL)
1744 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001745 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001746 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001747 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001748 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Al Viro82a2c1b2011-06-22 18:30:55 -04001750 if (dentry->d_inode != NULL)
1751 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001752
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001753 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001755 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001757 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001758 status = PTR_ERR(state);
1759 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001760 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001761 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001762 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001763
1764 if (opendata->o_arg.open_flags & O_EXCL) {
1765 nfs4_exclusive_attrset(opendata, sattr);
1766
1767 nfs_fattr_init(opendata->o_res.f_attr);
1768 status = nfs4_do_setattr(state->inode, cred,
1769 opendata->o_res.f_attr, sattr,
1770 state);
1771 if (status == 0)
1772 nfs_setattr_update_inode(state->inode, sattr);
1773 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1774 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001775 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 *res = state;
1778 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001779err_opendata_put:
1780 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001781err_put_state_owner:
1782 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 *res = NULL;
1785 return status;
1786}
1787
1788
Al Viro82a2c1b2011-06-22 18:30:55 -04001789static struct nfs4_state *nfs4_do_open(struct inode *dir, struct dentry *dentry, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790{
1791 struct nfs4_exception exception = { };
1792 struct nfs4_state *res;
1793 int status;
1794
1795 do {
Al Viro82a2c1b2011-06-22 18:30:55 -04001796 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 if (status == 0)
1798 break;
1799 /* NOTE: BAD_SEQID means the server and client disagree about the
1800 * book-keeping w.r.t. state-changing operations
1801 * (OPEN/CLOSE/LOCK/LOCKU...)
1802 * It is actually a sign of a bug on the client or on the server.
1803 *
1804 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001805 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 * have unhashed the old state_owner for us, and that we can
1807 * therefore safely retry using a new one. We should still warn
1808 * the user though...
1809 */
1810 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001811 printk(KERN_WARNING "NFS: v4 server %s "
1812 " returned a bad sequence-id error!\n",
1813 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 exception.retry = 1;
1815 continue;
1816 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001817 /*
1818 * BAD_STATEID on OPEN means that the server cancelled our
1819 * state before it received the OPEN_CONFIRM.
1820 * Recover by retrying the request as per the discussion
1821 * on Page 181 of RFC3530.
1822 */
1823 if (status == -NFS4ERR_BAD_STATEID) {
1824 exception.retry = 1;
1825 continue;
1826 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001827 if (status == -EAGAIN) {
1828 /* We must have found a delegation */
1829 exception.retry = 1;
1830 continue;
1831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1833 status, &exception));
1834 } while (exception.retry);
1835 return res;
1836}
1837
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001838static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1839 struct nfs_fattr *fattr, struct iattr *sattr,
1840 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001842 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001844 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 .iap = sattr,
1846 .server = server,
1847 .bitmask = server->attr_bitmask,
1848 };
1849 struct nfs_setattrres res = {
1850 .fattr = fattr,
1851 .server = server,
1852 };
1853 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001854 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1855 .rpc_argp = &arg,
1856 .rpc_resp = &res,
1857 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001859 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001860 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
Trond Myklebust0e574af2005-10-27 22:12:38 -04001862 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001864 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1865 /* Use that stateid */
1866 } else if (state != NULL) {
Trond Myklebust77041ed2010-07-01 12:49:11 -04001867 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001868 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1870
Bryan Schumaker7c513052011-03-24 17:12:24 +00001871 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001872 if (status == 0 && state != NULL)
1873 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001874 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875}
1876
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001877static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1878 struct nfs_fattr *fattr, struct iattr *sattr,
1879 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001881 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 struct nfs4_exception exception = { };
1883 int err;
1884 do {
1885 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001886 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 &exception);
1888 } while (exception.retry);
1889 return err;
1890}
1891
1892struct nfs4_closedata {
1893 struct inode *inode;
1894 struct nfs4_state *state;
1895 struct nfs_closeargs arg;
1896 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001897 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001898 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00001899 bool roc;
1900 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901};
1902
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001903static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001904{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001905 struct nfs4_closedata *calldata = data;
1906 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04001907 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07001908
Fred Isamanf7e89172011-01-06 11:36:32 +00001909 if (calldata->roc)
1910 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001911 nfs4_put_open_state(calldata->state);
1912 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001913 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04001914 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07001915 kfree(calldata);
1916}
1917
Trond Myklebust88069f72009-12-08 08:33:16 -05001918static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1919 fmode_t fmode)
1920{
1921 spin_lock(&state->owner->so_lock);
1922 if (!(fmode & FMODE_READ))
1923 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1924 if (!(fmode & FMODE_WRITE))
1925 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1926 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1927 spin_unlock(&state->owner->so_lock);
1928}
1929
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001930static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001932 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 struct nfs_server *server = NFS_SERVER(calldata->inode);
1935
Trond Myklebust14516c32010-07-31 14:29:06 -04001936 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1937 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 /* hmm. we are done with the inode, and in the process of freeing
1939 * the state_owner. we keep this around to process errors
1940 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 switch (task->tk_status) {
1942 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00001943 if (calldata->roc)
1944 pnfs_roc_set_barrier(state->inode,
1945 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04001946 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001947 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001948 nfs4_close_clear_stateid_flags(state,
1949 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 break;
1951 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001952 case -NFS4ERR_OLD_STATEID:
1953 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001955 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001956 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001958 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1959 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001961 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001962 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963}
1964
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001965static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001967 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001968 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001969 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001970
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001971 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001972 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001973
Trond Myklebust88069f72009-12-08 08:33:16 -05001974 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1975 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001976 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001977 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001978 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001979 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001980 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1981 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1982 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001983 }
1984 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001985 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1986 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1987 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001988 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001989 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001990 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05001991
1992 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001993 /* Note: exit _without_ calling nfs4_close_done */
1994 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001995 return;
1996 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001997
Fred Isamanf7e89172011-01-06 11:36:32 +00001998 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001999 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002000 if (calldata->roc &&
2001 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2002 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2003 task, NULL);
2004 return;
2005 }
2006 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002007
Trond Myklebust516a6af2005-10-27 22:12:41 -04002008 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002009 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002010 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Andy Adamson19ddab02009-04-01 09:22:20 -04002011 &calldata->arg.seq_args, &calldata->res.seq_res,
2012 1, task))
2013 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002014 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015}
2016
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002017static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002018 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002019 .rpc_call_done = nfs4_close_done,
2020 .rpc_release = nfs4_free_closedata,
2021};
2022
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023/*
2024 * It is possible for data to be read/written from a mem-mapped file
2025 * after the sys_close call (which hits the vfs layer as a flush).
2026 * This means that we can't safely call nfsv4 close on a file until
2027 * the inode is cleared. This in turn means that we are not good
2028 * NFSv4 citizens - we do not indicate to the server to update the file's
2029 * share state even when we are done with one of the three share
2030 * stateid's in the inode.
2031 *
2032 * NOTE: Caller must be holding the sp->so_owner semaphore!
2033 */
Al Viro643168c2011-06-22 18:20:23 -04002034int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002036 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002038 struct nfs4_state_owner *sp = state->owner;
2039 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002040 struct rpc_message msg = {
2041 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2042 .rpc_cred = state->owner->so_cred,
2043 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002044 struct rpc_task_setup task_setup_data = {
2045 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002046 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002047 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002048 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002049 .flags = RPC_TASK_ASYNC,
2050 };
Trond Myklebust95121352005-10-18 14:20:12 -07002051 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002053 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002055 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002056 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002058 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002059 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002061 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002062 if (calldata->arg.seqid == NULL)
2063 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002064 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002065 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002066 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002067 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002068 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002069 calldata->roc = roc;
Al Viro643168c2011-06-22 18:20:23 -04002070 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002071
Trond Myklebust1174dd12010-12-21 10:52:24 -05002072 msg.rpc_argp = &calldata->arg;
2073 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002074 task_setup_data.callback_data = calldata;
2075 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002076 if (IS_ERR(task))
2077 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002078 status = 0;
2079 if (wait)
2080 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002081 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002082 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002083out_free_calldata:
2084 kfree(calldata);
2085out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002086 if (roc)
2087 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002088 nfs4_put_open_state(state);
2089 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002090 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091}
2092
Trond Myklebust2b484292010-09-17 10:56:51 -04002093static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002094nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 struct nfs4_state *state;
2097
Trond Myklebust565277f2007-10-15 18:17:53 -04002098 /* Protect against concurrent sillydeletes */
Al Viro3d4ff432011-06-22 18:40:12 -04002099 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002100 if (IS_ERR(state))
2101 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002102 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002103 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104}
2105
Trond Myklebust1185a552009-12-03 15:54:02 -05002106static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002107{
2108 if (ctx->state == NULL)
2109 return;
2110 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002111 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002112 else
Al Viro643168c2011-06-22 18:20:23 -04002113 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002114}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
2116static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2117{
Benny Halevy43652ad2009-04-01 09:21:54 -04002118 struct nfs4_server_caps_arg args = {
2119 .fhandle = fhandle,
2120 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 struct nfs4_server_caps_res res = {};
2122 struct rpc_message msg = {
2123 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002124 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 .rpc_resp = &res,
2126 };
2127 int status;
2128
Bryan Schumaker7c513052011-03-24 17:12:24 +00002129 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 if (status == 0) {
2131 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002132 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2133 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2134 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2135 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2136 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2138 server->caps |= NFS_CAP_ACLS;
2139 if (res.has_links != 0)
2140 server->caps |= NFS_CAP_HARDLINKS;
2141 if (res.has_symlinks != 0)
2142 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002143 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2144 server->caps |= NFS_CAP_FILEID;
2145 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2146 server->caps |= NFS_CAP_MODE;
2147 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2148 server->caps |= NFS_CAP_NLINK;
2149 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2150 server->caps |= NFS_CAP_OWNER;
2151 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2152 server->caps |= NFS_CAP_OWNER_GROUP;
2153 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2154 server->caps |= NFS_CAP_ATIME;
2155 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2156 server->caps |= NFS_CAP_CTIME;
2157 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2158 server->caps |= NFS_CAP_MTIME;
2159
Trond Myklebusta65318b2009-03-11 14:10:28 -04002160 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2161 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2162 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 server->acl_bitmask = res.acl_bitmask;
2164 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002165
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 return status;
2167}
2168
Trond Myklebust55a97592006-06-09 09:34:19 -04002169int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170{
2171 struct nfs4_exception exception = { };
2172 int err;
2173 do {
2174 err = nfs4_handle_exception(server,
2175 _nfs4_server_capabilities(server, fhandle),
2176 &exception);
2177 } while (exception.retry);
2178 return err;
2179}
2180
2181static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2182 struct nfs_fsinfo *info)
2183{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 struct nfs4_lookup_root_arg args = {
2185 .bitmask = nfs4_fattr_bitmap,
2186 };
2187 struct nfs4_lookup_res res = {
2188 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002189 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 .fh = fhandle,
2191 };
2192 struct rpc_message msg = {
2193 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2194 .rpc_argp = &args,
2195 .rpc_resp = &res,
2196 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002197
Trond Myklebust0e574af2005-10-27 22:12:38 -04002198 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002199 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200}
2201
2202static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2203 struct nfs_fsinfo *info)
2204{
2205 struct nfs4_exception exception = { };
2206 int err;
2207 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002208 err = _nfs4_lookup_root(server, fhandle, info);
2209 switch (err) {
2210 case 0:
2211 case -NFS4ERR_WRONGSEC:
2212 break;
2213 default:
2214 err = nfs4_handle_exception(server, err, &exception);
2215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 } while (exception.retry);
2217 return err;
2218}
2219
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002220static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2221 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2222{
2223 struct rpc_auth *auth;
2224 int ret;
2225
2226 auth = rpcauth_create(flavor, server->client);
2227 if (!auth) {
2228 ret = -EIO;
2229 goto out;
2230 }
2231 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002232out:
2233 return ret;
2234}
2235
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002236static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002237 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002239 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002240 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002242 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2243 flav_array[len] = RPC_AUTH_NULL;
2244 len += 1;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002245
2246 for (i = 0; i < len; i++) {
2247 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002248 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002249 continue;
2250 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002251 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002252 /*
2253 * -EACCESS could mean that the user doesn't have correct permissions
2254 * to access the mount. It could also mean that we tried to mount
2255 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2256 * existing mount programs don't handle -EACCES very well so it should
2257 * be mapped to -EPERM instead.
2258 */
2259 if (status == -EACCES)
2260 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002261 return status;
2262}
2263
2264/*
2265 * get the file handle for the "/" directory on the server
2266 */
2267static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2268 struct nfs_fsinfo *info)
2269{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002270 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002271 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002272 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2273 /*
2274 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2275 * by nfs4_map_errors() as this function exits.
2276 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002277 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 if (status == 0)
2279 status = nfs4_server_capabilities(server, fhandle);
2280 if (status == 0)
2281 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002282 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283}
2284
Andy Adamson533eb462011-06-13 18:25:56 -04002285static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002286/*
2287 * Get locations and (maybe) other attributes of a referral.
2288 * Note that we'll actually follow the referral later when
2289 * we detect fsid mismatch in inode revalidation
2290 */
Andy Adamson533eb462011-06-13 18:25:56 -04002291static int nfs4_get_referral(struct inode *dir, const struct qstr *name,
2292 struct nfs_fattr *fattr, struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002293{
2294 int status = -ENOMEM;
2295 struct page *page = NULL;
2296 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002297
2298 page = alloc_page(GFP_KERNEL);
2299 if (page == NULL)
2300 goto out;
2301 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2302 if (locations == NULL)
2303 goto out;
2304
Trond Myklebustc228fd32007-01-13 02:28:11 -05002305 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002306 if (status != 0)
2307 goto out;
2308 /* Make sure server returned a different fsid for the referral */
2309 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002310 dprintk("%s: server did not return a different fsid for"
2311 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002312 status = -EIO;
2313 goto out;
2314 }
Andy Adamson533eb462011-06-13 18:25:56 -04002315 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2316 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002317
Andy Adamson533eb462011-06-13 18:25:56 -04002318 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002319 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002320 memset(fhandle, 0, sizeof(struct nfs_fh));
2321out:
2322 if (page)
2323 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002324 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002325 return status;
2326}
2327
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2329{
2330 struct nfs4_getattr_arg args = {
2331 .fh = fhandle,
2332 .bitmask = server->attr_bitmask,
2333 };
2334 struct nfs4_getattr_res res = {
2335 .fattr = fattr,
2336 .server = server,
2337 };
2338 struct rpc_message msg = {
2339 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2340 .rpc_argp = &args,
2341 .rpc_resp = &res,
2342 };
2343
Trond Myklebust0e574af2005-10-27 22:12:38 -04002344 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002345 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346}
2347
2348static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2349{
2350 struct nfs4_exception exception = { };
2351 int err;
2352 do {
2353 err = nfs4_handle_exception(server,
2354 _nfs4_proc_getattr(server, fhandle, fattr),
2355 &exception);
2356 } while (exception.retry);
2357 return err;
2358}
2359
2360/*
2361 * The file is not closed if it is opened due to the a request to change
2362 * the size of the file. The open call will not be needed once the
2363 * VFS layer lookup-intents are implemented.
2364 *
2365 * Close is called when the inode is destroyed.
2366 * If we haven't opened the file for O_WRONLY, we
2367 * need to in the size_change case to obtain a stateid.
2368 *
2369 * Got race?
2370 * Because OPEN is always done by name in nfsv4, it is
2371 * possible that we opened a different file by the same
2372 * name. We can recognize this race condition, but we
2373 * can't do anything about it besides returning an error.
2374 *
2375 * This will be fixed with VFS changes (lookup-intent).
2376 */
2377static int
2378nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2379 struct iattr *sattr)
2380{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002381 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002382 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002383 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 int status;
2385
Benny Halevy8a1636c2010-07-14 15:43:57 -04002386 if (pnfs_ld_layoutret_on_setattr(inode))
2387 pnfs_return_layout(inode);
2388
Trond Myklebust0e574af2005-10-27 22:12:38 -04002389 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
Trond Myklebustd5308382005-11-04 15:33:38 -05002391 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002392 if (sattr->ia_valid & ATTR_FILE) {
2393 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002394
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002395 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002396 if (ctx) {
2397 cred = ctx->cred;
2398 state = ctx->state;
2399 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002400 }
2401
2402 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002403 if (status == 0)
2404 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 return status;
2406}
2407
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002408static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2409 const struct qstr *name, struct nfs_fh *fhandle,
2410 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002411{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002412 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002413 int status;
2414 struct nfs4_lookup_arg args = {
2415 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002416 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002417 .name = name,
2418 };
2419 struct nfs4_lookup_res res = {
2420 .server = server,
2421 .fattr = fattr,
2422 .fh = fhandle,
2423 };
2424 struct rpc_message msg = {
2425 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2426 .rpc_argp = &args,
2427 .rpc_resp = &res,
2428 };
2429
2430 nfs_fattr_init(fattr);
2431
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002433 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 dprintk("NFS reply lookup: %d\n", status);
2435 return status;
2436}
2437
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002438void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2439{
2440 memset(fh, 0, sizeof(struct nfs_fh));
2441 fattr->fsid.major = 1;
2442 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2443 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2444 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2445 fattr->nlink = 2;
2446}
2447
Bryan Schumaker7c513052011-03-24 17:12:24 +00002448static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2449 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450{
2451 struct nfs4_exception exception = { };
2452 int err;
2453 do {
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002454 int status;
2455
2456 status = _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr);
2457 switch (status) {
2458 case -NFS4ERR_MOVED:
2459 err = nfs4_get_referral(dir, name, fattr, fhandle);
2460 break;
2461 case -NFS4ERR_WRONGSEC:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002462 nfs_fixup_secinfo_attributes(fattr, fhandle);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002463 }
2464 err = nfs4_handle_exception(NFS_SERVER(dir),
2465 status, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 } while (exception.retry);
2467 return err;
2468}
2469
2470static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2471{
Trond Myklebust76b32992007-08-10 17:45:11 -04002472 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 struct nfs4_accessargs args = {
2474 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002475 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002477 struct nfs4_accessres res = {
2478 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002479 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 struct rpc_message msg = {
2481 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2482 .rpc_argp = &args,
2483 .rpc_resp = &res,
2484 .rpc_cred = entry->cred,
2485 };
2486 int mode = entry->mask;
2487 int status;
2488
2489 /*
2490 * Determine which access bits we want to ask for...
2491 */
2492 if (mode & MAY_READ)
2493 args.access |= NFS4_ACCESS_READ;
2494 if (S_ISDIR(inode->i_mode)) {
2495 if (mode & MAY_WRITE)
2496 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2497 if (mode & MAY_EXEC)
2498 args.access |= NFS4_ACCESS_LOOKUP;
2499 } else {
2500 if (mode & MAY_WRITE)
2501 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2502 if (mode & MAY_EXEC)
2503 args.access |= NFS4_ACCESS_EXECUTE;
2504 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002505
2506 res.fattr = nfs_alloc_fattr();
2507 if (res.fattr == NULL)
2508 return -ENOMEM;
2509
Bryan Schumaker7c513052011-03-24 17:12:24 +00002510 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 if (!status) {
2512 entry->mask = 0;
2513 if (res.access & NFS4_ACCESS_READ)
2514 entry->mask |= MAY_READ;
2515 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2516 entry->mask |= MAY_WRITE;
2517 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2518 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002519 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002521 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 return status;
2523}
2524
2525static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2526{
2527 struct nfs4_exception exception = { };
2528 int err;
2529 do {
2530 err = nfs4_handle_exception(NFS_SERVER(inode),
2531 _nfs4_proc_access(inode, entry),
2532 &exception);
2533 } while (exception.retry);
2534 return err;
2535}
2536
2537/*
2538 * TODO: For the time being, we don't try to get any attributes
2539 * along with any of the zero-copy operations READ, READDIR,
2540 * READLINK, WRITE.
2541 *
2542 * In the case of the first three, we want to put the GETATTR
2543 * after the read-type operation -- this is because it is hard
2544 * to predict the length of a GETATTR response in v4, and thus
2545 * align the READ data correctly. This means that the GETATTR
2546 * may end up partially falling into the page cache, and we should
2547 * shift it into the 'tail' of the xdr_buf before processing.
2548 * To do this efficiently, we need to know the total length
2549 * of data received, which doesn't seem to be available outside
2550 * of the RPC layer.
2551 *
2552 * In the case of WRITE, we also want to put the GETATTR after
2553 * the operation -- in this case because we want to make sure
2554 * we get the post-operation mtime and size. This means that
2555 * we can't use xdr_encode_pages() as written: we need a variant
2556 * of it which would leave room in the 'tail' iovec.
2557 *
2558 * Both of these changes to the XDR layer would in fact be quite
2559 * minor, but I decided to leave them for a subsequent patch.
2560 */
2561static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2562 unsigned int pgbase, unsigned int pglen)
2563{
2564 struct nfs4_readlink args = {
2565 .fh = NFS_FH(inode),
2566 .pgbase = pgbase,
2567 .pglen = pglen,
2568 .pages = &page,
2569 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002570 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 struct rpc_message msg = {
2572 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2573 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002574 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 };
2576
Bryan Schumaker7c513052011-03-24 17:12:24 +00002577 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578}
2579
2580static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2581 unsigned int pgbase, unsigned int pglen)
2582{
2583 struct nfs4_exception exception = { };
2584 int err;
2585 do {
2586 err = nfs4_handle_exception(NFS_SERVER(inode),
2587 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2588 &exception);
2589 } while (exception.retry);
2590 return err;
2591}
2592
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593/*
2594 * Got race?
2595 * We will need to arrange for the VFS layer to provide an atomic open.
2596 * Until then, this create/open method is prone to inefficiency and race
2597 * conditions due to the lookup, create, and open VFS calls from sys_open()
2598 * placed on the wire.
2599 *
2600 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2601 * The file will be opened again in the subsequent VFS open call
2602 * (nfs4_proc_file_open).
2603 *
2604 * The open for read will just hang around to be used by any process that
2605 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2606 */
2607
2608static int
2609nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002610 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611{
Al Viro82a2c1b2011-06-22 18:30:55 -04002612 struct dentry *de = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002614 struct rpc_cred *cred = NULL;
2615 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 int status = 0;
2617
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002618 if (ctx != NULL) {
2619 cred = ctx->cred;
Al Viro3d4ff432011-06-22 18:40:12 -04002620 de = ctx->dentry;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002621 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002623 sattr->ia_mode &= ~current_umask();
Al Viro82a2c1b2011-06-22 18:30:55 -04002624 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002625 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 if (IS_ERR(state)) {
2627 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002628 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002630 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002631 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002632 if (ctx != NULL)
2633 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002634 else
Al Viro643168c2011-06-22 18:20:23 -04002635 nfs4_close_sync(state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636out:
2637 return status;
2638}
2639
2640static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2641{
Trond Myklebust16e42952005-10-27 22:12:44 -04002642 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002643 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002645 .name.len = name->len,
2646 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002647 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002649 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002650 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002651 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002653 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2654 .rpc_argp = &args,
2655 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002657 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658
Trond Myklebustd3468902010-04-16 16:22:50 -04002659 res.dir_attr = nfs_alloc_fattr();
2660 if (res.dir_attr == NULL)
2661 goto out;
2662
Bryan Schumaker7c513052011-03-24 17:12:24 +00002663 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002664 if (status == 0) {
2665 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002666 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002667 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002668 nfs_free_fattr(res.dir_attr);
2669out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 return status;
2671}
2672
2673static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2674{
2675 struct nfs4_exception exception = { };
2676 int err;
2677 do {
2678 err = nfs4_handle_exception(NFS_SERVER(dir),
2679 _nfs4_proc_remove(dir, name),
2680 &exception);
2681 } while (exception.retry);
2682 return err;
2683}
2684
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002685static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002687 struct nfs_server *server = NFS_SERVER(dir);
2688 struct nfs_removeargs *args = msg->rpc_argp;
2689 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
Trond Myklebusta65318b2009-03-11 14:10:28 -04002691 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002692 res->server = server;
Benny Halevydfb4f3092010-09-24 09:17:01 -04002693 res->seq_res.sr_slot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695}
2696
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002697static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002699 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2700
Trond Myklebust14516c32010-07-31 14:29:06 -04002701 if (!nfs4_sequence_done(task, &res->seq_res))
2702 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002703 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002704 return 0;
2705 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002706 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002707 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708}
2709
Jeff Laytond3d41522010-09-17 17:31:57 -04002710static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2711{
2712 struct nfs_server *server = NFS_SERVER(dir);
2713 struct nfs_renameargs *arg = msg->rpc_argp;
2714 struct nfs_renameres *res = msg->rpc_resp;
2715
2716 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2717 arg->bitmask = server->attr_bitmask;
2718 res->server = server;
2719}
2720
2721static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2722 struct inode *new_dir)
2723{
2724 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2725
2726 if (!nfs4_sequence_done(task, &res->seq_res))
2727 return 0;
2728 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2729 return 0;
2730
2731 update_changeattr(old_dir, &res->old_cinfo);
2732 nfs_post_op_update_inode(old_dir, res->old_fattr);
2733 update_changeattr(new_dir, &res->new_cinfo);
2734 nfs_post_op_update_inode(new_dir, res->new_fattr);
2735 return 1;
2736}
2737
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2739 struct inode *new_dir, struct qstr *new_name)
2740{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002741 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002742 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 .old_dir = NFS_FH(old_dir),
2744 .new_dir = NFS_FH(new_dir),
2745 .old_name = old_name,
2746 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002747 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002749 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002750 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002751 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 struct rpc_message msg = {
2753 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2754 .rpc_argp = &arg,
2755 .rpc_resp = &res,
2756 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002757 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758
Trond Myklebust011fff72010-04-16 16:22:49 -04002759 res.old_fattr = nfs_alloc_fattr();
2760 res.new_fattr = nfs_alloc_fattr();
2761 if (res.old_fattr == NULL || res.new_fattr == NULL)
2762 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763
Bryan Schumaker7c513052011-03-24 17:12:24 +00002764 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 if (!status) {
2766 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002767 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002769 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002771out:
2772 nfs_free_fattr(res.new_fattr);
2773 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 return status;
2775}
2776
2777static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2778 struct inode *new_dir, struct qstr *new_name)
2779{
2780 struct nfs4_exception exception = { };
2781 int err;
2782 do {
2783 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2784 _nfs4_proc_rename(old_dir, old_name,
2785 new_dir, new_name),
2786 &exception);
2787 } while (exception.retry);
2788 return err;
2789}
2790
2791static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2792{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002793 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 struct nfs4_link_arg arg = {
2795 .fh = NFS_FH(inode),
2796 .dir_fh = NFS_FH(dir),
2797 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002798 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002800 struct nfs4_link_res res = {
2801 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002802 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 struct rpc_message msg = {
2804 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2805 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002806 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002808 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809
Trond Myklebust136f2622010-04-16 16:22:49 -04002810 res.fattr = nfs_alloc_fattr();
2811 res.dir_attr = nfs_alloc_fattr();
2812 if (res.fattr == NULL || res.dir_attr == NULL)
2813 goto out;
2814
Bryan Schumaker7c513052011-03-24 17:12:24 +00002815 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002816 if (!status) {
2817 update_changeattr(dir, &res.cinfo);
2818 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002819 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002820 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002821out:
2822 nfs_free_fattr(res.dir_attr);
2823 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 return status;
2825}
2826
2827static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2828{
2829 struct nfs4_exception exception = { };
2830 int err;
2831 do {
2832 err = nfs4_handle_exception(NFS_SERVER(inode),
2833 _nfs4_proc_link(inode, dir, name),
2834 &exception);
2835 } while (exception.retry);
2836 return err;
2837}
2838
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002839struct nfs4_createdata {
2840 struct rpc_message msg;
2841 struct nfs4_create_arg arg;
2842 struct nfs4_create_res res;
2843 struct nfs_fh fh;
2844 struct nfs_fattr fattr;
2845 struct nfs_fattr dir_fattr;
2846};
2847
2848static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2849 struct qstr *name, struct iattr *sattr, u32 ftype)
2850{
2851 struct nfs4_createdata *data;
2852
2853 data = kzalloc(sizeof(*data), GFP_KERNEL);
2854 if (data != NULL) {
2855 struct nfs_server *server = NFS_SERVER(dir);
2856
2857 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2858 data->msg.rpc_argp = &data->arg;
2859 data->msg.rpc_resp = &data->res;
2860 data->arg.dir_fh = NFS_FH(dir);
2861 data->arg.server = server;
2862 data->arg.name = name;
2863 data->arg.attrs = sattr;
2864 data->arg.ftype = ftype;
2865 data->arg.bitmask = server->attr_bitmask;
2866 data->res.server = server;
2867 data->res.fh = &data->fh;
2868 data->res.fattr = &data->fattr;
2869 data->res.dir_fattr = &data->dir_fattr;
2870 nfs_fattr_init(data->res.fattr);
2871 nfs_fattr_init(data->res.dir_fattr);
2872 }
2873 return data;
2874}
2875
2876static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2877{
Bryan Schumaker7c513052011-03-24 17:12:24 +00002878 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00002879 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002880 if (status == 0) {
2881 update_changeattr(dir, &data->res.dir_cinfo);
2882 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2883 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2884 }
2885 return status;
2886}
2887
2888static void nfs4_free_createdata(struct nfs4_createdata *data)
2889{
2890 kfree(data);
2891}
2892
Chuck Lever4f390c12006-08-22 20:06:22 -04002893static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002894 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002896 struct nfs4_createdata *data;
2897 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898
Chuck Lever94a6d752006-08-22 20:06:23 -04002899 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002900 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002901
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002902 status = -ENOMEM;
2903 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2904 if (data == NULL)
2905 goto out;
2906
2907 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2908 data->arg.u.symlink.pages = &page;
2909 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002911 status = nfs4_do_create(dir, dentry, data);
2912
2913 nfs4_free_createdata(data);
2914out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 return status;
2916}
2917
Chuck Lever4f390c12006-08-22 20:06:22 -04002918static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002919 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920{
2921 struct nfs4_exception exception = { };
2922 int err;
2923 do {
2924 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002925 _nfs4_proc_symlink(dir, dentry, page,
2926 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 &exception);
2928 } while (exception.retry);
2929 return err;
2930}
2931
2932static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2933 struct iattr *sattr)
2934{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002935 struct nfs4_createdata *data;
2936 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002938 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2939 if (data == NULL)
2940 goto out;
2941
2942 status = nfs4_do_create(dir, dentry, data);
2943
2944 nfs4_free_createdata(data);
2945out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 return status;
2947}
2948
2949static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2950 struct iattr *sattr)
2951{
2952 struct nfs4_exception exception = { };
2953 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002954
2955 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 do {
2957 err = nfs4_handle_exception(NFS_SERVER(dir),
2958 _nfs4_proc_mkdir(dir, dentry, sattr),
2959 &exception);
2960 } while (exception.retry);
2961 return err;
2962}
2963
2964static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002965 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966{
2967 struct inode *dir = dentry->d_inode;
2968 struct nfs4_readdir_arg args = {
2969 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002970 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 .pgbase = 0,
2972 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002973 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04002974 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 };
2976 struct nfs4_readdir_res res;
2977 struct rpc_message msg = {
2978 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2979 .rpc_argp = &args,
2980 .rpc_resp = &res,
2981 .rpc_cred = cred,
2982 };
2983 int status;
2984
Harvey Harrison3110ff82008-05-02 13:42:44 -07002985 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002986 dentry->d_parent->d_name.name,
2987 dentry->d_name.name,
2988 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2990 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00002991 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebustac396122010-11-15 20:26:22 -05002992 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05002994 status += args.pgbase;
2995 }
Trond Myklebustc4812992007-09-28 17:11:45 -04002996
2997 nfs_invalidate_atime(dir);
2998
Harvey Harrison3110ff82008-05-02 13:42:44 -07002999 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 return status;
3001}
3002
3003static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003004 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005{
3006 struct nfs4_exception exception = { };
3007 int err;
3008 do {
3009 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3010 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003011 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 &exception);
3013 } while (exception.retry);
3014 return err;
3015}
3016
3017static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3018 struct iattr *sattr, dev_t rdev)
3019{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003020 struct nfs4_createdata *data;
3021 int mode = sattr->ia_mode;
3022 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023
3024 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3025 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003026
3027 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3028 if (data == NULL)
3029 goto out;
3030
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003032 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003034 data->arg.ftype = NF4BLK;
3035 data->arg.u.device.specdata1 = MAJOR(rdev);
3036 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 }
3038 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003039 data->arg.ftype = NF4CHR;
3040 data->arg.u.device.specdata1 = MAJOR(rdev);
3041 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003044 status = nfs4_do_create(dir, dentry, data);
3045
3046 nfs4_free_createdata(data);
3047out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 return status;
3049}
3050
3051static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3052 struct iattr *sattr, dev_t rdev)
3053{
3054 struct nfs4_exception exception = { };
3055 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003056
3057 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 do {
3059 err = nfs4_handle_exception(NFS_SERVER(dir),
3060 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3061 &exception);
3062 } while (exception.retry);
3063 return err;
3064}
3065
3066static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3067 struct nfs_fsstat *fsstat)
3068{
3069 struct nfs4_statfs_arg args = {
3070 .fh = fhandle,
3071 .bitmask = server->attr_bitmask,
3072 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003073 struct nfs4_statfs_res res = {
3074 .fsstat = fsstat,
3075 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 struct rpc_message msg = {
3077 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3078 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003079 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 };
3081
Trond Myklebust0e574af2005-10-27 22:12:38 -04003082 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003083 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084}
3085
3086static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3087{
3088 struct nfs4_exception exception = { };
3089 int err;
3090 do {
3091 err = nfs4_handle_exception(server,
3092 _nfs4_proc_statfs(server, fhandle, fsstat),
3093 &exception);
3094 } while (exception.retry);
3095 return err;
3096}
3097
3098static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3099 struct nfs_fsinfo *fsinfo)
3100{
3101 struct nfs4_fsinfo_arg args = {
3102 .fh = fhandle,
3103 .bitmask = server->attr_bitmask,
3104 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003105 struct nfs4_fsinfo_res res = {
3106 .fsinfo = fsinfo,
3107 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 struct rpc_message msg = {
3109 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3110 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003111 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 };
3113
Bryan Schumaker7c513052011-03-24 17:12:24 +00003114 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115}
3116
3117static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3118{
3119 struct nfs4_exception exception = { };
3120 int err;
3121
3122 do {
3123 err = nfs4_handle_exception(server,
3124 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3125 &exception);
3126 } while (exception.retry);
3127 return err;
3128}
3129
3130static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3131{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003132 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3134}
3135
3136static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3137 struct nfs_pathconf *pathconf)
3138{
3139 struct nfs4_pathconf_arg args = {
3140 .fh = fhandle,
3141 .bitmask = server->attr_bitmask,
3142 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003143 struct nfs4_pathconf_res res = {
3144 .pathconf = pathconf,
3145 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 struct rpc_message msg = {
3147 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3148 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003149 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 };
3151
3152 /* None of the pathconf attributes are mandatory to implement */
3153 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3154 memset(pathconf, 0, sizeof(*pathconf));
3155 return 0;
3156 }
3157
Trond Myklebust0e574af2005-10-27 22:12:38 -04003158 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003159 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160}
3161
3162static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3163 struct nfs_pathconf *pathconf)
3164{
3165 struct nfs4_exception exception = { };
3166 int err;
3167
3168 do {
3169 err = nfs4_handle_exception(server,
3170 _nfs4_proc_pathconf(server, fhandle, pathconf),
3171 &exception);
3172 } while (exception.retry);
3173 return err;
3174}
3175
Benny Halevyd20581a2011-05-22 19:52:03 +03003176void __nfs4_read_done_cb(struct nfs_read_data *data)
3177{
3178 nfs_invalidate_atime(data->inode);
3179}
3180
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003181static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182{
Trond Myklebustec06c092006-03-20 13:44:27 -05003183 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003185 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003186 nfs_restart_rpc(task, server->nfs_client);
Trond Myklebustec06c092006-03-20 13:44:27 -05003187 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003189
Benny Halevyd20581a2011-05-22 19:52:03 +03003190 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003192 renew_lease(server, data->timestamp);
3193 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194}
3195
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003196static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3197{
3198
3199 dprintk("--> %s\n", __func__);
3200
3201 if (!nfs4_sequence_done(task, &data->res.seq_res))
3202 return -EAGAIN;
3203
Benny Halevyd20581a2011-05-22 19:52:03 +03003204 return data->read_done_cb ? data->read_done_cb(task, data) :
3205 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003206}
3207
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003208static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003211 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003212 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213}
3214
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003215/* Reset the the nfs_read_data to send the read to the MDS. */
3216void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3217{
3218 dprintk("%s Reset task for i/o through\n", __func__);
3219 put_lseg(data->lseg);
3220 data->lseg = NULL;
3221 /* offsets will differ in the dense stripe case */
3222 data->args.offset = data->mds_offset;
3223 data->ds_clp = NULL;
3224 data->args.fh = NFS_FH(data->inode);
3225 data->read_done_cb = nfs4_read_done_cb;
3226 task->tk_ops = data->mds_ops;
3227 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3228}
3229EXPORT_SYMBOL_GPL(nfs4_reset_read);
3230
Fred Isamanb029bc92011-03-03 15:13:42 +00003231static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 struct inode *inode = data->inode;
3234
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003235 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003236 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003237 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003239 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003241 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003242 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003243 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244}
3245
Fred Isamanb029bc92011-03-03 15:13:42 +00003246static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3247{
3248 if (!nfs4_sequence_done(task, &data->res.seq_res))
3249 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003250 return data->write_done_cb ? data->write_done_cb(task, data) :
3251 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003252}
3253
Fred Isamana69aef12011-03-03 15:13:47 +00003254/* Reset the the nfs_write_data to send the write to the MDS. */
3255void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3256{
3257 dprintk("%s Reset task for i/o through\n", __func__);
3258 put_lseg(data->lseg);
3259 data->lseg = NULL;
3260 data->ds_clp = NULL;
3261 data->write_done_cb = nfs4_write_done_cb;
3262 data->args.fh = NFS_FH(data->inode);
3263 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3264 data->args.offset = data->mds_offset;
3265 data->res.fattr = &data->fattr;
3266 task->tk_ops = data->mds_ops;
3267 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3268}
3269EXPORT_SYMBOL_GPL(nfs4_reset_write);
3270
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003271static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003273 struct nfs_server *server = NFS_SERVER(data->inode);
3274
Fred Isaman7ffd1062011-03-03 15:13:46 +00003275 if (data->lseg) {
3276 data->args.bitmask = NULL;
3277 data->res.fattr = NULL;
3278 } else
3279 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isamanb029bc92011-03-03 15:13:42 +00003280 if (!data->write_done_cb)
3281 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003282 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 data->timestamp = jiffies;
3284
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003285 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286}
3287
Fred Isaman5f452432011-03-23 13:27:46 +00003288static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003291
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003292 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003293 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003294 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003296 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003297 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298}
3299
Fred Isaman5f452432011-03-23 13:27:46 +00003300static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3301{
3302 if (!nfs4_sequence_done(task, &data->res.seq_res))
3303 return -EAGAIN;
3304 return data->write_done_cb(task, data);
3305}
3306
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003307static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003309 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003310
3311 if (data->lseg) {
3312 data->args.bitmask = NULL;
3313 data->res.fattr = NULL;
3314 } else
3315 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isaman5f452432011-03-23 13:27:46 +00003316 if (!data->write_done_cb)
3317 data->write_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003318 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003319 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320}
3321
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003322struct nfs4_renewdata {
3323 struct nfs_client *client;
3324 unsigned long timestamp;
3325};
3326
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327/*
3328 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3329 * standalone procedure for queueing an asynchronous RENEW.
3330 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003331static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003332{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003333 struct nfs4_renewdata *data = calldata;
3334 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003335
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003336 if (atomic_read(&clp->cl_count) > 1)
3337 nfs4_schedule_state_renewal(clp);
3338 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003339 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003340}
3341
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003342static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003344 struct nfs4_renewdata *data = calldata;
3345 struct nfs_client *clp = data->client;
3346 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347
3348 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003349 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003350 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3351 return;
3352 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003353 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003354 return;
3355 }
3356 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003358 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359}
3360
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003361static const struct rpc_call_ops nfs4_renew_ops = {
3362 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003363 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003364};
3365
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003366static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367{
3368 struct rpc_message msg = {
3369 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3370 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003371 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003373 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003375 if (renew_flags == 0)
3376 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003377 if (!atomic_inc_not_zero(&clp->cl_count))
3378 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003379 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003380 if (data == NULL)
3381 return -ENOMEM;
3382 data->client = clp;
3383 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003385 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386}
3387
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003388static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389{
3390 struct rpc_message msg = {
3391 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3392 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003393 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 };
3395 unsigned long now = jiffies;
3396 int status;
3397
3398 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3399 if (status < 0)
3400 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003401 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 return 0;
3403}
3404
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003405static inline int nfs4_server_supports_acls(struct nfs_server *server)
3406{
3407 return (server->caps & NFS_CAP_ACLS)
3408 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3409 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3410}
3411
3412/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3413 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3414 * the stack.
3415 */
3416#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3417
3418static void buf_to_pages(const void *buf, size_t buflen,
3419 struct page **pages, unsigned int *pgbase)
3420{
3421 const void *p = buf;
3422
3423 *pgbase = offset_in_page(buf);
3424 p -= *pgbase;
3425 while (p < buf + buflen) {
3426 *(pages++) = virt_to_page(p);
3427 p += PAGE_CACHE_SIZE;
3428 }
3429}
3430
Neil Hormane9e3d722011-03-04 19:26:03 -05003431static int buf_to_pages_noslab(const void *buf, size_t buflen,
3432 struct page **pages, unsigned int *pgbase)
3433{
3434 struct page *newpage, **spages;
3435 int rc = 0;
3436 size_t len;
3437 spages = pages;
3438
3439 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003440 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003441 newpage = alloc_page(GFP_KERNEL);
3442
3443 if (newpage == NULL)
3444 goto unwind;
3445 memcpy(page_address(newpage), buf, len);
3446 buf += len;
3447 buflen -= len;
3448 *pages++ = newpage;
3449 rc++;
3450 } while (buflen != 0);
3451
3452 return rc;
3453
3454unwind:
3455 for(; rc > 0; rc--)
3456 __free_page(spages[rc-1]);
3457 return -ENOMEM;
3458}
3459
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003460struct nfs4_cached_acl {
3461 int cached;
3462 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003463 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003464};
3465
3466static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003467{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003468 struct nfs_inode *nfsi = NFS_I(inode);
3469
3470 spin_lock(&inode->i_lock);
3471 kfree(nfsi->nfs4_acl);
3472 nfsi->nfs4_acl = acl;
3473 spin_unlock(&inode->i_lock);
3474}
3475
3476static void nfs4_zap_acl_attr(struct inode *inode)
3477{
3478 nfs4_set_cached_acl(inode, NULL);
3479}
3480
3481static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3482{
3483 struct nfs_inode *nfsi = NFS_I(inode);
3484 struct nfs4_cached_acl *acl;
3485 int ret = -ENOENT;
3486
3487 spin_lock(&inode->i_lock);
3488 acl = nfsi->nfs4_acl;
3489 if (acl == NULL)
3490 goto out;
3491 if (buf == NULL) /* user is just asking for length */
3492 goto out_len;
3493 if (acl->cached == 0)
3494 goto out;
3495 ret = -ERANGE; /* see getxattr(2) man page */
3496 if (acl->len > buflen)
3497 goto out;
3498 memcpy(buf, acl->data, acl->len);
3499out_len:
3500 ret = acl->len;
3501out:
3502 spin_unlock(&inode->i_lock);
3503 return ret;
3504}
3505
3506static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3507{
3508 struct nfs4_cached_acl *acl;
3509
3510 if (buf && acl_len <= PAGE_SIZE) {
3511 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3512 if (acl == NULL)
3513 goto out;
3514 acl->cached = 1;
3515 memcpy(acl->data, buf, acl_len);
3516 } else {
3517 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3518 if (acl == NULL)
3519 goto out;
3520 acl->cached = 0;
3521 }
3522 acl->len = acl_len;
3523out:
3524 nfs4_set_cached_acl(inode, acl);
3525}
3526
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003527static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003528{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003529 struct page *pages[NFS4ACL_MAXPAGES];
3530 struct nfs_getaclargs args = {
3531 .fh = NFS_FH(inode),
3532 .acl_pages = pages,
3533 .acl_len = buflen,
3534 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003535 struct nfs_getaclres res = {
3536 .acl_len = buflen,
3537 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003538 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003539 struct rpc_message msg = {
3540 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3541 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003542 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003543 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003544 struct page *localpage = NULL;
3545 int ret;
3546
3547 if (buflen < PAGE_SIZE) {
3548 /* As long as we're doing a round trip to the server anyway,
3549 * let's be prepared for a page of acl data. */
3550 localpage = alloc_page(GFP_KERNEL);
3551 resp_buf = page_address(localpage);
3552 if (localpage == NULL)
3553 return -ENOMEM;
3554 args.acl_pages[0] = localpage;
3555 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003556 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003557 } else {
3558 resp_buf = buf;
3559 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3560 }
Bryan Schumaker7c513052011-03-24 17:12:24 +00003561 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003562 if (ret)
3563 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003564 if (res.acl_len > args.acl_len)
3565 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003566 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003567 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003568 if (buf) {
3569 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003570 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003571 goto out_free;
3572 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003573 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003574 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003575 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003576out_free:
3577 if (localpage)
3578 __free_page(localpage);
3579 return ret;
3580}
3581
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003582static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3583{
3584 struct nfs4_exception exception = { };
3585 ssize_t ret;
3586 do {
3587 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3588 if (ret >= 0)
3589 break;
3590 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3591 } while (exception.retry);
3592 return ret;
3593}
3594
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003595static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3596{
3597 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003598 int ret;
3599
3600 if (!nfs4_server_supports_acls(server))
3601 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003602 ret = nfs_revalidate_inode(server, inode);
3603 if (ret < 0)
3604 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003605 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3606 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003607 ret = nfs4_read_cached_acl(inode, buf, buflen);
3608 if (ret != -ENOENT)
3609 return ret;
3610 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003611}
3612
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003613static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003614{
3615 struct nfs_server *server = NFS_SERVER(inode);
3616 struct page *pages[NFS4ACL_MAXPAGES];
3617 struct nfs_setaclargs arg = {
3618 .fh = NFS_FH(inode),
3619 .acl_pages = pages,
3620 .acl_len = buflen,
3621 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003622 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003623 struct rpc_message msg = {
3624 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3625 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003626 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003627 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003628 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003629
3630 if (!nfs4_server_supports_acls(server))
3631 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003632 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3633 if (i < 0)
3634 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003635 nfs_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003636 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003637
3638 /*
3639 * Free each page after tx, so the only ref left is
3640 * held by the network stack
3641 */
3642 for (; i > 0; i--)
3643 put_page(pages[i-1]);
3644
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003645 /*
3646 * Acl update can result in inode attribute update.
3647 * so mark the attribute cache invalid.
3648 */
3649 spin_lock(&inode->i_lock);
3650 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3651 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003652 nfs_access_zap_cache(inode);
3653 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003654 return ret;
3655}
3656
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003657static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3658{
3659 struct nfs4_exception exception = { };
3660 int err;
3661 do {
3662 err = nfs4_handle_exception(NFS_SERVER(inode),
3663 __nfs4_proc_set_acl(inode, buf, buflen),
3664 &exception);
3665 } while (exception.retry);
3666 return err;
3667}
3668
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003670nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003672 struct nfs_client *clp = server->nfs_client;
3673
3674 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 return 0;
3676 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003677 case -NFS4ERR_ADMIN_REVOKED:
3678 case -NFS4ERR_BAD_STATEID:
3679 case -NFS4ERR_OPENMODE:
3680 if (state == NULL)
3681 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003682 nfs4_schedule_stateid_recovery(server, state);
3683 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003684 case -NFS4ERR_EXPIRED:
3685 if (state != NULL)
3686 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003688 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003689 nfs4_schedule_lease_recovery(clp);
3690 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003691#if defined(CONFIG_NFS_V4_1)
3692 case -NFS4ERR_BADSESSION:
3693 case -NFS4ERR_BADSLOT:
3694 case -NFS4ERR_BAD_HIGH_SLOT:
3695 case -NFS4ERR_DEADSESSION:
3696 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3697 case -NFS4ERR_SEQ_FALSE_RETRY:
3698 case -NFS4ERR_SEQ_MISORDERED:
3699 dprintk("%s ERROR %d, Reset session\n", __func__,
3700 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003701 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -04003702 task->tk_status = 0;
3703 return -EAGAIN;
3704#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003706 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003707 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003708 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3710 task->tk_status = 0;
3711 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003712 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 case -NFS4ERR_OLD_STATEID:
3714 task->tk_status = 0;
3715 return -EAGAIN;
3716 }
3717 task->tk_status = nfs4_map_errors(task->tk_status);
3718 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003719wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003720 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003721 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3722 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3723 task->tk_status = 0;
3724 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725}
3726
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003727int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3728 unsigned short port, struct rpc_cred *cred,
3729 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730{
3731 nfs4_verifier sc_verifier;
3732 struct nfs4_setclientid setclientid = {
3733 .sc_verifier = &sc_verifier,
3734 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003735 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 };
3737 struct rpc_message msg = {
3738 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3739 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003740 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003741 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 };
Al Virobc4785c2006-10-19 23:28:51 -07003743 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 int loop = 0;
3745 int status;
3746
Al Virobc4785c2006-10-19 23:28:51 -07003747 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3749 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3750
3751 for(;;) {
3752 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003753 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003754 clp->cl_ipaddr,
3755 rpc_peeraddr2str(clp->cl_rpcclient,
3756 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003757 rpc_peeraddr2str(clp->cl_rpcclient,
3758 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003759 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 clp->cl_id_uniquifier);
3761 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003762 sizeof(setclientid.sc_netid),
3763 rpc_peeraddr2str(clp->cl_rpcclient,
3764 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003766 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 clp->cl_ipaddr, port >> 8, port & 255);
3768
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003769 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 if (status != -NFS4ERR_CLID_INUSE)
3771 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003772 if (loop != 0) {
3773 ++clp->cl_id_uniquifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003775 }
3776 ++loop;
3777 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 }
3779 return status;
3780}
3781
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003782int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003783 struct nfs4_setclientid_res *arg,
3784 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785{
3786 struct nfs_fsinfo fsinfo;
3787 struct rpc_message msg = {
3788 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003789 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003791 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 };
3793 unsigned long now;
3794 int status;
3795
3796 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003797 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 if (status == 0) {
3799 spin_lock(&clp->cl_lock);
3800 clp->cl_lease_time = fsinfo.lease_time * HZ;
3801 clp->cl_last_renewal = now;
3802 spin_unlock(&clp->cl_lock);
3803 }
3804 return status;
3805}
3806
Trond Myklebustfe650402006-01-03 09:55:18 +01003807struct nfs4_delegreturndata {
3808 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003809 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003810 struct nfs_fh fh;
3811 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003812 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003813 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003814 int rpc_status;
3815};
3816
Trond Myklebustfe650402006-01-03 09:55:18 +01003817static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3818{
3819 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003820
Trond Myklebust14516c32010-07-31 14:29:06 -04003821 if (!nfs4_sequence_done(task, &data->res.seq_res))
3822 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003823
Ricardo Labiaga79708862009-12-07 09:23:21 -05003824 switch (task->tk_status) {
3825 case -NFS4ERR_STALE_STATEID:
3826 case -NFS4ERR_EXPIRED:
3827 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003828 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003829 break;
3830 default:
3831 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3832 -EAGAIN) {
3833 nfs_restart_rpc(task, data->res.server->nfs_client);
3834 return;
3835 }
3836 }
3837 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003838}
3839
3840static void nfs4_delegreturn_release(void *calldata)
3841{
Trond Myklebustfe650402006-01-03 09:55:18 +01003842 kfree(calldata);
3843}
3844
Andy Adamson938e1012009-04-01 09:22:28 -04003845#if defined(CONFIG_NFS_V4_1)
3846static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3847{
3848 struct nfs4_delegreturndata *d_data;
3849
3850 d_data = (struct nfs4_delegreturndata *)data;
3851
Trond Myklebust035168a2010-06-16 09:52:26 -04003852 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003853 &d_data->args.seq_args,
3854 &d_data->res.seq_res, 1, task))
3855 return;
3856 rpc_call_start(task);
3857}
3858#endif /* CONFIG_NFS_V4_1 */
3859
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003860static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003861#if defined(CONFIG_NFS_V4_1)
3862 .rpc_call_prepare = nfs4_delegreturn_prepare,
3863#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003864 .rpc_call_done = nfs4_delegreturn_done,
3865 .rpc_release = nfs4_delegreturn_release,
3866};
3867
Trond Myklebuste6f81072008-01-24 18:14:34 -05003868static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003869{
3870 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003871 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003872 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003873 struct rpc_message msg = {
3874 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3875 .rpc_cred = cred,
3876 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003877 struct rpc_task_setup task_setup_data = {
3878 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003879 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003880 .callback_ops = &nfs4_delegreturn_ops,
3881 .flags = RPC_TASK_ASYNC,
3882 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003883 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003884
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003885 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003886 if (data == NULL)
3887 return -ENOMEM;
3888 data->args.fhandle = &data->fh;
3889 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003890 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003891 nfs_copy_fh(&data->fh, NFS_FH(inode));
3892 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003893 data->res.fattr = &data->fattr;
3894 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003895 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003896 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003897 data->rpc_status = 0;
3898
Trond Myklebustc970aa82007-07-14 15:39:59 -04003899 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05003900 msg.rpc_argp = &data->args;
3901 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003902 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003903 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003904 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003905 if (!issync)
3906 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003907 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003908 if (status != 0)
3909 goto out;
3910 status = data->rpc_status;
3911 if (status != 0)
3912 goto out;
3913 nfs_refresh_inode(inode, &data->fattr);
3914out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003915 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003916 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917}
3918
Trond Myklebuste6f81072008-01-24 18:14:34 -05003919int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920{
3921 struct nfs_server *server = NFS_SERVER(inode);
3922 struct nfs4_exception exception = { };
3923 int err;
3924 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003925 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 switch (err) {
3927 case -NFS4ERR_STALE_STATEID:
3928 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 case 0:
3930 return 0;
3931 }
3932 err = nfs4_handle_exception(server, err, &exception);
3933 } while (exception.retry);
3934 return err;
3935}
3936
3937#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3938#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3939
3940/*
3941 * sleep, with exponential backoff, and retry the LOCK operation.
3942 */
3943static unsigned long
3944nfs4_set_lock_task_retry(unsigned long timeout)
3945{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003946 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 timeout <<= 1;
3948 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3949 return NFS4_LOCK_MAXTIMEOUT;
3950 return timeout;
3951}
3952
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3954{
3955 struct inode *inode = state->inode;
3956 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003957 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003958 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003960 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003962 struct nfs_lockt_res res = {
3963 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 };
3965 struct rpc_message msg = {
3966 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3967 .rpc_argp = &arg,
3968 .rpc_resp = &res,
3969 .rpc_cred = state->owner->so_cred,
3970 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 struct nfs4_lock_state *lsp;
3972 int status;
3973
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003974 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003975 status = nfs4_set_lock_state(state, request);
3976 if (status != 0)
3977 goto out;
3978 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003979 arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05003980 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003981 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003982 switch (status) {
3983 case 0:
3984 request->fl_type = F_UNLCK;
3985 break;
3986 case -NFS4ERR_DENIED:
3987 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003989 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003990out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 return status;
3992}
3993
3994static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3995{
3996 struct nfs4_exception exception = { };
3997 int err;
3998
3999 do {
4000 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4001 _nfs4_proc_getlk(state, cmd, request),
4002 &exception);
4003 } while (exception.retry);
4004 return err;
4005}
4006
4007static int do_vfs_lock(struct file *file, struct file_lock *fl)
4008{
4009 int res = 0;
4010 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4011 case FL_POSIX:
4012 res = posix_lock_file_wait(file, fl);
4013 break;
4014 case FL_FLOCK:
4015 res = flock_lock_file_wait(file, fl);
4016 break;
4017 default:
4018 BUG();
4019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 return res;
4021}
4022
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004023struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004024 struct nfs_locku_args arg;
4025 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004026 struct nfs4_lock_state *lsp;
4027 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004028 struct file_lock fl;
4029 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004030 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004031};
4032
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004033static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4034 struct nfs_open_context *ctx,
4035 struct nfs4_lock_state *lsp,
4036 struct nfs_seqid *seqid)
4037{
4038 struct nfs4_unlockdata *p;
4039 struct inode *inode = lsp->ls_state->inode;
4040
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004041 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004042 if (p == NULL)
4043 return NULL;
4044 p->arg.fh = NFS_FH(inode);
4045 p->arg.fl = &p->fl;
4046 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004047 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004048 p->arg.stateid = &lsp->ls_stateid;
4049 p->lsp = lsp;
4050 atomic_inc(&lsp->ls_count);
4051 /* Ensure we don't close file until we're done freeing locks! */
4052 p->ctx = get_nfs_open_context(ctx);
4053 memcpy(&p->fl, fl, sizeof(p->fl));
4054 p->server = NFS_SERVER(inode);
4055 return p;
4056}
4057
Trond Myklebust06f814a2006-01-03 09:55:07 +01004058static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004059{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004060 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004061 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004062 nfs4_put_lock_state(calldata->lsp);
4063 put_nfs_open_context(calldata->ctx);
4064 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004065}
4066
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004067static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004068{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004069 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004070
Trond Myklebust14516c32010-07-31 14:29:06 -04004071 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4072 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004073 switch (task->tk_status) {
4074 case 0:
4075 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004076 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004077 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01004078 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004079 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004080 case -NFS4ERR_BAD_STATEID:
4081 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004082 case -NFS4ERR_STALE_STATEID:
4083 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004084 break;
4085 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004086 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebust0110ee12009-12-07 09:00:24 -05004087 nfs_restart_rpc(task,
Trond Myklebustd61e6122009-12-05 19:32:19 -05004088 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004089 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004090}
4091
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004092static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004093{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004094 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004096 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004097 return;
4098 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004099 /* Note: exit _without_ running nfs4_locku_done */
4100 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004101 return;
4102 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004103 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004104 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004105 &calldata->arg.seq_args,
4106 &calldata->res.seq_res, 1, task))
4107 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004108 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109}
4110
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004111static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004112 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004113 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004114 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004115};
4116
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004117static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4118 struct nfs_open_context *ctx,
4119 struct nfs4_lock_state *lsp,
4120 struct nfs_seqid *seqid)
4121{
4122 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004123 struct rpc_message msg = {
4124 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4125 .rpc_cred = ctx->cred,
4126 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004127 struct rpc_task_setup task_setup_data = {
4128 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004129 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004130 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004131 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004132 .flags = RPC_TASK_ASYNC,
4133 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004134
Frank Filz137d6ac2007-07-09 15:32:29 -07004135 /* Ensure this is an unlock - when canceling a lock, the
4136 * canceled lock is passed in, and it won't be an unlock.
4137 */
4138 fl->fl_type = F_UNLCK;
4139
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004140 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4141 if (data == NULL) {
4142 nfs_free_seqid(seqid);
4143 return ERR_PTR(-ENOMEM);
4144 }
4145
Trond Myklebust1174dd12010-12-21 10:52:24 -05004146 msg.rpc_argp = &data->arg;
4147 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004148 task_setup_data.callback_data = data;
4149 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004150}
4151
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4153{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004154 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004155 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004156 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004157 struct rpc_task *task;
4158 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004159 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160
Trond Myklebust9b073572006-06-29 16:38:34 -04004161 status = nfs4_set_lock_state(state, request);
4162 /* Unlock _before_ we do the RPC call */
4163 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004164 down_read(&nfsi->rwsem);
4165 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4166 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004167 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004168 }
4169 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004170 if (status != 0)
4171 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004172 /* Is this a delegated lock? */
4173 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004174 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004175 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004176 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004177 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004178 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004179 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004180 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004181 status = PTR_ERR(task);
4182 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004183 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004184 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004185 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004186out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004187 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004188 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189}
4190
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004191struct nfs4_lockdata {
4192 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004193 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004194 struct nfs4_lock_state *lsp;
4195 struct nfs_open_context *ctx;
4196 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004197 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004198 int rpc_status;
4199 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004200 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004201};
4202
4203static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004204 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4205 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004206{
4207 struct nfs4_lockdata *p;
4208 struct inode *inode = lsp->ls_state->inode;
4209 struct nfs_server *server = NFS_SERVER(inode);
4210
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004211 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004212 if (p == NULL)
4213 return NULL;
4214
4215 p->arg.fh = NFS_FH(inode);
4216 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004217 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004218 if (p->arg.open_seqid == NULL)
4219 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004220 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004221 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004222 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004223 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004224 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004225 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004226 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004227 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004228 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004229 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004230 atomic_inc(&lsp->ls_count);
4231 p->ctx = get_nfs_open_context(ctx);
4232 memcpy(&p->fl, fl, sizeof(p->fl));
4233 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004234out_free_seqid:
4235 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004236out_free:
4237 kfree(p);
4238 return NULL;
4239}
4240
4241static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4242{
4243 struct nfs4_lockdata *data = calldata;
4244 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245
Harvey Harrison3110ff82008-05-02 13:42:44 -07004246 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004247 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4248 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004249 /* Do we need to do an open_to_lock_owner? */
4250 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004251 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4252 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004253 data->arg.open_stateid = &state->stateid;
4254 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004255 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004256 } else
4257 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004258 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004259 if (nfs4_setup_sequence(data->server,
4260 &data->arg.seq_args,
Andy Adamson66179ef2009-04-01 09:22:22 -04004261 &data->res.seq_res, 1, task))
4262 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004263 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004264 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004265}
4266
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004267static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4268{
4269 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4270 nfs4_lock_prepare(task, calldata);
4271}
4272
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004273static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4274{
4275 struct nfs4_lockdata *data = calldata;
4276
Harvey Harrison3110ff82008-05-02 13:42:44 -07004277 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004278
Trond Myklebust14516c32010-07-31 14:29:06 -04004279 if (!nfs4_sequence_done(task, &data->res.seq_res))
4280 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004281
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004282 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004283 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004284 if (data->rpc_status == 0)
4285 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4286 else
4287 goto out;
4288 }
4289 if (data->rpc_status == 0) {
4290 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4291 sizeof(data->lsp->ls_stateid.data));
4292 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Al Viro3d4ff432011-06-22 18:40:12 -04004293 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004294 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004295out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004296 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004297}
4298
4299static void nfs4_lock_release(void *calldata)
4300{
4301 struct nfs4_lockdata *data = calldata;
4302
Harvey Harrison3110ff82008-05-02 13:42:44 -07004303 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004304 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004305 if (data->cancelled != 0) {
4306 struct rpc_task *task;
4307 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4308 data->arg.lock_seqid);
4309 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004310 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004311 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004312 } else
4313 nfs_free_seqid(data->arg.lock_seqid);
4314 nfs4_put_lock_state(data->lsp);
4315 put_nfs_open_context(data->ctx);
4316 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004317 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004318}
4319
4320static const struct rpc_call_ops nfs4_lock_ops = {
4321 .rpc_call_prepare = nfs4_lock_prepare,
4322 .rpc_call_done = nfs4_lock_done,
4323 .rpc_release = nfs4_lock_release,
4324};
4325
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004326static const struct rpc_call_ops nfs4_recover_lock_ops = {
4327 .rpc_call_prepare = nfs4_recover_lock_prepare,
4328 .rpc_call_done = nfs4_lock_done,
4329 .rpc_release = nfs4_lock_release,
4330};
4331
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004332static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4333{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004334 switch (error) {
4335 case -NFS4ERR_ADMIN_REVOKED:
4336 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004337 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004338 if (new_lock_owner != 0 ||
4339 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004340 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004341 break;
4342 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004343 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004344 case -NFS4ERR_EXPIRED:
4345 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004346 };
4347}
4348
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004349static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004350{
4351 struct nfs4_lockdata *data;
4352 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004353 struct rpc_message msg = {
4354 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4355 .rpc_cred = state->owner->so_cred,
4356 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004357 struct rpc_task_setup task_setup_data = {
4358 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004359 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004360 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004361 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004362 .flags = RPC_TASK_ASYNC,
4363 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004364 int ret;
4365
Harvey Harrison3110ff82008-05-02 13:42:44 -07004366 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004367 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004368 fl->fl_u.nfs4_fl.owner,
4369 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004370 if (data == NULL)
4371 return -ENOMEM;
4372 if (IS_SETLKW(cmd))
4373 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004374 if (recovery_type > NFS_LOCK_NEW) {
4375 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004376 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004377 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4378 }
Trond Myklebust1174dd12010-12-21 10:52:24 -05004379 msg.rpc_argp = &data->arg;
4380 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004381 task_setup_data.callback_data = data;
4382 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004383 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004384 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004385 ret = nfs4_wait_for_completion_rpc_task(task);
4386 if (ret == 0) {
4387 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004388 if (ret)
4389 nfs4_handle_setlk_error(data->server, data->lsp,
4390 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004391 } else
4392 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004393 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004394 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004395 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396}
4397
4398static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4399{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004400 struct nfs_server *server = NFS_SERVER(state->inode);
4401 struct nfs4_exception exception = { };
4402 int err;
4403
4404 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004405 /* Cache the lock if possible... */
4406 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4407 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004408 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004409 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004410 break;
4411 nfs4_handle_exception(server, err, &exception);
4412 } while (exception.retry);
4413 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414}
4415
4416static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4417{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004418 struct nfs_server *server = NFS_SERVER(state->inode);
4419 struct nfs4_exception exception = { };
4420 int err;
4421
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004422 err = nfs4_set_lock_state(state, request);
4423 if (err != 0)
4424 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004425 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004426 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4427 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004428 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004429 switch (err) {
4430 default:
4431 goto out;
4432 case -NFS4ERR_GRACE:
4433 case -NFS4ERR_DELAY:
4434 nfs4_handle_exception(server, err, &exception);
4435 err = 0;
4436 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004437 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004438out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004439 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440}
4441
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004442#if defined(CONFIG_NFS_V4_1)
4443static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4444{
4445 int status;
4446 struct nfs_server *server = NFS_SERVER(state->inode);
4447
4448 status = nfs41_test_stateid(server, state);
4449 if (status == NFS_OK)
4450 return 0;
4451 nfs41_free_stateid(server, state);
4452 return nfs4_lock_expired(state, request);
4453}
4454#endif
4455
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4457{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004458 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004459 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004460 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004462 if ((fl_flags & FL_POSIX) &&
4463 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4464 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004465 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004466 status = nfs4_set_lock_state(state, request);
4467 if (status != 0)
4468 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004469 request->fl_flags |= FL_ACCESS;
4470 status = do_vfs_lock(request->fl_file, request);
4471 if (status < 0)
4472 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004473 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004474 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004475 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004476 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004477 request->fl_flags = fl_flags & ~FL_SLEEP;
4478 status = do_vfs_lock(request->fl_file, request);
4479 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004480 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004481 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004482 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004483 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004484 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004485 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004486 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004487 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004488out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004489 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004490out:
4491 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 return status;
4493}
4494
4495static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4496{
4497 struct nfs4_exception exception = { };
4498 int err;
4499
4500 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004501 err = _nfs4_proc_setlk(state, cmd, request);
4502 if (err == -NFS4ERR_DENIED)
4503 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004505 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 } while (exception.retry);
4507 return err;
4508}
4509
4510static int
4511nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4512{
4513 struct nfs_open_context *ctx;
4514 struct nfs4_state *state;
4515 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4516 int status;
4517
4518 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004519 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 state = ctx->state;
4521
4522 if (request->fl_start < 0 || request->fl_end < 0)
4523 return -EINVAL;
4524
Trond Myklebustd9531262009-07-21 19:22:38 -04004525 if (IS_GETLK(cmd)) {
4526 if (state != NULL)
4527 return nfs4_proc_getlk(state, F_GETLK, request);
4528 return 0;
4529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530
4531 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4532 return -EINVAL;
4533
Trond Myklebustd9531262009-07-21 19:22:38 -04004534 if (request->fl_type == F_UNLCK) {
4535 if (state != NULL)
4536 return nfs4_proc_unlck(state, cmd, request);
4537 return 0;
4538 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539
Trond Myklebustd9531262009-07-21 19:22:38 -04004540 if (state == NULL)
4541 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 do {
4543 status = nfs4_proc_setlk(state, cmd, request);
4544 if ((status != -EAGAIN) || IS_SETLK(cmd))
4545 break;
4546 timeout = nfs4_set_lock_task_retry(timeout);
4547 status = -ERESTARTSYS;
4548 if (signalled())
4549 break;
4550 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 return status;
4552}
4553
Trond Myklebust888e6942005-11-04 15:38:11 -05004554int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4555{
4556 struct nfs_server *server = NFS_SERVER(state->inode);
4557 struct nfs4_exception exception = { };
4558 int err;
4559
4560 err = nfs4_set_lock_state(state, fl);
4561 if (err != 0)
4562 goto out;
4563 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004564 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004565 switch (err) {
4566 default:
4567 printk(KERN_ERR "%s: unhandled error %d.\n",
4568 __func__, err);
4569 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004570 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004571 goto out;
4572 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004573 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004574 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004575 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004576 nfs4_schedule_lease_recovery(server->nfs_client);
4577 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004578 case -NFS4ERR_BADSESSION:
4579 case -NFS4ERR_BADSLOT:
4580 case -NFS4ERR_BAD_HIGH_SLOT:
4581 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4582 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004583 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004584 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004585 case -ERESTARTSYS:
4586 /*
4587 * The show must go on: exit, but mark the
4588 * stateid as needing recovery.
4589 */
4590 case -NFS4ERR_ADMIN_REVOKED:
4591 case -NFS4ERR_BAD_STATEID:
4592 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004593 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004594 err = 0;
4595 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004596 case -EKEYEXPIRED:
4597 /*
4598 * User RPCSEC_GSS context has expired.
4599 * We cannot recover this stateid now, so
4600 * skip it and allow recovery thread to
4601 * proceed.
4602 */
4603 err = 0;
4604 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004605 case -ENOMEM:
4606 case -NFS4ERR_DENIED:
4607 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4608 err = 0;
4609 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004610 case -NFS4ERR_DELAY:
4611 break;
4612 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004613 err = nfs4_handle_exception(server, err, &exception);
4614 } while (exception.retry);
4615out:
4616 return err;
4617}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004618
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004619static void nfs4_release_lockowner_release(void *calldata)
4620{
4621 kfree(calldata);
4622}
4623
4624const struct rpc_call_ops nfs4_release_lockowner_ops = {
4625 .rpc_release = nfs4_release_lockowner_release,
4626};
4627
4628void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4629{
4630 struct nfs_server *server = lsp->ls_state->owner->so_server;
4631 struct nfs_release_lockowner_args *args;
4632 struct rpc_message msg = {
4633 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4634 };
4635
4636 if (server->nfs_client->cl_mvops->minor_version != 0)
4637 return;
4638 args = kmalloc(sizeof(*args), GFP_NOFS);
4639 if (!args)
4640 return;
4641 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4642 args->lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004643 args->lock_owner.s_dev = server->s_dev;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004644 msg.rpc_argp = args;
4645 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4646}
4647
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004648#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4649
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004650static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4651 const void *buf, size_t buflen,
4652 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004653{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004654 if (strcmp(key, "") != 0)
4655 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004656
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004657 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004658}
4659
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004660static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4661 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004662{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004663 if (strcmp(key, "") != 0)
4664 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004665
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004666 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004667}
4668
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004669static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4670 size_t list_len, const char *name,
4671 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004672{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004673 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004674
J. Bruce Fields096455a2006-03-20 23:23:42 -05004675 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4676 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004677
4678 if (list && len <= list_len)
4679 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004680 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004681}
4682
Andy Adamson533eb462011-06-13 18:25:56 -04004683/*
4684 * nfs_fhget will use either the mounted_on_fileid or the fileid
4685 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004686static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4687{
Andy Adamson533eb462011-06-13 18:25:56 -04004688 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4689 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4690 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4691 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004692 return;
4693
4694 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4695 NFS_ATTR_FATTR_NLINK;
4696 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4697 fattr->nlink = 2;
4698}
4699
Trond Myklebust56659e92007-07-17 21:52:39 -04004700int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004701 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004702{
4703 struct nfs_server *server = NFS_SERVER(dir);
4704 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004705 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004706 };
4707 struct nfs4_fs_locations_arg args = {
4708 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004709 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004710 .page = page,
4711 .bitmask = bitmask,
4712 };
Benny Halevy22958462009-04-01 09:22:02 -04004713 struct nfs4_fs_locations_res res = {
4714 .fs_locations = fs_locations,
4715 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004716 struct rpc_message msg = {
4717 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4718 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004719 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004720 };
4721 int status;
4722
Harvey Harrison3110ff82008-05-02 13:42:44 -07004723 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04004724
4725 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
4726 * is not supported */
4727 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
4728 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
4729 else
4730 bitmask[0] |= FATTR4_WORD0_FILEID;
4731
Trond Myklebustc228fd32007-01-13 02:28:11 -05004732 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004733 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004734 fs_locations->nlocations = 0;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004735 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004736 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004737 return status;
4738}
4739
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00004740static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4741{
4742 int status;
4743 struct nfs4_secinfo_arg args = {
4744 .dir_fh = NFS_FH(dir),
4745 .name = name,
4746 };
4747 struct nfs4_secinfo_res res = {
4748 .flavors = flavors,
4749 };
4750 struct rpc_message msg = {
4751 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
4752 .rpc_argp = &args,
4753 .rpc_resp = &res,
4754 };
4755
4756 dprintk("NFS call secinfo %s\n", name->name);
4757 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4758 dprintk("NFS reply secinfo: %d\n", status);
4759 return status;
4760}
4761
4762int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4763{
4764 struct nfs4_exception exception = { };
4765 int err;
4766 do {
4767 err = nfs4_handle_exception(NFS_SERVER(dir),
4768 _nfs4_proc_secinfo(dir, name, flavors),
4769 &exception);
4770 } while (exception.retry);
4771 return err;
4772}
4773
Andy Adamson557134a2009-04-01 09:21:53 -04004774#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004775/*
Andy Adamson357f54d2010-12-14 10:11:57 -05004776 * Check the exchange flags returned by the server for invalid flags, having
4777 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4778 * DS flags set.
4779 */
4780static int nfs4_check_cl_exchange_flags(u32 flags)
4781{
4782 if (flags & ~EXCHGID4_FLAG_MASK_R)
4783 goto out_inval;
4784 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4785 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4786 goto out_inval;
4787 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4788 goto out_inval;
4789 return NFS_OK;
4790out_inval:
4791 return -NFS4ERR_INVAL;
4792}
4793
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004794static bool
4795nfs41_same_server_scope(struct server_scope *a, struct server_scope *b)
4796{
4797 if (a->server_scope_sz == b->server_scope_sz &&
4798 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
4799 return true;
4800
4801 return false;
4802}
4803
Andy Adamson357f54d2010-12-14 10:11:57 -05004804/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04004805 * nfs4_proc_exchange_id()
4806 *
4807 * Since the clientid has expired, all compounds using sessions
4808 * associated with the stale clientid will be returning
4809 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4810 * be in some phase of session reset.
4811 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004812int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004813{
4814 nfs4_verifier verifier;
4815 struct nfs41_exchange_id_args args = {
4816 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05004817 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04004818 };
4819 struct nfs41_exchange_id_res res = {
4820 .client = clp,
4821 };
4822 int status;
4823 struct rpc_message msg = {
4824 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4825 .rpc_argp = &args,
4826 .rpc_resp = &res,
4827 .rpc_cred = cred,
4828 };
4829 __be32 *p;
4830
4831 dprintk("--> %s\n", __func__);
4832 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004833
Benny Halevy99fe60d2009-04-01 09:22:29 -04004834 p = (u32 *)verifier.data;
4835 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4836 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4837 args.verifier = &verifier;
4838
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004839 args.id_len = scnprintf(args.id, sizeof(args.id),
4840 "%s/%s.%s/%u",
4841 clp->cl_ipaddr,
4842 init_utsname()->nodename,
4843 init_utsname()->domainname,
4844 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004845
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004846 res.server_scope = kzalloc(sizeof(struct server_scope), GFP_KERNEL);
4847 if (unlikely(!res.server_scope))
4848 return -ENOMEM;
4849
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004850 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004851 if (!status)
4852 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004853
4854 if (!status) {
4855 if (clp->server_scope &&
4856 !nfs41_same_server_scope(clp->server_scope,
4857 res.server_scope)) {
4858 dprintk("%s: server_scope mismatch detected\n",
4859 __func__);
4860 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
4861 kfree(clp->server_scope);
4862 clp->server_scope = NULL;
4863 }
4864
4865 if (!clp->server_scope)
4866 clp->server_scope = res.server_scope;
4867 else
4868 kfree(res.server_scope);
4869 }
4870
Benny Halevy99fe60d2009-04-01 09:22:29 -04004871 dprintk("<-- %s status= %d\n", __func__, status);
4872 return status;
4873}
4874
Andy Adamson2050f0c2009-04-01 09:22:30 -04004875struct nfs4_get_lease_time_data {
4876 struct nfs4_get_lease_time_args *args;
4877 struct nfs4_get_lease_time_res *res;
4878 struct nfs_client *clp;
4879};
4880
4881static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4882 void *calldata)
4883{
4884 int ret;
4885 struct nfs4_get_lease_time_data *data =
4886 (struct nfs4_get_lease_time_data *)calldata;
4887
4888 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004889 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004890 /* just setup sequence, do not trigger session recovery
4891 since we're invoked within one */
4892 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004893 &data->args->la_seq_args,
4894 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004895
4896 BUG_ON(ret == -EAGAIN);
4897 rpc_call_start(task);
4898 dprintk("<-- %s\n", __func__);
4899}
4900
4901/*
4902 * Called from nfs4_state_manager thread for session setup, so don't recover
4903 * from sequence operation or clientid errors.
4904 */
4905static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4906{
4907 struct nfs4_get_lease_time_data *data =
4908 (struct nfs4_get_lease_time_data *)calldata;
4909
4910 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04004911 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4912 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04004913 switch (task->tk_status) {
4914 case -NFS4ERR_DELAY:
4915 case -NFS4ERR_GRACE:
4916 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4917 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4918 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004919 /* fall through */
4920 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust0110ee12009-12-07 09:00:24 -05004921 nfs_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004922 return;
4923 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004924 dprintk("<-- %s\n", __func__);
4925}
4926
4927struct rpc_call_ops nfs4_get_lease_time_ops = {
4928 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4929 .rpc_call_done = nfs4_get_lease_time_done,
4930};
4931
4932int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4933{
4934 struct rpc_task *task;
4935 struct nfs4_get_lease_time_args args;
4936 struct nfs4_get_lease_time_res res = {
4937 .lr_fsinfo = fsinfo,
4938 };
4939 struct nfs4_get_lease_time_data data = {
4940 .args = &args,
4941 .res = &res,
4942 .clp = clp,
4943 };
4944 struct rpc_message msg = {
4945 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4946 .rpc_argp = &args,
4947 .rpc_resp = &res,
4948 };
4949 struct rpc_task_setup task_setup = {
4950 .rpc_client = clp->cl_rpcclient,
4951 .rpc_message = &msg,
4952 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004953 .callback_data = &data,
4954 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04004955 };
4956 int status;
4957
Andy Adamson2050f0c2009-04-01 09:22:30 -04004958 dprintk("--> %s\n", __func__);
4959 task = rpc_run_task(&task_setup);
4960
4961 if (IS_ERR(task))
4962 status = PTR_ERR(task);
4963 else {
4964 status = task->tk_status;
4965 rpc_put_task(task);
4966 }
4967 dprintk("<-- %s return %d\n", __func__, status);
4968
4969 return status;
4970}
4971
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004972/*
4973 * Reset a slot table
4974 */
Andy Adamson104aeba2010-01-14 17:45:10 -05004975static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4976 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004977{
Andy Adamson104aeba2010-01-14 17:45:10 -05004978 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004979 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004980 int ret = 0;
4981
Andy Adamson104aeba2010-01-14 17:45:10 -05004982 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4983 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004984
Andy Adamson104aeba2010-01-14 17:45:10 -05004985 /* Does the newly negotiated max_reqs match the existing slot table? */
4986 if (max_reqs != tbl->max_slots) {
4987 ret = -ENOMEM;
4988 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004989 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05004990 if (!new)
4991 goto out;
4992 ret = 0;
4993 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004994 }
4995 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05004996 if (new) {
4997 tbl->slots = new;
4998 tbl->max_slots = max_reqs;
4999 }
5000 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005001 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005002 spin_unlock(&tbl->slot_tbl_lock);
5003 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5004 tbl, tbl->slots, tbl->max_slots);
5005out:
5006 dprintk("<-- %s: return %d\n", __func__, ret);
5007 return ret;
5008}
5009
Andy Adamsonfc931582009-04-01 09:22:31 -04005010/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005011 * Reset the forechannel and backchannel slot tables
5012 */
5013static int nfs4_reset_slot_tables(struct nfs4_session *session)
5014{
5015 int status;
5016
5017 status = nfs4_reset_slot_table(&session->fc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05005018 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005019 if (status)
5020 return status;
5021
5022 status = nfs4_reset_slot_table(&session->bc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05005023 session->bc_attrs.max_reqs, 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005024 return status;
5025}
5026
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005027/* Destroy the slot table */
5028static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5029{
5030 if (session->fc_slot_table.slots != NULL) {
5031 kfree(session->fc_slot_table.slots);
5032 session->fc_slot_table.slots = NULL;
5033 }
5034 if (session->bc_slot_table.slots != NULL) {
5035 kfree(session->bc_slot_table.slots);
5036 session->bc_slot_table.slots = NULL;
5037 }
5038 return;
5039}
5040
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005041/*
Andy Adamsonfc931582009-04-01 09:22:31 -04005042 * Initialize slot table
5043 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005044static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
5045 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04005046{
Andy Adamsonfc931582009-04-01 09:22:31 -04005047 struct nfs4_slot *slot;
5048 int ret = -ENOMEM;
5049
5050 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
5051
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005052 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04005053
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005054 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04005055 if (!slot)
5056 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04005057 ret = 0;
5058
5059 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04005060 tbl->max_slots = max_slots;
5061 tbl->slots = slot;
5062 tbl->highest_used_slotid = -1; /* no slot is currently used */
5063 spin_unlock(&tbl->slot_tbl_lock);
5064 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5065 tbl, tbl->slots, tbl->max_slots);
5066out:
5067 dprintk("<-- %s: return %d\n", __func__, ret);
5068 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04005069}
5070
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005071/*
5072 * Initialize the forechannel and backchannel tables
5073 */
5074static int nfs4_init_slot_tables(struct nfs4_session *session)
5075{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005076 struct nfs4_slot_table *tbl;
5077 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005078
Trond Myklebustf26468f2009-12-05 19:32:11 -05005079 tbl = &session->fc_slot_table;
5080 if (tbl->slots == NULL) {
5081 status = nfs4_init_slot_table(tbl,
5082 session->fc_attrs.max_reqs, 1);
5083 if (status)
5084 return status;
5085 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005086
Trond Myklebustf26468f2009-12-05 19:32:11 -05005087 tbl = &session->bc_slot_table;
5088 if (tbl->slots == NULL) {
5089 status = nfs4_init_slot_table(tbl,
5090 session->bc_attrs.max_reqs, 0);
5091 if (status)
5092 nfs4_destroy_slot_tables(session);
5093 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005094
5095 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005096}
5097
5098struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5099{
5100 struct nfs4_session *session;
5101 struct nfs4_slot_table *tbl;
5102
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005103 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005104 if (!session)
5105 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005106
Andy Adamson557134a2009-04-01 09:21:53 -04005107 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05005108 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04005109 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005110 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005111 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005112
5113 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05005114 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005115 spin_lock_init(&tbl->slot_tbl_lock);
5116 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005117 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005118
Trond Myklebust1055d762010-06-16 09:52:27 -04005119 session->session_state = 1<<NFS4_SESSION_INITING;
5120
Andy Adamson557134a2009-04-01 09:21:53 -04005121 session->clp = clp;
5122 return session;
5123}
5124
5125void nfs4_destroy_session(struct nfs4_session *session)
5126{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005127 nfs4_proc_destroy_session(session);
5128 dprintk("%s Destroy backchannel for xprt %p\n",
5129 __func__, session->clp->cl_rpcclient->cl_xprt);
5130 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
5131 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005132 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005133 kfree(session);
5134}
5135
Andy Adamsonfc931582009-04-01 09:22:31 -04005136/*
5137 * Initialize the values to be used by the client in CREATE_SESSION
5138 * If nfs4_init_session set the fore channel request and response sizes,
5139 * use them.
5140 *
5141 * Set the back channel max_resp_sz_cached to zero to force the client to
5142 * always set csa_cachethis to FALSE because the current implementation
5143 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5144 */
5145static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5146{
5147 struct nfs4_session *session = args->client->cl_session;
5148 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5149 mxresp_sz = session->fc_attrs.max_resp_sz;
5150
5151 if (mxrqst_sz == 0)
5152 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5153 if (mxresp_sz == 0)
5154 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5155 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005156 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5157 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005158 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5159 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
5160
5161 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005162 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005163 __func__,
5164 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005165 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005166
5167 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005168 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5169 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5170 args->bc_attrs.max_resp_sz_cached = 0;
5171 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5172 args->bc_attrs.max_reqs = 1;
5173
5174 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5175 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5176 __func__,
5177 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5178 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5179 args->bc_attrs.max_reqs);
5180}
5181
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005182static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005183{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005184 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5185 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5186
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005187 if (rcvd->max_resp_sz > sent->max_resp_sz)
5188 return -EINVAL;
5189 /*
5190 * Our requested max_ops is the minimum we need; we're not
5191 * prepared to break up compounds into smaller pieces than that.
5192 * So, no point even trying to continue if the server won't
5193 * cooperate:
5194 */
5195 if (rcvd->max_ops < sent->max_ops)
5196 return -EINVAL;
5197 if (rcvd->max_reqs == 0)
5198 return -EINVAL;
5199 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005200}
5201
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005202static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5203{
5204 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5205 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005206
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005207 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5208 return -EINVAL;
5209 if (rcvd->max_resp_sz < sent->max_resp_sz)
5210 return -EINVAL;
5211 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5212 return -EINVAL;
5213 /* These would render the backchannel useless: */
5214 if (rcvd->max_ops == 0)
5215 return -EINVAL;
5216 if (rcvd->max_reqs == 0)
5217 return -EINVAL;
5218 return 0;
5219}
Andy Adamson8d353012009-04-01 09:22:32 -04005220
Andy Adamson8d353012009-04-01 09:22:32 -04005221static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5222 struct nfs4_session *session)
5223{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005224 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005225
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005226 ret = nfs4_verify_fore_channel_attrs(args, session);
5227 if (ret)
5228 return ret;
5229 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005230}
5231
Andy Adamsonfc931582009-04-01 09:22:31 -04005232static int _nfs4_proc_create_session(struct nfs_client *clp)
5233{
5234 struct nfs4_session *session = clp->cl_session;
5235 struct nfs41_create_session_args args = {
5236 .client = clp,
5237 .cb_program = NFS4_CALLBACK,
5238 };
5239 struct nfs41_create_session_res res = {
5240 .client = clp,
5241 };
5242 struct rpc_message msg = {
5243 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5244 .rpc_argp = &args,
5245 .rpc_resp = &res,
5246 };
5247 int status;
5248
5249 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005250 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005251
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005252 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005253
Andy Adamson8d353012009-04-01 09:22:32 -04005254 if (!status)
5255 /* Verify the session's negotiated channel_attrs values */
5256 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005257 if (!status) {
5258 /* Increment the clientid slot sequence id */
5259 clp->cl_seqid++;
5260 }
5261
5262 return status;
5263}
5264
5265/*
5266 * Issues a CREATE_SESSION operation to the server.
5267 * It is the responsibility of the caller to verify the session is
5268 * expired before calling this routine.
5269 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005270int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005271{
5272 int status;
5273 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005274 struct nfs4_session *session = clp->cl_session;
5275
5276 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5277
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005278 status = _nfs4_proc_create_session(clp);
Andy Adamsonfc931582009-04-01 09:22:31 -04005279 if (status)
5280 goto out;
5281
Trond Myklebustf26468f2009-12-05 19:32:11 -05005282 /* Init and reset the fore channel */
5283 status = nfs4_init_slot_tables(session);
5284 dprintk("slot table initialization returned %d\n", status);
5285 if (status)
5286 goto out;
5287 status = nfs4_reset_slot_tables(session);
5288 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005289 if (status)
5290 goto out;
5291
5292 ptr = (unsigned *)&session->sess_id.data[0];
5293 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5294 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005295out:
5296 dprintk("<-- %s\n", __func__);
5297 return status;
5298}
5299
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005300/*
5301 * Issue the over-the-wire RPC DESTROY_SESSION.
5302 * The caller must serialize access to this routine.
5303 */
5304int nfs4_proc_destroy_session(struct nfs4_session *session)
5305{
5306 int status = 0;
5307 struct rpc_message msg;
5308
5309 dprintk("--> nfs4_proc_destroy_session\n");
5310
5311 /* session is still being setup */
5312 if (session->clp->cl_cons_state != NFS_CS_READY)
5313 return status;
5314
5315 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5316 msg.rpc_argp = session;
5317 msg.rpc_resp = NULL;
5318 msg.rpc_cred = NULL;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005319 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005320
5321 if (status)
5322 printk(KERN_WARNING
5323 "Got error %d from the server on DESTROY_SESSION. "
5324 "Session has been destroyed regardless...\n", status);
5325
5326 dprintk("<-- nfs4_proc_destroy_session\n");
5327 return status;
5328}
5329
Trond Myklebustfccba802009-07-21 16:48:07 -04005330int nfs4_init_session(struct nfs_server *server)
5331{
5332 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005333 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005334 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005335 int ret;
5336
5337 if (!nfs4_has_session(clp))
5338 return 0;
5339
Trond Myklebust1055d762010-06-16 09:52:27 -04005340 session = clp->cl_session;
5341 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5342 return 0;
5343
Andy Adamson68bf05e2009-12-15 12:55:02 -05005344 rsize = server->rsize;
5345 if (rsize == 0)
5346 rsize = NFS_MAX_FILE_IO_SIZE;
5347 wsize = server->wsize;
5348 if (wsize == 0)
5349 wsize = NFS_MAX_FILE_IO_SIZE;
5350
Andy Adamson68bf05e2009-12-15 12:55:02 -05005351 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5352 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005353
Trond Myklebustfccba802009-07-21 16:48:07 -04005354 ret = nfs4_recover_expired_lease(server);
5355 if (!ret)
5356 ret = nfs4_check_client_ready(clp);
5357 return ret;
5358}
5359
Andy Adamsond83217c2011-03-01 01:34:17 +00005360int nfs4_init_ds_session(struct nfs_client *clp)
5361{
5362 struct nfs4_session *session = clp->cl_session;
5363 int ret;
5364
5365 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5366 return 0;
5367
5368 ret = nfs4_client_recover_expired_lease(clp);
5369 if (!ret)
5370 /* Test for the DS role */
5371 if (!is_ds_client(clp))
5372 ret = -ENODEV;
5373 if (!ret)
5374 ret = nfs4_check_client_ready(clp);
5375 return ret;
5376
5377}
5378EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5379
5380
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005381/*
5382 * Renew the cl_session lease.
5383 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005384struct nfs4_sequence_data {
5385 struct nfs_client *clp;
5386 struct nfs4_sequence_args args;
5387 struct nfs4_sequence_res res;
5388};
5389
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005390static void nfs41_sequence_release(void *data)
5391{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005392 struct nfs4_sequence_data *calldata = data;
5393 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005394
Alexandros Batsakis71358402010-02-05 03:45:05 -08005395 if (atomic_read(&clp->cl_count) > 1)
5396 nfs4_schedule_state_renewal(clp);
5397 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005398 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005399}
5400
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005401static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5402{
5403 switch(task->tk_status) {
5404 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005405 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5406 return -EAGAIN;
5407 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005408 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005409 }
5410 return 0;
5411}
5412
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005413static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005414{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005415 struct nfs4_sequence_data *calldata = data;
5416 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005417
Trond Myklebust14516c32010-07-31 14:29:06 -04005418 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5419 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005420
5421 if (task->tk_status < 0) {
5422 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005423 if (atomic_read(&clp->cl_count) == 1)
5424 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005425
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005426 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5427 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005428 return;
5429 }
5430 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005431 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005432out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005433 dprintk("<-- %s\n", __func__);
5434}
5435
5436static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5437{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005438 struct nfs4_sequence_data *calldata = data;
5439 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005440 struct nfs4_sequence_args *args;
5441 struct nfs4_sequence_res *res;
5442
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005443 args = task->tk_msg.rpc_argp;
5444 res = task->tk_msg.rpc_resp;
5445
Trond Myklebust035168a2010-06-16 09:52:26 -04005446 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005447 return;
5448 rpc_call_start(task);
5449}
5450
5451static const struct rpc_call_ops nfs41_sequence_ops = {
5452 .rpc_call_done = nfs41_sequence_call_done,
5453 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005454 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005455};
5456
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005457static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005458{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005459 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005460 struct rpc_message msg = {
5461 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5462 .rpc_cred = cred,
5463 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005464 struct rpc_task_setup task_setup_data = {
5465 .rpc_client = clp->cl_rpcclient,
5466 .rpc_message = &msg,
5467 .callback_ops = &nfs41_sequence_ops,
5468 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5469 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005470
Alexandros Batsakis71358402010-02-05 03:45:05 -08005471 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005472 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005473 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005474 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005475 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005476 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005477 }
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005478 msg.rpc_argp = &calldata->args;
5479 msg.rpc_resp = &calldata->res;
5480 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005481 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005482
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005483 return rpc_run_task(&task_setup_data);
5484}
5485
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005486static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005487{
5488 struct rpc_task *task;
5489 int ret = 0;
5490
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005491 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5492 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005493 task = _nfs41_proc_sequence(clp, cred);
5494 if (IS_ERR(task))
5495 ret = PTR_ERR(task);
5496 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005497 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005498 dprintk("<-- %s status=%d\n", __func__, ret);
5499 return ret;
5500}
5501
5502static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5503{
5504 struct rpc_task *task;
5505 int ret;
5506
5507 task = _nfs41_proc_sequence(clp, cred);
5508 if (IS_ERR(task)) {
5509 ret = PTR_ERR(task);
5510 goto out;
5511 }
5512 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005513 if (!ret) {
5514 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5515
5516 if (task->tk_status == 0)
5517 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005518 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005519 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005520 rpc_put_task(task);
5521out:
5522 dprintk("<-- %s status=%d\n", __func__, ret);
5523 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005524}
5525
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005526struct nfs4_reclaim_complete_data {
5527 struct nfs_client *clp;
5528 struct nfs41_reclaim_complete_args arg;
5529 struct nfs41_reclaim_complete_res res;
5530};
5531
5532static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5533{
5534 struct nfs4_reclaim_complete_data *calldata = data;
5535
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005536 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005537 if (nfs41_setup_sequence(calldata->clp->cl_session,
5538 &calldata->arg.seq_args,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005539 &calldata->res.seq_res, 0, task))
5540 return;
5541
5542 rpc_call_start(task);
5543}
5544
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005545static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5546{
5547 switch(task->tk_status) {
5548 case 0:
5549 case -NFS4ERR_COMPLETE_ALREADY:
5550 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5551 break;
5552 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005553 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005554 /* fall through */
5555 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005556 return -EAGAIN;
5557 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005558 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005559 }
5560 return 0;
5561}
5562
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005563static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5564{
5565 struct nfs4_reclaim_complete_data *calldata = data;
5566 struct nfs_client *clp = calldata->clp;
5567 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5568
5569 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005570 if (!nfs41_sequence_done(task, res))
5571 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005572
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005573 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5574 rpc_restart_call_prepare(task);
5575 return;
5576 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005577 dprintk("<-- %s\n", __func__);
5578}
5579
5580static void nfs4_free_reclaim_complete_data(void *data)
5581{
5582 struct nfs4_reclaim_complete_data *calldata = data;
5583
5584 kfree(calldata);
5585}
5586
5587static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5588 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5589 .rpc_call_done = nfs4_reclaim_complete_done,
5590 .rpc_release = nfs4_free_reclaim_complete_data,
5591};
5592
5593/*
5594 * Issue a global reclaim complete.
5595 */
5596static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5597{
5598 struct nfs4_reclaim_complete_data *calldata;
5599 struct rpc_task *task;
5600 struct rpc_message msg = {
5601 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5602 };
5603 struct rpc_task_setup task_setup_data = {
5604 .rpc_client = clp->cl_rpcclient,
5605 .rpc_message = &msg,
5606 .callback_ops = &nfs4_reclaim_complete_call_ops,
5607 .flags = RPC_TASK_ASYNC,
5608 };
5609 int status = -ENOMEM;
5610
5611 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005612 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005613 if (calldata == NULL)
5614 goto out;
5615 calldata->clp = clp;
5616 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005617
5618 msg.rpc_argp = &calldata->arg;
5619 msg.rpc_resp = &calldata->res;
5620 task_setup_data.callback_data = calldata;
5621 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005622 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005623 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005624 goto out;
5625 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005626 status = nfs4_wait_for_completion_rpc_task(task);
5627 if (status == 0)
5628 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005629 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005630 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005631out:
5632 dprintk("<-- %s status=%d\n", __func__, status);
5633 return status;
5634}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005635
5636static void
5637nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5638{
5639 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005640 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005641
5642 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005643 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5644 * right now covering the LAYOUTGET we are about to send.
5645 * However, that is not so catastrophic, and there seems
5646 * to be no way to prevent it completely.
5647 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005648 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5649 &lgp->res.seq_res, 0, task))
5650 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005651 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5652 NFS_I(lgp->args.inode)->layout,
5653 lgp->args.ctx->state)) {
5654 rpc_exit(task, NFS4_OK);
5655 return;
5656 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005657 rpc_call_start(task);
5658}
5659
5660static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5661{
5662 struct nfs4_layoutget *lgp = calldata;
5663 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5664
5665 dprintk("--> %s\n", __func__);
5666
5667 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5668 return;
5669
5670 switch (task->tk_status) {
5671 case 0:
5672 break;
5673 case -NFS4ERR_LAYOUTTRYLATER:
5674 case -NFS4ERR_RECALLCONFLICT:
5675 task->tk_status = -NFS4ERR_DELAY;
5676 /* Fall through */
5677 default:
5678 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5679 rpc_restart_call_prepare(task);
5680 return;
5681 }
5682 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005683 dprintk("<-- %s\n", __func__);
5684}
5685
5686static void nfs4_layoutget_release(void *calldata)
5687{
5688 struct nfs4_layoutget *lgp = calldata;
5689
5690 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005691 put_nfs_open_context(lgp->args.ctx);
5692 kfree(calldata);
5693 dprintk("<-- %s\n", __func__);
5694}
5695
5696static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5697 .rpc_call_prepare = nfs4_layoutget_prepare,
5698 .rpc_call_done = nfs4_layoutget_done,
5699 .rpc_release = nfs4_layoutget_release,
5700};
5701
5702int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5703{
5704 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5705 struct rpc_task *task;
5706 struct rpc_message msg = {
5707 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5708 .rpc_argp = &lgp->args,
5709 .rpc_resp = &lgp->res,
5710 };
5711 struct rpc_task_setup task_setup_data = {
5712 .rpc_client = server->client,
5713 .rpc_message = &msg,
5714 .callback_ops = &nfs4_layoutget_call_ops,
5715 .callback_data = lgp,
5716 .flags = RPC_TASK_ASYNC,
5717 };
5718 int status = 0;
5719
5720 dprintk("--> %s\n", __func__);
5721
Weston Andros Adamson35124a02011-03-24 16:48:21 -04005722 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005723 lgp->res.seq_res.sr_slot = NULL;
5724 task = rpc_run_task(&task_setup_data);
5725 if (IS_ERR(task))
5726 return PTR_ERR(task);
5727 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00005728 if (status == 0)
5729 status = task->tk_status;
5730 if (status == 0)
5731 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005732 rpc_put_task(task);
5733 dprintk("<-- %s status=%d\n", __func__, status);
5734 return status;
5735}
5736
Benny Halevycbe82602011-05-22 19:52:37 +03005737static void
5738nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
5739{
5740 struct nfs4_layoutreturn *lrp = calldata;
5741
5742 dprintk("--> %s\n", __func__);
5743 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
5744 &lrp->res.seq_res, 0, task))
5745 return;
5746 rpc_call_start(task);
5747}
5748
5749static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
5750{
5751 struct nfs4_layoutreturn *lrp = calldata;
5752 struct nfs_server *server;
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005753 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03005754
5755 dprintk("--> %s\n", __func__);
5756
5757 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
5758 return;
5759
5760 server = NFS_SERVER(lrp->args.inode);
5761 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5762 nfs_restart_rpc(task, lrp->clp);
5763 return;
5764 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005765 spin_lock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005766 if (task->tk_status == 0) {
Benny Halevycbe82602011-05-22 19:52:37 +03005767 if (lrp->res.lrs_present) {
Benny Halevycbe82602011-05-22 19:52:37 +03005768 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Benny Halevycbe82602011-05-22 19:52:37 +03005769 } else
5770 BUG_ON(!list_empty(&lo->plh_segs));
5771 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005772 lo->plh_block_lgets--;
5773 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005774 dprintk("<-- %s\n", __func__);
5775}
5776
5777static void nfs4_layoutreturn_release(void *calldata)
5778{
5779 struct nfs4_layoutreturn *lrp = calldata;
5780
5781 dprintk("--> %s\n", __func__);
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005782 put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03005783 kfree(calldata);
5784 dprintk("<-- %s\n", __func__);
5785}
5786
5787static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
5788 .rpc_call_prepare = nfs4_layoutreturn_prepare,
5789 .rpc_call_done = nfs4_layoutreturn_done,
5790 .rpc_release = nfs4_layoutreturn_release,
5791};
5792
5793int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
5794{
5795 struct rpc_task *task;
5796 struct rpc_message msg = {
5797 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
5798 .rpc_argp = &lrp->args,
5799 .rpc_resp = &lrp->res,
5800 };
5801 struct rpc_task_setup task_setup_data = {
5802 .rpc_client = lrp->clp->cl_rpcclient,
5803 .rpc_message = &msg,
5804 .callback_ops = &nfs4_layoutreturn_call_ops,
5805 .callback_data = lrp,
5806 };
5807 int status;
5808
5809 dprintk("--> %s\n", __func__);
5810 task = rpc_run_task(&task_setup_data);
5811 if (IS_ERR(task))
5812 return PTR_ERR(task);
5813 status = task->tk_status;
5814 dprintk("<-- %s status=%d\n", __func__, status);
5815 rpc_put_task(task);
5816 return status;
5817}
5818
Andy Adamson7f11d8d2011-07-30 20:52:35 -04005819/*
5820 * Retrieve the list of Data Server devices from the MDS.
5821 */
5822static int _nfs4_getdevicelist(struct nfs_server *server,
5823 const struct nfs_fh *fh,
5824 struct pnfs_devicelist *devlist)
5825{
5826 struct nfs4_getdevicelist_args args = {
5827 .fh = fh,
5828 .layoutclass = server->pnfs_curr_ld->id,
5829 };
5830 struct nfs4_getdevicelist_res res = {
5831 .devlist = devlist,
5832 };
5833 struct rpc_message msg = {
5834 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
5835 .rpc_argp = &args,
5836 .rpc_resp = &res,
5837 };
5838 int status;
5839
5840 dprintk("--> %s\n", __func__);
5841 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5842 &res.seq_res, 0);
5843 dprintk("<-- %s status=%d\n", __func__, status);
5844 return status;
5845}
5846
5847int nfs4_proc_getdevicelist(struct nfs_server *server,
5848 const struct nfs_fh *fh,
5849 struct pnfs_devicelist *devlist)
5850{
5851 struct nfs4_exception exception = { };
5852 int err;
5853
5854 do {
5855 err = nfs4_handle_exception(server,
5856 _nfs4_getdevicelist(server, fh, devlist),
5857 &exception);
5858 } while (exception.retry);
5859
5860 dprintk("%s: err=%d, num_devs=%u\n", __func__,
5861 err, devlist->num_devs);
5862
5863 return err;
5864}
5865EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
5866
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005867static int
5868_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5869{
5870 struct nfs4_getdeviceinfo_args args = {
5871 .pdev = pdev,
5872 };
5873 struct nfs4_getdeviceinfo_res res = {
5874 .pdev = pdev,
5875 };
5876 struct rpc_message msg = {
5877 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5878 .rpc_argp = &args,
5879 .rpc_resp = &res,
5880 };
5881 int status;
5882
5883 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005884 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005885 dprintk("<-- %s status=%d\n", __func__, status);
5886
5887 return status;
5888}
5889
5890int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5891{
5892 struct nfs4_exception exception = { };
5893 int err;
5894
5895 do {
5896 err = nfs4_handle_exception(server,
5897 _nfs4_proc_getdeviceinfo(server, pdev),
5898 &exception);
5899 } while (exception.retry);
5900 return err;
5901}
5902EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5903
Andy Adamson863a3c62011-03-23 13:27:54 +00005904static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
5905{
5906 struct nfs4_layoutcommit_data *data = calldata;
5907 struct nfs_server *server = NFS_SERVER(data->args.inode);
5908
5909 if (nfs4_setup_sequence(server, &data->args.seq_args,
5910 &data->res.seq_res, 1, task))
5911 return;
5912 rpc_call_start(task);
5913}
5914
5915static void
5916nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
5917{
5918 struct nfs4_layoutcommit_data *data = calldata;
5919 struct nfs_server *server = NFS_SERVER(data->args.inode);
5920
5921 if (!nfs4_sequence_done(task, &data->res.seq_res))
5922 return;
5923
5924 switch (task->tk_status) { /* Just ignore these failures */
5925 case NFS4ERR_DELEG_REVOKED: /* layout was recalled */
5926 case NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
5927 case NFS4ERR_BADLAYOUT: /* no layout */
5928 case NFS4ERR_GRACE: /* loca_recalim always false */
5929 task->tk_status = 0;
5930 }
5931
5932 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5933 nfs_restart_rpc(task, server->nfs_client);
5934 return;
5935 }
5936
5937 if (task->tk_status == 0)
5938 nfs_post_op_update_inode_force_wcc(data->args.inode,
5939 data->res.fattr);
5940}
5941
5942static void nfs4_layoutcommit_release(void *calldata)
5943{
5944 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04005945 struct pnfs_layout_segment *lseg, *tmp;
Andy Adamson863a3c62011-03-23 13:27:54 +00005946
Andy Adamsondb29c082011-07-30 20:52:38 -04005947 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00005948 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04005949 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
5950 list_del_init(&lseg->pls_lc_list);
5951 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
5952 &lseg->pls_flags))
5953 put_lseg(lseg);
5954 }
Andy Adamson863a3c62011-03-23 13:27:54 +00005955 put_rpccred(data->cred);
5956 kfree(data);
5957}
5958
5959static const struct rpc_call_ops nfs4_layoutcommit_ops = {
5960 .rpc_call_prepare = nfs4_layoutcommit_prepare,
5961 .rpc_call_done = nfs4_layoutcommit_done,
5962 .rpc_release = nfs4_layoutcommit_release,
5963};
5964
5965int
Andy Adamsonef311532011-03-12 02:58:10 -05005966nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00005967{
5968 struct rpc_message msg = {
5969 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
5970 .rpc_argp = &data->args,
5971 .rpc_resp = &data->res,
5972 .rpc_cred = data->cred,
5973 };
5974 struct rpc_task_setup task_setup_data = {
5975 .task = &data->task,
5976 .rpc_client = NFS_CLIENT(data->args.inode),
5977 .rpc_message = &msg,
5978 .callback_ops = &nfs4_layoutcommit_ops,
5979 .callback_data = data,
5980 .flags = RPC_TASK_ASYNC,
5981 };
5982 struct rpc_task *task;
5983 int status = 0;
5984
5985 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
5986 "lbw: %llu inode %lu\n",
5987 data->task.tk_pid, sync,
5988 data->args.lastbytewritten,
5989 data->args.inode->i_ino);
5990
5991 task = rpc_run_task(&task_setup_data);
5992 if (IS_ERR(task))
5993 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05005994 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00005995 goto out;
5996 status = nfs4_wait_for_completion_rpc_task(task);
5997 if (status != 0)
5998 goto out;
5999 status = task->tk_status;
6000out:
6001 dprintk("%s: status %d\n", __func__, status);
6002 rpc_put_task(task);
6003 return status;
6004}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006005
6006static int
6007_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6008 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6009{
6010 struct nfs41_secinfo_no_name_args args = {
6011 .style = SECINFO_STYLE_CURRENT_FH,
6012 };
6013 struct nfs4_secinfo_res res = {
6014 .flavors = flavors,
6015 };
6016 struct rpc_message msg = {
6017 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6018 .rpc_argp = &args,
6019 .rpc_resp = &res,
6020 };
6021 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6022}
6023
6024static int
6025nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6026 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6027{
6028 struct nfs4_exception exception = { };
6029 int err;
6030 do {
6031 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6032 switch (err) {
6033 case 0:
6034 case -NFS4ERR_WRONGSEC:
6035 case -NFS4ERR_NOTSUPP:
6036 break;
6037 default:
6038 err = nfs4_handle_exception(server, err, &exception);
6039 }
6040 } while (exception.retry);
6041 return err;
6042}
6043
6044static int
6045nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6046 struct nfs_fsinfo *info)
6047{
6048 int err;
6049 struct page *page;
6050 rpc_authflavor_t flavor;
6051 struct nfs4_secinfo_flavors *flavors;
6052
6053 page = alloc_page(GFP_KERNEL);
6054 if (!page) {
6055 err = -ENOMEM;
6056 goto out;
6057 }
6058
6059 flavors = page_address(page);
6060 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6061
6062 /*
6063 * Fall back on "guess and check" method if
6064 * the server doesn't support SECINFO_NO_NAME
6065 */
6066 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6067 err = nfs4_find_root_sec(server, fhandle, info);
6068 goto out_freepage;
6069 }
6070 if (err)
6071 goto out_freepage;
6072
6073 flavor = nfs_find_best_sec(flavors);
6074 if (err == 0)
6075 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6076
6077out_freepage:
6078 put_page(page);
6079 if (err == -EACCES)
6080 return -EPERM;
6081out:
6082 return err;
6083}
Bryan Schumaker7d974792011-06-02 14:59:08 -04006084static int _nfs41_test_stateid(struct nfs_server *server, struct nfs4_state *state)
6085{
6086 int status;
6087 struct nfs41_test_stateid_args args = {
6088 .stateid = &state->stateid,
6089 };
6090 struct nfs41_test_stateid_res res;
6091 struct rpc_message msg = {
6092 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6093 .rpc_argp = &args,
6094 .rpc_resp = &res,
6095 };
6096 args.seq_args.sa_session = res.seq_res.sr_session = NULL;
6097 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 0, 1);
6098 return status;
6099}
6100
6101static int nfs41_test_stateid(struct nfs_server *server, struct nfs4_state *state)
6102{
6103 struct nfs4_exception exception = { };
6104 int err;
6105 do {
6106 err = nfs4_handle_exception(server,
6107 _nfs41_test_stateid(server, state),
6108 &exception);
6109 } while (exception.retry);
6110 return err;
6111}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006112
6113static int _nfs4_free_stateid(struct nfs_server *server, struct nfs4_state *state)
6114{
6115 int status;
6116 struct nfs41_free_stateid_args args = {
6117 .stateid = &state->stateid,
6118 };
6119 struct nfs41_free_stateid_res res;
6120 struct rpc_message msg = {
6121 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6122 .rpc_argp = &args,
6123 .rpc_resp = &res,
6124 };
6125
6126 args.seq_args.sa_session = res.seq_res.sr_session = NULL;
6127 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 0, 1);
6128 return status;
6129}
6130
6131static int nfs41_free_stateid(struct nfs_server *server, struct nfs4_state *state)
6132{
6133 struct nfs4_exception exception = { };
6134 int err;
6135 do {
6136 err = nfs4_handle_exception(server,
6137 _nfs4_free_stateid(server, state),
6138 &exception);
6139 } while (exception.retry);
6140 return err;
6141}
Andy Adamson557134a2009-04-01 09:21:53 -04006142#endif /* CONFIG_NFS_V4_1 */
6143
Andy Adamson591d71c2009-04-01 09:22:47 -04006144struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006145 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006146 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006147 .recover_open = nfs4_open_reclaim,
6148 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006149 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006150 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006151};
6152
Andy Adamson591d71c2009-04-01 09:22:47 -04006153#if defined(CONFIG_NFS_V4_1)
6154struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6155 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6156 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6157 .recover_open = nfs4_open_reclaim,
6158 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006159 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006160 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006161 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006162};
6163#endif /* CONFIG_NFS_V4_1 */
6164
6165struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006166 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006167 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006168 .recover_open = nfs4_open_expired,
6169 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006170 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006171 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172};
6173
Andy Adamson591d71c2009-04-01 09:22:47 -04006174#if defined(CONFIG_NFS_V4_1)
6175struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6176 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6177 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006178 .recover_open = nfs41_open_expired,
6179 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006180 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006181 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006182};
6183#endif /* CONFIG_NFS_V4_1 */
6184
Benny Halevy29fba382009-04-01 09:22:44 -04006185struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6186 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006187 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006188 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006189};
6190
6191#if defined(CONFIG_NFS_V4_1)
6192struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
6193 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006194 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006195 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006196};
6197#endif
6198
Trond Myklebust97dc1352010-06-16 09:52:26 -04006199static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6200 .minor_version = 0,
6201 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04006202 .validate_stateid = nfs4_validate_delegation_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006203 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006204 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6205 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6206 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006207};
6208
6209#if defined(CONFIG_NFS_V4_1)
6210static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6211 .minor_version = 1,
6212 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04006213 .validate_stateid = nfs41_validate_delegation_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006214 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006215 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6216 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6217 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006218};
6219#endif
6220
Trond Myklebust97dc1352010-06-16 09:52:26 -04006221const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6222 [0] = &nfs_v4_0_minor_ops,
6223#if defined(CONFIG_NFS_V4_1)
6224 [1] = &nfs_v4_1_minor_ops,
6225#endif
6226};
6227
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006228static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006229 .permission = nfs_permission,
6230 .getattr = nfs_getattr,
6231 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006232 .getxattr = generic_getxattr,
6233 .setxattr = generic_setxattr,
6234 .listxattr = generic_listxattr,
6235 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006236};
6237
David Howells509de812006-08-22 20:06:11 -04006238const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239 .version = 4, /* protocol version */
6240 .dentry_ops = &nfs4_dentry_operations,
6241 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006242 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243 .getroot = nfs4_proc_get_root,
6244 .getattr = nfs4_proc_getattr,
6245 .setattr = nfs4_proc_setattr,
6246 .lookup = nfs4_proc_lookup,
6247 .access = nfs4_proc_access,
6248 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249 .create = nfs4_proc_create,
6250 .remove = nfs4_proc_remove,
6251 .unlink_setup = nfs4_proc_unlink_setup,
6252 .unlink_done = nfs4_proc_unlink_done,
6253 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006254 .rename_setup = nfs4_proc_rename_setup,
6255 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006256 .link = nfs4_proc_link,
6257 .symlink = nfs4_proc_symlink,
6258 .mkdir = nfs4_proc_mkdir,
6259 .rmdir = nfs4_proc_remove,
6260 .readdir = nfs4_proc_readdir,
6261 .mknod = nfs4_proc_mknod,
6262 .statfs = nfs4_proc_statfs,
6263 .fsinfo = nfs4_proc_fsinfo,
6264 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006265 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006266 .decode_dirent = nfs4_decode_dirent,
6267 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05006268 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006269 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006270 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006272 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006273 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006274 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006275 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006276 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00006277 .init_client = nfs4_init_client,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006278 .secinfo = nfs4_proc_secinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006279};
6280
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006281static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6282 .prefix = XATTR_NAME_NFSV4_ACL,
6283 .list = nfs4_xattr_list_nfs4_acl,
6284 .get = nfs4_xattr_get_nfs4_acl,
6285 .set = nfs4_xattr_set_nfs4_acl,
6286};
6287
6288const struct xattr_handler *nfs4_xattr_handlers[] = {
6289 &nfs4_xattr_nfs4_acl_handler,
6290 NULL
6291};
6292
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293/*
6294 * Local variables:
6295 * c-basic-offset: 8
6296 * End:
6297 */