blob: 26bece8f30830254e917b859799669576fad304a [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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Linus Torvalds1da177e2005-04-16 15:20:36 -070084/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050085static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Trond Myklebust52567b02009-10-23 14:46:42 -040087 if (err >= -1000)
88 return err;
89 switch (err) {
90 case -NFS4ERR_RESOURCE:
91 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +000092 case -NFS4ERR_WRONGSEC:
93 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -080094 case -NFS4ERR_BADOWNER:
95 case -NFS4ERR_BADNAME:
96 return -EINVAL;
Trond Myklebust52567b02009-10-23 14:46:42 -040097 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -070099 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400100 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400102 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
105/*
106 * This is our standard bitmap for GETATTR requests.
107 */
108const u32 nfs4_fattr_bitmap[2] = {
109 FATTR4_WORD0_TYPE
110 | FATTR4_WORD0_CHANGE
111 | FATTR4_WORD0_SIZE
112 | FATTR4_WORD0_FSID
113 | FATTR4_WORD0_FILEID,
114 FATTR4_WORD1_MODE
115 | FATTR4_WORD1_NUMLINKS
116 | FATTR4_WORD1_OWNER
117 | FATTR4_WORD1_OWNER_GROUP
118 | FATTR4_WORD1_RAWDEV
119 | FATTR4_WORD1_SPACE_USED
120 | FATTR4_WORD1_TIME_ACCESS
121 | FATTR4_WORD1_TIME_METADATA
122 | FATTR4_WORD1_TIME_MODIFY
123};
124
125const u32 nfs4_statfs_bitmap[2] = {
126 FATTR4_WORD0_FILES_AVAIL
127 | FATTR4_WORD0_FILES_FREE
128 | FATTR4_WORD0_FILES_TOTAL,
129 FATTR4_WORD1_SPACE_AVAIL
130 | FATTR4_WORD1_SPACE_FREE
131 | FATTR4_WORD1_SPACE_TOTAL
132};
133
Trond Myklebust4ce79712005-06-22 17:16:21 +0000134const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 FATTR4_WORD0_MAXLINK
136 | FATTR4_WORD0_MAXNAME,
137 0
138};
139
140const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
141 | FATTR4_WORD0_MAXREAD
142 | FATTR4_WORD0_MAXWRITE
143 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700144 FATTR4_WORD1_TIME_DELTA
Andy Adamson504913f2010-10-20 00:17:57 -0400145 | FATTR4_WORD1_FS_LAYOUT_TYPES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146};
147
Manoj Naik830b8e32006-06-09 09:34:25 -0400148const u32 nfs4_fs_locations_bitmap[2] = {
149 FATTR4_WORD0_TYPE
150 | FATTR4_WORD0_CHANGE
151 | FATTR4_WORD0_SIZE
152 | FATTR4_WORD0_FSID
153 | FATTR4_WORD0_FILEID
154 | FATTR4_WORD0_FS_LOCATIONS,
155 FATTR4_WORD1_MODE
156 | FATTR4_WORD1_NUMLINKS
157 | FATTR4_WORD1_OWNER
158 | FATTR4_WORD1_OWNER_GROUP
159 | FATTR4_WORD1_RAWDEV
160 | FATTR4_WORD1_SPACE_USED
161 | FATTR4_WORD1_TIME_ACCESS
162 | FATTR4_WORD1_TIME_METADATA
163 | FATTR4_WORD1_TIME_MODIFY
164 | FATTR4_WORD1_MOUNTED_ON_FILEID
165};
166
Al Virobc4785c2006-10-19 23:28:51 -0700167static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 struct nfs4_readdir_arg *readdir)
169{
Al Viro0dbb4c62006-10-19 23:28:49 -0700170 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 BUG_ON(readdir->count < 80);
173 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000174 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
176 return;
177 }
178
179 readdir->cookie = 0;
180 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
181 if (cookie == 2)
182 return;
183
184 /*
185 * NFSv4 servers do not return entries for '.' and '..'
186 * Therefore, we fake these entries here. We let '.'
187 * have cookie 0 and '..' have cookie 1. Note that
188 * when talking to the server, we always send cookie 0
189 * instead of 1 or 2.
190 */
Al Viro0dbb4c62006-10-19 23:28:49 -0700191 start = p = kmap_atomic(*readdir->pages, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 if (cookie == 0) {
194 *p++ = xdr_one; /* next */
195 *p++ = xdr_zero; /* cookie, first word */
196 *p++ = xdr_one; /* cookie, second word */
197 *p++ = xdr_one; /* entry len */
198 memcpy(p, ".\0\0\0", 4); /* entry */
199 p++;
200 *p++ = xdr_one; /* bitmap length */
201 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
202 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400203 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
205
206 *p++ = xdr_one; /* next */
207 *p++ = xdr_zero; /* cookie, first word */
208 *p++ = xdr_two; /* cookie, second word */
209 *p++ = xdr_two; /* entry len */
210 memcpy(p, "..\0\0", 4); /* entry */
211 p++;
212 *p++ = xdr_one; /* bitmap length */
213 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
214 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400215 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 readdir->pgbase = (char *)p - (char *)start;
218 readdir->count -= readdir->pgbase;
219 kunmap_atomic(start, KM_USER0);
220}
221
Trond Myklebust65de8722008-12-23 15:21:44 -0500222static int nfs4_wait_clnt_recover(struct nfs_client *clp)
223{
224 int res;
225
226 might_sleep();
227
Trond Myklebuste005e802008-12-23 15:21:48 -0500228 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400229 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500230 return res;
231}
232
233static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
234{
235 int res = 0;
236
237 might_sleep();
238
239 if (*timeout <= 0)
240 *timeout = NFS4_POLL_RETRY_MIN;
241 if (*timeout > NFS4_POLL_RETRY_MAX)
242 *timeout = NFS4_POLL_RETRY_MAX;
243 schedule_timeout_killable(*timeout);
244 if (fatal_signal_pending(current))
245 res = -ERESTARTSYS;
246 *timeout <<= 1;
247 return res;
248}
249
250/* This is the error handling routine for processes that are allowed
251 * to sleep.
252 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800253static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500254{
255 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500256 struct nfs4_state *state = exception->state;
Trond Myklebust65de8722008-12-23 15:21:44 -0500257 int ret = errorcode;
258
259 exception->retry = 0;
260 switch(errorcode) {
261 case 0:
262 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500263 case -NFS4ERR_ADMIN_REVOKED:
264 case -NFS4ERR_BAD_STATEID:
265 case -NFS4ERR_OPENMODE:
266 if (state == NULL)
267 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500268 nfs4_schedule_stateid_recovery(server, state);
269 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400270 case -NFS4ERR_EXPIRED:
271 if (state != NULL)
272 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500273 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500274 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500275 nfs4_schedule_lease_recovery(clp);
276 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500277#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400278 case -NFS4ERR_BADSESSION:
279 case -NFS4ERR_BADSLOT:
280 case -NFS4ERR_BAD_HIGH_SLOT:
281 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
282 case -NFS4ERR_DEADSESSION:
283 case -NFS4ERR_SEQ_FALSE_RETRY:
284 case -NFS4ERR_SEQ_MISORDERED:
285 dprintk("%s ERROR: %d Reset session\n", __func__,
286 errorcode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500287 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -0400288 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500289 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500290#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500291 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500292 if (exception->timeout > HZ) {
293 /* We have retried a decent amount, time to
294 * fail
295 */
296 ret = -EBUSY;
297 break;
298 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500299 case -NFS4ERR_GRACE:
300 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500301 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500302 ret = nfs4_delay(server->client, &exception->timeout);
303 if (ret != 0)
304 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400305 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500306 case -NFS4ERR_OLD_STATEID:
307 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800308 break;
309 case -NFS4ERR_BADOWNER:
310 /* The following works around a Linux server bug! */
311 case -NFS4ERR_BADNAME:
312 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
313 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
314 exception->retry = 1;
315 printk(KERN_WARNING "NFS: v4 server %s "
316 "does not accept raw "
317 "uid/gids. "
318 "Reenabling the idmapper.\n",
319 server->nfs_client->cl_hostname);
320 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500321 }
322 /* We failed to handle the error */
323 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500324wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500325 ret = nfs4_wait_clnt_recover(clp);
326 if (ret == 0)
327 exception->retry = 1;
328 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500329}
330
331
Trond Myklebust452e9352010-07-31 14:29:06 -0400332static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 spin_lock(&clp->cl_lock);
335 if (time_before(clp->cl_last_renewal,timestamp))
336 clp->cl_last_renewal = timestamp;
337 spin_unlock(&clp->cl_lock);
338}
339
Trond Myklebust452e9352010-07-31 14:29:06 -0400340static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
341{
342 do_renew_lease(server->nfs_client, timestamp);
343}
344
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400345#if defined(CONFIG_NFS_V4_1)
346
Benny Halevy510b8172009-04-01 09:22:14 -0400347/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400348 * nfs4_free_slot - free a slot and efficiently update slot table.
349 *
350 * freeing a slot is trivially done by clearing its respective bit
351 * in the bitmap.
352 * If the freed slotid equals highest_used_slotid we want to update it
353 * so that the server would be able to size down the slot table if needed,
354 * otherwise we know that the highest_used_slotid is still in use.
355 * When updating highest_used_slotid there may be "holes" in the bitmap
356 * so we need to scan down from highest_used_slotid to 0 looking for the now
357 * highest slotid in use.
358 * If none found, highest_used_slotid is set to -1.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500359 *
360 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400361 */
362static void
Benny Halevydfb4f3092010-09-24 09:17:01 -0400363nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *free_slot)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400364{
Benny Halevydfb4f3092010-09-24 09:17:01 -0400365 int free_slotid = free_slot - tbl->slots;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400366 int slotid = free_slotid;
367
Benny Halevydfb4f3092010-09-24 09:17:01 -0400368 BUG_ON(slotid < 0 || slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400369 /* clear used bit in bitmap */
370 __clear_bit(slotid, tbl->used_slots);
371
372 /* update highest_used_slotid when it is freed */
373 if (slotid == tbl->highest_used_slotid) {
374 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500375 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400376 tbl->highest_used_slotid = slotid;
377 else
378 tbl->highest_used_slotid = -1;
379 }
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400380 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
381 free_slotid, tbl->highest_used_slotid);
382}
383
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800384/*
Andy Adamson42acd022011-01-06 02:04:34 +0000385 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800386 */
Andy Adamson42acd022011-01-06 02:04:34 +0000387static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800388{
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800389 struct rpc_task *task;
390
Trond Myklebusta2118c32010-06-16 09:52:26 -0400391 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800392 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
393 if (task)
394 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800395 return;
396 }
397
398 if (ses->fc_slot_table.highest_used_slotid != -1)
399 return;
400
Andy Adamson42acd022011-01-06 02:04:34 +0000401 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
402 complete(&ses->fc_slot_table.complete);
403}
404
405/*
406 * Signal state manager thread if session back channel is drained
407 */
408void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
409{
410 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
411 ses->bc_slot_table.highest_used_slotid != -1)
412 return;
413 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
414 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800415}
416
Trond Myklebustd185a332010-06-16 09:52:25 -0400417static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400418{
419 struct nfs4_slot_table *tbl;
420
Trond Myklebustd185a332010-06-16 09:52:25 -0400421 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400422 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400423 /* just wake up the next guy waiting since
424 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500425 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500426 return;
Andy Adamson13615872009-04-01 09:22:17 -0400427 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500428
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500429 spin_lock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400430 nfs4_free_slot(tbl, res->sr_slot);
Andy Adamson42acd022011-01-06 02:04:34 +0000431 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500432 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400433 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400434}
435
Trond Myklebust14516c32010-07-31 14:29:06 -0400436static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400437{
438 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400439 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400440
441 /*
442 * sr_status remains 1 if an RPC level error occurred. The server
443 * may or may not have processed the sequence operation..
444 * Proceed as if the server received and processed the sequence
445 * operation.
446 */
447 if (res->sr_status == 1)
448 res->sr_status = NFS_OK;
449
Bryan Schumaker468f8612011-04-18 15:57:32 -0400450 /* don't increment the sequence number if the task wasn't sent */
451 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400452 goto out;
453
Andy Adamson691daf32009-12-04 15:55:39 -0500454 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400455 switch (res->sr_status) {
456 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400457 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400458 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400459 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400460 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400461 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500462 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500463 if (res->sr_status_flags != 0)
464 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400465 break;
466 case -NFS4ERR_DELAY:
467 /* The server detected a resend of the RPC call and
468 * returned NFS4ERR_DELAY as per Section 2.10.6.2
469 * of RFC5661.
470 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200471 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400472 __func__,
473 res->sr_slot - res->sr_session->fc_slot_table.slots,
474 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400475 goto out_retry;
476 default:
477 /* Just update the slot sequence no. */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400478 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400479 }
480out:
481 /* The session may be reset by one of the error handlers. */
482 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400483 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400484 return 1;
485out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400486 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400487 goto out;
488 rpc_delay(task, NFS4_POLL_RETRY_MAX);
489 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400490}
491
Trond Myklebust14516c32010-07-31 14:29:06 -0400492static int nfs4_sequence_done(struct rpc_task *task,
493 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400494{
Trond Myklebust14516c32010-07-31 14:29:06 -0400495 if (res->sr_session == NULL)
496 return 1;
497 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400498}
499
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400500/*
Benny Halevy510b8172009-04-01 09:22:14 -0400501 * nfs4_find_slot - efficiently look for a free slot
502 *
503 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
504 * If found, we mark the slot as used, update the highest_used_slotid,
505 * and respectively set up the sequence operation args.
506 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400507 *
508 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400509 */
510static u8
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800511nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400512{
513 int slotid;
514 u8 ret_id = NFS4_MAX_SLOT_TABLE;
515 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
516
Benny Halevy510b8172009-04-01 09:22:14 -0400517 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
518 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
519 tbl->max_slots);
520 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
521 if (slotid >= tbl->max_slots)
522 goto out;
523 __set_bit(slotid, tbl->used_slots);
524 if (slotid > tbl->highest_used_slotid)
525 tbl->highest_used_slotid = slotid;
526 ret_id = slotid;
527out:
528 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
529 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400530 return ret_id;
531}
532
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000533int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400534 struct nfs4_sequence_args *args,
535 struct nfs4_sequence_res *res,
536 int cache_reply,
537 struct rpc_task *task)
538{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400539 struct nfs4_slot *slot;
540 struct nfs4_slot_table *tbl;
541 u8 slotid;
542
543 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400544 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400545 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400546 return 0;
547
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400548 tbl = &session->fc_slot_table;
549
550 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400551 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800552 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamsonea028ac2009-12-04 15:55:38 -0500553 /*
554 * The state manager will wait until the slot table is empty.
555 * Schedule the reset thread
556 */
Andy Adamson05f0d232009-12-04 15:55:37 -0500557 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400558 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500559 dprintk("%s Schedule Session Reset\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500560 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400561 }
562
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800563 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
564 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
565 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
566 spin_unlock(&tbl->slot_tbl_lock);
567 dprintk("%s enforce FIFO order\n", __func__);
568 return -EAGAIN;
569 }
570
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800571 slotid = nfs4_find_slot(tbl);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400572 if (slotid == NFS4_MAX_SLOT_TABLE) {
573 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
574 spin_unlock(&tbl->slot_tbl_lock);
575 dprintk("<-- %s: no free slots\n", __func__);
576 return -EAGAIN;
577 }
578 spin_unlock(&tbl->slot_tbl_lock);
579
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800580 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400581 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400582 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400583 args->sa_slotid = slotid;
584 args->sa_cache_this = cache_reply;
585
586 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
587
Benny Halevy99fe60d2009-04-01 09:22:29 -0400588 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400589 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400590 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400591 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400592 /*
593 * sr_status is only set in decode_sequence, and so will remain
594 * set to 1 if an rpc level failure occurs.
595 */
596 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400597 return 0;
598}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000599EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400600
Trond Myklebust035168ab2010-06-16 09:52:26 -0400601int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400602 struct nfs4_sequence_args *args,
603 struct nfs4_sequence_res *res,
604 int cache_reply,
605 struct rpc_task *task)
606{
Trond Myklebust035168ab2010-06-16 09:52:26 -0400607 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400608 int ret = 0;
609
Trond Myklebust035168ab2010-06-16 09:52:26 -0400610 if (session == NULL) {
611 args->sa_session = NULL;
612 res->sr_session = NULL;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400613 goto out;
Trond Myklebust035168ab2010-06-16 09:52:26 -0400614 }
615
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200616 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400617 __func__, session->clp, session, res->sr_slot ?
618 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168ab2010-06-16 09:52:26 -0400619
620 ret = nfs41_setup_sequence(session, args, res, cache_reply,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400621 task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400622out:
623 dprintk("<-- %s status=%d\n", __func__, ret);
624 return ret;
625}
626
627struct nfs41_call_sync_data {
Trond Myklebust035168ab2010-06-16 09:52:26 -0400628 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400629 struct nfs4_sequence_args *seq_args;
630 struct nfs4_sequence_res *seq_res;
631 int cache_reply;
632};
633
634static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
635{
636 struct nfs41_call_sync_data *data = calldata;
637
Trond Myklebust035168ab2010-06-16 09:52:26 -0400638 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
639
640 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400641 data->seq_res, data->cache_reply, task))
642 return;
643 rpc_call_start(task);
644}
645
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800646static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
647{
648 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
649 nfs41_call_sync_prepare(task, calldata);
650}
651
Andy Adamson69ab40c2009-04-01 09:22:19 -0400652static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
653{
654 struct nfs41_call_sync_data *data = calldata;
655
Trond Myklebust14516c32010-07-31 14:29:06 -0400656 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400657}
658
Andy Adamsonce5039c2009-04-01 09:22:13 -0400659struct rpc_call_ops nfs41_call_sync_ops = {
660 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400661 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400662};
663
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800664struct rpc_call_ops nfs41_call_priv_sync_ops = {
665 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
666 .rpc_call_done = nfs41_call_sync_done,
667};
668
Bryan Schumaker7c513052011-03-24 17:12:24 +0000669static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
670 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400671 struct rpc_message *msg,
672 struct nfs4_sequence_args *args,
673 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800674 int cache_reply,
675 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400676{
677 int ret;
678 struct rpc_task *task;
679 struct nfs41_call_sync_data data = {
Trond Myklebust035168ab2010-06-16 09:52:26 -0400680 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400681 .seq_args = args,
682 .seq_res = res,
683 .cache_reply = cache_reply,
684 };
685 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000686 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400687 .rpc_message = msg,
688 .callback_ops = &nfs41_call_sync_ops,
689 .callback_data = &data
690 };
691
Benny Halevydfb4f3092010-09-24 09:17:01 -0400692 res->sr_slot = NULL;
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800693 if (privileged)
694 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400695 task = rpc_run_task(&task_setup);
696 if (IS_ERR(task))
697 ret = PTR_ERR(task);
698 else {
699 ret = task->tk_status;
700 rpc_put_task(task);
701 }
702 return ret;
703}
704
Bryan Schumaker7c513052011-03-24 17:12:24 +0000705int _nfs4_call_sync_session(struct rpc_clnt *clnt,
706 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400707 struct rpc_message *msg,
708 struct nfs4_sequence_args *args,
709 struct nfs4_sequence_res *res,
710 int cache_reply)
711{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000712 return nfs4_call_sync_sequence(clnt, server, msg, args, res, cache_reply, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400713}
714
Trond Myklebustdf896452010-06-16 09:52:26 -0400715#else
Trond Myklebust14516c32010-07-31 14:29:06 -0400716static int nfs4_sequence_done(struct rpc_task *task,
717 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400718{
Trond Myklebust14516c32010-07-31 14:29:06 -0400719 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400720}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400721#endif /* CONFIG_NFS_V4_1 */
722
Bryan Schumaker7c513052011-03-24 17:12:24 +0000723int _nfs4_call_sync(struct rpc_clnt *clnt,
724 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400725 struct rpc_message *msg,
726 struct nfs4_sequence_args *args,
727 struct nfs4_sequence_res *res,
728 int cache_reply)
729{
Benny Halevyf3752972009-04-01 09:22:04 -0400730 args->sa_session = res->sr_session = NULL;
Bryan Schumaker7c513052011-03-24 17:12:24 +0000731 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400732}
733
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000734static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000735int nfs4_call_sync(struct rpc_clnt *clnt,
736 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000737 struct rpc_message *msg,
738 struct nfs4_sequence_args *args,
739 struct nfs4_sequence_res *res,
740 int cache_reply)
741{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000742 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
743 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000744}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400745
Trond Myklebust38478b22006-05-25 01:40:57 -0400746static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
Trond Myklebust38478b22006-05-25 01:40:57 -0400748 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Trond Myklebust38478b22006-05-25 01:40:57 -0400750 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400751 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
752 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400753 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400754 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400755 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
757
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100758struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400759 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100760 struct nfs_openargs o_arg;
761 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100762 struct nfs_open_confirmargs c_arg;
763 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100764 struct nfs_fattr f_attr;
765 struct nfs_fattr dir_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100766 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400767 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100768 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400769 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100770 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100771 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400772 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100773 int rpc_status;
774 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100775};
776
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400777
778static void nfs4_init_opendata_res(struct nfs4_opendata *p)
779{
780 p->o_res.f_attr = &p->f_attr;
781 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400782 p->o_res.seqid = p->o_arg.seqid;
783 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400784 p->o_res.server = p->o_arg.server;
785 nfs_fattr_init(&p->f_attr);
786 nfs_fattr_init(&p->dir_attr);
787}
788
Al Viro82a2c1b2011-06-22 18:30:55 -0400789static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500790 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400791 const struct iattr *attrs,
792 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100793{
Al Viro82a2c1b2011-06-22 18:30:55 -0400794 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100795 struct inode *dir = parent->d_inode;
796 struct nfs_server *server = NFS_SERVER(dir);
797 struct nfs4_opendata *p;
798
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400799 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100800 if (p == NULL)
801 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400802 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100803 if (p->o_arg.seqid == NULL)
804 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400805 nfs_sb_active(dentry->d_sb);
806 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100807 p->dir = parent;
808 p->owner = sp;
809 atomic_inc(&sp->so_count);
810 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500811 p->o_arg.open_flags = flags;
812 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400813 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400814 p->o_arg.id = sp->so_owner_id.id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400815 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100816 p->o_arg.server = server;
817 p->o_arg.bitmask = server->attr_bitmask;
818 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400819 if (flags & O_CREAT) {
820 u32 *s;
821
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100822 p->o_arg.u.attrs = &p->attrs;
823 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400824 s = (u32 *) p->o_arg.u.verifier.data;
825 s[0] = jiffies;
826 s[1] = current->pid;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100827 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100828 p->c_arg.fh = &p->o_res.fh;
829 p->c_arg.stateid = &p->o_res.stateid;
830 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400831 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400832 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100833 return p;
834err_free:
835 kfree(p);
836err:
837 dput(parent);
838 return NULL;
839}
840
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400841static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100842{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400843 struct nfs4_opendata *p = container_of(kref,
844 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400845 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400846
847 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400848 if (p->state != NULL)
849 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400850 nfs4_put_state_owner(p->owner);
851 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400852 dput(p->dentry);
853 nfs_sb_deactive(sb);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400854 kfree(p);
855}
856
857static void nfs4_opendata_put(struct nfs4_opendata *p)
858{
859 if (p != NULL)
860 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100861}
862
Trond Myklebust06f814a2006-01-03 09:55:07 +0100863static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
864{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100865 int ret;
866
Trond Myklebust06f814a2006-01-03 09:55:07 +0100867 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100868 return ret;
869}
870
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500871static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400872{
873 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500874
875 if (open_mode & O_EXCL)
876 goto out;
877 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400878 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500879 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
880 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400881 break;
882 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500883 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
884 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400885 break;
886 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500887 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
888 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400889 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500890out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400891 return ret;
892}
893
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500894static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400895{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500896 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400897 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500898 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400899 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500900 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400901 return 1;
902}
903
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500904static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100905{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500906 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100907 case FMODE_WRITE:
908 state->n_wronly++;
909 break;
910 case FMODE_READ:
911 state->n_rdonly++;
912 break;
913 case FMODE_READ|FMODE_WRITE:
914 state->n_rdwr++;
915 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500916 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100917}
918
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500919static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400920{
921 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
922 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
923 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500924 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400925 case FMODE_READ:
926 set_bit(NFS_O_RDONLY_STATE, &state->flags);
927 break;
928 case FMODE_WRITE:
929 set_bit(NFS_O_WRONLY_STATE, &state->flags);
930 break;
931 case FMODE_READ|FMODE_WRITE:
932 set_bit(NFS_O_RDWR_STATE, &state->flags);
933 }
934}
935
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500936static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400937{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400938 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500939 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400940 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400941}
942
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500943static 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 -0700944{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400945 /*
946 * Protect the call to nfs4_state_set_mode_locked and
947 * serialise the stateid update
948 */
949 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400950 if (deleg_stateid != NULL) {
951 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
952 set_bit(NFS_DELEGATED_STATE, &state->flags);
953 }
954 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500955 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400956 write_sequnlock(&state->seqlock);
957 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500958 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700959 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960}
961
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500962static 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 -0500963{
964 struct nfs_inode *nfsi = NFS_I(state->inode);
965 struct nfs_delegation *deleg_cur;
966 int ret = 0;
967
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500968 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500969
970 rcu_read_lock();
971 deleg_cur = rcu_dereference(nfsi->delegation);
972 if (deleg_cur == NULL)
973 goto no_delegation;
974
975 spin_lock(&deleg_cur->lock);
976 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500977 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500978 goto no_delegation_unlock;
979
980 if (delegation == NULL)
981 delegation = &deleg_cur->stateid;
982 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
983 goto no_delegation_unlock;
984
Trond Myklebustb7391f42008-12-23 15:21:52 -0500985 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500986 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500987 ret = 1;
988no_delegation_unlock:
989 spin_unlock(&deleg_cur->lock);
990no_delegation:
991 rcu_read_unlock();
992
993 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500994 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500995 ret = 1;
996 }
997
998 return ret;
999}
1000
1001
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001002static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001003{
1004 struct nfs_delegation *delegation;
1005
1006 rcu_read_lock();
1007 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001008 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001009 rcu_read_unlock();
1010 return;
1011 }
1012 rcu_read_unlock();
1013 nfs_inode_return_delegation(inode);
1014}
1015
Trond Myklebust6ee41262007-07-08 14:11:36 -04001016static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001017{
1018 struct nfs4_state *state = opendata->state;
1019 struct nfs_inode *nfsi = NFS_I(state->inode);
1020 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001021 int open_mode = opendata->o_arg.open_flags & O_EXCL;
1022 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001023 nfs4_stateid stateid;
1024 int ret = -EAGAIN;
1025
Trond Myklebustaac00a82007-07-05 19:02:21 -04001026 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001027 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001028 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001029 if (can_open_cached(state, fmode, open_mode)) {
1030 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001031 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001032 goto out_return_state;
1033 }
1034 spin_unlock(&state->owner->so_lock);
1035 }
Trond Myklebust34310432008-12-23 15:21:38 -05001036 rcu_read_lock();
1037 delegation = rcu_dereference(nfsi->delegation);
1038 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001039 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001040 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001041 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001042 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001043 /* Save the delegation */
1044 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1045 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001046 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001047 if (ret != 0)
1048 goto out;
1049 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001050
1051 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001052 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001053 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001054 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001055out:
1056 return ERR_PTR(ret);
1057out_return_state:
1058 atomic_inc(&state->count);
1059 return state;
1060}
1061
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001062static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1063{
1064 struct inode *inode;
1065 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001066 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001067 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001068
Trond Myklebustaac00a82007-07-05 19:02:21 -04001069 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001070 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001071 goto out;
1072 }
1073
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001074 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001075 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001076 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001077 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001078 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001079 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001080 goto err;
1081 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001082 state = nfs4_get_open_state(inode, data->owner);
1083 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001084 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001085 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001086 int delegation_flags = 0;
1087
Trond Myklebust003707c2007-07-05 18:07:55 -04001088 rcu_read_lock();
1089 delegation = rcu_dereference(NFS_I(inode)->delegation);
1090 if (delegation)
1091 delegation_flags = delegation->flags;
1092 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -05001093 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001094 nfs_inode_set_delegation(state->inode,
1095 data->owner->so_cred,
1096 &data->o_res);
1097 else
1098 nfs_inode_reclaim_delegation(state->inode,
1099 data->owner->so_cred,
1100 &data->o_res);
1101 }
Trond Myklebust34310432008-12-23 15:21:38 -05001102
1103 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001104 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001105 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001106out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001107 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001108err_put_inode:
1109 iput(inode);
1110err:
1111 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001112}
1113
Trond Myklebust864472e2006-01-03 09:55:15 +01001114static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1115{
1116 struct nfs_inode *nfsi = NFS_I(state->inode);
1117 struct nfs_open_context *ctx;
1118
1119 spin_lock(&state->inode->i_lock);
1120 list_for_each_entry(ctx, &nfsi->open_files, list) {
1121 if (ctx->state != state)
1122 continue;
1123 get_nfs_open_context(ctx);
1124 spin_unlock(&state->inode->i_lock);
1125 return ctx;
1126 }
1127 spin_unlock(&state->inode->i_lock);
1128 return ERR_PTR(-ENOENT);
1129}
1130
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001131static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1132{
1133 struct nfs4_opendata *opendata;
1134
Al Viro3d4ff432011-06-22 18:40:12 -04001135 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001136 if (opendata == NULL)
1137 return ERR_PTR(-ENOMEM);
1138 opendata->state = state;
1139 atomic_inc(&state->count);
1140 return opendata;
1141}
1142
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001143static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001144{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001145 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001146 int ret;
1147
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001148 opendata->o_arg.open_flags = 0;
1149 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001150 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1151 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1152 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001153 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001154 if (ret != 0)
1155 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001156 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001157 if (IS_ERR(newstate))
1158 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001159 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001160 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001161 return 0;
1162}
1163
1164static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1165{
Trond Myklebust864472e2006-01-03 09:55:15 +01001166 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001167 int ret;
1168
1169 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001170 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001171 smp_rmb();
1172 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001173 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001174 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001175 if (ret != 0)
1176 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001177 if (newstate != state)
1178 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001179 }
1180 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001181 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001182 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001183 if (ret != 0)
1184 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001185 if (newstate != state)
1186 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001187 }
1188 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001189 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001190 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001191 if (ret != 0)
1192 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001193 if (newstate != state)
1194 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001195 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001196 /*
1197 * We may have performed cached opens for all three recoveries.
1198 * Check if we need to update the current stateid.
1199 */
1200 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1201 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001202 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001203 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1204 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001205 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001206 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001207 return 0;
1208}
1209
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210/*
1211 * OPEN_RECLAIM:
1212 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001214static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001216 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001217 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001218 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 int status;
1220
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001221 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1222 if (IS_ERR(opendata))
1223 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001224 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1225 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001226 rcu_read_lock();
1227 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001228 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001229 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001230 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001231 opendata->o_arg.u.delegation_type = delegation_type;
1232 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001233 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 return status;
1235}
1236
Trond Myklebust539cd032007-06-05 11:46:42 -04001237static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
1239 struct nfs_server *server = NFS_SERVER(state->inode);
1240 struct nfs4_exception exception = { };
1241 int err;
1242 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001243 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001244 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001245 break;
1246 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 } while (exception.retry);
1248 return err;
1249}
1250
Trond Myklebust864472e2006-01-03 09:55:15 +01001251static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1252{
1253 struct nfs_open_context *ctx;
1254 int ret;
1255
1256 ctx = nfs4_state_find_open_context(state);
1257 if (IS_ERR(ctx))
1258 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001259 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001260 put_nfs_open_context(ctx);
1261 return ret;
1262}
1263
Trond Myklebust13437e12007-07-06 15:10:43 -04001264static 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 -07001265{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001266 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001267 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001269 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1270 if (IS_ERR(opendata))
1271 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001272 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001273 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001274 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001275 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001276 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001277 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278}
1279
Trond Myklebust13437e12007-07-06 15:10:43 -04001280int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281{
1282 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001283 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 int err;
1285 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001286 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 switch (err) {
1288 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001289 case -ENOENT:
1290 case -ESTALE:
1291 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001292 case -NFS4ERR_BADSESSION:
1293 case -NFS4ERR_BADSLOT:
1294 case -NFS4ERR_BAD_HIGH_SLOT:
1295 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1296 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001297 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001298 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 case -NFS4ERR_STALE_CLIENTID:
1300 case -NFS4ERR_STALE_STATEID:
1301 case -NFS4ERR_EXPIRED:
1302 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001303 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001304 goto out;
1305 case -ERESTARTSYS:
1306 /*
1307 * The show must go on: exit, but mark the
1308 * stateid as needing recovery.
1309 */
1310 case -NFS4ERR_ADMIN_REVOKED:
1311 case -NFS4ERR_BAD_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001312 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001313 case -EKEYEXPIRED:
1314 /*
1315 * User RPCSEC_GSS context has expired.
1316 * We cannot recover this stateid now, so
1317 * skip it and allow recovery thread to
1318 * proceed.
1319 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001320 case -ENOMEM:
1321 err = 0;
1322 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 }
1324 err = nfs4_handle_exception(server, err, &exception);
1325 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001326out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 return err;
1328}
1329
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001330static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1331{
1332 struct nfs4_opendata *data = calldata;
1333
1334 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001335 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001336 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1337 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001338 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001339 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001340 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001341 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001342}
1343
1344static void nfs4_open_confirm_release(void *calldata)
1345{
1346 struct nfs4_opendata *data = calldata;
1347 struct nfs4_state *state = NULL;
1348
1349 /* If this request hasn't been cancelled, do nothing */
1350 if (data->cancelled == 0)
1351 goto out_free;
1352 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001353 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001354 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001355 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001356 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001357 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001358out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001359 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001360}
1361
1362static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001363 .rpc_call_done = nfs4_open_confirm_done,
1364 .rpc_release = nfs4_open_confirm_release,
1365};
1366
1367/*
1368 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1369 */
1370static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1371{
1372 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1373 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001374 struct rpc_message msg = {
1375 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1376 .rpc_argp = &data->c_arg,
1377 .rpc_resp = &data->c_res,
1378 .rpc_cred = data->owner->so_cred,
1379 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001380 struct rpc_task_setup task_setup_data = {
1381 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001382 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001383 .callback_ops = &nfs4_open_confirm_ops,
1384 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001385 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001386 .flags = RPC_TASK_ASYNC,
1387 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 int status;
1389
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001390 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001391 data->rpc_done = 0;
1392 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001393 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001394 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001395 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001396 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001397 status = nfs4_wait_for_completion_rpc_task(task);
1398 if (status != 0) {
1399 data->cancelled = 1;
1400 smp_wmb();
1401 } else
1402 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001403 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 return status;
1405}
1406
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001407static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001409 struct nfs4_opendata *data = calldata;
1410 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001411
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001412 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1413 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001414 /*
1415 * Check if we still need to send an OPEN call, or if we can use
1416 * a delegation instead.
1417 */
1418 if (data->state != NULL) {
1419 struct nfs_delegation *delegation;
1420
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001421 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001422 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001423 rcu_read_lock();
1424 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1425 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001426 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001427 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001428 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001429 }
1430 rcu_read_unlock();
1431 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001432 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001433 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001434 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001435 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001436 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001437 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1438 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001439 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04001440 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001441 &data->o_arg.seq_args,
1442 &data->o_res.seq_res, 1, task))
1443 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001444 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001445 return;
1446out_no_action:
1447 task->tk_action = NULL;
1448
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001449}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001451static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1452{
1453 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1454 nfs4_open_prepare(task, calldata);
1455}
1456
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001457static void nfs4_open_done(struct rpc_task *task, void *calldata)
1458{
1459 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001461 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001462
Trond Myklebust14516c32010-07-31 14:29:06 -04001463 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1464 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001465
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001466 if (task->tk_status == 0) {
1467 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001468 case S_IFREG:
1469 break;
1470 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001471 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001472 break;
1473 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001474 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001475 break;
1476 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001477 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001478 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001479 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001480 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1481 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001482 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001483 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001484}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001485
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001486static void nfs4_open_release(void *calldata)
1487{
1488 struct nfs4_opendata *data = calldata;
1489 struct nfs4_state *state = NULL;
1490
1491 /* If this request hasn't been cancelled, do nothing */
1492 if (data->cancelled == 0)
1493 goto out_free;
1494 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001495 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001496 goto out_free;
1497 /* In case we need an open_confirm, no cleanup! */
1498 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1499 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001500 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001501 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001502 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001503out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001504 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001505}
1506
1507static const struct rpc_call_ops nfs4_open_ops = {
1508 .rpc_call_prepare = nfs4_open_prepare,
1509 .rpc_call_done = nfs4_open_done,
1510 .rpc_release = nfs4_open_release,
1511};
1512
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001513static const struct rpc_call_ops nfs4_recover_open_ops = {
1514 .rpc_call_prepare = nfs4_recover_open_prepare,
1515 .rpc_call_done = nfs4_open_done,
1516 .rpc_release = nfs4_open_release,
1517};
1518
1519static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001520{
1521 struct inode *dir = data->dir->d_inode;
1522 struct nfs_server *server = NFS_SERVER(dir);
1523 struct nfs_openargs *o_arg = &data->o_arg;
1524 struct nfs_openres *o_res = &data->o_res;
1525 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001526 struct rpc_message msg = {
1527 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1528 .rpc_argp = o_arg,
1529 .rpc_resp = o_res,
1530 .rpc_cred = data->owner->so_cred,
1531 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001532 struct rpc_task_setup task_setup_data = {
1533 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001534 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001535 .callback_ops = &nfs4_open_ops,
1536 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001537 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001538 .flags = RPC_TASK_ASYNC,
1539 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001540 int status;
1541
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001542 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001543 data->rpc_done = 0;
1544 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001545 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001546 if (isrecover)
1547 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001548 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001549 if (IS_ERR(task))
1550 return PTR_ERR(task);
1551 status = nfs4_wait_for_completion_rpc_task(task);
1552 if (status != 0) {
1553 data->cancelled = 1;
1554 smp_wmb();
1555 } else
1556 status = data->rpc_status;
1557 rpc_put_task(task);
1558
1559 return status;
1560}
1561
1562static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1563{
1564 struct inode *dir = data->dir->d_inode;
1565 struct nfs_openres *o_res = &data->o_res;
1566 int status;
1567
1568 status = nfs4_run_open_task(data, 1);
1569 if (status != 0 || !data->rpc_done)
1570 return status;
1571
1572 nfs_refresh_inode(dir, o_res->dir_attr);
1573
1574 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1575 status = _nfs4_proc_open_confirm(data);
1576 if (status != 0)
1577 return status;
1578 }
1579
1580 return status;
1581}
1582
1583/*
1584 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1585 */
1586static int _nfs4_proc_open(struct nfs4_opendata *data)
1587{
1588 struct inode *dir = data->dir->d_inode;
1589 struct nfs_server *server = NFS_SERVER(dir);
1590 struct nfs_openargs *o_arg = &data->o_arg;
1591 struct nfs_openres *o_res = &data->o_res;
1592 int status;
1593
1594 status = nfs4_run_open_task(data, 0);
Trond Myklebust3e309912007-07-07 13:19:59 -04001595 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001596 return status;
1597
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001598 if (o_arg->open_flags & O_CREAT) {
1599 update_changeattr(dir, &o_res->cinfo);
1600 nfs_post_op_update_inode(dir, o_res->dir_attr);
1601 } else
1602 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001603 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1604 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001606 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001608 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 }
1610 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001611 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001612 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613}
1614
Andy Adamsond83217c2011-03-01 01:34:17 +00001615static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001616{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001617 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001618 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001619
Trond Myklebusta78cb572009-08-09 15:06:19 -04001620 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001621 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001622 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001623 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001624 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1625 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001626 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001627 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001628 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001629 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001630 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001631}
1632
Andy Adamsond83217c2011-03-01 01:34:17 +00001633static int nfs4_recover_expired_lease(struct nfs_server *server)
1634{
1635 return nfs4_client_recover_expired_lease(server->nfs_client);
1636}
1637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638/*
1639 * OPEN_EXPIRED:
1640 * reclaim state on the server after a network partition.
1641 * Assumes caller holds the appropriate lock
1642 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001643static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001645 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001646 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001648 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1649 if (IS_ERR(opendata))
1650 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001651 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001652 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001653 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001654 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001655 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656}
1657
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001658static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001659{
Trond Myklebust539cd032007-06-05 11:46:42 -04001660 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001661 struct nfs4_exception exception = { };
1662 int err;
1663
1664 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001665 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001666 switch (err) {
1667 default:
1668 goto out;
1669 case -NFS4ERR_GRACE:
1670 case -NFS4ERR_DELAY:
1671 nfs4_handle_exception(server, err, &exception);
1672 err = 0;
1673 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001674 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001675out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001676 return err;
1677}
1678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1680{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001682 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Trond Myklebust864472e2006-01-03 09:55:15 +01001684 ctx = nfs4_state_find_open_context(state);
1685 if (IS_ERR(ctx))
1686 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001687 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001688 put_nfs_open_context(ctx);
1689 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690}
1691
1692/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001693 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1694 * fields corresponding to attributes that were used to store the verifier.
1695 * Make sure we clobber those fields in the later setattr call
1696 */
1697static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1698{
1699 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1700 !(sattr->ia_valid & ATTR_ATIME_SET))
1701 sattr->ia_valid |= ATTR_ATIME;
1702
1703 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1704 !(sattr->ia_valid & ATTR_MTIME_SET))
1705 sattr->ia_valid |= ATTR_MTIME;
1706}
1707
1708/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001709 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 */
Al Viro82a2c1b2011-06-22 18:30:55 -04001711static 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 -07001712{
1713 struct nfs4_state_owner *sp;
1714 struct nfs4_state *state = NULL;
1715 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001716 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001717 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
1719 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 status = -ENOMEM;
1721 if (!(sp = nfs4_get_state_owner(server, cred))) {
1722 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1723 goto out_err;
1724 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001725 status = nfs4_recover_expired_lease(server);
1726 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001727 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001728 if (dentry->d_inode != NULL)
1729 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001730 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001731 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001732 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001733 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
Al Viro82a2c1b2011-06-22 18:30:55 -04001735 if (dentry->d_inode != NULL)
1736 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001737
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001738 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001740 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001742 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001743 status = PTR_ERR(state);
1744 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001745 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001746 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001747 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001748
1749 if (opendata->o_arg.open_flags & O_EXCL) {
1750 nfs4_exclusive_attrset(opendata, sattr);
1751
1752 nfs_fattr_init(opendata->o_res.f_attr);
1753 status = nfs4_do_setattr(state->inode, cred,
1754 opendata->o_res.f_attr, sattr,
1755 state);
1756 if (status == 0)
1757 nfs_setattr_update_inode(state->inode, sattr);
1758 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1759 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001760 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 *res = state;
1763 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001764err_opendata_put:
1765 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001766err_put_state_owner:
1767 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 *res = NULL;
1770 return status;
1771}
1772
1773
Al Viro82a2c1b2011-06-22 18:30:55 -04001774static 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 -07001775{
1776 struct nfs4_exception exception = { };
1777 struct nfs4_state *res;
1778 int status;
1779
1780 do {
Al Viro82a2c1b2011-06-22 18:30:55 -04001781 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 if (status == 0)
1783 break;
1784 /* NOTE: BAD_SEQID means the server and client disagree about the
1785 * book-keeping w.r.t. state-changing operations
1786 * (OPEN/CLOSE/LOCK/LOCKU...)
1787 * It is actually a sign of a bug on the client or on the server.
1788 *
1789 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001790 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 * have unhashed the old state_owner for us, and that we can
1792 * therefore safely retry using a new one. We should still warn
1793 * the user though...
1794 */
1795 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001796 printk(KERN_WARNING "NFS: v4 server %s "
1797 " returned a bad sequence-id error!\n",
1798 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 exception.retry = 1;
1800 continue;
1801 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001802 /*
1803 * BAD_STATEID on OPEN means that the server cancelled our
1804 * state before it received the OPEN_CONFIRM.
1805 * Recover by retrying the request as per the discussion
1806 * on Page 181 of RFC3530.
1807 */
1808 if (status == -NFS4ERR_BAD_STATEID) {
1809 exception.retry = 1;
1810 continue;
1811 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001812 if (status == -EAGAIN) {
1813 /* We must have found a delegation */
1814 exception.retry = 1;
1815 continue;
1816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1818 status, &exception));
1819 } while (exception.retry);
1820 return res;
1821}
1822
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001823static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1824 struct nfs_fattr *fattr, struct iattr *sattr,
1825 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001827 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001829 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 .iap = sattr,
1831 .server = server,
1832 .bitmask = server->attr_bitmask,
1833 };
1834 struct nfs_setattrres res = {
1835 .fattr = fattr,
1836 .server = server,
1837 };
1838 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001839 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1840 .rpc_argp = &arg,
1841 .rpc_resp = &res,
1842 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001844 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001845 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
Trond Myklebust0e574af2005-10-27 22:12:38 -04001847 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001849 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1850 /* Use that stateid */
1851 } else if (state != NULL) {
Trond Myklebust77041ed2010-07-01 12:49:11 -04001852 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001853 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1855
Bryan Schumaker7c513052011-03-24 17:12:24 +00001856 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001857 if (status == 0 && state != NULL)
1858 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001859 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860}
1861
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001862static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1863 struct nfs_fattr *fattr, struct iattr *sattr,
1864 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001866 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 struct nfs4_exception exception = { };
1868 int err;
1869 do {
1870 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001871 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 &exception);
1873 } while (exception.retry);
1874 return err;
1875}
1876
1877struct nfs4_closedata {
1878 struct inode *inode;
1879 struct nfs4_state *state;
1880 struct nfs_closeargs arg;
1881 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001882 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001883 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00001884 bool roc;
1885 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886};
1887
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001888static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001889{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001890 struct nfs4_closedata *calldata = data;
1891 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04001892 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07001893
Fred Isamanf7e89172011-01-06 11:36:32 +00001894 if (calldata->roc)
1895 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001896 nfs4_put_open_state(calldata->state);
1897 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001898 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04001899 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07001900 kfree(calldata);
1901}
1902
Trond Myklebust88069f72009-12-08 08:33:16 -05001903static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1904 fmode_t fmode)
1905{
1906 spin_lock(&state->owner->so_lock);
1907 if (!(fmode & FMODE_READ))
1908 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1909 if (!(fmode & FMODE_WRITE))
1910 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1911 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1912 spin_unlock(&state->owner->so_lock);
1913}
1914
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001915static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001917 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 struct nfs_server *server = NFS_SERVER(calldata->inode);
1920
Trond Myklebust14516c32010-07-31 14:29:06 -04001921 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1922 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 /* hmm. we are done with the inode, and in the process of freeing
1924 * the state_owner. we keep this around to process errors
1925 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 switch (task->tk_status) {
1927 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00001928 if (calldata->roc)
1929 pnfs_roc_set_barrier(state->inode,
1930 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04001931 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001932 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001933 nfs4_close_clear_stateid_flags(state,
1934 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 break;
1936 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001937 case -NFS4ERR_OLD_STATEID:
1938 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001940 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001941 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001943 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1944 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001946 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001947 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948}
1949
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001950static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001952 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001953 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001954 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001955
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001956 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001957 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001958
Trond Myklebust88069f72009-12-08 08:33:16 -05001959 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1960 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001961 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001962 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001963 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001964 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001965 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1966 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1967 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001968 }
1969 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001970 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1971 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1972 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001973 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001974 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001975 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05001976
1977 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001978 /* Note: exit _without_ calling nfs4_close_done */
1979 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001980 return;
1981 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001982
Fred Isamanf7e89172011-01-06 11:36:32 +00001983 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001984 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00001985 if (calldata->roc &&
1986 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
1987 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
1988 task, NULL);
1989 return;
1990 }
1991 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001992
Trond Myklebust516a6af2005-10-27 22:12:41 -04001993 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001994 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04001995 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Andy Adamson19ddab02009-04-01 09:22:20 -04001996 &calldata->arg.seq_args, &calldata->res.seq_res,
1997 1, task))
1998 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001999 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000}
2001
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002002static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002003 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002004 .rpc_call_done = nfs4_close_done,
2005 .rpc_release = nfs4_free_closedata,
2006};
2007
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008/*
2009 * It is possible for data to be read/written from a mem-mapped file
2010 * after the sys_close call (which hits the vfs layer as a flush).
2011 * This means that we can't safely call nfsv4 close on a file until
2012 * the inode is cleared. This in turn means that we are not good
2013 * NFSv4 citizens - we do not indicate to the server to update the file's
2014 * share state even when we are done with one of the three share
2015 * stateid's in the inode.
2016 *
2017 * NOTE: Caller must be holding the sp->so_owner semaphore!
2018 */
Al Viro643168c2011-06-22 18:20:23 -04002019int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002021 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002023 struct nfs4_state_owner *sp = state->owner;
2024 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002025 struct rpc_message msg = {
2026 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2027 .rpc_cred = state->owner->so_cred,
2028 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002029 struct rpc_task_setup task_setup_data = {
2030 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002031 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002032 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002033 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002034 .flags = RPC_TASK_ASYNC,
2035 };
Trond Myklebust95121352005-10-18 14:20:12 -07002036 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002038 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002040 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002041 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002043 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002044 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002046 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002047 if (calldata->arg.seqid == NULL)
2048 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002049 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002050 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002051 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002052 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002053 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002054 calldata->roc = roc;
Al Viro643168c2011-06-22 18:20:23 -04002055 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002056
Trond Myklebust1174dd12010-12-21 10:52:24 -05002057 msg.rpc_argp = &calldata->arg;
2058 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002059 task_setup_data.callback_data = calldata;
2060 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002061 if (IS_ERR(task))
2062 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002063 status = 0;
2064 if (wait)
2065 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002066 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002067 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002068out_free_calldata:
2069 kfree(calldata);
2070out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002071 if (roc)
2072 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002073 nfs4_put_open_state(state);
2074 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002075 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076}
2077
Trond Myklebust2b484292010-09-17 10:56:51 -04002078static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002079nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 struct nfs4_state *state;
2082
Trond Myklebust565277f2007-10-15 18:17:53 -04002083 /* Protect against concurrent sillydeletes */
Al Viro3d4ff432011-06-22 18:40:12 -04002084 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002085 if (IS_ERR(state))
2086 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002087 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002088 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089}
2090
Trond Myklebust1185a552009-12-03 15:54:02 -05002091static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002092{
2093 if (ctx->state == NULL)
2094 return;
2095 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002096 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002097 else
Al Viro643168c2011-06-22 18:20:23 -04002098 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002099}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
2101static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2102{
Benny Halevy43652ad2009-04-01 09:21:54 -04002103 struct nfs4_server_caps_arg args = {
2104 .fhandle = fhandle,
2105 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 struct nfs4_server_caps_res res = {};
2107 struct rpc_message msg = {
2108 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002109 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 .rpc_resp = &res,
2111 };
2112 int status;
2113
Bryan Schumaker7c513052011-03-24 17:12:24 +00002114 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 if (status == 0) {
2116 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002117 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2118 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2119 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2120 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2121 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2123 server->caps |= NFS_CAP_ACLS;
2124 if (res.has_links != 0)
2125 server->caps |= NFS_CAP_HARDLINKS;
2126 if (res.has_symlinks != 0)
2127 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002128 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2129 server->caps |= NFS_CAP_FILEID;
2130 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2131 server->caps |= NFS_CAP_MODE;
2132 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2133 server->caps |= NFS_CAP_NLINK;
2134 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2135 server->caps |= NFS_CAP_OWNER;
2136 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2137 server->caps |= NFS_CAP_OWNER_GROUP;
2138 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2139 server->caps |= NFS_CAP_ATIME;
2140 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2141 server->caps |= NFS_CAP_CTIME;
2142 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2143 server->caps |= NFS_CAP_MTIME;
2144
Trond Myklebusta65318b2009-03-11 14:10:28 -04002145 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2146 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2147 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 server->acl_bitmask = res.acl_bitmask;
2149 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002150
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 return status;
2152}
2153
Trond Myklebust55a97592006-06-09 09:34:19 -04002154int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155{
2156 struct nfs4_exception exception = { };
2157 int err;
2158 do {
2159 err = nfs4_handle_exception(server,
2160 _nfs4_server_capabilities(server, fhandle),
2161 &exception);
2162 } while (exception.retry);
2163 return err;
2164}
2165
2166static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2167 struct nfs_fsinfo *info)
2168{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 struct nfs4_lookup_root_arg args = {
2170 .bitmask = nfs4_fattr_bitmap,
2171 };
2172 struct nfs4_lookup_res res = {
2173 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002174 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 .fh = fhandle,
2176 };
2177 struct rpc_message msg = {
2178 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2179 .rpc_argp = &args,
2180 .rpc_resp = &res,
2181 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002182
Trond Myklebust0e574af2005-10-27 22:12:38 -04002183 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002184 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185}
2186
2187static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2188 struct nfs_fsinfo *info)
2189{
2190 struct nfs4_exception exception = { };
2191 int err;
2192 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002193 err = _nfs4_lookup_root(server, fhandle, info);
2194 switch (err) {
2195 case 0:
2196 case -NFS4ERR_WRONGSEC:
2197 break;
2198 default:
2199 err = nfs4_handle_exception(server, err, &exception);
2200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 } while (exception.retry);
2202 return err;
2203}
2204
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002205static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2206 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2207{
2208 struct rpc_auth *auth;
2209 int ret;
2210
2211 auth = rpcauth_create(flavor, server->client);
2212 if (!auth) {
2213 ret = -EIO;
2214 goto out;
2215 }
2216 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002217out:
2218 return ret;
2219}
2220
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002221static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002222 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002224 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002225 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002227 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2228 flav_array[len] = RPC_AUTH_NULL;
2229 len += 1;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002230
2231 for (i = 0; i < len; i++) {
2232 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002233 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002234 continue;
2235 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002236 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002237 /*
2238 * -EACCESS could mean that the user doesn't have correct permissions
2239 * to access the mount. It could also mean that we tried to mount
2240 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2241 * existing mount programs don't handle -EACCES very well so it should
2242 * be mapped to -EPERM instead.
2243 */
2244 if (status == -EACCES)
2245 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002246 return status;
2247}
2248
2249/*
2250 * get the file handle for the "/" directory on the server
2251 */
2252static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2253 struct nfs_fsinfo *info)
2254{
2255 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002256 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2257 /*
2258 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2259 * by nfs4_map_errors() as this function exits.
2260 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002261 status = nfs4_find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 if (status == 0)
2263 status = nfs4_server_capabilities(server, fhandle);
2264 if (status == 0)
2265 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002266 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267}
2268
Andy Adamson533eb462011-06-13 18:25:56 -04002269static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002270/*
2271 * Get locations and (maybe) other attributes of a referral.
2272 * Note that we'll actually follow the referral later when
2273 * we detect fsid mismatch in inode revalidation
2274 */
Andy Adamson533eb462011-06-13 18:25:56 -04002275static int nfs4_get_referral(struct inode *dir, const struct qstr *name,
2276 struct nfs_fattr *fattr, struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002277{
2278 int status = -ENOMEM;
2279 struct page *page = NULL;
2280 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002281
2282 page = alloc_page(GFP_KERNEL);
2283 if (page == NULL)
2284 goto out;
2285 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2286 if (locations == NULL)
2287 goto out;
2288
Trond Myklebustc228fd32007-01-13 02:28:11 -05002289 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002290 if (status != 0)
2291 goto out;
2292 /* Make sure server returned a different fsid for the referral */
2293 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002294 dprintk("%s: server did not return a different fsid for"
2295 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002296 status = -EIO;
2297 goto out;
2298 }
Andy Adamson533eb462011-06-13 18:25:56 -04002299 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2300 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002301
Andy Adamson533eb462011-06-13 18:25:56 -04002302 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002303 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002304 memset(fhandle, 0, sizeof(struct nfs_fh));
2305out:
2306 if (page)
2307 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002308 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002309 return status;
2310}
2311
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2313{
2314 struct nfs4_getattr_arg args = {
2315 .fh = fhandle,
2316 .bitmask = server->attr_bitmask,
2317 };
2318 struct nfs4_getattr_res res = {
2319 .fattr = fattr,
2320 .server = server,
2321 };
2322 struct rpc_message msg = {
2323 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2324 .rpc_argp = &args,
2325 .rpc_resp = &res,
2326 };
2327
Trond Myklebust0e574af2005-10-27 22:12:38 -04002328 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002329 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330}
2331
2332static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2333{
2334 struct nfs4_exception exception = { };
2335 int err;
2336 do {
2337 err = nfs4_handle_exception(server,
2338 _nfs4_proc_getattr(server, fhandle, fattr),
2339 &exception);
2340 } while (exception.retry);
2341 return err;
2342}
2343
2344/*
2345 * The file is not closed if it is opened due to the a request to change
2346 * the size of the file. The open call will not be needed once the
2347 * VFS layer lookup-intents are implemented.
2348 *
2349 * Close is called when the inode is destroyed.
2350 * If we haven't opened the file for O_WRONLY, we
2351 * need to in the size_change case to obtain a stateid.
2352 *
2353 * Got race?
2354 * Because OPEN is always done by name in nfsv4, it is
2355 * possible that we opened a different file by the same
2356 * name. We can recognize this race condition, but we
2357 * can't do anything about it besides returning an error.
2358 *
2359 * This will be fixed with VFS changes (lookup-intent).
2360 */
2361static int
2362nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2363 struct iattr *sattr)
2364{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002365 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002366 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002367 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 int status;
2369
Benny Halevy8a1636c2010-07-14 15:43:57 -04002370 if (pnfs_ld_layoutret_on_setattr(inode))
2371 pnfs_return_layout(inode);
2372
Trond Myklebust0e574af2005-10-27 22:12:38 -04002373 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
Trond Myklebustd5308382005-11-04 15:33:38 -05002375 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002376 if (sattr->ia_valid & ATTR_FILE) {
2377 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002378
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002379 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002380 if (ctx) {
2381 cred = ctx->cred;
2382 state = ctx->state;
2383 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002384 }
2385
2386 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002387 if (status == 0)
2388 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 return status;
2390}
2391
Bryan Schumaker7c513052011-03-24 17:12:24 +00002392static int _nfs4_proc_lookupfh(struct rpc_clnt *clnt, struct nfs_server *server,
2393 const struct nfs_fh *dirfh, const struct qstr *name,
2394 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002395{
2396 int status;
2397 struct nfs4_lookup_arg args = {
2398 .bitmask = server->attr_bitmask,
2399 .dir_fh = dirfh,
2400 .name = name,
2401 };
2402 struct nfs4_lookup_res res = {
2403 .server = server,
2404 .fattr = fattr,
2405 .fh = fhandle,
2406 };
2407 struct rpc_message msg = {
2408 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2409 .rpc_argp = &args,
2410 .rpc_resp = &res,
2411 };
2412
2413 nfs_fattr_init(fattr);
2414
2415 dprintk("NFS call lookupfh %s\n", name->name);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002416 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002417 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002418 return status;
2419}
2420
2421static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2422 struct qstr *name, struct nfs_fh *fhandle,
2423 struct nfs_fattr *fattr)
2424{
2425 struct nfs4_exception exception = { };
2426 int err;
2427 do {
Bryan Schumaker7c513052011-03-24 17:12:24 +00002428 err = _nfs4_proc_lookupfh(server->client, server, dirfh, name, fhandle, fattr);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002429 /* FIXME: !!!! */
2430 if (err == -NFS4ERR_MOVED) {
2431 err = -EREMOTE;
2432 break;
2433 }
2434 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002435 } while (exception.retry);
2436 return err;
2437}
2438
Bryan Schumaker7c513052011-03-24 17:12:24 +00002439static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2440 const struct qstr *name, struct nfs_fh *fhandle,
2441 struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002443 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
2445 dprintk("NFS call lookup %s\n", name->name);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002446 status = _nfs4_proc_lookupfh(clnt, NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002447 if (status == -NFS4ERR_MOVED)
2448 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 dprintk("NFS reply lookup: %d\n", status);
2450 return status;
2451}
2452
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002453void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2454{
2455 memset(fh, 0, sizeof(struct nfs_fh));
2456 fattr->fsid.major = 1;
2457 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2458 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2459 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2460 fattr->nlink = 2;
2461}
2462
Bryan Schumaker7c513052011-03-24 17:12:24 +00002463static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2464 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465{
2466 struct nfs4_exception exception = { };
2467 int err;
2468 do {
2469 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumaker7c513052011-03-24 17:12:24 +00002470 _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 &exception);
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002472 if (err == -EPERM)
2473 nfs_fixup_secinfo_attributes(fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 } while (exception.retry);
2475 return err;
2476}
2477
2478static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2479{
Trond Myklebust76b32992007-08-10 17:45:11 -04002480 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 struct nfs4_accessargs args = {
2482 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002483 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002485 struct nfs4_accessres res = {
2486 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002487 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 struct rpc_message msg = {
2489 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2490 .rpc_argp = &args,
2491 .rpc_resp = &res,
2492 .rpc_cred = entry->cred,
2493 };
2494 int mode = entry->mask;
2495 int status;
2496
2497 /*
2498 * Determine which access bits we want to ask for...
2499 */
2500 if (mode & MAY_READ)
2501 args.access |= NFS4_ACCESS_READ;
2502 if (S_ISDIR(inode->i_mode)) {
2503 if (mode & MAY_WRITE)
2504 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2505 if (mode & MAY_EXEC)
2506 args.access |= NFS4_ACCESS_LOOKUP;
2507 } else {
2508 if (mode & MAY_WRITE)
2509 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2510 if (mode & MAY_EXEC)
2511 args.access |= NFS4_ACCESS_EXECUTE;
2512 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002513
2514 res.fattr = nfs_alloc_fattr();
2515 if (res.fattr == NULL)
2516 return -ENOMEM;
2517
Bryan Schumaker7c513052011-03-24 17:12:24 +00002518 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 if (!status) {
2520 entry->mask = 0;
2521 if (res.access & NFS4_ACCESS_READ)
2522 entry->mask |= MAY_READ;
2523 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2524 entry->mask |= MAY_WRITE;
2525 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2526 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002527 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002529 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 return status;
2531}
2532
2533static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2534{
2535 struct nfs4_exception exception = { };
2536 int err;
2537 do {
2538 err = nfs4_handle_exception(NFS_SERVER(inode),
2539 _nfs4_proc_access(inode, entry),
2540 &exception);
2541 } while (exception.retry);
2542 return err;
2543}
2544
2545/*
2546 * TODO: For the time being, we don't try to get any attributes
2547 * along with any of the zero-copy operations READ, READDIR,
2548 * READLINK, WRITE.
2549 *
2550 * In the case of the first three, we want to put the GETATTR
2551 * after the read-type operation -- this is because it is hard
2552 * to predict the length of a GETATTR response in v4, and thus
2553 * align the READ data correctly. This means that the GETATTR
2554 * may end up partially falling into the page cache, and we should
2555 * shift it into the 'tail' of the xdr_buf before processing.
2556 * To do this efficiently, we need to know the total length
2557 * of data received, which doesn't seem to be available outside
2558 * of the RPC layer.
2559 *
2560 * In the case of WRITE, we also want to put the GETATTR after
2561 * the operation -- in this case because we want to make sure
2562 * we get the post-operation mtime and size. This means that
2563 * we can't use xdr_encode_pages() as written: we need a variant
2564 * of it which would leave room in the 'tail' iovec.
2565 *
2566 * Both of these changes to the XDR layer would in fact be quite
2567 * minor, but I decided to leave them for a subsequent patch.
2568 */
2569static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2570 unsigned int pgbase, unsigned int pglen)
2571{
2572 struct nfs4_readlink args = {
2573 .fh = NFS_FH(inode),
2574 .pgbase = pgbase,
2575 .pglen = pglen,
2576 .pages = &page,
2577 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002578 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 struct rpc_message msg = {
2580 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2581 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002582 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 };
2584
Bryan Schumaker7c513052011-03-24 17:12:24 +00002585 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 -07002586}
2587
2588static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2589 unsigned int pgbase, unsigned int pglen)
2590{
2591 struct nfs4_exception exception = { };
2592 int err;
2593 do {
2594 err = nfs4_handle_exception(NFS_SERVER(inode),
2595 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2596 &exception);
2597 } while (exception.retry);
2598 return err;
2599}
2600
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601/*
2602 * Got race?
2603 * We will need to arrange for the VFS layer to provide an atomic open.
2604 * Until then, this create/open method is prone to inefficiency and race
2605 * conditions due to the lookup, create, and open VFS calls from sys_open()
2606 * placed on the wire.
2607 *
2608 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2609 * The file will be opened again in the subsequent VFS open call
2610 * (nfs4_proc_file_open).
2611 *
2612 * The open for read will just hang around to be used by any process that
2613 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2614 */
2615
2616static int
2617nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002618 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619{
Al Viro82a2c1b2011-06-22 18:30:55 -04002620 struct dentry *de = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002622 struct rpc_cred *cred = NULL;
2623 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 int status = 0;
2625
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002626 if (ctx != NULL) {
2627 cred = ctx->cred;
Al Viro3d4ff432011-06-22 18:40:12 -04002628 de = ctx->dentry;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002629 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002631 sattr->ia_mode &= ~current_umask();
Al Viro82a2c1b2011-06-22 18:30:55 -04002632 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002633 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 if (IS_ERR(state)) {
2635 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002636 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002638 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002639 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002640 if (ctx != NULL)
2641 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002642 else
Al Viro643168c2011-06-22 18:20:23 -04002643 nfs4_close_sync(state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644out:
2645 return status;
2646}
2647
2648static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2649{
Trond Myklebust16e42952005-10-27 22:12:44 -04002650 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002651 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002653 .name.len = name->len,
2654 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002655 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002657 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002658 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002659 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002661 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2662 .rpc_argp = &args,
2663 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002665 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666
Trond Myklebustd3468902010-04-16 16:22:50 -04002667 res.dir_attr = nfs_alloc_fattr();
2668 if (res.dir_attr == NULL)
2669 goto out;
2670
Bryan Schumaker7c513052011-03-24 17:12:24 +00002671 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002672 if (status == 0) {
2673 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002674 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002675 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002676 nfs_free_fattr(res.dir_attr);
2677out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 return status;
2679}
2680
2681static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2682{
2683 struct nfs4_exception exception = { };
2684 int err;
2685 do {
2686 err = nfs4_handle_exception(NFS_SERVER(dir),
2687 _nfs4_proc_remove(dir, name),
2688 &exception);
2689 } while (exception.retry);
2690 return err;
2691}
2692
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002693static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002695 struct nfs_server *server = NFS_SERVER(dir);
2696 struct nfs_removeargs *args = msg->rpc_argp;
2697 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698
Trond Myklebusta65318b2009-03-11 14:10:28 -04002699 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002700 res->server = server;
Benny Halevydfb4f3092010-09-24 09:17:01 -04002701 res->seq_res.sr_slot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703}
2704
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002705static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002707 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2708
Trond Myklebust14516c32010-07-31 14:29:06 -04002709 if (!nfs4_sequence_done(task, &res->seq_res))
2710 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002711 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002712 return 0;
2713 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002714 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002715 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716}
2717
Jeff Laytond3d41522010-09-17 17:31:57 -04002718static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2719{
2720 struct nfs_server *server = NFS_SERVER(dir);
2721 struct nfs_renameargs *arg = msg->rpc_argp;
2722 struct nfs_renameres *res = msg->rpc_resp;
2723
2724 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2725 arg->bitmask = server->attr_bitmask;
2726 res->server = server;
2727}
2728
2729static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2730 struct inode *new_dir)
2731{
2732 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2733
2734 if (!nfs4_sequence_done(task, &res->seq_res))
2735 return 0;
2736 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2737 return 0;
2738
2739 update_changeattr(old_dir, &res->old_cinfo);
2740 nfs_post_op_update_inode(old_dir, res->old_fattr);
2741 update_changeattr(new_dir, &res->new_cinfo);
2742 nfs_post_op_update_inode(new_dir, res->new_fattr);
2743 return 1;
2744}
2745
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2747 struct inode *new_dir, struct qstr *new_name)
2748{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002749 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002750 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 .old_dir = NFS_FH(old_dir),
2752 .new_dir = NFS_FH(new_dir),
2753 .old_name = old_name,
2754 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002755 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002757 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002758 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002759 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 struct rpc_message msg = {
2761 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2762 .rpc_argp = &arg,
2763 .rpc_resp = &res,
2764 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002765 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
Trond Myklebust011fff72010-04-16 16:22:49 -04002767 res.old_fattr = nfs_alloc_fattr();
2768 res.new_fattr = nfs_alloc_fattr();
2769 if (res.old_fattr == NULL || res.new_fattr == NULL)
2770 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
Bryan Schumaker7c513052011-03-24 17:12:24 +00002772 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 if (!status) {
2774 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002775 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002777 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002779out:
2780 nfs_free_fattr(res.new_fattr);
2781 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 return status;
2783}
2784
2785static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2786 struct inode *new_dir, struct qstr *new_name)
2787{
2788 struct nfs4_exception exception = { };
2789 int err;
2790 do {
2791 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2792 _nfs4_proc_rename(old_dir, old_name,
2793 new_dir, new_name),
2794 &exception);
2795 } while (exception.retry);
2796 return err;
2797}
2798
2799static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2800{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002801 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 struct nfs4_link_arg arg = {
2803 .fh = NFS_FH(inode),
2804 .dir_fh = NFS_FH(dir),
2805 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002806 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002808 struct nfs4_link_res res = {
2809 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002810 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 struct rpc_message msg = {
2812 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2813 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002814 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002816 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
Trond Myklebust136f2622010-04-16 16:22:49 -04002818 res.fattr = nfs_alloc_fattr();
2819 res.dir_attr = nfs_alloc_fattr();
2820 if (res.fattr == NULL || res.dir_attr == NULL)
2821 goto out;
2822
Bryan Schumaker7c513052011-03-24 17:12:24 +00002823 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002824 if (!status) {
2825 update_changeattr(dir, &res.cinfo);
2826 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002827 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002828 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002829out:
2830 nfs_free_fattr(res.dir_attr);
2831 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 return status;
2833}
2834
2835static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2836{
2837 struct nfs4_exception exception = { };
2838 int err;
2839 do {
2840 err = nfs4_handle_exception(NFS_SERVER(inode),
2841 _nfs4_proc_link(inode, dir, name),
2842 &exception);
2843 } while (exception.retry);
2844 return err;
2845}
2846
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002847struct nfs4_createdata {
2848 struct rpc_message msg;
2849 struct nfs4_create_arg arg;
2850 struct nfs4_create_res res;
2851 struct nfs_fh fh;
2852 struct nfs_fattr fattr;
2853 struct nfs_fattr dir_fattr;
2854};
2855
2856static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2857 struct qstr *name, struct iattr *sattr, u32 ftype)
2858{
2859 struct nfs4_createdata *data;
2860
2861 data = kzalloc(sizeof(*data), GFP_KERNEL);
2862 if (data != NULL) {
2863 struct nfs_server *server = NFS_SERVER(dir);
2864
2865 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2866 data->msg.rpc_argp = &data->arg;
2867 data->msg.rpc_resp = &data->res;
2868 data->arg.dir_fh = NFS_FH(dir);
2869 data->arg.server = server;
2870 data->arg.name = name;
2871 data->arg.attrs = sattr;
2872 data->arg.ftype = ftype;
2873 data->arg.bitmask = server->attr_bitmask;
2874 data->res.server = server;
2875 data->res.fh = &data->fh;
2876 data->res.fattr = &data->fattr;
2877 data->res.dir_fattr = &data->dir_fattr;
2878 nfs_fattr_init(data->res.fattr);
2879 nfs_fattr_init(data->res.dir_fattr);
2880 }
2881 return data;
2882}
2883
2884static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2885{
Bryan Schumaker7c513052011-03-24 17:12:24 +00002886 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00002887 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002888 if (status == 0) {
2889 update_changeattr(dir, &data->res.dir_cinfo);
2890 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2891 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2892 }
2893 return status;
2894}
2895
2896static void nfs4_free_createdata(struct nfs4_createdata *data)
2897{
2898 kfree(data);
2899}
2900
Chuck Lever4f390c12006-08-22 20:06:22 -04002901static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002902 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002904 struct nfs4_createdata *data;
2905 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906
Chuck Lever94a6d752006-08-22 20:06:23 -04002907 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002908 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002909
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002910 status = -ENOMEM;
2911 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2912 if (data == NULL)
2913 goto out;
2914
2915 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2916 data->arg.u.symlink.pages = &page;
2917 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002919 status = nfs4_do_create(dir, dentry, data);
2920
2921 nfs4_free_createdata(data);
2922out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 return status;
2924}
2925
Chuck Lever4f390c12006-08-22 20:06:22 -04002926static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002927 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928{
2929 struct nfs4_exception exception = { };
2930 int err;
2931 do {
2932 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002933 _nfs4_proc_symlink(dir, dentry, page,
2934 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 &exception);
2936 } while (exception.retry);
2937 return err;
2938}
2939
2940static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2941 struct iattr *sattr)
2942{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002943 struct nfs4_createdata *data;
2944 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002946 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2947 if (data == NULL)
2948 goto out;
2949
2950 status = nfs4_do_create(dir, dentry, data);
2951
2952 nfs4_free_createdata(data);
2953out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 return status;
2955}
2956
2957static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2958 struct iattr *sattr)
2959{
2960 struct nfs4_exception exception = { };
2961 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002962
2963 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 do {
2965 err = nfs4_handle_exception(NFS_SERVER(dir),
2966 _nfs4_proc_mkdir(dir, dentry, sattr),
2967 &exception);
2968 } while (exception.retry);
2969 return err;
2970}
2971
2972static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002973 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974{
2975 struct inode *dir = dentry->d_inode;
2976 struct nfs4_readdir_arg args = {
2977 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002978 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 .pgbase = 0,
2980 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002981 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04002982 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 };
2984 struct nfs4_readdir_res res;
2985 struct rpc_message msg = {
2986 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2987 .rpc_argp = &args,
2988 .rpc_resp = &res,
2989 .rpc_cred = cred,
2990 };
2991 int status;
2992
Harvey Harrison3110ff82008-05-02 13:42:44 -07002993 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002994 dentry->d_parent->d_name.name,
2995 dentry->d_name.name,
2996 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2998 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00002999 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 -05003000 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003002 status += args.pgbase;
3003 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003004
3005 nfs_invalidate_atime(dir);
3006
Harvey Harrison3110ff82008-05-02 13:42:44 -07003007 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 return status;
3009}
3010
3011static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003012 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013{
3014 struct nfs4_exception exception = { };
3015 int err;
3016 do {
3017 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3018 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003019 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 &exception);
3021 } while (exception.retry);
3022 return err;
3023}
3024
3025static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3026 struct iattr *sattr, dev_t rdev)
3027{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003028 struct nfs4_createdata *data;
3029 int mode = sattr->ia_mode;
3030 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
3032 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3033 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003034
3035 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3036 if (data == NULL)
3037 goto out;
3038
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003040 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003042 data->arg.ftype = NF4BLK;
3043 data->arg.u.device.specdata1 = MAJOR(rdev);
3044 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 }
3046 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003047 data->arg.ftype = NF4CHR;
3048 data->arg.u.device.specdata1 = MAJOR(rdev);
3049 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003052 status = nfs4_do_create(dir, dentry, data);
3053
3054 nfs4_free_createdata(data);
3055out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 return status;
3057}
3058
3059static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3060 struct iattr *sattr, dev_t rdev)
3061{
3062 struct nfs4_exception exception = { };
3063 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003064
3065 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 do {
3067 err = nfs4_handle_exception(NFS_SERVER(dir),
3068 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3069 &exception);
3070 } while (exception.retry);
3071 return err;
3072}
3073
3074static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3075 struct nfs_fsstat *fsstat)
3076{
3077 struct nfs4_statfs_arg args = {
3078 .fh = fhandle,
3079 .bitmask = server->attr_bitmask,
3080 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003081 struct nfs4_statfs_res res = {
3082 .fsstat = fsstat,
3083 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 struct rpc_message msg = {
3085 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3086 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003087 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 };
3089
Trond Myklebust0e574af2005-10-27 22:12:38 -04003090 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003091 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092}
3093
3094static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3095{
3096 struct nfs4_exception exception = { };
3097 int err;
3098 do {
3099 err = nfs4_handle_exception(server,
3100 _nfs4_proc_statfs(server, fhandle, fsstat),
3101 &exception);
3102 } while (exception.retry);
3103 return err;
3104}
3105
3106static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3107 struct nfs_fsinfo *fsinfo)
3108{
3109 struct nfs4_fsinfo_arg args = {
3110 .fh = fhandle,
3111 .bitmask = server->attr_bitmask,
3112 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003113 struct nfs4_fsinfo_res res = {
3114 .fsinfo = fsinfo,
3115 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 struct rpc_message msg = {
3117 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3118 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003119 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 };
3121
Bryan Schumaker7c513052011-03-24 17:12:24 +00003122 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123}
3124
3125static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3126{
3127 struct nfs4_exception exception = { };
3128 int err;
3129
3130 do {
3131 err = nfs4_handle_exception(server,
3132 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3133 &exception);
3134 } while (exception.retry);
3135 return err;
3136}
3137
3138static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3139{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003140 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3142}
3143
3144static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3145 struct nfs_pathconf *pathconf)
3146{
3147 struct nfs4_pathconf_arg args = {
3148 .fh = fhandle,
3149 .bitmask = server->attr_bitmask,
3150 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003151 struct nfs4_pathconf_res res = {
3152 .pathconf = pathconf,
3153 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 struct rpc_message msg = {
3155 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3156 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003157 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 };
3159
3160 /* None of the pathconf attributes are mandatory to implement */
3161 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3162 memset(pathconf, 0, sizeof(*pathconf));
3163 return 0;
3164 }
3165
Trond Myklebust0e574af2005-10-27 22:12:38 -04003166 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003167 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168}
3169
3170static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3171 struct nfs_pathconf *pathconf)
3172{
3173 struct nfs4_exception exception = { };
3174 int err;
3175
3176 do {
3177 err = nfs4_handle_exception(server,
3178 _nfs4_proc_pathconf(server, fhandle, pathconf),
3179 &exception);
3180 } while (exception.retry);
3181 return err;
3182}
3183
Benny Halevyd20581a2011-05-22 19:52:03 +03003184void __nfs4_read_done_cb(struct nfs_read_data *data)
3185{
3186 nfs_invalidate_atime(data->inode);
3187}
3188
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003189static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190{
Trond Myklebustec06c092006-03-20 13:44:27 -05003191 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003193 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003194 nfs_restart_rpc(task, server->nfs_client);
Trond Myklebustec06c092006-03-20 13:44:27 -05003195 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003197
Benny Halevyd20581a2011-05-22 19:52:03 +03003198 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003200 renew_lease(server, data->timestamp);
3201 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202}
3203
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003204static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3205{
3206
3207 dprintk("--> %s\n", __func__);
3208
3209 if (!nfs4_sequence_done(task, &data->res.seq_res))
3210 return -EAGAIN;
3211
Benny Halevyd20581a2011-05-22 19:52:03 +03003212 return data->read_done_cb ? data->read_done_cb(task, data) :
3213 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003214}
3215
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003216static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003219 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003220 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221}
3222
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003223/* Reset the the nfs_read_data to send the read to the MDS. */
3224void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3225{
3226 dprintk("%s Reset task for i/o through\n", __func__);
3227 put_lseg(data->lseg);
3228 data->lseg = NULL;
3229 /* offsets will differ in the dense stripe case */
3230 data->args.offset = data->mds_offset;
3231 data->ds_clp = NULL;
3232 data->args.fh = NFS_FH(data->inode);
3233 data->read_done_cb = nfs4_read_done_cb;
3234 task->tk_ops = data->mds_ops;
3235 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3236}
3237EXPORT_SYMBOL_GPL(nfs4_reset_read);
3238
Fred Isamanb029bc92011-03-03 15:13:42 +00003239static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 struct inode *inode = data->inode;
3242
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003243 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003244 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003245 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003247 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003249 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003250 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003251 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252}
3253
Fred Isamanb029bc92011-03-03 15:13:42 +00003254static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3255{
3256 if (!nfs4_sequence_done(task, &data->res.seq_res))
3257 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003258 return data->write_done_cb ? data->write_done_cb(task, data) :
3259 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003260}
3261
Fred Isamana69aef12011-03-03 15:13:47 +00003262/* Reset the the nfs_write_data to send the write to the MDS. */
3263void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3264{
3265 dprintk("%s Reset task for i/o through\n", __func__);
3266 put_lseg(data->lseg);
3267 data->lseg = NULL;
3268 data->ds_clp = NULL;
3269 data->write_done_cb = nfs4_write_done_cb;
3270 data->args.fh = NFS_FH(data->inode);
3271 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3272 data->args.offset = data->mds_offset;
3273 data->res.fattr = &data->fattr;
3274 task->tk_ops = data->mds_ops;
3275 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3276}
3277EXPORT_SYMBOL_GPL(nfs4_reset_write);
3278
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003279static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003281 struct nfs_server *server = NFS_SERVER(data->inode);
3282
Fred Isaman7ffd1062011-03-03 15:13:46 +00003283 if (data->lseg) {
3284 data->args.bitmask = NULL;
3285 data->res.fattr = NULL;
3286 } else
3287 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isamanb029bc92011-03-03 15:13:42 +00003288 if (!data->write_done_cb)
3289 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003290 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 data->timestamp = jiffies;
3292
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003293 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294}
3295
Fred Isaman5f452432011-03-23 13:27:46 +00003296static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003299
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003300 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003301 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003302 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003304 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003305 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306}
3307
Fred Isaman5f452432011-03-23 13:27:46 +00003308static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3309{
3310 if (!nfs4_sequence_done(task, &data->res.seq_res))
3311 return -EAGAIN;
3312 return data->write_done_cb(task, data);
3313}
3314
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003315static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003317 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003318
3319 if (data->lseg) {
3320 data->args.bitmask = NULL;
3321 data->res.fattr = NULL;
3322 } else
3323 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isaman5f452432011-03-23 13:27:46 +00003324 if (!data->write_done_cb)
3325 data->write_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003326 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003327 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328}
3329
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003330struct nfs4_renewdata {
3331 struct nfs_client *client;
3332 unsigned long timestamp;
3333};
3334
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335/*
3336 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3337 * standalone procedure for queueing an asynchronous RENEW.
3338 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003339static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003340{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003341 struct nfs4_renewdata *data = calldata;
3342 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003343
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003344 if (atomic_read(&clp->cl_count) > 1)
3345 nfs4_schedule_state_renewal(clp);
3346 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003347 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003348}
3349
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003350static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003352 struct nfs4_renewdata *data = calldata;
3353 struct nfs_client *clp = data->client;
3354 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355
3356 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003357 /* Unless we're shutting down, schedule state recovery! */
3358 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003359 nfs4_schedule_lease_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 return;
3361 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003362 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363}
3364
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003365static const struct rpc_call_ops nfs4_renew_ops = {
3366 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003367 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003368};
3369
David Howellsadfa6f92006-08-22 20:06:08 -04003370int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371{
3372 struct rpc_message msg = {
3373 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3374 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003375 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003377 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003379 if (!atomic_inc_not_zero(&clp->cl_count))
3380 return -EIO;
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003381 data = kmalloc(sizeof(*data), GFP_KERNEL);
3382 if (data == NULL)
3383 return -ENOMEM;
3384 data->client = clp;
3385 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003387 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388}
3389
David Howellsadfa6f92006-08-22 20:06:08 -04003390int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391{
3392 struct rpc_message msg = {
3393 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3394 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003395 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 };
3397 unsigned long now = jiffies;
3398 int status;
3399
3400 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3401 if (status < 0)
3402 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003403 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 return 0;
3405}
3406
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003407static inline int nfs4_server_supports_acls(struct nfs_server *server)
3408{
3409 return (server->caps & NFS_CAP_ACLS)
3410 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3411 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3412}
3413
3414/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3415 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3416 * the stack.
3417 */
3418#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3419
3420static void buf_to_pages(const void *buf, size_t buflen,
3421 struct page **pages, unsigned int *pgbase)
3422{
3423 const void *p = buf;
3424
3425 *pgbase = offset_in_page(buf);
3426 p -= *pgbase;
3427 while (p < buf + buflen) {
3428 *(pages++) = virt_to_page(p);
3429 p += PAGE_CACHE_SIZE;
3430 }
3431}
3432
Neil Hormane9e3d722011-03-04 19:26:03 -05003433static int buf_to_pages_noslab(const void *buf, size_t buflen,
3434 struct page **pages, unsigned int *pgbase)
3435{
3436 struct page *newpage, **spages;
3437 int rc = 0;
3438 size_t len;
3439 spages = pages;
3440
3441 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003442 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003443 newpage = alloc_page(GFP_KERNEL);
3444
3445 if (newpage == NULL)
3446 goto unwind;
3447 memcpy(page_address(newpage), buf, len);
3448 buf += len;
3449 buflen -= len;
3450 *pages++ = newpage;
3451 rc++;
3452 } while (buflen != 0);
3453
3454 return rc;
3455
3456unwind:
3457 for(; rc > 0; rc--)
3458 __free_page(spages[rc-1]);
3459 return -ENOMEM;
3460}
3461
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003462struct nfs4_cached_acl {
3463 int cached;
3464 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003465 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003466};
3467
3468static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003469{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003470 struct nfs_inode *nfsi = NFS_I(inode);
3471
3472 spin_lock(&inode->i_lock);
3473 kfree(nfsi->nfs4_acl);
3474 nfsi->nfs4_acl = acl;
3475 spin_unlock(&inode->i_lock);
3476}
3477
3478static void nfs4_zap_acl_attr(struct inode *inode)
3479{
3480 nfs4_set_cached_acl(inode, NULL);
3481}
3482
3483static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3484{
3485 struct nfs_inode *nfsi = NFS_I(inode);
3486 struct nfs4_cached_acl *acl;
3487 int ret = -ENOENT;
3488
3489 spin_lock(&inode->i_lock);
3490 acl = nfsi->nfs4_acl;
3491 if (acl == NULL)
3492 goto out;
3493 if (buf == NULL) /* user is just asking for length */
3494 goto out_len;
3495 if (acl->cached == 0)
3496 goto out;
3497 ret = -ERANGE; /* see getxattr(2) man page */
3498 if (acl->len > buflen)
3499 goto out;
3500 memcpy(buf, acl->data, acl->len);
3501out_len:
3502 ret = acl->len;
3503out:
3504 spin_unlock(&inode->i_lock);
3505 return ret;
3506}
3507
3508static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3509{
3510 struct nfs4_cached_acl *acl;
3511
3512 if (buf && acl_len <= PAGE_SIZE) {
3513 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3514 if (acl == NULL)
3515 goto out;
3516 acl->cached = 1;
3517 memcpy(acl->data, buf, acl_len);
3518 } else {
3519 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3520 if (acl == NULL)
3521 goto out;
3522 acl->cached = 0;
3523 }
3524 acl->len = acl_len;
3525out:
3526 nfs4_set_cached_acl(inode, acl);
3527}
3528
Trond Myklebust16b42892006-08-24 12:27:15 -04003529static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003530{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003531 struct page *pages[NFS4ACL_MAXPAGES];
3532 struct nfs_getaclargs args = {
3533 .fh = NFS_FH(inode),
3534 .acl_pages = pages,
3535 .acl_len = buflen,
3536 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003537 struct nfs_getaclres res = {
3538 .acl_len = buflen,
3539 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003540 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003541 struct rpc_message msg = {
3542 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3543 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003544 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003545 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003546 struct page *localpage = NULL;
3547 int ret;
3548
3549 if (buflen < PAGE_SIZE) {
3550 /* As long as we're doing a round trip to the server anyway,
3551 * let's be prepared for a page of acl data. */
3552 localpage = alloc_page(GFP_KERNEL);
3553 resp_buf = page_address(localpage);
3554 if (localpage == NULL)
3555 return -ENOMEM;
3556 args.acl_pages[0] = localpage;
3557 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003558 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003559 } else {
3560 resp_buf = buf;
3561 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3562 }
Bryan Schumaker7c513052011-03-24 17:12:24 +00003563 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 +00003564 if (ret)
3565 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003566 if (res.acl_len > args.acl_len)
3567 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003568 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003569 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003570 if (buf) {
3571 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003572 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003573 goto out_free;
3574 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003575 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003576 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003577 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003578out_free:
3579 if (localpage)
3580 __free_page(localpage);
3581 return ret;
3582}
3583
Trond Myklebust16b42892006-08-24 12:27:15 -04003584static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3585{
3586 struct nfs4_exception exception = { };
3587 ssize_t ret;
3588 do {
3589 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3590 if (ret >= 0)
3591 break;
3592 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3593 } while (exception.retry);
3594 return ret;
3595}
3596
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003597static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3598{
3599 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003600 int ret;
3601
3602 if (!nfs4_server_supports_acls(server))
3603 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003604 ret = nfs_revalidate_inode(server, inode);
3605 if (ret < 0)
3606 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003607 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3608 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003609 ret = nfs4_read_cached_acl(inode, buf, buflen);
3610 if (ret != -ENOENT)
3611 return ret;
3612 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003613}
3614
Trond Myklebust16b42892006-08-24 12:27:15 -04003615static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003616{
3617 struct nfs_server *server = NFS_SERVER(inode);
3618 struct page *pages[NFS4ACL_MAXPAGES];
3619 struct nfs_setaclargs arg = {
3620 .fh = NFS_FH(inode),
3621 .acl_pages = pages,
3622 .acl_len = buflen,
3623 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003624 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003625 struct rpc_message msg = {
3626 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3627 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003628 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003629 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003630 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003631
3632 if (!nfs4_server_supports_acls(server))
3633 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003634 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3635 if (i < 0)
3636 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003637 nfs_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003638 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003639
3640 /*
3641 * Free each page after tx, so the only ref left is
3642 * held by the network stack
3643 */
3644 for (; i > 0; i--)
3645 put_page(pages[i-1]);
3646
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003647 /*
3648 * Acl update can result in inode attribute update.
3649 * so mark the attribute cache invalid.
3650 */
3651 spin_lock(&inode->i_lock);
3652 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3653 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003654 nfs_access_zap_cache(inode);
3655 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003656 return ret;
3657}
3658
Trond Myklebust16b42892006-08-24 12:27:15 -04003659static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3660{
3661 struct nfs4_exception exception = { };
3662 int err;
3663 do {
3664 err = nfs4_handle_exception(NFS_SERVER(inode),
3665 __nfs4_proc_set_acl(inode, buf, buflen),
3666 &exception);
3667 } while (exception.retry);
3668 return err;
3669}
3670
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003672nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003674 struct nfs_client *clp = server->nfs_client;
3675
3676 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 return 0;
3678 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003679 case -NFS4ERR_ADMIN_REVOKED:
3680 case -NFS4ERR_BAD_STATEID:
3681 case -NFS4ERR_OPENMODE:
3682 if (state == NULL)
3683 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003684 nfs4_schedule_stateid_recovery(server, state);
3685 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003686 case -NFS4ERR_EXPIRED:
3687 if (state != NULL)
3688 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003690 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003691 nfs4_schedule_lease_recovery(clp);
3692 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003693#if defined(CONFIG_NFS_V4_1)
3694 case -NFS4ERR_BADSESSION:
3695 case -NFS4ERR_BADSLOT:
3696 case -NFS4ERR_BAD_HIGH_SLOT:
3697 case -NFS4ERR_DEADSESSION:
3698 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3699 case -NFS4ERR_SEQ_FALSE_RETRY:
3700 case -NFS4ERR_SEQ_MISORDERED:
3701 dprintk("%s ERROR %d, Reset session\n", __func__,
3702 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003703 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -04003704 task->tk_status = 0;
3705 return -EAGAIN;
3706#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003708 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003709 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003710 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3712 task->tk_status = 0;
3713 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003714 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 case -NFS4ERR_OLD_STATEID:
3716 task->tk_status = 0;
3717 return -EAGAIN;
3718 }
3719 task->tk_status = nfs4_map_errors(task->tk_status);
3720 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003721wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003722 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003723 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3724 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3725 task->tk_status = 0;
3726 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727}
3728
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003729int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3730 unsigned short port, struct rpc_cred *cred,
3731 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732{
3733 nfs4_verifier sc_verifier;
3734 struct nfs4_setclientid setclientid = {
3735 .sc_verifier = &sc_verifier,
3736 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003737 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 };
3739 struct rpc_message msg = {
3740 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3741 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003742 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003743 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 };
Al Virobc4785c2006-10-19 23:28:51 -07003745 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 int loop = 0;
3747 int status;
3748
Al Virobc4785c2006-10-19 23:28:51 -07003749 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3751 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3752
3753 for(;;) {
3754 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003755 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003756 clp->cl_ipaddr,
3757 rpc_peeraddr2str(clp->cl_rpcclient,
3758 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003759 rpc_peeraddr2str(clp->cl_rpcclient,
3760 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003761 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 clp->cl_id_uniquifier);
3763 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003764 sizeof(setclientid.sc_netid),
3765 rpc_peeraddr2str(clp->cl_rpcclient,
3766 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003768 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 clp->cl_ipaddr, port >> 8, port & 255);
3770
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003771 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 if (status != -NFS4ERR_CLID_INUSE)
3773 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003774 if (loop != 0) {
3775 ++clp->cl_id_uniquifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003777 }
3778 ++loop;
3779 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 }
3781 return status;
3782}
3783
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003784int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003785 struct nfs4_setclientid_res *arg,
3786 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787{
3788 struct nfs_fsinfo fsinfo;
3789 struct rpc_message msg = {
3790 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003791 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003793 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 };
3795 unsigned long now;
3796 int status;
3797
3798 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003799 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 if (status == 0) {
3801 spin_lock(&clp->cl_lock);
3802 clp->cl_lease_time = fsinfo.lease_time * HZ;
3803 clp->cl_last_renewal = now;
3804 spin_unlock(&clp->cl_lock);
3805 }
3806 return status;
3807}
3808
Trond Myklebustfe650402006-01-03 09:55:18 +01003809struct nfs4_delegreturndata {
3810 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003811 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003812 struct nfs_fh fh;
3813 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003814 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003815 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003816 int rpc_status;
3817};
3818
Trond Myklebustfe650402006-01-03 09:55:18 +01003819static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3820{
3821 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003822
Trond Myklebust14516c32010-07-31 14:29:06 -04003823 if (!nfs4_sequence_done(task, &data->res.seq_res))
3824 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003825
Ricardo Labiaga79708862009-12-07 09:23:21 -05003826 switch (task->tk_status) {
3827 case -NFS4ERR_STALE_STATEID:
3828 case -NFS4ERR_EXPIRED:
3829 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003830 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003831 break;
3832 default:
3833 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3834 -EAGAIN) {
3835 nfs_restart_rpc(task, data->res.server->nfs_client);
3836 return;
3837 }
3838 }
3839 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003840}
3841
3842static void nfs4_delegreturn_release(void *calldata)
3843{
Trond Myklebustfe650402006-01-03 09:55:18 +01003844 kfree(calldata);
3845}
3846
Andy Adamson938e1012009-04-01 09:22:28 -04003847#if defined(CONFIG_NFS_V4_1)
3848static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3849{
3850 struct nfs4_delegreturndata *d_data;
3851
3852 d_data = (struct nfs4_delegreturndata *)data;
3853
Trond Myklebust035168ab2010-06-16 09:52:26 -04003854 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003855 &d_data->args.seq_args,
3856 &d_data->res.seq_res, 1, task))
3857 return;
3858 rpc_call_start(task);
3859}
3860#endif /* CONFIG_NFS_V4_1 */
3861
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003862static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003863#if defined(CONFIG_NFS_V4_1)
3864 .rpc_call_prepare = nfs4_delegreturn_prepare,
3865#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003866 .rpc_call_done = nfs4_delegreturn_done,
3867 .rpc_release = nfs4_delegreturn_release,
3868};
3869
Trond Myklebuste6f81072008-01-24 18:14:34 -05003870static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003871{
3872 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003873 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003874 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003875 struct rpc_message msg = {
3876 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3877 .rpc_cred = cred,
3878 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003879 struct rpc_task_setup task_setup_data = {
3880 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003881 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003882 .callback_ops = &nfs4_delegreturn_ops,
3883 .flags = RPC_TASK_ASYNC,
3884 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003885 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003886
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003887 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003888 if (data == NULL)
3889 return -ENOMEM;
3890 data->args.fhandle = &data->fh;
3891 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003892 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003893 nfs_copy_fh(&data->fh, NFS_FH(inode));
3894 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003895 data->res.fattr = &data->fattr;
3896 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003897 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003898 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003899 data->rpc_status = 0;
3900
Trond Myklebustc970aa82007-07-14 15:39:59 -04003901 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05003902 msg.rpc_argp = &data->args;
3903 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003904 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003905 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003906 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003907 if (!issync)
3908 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003909 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003910 if (status != 0)
3911 goto out;
3912 status = data->rpc_status;
3913 if (status != 0)
3914 goto out;
3915 nfs_refresh_inode(inode, &data->fattr);
3916out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003917 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003918 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919}
3920
Trond Myklebuste6f81072008-01-24 18:14:34 -05003921int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922{
3923 struct nfs_server *server = NFS_SERVER(inode);
3924 struct nfs4_exception exception = { };
3925 int err;
3926 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003927 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 switch (err) {
3929 case -NFS4ERR_STALE_STATEID:
3930 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 case 0:
3932 return 0;
3933 }
3934 err = nfs4_handle_exception(server, err, &exception);
3935 } while (exception.retry);
3936 return err;
3937}
3938
3939#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3940#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3941
3942/*
3943 * sleep, with exponential backoff, and retry the LOCK operation.
3944 */
3945static unsigned long
3946nfs4_set_lock_task_retry(unsigned long timeout)
3947{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003948 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 timeout <<= 1;
3950 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3951 return NFS4_LOCK_MAXTIMEOUT;
3952 return timeout;
3953}
3954
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3956{
3957 struct inode *inode = state->inode;
3958 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003959 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003960 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003962 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003964 struct nfs_lockt_res res = {
3965 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 };
3967 struct rpc_message msg = {
3968 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3969 .rpc_argp = &arg,
3970 .rpc_resp = &res,
3971 .rpc_cred = state->owner->so_cred,
3972 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 struct nfs4_lock_state *lsp;
3974 int status;
3975
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003976 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003977 status = nfs4_set_lock_state(state, request);
3978 if (status != 0)
3979 goto out;
3980 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003981 arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05003982 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003983 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003984 switch (status) {
3985 case 0:
3986 request->fl_type = F_UNLCK;
3987 break;
3988 case -NFS4ERR_DENIED:
3989 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003991 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003992out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 return status;
3994}
3995
3996static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3997{
3998 struct nfs4_exception exception = { };
3999 int err;
4000
4001 do {
4002 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4003 _nfs4_proc_getlk(state, cmd, request),
4004 &exception);
4005 } while (exception.retry);
4006 return err;
4007}
4008
4009static int do_vfs_lock(struct file *file, struct file_lock *fl)
4010{
4011 int res = 0;
4012 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4013 case FL_POSIX:
4014 res = posix_lock_file_wait(file, fl);
4015 break;
4016 case FL_FLOCK:
4017 res = flock_lock_file_wait(file, fl);
4018 break;
4019 default:
4020 BUG();
4021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 return res;
4023}
4024
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004025struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004026 struct nfs_locku_args arg;
4027 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004028 struct nfs4_lock_state *lsp;
4029 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004030 struct file_lock fl;
4031 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004032 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004033};
4034
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004035static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4036 struct nfs_open_context *ctx,
4037 struct nfs4_lock_state *lsp,
4038 struct nfs_seqid *seqid)
4039{
4040 struct nfs4_unlockdata *p;
4041 struct inode *inode = lsp->ls_state->inode;
4042
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004043 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004044 if (p == NULL)
4045 return NULL;
4046 p->arg.fh = NFS_FH(inode);
4047 p->arg.fl = &p->fl;
4048 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004049 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004050 p->arg.stateid = &lsp->ls_stateid;
4051 p->lsp = lsp;
4052 atomic_inc(&lsp->ls_count);
4053 /* Ensure we don't close file until we're done freeing locks! */
4054 p->ctx = get_nfs_open_context(ctx);
4055 memcpy(&p->fl, fl, sizeof(p->fl));
4056 p->server = NFS_SERVER(inode);
4057 return p;
4058}
4059
Trond Myklebust06f814a2006-01-03 09:55:07 +01004060static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004061{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004062 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004063 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004064 nfs4_put_lock_state(calldata->lsp);
4065 put_nfs_open_context(calldata->ctx);
4066 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004067}
4068
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004069static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004070{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004071 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004072
Trond Myklebust14516c32010-07-31 14:29:06 -04004073 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4074 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004075 switch (task->tk_status) {
4076 case 0:
4077 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004078 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004079 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01004080 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004081 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004082 case -NFS4ERR_BAD_STATEID:
4083 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004084 case -NFS4ERR_STALE_STATEID:
4085 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004086 break;
4087 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004088 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebust0110ee12009-12-07 09:00:24 -05004089 nfs_restart_rpc(task,
Trond Myklebustd61e6122009-12-05 19:32:19 -05004090 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004091 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004092}
4093
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004094static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004095{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004096 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004098 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004099 return;
4100 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004101 /* Note: exit _without_ running nfs4_locku_done */
4102 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004103 return;
4104 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004105 calldata->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04004106 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004107 &calldata->arg.seq_args,
4108 &calldata->res.seq_res, 1, task))
4109 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004110 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111}
4112
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004113static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004114 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004115 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004116 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004117};
4118
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004119static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4120 struct nfs_open_context *ctx,
4121 struct nfs4_lock_state *lsp,
4122 struct nfs_seqid *seqid)
4123{
4124 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004125 struct rpc_message msg = {
4126 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4127 .rpc_cred = ctx->cred,
4128 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004129 struct rpc_task_setup task_setup_data = {
4130 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004131 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004132 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004133 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004134 .flags = RPC_TASK_ASYNC,
4135 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004136
Frank Filz137d6ac2007-07-09 15:32:29 -07004137 /* Ensure this is an unlock - when canceling a lock, the
4138 * canceled lock is passed in, and it won't be an unlock.
4139 */
4140 fl->fl_type = F_UNLCK;
4141
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004142 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4143 if (data == NULL) {
4144 nfs_free_seqid(seqid);
4145 return ERR_PTR(-ENOMEM);
4146 }
4147
Trond Myklebust1174dd12010-12-21 10:52:24 -05004148 msg.rpc_argp = &data->arg;
4149 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004150 task_setup_data.callback_data = data;
4151 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004152}
4153
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4155{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004156 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004157 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004158 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004159 struct rpc_task *task;
4160 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004161 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162
Trond Myklebust9b073572006-06-29 16:38:34 -04004163 status = nfs4_set_lock_state(state, request);
4164 /* Unlock _before_ we do the RPC call */
4165 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004166 down_read(&nfsi->rwsem);
4167 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4168 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004169 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004170 }
4171 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004172 if (status != 0)
4173 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004174 /* Is this a delegated lock? */
4175 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004176 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004177 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004178 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004179 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004180 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004181 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004182 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004183 status = PTR_ERR(task);
4184 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004185 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004186 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004187 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004188out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004189 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004190 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191}
4192
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004193struct nfs4_lockdata {
4194 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004195 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004196 struct nfs4_lock_state *lsp;
4197 struct nfs_open_context *ctx;
4198 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004199 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004200 int rpc_status;
4201 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004202 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004203};
4204
4205static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004206 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4207 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004208{
4209 struct nfs4_lockdata *p;
4210 struct inode *inode = lsp->ls_state->inode;
4211 struct nfs_server *server = NFS_SERVER(inode);
4212
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004213 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004214 if (p == NULL)
4215 return NULL;
4216
4217 p->arg.fh = NFS_FH(inode);
4218 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004219 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004220 if (p->arg.open_seqid == NULL)
4221 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004222 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004223 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004224 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004225 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004226 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004227 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004228 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004229 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004230 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004231 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004232 atomic_inc(&lsp->ls_count);
4233 p->ctx = get_nfs_open_context(ctx);
4234 memcpy(&p->fl, fl, sizeof(p->fl));
4235 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004236out_free_seqid:
4237 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004238out_free:
4239 kfree(p);
4240 return NULL;
4241}
4242
4243static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4244{
4245 struct nfs4_lockdata *data = calldata;
4246 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247
Harvey Harrison3110ff82008-05-02 13:42:44 -07004248 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004249 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4250 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004251 /* Do we need to do an open_to_lock_owner? */
4252 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004253 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4254 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004255 data->arg.open_stateid = &state->stateid;
4256 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004257 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004258 } else
4259 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004260 data->timestamp = jiffies;
Trond Myklebust035168ab2010-06-16 09:52:26 -04004261 if (nfs4_setup_sequence(data->server,
4262 &data->arg.seq_args,
Andy Adamson66179ef2009-04-01 09:22:22 -04004263 &data->res.seq_res, 1, task))
4264 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004265 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004266 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004267}
4268
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004269static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4270{
4271 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4272 nfs4_lock_prepare(task, calldata);
4273}
4274
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004275static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4276{
4277 struct nfs4_lockdata *data = calldata;
4278
Harvey Harrison3110ff82008-05-02 13:42:44 -07004279 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004280
Trond Myklebust14516c32010-07-31 14:29:06 -04004281 if (!nfs4_sequence_done(task, &data->res.seq_res))
4282 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004283
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004284 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004285 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004286 if (data->rpc_status == 0)
4287 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4288 else
4289 goto out;
4290 }
4291 if (data->rpc_status == 0) {
4292 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4293 sizeof(data->lsp->ls_stateid.data));
4294 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Al Viro3d4ff432011-06-22 18:40:12 -04004295 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004296 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004297out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004298 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004299}
4300
4301static void nfs4_lock_release(void *calldata)
4302{
4303 struct nfs4_lockdata *data = calldata;
4304
Harvey Harrison3110ff82008-05-02 13:42:44 -07004305 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004306 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004307 if (data->cancelled != 0) {
4308 struct rpc_task *task;
4309 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4310 data->arg.lock_seqid);
4311 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004312 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004313 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004314 } else
4315 nfs_free_seqid(data->arg.lock_seqid);
4316 nfs4_put_lock_state(data->lsp);
4317 put_nfs_open_context(data->ctx);
4318 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004319 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004320}
4321
4322static const struct rpc_call_ops nfs4_lock_ops = {
4323 .rpc_call_prepare = nfs4_lock_prepare,
4324 .rpc_call_done = nfs4_lock_done,
4325 .rpc_release = nfs4_lock_release,
4326};
4327
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004328static const struct rpc_call_ops nfs4_recover_lock_ops = {
4329 .rpc_call_prepare = nfs4_recover_lock_prepare,
4330 .rpc_call_done = nfs4_lock_done,
4331 .rpc_release = nfs4_lock_release,
4332};
4333
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004334static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4335{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004336 switch (error) {
4337 case -NFS4ERR_ADMIN_REVOKED:
4338 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004339 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004340 if (new_lock_owner != 0 ||
4341 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004342 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004343 break;
4344 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004345 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004346 case -NFS4ERR_EXPIRED:
4347 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004348 };
4349}
4350
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004351static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004352{
4353 struct nfs4_lockdata *data;
4354 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004355 struct rpc_message msg = {
4356 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4357 .rpc_cred = state->owner->so_cred,
4358 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004359 struct rpc_task_setup task_setup_data = {
4360 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004361 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004362 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004363 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004364 .flags = RPC_TASK_ASYNC,
4365 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004366 int ret;
4367
Harvey Harrison3110ff82008-05-02 13:42:44 -07004368 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004369 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004370 fl->fl_u.nfs4_fl.owner,
4371 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004372 if (data == NULL)
4373 return -ENOMEM;
4374 if (IS_SETLKW(cmd))
4375 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004376 if (recovery_type > NFS_LOCK_NEW) {
4377 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004378 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004379 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4380 }
Trond Myklebust1174dd12010-12-21 10:52:24 -05004381 msg.rpc_argp = &data->arg;
4382 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004383 task_setup_data.callback_data = data;
4384 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004385 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004386 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004387 ret = nfs4_wait_for_completion_rpc_task(task);
4388 if (ret == 0) {
4389 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004390 if (ret)
4391 nfs4_handle_setlk_error(data->server, data->lsp,
4392 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004393 } else
4394 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004395 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004396 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004397 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398}
4399
4400static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4401{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004402 struct nfs_server *server = NFS_SERVER(state->inode);
4403 struct nfs4_exception exception = { };
4404 int err;
4405
4406 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004407 /* Cache the lock if possible... */
4408 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4409 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004410 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004411 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004412 break;
4413 nfs4_handle_exception(server, err, &exception);
4414 } while (exception.retry);
4415 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416}
4417
4418static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4419{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004420 struct nfs_server *server = NFS_SERVER(state->inode);
4421 struct nfs4_exception exception = { };
4422 int err;
4423
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004424 err = nfs4_set_lock_state(state, request);
4425 if (err != 0)
4426 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004427 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004428 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4429 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004430 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004431 switch (err) {
4432 default:
4433 goto out;
4434 case -NFS4ERR_GRACE:
4435 case -NFS4ERR_DELAY:
4436 nfs4_handle_exception(server, err, &exception);
4437 err = 0;
4438 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004439 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004440out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004441 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442}
4443
4444static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4445{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004446 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004447 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004448 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004450 if ((fl_flags & FL_POSIX) &&
4451 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4452 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004453 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004454 status = nfs4_set_lock_state(state, request);
4455 if (status != 0)
4456 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004457 request->fl_flags |= FL_ACCESS;
4458 status = do_vfs_lock(request->fl_file, request);
4459 if (status < 0)
4460 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004461 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004462 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004463 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004464 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004465 request->fl_flags = fl_flags & ~FL_SLEEP;
4466 status = do_vfs_lock(request->fl_file, request);
4467 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004468 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004469 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004470 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004471 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004472 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004473 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004474 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004475 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004476out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004477 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004478out:
4479 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 return status;
4481}
4482
4483static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4484{
4485 struct nfs4_exception exception = { };
4486 int err;
4487
4488 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004489 err = _nfs4_proc_setlk(state, cmd, request);
4490 if (err == -NFS4ERR_DENIED)
4491 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004493 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 } while (exception.retry);
4495 return err;
4496}
4497
4498static int
4499nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4500{
4501 struct nfs_open_context *ctx;
4502 struct nfs4_state *state;
4503 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4504 int status;
4505
4506 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004507 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 state = ctx->state;
4509
4510 if (request->fl_start < 0 || request->fl_end < 0)
4511 return -EINVAL;
4512
Trond Myklebustd9531262009-07-21 19:22:38 -04004513 if (IS_GETLK(cmd)) {
4514 if (state != NULL)
4515 return nfs4_proc_getlk(state, F_GETLK, request);
4516 return 0;
4517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518
4519 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4520 return -EINVAL;
4521
Trond Myklebustd9531262009-07-21 19:22:38 -04004522 if (request->fl_type == F_UNLCK) {
4523 if (state != NULL)
4524 return nfs4_proc_unlck(state, cmd, request);
4525 return 0;
4526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527
Trond Myklebustd9531262009-07-21 19:22:38 -04004528 if (state == NULL)
4529 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 do {
4531 status = nfs4_proc_setlk(state, cmd, request);
4532 if ((status != -EAGAIN) || IS_SETLK(cmd))
4533 break;
4534 timeout = nfs4_set_lock_task_retry(timeout);
4535 status = -ERESTARTSYS;
4536 if (signalled())
4537 break;
4538 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 return status;
4540}
4541
Trond Myklebust888e6942005-11-04 15:38:11 -05004542int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4543{
4544 struct nfs_server *server = NFS_SERVER(state->inode);
4545 struct nfs4_exception exception = { };
4546 int err;
4547
4548 err = nfs4_set_lock_state(state, fl);
4549 if (err != 0)
4550 goto out;
4551 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004552 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004553 switch (err) {
4554 default:
4555 printk(KERN_ERR "%s: unhandled error %d.\n",
4556 __func__, err);
4557 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004558 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004559 goto out;
4560 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004561 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004562 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004563 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004564 nfs4_schedule_lease_recovery(server->nfs_client);
4565 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004566 case -NFS4ERR_BADSESSION:
4567 case -NFS4ERR_BADSLOT:
4568 case -NFS4ERR_BAD_HIGH_SLOT:
4569 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4570 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004571 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004572 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004573 case -ERESTARTSYS:
4574 /*
4575 * The show must go on: exit, but mark the
4576 * stateid as needing recovery.
4577 */
4578 case -NFS4ERR_ADMIN_REVOKED:
4579 case -NFS4ERR_BAD_STATEID:
4580 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004581 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004582 err = 0;
4583 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004584 case -EKEYEXPIRED:
4585 /*
4586 * User RPCSEC_GSS context has expired.
4587 * We cannot recover this stateid now, so
4588 * skip it and allow recovery thread to
4589 * proceed.
4590 */
4591 err = 0;
4592 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004593 case -ENOMEM:
4594 case -NFS4ERR_DENIED:
4595 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4596 err = 0;
4597 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004598 case -NFS4ERR_DELAY:
4599 break;
4600 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004601 err = nfs4_handle_exception(server, err, &exception);
4602 } while (exception.retry);
4603out:
4604 return err;
4605}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004606
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004607static void nfs4_release_lockowner_release(void *calldata)
4608{
4609 kfree(calldata);
4610}
4611
4612const struct rpc_call_ops nfs4_release_lockowner_ops = {
4613 .rpc_release = nfs4_release_lockowner_release,
4614};
4615
4616void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4617{
4618 struct nfs_server *server = lsp->ls_state->owner->so_server;
4619 struct nfs_release_lockowner_args *args;
4620 struct rpc_message msg = {
4621 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4622 };
4623
4624 if (server->nfs_client->cl_mvops->minor_version != 0)
4625 return;
4626 args = kmalloc(sizeof(*args), GFP_NOFS);
4627 if (!args)
4628 return;
4629 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4630 args->lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004631 args->lock_owner.s_dev = server->s_dev;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004632 msg.rpc_argp = args;
4633 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4634}
4635
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004636#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4637
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004638static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4639 const void *buf, size_t buflen,
4640 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004641{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004642 if (strcmp(key, "") != 0)
4643 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004644
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004645 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004646}
4647
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004648static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4649 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004650{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004651 if (strcmp(key, "") != 0)
4652 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004653
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004654 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004655}
4656
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004657static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4658 size_t list_len, const char *name,
4659 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004660{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004661 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004662
J. Bruce Fields096455a2006-03-20 23:23:42 -05004663 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4664 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004665
4666 if (list && len <= list_len)
4667 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004668 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004669}
4670
Andy Adamson533eb462011-06-13 18:25:56 -04004671/*
4672 * nfs_fhget will use either the mounted_on_fileid or the fileid
4673 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004674static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4675{
Andy Adamson533eb462011-06-13 18:25:56 -04004676 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4677 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4678 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4679 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004680 return;
4681
4682 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4683 NFS_ATTR_FATTR_NLINK;
4684 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4685 fattr->nlink = 2;
4686}
4687
Trond Myklebust56659e92007-07-17 21:52:39 -04004688int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004689 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004690{
4691 struct nfs_server *server = NFS_SERVER(dir);
4692 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004693 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004694 };
4695 struct nfs4_fs_locations_arg args = {
4696 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004697 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004698 .page = page,
4699 .bitmask = bitmask,
4700 };
Benny Halevy22958462009-04-01 09:22:02 -04004701 struct nfs4_fs_locations_res res = {
4702 .fs_locations = fs_locations,
4703 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004704 struct rpc_message msg = {
4705 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4706 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004707 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004708 };
4709 int status;
4710
Harvey Harrison3110ff82008-05-02 13:42:44 -07004711 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04004712
4713 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
4714 * is not supported */
4715 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
4716 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
4717 else
4718 bitmask[0] |= FATTR4_WORD0_FILEID;
4719
Trond Myklebustc228fd32007-01-13 02:28:11 -05004720 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004721 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004722 fs_locations->nlocations = 0;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004723 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004724 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004725 return status;
4726}
4727
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00004728static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4729{
4730 int status;
4731 struct nfs4_secinfo_arg args = {
4732 .dir_fh = NFS_FH(dir),
4733 .name = name,
4734 };
4735 struct nfs4_secinfo_res res = {
4736 .flavors = flavors,
4737 };
4738 struct rpc_message msg = {
4739 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
4740 .rpc_argp = &args,
4741 .rpc_resp = &res,
4742 };
4743
4744 dprintk("NFS call secinfo %s\n", name->name);
4745 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4746 dprintk("NFS reply secinfo: %d\n", status);
4747 return status;
4748}
4749
4750int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4751{
4752 struct nfs4_exception exception = { };
4753 int err;
4754 do {
4755 err = nfs4_handle_exception(NFS_SERVER(dir),
4756 _nfs4_proc_secinfo(dir, name, flavors),
4757 &exception);
4758 } while (exception.retry);
4759 return err;
4760}
4761
Andy Adamson557134a2009-04-01 09:21:53 -04004762#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004763/*
Andy Adamson357f54d2010-12-14 10:11:57 -05004764 * Check the exchange flags returned by the server for invalid flags, having
4765 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4766 * DS flags set.
4767 */
4768static int nfs4_check_cl_exchange_flags(u32 flags)
4769{
4770 if (flags & ~EXCHGID4_FLAG_MASK_R)
4771 goto out_inval;
4772 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4773 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4774 goto out_inval;
4775 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4776 goto out_inval;
4777 return NFS_OK;
4778out_inval:
4779 return -NFS4ERR_INVAL;
4780}
4781
4782/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04004783 * nfs4_proc_exchange_id()
4784 *
4785 * Since the clientid has expired, all compounds using sessions
4786 * associated with the stale clientid will be returning
4787 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4788 * be in some phase of session reset.
4789 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004790int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004791{
4792 nfs4_verifier verifier;
4793 struct nfs41_exchange_id_args args = {
4794 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05004795 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04004796 };
4797 struct nfs41_exchange_id_res res = {
4798 .client = clp,
4799 };
4800 int status;
4801 struct rpc_message msg = {
4802 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4803 .rpc_argp = &args,
4804 .rpc_resp = &res,
4805 .rpc_cred = cred,
4806 };
4807 __be32 *p;
4808
4809 dprintk("--> %s\n", __func__);
4810 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004811
Benny Halevy99fe60d2009-04-01 09:22:29 -04004812 p = (u32 *)verifier.data;
4813 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4814 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4815 args.verifier = &verifier;
4816
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004817 args.id_len = scnprintf(args.id, sizeof(args.id),
4818 "%s/%s.%s/%u",
4819 clp->cl_ipaddr,
4820 init_utsname()->nodename,
4821 init_utsname()->domainname,
4822 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004823
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004824 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004825 if (!status)
4826 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004827 dprintk("<-- %s status= %d\n", __func__, status);
4828 return status;
4829}
4830
Andy Adamson2050f0c2009-04-01 09:22:30 -04004831struct nfs4_get_lease_time_data {
4832 struct nfs4_get_lease_time_args *args;
4833 struct nfs4_get_lease_time_res *res;
4834 struct nfs_client *clp;
4835};
4836
4837static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4838 void *calldata)
4839{
4840 int ret;
4841 struct nfs4_get_lease_time_data *data =
4842 (struct nfs4_get_lease_time_data *)calldata;
4843
4844 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004845 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004846 /* just setup sequence, do not trigger session recovery
4847 since we're invoked within one */
4848 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004849 &data->args->la_seq_args,
4850 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004851
4852 BUG_ON(ret == -EAGAIN);
4853 rpc_call_start(task);
4854 dprintk("<-- %s\n", __func__);
4855}
4856
4857/*
4858 * Called from nfs4_state_manager thread for session setup, so don't recover
4859 * from sequence operation or clientid errors.
4860 */
4861static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4862{
4863 struct nfs4_get_lease_time_data *data =
4864 (struct nfs4_get_lease_time_data *)calldata;
4865
4866 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04004867 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4868 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04004869 switch (task->tk_status) {
4870 case -NFS4ERR_DELAY:
4871 case -NFS4ERR_GRACE:
4872 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4873 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4874 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004875 /* fall through */
4876 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust0110ee12009-12-07 09:00:24 -05004877 nfs_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004878 return;
4879 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004880 dprintk("<-- %s\n", __func__);
4881}
4882
4883struct rpc_call_ops nfs4_get_lease_time_ops = {
4884 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4885 .rpc_call_done = nfs4_get_lease_time_done,
4886};
4887
4888int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4889{
4890 struct rpc_task *task;
4891 struct nfs4_get_lease_time_args args;
4892 struct nfs4_get_lease_time_res res = {
4893 .lr_fsinfo = fsinfo,
4894 };
4895 struct nfs4_get_lease_time_data data = {
4896 .args = &args,
4897 .res = &res,
4898 .clp = clp,
4899 };
4900 struct rpc_message msg = {
4901 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4902 .rpc_argp = &args,
4903 .rpc_resp = &res,
4904 };
4905 struct rpc_task_setup task_setup = {
4906 .rpc_client = clp->cl_rpcclient,
4907 .rpc_message = &msg,
4908 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004909 .callback_data = &data,
4910 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04004911 };
4912 int status;
4913
Andy Adamson2050f0c2009-04-01 09:22:30 -04004914 dprintk("--> %s\n", __func__);
4915 task = rpc_run_task(&task_setup);
4916
4917 if (IS_ERR(task))
4918 status = PTR_ERR(task);
4919 else {
4920 status = task->tk_status;
4921 rpc_put_task(task);
4922 }
4923 dprintk("<-- %s return %d\n", __func__, status);
4924
4925 return status;
4926}
4927
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004928/*
4929 * Reset a slot table
4930 */
Andy Adamson104aeba2010-01-14 17:45:10 -05004931static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4932 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004933{
Andy Adamson104aeba2010-01-14 17:45:10 -05004934 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004935 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004936 int ret = 0;
4937
Andy Adamson104aeba2010-01-14 17:45:10 -05004938 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4939 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004940
Andy Adamson104aeba2010-01-14 17:45:10 -05004941 /* Does the newly negotiated max_reqs match the existing slot table? */
4942 if (max_reqs != tbl->max_slots) {
4943 ret = -ENOMEM;
4944 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004945 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05004946 if (!new)
4947 goto out;
4948 ret = 0;
4949 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004950 }
4951 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05004952 if (new) {
4953 tbl->slots = new;
4954 tbl->max_slots = max_reqs;
4955 }
4956 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004957 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004958 spin_unlock(&tbl->slot_tbl_lock);
4959 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4960 tbl, tbl->slots, tbl->max_slots);
4961out:
4962 dprintk("<-- %s: return %d\n", __func__, ret);
4963 return ret;
4964}
4965
Andy Adamsonfc931582009-04-01 09:22:31 -04004966/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004967 * Reset the forechannel and backchannel slot tables
4968 */
4969static int nfs4_reset_slot_tables(struct nfs4_session *session)
4970{
4971 int status;
4972
4973 status = nfs4_reset_slot_table(&session->fc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004974 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004975 if (status)
4976 return status;
4977
4978 status = nfs4_reset_slot_table(&session->bc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004979 session->bc_attrs.max_reqs, 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004980 return status;
4981}
4982
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004983/* Destroy the slot table */
4984static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4985{
4986 if (session->fc_slot_table.slots != NULL) {
4987 kfree(session->fc_slot_table.slots);
4988 session->fc_slot_table.slots = NULL;
4989 }
4990 if (session->bc_slot_table.slots != NULL) {
4991 kfree(session->bc_slot_table.slots);
4992 session->bc_slot_table.slots = NULL;
4993 }
4994 return;
4995}
4996
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004997/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004998 * Initialize slot table
4999 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005000static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
5001 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04005002{
Andy Adamsonfc931582009-04-01 09:22:31 -04005003 struct nfs4_slot *slot;
5004 int ret = -ENOMEM;
5005
5006 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
5007
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005008 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04005009
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005010 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04005011 if (!slot)
5012 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04005013 ret = 0;
5014
5015 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04005016 tbl->max_slots = max_slots;
5017 tbl->slots = slot;
5018 tbl->highest_used_slotid = -1; /* no slot is currently used */
5019 spin_unlock(&tbl->slot_tbl_lock);
5020 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5021 tbl, tbl->slots, tbl->max_slots);
5022out:
5023 dprintk("<-- %s: return %d\n", __func__, ret);
5024 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04005025}
5026
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005027/*
5028 * Initialize the forechannel and backchannel tables
5029 */
5030static int nfs4_init_slot_tables(struct nfs4_session *session)
5031{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005032 struct nfs4_slot_table *tbl;
5033 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005034
Trond Myklebustf26468f2009-12-05 19:32:11 -05005035 tbl = &session->fc_slot_table;
5036 if (tbl->slots == NULL) {
5037 status = nfs4_init_slot_table(tbl,
5038 session->fc_attrs.max_reqs, 1);
5039 if (status)
5040 return status;
5041 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005042
Trond Myklebustf26468f2009-12-05 19:32:11 -05005043 tbl = &session->bc_slot_table;
5044 if (tbl->slots == NULL) {
5045 status = nfs4_init_slot_table(tbl,
5046 session->bc_attrs.max_reqs, 0);
5047 if (status)
5048 nfs4_destroy_slot_tables(session);
5049 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005050
5051 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005052}
5053
5054struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5055{
5056 struct nfs4_session *session;
5057 struct nfs4_slot_table *tbl;
5058
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005059 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005060 if (!session)
5061 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005062
Andy Adamson557134a2009-04-01 09:21:53 -04005063 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05005064 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04005065 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005066 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005067 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005068
5069 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05005070 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005071 spin_lock_init(&tbl->slot_tbl_lock);
5072 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005073 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005074
Trond Myklebust1055d762010-06-16 09:52:27 -04005075 session->session_state = 1<<NFS4_SESSION_INITING;
5076
Andy Adamson557134a2009-04-01 09:21:53 -04005077 session->clp = clp;
5078 return session;
5079}
5080
5081void nfs4_destroy_session(struct nfs4_session *session)
5082{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005083 nfs4_proc_destroy_session(session);
5084 dprintk("%s Destroy backchannel for xprt %p\n",
5085 __func__, session->clp->cl_rpcclient->cl_xprt);
5086 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
5087 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005088 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005089 kfree(session);
5090}
5091
Andy Adamsonfc931582009-04-01 09:22:31 -04005092/*
5093 * Initialize the values to be used by the client in CREATE_SESSION
5094 * If nfs4_init_session set the fore channel request and response sizes,
5095 * use them.
5096 *
5097 * Set the back channel max_resp_sz_cached to zero to force the client to
5098 * always set csa_cachethis to FALSE because the current implementation
5099 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5100 */
5101static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5102{
5103 struct nfs4_session *session = args->client->cl_session;
5104 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5105 mxresp_sz = session->fc_attrs.max_resp_sz;
5106
5107 if (mxrqst_sz == 0)
5108 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5109 if (mxresp_sz == 0)
5110 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5111 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005112 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5113 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005114 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5115 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
5116
5117 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005118 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005119 __func__,
5120 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005121 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005122
5123 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005124 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5125 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5126 args->bc_attrs.max_resp_sz_cached = 0;
5127 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5128 args->bc_attrs.max_reqs = 1;
5129
5130 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5131 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5132 __func__,
5133 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5134 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5135 args->bc_attrs.max_reqs);
5136}
5137
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005138static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005139{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005140 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5141 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5142
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005143 if (rcvd->max_resp_sz > sent->max_resp_sz)
5144 return -EINVAL;
5145 /*
5146 * Our requested max_ops is the minimum we need; we're not
5147 * prepared to break up compounds into smaller pieces than that.
5148 * So, no point even trying to continue if the server won't
5149 * cooperate:
5150 */
5151 if (rcvd->max_ops < sent->max_ops)
5152 return -EINVAL;
5153 if (rcvd->max_reqs == 0)
5154 return -EINVAL;
5155 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005156}
5157
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005158static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5159{
5160 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5161 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005162
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005163 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5164 return -EINVAL;
5165 if (rcvd->max_resp_sz < sent->max_resp_sz)
5166 return -EINVAL;
5167 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5168 return -EINVAL;
5169 /* These would render the backchannel useless: */
5170 if (rcvd->max_ops == 0)
5171 return -EINVAL;
5172 if (rcvd->max_reqs == 0)
5173 return -EINVAL;
5174 return 0;
5175}
Andy Adamson8d353012009-04-01 09:22:32 -04005176
Andy Adamson8d353012009-04-01 09:22:32 -04005177static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5178 struct nfs4_session *session)
5179{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005180 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005181
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005182 ret = nfs4_verify_fore_channel_attrs(args, session);
5183 if (ret)
5184 return ret;
5185 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005186}
5187
Andy Adamsonfc931582009-04-01 09:22:31 -04005188static int _nfs4_proc_create_session(struct nfs_client *clp)
5189{
5190 struct nfs4_session *session = clp->cl_session;
5191 struct nfs41_create_session_args args = {
5192 .client = clp,
5193 .cb_program = NFS4_CALLBACK,
5194 };
5195 struct nfs41_create_session_res res = {
5196 .client = clp,
5197 };
5198 struct rpc_message msg = {
5199 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5200 .rpc_argp = &args,
5201 .rpc_resp = &res,
5202 };
5203 int status;
5204
5205 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005206 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005207
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005208 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005209
Andy Adamson8d353012009-04-01 09:22:32 -04005210 if (!status)
5211 /* Verify the session's negotiated channel_attrs values */
5212 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005213 if (!status) {
5214 /* Increment the clientid slot sequence id */
5215 clp->cl_seqid++;
5216 }
5217
5218 return status;
5219}
5220
5221/*
5222 * Issues a CREATE_SESSION operation to the server.
5223 * It is the responsibility of the caller to verify the session is
5224 * expired before calling this routine.
5225 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005226int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005227{
5228 int status;
5229 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005230 struct nfs4_session *session = clp->cl_session;
5231
5232 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5233
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005234 status = _nfs4_proc_create_session(clp);
Andy Adamsonfc931582009-04-01 09:22:31 -04005235 if (status)
5236 goto out;
5237
Trond Myklebustf26468f2009-12-05 19:32:11 -05005238 /* Init and reset the fore channel */
5239 status = nfs4_init_slot_tables(session);
5240 dprintk("slot table initialization returned %d\n", status);
5241 if (status)
5242 goto out;
5243 status = nfs4_reset_slot_tables(session);
5244 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005245 if (status)
5246 goto out;
5247
5248 ptr = (unsigned *)&session->sess_id.data[0];
5249 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5250 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005251out:
5252 dprintk("<-- %s\n", __func__);
5253 return status;
5254}
5255
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005256/*
5257 * Issue the over-the-wire RPC DESTROY_SESSION.
5258 * The caller must serialize access to this routine.
5259 */
5260int nfs4_proc_destroy_session(struct nfs4_session *session)
5261{
5262 int status = 0;
5263 struct rpc_message msg;
5264
5265 dprintk("--> nfs4_proc_destroy_session\n");
5266
5267 /* session is still being setup */
5268 if (session->clp->cl_cons_state != NFS_CS_READY)
5269 return status;
5270
5271 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5272 msg.rpc_argp = session;
5273 msg.rpc_resp = NULL;
5274 msg.rpc_cred = NULL;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005275 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005276
5277 if (status)
5278 printk(KERN_WARNING
5279 "Got error %d from the server on DESTROY_SESSION. "
5280 "Session has been destroyed regardless...\n", status);
5281
5282 dprintk("<-- nfs4_proc_destroy_session\n");
5283 return status;
5284}
5285
Trond Myklebustfccba802009-07-21 16:48:07 -04005286int nfs4_init_session(struct nfs_server *server)
5287{
5288 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005289 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005290 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005291 int ret;
5292
5293 if (!nfs4_has_session(clp))
5294 return 0;
5295
Trond Myklebust1055d762010-06-16 09:52:27 -04005296 session = clp->cl_session;
5297 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5298 return 0;
5299
Andy Adamson68bf05e2009-12-15 12:55:02 -05005300 rsize = server->rsize;
5301 if (rsize == 0)
5302 rsize = NFS_MAX_FILE_IO_SIZE;
5303 wsize = server->wsize;
5304 if (wsize == 0)
5305 wsize = NFS_MAX_FILE_IO_SIZE;
5306
Andy Adamson68bf05e2009-12-15 12:55:02 -05005307 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5308 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005309
Trond Myklebustfccba802009-07-21 16:48:07 -04005310 ret = nfs4_recover_expired_lease(server);
5311 if (!ret)
5312 ret = nfs4_check_client_ready(clp);
5313 return ret;
5314}
5315
Andy Adamsond83217c2011-03-01 01:34:17 +00005316int nfs4_init_ds_session(struct nfs_client *clp)
5317{
5318 struct nfs4_session *session = clp->cl_session;
5319 int ret;
5320
5321 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5322 return 0;
5323
5324 ret = nfs4_client_recover_expired_lease(clp);
5325 if (!ret)
5326 /* Test for the DS role */
5327 if (!is_ds_client(clp))
5328 ret = -ENODEV;
5329 if (!ret)
5330 ret = nfs4_check_client_ready(clp);
5331 return ret;
5332
5333}
5334EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5335
5336
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005337/*
5338 * Renew the cl_session lease.
5339 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005340struct nfs4_sequence_data {
5341 struct nfs_client *clp;
5342 struct nfs4_sequence_args args;
5343 struct nfs4_sequence_res res;
5344};
5345
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005346static void nfs41_sequence_release(void *data)
5347{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005348 struct nfs4_sequence_data *calldata = data;
5349 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005350
Alexandros Batsakis71358402010-02-05 03:45:05 -08005351 if (atomic_read(&clp->cl_count) > 1)
5352 nfs4_schedule_state_renewal(clp);
5353 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005354 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005355}
5356
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005357static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5358{
5359 switch(task->tk_status) {
5360 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005361 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5362 return -EAGAIN;
5363 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005364 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005365 }
5366 return 0;
5367}
5368
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005369static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005370{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005371 struct nfs4_sequence_data *calldata = data;
5372 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005373
Trond Myklebust14516c32010-07-31 14:29:06 -04005374 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5375 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005376
5377 if (task->tk_status < 0) {
5378 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005379 if (atomic_read(&clp->cl_count) == 1)
5380 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005381
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005382 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5383 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005384 return;
5385 }
5386 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005387 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005388out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005389 dprintk("<-- %s\n", __func__);
5390}
5391
5392static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5393{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005394 struct nfs4_sequence_data *calldata = data;
5395 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005396 struct nfs4_sequence_args *args;
5397 struct nfs4_sequence_res *res;
5398
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005399 args = task->tk_msg.rpc_argp;
5400 res = task->tk_msg.rpc_resp;
5401
Trond Myklebust035168ab2010-06-16 09:52:26 -04005402 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005403 return;
5404 rpc_call_start(task);
5405}
5406
5407static const struct rpc_call_ops nfs41_sequence_ops = {
5408 .rpc_call_done = nfs41_sequence_call_done,
5409 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005410 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005411};
5412
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005413static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005414{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005415 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005416 struct rpc_message msg = {
5417 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5418 .rpc_cred = cred,
5419 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005420 struct rpc_task_setup task_setup_data = {
5421 .rpc_client = clp->cl_rpcclient,
5422 .rpc_message = &msg,
5423 .callback_ops = &nfs41_sequence_ops,
5424 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5425 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005426
Alexandros Batsakis71358402010-02-05 03:45:05 -08005427 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005428 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005429 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005430 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005431 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005432 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005433 }
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005434 msg.rpc_argp = &calldata->args;
5435 msg.rpc_resp = &calldata->res;
5436 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005437 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005438
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005439 return rpc_run_task(&task_setup_data);
5440}
5441
5442static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5443{
5444 struct rpc_task *task;
5445 int ret = 0;
5446
5447 task = _nfs41_proc_sequence(clp, cred);
5448 if (IS_ERR(task))
5449 ret = PTR_ERR(task);
5450 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005451 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005452 dprintk("<-- %s status=%d\n", __func__, ret);
5453 return ret;
5454}
5455
5456static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5457{
5458 struct rpc_task *task;
5459 int ret;
5460
5461 task = _nfs41_proc_sequence(clp, cred);
5462 if (IS_ERR(task)) {
5463 ret = PTR_ERR(task);
5464 goto out;
5465 }
5466 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005467 if (!ret) {
5468 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5469
5470 if (task->tk_status == 0)
5471 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005472 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005473 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005474 rpc_put_task(task);
5475out:
5476 dprintk("<-- %s status=%d\n", __func__, ret);
5477 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005478}
5479
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005480struct nfs4_reclaim_complete_data {
5481 struct nfs_client *clp;
5482 struct nfs41_reclaim_complete_args arg;
5483 struct nfs41_reclaim_complete_res res;
5484};
5485
5486static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5487{
5488 struct nfs4_reclaim_complete_data *calldata = data;
5489
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005490 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168ab2010-06-16 09:52:26 -04005491 if (nfs41_setup_sequence(calldata->clp->cl_session,
5492 &calldata->arg.seq_args,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005493 &calldata->res.seq_res, 0, task))
5494 return;
5495
5496 rpc_call_start(task);
5497}
5498
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005499static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5500{
5501 switch(task->tk_status) {
5502 case 0:
5503 case -NFS4ERR_COMPLETE_ALREADY:
5504 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5505 break;
5506 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005507 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005508 /* fall through */
5509 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005510 return -EAGAIN;
5511 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005512 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005513 }
5514 return 0;
5515}
5516
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005517static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5518{
5519 struct nfs4_reclaim_complete_data *calldata = data;
5520 struct nfs_client *clp = calldata->clp;
5521 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5522
5523 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005524 if (!nfs41_sequence_done(task, res))
5525 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005526
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005527 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5528 rpc_restart_call_prepare(task);
5529 return;
5530 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005531 dprintk("<-- %s\n", __func__);
5532}
5533
5534static void nfs4_free_reclaim_complete_data(void *data)
5535{
5536 struct nfs4_reclaim_complete_data *calldata = data;
5537
5538 kfree(calldata);
5539}
5540
5541static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5542 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5543 .rpc_call_done = nfs4_reclaim_complete_done,
5544 .rpc_release = nfs4_free_reclaim_complete_data,
5545};
5546
5547/*
5548 * Issue a global reclaim complete.
5549 */
5550static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5551{
5552 struct nfs4_reclaim_complete_data *calldata;
5553 struct rpc_task *task;
5554 struct rpc_message msg = {
5555 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5556 };
5557 struct rpc_task_setup task_setup_data = {
5558 .rpc_client = clp->cl_rpcclient,
5559 .rpc_message = &msg,
5560 .callback_ops = &nfs4_reclaim_complete_call_ops,
5561 .flags = RPC_TASK_ASYNC,
5562 };
5563 int status = -ENOMEM;
5564
5565 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005566 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005567 if (calldata == NULL)
5568 goto out;
5569 calldata->clp = clp;
5570 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005571
5572 msg.rpc_argp = &calldata->arg;
5573 msg.rpc_resp = &calldata->res;
5574 task_setup_data.callback_data = calldata;
5575 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005576 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005577 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005578 goto out;
5579 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005580 status = nfs4_wait_for_completion_rpc_task(task);
5581 if (status == 0)
5582 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005583 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005584 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005585out:
5586 dprintk("<-- %s status=%d\n", __func__, status);
5587 return status;
5588}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005589
5590static void
5591nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5592{
5593 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005594 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005595
5596 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005597 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5598 * right now covering the LAYOUTGET we are about to send.
5599 * However, that is not so catastrophic, and there seems
5600 * to be no way to prevent it completely.
5601 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005602 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5603 &lgp->res.seq_res, 0, task))
5604 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005605 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5606 NFS_I(lgp->args.inode)->layout,
5607 lgp->args.ctx->state)) {
5608 rpc_exit(task, NFS4_OK);
5609 return;
5610 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005611 rpc_call_start(task);
5612}
5613
5614static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5615{
5616 struct nfs4_layoutget *lgp = calldata;
5617 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5618
5619 dprintk("--> %s\n", __func__);
5620
5621 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5622 return;
5623
5624 switch (task->tk_status) {
5625 case 0:
5626 break;
5627 case -NFS4ERR_LAYOUTTRYLATER:
5628 case -NFS4ERR_RECALLCONFLICT:
5629 task->tk_status = -NFS4ERR_DELAY;
5630 /* Fall through */
5631 default:
5632 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5633 rpc_restart_call_prepare(task);
5634 return;
5635 }
5636 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005637 dprintk("<-- %s\n", __func__);
5638}
5639
5640static void nfs4_layoutget_release(void *calldata)
5641{
5642 struct nfs4_layoutget *lgp = calldata;
5643
5644 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005645 put_nfs_open_context(lgp->args.ctx);
5646 kfree(calldata);
5647 dprintk("<-- %s\n", __func__);
5648}
5649
5650static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5651 .rpc_call_prepare = nfs4_layoutget_prepare,
5652 .rpc_call_done = nfs4_layoutget_done,
5653 .rpc_release = nfs4_layoutget_release,
5654};
5655
5656int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5657{
5658 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5659 struct rpc_task *task;
5660 struct rpc_message msg = {
5661 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5662 .rpc_argp = &lgp->args,
5663 .rpc_resp = &lgp->res,
5664 };
5665 struct rpc_task_setup task_setup_data = {
5666 .rpc_client = server->client,
5667 .rpc_message = &msg,
5668 .callback_ops = &nfs4_layoutget_call_ops,
5669 .callback_data = lgp,
5670 .flags = RPC_TASK_ASYNC,
5671 };
5672 int status = 0;
5673
5674 dprintk("--> %s\n", __func__);
5675
Weston Andros Adamson35124a02011-03-24 16:48:21 -04005676 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005677 lgp->res.seq_res.sr_slot = NULL;
5678 task = rpc_run_task(&task_setup_data);
5679 if (IS_ERR(task))
5680 return PTR_ERR(task);
5681 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00005682 if (status == 0)
5683 status = task->tk_status;
5684 if (status == 0)
5685 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005686 rpc_put_task(task);
5687 dprintk("<-- %s status=%d\n", __func__, status);
5688 return status;
5689}
5690
Benny Halevycbe82602011-05-22 19:52:37 +03005691static void
5692nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
5693{
5694 struct nfs4_layoutreturn *lrp = calldata;
5695
5696 dprintk("--> %s\n", __func__);
5697 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
5698 &lrp->res.seq_res, 0, task))
5699 return;
5700 rpc_call_start(task);
5701}
5702
5703static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
5704{
5705 struct nfs4_layoutreturn *lrp = calldata;
5706 struct nfs_server *server;
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005707 struct pnfs_layout_hdr *lo = NFS_I(lrp->args.inode)->layout;
Benny Halevycbe82602011-05-22 19:52:37 +03005708
5709 dprintk("--> %s\n", __func__);
5710
5711 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
5712 return;
5713
5714 server = NFS_SERVER(lrp->args.inode);
5715 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5716 nfs_restart_rpc(task, lrp->clp);
5717 return;
5718 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005719 spin_lock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005720 if (task->tk_status == 0) {
Benny Halevycbe82602011-05-22 19:52:37 +03005721 if (lrp->res.lrs_present) {
Benny Halevycbe82602011-05-22 19:52:37 +03005722 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Benny Halevycbe82602011-05-22 19:52:37 +03005723 } else
5724 BUG_ON(!list_empty(&lo->plh_segs));
5725 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005726 lo->plh_block_lgets--;
5727 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005728 dprintk("<-- %s\n", __func__);
5729}
5730
5731static void nfs4_layoutreturn_release(void *calldata)
5732{
5733 struct nfs4_layoutreturn *lrp = calldata;
5734
5735 dprintk("--> %s\n", __func__);
5736 put_layout_hdr(NFS_I(lrp->args.inode)->layout);
5737 kfree(calldata);
5738 dprintk("<-- %s\n", __func__);
5739}
5740
5741static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
5742 .rpc_call_prepare = nfs4_layoutreturn_prepare,
5743 .rpc_call_done = nfs4_layoutreturn_done,
5744 .rpc_release = nfs4_layoutreturn_release,
5745};
5746
5747int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
5748{
5749 struct rpc_task *task;
5750 struct rpc_message msg = {
5751 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
5752 .rpc_argp = &lrp->args,
5753 .rpc_resp = &lrp->res,
5754 };
5755 struct rpc_task_setup task_setup_data = {
5756 .rpc_client = lrp->clp->cl_rpcclient,
5757 .rpc_message = &msg,
5758 .callback_ops = &nfs4_layoutreturn_call_ops,
5759 .callback_data = lrp,
5760 };
5761 int status;
5762
5763 dprintk("--> %s\n", __func__);
5764 task = rpc_run_task(&task_setup_data);
5765 if (IS_ERR(task))
5766 return PTR_ERR(task);
5767 status = task->tk_status;
5768 dprintk("<-- %s status=%d\n", __func__, status);
5769 rpc_put_task(task);
5770 return status;
5771}
5772
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005773static int
5774_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5775{
5776 struct nfs4_getdeviceinfo_args args = {
5777 .pdev = pdev,
5778 };
5779 struct nfs4_getdeviceinfo_res res = {
5780 .pdev = pdev,
5781 };
5782 struct rpc_message msg = {
5783 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5784 .rpc_argp = &args,
5785 .rpc_resp = &res,
5786 };
5787 int status;
5788
5789 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005790 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005791 dprintk("<-- %s status=%d\n", __func__, status);
5792
5793 return status;
5794}
5795
5796int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5797{
5798 struct nfs4_exception exception = { };
5799 int err;
5800
5801 do {
5802 err = nfs4_handle_exception(server,
5803 _nfs4_proc_getdeviceinfo(server, pdev),
5804 &exception);
5805 } while (exception.retry);
5806 return err;
5807}
5808EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5809
Andy Adamson863a3c62011-03-23 13:27:54 +00005810static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
5811{
5812 struct nfs4_layoutcommit_data *data = calldata;
5813 struct nfs_server *server = NFS_SERVER(data->args.inode);
5814
5815 if (nfs4_setup_sequence(server, &data->args.seq_args,
5816 &data->res.seq_res, 1, task))
5817 return;
5818 rpc_call_start(task);
5819}
5820
5821static void
5822nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
5823{
5824 struct nfs4_layoutcommit_data *data = calldata;
5825 struct nfs_server *server = NFS_SERVER(data->args.inode);
5826
5827 if (!nfs4_sequence_done(task, &data->res.seq_res))
5828 return;
5829
5830 switch (task->tk_status) { /* Just ignore these failures */
5831 case NFS4ERR_DELEG_REVOKED: /* layout was recalled */
5832 case NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
5833 case NFS4ERR_BADLAYOUT: /* no layout */
5834 case NFS4ERR_GRACE: /* loca_recalim always false */
5835 task->tk_status = 0;
5836 }
5837
5838 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5839 nfs_restart_rpc(task, server->nfs_client);
5840 return;
5841 }
5842
5843 if (task->tk_status == 0)
5844 nfs_post_op_update_inode_force_wcc(data->args.inode,
5845 data->res.fattr);
5846}
5847
5848static void nfs4_layoutcommit_release(void *calldata)
5849{
5850 struct nfs4_layoutcommit_data *data = calldata;
5851
5852 /* Matched by references in pnfs_set_layoutcommit */
5853 put_lseg(data->lseg);
5854 put_rpccred(data->cred);
5855 kfree(data);
5856}
5857
5858static const struct rpc_call_ops nfs4_layoutcommit_ops = {
5859 .rpc_call_prepare = nfs4_layoutcommit_prepare,
5860 .rpc_call_done = nfs4_layoutcommit_done,
5861 .rpc_release = nfs4_layoutcommit_release,
5862};
5863
5864int
Andy Adamsonef311532011-03-12 02:58:10 -05005865nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00005866{
5867 struct rpc_message msg = {
5868 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
5869 .rpc_argp = &data->args,
5870 .rpc_resp = &data->res,
5871 .rpc_cred = data->cred,
5872 };
5873 struct rpc_task_setup task_setup_data = {
5874 .task = &data->task,
5875 .rpc_client = NFS_CLIENT(data->args.inode),
5876 .rpc_message = &msg,
5877 .callback_ops = &nfs4_layoutcommit_ops,
5878 .callback_data = data,
5879 .flags = RPC_TASK_ASYNC,
5880 };
5881 struct rpc_task *task;
5882 int status = 0;
5883
5884 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
5885 "lbw: %llu inode %lu\n",
5886 data->task.tk_pid, sync,
5887 data->args.lastbytewritten,
5888 data->args.inode->i_ino);
5889
5890 task = rpc_run_task(&task_setup_data);
5891 if (IS_ERR(task))
5892 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05005893 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00005894 goto out;
5895 status = nfs4_wait_for_completion_rpc_task(task);
5896 if (status != 0)
5897 goto out;
5898 status = task->tk_status;
5899out:
5900 dprintk("%s: status %d\n", __func__, status);
5901 rpc_put_task(task);
5902 return status;
5903}
Andy Adamson557134a2009-04-01 09:21:53 -04005904#endif /* CONFIG_NFS_V4_1 */
5905
Andy Adamson591d71c2009-04-01 09:22:47 -04005906struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005907 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005908 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909 .recover_open = nfs4_open_reclaim,
5910 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04005911 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005912 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913};
5914
Andy Adamson591d71c2009-04-01 09:22:47 -04005915#if defined(CONFIG_NFS_V4_1)
5916struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5917 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5918 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5919 .recover_open = nfs4_open_reclaim,
5920 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05005921 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005922 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005923 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04005924};
5925#endif /* CONFIG_NFS_V4_1 */
5926
5927struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005928 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005929 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930 .recover_open = nfs4_open_expired,
5931 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04005932 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005933 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934};
5935
Andy Adamson591d71c2009-04-01 09:22:47 -04005936#if defined(CONFIG_NFS_V4_1)
5937struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5938 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5939 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5940 .recover_open = nfs4_open_expired,
5941 .recover_lock = nfs4_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05005942 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005943 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04005944};
5945#endif /* CONFIG_NFS_V4_1 */
5946
Benny Halevy29fba382009-04-01 09:22:44 -04005947struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5948 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04005949 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005950 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04005951};
5952
5953#if defined(CONFIG_NFS_V4_1)
5954struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5955 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04005956 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005957 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04005958};
5959#endif
5960
Trond Myklebust97dc1352010-06-16 09:52:26 -04005961static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
5962 .minor_version = 0,
5963 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04005964 .validate_stateid = nfs4_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005965 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
5966 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
5967 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005968};
5969
5970#if defined(CONFIG_NFS_V4_1)
5971static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
5972 .minor_version = 1,
5973 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04005974 .validate_stateid = nfs41_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005975 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
5976 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
5977 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005978};
5979#endif
5980
Trond Myklebust97dc1352010-06-16 09:52:26 -04005981const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
5982 [0] = &nfs_v4_0_minor_ops,
5983#if defined(CONFIG_NFS_V4_1)
5984 [1] = &nfs_v4_1_minor_ops,
5985#endif
5986};
5987
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005988static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005989 .permission = nfs_permission,
5990 .getattr = nfs_getattr,
5991 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005992 .getxattr = generic_getxattr,
5993 .setxattr = generic_setxattr,
5994 .listxattr = generic_listxattr,
5995 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005996};
5997
David Howells509de812006-08-22 20:06:11 -04005998const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999 .version = 4, /* protocol version */
6000 .dentry_ops = &nfs4_dentry_operations,
6001 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006002 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003 .getroot = nfs4_proc_get_root,
6004 .getattr = nfs4_proc_getattr,
6005 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04006006 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006007 .lookup = nfs4_proc_lookup,
6008 .access = nfs4_proc_access,
6009 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010 .create = nfs4_proc_create,
6011 .remove = nfs4_proc_remove,
6012 .unlink_setup = nfs4_proc_unlink_setup,
6013 .unlink_done = nfs4_proc_unlink_done,
6014 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006015 .rename_setup = nfs4_proc_rename_setup,
6016 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017 .link = nfs4_proc_link,
6018 .symlink = nfs4_proc_symlink,
6019 .mkdir = nfs4_proc_mkdir,
6020 .rmdir = nfs4_proc_remove,
6021 .readdir = nfs4_proc_readdir,
6022 .mknod = nfs4_proc_mknod,
6023 .statfs = nfs4_proc_statfs,
6024 .fsinfo = nfs4_proc_fsinfo,
6025 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006026 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027 .decode_dirent = nfs4_decode_dirent,
6028 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05006029 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006031 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006033 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006035 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006036 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006037 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00006038 .init_client = nfs4_init_client,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006039 .secinfo = nfs4_proc_secinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040};
6041
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006042static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6043 .prefix = XATTR_NAME_NFSV4_ACL,
6044 .list = nfs4_xattr_list_nfs4_acl,
6045 .get = nfs4_xattr_get_nfs4_acl,
6046 .set = nfs4_xattr_set_nfs4_acl,
6047};
6048
6049const struct xattr_handler *nfs4_xattr_handlers[] = {
6050 &nfs4_xattr_nfs4_acl_handler,
6051 NULL
6052};
6053
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054/*
6055 * Local variables:
6056 * c-basic-offset: 8
6057 * End:
6058 */