blob: 8c77039e7a811a0fb08ab5e8f7f04e82e087642d [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
Fred Isamandae100c2011-07-30 20:52:37 -0400143const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 | FATTR4_WORD0_MAXREAD
145 | FATTR4_WORD0_MAXWRITE
146 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700147 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400148 | FATTR4_WORD1_FS_LAYOUT_TYPES,
149 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150};
151
Manoj Naik830b8e32006-06-09 09:34:25 -0400152const u32 nfs4_fs_locations_bitmap[2] = {
153 FATTR4_WORD0_TYPE
154 | FATTR4_WORD0_CHANGE
155 | FATTR4_WORD0_SIZE
156 | FATTR4_WORD0_FSID
157 | FATTR4_WORD0_FILEID
158 | FATTR4_WORD0_FS_LOCATIONS,
159 FATTR4_WORD1_MODE
160 | FATTR4_WORD1_NUMLINKS
161 | FATTR4_WORD1_OWNER
162 | FATTR4_WORD1_OWNER_GROUP
163 | FATTR4_WORD1_RAWDEV
164 | FATTR4_WORD1_SPACE_USED
165 | FATTR4_WORD1_TIME_ACCESS
166 | FATTR4_WORD1_TIME_METADATA
167 | FATTR4_WORD1_TIME_MODIFY
168 | FATTR4_WORD1_MOUNTED_ON_FILEID
169};
170
Al Virobc4785c2006-10-19 23:28:51 -0700171static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 struct nfs4_readdir_arg *readdir)
173{
Al Viro0dbb4c62006-10-19 23:28:49 -0700174 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 BUG_ON(readdir->count < 80);
177 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000178 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
180 return;
181 }
182
183 readdir->cookie = 0;
184 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
185 if (cookie == 2)
186 return;
187
188 /*
189 * NFSv4 servers do not return entries for '.' and '..'
190 * Therefore, we fake these entries here. We let '.'
191 * have cookie 0 and '..' have cookie 1. Note that
192 * when talking to the server, we always send cookie 0
193 * instead of 1 or 2.
194 */
Al Viro0dbb4c62006-10-19 23:28:49 -0700195 start = p = kmap_atomic(*readdir->pages, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 if (cookie == 0) {
198 *p++ = xdr_one; /* next */
199 *p++ = xdr_zero; /* cookie, first word */
200 *p++ = xdr_one; /* cookie, second word */
201 *p++ = xdr_one; /* entry len */
202 memcpy(p, ".\0\0\0", 4); /* entry */
203 p++;
204 *p++ = xdr_one; /* bitmap length */
205 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
206 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400207 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
209
210 *p++ = xdr_one; /* next */
211 *p++ = xdr_zero; /* cookie, first word */
212 *p++ = xdr_two; /* cookie, second word */
213 *p++ = xdr_two; /* entry len */
214 memcpy(p, "..\0\0", 4); /* entry */
215 p++;
216 *p++ = xdr_one; /* bitmap length */
217 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
218 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400219 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 readdir->pgbase = (char *)p - (char *)start;
222 readdir->count -= readdir->pgbase;
223 kunmap_atomic(start, KM_USER0);
224}
225
Trond Myklebust65de8722008-12-23 15:21:44 -0500226static int nfs4_wait_clnt_recover(struct nfs_client *clp)
227{
228 int res;
229
230 might_sleep();
231
Trond Myklebuste005e802008-12-23 15:21:48 -0500232 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400233 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500234 return res;
235}
236
237static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
238{
239 int res = 0;
240
241 might_sleep();
242
243 if (*timeout <= 0)
244 *timeout = NFS4_POLL_RETRY_MIN;
245 if (*timeout > NFS4_POLL_RETRY_MAX)
246 *timeout = NFS4_POLL_RETRY_MAX;
247 schedule_timeout_killable(*timeout);
248 if (fatal_signal_pending(current))
249 res = -ERESTARTSYS;
250 *timeout <<= 1;
251 return res;
252}
253
254/* This is the error handling routine for processes that are allowed
255 * to sleep.
256 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800257static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500258{
259 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500260 struct nfs4_state *state = exception->state;
Trond Myklebust65de8722008-12-23 15:21:44 -0500261 int ret = errorcode;
262
263 exception->retry = 0;
264 switch(errorcode) {
265 case 0:
266 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500267 case -NFS4ERR_ADMIN_REVOKED:
268 case -NFS4ERR_BAD_STATEID:
269 case -NFS4ERR_OPENMODE:
270 if (state == NULL)
271 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500272 nfs4_schedule_stateid_recovery(server, state);
273 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400274 case -NFS4ERR_EXPIRED:
275 if (state != NULL)
276 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500277 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500278 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500279 nfs4_schedule_lease_recovery(clp);
280 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500281#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400282 case -NFS4ERR_BADSESSION:
283 case -NFS4ERR_BADSLOT:
284 case -NFS4ERR_BAD_HIGH_SLOT:
285 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
286 case -NFS4ERR_DEADSESSION:
287 case -NFS4ERR_SEQ_FALSE_RETRY:
288 case -NFS4ERR_SEQ_MISORDERED:
289 dprintk("%s ERROR: %d Reset session\n", __func__,
290 errorcode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500291 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -0400292 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500293 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500294#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500295 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500296 if (exception->timeout > HZ) {
297 /* We have retried a decent amount, time to
298 * fail
299 */
300 ret = -EBUSY;
301 break;
302 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500303 case -NFS4ERR_GRACE:
304 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500305 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500306 ret = nfs4_delay(server->client, &exception->timeout);
307 if (ret != 0)
308 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400309 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500310 case -NFS4ERR_OLD_STATEID:
311 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800312 break;
313 case -NFS4ERR_BADOWNER:
314 /* The following works around a Linux server bug! */
315 case -NFS4ERR_BADNAME:
316 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
317 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
318 exception->retry = 1;
319 printk(KERN_WARNING "NFS: v4 server %s "
320 "does not accept raw "
321 "uid/gids. "
322 "Reenabling the idmapper.\n",
323 server->nfs_client->cl_hostname);
324 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500325 }
326 /* We failed to handle the error */
327 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500328wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500329 ret = nfs4_wait_clnt_recover(clp);
330 if (ret == 0)
331 exception->retry = 1;
332 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500333}
334
335
Trond Myklebust452e9352010-07-31 14:29:06 -0400336static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 spin_lock(&clp->cl_lock);
339 if (time_before(clp->cl_last_renewal,timestamp))
340 clp->cl_last_renewal = timestamp;
341 spin_unlock(&clp->cl_lock);
342}
343
Trond Myklebust452e9352010-07-31 14:29:06 -0400344static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
345{
346 do_renew_lease(server->nfs_client, timestamp);
347}
348
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400349#if defined(CONFIG_NFS_V4_1)
350
Benny Halevy510b8172009-04-01 09:22:14 -0400351/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400352 * nfs4_free_slot - free a slot and efficiently update slot table.
353 *
354 * freeing a slot is trivially done by clearing its respective bit
355 * in the bitmap.
356 * If the freed slotid equals highest_used_slotid we want to update it
357 * so that the server would be able to size down the slot table if needed,
358 * otherwise we know that the highest_used_slotid is still in use.
359 * When updating highest_used_slotid there may be "holes" in the bitmap
360 * so we need to scan down from highest_used_slotid to 0 looking for the now
361 * highest slotid in use.
362 * If none found, highest_used_slotid is set to -1.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500363 *
364 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400365 */
366static void
Benny Halevydfb4f3092010-09-24 09:17:01 -0400367nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *free_slot)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400368{
Benny Halevydfb4f3092010-09-24 09:17:01 -0400369 int free_slotid = free_slot - tbl->slots;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400370 int slotid = free_slotid;
371
Benny Halevydfb4f3092010-09-24 09:17:01 -0400372 BUG_ON(slotid < 0 || slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400373 /* clear used bit in bitmap */
374 __clear_bit(slotid, tbl->used_slots);
375
376 /* update highest_used_slotid when it is freed */
377 if (slotid == tbl->highest_used_slotid) {
378 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500379 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400380 tbl->highest_used_slotid = slotid;
381 else
382 tbl->highest_used_slotid = -1;
383 }
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400384 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
385 free_slotid, tbl->highest_used_slotid);
386}
387
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800388/*
Andy Adamson42acd022011-01-06 02:04:34 +0000389 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800390 */
Andy Adamson42acd022011-01-06 02:04:34 +0000391static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800392{
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800393 struct rpc_task *task;
394
Trond Myklebusta2118c32010-06-16 09:52:26 -0400395 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800396 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
397 if (task)
398 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800399 return;
400 }
401
402 if (ses->fc_slot_table.highest_used_slotid != -1)
403 return;
404
Andy Adamson42acd022011-01-06 02:04:34 +0000405 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
406 complete(&ses->fc_slot_table.complete);
407}
408
409/*
410 * Signal state manager thread if session back channel is drained
411 */
412void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
413{
414 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
415 ses->bc_slot_table.highest_used_slotid != -1)
416 return;
417 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
418 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800419}
420
Trond Myklebustd185a332010-06-16 09:52:25 -0400421static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400422{
423 struct nfs4_slot_table *tbl;
424
Trond Myklebustd185a332010-06-16 09:52:25 -0400425 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400426 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400427 /* just wake up the next guy waiting since
428 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500429 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500430 return;
Andy Adamson13615872009-04-01 09:22:17 -0400431 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500432
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500433 spin_lock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400434 nfs4_free_slot(tbl, res->sr_slot);
Andy Adamson42acd022011-01-06 02:04:34 +0000435 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500436 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400437 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400438}
439
Trond Myklebust14516c32010-07-31 14:29:06 -0400440static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400441{
442 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400443 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400444
445 /*
446 * sr_status remains 1 if an RPC level error occurred. The server
447 * may or may not have processed the sequence operation..
448 * Proceed as if the server received and processed the sequence
449 * operation.
450 */
451 if (res->sr_status == 1)
452 res->sr_status = NFS_OK;
453
Bryan Schumaker468f8612011-04-18 15:57:32 -0400454 /* don't increment the sequence number if the task wasn't sent */
455 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400456 goto out;
457
Andy Adamson691daf32009-12-04 15:55:39 -0500458 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400459 switch (res->sr_status) {
460 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400461 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400462 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400463 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400464 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400465 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500466 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500467 if (res->sr_status_flags != 0)
468 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400469 break;
470 case -NFS4ERR_DELAY:
471 /* The server detected a resend of the RPC call and
472 * returned NFS4ERR_DELAY as per Section 2.10.6.2
473 * of RFC5661.
474 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200475 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400476 __func__,
477 res->sr_slot - res->sr_session->fc_slot_table.slots,
478 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400479 goto out_retry;
480 default:
481 /* Just update the slot sequence no. */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400482 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400483 }
484out:
485 /* The session may be reset by one of the error handlers. */
486 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400487 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400488 return 1;
489out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400490 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400491 goto out;
492 rpc_delay(task, NFS4_POLL_RETRY_MAX);
493 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400494}
495
Trond Myklebust14516c32010-07-31 14:29:06 -0400496static int nfs4_sequence_done(struct rpc_task *task,
497 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400498{
Trond Myklebust14516c32010-07-31 14:29:06 -0400499 if (res->sr_session == NULL)
500 return 1;
501 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400502}
503
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400504/*
Benny Halevy510b8172009-04-01 09:22:14 -0400505 * nfs4_find_slot - efficiently look for a free slot
506 *
507 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
508 * If found, we mark the slot as used, update the highest_used_slotid,
509 * and respectively set up the sequence operation args.
510 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400511 *
512 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400513 */
514static u8
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800515nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400516{
517 int slotid;
518 u8 ret_id = NFS4_MAX_SLOT_TABLE;
519 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
520
Benny Halevy510b8172009-04-01 09:22:14 -0400521 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
522 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
523 tbl->max_slots);
524 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
525 if (slotid >= tbl->max_slots)
526 goto out;
527 __set_bit(slotid, tbl->used_slots);
528 if (slotid > tbl->highest_used_slotid)
529 tbl->highest_used_slotid = slotid;
530 ret_id = slotid;
531out:
532 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
533 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400534 return ret_id;
535}
536
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000537int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400538 struct nfs4_sequence_args *args,
539 struct nfs4_sequence_res *res,
540 int cache_reply,
541 struct rpc_task *task)
542{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400543 struct nfs4_slot *slot;
544 struct nfs4_slot_table *tbl;
545 u8 slotid;
546
547 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400548 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400549 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400550 return 0;
551
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400552 tbl = &session->fc_slot_table;
553
554 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400555 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800556 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamsonea028ac2009-12-04 15:55:38 -0500557 /*
558 * The state manager will wait until the slot table is empty.
559 * Schedule the reset thread
560 */
Andy Adamson05f0d232009-12-04 15:55:37 -0500561 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400562 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500563 dprintk("%s Schedule Session Reset\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500564 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400565 }
566
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800567 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
568 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
569 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
570 spin_unlock(&tbl->slot_tbl_lock);
571 dprintk("%s enforce FIFO order\n", __func__);
572 return -EAGAIN;
573 }
574
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800575 slotid = nfs4_find_slot(tbl);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400576 if (slotid == NFS4_MAX_SLOT_TABLE) {
577 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
578 spin_unlock(&tbl->slot_tbl_lock);
579 dprintk("<-- %s: no free slots\n", __func__);
580 return -EAGAIN;
581 }
582 spin_unlock(&tbl->slot_tbl_lock);
583
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800584 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400585 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400586 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400587 args->sa_slotid = slotid;
588 args->sa_cache_this = cache_reply;
589
590 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
591
Benny Halevy99fe60d2009-04-01 09:22:29 -0400592 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400593 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400594 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400595 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400596 /*
597 * sr_status is only set in decode_sequence, and so will remain
598 * set to 1 if an rpc level failure occurs.
599 */
600 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400601 return 0;
602}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000603EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400604
Trond Myklebust035168a2010-06-16 09:52:26 -0400605int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400606 struct nfs4_sequence_args *args,
607 struct nfs4_sequence_res *res,
608 int cache_reply,
609 struct rpc_task *task)
610{
Trond Myklebust035168a2010-06-16 09:52:26 -0400611 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400612 int ret = 0;
613
Trond Myklebust035168a2010-06-16 09:52:26 -0400614 if (session == NULL) {
615 args->sa_session = NULL;
616 res->sr_session = NULL;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400617 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400618 }
619
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200620 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400621 __func__, session->clp, session, res->sr_slot ?
622 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400623
624 ret = nfs41_setup_sequence(session, args, res, cache_reply,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400625 task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400626out:
627 dprintk("<-- %s status=%d\n", __func__, ret);
628 return ret;
629}
630
631struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400632 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400633 struct nfs4_sequence_args *seq_args;
634 struct nfs4_sequence_res *seq_res;
635 int cache_reply;
636};
637
638static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
639{
640 struct nfs41_call_sync_data *data = calldata;
641
Trond Myklebust035168a2010-06-16 09:52:26 -0400642 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
643
644 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400645 data->seq_res, data->cache_reply, task))
646 return;
647 rpc_call_start(task);
648}
649
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800650static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
651{
652 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
653 nfs41_call_sync_prepare(task, calldata);
654}
655
Andy Adamson69ab40c2009-04-01 09:22:19 -0400656static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
657{
658 struct nfs41_call_sync_data *data = calldata;
659
Trond Myklebust14516c32010-07-31 14:29:06 -0400660 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400661}
662
Andy Adamsonce5039c2009-04-01 09:22:13 -0400663struct rpc_call_ops nfs41_call_sync_ops = {
664 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400665 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400666};
667
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800668struct rpc_call_ops nfs41_call_priv_sync_ops = {
669 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
670 .rpc_call_done = nfs41_call_sync_done,
671};
672
Bryan Schumaker7c513052011-03-24 17:12:24 +0000673static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
674 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400675 struct rpc_message *msg,
676 struct nfs4_sequence_args *args,
677 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800678 int cache_reply,
679 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400680{
681 int ret;
682 struct rpc_task *task;
683 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400684 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400685 .seq_args = args,
686 .seq_res = res,
687 .cache_reply = cache_reply,
688 };
689 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000690 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400691 .rpc_message = msg,
692 .callback_ops = &nfs41_call_sync_ops,
693 .callback_data = &data
694 };
695
Benny Halevydfb4f3092010-09-24 09:17:01 -0400696 res->sr_slot = NULL;
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800697 if (privileged)
698 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400699 task = rpc_run_task(&task_setup);
700 if (IS_ERR(task))
701 ret = PTR_ERR(task);
702 else {
703 ret = task->tk_status;
704 rpc_put_task(task);
705 }
706 return ret;
707}
708
Bryan Schumaker7c513052011-03-24 17:12:24 +0000709int _nfs4_call_sync_session(struct rpc_clnt *clnt,
710 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400711 struct rpc_message *msg,
712 struct nfs4_sequence_args *args,
713 struct nfs4_sequence_res *res,
714 int cache_reply)
715{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000716 return nfs4_call_sync_sequence(clnt, server, msg, args, res, cache_reply, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400717}
718
Trond Myklebustdf896452010-06-16 09:52:26 -0400719#else
Trond Myklebust14516c32010-07-31 14:29:06 -0400720static int nfs4_sequence_done(struct rpc_task *task,
721 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400722{
Trond Myklebust14516c32010-07-31 14:29:06 -0400723 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400724}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400725#endif /* CONFIG_NFS_V4_1 */
726
Bryan Schumaker7c513052011-03-24 17:12:24 +0000727int _nfs4_call_sync(struct rpc_clnt *clnt,
728 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400729 struct rpc_message *msg,
730 struct nfs4_sequence_args *args,
731 struct nfs4_sequence_res *res,
732 int cache_reply)
733{
Benny Halevyf3752972009-04-01 09:22:04 -0400734 args->sa_session = res->sr_session = NULL;
Bryan Schumaker7c513052011-03-24 17:12:24 +0000735 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400736}
737
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000738static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000739int nfs4_call_sync(struct rpc_clnt *clnt,
740 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000741 struct rpc_message *msg,
742 struct nfs4_sequence_args *args,
743 struct nfs4_sequence_res *res,
744 int cache_reply)
745{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000746 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
747 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000748}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400749
Trond Myklebust38478b22006-05-25 01:40:57 -0400750static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
Trond Myklebust38478b22006-05-25 01:40:57 -0400752 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
Trond Myklebust38478b22006-05-25 01:40:57 -0400754 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400755 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
756 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400757 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400758 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400759 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100762struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400763 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100764 struct nfs_openargs o_arg;
765 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100766 struct nfs_open_confirmargs c_arg;
767 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100768 struct nfs_fattr f_attr;
769 struct nfs_fattr dir_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100770 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400771 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100772 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400773 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100774 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100775 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400776 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100777 int rpc_status;
778 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100779};
780
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400781
782static void nfs4_init_opendata_res(struct nfs4_opendata *p)
783{
784 p->o_res.f_attr = &p->f_attr;
785 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400786 p->o_res.seqid = p->o_arg.seqid;
787 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400788 p->o_res.server = p->o_arg.server;
789 nfs_fattr_init(&p->f_attr);
790 nfs_fattr_init(&p->dir_attr);
791}
792
Al Viro82a2c1b2011-06-22 18:30:55 -0400793static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500794 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400795 const struct iattr *attrs,
796 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100797{
Al Viro82a2c1b2011-06-22 18:30:55 -0400798 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100799 struct inode *dir = parent->d_inode;
800 struct nfs_server *server = NFS_SERVER(dir);
801 struct nfs4_opendata *p;
802
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400803 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100804 if (p == NULL)
805 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400806 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100807 if (p->o_arg.seqid == NULL)
808 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400809 nfs_sb_active(dentry->d_sb);
810 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100811 p->dir = parent;
812 p->owner = sp;
813 atomic_inc(&sp->so_count);
814 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500815 p->o_arg.open_flags = flags;
816 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400817 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400818 p->o_arg.id = sp->so_owner_id.id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400819 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100820 p->o_arg.server = server;
821 p->o_arg.bitmask = server->attr_bitmask;
822 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400823 if (flags & O_CREAT) {
824 u32 *s;
825
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100826 p->o_arg.u.attrs = &p->attrs;
827 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400828 s = (u32 *) p->o_arg.u.verifier.data;
829 s[0] = jiffies;
830 s[1] = current->pid;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100831 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100832 p->c_arg.fh = &p->o_res.fh;
833 p->c_arg.stateid = &p->o_res.stateid;
834 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400835 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400836 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100837 return p;
838err_free:
839 kfree(p);
840err:
841 dput(parent);
842 return NULL;
843}
844
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400845static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100846{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400847 struct nfs4_opendata *p = container_of(kref,
848 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400849 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400850
851 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400852 if (p->state != NULL)
853 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400854 nfs4_put_state_owner(p->owner);
855 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400856 dput(p->dentry);
857 nfs_sb_deactive(sb);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400858 kfree(p);
859}
860
861static void nfs4_opendata_put(struct nfs4_opendata *p)
862{
863 if (p != NULL)
864 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100865}
866
Trond Myklebust06f814a2006-01-03 09:55:07 +0100867static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
868{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100869 int ret;
870
Trond Myklebust06f814a2006-01-03 09:55:07 +0100871 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100872 return ret;
873}
874
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500875static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400876{
877 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500878
879 if (open_mode & O_EXCL)
880 goto out;
881 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400882 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500883 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
884 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400885 break;
886 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500887 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
888 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400889 break;
890 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500891 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
892 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400893 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500894out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400895 return ret;
896}
897
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500898static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400899{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500900 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400901 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500902 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400903 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500904 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400905 return 1;
906}
907
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500908static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100909{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500910 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100911 case FMODE_WRITE:
912 state->n_wronly++;
913 break;
914 case FMODE_READ:
915 state->n_rdonly++;
916 break;
917 case FMODE_READ|FMODE_WRITE:
918 state->n_rdwr++;
919 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500920 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100921}
922
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500923static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400924{
925 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
926 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
927 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500928 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400929 case FMODE_READ:
930 set_bit(NFS_O_RDONLY_STATE, &state->flags);
931 break;
932 case FMODE_WRITE:
933 set_bit(NFS_O_WRONLY_STATE, &state->flags);
934 break;
935 case FMODE_READ|FMODE_WRITE:
936 set_bit(NFS_O_RDWR_STATE, &state->flags);
937 }
938}
939
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500940static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400941{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400942 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500943 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400944 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400945}
946
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500947static 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 -0700948{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400949 /*
950 * Protect the call to nfs4_state_set_mode_locked and
951 * serialise the stateid update
952 */
953 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400954 if (deleg_stateid != NULL) {
955 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
956 set_bit(NFS_DELEGATED_STATE, &state->flags);
957 }
958 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500959 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400960 write_sequnlock(&state->seqlock);
961 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500962 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700963 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964}
965
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500966static 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 -0500967{
968 struct nfs_inode *nfsi = NFS_I(state->inode);
969 struct nfs_delegation *deleg_cur;
970 int ret = 0;
971
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500972 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500973
974 rcu_read_lock();
975 deleg_cur = rcu_dereference(nfsi->delegation);
976 if (deleg_cur == NULL)
977 goto no_delegation;
978
979 spin_lock(&deleg_cur->lock);
980 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500981 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500982 goto no_delegation_unlock;
983
984 if (delegation == NULL)
985 delegation = &deleg_cur->stateid;
986 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
987 goto no_delegation_unlock;
988
Trond Myklebustb7391f42008-12-23 15:21:52 -0500989 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500990 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500991 ret = 1;
992no_delegation_unlock:
993 spin_unlock(&deleg_cur->lock);
994no_delegation:
995 rcu_read_unlock();
996
997 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500998 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500999 ret = 1;
1000 }
1001
1002 return ret;
1003}
1004
1005
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001006static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001007{
1008 struct nfs_delegation *delegation;
1009
1010 rcu_read_lock();
1011 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001012 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001013 rcu_read_unlock();
1014 return;
1015 }
1016 rcu_read_unlock();
1017 nfs_inode_return_delegation(inode);
1018}
1019
Trond Myklebust6ee41262007-07-08 14:11:36 -04001020static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001021{
1022 struct nfs4_state *state = opendata->state;
1023 struct nfs_inode *nfsi = NFS_I(state->inode);
1024 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001025 int open_mode = opendata->o_arg.open_flags & O_EXCL;
1026 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001027 nfs4_stateid stateid;
1028 int ret = -EAGAIN;
1029
Trond Myklebustaac00a82007-07-05 19:02:21 -04001030 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001031 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001032 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001033 if (can_open_cached(state, fmode, open_mode)) {
1034 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001035 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001036 goto out_return_state;
1037 }
1038 spin_unlock(&state->owner->so_lock);
1039 }
Trond Myklebust34310432008-12-23 15:21:38 -05001040 rcu_read_lock();
1041 delegation = rcu_dereference(nfsi->delegation);
1042 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001043 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001044 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001045 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001046 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001047 /* Save the delegation */
1048 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1049 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001050 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001051 if (ret != 0)
1052 goto out;
1053 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001054
1055 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001056 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001057 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001058 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001059out:
1060 return ERR_PTR(ret);
1061out_return_state:
1062 atomic_inc(&state->count);
1063 return state;
1064}
1065
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001066static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1067{
1068 struct inode *inode;
1069 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001070 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001071 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001072
Trond Myklebustaac00a82007-07-05 19:02:21 -04001073 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001074 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001075 goto out;
1076 }
1077
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001078 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001079 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001080 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001081 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001082 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001083 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001084 goto err;
1085 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001086 state = nfs4_get_open_state(inode, data->owner);
1087 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001088 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001089 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001090 int delegation_flags = 0;
1091
Trond Myklebust003707c2007-07-05 18:07:55 -04001092 rcu_read_lock();
1093 delegation = rcu_dereference(NFS_I(inode)->delegation);
1094 if (delegation)
1095 delegation_flags = delegation->flags;
1096 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -05001097 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001098 nfs_inode_set_delegation(state->inode,
1099 data->owner->so_cred,
1100 &data->o_res);
1101 else
1102 nfs_inode_reclaim_delegation(state->inode,
1103 data->owner->so_cred,
1104 &data->o_res);
1105 }
Trond Myklebust34310432008-12-23 15:21:38 -05001106
1107 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001108 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001109 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001110out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001111 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001112err_put_inode:
1113 iput(inode);
1114err:
1115 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001116}
1117
Trond Myklebust864472e2006-01-03 09:55:15 +01001118static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1119{
1120 struct nfs_inode *nfsi = NFS_I(state->inode);
1121 struct nfs_open_context *ctx;
1122
1123 spin_lock(&state->inode->i_lock);
1124 list_for_each_entry(ctx, &nfsi->open_files, list) {
1125 if (ctx->state != state)
1126 continue;
1127 get_nfs_open_context(ctx);
1128 spin_unlock(&state->inode->i_lock);
1129 return ctx;
1130 }
1131 spin_unlock(&state->inode->i_lock);
1132 return ERR_PTR(-ENOENT);
1133}
1134
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001135static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1136{
1137 struct nfs4_opendata *opendata;
1138
Al Viro3d4ff432011-06-22 18:40:12 -04001139 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001140 if (opendata == NULL)
1141 return ERR_PTR(-ENOMEM);
1142 opendata->state = state;
1143 atomic_inc(&state->count);
1144 return opendata;
1145}
1146
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001147static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001148{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001149 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001150 int ret;
1151
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001152 opendata->o_arg.open_flags = 0;
1153 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001154 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1155 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1156 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001157 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001158 if (ret != 0)
1159 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001160 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001161 if (IS_ERR(newstate))
1162 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001163 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001164 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001165 return 0;
1166}
1167
1168static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1169{
Trond Myklebust864472e2006-01-03 09:55:15 +01001170 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001171 int ret;
1172
1173 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001174 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001175 smp_rmb();
1176 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001177 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001178 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001179 if (ret != 0)
1180 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001181 if (newstate != state)
1182 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001183 }
1184 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001185 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001186 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001187 if (ret != 0)
1188 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001189 if (newstate != state)
1190 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001191 }
1192 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001193 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001194 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001195 if (ret != 0)
1196 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001197 if (newstate != state)
1198 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001199 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001200 /*
1201 * We may have performed cached opens for all three recoveries.
1202 * Check if we need to update the current stateid.
1203 */
1204 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1205 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001206 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001207 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1208 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001209 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001210 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001211 return 0;
1212}
1213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214/*
1215 * OPEN_RECLAIM:
1216 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001218static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001220 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001221 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001222 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 int status;
1224
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001225 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1226 if (IS_ERR(opendata))
1227 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001228 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1229 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001230 rcu_read_lock();
1231 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001232 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001233 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001234 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001235 opendata->o_arg.u.delegation_type = delegation_type;
1236 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001237 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 return status;
1239}
1240
Trond Myklebust539cd032007-06-05 11:46:42 -04001241static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
1243 struct nfs_server *server = NFS_SERVER(state->inode);
1244 struct nfs4_exception exception = { };
1245 int err;
1246 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001247 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001248 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001249 break;
1250 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 } while (exception.retry);
1252 return err;
1253}
1254
Trond Myklebust864472e2006-01-03 09:55:15 +01001255static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1256{
1257 struct nfs_open_context *ctx;
1258 int ret;
1259
1260 ctx = nfs4_state_find_open_context(state);
1261 if (IS_ERR(ctx))
1262 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001263 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001264 put_nfs_open_context(ctx);
1265 return ret;
1266}
1267
Trond Myklebust13437e12007-07-06 15:10:43 -04001268static 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 -07001269{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001270 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001271 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001273 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1274 if (IS_ERR(opendata))
1275 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001276 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001277 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001278 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001279 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001280 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001281 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282}
1283
Trond Myklebust13437e12007-07-06 15:10:43 -04001284int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
1286 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001287 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 int err;
1289 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001290 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 switch (err) {
1292 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001293 case -ENOENT:
1294 case -ESTALE:
1295 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001296 case -NFS4ERR_BADSESSION:
1297 case -NFS4ERR_BADSLOT:
1298 case -NFS4ERR_BAD_HIGH_SLOT:
1299 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1300 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001301 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001302 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 case -NFS4ERR_STALE_CLIENTID:
1304 case -NFS4ERR_STALE_STATEID:
1305 case -NFS4ERR_EXPIRED:
1306 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001307 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001308 goto out;
1309 case -ERESTARTSYS:
1310 /*
1311 * The show must go on: exit, but mark the
1312 * stateid as needing recovery.
1313 */
1314 case -NFS4ERR_ADMIN_REVOKED:
1315 case -NFS4ERR_BAD_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001316 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001317 case -EKEYEXPIRED:
1318 /*
1319 * User RPCSEC_GSS context has expired.
1320 * We cannot recover this stateid now, so
1321 * skip it and allow recovery thread to
1322 * proceed.
1323 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001324 case -ENOMEM:
1325 err = 0;
1326 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 }
1328 err = nfs4_handle_exception(server, err, &exception);
1329 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001330out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 return err;
1332}
1333
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001334static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1335{
1336 struct nfs4_opendata *data = calldata;
1337
1338 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001339 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001340 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1341 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001342 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001343 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001344 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001345 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001346}
1347
1348static void nfs4_open_confirm_release(void *calldata)
1349{
1350 struct nfs4_opendata *data = calldata;
1351 struct nfs4_state *state = NULL;
1352
1353 /* If this request hasn't been cancelled, do nothing */
1354 if (data->cancelled == 0)
1355 goto out_free;
1356 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001357 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001358 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001359 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001360 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001361 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001362out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001363 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001364}
1365
1366static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001367 .rpc_call_done = nfs4_open_confirm_done,
1368 .rpc_release = nfs4_open_confirm_release,
1369};
1370
1371/*
1372 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1373 */
1374static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1375{
1376 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1377 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001378 struct rpc_message msg = {
1379 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1380 .rpc_argp = &data->c_arg,
1381 .rpc_resp = &data->c_res,
1382 .rpc_cred = data->owner->so_cred,
1383 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001384 struct rpc_task_setup task_setup_data = {
1385 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001386 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001387 .callback_ops = &nfs4_open_confirm_ops,
1388 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001389 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001390 .flags = RPC_TASK_ASYNC,
1391 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 int status;
1393
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001394 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001395 data->rpc_done = 0;
1396 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001397 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001398 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001399 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001400 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001401 status = nfs4_wait_for_completion_rpc_task(task);
1402 if (status != 0) {
1403 data->cancelled = 1;
1404 smp_wmb();
1405 } else
1406 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001407 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 return status;
1409}
1410
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001411static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001413 struct nfs4_opendata *data = calldata;
1414 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001415
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001416 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1417 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001418 /*
1419 * Check if we still need to send an OPEN call, or if we can use
1420 * a delegation instead.
1421 */
1422 if (data->state != NULL) {
1423 struct nfs_delegation *delegation;
1424
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001425 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001426 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001427 rcu_read_lock();
1428 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1429 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001430 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001431 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001432 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001433 }
1434 rcu_read_unlock();
1435 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001436 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001437 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001438 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001439 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001440 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001441 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1442 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001443 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001444 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001445 &data->o_arg.seq_args,
1446 &data->o_res.seq_res, 1, task))
1447 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001448 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001449 return;
1450out_no_action:
1451 task->tk_action = NULL;
1452
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001453}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001455static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1456{
1457 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1458 nfs4_open_prepare(task, calldata);
1459}
1460
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001461static void nfs4_open_done(struct rpc_task *task, void *calldata)
1462{
1463 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001465 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001466
Trond Myklebust14516c32010-07-31 14:29:06 -04001467 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1468 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001469
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001470 if (task->tk_status == 0) {
1471 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001472 case S_IFREG:
1473 break;
1474 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001475 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001476 break;
1477 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001478 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001479 break;
1480 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001481 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001482 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001483 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001484 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1485 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001486 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001487 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001488}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001489
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001490static void nfs4_open_release(void *calldata)
1491{
1492 struct nfs4_opendata *data = calldata;
1493 struct nfs4_state *state = NULL;
1494
1495 /* If this request hasn't been cancelled, do nothing */
1496 if (data->cancelled == 0)
1497 goto out_free;
1498 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001499 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001500 goto out_free;
1501 /* In case we need an open_confirm, no cleanup! */
1502 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1503 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001504 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001505 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001506 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001507out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001508 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001509}
1510
1511static const struct rpc_call_ops nfs4_open_ops = {
1512 .rpc_call_prepare = nfs4_open_prepare,
1513 .rpc_call_done = nfs4_open_done,
1514 .rpc_release = nfs4_open_release,
1515};
1516
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001517static const struct rpc_call_ops nfs4_recover_open_ops = {
1518 .rpc_call_prepare = nfs4_recover_open_prepare,
1519 .rpc_call_done = nfs4_open_done,
1520 .rpc_release = nfs4_open_release,
1521};
1522
1523static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001524{
1525 struct inode *dir = data->dir->d_inode;
1526 struct nfs_server *server = NFS_SERVER(dir);
1527 struct nfs_openargs *o_arg = &data->o_arg;
1528 struct nfs_openres *o_res = &data->o_res;
1529 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001530 struct rpc_message msg = {
1531 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1532 .rpc_argp = o_arg,
1533 .rpc_resp = o_res,
1534 .rpc_cred = data->owner->so_cred,
1535 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001536 struct rpc_task_setup task_setup_data = {
1537 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001538 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001539 .callback_ops = &nfs4_open_ops,
1540 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001541 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001542 .flags = RPC_TASK_ASYNC,
1543 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001544 int status;
1545
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001546 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001547 data->rpc_done = 0;
1548 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001549 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001550 if (isrecover)
1551 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001552 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001553 if (IS_ERR(task))
1554 return PTR_ERR(task);
1555 status = nfs4_wait_for_completion_rpc_task(task);
1556 if (status != 0) {
1557 data->cancelled = 1;
1558 smp_wmb();
1559 } else
1560 status = data->rpc_status;
1561 rpc_put_task(task);
1562
1563 return status;
1564}
1565
1566static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1567{
1568 struct inode *dir = data->dir->d_inode;
1569 struct nfs_openres *o_res = &data->o_res;
1570 int status;
1571
1572 status = nfs4_run_open_task(data, 1);
1573 if (status != 0 || !data->rpc_done)
1574 return status;
1575
1576 nfs_refresh_inode(dir, o_res->dir_attr);
1577
1578 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1579 status = _nfs4_proc_open_confirm(data);
1580 if (status != 0)
1581 return status;
1582 }
1583
1584 return status;
1585}
1586
1587/*
1588 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1589 */
1590static int _nfs4_proc_open(struct nfs4_opendata *data)
1591{
1592 struct inode *dir = data->dir->d_inode;
1593 struct nfs_server *server = NFS_SERVER(dir);
1594 struct nfs_openargs *o_arg = &data->o_arg;
1595 struct nfs_openres *o_res = &data->o_res;
1596 int status;
1597
1598 status = nfs4_run_open_task(data, 0);
Trond Myklebust3e309912007-07-07 13:19:59 -04001599 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001600 return status;
1601
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001602 if (o_arg->open_flags & O_CREAT) {
1603 update_changeattr(dir, &o_res->cinfo);
1604 nfs_post_op_update_inode(dir, o_res->dir_attr);
1605 } else
1606 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001607 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1608 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001610 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001612 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 }
1614 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001615 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001616 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617}
1618
Andy Adamsond83217c2011-03-01 01:34:17 +00001619static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001620{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001621 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001622 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001623
Trond Myklebusta78cb572009-08-09 15:06:19 -04001624 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001625 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001626 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001627 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001628 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1629 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001630 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001631 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001632 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001633 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001634 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001635}
1636
Andy Adamsond83217c2011-03-01 01:34:17 +00001637static int nfs4_recover_expired_lease(struct nfs_server *server)
1638{
1639 return nfs4_client_recover_expired_lease(server->nfs_client);
1640}
1641
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642/*
1643 * OPEN_EXPIRED:
1644 * reclaim state on the server after a network partition.
1645 * Assumes caller holds the appropriate lock
1646 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001647static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001649 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001650 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001652 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1653 if (IS_ERR(opendata))
1654 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001655 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001656 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001657 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001658 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001659 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660}
1661
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001662static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001663{
Trond Myklebust539cd032007-06-05 11:46:42 -04001664 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001665 struct nfs4_exception exception = { };
1666 int err;
1667
1668 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001669 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001670 switch (err) {
1671 default:
1672 goto out;
1673 case -NFS4ERR_GRACE:
1674 case -NFS4ERR_DELAY:
1675 nfs4_handle_exception(server, err, &exception);
1676 err = 0;
1677 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001678 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001679out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001680 return err;
1681}
1682
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1684{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001686 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Trond Myklebust864472e2006-01-03 09:55:15 +01001688 ctx = nfs4_state_find_open_context(state);
1689 if (IS_ERR(ctx))
1690 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001691 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001692 put_nfs_open_context(ctx);
1693 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694}
1695
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001696#if defined(CONFIG_NFS_V4_1)
1697static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1698{
1699 int status;
1700 struct nfs_server *server = NFS_SERVER(state->inode);
1701
1702 status = nfs41_test_stateid(server, state);
1703 if (status == NFS_OK)
1704 return 0;
1705 nfs41_free_stateid(server, state);
1706 return nfs4_open_expired(sp, state);
1707}
1708#endif
1709
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001711 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1712 * fields corresponding to attributes that were used to store the verifier.
1713 * Make sure we clobber those fields in the later setattr call
1714 */
1715static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1716{
1717 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1718 !(sattr->ia_valid & ATTR_ATIME_SET))
1719 sattr->ia_valid |= ATTR_ATIME;
1720
1721 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1722 !(sattr->ia_valid & ATTR_MTIME_SET))
1723 sattr->ia_valid |= ATTR_MTIME;
1724}
1725
1726/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001727 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 */
Al Viro82a2c1b2011-06-22 18:30:55 -04001729static 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 -07001730{
1731 struct nfs4_state_owner *sp;
1732 struct nfs4_state *state = NULL;
1733 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001734 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001735 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
1737 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 status = -ENOMEM;
1739 if (!(sp = nfs4_get_state_owner(server, cred))) {
1740 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1741 goto out_err;
1742 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001743 status = nfs4_recover_expired_lease(server);
1744 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001745 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001746 if (dentry->d_inode != NULL)
1747 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001748 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001749 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001750 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001751 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Al Viro82a2c1b2011-06-22 18:30:55 -04001753 if (dentry->d_inode != NULL)
1754 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001755
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001756 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001758 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001760 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001761 status = PTR_ERR(state);
1762 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001763 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001764 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001765 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001766
1767 if (opendata->o_arg.open_flags & O_EXCL) {
1768 nfs4_exclusive_attrset(opendata, sattr);
1769
1770 nfs_fattr_init(opendata->o_res.f_attr);
1771 status = nfs4_do_setattr(state->inode, cred,
1772 opendata->o_res.f_attr, sattr,
1773 state);
1774 if (status == 0)
1775 nfs_setattr_update_inode(state->inode, sattr);
1776 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1777 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001778 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 *res = state;
1781 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001782err_opendata_put:
1783 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001784err_put_state_owner:
1785 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 *res = NULL;
1788 return status;
1789}
1790
1791
Al Viro82a2c1b2011-06-22 18:30:55 -04001792static 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 -07001793{
1794 struct nfs4_exception exception = { };
1795 struct nfs4_state *res;
1796 int status;
1797
1798 do {
Al Viro82a2c1b2011-06-22 18:30:55 -04001799 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 if (status == 0)
1801 break;
1802 /* NOTE: BAD_SEQID means the server and client disagree about the
1803 * book-keeping w.r.t. state-changing operations
1804 * (OPEN/CLOSE/LOCK/LOCKU...)
1805 * It is actually a sign of a bug on the client or on the server.
1806 *
1807 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001808 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 * have unhashed the old state_owner for us, and that we can
1810 * therefore safely retry using a new one. We should still warn
1811 * the user though...
1812 */
1813 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001814 printk(KERN_WARNING "NFS: v4 server %s "
1815 " returned a bad sequence-id error!\n",
1816 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 exception.retry = 1;
1818 continue;
1819 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001820 /*
1821 * BAD_STATEID on OPEN means that the server cancelled our
1822 * state before it received the OPEN_CONFIRM.
1823 * Recover by retrying the request as per the discussion
1824 * on Page 181 of RFC3530.
1825 */
1826 if (status == -NFS4ERR_BAD_STATEID) {
1827 exception.retry = 1;
1828 continue;
1829 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001830 if (status == -EAGAIN) {
1831 /* We must have found a delegation */
1832 exception.retry = 1;
1833 continue;
1834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1836 status, &exception));
1837 } while (exception.retry);
1838 return res;
1839}
1840
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001841static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1842 struct nfs_fattr *fattr, struct iattr *sattr,
1843 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001845 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001847 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 .iap = sattr,
1849 .server = server,
1850 .bitmask = server->attr_bitmask,
1851 };
1852 struct nfs_setattrres res = {
1853 .fattr = fattr,
1854 .server = server,
1855 };
1856 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001857 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1858 .rpc_argp = &arg,
1859 .rpc_resp = &res,
1860 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001862 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001863 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
Trond Myklebust0e574af2005-10-27 22:12:38 -04001865 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001867 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1868 /* Use that stateid */
1869 } else if (state != NULL) {
Trond Myklebust77041ed2010-07-01 12:49:11 -04001870 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001871 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1873
Bryan Schumaker7c513052011-03-24 17:12:24 +00001874 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001875 if (status == 0 && state != NULL)
1876 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001877 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878}
1879
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001880static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1881 struct nfs_fattr *fattr, struct iattr *sattr,
1882 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001884 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 struct nfs4_exception exception = { };
1886 int err;
1887 do {
1888 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001889 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 &exception);
1891 } while (exception.retry);
1892 return err;
1893}
1894
1895struct nfs4_closedata {
1896 struct inode *inode;
1897 struct nfs4_state *state;
1898 struct nfs_closeargs arg;
1899 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001900 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001901 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00001902 bool roc;
1903 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904};
1905
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001906static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001907{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001908 struct nfs4_closedata *calldata = data;
1909 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04001910 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07001911
Fred Isamanf7e89172011-01-06 11:36:32 +00001912 if (calldata->roc)
1913 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001914 nfs4_put_open_state(calldata->state);
1915 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001916 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04001917 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07001918 kfree(calldata);
1919}
1920
Trond Myklebust88069f72009-12-08 08:33:16 -05001921static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1922 fmode_t fmode)
1923{
1924 spin_lock(&state->owner->so_lock);
1925 if (!(fmode & FMODE_READ))
1926 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1927 if (!(fmode & FMODE_WRITE))
1928 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1929 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1930 spin_unlock(&state->owner->so_lock);
1931}
1932
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001933static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001935 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 struct nfs_server *server = NFS_SERVER(calldata->inode);
1938
Trond Myklebust14516c32010-07-31 14:29:06 -04001939 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1940 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 /* hmm. we are done with the inode, and in the process of freeing
1942 * the state_owner. we keep this around to process errors
1943 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 switch (task->tk_status) {
1945 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00001946 if (calldata->roc)
1947 pnfs_roc_set_barrier(state->inode,
1948 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04001949 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001950 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001951 nfs4_close_clear_stateid_flags(state,
1952 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 break;
1954 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001955 case -NFS4ERR_OLD_STATEID:
1956 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001958 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001959 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001961 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1962 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001964 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001965 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966}
1967
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001968static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001970 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001971 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001972 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001973
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001974 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001975 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001976
Trond Myklebust88069f72009-12-08 08:33:16 -05001977 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1978 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001979 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001980 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001981 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001982 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001983 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1984 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1985 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001986 }
1987 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001988 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1989 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1990 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001991 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001992 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001993 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05001994
1995 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001996 /* Note: exit _without_ calling nfs4_close_done */
1997 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001998 return;
1999 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002000
Fred Isamanf7e89172011-01-06 11:36:32 +00002001 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002002 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002003 if (calldata->roc &&
2004 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2005 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2006 task, NULL);
2007 return;
2008 }
2009 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002010
Trond Myklebust516a6af2005-10-27 22:12:41 -04002011 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002012 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002013 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Andy Adamson19ddab02009-04-01 09:22:20 -04002014 &calldata->arg.seq_args, &calldata->res.seq_res,
2015 1, task))
2016 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002017 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018}
2019
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002020static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002021 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002022 .rpc_call_done = nfs4_close_done,
2023 .rpc_release = nfs4_free_closedata,
2024};
2025
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026/*
2027 * It is possible for data to be read/written from a mem-mapped file
2028 * after the sys_close call (which hits the vfs layer as a flush).
2029 * This means that we can't safely call nfsv4 close on a file until
2030 * the inode is cleared. This in turn means that we are not good
2031 * NFSv4 citizens - we do not indicate to the server to update the file's
2032 * share state even when we are done with one of the three share
2033 * stateid's in the inode.
2034 *
2035 * NOTE: Caller must be holding the sp->so_owner semaphore!
2036 */
Al Viro643168c2011-06-22 18:20:23 -04002037int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002039 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002041 struct nfs4_state_owner *sp = state->owner;
2042 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002043 struct rpc_message msg = {
2044 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2045 .rpc_cred = state->owner->so_cred,
2046 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002047 struct rpc_task_setup task_setup_data = {
2048 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002049 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002050 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002051 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002052 .flags = RPC_TASK_ASYNC,
2053 };
Trond Myklebust95121352005-10-18 14:20:12 -07002054 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002056 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002058 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002059 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002061 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002062 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002064 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002065 if (calldata->arg.seqid == NULL)
2066 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002067 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002068 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002069 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002070 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002071 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002072 calldata->roc = roc;
Al Viro643168c2011-06-22 18:20:23 -04002073 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002074
Trond Myklebust1174dd12010-12-21 10:52:24 -05002075 msg.rpc_argp = &calldata->arg;
2076 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002077 task_setup_data.callback_data = calldata;
2078 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002079 if (IS_ERR(task))
2080 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002081 status = 0;
2082 if (wait)
2083 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002084 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002085 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002086out_free_calldata:
2087 kfree(calldata);
2088out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002089 if (roc)
2090 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002091 nfs4_put_open_state(state);
2092 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002093 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094}
2095
Trond Myklebust2b484292010-09-17 10:56:51 -04002096static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002097nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 struct nfs4_state *state;
2100
Trond Myklebust565277f2007-10-15 18:17:53 -04002101 /* Protect against concurrent sillydeletes */
Al Viro3d4ff432011-06-22 18:40:12 -04002102 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002103 if (IS_ERR(state))
2104 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002105 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002106 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107}
2108
Trond Myklebust1185a552009-12-03 15:54:02 -05002109static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002110{
2111 if (ctx->state == NULL)
2112 return;
2113 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002114 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002115 else
Al Viro643168c2011-06-22 18:20:23 -04002116 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002117}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
2119static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2120{
Benny Halevy43652ad2009-04-01 09:21:54 -04002121 struct nfs4_server_caps_arg args = {
2122 .fhandle = fhandle,
2123 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 struct nfs4_server_caps_res res = {};
2125 struct rpc_message msg = {
2126 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002127 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 .rpc_resp = &res,
2129 };
2130 int status;
2131
Bryan Schumaker7c513052011-03-24 17:12:24 +00002132 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 if (status == 0) {
2134 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002135 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2136 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2137 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2138 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2139 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2141 server->caps |= NFS_CAP_ACLS;
2142 if (res.has_links != 0)
2143 server->caps |= NFS_CAP_HARDLINKS;
2144 if (res.has_symlinks != 0)
2145 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002146 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2147 server->caps |= NFS_CAP_FILEID;
2148 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2149 server->caps |= NFS_CAP_MODE;
2150 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2151 server->caps |= NFS_CAP_NLINK;
2152 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2153 server->caps |= NFS_CAP_OWNER;
2154 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2155 server->caps |= NFS_CAP_OWNER_GROUP;
2156 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2157 server->caps |= NFS_CAP_ATIME;
2158 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2159 server->caps |= NFS_CAP_CTIME;
2160 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2161 server->caps |= NFS_CAP_MTIME;
2162
Trond Myklebusta65318b2009-03-11 14:10:28 -04002163 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2164 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2165 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 server->acl_bitmask = res.acl_bitmask;
2167 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002168
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 return status;
2170}
2171
Trond Myklebust55a97592006-06-09 09:34:19 -04002172int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173{
2174 struct nfs4_exception exception = { };
2175 int err;
2176 do {
2177 err = nfs4_handle_exception(server,
2178 _nfs4_server_capabilities(server, fhandle),
2179 &exception);
2180 } while (exception.retry);
2181 return err;
2182}
2183
2184static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2185 struct nfs_fsinfo *info)
2186{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 struct nfs4_lookup_root_arg args = {
2188 .bitmask = nfs4_fattr_bitmap,
2189 };
2190 struct nfs4_lookup_res res = {
2191 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002192 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 .fh = fhandle,
2194 };
2195 struct rpc_message msg = {
2196 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2197 .rpc_argp = &args,
2198 .rpc_resp = &res,
2199 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002200
Trond Myklebust0e574af2005-10-27 22:12:38 -04002201 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002202 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203}
2204
2205static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2206 struct nfs_fsinfo *info)
2207{
2208 struct nfs4_exception exception = { };
2209 int err;
2210 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002211 err = _nfs4_lookup_root(server, fhandle, info);
2212 switch (err) {
2213 case 0:
2214 case -NFS4ERR_WRONGSEC:
2215 break;
2216 default:
2217 err = nfs4_handle_exception(server, err, &exception);
2218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 } while (exception.retry);
2220 return err;
2221}
2222
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002223static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2224 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2225{
2226 struct rpc_auth *auth;
2227 int ret;
2228
2229 auth = rpcauth_create(flavor, server->client);
2230 if (!auth) {
2231 ret = -EIO;
2232 goto out;
2233 }
2234 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002235out:
2236 return ret;
2237}
2238
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002239static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002240 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002242 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002243 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002245 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2246 flav_array[len] = RPC_AUTH_NULL;
2247 len += 1;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002248
2249 for (i = 0; i < len; i++) {
2250 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002251 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002252 continue;
2253 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002254 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002255 /*
2256 * -EACCESS could mean that the user doesn't have correct permissions
2257 * to access the mount. It could also mean that we tried to mount
2258 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2259 * existing mount programs don't handle -EACCES very well so it should
2260 * be mapped to -EPERM instead.
2261 */
2262 if (status == -EACCES)
2263 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002264 return status;
2265}
2266
2267/*
2268 * get the file handle for the "/" directory on the server
2269 */
2270static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2271 struct nfs_fsinfo *info)
2272{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002273 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002274 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002275 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2276 /*
2277 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2278 * by nfs4_map_errors() as this function exits.
2279 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002280 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 if (status == 0)
2282 status = nfs4_server_capabilities(server, fhandle);
2283 if (status == 0)
2284 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002285 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286}
2287
Andy Adamson533eb462011-06-13 18:25:56 -04002288static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002289/*
2290 * Get locations and (maybe) other attributes of a referral.
2291 * Note that we'll actually follow the referral later when
2292 * we detect fsid mismatch in inode revalidation
2293 */
Andy Adamson533eb462011-06-13 18:25:56 -04002294static int nfs4_get_referral(struct inode *dir, const struct qstr *name,
2295 struct nfs_fattr *fattr, struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002296{
2297 int status = -ENOMEM;
2298 struct page *page = NULL;
2299 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002300
2301 page = alloc_page(GFP_KERNEL);
2302 if (page == NULL)
2303 goto out;
2304 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2305 if (locations == NULL)
2306 goto out;
2307
Trond Myklebustc228fd32007-01-13 02:28:11 -05002308 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002309 if (status != 0)
2310 goto out;
2311 /* Make sure server returned a different fsid for the referral */
2312 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002313 dprintk("%s: server did not return a different fsid for"
2314 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002315 status = -EIO;
2316 goto out;
2317 }
Andy Adamson533eb462011-06-13 18:25:56 -04002318 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2319 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002320
Andy Adamson533eb462011-06-13 18:25:56 -04002321 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002322 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002323 memset(fhandle, 0, sizeof(struct nfs_fh));
2324out:
2325 if (page)
2326 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002327 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002328 return status;
2329}
2330
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2332{
2333 struct nfs4_getattr_arg args = {
2334 .fh = fhandle,
2335 .bitmask = server->attr_bitmask,
2336 };
2337 struct nfs4_getattr_res res = {
2338 .fattr = fattr,
2339 .server = server,
2340 };
2341 struct rpc_message msg = {
2342 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2343 .rpc_argp = &args,
2344 .rpc_resp = &res,
2345 };
2346
Trond Myklebust0e574af2005-10-27 22:12:38 -04002347 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002348 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349}
2350
2351static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2352{
2353 struct nfs4_exception exception = { };
2354 int err;
2355 do {
2356 err = nfs4_handle_exception(server,
2357 _nfs4_proc_getattr(server, fhandle, fattr),
2358 &exception);
2359 } while (exception.retry);
2360 return err;
2361}
2362
2363/*
2364 * The file is not closed if it is opened due to the a request to change
2365 * the size of the file. The open call will not be needed once the
2366 * VFS layer lookup-intents are implemented.
2367 *
2368 * Close is called when the inode is destroyed.
2369 * If we haven't opened the file for O_WRONLY, we
2370 * need to in the size_change case to obtain a stateid.
2371 *
2372 * Got race?
2373 * Because OPEN is always done by name in nfsv4, it is
2374 * possible that we opened a different file by the same
2375 * name. We can recognize this race condition, but we
2376 * can't do anything about it besides returning an error.
2377 *
2378 * This will be fixed with VFS changes (lookup-intent).
2379 */
2380static int
2381nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2382 struct iattr *sattr)
2383{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002384 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002385 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002386 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 int status;
2388
Benny Halevy8a1636c2010-07-14 15:43:57 -04002389 if (pnfs_ld_layoutret_on_setattr(inode))
2390 pnfs_return_layout(inode);
2391
Trond Myklebust0e574af2005-10-27 22:12:38 -04002392 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
Trond Myklebustd5308382005-11-04 15:33:38 -05002394 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002395 if (sattr->ia_valid & ATTR_FILE) {
2396 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002397
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002398 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002399 if (ctx) {
2400 cred = ctx->cred;
2401 state = ctx->state;
2402 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002403 }
2404
2405 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002406 if (status == 0)
2407 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 return status;
2409}
2410
Bryan Schumaker7c513052011-03-24 17:12:24 +00002411static int _nfs4_proc_lookupfh(struct rpc_clnt *clnt, struct nfs_server *server,
2412 const struct nfs_fh *dirfh, const struct qstr *name,
2413 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002414{
2415 int status;
2416 struct nfs4_lookup_arg args = {
2417 .bitmask = server->attr_bitmask,
2418 .dir_fh = dirfh,
2419 .name = name,
2420 };
2421 struct nfs4_lookup_res res = {
2422 .server = server,
2423 .fattr = fattr,
2424 .fh = fhandle,
2425 };
2426 struct rpc_message msg = {
2427 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2428 .rpc_argp = &args,
2429 .rpc_resp = &res,
2430 };
2431
2432 nfs_fattr_init(fattr);
2433
2434 dprintk("NFS call lookupfh %s\n", name->name);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002435 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002436 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002437 return status;
2438}
2439
2440static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2441 struct qstr *name, struct nfs_fh *fhandle,
2442 struct nfs_fattr *fattr)
2443{
2444 struct nfs4_exception exception = { };
2445 int err;
2446 do {
Bryan Schumaker7c513052011-03-24 17:12:24 +00002447 err = _nfs4_proc_lookupfh(server->client, server, dirfh, name, fhandle, fattr);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002448 /* FIXME: !!!! */
2449 if (err == -NFS4ERR_MOVED) {
2450 err = -EREMOTE;
2451 break;
2452 }
2453 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002454 } while (exception.retry);
2455 return err;
2456}
2457
Bryan Schumaker7c513052011-03-24 17:12:24 +00002458static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2459 const struct qstr *name, struct nfs_fh *fhandle,
2460 struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002462 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
2464 dprintk("NFS call lookup %s\n", name->name);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002465 status = _nfs4_proc_lookupfh(clnt, NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002466 if (status == -NFS4ERR_MOVED)
2467 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 dprintk("NFS reply lookup: %d\n", status);
2469 return status;
2470}
2471
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002472void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2473{
2474 memset(fh, 0, sizeof(struct nfs_fh));
2475 fattr->fsid.major = 1;
2476 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2477 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2478 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2479 fattr->nlink = 2;
2480}
2481
Bryan Schumaker7c513052011-03-24 17:12:24 +00002482static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2483 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484{
2485 struct nfs4_exception exception = { };
2486 int err;
2487 do {
2488 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumaker7c513052011-03-24 17:12:24 +00002489 _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 &exception);
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002491 if (err == -EPERM)
2492 nfs_fixup_secinfo_attributes(fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 } while (exception.retry);
2494 return err;
2495}
2496
2497static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2498{
Trond Myklebust76b32992007-08-10 17:45:11 -04002499 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 struct nfs4_accessargs args = {
2501 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002502 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002504 struct nfs4_accessres res = {
2505 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002506 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 struct rpc_message msg = {
2508 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2509 .rpc_argp = &args,
2510 .rpc_resp = &res,
2511 .rpc_cred = entry->cred,
2512 };
2513 int mode = entry->mask;
2514 int status;
2515
2516 /*
2517 * Determine which access bits we want to ask for...
2518 */
2519 if (mode & MAY_READ)
2520 args.access |= NFS4_ACCESS_READ;
2521 if (S_ISDIR(inode->i_mode)) {
2522 if (mode & MAY_WRITE)
2523 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2524 if (mode & MAY_EXEC)
2525 args.access |= NFS4_ACCESS_LOOKUP;
2526 } else {
2527 if (mode & MAY_WRITE)
2528 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2529 if (mode & MAY_EXEC)
2530 args.access |= NFS4_ACCESS_EXECUTE;
2531 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002532
2533 res.fattr = nfs_alloc_fattr();
2534 if (res.fattr == NULL)
2535 return -ENOMEM;
2536
Bryan Schumaker7c513052011-03-24 17:12:24 +00002537 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 if (!status) {
2539 entry->mask = 0;
2540 if (res.access & NFS4_ACCESS_READ)
2541 entry->mask |= MAY_READ;
2542 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2543 entry->mask |= MAY_WRITE;
2544 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2545 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002546 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002548 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 return status;
2550}
2551
2552static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2553{
2554 struct nfs4_exception exception = { };
2555 int err;
2556 do {
2557 err = nfs4_handle_exception(NFS_SERVER(inode),
2558 _nfs4_proc_access(inode, entry),
2559 &exception);
2560 } while (exception.retry);
2561 return err;
2562}
2563
2564/*
2565 * TODO: For the time being, we don't try to get any attributes
2566 * along with any of the zero-copy operations READ, READDIR,
2567 * READLINK, WRITE.
2568 *
2569 * In the case of the first three, we want to put the GETATTR
2570 * after the read-type operation -- this is because it is hard
2571 * to predict the length of a GETATTR response in v4, and thus
2572 * align the READ data correctly. This means that the GETATTR
2573 * may end up partially falling into the page cache, and we should
2574 * shift it into the 'tail' of the xdr_buf before processing.
2575 * To do this efficiently, we need to know the total length
2576 * of data received, which doesn't seem to be available outside
2577 * of the RPC layer.
2578 *
2579 * In the case of WRITE, we also want to put the GETATTR after
2580 * the operation -- in this case because we want to make sure
2581 * we get the post-operation mtime and size. This means that
2582 * we can't use xdr_encode_pages() as written: we need a variant
2583 * of it which would leave room in the 'tail' iovec.
2584 *
2585 * Both of these changes to the XDR layer would in fact be quite
2586 * minor, but I decided to leave them for a subsequent patch.
2587 */
2588static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2589 unsigned int pgbase, unsigned int pglen)
2590{
2591 struct nfs4_readlink args = {
2592 .fh = NFS_FH(inode),
2593 .pgbase = pgbase,
2594 .pglen = pglen,
2595 .pages = &page,
2596 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002597 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 struct rpc_message msg = {
2599 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2600 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002601 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 };
2603
Bryan Schumaker7c513052011-03-24 17:12:24 +00002604 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 -07002605}
2606
2607static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2608 unsigned int pgbase, unsigned int pglen)
2609{
2610 struct nfs4_exception exception = { };
2611 int err;
2612 do {
2613 err = nfs4_handle_exception(NFS_SERVER(inode),
2614 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2615 &exception);
2616 } while (exception.retry);
2617 return err;
2618}
2619
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620/*
2621 * Got race?
2622 * We will need to arrange for the VFS layer to provide an atomic open.
2623 * Until then, this create/open method is prone to inefficiency and race
2624 * conditions due to the lookup, create, and open VFS calls from sys_open()
2625 * placed on the wire.
2626 *
2627 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2628 * The file will be opened again in the subsequent VFS open call
2629 * (nfs4_proc_file_open).
2630 *
2631 * The open for read will just hang around to be used by any process that
2632 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2633 */
2634
2635static int
2636nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002637 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638{
Al Viro82a2c1b2011-06-22 18:30:55 -04002639 struct dentry *de = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002641 struct rpc_cred *cred = NULL;
2642 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 int status = 0;
2644
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002645 if (ctx != NULL) {
2646 cred = ctx->cred;
Al Viro3d4ff432011-06-22 18:40:12 -04002647 de = ctx->dentry;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002648 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002650 sattr->ia_mode &= ~current_umask();
Al Viro82a2c1b2011-06-22 18:30:55 -04002651 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002652 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 if (IS_ERR(state)) {
2654 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002655 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002657 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002658 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002659 if (ctx != NULL)
2660 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002661 else
Al Viro643168c2011-06-22 18:20:23 -04002662 nfs4_close_sync(state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663out:
2664 return status;
2665}
2666
2667static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2668{
Trond Myklebust16e42952005-10-27 22:12:44 -04002669 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002670 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002672 .name.len = name->len,
2673 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002674 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002676 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002677 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002678 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002680 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2681 .rpc_argp = &args,
2682 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002684 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685
Trond Myklebustd3468902010-04-16 16:22:50 -04002686 res.dir_attr = nfs_alloc_fattr();
2687 if (res.dir_attr == NULL)
2688 goto out;
2689
Bryan Schumaker7c513052011-03-24 17:12:24 +00002690 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002691 if (status == 0) {
2692 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002693 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002694 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002695 nfs_free_fattr(res.dir_attr);
2696out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 return status;
2698}
2699
2700static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2701{
2702 struct nfs4_exception exception = { };
2703 int err;
2704 do {
2705 err = nfs4_handle_exception(NFS_SERVER(dir),
2706 _nfs4_proc_remove(dir, name),
2707 &exception);
2708 } while (exception.retry);
2709 return err;
2710}
2711
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002712static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002714 struct nfs_server *server = NFS_SERVER(dir);
2715 struct nfs_removeargs *args = msg->rpc_argp;
2716 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717
Trond Myklebusta65318b2009-03-11 14:10:28 -04002718 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002719 res->server = server;
Benny Halevydfb4f3092010-09-24 09:17:01 -04002720 res->seq_res.sr_slot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722}
2723
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002724static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002726 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2727
Trond Myklebust14516c32010-07-31 14:29:06 -04002728 if (!nfs4_sequence_done(task, &res->seq_res))
2729 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002730 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002731 return 0;
2732 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002733 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002734 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735}
2736
Jeff Laytond3d41522010-09-17 17:31:57 -04002737static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2738{
2739 struct nfs_server *server = NFS_SERVER(dir);
2740 struct nfs_renameargs *arg = msg->rpc_argp;
2741 struct nfs_renameres *res = msg->rpc_resp;
2742
2743 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2744 arg->bitmask = server->attr_bitmask;
2745 res->server = server;
2746}
2747
2748static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2749 struct inode *new_dir)
2750{
2751 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2752
2753 if (!nfs4_sequence_done(task, &res->seq_res))
2754 return 0;
2755 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2756 return 0;
2757
2758 update_changeattr(old_dir, &res->old_cinfo);
2759 nfs_post_op_update_inode(old_dir, res->old_fattr);
2760 update_changeattr(new_dir, &res->new_cinfo);
2761 nfs_post_op_update_inode(new_dir, res->new_fattr);
2762 return 1;
2763}
2764
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2766 struct inode *new_dir, struct qstr *new_name)
2767{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002768 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002769 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 .old_dir = NFS_FH(old_dir),
2771 .new_dir = NFS_FH(new_dir),
2772 .old_name = old_name,
2773 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002774 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002776 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002777 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002778 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 struct rpc_message msg = {
2780 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2781 .rpc_argp = &arg,
2782 .rpc_resp = &res,
2783 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002784 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
Trond Myklebust011fff72010-04-16 16:22:49 -04002786 res.old_fattr = nfs_alloc_fattr();
2787 res.new_fattr = nfs_alloc_fattr();
2788 if (res.old_fattr == NULL || res.new_fattr == NULL)
2789 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790
Bryan Schumaker7c513052011-03-24 17:12:24 +00002791 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 if (!status) {
2793 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002794 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002796 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002798out:
2799 nfs_free_fattr(res.new_fattr);
2800 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 return status;
2802}
2803
2804static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2805 struct inode *new_dir, struct qstr *new_name)
2806{
2807 struct nfs4_exception exception = { };
2808 int err;
2809 do {
2810 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2811 _nfs4_proc_rename(old_dir, old_name,
2812 new_dir, new_name),
2813 &exception);
2814 } while (exception.retry);
2815 return err;
2816}
2817
2818static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2819{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002820 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 struct nfs4_link_arg arg = {
2822 .fh = NFS_FH(inode),
2823 .dir_fh = NFS_FH(dir),
2824 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002825 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002827 struct nfs4_link_res res = {
2828 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002829 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 struct rpc_message msg = {
2831 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2832 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002833 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002835 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836
Trond Myklebust136f2622010-04-16 16:22:49 -04002837 res.fattr = nfs_alloc_fattr();
2838 res.dir_attr = nfs_alloc_fattr();
2839 if (res.fattr == NULL || res.dir_attr == NULL)
2840 goto out;
2841
Bryan Schumaker7c513052011-03-24 17:12:24 +00002842 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002843 if (!status) {
2844 update_changeattr(dir, &res.cinfo);
2845 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002846 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002847 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002848out:
2849 nfs_free_fattr(res.dir_attr);
2850 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 return status;
2852}
2853
2854static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2855{
2856 struct nfs4_exception exception = { };
2857 int err;
2858 do {
2859 err = nfs4_handle_exception(NFS_SERVER(inode),
2860 _nfs4_proc_link(inode, dir, name),
2861 &exception);
2862 } while (exception.retry);
2863 return err;
2864}
2865
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002866struct nfs4_createdata {
2867 struct rpc_message msg;
2868 struct nfs4_create_arg arg;
2869 struct nfs4_create_res res;
2870 struct nfs_fh fh;
2871 struct nfs_fattr fattr;
2872 struct nfs_fattr dir_fattr;
2873};
2874
2875static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2876 struct qstr *name, struct iattr *sattr, u32 ftype)
2877{
2878 struct nfs4_createdata *data;
2879
2880 data = kzalloc(sizeof(*data), GFP_KERNEL);
2881 if (data != NULL) {
2882 struct nfs_server *server = NFS_SERVER(dir);
2883
2884 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2885 data->msg.rpc_argp = &data->arg;
2886 data->msg.rpc_resp = &data->res;
2887 data->arg.dir_fh = NFS_FH(dir);
2888 data->arg.server = server;
2889 data->arg.name = name;
2890 data->arg.attrs = sattr;
2891 data->arg.ftype = ftype;
2892 data->arg.bitmask = server->attr_bitmask;
2893 data->res.server = server;
2894 data->res.fh = &data->fh;
2895 data->res.fattr = &data->fattr;
2896 data->res.dir_fattr = &data->dir_fattr;
2897 nfs_fattr_init(data->res.fattr);
2898 nfs_fattr_init(data->res.dir_fattr);
2899 }
2900 return data;
2901}
2902
2903static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2904{
Bryan Schumaker7c513052011-03-24 17:12:24 +00002905 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00002906 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002907 if (status == 0) {
2908 update_changeattr(dir, &data->res.dir_cinfo);
2909 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2910 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2911 }
2912 return status;
2913}
2914
2915static void nfs4_free_createdata(struct nfs4_createdata *data)
2916{
2917 kfree(data);
2918}
2919
Chuck Lever4f390c12006-08-22 20:06:22 -04002920static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002921 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002923 struct nfs4_createdata *data;
2924 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925
Chuck Lever94a6d752006-08-22 20:06:23 -04002926 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002927 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002928
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002929 status = -ENOMEM;
2930 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2931 if (data == NULL)
2932 goto out;
2933
2934 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2935 data->arg.u.symlink.pages = &page;
2936 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002938 status = nfs4_do_create(dir, dentry, data);
2939
2940 nfs4_free_createdata(data);
2941out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 return status;
2943}
2944
Chuck Lever4f390c12006-08-22 20:06:22 -04002945static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002946 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947{
2948 struct nfs4_exception exception = { };
2949 int err;
2950 do {
2951 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002952 _nfs4_proc_symlink(dir, dentry, page,
2953 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 &exception);
2955 } while (exception.retry);
2956 return err;
2957}
2958
2959static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2960 struct iattr *sattr)
2961{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002962 struct nfs4_createdata *data;
2963 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002965 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2966 if (data == NULL)
2967 goto out;
2968
2969 status = nfs4_do_create(dir, dentry, data);
2970
2971 nfs4_free_createdata(data);
2972out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 return status;
2974}
2975
2976static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2977 struct iattr *sattr)
2978{
2979 struct nfs4_exception exception = { };
2980 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002981
2982 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 do {
2984 err = nfs4_handle_exception(NFS_SERVER(dir),
2985 _nfs4_proc_mkdir(dir, dentry, sattr),
2986 &exception);
2987 } while (exception.retry);
2988 return err;
2989}
2990
2991static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002992 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993{
2994 struct inode *dir = dentry->d_inode;
2995 struct nfs4_readdir_arg args = {
2996 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002997 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 .pgbase = 0,
2999 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09003000 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04003001 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 };
3003 struct nfs4_readdir_res res;
3004 struct rpc_message msg = {
3005 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
3006 .rpc_argp = &args,
3007 .rpc_resp = &res,
3008 .rpc_cred = cred,
3009 };
3010 int status;
3011
Harvey Harrison3110ff82008-05-02 13:42:44 -07003012 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003013 dentry->d_parent->d_name.name,
3014 dentry->d_name.name,
3015 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3017 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003018 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 -05003019 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003021 status += args.pgbase;
3022 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003023
3024 nfs_invalidate_atime(dir);
3025
Harvey Harrison3110ff82008-05-02 13:42:44 -07003026 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 return status;
3028}
3029
3030static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003031 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032{
3033 struct nfs4_exception exception = { };
3034 int err;
3035 do {
3036 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3037 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003038 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 &exception);
3040 } while (exception.retry);
3041 return err;
3042}
3043
3044static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3045 struct iattr *sattr, dev_t rdev)
3046{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003047 struct nfs4_createdata *data;
3048 int mode = sattr->ia_mode;
3049 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
3051 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3052 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003053
3054 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3055 if (data == NULL)
3056 goto out;
3057
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003059 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003061 data->arg.ftype = NF4BLK;
3062 data->arg.u.device.specdata1 = MAJOR(rdev);
3063 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 }
3065 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003066 data->arg.ftype = NF4CHR;
3067 data->arg.u.device.specdata1 = MAJOR(rdev);
3068 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003071 status = nfs4_do_create(dir, dentry, data);
3072
3073 nfs4_free_createdata(data);
3074out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 return status;
3076}
3077
3078static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3079 struct iattr *sattr, dev_t rdev)
3080{
3081 struct nfs4_exception exception = { };
3082 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003083
3084 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 do {
3086 err = nfs4_handle_exception(NFS_SERVER(dir),
3087 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3088 &exception);
3089 } while (exception.retry);
3090 return err;
3091}
3092
3093static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3094 struct nfs_fsstat *fsstat)
3095{
3096 struct nfs4_statfs_arg args = {
3097 .fh = fhandle,
3098 .bitmask = server->attr_bitmask,
3099 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003100 struct nfs4_statfs_res res = {
3101 .fsstat = fsstat,
3102 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 struct rpc_message msg = {
3104 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3105 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003106 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 };
3108
Trond Myklebust0e574af2005-10-27 22:12:38 -04003109 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003110 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111}
3112
3113static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3114{
3115 struct nfs4_exception exception = { };
3116 int err;
3117 do {
3118 err = nfs4_handle_exception(server,
3119 _nfs4_proc_statfs(server, fhandle, fsstat),
3120 &exception);
3121 } while (exception.retry);
3122 return err;
3123}
3124
3125static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3126 struct nfs_fsinfo *fsinfo)
3127{
3128 struct nfs4_fsinfo_arg args = {
3129 .fh = fhandle,
3130 .bitmask = server->attr_bitmask,
3131 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003132 struct nfs4_fsinfo_res res = {
3133 .fsinfo = fsinfo,
3134 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 struct rpc_message msg = {
3136 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3137 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003138 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 };
3140
Bryan Schumaker7c513052011-03-24 17:12:24 +00003141 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142}
3143
3144static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3145{
3146 struct nfs4_exception exception = { };
3147 int err;
3148
3149 do {
3150 err = nfs4_handle_exception(server,
3151 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3152 &exception);
3153 } while (exception.retry);
3154 return err;
3155}
3156
3157static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3158{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003159 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3161}
3162
3163static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3164 struct nfs_pathconf *pathconf)
3165{
3166 struct nfs4_pathconf_arg args = {
3167 .fh = fhandle,
3168 .bitmask = server->attr_bitmask,
3169 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003170 struct nfs4_pathconf_res res = {
3171 .pathconf = pathconf,
3172 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 struct rpc_message msg = {
3174 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3175 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003176 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 };
3178
3179 /* None of the pathconf attributes are mandatory to implement */
3180 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3181 memset(pathconf, 0, sizeof(*pathconf));
3182 return 0;
3183 }
3184
Trond Myklebust0e574af2005-10-27 22:12:38 -04003185 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003186 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187}
3188
3189static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3190 struct nfs_pathconf *pathconf)
3191{
3192 struct nfs4_exception exception = { };
3193 int err;
3194
3195 do {
3196 err = nfs4_handle_exception(server,
3197 _nfs4_proc_pathconf(server, fhandle, pathconf),
3198 &exception);
3199 } while (exception.retry);
3200 return err;
3201}
3202
Benny Halevyd20581a2011-05-22 19:52:03 +03003203void __nfs4_read_done_cb(struct nfs_read_data *data)
3204{
3205 nfs_invalidate_atime(data->inode);
3206}
3207
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003208static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209{
Trond Myklebustec06c092006-03-20 13:44:27 -05003210 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003212 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003213 nfs_restart_rpc(task, server->nfs_client);
Trond Myklebustec06c092006-03-20 13:44:27 -05003214 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003216
Benny Halevyd20581a2011-05-22 19:52:03 +03003217 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003219 renew_lease(server, data->timestamp);
3220 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221}
3222
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003223static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3224{
3225
3226 dprintk("--> %s\n", __func__);
3227
3228 if (!nfs4_sequence_done(task, &data->res.seq_res))
3229 return -EAGAIN;
3230
Benny Halevyd20581a2011-05-22 19:52:03 +03003231 return data->read_done_cb ? data->read_done_cb(task, data) :
3232 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003233}
3234
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003235static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003238 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003239 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240}
3241
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003242/* Reset the the nfs_read_data to send the read to the MDS. */
3243void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3244{
3245 dprintk("%s Reset task for i/o through\n", __func__);
3246 put_lseg(data->lseg);
3247 data->lseg = NULL;
3248 /* offsets will differ in the dense stripe case */
3249 data->args.offset = data->mds_offset;
3250 data->ds_clp = NULL;
3251 data->args.fh = NFS_FH(data->inode);
3252 data->read_done_cb = nfs4_read_done_cb;
3253 task->tk_ops = data->mds_ops;
3254 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3255}
3256EXPORT_SYMBOL_GPL(nfs4_reset_read);
3257
Fred Isamanb029bc92011-03-03 15:13:42 +00003258static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 struct inode *inode = data->inode;
3261
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003262 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003263 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003264 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003266 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003268 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003269 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003270 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271}
3272
Fred Isamanb029bc92011-03-03 15:13:42 +00003273static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3274{
3275 if (!nfs4_sequence_done(task, &data->res.seq_res))
3276 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003277 return data->write_done_cb ? data->write_done_cb(task, data) :
3278 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003279}
3280
Fred Isamana69aef12011-03-03 15:13:47 +00003281/* Reset the the nfs_write_data to send the write to the MDS. */
3282void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3283{
3284 dprintk("%s Reset task for i/o through\n", __func__);
3285 put_lseg(data->lseg);
3286 data->lseg = NULL;
3287 data->ds_clp = NULL;
3288 data->write_done_cb = nfs4_write_done_cb;
3289 data->args.fh = NFS_FH(data->inode);
3290 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3291 data->args.offset = data->mds_offset;
3292 data->res.fattr = &data->fattr;
3293 task->tk_ops = data->mds_ops;
3294 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3295}
3296EXPORT_SYMBOL_GPL(nfs4_reset_write);
3297
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003298static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003300 struct nfs_server *server = NFS_SERVER(data->inode);
3301
Fred Isaman7ffd1062011-03-03 15:13:46 +00003302 if (data->lseg) {
3303 data->args.bitmask = NULL;
3304 data->res.fattr = NULL;
3305 } else
3306 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isamanb029bc92011-03-03 15:13:42 +00003307 if (!data->write_done_cb)
3308 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003309 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 data->timestamp = jiffies;
3311
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003312 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313}
3314
Fred Isaman5f452432011-03-23 13:27:46 +00003315static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003318
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003319 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003320 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003321 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003323 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003324 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325}
3326
Fred Isaman5f452432011-03-23 13:27:46 +00003327static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3328{
3329 if (!nfs4_sequence_done(task, &data->res.seq_res))
3330 return -EAGAIN;
3331 return data->write_done_cb(task, data);
3332}
3333
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003334static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003336 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003337
3338 if (data->lseg) {
3339 data->args.bitmask = NULL;
3340 data->res.fattr = NULL;
3341 } else
3342 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isaman5f452432011-03-23 13:27:46 +00003343 if (!data->write_done_cb)
3344 data->write_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003345 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003346 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347}
3348
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003349struct nfs4_renewdata {
3350 struct nfs_client *client;
3351 unsigned long timestamp;
3352};
3353
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354/*
3355 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3356 * standalone procedure for queueing an asynchronous RENEW.
3357 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003358static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003359{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003360 struct nfs4_renewdata *data = calldata;
3361 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003362
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003363 if (atomic_read(&clp->cl_count) > 1)
3364 nfs4_schedule_state_renewal(clp);
3365 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003366 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003367}
3368
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003369static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003371 struct nfs4_renewdata *data = calldata;
3372 struct nfs_client *clp = data->client;
3373 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374
3375 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003376 /* Unless we're shutting down, schedule state recovery! */
3377 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003378 nfs4_schedule_lease_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 return;
3380 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003381 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382}
3383
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003384static const struct rpc_call_ops nfs4_renew_ops = {
3385 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003386 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003387};
3388
David Howellsadfa6f92006-08-22 20:06:08 -04003389int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390{
3391 struct rpc_message msg = {
3392 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3393 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003394 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003396 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003398 if (!atomic_inc_not_zero(&clp->cl_count))
3399 return -EIO;
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003400 data = kmalloc(sizeof(*data), GFP_KERNEL);
3401 if (data == NULL)
3402 return -ENOMEM;
3403 data->client = clp;
3404 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003406 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407}
3408
David Howellsadfa6f92006-08-22 20:06:08 -04003409int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410{
3411 struct rpc_message msg = {
3412 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3413 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003414 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 };
3416 unsigned long now = jiffies;
3417 int status;
3418
3419 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3420 if (status < 0)
3421 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003422 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 return 0;
3424}
3425
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003426static inline int nfs4_server_supports_acls(struct nfs_server *server)
3427{
3428 return (server->caps & NFS_CAP_ACLS)
3429 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3430 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3431}
3432
3433/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3434 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3435 * the stack.
3436 */
3437#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3438
3439static void buf_to_pages(const void *buf, size_t buflen,
3440 struct page **pages, unsigned int *pgbase)
3441{
3442 const void *p = buf;
3443
3444 *pgbase = offset_in_page(buf);
3445 p -= *pgbase;
3446 while (p < buf + buflen) {
3447 *(pages++) = virt_to_page(p);
3448 p += PAGE_CACHE_SIZE;
3449 }
3450}
3451
Neil Hormane9e3d722011-03-04 19:26:03 -05003452static int buf_to_pages_noslab(const void *buf, size_t buflen,
3453 struct page **pages, unsigned int *pgbase)
3454{
3455 struct page *newpage, **spages;
3456 int rc = 0;
3457 size_t len;
3458 spages = pages;
3459
3460 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003461 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003462 newpage = alloc_page(GFP_KERNEL);
3463
3464 if (newpage == NULL)
3465 goto unwind;
3466 memcpy(page_address(newpage), buf, len);
3467 buf += len;
3468 buflen -= len;
3469 *pages++ = newpage;
3470 rc++;
3471 } while (buflen != 0);
3472
3473 return rc;
3474
3475unwind:
3476 for(; rc > 0; rc--)
3477 __free_page(spages[rc-1]);
3478 return -ENOMEM;
3479}
3480
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003481struct nfs4_cached_acl {
3482 int cached;
3483 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003484 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003485};
3486
3487static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003488{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003489 struct nfs_inode *nfsi = NFS_I(inode);
3490
3491 spin_lock(&inode->i_lock);
3492 kfree(nfsi->nfs4_acl);
3493 nfsi->nfs4_acl = acl;
3494 spin_unlock(&inode->i_lock);
3495}
3496
3497static void nfs4_zap_acl_attr(struct inode *inode)
3498{
3499 nfs4_set_cached_acl(inode, NULL);
3500}
3501
3502static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3503{
3504 struct nfs_inode *nfsi = NFS_I(inode);
3505 struct nfs4_cached_acl *acl;
3506 int ret = -ENOENT;
3507
3508 spin_lock(&inode->i_lock);
3509 acl = nfsi->nfs4_acl;
3510 if (acl == NULL)
3511 goto out;
3512 if (buf == NULL) /* user is just asking for length */
3513 goto out_len;
3514 if (acl->cached == 0)
3515 goto out;
3516 ret = -ERANGE; /* see getxattr(2) man page */
3517 if (acl->len > buflen)
3518 goto out;
3519 memcpy(buf, acl->data, acl->len);
3520out_len:
3521 ret = acl->len;
3522out:
3523 spin_unlock(&inode->i_lock);
3524 return ret;
3525}
3526
3527static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3528{
3529 struct nfs4_cached_acl *acl;
3530
3531 if (buf && acl_len <= PAGE_SIZE) {
3532 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3533 if (acl == NULL)
3534 goto out;
3535 acl->cached = 1;
3536 memcpy(acl->data, buf, acl_len);
3537 } else {
3538 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3539 if (acl == NULL)
3540 goto out;
3541 acl->cached = 0;
3542 }
3543 acl->len = acl_len;
3544out:
3545 nfs4_set_cached_acl(inode, acl);
3546}
3547
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003548static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003549{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003550 struct page *pages[NFS4ACL_MAXPAGES];
3551 struct nfs_getaclargs args = {
3552 .fh = NFS_FH(inode),
3553 .acl_pages = pages,
3554 .acl_len = buflen,
3555 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003556 struct nfs_getaclres res = {
3557 .acl_len = buflen,
3558 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003559 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003560 struct rpc_message msg = {
3561 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3562 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003563 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003564 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003565 struct page *localpage = NULL;
3566 int ret;
3567
3568 if (buflen < PAGE_SIZE) {
3569 /* As long as we're doing a round trip to the server anyway,
3570 * let's be prepared for a page of acl data. */
3571 localpage = alloc_page(GFP_KERNEL);
3572 resp_buf = page_address(localpage);
3573 if (localpage == NULL)
3574 return -ENOMEM;
3575 args.acl_pages[0] = localpage;
3576 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003577 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003578 } else {
3579 resp_buf = buf;
3580 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3581 }
Bryan Schumaker7c513052011-03-24 17:12:24 +00003582 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 +00003583 if (ret)
3584 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003585 if (res.acl_len > args.acl_len)
3586 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003587 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003588 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003589 if (buf) {
3590 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003591 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003592 goto out_free;
3593 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003594 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003595 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003596 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003597out_free:
3598 if (localpage)
3599 __free_page(localpage);
3600 return ret;
3601}
3602
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003603static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3604{
3605 struct nfs4_exception exception = { };
3606 ssize_t ret;
3607 do {
3608 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3609 if (ret >= 0)
3610 break;
3611 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3612 } while (exception.retry);
3613 return ret;
3614}
3615
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003616static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3617{
3618 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003619 int ret;
3620
3621 if (!nfs4_server_supports_acls(server))
3622 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003623 ret = nfs_revalidate_inode(server, inode);
3624 if (ret < 0)
3625 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003626 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3627 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003628 ret = nfs4_read_cached_acl(inode, buf, buflen);
3629 if (ret != -ENOENT)
3630 return ret;
3631 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003632}
3633
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003634static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003635{
3636 struct nfs_server *server = NFS_SERVER(inode);
3637 struct page *pages[NFS4ACL_MAXPAGES];
3638 struct nfs_setaclargs arg = {
3639 .fh = NFS_FH(inode),
3640 .acl_pages = pages,
3641 .acl_len = buflen,
3642 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003643 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003644 struct rpc_message msg = {
3645 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3646 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003647 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003648 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003649 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003650
3651 if (!nfs4_server_supports_acls(server))
3652 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003653 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3654 if (i < 0)
3655 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003656 nfs_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003657 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003658
3659 /*
3660 * Free each page after tx, so the only ref left is
3661 * held by the network stack
3662 */
3663 for (; i > 0; i--)
3664 put_page(pages[i-1]);
3665
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003666 /*
3667 * Acl update can result in inode attribute update.
3668 * so mark the attribute cache invalid.
3669 */
3670 spin_lock(&inode->i_lock);
3671 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3672 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003673 nfs_access_zap_cache(inode);
3674 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003675 return ret;
3676}
3677
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003678static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3679{
3680 struct nfs4_exception exception = { };
3681 int err;
3682 do {
3683 err = nfs4_handle_exception(NFS_SERVER(inode),
3684 __nfs4_proc_set_acl(inode, buf, buflen),
3685 &exception);
3686 } while (exception.retry);
3687 return err;
3688}
3689
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003691nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003693 struct nfs_client *clp = server->nfs_client;
3694
3695 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 return 0;
3697 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003698 case -NFS4ERR_ADMIN_REVOKED:
3699 case -NFS4ERR_BAD_STATEID:
3700 case -NFS4ERR_OPENMODE:
3701 if (state == NULL)
3702 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003703 nfs4_schedule_stateid_recovery(server, state);
3704 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003705 case -NFS4ERR_EXPIRED:
3706 if (state != NULL)
3707 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003709 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003710 nfs4_schedule_lease_recovery(clp);
3711 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003712#if defined(CONFIG_NFS_V4_1)
3713 case -NFS4ERR_BADSESSION:
3714 case -NFS4ERR_BADSLOT:
3715 case -NFS4ERR_BAD_HIGH_SLOT:
3716 case -NFS4ERR_DEADSESSION:
3717 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3718 case -NFS4ERR_SEQ_FALSE_RETRY:
3719 case -NFS4ERR_SEQ_MISORDERED:
3720 dprintk("%s ERROR %d, Reset session\n", __func__,
3721 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003722 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -04003723 task->tk_status = 0;
3724 return -EAGAIN;
3725#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003727 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003728 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003729 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3731 task->tk_status = 0;
3732 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003733 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 case -NFS4ERR_OLD_STATEID:
3735 task->tk_status = 0;
3736 return -EAGAIN;
3737 }
3738 task->tk_status = nfs4_map_errors(task->tk_status);
3739 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003740wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003741 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003742 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3743 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3744 task->tk_status = 0;
3745 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746}
3747
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003748int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3749 unsigned short port, struct rpc_cred *cred,
3750 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751{
3752 nfs4_verifier sc_verifier;
3753 struct nfs4_setclientid setclientid = {
3754 .sc_verifier = &sc_verifier,
3755 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003756 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 };
3758 struct rpc_message msg = {
3759 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3760 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003761 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003762 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 };
Al Virobc4785c2006-10-19 23:28:51 -07003764 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 int loop = 0;
3766 int status;
3767
Al Virobc4785c2006-10-19 23:28:51 -07003768 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3770 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3771
3772 for(;;) {
3773 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003774 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003775 clp->cl_ipaddr,
3776 rpc_peeraddr2str(clp->cl_rpcclient,
3777 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003778 rpc_peeraddr2str(clp->cl_rpcclient,
3779 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003780 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 clp->cl_id_uniquifier);
3782 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003783 sizeof(setclientid.sc_netid),
3784 rpc_peeraddr2str(clp->cl_rpcclient,
3785 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003787 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 clp->cl_ipaddr, port >> 8, port & 255);
3789
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003790 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 if (status != -NFS4ERR_CLID_INUSE)
3792 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003793 if (loop != 0) {
3794 ++clp->cl_id_uniquifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003796 }
3797 ++loop;
3798 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 }
3800 return status;
3801}
3802
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003803int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003804 struct nfs4_setclientid_res *arg,
3805 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806{
3807 struct nfs_fsinfo fsinfo;
3808 struct rpc_message msg = {
3809 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003810 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003812 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 };
3814 unsigned long now;
3815 int status;
3816
3817 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003818 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 if (status == 0) {
3820 spin_lock(&clp->cl_lock);
3821 clp->cl_lease_time = fsinfo.lease_time * HZ;
3822 clp->cl_last_renewal = now;
3823 spin_unlock(&clp->cl_lock);
3824 }
3825 return status;
3826}
3827
Trond Myklebustfe650402006-01-03 09:55:18 +01003828struct nfs4_delegreturndata {
3829 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003830 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003831 struct nfs_fh fh;
3832 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003833 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003834 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003835 int rpc_status;
3836};
3837
Trond Myklebustfe650402006-01-03 09:55:18 +01003838static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3839{
3840 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003841
Trond Myklebust14516c32010-07-31 14:29:06 -04003842 if (!nfs4_sequence_done(task, &data->res.seq_res))
3843 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003844
Ricardo Labiaga79708862009-12-07 09:23:21 -05003845 switch (task->tk_status) {
3846 case -NFS4ERR_STALE_STATEID:
3847 case -NFS4ERR_EXPIRED:
3848 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003849 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003850 break;
3851 default:
3852 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3853 -EAGAIN) {
3854 nfs_restart_rpc(task, data->res.server->nfs_client);
3855 return;
3856 }
3857 }
3858 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003859}
3860
3861static void nfs4_delegreturn_release(void *calldata)
3862{
Trond Myklebustfe650402006-01-03 09:55:18 +01003863 kfree(calldata);
3864}
3865
Andy Adamson938e1012009-04-01 09:22:28 -04003866#if defined(CONFIG_NFS_V4_1)
3867static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3868{
3869 struct nfs4_delegreturndata *d_data;
3870
3871 d_data = (struct nfs4_delegreturndata *)data;
3872
Trond Myklebust035168a2010-06-16 09:52:26 -04003873 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003874 &d_data->args.seq_args,
3875 &d_data->res.seq_res, 1, task))
3876 return;
3877 rpc_call_start(task);
3878}
3879#endif /* CONFIG_NFS_V4_1 */
3880
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003881static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003882#if defined(CONFIG_NFS_V4_1)
3883 .rpc_call_prepare = nfs4_delegreturn_prepare,
3884#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003885 .rpc_call_done = nfs4_delegreturn_done,
3886 .rpc_release = nfs4_delegreturn_release,
3887};
3888
Trond Myklebuste6f81072008-01-24 18:14:34 -05003889static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003890{
3891 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003892 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003893 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003894 struct rpc_message msg = {
3895 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3896 .rpc_cred = cred,
3897 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003898 struct rpc_task_setup task_setup_data = {
3899 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003900 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003901 .callback_ops = &nfs4_delegreturn_ops,
3902 .flags = RPC_TASK_ASYNC,
3903 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003904 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003905
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003906 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003907 if (data == NULL)
3908 return -ENOMEM;
3909 data->args.fhandle = &data->fh;
3910 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003911 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003912 nfs_copy_fh(&data->fh, NFS_FH(inode));
3913 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003914 data->res.fattr = &data->fattr;
3915 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003916 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003917 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003918 data->rpc_status = 0;
3919
Trond Myklebustc970aa82007-07-14 15:39:59 -04003920 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05003921 msg.rpc_argp = &data->args;
3922 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003923 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003924 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003925 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003926 if (!issync)
3927 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003928 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003929 if (status != 0)
3930 goto out;
3931 status = data->rpc_status;
3932 if (status != 0)
3933 goto out;
3934 nfs_refresh_inode(inode, &data->fattr);
3935out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003936 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003937 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938}
3939
Trond Myklebuste6f81072008-01-24 18:14:34 -05003940int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941{
3942 struct nfs_server *server = NFS_SERVER(inode);
3943 struct nfs4_exception exception = { };
3944 int err;
3945 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003946 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 switch (err) {
3948 case -NFS4ERR_STALE_STATEID:
3949 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 case 0:
3951 return 0;
3952 }
3953 err = nfs4_handle_exception(server, err, &exception);
3954 } while (exception.retry);
3955 return err;
3956}
3957
3958#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3959#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3960
3961/*
3962 * sleep, with exponential backoff, and retry the LOCK operation.
3963 */
3964static unsigned long
3965nfs4_set_lock_task_retry(unsigned long timeout)
3966{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003967 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 timeout <<= 1;
3969 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3970 return NFS4_LOCK_MAXTIMEOUT;
3971 return timeout;
3972}
3973
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3975{
3976 struct inode *inode = state->inode;
3977 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003978 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003979 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003981 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003983 struct nfs_lockt_res res = {
3984 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 };
3986 struct rpc_message msg = {
3987 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3988 .rpc_argp = &arg,
3989 .rpc_resp = &res,
3990 .rpc_cred = state->owner->so_cred,
3991 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 struct nfs4_lock_state *lsp;
3993 int status;
3994
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003995 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003996 status = nfs4_set_lock_state(state, request);
3997 if (status != 0)
3998 goto out;
3999 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004000 arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004001 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004002 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004003 switch (status) {
4004 case 0:
4005 request->fl_type = F_UNLCK;
4006 break;
4007 case -NFS4ERR_DENIED:
4008 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004010 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004011out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 return status;
4013}
4014
4015static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4016{
4017 struct nfs4_exception exception = { };
4018 int err;
4019
4020 do {
4021 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4022 _nfs4_proc_getlk(state, cmd, request),
4023 &exception);
4024 } while (exception.retry);
4025 return err;
4026}
4027
4028static int do_vfs_lock(struct file *file, struct file_lock *fl)
4029{
4030 int res = 0;
4031 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4032 case FL_POSIX:
4033 res = posix_lock_file_wait(file, fl);
4034 break;
4035 case FL_FLOCK:
4036 res = flock_lock_file_wait(file, fl);
4037 break;
4038 default:
4039 BUG();
4040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 return res;
4042}
4043
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004044struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004045 struct nfs_locku_args arg;
4046 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004047 struct nfs4_lock_state *lsp;
4048 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004049 struct file_lock fl;
4050 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004051 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004052};
4053
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004054static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4055 struct nfs_open_context *ctx,
4056 struct nfs4_lock_state *lsp,
4057 struct nfs_seqid *seqid)
4058{
4059 struct nfs4_unlockdata *p;
4060 struct inode *inode = lsp->ls_state->inode;
4061
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004062 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004063 if (p == NULL)
4064 return NULL;
4065 p->arg.fh = NFS_FH(inode);
4066 p->arg.fl = &p->fl;
4067 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004068 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004069 p->arg.stateid = &lsp->ls_stateid;
4070 p->lsp = lsp;
4071 atomic_inc(&lsp->ls_count);
4072 /* Ensure we don't close file until we're done freeing locks! */
4073 p->ctx = get_nfs_open_context(ctx);
4074 memcpy(&p->fl, fl, sizeof(p->fl));
4075 p->server = NFS_SERVER(inode);
4076 return p;
4077}
4078
Trond Myklebust06f814a2006-01-03 09:55:07 +01004079static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004080{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004081 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004082 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004083 nfs4_put_lock_state(calldata->lsp);
4084 put_nfs_open_context(calldata->ctx);
4085 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004086}
4087
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004088static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004089{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004090 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004091
Trond Myklebust14516c32010-07-31 14:29:06 -04004092 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4093 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004094 switch (task->tk_status) {
4095 case 0:
4096 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004097 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004098 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01004099 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004100 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004101 case -NFS4ERR_BAD_STATEID:
4102 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004103 case -NFS4ERR_STALE_STATEID:
4104 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004105 break;
4106 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004107 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebust0110ee12009-12-07 09:00:24 -05004108 nfs_restart_rpc(task,
Trond Myklebustd61e6122009-12-05 19:32:19 -05004109 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004110 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004111}
4112
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004113static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004114{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004115 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004117 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004118 return;
4119 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004120 /* Note: exit _without_ running nfs4_locku_done */
4121 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004122 return;
4123 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004124 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004125 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004126 &calldata->arg.seq_args,
4127 &calldata->res.seq_res, 1, task))
4128 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004129 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130}
4131
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004132static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004133 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004134 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004135 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004136};
4137
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004138static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4139 struct nfs_open_context *ctx,
4140 struct nfs4_lock_state *lsp,
4141 struct nfs_seqid *seqid)
4142{
4143 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004144 struct rpc_message msg = {
4145 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4146 .rpc_cred = ctx->cred,
4147 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004148 struct rpc_task_setup task_setup_data = {
4149 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004150 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004151 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004152 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004153 .flags = RPC_TASK_ASYNC,
4154 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004155
Frank Filz137d6ac2007-07-09 15:32:29 -07004156 /* Ensure this is an unlock - when canceling a lock, the
4157 * canceled lock is passed in, and it won't be an unlock.
4158 */
4159 fl->fl_type = F_UNLCK;
4160
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004161 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4162 if (data == NULL) {
4163 nfs_free_seqid(seqid);
4164 return ERR_PTR(-ENOMEM);
4165 }
4166
Trond Myklebust1174dd12010-12-21 10:52:24 -05004167 msg.rpc_argp = &data->arg;
4168 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004169 task_setup_data.callback_data = data;
4170 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004171}
4172
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4174{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004175 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004176 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004177 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004178 struct rpc_task *task;
4179 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004180 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181
Trond Myklebust9b073572006-06-29 16:38:34 -04004182 status = nfs4_set_lock_state(state, request);
4183 /* Unlock _before_ we do the RPC call */
4184 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004185 down_read(&nfsi->rwsem);
4186 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4187 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004188 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004189 }
4190 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004191 if (status != 0)
4192 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004193 /* Is this a delegated lock? */
4194 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004195 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004196 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004197 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004198 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004199 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004200 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004201 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004202 status = PTR_ERR(task);
4203 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004204 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004205 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004206 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004207out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004208 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004209 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210}
4211
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004212struct nfs4_lockdata {
4213 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004214 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004215 struct nfs4_lock_state *lsp;
4216 struct nfs_open_context *ctx;
4217 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004218 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004219 int rpc_status;
4220 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004221 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004222};
4223
4224static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004225 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4226 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004227{
4228 struct nfs4_lockdata *p;
4229 struct inode *inode = lsp->ls_state->inode;
4230 struct nfs_server *server = NFS_SERVER(inode);
4231
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004232 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004233 if (p == NULL)
4234 return NULL;
4235
4236 p->arg.fh = NFS_FH(inode);
4237 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004238 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004239 if (p->arg.open_seqid == NULL)
4240 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004241 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004242 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004243 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004244 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004245 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004246 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004247 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004248 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004249 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004250 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004251 atomic_inc(&lsp->ls_count);
4252 p->ctx = get_nfs_open_context(ctx);
4253 memcpy(&p->fl, fl, sizeof(p->fl));
4254 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004255out_free_seqid:
4256 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004257out_free:
4258 kfree(p);
4259 return NULL;
4260}
4261
4262static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4263{
4264 struct nfs4_lockdata *data = calldata;
4265 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266
Harvey Harrison3110ff82008-05-02 13:42:44 -07004267 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004268 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4269 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004270 /* Do we need to do an open_to_lock_owner? */
4271 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004272 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4273 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004274 data->arg.open_stateid = &state->stateid;
4275 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004276 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004277 } else
4278 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004279 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004280 if (nfs4_setup_sequence(data->server,
4281 &data->arg.seq_args,
Andy Adamson66179ef2009-04-01 09:22:22 -04004282 &data->res.seq_res, 1, task))
4283 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004284 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004285 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004286}
4287
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004288static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4289{
4290 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4291 nfs4_lock_prepare(task, calldata);
4292}
4293
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004294static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4295{
4296 struct nfs4_lockdata *data = calldata;
4297
Harvey Harrison3110ff82008-05-02 13:42:44 -07004298 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004299
Trond Myklebust14516c32010-07-31 14:29:06 -04004300 if (!nfs4_sequence_done(task, &data->res.seq_res))
4301 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004302
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004303 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004304 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004305 if (data->rpc_status == 0)
4306 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4307 else
4308 goto out;
4309 }
4310 if (data->rpc_status == 0) {
4311 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4312 sizeof(data->lsp->ls_stateid.data));
4313 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Al Viro3d4ff432011-06-22 18:40:12 -04004314 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004315 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004316out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004317 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004318}
4319
4320static void nfs4_lock_release(void *calldata)
4321{
4322 struct nfs4_lockdata *data = calldata;
4323
Harvey Harrison3110ff82008-05-02 13:42:44 -07004324 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004325 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004326 if (data->cancelled != 0) {
4327 struct rpc_task *task;
4328 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4329 data->arg.lock_seqid);
4330 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004331 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004332 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004333 } else
4334 nfs_free_seqid(data->arg.lock_seqid);
4335 nfs4_put_lock_state(data->lsp);
4336 put_nfs_open_context(data->ctx);
4337 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004338 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004339}
4340
4341static const struct rpc_call_ops nfs4_lock_ops = {
4342 .rpc_call_prepare = nfs4_lock_prepare,
4343 .rpc_call_done = nfs4_lock_done,
4344 .rpc_release = nfs4_lock_release,
4345};
4346
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004347static const struct rpc_call_ops nfs4_recover_lock_ops = {
4348 .rpc_call_prepare = nfs4_recover_lock_prepare,
4349 .rpc_call_done = nfs4_lock_done,
4350 .rpc_release = nfs4_lock_release,
4351};
4352
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004353static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4354{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004355 switch (error) {
4356 case -NFS4ERR_ADMIN_REVOKED:
4357 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004358 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004359 if (new_lock_owner != 0 ||
4360 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004361 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004362 break;
4363 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004364 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004365 case -NFS4ERR_EXPIRED:
4366 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004367 };
4368}
4369
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004370static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004371{
4372 struct nfs4_lockdata *data;
4373 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004374 struct rpc_message msg = {
4375 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4376 .rpc_cred = state->owner->so_cred,
4377 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004378 struct rpc_task_setup task_setup_data = {
4379 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004380 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004381 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004382 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004383 .flags = RPC_TASK_ASYNC,
4384 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004385 int ret;
4386
Harvey Harrison3110ff82008-05-02 13:42:44 -07004387 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004388 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004389 fl->fl_u.nfs4_fl.owner,
4390 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004391 if (data == NULL)
4392 return -ENOMEM;
4393 if (IS_SETLKW(cmd))
4394 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004395 if (recovery_type > NFS_LOCK_NEW) {
4396 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004397 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004398 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4399 }
Trond Myklebust1174dd12010-12-21 10:52:24 -05004400 msg.rpc_argp = &data->arg;
4401 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004402 task_setup_data.callback_data = data;
4403 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004404 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004405 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004406 ret = nfs4_wait_for_completion_rpc_task(task);
4407 if (ret == 0) {
4408 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004409 if (ret)
4410 nfs4_handle_setlk_error(data->server, data->lsp,
4411 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004412 } else
4413 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004414 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004415 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004416 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417}
4418
4419static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4420{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004421 struct nfs_server *server = NFS_SERVER(state->inode);
4422 struct nfs4_exception exception = { };
4423 int err;
4424
4425 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004426 /* Cache the lock if possible... */
4427 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4428 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004429 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004430 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004431 break;
4432 nfs4_handle_exception(server, err, &exception);
4433 } while (exception.retry);
4434 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435}
4436
4437static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4438{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004439 struct nfs_server *server = NFS_SERVER(state->inode);
4440 struct nfs4_exception exception = { };
4441 int err;
4442
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004443 err = nfs4_set_lock_state(state, request);
4444 if (err != 0)
4445 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004446 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004447 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4448 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004449 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004450 switch (err) {
4451 default:
4452 goto out;
4453 case -NFS4ERR_GRACE:
4454 case -NFS4ERR_DELAY:
4455 nfs4_handle_exception(server, err, &exception);
4456 err = 0;
4457 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004458 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004459out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004460 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461}
4462
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004463#if defined(CONFIG_NFS_V4_1)
4464static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4465{
4466 int status;
4467 struct nfs_server *server = NFS_SERVER(state->inode);
4468
4469 status = nfs41_test_stateid(server, state);
4470 if (status == NFS_OK)
4471 return 0;
4472 nfs41_free_stateid(server, state);
4473 return nfs4_lock_expired(state, request);
4474}
4475#endif
4476
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4478{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004479 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004480 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004481 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004483 if ((fl_flags & FL_POSIX) &&
4484 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4485 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004486 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004487 status = nfs4_set_lock_state(state, request);
4488 if (status != 0)
4489 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004490 request->fl_flags |= FL_ACCESS;
4491 status = do_vfs_lock(request->fl_file, request);
4492 if (status < 0)
4493 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004494 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004495 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004496 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004497 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004498 request->fl_flags = fl_flags & ~FL_SLEEP;
4499 status = do_vfs_lock(request->fl_file, request);
4500 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004501 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004502 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004503 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004504 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004505 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004506 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004507 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004508 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004509out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004510 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004511out:
4512 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 return status;
4514}
4515
4516static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4517{
4518 struct nfs4_exception exception = { };
4519 int err;
4520
4521 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004522 err = _nfs4_proc_setlk(state, cmd, request);
4523 if (err == -NFS4ERR_DENIED)
4524 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004526 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 } while (exception.retry);
4528 return err;
4529}
4530
4531static int
4532nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4533{
4534 struct nfs_open_context *ctx;
4535 struct nfs4_state *state;
4536 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4537 int status;
4538
4539 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004540 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 state = ctx->state;
4542
4543 if (request->fl_start < 0 || request->fl_end < 0)
4544 return -EINVAL;
4545
Trond Myklebustd9531262009-07-21 19:22:38 -04004546 if (IS_GETLK(cmd)) {
4547 if (state != NULL)
4548 return nfs4_proc_getlk(state, F_GETLK, request);
4549 return 0;
4550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551
4552 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4553 return -EINVAL;
4554
Trond Myklebustd9531262009-07-21 19:22:38 -04004555 if (request->fl_type == F_UNLCK) {
4556 if (state != NULL)
4557 return nfs4_proc_unlck(state, cmd, request);
4558 return 0;
4559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560
Trond Myklebustd9531262009-07-21 19:22:38 -04004561 if (state == NULL)
4562 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 do {
4564 status = nfs4_proc_setlk(state, cmd, request);
4565 if ((status != -EAGAIN) || IS_SETLK(cmd))
4566 break;
4567 timeout = nfs4_set_lock_task_retry(timeout);
4568 status = -ERESTARTSYS;
4569 if (signalled())
4570 break;
4571 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 return status;
4573}
4574
Trond Myklebust888e6942005-11-04 15:38:11 -05004575int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4576{
4577 struct nfs_server *server = NFS_SERVER(state->inode);
4578 struct nfs4_exception exception = { };
4579 int err;
4580
4581 err = nfs4_set_lock_state(state, fl);
4582 if (err != 0)
4583 goto out;
4584 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004585 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004586 switch (err) {
4587 default:
4588 printk(KERN_ERR "%s: unhandled error %d.\n",
4589 __func__, err);
4590 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004591 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004592 goto out;
4593 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004594 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004595 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004596 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004597 nfs4_schedule_lease_recovery(server->nfs_client);
4598 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004599 case -NFS4ERR_BADSESSION:
4600 case -NFS4ERR_BADSLOT:
4601 case -NFS4ERR_BAD_HIGH_SLOT:
4602 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4603 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004604 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004605 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004606 case -ERESTARTSYS:
4607 /*
4608 * The show must go on: exit, but mark the
4609 * stateid as needing recovery.
4610 */
4611 case -NFS4ERR_ADMIN_REVOKED:
4612 case -NFS4ERR_BAD_STATEID:
4613 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004614 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004615 err = 0;
4616 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004617 case -EKEYEXPIRED:
4618 /*
4619 * User RPCSEC_GSS context has expired.
4620 * We cannot recover this stateid now, so
4621 * skip it and allow recovery thread to
4622 * proceed.
4623 */
4624 err = 0;
4625 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004626 case -ENOMEM:
4627 case -NFS4ERR_DENIED:
4628 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4629 err = 0;
4630 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004631 case -NFS4ERR_DELAY:
4632 break;
4633 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004634 err = nfs4_handle_exception(server, err, &exception);
4635 } while (exception.retry);
4636out:
4637 return err;
4638}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004639
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004640static void nfs4_release_lockowner_release(void *calldata)
4641{
4642 kfree(calldata);
4643}
4644
4645const struct rpc_call_ops nfs4_release_lockowner_ops = {
4646 .rpc_release = nfs4_release_lockowner_release,
4647};
4648
4649void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4650{
4651 struct nfs_server *server = lsp->ls_state->owner->so_server;
4652 struct nfs_release_lockowner_args *args;
4653 struct rpc_message msg = {
4654 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4655 };
4656
4657 if (server->nfs_client->cl_mvops->minor_version != 0)
4658 return;
4659 args = kmalloc(sizeof(*args), GFP_NOFS);
4660 if (!args)
4661 return;
4662 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4663 args->lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004664 args->lock_owner.s_dev = server->s_dev;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004665 msg.rpc_argp = args;
4666 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4667}
4668
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004669#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4670
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004671static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4672 const void *buf, size_t buflen,
4673 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004674{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004675 if (strcmp(key, "") != 0)
4676 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004677
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004678 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004679}
4680
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004681static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4682 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004683{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004684 if (strcmp(key, "") != 0)
4685 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004686
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004687 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004688}
4689
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004690static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4691 size_t list_len, const char *name,
4692 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004693{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004694 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004695
J. Bruce Fields096455a2006-03-20 23:23:42 -05004696 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4697 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004698
4699 if (list && len <= list_len)
4700 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004701 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004702}
4703
Andy Adamson533eb462011-06-13 18:25:56 -04004704/*
4705 * nfs_fhget will use either the mounted_on_fileid or the fileid
4706 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004707static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4708{
Andy Adamson533eb462011-06-13 18:25:56 -04004709 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4710 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4711 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4712 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004713 return;
4714
4715 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4716 NFS_ATTR_FATTR_NLINK;
4717 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4718 fattr->nlink = 2;
4719}
4720
Trond Myklebust56659e92007-07-17 21:52:39 -04004721int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004722 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004723{
4724 struct nfs_server *server = NFS_SERVER(dir);
4725 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004726 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004727 };
4728 struct nfs4_fs_locations_arg args = {
4729 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004730 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004731 .page = page,
4732 .bitmask = bitmask,
4733 };
Benny Halevy22958462009-04-01 09:22:02 -04004734 struct nfs4_fs_locations_res res = {
4735 .fs_locations = fs_locations,
4736 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004737 struct rpc_message msg = {
4738 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4739 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004740 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004741 };
4742 int status;
4743
Harvey Harrison3110ff82008-05-02 13:42:44 -07004744 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04004745
4746 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
4747 * is not supported */
4748 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
4749 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
4750 else
4751 bitmask[0] |= FATTR4_WORD0_FILEID;
4752
Trond Myklebustc228fd32007-01-13 02:28:11 -05004753 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004754 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004755 fs_locations->nlocations = 0;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004756 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004757 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004758 return status;
4759}
4760
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00004761static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4762{
4763 int status;
4764 struct nfs4_secinfo_arg args = {
4765 .dir_fh = NFS_FH(dir),
4766 .name = name,
4767 };
4768 struct nfs4_secinfo_res res = {
4769 .flavors = flavors,
4770 };
4771 struct rpc_message msg = {
4772 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
4773 .rpc_argp = &args,
4774 .rpc_resp = &res,
4775 };
4776
4777 dprintk("NFS call secinfo %s\n", name->name);
4778 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4779 dprintk("NFS reply secinfo: %d\n", status);
4780 return status;
4781}
4782
4783int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4784{
4785 struct nfs4_exception exception = { };
4786 int err;
4787 do {
4788 err = nfs4_handle_exception(NFS_SERVER(dir),
4789 _nfs4_proc_secinfo(dir, name, flavors),
4790 &exception);
4791 } while (exception.retry);
4792 return err;
4793}
4794
Andy Adamson557134a2009-04-01 09:21:53 -04004795#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004796/*
Andy Adamson357f54d2010-12-14 10:11:57 -05004797 * Check the exchange flags returned by the server for invalid flags, having
4798 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4799 * DS flags set.
4800 */
4801static int nfs4_check_cl_exchange_flags(u32 flags)
4802{
4803 if (flags & ~EXCHGID4_FLAG_MASK_R)
4804 goto out_inval;
4805 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4806 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4807 goto out_inval;
4808 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4809 goto out_inval;
4810 return NFS_OK;
4811out_inval:
4812 return -NFS4ERR_INVAL;
4813}
4814
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004815static bool
4816nfs41_same_server_scope(struct server_scope *a, struct server_scope *b)
4817{
4818 if (a->server_scope_sz == b->server_scope_sz &&
4819 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
4820 return true;
4821
4822 return false;
4823}
4824
Andy Adamson357f54d2010-12-14 10:11:57 -05004825/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04004826 * nfs4_proc_exchange_id()
4827 *
4828 * Since the clientid has expired, all compounds using sessions
4829 * associated with the stale clientid will be returning
4830 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4831 * be in some phase of session reset.
4832 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004833int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004834{
4835 nfs4_verifier verifier;
4836 struct nfs41_exchange_id_args args = {
4837 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05004838 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04004839 };
4840 struct nfs41_exchange_id_res res = {
4841 .client = clp,
4842 };
4843 int status;
4844 struct rpc_message msg = {
4845 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4846 .rpc_argp = &args,
4847 .rpc_resp = &res,
4848 .rpc_cred = cred,
4849 };
4850 __be32 *p;
4851
4852 dprintk("--> %s\n", __func__);
4853 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004854
Benny Halevy99fe60d2009-04-01 09:22:29 -04004855 p = (u32 *)verifier.data;
4856 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4857 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4858 args.verifier = &verifier;
4859
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004860 args.id_len = scnprintf(args.id, sizeof(args.id),
4861 "%s/%s.%s/%u",
4862 clp->cl_ipaddr,
4863 init_utsname()->nodename,
4864 init_utsname()->domainname,
4865 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004866
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004867 res.server_scope = kzalloc(sizeof(struct server_scope), GFP_KERNEL);
4868 if (unlikely(!res.server_scope))
4869 return -ENOMEM;
4870
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004871 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004872 if (!status)
4873 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004874
4875 if (!status) {
4876 if (clp->server_scope &&
4877 !nfs41_same_server_scope(clp->server_scope,
4878 res.server_scope)) {
4879 dprintk("%s: server_scope mismatch detected\n",
4880 __func__);
4881 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
4882 kfree(clp->server_scope);
4883 clp->server_scope = NULL;
4884 }
4885
4886 if (!clp->server_scope)
4887 clp->server_scope = res.server_scope;
4888 else
4889 kfree(res.server_scope);
4890 }
4891
Benny Halevy99fe60d2009-04-01 09:22:29 -04004892 dprintk("<-- %s status= %d\n", __func__, status);
4893 return status;
4894}
4895
Andy Adamson2050f0c2009-04-01 09:22:30 -04004896struct nfs4_get_lease_time_data {
4897 struct nfs4_get_lease_time_args *args;
4898 struct nfs4_get_lease_time_res *res;
4899 struct nfs_client *clp;
4900};
4901
4902static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4903 void *calldata)
4904{
4905 int ret;
4906 struct nfs4_get_lease_time_data *data =
4907 (struct nfs4_get_lease_time_data *)calldata;
4908
4909 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004910 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004911 /* just setup sequence, do not trigger session recovery
4912 since we're invoked within one */
4913 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004914 &data->args->la_seq_args,
4915 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004916
4917 BUG_ON(ret == -EAGAIN);
4918 rpc_call_start(task);
4919 dprintk("<-- %s\n", __func__);
4920}
4921
4922/*
4923 * Called from nfs4_state_manager thread for session setup, so don't recover
4924 * from sequence operation or clientid errors.
4925 */
4926static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4927{
4928 struct nfs4_get_lease_time_data *data =
4929 (struct nfs4_get_lease_time_data *)calldata;
4930
4931 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04004932 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4933 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04004934 switch (task->tk_status) {
4935 case -NFS4ERR_DELAY:
4936 case -NFS4ERR_GRACE:
4937 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4938 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4939 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004940 /* fall through */
4941 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust0110ee12009-12-07 09:00:24 -05004942 nfs_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004943 return;
4944 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004945 dprintk("<-- %s\n", __func__);
4946}
4947
4948struct rpc_call_ops nfs4_get_lease_time_ops = {
4949 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4950 .rpc_call_done = nfs4_get_lease_time_done,
4951};
4952
4953int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4954{
4955 struct rpc_task *task;
4956 struct nfs4_get_lease_time_args args;
4957 struct nfs4_get_lease_time_res res = {
4958 .lr_fsinfo = fsinfo,
4959 };
4960 struct nfs4_get_lease_time_data data = {
4961 .args = &args,
4962 .res = &res,
4963 .clp = clp,
4964 };
4965 struct rpc_message msg = {
4966 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4967 .rpc_argp = &args,
4968 .rpc_resp = &res,
4969 };
4970 struct rpc_task_setup task_setup = {
4971 .rpc_client = clp->cl_rpcclient,
4972 .rpc_message = &msg,
4973 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004974 .callback_data = &data,
4975 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04004976 };
4977 int status;
4978
Andy Adamson2050f0c2009-04-01 09:22:30 -04004979 dprintk("--> %s\n", __func__);
4980 task = rpc_run_task(&task_setup);
4981
4982 if (IS_ERR(task))
4983 status = PTR_ERR(task);
4984 else {
4985 status = task->tk_status;
4986 rpc_put_task(task);
4987 }
4988 dprintk("<-- %s return %d\n", __func__, status);
4989
4990 return status;
4991}
4992
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004993/*
4994 * Reset a slot table
4995 */
Andy Adamson104aeba2010-01-14 17:45:10 -05004996static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4997 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004998{
Andy Adamson104aeba2010-01-14 17:45:10 -05004999 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005000 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005001 int ret = 0;
5002
Andy Adamson104aeba2010-01-14 17:45:10 -05005003 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5004 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005005
Andy Adamson104aeba2010-01-14 17:45:10 -05005006 /* Does the newly negotiated max_reqs match the existing slot table? */
5007 if (max_reqs != tbl->max_slots) {
5008 ret = -ENOMEM;
5009 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005010 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005011 if (!new)
5012 goto out;
5013 ret = 0;
5014 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005015 }
5016 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05005017 if (new) {
5018 tbl->slots = new;
5019 tbl->max_slots = max_reqs;
5020 }
5021 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005022 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005023 spin_unlock(&tbl->slot_tbl_lock);
5024 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5025 tbl, tbl->slots, tbl->max_slots);
5026out:
5027 dprintk("<-- %s: return %d\n", __func__, ret);
5028 return ret;
5029}
5030
Andy Adamsonfc931582009-04-01 09:22:31 -04005031/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005032 * Reset the forechannel and backchannel slot tables
5033 */
5034static int nfs4_reset_slot_tables(struct nfs4_session *session)
5035{
5036 int status;
5037
5038 status = nfs4_reset_slot_table(&session->fc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05005039 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005040 if (status)
5041 return status;
5042
5043 status = nfs4_reset_slot_table(&session->bc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05005044 session->bc_attrs.max_reqs, 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005045 return status;
5046}
5047
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005048/* Destroy the slot table */
5049static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5050{
5051 if (session->fc_slot_table.slots != NULL) {
5052 kfree(session->fc_slot_table.slots);
5053 session->fc_slot_table.slots = NULL;
5054 }
5055 if (session->bc_slot_table.slots != NULL) {
5056 kfree(session->bc_slot_table.slots);
5057 session->bc_slot_table.slots = NULL;
5058 }
5059 return;
5060}
5061
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005062/*
Andy Adamsonfc931582009-04-01 09:22:31 -04005063 * Initialize slot table
5064 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005065static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
5066 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04005067{
Andy Adamsonfc931582009-04-01 09:22:31 -04005068 struct nfs4_slot *slot;
5069 int ret = -ENOMEM;
5070
5071 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
5072
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005073 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04005074
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005075 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04005076 if (!slot)
5077 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04005078 ret = 0;
5079
5080 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04005081 tbl->max_slots = max_slots;
5082 tbl->slots = slot;
5083 tbl->highest_used_slotid = -1; /* no slot is currently used */
5084 spin_unlock(&tbl->slot_tbl_lock);
5085 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5086 tbl, tbl->slots, tbl->max_slots);
5087out:
5088 dprintk("<-- %s: return %d\n", __func__, ret);
5089 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04005090}
5091
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005092/*
5093 * Initialize the forechannel and backchannel tables
5094 */
5095static int nfs4_init_slot_tables(struct nfs4_session *session)
5096{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005097 struct nfs4_slot_table *tbl;
5098 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005099
Trond Myklebustf26468f2009-12-05 19:32:11 -05005100 tbl = &session->fc_slot_table;
5101 if (tbl->slots == NULL) {
5102 status = nfs4_init_slot_table(tbl,
5103 session->fc_attrs.max_reqs, 1);
5104 if (status)
5105 return status;
5106 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005107
Trond Myklebustf26468f2009-12-05 19:32:11 -05005108 tbl = &session->bc_slot_table;
5109 if (tbl->slots == NULL) {
5110 status = nfs4_init_slot_table(tbl,
5111 session->bc_attrs.max_reqs, 0);
5112 if (status)
5113 nfs4_destroy_slot_tables(session);
5114 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005115
5116 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005117}
5118
5119struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5120{
5121 struct nfs4_session *session;
5122 struct nfs4_slot_table *tbl;
5123
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005124 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005125 if (!session)
5126 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005127
Andy Adamson557134a2009-04-01 09:21:53 -04005128 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05005129 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04005130 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005131 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005132 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005133
5134 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05005135 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005136 spin_lock_init(&tbl->slot_tbl_lock);
5137 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005138 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005139
Trond Myklebust1055d762010-06-16 09:52:27 -04005140 session->session_state = 1<<NFS4_SESSION_INITING;
5141
Andy Adamson557134a2009-04-01 09:21:53 -04005142 session->clp = clp;
5143 return session;
5144}
5145
5146void nfs4_destroy_session(struct nfs4_session *session)
5147{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005148 nfs4_proc_destroy_session(session);
5149 dprintk("%s Destroy backchannel for xprt %p\n",
5150 __func__, session->clp->cl_rpcclient->cl_xprt);
5151 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
5152 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005153 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005154 kfree(session);
5155}
5156
Andy Adamsonfc931582009-04-01 09:22:31 -04005157/*
5158 * Initialize the values to be used by the client in CREATE_SESSION
5159 * If nfs4_init_session set the fore channel request and response sizes,
5160 * use them.
5161 *
5162 * Set the back channel max_resp_sz_cached to zero to force the client to
5163 * always set csa_cachethis to FALSE because the current implementation
5164 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5165 */
5166static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5167{
5168 struct nfs4_session *session = args->client->cl_session;
5169 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5170 mxresp_sz = session->fc_attrs.max_resp_sz;
5171
5172 if (mxrqst_sz == 0)
5173 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5174 if (mxresp_sz == 0)
5175 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5176 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005177 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5178 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005179 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5180 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
5181
5182 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005183 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005184 __func__,
5185 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005186 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005187
5188 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005189 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5190 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5191 args->bc_attrs.max_resp_sz_cached = 0;
5192 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5193 args->bc_attrs.max_reqs = 1;
5194
5195 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5196 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5197 __func__,
5198 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5199 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5200 args->bc_attrs.max_reqs);
5201}
5202
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005203static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005204{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005205 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5206 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5207
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005208 if (rcvd->max_resp_sz > sent->max_resp_sz)
5209 return -EINVAL;
5210 /*
5211 * Our requested max_ops is the minimum we need; we're not
5212 * prepared to break up compounds into smaller pieces than that.
5213 * So, no point even trying to continue if the server won't
5214 * cooperate:
5215 */
5216 if (rcvd->max_ops < sent->max_ops)
5217 return -EINVAL;
5218 if (rcvd->max_reqs == 0)
5219 return -EINVAL;
5220 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005221}
5222
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005223static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5224{
5225 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5226 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005227
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005228 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5229 return -EINVAL;
5230 if (rcvd->max_resp_sz < sent->max_resp_sz)
5231 return -EINVAL;
5232 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5233 return -EINVAL;
5234 /* These would render the backchannel useless: */
5235 if (rcvd->max_ops == 0)
5236 return -EINVAL;
5237 if (rcvd->max_reqs == 0)
5238 return -EINVAL;
5239 return 0;
5240}
Andy Adamson8d353012009-04-01 09:22:32 -04005241
Andy Adamson8d353012009-04-01 09:22:32 -04005242static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5243 struct nfs4_session *session)
5244{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005245 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005246
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005247 ret = nfs4_verify_fore_channel_attrs(args, session);
5248 if (ret)
5249 return ret;
5250 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005251}
5252
Andy Adamsonfc931582009-04-01 09:22:31 -04005253static int _nfs4_proc_create_session(struct nfs_client *clp)
5254{
5255 struct nfs4_session *session = clp->cl_session;
5256 struct nfs41_create_session_args args = {
5257 .client = clp,
5258 .cb_program = NFS4_CALLBACK,
5259 };
5260 struct nfs41_create_session_res res = {
5261 .client = clp,
5262 };
5263 struct rpc_message msg = {
5264 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5265 .rpc_argp = &args,
5266 .rpc_resp = &res,
5267 };
5268 int status;
5269
5270 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005271 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005272
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005273 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005274
Andy Adamson8d353012009-04-01 09:22:32 -04005275 if (!status)
5276 /* Verify the session's negotiated channel_attrs values */
5277 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005278 if (!status) {
5279 /* Increment the clientid slot sequence id */
5280 clp->cl_seqid++;
5281 }
5282
5283 return status;
5284}
5285
5286/*
5287 * Issues a CREATE_SESSION operation to the server.
5288 * It is the responsibility of the caller to verify the session is
5289 * expired before calling this routine.
5290 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005291int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005292{
5293 int status;
5294 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005295 struct nfs4_session *session = clp->cl_session;
5296
5297 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5298
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005299 status = _nfs4_proc_create_session(clp);
Andy Adamsonfc931582009-04-01 09:22:31 -04005300 if (status)
5301 goto out;
5302
Trond Myklebustf26468f2009-12-05 19:32:11 -05005303 /* Init and reset the fore channel */
5304 status = nfs4_init_slot_tables(session);
5305 dprintk("slot table initialization returned %d\n", status);
5306 if (status)
5307 goto out;
5308 status = nfs4_reset_slot_tables(session);
5309 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005310 if (status)
5311 goto out;
5312
5313 ptr = (unsigned *)&session->sess_id.data[0];
5314 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5315 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005316out:
5317 dprintk("<-- %s\n", __func__);
5318 return status;
5319}
5320
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005321/*
5322 * Issue the over-the-wire RPC DESTROY_SESSION.
5323 * The caller must serialize access to this routine.
5324 */
5325int nfs4_proc_destroy_session(struct nfs4_session *session)
5326{
5327 int status = 0;
5328 struct rpc_message msg;
5329
5330 dprintk("--> nfs4_proc_destroy_session\n");
5331
5332 /* session is still being setup */
5333 if (session->clp->cl_cons_state != NFS_CS_READY)
5334 return status;
5335
5336 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5337 msg.rpc_argp = session;
5338 msg.rpc_resp = NULL;
5339 msg.rpc_cred = NULL;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005340 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005341
5342 if (status)
5343 printk(KERN_WARNING
5344 "Got error %d from the server on DESTROY_SESSION. "
5345 "Session has been destroyed regardless...\n", status);
5346
5347 dprintk("<-- nfs4_proc_destroy_session\n");
5348 return status;
5349}
5350
Trond Myklebustfccba802009-07-21 16:48:07 -04005351int nfs4_init_session(struct nfs_server *server)
5352{
5353 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005354 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005355 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005356 int ret;
5357
5358 if (!nfs4_has_session(clp))
5359 return 0;
5360
Trond Myklebust1055d762010-06-16 09:52:27 -04005361 session = clp->cl_session;
5362 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5363 return 0;
5364
Andy Adamson68bf05e2009-12-15 12:55:02 -05005365 rsize = server->rsize;
5366 if (rsize == 0)
5367 rsize = NFS_MAX_FILE_IO_SIZE;
5368 wsize = server->wsize;
5369 if (wsize == 0)
5370 wsize = NFS_MAX_FILE_IO_SIZE;
5371
Andy Adamson68bf05e2009-12-15 12:55:02 -05005372 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5373 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005374
Trond Myklebustfccba802009-07-21 16:48:07 -04005375 ret = nfs4_recover_expired_lease(server);
5376 if (!ret)
5377 ret = nfs4_check_client_ready(clp);
5378 return ret;
5379}
5380
Andy Adamsond83217c2011-03-01 01:34:17 +00005381int nfs4_init_ds_session(struct nfs_client *clp)
5382{
5383 struct nfs4_session *session = clp->cl_session;
5384 int ret;
5385
5386 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5387 return 0;
5388
5389 ret = nfs4_client_recover_expired_lease(clp);
5390 if (!ret)
5391 /* Test for the DS role */
5392 if (!is_ds_client(clp))
5393 ret = -ENODEV;
5394 if (!ret)
5395 ret = nfs4_check_client_ready(clp);
5396 return ret;
5397
5398}
5399EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5400
5401
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005402/*
5403 * Renew the cl_session lease.
5404 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005405struct nfs4_sequence_data {
5406 struct nfs_client *clp;
5407 struct nfs4_sequence_args args;
5408 struct nfs4_sequence_res res;
5409};
5410
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005411static void nfs41_sequence_release(void *data)
5412{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005413 struct nfs4_sequence_data *calldata = data;
5414 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005415
Alexandros Batsakis71358402010-02-05 03:45:05 -08005416 if (atomic_read(&clp->cl_count) > 1)
5417 nfs4_schedule_state_renewal(clp);
5418 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005419 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005420}
5421
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005422static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5423{
5424 switch(task->tk_status) {
5425 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005426 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5427 return -EAGAIN;
5428 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005429 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005430 }
5431 return 0;
5432}
5433
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005434static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005435{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005436 struct nfs4_sequence_data *calldata = data;
5437 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005438
Trond Myklebust14516c32010-07-31 14:29:06 -04005439 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5440 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005441
5442 if (task->tk_status < 0) {
5443 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005444 if (atomic_read(&clp->cl_count) == 1)
5445 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005446
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005447 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5448 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005449 return;
5450 }
5451 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005452 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005453out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005454 dprintk("<-- %s\n", __func__);
5455}
5456
5457static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5458{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005459 struct nfs4_sequence_data *calldata = data;
5460 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005461 struct nfs4_sequence_args *args;
5462 struct nfs4_sequence_res *res;
5463
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005464 args = task->tk_msg.rpc_argp;
5465 res = task->tk_msg.rpc_resp;
5466
Trond Myklebust035168a2010-06-16 09:52:26 -04005467 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005468 return;
5469 rpc_call_start(task);
5470}
5471
5472static const struct rpc_call_ops nfs41_sequence_ops = {
5473 .rpc_call_done = nfs41_sequence_call_done,
5474 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005475 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005476};
5477
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005478static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005479{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005480 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005481 struct rpc_message msg = {
5482 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5483 .rpc_cred = cred,
5484 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005485 struct rpc_task_setup task_setup_data = {
5486 .rpc_client = clp->cl_rpcclient,
5487 .rpc_message = &msg,
5488 .callback_ops = &nfs41_sequence_ops,
5489 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5490 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005491
Alexandros Batsakis71358402010-02-05 03:45:05 -08005492 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005493 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005494 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005495 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005496 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005497 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005498 }
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005499 msg.rpc_argp = &calldata->args;
5500 msg.rpc_resp = &calldata->res;
5501 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005502 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005503
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005504 return rpc_run_task(&task_setup_data);
5505}
5506
5507static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5508{
5509 struct rpc_task *task;
5510 int ret = 0;
5511
5512 task = _nfs41_proc_sequence(clp, cred);
5513 if (IS_ERR(task))
5514 ret = PTR_ERR(task);
5515 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005516 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005517 dprintk("<-- %s status=%d\n", __func__, ret);
5518 return ret;
5519}
5520
5521static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5522{
5523 struct rpc_task *task;
5524 int ret;
5525
5526 task = _nfs41_proc_sequence(clp, cred);
5527 if (IS_ERR(task)) {
5528 ret = PTR_ERR(task);
5529 goto out;
5530 }
5531 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005532 if (!ret) {
5533 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5534
5535 if (task->tk_status == 0)
5536 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005537 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005538 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005539 rpc_put_task(task);
5540out:
5541 dprintk("<-- %s status=%d\n", __func__, ret);
5542 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005543}
5544
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005545struct nfs4_reclaim_complete_data {
5546 struct nfs_client *clp;
5547 struct nfs41_reclaim_complete_args arg;
5548 struct nfs41_reclaim_complete_res res;
5549};
5550
5551static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5552{
5553 struct nfs4_reclaim_complete_data *calldata = data;
5554
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005555 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005556 if (nfs41_setup_sequence(calldata->clp->cl_session,
5557 &calldata->arg.seq_args,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005558 &calldata->res.seq_res, 0, task))
5559 return;
5560
5561 rpc_call_start(task);
5562}
5563
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005564static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5565{
5566 switch(task->tk_status) {
5567 case 0:
5568 case -NFS4ERR_COMPLETE_ALREADY:
5569 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5570 break;
5571 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005572 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005573 /* fall through */
5574 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005575 return -EAGAIN;
5576 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005577 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005578 }
5579 return 0;
5580}
5581
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005582static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5583{
5584 struct nfs4_reclaim_complete_data *calldata = data;
5585 struct nfs_client *clp = calldata->clp;
5586 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5587
5588 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005589 if (!nfs41_sequence_done(task, res))
5590 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005591
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005592 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5593 rpc_restart_call_prepare(task);
5594 return;
5595 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005596 dprintk("<-- %s\n", __func__);
5597}
5598
5599static void nfs4_free_reclaim_complete_data(void *data)
5600{
5601 struct nfs4_reclaim_complete_data *calldata = data;
5602
5603 kfree(calldata);
5604}
5605
5606static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5607 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5608 .rpc_call_done = nfs4_reclaim_complete_done,
5609 .rpc_release = nfs4_free_reclaim_complete_data,
5610};
5611
5612/*
5613 * Issue a global reclaim complete.
5614 */
5615static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5616{
5617 struct nfs4_reclaim_complete_data *calldata;
5618 struct rpc_task *task;
5619 struct rpc_message msg = {
5620 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5621 };
5622 struct rpc_task_setup task_setup_data = {
5623 .rpc_client = clp->cl_rpcclient,
5624 .rpc_message = &msg,
5625 .callback_ops = &nfs4_reclaim_complete_call_ops,
5626 .flags = RPC_TASK_ASYNC,
5627 };
5628 int status = -ENOMEM;
5629
5630 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005631 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005632 if (calldata == NULL)
5633 goto out;
5634 calldata->clp = clp;
5635 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005636
5637 msg.rpc_argp = &calldata->arg;
5638 msg.rpc_resp = &calldata->res;
5639 task_setup_data.callback_data = calldata;
5640 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005641 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005642 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005643 goto out;
5644 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005645 status = nfs4_wait_for_completion_rpc_task(task);
5646 if (status == 0)
5647 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005648 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005649 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005650out:
5651 dprintk("<-- %s status=%d\n", __func__, status);
5652 return status;
5653}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005654
5655static void
5656nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5657{
5658 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005659 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005660
5661 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005662 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5663 * right now covering the LAYOUTGET we are about to send.
5664 * However, that is not so catastrophic, and there seems
5665 * to be no way to prevent it completely.
5666 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005667 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5668 &lgp->res.seq_res, 0, task))
5669 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005670 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5671 NFS_I(lgp->args.inode)->layout,
5672 lgp->args.ctx->state)) {
5673 rpc_exit(task, NFS4_OK);
5674 return;
5675 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005676 rpc_call_start(task);
5677}
5678
5679static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5680{
5681 struct nfs4_layoutget *lgp = calldata;
5682 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5683
5684 dprintk("--> %s\n", __func__);
5685
5686 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5687 return;
5688
5689 switch (task->tk_status) {
5690 case 0:
5691 break;
5692 case -NFS4ERR_LAYOUTTRYLATER:
5693 case -NFS4ERR_RECALLCONFLICT:
5694 task->tk_status = -NFS4ERR_DELAY;
5695 /* Fall through */
5696 default:
5697 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5698 rpc_restart_call_prepare(task);
5699 return;
5700 }
5701 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005702 dprintk("<-- %s\n", __func__);
5703}
5704
5705static void nfs4_layoutget_release(void *calldata)
5706{
5707 struct nfs4_layoutget *lgp = calldata;
5708
5709 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005710 put_nfs_open_context(lgp->args.ctx);
5711 kfree(calldata);
5712 dprintk("<-- %s\n", __func__);
5713}
5714
5715static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5716 .rpc_call_prepare = nfs4_layoutget_prepare,
5717 .rpc_call_done = nfs4_layoutget_done,
5718 .rpc_release = nfs4_layoutget_release,
5719};
5720
5721int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5722{
5723 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5724 struct rpc_task *task;
5725 struct rpc_message msg = {
5726 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5727 .rpc_argp = &lgp->args,
5728 .rpc_resp = &lgp->res,
5729 };
5730 struct rpc_task_setup task_setup_data = {
5731 .rpc_client = server->client,
5732 .rpc_message = &msg,
5733 .callback_ops = &nfs4_layoutget_call_ops,
5734 .callback_data = lgp,
5735 .flags = RPC_TASK_ASYNC,
5736 };
5737 int status = 0;
5738
5739 dprintk("--> %s\n", __func__);
5740
Weston Andros Adamson35124a02011-03-24 16:48:21 -04005741 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005742 lgp->res.seq_res.sr_slot = NULL;
5743 task = rpc_run_task(&task_setup_data);
5744 if (IS_ERR(task))
5745 return PTR_ERR(task);
5746 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00005747 if (status == 0)
5748 status = task->tk_status;
5749 if (status == 0)
5750 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005751 rpc_put_task(task);
5752 dprintk("<-- %s status=%d\n", __func__, status);
5753 return status;
5754}
5755
Benny Halevycbe82602011-05-22 19:52:37 +03005756static void
5757nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
5758{
5759 struct nfs4_layoutreturn *lrp = calldata;
5760
5761 dprintk("--> %s\n", __func__);
5762 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
5763 &lrp->res.seq_res, 0, task))
5764 return;
5765 rpc_call_start(task);
5766}
5767
5768static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
5769{
5770 struct nfs4_layoutreturn *lrp = calldata;
5771 struct nfs_server *server;
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005772 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03005773
5774 dprintk("--> %s\n", __func__);
5775
5776 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
5777 return;
5778
5779 server = NFS_SERVER(lrp->args.inode);
5780 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5781 nfs_restart_rpc(task, lrp->clp);
5782 return;
5783 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005784 spin_lock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005785 if (task->tk_status == 0) {
Benny Halevycbe82602011-05-22 19:52:37 +03005786 if (lrp->res.lrs_present) {
Benny Halevycbe82602011-05-22 19:52:37 +03005787 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Benny Halevycbe82602011-05-22 19:52:37 +03005788 } else
5789 BUG_ON(!list_empty(&lo->plh_segs));
5790 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005791 lo->plh_block_lgets--;
5792 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005793 dprintk("<-- %s\n", __func__);
5794}
5795
5796static void nfs4_layoutreturn_release(void *calldata)
5797{
5798 struct nfs4_layoutreturn *lrp = calldata;
5799
5800 dprintk("--> %s\n", __func__);
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005801 put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03005802 kfree(calldata);
5803 dprintk("<-- %s\n", __func__);
5804}
5805
5806static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
5807 .rpc_call_prepare = nfs4_layoutreturn_prepare,
5808 .rpc_call_done = nfs4_layoutreturn_done,
5809 .rpc_release = nfs4_layoutreturn_release,
5810};
5811
5812int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
5813{
5814 struct rpc_task *task;
5815 struct rpc_message msg = {
5816 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
5817 .rpc_argp = &lrp->args,
5818 .rpc_resp = &lrp->res,
5819 };
5820 struct rpc_task_setup task_setup_data = {
5821 .rpc_client = lrp->clp->cl_rpcclient,
5822 .rpc_message = &msg,
5823 .callback_ops = &nfs4_layoutreturn_call_ops,
5824 .callback_data = lrp,
5825 };
5826 int status;
5827
5828 dprintk("--> %s\n", __func__);
5829 task = rpc_run_task(&task_setup_data);
5830 if (IS_ERR(task))
5831 return PTR_ERR(task);
5832 status = task->tk_status;
5833 dprintk("<-- %s status=%d\n", __func__, status);
5834 rpc_put_task(task);
5835 return status;
5836}
5837
Andy Adamson7f11d8d2011-07-30 20:52:35 -04005838/*
5839 * Retrieve the list of Data Server devices from the MDS.
5840 */
5841static int _nfs4_getdevicelist(struct nfs_server *server,
5842 const struct nfs_fh *fh,
5843 struct pnfs_devicelist *devlist)
5844{
5845 struct nfs4_getdevicelist_args args = {
5846 .fh = fh,
5847 .layoutclass = server->pnfs_curr_ld->id,
5848 };
5849 struct nfs4_getdevicelist_res res = {
5850 .devlist = devlist,
5851 };
5852 struct rpc_message msg = {
5853 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
5854 .rpc_argp = &args,
5855 .rpc_resp = &res,
5856 };
5857 int status;
5858
5859 dprintk("--> %s\n", __func__);
5860 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5861 &res.seq_res, 0);
5862 dprintk("<-- %s status=%d\n", __func__, status);
5863 return status;
5864}
5865
5866int nfs4_proc_getdevicelist(struct nfs_server *server,
5867 const struct nfs_fh *fh,
5868 struct pnfs_devicelist *devlist)
5869{
5870 struct nfs4_exception exception = { };
5871 int err;
5872
5873 do {
5874 err = nfs4_handle_exception(server,
5875 _nfs4_getdevicelist(server, fh, devlist),
5876 &exception);
5877 } while (exception.retry);
5878
5879 dprintk("%s: err=%d, num_devs=%u\n", __func__,
5880 err, devlist->num_devs);
5881
5882 return err;
5883}
5884EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
5885
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005886static int
5887_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5888{
5889 struct nfs4_getdeviceinfo_args args = {
5890 .pdev = pdev,
5891 };
5892 struct nfs4_getdeviceinfo_res res = {
5893 .pdev = pdev,
5894 };
5895 struct rpc_message msg = {
5896 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5897 .rpc_argp = &args,
5898 .rpc_resp = &res,
5899 };
5900 int status;
5901
5902 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005903 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005904 dprintk("<-- %s status=%d\n", __func__, status);
5905
5906 return status;
5907}
5908
5909int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5910{
5911 struct nfs4_exception exception = { };
5912 int err;
5913
5914 do {
5915 err = nfs4_handle_exception(server,
5916 _nfs4_proc_getdeviceinfo(server, pdev),
5917 &exception);
5918 } while (exception.retry);
5919 return err;
5920}
5921EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5922
Andy Adamson863a3c62011-03-23 13:27:54 +00005923static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
5924{
5925 struct nfs4_layoutcommit_data *data = calldata;
5926 struct nfs_server *server = NFS_SERVER(data->args.inode);
5927
5928 if (nfs4_setup_sequence(server, &data->args.seq_args,
5929 &data->res.seq_res, 1, task))
5930 return;
5931 rpc_call_start(task);
5932}
5933
5934static void
5935nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
5936{
5937 struct nfs4_layoutcommit_data *data = calldata;
5938 struct nfs_server *server = NFS_SERVER(data->args.inode);
5939
5940 if (!nfs4_sequence_done(task, &data->res.seq_res))
5941 return;
5942
5943 switch (task->tk_status) { /* Just ignore these failures */
5944 case NFS4ERR_DELEG_REVOKED: /* layout was recalled */
5945 case NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
5946 case NFS4ERR_BADLAYOUT: /* no layout */
5947 case NFS4ERR_GRACE: /* loca_recalim always false */
5948 task->tk_status = 0;
5949 }
5950
5951 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5952 nfs_restart_rpc(task, server->nfs_client);
5953 return;
5954 }
5955
5956 if (task->tk_status == 0)
5957 nfs_post_op_update_inode_force_wcc(data->args.inode,
5958 data->res.fattr);
5959}
5960
5961static void nfs4_layoutcommit_release(void *calldata)
5962{
5963 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04005964 struct pnfs_layout_segment *lseg, *tmp;
Andy Adamson863a3c62011-03-23 13:27:54 +00005965
Andy Adamsondb29c082011-07-30 20:52:38 -04005966 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00005967 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04005968 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
5969 list_del_init(&lseg->pls_lc_list);
5970 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
5971 &lseg->pls_flags))
5972 put_lseg(lseg);
5973 }
Andy Adamson863a3c62011-03-23 13:27:54 +00005974 put_rpccred(data->cred);
5975 kfree(data);
5976}
5977
5978static const struct rpc_call_ops nfs4_layoutcommit_ops = {
5979 .rpc_call_prepare = nfs4_layoutcommit_prepare,
5980 .rpc_call_done = nfs4_layoutcommit_done,
5981 .rpc_release = nfs4_layoutcommit_release,
5982};
5983
5984int
Andy Adamsonef311532011-03-12 02:58:10 -05005985nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00005986{
5987 struct rpc_message msg = {
5988 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
5989 .rpc_argp = &data->args,
5990 .rpc_resp = &data->res,
5991 .rpc_cred = data->cred,
5992 };
5993 struct rpc_task_setup task_setup_data = {
5994 .task = &data->task,
5995 .rpc_client = NFS_CLIENT(data->args.inode),
5996 .rpc_message = &msg,
5997 .callback_ops = &nfs4_layoutcommit_ops,
5998 .callback_data = data,
5999 .flags = RPC_TASK_ASYNC,
6000 };
6001 struct rpc_task *task;
6002 int status = 0;
6003
6004 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6005 "lbw: %llu inode %lu\n",
6006 data->task.tk_pid, sync,
6007 data->args.lastbytewritten,
6008 data->args.inode->i_ino);
6009
6010 task = rpc_run_task(&task_setup_data);
6011 if (IS_ERR(task))
6012 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006013 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006014 goto out;
6015 status = nfs4_wait_for_completion_rpc_task(task);
6016 if (status != 0)
6017 goto out;
6018 status = task->tk_status;
6019out:
6020 dprintk("%s: status %d\n", __func__, status);
6021 rpc_put_task(task);
6022 return status;
6023}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006024
6025static int
6026_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6027 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6028{
6029 struct nfs41_secinfo_no_name_args args = {
6030 .style = SECINFO_STYLE_CURRENT_FH,
6031 };
6032 struct nfs4_secinfo_res res = {
6033 .flavors = flavors,
6034 };
6035 struct rpc_message msg = {
6036 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6037 .rpc_argp = &args,
6038 .rpc_resp = &res,
6039 };
6040 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6041}
6042
6043static int
6044nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6045 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6046{
6047 struct nfs4_exception exception = { };
6048 int err;
6049 do {
6050 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6051 switch (err) {
6052 case 0:
6053 case -NFS4ERR_WRONGSEC:
6054 case -NFS4ERR_NOTSUPP:
6055 break;
6056 default:
6057 err = nfs4_handle_exception(server, err, &exception);
6058 }
6059 } while (exception.retry);
6060 return err;
6061}
6062
6063static int
6064nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6065 struct nfs_fsinfo *info)
6066{
6067 int err;
6068 struct page *page;
6069 rpc_authflavor_t flavor;
6070 struct nfs4_secinfo_flavors *flavors;
6071
6072 page = alloc_page(GFP_KERNEL);
6073 if (!page) {
6074 err = -ENOMEM;
6075 goto out;
6076 }
6077
6078 flavors = page_address(page);
6079 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6080
6081 /*
6082 * Fall back on "guess and check" method if
6083 * the server doesn't support SECINFO_NO_NAME
6084 */
6085 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6086 err = nfs4_find_root_sec(server, fhandle, info);
6087 goto out_freepage;
6088 }
6089 if (err)
6090 goto out_freepage;
6091
6092 flavor = nfs_find_best_sec(flavors);
6093 if (err == 0)
6094 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6095
6096out_freepage:
6097 put_page(page);
6098 if (err == -EACCES)
6099 return -EPERM;
6100out:
6101 return err;
6102}
Bryan Schumaker7d974792011-06-02 14:59:08 -04006103static int _nfs41_test_stateid(struct nfs_server *server, struct nfs4_state *state)
6104{
6105 int status;
6106 struct nfs41_test_stateid_args args = {
6107 .stateid = &state->stateid,
6108 };
6109 struct nfs41_test_stateid_res res;
6110 struct rpc_message msg = {
6111 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6112 .rpc_argp = &args,
6113 .rpc_resp = &res,
6114 };
6115 args.seq_args.sa_session = res.seq_res.sr_session = NULL;
6116 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 0, 1);
6117 return status;
6118}
6119
6120static int nfs41_test_stateid(struct nfs_server *server, struct nfs4_state *state)
6121{
6122 struct nfs4_exception exception = { };
6123 int err;
6124 do {
6125 err = nfs4_handle_exception(server,
6126 _nfs41_test_stateid(server, state),
6127 &exception);
6128 } while (exception.retry);
6129 return err;
6130}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006131
6132static int _nfs4_free_stateid(struct nfs_server *server, struct nfs4_state *state)
6133{
6134 int status;
6135 struct nfs41_free_stateid_args args = {
6136 .stateid = &state->stateid,
6137 };
6138 struct nfs41_free_stateid_res res;
6139 struct rpc_message msg = {
6140 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6141 .rpc_argp = &args,
6142 .rpc_resp = &res,
6143 };
6144
6145 args.seq_args.sa_session = res.seq_res.sr_session = NULL;
6146 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 0, 1);
6147 return status;
6148}
6149
6150static int nfs41_free_stateid(struct nfs_server *server, struct nfs4_state *state)
6151{
6152 struct nfs4_exception exception = { };
6153 int err;
6154 do {
6155 err = nfs4_handle_exception(server,
6156 _nfs4_free_stateid(server, state),
6157 &exception);
6158 } while (exception.retry);
6159 return err;
6160}
Andy Adamson557134a2009-04-01 09:21:53 -04006161#endif /* CONFIG_NFS_V4_1 */
6162
Andy Adamson591d71c2009-04-01 09:22:47 -04006163struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006164 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006165 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166 .recover_open = nfs4_open_reclaim,
6167 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006168 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006169 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170};
6171
Andy Adamson591d71c2009-04-01 09:22:47 -04006172#if defined(CONFIG_NFS_V4_1)
6173struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6174 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6175 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6176 .recover_open = nfs4_open_reclaim,
6177 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006178 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006179 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006180 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006181};
6182#endif /* CONFIG_NFS_V4_1 */
6183
6184struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006185 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006186 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187 .recover_open = nfs4_open_expired,
6188 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006189 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006190 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191};
6192
Andy Adamson591d71c2009-04-01 09:22:47 -04006193#if defined(CONFIG_NFS_V4_1)
6194struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6195 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6196 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006197 .recover_open = nfs41_open_expired,
6198 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006199 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006200 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006201};
6202#endif /* CONFIG_NFS_V4_1 */
6203
Benny Halevy29fba382009-04-01 09:22:44 -04006204struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6205 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006206 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006207 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006208};
6209
6210#if defined(CONFIG_NFS_V4_1)
6211struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
6212 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006213 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006214 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006215};
6216#endif
6217
Trond Myklebust97dc1352010-06-16 09:52:26 -04006218static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6219 .minor_version = 0,
6220 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04006221 .validate_stateid = nfs4_validate_delegation_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006222 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006223 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6224 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6225 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006226};
6227
6228#if defined(CONFIG_NFS_V4_1)
6229static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6230 .minor_version = 1,
6231 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04006232 .validate_stateid = nfs41_validate_delegation_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006233 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006234 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6235 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6236 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006237};
6238#endif
6239
Trond Myklebust97dc1352010-06-16 09:52:26 -04006240const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6241 [0] = &nfs_v4_0_minor_ops,
6242#if defined(CONFIG_NFS_V4_1)
6243 [1] = &nfs_v4_1_minor_ops,
6244#endif
6245};
6246
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006247static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006248 .permission = nfs_permission,
6249 .getattr = nfs_getattr,
6250 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006251 .getxattr = generic_getxattr,
6252 .setxattr = generic_setxattr,
6253 .listxattr = generic_listxattr,
6254 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006255};
6256
David Howells509de812006-08-22 20:06:11 -04006257const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006258 .version = 4, /* protocol version */
6259 .dentry_ops = &nfs4_dentry_operations,
6260 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006261 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262 .getroot = nfs4_proc_get_root,
6263 .getattr = nfs4_proc_getattr,
6264 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04006265 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006266 .lookup = nfs4_proc_lookup,
6267 .access = nfs4_proc_access,
6268 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006269 .create = nfs4_proc_create,
6270 .remove = nfs4_proc_remove,
6271 .unlink_setup = nfs4_proc_unlink_setup,
6272 .unlink_done = nfs4_proc_unlink_done,
6273 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006274 .rename_setup = nfs4_proc_rename_setup,
6275 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006276 .link = nfs4_proc_link,
6277 .symlink = nfs4_proc_symlink,
6278 .mkdir = nfs4_proc_mkdir,
6279 .rmdir = nfs4_proc_remove,
6280 .readdir = nfs4_proc_readdir,
6281 .mknod = nfs4_proc_mknod,
6282 .statfs = nfs4_proc_statfs,
6283 .fsinfo = nfs4_proc_fsinfo,
6284 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006285 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006286 .decode_dirent = nfs4_decode_dirent,
6287 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05006288 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006290 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006292 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006294 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006295 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006296 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00006297 .init_client = nfs4_init_client,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006298 .secinfo = nfs4_proc_secinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006299};
6300
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006301static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6302 .prefix = XATTR_NAME_NFSV4_ACL,
6303 .list = nfs4_xattr_list_nfs4_acl,
6304 .get = nfs4_xattr_get_nfs4_acl,
6305 .set = nfs4_xattr_set_nfs4_acl,
6306};
6307
6308const struct xattr_handler *nfs4_xattr_handlers[] = {
6309 &nfs4_xattr_nfs4_acl_handler,
6310 NULL
6311};
6312
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313/*
6314 * Local variables:
6315 * c-basic-offset: 8
6316 * End:
6317 */