blob: efa6ae822ca80bc7634e1e5cc6613bbdf0cb41a5 [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 Halevydfb4f302010-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 Halevydfb4f302010-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 Halevydfb4f302010-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 Halevydfb4f302010-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 Halevydfb4f302010-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 Halevydfb4f302010-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 Halevydfb4f302010-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 Halevydfb4f302010-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 Halevydfb4f302010-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 Halevydfb4f302010-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 Halevydfb4f302010-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 Halevydfb4f302010-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 Halevydfb4f302010-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 Myklebuste56e0b782006-01-03 09:55:08 +0100769 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400770 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100771 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
Al Viro82a2c1b2011-06-22 18:30:55 -0400792static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
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{
Al Viro82a2c1b2011-06-22 18:30:55 -0400797 struct dentry *parent = dget_parent(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 Viro82a2c1b2011-06-22 18:30:55 -0400808 nfs_sb_active(dentry->d_sb);
809 p->dentry = dget(dentry);
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;
Al Viro82a2c1b2011-06-22 18:30:55 -0400818 p->o_arg.name = &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);
Al Viro82a2c1b2011-06-22 18:30:55 -0400848 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400849
850 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400851 if (p->state != NULL)
852 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400853 nfs4_put_state_owner(p->owner);
854 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400855 dput(p->dentry);
856 nfs_sb_deactive(sb);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400857 kfree(p);
858}
859
860static void nfs4_opendata_put(struct nfs4_opendata *p)
861{
862 if (p != NULL)
863 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100864}
865
Trond Myklebust06f814a2006-01-03 09:55:07 +0100866static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
867{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100868 int ret;
869
Trond Myklebust06f814a2006-01-03 09:55:07 +0100870 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100871 return ret;
872}
873
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500874static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400875{
876 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500877
878 if (open_mode & O_EXCL)
879 goto out;
880 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400881 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500882 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
883 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400884 break;
885 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500886 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
887 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400888 break;
889 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500890 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
891 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400892 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500893out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400894 return ret;
895}
896
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500897static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400898{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500899 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400900 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500901 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400902 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500903 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400904 return 1;
905}
906
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500907static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100908{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500909 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100910 case FMODE_WRITE:
911 state->n_wronly++;
912 break;
913 case FMODE_READ:
914 state->n_rdonly++;
915 break;
916 case FMODE_READ|FMODE_WRITE:
917 state->n_rdwr++;
918 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500919 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100920}
921
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500922static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400923{
924 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
925 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
926 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500927 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400928 case FMODE_READ:
929 set_bit(NFS_O_RDONLY_STATE, &state->flags);
930 break;
931 case FMODE_WRITE:
932 set_bit(NFS_O_WRONLY_STATE, &state->flags);
933 break;
934 case FMODE_READ|FMODE_WRITE:
935 set_bit(NFS_O_RDWR_STATE, &state->flags);
936 }
937}
938
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500939static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400940{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400941 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500942 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400943 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400944}
945
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500946static 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 -0700947{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400948 /*
949 * Protect the call to nfs4_state_set_mode_locked and
950 * serialise the stateid update
951 */
952 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400953 if (deleg_stateid != NULL) {
954 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
955 set_bit(NFS_DELEGATED_STATE, &state->flags);
956 }
957 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500958 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400959 write_sequnlock(&state->seqlock);
960 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500961 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700962 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}
964
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500965static 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 -0500966{
967 struct nfs_inode *nfsi = NFS_I(state->inode);
968 struct nfs_delegation *deleg_cur;
969 int ret = 0;
970
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500971 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500972
973 rcu_read_lock();
974 deleg_cur = rcu_dereference(nfsi->delegation);
975 if (deleg_cur == NULL)
976 goto no_delegation;
977
978 spin_lock(&deleg_cur->lock);
979 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500980 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500981 goto no_delegation_unlock;
982
983 if (delegation == NULL)
984 delegation = &deleg_cur->stateid;
985 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
986 goto no_delegation_unlock;
987
Trond Myklebustb7391f42008-12-23 15:21:52 -0500988 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500989 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500990 ret = 1;
991no_delegation_unlock:
992 spin_unlock(&deleg_cur->lock);
993no_delegation:
994 rcu_read_unlock();
995
996 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500997 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500998 ret = 1;
999 }
1000
1001 return ret;
1002}
1003
1004
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001005static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001006{
1007 struct nfs_delegation *delegation;
1008
1009 rcu_read_lock();
1010 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001011 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001012 rcu_read_unlock();
1013 return;
1014 }
1015 rcu_read_unlock();
1016 nfs_inode_return_delegation(inode);
1017}
1018
Trond Myklebust6ee41262007-07-08 14:11:36 -04001019static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001020{
1021 struct nfs4_state *state = opendata->state;
1022 struct nfs_inode *nfsi = NFS_I(state->inode);
1023 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001024 int open_mode = opendata->o_arg.open_flags & O_EXCL;
1025 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001026 nfs4_stateid stateid;
1027 int ret = -EAGAIN;
1028
Trond Myklebustaac00a82007-07-05 19:02:21 -04001029 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001030 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001031 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001032 if (can_open_cached(state, fmode, open_mode)) {
1033 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001034 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001035 goto out_return_state;
1036 }
1037 spin_unlock(&state->owner->so_lock);
1038 }
Trond Myklebust34310432008-12-23 15:21:38 -05001039 rcu_read_lock();
1040 delegation = rcu_dereference(nfsi->delegation);
1041 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001042 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001043 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001044 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001045 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001046 /* Save the delegation */
1047 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1048 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001049 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001050 if (ret != 0)
1051 goto out;
1052 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001053
1054 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001055 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001056 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001057 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001058out:
1059 return ERR_PTR(ret);
1060out_return_state:
1061 atomic_inc(&state->count);
1062 return state;
1063}
1064
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001065static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1066{
1067 struct inode *inode;
1068 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001069 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001070 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001071
Trond Myklebustaac00a82007-07-05 19:02:21 -04001072 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001073 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001074 goto out;
1075 }
1076
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001077 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001078 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001079 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001080 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001081 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001082 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001083 goto err;
1084 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001085 state = nfs4_get_open_state(inode, data->owner);
1086 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001087 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001088 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001089 int delegation_flags = 0;
1090
Trond Myklebust003707c2007-07-05 18:07:55 -04001091 rcu_read_lock();
1092 delegation = rcu_dereference(NFS_I(inode)->delegation);
1093 if (delegation)
1094 delegation_flags = delegation->flags;
1095 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -05001096 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001097 nfs_inode_set_delegation(state->inode,
1098 data->owner->so_cred,
1099 &data->o_res);
1100 else
1101 nfs_inode_reclaim_delegation(state->inode,
1102 data->owner->so_cred,
1103 &data->o_res);
1104 }
Trond Myklebust34310432008-12-23 15:21:38 -05001105
1106 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001107 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001108 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001109out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001110 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001111err_put_inode:
1112 iput(inode);
1113err:
1114 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001115}
1116
Trond Myklebust864472e2006-01-03 09:55:15 +01001117static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1118{
1119 struct nfs_inode *nfsi = NFS_I(state->inode);
1120 struct nfs_open_context *ctx;
1121
1122 spin_lock(&state->inode->i_lock);
1123 list_for_each_entry(ctx, &nfsi->open_files, list) {
1124 if (ctx->state != state)
1125 continue;
1126 get_nfs_open_context(ctx);
1127 spin_unlock(&state->inode->i_lock);
1128 return ctx;
1129 }
1130 spin_unlock(&state->inode->i_lock);
1131 return ERR_PTR(-ENOENT);
1132}
1133
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001134static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1135{
1136 struct nfs4_opendata *opendata;
1137
Al Viro3d4ff432011-06-22 18:40:12 -04001138 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001139 if (opendata == NULL)
1140 return ERR_PTR(-ENOMEM);
1141 opendata->state = state;
1142 atomic_inc(&state->count);
1143 return opendata;
1144}
1145
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001146static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001147{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001148 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001149 int ret;
1150
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001151 opendata->o_arg.open_flags = 0;
1152 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001153 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1154 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1155 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001156 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001157 if (ret != 0)
1158 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001159 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001160 if (IS_ERR(newstate))
1161 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001162 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001163 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001164 return 0;
1165}
1166
1167static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1168{
Trond Myklebust864472e2006-01-03 09:55:15 +01001169 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001170 int ret;
1171
1172 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001173 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001174 smp_rmb();
1175 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001176 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001177 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001178 if (ret != 0)
1179 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001180 if (newstate != state)
1181 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001182 }
1183 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001184 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001185 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001186 if (ret != 0)
1187 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001188 if (newstate != state)
1189 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001190 }
1191 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001192 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001193 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001194 if (ret != 0)
1195 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001196 if (newstate != state)
1197 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001198 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001199 /*
1200 * We may have performed cached opens for all three recoveries.
1201 * Check if we need to update the current stateid.
1202 */
1203 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1204 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001205 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001206 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1207 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001208 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001209 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001210 return 0;
1211}
1212
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213/*
1214 * OPEN_RECLAIM:
1215 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001217static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001219 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001220 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001221 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 int status;
1223
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001224 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1225 if (IS_ERR(opendata))
1226 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001227 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1228 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001229 rcu_read_lock();
1230 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001231 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001232 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001233 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001234 opendata->o_arg.u.delegation_type = delegation_type;
1235 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001236 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 return status;
1238}
1239
Trond Myklebust539cd032007-06-05 11:46:42 -04001240static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
1242 struct nfs_server *server = NFS_SERVER(state->inode);
1243 struct nfs4_exception exception = { };
1244 int err;
1245 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001246 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001247 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001248 break;
1249 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 } while (exception.retry);
1251 return err;
1252}
1253
Trond Myklebust864472e2006-01-03 09:55:15 +01001254static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1255{
1256 struct nfs_open_context *ctx;
1257 int ret;
1258
1259 ctx = nfs4_state_find_open_context(state);
1260 if (IS_ERR(ctx))
1261 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001262 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001263 put_nfs_open_context(ctx);
1264 return ret;
1265}
1266
Trond Myklebust13437e12007-07-06 15:10:43 -04001267static 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 -07001268{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001269 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001270 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001272 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1273 if (IS_ERR(opendata))
1274 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001275 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001276 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001277 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001278 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001279 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001280 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281}
1282
Trond Myklebust13437e12007-07-06 15:10:43 -04001283int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284{
1285 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001286 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 int err;
1288 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001289 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 switch (err) {
1291 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001292 case -ENOENT:
1293 case -ESTALE:
1294 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001295 case -NFS4ERR_BADSESSION:
1296 case -NFS4ERR_BADSLOT:
1297 case -NFS4ERR_BAD_HIGH_SLOT:
1298 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1299 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001300 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001301 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 case -NFS4ERR_STALE_CLIENTID:
1303 case -NFS4ERR_STALE_STATEID:
1304 case -NFS4ERR_EXPIRED:
1305 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001306 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001307 goto out;
1308 case -ERESTARTSYS:
1309 /*
1310 * The show must go on: exit, but mark the
1311 * stateid as needing recovery.
1312 */
1313 case -NFS4ERR_ADMIN_REVOKED:
1314 case -NFS4ERR_BAD_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001315 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001316 case -EKEYEXPIRED:
1317 /*
1318 * User RPCSEC_GSS context has expired.
1319 * We cannot recover this stateid now, so
1320 * skip it and allow recovery thread to
1321 * proceed.
1322 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001323 case -ENOMEM:
1324 err = 0;
1325 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 }
1327 err = nfs4_handle_exception(server, err, &exception);
1328 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001329out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 return err;
1331}
1332
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001333static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1334{
1335 struct nfs4_opendata *data = calldata;
1336
1337 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001338 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001339 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1340 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001341 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001342 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001343 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001344 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001345}
1346
1347static void nfs4_open_confirm_release(void *calldata)
1348{
1349 struct nfs4_opendata *data = calldata;
1350 struct nfs4_state *state = NULL;
1351
1352 /* If this request hasn't been cancelled, do nothing */
1353 if (data->cancelled == 0)
1354 goto out_free;
1355 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001356 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001357 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001358 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001359 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001360 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001361out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001362 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001363}
1364
1365static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001366 .rpc_call_done = nfs4_open_confirm_done,
1367 .rpc_release = nfs4_open_confirm_release,
1368};
1369
1370/*
1371 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1372 */
1373static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1374{
1375 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1376 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001377 struct rpc_message msg = {
1378 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1379 .rpc_argp = &data->c_arg,
1380 .rpc_resp = &data->c_res,
1381 .rpc_cred = data->owner->so_cred,
1382 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001383 struct rpc_task_setup task_setup_data = {
1384 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001385 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001386 .callback_ops = &nfs4_open_confirm_ops,
1387 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001388 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001389 .flags = RPC_TASK_ASYNC,
1390 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 int status;
1392
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001393 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001394 data->rpc_done = 0;
1395 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001396 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001397 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001398 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001399 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001400 status = nfs4_wait_for_completion_rpc_task(task);
1401 if (status != 0) {
1402 data->cancelled = 1;
1403 smp_wmb();
1404 } else
1405 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001406 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 return status;
1408}
1409
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001410static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001412 struct nfs4_opendata *data = calldata;
1413 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001414
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001415 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1416 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001417 /*
1418 * Check if we still need to send an OPEN call, or if we can use
1419 * a delegation instead.
1420 */
1421 if (data->state != NULL) {
1422 struct nfs_delegation *delegation;
1423
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001424 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001425 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001426 rcu_read_lock();
1427 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1428 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001429 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001430 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001431 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001432 }
1433 rcu_read_unlock();
1434 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001435 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001436 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001437 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001438 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001439 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001440 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1441 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001442 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001443 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001444 &data->o_arg.seq_args,
1445 &data->o_res.seq_res, 1, task))
1446 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001447 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001448 return;
1449out_no_action:
1450 task->tk_action = NULL;
1451
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001452}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001454static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1455{
1456 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1457 nfs4_open_prepare(task, calldata);
1458}
1459
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001460static void nfs4_open_done(struct rpc_task *task, void *calldata)
1461{
1462 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001464 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001465
Trond Myklebust14516c32010-07-31 14:29:06 -04001466 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1467 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001468
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001469 if (task->tk_status == 0) {
1470 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001471 case S_IFREG:
1472 break;
1473 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001474 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001475 break;
1476 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001477 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001478 break;
1479 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001480 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001481 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001482 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001483 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1484 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001485 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001486 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001487}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001488
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001489static void nfs4_open_release(void *calldata)
1490{
1491 struct nfs4_opendata *data = calldata;
1492 struct nfs4_state *state = NULL;
1493
1494 /* If this request hasn't been cancelled, do nothing */
1495 if (data->cancelled == 0)
1496 goto out_free;
1497 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001498 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001499 goto out_free;
1500 /* In case we need an open_confirm, no cleanup! */
1501 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1502 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001503 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001504 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001505 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001506out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001507 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001508}
1509
1510static const struct rpc_call_ops nfs4_open_ops = {
1511 .rpc_call_prepare = nfs4_open_prepare,
1512 .rpc_call_done = nfs4_open_done,
1513 .rpc_release = nfs4_open_release,
1514};
1515
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001516static const struct rpc_call_ops nfs4_recover_open_ops = {
1517 .rpc_call_prepare = nfs4_recover_open_prepare,
1518 .rpc_call_done = nfs4_open_done,
1519 .rpc_release = nfs4_open_release,
1520};
1521
1522static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001523{
1524 struct inode *dir = data->dir->d_inode;
1525 struct nfs_server *server = NFS_SERVER(dir);
1526 struct nfs_openargs *o_arg = &data->o_arg;
1527 struct nfs_openres *o_res = &data->o_res;
1528 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001529 struct rpc_message msg = {
1530 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1531 .rpc_argp = o_arg,
1532 .rpc_resp = o_res,
1533 .rpc_cred = data->owner->so_cred,
1534 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001535 struct rpc_task_setup task_setup_data = {
1536 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001537 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001538 .callback_ops = &nfs4_open_ops,
1539 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001540 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001541 .flags = RPC_TASK_ASYNC,
1542 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001543 int status;
1544
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001545 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001546 data->rpc_done = 0;
1547 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001548 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001549 if (isrecover)
1550 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001551 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001552 if (IS_ERR(task))
1553 return PTR_ERR(task);
1554 status = nfs4_wait_for_completion_rpc_task(task);
1555 if (status != 0) {
1556 data->cancelled = 1;
1557 smp_wmb();
1558 } else
1559 status = data->rpc_status;
1560 rpc_put_task(task);
1561
1562 return status;
1563}
1564
1565static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1566{
1567 struct inode *dir = data->dir->d_inode;
1568 struct nfs_openres *o_res = &data->o_res;
1569 int status;
1570
1571 status = nfs4_run_open_task(data, 1);
1572 if (status != 0 || !data->rpc_done)
1573 return status;
1574
1575 nfs_refresh_inode(dir, o_res->dir_attr);
1576
1577 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1578 status = _nfs4_proc_open_confirm(data);
1579 if (status != 0)
1580 return status;
1581 }
1582
1583 return status;
1584}
1585
1586/*
1587 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1588 */
1589static int _nfs4_proc_open(struct nfs4_opendata *data)
1590{
1591 struct inode *dir = data->dir->d_inode;
1592 struct nfs_server *server = NFS_SERVER(dir);
1593 struct nfs_openargs *o_arg = &data->o_arg;
1594 struct nfs_openres *o_res = &data->o_res;
1595 int status;
1596
1597 status = nfs4_run_open_task(data, 0);
Trond Myklebust3e309912007-07-07 13:19:59 -04001598 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001599 return status;
1600
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001601 if (o_arg->open_flags & O_CREAT) {
1602 update_changeattr(dir, &o_res->cinfo);
1603 nfs_post_op_update_inode(dir, o_res->dir_attr);
1604 } else
1605 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001606 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1607 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001609 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001611 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 }
1613 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001614 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001615 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616}
1617
Andy Adamsond83217c2011-03-01 01:34:17 +00001618static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001619{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001620 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001621 int ret;
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001622
Trond Myklebusta78cb572009-08-09 15:06:19 -04001623 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001624 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001625 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001626 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001627 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1628 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001629 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001630 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001631 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001632 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001633 return ret;
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001634}
1635
Andy Adamsond83217c2011-03-01 01:34:17 +00001636static int nfs4_recover_expired_lease(struct nfs_server *server)
1637{
1638 return nfs4_client_recover_expired_lease(server->nfs_client);
1639}
1640
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641/*
1642 * OPEN_EXPIRED:
1643 * reclaim state on the server after a network partition.
1644 * Assumes caller holds the appropriate lock
1645 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001646static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001648 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001649 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001651 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1652 if (IS_ERR(opendata))
1653 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001654 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001655 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001656 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001657 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001658 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659}
1660
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001661static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001662{
Trond Myklebust539cd032007-06-05 11:46:42 -04001663 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001664 struct nfs4_exception exception = { };
1665 int err;
1666
1667 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001668 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001669 switch (err) {
1670 default:
1671 goto out;
1672 case -NFS4ERR_GRACE:
1673 case -NFS4ERR_DELAY:
1674 nfs4_handle_exception(server, err, &exception);
1675 err = 0;
1676 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001677 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001678out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001679 return err;
1680}
1681
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1683{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001685 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686
Trond Myklebust864472e2006-01-03 09:55:15 +01001687 ctx = nfs4_state_find_open_context(state);
1688 if (IS_ERR(ctx))
1689 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001690 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001691 put_nfs_open_context(ctx);
1692 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693}
1694
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001695#if defined(CONFIG_NFS_V4_1)
1696static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1697{
1698 int status;
1699 struct nfs_server *server = NFS_SERVER(state->inode);
1700
1701 status = nfs41_test_stateid(server, state);
1702 if (status == NFS_OK)
1703 return 0;
1704 nfs41_free_stateid(server, state);
1705 return nfs4_open_expired(sp, state);
1706}
1707#endif
1708
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001710 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1711 * fields corresponding to attributes that were used to store the verifier.
1712 * Make sure we clobber those fields in the later setattr call
1713 */
1714static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1715{
1716 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1717 !(sattr->ia_valid & ATTR_ATIME_SET))
1718 sattr->ia_valid |= ATTR_ATIME;
1719
1720 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1721 !(sattr->ia_valid & ATTR_MTIME_SET))
1722 sattr->ia_valid |= ATTR_MTIME;
1723}
1724
1725/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001726 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 */
Al Viro82a2c1b2011-06-22 18:30:55 -04001728static int _nfs4_do_open(struct inode *dir, struct dentry *dentry, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729{
1730 struct nfs4_state_owner *sp;
1731 struct nfs4_state *state = NULL;
1732 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001733 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001734 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
1736 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 status = -ENOMEM;
1738 if (!(sp = nfs4_get_state_owner(server, cred))) {
1739 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1740 goto out_err;
1741 }
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001742 status = nfs4_recover_expired_lease(server);
1743 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001744 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001745 if (dentry->d_inode != NULL)
1746 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001747 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001748 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001749 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001750 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Al Viro82a2c1b2011-06-22 18:30:55 -04001752 if (dentry->d_inode != NULL)
1753 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001754
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001755 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001757 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001759 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001760 status = PTR_ERR(state);
1761 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001762 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001763 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001764 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001765
1766 if (opendata->o_arg.open_flags & O_EXCL) {
1767 nfs4_exclusive_attrset(opendata, sattr);
1768
1769 nfs_fattr_init(opendata->o_res.f_attr);
1770 status = nfs4_do_setattr(state->inode, cred,
1771 opendata->o_res.f_attr, sattr,
1772 state);
1773 if (status == 0)
1774 nfs_setattr_update_inode(state->inode, sattr);
1775 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1776 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001777 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 *res = state;
1780 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001781err_opendata_put:
1782 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001783err_put_state_owner:
1784 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 *res = NULL;
1787 return status;
1788}
1789
1790
Al Viro82a2c1b2011-06-22 18:30:55 -04001791static struct nfs4_state *nfs4_do_open(struct inode *dir, struct dentry *dentry, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792{
1793 struct nfs4_exception exception = { };
1794 struct nfs4_state *res;
1795 int status;
1796
1797 do {
Al Viro82a2c1b2011-06-22 18:30:55 -04001798 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 if (status == 0)
1800 break;
1801 /* NOTE: BAD_SEQID means the server and client disagree about the
1802 * book-keeping w.r.t. state-changing operations
1803 * (OPEN/CLOSE/LOCK/LOCKU...)
1804 * It is actually a sign of a bug on the client or on the server.
1805 *
1806 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001807 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 * have unhashed the old state_owner for us, and that we can
1809 * therefore safely retry using a new one. We should still warn
1810 * the user though...
1811 */
1812 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001813 printk(KERN_WARNING "NFS: v4 server %s "
1814 " returned a bad sequence-id error!\n",
1815 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 exception.retry = 1;
1817 continue;
1818 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001819 /*
1820 * BAD_STATEID on OPEN means that the server cancelled our
1821 * state before it received the OPEN_CONFIRM.
1822 * Recover by retrying the request as per the discussion
1823 * on Page 181 of RFC3530.
1824 */
1825 if (status == -NFS4ERR_BAD_STATEID) {
1826 exception.retry = 1;
1827 continue;
1828 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001829 if (status == -EAGAIN) {
1830 /* We must have found a delegation */
1831 exception.retry = 1;
1832 continue;
1833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1835 status, &exception));
1836 } while (exception.retry);
1837 return res;
1838}
1839
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001840static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1841 struct nfs_fattr *fattr, struct iattr *sattr,
1842 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001844 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001846 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 .iap = sattr,
1848 .server = server,
1849 .bitmask = server->attr_bitmask,
1850 };
1851 struct nfs_setattrres res = {
1852 .fattr = fattr,
1853 .server = server,
1854 };
1855 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001856 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1857 .rpc_argp = &arg,
1858 .rpc_resp = &res,
1859 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001861 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001862 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
Trond Myklebust0e574af2005-10-27 22:12:38 -04001864 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001866 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1867 /* Use that stateid */
1868 } else if (state != NULL) {
Trond Myklebust77041ed2010-07-01 12:49:11 -04001869 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001870 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1872
Bryan Schumaker7c513052011-03-24 17:12:24 +00001873 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001874 if (status == 0 && state != NULL)
1875 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001876 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877}
1878
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001879static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1880 struct nfs_fattr *fattr, struct iattr *sattr,
1881 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001883 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 struct nfs4_exception exception = { };
1885 int err;
1886 do {
1887 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001888 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 &exception);
1890 } while (exception.retry);
1891 return err;
1892}
1893
1894struct nfs4_closedata {
1895 struct inode *inode;
1896 struct nfs4_state *state;
1897 struct nfs_closeargs arg;
1898 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001899 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001900 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00001901 bool roc;
1902 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903};
1904
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001905static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001906{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001907 struct nfs4_closedata *calldata = data;
1908 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04001909 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07001910
Fred Isamanf7e89172011-01-06 11:36:32 +00001911 if (calldata->roc)
1912 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001913 nfs4_put_open_state(calldata->state);
1914 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001915 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04001916 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07001917 kfree(calldata);
1918}
1919
Trond Myklebust88069f72009-12-08 08:33:16 -05001920static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1921 fmode_t fmode)
1922{
1923 spin_lock(&state->owner->so_lock);
1924 if (!(fmode & FMODE_READ))
1925 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1926 if (!(fmode & FMODE_WRITE))
1927 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1928 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1929 spin_unlock(&state->owner->so_lock);
1930}
1931
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001932static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001934 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 struct nfs_server *server = NFS_SERVER(calldata->inode);
1937
Trond Myklebust14516c32010-07-31 14:29:06 -04001938 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1939 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 /* hmm. we are done with the inode, and in the process of freeing
1941 * the state_owner. we keep this around to process errors
1942 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 switch (task->tk_status) {
1944 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00001945 if (calldata->roc)
1946 pnfs_roc_set_barrier(state->inode,
1947 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04001948 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001949 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001950 nfs4_close_clear_stateid_flags(state,
1951 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 break;
1953 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001954 case -NFS4ERR_OLD_STATEID:
1955 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001957 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001958 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001960 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1961 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001963 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001964 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965}
1966
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001967static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001969 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001970 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001971 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001972
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001973 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001974 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001975
Trond Myklebust88069f72009-12-08 08:33:16 -05001976 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1977 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001978 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001979 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001980 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001981 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001982 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1983 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1984 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001985 }
1986 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001987 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1988 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1989 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001990 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001991 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001992 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05001993
1994 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001995 /* Note: exit _without_ calling nfs4_close_done */
1996 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001997 return;
1998 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001999
Fred Isamanf7e89172011-01-06 11:36:32 +00002000 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002001 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002002 if (calldata->roc &&
2003 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2004 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2005 task, NULL);
2006 return;
2007 }
2008 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002009
Trond Myklebust516a6af2005-10-27 22:12:41 -04002010 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002011 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002012 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Andy Adamson19ddab02009-04-01 09:22:20 -04002013 &calldata->arg.seq_args, &calldata->res.seq_res,
2014 1, task))
2015 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002016 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017}
2018
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002019static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002020 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002021 .rpc_call_done = nfs4_close_done,
2022 .rpc_release = nfs4_free_closedata,
2023};
2024
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025/*
2026 * It is possible for data to be read/written from a mem-mapped file
2027 * after the sys_close call (which hits the vfs layer as a flush).
2028 * This means that we can't safely call nfsv4 close on a file until
2029 * the inode is cleared. This in turn means that we are not good
2030 * NFSv4 citizens - we do not indicate to the server to update the file's
2031 * share state even when we are done with one of the three share
2032 * stateid's in the inode.
2033 *
2034 * NOTE: Caller must be holding the sp->so_owner semaphore!
2035 */
Al Viro643168c2011-06-22 18:20:23 -04002036int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002038 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002040 struct nfs4_state_owner *sp = state->owner;
2041 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002042 struct rpc_message msg = {
2043 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2044 .rpc_cred = state->owner->so_cred,
2045 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002046 struct rpc_task_setup task_setup_data = {
2047 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002048 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002049 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002050 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002051 .flags = RPC_TASK_ASYNC,
2052 };
Trond Myklebust95121352005-10-18 14:20:12 -07002053 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002055 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002057 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002058 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002060 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002061 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002063 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002064 if (calldata->arg.seqid == NULL)
2065 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002066 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002067 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002068 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002069 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002070 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002071 calldata->roc = roc;
Al Viro643168c2011-06-22 18:20:23 -04002072 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002073
Trond Myklebust1174dd12010-12-21 10:52:24 -05002074 msg.rpc_argp = &calldata->arg;
2075 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002076 task_setup_data.callback_data = calldata;
2077 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002078 if (IS_ERR(task))
2079 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002080 status = 0;
2081 if (wait)
2082 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002083 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002084 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002085out_free_calldata:
2086 kfree(calldata);
2087out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002088 if (roc)
2089 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002090 nfs4_put_open_state(state);
2091 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002092 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093}
2094
Trond Myklebust2b484292010-09-17 10:56:51 -04002095static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002096nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 struct nfs4_state *state;
2099
Trond Myklebust565277f2007-10-15 18:17:53 -04002100 /* Protect against concurrent sillydeletes */
Al Viro3d4ff432011-06-22 18:40:12 -04002101 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002102 if (IS_ERR(state))
2103 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002104 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002105 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106}
2107
Trond Myklebust1185a552009-12-03 15:54:02 -05002108static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002109{
2110 if (ctx->state == NULL)
2111 return;
2112 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002113 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002114 else
Al Viro643168c2011-06-22 18:20:23 -04002115 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002116}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
2118static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2119{
Benny Halevy43652ad2009-04-01 09:21:54 -04002120 struct nfs4_server_caps_arg args = {
2121 .fhandle = fhandle,
2122 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 struct nfs4_server_caps_res res = {};
2124 struct rpc_message msg = {
2125 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002126 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 .rpc_resp = &res,
2128 };
2129 int status;
2130
Bryan Schumaker7c513052011-03-24 17:12:24 +00002131 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 if (status == 0) {
2133 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002134 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2135 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2136 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2137 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2138 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2140 server->caps |= NFS_CAP_ACLS;
2141 if (res.has_links != 0)
2142 server->caps |= NFS_CAP_HARDLINKS;
2143 if (res.has_symlinks != 0)
2144 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002145 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2146 server->caps |= NFS_CAP_FILEID;
2147 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2148 server->caps |= NFS_CAP_MODE;
2149 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2150 server->caps |= NFS_CAP_NLINK;
2151 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2152 server->caps |= NFS_CAP_OWNER;
2153 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2154 server->caps |= NFS_CAP_OWNER_GROUP;
2155 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2156 server->caps |= NFS_CAP_ATIME;
2157 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2158 server->caps |= NFS_CAP_CTIME;
2159 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2160 server->caps |= NFS_CAP_MTIME;
2161
Trond Myklebusta65318b2009-03-11 14:10:28 -04002162 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2163 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2164 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 server->acl_bitmask = res.acl_bitmask;
2166 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002167
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 return status;
2169}
2170
Trond Myklebust55a97592006-06-09 09:34:19 -04002171int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172{
2173 struct nfs4_exception exception = { };
2174 int err;
2175 do {
2176 err = nfs4_handle_exception(server,
2177 _nfs4_server_capabilities(server, fhandle),
2178 &exception);
2179 } while (exception.retry);
2180 return err;
2181}
2182
2183static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2184 struct nfs_fsinfo *info)
2185{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 struct nfs4_lookup_root_arg args = {
2187 .bitmask = nfs4_fattr_bitmap,
2188 };
2189 struct nfs4_lookup_res res = {
2190 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002191 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 .fh = fhandle,
2193 };
2194 struct rpc_message msg = {
2195 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2196 .rpc_argp = &args,
2197 .rpc_resp = &res,
2198 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002199
Trond Myklebust0e574af2005-10-27 22:12:38 -04002200 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002201 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202}
2203
2204static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2205 struct nfs_fsinfo *info)
2206{
2207 struct nfs4_exception exception = { };
2208 int err;
2209 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002210 err = _nfs4_lookup_root(server, fhandle, info);
2211 switch (err) {
2212 case 0:
2213 case -NFS4ERR_WRONGSEC:
2214 break;
2215 default:
2216 err = nfs4_handle_exception(server, err, &exception);
2217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 } while (exception.retry);
2219 return err;
2220}
2221
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002222static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2223 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2224{
2225 struct rpc_auth *auth;
2226 int ret;
2227
2228 auth = rpcauth_create(flavor, server->client);
2229 if (!auth) {
2230 ret = -EIO;
2231 goto out;
2232 }
2233 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002234out:
2235 return ret;
2236}
2237
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002238static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002239 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002241 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002242 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002244 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2245 flav_array[len] = RPC_AUTH_NULL;
2246 len += 1;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002247
2248 for (i = 0; i < len; i++) {
2249 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002250 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002251 continue;
2252 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002253 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002254 /*
2255 * -EACCESS could mean that the user doesn't have correct permissions
2256 * to access the mount. It could also mean that we tried to mount
2257 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2258 * existing mount programs don't handle -EACCES very well so it should
2259 * be mapped to -EPERM instead.
2260 */
2261 if (status == -EACCES)
2262 status = -EPERM;
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002263 return status;
2264}
2265
2266/*
2267 * get the file handle for the "/" directory on the server
2268 */
2269static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2270 struct nfs_fsinfo *info)
2271{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002272 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002273 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002274 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2275 /*
2276 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2277 * by nfs4_map_errors() as this function exits.
2278 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002279 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 if (status == 0)
2281 status = nfs4_server_capabilities(server, fhandle);
2282 if (status == 0)
2283 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002284 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285}
2286
Andy Adamson533eb462011-06-13 18:25:56 -04002287static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002288/*
2289 * Get locations and (maybe) other attributes of a referral.
2290 * Note that we'll actually follow the referral later when
2291 * we detect fsid mismatch in inode revalidation
2292 */
Andy Adamson533eb462011-06-13 18:25:56 -04002293static int nfs4_get_referral(struct inode *dir, const struct qstr *name,
2294 struct nfs_fattr *fattr, struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002295{
2296 int status = -ENOMEM;
2297 struct page *page = NULL;
2298 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002299
2300 page = alloc_page(GFP_KERNEL);
2301 if (page == NULL)
2302 goto out;
2303 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2304 if (locations == NULL)
2305 goto out;
2306
Trond Myklebustc228fd32007-01-13 02:28:11 -05002307 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002308 if (status != 0)
2309 goto out;
2310 /* Make sure server returned a different fsid for the referral */
2311 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002312 dprintk("%s: server did not return a different fsid for"
2313 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002314 status = -EIO;
2315 goto out;
2316 }
Andy Adamson533eb462011-06-13 18:25:56 -04002317 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2318 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002319
Andy Adamson533eb462011-06-13 18:25:56 -04002320 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002321 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002322 memset(fhandle, 0, sizeof(struct nfs_fh));
2323out:
2324 if (page)
2325 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002326 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002327 return status;
2328}
2329
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2331{
2332 struct nfs4_getattr_arg args = {
2333 .fh = fhandle,
2334 .bitmask = server->attr_bitmask,
2335 };
2336 struct nfs4_getattr_res res = {
2337 .fattr = fattr,
2338 .server = server,
2339 };
2340 struct rpc_message msg = {
2341 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2342 .rpc_argp = &args,
2343 .rpc_resp = &res,
2344 };
2345
Trond Myklebust0e574af2005-10-27 22:12:38 -04002346 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002347 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348}
2349
2350static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2351{
2352 struct nfs4_exception exception = { };
2353 int err;
2354 do {
2355 err = nfs4_handle_exception(server,
2356 _nfs4_proc_getattr(server, fhandle, fattr),
2357 &exception);
2358 } while (exception.retry);
2359 return err;
2360}
2361
2362/*
2363 * The file is not closed if it is opened due to the a request to change
2364 * the size of the file. The open call will not be needed once the
2365 * VFS layer lookup-intents are implemented.
2366 *
2367 * Close is called when the inode is destroyed.
2368 * If we haven't opened the file for O_WRONLY, we
2369 * need to in the size_change case to obtain a stateid.
2370 *
2371 * Got race?
2372 * Because OPEN is always done by name in nfsv4, it is
2373 * possible that we opened a different file by the same
2374 * name. We can recognize this race condition, but we
2375 * can't do anything about it besides returning an error.
2376 *
2377 * This will be fixed with VFS changes (lookup-intent).
2378 */
2379static int
2380nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2381 struct iattr *sattr)
2382{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002383 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002384 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002385 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 int status;
2387
Benny Halevy8a1636c2010-07-14 15:43:57 -04002388 if (pnfs_ld_layoutret_on_setattr(inode))
2389 pnfs_return_layout(inode);
2390
Trond Myklebust0e574af2005-10-27 22:12:38 -04002391 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
Trond Myklebustd5308382005-11-04 15:33:38 -05002393 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002394 if (sattr->ia_valid & ATTR_FILE) {
2395 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002396
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002397 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002398 if (ctx) {
2399 cred = ctx->cred;
2400 state = ctx->state;
2401 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002402 }
2403
2404 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002405 if (status == 0)
2406 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 return status;
2408}
2409
Bryan Schumaker7c513052011-03-24 17:12:24 +00002410static int _nfs4_proc_lookupfh(struct rpc_clnt *clnt, struct nfs_server *server,
2411 const struct nfs_fh *dirfh, const struct qstr *name,
2412 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002413{
2414 int status;
2415 struct nfs4_lookup_arg args = {
2416 .bitmask = server->attr_bitmask,
2417 .dir_fh = dirfh,
2418 .name = name,
2419 };
2420 struct nfs4_lookup_res res = {
2421 .server = server,
2422 .fattr = fattr,
2423 .fh = fhandle,
2424 };
2425 struct rpc_message msg = {
2426 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2427 .rpc_argp = &args,
2428 .rpc_resp = &res,
2429 };
2430
2431 nfs_fattr_init(fattr);
2432
2433 dprintk("NFS call lookupfh %s\n", name->name);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002434 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002435 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002436 return status;
2437}
2438
2439static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2440 struct qstr *name, struct nfs_fh *fhandle,
2441 struct nfs_fattr *fattr)
2442{
2443 struct nfs4_exception exception = { };
2444 int err;
2445 do {
Bryan Schumaker7c513052011-03-24 17:12:24 +00002446 err = _nfs4_proc_lookupfh(server->client, server, dirfh, name, fhandle, fattr);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002447 /* FIXME: !!!! */
2448 if (err == -NFS4ERR_MOVED) {
2449 err = -EREMOTE;
2450 break;
2451 }
2452 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002453 } while (exception.retry);
2454 return err;
2455}
2456
Bryan Schumaker7c513052011-03-24 17:12:24 +00002457static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2458 const struct qstr *name, struct nfs_fh *fhandle,
2459 struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002461 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
2463 dprintk("NFS call lookup %s\n", name->name);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002464 status = _nfs4_proc_lookupfh(clnt, NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002465 if (status == -NFS4ERR_MOVED)
2466 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 dprintk("NFS reply lookup: %d\n", status);
2468 return status;
2469}
2470
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002471void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2472{
2473 memset(fh, 0, sizeof(struct nfs_fh));
2474 fattr->fsid.major = 1;
2475 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2476 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2477 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2478 fattr->nlink = 2;
2479}
2480
Bryan Schumaker7c513052011-03-24 17:12:24 +00002481static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2482 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483{
2484 struct nfs4_exception exception = { };
2485 int err;
2486 do {
2487 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumaker7c513052011-03-24 17:12:24 +00002488 _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 &exception);
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002490 if (err == -EPERM)
2491 nfs_fixup_secinfo_attributes(fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 } while (exception.retry);
2493 return err;
2494}
2495
2496static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2497{
Trond Myklebust76b32992007-08-10 17:45:11 -04002498 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 struct nfs4_accessargs args = {
2500 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002501 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002503 struct nfs4_accessres res = {
2504 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002505 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 struct rpc_message msg = {
2507 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2508 .rpc_argp = &args,
2509 .rpc_resp = &res,
2510 .rpc_cred = entry->cred,
2511 };
2512 int mode = entry->mask;
2513 int status;
2514
2515 /*
2516 * Determine which access bits we want to ask for...
2517 */
2518 if (mode & MAY_READ)
2519 args.access |= NFS4_ACCESS_READ;
2520 if (S_ISDIR(inode->i_mode)) {
2521 if (mode & MAY_WRITE)
2522 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2523 if (mode & MAY_EXEC)
2524 args.access |= NFS4_ACCESS_LOOKUP;
2525 } else {
2526 if (mode & MAY_WRITE)
2527 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2528 if (mode & MAY_EXEC)
2529 args.access |= NFS4_ACCESS_EXECUTE;
2530 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002531
2532 res.fattr = nfs_alloc_fattr();
2533 if (res.fattr == NULL)
2534 return -ENOMEM;
2535
Bryan Schumaker7c513052011-03-24 17:12:24 +00002536 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 if (!status) {
2538 entry->mask = 0;
2539 if (res.access & NFS4_ACCESS_READ)
2540 entry->mask |= MAY_READ;
2541 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2542 entry->mask |= MAY_WRITE;
2543 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2544 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002545 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002547 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 return status;
2549}
2550
2551static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2552{
2553 struct nfs4_exception exception = { };
2554 int err;
2555 do {
2556 err = nfs4_handle_exception(NFS_SERVER(inode),
2557 _nfs4_proc_access(inode, entry),
2558 &exception);
2559 } while (exception.retry);
2560 return err;
2561}
2562
2563/*
2564 * TODO: For the time being, we don't try to get any attributes
2565 * along with any of the zero-copy operations READ, READDIR,
2566 * READLINK, WRITE.
2567 *
2568 * In the case of the first three, we want to put the GETATTR
2569 * after the read-type operation -- this is because it is hard
2570 * to predict the length of a GETATTR response in v4, and thus
2571 * align the READ data correctly. This means that the GETATTR
2572 * may end up partially falling into the page cache, and we should
2573 * shift it into the 'tail' of the xdr_buf before processing.
2574 * To do this efficiently, we need to know the total length
2575 * of data received, which doesn't seem to be available outside
2576 * of the RPC layer.
2577 *
2578 * In the case of WRITE, we also want to put the GETATTR after
2579 * the operation -- in this case because we want to make sure
2580 * we get the post-operation mtime and size. This means that
2581 * we can't use xdr_encode_pages() as written: we need a variant
2582 * of it which would leave room in the 'tail' iovec.
2583 *
2584 * Both of these changes to the XDR layer would in fact be quite
2585 * minor, but I decided to leave them for a subsequent patch.
2586 */
2587static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2588 unsigned int pgbase, unsigned int pglen)
2589{
2590 struct nfs4_readlink args = {
2591 .fh = NFS_FH(inode),
2592 .pgbase = pgbase,
2593 .pglen = pglen,
2594 .pages = &page,
2595 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002596 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 struct rpc_message msg = {
2598 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2599 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002600 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 };
2602
Bryan Schumaker7c513052011-03-24 17:12:24 +00002603 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 -07002604}
2605
2606static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2607 unsigned int pgbase, unsigned int pglen)
2608{
2609 struct nfs4_exception exception = { };
2610 int err;
2611 do {
2612 err = nfs4_handle_exception(NFS_SERVER(inode),
2613 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2614 &exception);
2615 } while (exception.retry);
2616 return err;
2617}
2618
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619/*
2620 * Got race?
2621 * We will need to arrange for the VFS layer to provide an atomic open.
2622 * Until then, this create/open method is prone to inefficiency and race
2623 * conditions due to the lookup, create, and open VFS calls from sys_open()
2624 * placed on the wire.
2625 *
2626 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2627 * The file will be opened again in the subsequent VFS open call
2628 * (nfs4_proc_file_open).
2629 *
2630 * The open for read will just hang around to be used by any process that
2631 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2632 */
2633
2634static int
2635nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002636 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637{
Al Viro82a2c1b2011-06-22 18:30:55 -04002638 struct dentry *de = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002640 struct rpc_cred *cred = NULL;
2641 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 int status = 0;
2643
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002644 if (ctx != NULL) {
2645 cred = ctx->cred;
Al Viro3d4ff432011-06-22 18:40:12 -04002646 de = ctx->dentry;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002647 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002649 sattr->ia_mode &= ~current_umask();
Al Viro82a2c1b2011-06-22 18:30:55 -04002650 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002651 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 if (IS_ERR(state)) {
2653 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002654 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002656 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002657 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002658 if (ctx != NULL)
2659 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002660 else
Al Viro643168c2011-06-22 18:20:23 -04002661 nfs4_close_sync(state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662out:
2663 return status;
2664}
2665
2666static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2667{
Trond Myklebust16e42952005-10-27 22:12:44 -04002668 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002669 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002671 .name.len = name->len,
2672 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002673 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002675 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002676 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002677 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002679 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2680 .rpc_argp = &args,
2681 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002683 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
Trond Myklebustd3468902010-04-16 16:22:50 -04002685 res.dir_attr = nfs_alloc_fattr();
2686 if (res.dir_attr == NULL)
2687 goto out;
2688
Bryan Schumaker7c513052011-03-24 17:12:24 +00002689 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002690 if (status == 0) {
2691 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002692 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002693 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002694 nfs_free_fattr(res.dir_attr);
2695out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 return status;
2697}
2698
2699static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2700{
2701 struct nfs4_exception exception = { };
2702 int err;
2703 do {
2704 err = nfs4_handle_exception(NFS_SERVER(dir),
2705 _nfs4_proc_remove(dir, name),
2706 &exception);
2707 } while (exception.retry);
2708 return err;
2709}
2710
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002711static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002713 struct nfs_server *server = NFS_SERVER(dir);
2714 struct nfs_removeargs *args = msg->rpc_argp;
2715 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716
Trond Myklebusta65318b2009-03-11 14:10:28 -04002717 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002718 res->server = server;
Benny Halevydfb4f302010-09-24 09:17:01 -04002719 res->seq_res.sr_slot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721}
2722
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002723static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002725 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2726
Trond Myklebust14516c32010-07-31 14:29:06 -04002727 if (!nfs4_sequence_done(task, &res->seq_res))
2728 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002729 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002730 return 0;
2731 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002732 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002733 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734}
2735
Jeff Laytond3d41522010-09-17 17:31:57 -04002736static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2737{
2738 struct nfs_server *server = NFS_SERVER(dir);
2739 struct nfs_renameargs *arg = msg->rpc_argp;
2740 struct nfs_renameres *res = msg->rpc_resp;
2741
2742 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2743 arg->bitmask = server->attr_bitmask;
2744 res->server = server;
2745}
2746
2747static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2748 struct inode *new_dir)
2749{
2750 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2751
2752 if (!nfs4_sequence_done(task, &res->seq_res))
2753 return 0;
2754 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2755 return 0;
2756
2757 update_changeattr(old_dir, &res->old_cinfo);
2758 nfs_post_op_update_inode(old_dir, res->old_fattr);
2759 update_changeattr(new_dir, &res->new_cinfo);
2760 nfs_post_op_update_inode(new_dir, res->new_fattr);
2761 return 1;
2762}
2763
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2765 struct inode *new_dir, struct qstr *new_name)
2766{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002767 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002768 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 .old_dir = NFS_FH(old_dir),
2770 .new_dir = NFS_FH(new_dir),
2771 .old_name = old_name,
2772 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002773 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002775 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002776 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002777 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 struct rpc_message msg = {
2779 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2780 .rpc_argp = &arg,
2781 .rpc_resp = &res,
2782 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002783 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
Trond Myklebust011fff72010-04-16 16:22:49 -04002785 res.old_fattr = nfs_alloc_fattr();
2786 res.new_fattr = nfs_alloc_fattr();
2787 if (res.old_fattr == NULL || res.new_fattr == NULL)
2788 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789
Bryan Schumaker7c513052011-03-24 17:12:24 +00002790 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 if (!status) {
2792 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002793 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002795 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002797out:
2798 nfs_free_fattr(res.new_fattr);
2799 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 return status;
2801}
2802
2803static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2804 struct inode *new_dir, struct qstr *new_name)
2805{
2806 struct nfs4_exception exception = { };
2807 int err;
2808 do {
2809 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2810 _nfs4_proc_rename(old_dir, old_name,
2811 new_dir, new_name),
2812 &exception);
2813 } while (exception.retry);
2814 return err;
2815}
2816
2817static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2818{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002819 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 struct nfs4_link_arg arg = {
2821 .fh = NFS_FH(inode),
2822 .dir_fh = NFS_FH(dir),
2823 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002824 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002826 struct nfs4_link_res res = {
2827 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002828 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 struct rpc_message msg = {
2830 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2831 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002832 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002834 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
Trond Myklebust136f2622010-04-16 16:22:49 -04002836 res.fattr = nfs_alloc_fattr();
2837 res.dir_attr = nfs_alloc_fattr();
2838 if (res.fattr == NULL || res.dir_attr == NULL)
2839 goto out;
2840
Bryan Schumaker7c513052011-03-24 17:12:24 +00002841 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002842 if (!status) {
2843 update_changeattr(dir, &res.cinfo);
2844 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002845 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002846 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002847out:
2848 nfs_free_fattr(res.dir_attr);
2849 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 return status;
2851}
2852
2853static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2854{
2855 struct nfs4_exception exception = { };
2856 int err;
2857 do {
2858 err = nfs4_handle_exception(NFS_SERVER(inode),
2859 _nfs4_proc_link(inode, dir, name),
2860 &exception);
2861 } while (exception.retry);
2862 return err;
2863}
2864
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002865struct nfs4_createdata {
2866 struct rpc_message msg;
2867 struct nfs4_create_arg arg;
2868 struct nfs4_create_res res;
2869 struct nfs_fh fh;
2870 struct nfs_fattr fattr;
2871 struct nfs_fattr dir_fattr;
2872};
2873
2874static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2875 struct qstr *name, struct iattr *sattr, u32 ftype)
2876{
2877 struct nfs4_createdata *data;
2878
2879 data = kzalloc(sizeof(*data), GFP_KERNEL);
2880 if (data != NULL) {
2881 struct nfs_server *server = NFS_SERVER(dir);
2882
2883 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2884 data->msg.rpc_argp = &data->arg;
2885 data->msg.rpc_resp = &data->res;
2886 data->arg.dir_fh = NFS_FH(dir);
2887 data->arg.server = server;
2888 data->arg.name = name;
2889 data->arg.attrs = sattr;
2890 data->arg.ftype = ftype;
2891 data->arg.bitmask = server->attr_bitmask;
2892 data->res.server = server;
2893 data->res.fh = &data->fh;
2894 data->res.fattr = &data->fattr;
2895 data->res.dir_fattr = &data->dir_fattr;
2896 nfs_fattr_init(data->res.fattr);
2897 nfs_fattr_init(data->res.dir_fattr);
2898 }
2899 return data;
2900}
2901
2902static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2903{
Bryan Schumaker7c513052011-03-24 17:12:24 +00002904 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00002905 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002906 if (status == 0) {
2907 update_changeattr(dir, &data->res.dir_cinfo);
2908 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2909 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2910 }
2911 return status;
2912}
2913
2914static void nfs4_free_createdata(struct nfs4_createdata *data)
2915{
2916 kfree(data);
2917}
2918
Chuck Lever4f390c12006-08-22 20:06:22 -04002919static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002920 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002922 struct nfs4_createdata *data;
2923 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
Chuck Lever94a6d752006-08-22 20:06:23 -04002925 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002926 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002927
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002928 status = -ENOMEM;
2929 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2930 if (data == NULL)
2931 goto out;
2932
2933 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2934 data->arg.u.symlink.pages = &page;
2935 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002937 status = nfs4_do_create(dir, dentry, data);
2938
2939 nfs4_free_createdata(data);
2940out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 return status;
2942}
2943
Chuck Lever4f390c12006-08-22 20:06:22 -04002944static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002945 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946{
2947 struct nfs4_exception exception = { };
2948 int err;
2949 do {
2950 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002951 _nfs4_proc_symlink(dir, dentry, page,
2952 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 &exception);
2954 } while (exception.retry);
2955 return err;
2956}
2957
2958static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2959 struct iattr *sattr)
2960{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002961 struct nfs4_createdata *data;
2962 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002964 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2965 if (data == NULL)
2966 goto out;
2967
2968 status = nfs4_do_create(dir, dentry, data);
2969
2970 nfs4_free_createdata(data);
2971out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 return status;
2973}
2974
2975static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2976 struct iattr *sattr)
2977{
2978 struct nfs4_exception exception = { };
2979 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002980
2981 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 do {
2983 err = nfs4_handle_exception(NFS_SERVER(dir),
2984 _nfs4_proc_mkdir(dir, dentry, sattr),
2985 &exception);
2986 } while (exception.retry);
2987 return err;
2988}
2989
2990static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002991 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992{
2993 struct inode *dir = dentry->d_inode;
2994 struct nfs4_readdir_arg args = {
2995 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002996 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 .pgbase = 0,
2998 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002999 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003000 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 };
3002 struct nfs4_readdir_res res;
3003 struct rpc_message msg = {
3004 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3005 .rpc_argp = &args,
3006 .rpc_resp = &res,
3007 .rpc_cred = cred,
3008 };
3009 int status;
3010
Harvey Harrison3110ff82008-05-02 13:42:44 -07003011 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003012 dentry->d_parent->d_name.name,
3013 dentry->d_name.name,
3014 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3016 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003017 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 -05003018 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003020 status += args.pgbase;
3021 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003022
3023 nfs_invalidate_atime(dir);
3024
Harvey Harrison3110ff82008-05-02 13:42:44 -07003025 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 return status;
3027}
3028
3029static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003030 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031{
3032 struct nfs4_exception exception = { };
3033 int err;
3034 do {
3035 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3036 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003037 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 &exception);
3039 } while (exception.retry);
3040 return err;
3041}
3042
3043static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3044 struct iattr *sattr, dev_t rdev)
3045{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003046 struct nfs4_createdata *data;
3047 int mode = sattr->ia_mode;
3048 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049
3050 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3051 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003052
3053 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3054 if (data == NULL)
3055 goto out;
3056
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003058 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003060 data->arg.ftype = NF4BLK;
3061 data->arg.u.device.specdata1 = MAJOR(rdev);
3062 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 }
3064 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003065 data->arg.ftype = NF4CHR;
3066 data->arg.u.device.specdata1 = MAJOR(rdev);
3067 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003070 status = nfs4_do_create(dir, dentry, data);
3071
3072 nfs4_free_createdata(data);
3073out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 return status;
3075}
3076
3077static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3078 struct iattr *sattr, dev_t rdev)
3079{
3080 struct nfs4_exception exception = { };
3081 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003082
3083 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 do {
3085 err = nfs4_handle_exception(NFS_SERVER(dir),
3086 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3087 &exception);
3088 } while (exception.retry);
3089 return err;
3090}
3091
3092static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3093 struct nfs_fsstat *fsstat)
3094{
3095 struct nfs4_statfs_arg args = {
3096 .fh = fhandle,
3097 .bitmask = server->attr_bitmask,
3098 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003099 struct nfs4_statfs_res res = {
3100 .fsstat = fsstat,
3101 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 struct rpc_message msg = {
3103 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3104 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003105 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 };
3107
Trond Myklebust0e574af2005-10-27 22:12:38 -04003108 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003109 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110}
3111
3112static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3113{
3114 struct nfs4_exception exception = { };
3115 int err;
3116 do {
3117 err = nfs4_handle_exception(server,
3118 _nfs4_proc_statfs(server, fhandle, fsstat),
3119 &exception);
3120 } while (exception.retry);
3121 return err;
3122}
3123
3124static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3125 struct nfs_fsinfo *fsinfo)
3126{
3127 struct nfs4_fsinfo_arg args = {
3128 .fh = fhandle,
3129 .bitmask = server->attr_bitmask,
3130 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003131 struct nfs4_fsinfo_res res = {
3132 .fsinfo = fsinfo,
3133 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 struct rpc_message msg = {
3135 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3136 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003137 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 };
3139
Bryan Schumaker7c513052011-03-24 17:12:24 +00003140 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141}
3142
3143static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3144{
3145 struct nfs4_exception exception = { };
3146 int err;
3147
3148 do {
3149 err = nfs4_handle_exception(server,
3150 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3151 &exception);
3152 } while (exception.retry);
3153 return err;
3154}
3155
3156static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3157{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003158 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3160}
3161
3162static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3163 struct nfs_pathconf *pathconf)
3164{
3165 struct nfs4_pathconf_arg args = {
3166 .fh = fhandle,
3167 .bitmask = server->attr_bitmask,
3168 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003169 struct nfs4_pathconf_res res = {
3170 .pathconf = pathconf,
3171 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 struct rpc_message msg = {
3173 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3174 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003175 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 };
3177
3178 /* None of the pathconf attributes are mandatory to implement */
3179 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3180 memset(pathconf, 0, sizeof(*pathconf));
3181 return 0;
3182 }
3183
Trond Myklebust0e574af2005-10-27 22:12:38 -04003184 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003185 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186}
3187
3188static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3189 struct nfs_pathconf *pathconf)
3190{
3191 struct nfs4_exception exception = { };
3192 int err;
3193
3194 do {
3195 err = nfs4_handle_exception(server,
3196 _nfs4_proc_pathconf(server, fhandle, pathconf),
3197 &exception);
3198 } while (exception.retry);
3199 return err;
3200}
3201
Benny Halevyd20581a2011-05-22 19:52:03 +03003202void __nfs4_read_done_cb(struct nfs_read_data *data)
3203{
3204 nfs_invalidate_atime(data->inode);
3205}
3206
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003207static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208{
Trond Myklebustec06c092006-03-20 13:44:27 -05003209 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003211 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003212 nfs_restart_rpc(task, server->nfs_client);
Trond Myklebustec06c092006-03-20 13:44:27 -05003213 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003215
Benny Halevyd20581a2011-05-22 19:52:03 +03003216 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003218 renew_lease(server, data->timestamp);
3219 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220}
3221
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003222static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3223{
3224
3225 dprintk("--> %s\n", __func__);
3226
3227 if (!nfs4_sequence_done(task, &data->res.seq_res))
3228 return -EAGAIN;
3229
Benny Halevyd20581a2011-05-22 19:52:03 +03003230 return data->read_done_cb ? data->read_done_cb(task, data) :
3231 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003232}
3233
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003234static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003237 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003238 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239}
3240
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003241/* Reset the the nfs_read_data to send the read to the MDS. */
3242void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3243{
3244 dprintk("%s Reset task for i/o through\n", __func__);
3245 put_lseg(data->lseg);
3246 data->lseg = NULL;
3247 /* offsets will differ in the dense stripe case */
3248 data->args.offset = data->mds_offset;
3249 data->ds_clp = NULL;
3250 data->args.fh = NFS_FH(data->inode);
3251 data->read_done_cb = nfs4_read_done_cb;
3252 task->tk_ops = data->mds_ops;
3253 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3254}
3255EXPORT_SYMBOL_GPL(nfs4_reset_read);
3256
Fred Isamanb029bc92011-03-03 15:13:42 +00003257static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 struct inode *inode = data->inode;
3260
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003261 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003262 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003263 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003265 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003267 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003268 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003269 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270}
3271
Fred Isamanb029bc92011-03-03 15:13:42 +00003272static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3273{
3274 if (!nfs4_sequence_done(task, &data->res.seq_res))
3275 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003276 return data->write_done_cb ? data->write_done_cb(task, data) :
3277 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003278}
3279
Fred Isamana69aef12011-03-03 15:13:47 +00003280/* Reset the the nfs_write_data to send the write to the MDS. */
3281void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3282{
3283 dprintk("%s Reset task for i/o through\n", __func__);
3284 put_lseg(data->lseg);
3285 data->lseg = NULL;
3286 data->ds_clp = NULL;
3287 data->write_done_cb = nfs4_write_done_cb;
3288 data->args.fh = NFS_FH(data->inode);
3289 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3290 data->args.offset = data->mds_offset;
3291 data->res.fattr = &data->fattr;
3292 task->tk_ops = data->mds_ops;
3293 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3294}
3295EXPORT_SYMBOL_GPL(nfs4_reset_write);
3296
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003297static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003299 struct nfs_server *server = NFS_SERVER(data->inode);
3300
Fred Isaman7ffd1062011-03-03 15:13:46 +00003301 if (data->lseg) {
3302 data->args.bitmask = NULL;
3303 data->res.fattr = NULL;
3304 } else
3305 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isamanb029bc92011-03-03 15:13:42 +00003306 if (!data->write_done_cb)
3307 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003308 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 data->timestamp = jiffies;
3310
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003311 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312}
3313
Fred Isaman5f452432011-03-23 13:27:46 +00003314static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003317
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003318 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003319 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003320 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003322 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003323 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324}
3325
Fred Isaman5f452432011-03-23 13:27:46 +00003326static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3327{
3328 if (!nfs4_sequence_done(task, &data->res.seq_res))
3329 return -EAGAIN;
3330 return data->write_done_cb(task, data);
3331}
3332
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003333static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003335 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003336
3337 if (data->lseg) {
3338 data->args.bitmask = NULL;
3339 data->res.fattr = NULL;
3340 } else
3341 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isaman5f452432011-03-23 13:27:46 +00003342 if (!data->write_done_cb)
3343 data->write_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003344 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003345 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346}
3347
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003348struct nfs4_renewdata {
3349 struct nfs_client *client;
3350 unsigned long timestamp;
3351};
3352
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353/*
3354 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3355 * standalone procedure for queueing an asynchronous RENEW.
3356 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003357static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003358{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003359 struct nfs4_renewdata *data = calldata;
3360 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003361
Alexandros Batsakis0851de02010-02-05 03:45:06 -08003362 if (atomic_read(&clp->cl_count) > 1)
3363 nfs4_schedule_state_renewal(clp);
3364 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003365 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003366}
3367
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003368static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003370 struct nfs4_renewdata *data = calldata;
3371 struct nfs_client *clp = data->client;
3372 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373
3374 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003375 /* Unless we're shutting down, schedule state recovery! */
3376 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003377 nfs4_schedule_lease_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 return;
3379 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003380 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381}
3382
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003383static const struct rpc_call_ops nfs4_renew_ops = {
3384 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003385 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003386};
3387
David Howellsadfa6f92006-08-22 20:06:08 -04003388int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389{
3390 struct rpc_message msg = {
3391 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3392 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003393 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003395 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396
Alexandros Batsakis0851de02010-02-05 03:45:06 -08003397 if (!atomic_inc_not_zero(&clp->cl_count))
3398 return -EIO;
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003399 data = kmalloc(sizeof(*data), GFP_KERNEL);
3400 if (data == NULL)
3401 return -ENOMEM;
3402 data->client = clp;
3403 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003405 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406}
3407
David Howellsadfa6f92006-08-22 20:06:08 -04003408int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409{
3410 struct rpc_message msg = {
3411 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3412 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003413 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 };
3415 unsigned long now = jiffies;
3416 int status;
3417
3418 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3419 if (status < 0)
3420 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003421 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 return 0;
3423}
3424
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003425static inline int nfs4_server_supports_acls(struct nfs_server *server)
3426{
3427 return (server->caps & NFS_CAP_ACLS)
3428 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3429 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3430}
3431
3432/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3433 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3434 * the stack.
3435 */
3436#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3437
3438static void buf_to_pages(const void *buf, size_t buflen,
3439 struct page **pages, unsigned int *pgbase)
3440{
3441 const void *p = buf;
3442
3443 *pgbase = offset_in_page(buf);
3444 p -= *pgbase;
3445 while (p < buf + buflen) {
3446 *(pages++) = virt_to_page(p);
3447 p += PAGE_CACHE_SIZE;
3448 }
3449}
3450
Neil Hormane9e3d722011-03-04 19:26:03 -05003451static int buf_to_pages_noslab(const void *buf, size_t buflen,
3452 struct page **pages, unsigned int *pgbase)
3453{
3454 struct page *newpage, **spages;
3455 int rc = 0;
3456 size_t len;
3457 spages = pages;
3458
3459 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003460 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003461 newpage = alloc_page(GFP_KERNEL);
3462
3463 if (newpage == NULL)
3464 goto unwind;
3465 memcpy(page_address(newpage), buf, len);
3466 buf += len;
3467 buflen -= len;
3468 *pages++ = newpage;
3469 rc++;
3470 } while (buflen != 0);
3471
3472 return rc;
3473
3474unwind:
3475 for(; rc > 0; rc--)
3476 __free_page(spages[rc-1]);
3477 return -ENOMEM;
3478}
3479
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003480struct nfs4_cached_acl {
3481 int cached;
3482 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003483 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003484};
3485
3486static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003487{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003488 struct nfs_inode *nfsi = NFS_I(inode);
3489
3490 spin_lock(&inode->i_lock);
3491 kfree(nfsi->nfs4_acl);
3492 nfsi->nfs4_acl = acl;
3493 spin_unlock(&inode->i_lock);
3494}
3495
3496static void nfs4_zap_acl_attr(struct inode *inode)
3497{
3498 nfs4_set_cached_acl(inode, NULL);
3499}
3500
3501static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3502{
3503 struct nfs_inode *nfsi = NFS_I(inode);
3504 struct nfs4_cached_acl *acl;
3505 int ret = -ENOENT;
3506
3507 spin_lock(&inode->i_lock);
3508 acl = nfsi->nfs4_acl;
3509 if (acl == NULL)
3510 goto out;
3511 if (buf == NULL) /* user is just asking for length */
3512 goto out_len;
3513 if (acl->cached == 0)
3514 goto out;
3515 ret = -ERANGE; /* see getxattr(2) man page */
3516 if (acl->len > buflen)
3517 goto out;
3518 memcpy(buf, acl->data, acl->len);
3519out_len:
3520 ret = acl->len;
3521out:
3522 spin_unlock(&inode->i_lock);
3523 return ret;
3524}
3525
3526static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3527{
3528 struct nfs4_cached_acl *acl;
3529
3530 if (buf && acl_len <= PAGE_SIZE) {
3531 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3532 if (acl == NULL)
3533 goto out;
3534 acl->cached = 1;
3535 memcpy(acl->data, buf, acl_len);
3536 } else {
3537 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3538 if (acl == NULL)
3539 goto out;
3540 acl->cached = 0;
3541 }
3542 acl->len = acl_len;
3543out:
3544 nfs4_set_cached_acl(inode, acl);
3545}
3546
Trond Myklebust16b42892006-08-24 12:27:15 -04003547static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003548{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003549 struct page *pages[NFS4ACL_MAXPAGES];
3550 struct nfs_getaclargs args = {
3551 .fh = NFS_FH(inode),
3552 .acl_pages = pages,
3553 .acl_len = buflen,
3554 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003555 struct nfs_getaclres res = {
3556 .acl_len = buflen,
3557 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003558 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003559 struct rpc_message msg = {
3560 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3561 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003562 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003563 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003564 struct page *localpage = NULL;
3565 int ret;
3566
3567 if (buflen < PAGE_SIZE) {
3568 /* As long as we're doing a round trip to the server anyway,
3569 * let's be prepared for a page of acl data. */
3570 localpage = alloc_page(GFP_KERNEL);
3571 resp_buf = page_address(localpage);
3572 if (localpage == NULL)
3573 return -ENOMEM;
3574 args.acl_pages[0] = localpage;
3575 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003576 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003577 } else {
3578 resp_buf = buf;
3579 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3580 }
Bryan Schumaker7c513052011-03-24 17:12:24 +00003581 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 +00003582 if (ret)
3583 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003584 if (res.acl_len > args.acl_len)
3585 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003586 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003587 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003588 if (buf) {
3589 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003590 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003591 goto out_free;
3592 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003593 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003594 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003595 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003596out_free:
3597 if (localpage)
3598 __free_page(localpage);
3599 return ret;
3600}
3601
Trond Myklebust16b42892006-08-24 12:27:15 -04003602static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3603{
3604 struct nfs4_exception exception = { };
3605 ssize_t ret;
3606 do {
3607 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3608 if (ret >= 0)
3609 break;
3610 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3611 } while (exception.retry);
3612 return ret;
3613}
3614
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003615static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3616{
3617 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003618 int ret;
3619
3620 if (!nfs4_server_supports_acls(server))
3621 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003622 ret = nfs_revalidate_inode(server, inode);
3623 if (ret < 0)
3624 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003625 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3626 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003627 ret = nfs4_read_cached_acl(inode, buf, buflen);
3628 if (ret != -ENOENT)
3629 return ret;
3630 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003631}
3632
Trond Myklebust16b42892006-08-24 12:27:15 -04003633static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003634{
3635 struct nfs_server *server = NFS_SERVER(inode);
3636 struct page *pages[NFS4ACL_MAXPAGES];
3637 struct nfs_setaclargs arg = {
3638 .fh = NFS_FH(inode),
3639 .acl_pages = pages,
3640 .acl_len = buflen,
3641 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003642 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003643 struct rpc_message msg = {
3644 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3645 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003646 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003647 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003648 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003649
3650 if (!nfs4_server_supports_acls(server))
3651 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003652 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3653 if (i < 0)
3654 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003655 nfs_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003656 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003657
3658 /*
3659 * Free each page after tx, so the only ref left is
3660 * held by the network stack
3661 */
3662 for (; i > 0; i--)
3663 put_page(pages[i-1]);
3664
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003665 /*
3666 * Acl update can result in inode attribute update.
3667 * so mark the attribute cache invalid.
3668 */
3669 spin_lock(&inode->i_lock);
3670 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3671 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003672 nfs_access_zap_cache(inode);
3673 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003674 return ret;
3675}
3676
Trond Myklebust16b42892006-08-24 12:27:15 -04003677static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3678{
3679 struct nfs4_exception exception = { };
3680 int err;
3681 do {
3682 err = nfs4_handle_exception(NFS_SERVER(inode),
3683 __nfs4_proc_set_acl(inode, buf, buflen),
3684 &exception);
3685 } while (exception.retry);
3686 return err;
3687}
3688
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003690nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003692 struct nfs_client *clp = server->nfs_client;
3693
3694 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 return 0;
3696 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003697 case -NFS4ERR_ADMIN_REVOKED:
3698 case -NFS4ERR_BAD_STATEID:
3699 case -NFS4ERR_OPENMODE:
3700 if (state == NULL)
3701 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003702 nfs4_schedule_stateid_recovery(server, state);
3703 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003704 case -NFS4ERR_EXPIRED:
3705 if (state != NULL)
3706 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003708 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003709 nfs4_schedule_lease_recovery(clp);
3710 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003711#if defined(CONFIG_NFS_V4_1)
3712 case -NFS4ERR_BADSESSION:
3713 case -NFS4ERR_BADSLOT:
3714 case -NFS4ERR_BAD_HIGH_SLOT:
3715 case -NFS4ERR_DEADSESSION:
3716 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3717 case -NFS4ERR_SEQ_FALSE_RETRY:
3718 case -NFS4ERR_SEQ_MISORDERED:
3719 dprintk("%s ERROR %d, Reset session\n", __func__,
3720 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003721 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -04003722 task->tk_status = 0;
3723 return -EAGAIN;
3724#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003726 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003727 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003728 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3730 task->tk_status = 0;
3731 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003732 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 case -NFS4ERR_OLD_STATEID:
3734 task->tk_status = 0;
3735 return -EAGAIN;
3736 }
3737 task->tk_status = nfs4_map_errors(task->tk_status);
3738 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003739wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003740 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003741 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3742 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3743 task->tk_status = 0;
3744 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745}
3746
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003747int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3748 unsigned short port, struct rpc_cred *cred,
3749 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750{
3751 nfs4_verifier sc_verifier;
3752 struct nfs4_setclientid setclientid = {
3753 .sc_verifier = &sc_verifier,
3754 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003755 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 };
3757 struct rpc_message msg = {
3758 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3759 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003760 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003761 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 };
Al Virobc4785c2006-10-19 23:28:51 -07003763 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 int loop = 0;
3765 int status;
3766
Al Virobc4785c2006-10-19 23:28:51 -07003767 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3769 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3770
3771 for(;;) {
3772 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003773 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003774 clp->cl_ipaddr,
3775 rpc_peeraddr2str(clp->cl_rpcclient,
3776 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003777 rpc_peeraddr2str(clp->cl_rpcclient,
3778 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003779 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 clp->cl_id_uniquifier);
3781 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003782 sizeof(setclientid.sc_netid),
3783 rpc_peeraddr2str(clp->cl_rpcclient,
3784 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003786 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 clp->cl_ipaddr, port >> 8, port & 255);
3788
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003789 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 if (status != -NFS4ERR_CLID_INUSE)
3791 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003792 if (loop != 0) {
3793 ++clp->cl_id_uniquifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003795 }
3796 ++loop;
3797 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 }
3799 return status;
3800}
3801
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003802int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003803 struct nfs4_setclientid_res *arg,
3804 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805{
3806 struct nfs_fsinfo fsinfo;
3807 struct rpc_message msg = {
3808 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003809 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003811 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 };
3813 unsigned long now;
3814 int status;
3815
3816 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003817 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 if (status == 0) {
3819 spin_lock(&clp->cl_lock);
3820 clp->cl_lease_time = fsinfo.lease_time * HZ;
3821 clp->cl_last_renewal = now;
3822 spin_unlock(&clp->cl_lock);
3823 }
3824 return status;
3825}
3826
Trond Myklebustfe650402006-01-03 09:55:18 +01003827struct nfs4_delegreturndata {
3828 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003829 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003830 struct nfs_fh fh;
3831 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003832 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003833 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003834 int rpc_status;
3835};
3836
Trond Myklebustfe650402006-01-03 09:55:18 +01003837static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3838{
3839 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003840
Trond Myklebust14516c32010-07-31 14:29:06 -04003841 if (!nfs4_sequence_done(task, &data->res.seq_res))
3842 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003843
Ricardo Labiaga79708862009-12-07 09:23:21 -05003844 switch (task->tk_status) {
3845 case -NFS4ERR_STALE_STATEID:
3846 case -NFS4ERR_EXPIRED:
3847 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003848 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003849 break;
3850 default:
3851 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3852 -EAGAIN) {
3853 nfs_restart_rpc(task, data->res.server->nfs_client);
3854 return;
3855 }
3856 }
3857 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003858}
3859
3860static void nfs4_delegreturn_release(void *calldata)
3861{
Trond Myklebustfe650402006-01-03 09:55:18 +01003862 kfree(calldata);
3863}
3864
Andy Adamson938e1012009-04-01 09:22:28 -04003865#if defined(CONFIG_NFS_V4_1)
3866static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3867{
3868 struct nfs4_delegreturndata *d_data;
3869
3870 d_data = (struct nfs4_delegreturndata *)data;
3871
Trond Myklebust035168a2010-06-16 09:52:26 -04003872 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003873 &d_data->args.seq_args,
3874 &d_data->res.seq_res, 1, task))
3875 return;
3876 rpc_call_start(task);
3877}
3878#endif /* CONFIG_NFS_V4_1 */
3879
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003880static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003881#if defined(CONFIG_NFS_V4_1)
3882 .rpc_call_prepare = nfs4_delegreturn_prepare,
3883#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003884 .rpc_call_done = nfs4_delegreturn_done,
3885 .rpc_release = nfs4_delegreturn_release,
3886};
3887
Trond Myklebuste6f81072008-01-24 18:14:34 -05003888static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003889{
3890 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003891 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003892 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003893 struct rpc_message msg = {
3894 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3895 .rpc_cred = cred,
3896 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003897 struct rpc_task_setup task_setup_data = {
3898 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003899 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003900 .callback_ops = &nfs4_delegreturn_ops,
3901 .flags = RPC_TASK_ASYNC,
3902 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003903 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003904
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003905 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003906 if (data == NULL)
3907 return -ENOMEM;
3908 data->args.fhandle = &data->fh;
3909 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003910 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003911 nfs_copy_fh(&data->fh, NFS_FH(inode));
3912 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003913 data->res.fattr = &data->fattr;
3914 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003915 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003916 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003917 data->rpc_status = 0;
3918
Trond Myklebustc970aa82007-07-14 15:39:59 -04003919 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05003920 msg.rpc_argp = &data->args;
3921 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003922 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003923 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003924 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003925 if (!issync)
3926 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003927 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003928 if (status != 0)
3929 goto out;
3930 status = data->rpc_status;
3931 if (status != 0)
3932 goto out;
3933 nfs_refresh_inode(inode, &data->fattr);
3934out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003935 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003936 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937}
3938
Trond Myklebuste6f81072008-01-24 18:14:34 -05003939int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940{
3941 struct nfs_server *server = NFS_SERVER(inode);
3942 struct nfs4_exception exception = { };
3943 int err;
3944 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003945 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 switch (err) {
3947 case -NFS4ERR_STALE_STATEID:
3948 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 case 0:
3950 return 0;
3951 }
3952 err = nfs4_handle_exception(server, err, &exception);
3953 } while (exception.retry);
3954 return err;
3955}
3956
3957#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3958#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3959
3960/*
3961 * sleep, with exponential backoff, and retry the LOCK operation.
3962 */
3963static unsigned long
3964nfs4_set_lock_task_retry(unsigned long timeout)
3965{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003966 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 timeout <<= 1;
3968 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3969 return NFS4_LOCK_MAXTIMEOUT;
3970 return timeout;
3971}
3972
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3974{
3975 struct inode *inode = state->inode;
3976 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003977 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003978 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003980 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003982 struct nfs_lockt_res res = {
3983 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 };
3985 struct rpc_message msg = {
3986 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3987 .rpc_argp = &arg,
3988 .rpc_resp = &res,
3989 .rpc_cred = state->owner->so_cred,
3990 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 struct nfs4_lock_state *lsp;
3992 int status;
3993
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003994 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003995 status = nfs4_set_lock_state(state, request);
3996 if (status != 0)
3997 goto out;
3998 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003999 arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004000 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004001 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004002 switch (status) {
4003 case 0:
4004 request->fl_type = F_UNLCK;
4005 break;
4006 case -NFS4ERR_DENIED:
4007 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004009 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004010out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 return status;
4012}
4013
4014static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4015{
4016 struct nfs4_exception exception = { };
4017 int err;
4018
4019 do {
4020 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4021 _nfs4_proc_getlk(state, cmd, request),
4022 &exception);
4023 } while (exception.retry);
4024 return err;
4025}
4026
4027static int do_vfs_lock(struct file *file, struct file_lock *fl)
4028{
4029 int res = 0;
4030 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4031 case FL_POSIX:
4032 res = posix_lock_file_wait(file, fl);
4033 break;
4034 case FL_FLOCK:
4035 res = flock_lock_file_wait(file, fl);
4036 break;
4037 default:
4038 BUG();
4039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 return res;
4041}
4042
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004043struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004044 struct nfs_locku_args arg;
4045 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004046 struct nfs4_lock_state *lsp;
4047 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004048 struct file_lock fl;
4049 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004050 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004051};
4052
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004053static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4054 struct nfs_open_context *ctx,
4055 struct nfs4_lock_state *lsp,
4056 struct nfs_seqid *seqid)
4057{
4058 struct nfs4_unlockdata *p;
4059 struct inode *inode = lsp->ls_state->inode;
4060
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004061 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004062 if (p == NULL)
4063 return NULL;
4064 p->arg.fh = NFS_FH(inode);
4065 p->arg.fl = &p->fl;
4066 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004067 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004068 p->arg.stateid = &lsp->ls_stateid;
4069 p->lsp = lsp;
4070 atomic_inc(&lsp->ls_count);
4071 /* Ensure we don't close file until we're done freeing locks! */
4072 p->ctx = get_nfs_open_context(ctx);
4073 memcpy(&p->fl, fl, sizeof(p->fl));
4074 p->server = NFS_SERVER(inode);
4075 return p;
4076}
4077
Trond Myklebust06f814a2006-01-03 09:55:07 +01004078static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004079{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004080 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004081 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004082 nfs4_put_lock_state(calldata->lsp);
4083 put_nfs_open_context(calldata->ctx);
4084 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004085}
4086
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004087static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004088{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004089 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004090
Trond Myklebust14516c32010-07-31 14:29:06 -04004091 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4092 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004093 switch (task->tk_status) {
4094 case 0:
4095 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004096 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004097 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01004098 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004099 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004100 case -NFS4ERR_BAD_STATEID:
4101 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004102 case -NFS4ERR_STALE_STATEID:
4103 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004104 break;
4105 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004106 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebust0110ee12009-12-07 09:00:24 -05004107 nfs_restart_rpc(task,
Trond Myklebustd61e6122009-12-05 19:32:19 -05004108 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004109 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004110}
4111
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004112static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004113{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004114 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004116 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004117 return;
4118 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004119 /* Note: exit _without_ running nfs4_locku_done */
4120 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004121 return;
4122 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004123 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004124 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004125 &calldata->arg.seq_args,
4126 &calldata->res.seq_res, 1, task))
4127 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004128 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129}
4130
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004131static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004132 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004133 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004134 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004135};
4136
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004137static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4138 struct nfs_open_context *ctx,
4139 struct nfs4_lock_state *lsp,
4140 struct nfs_seqid *seqid)
4141{
4142 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004143 struct rpc_message msg = {
4144 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4145 .rpc_cred = ctx->cred,
4146 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004147 struct rpc_task_setup task_setup_data = {
4148 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004149 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004150 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004151 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004152 .flags = RPC_TASK_ASYNC,
4153 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004154
Frank Filz137d6ac2007-07-09 15:32:29 -07004155 /* Ensure this is an unlock - when canceling a lock, the
4156 * canceled lock is passed in, and it won't be an unlock.
4157 */
4158 fl->fl_type = F_UNLCK;
4159
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004160 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4161 if (data == NULL) {
4162 nfs_free_seqid(seqid);
4163 return ERR_PTR(-ENOMEM);
4164 }
4165
Trond Myklebust1174dd12010-12-21 10:52:24 -05004166 msg.rpc_argp = &data->arg;
4167 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004168 task_setup_data.callback_data = data;
4169 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004170}
4171
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4173{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004174 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004175 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004176 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004177 struct rpc_task *task;
4178 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004179 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180
Trond Myklebust9b073572006-06-29 16:38:34 -04004181 status = nfs4_set_lock_state(state, request);
4182 /* Unlock _before_ we do the RPC call */
4183 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004184 down_read(&nfsi->rwsem);
4185 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4186 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004187 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004188 }
4189 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004190 if (status != 0)
4191 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004192 /* Is this a delegated lock? */
4193 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004194 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004195 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004196 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004197 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004198 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004199 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004200 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004201 status = PTR_ERR(task);
4202 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004203 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004204 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004205 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004206out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004207 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004208 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209}
4210
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004211struct nfs4_lockdata {
4212 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004213 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004214 struct nfs4_lock_state *lsp;
4215 struct nfs_open_context *ctx;
4216 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004217 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004218 int rpc_status;
4219 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004220 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004221};
4222
4223static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004224 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4225 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004226{
4227 struct nfs4_lockdata *p;
4228 struct inode *inode = lsp->ls_state->inode;
4229 struct nfs_server *server = NFS_SERVER(inode);
4230
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004231 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004232 if (p == NULL)
4233 return NULL;
4234
4235 p->arg.fh = NFS_FH(inode);
4236 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004237 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004238 if (p->arg.open_seqid == NULL)
4239 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004240 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004241 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004242 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004243 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004244 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004245 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004246 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004247 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004248 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004249 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004250 atomic_inc(&lsp->ls_count);
4251 p->ctx = get_nfs_open_context(ctx);
4252 memcpy(&p->fl, fl, sizeof(p->fl));
4253 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004254out_free_seqid:
4255 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004256out_free:
4257 kfree(p);
4258 return NULL;
4259}
4260
4261static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4262{
4263 struct nfs4_lockdata *data = calldata;
4264 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265
Harvey Harrison3110ff82008-05-02 13:42:44 -07004266 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004267 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4268 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004269 /* Do we need to do an open_to_lock_owner? */
4270 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004271 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4272 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004273 data->arg.open_stateid = &state->stateid;
4274 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004275 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004276 } else
4277 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004278 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004279 if (nfs4_setup_sequence(data->server,
4280 &data->arg.seq_args,
Andy Adamson66179ef2009-04-01 09:22:22 -04004281 &data->res.seq_res, 1, task))
4282 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004283 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004284 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004285}
4286
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004287static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4288{
4289 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4290 nfs4_lock_prepare(task, calldata);
4291}
4292
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004293static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4294{
4295 struct nfs4_lockdata *data = calldata;
4296
Harvey Harrison3110ff82008-05-02 13:42:44 -07004297 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004298
Trond Myklebust14516c32010-07-31 14:29:06 -04004299 if (!nfs4_sequence_done(task, &data->res.seq_res))
4300 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004301
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004302 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004303 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004304 if (data->rpc_status == 0)
4305 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4306 else
4307 goto out;
4308 }
4309 if (data->rpc_status == 0) {
4310 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4311 sizeof(data->lsp->ls_stateid.data));
4312 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Al Viro3d4ff432011-06-22 18:40:12 -04004313 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004314 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004315out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004316 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004317}
4318
4319static void nfs4_lock_release(void *calldata)
4320{
4321 struct nfs4_lockdata *data = calldata;
4322
Harvey Harrison3110ff82008-05-02 13:42:44 -07004323 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004324 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004325 if (data->cancelled != 0) {
4326 struct rpc_task *task;
4327 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4328 data->arg.lock_seqid);
4329 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004330 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004331 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004332 } else
4333 nfs_free_seqid(data->arg.lock_seqid);
4334 nfs4_put_lock_state(data->lsp);
4335 put_nfs_open_context(data->ctx);
4336 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004337 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004338}
4339
4340static const struct rpc_call_ops nfs4_lock_ops = {
4341 .rpc_call_prepare = nfs4_lock_prepare,
4342 .rpc_call_done = nfs4_lock_done,
4343 .rpc_release = nfs4_lock_release,
4344};
4345
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004346static const struct rpc_call_ops nfs4_recover_lock_ops = {
4347 .rpc_call_prepare = nfs4_recover_lock_prepare,
4348 .rpc_call_done = nfs4_lock_done,
4349 .rpc_release = nfs4_lock_release,
4350};
4351
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004352static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4353{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004354 switch (error) {
4355 case -NFS4ERR_ADMIN_REVOKED:
4356 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004357 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004358 if (new_lock_owner != 0 ||
4359 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004360 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004361 break;
4362 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004363 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004364 case -NFS4ERR_EXPIRED:
4365 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004366 };
4367}
4368
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004369static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004370{
4371 struct nfs4_lockdata *data;
4372 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004373 struct rpc_message msg = {
4374 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4375 .rpc_cred = state->owner->so_cred,
4376 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004377 struct rpc_task_setup task_setup_data = {
4378 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004379 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004380 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004381 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004382 .flags = RPC_TASK_ASYNC,
4383 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004384 int ret;
4385
Harvey Harrison3110ff82008-05-02 13:42:44 -07004386 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004387 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004388 fl->fl_u.nfs4_fl.owner,
4389 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004390 if (data == NULL)
4391 return -ENOMEM;
4392 if (IS_SETLKW(cmd))
4393 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004394 if (recovery_type > NFS_LOCK_NEW) {
4395 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004396 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004397 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4398 }
Trond Myklebust1174dd12010-12-21 10:52:24 -05004399 msg.rpc_argp = &data->arg;
4400 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004401 task_setup_data.callback_data = data;
4402 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004403 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004404 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004405 ret = nfs4_wait_for_completion_rpc_task(task);
4406 if (ret == 0) {
4407 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004408 if (ret)
4409 nfs4_handle_setlk_error(data->server, data->lsp,
4410 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004411 } else
4412 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004413 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004414 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004415 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416}
4417
4418static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4419{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004420 struct nfs_server *server = NFS_SERVER(state->inode);
4421 struct nfs4_exception exception = { };
4422 int err;
4423
4424 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004425 /* Cache the lock if possible... */
4426 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4427 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004428 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004429 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004430 break;
4431 nfs4_handle_exception(server, err, &exception);
4432 } while (exception.retry);
4433 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434}
4435
4436static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4437{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004438 struct nfs_server *server = NFS_SERVER(state->inode);
4439 struct nfs4_exception exception = { };
4440 int err;
4441
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004442 err = nfs4_set_lock_state(state, request);
4443 if (err != 0)
4444 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004445 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004446 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4447 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004448 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004449 switch (err) {
4450 default:
4451 goto out;
4452 case -NFS4ERR_GRACE:
4453 case -NFS4ERR_DELAY:
4454 nfs4_handle_exception(server, err, &exception);
4455 err = 0;
4456 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004457 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004458out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004459 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460}
4461
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004462#if defined(CONFIG_NFS_V4_1)
4463static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4464{
4465 int status;
4466 struct nfs_server *server = NFS_SERVER(state->inode);
4467
4468 status = nfs41_test_stateid(server, state);
4469 if (status == NFS_OK)
4470 return 0;
4471 nfs41_free_stateid(server, state);
4472 return nfs4_lock_expired(state, request);
4473}
4474#endif
4475
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4477{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004478 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004479 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004480 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004482 if ((fl_flags & FL_POSIX) &&
4483 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4484 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004485 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004486 status = nfs4_set_lock_state(state, request);
4487 if (status != 0)
4488 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004489 request->fl_flags |= FL_ACCESS;
4490 status = do_vfs_lock(request->fl_file, request);
4491 if (status < 0)
4492 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004493 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004494 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004495 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004496 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004497 request->fl_flags = fl_flags & ~FL_SLEEP;
4498 status = do_vfs_lock(request->fl_file, request);
4499 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004500 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004501 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004502 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004503 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004504 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004505 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004506 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004507 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004508out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004509 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004510out:
4511 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 return status;
4513}
4514
4515static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4516{
4517 struct nfs4_exception exception = { };
4518 int err;
4519
4520 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004521 err = _nfs4_proc_setlk(state, cmd, request);
4522 if (err == -NFS4ERR_DENIED)
4523 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004525 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 } while (exception.retry);
4527 return err;
4528}
4529
4530static int
4531nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4532{
4533 struct nfs_open_context *ctx;
4534 struct nfs4_state *state;
4535 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4536 int status;
4537
4538 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004539 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 state = ctx->state;
4541
4542 if (request->fl_start < 0 || request->fl_end < 0)
4543 return -EINVAL;
4544
Trond Myklebustd9531262009-07-21 19:22:38 -04004545 if (IS_GETLK(cmd)) {
4546 if (state != NULL)
4547 return nfs4_proc_getlk(state, F_GETLK, request);
4548 return 0;
4549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550
4551 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4552 return -EINVAL;
4553
Trond Myklebustd9531262009-07-21 19:22:38 -04004554 if (request->fl_type == F_UNLCK) {
4555 if (state != NULL)
4556 return nfs4_proc_unlck(state, cmd, request);
4557 return 0;
4558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559
Trond Myklebustd9531262009-07-21 19:22:38 -04004560 if (state == NULL)
4561 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 do {
4563 status = nfs4_proc_setlk(state, cmd, request);
4564 if ((status != -EAGAIN) || IS_SETLK(cmd))
4565 break;
4566 timeout = nfs4_set_lock_task_retry(timeout);
4567 status = -ERESTARTSYS;
4568 if (signalled())
4569 break;
4570 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 return status;
4572}
4573
Trond Myklebust888e6942005-11-04 15:38:11 -05004574int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4575{
4576 struct nfs_server *server = NFS_SERVER(state->inode);
4577 struct nfs4_exception exception = { };
4578 int err;
4579
4580 err = nfs4_set_lock_state(state, fl);
4581 if (err != 0)
4582 goto out;
4583 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004584 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004585 switch (err) {
4586 default:
4587 printk(KERN_ERR "%s: unhandled error %d.\n",
4588 __func__, err);
4589 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004590 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004591 goto out;
4592 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004593 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004594 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004595 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004596 nfs4_schedule_lease_recovery(server->nfs_client);
4597 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004598 case -NFS4ERR_BADSESSION:
4599 case -NFS4ERR_BADSLOT:
4600 case -NFS4ERR_BAD_HIGH_SLOT:
4601 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4602 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004603 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004604 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004605 case -ERESTARTSYS:
4606 /*
4607 * The show must go on: exit, but mark the
4608 * stateid as needing recovery.
4609 */
4610 case -NFS4ERR_ADMIN_REVOKED:
4611 case -NFS4ERR_BAD_STATEID:
4612 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004613 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004614 err = 0;
4615 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004616 case -EKEYEXPIRED:
4617 /*
4618 * User RPCSEC_GSS context has expired.
4619 * We cannot recover this stateid now, so
4620 * skip it and allow recovery thread to
4621 * proceed.
4622 */
4623 err = 0;
4624 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004625 case -ENOMEM:
4626 case -NFS4ERR_DENIED:
4627 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4628 err = 0;
4629 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004630 case -NFS4ERR_DELAY:
4631 break;
4632 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004633 err = nfs4_handle_exception(server, err, &exception);
4634 } while (exception.retry);
4635out:
4636 return err;
4637}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004638
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004639static void nfs4_release_lockowner_release(void *calldata)
4640{
4641 kfree(calldata);
4642}
4643
4644const struct rpc_call_ops nfs4_release_lockowner_ops = {
4645 .rpc_release = nfs4_release_lockowner_release,
4646};
4647
4648void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4649{
4650 struct nfs_server *server = lsp->ls_state->owner->so_server;
4651 struct nfs_release_lockowner_args *args;
4652 struct rpc_message msg = {
4653 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4654 };
4655
4656 if (server->nfs_client->cl_mvops->minor_version != 0)
4657 return;
4658 args = kmalloc(sizeof(*args), GFP_NOFS);
4659 if (!args)
4660 return;
4661 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4662 args->lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004663 args->lock_owner.s_dev = server->s_dev;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004664 msg.rpc_argp = args;
4665 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4666}
4667
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004668#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4669
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004670static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4671 const void *buf, size_t buflen,
4672 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004673{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004674 if (strcmp(key, "") != 0)
4675 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004676
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004677 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004678}
4679
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004680static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4681 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004682{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004683 if (strcmp(key, "") != 0)
4684 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004685
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004686 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004687}
4688
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004689static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4690 size_t list_len, const char *name,
4691 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004692{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004693 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004694
J. Bruce Fields096455a2006-03-20 23:23:42 -05004695 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4696 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004697
4698 if (list && len <= list_len)
4699 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004700 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004701}
4702
Andy Adamson533eb462011-06-13 18:25:56 -04004703/*
4704 * nfs_fhget will use either the mounted_on_fileid or the fileid
4705 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004706static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4707{
Andy Adamson533eb462011-06-13 18:25:56 -04004708 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4709 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4710 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4711 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004712 return;
4713
4714 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4715 NFS_ATTR_FATTR_NLINK;
4716 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4717 fattr->nlink = 2;
4718}
4719
Trond Myklebust56659e92007-07-17 21:52:39 -04004720int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004721 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004722{
4723 struct nfs_server *server = NFS_SERVER(dir);
4724 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004725 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004726 };
4727 struct nfs4_fs_locations_arg args = {
4728 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004729 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004730 .page = page,
4731 .bitmask = bitmask,
4732 };
Benny Halevy22958462009-04-01 09:22:02 -04004733 struct nfs4_fs_locations_res res = {
4734 .fs_locations = fs_locations,
4735 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004736 struct rpc_message msg = {
4737 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4738 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004739 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004740 };
4741 int status;
4742
Harvey Harrison3110ff82008-05-02 13:42:44 -07004743 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04004744
4745 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
4746 * is not supported */
4747 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
4748 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
4749 else
4750 bitmask[0] |= FATTR4_WORD0_FILEID;
4751
Trond Myklebustc228fd32007-01-13 02:28:11 -05004752 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004753 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004754 fs_locations->nlocations = 0;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004755 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004756 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004757 return status;
4758}
4759
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00004760static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4761{
4762 int status;
4763 struct nfs4_secinfo_arg args = {
4764 .dir_fh = NFS_FH(dir),
4765 .name = name,
4766 };
4767 struct nfs4_secinfo_res res = {
4768 .flavors = flavors,
4769 };
4770 struct rpc_message msg = {
4771 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
4772 .rpc_argp = &args,
4773 .rpc_resp = &res,
4774 };
4775
4776 dprintk("NFS call secinfo %s\n", name->name);
4777 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4778 dprintk("NFS reply secinfo: %d\n", status);
4779 return status;
4780}
4781
4782int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4783{
4784 struct nfs4_exception exception = { };
4785 int err;
4786 do {
4787 err = nfs4_handle_exception(NFS_SERVER(dir),
4788 _nfs4_proc_secinfo(dir, name, flavors),
4789 &exception);
4790 } while (exception.retry);
4791 return err;
4792}
4793
Andy Adamson557134a2009-04-01 09:21:53 -04004794#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004795/*
Andy Adamson357f54d2010-12-14 10:11:57 -05004796 * Check the exchange flags returned by the server for invalid flags, having
4797 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4798 * DS flags set.
4799 */
4800static int nfs4_check_cl_exchange_flags(u32 flags)
4801{
4802 if (flags & ~EXCHGID4_FLAG_MASK_R)
4803 goto out_inval;
4804 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4805 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4806 goto out_inval;
4807 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4808 goto out_inval;
4809 return NFS_OK;
4810out_inval:
4811 return -NFS4ERR_INVAL;
4812}
4813
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004814static bool
4815nfs41_same_server_scope(struct server_scope *a, struct server_scope *b)
4816{
4817 if (a->server_scope_sz == b->server_scope_sz &&
4818 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
4819 return true;
4820
4821 return false;
4822}
4823
Andy Adamson357f54d2010-12-14 10:11:57 -05004824/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04004825 * nfs4_proc_exchange_id()
4826 *
4827 * Since the clientid has expired, all compounds using sessions
4828 * associated with the stale clientid will be returning
4829 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4830 * be in some phase of session reset.
4831 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004832int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004833{
4834 nfs4_verifier verifier;
4835 struct nfs41_exchange_id_args args = {
4836 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05004837 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04004838 };
4839 struct nfs41_exchange_id_res res = {
4840 .client = clp,
4841 };
4842 int status;
4843 struct rpc_message msg = {
4844 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4845 .rpc_argp = &args,
4846 .rpc_resp = &res,
4847 .rpc_cred = cred,
4848 };
4849 __be32 *p;
4850
4851 dprintk("--> %s\n", __func__);
4852 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004853
Benny Halevy99fe60d2009-04-01 09:22:29 -04004854 p = (u32 *)verifier.data;
4855 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4856 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4857 args.verifier = &verifier;
4858
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004859 args.id_len = scnprintf(args.id, sizeof(args.id),
4860 "%s/%s.%s/%u",
4861 clp->cl_ipaddr,
4862 init_utsname()->nodename,
4863 init_utsname()->domainname,
4864 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004865
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004866 res.server_scope = kzalloc(sizeof(struct server_scope), GFP_KERNEL);
4867 if (unlikely(!res.server_scope))
4868 return -ENOMEM;
4869
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004870 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004871 if (!status)
4872 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004873
4874 if (!status) {
4875 if (clp->server_scope &&
4876 !nfs41_same_server_scope(clp->server_scope,
4877 res.server_scope)) {
4878 dprintk("%s: server_scope mismatch detected\n",
4879 __func__);
4880 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
4881 kfree(clp->server_scope);
4882 clp->server_scope = NULL;
4883 }
4884
4885 if (!clp->server_scope)
4886 clp->server_scope = res.server_scope;
4887 else
4888 kfree(res.server_scope);
4889 }
4890
Benny Halevy99fe60d2009-04-01 09:22:29 -04004891 dprintk("<-- %s status= %d\n", __func__, status);
4892 return status;
4893}
4894
Andy Adamson2050f0c2009-04-01 09:22:30 -04004895struct nfs4_get_lease_time_data {
4896 struct nfs4_get_lease_time_args *args;
4897 struct nfs4_get_lease_time_res *res;
4898 struct nfs_client *clp;
4899};
4900
4901static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4902 void *calldata)
4903{
4904 int ret;
4905 struct nfs4_get_lease_time_data *data =
4906 (struct nfs4_get_lease_time_data *)calldata;
4907
4908 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004909 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004910 /* just setup sequence, do not trigger session recovery
4911 since we're invoked within one */
4912 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004913 &data->args->la_seq_args,
4914 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004915
4916 BUG_ON(ret == -EAGAIN);
4917 rpc_call_start(task);
4918 dprintk("<-- %s\n", __func__);
4919}
4920
4921/*
4922 * Called from nfs4_state_manager thread for session setup, so don't recover
4923 * from sequence operation or clientid errors.
4924 */
4925static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4926{
4927 struct nfs4_get_lease_time_data *data =
4928 (struct nfs4_get_lease_time_data *)calldata;
4929
4930 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04004931 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4932 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04004933 switch (task->tk_status) {
4934 case -NFS4ERR_DELAY:
4935 case -NFS4ERR_GRACE:
4936 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4937 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4938 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004939 /* fall through */
4940 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust0110ee12009-12-07 09:00:24 -05004941 nfs_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004942 return;
4943 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004944 dprintk("<-- %s\n", __func__);
4945}
4946
4947struct rpc_call_ops nfs4_get_lease_time_ops = {
4948 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4949 .rpc_call_done = nfs4_get_lease_time_done,
4950};
4951
4952int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4953{
4954 struct rpc_task *task;
4955 struct nfs4_get_lease_time_args args;
4956 struct nfs4_get_lease_time_res res = {
4957 .lr_fsinfo = fsinfo,
4958 };
4959 struct nfs4_get_lease_time_data data = {
4960 .args = &args,
4961 .res = &res,
4962 .clp = clp,
4963 };
4964 struct rpc_message msg = {
4965 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4966 .rpc_argp = &args,
4967 .rpc_resp = &res,
4968 };
4969 struct rpc_task_setup task_setup = {
4970 .rpc_client = clp->cl_rpcclient,
4971 .rpc_message = &msg,
4972 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004973 .callback_data = &data,
4974 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04004975 };
4976 int status;
4977
Andy Adamson2050f0c2009-04-01 09:22:30 -04004978 dprintk("--> %s\n", __func__);
4979 task = rpc_run_task(&task_setup);
4980
4981 if (IS_ERR(task))
4982 status = PTR_ERR(task);
4983 else {
4984 status = task->tk_status;
4985 rpc_put_task(task);
4986 }
4987 dprintk("<-- %s return %d\n", __func__, status);
4988
4989 return status;
4990}
4991
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004992/*
4993 * Reset a slot table
4994 */
Andy Adamson104aeba2010-01-14 17:45:10 -05004995static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4996 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004997{
Andy Adamson104aeba2010-01-14 17:45:10 -05004998 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004999 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005000 int ret = 0;
5001
Andy Adamson104aeba2010-01-14 17:45:10 -05005002 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5003 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005004
Andy Adamson104aeba2010-01-14 17:45:10 -05005005 /* Does the newly negotiated max_reqs match the existing slot table? */
5006 if (max_reqs != tbl->max_slots) {
5007 ret = -ENOMEM;
5008 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005009 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005010 if (!new)
5011 goto out;
5012 ret = 0;
5013 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005014 }
5015 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05005016 if (new) {
5017 tbl->slots = new;
5018 tbl->max_slots = max_reqs;
5019 }
5020 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005021 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005022 spin_unlock(&tbl->slot_tbl_lock);
5023 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5024 tbl, tbl->slots, tbl->max_slots);
5025out:
5026 dprintk("<-- %s: return %d\n", __func__, ret);
5027 return ret;
5028}
5029
Andy Adamsonfc931582009-04-01 09:22:31 -04005030/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005031 * Reset the forechannel and backchannel slot tables
5032 */
5033static int nfs4_reset_slot_tables(struct nfs4_session *session)
5034{
5035 int status;
5036
5037 status = nfs4_reset_slot_table(&session->fc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05005038 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005039 if (status)
5040 return status;
5041
5042 status = nfs4_reset_slot_table(&session->bc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05005043 session->bc_attrs.max_reqs, 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005044 return status;
5045}
5046
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005047/* Destroy the slot table */
5048static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5049{
5050 if (session->fc_slot_table.slots != NULL) {
5051 kfree(session->fc_slot_table.slots);
5052 session->fc_slot_table.slots = NULL;
5053 }
5054 if (session->bc_slot_table.slots != NULL) {
5055 kfree(session->bc_slot_table.slots);
5056 session->bc_slot_table.slots = NULL;
5057 }
5058 return;
5059}
5060
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005061/*
Andy Adamsonfc931582009-04-01 09:22:31 -04005062 * Initialize slot table
5063 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005064static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
5065 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04005066{
Andy Adamsonfc931582009-04-01 09:22:31 -04005067 struct nfs4_slot *slot;
5068 int ret = -ENOMEM;
5069
5070 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
5071
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005072 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04005073
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005074 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04005075 if (!slot)
5076 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04005077 ret = 0;
5078
5079 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04005080 tbl->max_slots = max_slots;
5081 tbl->slots = slot;
5082 tbl->highest_used_slotid = -1; /* no slot is currently used */
5083 spin_unlock(&tbl->slot_tbl_lock);
5084 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5085 tbl, tbl->slots, tbl->max_slots);
5086out:
5087 dprintk("<-- %s: return %d\n", __func__, ret);
5088 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04005089}
5090
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005091/*
5092 * Initialize the forechannel and backchannel tables
5093 */
5094static int nfs4_init_slot_tables(struct nfs4_session *session)
5095{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005096 struct nfs4_slot_table *tbl;
5097 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005098
Trond Myklebustf26468f2009-12-05 19:32:11 -05005099 tbl = &session->fc_slot_table;
5100 if (tbl->slots == NULL) {
5101 status = nfs4_init_slot_table(tbl,
5102 session->fc_attrs.max_reqs, 1);
5103 if (status)
5104 return status;
5105 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005106
Trond Myklebustf26468f2009-12-05 19:32:11 -05005107 tbl = &session->bc_slot_table;
5108 if (tbl->slots == NULL) {
5109 status = nfs4_init_slot_table(tbl,
5110 session->bc_attrs.max_reqs, 0);
5111 if (status)
5112 nfs4_destroy_slot_tables(session);
5113 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005114
5115 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005116}
5117
5118struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5119{
5120 struct nfs4_session *session;
5121 struct nfs4_slot_table *tbl;
5122
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005123 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005124 if (!session)
5125 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005126
Andy Adamson557134a2009-04-01 09:21:53 -04005127 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05005128 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04005129 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005130 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005131 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005132
5133 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05005134 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005135 spin_lock_init(&tbl->slot_tbl_lock);
5136 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005137 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005138
Trond Myklebust1055d762010-06-16 09:52:27 -04005139 session->session_state = 1<<NFS4_SESSION_INITING;
5140
Andy Adamson557134a2009-04-01 09:21:53 -04005141 session->clp = clp;
5142 return session;
5143}
5144
5145void nfs4_destroy_session(struct nfs4_session *session)
5146{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005147 nfs4_proc_destroy_session(session);
5148 dprintk("%s Destroy backchannel for xprt %p\n",
5149 __func__, session->clp->cl_rpcclient->cl_xprt);
5150 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
5151 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005152 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005153 kfree(session);
5154}
5155
Andy Adamsonfc931582009-04-01 09:22:31 -04005156/*
5157 * Initialize the values to be used by the client in CREATE_SESSION
5158 * If nfs4_init_session set the fore channel request and response sizes,
5159 * use them.
5160 *
5161 * Set the back channel max_resp_sz_cached to zero to force the client to
5162 * always set csa_cachethis to FALSE because the current implementation
5163 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5164 */
5165static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5166{
5167 struct nfs4_session *session = args->client->cl_session;
5168 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5169 mxresp_sz = session->fc_attrs.max_resp_sz;
5170
5171 if (mxrqst_sz == 0)
5172 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5173 if (mxresp_sz == 0)
5174 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5175 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005176 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5177 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005178 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5179 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
5180
5181 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005182 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005183 __func__,
5184 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005185 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005186
5187 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005188 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5189 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5190 args->bc_attrs.max_resp_sz_cached = 0;
5191 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5192 args->bc_attrs.max_reqs = 1;
5193
5194 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5195 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5196 __func__,
5197 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5198 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5199 args->bc_attrs.max_reqs);
5200}
5201
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005202static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005203{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005204 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5205 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5206
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005207 if (rcvd->max_resp_sz > sent->max_resp_sz)
5208 return -EINVAL;
5209 /*
5210 * Our requested max_ops is the minimum we need; we're not
5211 * prepared to break up compounds into smaller pieces than that.
5212 * So, no point even trying to continue if the server won't
5213 * cooperate:
5214 */
5215 if (rcvd->max_ops < sent->max_ops)
5216 return -EINVAL;
5217 if (rcvd->max_reqs == 0)
5218 return -EINVAL;
5219 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005220}
5221
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005222static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5223{
5224 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5225 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005226
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005227 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5228 return -EINVAL;
5229 if (rcvd->max_resp_sz < sent->max_resp_sz)
5230 return -EINVAL;
5231 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5232 return -EINVAL;
5233 /* These would render the backchannel useless: */
5234 if (rcvd->max_ops == 0)
5235 return -EINVAL;
5236 if (rcvd->max_reqs == 0)
5237 return -EINVAL;
5238 return 0;
5239}
Andy Adamson8d353012009-04-01 09:22:32 -04005240
Andy Adamson8d353012009-04-01 09:22:32 -04005241static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5242 struct nfs4_session *session)
5243{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005244 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005245
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005246 ret = nfs4_verify_fore_channel_attrs(args, session);
5247 if (ret)
5248 return ret;
5249 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005250}
5251
Andy Adamsonfc931582009-04-01 09:22:31 -04005252static int _nfs4_proc_create_session(struct nfs_client *clp)
5253{
5254 struct nfs4_session *session = clp->cl_session;
5255 struct nfs41_create_session_args args = {
5256 .client = clp,
5257 .cb_program = NFS4_CALLBACK,
5258 };
5259 struct nfs41_create_session_res res = {
5260 .client = clp,
5261 };
5262 struct rpc_message msg = {
5263 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5264 .rpc_argp = &args,
5265 .rpc_resp = &res,
5266 };
5267 int status;
5268
5269 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005270 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005271
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005272 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005273
Andy Adamson8d353012009-04-01 09:22:32 -04005274 if (!status)
5275 /* Verify the session's negotiated channel_attrs values */
5276 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005277 if (!status) {
5278 /* Increment the clientid slot sequence id */
5279 clp->cl_seqid++;
5280 }
5281
5282 return status;
5283}
5284
5285/*
5286 * Issues a CREATE_SESSION operation to the server.
5287 * It is the responsibility of the caller to verify the session is
5288 * expired before calling this routine.
5289 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005290int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005291{
5292 int status;
5293 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005294 struct nfs4_session *session = clp->cl_session;
5295
5296 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5297
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005298 status = _nfs4_proc_create_session(clp);
Andy Adamsonfc931582009-04-01 09:22:31 -04005299 if (status)
5300 goto out;
5301
Trond Myklebustf26468f2009-12-05 19:32:11 -05005302 /* Init and reset the fore channel */
5303 status = nfs4_init_slot_tables(session);
5304 dprintk("slot table initialization returned %d\n", status);
5305 if (status)
5306 goto out;
5307 status = nfs4_reset_slot_tables(session);
5308 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005309 if (status)
5310 goto out;
5311
5312 ptr = (unsigned *)&session->sess_id.data[0];
5313 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5314 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005315out:
5316 dprintk("<-- %s\n", __func__);
5317 return status;
5318}
5319
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005320/*
5321 * Issue the over-the-wire RPC DESTROY_SESSION.
5322 * The caller must serialize access to this routine.
5323 */
5324int nfs4_proc_destroy_session(struct nfs4_session *session)
5325{
5326 int status = 0;
5327 struct rpc_message msg;
5328
5329 dprintk("--> nfs4_proc_destroy_session\n");
5330
5331 /* session is still being setup */
5332 if (session->clp->cl_cons_state != NFS_CS_READY)
5333 return status;
5334
5335 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5336 msg.rpc_argp = session;
5337 msg.rpc_resp = NULL;
5338 msg.rpc_cred = NULL;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005339 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005340
5341 if (status)
5342 printk(KERN_WARNING
5343 "Got error %d from the server on DESTROY_SESSION. "
5344 "Session has been destroyed regardless...\n", status);
5345
5346 dprintk("<-- nfs4_proc_destroy_session\n");
5347 return status;
5348}
5349
Trond Myklebustfccba802009-07-21 16:48:07 -04005350int nfs4_init_session(struct nfs_server *server)
5351{
5352 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005353 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005354 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005355 int ret;
5356
5357 if (!nfs4_has_session(clp))
5358 return 0;
5359
Trond Myklebust1055d762010-06-16 09:52:27 -04005360 session = clp->cl_session;
5361 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5362 return 0;
5363
Andy Adamson68bf05e2009-12-15 12:55:02 -05005364 rsize = server->rsize;
5365 if (rsize == 0)
5366 rsize = NFS_MAX_FILE_IO_SIZE;
5367 wsize = server->wsize;
5368 if (wsize == 0)
5369 wsize = NFS_MAX_FILE_IO_SIZE;
5370
Andy Adamson68bf05e2009-12-15 12:55:02 -05005371 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5372 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005373
Trond Myklebustfccba802009-07-21 16:48:07 -04005374 ret = nfs4_recover_expired_lease(server);
5375 if (!ret)
5376 ret = nfs4_check_client_ready(clp);
5377 return ret;
5378}
5379
Andy Adamsond83217c2011-03-01 01:34:17 +00005380int nfs4_init_ds_session(struct nfs_client *clp)
5381{
5382 struct nfs4_session *session = clp->cl_session;
5383 int ret;
5384
5385 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5386 return 0;
5387
5388 ret = nfs4_client_recover_expired_lease(clp);
5389 if (!ret)
5390 /* Test for the DS role */
5391 if (!is_ds_client(clp))
5392 ret = -ENODEV;
5393 if (!ret)
5394 ret = nfs4_check_client_ready(clp);
5395 return ret;
5396
5397}
5398EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5399
5400
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005401/*
5402 * Renew the cl_session lease.
5403 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005404struct nfs4_sequence_data {
5405 struct nfs_client *clp;
5406 struct nfs4_sequence_args args;
5407 struct nfs4_sequence_res res;
5408};
5409
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005410static void nfs41_sequence_release(void *data)
5411{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005412 struct nfs4_sequence_data *calldata = data;
5413 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005414
Alexandros Batsakis71358402010-02-05 03:45:05 -08005415 if (atomic_read(&clp->cl_count) > 1)
5416 nfs4_schedule_state_renewal(clp);
5417 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005418 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005419}
5420
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005421static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5422{
5423 switch(task->tk_status) {
5424 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005425 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5426 return -EAGAIN;
5427 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005428 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005429 }
5430 return 0;
5431}
5432
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005433static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005434{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005435 struct nfs4_sequence_data *calldata = data;
5436 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005437
Trond Myklebust14516c32010-07-31 14:29:06 -04005438 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5439 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005440
5441 if (task->tk_status < 0) {
5442 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005443 if (atomic_read(&clp->cl_count) == 1)
5444 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005445
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005446 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5447 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005448 return;
5449 }
5450 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005451 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005452out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005453 dprintk("<-- %s\n", __func__);
5454}
5455
5456static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5457{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005458 struct nfs4_sequence_data *calldata = data;
5459 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005460 struct nfs4_sequence_args *args;
5461 struct nfs4_sequence_res *res;
5462
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005463 args = task->tk_msg.rpc_argp;
5464 res = task->tk_msg.rpc_resp;
5465
Trond Myklebust035168a2010-06-16 09:52:26 -04005466 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005467 return;
5468 rpc_call_start(task);
5469}
5470
5471static const struct rpc_call_ops nfs41_sequence_ops = {
5472 .rpc_call_done = nfs41_sequence_call_done,
5473 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005474 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005475};
5476
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005477static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005478{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005479 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005480 struct rpc_message msg = {
5481 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5482 .rpc_cred = cred,
5483 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005484 struct rpc_task_setup task_setup_data = {
5485 .rpc_client = clp->cl_rpcclient,
5486 .rpc_message = &msg,
5487 .callback_ops = &nfs41_sequence_ops,
5488 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5489 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005490
Alexandros Batsakis71358402010-02-05 03:45:05 -08005491 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005492 return ERR_PTR(-EIO);
Benny Halevydfb4f302010-09-24 09:17:01 -04005493 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005494 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005495 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005496 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005497 }
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005498 msg.rpc_argp = &calldata->args;
5499 msg.rpc_resp = &calldata->res;
5500 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005501 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005502
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005503 return rpc_run_task(&task_setup_data);
5504}
5505
5506static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5507{
5508 struct rpc_task *task;
5509 int ret = 0;
5510
5511 task = _nfs41_proc_sequence(clp, cred);
5512 if (IS_ERR(task))
5513 ret = PTR_ERR(task);
5514 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005515 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005516 dprintk("<-- %s status=%d\n", __func__, ret);
5517 return ret;
5518}
5519
5520static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5521{
5522 struct rpc_task *task;
5523 int ret;
5524
5525 task = _nfs41_proc_sequence(clp, cred);
5526 if (IS_ERR(task)) {
5527 ret = PTR_ERR(task);
5528 goto out;
5529 }
5530 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005531 if (!ret) {
5532 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5533
5534 if (task->tk_status == 0)
5535 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005536 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005537 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005538 rpc_put_task(task);
5539out:
5540 dprintk("<-- %s status=%d\n", __func__, ret);
5541 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005542}
5543
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005544struct nfs4_reclaim_complete_data {
5545 struct nfs_client *clp;
5546 struct nfs41_reclaim_complete_args arg;
5547 struct nfs41_reclaim_complete_res res;
5548};
5549
5550static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5551{
5552 struct nfs4_reclaim_complete_data *calldata = data;
5553
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005554 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005555 if (nfs41_setup_sequence(calldata->clp->cl_session,
5556 &calldata->arg.seq_args,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005557 &calldata->res.seq_res, 0, task))
5558 return;
5559
5560 rpc_call_start(task);
5561}
5562
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005563static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5564{
5565 switch(task->tk_status) {
5566 case 0:
5567 case -NFS4ERR_COMPLETE_ALREADY:
5568 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5569 break;
5570 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005571 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005572 /* fall through */
5573 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005574 return -EAGAIN;
5575 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005576 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005577 }
5578 return 0;
5579}
5580
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005581static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5582{
5583 struct nfs4_reclaim_complete_data *calldata = data;
5584 struct nfs_client *clp = calldata->clp;
5585 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5586
5587 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005588 if (!nfs41_sequence_done(task, res))
5589 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005590
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005591 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5592 rpc_restart_call_prepare(task);
5593 return;
5594 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005595 dprintk("<-- %s\n", __func__);
5596}
5597
5598static void nfs4_free_reclaim_complete_data(void *data)
5599{
5600 struct nfs4_reclaim_complete_data *calldata = data;
5601
5602 kfree(calldata);
5603}
5604
5605static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5606 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5607 .rpc_call_done = nfs4_reclaim_complete_done,
5608 .rpc_release = nfs4_free_reclaim_complete_data,
5609};
5610
5611/*
5612 * Issue a global reclaim complete.
5613 */
5614static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5615{
5616 struct nfs4_reclaim_complete_data *calldata;
5617 struct rpc_task *task;
5618 struct rpc_message msg = {
5619 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5620 };
5621 struct rpc_task_setup task_setup_data = {
5622 .rpc_client = clp->cl_rpcclient,
5623 .rpc_message = &msg,
5624 .callback_ops = &nfs4_reclaim_complete_call_ops,
5625 .flags = RPC_TASK_ASYNC,
5626 };
5627 int status = -ENOMEM;
5628
5629 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005630 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005631 if (calldata == NULL)
5632 goto out;
5633 calldata->clp = clp;
5634 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005635
5636 msg.rpc_argp = &calldata->arg;
5637 msg.rpc_resp = &calldata->res;
5638 task_setup_data.callback_data = calldata;
5639 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005640 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005641 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005642 goto out;
5643 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005644 status = nfs4_wait_for_completion_rpc_task(task);
5645 if (status == 0)
5646 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005647 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005648 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005649out:
5650 dprintk("<-- %s status=%d\n", __func__, status);
5651 return status;
5652}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005653
5654static void
5655nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5656{
5657 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005658 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005659
5660 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005661 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5662 * right now covering the LAYOUTGET we are about to send.
5663 * However, that is not so catastrophic, and there seems
5664 * to be no way to prevent it completely.
5665 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005666 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5667 &lgp->res.seq_res, 0, task))
5668 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005669 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5670 NFS_I(lgp->args.inode)->layout,
5671 lgp->args.ctx->state)) {
5672 rpc_exit(task, NFS4_OK);
5673 return;
5674 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005675 rpc_call_start(task);
5676}
5677
5678static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5679{
5680 struct nfs4_layoutget *lgp = calldata;
5681 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5682
5683 dprintk("--> %s\n", __func__);
5684
5685 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5686 return;
5687
5688 switch (task->tk_status) {
5689 case 0:
5690 break;
5691 case -NFS4ERR_LAYOUTTRYLATER:
5692 case -NFS4ERR_RECALLCONFLICT:
5693 task->tk_status = -NFS4ERR_DELAY;
5694 /* Fall through */
5695 default:
5696 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5697 rpc_restart_call_prepare(task);
5698 return;
5699 }
5700 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005701 dprintk("<-- %s\n", __func__);
5702}
5703
5704static void nfs4_layoutget_release(void *calldata)
5705{
5706 struct nfs4_layoutget *lgp = calldata;
5707
5708 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005709 put_nfs_open_context(lgp->args.ctx);
5710 kfree(calldata);
5711 dprintk("<-- %s\n", __func__);
5712}
5713
5714static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5715 .rpc_call_prepare = nfs4_layoutget_prepare,
5716 .rpc_call_done = nfs4_layoutget_done,
5717 .rpc_release = nfs4_layoutget_release,
5718};
5719
5720int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5721{
5722 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5723 struct rpc_task *task;
5724 struct rpc_message msg = {
5725 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5726 .rpc_argp = &lgp->args,
5727 .rpc_resp = &lgp->res,
5728 };
5729 struct rpc_task_setup task_setup_data = {
5730 .rpc_client = server->client,
5731 .rpc_message = &msg,
5732 .callback_ops = &nfs4_layoutget_call_ops,
5733 .callback_data = lgp,
5734 .flags = RPC_TASK_ASYNC,
5735 };
5736 int status = 0;
5737
5738 dprintk("--> %s\n", __func__);
5739
Weston Andros Adamson35124a02011-03-24 16:48:21 -04005740 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005741 lgp->res.seq_res.sr_slot = NULL;
5742 task = rpc_run_task(&task_setup_data);
5743 if (IS_ERR(task))
5744 return PTR_ERR(task);
5745 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00005746 if (status == 0)
5747 status = task->tk_status;
5748 if (status == 0)
5749 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005750 rpc_put_task(task);
5751 dprintk("<-- %s status=%d\n", __func__, status);
5752 return status;
5753}
5754
Benny Halevycbe82602011-05-22 19:52:37 +03005755static void
5756nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
5757{
5758 struct nfs4_layoutreturn *lrp = calldata;
5759
5760 dprintk("--> %s\n", __func__);
5761 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
5762 &lrp->res.seq_res, 0, task))
5763 return;
5764 rpc_call_start(task);
5765}
5766
5767static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
5768{
5769 struct nfs4_layoutreturn *lrp = calldata;
5770 struct nfs_server *server;
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005771 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03005772
5773 dprintk("--> %s\n", __func__);
5774
5775 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
5776 return;
5777
5778 server = NFS_SERVER(lrp->args.inode);
5779 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5780 nfs_restart_rpc(task, lrp->clp);
5781 return;
5782 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005783 spin_lock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005784 if (task->tk_status == 0) {
Benny Halevycbe82602011-05-22 19:52:37 +03005785 if (lrp->res.lrs_present) {
Benny Halevycbe82602011-05-22 19:52:37 +03005786 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Benny Halevycbe82602011-05-22 19:52:37 +03005787 } else
5788 BUG_ON(!list_empty(&lo->plh_segs));
5789 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005790 lo->plh_block_lgets--;
5791 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005792 dprintk("<-- %s\n", __func__);
5793}
5794
5795static void nfs4_layoutreturn_release(void *calldata)
5796{
5797 struct nfs4_layoutreturn *lrp = calldata;
5798
5799 dprintk("--> %s\n", __func__);
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005800 put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03005801 kfree(calldata);
5802 dprintk("<-- %s\n", __func__);
5803}
5804
5805static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
5806 .rpc_call_prepare = nfs4_layoutreturn_prepare,
5807 .rpc_call_done = nfs4_layoutreturn_done,
5808 .rpc_release = nfs4_layoutreturn_release,
5809};
5810
5811int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
5812{
5813 struct rpc_task *task;
5814 struct rpc_message msg = {
5815 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
5816 .rpc_argp = &lrp->args,
5817 .rpc_resp = &lrp->res,
5818 };
5819 struct rpc_task_setup task_setup_data = {
5820 .rpc_client = lrp->clp->cl_rpcclient,
5821 .rpc_message = &msg,
5822 .callback_ops = &nfs4_layoutreturn_call_ops,
5823 .callback_data = lrp,
5824 };
5825 int status;
5826
5827 dprintk("--> %s\n", __func__);
5828 task = rpc_run_task(&task_setup_data);
5829 if (IS_ERR(task))
5830 return PTR_ERR(task);
5831 status = task->tk_status;
5832 dprintk("<-- %s status=%d\n", __func__, status);
5833 rpc_put_task(task);
5834 return status;
5835}
5836
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005837static int
5838_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5839{
5840 struct nfs4_getdeviceinfo_args args = {
5841 .pdev = pdev,
5842 };
5843 struct nfs4_getdeviceinfo_res res = {
5844 .pdev = pdev,
5845 };
5846 struct rpc_message msg = {
5847 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5848 .rpc_argp = &args,
5849 .rpc_resp = &res,
5850 };
5851 int status;
5852
5853 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005854 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005855 dprintk("<-- %s status=%d\n", __func__, status);
5856
5857 return status;
5858}
5859
5860int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5861{
5862 struct nfs4_exception exception = { };
5863 int err;
5864
5865 do {
5866 err = nfs4_handle_exception(server,
5867 _nfs4_proc_getdeviceinfo(server, pdev),
5868 &exception);
5869 } while (exception.retry);
5870 return err;
5871}
5872EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5873
Andy Adamson863a3c62011-03-23 13:27:54 +00005874static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
5875{
5876 struct nfs4_layoutcommit_data *data = calldata;
5877 struct nfs_server *server = NFS_SERVER(data->args.inode);
5878
5879 if (nfs4_setup_sequence(server, &data->args.seq_args,
5880 &data->res.seq_res, 1, task))
5881 return;
5882 rpc_call_start(task);
5883}
5884
5885static void
5886nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
5887{
5888 struct nfs4_layoutcommit_data *data = calldata;
5889 struct nfs_server *server = NFS_SERVER(data->args.inode);
5890
5891 if (!nfs4_sequence_done(task, &data->res.seq_res))
5892 return;
5893
5894 switch (task->tk_status) { /* Just ignore these failures */
5895 case NFS4ERR_DELEG_REVOKED: /* layout was recalled */
5896 case NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
5897 case NFS4ERR_BADLAYOUT: /* no layout */
5898 case NFS4ERR_GRACE: /* loca_recalim always false */
5899 task->tk_status = 0;
5900 }
5901
5902 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5903 nfs_restart_rpc(task, server->nfs_client);
5904 return;
5905 }
5906
5907 if (task->tk_status == 0)
5908 nfs_post_op_update_inode_force_wcc(data->args.inode,
5909 data->res.fattr);
5910}
5911
5912static void nfs4_layoutcommit_release(void *calldata)
5913{
5914 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04005915 struct pnfs_layout_segment *lseg, *tmp;
Andy Adamson863a3c62011-03-23 13:27:54 +00005916
5917 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04005918 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
5919 list_del_init(&lseg->pls_lc_list);
5920 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
5921 &lseg->pls_flags))
5922 put_lseg(lseg);
5923 }
Andy Adamson863a3c62011-03-23 13:27:54 +00005924 put_rpccred(data->cred);
5925 kfree(data);
5926}
5927
5928static const struct rpc_call_ops nfs4_layoutcommit_ops = {
5929 .rpc_call_prepare = nfs4_layoutcommit_prepare,
5930 .rpc_call_done = nfs4_layoutcommit_done,
5931 .rpc_release = nfs4_layoutcommit_release,
5932};
5933
5934int
Andy Adamsonef311532011-03-12 02:58:10 -05005935nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00005936{
5937 struct rpc_message msg = {
5938 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
5939 .rpc_argp = &data->args,
5940 .rpc_resp = &data->res,
5941 .rpc_cred = data->cred,
5942 };
5943 struct rpc_task_setup task_setup_data = {
5944 .task = &data->task,
5945 .rpc_client = NFS_CLIENT(data->args.inode),
5946 .rpc_message = &msg,
5947 .callback_ops = &nfs4_layoutcommit_ops,
5948 .callback_data = data,
5949 .flags = RPC_TASK_ASYNC,
5950 };
5951 struct rpc_task *task;
5952 int status = 0;
5953
5954 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
5955 "lbw: %llu inode %lu\n",
5956 data->task.tk_pid, sync,
5957 data->args.lastbytewritten,
5958 data->args.inode->i_ino);
5959
5960 task = rpc_run_task(&task_setup_data);
5961 if (IS_ERR(task))
5962 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05005963 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00005964 goto out;
5965 status = nfs4_wait_for_completion_rpc_task(task);
5966 if (status != 0)
5967 goto out;
5968 status = task->tk_status;
5969out:
5970 dprintk("%s: status %d\n", __func__, status);
5971 rpc_put_task(task);
5972 return status;
5973}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04005974
5975static int
5976_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
5977 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
5978{
5979 struct nfs41_secinfo_no_name_args args = {
5980 .style = SECINFO_STYLE_CURRENT_FH,
5981 };
5982 struct nfs4_secinfo_res res = {
5983 .flavors = flavors,
5984 };
5985 struct rpc_message msg = {
5986 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
5987 .rpc_argp = &args,
5988 .rpc_resp = &res,
5989 };
5990 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
5991}
5992
5993static int
5994nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
5995 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
5996{
5997 struct nfs4_exception exception = { };
5998 int err;
5999 do {
6000 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6001 switch (err) {
6002 case 0:
6003 case -NFS4ERR_WRONGSEC:
6004 case -NFS4ERR_NOTSUPP:
6005 break;
6006 default:
6007 err = nfs4_handle_exception(server, err, &exception);
6008 }
6009 } while (exception.retry);
6010 return err;
6011}
6012
6013static int
6014nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6015 struct nfs_fsinfo *info)
6016{
6017 int err;
6018 struct page *page;
6019 rpc_authflavor_t flavor;
6020 struct nfs4_secinfo_flavors *flavors;
6021
6022 page = alloc_page(GFP_KERNEL);
6023 if (!page) {
6024 err = -ENOMEM;
6025 goto out;
6026 }
6027
6028 flavors = page_address(page);
6029 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6030
6031 /*
6032 * Fall back on "guess and check" method if
6033 * the server doesn't support SECINFO_NO_NAME
6034 */
6035 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6036 err = nfs4_find_root_sec(server, fhandle, info);
6037 goto out_freepage;
6038 }
6039 if (err)
6040 goto out_freepage;
6041
6042 flavor = nfs_find_best_sec(flavors);
6043 if (err == 0)
6044 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6045
6046out_freepage:
6047 put_page(page);
6048 if (err == -EACCES)
6049 return -EPERM;
6050out:
6051 return err;
6052}
Bryan Schumaker7d974792011-06-02 14:59:08 -04006053static int _nfs41_test_stateid(struct nfs_server *server, struct nfs4_state *state)
6054{
6055 int status;
6056 struct nfs41_test_stateid_args args = {
6057 .stateid = &state->stateid,
6058 };
6059 struct nfs41_test_stateid_res res;
6060 struct rpc_message msg = {
6061 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6062 .rpc_argp = &args,
6063 .rpc_resp = &res,
6064 };
6065 args.seq_args.sa_session = res.seq_res.sr_session = NULL;
6066 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 0, 1);
6067 return status;
6068}
6069
6070static int nfs41_test_stateid(struct nfs_server *server, struct nfs4_state *state)
6071{
6072 struct nfs4_exception exception = { };
6073 int err;
6074 do {
6075 err = nfs4_handle_exception(server,
6076 _nfs41_test_stateid(server, state),
6077 &exception);
6078 } while (exception.retry);
6079 return err;
6080}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006081
6082static int _nfs4_free_stateid(struct nfs_server *server, struct nfs4_state *state)
6083{
6084 int status;
6085 struct nfs41_free_stateid_args args = {
6086 .stateid = &state->stateid,
6087 };
6088 struct nfs41_free_stateid_res res;
6089 struct rpc_message msg = {
6090 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6091 .rpc_argp = &args,
6092 .rpc_resp = &res,
6093 };
6094
6095 args.seq_args.sa_session = res.seq_res.sr_session = NULL;
6096 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 0, 1);
6097 return status;
6098}
6099
6100static int nfs41_free_stateid(struct nfs_server *server, struct nfs4_state *state)
6101{
6102 struct nfs4_exception exception = { };
6103 int err;
6104 do {
6105 err = nfs4_handle_exception(server,
6106 _nfs4_free_stateid(server, state),
6107 &exception);
6108 } while (exception.retry);
6109 return err;
6110}
Andy Adamson557134a2009-04-01 09:21:53 -04006111#endif /* CONFIG_NFS_V4_1 */
6112
Andy Adamson591d71c2009-04-01 09:22:47 -04006113struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006114 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006115 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006116 .recover_open = nfs4_open_reclaim,
6117 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006118 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006119 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120};
6121
Andy Adamson591d71c2009-04-01 09:22:47 -04006122#if defined(CONFIG_NFS_V4_1)
6123struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6124 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6125 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6126 .recover_open = nfs4_open_reclaim,
6127 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006128 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006129 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006130 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006131};
6132#endif /* CONFIG_NFS_V4_1 */
6133
6134struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006135 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006136 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006137 .recover_open = nfs4_open_expired,
6138 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006139 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006140 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006141};
6142
Andy Adamson591d71c2009-04-01 09:22:47 -04006143#if defined(CONFIG_NFS_V4_1)
6144struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6145 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6146 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006147 .recover_open = nfs41_open_expired,
6148 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006149 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006150 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006151};
6152#endif /* CONFIG_NFS_V4_1 */
6153
Benny Halevy29fba382009-04-01 09:22:44 -04006154struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6155 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006156 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006157 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006158};
6159
6160#if defined(CONFIG_NFS_V4_1)
6161struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
6162 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006163 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006164 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006165};
6166#endif
6167
Trond Myklebust97dc1352010-06-16 09:52:26 -04006168static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6169 .minor_version = 0,
6170 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04006171 .validate_stateid = nfs4_validate_delegation_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006172 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006173 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6174 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6175 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006176};
6177
6178#if defined(CONFIG_NFS_V4_1)
6179static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6180 .minor_version = 1,
6181 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04006182 .validate_stateid = nfs41_validate_delegation_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006183 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006184 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6185 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6186 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006187};
6188#endif
6189
Trond Myklebust97dc1352010-06-16 09:52:26 -04006190const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6191 [0] = &nfs_v4_0_minor_ops,
6192#if defined(CONFIG_NFS_V4_1)
6193 [1] = &nfs_v4_1_minor_ops,
6194#endif
6195};
6196
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006197static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006198 .permission = nfs_permission,
6199 .getattr = nfs_getattr,
6200 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006201 .getxattr = generic_getxattr,
6202 .setxattr = generic_setxattr,
6203 .listxattr = generic_listxattr,
6204 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006205};
6206
David Howells509de812006-08-22 20:06:11 -04006207const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006208 .version = 4, /* protocol version */
6209 .dentry_ops = &nfs4_dentry_operations,
6210 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006211 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006212 .getroot = nfs4_proc_get_root,
6213 .getattr = nfs4_proc_getattr,
6214 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04006215 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216 .lookup = nfs4_proc_lookup,
6217 .access = nfs4_proc_access,
6218 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219 .create = nfs4_proc_create,
6220 .remove = nfs4_proc_remove,
6221 .unlink_setup = nfs4_proc_unlink_setup,
6222 .unlink_done = nfs4_proc_unlink_done,
6223 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006224 .rename_setup = nfs4_proc_rename_setup,
6225 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226 .link = nfs4_proc_link,
6227 .symlink = nfs4_proc_symlink,
6228 .mkdir = nfs4_proc_mkdir,
6229 .rmdir = nfs4_proc_remove,
6230 .readdir = nfs4_proc_readdir,
6231 .mknod = nfs4_proc_mknod,
6232 .statfs = nfs4_proc_statfs,
6233 .fsinfo = nfs4_proc_fsinfo,
6234 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006235 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006236 .decode_dirent = nfs4_decode_dirent,
6237 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05006238 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006240 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006241 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006242 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006244 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006245 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006246 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00006247 .init_client = nfs4_init_client,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006248 .secinfo = nfs4_proc_secinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249};
6250
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006251static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6252 .prefix = XATTR_NAME_NFSV4_ACL,
6253 .list = nfs4_xattr_list_nfs4_acl,
6254 .get = nfs4_xattr_get_nfs4_acl,
6255 .set = nfs4_xattr_set_nfs4_acl,
6256};
6257
6258const struct xattr_handler *nfs4_xattr_handlers[] = {
6259 &nfs4_xattr_nfs4_acl_handler,
6260 NULL
6261};
6262
Linus Torvalds1da177e2005-04-16 15:20:36 -07006263/*
6264 * Local variables:
6265 * c-basic-offset: 8
6266 * End:
6267 */