blob: 93ef77666efc867134dedfd1428033c40d7bbf39 [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 *);
Bryan Schumaker7c513052011-03-24 17:12:24 +000076static int _nfs4_proc_lookup(struct rpc_clnt *client, struct inode *dir,
77 const struct qstr *name, struct nfs_fh *fhandle,
78 struct nfs_fattr *fattr);
Trond Myklebust99367812007-07-17 21:52:41 -040079static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040080static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
81 struct nfs_fattr *fattr, struct iattr *sattr,
82 struct nfs4_state *state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040083#ifdef CONFIG_NFS_V4_1
84static int nfs41_test_stateid(struct nfs_server *, struct nfs4_state *);
85static int nfs41_free_stateid(struct nfs_server *, struct nfs4_state *);
86#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070087/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050088static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Trond Myklebust52567b02009-10-23 14:46:42 -040090 if (err >= -1000)
91 return err;
92 switch (err) {
93 case -NFS4ERR_RESOURCE:
94 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +000095 case -NFS4ERR_WRONGSEC:
96 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -080097 case -NFS4ERR_BADOWNER:
98 case -NFS4ERR_BADNAME:
99 return -EINVAL;
Trond Myklebust52567b02009-10-23 14:46:42 -0400100 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700102 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400103 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400105 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
108/*
109 * This is our standard bitmap for GETATTR requests.
110 */
111const u32 nfs4_fattr_bitmap[2] = {
112 FATTR4_WORD0_TYPE
113 | FATTR4_WORD0_CHANGE
114 | FATTR4_WORD0_SIZE
115 | FATTR4_WORD0_FSID
116 | FATTR4_WORD0_FILEID,
117 FATTR4_WORD1_MODE
118 | FATTR4_WORD1_NUMLINKS
119 | FATTR4_WORD1_OWNER
120 | FATTR4_WORD1_OWNER_GROUP
121 | FATTR4_WORD1_RAWDEV
122 | FATTR4_WORD1_SPACE_USED
123 | FATTR4_WORD1_TIME_ACCESS
124 | FATTR4_WORD1_TIME_METADATA
125 | FATTR4_WORD1_TIME_MODIFY
126};
127
128const u32 nfs4_statfs_bitmap[2] = {
129 FATTR4_WORD0_FILES_AVAIL
130 | FATTR4_WORD0_FILES_FREE
131 | FATTR4_WORD0_FILES_TOTAL,
132 FATTR4_WORD1_SPACE_AVAIL
133 | FATTR4_WORD1_SPACE_FREE
134 | FATTR4_WORD1_SPACE_TOTAL
135};
136
Trond Myklebust4ce79712005-06-22 17:16:21 +0000137const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 FATTR4_WORD0_MAXLINK
139 | FATTR4_WORD0_MAXNAME,
140 0
141};
142
143const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
144 | FATTR4_WORD0_MAXREAD
145 | FATTR4_WORD0_MAXWRITE
146 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700147 FATTR4_WORD1_TIME_DELTA
Andy Adamson504913f2010-10-20 00:17:57 -0400148 | FATTR4_WORD1_FS_LAYOUT_TYPES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149};
150
Manoj Naik830b8e32006-06-09 09:34:25 -0400151const u32 nfs4_fs_locations_bitmap[2] = {
152 FATTR4_WORD0_TYPE
153 | FATTR4_WORD0_CHANGE
154 | FATTR4_WORD0_SIZE
155 | FATTR4_WORD0_FSID
156 | FATTR4_WORD0_FILEID
157 | FATTR4_WORD0_FS_LOCATIONS,
158 FATTR4_WORD1_MODE
159 | FATTR4_WORD1_NUMLINKS
160 | FATTR4_WORD1_OWNER
161 | FATTR4_WORD1_OWNER_GROUP
162 | FATTR4_WORD1_RAWDEV
163 | FATTR4_WORD1_SPACE_USED
164 | FATTR4_WORD1_TIME_ACCESS
165 | FATTR4_WORD1_TIME_METADATA
166 | FATTR4_WORD1_TIME_MODIFY
167 | FATTR4_WORD1_MOUNTED_ON_FILEID
168};
169
Al Virobc4785c2006-10-19 23:28:51 -0700170static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 struct nfs4_readdir_arg *readdir)
172{
Al Viro0dbb4c62006-10-19 23:28:49 -0700173 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 BUG_ON(readdir->count < 80);
176 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000177 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
179 return;
180 }
181
182 readdir->cookie = 0;
183 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
184 if (cookie == 2)
185 return;
186
187 /*
188 * NFSv4 servers do not return entries for '.' and '..'
189 * Therefore, we fake these entries here. We let '.'
190 * have cookie 0 and '..' have cookie 1. Note that
191 * when talking to the server, we always send cookie 0
192 * instead of 1 or 2.
193 */
Al Viro0dbb4c62006-10-19 23:28:49 -0700194 start = p = kmap_atomic(*readdir->pages, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 if (cookie == 0) {
197 *p++ = xdr_one; /* next */
198 *p++ = xdr_zero; /* cookie, first word */
199 *p++ = xdr_one; /* cookie, second word */
200 *p++ = xdr_one; /* entry len */
201 memcpy(p, ".\0\0\0", 4); /* entry */
202 p++;
203 *p++ = xdr_one; /* bitmap length */
204 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
205 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400206 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
208
209 *p++ = xdr_one; /* next */
210 *p++ = xdr_zero; /* cookie, first word */
211 *p++ = xdr_two; /* cookie, second word */
212 *p++ = xdr_two; /* entry len */
213 memcpy(p, "..\0\0", 4); /* entry */
214 p++;
215 *p++ = xdr_one; /* bitmap length */
216 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
217 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400218 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 readdir->pgbase = (char *)p - (char *)start;
221 readdir->count -= readdir->pgbase;
222 kunmap_atomic(start, KM_USER0);
223}
224
Trond Myklebust65de8722008-12-23 15:21:44 -0500225static int nfs4_wait_clnt_recover(struct nfs_client *clp)
226{
227 int res;
228
229 might_sleep();
230
Trond Myklebuste005e802008-12-23 15:21:48 -0500231 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400232 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500233 return res;
234}
235
236static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
237{
238 int res = 0;
239
240 might_sleep();
241
242 if (*timeout <= 0)
243 *timeout = NFS4_POLL_RETRY_MIN;
244 if (*timeout > NFS4_POLL_RETRY_MAX)
245 *timeout = NFS4_POLL_RETRY_MAX;
246 schedule_timeout_killable(*timeout);
247 if (fatal_signal_pending(current))
248 res = -ERESTARTSYS;
249 *timeout <<= 1;
250 return res;
251}
252
253/* This is the error handling routine for processes that are allowed
254 * to sleep.
255 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800256static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500257{
258 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500259 struct nfs4_state *state = exception->state;
Trond Myklebust65de8722008-12-23 15:21:44 -0500260 int ret = errorcode;
261
262 exception->retry = 0;
263 switch(errorcode) {
264 case 0:
265 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500266 case -NFS4ERR_ADMIN_REVOKED:
267 case -NFS4ERR_BAD_STATEID:
268 case -NFS4ERR_OPENMODE:
269 if (state == NULL)
270 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500271 nfs4_schedule_stateid_recovery(server, state);
272 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400273 case -NFS4ERR_EXPIRED:
274 if (state != NULL)
275 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500276 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500277 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500278 nfs4_schedule_lease_recovery(clp);
279 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500280#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400281 case -NFS4ERR_BADSESSION:
282 case -NFS4ERR_BADSLOT:
283 case -NFS4ERR_BAD_HIGH_SLOT:
284 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
285 case -NFS4ERR_DEADSESSION:
286 case -NFS4ERR_SEQ_FALSE_RETRY:
287 case -NFS4ERR_SEQ_MISORDERED:
288 dprintk("%s ERROR: %d Reset session\n", __func__,
289 errorcode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500290 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -0400291 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500292 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500293#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500294 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500295 if (exception->timeout > HZ) {
296 /* We have retried a decent amount, time to
297 * fail
298 */
299 ret = -EBUSY;
300 break;
301 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500302 case -NFS4ERR_GRACE:
303 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500304 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500305 ret = nfs4_delay(server->client, &exception->timeout);
306 if (ret != 0)
307 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400308 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500309 case -NFS4ERR_OLD_STATEID:
310 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800311 break;
312 case -NFS4ERR_BADOWNER:
313 /* The following works around a Linux server bug! */
314 case -NFS4ERR_BADNAME:
315 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
316 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
317 exception->retry = 1;
318 printk(KERN_WARNING "NFS: v4 server %s "
319 "does not accept raw "
320 "uid/gids. "
321 "Reenabling the idmapper.\n",
322 server->nfs_client->cl_hostname);
323 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500324 }
325 /* We failed to handle the error */
326 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500327wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500328 ret = nfs4_wait_clnt_recover(clp);
329 if (ret == 0)
330 exception->retry = 1;
331 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500332}
333
334
Trond Myklebust452e9352010-07-31 14:29:06 -0400335static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 spin_lock(&clp->cl_lock);
338 if (time_before(clp->cl_last_renewal,timestamp))
339 clp->cl_last_renewal = timestamp;
340 spin_unlock(&clp->cl_lock);
341}
342
Trond Myklebust452e9352010-07-31 14:29:06 -0400343static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
344{
345 do_renew_lease(server->nfs_client, timestamp);
346}
347
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400348#if defined(CONFIG_NFS_V4_1)
349
Benny Halevy510b8172009-04-01 09:22:14 -0400350/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400351 * nfs4_free_slot - free a slot and efficiently update slot table.
352 *
353 * freeing a slot is trivially done by clearing its respective bit
354 * in the bitmap.
355 * If the freed slotid equals highest_used_slotid we want to update it
356 * so that the server would be able to size down the slot table if needed,
357 * otherwise we know that the highest_used_slotid is still in use.
358 * When updating highest_used_slotid there may be "holes" in the bitmap
359 * so we need to scan down from highest_used_slotid to 0 looking for the now
360 * highest slotid in use.
361 * If none found, highest_used_slotid is set to -1.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500362 *
363 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400364 */
365static void
Benny Halevydfb4f3092010-09-24 09:17:01 -0400366nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *free_slot)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400367{
Benny Halevydfb4f3092010-09-24 09:17:01 -0400368 int free_slotid = free_slot - tbl->slots;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400369 int slotid = free_slotid;
370
Benny Halevydfb4f3092010-09-24 09:17:01 -0400371 BUG_ON(slotid < 0 || slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400372 /* clear used bit in bitmap */
373 __clear_bit(slotid, tbl->used_slots);
374
375 /* update highest_used_slotid when it is freed */
376 if (slotid == tbl->highest_used_slotid) {
377 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500378 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400379 tbl->highest_used_slotid = slotid;
380 else
381 tbl->highest_used_slotid = -1;
382 }
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400383 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
384 free_slotid, tbl->highest_used_slotid);
385}
386
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800387/*
Andy Adamson42acd022011-01-06 02:04:34 +0000388 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800389 */
Andy Adamson42acd022011-01-06 02:04:34 +0000390static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800391{
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800392 struct rpc_task *task;
393
Trond Myklebusta2118c32010-06-16 09:52:26 -0400394 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800395 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
396 if (task)
397 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800398 return;
399 }
400
401 if (ses->fc_slot_table.highest_used_slotid != -1)
402 return;
403
Andy Adamson42acd022011-01-06 02:04:34 +0000404 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
405 complete(&ses->fc_slot_table.complete);
406}
407
408/*
409 * Signal state manager thread if session back channel is drained
410 */
411void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
412{
413 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
414 ses->bc_slot_table.highest_used_slotid != -1)
415 return;
416 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
417 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800418}
419
Trond Myklebustd185a332010-06-16 09:52:25 -0400420static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400421{
422 struct nfs4_slot_table *tbl;
423
Trond Myklebustd185a332010-06-16 09:52:25 -0400424 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400425 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400426 /* just wake up the next guy waiting since
427 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500428 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500429 return;
Andy Adamson13615872009-04-01 09:22:17 -0400430 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500431
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500432 spin_lock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400433 nfs4_free_slot(tbl, res->sr_slot);
Andy Adamson42acd022011-01-06 02:04:34 +0000434 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500435 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400436 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400437}
438
Trond Myklebust14516c32010-07-31 14:29:06 -0400439static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400440{
441 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400442 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400443
444 /*
445 * sr_status remains 1 if an RPC level error occurred. The server
446 * may or may not have processed the sequence operation..
447 * Proceed as if the server received and processed the sequence
448 * operation.
449 */
450 if (res->sr_status == 1)
451 res->sr_status = NFS_OK;
452
Bryan Schumaker468f8612011-04-18 15:57:32 -0400453 /* don't increment the sequence number if the task wasn't sent */
454 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400455 goto out;
456
Andy Adamson691daf32009-12-04 15:55:39 -0500457 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400458 switch (res->sr_status) {
459 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400460 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400461 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400462 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400463 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400464 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500465 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500466 if (res->sr_status_flags != 0)
467 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400468 break;
469 case -NFS4ERR_DELAY:
470 /* The server detected a resend of the RPC call and
471 * returned NFS4ERR_DELAY as per Section 2.10.6.2
472 * of RFC5661.
473 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200474 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400475 __func__,
476 res->sr_slot - res->sr_session->fc_slot_table.slots,
477 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400478 goto out_retry;
479 default:
480 /* Just update the slot sequence no. */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400481 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400482 }
483out:
484 /* The session may be reset by one of the error handlers. */
485 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400486 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400487 return 1;
488out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400489 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400490 goto out;
491 rpc_delay(task, NFS4_POLL_RETRY_MAX);
492 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400493}
494
Trond Myklebust14516c32010-07-31 14:29:06 -0400495static int nfs4_sequence_done(struct rpc_task *task,
496 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400497{
Trond Myklebust14516c32010-07-31 14:29:06 -0400498 if (res->sr_session == NULL)
499 return 1;
500 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400501}
502
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400503/*
Benny Halevy510b8172009-04-01 09:22:14 -0400504 * nfs4_find_slot - efficiently look for a free slot
505 *
506 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
507 * If found, we mark the slot as used, update the highest_used_slotid,
508 * and respectively set up the sequence operation args.
509 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400510 *
511 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400512 */
513static u8
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800514nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400515{
516 int slotid;
517 u8 ret_id = NFS4_MAX_SLOT_TABLE;
518 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
519
Benny Halevy510b8172009-04-01 09:22:14 -0400520 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
521 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
522 tbl->max_slots);
523 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
524 if (slotid >= tbl->max_slots)
525 goto out;
526 __set_bit(slotid, tbl->used_slots);
527 if (slotid > tbl->highest_used_slotid)
528 tbl->highest_used_slotid = slotid;
529 ret_id = slotid;
530out:
531 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
532 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400533 return ret_id;
534}
535
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000536int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400537 struct nfs4_sequence_args *args,
538 struct nfs4_sequence_res *res,
539 int cache_reply,
540 struct rpc_task *task)
541{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400542 struct nfs4_slot *slot;
543 struct nfs4_slot_table *tbl;
544 u8 slotid;
545
546 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400547 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400548 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400549 return 0;
550
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400551 tbl = &session->fc_slot_table;
552
553 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400554 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800555 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamsonea028ac2009-12-04 15:55:38 -0500556 /*
557 * The state manager will wait until the slot table is empty.
558 * Schedule the reset thread
559 */
Andy Adamson05f0d232009-12-04 15:55:37 -0500560 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400561 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500562 dprintk("%s Schedule Session Reset\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500563 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400564 }
565
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800566 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
567 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
568 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
569 spin_unlock(&tbl->slot_tbl_lock);
570 dprintk("%s enforce FIFO order\n", __func__);
571 return -EAGAIN;
572 }
573
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800574 slotid = nfs4_find_slot(tbl);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400575 if (slotid == NFS4_MAX_SLOT_TABLE) {
576 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
577 spin_unlock(&tbl->slot_tbl_lock);
578 dprintk("<-- %s: no free slots\n", __func__);
579 return -EAGAIN;
580 }
581 spin_unlock(&tbl->slot_tbl_lock);
582
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800583 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400584 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400585 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400586 args->sa_slotid = slotid;
587 args->sa_cache_this = cache_reply;
588
589 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
590
Benny Halevy99fe60d2009-04-01 09:22:29 -0400591 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400592 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400593 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400594 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400595 /*
596 * sr_status is only set in decode_sequence, and so will remain
597 * set to 1 if an rpc level failure occurs.
598 */
599 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400600 return 0;
601}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000602EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400603
Trond Myklebust035168a2010-06-16 09:52:26 -0400604int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400605 struct nfs4_sequence_args *args,
606 struct nfs4_sequence_res *res,
607 int cache_reply,
608 struct rpc_task *task)
609{
Trond Myklebust035168a2010-06-16 09:52:26 -0400610 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400611 int ret = 0;
612
Trond Myklebust035168a2010-06-16 09:52:26 -0400613 if (session == NULL) {
614 args->sa_session = NULL;
615 res->sr_session = NULL;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400616 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400617 }
618
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200619 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400620 __func__, session->clp, session, res->sr_slot ?
621 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400622
623 ret = nfs41_setup_sequence(session, args, res, cache_reply,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400624 task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400625out:
626 dprintk("<-- %s status=%d\n", __func__, ret);
627 return ret;
628}
629
630struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400631 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400632 struct nfs4_sequence_args *seq_args;
633 struct nfs4_sequence_res *seq_res;
634 int cache_reply;
635};
636
637static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
638{
639 struct nfs41_call_sync_data *data = calldata;
640
Trond Myklebust035168a2010-06-16 09:52:26 -0400641 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
642
643 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400644 data->seq_res, data->cache_reply, task))
645 return;
646 rpc_call_start(task);
647}
648
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800649static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
650{
651 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
652 nfs41_call_sync_prepare(task, calldata);
653}
654
Andy Adamson69ab40c2009-04-01 09:22:19 -0400655static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
656{
657 struct nfs41_call_sync_data *data = calldata;
658
Trond Myklebust14516c32010-07-31 14:29:06 -0400659 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400660}
661
Andy Adamsonce5039c2009-04-01 09:22:13 -0400662struct rpc_call_ops nfs41_call_sync_ops = {
663 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400664 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400665};
666
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800667struct rpc_call_ops nfs41_call_priv_sync_ops = {
668 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
669 .rpc_call_done = nfs41_call_sync_done,
670};
671
Bryan Schumaker7c513052011-03-24 17:12:24 +0000672static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
673 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400674 struct rpc_message *msg,
675 struct nfs4_sequence_args *args,
676 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800677 int cache_reply,
678 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400679{
680 int ret;
681 struct rpc_task *task;
682 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400683 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400684 .seq_args = args,
685 .seq_res = res,
686 .cache_reply = cache_reply,
687 };
688 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000689 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400690 .rpc_message = msg,
691 .callback_ops = &nfs41_call_sync_ops,
692 .callback_data = &data
693 };
694
Benny Halevydfb4f3092010-09-24 09:17:01 -0400695 res->sr_slot = NULL;
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800696 if (privileged)
697 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400698 task = rpc_run_task(&task_setup);
699 if (IS_ERR(task))
700 ret = PTR_ERR(task);
701 else {
702 ret = task->tk_status;
703 rpc_put_task(task);
704 }
705 return ret;
706}
707
Bryan Schumaker7c513052011-03-24 17:12:24 +0000708int _nfs4_call_sync_session(struct rpc_clnt *clnt,
709 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400710 struct rpc_message *msg,
711 struct nfs4_sequence_args *args,
712 struct nfs4_sequence_res *res,
713 int cache_reply)
714{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000715 return nfs4_call_sync_sequence(clnt, server, msg, args, res, cache_reply, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400716}
717
Trond Myklebustdf896452010-06-16 09:52:26 -0400718#else
Trond Myklebust14516c32010-07-31 14:29:06 -0400719static int nfs4_sequence_done(struct rpc_task *task,
720 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400721{
Trond Myklebust14516c32010-07-31 14:29:06 -0400722 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400723}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400724#endif /* CONFIG_NFS_V4_1 */
725
Bryan Schumaker7c513052011-03-24 17:12:24 +0000726int _nfs4_call_sync(struct rpc_clnt *clnt,
727 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400728 struct rpc_message *msg,
729 struct nfs4_sequence_args *args,
730 struct nfs4_sequence_res *res,
731 int cache_reply)
732{
Benny Halevyf3752972009-04-01 09:22:04 -0400733 args->sa_session = res->sr_session = NULL;
Bryan Schumaker7c513052011-03-24 17:12:24 +0000734 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400735}
736
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000737static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000738int nfs4_call_sync(struct rpc_clnt *clnt,
739 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000740 struct rpc_message *msg,
741 struct nfs4_sequence_args *args,
742 struct nfs4_sequence_res *res,
743 int cache_reply)
744{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000745 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
746 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000747}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400748
Trond Myklebust38478b22006-05-25 01:40:57 -0400749static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
Trond Myklebust38478b22006-05-25 01:40:57 -0400751 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Trond Myklebust38478b22006-05-25 01:40:57 -0400753 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400754 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
755 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400756 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400757 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400758 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
760
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100761struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400762 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100763 struct nfs_openargs o_arg;
764 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100765 struct nfs_open_confirmargs c_arg;
766 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100767 struct nfs_fattr f_attr;
768 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400769 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100770 struct dentry *dir;
771 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400772 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100773 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100774 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400775 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100776 int rpc_status;
777 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100778};
779
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400780
781static void nfs4_init_opendata_res(struct nfs4_opendata *p)
782{
783 p->o_res.f_attr = &p->f_attr;
784 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400785 p->o_res.seqid = p->o_arg.seqid;
786 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400787 p->o_res.server = p->o_arg.server;
788 nfs_fattr_init(&p->f_attr);
789 nfs_fattr_init(&p->dir_attr);
790}
791
Trond Myklebustad389da2007-06-05 12:30:00 -0400792static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500793 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400794 const struct iattr *attrs,
795 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100796{
Trond Myklebustad389da2007-06-05 12:30:00 -0400797 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100798 struct inode *dir = parent->d_inode;
799 struct nfs_server *server = NFS_SERVER(dir);
800 struct nfs4_opendata *p;
801
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400802 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100803 if (p == NULL)
804 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400805 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100806 if (p->o_arg.seqid == NULL)
807 goto err_free;
Al Virof6948692010-01-30 13:51:04 -0500808 path_get(path);
809 p->path = *path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100810 p->dir = parent;
811 p->owner = sp;
812 atomic_inc(&sp->so_count);
813 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500814 p->o_arg.open_flags = flags;
815 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400816 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400817 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400818 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100819 p->o_arg.server = server;
820 p->o_arg.bitmask = server->attr_bitmask;
821 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400822 if (flags & O_CREAT) {
823 u32 *s;
824
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100825 p->o_arg.u.attrs = &p->attrs;
826 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400827 s = (u32 *) p->o_arg.u.verifier.data;
828 s[0] = jiffies;
829 s[1] = current->pid;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100830 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100831 p->c_arg.fh = &p->o_res.fh;
832 p->c_arg.stateid = &p->o_res.stateid;
833 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400834 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400835 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100836 return p;
837err_free:
838 kfree(p);
839err:
840 dput(parent);
841 return NULL;
842}
843
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400844static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100845{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400846 struct nfs4_opendata *p = container_of(kref,
847 struct nfs4_opendata, kref);
848
849 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400850 if (p->state != NULL)
851 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400852 nfs4_put_state_owner(p->owner);
853 dput(p->dir);
Jan Blunck31f31db12008-05-02 13:42:45 -0700854 path_put(&p->path);
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
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001136 opendata = nfs4_opendata_alloc(&ctx->path, 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);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001160 nfs4_close_state(&opendata->path, 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))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001358 nfs4_close_state(&data->path, 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))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001503 nfs4_close_state(&data->path, 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)
Trond Myklebust539cd032007-06-05 11:46:42 -04001654 d_drop(ctx->path.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 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001726static int _nfs4_do_open(struct inode *dir, struct path *path, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
Linus Torvalds1da177e2005-04-16 15:20:36 -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;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001743 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001744 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001745 status = -ENOMEM;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001746 opendata = nfs4_opendata_alloc(path, 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
Trond Myklebustaac00a82007-07-05 19:02:21 -04001750 if (path->dentry->d_inode != NULL)
1751 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1752
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
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001789static struct nfs4_state *nfs4_do_open(struct inode *dir, struct path *path, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790{
1791 struct nfs4_exception exception = { };
1792 struct nfs4_state *res;
1793 int status;
1794
1795 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001796 status = _nfs4_do_open(dir, path, 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 {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001893 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 struct inode *inode;
1895 struct nfs4_state *state;
1896 struct nfs_closeargs arg;
1897 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001898 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001899 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00001900 bool roc;
1901 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902};
1903
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001904static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001905{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001906 struct nfs4_closedata *calldata = data;
1907 struct nfs4_state_owner *sp = calldata->state->owner;
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);
Jan Blunck31f31db12008-05-02 13:42:45 -07001914 path_put(&calldata->path);
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 */
Fred Isamanf7e89172011-01-06 11:36:32 +00002034int nfs4_do_close(struct path *path, struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -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 Virof6948692010-01-30 13:51:04 -05002070 path_get(path);
2071 calldata->path = *path;
Trond Myklebust95121352005-10-18 14:20:12 -07002072
Trond Myklebust1174dd12010-12-21 10:52:24 -05002073 msg.rpc_argp = &calldata->arg;
2074 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002075 task_setup_data.callback_data = calldata;
2076 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002077 if (IS_ERR(task))
2078 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002079 status = 0;
2080 if (wait)
2081 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002082 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002083 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002084out_free_calldata:
2085 kfree(calldata);
2086out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002087 if (roc)
2088 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002089 nfs4_put_open_state(state);
2090 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002091 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092}
2093
Trond Myklebust2b484292010-09-17 10:56:51 -04002094static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002095nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 struct nfs4_state *state;
2098
Trond Myklebust565277f2007-10-15 18:17:53 -04002099 /* Protect against concurrent sillydeletes */
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002100 state = nfs4_do_open(dir, &ctx->path, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002101 if (IS_ERR(state))
2102 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002103 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002104 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105}
2106
Trond Myklebust1185a552009-12-03 15:54:02 -05002107static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002108{
2109 if (ctx->state == NULL)
2110 return;
2111 if (is_sync)
2112 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2113 else
2114 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2115}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
2117static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2118{
Benny Halevy43652ad2009-04-01 09:21:54 -04002119 struct nfs4_server_caps_arg args = {
2120 .fhandle = fhandle,
2121 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 struct nfs4_server_caps_res res = {};
2123 struct rpc_message msg = {
2124 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002125 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 .rpc_resp = &res,
2127 };
2128 int status;
2129
Bryan Schumaker7c513052011-03-24 17:12:24 +00002130 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 if (status == 0) {
2132 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002133 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2134 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2135 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2136 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2137 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2139 server->caps |= NFS_CAP_ACLS;
2140 if (res.has_links != 0)
2141 server->caps |= NFS_CAP_HARDLINKS;
2142 if (res.has_symlinks != 0)
2143 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002144 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2145 server->caps |= NFS_CAP_FILEID;
2146 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2147 server->caps |= NFS_CAP_MODE;
2148 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2149 server->caps |= NFS_CAP_NLINK;
2150 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2151 server->caps |= NFS_CAP_OWNER;
2152 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2153 server->caps |= NFS_CAP_OWNER_GROUP;
2154 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2155 server->caps |= NFS_CAP_ATIME;
2156 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2157 server->caps |= NFS_CAP_CTIME;
2158 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2159 server->caps |= NFS_CAP_MTIME;
2160
Trond Myklebusta65318b2009-03-11 14:10:28 -04002161 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2162 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2163 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 server->acl_bitmask = res.acl_bitmask;
2165 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002166
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 return status;
2168}
2169
Trond Myklebust55a97592006-06-09 09:34:19 -04002170int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171{
2172 struct nfs4_exception exception = { };
2173 int err;
2174 do {
2175 err = nfs4_handle_exception(server,
2176 _nfs4_server_capabilities(server, fhandle),
2177 &exception);
2178 } while (exception.retry);
2179 return err;
2180}
2181
2182static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2183 struct nfs_fsinfo *info)
2184{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 struct nfs4_lookup_root_arg args = {
2186 .bitmask = nfs4_fattr_bitmap,
2187 };
2188 struct nfs4_lookup_res res = {
2189 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002190 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 .fh = fhandle,
2192 };
2193 struct rpc_message msg = {
2194 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2195 .rpc_argp = &args,
2196 .rpc_resp = &res,
2197 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002198
Trond Myklebust0e574af2005-10-27 22:12:38 -04002199 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002200 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201}
2202
2203static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2204 struct nfs_fsinfo *info)
2205{
2206 struct nfs4_exception exception = { };
2207 int err;
2208 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002209 err = _nfs4_lookup_root(server, fhandle, info);
2210 switch (err) {
2211 case 0:
2212 case -NFS4ERR_WRONGSEC:
2213 break;
2214 default:
2215 err = nfs4_handle_exception(server, err, &exception);
2216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 } while (exception.retry);
2218 return err;
2219}
2220
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002221static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2222 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2223{
2224 struct rpc_auth *auth;
2225 int ret;
2226
2227 auth = rpcauth_create(flavor, server->client);
2228 if (!auth) {
2229 ret = -EIO;
2230 goto out;
2231 }
2232 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002233out:
2234 return ret;
2235}
2236
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002237static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002238 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002240 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002241 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002243 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2244 flav_array[len] = RPC_AUTH_NULL;
2245 len += 1;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002246
2247 for (i = 0; i < len; i++) {
2248 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002249 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002250 continue;
2251 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002252 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002253 /*
2254 * -EACCESS could mean that the user doesn't have correct permissions
2255 * to access the mount. It could also mean that we tried to mount
2256 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2257 * existing mount programs don't handle -EACCES very well so it should
2258 * be mapped to -EPERM instead.
2259 */
2260 if (status == -EACCES)
2261 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002262 return status;
2263}
2264
2265/*
2266 * get the file handle for the "/" directory on the server
2267 */
2268static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2269 struct nfs_fsinfo *info)
2270{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002271 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002272 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002273 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2274 /*
2275 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2276 * by nfs4_map_errors() as this function exits.
2277 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002278 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 if (status == 0)
2280 status = nfs4_server_capabilities(server, fhandle);
2281 if (status == 0)
2282 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002283 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284}
2285
Andy Adamson533eb462011-06-13 18:25:56 -04002286static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002287/*
2288 * Get locations and (maybe) other attributes of a referral.
2289 * Note that we'll actually follow the referral later when
2290 * we detect fsid mismatch in inode revalidation
2291 */
Andy Adamson533eb462011-06-13 18:25:56 -04002292static int nfs4_get_referral(struct inode *dir, const struct qstr *name,
2293 struct nfs_fattr *fattr, struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002294{
2295 int status = -ENOMEM;
2296 struct page *page = NULL;
2297 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002298
2299 page = alloc_page(GFP_KERNEL);
2300 if (page == NULL)
2301 goto out;
2302 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2303 if (locations == NULL)
2304 goto out;
2305
Trond Myklebustc228fd32007-01-13 02:28:11 -05002306 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002307 if (status != 0)
2308 goto out;
2309 /* Make sure server returned a different fsid for the referral */
2310 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002311 dprintk("%s: server did not return a different fsid for"
2312 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002313 status = -EIO;
2314 goto out;
2315 }
Andy Adamson533eb462011-06-13 18:25:56 -04002316 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2317 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002318
Andy Adamson533eb462011-06-13 18:25:56 -04002319 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002320 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002321 memset(fhandle, 0, sizeof(struct nfs_fh));
2322out:
2323 if (page)
2324 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002325 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002326 return status;
2327}
2328
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2330{
2331 struct nfs4_getattr_arg args = {
2332 .fh = fhandle,
2333 .bitmask = server->attr_bitmask,
2334 };
2335 struct nfs4_getattr_res res = {
2336 .fattr = fattr,
2337 .server = server,
2338 };
2339 struct rpc_message msg = {
2340 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2341 .rpc_argp = &args,
2342 .rpc_resp = &res,
2343 };
2344
Trond Myklebust0e574af2005-10-27 22:12:38 -04002345 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002346 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347}
2348
2349static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2350{
2351 struct nfs4_exception exception = { };
2352 int err;
2353 do {
2354 err = nfs4_handle_exception(server,
2355 _nfs4_proc_getattr(server, fhandle, fattr),
2356 &exception);
2357 } while (exception.retry);
2358 return err;
2359}
2360
2361/*
2362 * The file is not closed if it is opened due to the a request to change
2363 * the size of the file. The open call will not be needed once the
2364 * VFS layer lookup-intents are implemented.
2365 *
2366 * Close is called when the inode is destroyed.
2367 * If we haven't opened the file for O_WRONLY, we
2368 * need to in the size_change case to obtain a stateid.
2369 *
2370 * Got race?
2371 * Because OPEN is always done by name in nfsv4, it is
2372 * possible that we opened a different file by the same
2373 * name. We can recognize this race condition, but we
2374 * can't do anything about it besides returning an error.
2375 *
2376 * This will be fixed with VFS changes (lookup-intent).
2377 */
2378static int
2379nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2380 struct iattr *sattr)
2381{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002382 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002383 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002384 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 int status;
2386
Benny Halevy8a1636c2010-07-14 15:43:57 -04002387 if (pnfs_ld_layoutret_on_setattr(inode))
2388 pnfs_return_layout(inode);
2389
Trond Myklebust0e574af2005-10-27 22:12:38 -04002390 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
Trond Myklebustd5308382005-11-04 15:33:38 -05002392 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002393 if (sattr->ia_valid & ATTR_FILE) {
2394 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002395
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002396 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002397 if (ctx) {
2398 cred = ctx->cred;
2399 state = ctx->state;
2400 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002401 }
2402
2403 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002404 if (status == 0)
2405 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 return status;
2407}
2408
Bryan Schumaker7c513052011-03-24 17:12:24 +00002409static int _nfs4_proc_lookupfh(struct rpc_clnt *clnt, struct nfs_server *server,
2410 const struct nfs_fh *dirfh, const struct qstr *name,
2411 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002412{
2413 int status;
2414 struct nfs4_lookup_arg args = {
2415 .bitmask = server->attr_bitmask,
2416 .dir_fh = dirfh,
2417 .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
2432 dprintk("NFS call lookupfh %s\n", name->name);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002433 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002434 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002435 return status;
2436}
2437
2438static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2439 struct qstr *name, struct nfs_fh *fhandle,
2440 struct nfs_fattr *fattr)
2441{
2442 struct nfs4_exception exception = { };
2443 int err;
2444 do {
Bryan Schumaker7c513052011-03-24 17:12:24 +00002445 err = _nfs4_proc_lookupfh(server->client, server, dirfh, name, fhandle, fattr);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002446 /* FIXME: !!!! */
2447 if (err == -NFS4ERR_MOVED) {
2448 err = -EREMOTE;
2449 break;
2450 }
2451 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002452 } while (exception.retry);
2453 return err;
2454}
2455
Bryan Schumaker7c513052011-03-24 17:12:24 +00002456static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2457 const struct qstr *name, struct nfs_fh *fhandle,
2458 struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002460 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461
2462 dprintk("NFS call lookup %s\n", name->name);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002463 status = _nfs4_proc_lookupfh(clnt, NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002464 if (status == -NFS4ERR_MOVED)
2465 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 dprintk("NFS reply lookup: %d\n", status);
2467 return status;
2468}
2469
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002470void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2471{
2472 memset(fh, 0, sizeof(struct nfs_fh));
2473 fattr->fsid.major = 1;
2474 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2475 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2476 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2477 fattr->nlink = 2;
2478}
2479
Bryan Schumaker7c513052011-03-24 17:12:24 +00002480static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2481 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482{
2483 struct nfs4_exception exception = { };
2484 int err;
2485 do {
2486 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumaker7c513052011-03-24 17:12:24 +00002487 _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 &exception);
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002489 if (err == -EPERM)
2490 nfs_fixup_secinfo_attributes(fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 } while (exception.retry);
2492 return err;
2493}
2494
2495static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2496{
Trond Myklebust76b32992007-08-10 17:45:11 -04002497 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 struct nfs4_accessargs args = {
2499 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002500 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002502 struct nfs4_accessres res = {
2503 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002504 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 struct rpc_message msg = {
2506 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2507 .rpc_argp = &args,
2508 .rpc_resp = &res,
2509 .rpc_cred = entry->cred,
2510 };
2511 int mode = entry->mask;
2512 int status;
2513
2514 /*
2515 * Determine which access bits we want to ask for...
2516 */
2517 if (mode & MAY_READ)
2518 args.access |= NFS4_ACCESS_READ;
2519 if (S_ISDIR(inode->i_mode)) {
2520 if (mode & MAY_WRITE)
2521 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2522 if (mode & MAY_EXEC)
2523 args.access |= NFS4_ACCESS_LOOKUP;
2524 } else {
2525 if (mode & MAY_WRITE)
2526 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2527 if (mode & MAY_EXEC)
2528 args.access |= NFS4_ACCESS_EXECUTE;
2529 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002530
2531 res.fattr = nfs_alloc_fattr();
2532 if (res.fattr == NULL)
2533 return -ENOMEM;
2534
Bryan Schumaker7c513052011-03-24 17:12:24 +00002535 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 if (!status) {
2537 entry->mask = 0;
2538 if (res.access & NFS4_ACCESS_READ)
2539 entry->mask |= MAY_READ;
2540 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2541 entry->mask |= MAY_WRITE;
2542 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2543 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002544 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002546 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 return status;
2548}
2549
2550static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2551{
2552 struct nfs4_exception exception = { };
2553 int err;
2554 do {
2555 err = nfs4_handle_exception(NFS_SERVER(inode),
2556 _nfs4_proc_access(inode, entry),
2557 &exception);
2558 } while (exception.retry);
2559 return err;
2560}
2561
2562/*
2563 * TODO: For the time being, we don't try to get any attributes
2564 * along with any of the zero-copy operations READ, READDIR,
2565 * READLINK, WRITE.
2566 *
2567 * In the case of the first three, we want to put the GETATTR
2568 * after the read-type operation -- this is because it is hard
2569 * to predict the length of a GETATTR response in v4, and thus
2570 * align the READ data correctly. This means that the GETATTR
2571 * may end up partially falling into the page cache, and we should
2572 * shift it into the 'tail' of the xdr_buf before processing.
2573 * To do this efficiently, we need to know the total length
2574 * of data received, which doesn't seem to be available outside
2575 * of the RPC layer.
2576 *
2577 * In the case of WRITE, we also want to put the GETATTR after
2578 * the operation -- in this case because we want to make sure
2579 * we get the post-operation mtime and size. This means that
2580 * we can't use xdr_encode_pages() as written: we need a variant
2581 * of it which would leave room in the 'tail' iovec.
2582 *
2583 * Both of these changes to the XDR layer would in fact be quite
2584 * minor, but I decided to leave them for a subsequent patch.
2585 */
2586static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2587 unsigned int pgbase, unsigned int pglen)
2588{
2589 struct nfs4_readlink args = {
2590 .fh = NFS_FH(inode),
2591 .pgbase = pgbase,
2592 .pglen = pglen,
2593 .pages = &page,
2594 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002595 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 struct rpc_message msg = {
2597 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2598 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002599 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 };
2601
Bryan Schumaker7c513052011-03-24 17:12:24 +00002602 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 -07002603}
2604
2605static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2606 unsigned int pgbase, unsigned int pglen)
2607{
2608 struct nfs4_exception exception = { };
2609 int err;
2610 do {
2611 err = nfs4_handle_exception(NFS_SERVER(inode),
2612 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2613 &exception);
2614 } while (exception.retry);
2615 return err;
2616}
2617
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618/*
2619 * Got race?
2620 * We will need to arrange for the VFS layer to provide an atomic open.
2621 * Until then, this create/open method is prone to inefficiency and race
2622 * conditions due to the lookup, create, and open VFS calls from sys_open()
2623 * placed on the wire.
2624 *
2625 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2626 * The file will be opened again in the subsequent VFS open call
2627 * (nfs4_proc_file_open).
2628 *
2629 * The open for read will just hang around to be used by any process that
2630 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2631 */
2632
2633static int
2634nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002635 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636{
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002637 struct path my_path = {
Trond Myklebustad389da2007-06-05 12:30:00 -04002638 .dentry = dentry,
2639 };
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002640 struct path *path = &my_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002642 struct rpc_cred *cred = NULL;
2643 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 int status = 0;
2645
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002646 if (ctx != NULL) {
2647 cred = ctx->cred;
2648 path = &ctx->path;
2649 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002651 sattr->ia_mode &= ~current_umask();
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002652 state = nfs4_do_open(dir, path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002653 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 if (IS_ERR(state)) {
2655 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002656 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002658 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002659 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002660 if (ctx != NULL)
2661 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002662 else
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002663 nfs4_close_sync(path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664out:
2665 return status;
2666}
2667
2668static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2669{
Trond Myklebust16e42952005-10-27 22:12:44 -04002670 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002671 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002673 .name.len = name->len,
2674 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002675 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002677 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002678 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002679 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002681 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2682 .rpc_argp = &args,
2683 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002685 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686
Trond Myklebustd3468902010-04-16 16:22:50 -04002687 res.dir_attr = nfs_alloc_fattr();
2688 if (res.dir_attr == NULL)
2689 goto out;
2690
Bryan Schumaker7c513052011-03-24 17:12:24 +00002691 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002692 if (status == 0) {
2693 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002694 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002695 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002696 nfs_free_fattr(res.dir_attr);
2697out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 return status;
2699}
2700
2701static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2702{
2703 struct nfs4_exception exception = { };
2704 int err;
2705 do {
2706 err = nfs4_handle_exception(NFS_SERVER(dir),
2707 _nfs4_proc_remove(dir, name),
2708 &exception);
2709 } while (exception.retry);
2710 return err;
2711}
2712
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002713static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002715 struct nfs_server *server = NFS_SERVER(dir);
2716 struct nfs_removeargs *args = msg->rpc_argp;
2717 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718
Trond Myklebusta65318b2009-03-11 14:10:28 -04002719 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002720 res->server = server;
Benny Halevydfb4f3092010-09-24 09:17:01 -04002721 res->seq_res.sr_slot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723}
2724
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002725static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002727 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2728
Trond Myklebust14516c32010-07-31 14:29:06 -04002729 if (!nfs4_sequence_done(task, &res->seq_res))
2730 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002731 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002732 return 0;
2733 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002734 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002735 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736}
2737
Jeff Laytond3d41522010-09-17 17:31:57 -04002738static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2739{
2740 struct nfs_server *server = NFS_SERVER(dir);
2741 struct nfs_renameargs *arg = msg->rpc_argp;
2742 struct nfs_renameres *res = msg->rpc_resp;
2743
2744 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2745 arg->bitmask = server->attr_bitmask;
2746 res->server = server;
2747}
2748
2749static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2750 struct inode *new_dir)
2751{
2752 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2753
2754 if (!nfs4_sequence_done(task, &res->seq_res))
2755 return 0;
2756 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2757 return 0;
2758
2759 update_changeattr(old_dir, &res->old_cinfo);
2760 nfs_post_op_update_inode(old_dir, res->old_fattr);
2761 update_changeattr(new_dir, &res->new_cinfo);
2762 nfs_post_op_update_inode(new_dir, res->new_fattr);
2763 return 1;
2764}
2765
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2767 struct inode *new_dir, struct qstr *new_name)
2768{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002769 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002770 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 .old_dir = NFS_FH(old_dir),
2772 .new_dir = NFS_FH(new_dir),
2773 .old_name = old_name,
2774 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002775 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002777 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002778 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002779 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 struct rpc_message msg = {
2781 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2782 .rpc_argp = &arg,
2783 .rpc_resp = &res,
2784 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002785 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
Trond Myklebust011fff72010-04-16 16:22:49 -04002787 res.old_fattr = nfs_alloc_fattr();
2788 res.new_fattr = nfs_alloc_fattr();
2789 if (res.old_fattr == NULL || res.new_fattr == NULL)
2790 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791
Bryan Schumaker7c513052011-03-24 17:12:24 +00002792 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 if (!status) {
2794 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002795 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002797 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002799out:
2800 nfs_free_fattr(res.new_fattr);
2801 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 return status;
2803}
2804
2805static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2806 struct inode *new_dir, struct qstr *new_name)
2807{
2808 struct nfs4_exception exception = { };
2809 int err;
2810 do {
2811 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2812 _nfs4_proc_rename(old_dir, old_name,
2813 new_dir, new_name),
2814 &exception);
2815 } while (exception.retry);
2816 return err;
2817}
2818
2819static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2820{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002821 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 struct nfs4_link_arg arg = {
2823 .fh = NFS_FH(inode),
2824 .dir_fh = NFS_FH(dir),
2825 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002826 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002828 struct nfs4_link_res res = {
2829 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002830 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 struct rpc_message msg = {
2832 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2833 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002834 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002836 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837
Trond Myklebust136f2622010-04-16 16:22:49 -04002838 res.fattr = nfs_alloc_fattr();
2839 res.dir_attr = nfs_alloc_fattr();
2840 if (res.fattr == NULL || res.dir_attr == NULL)
2841 goto out;
2842
Bryan Schumaker7c513052011-03-24 17:12:24 +00002843 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002844 if (!status) {
2845 update_changeattr(dir, &res.cinfo);
2846 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002847 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002848 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002849out:
2850 nfs_free_fattr(res.dir_attr);
2851 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 return status;
2853}
2854
2855static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2856{
2857 struct nfs4_exception exception = { };
2858 int err;
2859 do {
2860 err = nfs4_handle_exception(NFS_SERVER(inode),
2861 _nfs4_proc_link(inode, dir, name),
2862 &exception);
2863 } while (exception.retry);
2864 return err;
2865}
2866
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002867struct nfs4_createdata {
2868 struct rpc_message msg;
2869 struct nfs4_create_arg arg;
2870 struct nfs4_create_res res;
2871 struct nfs_fh fh;
2872 struct nfs_fattr fattr;
2873 struct nfs_fattr dir_fattr;
2874};
2875
2876static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2877 struct qstr *name, struct iattr *sattr, u32 ftype)
2878{
2879 struct nfs4_createdata *data;
2880
2881 data = kzalloc(sizeof(*data), GFP_KERNEL);
2882 if (data != NULL) {
2883 struct nfs_server *server = NFS_SERVER(dir);
2884
2885 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2886 data->msg.rpc_argp = &data->arg;
2887 data->msg.rpc_resp = &data->res;
2888 data->arg.dir_fh = NFS_FH(dir);
2889 data->arg.server = server;
2890 data->arg.name = name;
2891 data->arg.attrs = sattr;
2892 data->arg.ftype = ftype;
2893 data->arg.bitmask = server->attr_bitmask;
2894 data->res.server = server;
2895 data->res.fh = &data->fh;
2896 data->res.fattr = &data->fattr;
2897 data->res.dir_fattr = &data->dir_fattr;
2898 nfs_fattr_init(data->res.fattr);
2899 nfs_fattr_init(data->res.dir_fattr);
2900 }
2901 return data;
2902}
2903
2904static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2905{
Bryan Schumaker7c513052011-03-24 17:12:24 +00002906 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00002907 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002908 if (status == 0) {
2909 update_changeattr(dir, &data->res.dir_cinfo);
2910 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2911 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2912 }
2913 return status;
2914}
2915
2916static void nfs4_free_createdata(struct nfs4_createdata *data)
2917{
2918 kfree(data);
2919}
2920
Chuck Lever4f390c12006-08-22 20:06:22 -04002921static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002922 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002924 struct nfs4_createdata *data;
2925 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926
Chuck Lever94a6d752006-08-22 20:06:23 -04002927 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002928 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002929
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002930 status = -ENOMEM;
2931 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2932 if (data == NULL)
2933 goto out;
2934
2935 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2936 data->arg.u.symlink.pages = &page;
2937 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002939 status = nfs4_do_create(dir, dentry, data);
2940
2941 nfs4_free_createdata(data);
2942out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 return status;
2944}
2945
Chuck Lever4f390c12006-08-22 20:06:22 -04002946static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002947 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948{
2949 struct nfs4_exception exception = { };
2950 int err;
2951 do {
2952 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002953 _nfs4_proc_symlink(dir, dentry, page,
2954 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 &exception);
2956 } while (exception.retry);
2957 return err;
2958}
2959
2960static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2961 struct iattr *sattr)
2962{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002963 struct nfs4_createdata *data;
2964 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002966 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2967 if (data == NULL)
2968 goto out;
2969
2970 status = nfs4_do_create(dir, dentry, data);
2971
2972 nfs4_free_createdata(data);
2973out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 return status;
2975}
2976
2977static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2978 struct iattr *sattr)
2979{
2980 struct nfs4_exception exception = { };
2981 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002982
2983 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 do {
2985 err = nfs4_handle_exception(NFS_SERVER(dir),
2986 _nfs4_proc_mkdir(dir, dentry, sattr),
2987 &exception);
2988 } while (exception.retry);
2989 return err;
2990}
2991
2992static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002993 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994{
2995 struct inode *dir = dentry->d_inode;
2996 struct nfs4_readdir_arg args = {
2997 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002998 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 .pgbase = 0,
3000 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003001 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003002 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 };
3004 struct nfs4_readdir_res res;
3005 struct rpc_message msg = {
3006 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3007 .rpc_argp = &args,
3008 .rpc_resp = &res,
3009 .rpc_cred = cred,
3010 };
3011 int status;
3012
Harvey Harrison3110ff82008-05-02 13:42:44 -07003013 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003014 dentry->d_parent->d_name.name,
3015 dentry->d_name.name,
3016 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3018 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003019 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 -05003020 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003022 status += args.pgbase;
3023 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003024
3025 nfs_invalidate_atime(dir);
3026
Harvey Harrison3110ff82008-05-02 13:42:44 -07003027 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 return status;
3029}
3030
3031static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003032 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033{
3034 struct nfs4_exception exception = { };
3035 int err;
3036 do {
3037 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3038 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003039 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 &exception);
3041 } while (exception.retry);
3042 return err;
3043}
3044
3045static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3046 struct iattr *sattr, dev_t rdev)
3047{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003048 struct nfs4_createdata *data;
3049 int mode = sattr->ia_mode;
3050 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051
3052 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3053 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003054
3055 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3056 if (data == NULL)
3057 goto out;
3058
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003060 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003062 data->arg.ftype = NF4BLK;
3063 data->arg.u.device.specdata1 = MAJOR(rdev);
3064 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 }
3066 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003067 data->arg.ftype = NF4CHR;
3068 data->arg.u.device.specdata1 = MAJOR(rdev);
3069 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003072 status = nfs4_do_create(dir, dentry, data);
3073
3074 nfs4_free_createdata(data);
3075out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 return status;
3077}
3078
3079static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3080 struct iattr *sattr, dev_t rdev)
3081{
3082 struct nfs4_exception exception = { };
3083 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003084
3085 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 do {
3087 err = nfs4_handle_exception(NFS_SERVER(dir),
3088 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3089 &exception);
3090 } while (exception.retry);
3091 return err;
3092}
3093
3094static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3095 struct nfs_fsstat *fsstat)
3096{
3097 struct nfs4_statfs_arg args = {
3098 .fh = fhandle,
3099 .bitmask = server->attr_bitmask,
3100 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003101 struct nfs4_statfs_res res = {
3102 .fsstat = fsstat,
3103 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 struct rpc_message msg = {
3105 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3106 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003107 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 };
3109
Trond Myklebust0e574af2005-10-27 22:12:38 -04003110 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003111 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112}
3113
3114static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3115{
3116 struct nfs4_exception exception = { };
3117 int err;
3118 do {
3119 err = nfs4_handle_exception(server,
3120 _nfs4_proc_statfs(server, fhandle, fsstat),
3121 &exception);
3122 } while (exception.retry);
3123 return err;
3124}
3125
3126static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3127 struct nfs_fsinfo *fsinfo)
3128{
3129 struct nfs4_fsinfo_arg args = {
3130 .fh = fhandle,
3131 .bitmask = server->attr_bitmask,
3132 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003133 struct nfs4_fsinfo_res res = {
3134 .fsinfo = fsinfo,
3135 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 struct rpc_message msg = {
3137 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3138 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003139 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 };
3141
Bryan Schumaker7c513052011-03-24 17:12:24 +00003142 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143}
3144
3145static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3146{
3147 struct nfs4_exception exception = { };
3148 int err;
3149
3150 do {
3151 err = nfs4_handle_exception(server,
3152 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3153 &exception);
3154 } while (exception.retry);
3155 return err;
3156}
3157
3158static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3159{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003160 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3162}
3163
3164static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3165 struct nfs_pathconf *pathconf)
3166{
3167 struct nfs4_pathconf_arg args = {
3168 .fh = fhandle,
3169 .bitmask = server->attr_bitmask,
3170 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003171 struct nfs4_pathconf_res res = {
3172 .pathconf = pathconf,
3173 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 struct rpc_message msg = {
3175 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3176 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003177 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 };
3179
3180 /* None of the pathconf attributes are mandatory to implement */
3181 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3182 memset(pathconf, 0, sizeof(*pathconf));
3183 return 0;
3184 }
3185
Trond Myklebust0e574af2005-10-27 22:12:38 -04003186 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003187 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188}
3189
3190static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3191 struct nfs_pathconf *pathconf)
3192{
3193 struct nfs4_exception exception = { };
3194 int err;
3195
3196 do {
3197 err = nfs4_handle_exception(server,
3198 _nfs4_proc_pathconf(server, fhandle, pathconf),
3199 &exception);
3200 } while (exception.retry);
3201 return err;
3202}
3203
Benny Halevyd20581a2011-05-22 19:52:03 +03003204void __nfs4_read_done_cb(struct nfs_read_data *data)
3205{
3206 nfs_invalidate_atime(data->inode);
3207}
3208
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003209static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210{
Trond Myklebustec06c092006-03-20 13:44:27 -05003211 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003213 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003214 nfs_restart_rpc(task, server->nfs_client);
Trond Myklebustec06c092006-03-20 13:44:27 -05003215 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003217
Benny Halevyd20581a2011-05-22 19:52:03 +03003218 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003220 renew_lease(server, data->timestamp);
3221 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222}
3223
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003224static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3225{
3226
3227 dprintk("--> %s\n", __func__);
3228
3229 if (!nfs4_sequence_done(task, &data->res.seq_res))
3230 return -EAGAIN;
3231
Benny Halevyd20581a2011-05-22 19:52:03 +03003232 return data->read_done_cb ? data->read_done_cb(task, data) :
3233 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003234}
3235
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003236static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003239 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003240 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241}
3242
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003243/* Reset the the nfs_read_data to send the read to the MDS. */
3244void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3245{
3246 dprintk("%s Reset task for i/o through\n", __func__);
3247 put_lseg(data->lseg);
3248 data->lseg = NULL;
3249 /* offsets will differ in the dense stripe case */
3250 data->args.offset = data->mds_offset;
3251 data->ds_clp = NULL;
3252 data->args.fh = NFS_FH(data->inode);
3253 data->read_done_cb = nfs4_read_done_cb;
3254 task->tk_ops = data->mds_ops;
3255 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3256}
3257EXPORT_SYMBOL_GPL(nfs4_reset_read);
3258
Fred Isamanb029bc92011-03-03 15:13:42 +00003259static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 struct inode *inode = data->inode;
3262
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003263 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003264 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003265 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003267 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003269 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003270 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003271 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272}
3273
Fred Isamanb029bc92011-03-03 15:13:42 +00003274static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3275{
3276 if (!nfs4_sequence_done(task, &data->res.seq_res))
3277 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003278 return data->write_done_cb ? data->write_done_cb(task, data) :
3279 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003280}
3281
Fred Isamana69aef12011-03-03 15:13:47 +00003282/* Reset the the nfs_write_data to send the write to the MDS. */
3283void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3284{
3285 dprintk("%s Reset task for i/o through\n", __func__);
3286 put_lseg(data->lseg);
3287 data->lseg = NULL;
3288 data->ds_clp = NULL;
3289 data->write_done_cb = nfs4_write_done_cb;
3290 data->args.fh = NFS_FH(data->inode);
3291 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3292 data->args.offset = data->mds_offset;
3293 data->res.fattr = &data->fattr;
3294 task->tk_ops = data->mds_ops;
3295 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3296}
3297EXPORT_SYMBOL_GPL(nfs4_reset_write);
3298
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003299static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003301 struct nfs_server *server = NFS_SERVER(data->inode);
3302
Fred Isaman7ffd1062011-03-03 15:13:46 +00003303 if (data->lseg) {
3304 data->args.bitmask = NULL;
3305 data->res.fattr = NULL;
3306 } else
3307 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isamanb029bc92011-03-03 15:13:42 +00003308 if (!data->write_done_cb)
3309 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003310 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 data->timestamp = jiffies;
3312
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003313 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314}
3315
Fred Isaman5f452432011-03-23 13:27:46 +00003316static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003319
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003320 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003321 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003322 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003324 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003325 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326}
3327
Fred Isaman5f452432011-03-23 13:27:46 +00003328static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3329{
3330 if (!nfs4_sequence_done(task, &data->res.seq_res))
3331 return -EAGAIN;
3332 return data->write_done_cb(task, data);
3333}
3334
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003335static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003337 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003338
3339 if (data->lseg) {
3340 data->args.bitmask = NULL;
3341 data->res.fattr = NULL;
3342 } else
3343 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isaman5f452432011-03-23 13:27:46 +00003344 if (!data->write_done_cb)
3345 data->write_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003346 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003347 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348}
3349
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003350struct nfs4_renewdata {
3351 struct nfs_client *client;
3352 unsigned long timestamp;
3353};
3354
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355/*
3356 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3357 * standalone procedure for queueing an asynchronous RENEW.
3358 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003359static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003360{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003361 struct nfs4_renewdata *data = calldata;
3362 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003363
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003364 if (atomic_read(&clp->cl_count) > 1)
3365 nfs4_schedule_state_renewal(clp);
3366 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003367 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003368}
3369
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003370static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003372 struct nfs4_renewdata *data = calldata;
3373 struct nfs_client *clp = data->client;
3374 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375
3376 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003377 /* Unless we're shutting down, schedule state recovery! */
3378 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003379 nfs4_schedule_lease_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 return;
3381 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003382 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383}
3384
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003385static const struct rpc_call_ops nfs4_renew_ops = {
3386 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003387 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003388};
3389
David Howellsadfa6f92006-08-22 20:06:08 -04003390int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391{
3392 struct rpc_message msg = {
3393 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3394 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003395 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003397 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003399 if (!atomic_inc_not_zero(&clp->cl_count))
3400 return -EIO;
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003401 data = kmalloc(sizeof(*data), GFP_KERNEL);
3402 if (data == NULL)
3403 return -ENOMEM;
3404 data->client = clp;
3405 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003407 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408}
3409
David Howellsadfa6f92006-08-22 20:06:08 -04003410int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411{
3412 struct rpc_message msg = {
3413 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3414 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003415 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 };
3417 unsigned long now = jiffies;
3418 int status;
3419
3420 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3421 if (status < 0)
3422 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003423 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 return 0;
3425}
3426
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003427static inline int nfs4_server_supports_acls(struct nfs_server *server)
3428{
3429 return (server->caps & NFS_CAP_ACLS)
3430 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3431 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3432}
3433
3434/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3435 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3436 * the stack.
3437 */
3438#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3439
3440static void buf_to_pages(const void *buf, size_t buflen,
3441 struct page **pages, unsigned int *pgbase)
3442{
3443 const void *p = buf;
3444
3445 *pgbase = offset_in_page(buf);
3446 p -= *pgbase;
3447 while (p < buf + buflen) {
3448 *(pages++) = virt_to_page(p);
3449 p += PAGE_CACHE_SIZE;
3450 }
3451}
3452
Neil Hormane9e3d722011-03-04 19:26:03 -05003453static int buf_to_pages_noslab(const void *buf, size_t buflen,
3454 struct page **pages, unsigned int *pgbase)
3455{
3456 struct page *newpage, **spages;
3457 int rc = 0;
3458 size_t len;
3459 spages = pages;
3460
3461 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003462 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003463 newpage = alloc_page(GFP_KERNEL);
3464
3465 if (newpage == NULL)
3466 goto unwind;
3467 memcpy(page_address(newpage), buf, len);
3468 buf += len;
3469 buflen -= len;
3470 *pages++ = newpage;
3471 rc++;
3472 } while (buflen != 0);
3473
3474 return rc;
3475
3476unwind:
3477 for(; rc > 0; rc--)
3478 __free_page(spages[rc-1]);
3479 return -ENOMEM;
3480}
3481
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003482struct nfs4_cached_acl {
3483 int cached;
3484 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003485 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003486};
3487
3488static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003489{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003490 struct nfs_inode *nfsi = NFS_I(inode);
3491
3492 spin_lock(&inode->i_lock);
3493 kfree(nfsi->nfs4_acl);
3494 nfsi->nfs4_acl = acl;
3495 spin_unlock(&inode->i_lock);
3496}
3497
3498static void nfs4_zap_acl_attr(struct inode *inode)
3499{
3500 nfs4_set_cached_acl(inode, NULL);
3501}
3502
3503static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3504{
3505 struct nfs_inode *nfsi = NFS_I(inode);
3506 struct nfs4_cached_acl *acl;
3507 int ret = -ENOENT;
3508
3509 spin_lock(&inode->i_lock);
3510 acl = nfsi->nfs4_acl;
3511 if (acl == NULL)
3512 goto out;
3513 if (buf == NULL) /* user is just asking for length */
3514 goto out_len;
3515 if (acl->cached == 0)
3516 goto out;
3517 ret = -ERANGE; /* see getxattr(2) man page */
3518 if (acl->len > buflen)
3519 goto out;
3520 memcpy(buf, acl->data, acl->len);
3521out_len:
3522 ret = acl->len;
3523out:
3524 spin_unlock(&inode->i_lock);
3525 return ret;
3526}
3527
3528static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3529{
3530 struct nfs4_cached_acl *acl;
3531
3532 if (buf && acl_len <= PAGE_SIZE) {
3533 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3534 if (acl == NULL)
3535 goto out;
3536 acl->cached = 1;
3537 memcpy(acl->data, buf, acl_len);
3538 } else {
3539 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3540 if (acl == NULL)
3541 goto out;
3542 acl->cached = 0;
3543 }
3544 acl->len = acl_len;
3545out:
3546 nfs4_set_cached_acl(inode, acl);
3547}
3548
Trond Myklebust16b42892006-08-24 12:27:15 -04003549static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003550{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003551 struct page *pages[NFS4ACL_MAXPAGES];
3552 struct nfs_getaclargs args = {
3553 .fh = NFS_FH(inode),
3554 .acl_pages = pages,
3555 .acl_len = buflen,
3556 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003557 struct nfs_getaclres res = {
3558 .acl_len = buflen,
3559 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003560 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003561 struct rpc_message msg = {
3562 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3563 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003564 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003565 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003566 struct page *localpage = NULL;
3567 int ret;
3568
3569 if (buflen < PAGE_SIZE) {
3570 /* As long as we're doing a round trip to the server anyway,
3571 * let's be prepared for a page of acl data. */
3572 localpage = alloc_page(GFP_KERNEL);
3573 resp_buf = page_address(localpage);
3574 if (localpage == NULL)
3575 return -ENOMEM;
3576 args.acl_pages[0] = localpage;
3577 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003578 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003579 } else {
3580 resp_buf = buf;
3581 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3582 }
Bryan Schumaker7c513052011-03-24 17:12:24 +00003583 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 +00003584 if (ret)
3585 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003586 if (res.acl_len > args.acl_len)
3587 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003588 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003589 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003590 if (buf) {
3591 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003592 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003593 goto out_free;
3594 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003595 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003596 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003597 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003598out_free:
3599 if (localpage)
3600 __free_page(localpage);
3601 return ret;
3602}
3603
Trond Myklebust16b42892006-08-24 12:27:15 -04003604static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3605{
3606 struct nfs4_exception exception = { };
3607 ssize_t ret;
3608 do {
3609 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3610 if (ret >= 0)
3611 break;
3612 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3613 } while (exception.retry);
3614 return ret;
3615}
3616
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003617static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3618{
3619 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003620 int ret;
3621
3622 if (!nfs4_server_supports_acls(server))
3623 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003624 ret = nfs_revalidate_inode(server, inode);
3625 if (ret < 0)
3626 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003627 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3628 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003629 ret = nfs4_read_cached_acl(inode, buf, buflen);
3630 if (ret != -ENOENT)
3631 return ret;
3632 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003633}
3634
Trond Myklebust16b42892006-08-24 12:27:15 -04003635static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003636{
3637 struct nfs_server *server = NFS_SERVER(inode);
3638 struct page *pages[NFS4ACL_MAXPAGES];
3639 struct nfs_setaclargs arg = {
3640 .fh = NFS_FH(inode),
3641 .acl_pages = pages,
3642 .acl_len = buflen,
3643 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003644 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003645 struct rpc_message msg = {
3646 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3647 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003648 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003649 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003650 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003651
3652 if (!nfs4_server_supports_acls(server))
3653 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003654 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3655 if (i < 0)
3656 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003657 nfs_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003658 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003659
3660 /*
3661 * Free each page after tx, so the only ref left is
3662 * held by the network stack
3663 */
3664 for (; i > 0; i--)
3665 put_page(pages[i-1]);
3666
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003667 /*
3668 * Acl update can result in inode attribute update.
3669 * so mark the attribute cache invalid.
3670 */
3671 spin_lock(&inode->i_lock);
3672 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3673 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003674 nfs_access_zap_cache(inode);
3675 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003676 return ret;
3677}
3678
Trond Myklebust16b42892006-08-24 12:27:15 -04003679static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3680{
3681 struct nfs4_exception exception = { };
3682 int err;
3683 do {
3684 err = nfs4_handle_exception(NFS_SERVER(inode),
3685 __nfs4_proc_set_acl(inode, buf, buflen),
3686 &exception);
3687 } while (exception.retry);
3688 return err;
3689}
3690
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003692nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003694 struct nfs_client *clp = server->nfs_client;
3695
3696 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 return 0;
3698 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003699 case -NFS4ERR_ADMIN_REVOKED:
3700 case -NFS4ERR_BAD_STATEID:
3701 case -NFS4ERR_OPENMODE:
3702 if (state == NULL)
3703 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003704 nfs4_schedule_stateid_recovery(server, state);
3705 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003706 case -NFS4ERR_EXPIRED:
3707 if (state != NULL)
3708 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003710 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003711 nfs4_schedule_lease_recovery(clp);
3712 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003713#if defined(CONFIG_NFS_V4_1)
3714 case -NFS4ERR_BADSESSION:
3715 case -NFS4ERR_BADSLOT:
3716 case -NFS4ERR_BAD_HIGH_SLOT:
3717 case -NFS4ERR_DEADSESSION:
3718 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3719 case -NFS4ERR_SEQ_FALSE_RETRY:
3720 case -NFS4ERR_SEQ_MISORDERED:
3721 dprintk("%s ERROR %d, Reset session\n", __func__,
3722 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003723 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -04003724 task->tk_status = 0;
3725 return -EAGAIN;
3726#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003728 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003729 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003730 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3732 task->tk_status = 0;
3733 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003734 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 case -NFS4ERR_OLD_STATEID:
3736 task->tk_status = 0;
3737 return -EAGAIN;
3738 }
3739 task->tk_status = nfs4_map_errors(task->tk_status);
3740 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003741wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003742 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003743 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3744 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3745 task->tk_status = 0;
3746 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747}
3748
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003749int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3750 unsigned short port, struct rpc_cred *cred,
3751 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752{
3753 nfs4_verifier sc_verifier;
3754 struct nfs4_setclientid setclientid = {
3755 .sc_verifier = &sc_verifier,
3756 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003757 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 };
3759 struct rpc_message msg = {
3760 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3761 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003762 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003763 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 };
Al Virobc4785c2006-10-19 23:28:51 -07003765 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 int loop = 0;
3767 int status;
3768
Al Virobc4785c2006-10-19 23:28:51 -07003769 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3771 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3772
3773 for(;;) {
3774 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003775 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003776 clp->cl_ipaddr,
3777 rpc_peeraddr2str(clp->cl_rpcclient,
3778 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003779 rpc_peeraddr2str(clp->cl_rpcclient,
3780 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003781 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 clp->cl_id_uniquifier);
3783 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003784 sizeof(setclientid.sc_netid),
3785 rpc_peeraddr2str(clp->cl_rpcclient,
3786 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003788 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 clp->cl_ipaddr, port >> 8, port & 255);
3790
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003791 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 if (status != -NFS4ERR_CLID_INUSE)
3793 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003794 if (loop != 0) {
3795 ++clp->cl_id_uniquifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003797 }
3798 ++loop;
3799 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 }
3801 return status;
3802}
3803
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003804int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003805 struct nfs4_setclientid_res *arg,
3806 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807{
3808 struct nfs_fsinfo fsinfo;
3809 struct rpc_message msg = {
3810 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003811 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003813 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 };
3815 unsigned long now;
3816 int status;
3817
3818 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003819 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 if (status == 0) {
3821 spin_lock(&clp->cl_lock);
3822 clp->cl_lease_time = fsinfo.lease_time * HZ;
3823 clp->cl_last_renewal = now;
3824 spin_unlock(&clp->cl_lock);
3825 }
3826 return status;
3827}
3828
Trond Myklebustfe650402006-01-03 09:55:18 +01003829struct nfs4_delegreturndata {
3830 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003831 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003832 struct nfs_fh fh;
3833 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003834 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003835 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003836 int rpc_status;
3837};
3838
Trond Myklebustfe650402006-01-03 09:55:18 +01003839static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3840{
3841 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003842
Trond Myklebust14516c32010-07-31 14:29:06 -04003843 if (!nfs4_sequence_done(task, &data->res.seq_res))
3844 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003845
Ricardo Labiaga79708862009-12-07 09:23:21 -05003846 switch (task->tk_status) {
3847 case -NFS4ERR_STALE_STATEID:
3848 case -NFS4ERR_EXPIRED:
3849 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003850 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003851 break;
3852 default:
3853 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3854 -EAGAIN) {
3855 nfs_restart_rpc(task, data->res.server->nfs_client);
3856 return;
3857 }
3858 }
3859 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003860}
3861
3862static void nfs4_delegreturn_release(void *calldata)
3863{
Trond Myklebustfe650402006-01-03 09:55:18 +01003864 kfree(calldata);
3865}
3866
Andy Adamson938e1012009-04-01 09:22:28 -04003867#if defined(CONFIG_NFS_V4_1)
3868static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3869{
3870 struct nfs4_delegreturndata *d_data;
3871
3872 d_data = (struct nfs4_delegreturndata *)data;
3873
Trond Myklebust035168a2010-06-16 09:52:26 -04003874 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003875 &d_data->args.seq_args,
3876 &d_data->res.seq_res, 1, task))
3877 return;
3878 rpc_call_start(task);
3879}
3880#endif /* CONFIG_NFS_V4_1 */
3881
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003882static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003883#if defined(CONFIG_NFS_V4_1)
3884 .rpc_call_prepare = nfs4_delegreturn_prepare,
3885#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003886 .rpc_call_done = nfs4_delegreturn_done,
3887 .rpc_release = nfs4_delegreturn_release,
3888};
3889
Trond Myklebuste6f81072008-01-24 18:14:34 -05003890static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003891{
3892 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003893 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003894 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003895 struct rpc_message msg = {
3896 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3897 .rpc_cred = cred,
3898 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003899 struct rpc_task_setup task_setup_data = {
3900 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003901 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003902 .callback_ops = &nfs4_delegreturn_ops,
3903 .flags = RPC_TASK_ASYNC,
3904 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003905 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003906
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003907 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003908 if (data == NULL)
3909 return -ENOMEM;
3910 data->args.fhandle = &data->fh;
3911 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003912 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003913 nfs_copy_fh(&data->fh, NFS_FH(inode));
3914 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003915 data->res.fattr = &data->fattr;
3916 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003917 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003918 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003919 data->rpc_status = 0;
3920
Trond Myklebustc970aa82007-07-14 15:39:59 -04003921 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05003922 msg.rpc_argp = &data->args;
3923 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003924 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003925 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003926 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003927 if (!issync)
3928 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003929 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003930 if (status != 0)
3931 goto out;
3932 status = data->rpc_status;
3933 if (status != 0)
3934 goto out;
3935 nfs_refresh_inode(inode, &data->fattr);
3936out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003937 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003938 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939}
3940
Trond Myklebuste6f81072008-01-24 18:14:34 -05003941int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942{
3943 struct nfs_server *server = NFS_SERVER(inode);
3944 struct nfs4_exception exception = { };
3945 int err;
3946 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003947 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 switch (err) {
3949 case -NFS4ERR_STALE_STATEID:
3950 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 case 0:
3952 return 0;
3953 }
3954 err = nfs4_handle_exception(server, err, &exception);
3955 } while (exception.retry);
3956 return err;
3957}
3958
3959#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3960#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3961
3962/*
3963 * sleep, with exponential backoff, and retry the LOCK operation.
3964 */
3965static unsigned long
3966nfs4_set_lock_task_retry(unsigned long timeout)
3967{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003968 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 timeout <<= 1;
3970 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3971 return NFS4_LOCK_MAXTIMEOUT;
3972 return timeout;
3973}
3974
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3976{
3977 struct inode *inode = state->inode;
3978 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003979 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003980 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003982 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003984 struct nfs_lockt_res res = {
3985 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 };
3987 struct rpc_message msg = {
3988 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3989 .rpc_argp = &arg,
3990 .rpc_resp = &res,
3991 .rpc_cred = state->owner->so_cred,
3992 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 struct nfs4_lock_state *lsp;
3994 int status;
3995
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003996 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003997 status = nfs4_set_lock_state(state, request);
3998 if (status != 0)
3999 goto out;
4000 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004001 arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004002 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004003 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004004 switch (status) {
4005 case 0:
4006 request->fl_type = F_UNLCK;
4007 break;
4008 case -NFS4ERR_DENIED:
4009 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004011 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004012out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 return status;
4014}
4015
4016static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4017{
4018 struct nfs4_exception exception = { };
4019 int err;
4020
4021 do {
4022 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4023 _nfs4_proc_getlk(state, cmd, request),
4024 &exception);
4025 } while (exception.retry);
4026 return err;
4027}
4028
4029static int do_vfs_lock(struct file *file, struct file_lock *fl)
4030{
4031 int res = 0;
4032 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4033 case FL_POSIX:
4034 res = posix_lock_file_wait(file, fl);
4035 break;
4036 case FL_FLOCK:
4037 res = flock_lock_file_wait(file, fl);
4038 break;
4039 default:
4040 BUG();
4041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 return res;
4043}
4044
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004045struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004046 struct nfs_locku_args arg;
4047 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004048 struct nfs4_lock_state *lsp;
4049 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004050 struct file_lock fl;
4051 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004052 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004053};
4054
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004055static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4056 struct nfs_open_context *ctx,
4057 struct nfs4_lock_state *lsp,
4058 struct nfs_seqid *seqid)
4059{
4060 struct nfs4_unlockdata *p;
4061 struct inode *inode = lsp->ls_state->inode;
4062
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004063 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004064 if (p == NULL)
4065 return NULL;
4066 p->arg.fh = NFS_FH(inode);
4067 p->arg.fl = &p->fl;
4068 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004069 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004070 p->arg.stateid = &lsp->ls_stateid;
4071 p->lsp = lsp;
4072 atomic_inc(&lsp->ls_count);
4073 /* Ensure we don't close file until we're done freeing locks! */
4074 p->ctx = get_nfs_open_context(ctx);
4075 memcpy(&p->fl, fl, sizeof(p->fl));
4076 p->server = NFS_SERVER(inode);
4077 return p;
4078}
4079
Trond Myklebust06f814a2006-01-03 09:55:07 +01004080static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004081{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004082 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004083 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004084 nfs4_put_lock_state(calldata->lsp);
4085 put_nfs_open_context(calldata->ctx);
4086 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004087}
4088
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004089static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004090{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004091 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004092
Trond Myklebust14516c32010-07-31 14:29:06 -04004093 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4094 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004095 switch (task->tk_status) {
4096 case 0:
4097 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004098 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004099 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01004100 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004101 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004102 case -NFS4ERR_BAD_STATEID:
4103 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004104 case -NFS4ERR_STALE_STATEID:
4105 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004106 break;
4107 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004108 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebust0110ee12009-12-07 09:00:24 -05004109 nfs_restart_rpc(task,
Trond Myklebustd61e6122009-12-05 19:32:19 -05004110 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004111 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004112}
4113
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004114static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004115{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004116 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004118 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004119 return;
4120 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004121 /* Note: exit _without_ running nfs4_locku_done */
4122 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004123 return;
4124 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004125 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004126 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004127 &calldata->arg.seq_args,
4128 &calldata->res.seq_res, 1, task))
4129 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004130 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131}
4132
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004133static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004134 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004135 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004136 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004137};
4138
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004139static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4140 struct nfs_open_context *ctx,
4141 struct nfs4_lock_state *lsp,
4142 struct nfs_seqid *seqid)
4143{
4144 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004145 struct rpc_message msg = {
4146 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4147 .rpc_cred = ctx->cred,
4148 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004149 struct rpc_task_setup task_setup_data = {
4150 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004151 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004152 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004153 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004154 .flags = RPC_TASK_ASYNC,
4155 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004156
Frank Filz137d6ac2007-07-09 15:32:29 -07004157 /* Ensure this is an unlock - when canceling a lock, the
4158 * canceled lock is passed in, and it won't be an unlock.
4159 */
4160 fl->fl_type = F_UNLCK;
4161
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004162 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4163 if (data == NULL) {
4164 nfs_free_seqid(seqid);
4165 return ERR_PTR(-ENOMEM);
4166 }
4167
Trond Myklebust1174dd12010-12-21 10:52:24 -05004168 msg.rpc_argp = &data->arg;
4169 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004170 task_setup_data.callback_data = data;
4171 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004172}
4173
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4175{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004176 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004177 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004178 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004179 struct rpc_task *task;
4180 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004181 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182
Trond Myklebust9b073572006-06-29 16:38:34 -04004183 status = nfs4_set_lock_state(state, request);
4184 /* Unlock _before_ we do the RPC call */
4185 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004186 down_read(&nfsi->rwsem);
4187 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4188 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004189 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004190 }
4191 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004192 if (status != 0)
4193 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004194 /* Is this a delegated lock? */
4195 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004196 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004197 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004198 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004199 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004200 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004201 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004202 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004203 status = PTR_ERR(task);
4204 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004205 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004206 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004207 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004208out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004209 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004210 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211}
4212
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004213struct nfs4_lockdata {
4214 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004215 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004216 struct nfs4_lock_state *lsp;
4217 struct nfs_open_context *ctx;
4218 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004219 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004220 int rpc_status;
4221 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004222 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004223};
4224
4225static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004226 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4227 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004228{
4229 struct nfs4_lockdata *p;
4230 struct inode *inode = lsp->ls_state->inode;
4231 struct nfs_server *server = NFS_SERVER(inode);
4232
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004233 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004234 if (p == NULL)
4235 return NULL;
4236
4237 p->arg.fh = NFS_FH(inode);
4238 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004239 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004240 if (p->arg.open_seqid == NULL)
4241 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004242 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004243 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004244 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004245 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004246 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004247 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004248 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004249 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004250 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004251 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004252 atomic_inc(&lsp->ls_count);
4253 p->ctx = get_nfs_open_context(ctx);
4254 memcpy(&p->fl, fl, sizeof(p->fl));
4255 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004256out_free_seqid:
4257 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004258out_free:
4259 kfree(p);
4260 return NULL;
4261}
4262
4263static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4264{
4265 struct nfs4_lockdata *data = calldata;
4266 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267
Harvey Harrison3110ff82008-05-02 13:42:44 -07004268 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004269 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4270 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004271 /* Do we need to do an open_to_lock_owner? */
4272 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004273 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4274 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004275 data->arg.open_stateid = &state->stateid;
4276 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004277 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004278 } else
4279 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004280 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004281 if (nfs4_setup_sequence(data->server,
4282 &data->arg.seq_args,
Andy Adamson66179ef2009-04-01 09:22:22 -04004283 &data->res.seq_res, 1, task))
4284 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004285 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004286 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004287}
4288
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004289static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4290{
4291 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4292 nfs4_lock_prepare(task, calldata);
4293}
4294
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004295static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4296{
4297 struct nfs4_lockdata *data = calldata;
4298
Harvey Harrison3110ff82008-05-02 13:42:44 -07004299 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004300
Trond Myklebust14516c32010-07-31 14:29:06 -04004301 if (!nfs4_sequence_done(task, &data->res.seq_res))
4302 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004303
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004304 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004305 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004306 if (data->rpc_status == 0)
4307 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4308 else
4309 goto out;
4310 }
4311 if (data->rpc_status == 0) {
4312 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4313 sizeof(data->lsp->ls_stateid.data));
4314 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04004315 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004316 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004317out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004318 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004319}
4320
4321static void nfs4_lock_release(void *calldata)
4322{
4323 struct nfs4_lockdata *data = calldata;
4324
Harvey Harrison3110ff82008-05-02 13:42:44 -07004325 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004326 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004327 if (data->cancelled != 0) {
4328 struct rpc_task *task;
4329 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4330 data->arg.lock_seqid);
4331 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004332 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004333 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004334 } else
4335 nfs_free_seqid(data->arg.lock_seqid);
4336 nfs4_put_lock_state(data->lsp);
4337 put_nfs_open_context(data->ctx);
4338 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004339 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004340}
4341
4342static const struct rpc_call_ops nfs4_lock_ops = {
4343 .rpc_call_prepare = nfs4_lock_prepare,
4344 .rpc_call_done = nfs4_lock_done,
4345 .rpc_release = nfs4_lock_release,
4346};
4347
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004348static const struct rpc_call_ops nfs4_recover_lock_ops = {
4349 .rpc_call_prepare = nfs4_recover_lock_prepare,
4350 .rpc_call_done = nfs4_lock_done,
4351 .rpc_release = nfs4_lock_release,
4352};
4353
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004354static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4355{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004356 switch (error) {
4357 case -NFS4ERR_ADMIN_REVOKED:
4358 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004359 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004360 if (new_lock_owner != 0 ||
4361 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004362 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004363 break;
4364 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004365 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004366 case -NFS4ERR_EXPIRED:
4367 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004368 };
4369}
4370
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004371static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004372{
4373 struct nfs4_lockdata *data;
4374 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004375 struct rpc_message msg = {
4376 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4377 .rpc_cred = state->owner->so_cred,
4378 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004379 struct rpc_task_setup task_setup_data = {
4380 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004381 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004382 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004383 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004384 .flags = RPC_TASK_ASYNC,
4385 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004386 int ret;
4387
Harvey Harrison3110ff82008-05-02 13:42:44 -07004388 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004389 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004390 fl->fl_u.nfs4_fl.owner,
4391 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004392 if (data == NULL)
4393 return -ENOMEM;
4394 if (IS_SETLKW(cmd))
4395 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004396 if (recovery_type > NFS_LOCK_NEW) {
4397 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004398 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004399 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4400 }
Trond Myklebust1174dd12010-12-21 10:52:24 -05004401 msg.rpc_argp = &data->arg;
4402 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004403 task_setup_data.callback_data = data;
4404 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004405 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004406 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004407 ret = nfs4_wait_for_completion_rpc_task(task);
4408 if (ret == 0) {
4409 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004410 if (ret)
4411 nfs4_handle_setlk_error(data->server, data->lsp,
4412 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004413 } else
4414 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004415 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004416 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004417 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418}
4419
4420static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4421{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004422 struct nfs_server *server = NFS_SERVER(state->inode);
4423 struct nfs4_exception exception = { };
4424 int err;
4425
4426 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004427 /* Cache the lock if possible... */
4428 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4429 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004430 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004431 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004432 break;
4433 nfs4_handle_exception(server, err, &exception);
4434 } while (exception.retry);
4435 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436}
4437
4438static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4439{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004440 struct nfs_server *server = NFS_SERVER(state->inode);
4441 struct nfs4_exception exception = { };
4442 int err;
4443
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004444 err = nfs4_set_lock_state(state, request);
4445 if (err != 0)
4446 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004447 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004448 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4449 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004450 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004451 switch (err) {
4452 default:
4453 goto out;
4454 case -NFS4ERR_GRACE:
4455 case -NFS4ERR_DELAY:
4456 nfs4_handle_exception(server, err, &exception);
4457 err = 0;
4458 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004459 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004460out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004461 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462}
4463
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004464#if defined(CONFIG_NFS_V4_1)
4465static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4466{
4467 int status;
4468 struct nfs_server *server = NFS_SERVER(state->inode);
4469
4470 status = nfs41_test_stateid(server, state);
4471 if (status == NFS_OK)
4472 return 0;
4473 nfs41_free_stateid(server, state);
4474 return nfs4_lock_expired(state, request);
4475}
4476#endif
4477
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4479{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004480 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004481 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004482 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004484 if ((fl_flags & FL_POSIX) &&
4485 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4486 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004487 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004488 status = nfs4_set_lock_state(state, request);
4489 if (status != 0)
4490 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004491 request->fl_flags |= FL_ACCESS;
4492 status = do_vfs_lock(request->fl_file, request);
4493 if (status < 0)
4494 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004495 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004496 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004497 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004498 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004499 request->fl_flags = fl_flags & ~FL_SLEEP;
4500 status = do_vfs_lock(request->fl_file, request);
4501 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004502 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004503 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004504 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004505 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004506 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004507 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004508 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004509 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004510out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004511 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004512out:
4513 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 return status;
4515}
4516
4517static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4518{
4519 struct nfs4_exception exception = { };
4520 int err;
4521
4522 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004523 err = _nfs4_proc_setlk(state, cmd, request);
4524 if (err == -NFS4ERR_DENIED)
4525 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004527 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 } while (exception.retry);
4529 return err;
4530}
4531
4532static int
4533nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4534{
4535 struct nfs_open_context *ctx;
4536 struct nfs4_state *state;
4537 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4538 int status;
4539
4540 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004541 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 state = ctx->state;
4543
4544 if (request->fl_start < 0 || request->fl_end < 0)
4545 return -EINVAL;
4546
Trond Myklebustd9531262009-07-21 19:22:38 -04004547 if (IS_GETLK(cmd)) {
4548 if (state != NULL)
4549 return nfs4_proc_getlk(state, F_GETLK, request);
4550 return 0;
4551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552
4553 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4554 return -EINVAL;
4555
Trond Myklebustd9531262009-07-21 19:22:38 -04004556 if (request->fl_type == F_UNLCK) {
4557 if (state != NULL)
4558 return nfs4_proc_unlck(state, cmd, request);
4559 return 0;
4560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561
Trond Myklebustd9531262009-07-21 19:22:38 -04004562 if (state == NULL)
4563 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 do {
4565 status = nfs4_proc_setlk(state, cmd, request);
4566 if ((status != -EAGAIN) || IS_SETLK(cmd))
4567 break;
4568 timeout = nfs4_set_lock_task_retry(timeout);
4569 status = -ERESTARTSYS;
4570 if (signalled())
4571 break;
4572 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573 return status;
4574}
4575
Trond Myklebust888e6942005-11-04 15:38:11 -05004576int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4577{
4578 struct nfs_server *server = NFS_SERVER(state->inode);
4579 struct nfs4_exception exception = { };
4580 int err;
4581
4582 err = nfs4_set_lock_state(state, fl);
4583 if (err != 0)
4584 goto out;
4585 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004586 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004587 switch (err) {
4588 default:
4589 printk(KERN_ERR "%s: unhandled error %d.\n",
4590 __func__, err);
4591 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004592 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004593 goto out;
4594 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004595 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004596 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004597 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004598 nfs4_schedule_lease_recovery(server->nfs_client);
4599 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004600 case -NFS4ERR_BADSESSION:
4601 case -NFS4ERR_BADSLOT:
4602 case -NFS4ERR_BAD_HIGH_SLOT:
4603 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4604 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004605 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004606 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004607 case -ERESTARTSYS:
4608 /*
4609 * The show must go on: exit, but mark the
4610 * stateid as needing recovery.
4611 */
4612 case -NFS4ERR_ADMIN_REVOKED:
4613 case -NFS4ERR_BAD_STATEID:
4614 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004615 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004616 err = 0;
4617 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004618 case -EKEYEXPIRED:
4619 /*
4620 * User RPCSEC_GSS context has expired.
4621 * We cannot recover this stateid now, so
4622 * skip it and allow recovery thread to
4623 * proceed.
4624 */
4625 err = 0;
4626 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004627 case -ENOMEM:
4628 case -NFS4ERR_DENIED:
4629 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4630 err = 0;
4631 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004632 case -NFS4ERR_DELAY:
4633 break;
4634 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004635 err = nfs4_handle_exception(server, err, &exception);
4636 } while (exception.retry);
4637out:
4638 return err;
4639}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004640
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004641static void nfs4_release_lockowner_release(void *calldata)
4642{
4643 kfree(calldata);
4644}
4645
4646const struct rpc_call_ops nfs4_release_lockowner_ops = {
4647 .rpc_release = nfs4_release_lockowner_release,
4648};
4649
4650void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4651{
4652 struct nfs_server *server = lsp->ls_state->owner->so_server;
4653 struct nfs_release_lockowner_args *args;
4654 struct rpc_message msg = {
4655 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4656 };
4657
4658 if (server->nfs_client->cl_mvops->minor_version != 0)
4659 return;
4660 args = kmalloc(sizeof(*args), GFP_NOFS);
4661 if (!args)
4662 return;
4663 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4664 args->lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004665 args->lock_owner.s_dev = server->s_dev;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004666 msg.rpc_argp = args;
4667 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4668}
4669
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004670#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4671
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004672static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4673 const void *buf, size_t buflen,
4674 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004675{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004676 if (strcmp(key, "") != 0)
4677 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004678
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004679 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004680}
4681
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004682static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4683 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004684{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004685 if (strcmp(key, "") != 0)
4686 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004687
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004688 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004689}
4690
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004691static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4692 size_t list_len, const char *name,
4693 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004694{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004695 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004696
J. Bruce Fields096455a2006-03-20 23:23:42 -05004697 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4698 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004699
4700 if (list && len <= list_len)
4701 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004702 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004703}
4704
Andy Adamson533eb462011-06-13 18:25:56 -04004705/*
4706 * nfs_fhget will use either the mounted_on_fileid or the fileid
4707 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004708static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4709{
Andy Adamson533eb462011-06-13 18:25:56 -04004710 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4711 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4712 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4713 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004714 return;
4715
4716 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4717 NFS_ATTR_FATTR_NLINK;
4718 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4719 fattr->nlink = 2;
4720}
4721
Trond Myklebust56659e92007-07-17 21:52:39 -04004722int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004723 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004724{
4725 struct nfs_server *server = NFS_SERVER(dir);
4726 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004727 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004728 };
4729 struct nfs4_fs_locations_arg args = {
4730 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004731 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004732 .page = page,
4733 .bitmask = bitmask,
4734 };
Benny Halevy22958462009-04-01 09:22:02 -04004735 struct nfs4_fs_locations_res res = {
4736 .fs_locations = fs_locations,
4737 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004738 struct rpc_message msg = {
4739 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4740 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004741 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004742 };
4743 int status;
4744
Harvey Harrison3110ff82008-05-02 13:42:44 -07004745 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04004746
4747 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
4748 * is not supported */
4749 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
4750 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
4751 else
4752 bitmask[0] |= FATTR4_WORD0_FILEID;
4753
Trond Myklebustc228fd32007-01-13 02:28:11 -05004754 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004755 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004756 fs_locations->nlocations = 0;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004757 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004758 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004759 return status;
4760}
4761
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00004762static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4763{
4764 int status;
4765 struct nfs4_secinfo_arg args = {
4766 .dir_fh = NFS_FH(dir),
4767 .name = name,
4768 };
4769 struct nfs4_secinfo_res res = {
4770 .flavors = flavors,
4771 };
4772 struct rpc_message msg = {
4773 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
4774 .rpc_argp = &args,
4775 .rpc_resp = &res,
4776 };
4777
4778 dprintk("NFS call secinfo %s\n", name->name);
4779 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4780 dprintk("NFS reply secinfo: %d\n", status);
4781 return status;
4782}
4783
4784int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4785{
4786 struct nfs4_exception exception = { };
4787 int err;
4788 do {
4789 err = nfs4_handle_exception(NFS_SERVER(dir),
4790 _nfs4_proc_secinfo(dir, name, flavors),
4791 &exception);
4792 } while (exception.retry);
4793 return err;
4794}
4795
Andy Adamson557134a2009-04-01 09:21:53 -04004796#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004797/*
Andy Adamson357f54d2010-12-14 10:11:57 -05004798 * Check the exchange flags returned by the server for invalid flags, having
4799 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4800 * DS flags set.
4801 */
4802static int nfs4_check_cl_exchange_flags(u32 flags)
4803{
4804 if (flags & ~EXCHGID4_FLAG_MASK_R)
4805 goto out_inval;
4806 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4807 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4808 goto out_inval;
4809 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4810 goto out_inval;
4811 return NFS_OK;
4812out_inval:
4813 return -NFS4ERR_INVAL;
4814}
4815
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004816static bool
4817nfs41_same_server_scope(struct server_scope *a, struct server_scope *b)
4818{
4819 if (a->server_scope_sz == b->server_scope_sz &&
4820 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
4821 return true;
4822
4823 return false;
4824}
4825
Andy Adamson357f54d2010-12-14 10:11:57 -05004826/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04004827 * nfs4_proc_exchange_id()
4828 *
4829 * Since the clientid has expired, all compounds using sessions
4830 * associated with the stale clientid will be returning
4831 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4832 * be in some phase of session reset.
4833 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004834int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004835{
4836 nfs4_verifier verifier;
4837 struct nfs41_exchange_id_args args = {
4838 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05004839 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04004840 };
4841 struct nfs41_exchange_id_res res = {
4842 .client = clp,
4843 };
4844 int status;
4845 struct rpc_message msg = {
4846 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4847 .rpc_argp = &args,
4848 .rpc_resp = &res,
4849 .rpc_cred = cred,
4850 };
4851 __be32 *p;
4852
4853 dprintk("--> %s\n", __func__);
4854 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004855
Benny Halevy99fe60d2009-04-01 09:22:29 -04004856 p = (u32 *)verifier.data;
4857 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4858 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4859 args.verifier = &verifier;
4860
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004861 args.id_len = scnprintf(args.id, sizeof(args.id),
4862 "%s/%s.%s/%u",
4863 clp->cl_ipaddr,
4864 init_utsname()->nodename,
4865 init_utsname()->domainname,
4866 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004867
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004868 res.server_scope = kzalloc(sizeof(struct server_scope), GFP_KERNEL);
4869 if (unlikely(!res.server_scope))
4870 return -ENOMEM;
4871
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004872 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004873 if (!status)
4874 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004875
4876 if (!status) {
4877 if (clp->server_scope &&
4878 !nfs41_same_server_scope(clp->server_scope,
4879 res.server_scope)) {
4880 dprintk("%s: server_scope mismatch detected\n",
4881 __func__);
4882 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
4883 kfree(clp->server_scope);
4884 clp->server_scope = NULL;
4885 }
4886
4887 if (!clp->server_scope)
4888 clp->server_scope = res.server_scope;
4889 else
4890 kfree(res.server_scope);
4891 }
4892
Benny Halevy99fe60d2009-04-01 09:22:29 -04004893 dprintk("<-- %s status= %d\n", __func__, status);
4894 return status;
4895}
4896
Andy Adamson2050f0c2009-04-01 09:22:30 -04004897struct nfs4_get_lease_time_data {
4898 struct nfs4_get_lease_time_args *args;
4899 struct nfs4_get_lease_time_res *res;
4900 struct nfs_client *clp;
4901};
4902
4903static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4904 void *calldata)
4905{
4906 int ret;
4907 struct nfs4_get_lease_time_data *data =
4908 (struct nfs4_get_lease_time_data *)calldata;
4909
4910 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004911 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004912 /* just setup sequence, do not trigger session recovery
4913 since we're invoked within one */
4914 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004915 &data->args->la_seq_args,
4916 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004917
4918 BUG_ON(ret == -EAGAIN);
4919 rpc_call_start(task);
4920 dprintk("<-- %s\n", __func__);
4921}
4922
4923/*
4924 * Called from nfs4_state_manager thread for session setup, so don't recover
4925 * from sequence operation or clientid errors.
4926 */
4927static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4928{
4929 struct nfs4_get_lease_time_data *data =
4930 (struct nfs4_get_lease_time_data *)calldata;
4931
4932 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04004933 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4934 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04004935 switch (task->tk_status) {
4936 case -NFS4ERR_DELAY:
4937 case -NFS4ERR_GRACE:
4938 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4939 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4940 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004941 /* fall through */
4942 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust0110ee12009-12-07 09:00:24 -05004943 nfs_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004944 return;
4945 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004946 dprintk("<-- %s\n", __func__);
4947}
4948
4949struct rpc_call_ops nfs4_get_lease_time_ops = {
4950 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4951 .rpc_call_done = nfs4_get_lease_time_done,
4952};
4953
4954int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4955{
4956 struct rpc_task *task;
4957 struct nfs4_get_lease_time_args args;
4958 struct nfs4_get_lease_time_res res = {
4959 .lr_fsinfo = fsinfo,
4960 };
4961 struct nfs4_get_lease_time_data data = {
4962 .args = &args,
4963 .res = &res,
4964 .clp = clp,
4965 };
4966 struct rpc_message msg = {
4967 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4968 .rpc_argp = &args,
4969 .rpc_resp = &res,
4970 };
4971 struct rpc_task_setup task_setup = {
4972 .rpc_client = clp->cl_rpcclient,
4973 .rpc_message = &msg,
4974 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004975 .callback_data = &data,
4976 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04004977 };
4978 int status;
4979
Andy Adamson2050f0c2009-04-01 09:22:30 -04004980 dprintk("--> %s\n", __func__);
4981 task = rpc_run_task(&task_setup);
4982
4983 if (IS_ERR(task))
4984 status = PTR_ERR(task);
4985 else {
4986 status = task->tk_status;
4987 rpc_put_task(task);
4988 }
4989 dprintk("<-- %s return %d\n", __func__, status);
4990
4991 return status;
4992}
4993
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004994/*
4995 * Reset a slot table
4996 */
Andy Adamson104aeba2010-01-14 17:45:10 -05004997static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4998 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004999{
Andy Adamson104aeba2010-01-14 17:45:10 -05005000 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005001 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005002 int ret = 0;
5003
Andy Adamson104aeba2010-01-14 17:45:10 -05005004 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5005 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005006
Andy Adamson104aeba2010-01-14 17:45:10 -05005007 /* Does the newly negotiated max_reqs match the existing slot table? */
5008 if (max_reqs != tbl->max_slots) {
5009 ret = -ENOMEM;
5010 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005011 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005012 if (!new)
5013 goto out;
5014 ret = 0;
5015 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005016 }
5017 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05005018 if (new) {
5019 tbl->slots = new;
5020 tbl->max_slots = max_reqs;
5021 }
5022 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005023 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005024 spin_unlock(&tbl->slot_tbl_lock);
5025 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5026 tbl, tbl->slots, tbl->max_slots);
5027out:
5028 dprintk("<-- %s: return %d\n", __func__, ret);
5029 return ret;
5030}
5031
Andy Adamsonfc931582009-04-01 09:22:31 -04005032/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005033 * Reset the forechannel and backchannel slot tables
5034 */
5035static int nfs4_reset_slot_tables(struct nfs4_session *session)
5036{
5037 int status;
5038
5039 status = nfs4_reset_slot_table(&session->fc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05005040 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005041 if (status)
5042 return status;
5043
5044 status = nfs4_reset_slot_table(&session->bc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05005045 session->bc_attrs.max_reqs, 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005046 return status;
5047}
5048
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005049/* Destroy the slot table */
5050static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5051{
5052 if (session->fc_slot_table.slots != NULL) {
5053 kfree(session->fc_slot_table.slots);
5054 session->fc_slot_table.slots = NULL;
5055 }
5056 if (session->bc_slot_table.slots != NULL) {
5057 kfree(session->bc_slot_table.slots);
5058 session->bc_slot_table.slots = NULL;
5059 }
5060 return;
5061}
5062
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005063/*
Andy Adamsonfc931582009-04-01 09:22:31 -04005064 * Initialize slot table
5065 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005066static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
5067 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04005068{
Andy Adamsonfc931582009-04-01 09:22:31 -04005069 struct nfs4_slot *slot;
5070 int ret = -ENOMEM;
5071
5072 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
5073
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005074 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04005075
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005076 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04005077 if (!slot)
5078 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04005079 ret = 0;
5080
5081 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04005082 tbl->max_slots = max_slots;
5083 tbl->slots = slot;
5084 tbl->highest_used_slotid = -1; /* no slot is currently used */
5085 spin_unlock(&tbl->slot_tbl_lock);
5086 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5087 tbl, tbl->slots, tbl->max_slots);
5088out:
5089 dprintk("<-- %s: return %d\n", __func__, ret);
5090 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04005091}
5092
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005093/*
5094 * Initialize the forechannel and backchannel tables
5095 */
5096static int nfs4_init_slot_tables(struct nfs4_session *session)
5097{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005098 struct nfs4_slot_table *tbl;
5099 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005100
Trond Myklebustf26468f2009-12-05 19:32:11 -05005101 tbl = &session->fc_slot_table;
5102 if (tbl->slots == NULL) {
5103 status = nfs4_init_slot_table(tbl,
5104 session->fc_attrs.max_reqs, 1);
5105 if (status)
5106 return status;
5107 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005108
Trond Myklebustf26468f2009-12-05 19:32:11 -05005109 tbl = &session->bc_slot_table;
5110 if (tbl->slots == NULL) {
5111 status = nfs4_init_slot_table(tbl,
5112 session->bc_attrs.max_reqs, 0);
5113 if (status)
5114 nfs4_destroy_slot_tables(session);
5115 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005116
5117 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005118}
5119
5120struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5121{
5122 struct nfs4_session *session;
5123 struct nfs4_slot_table *tbl;
5124
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005125 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005126 if (!session)
5127 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005128
Andy Adamson557134a2009-04-01 09:21:53 -04005129 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05005130 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04005131 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005132 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005133 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005134
5135 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05005136 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005137 spin_lock_init(&tbl->slot_tbl_lock);
5138 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005139 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005140
Trond Myklebust1055d762010-06-16 09:52:27 -04005141 session->session_state = 1<<NFS4_SESSION_INITING;
5142
Andy Adamson557134a2009-04-01 09:21:53 -04005143 session->clp = clp;
5144 return session;
5145}
5146
5147void nfs4_destroy_session(struct nfs4_session *session)
5148{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005149 nfs4_proc_destroy_session(session);
5150 dprintk("%s Destroy backchannel for xprt %p\n",
5151 __func__, session->clp->cl_rpcclient->cl_xprt);
5152 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
5153 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005154 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005155 kfree(session);
5156}
5157
Andy Adamsonfc931582009-04-01 09:22:31 -04005158/*
5159 * Initialize the values to be used by the client in CREATE_SESSION
5160 * If nfs4_init_session set the fore channel request and response sizes,
5161 * use them.
5162 *
5163 * Set the back channel max_resp_sz_cached to zero to force the client to
5164 * always set csa_cachethis to FALSE because the current implementation
5165 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5166 */
5167static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5168{
5169 struct nfs4_session *session = args->client->cl_session;
5170 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5171 mxresp_sz = session->fc_attrs.max_resp_sz;
5172
5173 if (mxrqst_sz == 0)
5174 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5175 if (mxresp_sz == 0)
5176 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5177 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005178 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5179 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005180 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5181 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
5182
5183 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005184 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005185 __func__,
5186 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005187 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005188
5189 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005190 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5191 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5192 args->bc_attrs.max_resp_sz_cached = 0;
5193 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5194 args->bc_attrs.max_reqs = 1;
5195
5196 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5197 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5198 __func__,
5199 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5200 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5201 args->bc_attrs.max_reqs);
5202}
5203
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005204static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005205{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005206 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5207 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5208
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005209 if (rcvd->max_resp_sz > sent->max_resp_sz)
5210 return -EINVAL;
5211 /*
5212 * Our requested max_ops is the minimum we need; we're not
5213 * prepared to break up compounds into smaller pieces than that.
5214 * So, no point even trying to continue if the server won't
5215 * cooperate:
5216 */
5217 if (rcvd->max_ops < sent->max_ops)
5218 return -EINVAL;
5219 if (rcvd->max_reqs == 0)
5220 return -EINVAL;
5221 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005222}
5223
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005224static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5225{
5226 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5227 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005228
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005229 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5230 return -EINVAL;
5231 if (rcvd->max_resp_sz < sent->max_resp_sz)
5232 return -EINVAL;
5233 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5234 return -EINVAL;
5235 /* These would render the backchannel useless: */
5236 if (rcvd->max_ops == 0)
5237 return -EINVAL;
5238 if (rcvd->max_reqs == 0)
5239 return -EINVAL;
5240 return 0;
5241}
Andy Adamson8d353012009-04-01 09:22:32 -04005242
Andy Adamson8d353012009-04-01 09:22:32 -04005243static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5244 struct nfs4_session *session)
5245{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005246 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005247
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005248 ret = nfs4_verify_fore_channel_attrs(args, session);
5249 if (ret)
5250 return ret;
5251 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005252}
5253
Andy Adamsonfc931582009-04-01 09:22:31 -04005254static int _nfs4_proc_create_session(struct nfs_client *clp)
5255{
5256 struct nfs4_session *session = clp->cl_session;
5257 struct nfs41_create_session_args args = {
5258 .client = clp,
5259 .cb_program = NFS4_CALLBACK,
5260 };
5261 struct nfs41_create_session_res res = {
5262 .client = clp,
5263 };
5264 struct rpc_message msg = {
5265 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5266 .rpc_argp = &args,
5267 .rpc_resp = &res,
5268 };
5269 int status;
5270
5271 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005272 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005273
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005274 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005275
Andy Adamson8d353012009-04-01 09:22:32 -04005276 if (!status)
5277 /* Verify the session's negotiated channel_attrs values */
5278 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005279 if (!status) {
5280 /* Increment the clientid slot sequence id */
5281 clp->cl_seqid++;
5282 }
5283
5284 return status;
5285}
5286
5287/*
5288 * Issues a CREATE_SESSION operation to the server.
5289 * It is the responsibility of the caller to verify the session is
5290 * expired before calling this routine.
5291 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005292int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005293{
5294 int status;
5295 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005296 struct nfs4_session *session = clp->cl_session;
5297
5298 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5299
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005300 status = _nfs4_proc_create_session(clp);
Andy Adamsonfc931582009-04-01 09:22:31 -04005301 if (status)
5302 goto out;
5303
Trond Myklebustf26468f2009-12-05 19:32:11 -05005304 /* Init and reset the fore channel */
5305 status = nfs4_init_slot_tables(session);
5306 dprintk("slot table initialization returned %d\n", status);
5307 if (status)
5308 goto out;
5309 status = nfs4_reset_slot_tables(session);
5310 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005311 if (status)
5312 goto out;
5313
5314 ptr = (unsigned *)&session->sess_id.data[0];
5315 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5316 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005317out:
5318 dprintk("<-- %s\n", __func__);
5319 return status;
5320}
5321
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005322/*
5323 * Issue the over-the-wire RPC DESTROY_SESSION.
5324 * The caller must serialize access to this routine.
5325 */
5326int nfs4_proc_destroy_session(struct nfs4_session *session)
5327{
5328 int status = 0;
5329 struct rpc_message msg;
5330
5331 dprintk("--> nfs4_proc_destroy_session\n");
5332
5333 /* session is still being setup */
5334 if (session->clp->cl_cons_state != NFS_CS_READY)
5335 return status;
5336
5337 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5338 msg.rpc_argp = session;
5339 msg.rpc_resp = NULL;
5340 msg.rpc_cred = NULL;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005341 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005342
5343 if (status)
5344 printk(KERN_WARNING
5345 "Got error %d from the server on DESTROY_SESSION. "
5346 "Session has been destroyed regardless...\n", status);
5347
5348 dprintk("<-- nfs4_proc_destroy_session\n");
5349 return status;
5350}
5351
Trond Myklebustfccba802009-07-21 16:48:07 -04005352int nfs4_init_session(struct nfs_server *server)
5353{
5354 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005355 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005356 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005357 int ret;
5358
5359 if (!nfs4_has_session(clp))
5360 return 0;
5361
Trond Myklebust1055d762010-06-16 09:52:27 -04005362 session = clp->cl_session;
5363 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5364 return 0;
5365
Andy Adamson68bf05e2009-12-15 12:55:02 -05005366 rsize = server->rsize;
5367 if (rsize == 0)
5368 rsize = NFS_MAX_FILE_IO_SIZE;
5369 wsize = server->wsize;
5370 if (wsize == 0)
5371 wsize = NFS_MAX_FILE_IO_SIZE;
5372
Andy Adamson68bf05e2009-12-15 12:55:02 -05005373 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5374 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005375
Trond Myklebustfccba802009-07-21 16:48:07 -04005376 ret = nfs4_recover_expired_lease(server);
5377 if (!ret)
5378 ret = nfs4_check_client_ready(clp);
5379 return ret;
5380}
5381
Andy Adamsond83217c2011-03-01 01:34:17 +00005382int nfs4_init_ds_session(struct nfs_client *clp)
5383{
5384 struct nfs4_session *session = clp->cl_session;
5385 int ret;
5386
5387 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5388 return 0;
5389
5390 ret = nfs4_client_recover_expired_lease(clp);
5391 if (!ret)
5392 /* Test for the DS role */
5393 if (!is_ds_client(clp))
5394 ret = -ENODEV;
5395 if (!ret)
5396 ret = nfs4_check_client_ready(clp);
5397 return ret;
5398
5399}
5400EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5401
5402
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005403/*
5404 * Renew the cl_session lease.
5405 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005406struct nfs4_sequence_data {
5407 struct nfs_client *clp;
5408 struct nfs4_sequence_args args;
5409 struct nfs4_sequence_res res;
5410};
5411
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005412static void nfs41_sequence_release(void *data)
5413{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005414 struct nfs4_sequence_data *calldata = data;
5415 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005416
Alexandros Batsakis71358402010-02-05 03:45:05 -08005417 if (atomic_read(&clp->cl_count) > 1)
5418 nfs4_schedule_state_renewal(clp);
5419 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005420 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005421}
5422
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005423static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5424{
5425 switch(task->tk_status) {
5426 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005427 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5428 return -EAGAIN;
5429 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005430 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005431 }
5432 return 0;
5433}
5434
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005435static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005436{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005437 struct nfs4_sequence_data *calldata = data;
5438 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005439
Trond Myklebust14516c32010-07-31 14:29:06 -04005440 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5441 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005442
5443 if (task->tk_status < 0) {
5444 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005445 if (atomic_read(&clp->cl_count) == 1)
5446 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005447
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005448 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5449 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005450 return;
5451 }
5452 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005453 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005454out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005455 dprintk("<-- %s\n", __func__);
5456}
5457
5458static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5459{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005460 struct nfs4_sequence_data *calldata = data;
5461 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005462 struct nfs4_sequence_args *args;
5463 struct nfs4_sequence_res *res;
5464
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005465 args = task->tk_msg.rpc_argp;
5466 res = task->tk_msg.rpc_resp;
5467
Trond Myklebust035168a2010-06-16 09:52:26 -04005468 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005469 return;
5470 rpc_call_start(task);
5471}
5472
5473static const struct rpc_call_ops nfs41_sequence_ops = {
5474 .rpc_call_done = nfs41_sequence_call_done,
5475 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005476 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005477};
5478
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005479static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005480{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005481 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005482 struct rpc_message msg = {
5483 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5484 .rpc_cred = cred,
5485 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005486 struct rpc_task_setup task_setup_data = {
5487 .rpc_client = clp->cl_rpcclient,
5488 .rpc_message = &msg,
5489 .callback_ops = &nfs41_sequence_ops,
5490 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5491 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005492
Alexandros Batsakis71358402010-02-05 03:45:05 -08005493 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005494 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005495 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005496 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005497 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005498 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005499 }
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005500 msg.rpc_argp = &calldata->args;
5501 msg.rpc_resp = &calldata->res;
5502 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005503 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005504
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005505 return rpc_run_task(&task_setup_data);
5506}
5507
5508static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5509{
5510 struct rpc_task *task;
5511 int ret = 0;
5512
5513 task = _nfs41_proc_sequence(clp, cred);
5514 if (IS_ERR(task))
5515 ret = PTR_ERR(task);
5516 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005517 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005518 dprintk("<-- %s status=%d\n", __func__, ret);
5519 return ret;
5520}
5521
5522static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5523{
5524 struct rpc_task *task;
5525 int ret;
5526
5527 task = _nfs41_proc_sequence(clp, cred);
5528 if (IS_ERR(task)) {
5529 ret = PTR_ERR(task);
5530 goto out;
5531 }
5532 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005533 if (!ret) {
5534 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5535
5536 if (task->tk_status == 0)
5537 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005538 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005539 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005540 rpc_put_task(task);
5541out:
5542 dprintk("<-- %s status=%d\n", __func__, ret);
5543 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005544}
5545
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005546struct nfs4_reclaim_complete_data {
5547 struct nfs_client *clp;
5548 struct nfs41_reclaim_complete_args arg;
5549 struct nfs41_reclaim_complete_res res;
5550};
5551
5552static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5553{
5554 struct nfs4_reclaim_complete_data *calldata = data;
5555
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005556 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005557 if (nfs41_setup_sequence(calldata->clp->cl_session,
5558 &calldata->arg.seq_args,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005559 &calldata->res.seq_res, 0, task))
5560 return;
5561
5562 rpc_call_start(task);
5563}
5564
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005565static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5566{
5567 switch(task->tk_status) {
5568 case 0:
5569 case -NFS4ERR_COMPLETE_ALREADY:
5570 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5571 break;
5572 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005573 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005574 /* fall through */
5575 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005576 return -EAGAIN;
5577 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005578 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005579 }
5580 return 0;
5581}
5582
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005583static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5584{
5585 struct nfs4_reclaim_complete_data *calldata = data;
5586 struct nfs_client *clp = calldata->clp;
5587 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5588
5589 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005590 if (!nfs41_sequence_done(task, res))
5591 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005592
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005593 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5594 rpc_restart_call_prepare(task);
5595 return;
5596 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005597 dprintk("<-- %s\n", __func__);
5598}
5599
5600static void nfs4_free_reclaim_complete_data(void *data)
5601{
5602 struct nfs4_reclaim_complete_data *calldata = data;
5603
5604 kfree(calldata);
5605}
5606
5607static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5608 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5609 .rpc_call_done = nfs4_reclaim_complete_done,
5610 .rpc_release = nfs4_free_reclaim_complete_data,
5611};
5612
5613/*
5614 * Issue a global reclaim complete.
5615 */
5616static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5617{
5618 struct nfs4_reclaim_complete_data *calldata;
5619 struct rpc_task *task;
5620 struct rpc_message msg = {
5621 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5622 };
5623 struct rpc_task_setup task_setup_data = {
5624 .rpc_client = clp->cl_rpcclient,
5625 .rpc_message = &msg,
5626 .callback_ops = &nfs4_reclaim_complete_call_ops,
5627 .flags = RPC_TASK_ASYNC,
5628 };
5629 int status = -ENOMEM;
5630
5631 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005632 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005633 if (calldata == NULL)
5634 goto out;
5635 calldata->clp = clp;
5636 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005637
5638 msg.rpc_argp = &calldata->arg;
5639 msg.rpc_resp = &calldata->res;
5640 task_setup_data.callback_data = calldata;
5641 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005642 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005643 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005644 goto out;
5645 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005646 status = nfs4_wait_for_completion_rpc_task(task);
5647 if (status == 0)
5648 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005649 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005650 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005651out:
5652 dprintk("<-- %s status=%d\n", __func__, status);
5653 return status;
5654}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005655
5656static void
5657nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5658{
5659 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005660 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005661
5662 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005663 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5664 * right now covering the LAYOUTGET we are about to send.
5665 * However, that is not so catastrophic, and there seems
5666 * to be no way to prevent it completely.
5667 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005668 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5669 &lgp->res.seq_res, 0, task))
5670 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005671 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5672 NFS_I(lgp->args.inode)->layout,
5673 lgp->args.ctx->state)) {
5674 rpc_exit(task, NFS4_OK);
5675 return;
5676 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005677 rpc_call_start(task);
5678}
5679
5680static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5681{
5682 struct nfs4_layoutget *lgp = calldata;
5683 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5684
5685 dprintk("--> %s\n", __func__);
5686
5687 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5688 return;
5689
5690 switch (task->tk_status) {
5691 case 0:
5692 break;
5693 case -NFS4ERR_LAYOUTTRYLATER:
5694 case -NFS4ERR_RECALLCONFLICT:
5695 task->tk_status = -NFS4ERR_DELAY;
5696 /* Fall through */
5697 default:
5698 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5699 rpc_restart_call_prepare(task);
5700 return;
5701 }
5702 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005703 dprintk("<-- %s\n", __func__);
5704}
5705
5706static void nfs4_layoutget_release(void *calldata)
5707{
5708 struct nfs4_layoutget *lgp = calldata;
5709
5710 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005711 put_nfs_open_context(lgp->args.ctx);
5712 kfree(calldata);
5713 dprintk("<-- %s\n", __func__);
5714}
5715
5716static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5717 .rpc_call_prepare = nfs4_layoutget_prepare,
5718 .rpc_call_done = nfs4_layoutget_done,
5719 .rpc_release = nfs4_layoutget_release,
5720};
5721
5722int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5723{
5724 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5725 struct rpc_task *task;
5726 struct rpc_message msg = {
5727 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5728 .rpc_argp = &lgp->args,
5729 .rpc_resp = &lgp->res,
5730 };
5731 struct rpc_task_setup task_setup_data = {
5732 .rpc_client = server->client,
5733 .rpc_message = &msg,
5734 .callback_ops = &nfs4_layoutget_call_ops,
5735 .callback_data = lgp,
5736 .flags = RPC_TASK_ASYNC,
5737 };
5738 int status = 0;
5739
5740 dprintk("--> %s\n", __func__);
5741
Weston Andros Adamson35124a02011-03-24 16:48:21 -04005742 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005743 lgp->res.seq_res.sr_slot = NULL;
5744 task = rpc_run_task(&task_setup_data);
5745 if (IS_ERR(task))
5746 return PTR_ERR(task);
5747 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00005748 if (status == 0)
5749 status = task->tk_status;
5750 if (status == 0)
5751 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005752 rpc_put_task(task);
5753 dprintk("<-- %s status=%d\n", __func__, status);
5754 return status;
5755}
5756
Benny Halevycbe82602011-05-22 19:52:37 +03005757static void
5758nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
5759{
5760 struct nfs4_layoutreturn *lrp = calldata;
5761
5762 dprintk("--> %s\n", __func__);
5763 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
5764 &lrp->res.seq_res, 0, task))
5765 return;
5766 rpc_call_start(task);
5767}
5768
5769static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
5770{
5771 struct nfs4_layoutreturn *lrp = calldata;
5772 struct nfs_server *server;
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005773 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03005774
5775 dprintk("--> %s\n", __func__);
5776
5777 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
5778 return;
5779
5780 server = NFS_SERVER(lrp->args.inode);
5781 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5782 nfs_restart_rpc(task, lrp->clp);
5783 return;
5784 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005785 spin_lock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005786 if (task->tk_status == 0) {
Benny Halevycbe82602011-05-22 19:52:37 +03005787 if (lrp->res.lrs_present) {
Benny Halevycbe82602011-05-22 19:52:37 +03005788 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Benny Halevycbe82602011-05-22 19:52:37 +03005789 } else
5790 BUG_ON(!list_empty(&lo->plh_segs));
5791 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005792 lo->plh_block_lgets--;
5793 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005794 dprintk("<-- %s\n", __func__);
5795}
5796
5797static void nfs4_layoutreturn_release(void *calldata)
5798{
5799 struct nfs4_layoutreturn *lrp = calldata;
5800
5801 dprintk("--> %s\n", __func__);
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005802 put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03005803 kfree(calldata);
5804 dprintk("<-- %s\n", __func__);
5805}
5806
5807static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
5808 .rpc_call_prepare = nfs4_layoutreturn_prepare,
5809 .rpc_call_done = nfs4_layoutreturn_done,
5810 .rpc_release = nfs4_layoutreturn_release,
5811};
5812
5813int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
5814{
5815 struct rpc_task *task;
5816 struct rpc_message msg = {
5817 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
5818 .rpc_argp = &lrp->args,
5819 .rpc_resp = &lrp->res,
5820 };
5821 struct rpc_task_setup task_setup_data = {
5822 .rpc_client = lrp->clp->cl_rpcclient,
5823 .rpc_message = &msg,
5824 .callback_ops = &nfs4_layoutreturn_call_ops,
5825 .callback_data = lrp,
5826 };
5827 int status;
5828
5829 dprintk("--> %s\n", __func__);
5830 task = rpc_run_task(&task_setup_data);
5831 if (IS_ERR(task))
5832 return PTR_ERR(task);
5833 status = task->tk_status;
5834 dprintk("<-- %s status=%d\n", __func__, status);
5835 rpc_put_task(task);
5836 return status;
5837}
5838
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005839static int
5840_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5841{
5842 struct nfs4_getdeviceinfo_args args = {
5843 .pdev = pdev,
5844 };
5845 struct nfs4_getdeviceinfo_res res = {
5846 .pdev = pdev,
5847 };
5848 struct rpc_message msg = {
5849 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5850 .rpc_argp = &args,
5851 .rpc_resp = &res,
5852 };
5853 int status;
5854
5855 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005856 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005857 dprintk("<-- %s status=%d\n", __func__, status);
5858
5859 return status;
5860}
5861
5862int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5863{
5864 struct nfs4_exception exception = { };
5865 int err;
5866
5867 do {
5868 err = nfs4_handle_exception(server,
5869 _nfs4_proc_getdeviceinfo(server, pdev),
5870 &exception);
5871 } while (exception.retry);
5872 return err;
5873}
5874EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5875
Andy Adamson863a3c62011-03-23 13:27:54 +00005876static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
5877{
5878 struct nfs4_layoutcommit_data *data = calldata;
5879 struct nfs_server *server = NFS_SERVER(data->args.inode);
5880
5881 if (nfs4_setup_sequence(server, &data->args.seq_args,
5882 &data->res.seq_res, 1, task))
5883 return;
5884 rpc_call_start(task);
5885}
5886
5887static void
5888nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
5889{
5890 struct nfs4_layoutcommit_data *data = calldata;
5891 struct nfs_server *server = NFS_SERVER(data->args.inode);
5892
5893 if (!nfs4_sequence_done(task, &data->res.seq_res))
5894 return;
5895
5896 switch (task->tk_status) { /* Just ignore these failures */
5897 case NFS4ERR_DELEG_REVOKED: /* layout was recalled */
5898 case NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
5899 case NFS4ERR_BADLAYOUT: /* no layout */
5900 case NFS4ERR_GRACE: /* loca_recalim always false */
5901 task->tk_status = 0;
5902 }
5903
5904 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5905 nfs_restart_rpc(task, server->nfs_client);
5906 return;
5907 }
5908
5909 if (task->tk_status == 0)
5910 nfs_post_op_update_inode_force_wcc(data->args.inode,
5911 data->res.fattr);
5912}
5913
5914static void nfs4_layoutcommit_release(void *calldata)
5915{
5916 struct nfs4_layoutcommit_data *data = calldata;
5917
5918 /* Matched by references in pnfs_set_layoutcommit */
5919 put_lseg(data->lseg);
5920 put_rpccred(data->cred);
5921 kfree(data);
5922}
5923
5924static const struct rpc_call_ops nfs4_layoutcommit_ops = {
5925 .rpc_call_prepare = nfs4_layoutcommit_prepare,
5926 .rpc_call_done = nfs4_layoutcommit_done,
5927 .rpc_release = nfs4_layoutcommit_release,
5928};
5929
5930int
Andy Adamsonef311532011-03-12 02:58:10 -05005931nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00005932{
5933 struct rpc_message msg = {
5934 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
5935 .rpc_argp = &data->args,
5936 .rpc_resp = &data->res,
5937 .rpc_cred = data->cred,
5938 };
5939 struct rpc_task_setup task_setup_data = {
5940 .task = &data->task,
5941 .rpc_client = NFS_CLIENT(data->args.inode),
5942 .rpc_message = &msg,
5943 .callback_ops = &nfs4_layoutcommit_ops,
5944 .callback_data = data,
5945 .flags = RPC_TASK_ASYNC,
5946 };
5947 struct rpc_task *task;
5948 int status = 0;
5949
5950 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
5951 "lbw: %llu inode %lu\n",
5952 data->task.tk_pid, sync,
5953 data->args.lastbytewritten,
5954 data->args.inode->i_ino);
5955
5956 task = rpc_run_task(&task_setup_data);
5957 if (IS_ERR(task))
5958 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05005959 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00005960 goto out;
5961 status = nfs4_wait_for_completion_rpc_task(task);
5962 if (status != 0)
5963 goto out;
5964 status = task->tk_status;
5965out:
5966 dprintk("%s: status %d\n", __func__, status);
5967 rpc_put_task(task);
5968 return status;
5969}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04005970
5971static int
5972_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
5973 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
5974{
5975 struct nfs41_secinfo_no_name_args args = {
5976 .style = SECINFO_STYLE_CURRENT_FH,
5977 };
5978 struct nfs4_secinfo_res res = {
5979 .flavors = flavors,
5980 };
5981 struct rpc_message msg = {
5982 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
5983 .rpc_argp = &args,
5984 .rpc_resp = &res,
5985 };
5986 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5987}
5988
5989static int
5990nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
5991 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
5992{
5993 struct nfs4_exception exception = { };
5994 int err;
5995 do {
5996 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
5997 switch (err) {
5998 case 0:
5999 case -NFS4ERR_WRONGSEC:
6000 case -NFS4ERR_NOTSUPP:
6001 break;
6002 default:
6003 err = nfs4_handle_exception(server, err, &exception);
6004 }
6005 } while (exception.retry);
6006 return err;
6007}
6008
6009static int
6010nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6011 struct nfs_fsinfo *info)
6012{
6013 int err;
6014 struct page *page;
6015 rpc_authflavor_t flavor;
6016 struct nfs4_secinfo_flavors *flavors;
6017
6018 page = alloc_page(GFP_KERNEL);
6019 if (!page) {
6020 err = -ENOMEM;
6021 goto out;
6022 }
6023
6024 flavors = page_address(page);
6025 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6026
6027 /*
6028 * Fall back on "guess and check" method if
6029 * the server doesn't support SECINFO_NO_NAME
6030 */
6031 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6032 err = nfs4_find_root_sec(server, fhandle, info);
6033 goto out_freepage;
6034 }
6035 if (err)
6036 goto out_freepage;
6037
6038 flavor = nfs_find_best_sec(flavors);
6039 if (err == 0)
6040 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6041
6042out_freepage:
6043 put_page(page);
6044 if (err == -EACCES)
6045 return -EPERM;
6046out:
6047 return err;
6048}
Bryan Schumaker7d974792011-06-02 14:59:08 -04006049static int _nfs41_test_stateid(struct nfs_server *server, struct nfs4_state *state)
6050{
6051 int status;
6052 struct nfs41_test_stateid_args args = {
6053 .stateid = &state->stateid,
6054 };
6055 struct nfs41_test_stateid_res res;
6056 struct rpc_message msg = {
6057 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6058 .rpc_argp = &args,
6059 .rpc_resp = &res,
6060 };
6061 args.seq_args.sa_session = res.seq_res.sr_session = NULL;
6062 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 0, 1);
6063 return status;
6064}
6065
6066static int nfs41_test_stateid(struct nfs_server *server, struct nfs4_state *state)
6067{
6068 struct nfs4_exception exception = { };
6069 int err;
6070 do {
6071 err = nfs4_handle_exception(server,
6072 _nfs41_test_stateid(server, state),
6073 &exception);
6074 } while (exception.retry);
6075 return err;
6076}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006077
6078static int _nfs4_free_stateid(struct nfs_server *server, struct nfs4_state *state)
6079{
6080 int status;
6081 struct nfs41_free_stateid_args args = {
6082 .stateid = &state->stateid,
6083 };
6084 struct nfs41_free_stateid_res res;
6085 struct rpc_message msg = {
6086 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6087 .rpc_argp = &args,
6088 .rpc_resp = &res,
6089 };
6090
6091 args.seq_args.sa_session = res.seq_res.sr_session = NULL;
6092 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 0, 1);
6093 return status;
6094}
6095
6096static int nfs41_free_stateid(struct nfs_server *server, struct nfs4_state *state)
6097{
6098 struct nfs4_exception exception = { };
6099 int err;
6100 do {
6101 err = nfs4_handle_exception(server,
6102 _nfs4_free_stateid(server, state),
6103 &exception);
6104 } while (exception.retry);
6105 return err;
6106}
Andy Adamson557134a2009-04-01 09:21:53 -04006107#endif /* CONFIG_NFS_V4_1 */
6108
Andy Adamson591d71c2009-04-01 09:22:47 -04006109struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006110 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006111 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006112 .recover_open = nfs4_open_reclaim,
6113 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006114 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006115 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116};
6117
Andy Adamson591d71c2009-04-01 09:22:47 -04006118#if defined(CONFIG_NFS_V4_1)
6119struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6120 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6121 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6122 .recover_open = nfs4_open_reclaim,
6123 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006124 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006125 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006126 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006127};
6128#endif /* CONFIG_NFS_V4_1 */
6129
6130struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006131 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006132 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133 .recover_open = nfs4_open_expired,
6134 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006135 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006136 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137};
6138
Andy Adamson591d71c2009-04-01 09:22:47 -04006139#if defined(CONFIG_NFS_V4_1)
6140struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6141 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6142 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006143 .recover_open = nfs41_open_expired,
6144 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006145 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006146 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006147};
6148#endif /* CONFIG_NFS_V4_1 */
6149
Benny Halevy29fba382009-04-01 09:22:44 -04006150struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6151 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006152 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006153 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006154};
6155
6156#if defined(CONFIG_NFS_V4_1)
6157struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
6158 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006159 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006160 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006161};
6162#endif
6163
Trond Myklebust97dc1352010-06-16 09:52:26 -04006164static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6165 .minor_version = 0,
6166 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04006167 .validate_stateid = nfs4_validate_delegation_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006168 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006169 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6170 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6171 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006172};
6173
6174#if defined(CONFIG_NFS_V4_1)
6175static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6176 .minor_version = 1,
6177 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04006178 .validate_stateid = nfs41_validate_delegation_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006179 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006180 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6181 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6182 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006183};
6184#endif
6185
Trond Myklebust97dc1352010-06-16 09:52:26 -04006186const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6187 [0] = &nfs_v4_0_minor_ops,
6188#if defined(CONFIG_NFS_V4_1)
6189 [1] = &nfs_v4_1_minor_ops,
6190#endif
6191};
6192
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006193static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006194 .permission = nfs_permission,
6195 .getattr = nfs_getattr,
6196 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006197 .getxattr = generic_getxattr,
6198 .setxattr = generic_setxattr,
6199 .listxattr = generic_listxattr,
6200 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006201};
6202
David Howells509de812006-08-22 20:06:11 -04006203const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006204 .version = 4, /* protocol version */
6205 .dentry_ops = &nfs4_dentry_operations,
6206 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006207 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208 .getroot = nfs4_proc_get_root,
6209 .getattr = nfs4_proc_getattr,
6210 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04006211 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006212 .lookup = nfs4_proc_lookup,
6213 .access = nfs4_proc_access,
6214 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006215 .create = nfs4_proc_create,
6216 .remove = nfs4_proc_remove,
6217 .unlink_setup = nfs4_proc_unlink_setup,
6218 .unlink_done = nfs4_proc_unlink_done,
6219 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006220 .rename_setup = nfs4_proc_rename_setup,
6221 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222 .link = nfs4_proc_link,
6223 .symlink = nfs4_proc_symlink,
6224 .mkdir = nfs4_proc_mkdir,
6225 .rmdir = nfs4_proc_remove,
6226 .readdir = nfs4_proc_readdir,
6227 .mknod = nfs4_proc_mknod,
6228 .statfs = nfs4_proc_statfs,
6229 .fsinfo = nfs4_proc_fsinfo,
6230 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006231 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232 .decode_dirent = nfs4_decode_dirent,
6233 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05006234 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006235 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006236 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006238 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006240 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006241 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006242 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00006243 .init_client = nfs4_init_client,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006244 .secinfo = nfs4_proc_secinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245};
6246
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006247static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6248 .prefix = XATTR_NAME_NFSV4_ACL,
6249 .list = nfs4_xattr_list_nfs4_acl,
6250 .get = nfs4_xattr_get_nfs4_acl,
6251 .set = nfs4_xattr_set_nfs4_acl,
6252};
6253
6254const struct xattr_handler *nfs4_xattr_handlers[] = {
6255 &nfs4_xattr_nfs4_acl_handler,
6256 NULL
6257};
6258
Linus Torvalds1da177e2005-04-16 15:20:36 -07006259/*
6260 * Local variables:
6261 * c-basic-offset: 8
6262 * End:
6263 */