blob: e99d674149ae49a8b33ac59c770189adb70353e8 [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 Myklebust035168a2010-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 Myklebust035168a2010-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 Myklebust035168a2010-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 Myklebust035168a2010-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 Myklebust035168a2010-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 Myklebust035168a2010-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 Myklebust035168a2010-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 Myklebust035168a2010-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 Myklebustad389da2007-06-05 12:30:00 -0400766 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100767 struct dentry *dir;
768 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
Trond Myklebustad389da2007-06-05 12:30:00 -0400789static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
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{
Trond Myklebustad389da2007-06-05 12:30:00 -0400794 struct dentry *parent = dget_parent(path->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 Virof6948692010-01-30 13:51:04 -0500805 path_get(path);
806 p->path = *path;
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;
Trond Myklebustad389da2007-06-05 12:30:00 -0400815 p->o_arg.name = &p->path.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);
845
846 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400847 if (p->state != NULL)
848 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400849 nfs4_put_state_owner(p->owner);
850 dput(p->dir);
Jan Blunck31f31db12008-05-02 13:42:45 -0700851 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400852 kfree(p);
853}
854
855static void nfs4_opendata_put(struct nfs4_opendata *p)
856{
857 if (p != NULL)
858 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100859}
860
Trond Myklebust06f814a2006-01-03 09:55:07 +0100861static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
862{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100863 int ret;
864
Trond Myklebust06f814a2006-01-03 09:55:07 +0100865 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100866 return ret;
867}
868
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500869static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400870{
871 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500872
873 if (open_mode & O_EXCL)
874 goto out;
875 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400876 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500877 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
878 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400879 break;
880 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500881 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
882 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400883 break;
884 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500885 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
886 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400887 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500888out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400889 return ret;
890}
891
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500892static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400893{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500894 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400895 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500896 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400897 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500898 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400899 return 1;
900}
901
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500902static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100903{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500904 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100905 case FMODE_WRITE:
906 state->n_wronly++;
907 break;
908 case FMODE_READ:
909 state->n_rdonly++;
910 break;
911 case FMODE_READ|FMODE_WRITE:
912 state->n_rdwr++;
913 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500914 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100915}
916
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500917static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400918{
919 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
920 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
921 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500922 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400923 case FMODE_READ:
924 set_bit(NFS_O_RDONLY_STATE, &state->flags);
925 break;
926 case FMODE_WRITE:
927 set_bit(NFS_O_WRONLY_STATE, &state->flags);
928 break;
929 case FMODE_READ|FMODE_WRITE:
930 set_bit(NFS_O_RDWR_STATE, &state->flags);
931 }
932}
933
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500934static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400935{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400936 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500937 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400938 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400939}
940
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500941static 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 -0700942{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400943 /*
944 * Protect the call to nfs4_state_set_mode_locked and
945 * serialise the stateid update
946 */
947 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400948 if (deleg_stateid != NULL) {
949 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
950 set_bit(NFS_DELEGATED_STATE, &state->flags);
951 }
952 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500953 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400954 write_sequnlock(&state->seqlock);
955 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500956 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700957 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958}
959
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500960static 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 -0500961{
962 struct nfs_inode *nfsi = NFS_I(state->inode);
963 struct nfs_delegation *deleg_cur;
964 int ret = 0;
965
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500966 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500967
968 rcu_read_lock();
969 deleg_cur = rcu_dereference(nfsi->delegation);
970 if (deleg_cur == NULL)
971 goto no_delegation;
972
973 spin_lock(&deleg_cur->lock);
974 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500975 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500976 goto no_delegation_unlock;
977
978 if (delegation == NULL)
979 delegation = &deleg_cur->stateid;
980 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
981 goto no_delegation_unlock;
982
Trond Myklebustb7391f42008-12-23 15:21:52 -0500983 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500984 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500985 ret = 1;
986no_delegation_unlock:
987 spin_unlock(&deleg_cur->lock);
988no_delegation:
989 rcu_read_unlock();
990
991 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500992 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500993 ret = 1;
994 }
995
996 return ret;
997}
998
999
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001000static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001001{
1002 struct nfs_delegation *delegation;
1003
1004 rcu_read_lock();
1005 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001006 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001007 rcu_read_unlock();
1008 return;
1009 }
1010 rcu_read_unlock();
1011 nfs_inode_return_delegation(inode);
1012}
1013
Trond Myklebust6ee41262007-07-08 14:11:36 -04001014static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001015{
1016 struct nfs4_state *state = opendata->state;
1017 struct nfs_inode *nfsi = NFS_I(state->inode);
1018 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001019 int open_mode = opendata->o_arg.open_flags & O_EXCL;
1020 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001021 nfs4_stateid stateid;
1022 int ret = -EAGAIN;
1023
Trond Myklebustaac00a82007-07-05 19:02:21 -04001024 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001025 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001026 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001027 if (can_open_cached(state, fmode, open_mode)) {
1028 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001029 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001030 goto out_return_state;
1031 }
1032 spin_unlock(&state->owner->so_lock);
1033 }
Trond Myklebust34310432008-12-23 15:21:38 -05001034 rcu_read_lock();
1035 delegation = rcu_dereference(nfsi->delegation);
1036 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001037 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001038 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001039 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001040 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001041 /* Save the delegation */
1042 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1043 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001044 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001045 if (ret != 0)
1046 goto out;
1047 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001048
1049 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001050 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001051 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001052 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001053out:
1054 return ERR_PTR(ret);
1055out_return_state:
1056 atomic_inc(&state->count);
1057 return state;
1058}
1059
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001060static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1061{
1062 struct inode *inode;
1063 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001064 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001065 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001066
Trond Myklebustaac00a82007-07-05 19:02:21 -04001067 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001068 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001069 goto out;
1070 }
1071
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001072 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001073 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001074 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001075 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001076 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001077 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001078 goto err;
1079 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001080 state = nfs4_get_open_state(inode, data->owner);
1081 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001082 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001083 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001084 int delegation_flags = 0;
1085
Trond Myklebust003707c2007-07-05 18:07:55 -04001086 rcu_read_lock();
1087 delegation = rcu_dereference(NFS_I(inode)->delegation);
1088 if (delegation)
1089 delegation_flags = delegation->flags;
1090 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -05001091 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001092 nfs_inode_set_delegation(state->inode,
1093 data->owner->so_cred,
1094 &data->o_res);
1095 else
1096 nfs_inode_reclaim_delegation(state->inode,
1097 data->owner->so_cred,
1098 &data->o_res);
1099 }
Trond Myklebust34310432008-12-23 15:21:38 -05001100
1101 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001102 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001103 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001104out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001105 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001106err_put_inode:
1107 iput(inode);
1108err:
1109 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001110}
1111
Trond Myklebust864472e2006-01-03 09:55:15 +01001112static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1113{
1114 struct nfs_inode *nfsi = NFS_I(state->inode);
1115 struct nfs_open_context *ctx;
1116
1117 spin_lock(&state->inode->i_lock);
1118 list_for_each_entry(ctx, &nfsi->open_files, list) {
1119 if (ctx->state != state)
1120 continue;
1121 get_nfs_open_context(ctx);
1122 spin_unlock(&state->inode->i_lock);
1123 return ctx;
1124 }
1125 spin_unlock(&state->inode->i_lock);
1126 return ERR_PTR(-ENOENT);
1127}
1128
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001129static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1130{
1131 struct nfs4_opendata *opendata;
1132
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001133 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001134 if (opendata == NULL)
1135 return ERR_PTR(-ENOMEM);
1136 opendata->state = state;
1137 atomic_inc(&state->count);
1138 return opendata;
1139}
1140
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001141static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001142{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001143 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001144 int ret;
1145
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001146 opendata->o_arg.open_flags = 0;
1147 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001148 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1149 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1150 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001151 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001152 if (ret != 0)
1153 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001154 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001155 if (IS_ERR(newstate))
1156 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001157 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001158 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001159 return 0;
1160}
1161
1162static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1163{
Trond Myklebust864472e2006-01-03 09:55:15 +01001164 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001165 int ret;
1166
1167 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001168 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001169 smp_rmb();
1170 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001171 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001172 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001173 if (ret != 0)
1174 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001175 if (newstate != state)
1176 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001177 }
1178 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001179 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001180 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001181 if (ret != 0)
1182 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001183 if (newstate != state)
1184 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001185 }
1186 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001187 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001188 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001189 if (ret != 0)
1190 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001191 if (newstate != state)
1192 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001193 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001194 /*
1195 * We may have performed cached opens for all three recoveries.
1196 * Check if we need to update the current stateid.
1197 */
1198 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1199 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001200 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001201 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1202 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001203 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001204 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001205 return 0;
1206}
1207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208/*
1209 * OPEN_RECLAIM:
1210 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001212static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001214 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001215 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001216 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 int status;
1218
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001219 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1220 if (IS_ERR(opendata))
1221 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001222 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1223 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001224 rcu_read_lock();
1225 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001226 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001227 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001228 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001229 opendata->o_arg.u.delegation_type = delegation_type;
1230 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001231 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 return status;
1233}
1234
Trond Myklebust539cd032007-06-05 11:46:42 -04001235static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
1237 struct nfs_server *server = NFS_SERVER(state->inode);
1238 struct nfs4_exception exception = { };
1239 int err;
1240 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001241 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001242 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001243 break;
1244 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 } while (exception.retry);
1246 return err;
1247}
1248
Trond Myklebust864472e2006-01-03 09:55:15 +01001249static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1250{
1251 struct nfs_open_context *ctx;
1252 int ret;
1253
1254 ctx = nfs4_state_find_open_context(state);
1255 if (IS_ERR(ctx))
1256 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001257 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001258 put_nfs_open_context(ctx);
1259 return ret;
1260}
1261
Trond Myklebust13437e12007-07-06 15:10:43 -04001262static 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 -07001263{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001264 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001265 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001267 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1268 if (IS_ERR(opendata))
1269 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001270 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001271 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001272 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001273 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001274 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001275 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276}
1277
Trond Myklebust13437e12007-07-06 15:10:43 -04001278int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
1280 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001281 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 int err;
1283 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001284 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 switch (err) {
1286 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001287 case -ENOENT:
1288 case -ESTALE:
1289 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001290 case -NFS4ERR_BADSESSION:
1291 case -NFS4ERR_BADSLOT:
1292 case -NFS4ERR_BAD_HIGH_SLOT:
1293 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1294 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001295 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001296 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 case -NFS4ERR_STALE_CLIENTID:
1298 case -NFS4ERR_STALE_STATEID:
1299 case -NFS4ERR_EXPIRED:
1300 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001301 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001302 goto out;
1303 case -ERESTARTSYS:
1304 /*
1305 * The show must go on: exit, but mark the
1306 * stateid as needing recovery.
1307 */
1308 case -NFS4ERR_ADMIN_REVOKED:
1309 case -NFS4ERR_BAD_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001310 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001311 case -EKEYEXPIRED:
1312 /*
1313 * User RPCSEC_GSS context has expired.
1314 * We cannot recover this stateid now, so
1315 * skip it and allow recovery thread to
1316 * proceed.
1317 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001318 case -ENOMEM:
1319 err = 0;
1320 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 }
1322 err = nfs4_handle_exception(server, err, &exception);
1323 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001324out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 return err;
1326}
1327
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001328static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1329{
1330 struct nfs4_opendata *data = calldata;
1331
1332 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001333 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001334 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1335 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001336 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001337 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001338 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001339 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001340}
1341
1342static void nfs4_open_confirm_release(void *calldata)
1343{
1344 struct nfs4_opendata *data = calldata;
1345 struct nfs4_state *state = NULL;
1346
1347 /* If this request hasn't been cancelled, do nothing */
1348 if (data->cancelled == 0)
1349 goto out_free;
1350 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001351 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001352 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001353 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001354 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001355 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001356out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001357 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001358}
1359
1360static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001361 .rpc_call_done = nfs4_open_confirm_done,
1362 .rpc_release = nfs4_open_confirm_release,
1363};
1364
1365/*
1366 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1367 */
1368static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1369{
1370 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1371 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001372 struct rpc_message msg = {
1373 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1374 .rpc_argp = &data->c_arg,
1375 .rpc_resp = &data->c_res,
1376 .rpc_cred = data->owner->so_cred,
1377 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001378 struct rpc_task_setup task_setup_data = {
1379 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001380 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001381 .callback_ops = &nfs4_open_confirm_ops,
1382 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001383 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001384 .flags = RPC_TASK_ASYNC,
1385 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 int status;
1387
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001388 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001389 data->rpc_done = 0;
1390 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001391 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001392 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001393 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001394 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001395 status = nfs4_wait_for_completion_rpc_task(task);
1396 if (status != 0) {
1397 data->cancelled = 1;
1398 smp_wmb();
1399 } else
1400 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001401 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 return status;
1403}
1404
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001405static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001407 struct nfs4_opendata *data = calldata;
1408 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001409
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001410 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1411 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001412 /*
1413 * Check if we still need to send an OPEN call, or if we can use
1414 * a delegation instead.
1415 */
1416 if (data->state != NULL) {
1417 struct nfs_delegation *delegation;
1418
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001419 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001420 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001421 rcu_read_lock();
1422 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1423 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001424 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001425 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001426 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001427 }
1428 rcu_read_unlock();
1429 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001430 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001431 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001432 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001433 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001434 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001435 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1436 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001437 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001438 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001439 &data->o_arg.seq_args,
1440 &data->o_res.seq_res, 1, task))
1441 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001442 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001443 return;
1444out_no_action:
1445 task->tk_action = NULL;
1446
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001447}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001449static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1450{
1451 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1452 nfs4_open_prepare(task, calldata);
1453}
1454
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001455static void nfs4_open_done(struct rpc_task *task, void *calldata)
1456{
1457 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001459 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001460
Trond Myklebust14516c32010-07-31 14:29:06 -04001461 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1462 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001463
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001464 if (task->tk_status == 0) {
1465 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001466 case S_IFREG:
1467 break;
1468 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001469 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001470 break;
1471 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001472 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001473 break;
1474 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001475 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001476 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001477 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001478 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1479 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001480 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001481 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001482}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001483
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001484static void nfs4_open_release(void *calldata)
1485{
1486 struct nfs4_opendata *data = calldata;
1487 struct nfs4_state *state = NULL;
1488
1489 /* If this request hasn't been cancelled, do nothing */
1490 if (data->cancelled == 0)
1491 goto out_free;
1492 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001493 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001494 goto out_free;
1495 /* In case we need an open_confirm, no cleanup! */
1496 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1497 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001498 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001499 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001500 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001501out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001502 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001503}
1504
1505static const struct rpc_call_ops nfs4_open_ops = {
1506 .rpc_call_prepare = nfs4_open_prepare,
1507 .rpc_call_done = nfs4_open_done,
1508 .rpc_release = nfs4_open_release,
1509};
1510
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001511static const struct rpc_call_ops nfs4_recover_open_ops = {
1512 .rpc_call_prepare = nfs4_recover_open_prepare,
1513 .rpc_call_done = nfs4_open_done,
1514 .rpc_release = nfs4_open_release,
1515};
1516
1517static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001518{
1519 struct inode *dir = data->dir->d_inode;
1520 struct nfs_server *server = NFS_SERVER(dir);
1521 struct nfs_openargs *o_arg = &data->o_arg;
1522 struct nfs_openres *o_res = &data->o_res;
1523 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001524 struct rpc_message msg = {
1525 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1526 .rpc_argp = o_arg,
1527 .rpc_resp = o_res,
1528 .rpc_cred = data->owner->so_cred,
1529 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001530 struct rpc_task_setup task_setup_data = {
1531 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001532 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001533 .callback_ops = &nfs4_open_ops,
1534 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001535 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001536 .flags = RPC_TASK_ASYNC,
1537 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001538 int status;
1539
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001540 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001541 data->rpc_done = 0;
1542 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001543 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001544 if (isrecover)
1545 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001546 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001547 if (IS_ERR(task))
1548 return PTR_ERR(task);
1549 status = nfs4_wait_for_completion_rpc_task(task);
1550 if (status != 0) {
1551 data->cancelled = 1;
1552 smp_wmb();
1553 } else
1554 status = data->rpc_status;
1555 rpc_put_task(task);
1556
1557 return status;
1558}
1559
1560static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1561{
1562 struct inode *dir = data->dir->d_inode;
1563 struct nfs_openres *o_res = &data->o_res;
1564 int status;
1565
1566 status = nfs4_run_open_task(data, 1);
1567 if (status != 0 || !data->rpc_done)
1568 return status;
1569
1570 nfs_refresh_inode(dir, o_res->dir_attr);
1571
1572 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1573 status = _nfs4_proc_open_confirm(data);
1574 if (status != 0)
1575 return status;
1576 }
1577
1578 return status;
1579}
1580
1581/*
1582 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1583 */
1584static int _nfs4_proc_open(struct nfs4_opendata *data)
1585{
1586 struct inode *dir = data->dir->d_inode;
1587 struct nfs_server *server = NFS_SERVER(dir);
1588 struct nfs_openargs *o_arg = &data->o_arg;
1589 struct nfs_openres *o_res = &data->o_res;
1590 int status;
1591
1592 status = nfs4_run_open_task(data, 0);
Trond Myklebust3e309912007-07-07 13:19:59 -04001593 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001594 return status;
1595
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001596 if (o_arg->open_flags & O_CREAT) {
1597 update_changeattr(dir, &o_res->cinfo);
1598 nfs_post_op_update_inode(dir, o_res->dir_attr);
1599 } else
1600 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001601 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1602 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001604 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001606 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 }
1608 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001609 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001610 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611}
1612
Andy Adamsond83217c2011-03-01 01:34:17 +00001613static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001614{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001615 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001616 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001617
Trond Myklebusta78cb572009-08-09 15:06:19 -04001618 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001619 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001620 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001621 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001622 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1623 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001624 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001625 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001626 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001627 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001628 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001629}
1630
Andy Adamsond83217c2011-03-01 01:34:17 +00001631static int nfs4_recover_expired_lease(struct nfs_server *server)
1632{
1633 return nfs4_client_recover_expired_lease(server->nfs_client);
1634}
1635
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636/*
1637 * OPEN_EXPIRED:
1638 * reclaim state on the server after a network partition.
1639 * Assumes caller holds the appropriate lock
1640 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001641static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001643 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001644 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001646 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1647 if (IS_ERR(opendata))
1648 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001649 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001650 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001651 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001652 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001653 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654}
1655
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001656static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001657{
Trond Myklebust539cd032007-06-05 11:46:42 -04001658 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001659 struct nfs4_exception exception = { };
1660 int err;
1661
1662 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001663 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001664 switch (err) {
1665 default:
1666 goto out;
1667 case -NFS4ERR_GRACE:
1668 case -NFS4ERR_DELAY:
1669 nfs4_handle_exception(server, err, &exception);
1670 err = 0;
1671 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001672 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001673out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001674 return err;
1675}
1676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1678{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001680 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Trond Myklebust864472e2006-01-03 09:55:15 +01001682 ctx = nfs4_state_find_open_context(state);
1683 if (IS_ERR(ctx))
1684 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001685 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001686 put_nfs_open_context(ctx);
1687 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688}
1689
1690/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001691 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1692 * fields corresponding to attributes that were used to store the verifier.
1693 * Make sure we clobber those fields in the later setattr call
1694 */
1695static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1696{
1697 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1698 !(sattr->ia_valid & ATTR_ATIME_SET))
1699 sattr->ia_valid |= ATTR_ATIME;
1700
1701 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1702 !(sattr->ia_valid & ATTR_MTIME_SET))
1703 sattr->ia_valid |= ATTR_MTIME;
1704}
1705
1706/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001707 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001709static int _nfs4_do_open(struct inode *dir, struct path *path, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710{
1711 struct nfs4_state_owner *sp;
1712 struct nfs4_state *state = NULL;
1713 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001714 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001715 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
1717 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 status = -ENOMEM;
1719 if (!(sp = nfs4_get_state_owner(server, cred))) {
1720 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1721 goto out_err;
1722 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001723 status = nfs4_recover_expired_lease(server);
1724 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001725 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001726 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001727 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001728 status = -ENOMEM;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001729 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001730 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001731 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
Trond Myklebustaac00a82007-07-05 19:02:21 -04001733 if (path->dentry->d_inode != NULL)
1734 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1735
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001736 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001738 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001740 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001741 status = PTR_ERR(state);
1742 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001743 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001744 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001745 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001746
1747 if (opendata->o_arg.open_flags & O_EXCL) {
1748 nfs4_exclusive_attrset(opendata, sattr);
1749
1750 nfs_fattr_init(opendata->o_res.f_attr);
1751 status = nfs4_do_setattr(state->inode, cred,
1752 opendata->o_res.f_attr, sattr,
1753 state);
1754 if (status == 0)
1755 nfs_setattr_update_inode(state->inode, sattr);
1756 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1757 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001758 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 *res = state;
1761 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001762err_opendata_put:
1763 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001764err_put_state_owner:
1765 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 *res = NULL;
1768 return status;
1769}
1770
1771
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001772static struct nfs4_state *nfs4_do_open(struct inode *dir, struct path *path, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773{
1774 struct nfs4_exception exception = { };
1775 struct nfs4_state *res;
1776 int status;
1777
1778 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001779 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 if (status == 0)
1781 break;
1782 /* NOTE: BAD_SEQID means the server and client disagree about the
1783 * book-keeping w.r.t. state-changing operations
1784 * (OPEN/CLOSE/LOCK/LOCKU...)
1785 * It is actually a sign of a bug on the client or on the server.
1786 *
1787 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001788 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 * have unhashed the old state_owner for us, and that we can
1790 * therefore safely retry using a new one. We should still warn
1791 * the user though...
1792 */
1793 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001794 printk(KERN_WARNING "NFS: v4 server %s "
1795 " returned a bad sequence-id error!\n",
1796 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 exception.retry = 1;
1798 continue;
1799 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001800 /*
1801 * BAD_STATEID on OPEN means that the server cancelled our
1802 * state before it received the OPEN_CONFIRM.
1803 * Recover by retrying the request as per the discussion
1804 * on Page 181 of RFC3530.
1805 */
1806 if (status == -NFS4ERR_BAD_STATEID) {
1807 exception.retry = 1;
1808 continue;
1809 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001810 if (status == -EAGAIN) {
1811 /* We must have found a delegation */
1812 exception.retry = 1;
1813 continue;
1814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1816 status, &exception));
1817 } while (exception.retry);
1818 return res;
1819}
1820
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001821static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1822 struct nfs_fattr *fattr, struct iattr *sattr,
1823 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001825 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001827 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 .iap = sattr,
1829 .server = server,
1830 .bitmask = server->attr_bitmask,
1831 };
1832 struct nfs_setattrres res = {
1833 .fattr = fattr,
1834 .server = server,
1835 };
1836 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001837 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1838 .rpc_argp = &arg,
1839 .rpc_resp = &res,
1840 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001842 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001843 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Trond Myklebust0e574af2005-10-27 22:12:38 -04001845 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001847 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1848 /* Use that stateid */
1849 } else if (state != NULL) {
Trond Myklebust77041ed2010-07-01 12:49:11 -04001850 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001851 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1853
Bryan Schumaker7c513052011-03-24 17:12:24 +00001854 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001855 if (status == 0 && state != NULL)
1856 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001857 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858}
1859
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001860static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1861 struct nfs_fattr *fattr, struct iattr *sattr,
1862 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001864 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 struct nfs4_exception exception = { };
1866 int err;
1867 do {
1868 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001869 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 &exception);
1871 } while (exception.retry);
1872 return err;
1873}
1874
1875struct nfs4_closedata {
1876 struct inode *inode;
1877 struct nfs4_state *state;
1878 struct nfs_closeargs arg;
1879 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001880 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001881 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00001882 bool roc;
1883 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884};
1885
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001886static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001887{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001888 struct nfs4_closedata *calldata = data;
1889 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04001890 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07001891
Fred Isamanf7e89172011-01-06 11:36:32 +00001892 if (calldata->roc)
1893 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001894 nfs4_put_open_state(calldata->state);
1895 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001896 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04001897 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07001898 kfree(calldata);
1899}
1900
Trond Myklebust88069f72009-12-08 08:33:16 -05001901static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1902 fmode_t fmode)
1903{
1904 spin_lock(&state->owner->so_lock);
1905 if (!(fmode & FMODE_READ))
1906 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1907 if (!(fmode & FMODE_WRITE))
1908 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1909 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1910 spin_unlock(&state->owner->so_lock);
1911}
1912
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001913static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001915 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 struct nfs_server *server = NFS_SERVER(calldata->inode);
1918
Trond Myklebust14516c32010-07-31 14:29:06 -04001919 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1920 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 /* hmm. we are done with the inode, and in the process of freeing
1922 * the state_owner. we keep this around to process errors
1923 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 switch (task->tk_status) {
1925 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00001926 if (calldata->roc)
1927 pnfs_roc_set_barrier(state->inode,
1928 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04001929 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001930 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001931 nfs4_close_clear_stateid_flags(state,
1932 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 break;
1934 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001935 case -NFS4ERR_OLD_STATEID:
1936 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001938 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001939 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001941 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1942 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001944 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001945 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946}
1947
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001948static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001950 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001951 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001952 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001953
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001954 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001955 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001956
Trond Myklebust88069f72009-12-08 08:33:16 -05001957 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1958 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001959 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001960 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001961 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001962 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001963 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1964 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1965 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001966 }
1967 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001968 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1969 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1970 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001971 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001972 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001973 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05001974
1975 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001976 /* Note: exit _without_ calling nfs4_close_done */
1977 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001978 return;
1979 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001980
Fred Isamanf7e89172011-01-06 11:36:32 +00001981 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001982 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00001983 if (calldata->roc &&
1984 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
1985 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
1986 task, NULL);
1987 return;
1988 }
1989 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001990
Trond Myklebust516a6af2005-10-27 22:12:41 -04001991 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001992 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001993 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Andy Adamson19ddab02009-04-01 09:22:20 -04001994 &calldata->arg.seq_args, &calldata->res.seq_res,
1995 1, task))
1996 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001997 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998}
1999
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002000static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002001 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002002 .rpc_call_done = nfs4_close_done,
2003 .rpc_release = nfs4_free_closedata,
2004};
2005
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006/*
2007 * It is possible for data to be read/written from a mem-mapped file
2008 * after the sys_close call (which hits the vfs layer as a flush).
2009 * This means that we can't safely call nfsv4 close on a file until
2010 * the inode is cleared. This in turn means that we are not good
2011 * NFSv4 citizens - we do not indicate to the server to update the file's
2012 * share state even when we are done with one of the three share
2013 * stateid's in the inode.
2014 *
2015 * NOTE: Caller must be holding the sp->so_owner semaphore!
2016 */
Al Viro643168c2011-06-22 18:20:23 -04002017int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002019 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002021 struct nfs4_state_owner *sp = state->owner;
2022 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002023 struct rpc_message msg = {
2024 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2025 .rpc_cred = state->owner->so_cred,
2026 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002027 struct rpc_task_setup task_setup_data = {
2028 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002029 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002030 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002031 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002032 .flags = RPC_TASK_ASYNC,
2033 };
Trond Myklebust95121352005-10-18 14:20:12 -07002034 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002036 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002038 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002039 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002041 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002042 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002044 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002045 if (calldata->arg.seqid == NULL)
2046 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002047 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002048 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002049 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002050 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002051 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002052 calldata->roc = roc;
Al Viro643168c2011-06-22 18:20:23 -04002053 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002054
Trond Myklebust1174dd12010-12-21 10:52:24 -05002055 msg.rpc_argp = &calldata->arg;
2056 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002057 task_setup_data.callback_data = calldata;
2058 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002059 if (IS_ERR(task))
2060 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002061 status = 0;
2062 if (wait)
2063 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002064 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002065 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002066out_free_calldata:
2067 kfree(calldata);
2068out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002069 if (roc)
2070 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002071 nfs4_put_open_state(state);
2072 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002073 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074}
2075
Trond Myklebust2b484292010-09-17 10:56:51 -04002076static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002077nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 struct nfs4_state *state;
2080
Trond Myklebust565277f2007-10-15 18:17:53 -04002081 /* Protect against concurrent sillydeletes */
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002082 state = nfs4_do_open(dir, &ctx->path, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002083 if (IS_ERR(state))
2084 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002085 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002086 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087}
2088
Trond Myklebust1185a552009-12-03 15:54:02 -05002089static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002090{
2091 if (ctx->state == NULL)
2092 return;
2093 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002094 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002095 else
Al Viro643168c2011-06-22 18:20:23 -04002096 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002097}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
2099static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2100{
Benny Halevy43652ad2009-04-01 09:21:54 -04002101 struct nfs4_server_caps_arg args = {
2102 .fhandle = fhandle,
2103 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 struct nfs4_server_caps_res res = {};
2105 struct rpc_message msg = {
2106 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002107 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 .rpc_resp = &res,
2109 };
2110 int status;
2111
Bryan Schumaker7c513052011-03-24 17:12:24 +00002112 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 if (status == 0) {
2114 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002115 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2116 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2117 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2118 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2119 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2121 server->caps |= NFS_CAP_ACLS;
2122 if (res.has_links != 0)
2123 server->caps |= NFS_CAP_HARDLINKS;
2124 if (res.has_symlinks != 0)
2125 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002126 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2127 server->caps |= NFS_CAP_FILEID;
2128 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2129 server->caps |= NFS_CAP_MODE;
2130 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2131 server->caps |= NFS_CAP_NLINK;
2132 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2133 server->caps |= NFS_CAP_OWNER;
2134 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2135 server->caps |= NFS_CAP_OWNER_GROUP;
2136 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2137 server->caps |= NFS_CAP_ATIME;
2138 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2139 server->caps |= NFS_CAP_CTIME;
2140 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2141 server->caps |= NFS_CAP_MTIME;
2142
Trond Myklebusta65318b2009-03-11 14:10:28 -04002143 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2144 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2145 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 server->acl_bitmask = res.acl_bitmask;
2147 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002148
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 return status;
2150}
2151
Trond Myklebust55a97592006-06-09 09:34:19 -04002152int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153{
2154 struct nfs4_exception exception = { };
2155 int err;
2156 do {
2157 err = nfs4_handle_exception(server,
2158 _nfs4_server_capabilities(server, fhandle),
2159 &exception);
2160 } while (exception.retry);
2161 return err;
2162}
2163
2164static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2165 struct nfs_fsinfo *info)
2166{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 struct nfs4_lookup_root_arg args = {
2168 .bitmask = nfs4_fattr_bitmap,
2169 };
2170 struct nfs4_lookup_res res = {
2171 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002172 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 .fh = fhandle,
2174 };
2175 struct rpc_message msg = {
2176 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2177 .rpc_argp = &args,
2178 .rpc_resp = &res,
2179 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002180
Trond Myklebust0e574af2005-10-27 22:12:38 -04002181 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002182 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183}
2184
2185static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2186 struct nfs_fsinfo *info)
2187{
2188 struct nfs4_exception exception = { };
2189 int err;
2190 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002191 err = _nfs4_lookup_root(server, fhandle, info);
2192 switch (err) {
2193 case 0:
2194 case -NFS4ERR_WRONGSEC:
2195 break;
2196 default:
2197 err = nfs4_handle_exception(server, err, &exception);
2198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 } while (exception.retry);
2200 return err;
2201}
2202
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002203static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2204 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2205{
2206 struct rpc_auth *auth;
2207 int ret;
2208
2209 auth = rpcauth_create(flavor, server->client);
2210 if (!auth) {
2211 ret = -EIO;
2212 goto out;
2213 }
2214 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002215out:
2216 return ret;
2217}
2218
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002219static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002220 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002222 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002223 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002225 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2226 flav_array[len] = RPC_AUTH_NULL;
2227 len += 1;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002228
2229 for (i = 0; i < len; i++) {
2230 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002231 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002232 continue;
2233 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002234 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002235 /*
2236 * -EACCESS could mean that the user doesn't have correct permissions
2237 * to access the mount. It could also mean that we tried to mount
2238 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2239 * existing mount programs don't handle -EACCES very well so it should
2240 * be mapped to -EPERM instead.
2241 */
2242 if (status == -EACCES)
2243 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002244 return status;
2245}
2246
2247/*
2248 * get the file handle for the "/" directory on the server
2249 */
2250static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2251 struct nfs_fsinfo *info)
2252{
2253 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002254 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2255 /*
2256 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2257 * by nfs4_map_errors() as this function exits.
2258 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002259 status = nfs4_find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 if (status == 0)
2261 status = nfs4_server_capabilities(server, fhandle);
2262 if (status == 0)
2263 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002264 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265}
2266
Andy Adamson533eb462011-06-13 18:25:56 -04002267static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002268/*
2269 * Get locations and (maybe) other attributes of a referral.
2270 * Note that we'll actually follow the referral later when
2271 * we detect fsid mismatch in inode revalidation
2272 */
Andy Adamson533eb462011-06-13 18:25:56 -04002273static int nfs4_get_referral(struct inode *dir, const struct qstr *name,
2274 struct nfs_fattr *fattr, struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002275{
2276 int status = -ENOMEM;
2277 struct page *page = NULL;
2278 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002279
2280 page = alloc_page(GFP_KERNEL);
2281 if (page == NULL)
2282 goto out;
2283 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2284 if (locations == NULL)
2285 goto out;
2286
Trond Myklebustc228fd32007-01-13 02:28:11 -05002287 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002288 if (status != 0)
2289 goto out;
2290 /* Make sure server returned a different fsid for the referral */
2291 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002292 dprintk("%s: server did not return a different fsid for"
2293 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002294 status = -EIO;
2295 goto out;
2296 }
Andy Adamson533eb462011-06-13 18:25:56 -04002297 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2298 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002299
Andy Adamson533eb462011-06-13 18:25:56 -04002300 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002301 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002302 memset(fhandle, 0, sizeof(struct nfs_fh));
2303out:
2304 if (page)
2305 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002306 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002307 return status;
2308}
2309
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2311{
2312 struct nfs4_getattr_arg args = {
2313 .fh = fhandle,
2314 .bitmask = server->attr_bitmask,
2315 };
2316 struct nfs4_getattr_res res = {
2317 .fattr = fattr,
2318 .server = server,
2319 };
2320 struct rpc_message msg = {
2321 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2322 .rpc_argp = &args,
2323 .rpc_resp = &res,
2324 };
2325
Trond Myklebust0e574af2005-10-27 22:12:38 -04002326 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002327 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328}
2329
2330static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2331{
2332 struct nfs4_exception exception = { };
2333 int err;
2334 do {
2335 err = nfs4_handle_exception(server,
2336 _nfs4_proc_getattr(server, fhandle, fattr),
2337 &exception);
2338 } while (exception.retry);
2339 return err;
2340}
2341
2342/*
2343 * The file is not closed if it is opened due to the a request to change
2344 * the size of the file. The open call will not be needed once the
2345 * VFS layer lookup-intents are implemented.
2346 *
2347 * Close is called when the inode is destroyed.
2348 * If we haven't opened the file for O_WRONLY, we
2349 * need to in the size_change case to obtain a stateid.
2350 *
2351 * Got race?
2352 * Because OPEN is always done by name in nfsv4, it is
2353 * possible that we opened a different file by the same
2354 * name. We can recognize this race condition, but we
2355 * can't do anything about it besides returning an error.
2356 *
2357 * This will be fixed with VFS changes (lookup-intent).
2358 */
2359static int
2360nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2361 struct iattr *sattr)
2362{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002363 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002364 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002365 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 int status;
2367
Benny Halevy8a1636c2010-07-14 15:43:57 -04002368 if (pnfs_ld_layoutret_on_setattr(inode))
2369 pnfs_return_layout(inode);
2370
Trond Myklebust0e574af2005-10-27 22:12:38 -04002371 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
Trond Myklebustd5308382005-11-04 15:33:38 -05002373 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002374 if (sattr->ia_valid & ATTR_FILE) {
2375 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002376
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002377 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002378 if (ctx) {
2379 cred = ctx->cred;
2380 state = ctx->state;
2381 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002382 }
2383
2384 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002385 if (status == 0)
2386 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 return status;
2388}
2389
Bryan Schumaker7c513052011-03-24 17:12:24 +00002390static int _nfs4_proc_lookupfh(struct rpc_clnt *clnt, struct nfs_server *server,
2391 const struct nfs_fh *dirfh, const struct qstr *name,
2392 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002393{
2394 int status;
2395 struct nfs4_lookup_arg args = {
2396 .bitmask = server->attr_bitmask,
2397 .dir_fh = dirfh,
2398 .name = name,
2399 };
2400 struct nfs4_lookup_res res = {
2401 .server = server,
2402 .fattr = fattr,
2403 .fh = fhandle,
2404 };
2405 struct rpc_message msg = {
2406 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2407 .rpc_argp = &args,
2408 .rpc_resp = &res,
2409 };
2410
2411 nfs_fattr_init(fattr);
2412
2413 dprintk("NFS call lookupfh %s\n", name->name);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002414 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002415 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002416 return status;
2417}
2418
2419static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2420 struct qstr *name, struct nfs_fh *fhandle,
2421 struct nfs_fattr *fattr)
2422{
2423 struct nfs4_exception exception = { };
2424 int err;
2425 do {
Bryan Schumaker7c513052011-03-24 17:12:24 +00002426 err = _nfs4_proc_lookupfh(server->client, server, dirfh, name, fhandle, fattr);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002427 /* FIXME: !!!! */
2428 if (err == -NFS4ERR_MOVED) {
2429 err = -EREMOTE;
2430 break;
2431 }
2432 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002433 } while (exception.retry);
2434 return err;
2435}
2436
Bryan Schumaker7c513052011-03-24 17:12:24 +00002437static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2438 const struct qstr *name, struct nfs_fh *fhandle,
2439 struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002441 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
2443 dprintk("NFS call lookup %s\n", name->name);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002444 status = _nfs4_proc_lookupfh(clnt, NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002445 if (status == -NFS4ERR_MOVED)
2446 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 dprintk("NFS reply lookup: %d\n", status);
2448 return status;
2449}
2450
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002451void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2452{
2453 memset(fh, 0, sizeof(struct nfs_fh));
2454 fattr->fsid.major = 1;
2455 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2456 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2457 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2458 fattr->nlink = 2;
2459}
2460
Bryan Schumaker7c513052011-03-24 17:12:24 +00002461static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2462 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463{
2464 struct nfs4_exception exception = { };
2465 int err;
2466 do {
2467 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumaker7c513052011-03-24 17:12:24 +00002468 _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 &exception);
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002470 if (err == -EPERM)
2471 nfs_fixup_secinfo_attributes(fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 } while (exception.retry);
2473 return err;
2474}
2475
2476static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2477{
Trond Myklebust76b32992007-08-10 17:45:11 -04002478 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 struct nfs4_accessargs args = {
2480 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002481 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002483 struct nfs4_accessres res = {
2484 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002485 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 struct rpc_message msg = {
2487 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2488 .rpc_argp = &args,
2489 .rpc_resp = &res,
2490 .rpc_cred = entry->cred,
2491 };
2492 int mode = entry->mask;
2493 int status;
2494
2495 /*
2496 * Determine which access bits we want to ask for...
2497 */
2498 if (mode & MAY_READ)
2499 args.access |= NFS4_ACCESS_READ;
2500 if (S_ISDIR(inode->i_mode)) {
2501 if (mode & MAY_WRITE)
2502 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2503 if (mode & MAY_EXEC)
2504 args.access |= NFS4_ACCESS_LOOKUP;
2505 } else {
2506 if (mode & MAY_WRITE)
2507 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2508 if (mode & MAY_EXEC)
2509 args.access |= NFS4_ACCESS_EXECUTE;
2510 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002511
2512 res.fattr = nfs_alloc_fattr();
2513 if (res.fattr == NULL)
2514 return -ENOMEM;
2515
Bryan Schumaker7c513052011-03-24 17:12:24 +00002516 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 if (!status) {
2518 entry->mask = 0;
2519 if (res.access & NFS4_ACCESS_READ)
2520 entry->mask |= MAY_READ;
2521 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2522 entry->mask |= MAY_WRITE;
2523 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2524 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002525 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002527 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 return status;
2529}
2530
2531static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2532{
2533 struct nfs4_exception exception = { };
2534 int err;
2535 do {
2536 err = nfs4_handle_exception(NFS_SERVER(inode),
2537 _nfs4_proc_access(inode, entry),
2538 &exception);
2539 } while (exception.retry);
2540 return err;
2541}
2542
2543/*
2544 * TODO: For the time being, we don't try to get any attributes
2545 * along with any of the zero-copy operations READ, READDIR,
2546 * READLINK, WRITE.
2547 *
2548 * In the case of the first three, we want to put the GETATTR
2549 * after the read-type operation -- this is because it is hard
2550 * to predict the length of a GETATTR response in v4, and thus
2551 * align the READ data correctly. This means that the GETATTR
2552 * may end up partially falling into the page cache, and we should
2553 * shift it into the 'tail' of the xdr_buf before processing.
2554 * To do this efficiently, we need to know the total length
2555 * of data received, which doesn't seem to be available outside
2556 * of the RPC layer.
2557 *
2558 * In the case of WRITE, we also want to put the GETATTR after
2559 * the operation -- in this case because we want to make sure
2560 * we get the post-operation mtime and size. This means that
2561 * we can't use xdr_encode_pages() as written: we need a variant
2562 * of it which would leave room in the 'tail' iovec.
2563 *
2564 * Both of these changes to the XDR layer would in fact be quite
2565 * minor, but I decided to leave them for a subsequent patch.
2566 */
2567static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2568 unsigned int pgbase, unsigned int pglen)
2569{
2570 struct nfs4_readlink args = {
2571 .fh = NFS_FH(inode),
2572 .pgbase = pgbase,
2573 .pglen = pglen,
2574 .pages = &page,
2575 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002576 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 struct rpc_message msg = {
2578 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2579 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002580 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 };
2582
Bryan Schumaker7c513052011-03-24 17:12:24 +00002583 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 -07002584}
2585
2586static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2587 unsigned int pgbase, unsigned int pglen)
2588{
2589 struct nfs4_exception exception = { };
2590 int err;
2591 do {
2592 err = nfs4_handle_exception(NFS_SERVER(inode),
2593 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2594 &exception);
2595 } while (exception.retry);
2596 return err;
2597}
2598
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599/*
2600 * Got race?
2601 * We will need to arrange for the VFS layer to provide an atomic open.
2602 * Until then, this create/open method is prone to inefficiency and race
2603 * conditions due to the lookup, create, and open VFS calls from sys_open()
2604 * placed on the wire.
2605 *
2606 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2607 * The file will be opened again in the subsequent VFS open call
2608 * (nfs4_proc_file_open).
2609 *
2610 * The open for read will just hang around to be used by any process that
2611 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2612 */
2613
2614static int
2615nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002616 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617{
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002618 struct path my_path = {
Trond Myklebustad389da2007-06-05 12:30:00 -04002619 .dentry = dentry,
2620 };
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002621 struct path *path = &my_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002623 struct rpc_cred *cred = NULL;
2624 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 int status = 0;
2626
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002627 if (ctx != NULL) {
2628 cred = ctx->cred;
2629 path = &ctx->path;
2630 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002632 sattr->ia_mode &= ~current_umask();
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002633 state = nfs4_do_open(dir, path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002634 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 if (IS_ERR(state)) {
2636 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002637 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002639 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002640 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002641 if (ctx != NULL)
2642 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002643 else
Al Viro643168c2011-06-22 18:20:23 -04002644 nfs4_close_sync(state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645out:
2646 return status;
2647}
2648
2649static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2650{
Trond Myklebust16e42952005-10-27 22:12:44 -04002651 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002652 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002654 .name.len = name->len,
2655 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002656 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002658 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002659 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002660 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002662 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2663 .rpc_argp = &args,
2664 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002666 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667
Trond Myklebustd3468902010-04-16 16:22:50 -04002668 res.dir_attr = nfs_alloc_fattr();
2669 if (res.dir_attr == NULL)
2670 goto out;
2671
Bryan Schumaker7c513052011-03-24 17:12:24 +00002672 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002673 if (status == 0) {
2674 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002675 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002676 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002677 nfs_free_fattr(res.dir_attr);
2678out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 return status;
2680}
2681
2682static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2683{
2684 struct nfs4_exception exception = { };
2685 int err;
2686 do {
2687 err = nfs4_handle_exception(NFS_SERVER(dir),
2688 _nfs4_proc_remove(dir, name),
2689 &exception);
2690 } while (exception.retry);
2691 return err;
2692}
2693
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002694static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002696 struct nfs_server *server = NFS_SERVER(dir);
2697 struct nfs_removeargs *args = msg->rpc_argp;
2698 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
Trond Myklebusta65318b2009-03-11 14:10:28 -04002700 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002701 res->server = server;
Benny Halevydfb4f3092010-09-24 09:17:01 -04002702 res->seq_res.sr_slot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704}
2705
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002706static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002708 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2709
Trond Myklebust14516c32010-07-31 14:29:06 -04002710 if (!nfs4_sequence_done(task, &res->seq_res))
2711 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002712 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002713 return 0;
2714 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002715 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002716 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717}
2718
Jeff Laytond3d41522010-09-17 17:31:57 -04002719static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2720{
2721 struct nfs_server *server = NFS_SERVER(dir);
2722 struct nfs_renameargs *arg = msg->rpc_argp;
2723 struct nfs_renameres *res = msg->rpc_resp;
2724
2725 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2726 arg->bitmask = server->attr_bitmask;
2727 res->server = server;
2728}
2729
2730static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2731 struct inode *new_dir)
2732{
2733 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2734
2735 if (!nfs4_sequence_done(task, &res->seq_res))
2736 return 0;
2737 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2738 return 0;
2739
2740 update_changeattr(old_dir, &res->old_cinfo);
2741 nfs_post_op_update_inode(old_dir, res->old_fattr);
2742 update_changeattr(new_dir, &res->new_cinfo);
2743 nfs_post_op_update_inode(new_dir, res->new_fattr);
2744 return 1;
2745}
2746
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2748 struct inode *new_dir, struct qstr *new_name)
2749{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002750 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002751 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 .old_dir = NFS_FH(old_dir),
2753 .new_dir = NFS_FH(new_dir),
2754 .old_name = old_name,
2755 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002756 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002758 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002759 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002760 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 struct rpc_message msg = {
2762 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2763 .rpc_argp = &arg,
2764 .rpc_resp = &res,
2765 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002766 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767
Trond Myklebust011fff72010-04-16 16:22:49 -04002768 res.old_fattr = nfs_alloc_fattr();
2769 res.new_fattr = nfs_alloc_fattr();
2770 if (res.old_fattr == NULL || res.new_fattr == NULL)
2771 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772
Bryan Schumaker7c513052011-03-24 17:12:24 +00002773 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 if (!status) {
2775 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002776 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002778 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002780out:
2781 nfs_free_fattr(res.new_fattr);
2782 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 return status;
2784}
2785
2786static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2787 struct inode *new_dir, struct qstr *new_name)
2788{
2789 struct nfs4_exception exception = { };
2790 int err;
2791 do {
2792 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2793 _nfs4_proc_rename(old_dir, old_name,
2794 new_dir, new_name),
2795 &exception);
2796 } while (exception.retry);
2797 return err;
2798}
2799
2800static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2801{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002802 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 struct nfs4_link_arg arg = {
2804 .fh = NFS_FH(inode),
2805 .dir_fh = NFS_FH(dir),
2806 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002807 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002809 struct nfs4_link_res res = {
2810 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002811 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 struct rpc_message msg = {
2813 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2814 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002815 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002817 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818
Trond Myklebust136f2622010-04-16 16:22:49 -04002819 res.fattr = nfs_alloc_fattr();
2820 res.dir_attr = nfs_alloc_fattr();
2821 if (res.fattr == NULL || res.dir_attr == NULL)
2822 goto out;
2823
Bryan Schumaker7c513052011-03-24 17:12:24 +00002824 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002825 if (!status) {
2826 update_changeattr(dir, &res.cinfo);
2827 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002828 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002829 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002830out:
2831 nfs_free_fattr(res.dir_attr);
2832 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 return status;
2834}
2835
2836static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2837{
2838 struct nfs4_exception exception = { };
2839 int err;
2840 do {
2841 err = nfs4_handle_exception(NFS_SERVER(inode),
2842 _nfs4_proc_link(inode, dir, name),
2843 &exception);
2844 } while (exception.retry);
2845 return err;
2846}
2847
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002848struct nfs4_createdata {
2849 struct rpc_message msg;
2850 struct nfs4_create_arg arg;
2851 struct nfs4_create_res res;
2852 struct nfs_fh fh;
2853 struct nfs_fattr fattr;
2854 struct nfs_fattr dir_fattr;
2855};
2856
2857static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2858 struct qstr *name, struct iattr *sattr, u32 ftype)
2859{
2860 struct nfs4_createdata *data;
2861
2862 data = kzalloc(sizeof(*data), GFP_KERNEL);
2863 if (data != NULL) {
2864 struct nfs_server *server = NFS_SERVER(dir);
2865
2866 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2867 data->msg.rpc_argp = &data->arg;
2868 data->msg.rpc_resp = &data->res;
2869 data->arg.dir_fh = NFS_FH(dir);
2870 data->arg.server = server;
2871 data->arg.name = name;
2872 data->arg.attrs = sattr;
2873 data->arg.ftype = ftype;
2874 data->arg.bitmask = server->attr_bitmask;
2875 data->res.server = server;
2876 data->res.fh = &data->fh;
2877 data->res.fattr = &data->fattr;
2878 data->res.dir_fattr = &data->dir_fattr;
2879 nfs_fattr_init(data->res.fattr);
2880 nfs_fattr_init(data->res.dir_fattr);
2881 }
2882 return data;
2883}
2884
2885static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2886{
Bryan Schumaker7c513052011-03-24 17:12:24 +00002887 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00002888 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002889 if (status == 0) {
2890 update_changeattr(dir, &data->res.dir_cinfo);
2891 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2892 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2893 }
2894 return status;
2895}
2896
2897static void nfs4_free_createdata(struct nfs4_createdata *data)
2898{
2899 kfree(data);
2900}
2901
Chuck Lever4f390c12006-08-22 20:06:22 -04002902static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002903 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002905 struct nfs4_createdata *data;
2906 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907
Chuck Lever94a6d752006-08-22 20:06:23 -04002908 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002909 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002910
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002911 status = -ENOMEM;
2912 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2913 if (data == NULL)
2914 goto out;
2915
2916 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2917 data->arg.u.symlink.pages = &page;
2918 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002920 status = nfs4_do_create(dir, dentry, data);
2921
2922 nfs4_free_createdata(data);
2923out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 return status;
2925}
2926
Chuck Lever4f390c12006-08-22 20:06:22 -04002927static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002928 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929{
2930 struct nfs4_exception exception = { };
2931 int err;
2932 do {
2933 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002934 _nfs4_proc_symlink(dir, dentry, page,
2935 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 &exception);
2937 } while (exception.retry);
2938 return err;
2939}
2940
2941static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2942 struct iattr *sattr)
2943{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002944 struct nfs4_createdata *data;
2945 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002947 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2948 if (data == NULL)
2949 goto out;
2950
2951 status = nfs4_do_create(dir, dentry, data);
2952
2953 nfs4_free_createdata(data);
2954out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 return status;
2956}
2957
2958static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2959 struct iattr *sattr)
2960{
2961 struct nfs4_exception exception = { };
2962 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002963
2964 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 do {
2966 err = nfs4_handle_exception(NFS_SERVER(dir),
2967 _nfs4_proc_mkdir(dir, dentry, sattr),
2968 &exception);
2969 } while (exception.retry);
2970 return err;
2971}
2972
2973static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002974 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975{
2976 struct inode *dir = dentry->d_inode;
2977 struct nfs4_readdir_arg args = {
2978 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002979 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 .pgbase = 0,
2981 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002982 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04002983 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 };
2985 struct nfs4_readdir_res res;
2986 struct rpc_message msg = {
2987 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2988 .rpc_argp = &args,
2989 .rpc_resp = &res,
2990 .rpc_cred = cred,
2991 };
2992 int status;
2993
Harvey Harrison3110ff82008-05-02 13:42:44 -07002994 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002995 dentry->d_parent->d_name.name,
2996 dentry->d_name.name,
2997 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2999 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003000 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 -05003001 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003003 status += args.pgbase;
3004 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003005
3006 nfs_invalidate_atime(dir);
3007
Harvey Harrison3110ff82008-05-02 13:42:44 -07003008 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 return status;
3010}
3011
3012static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003013 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014{
3015 struct nfs4_exception exception = { };
3016 int err;
3017 do {
3018 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3019 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003020 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 &exception);
3022 } while (exception.retry);
3023 return err;
3024}
3025
3026static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3027 struct iattr *sattr, dev_t rdev)
3028{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003029 struct nfs4_createdata *data;
3030 int mode = sattr->ia_mode;
3031 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032
3033 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3034 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003035
3036 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3037 if (data == NULL)
3038 goto out;
3039
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003041 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003043 data->arg.ftype = NF4BLK;
3044 data->arg.u.device.specdata1 = MAJOR(rdev);
3045 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 }
3047 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003048 data->arg.ftype = NF4CHR;
3049 data->arg.u.device.specdata1 = MAJOR(rdev);
3050 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003053 status = nfs4_do_create(dir, dentry, data);
3054
3055 nfs4_free_createdata(data);
3056out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 return status;
3058}
3059
3060static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3061 struct iattr *sattr, dev_t rdev)
3062{
3063 struct nfs4_exception exception = { };
3064 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003065
3066 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 do {
3068 err = nfs4_handle_exception(NFS_SERVER(dir),
3069 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3070 &exception);
3071 } while (exception.retry);
3072 return err;
3073}
3074
3075static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3076 struct nfs_fsstat *fsstat)
3077{
3078 struct nfs4_statfs_arg args = {
3079 .fh = fhandle,
3080 .bitmask = server->attr_bitmask,
3081 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003082 struct nfs4_statfs_res res = {
3083 .fsstat = fsstat,
3084 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 struct rpc_message msg = {
3086 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3087 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003088 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 };
3090
Trond Myklebust0e574af2005-10-27 22:12:38 -04003091 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003092 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093}
3094
3095static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3096{
3097 struct nfs4_exception exception = { };
3098 int err;
3099 do {
3100 err = nfs4_handle_exception(server,
3101 _nfs4_proc_statfs(server, fhandle, fsstat),
3102 &exception);
3103 } while (exception.retry);
3104 return err;
3105}
3106
3107static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3108 struct nfs_fsinfo *fsinfo)
3109{
3110 struct nfs4_fsinfo_arg args = {
3111 .fh = fhandle,
3112 .bitmask = server->attr_bitmask,
3113 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003114 struct nfs4_fsinfo_res res = {
3115 .fsinfo = fsinfo,
3116 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 struct rpc_message msg = {
3118 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3119 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003120 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 };
3122
Bryan Schumaker7c513052011-03-24 17:12:24 +00003123 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124}
3125
3126static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3127{
3128 struct nfs4_exception exception = { };
3129 int err;
3130
3131 do {
3132 err = nfs4_handle_exception(server,
3133 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3134 &exception);
3135 } while (exception.retry);
3136 return err;
3137}
3138
3139static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3140{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003141 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3143}
3144
3145static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3146 struct nfs_pathconf *pathconf)
3147{
3148 struct nfs4_pathconf_arg args = {
3149 .fh = fhandle,
3150 .bitmask = server->attr_bitmask,
3151 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003152 struct nfs4_pathconf_res res = {
3153 .pathconf = pathconf,
3154 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 struct rpc_message msg = {
3156 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3157 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003158 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 };
3160
3161 /* None of the pathconf attributes are mandatory to implement */
3162 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3163 memset(pathconf, 0, sizeof(*pathconf));
3164 return 0;
3165 }
3166
Trond Myklebust0e574af2005-10-27 22:12:38 -04003167 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003168 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169}
3170
3171static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3172 struct nfs_pathconf *pathconf)
3173{
3174 struct nfs4_exception exception = { };
3175 int err;
3176
3177 do {
3178 err = nfs4_handle_exception(server,
3179 _nfs4_proc_pathconf(server, fhandle, pathconf),
3180 &exception);
3181 } while (exception.retry);
3182 return err;
3183}
3184
Benny Halevyd20581a2011-05-22 19:52:03 +03003185void __nfs4_read_done_cb(struct nfs_read_data *data)
3186{
3187 nfs_invalidate_atime(data->inode);
3188}
3189
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003190static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191{
Trond Myklebustec06c092006-03-20 13:44:27 -05003192 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003194 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003195 nfs_restart_rpc(task, server->nfs_client);
Trond Myklebustec06c092006-03-20 13:44:27 -05003196 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003198
Benny Halevyd20581a2011-05-22 19:52:03 +03003199 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003201 renew_lease(server, data->timestamp);
3202 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203}
3204
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003205static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3206{
3207
3208 dprintk("--> %s\n", __func__);
3209
3210 if (!nfs4_sequence_done(task, &data->res.seq_res))
3211 return -EAGAIN;
3212
Benny Halevyd20581a2011-05-22 19:52:03 +03003213 return data->read_done_cb ? data->read_done_cb(task, data) :
3214 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003215}
3216
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003217static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003220 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003221 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222}
3223
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003224/* Reset the the nfs_read_data to send the read to the MDS. */
3225void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3226{
3227 dprintk("%s Reset task for i/o through\n", __func__);
3228 put_lseg(data->lseg);
3229 data->lseg = NULL;
3230 /* offsets will differ in the dense stripe case */
3231 data->args.offset = data->mds_offset;
3232 data->ds_clp = NULL;
3233 data->args.fh = NFS_FH(data->inode);
3234 data->read_done_cb = nfs4_read_done_cb;
3235 task->tk_ops = data->mds_ops;
3236 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3237}
3238EXPORT_SYMBOL_GPL(nfs4_reset_read);
3239
Fred Isamanb029bc92011-03-03 15:13:42 +00003240static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 struct inode *inode = data->inode;
3243
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003244 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003245 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003246 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003248 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003250 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003251 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003252 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253}
3254
Fred Isamanb029bc92011-03-03 15:13:42 +00003255static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3256{
3257 if (!nfs4_sequence_done(task, &data->res.seq_res))
3258 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003259 return data->write_done_cb ? data->write_done_cb(task, data) :
3260 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003261}
3262
Fred Isamana69aef12011-03-03 15:13:47 +00003263/* Reset the the nfs_write_data to send the write to the MDS. */
3264void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3265{
3266 dprintk("%s Reset task for i/o through\n", __func__);
3267 put_lseg(data->lseg);
3268 data->lseg = NULL;
3269 data->ds_clp = NULL;
3270 data->write_done_cb = nfs4_write_done_cb;
3271 data->args.fh = NFS_FH(data->inode);
3272 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3273 data->args.offset = data->mds_offset;
3274 data->res.fattr = &data->fattr;
3275 task->tk_ops = data->mds_ops;
3276 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3277}
3278EXPORT_SYMBOL_GPL(nfs4_reset_write);
3279
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003280static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003282 struct nfs_server *server = NFS_SERVER(data->inode);
3283
Fred Isaman7ffd1062011-03-03 15:13:46 +00003284 if (data->lseg) {
3285 data->args.bitmask = NULL;
3286 data->res.fattr = NULL;
3287 } else
3288 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isamanb029bc92011-03-03 15:13:42 +00003289 if (!data->write_done_cb)
3290 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003291 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 data->timestamp = jiffies;
3293
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003294 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295}
3296
Fred Isaman5f452432011-03-23 13:27:46 +00003297static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003300
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003301 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003302 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003303 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003305 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003306 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307}
3308
Fred Isaman5f452432011-03-23 13:27:46 +00003309static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3310{
3311 if (!nfs4_sequence_done(task, &data->res.seq_res))
3312 return -EAGAIN;
3313 return data->write_done_cb(task, data);
3314}
3315
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003316static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003318 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003319
3320 if (data->lseg) {
3321 data->args.bitmask = NULL;
3322 data->res.fattr = NULL;
3323 } else
3324 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isaman5f452432011-03-23 13:27:46 +00003325 if (!data->write_done_cb)
3326 data->write_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003327 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003328 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329}
3330
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003331struct nfs4_renewdata {
3332 struct nfs_client *client;
3333 unsigned long timestamp;
3334};
3335
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336/*
3337 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3338 * standalone procedure for queueing an asynchronous RENEW.
3339 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003340static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003341{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003342 struct nfs4_renewdata *data = calldata;
3343 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003344
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003345 if (atomic_read(&clp->cl_count) > 1)
3346 nfs4_schedule_state_renewal(clp);
3347 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003348 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003349}
3350
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003351static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003353 struct nfs4_renewdata *data = calldata;
3354 struct nfs_client *clp = data->client;
3355 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356
3357 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003358 /* Unless we're shutting down, schedule state recovery! */
3359 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003360 nfs4_schedule_lease_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 return;
3362 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003363 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364}
3365
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003366static const struct rpc_call_ops nfs4_renew_ops = {
3367 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003368 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003369};
3370
David Howellsadfa6f92006-08-22 20:06:08 -04003371int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372{
3373 struct rpc_message msg = {
3374 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3375 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003376 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003378 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003380 if (!atomic_inc_not_zero(&clp->cl_count))
3381 return -EIO;
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003382 data = kmalloc(sizeof(*data), GFP_KERNEL);
3383 if (data == NULL)
3384 return -ENOMEM;
3385 data->client = clp;
3386 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003388 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389}
3390
David Howellsadfa6f92006-08-22 20:06:08 -04003391int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392{
3393 struct rpc_message msg = {
3394 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3395 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003396 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 };
3398 unsigned long now = jiffies;
3399 int status;
3400
3401 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3402 if (status < 0)
3403 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003404 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 return 0;
3406}
3407
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003408static inline int nfs4_server_supports_acls(struct nfs_server *server)
3409{
3410 return (server->caps & NFS_CAP_ACLS)
3411 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3412 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3413}
3414
3415/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3416 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3417 * the stack.
3418 */
3419#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3420
3421static void buf_to_pages(const void *buf, size_t buflen,
3422 struct page **pages, unsigned int *pgbase)
3423{
3424 const void *p = buf;
3425
3426 *pgbase = offset_in_page(buf);
3427 p -= *pgbase;
3428 while (p < buf + buflen) {
3429 *(pages++) = virt_to_page(p);
3430 p += PAGE_CACHE_SIZE;
3431 }
3432}
3433
Neil Hormane9e3d722011-03-04 19:26:03 -05003434static int buf_to_pages_noslab(const void *buf, size_t buflen,
3435 struct page **pages, unsigned int *pgbase)
3436{
3437 struct page *newpage, **spages;
3438 int rc = 0;
3439 size_t len;
3440 spages = pages;
3441
3442 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003443 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003444 newpage = alloc_page(GFP_KERNEL);
3445
3446 if (newpage == NULL)
3447 goto unwind;
3448 memcpy(page_address(newpage), buf, len);
3449 buf += len;
3450 buflen -= len;
3451 *pages++ = newpage;
3452 rc++;
3453 } while (buflen != 0);
3454
3455 return rc;
3456
3457unwind:
3458 for(; rc > 0; rc--)
3459 __free_page(spages[rc-1]);
3460 return -ENOMEM;
3461}
3462
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003463struct nfs4_cached_acl {
3464 int cached;
3465 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003466 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003467};
3468
3469static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003470{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003471 struct nfs_inode *nfsi = NFS_I(inode);
3472
3473 spin_lock(&inode->i_lock);
3474 kfree(nfsi->nfs4_acl);
3475 nfsi->nfs4_acl = acl;
3476 spin_unlock(&inode->i_lock);
3477}
3478
3479static void nfs4_zap_acl_attr(struct inode *inode)
3480{
3481 nfs4_set_cached_acl(inode, NULL);
3482}
3483
3484static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3485{
3486 struct nfs_inode *nfsi = NFS_I(inode);
3487 struct nfs4_cached_acl *acl;
3488 int ret = -ENOENT;
3489
3490 spin_lock(&inode->i_lock);
3491 acl = nfsi->nfs4_acl;
3492 if (acl == NULL)
3493 goto out;
3494 if (buf == NULL) /* user is just asking for length */
3495 goto out_len;
3496 if (acl->cached == 0)
3497 goto out;
3498 ret = -ERANGE; /* see getxattr(2) man page */
3499 if (acl->len > buflen)
3500 goto out;
3501 memcpy(buf, acl->data, acl->len);
3502out_len:
3503 ret = acl->len;
3504out:
3505 spin_unlock(&inode->i_lock);
3506 return ret;
3507}
3508
3509static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3510{
3511 struct nfs4_cached_acl *acl;
3512
3513 if (buf && acl_len <= PAGE_SIZE) {
3514 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3515 if (acl == NULL)
3516 goto out;
3517 acl->cached = 1;
3518 memcpy(acl->data, buf, acl_len);
3519 } else {
3520 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3521 if (acl == NULL)
3522 goto out;
3523 acl->cached = 0;
3524 }
3525 acl->len = acl_len;
3526out:
3527 nfs4_set_cached_acl(inode, acl);
3528}
3529
Trond Myklebust16b42892006-08-24 12:27:15 -04003530static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003531{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003532 struct page *pages[NFS4ACL_MAXPAGES];
3533 struct nfs_getaclargs args = {
3534 .fh = NFS_FH(inode),
3535 .acl_pages = pages,
3536 .acl_len = buflen,
3537 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003538 struct nfs_getaclres res = {
3539 .acl_len = buflen,
3540 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003541 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003542 struct rpc_message msg = {
3543 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3544 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003545 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003546 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003547 struct page *localpage = NULL;
3548 int ret;
3549
3550 if (buflen < PAGE_SIZE) {
3551 /* As long as we're doing a round trip to the server anyway,
3552 * let's be prepared for a page of acl data. */
3553 localpage = alloc_page(GFP_KERNEL);
3554 resp_buf = page_address(localpage);
3555 if (localpage == NULL)
3556 return -ENOMEM;
3557 args.acl_pages[0] = localpage;
3558 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003559 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003560 } else {
3561 resp_buf = buf;
3562 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3563 }
Bryan Schumaker7c513052011-03-24 17:12:24 +00003564 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 +00003565 if (ret)
3566 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003567 if (res.acl_len > args.acl_len)
3568 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003569 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003570 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003571 if (buf) {
3572 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003573 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003574 goto out_free;
3575 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003576 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003577 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003578 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003579out_free:
3580 if (localpage)
3581 __free_page(localpage);
3582 return ret;
3583}
3584
Trond Myklebust16b42892006-08-24 12:27:15 -04003585static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3586{
3587 struct nfs4_exception exception = { };
3588 ssize_t ret;
3589 do {
3590 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3591 if (ret >= 0)
3592 break;
3593 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3594 } while (exception.retry);
3595 return ret;
3596}
3597
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003598static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3599{
3600 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003601 int ret;
3602
3603 if (!nfs4_server_supports_acls(server))
3604 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003605 ret = nfs_revalidate_inode(server, inode);
3606 if (ret < 0)
3607 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003608 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3609 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003610 ret = nfs4_read_cached_acl(inode, buf, buflen);
3611 if (ret != -ENOENT)
3612 return ret;
3613 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003614}
3615
Trond Myklebust16b42892006-08-24 12:27:15 -04003616static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003617{
3618 struct nfs_server *server = NFS_SERVER(inode);
3619 struct page *pages[NFS4ACL_MAXPAGES];
3620 struct nfs_setaclargs arg = {
3621 .fh = NFS_FH(inode),
3622 .acl_pages = pages,
3623 .acl_len = buflen,
3624 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003625 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003626 struct rpc_message msg = {
3627 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3628 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003629 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003630 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003631 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003632
3633 if (!nfs4_server_supports_acls(server))
3634 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003635 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3636 if (i < 0)
3637 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003638 nfs_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003639 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003640
3641 /*
3642 * Free each page after tx, so the only ref left is
3643 * held by the network stack
3644 */
3645 for (; i > 0; i--)
3646 put_page(pages[i-1]);
3647
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003648 /*
3649 * Acl update can result in inode attribute update.
3650 * so mark the attribute cache invalid.
3651 */
3652 spin_lock(&inode->i_lock);
3653 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3654 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003655 nfs_access_zap_cache(inode);
3656 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003657 return ret;
3658}
3659
Trond Myklebust16b42892006-08-24 12:27:15 -04003660static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3661{
3662 struct nfs4_exception exception = { };
3663 int err;
3664 do {
3665 err = nfs4_handle_exception(NFS_SERVER(inode),
3666 __nfs4_proc_set_acl(inode, buf, buflen),
3667 &exception);
3668 } while (exception.retry);
3669 return err;
3670}
3671
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003673nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003675 struct nfs_client *clp = server->nfs_client;
3676
3677 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 return 0;
3679 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003680 case -NFS4ERR_ADMIN_REVOKED:
3681 case -NFS4ERR_BAD_STATEID:
3682 case -NFS4ERR_OPENMODE:
3683 if (state == NULL)
3684 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003685 nfs4_schedule_stateid_recovery(server, state);
3686 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003687 case -NFS4ERR_EXPIRED:
3688 if (state != NULL)
3689 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003691 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003692 nfs4_schedule_lease_recovery(clp);
3693 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003694#if defined(CONFIG_NFS_V4_1)
3695 case -NFS4ERR_BADSESSION:
3696 case -NFS4ERR_BADSLOT:
3697 case -NFS4ERR_BAD_HIGH_SLOT:
3698 case -NFS4ERR_DEADSESSION:
3699 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3700 case -NFS4ERR_SEQ_FALSE_RETRY:
3701 case -NFS4ERR_SEQ_MISORDERED:
3702 dprintk("%s ERROR %d, Reset session\n", __func__,
3703 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003704 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -04003705 task->tk_status = 0;
3706 return -EAGAIN;
3707#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003709 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003710 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003711 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3713 task->tk_status = 0;
3714 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003715 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 case -NFS4ERR_OLD_STATEID:
3717 task->tk_status = 0;
3718 return -EAGAIN;
3719 }
3720 task->tk_status = nfs4_map_errors(task->tk_status);
3721 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003722wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003723 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003724 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3725 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3726 task->tk_status = 0;
3727 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728}
3729
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003730int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3731 unsigned short port, struct rpc_cred *cred,
3732 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733{
3734 nfs4_verifier sc_verifier;
3735 struct nfs4_setclientid setclientid = {
3736 .sc_verifier = &sc_verifier,
3737 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003738 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 };
3740 struct rpc_message msg = {
3741 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3742 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003743 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003744 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 };
Al Virobc4785c2006-10-19 23:28:51 -07003746 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 int loop = 0;
3748 int status;
3749
Al Virobc4785c2006-10-19 23:28:51 -07003750 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3752 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3753
3754 for(;;) {
3755 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003756 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003757 clp->cl_ipaddr,
3758 rpc_peeraddr2str(clp->cl_rpcclient,
3759 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003760 rpc_peeraddr2str(clp->cl_rpcclient,
3761 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003762 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 clp->cl_id_uniquifier);
3764 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003765 sizeof(setclientid.sc_netid),
3766 rpc_peeraddr2str(clp->cl_rpcclient,
3767 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003769 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 clp->cl_ipaddr, port >> 8, port & 255);
3771
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003772 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 if (status != -NFS4ERR_CLID_INUSE)
3774 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003775 if (loop != 0) {
3776 ++clp->cl_id_uniquifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003778 }
3779 ++loop;
3780 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 }
3782 return status;
3783}
3784
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003785int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003786 struct nfs4_setclientid_res *arg,
3787 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788{
3789 struct nfs_fsinfo fsinfo;
3790 struct rpc_message msg = {
3791 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003792 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003794 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 };
3796 unsigned long now;
3797 int status;
3798
3799 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003800 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 if (status == 0) {
3802 spin_lock(&clp->cl_lock);
3803 clp->cl_lease_time = fsinfo.lease_time * HZ;
3804 clp->cl_last_renewal = now;
3805 spin_unlock(&clp->cl_lock);
3806 }
3807 return status;
3808}
3809
Trond Myklebustfe650402006-01-03 09:55:18 +01003810struct nfs4_delegreturndata {
3811 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003812 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003813 struct nfs_fh fh;
3814 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003815 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003816 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003817 int rpc_status;
3818};
3819
Trond Myklebustfe650402006-01-03 09:55:18 +01003820static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3821{
3822 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003823
Trond Myklebust14516c32010-07-31 14:29:06 -04003824 if (!nfs4_sequence_done(task, &data->res.seq_res))
3825 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003826
Ricardo Labiaga79708862009-12-07 09:23:21 -05003827 switch (task->tk_status) {
3828 case -NFS4ERR_STALE_STATEID:
3829 case -NFS4ERR_EXPIRED:
3830 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003831 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003832 break;
3833 default:
3834 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3835 -EAGAIN) {
3836 nfs_restart_rpc(task, data->res.server->nfs_client);
3837 return;
3838 }
3839 }
3840 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003841}
3842
3843static void nfs4_delegreturn_release(void *calldata)
3844{
Trond Myklebustfe650402006-01-03 09:55:18 +01003845 kfree(calldata);
3846}
3847
Andy Adamson938e1012009-04-01 09:22:28 -04003848#if defined(CONFIG_NFS_V4_1)
3849static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3850{
3851 struct nfs4_delegreturndata *d_data;
3852
3853 d_data = (struct nfs4_delegreturndata *)data;
3854
Trond Myklebust035168a2010-06-16 09:52:26 -04003855 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003856 &d_data->args.seq_args,
3857 &d_data->res.seq_res, 1, task))
3858 return;
3859 rpc_call_start(task);
3860}
3861#endif /* CONFIG_NFS_V4_1 */
3862
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003863static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003864#if defined(CONFIG_NFS_V4_1)
3865 .rpc_call_prepare = nfs4_delegreturn_prepare,
3866#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003867 .rpc_call_done = nfs4_delegreturn_done,
3868 .rpc_release = nfs4_delegreturn_release,
3869};
3870
Trond Myklebuste6f81072008-01-24 18:14:34 -05003871static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003872{
3873 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003874 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003875 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003876 struct rpc_message msg = {
3877 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3878 .rpc_cred = cred,
3879 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003880 struct rpc_task_setup task_setup_data = {
3881 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003882 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003883 .callback_ops = &nfs4_delegreturn_ops,
3884 .flags = RPC_TASK_ASYNC,
3885 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003886 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003887
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003888 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003889 if (data == NULL)
3890 return -ENOMEM;
3891 data->args.fhandle = &data->fh;
3892 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003893 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003894 nfs_copy_fh(&data->fh, NFS_FH(inode));
3895 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003896 data->res.fattr = &data->fattr;
3897 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003898 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003899 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003900 data->rpc_status = 0;
3901
Trond Myklebustc970aa82007-07-14 15:39:59 -04003902 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05003903 msg.rpc_argp = &data->args;
3904 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003905 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003906 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003907 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003908 if (!issync)
3909 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003910 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003911 if (status != 0)
3912 goto out;
3913 status = data->rpc_status;
3914 if (status != 0)
3915 goto out;
3916 nfs_refresh_inode(inode, &data->fattr);
3917out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003918 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003919 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920}
3921
Trond Myklebuste6f81072008-01-24 18:14:34 -05003922int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923{
3924 struct nfs_server *server = NFS_SERVER(inode);
3925 struct nfs4_exception exception = { };
3926 int err;
3927 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003928 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 switch (err) {
3930 case -NFS4ERR_STALE_STATEID:
3931 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 case 0:
3933 return 0;
3934 }
3935 err = nfs4_handle_exception(server, err, &exception);
3936 } while (exception.retry);
3937 return err;
3938}
3939
3940#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3941#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3942
3943/*
3944 * sleep, with exponential backoff, and retry the LOCK operation.
3945 */
3946static unsigned long
3947nfs4_set_lock_task_retry(unsigned long timeout)
3948{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003949 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 timeout <<= 1;
3951 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3952 return NFS4_LOCK_MAXTIMEOUT;
3953 return timeout;
3954}
3955
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3957{
3958 struct inode *inode = state->inode;
3959 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003960 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003961 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003963 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003965 struct nfs_lockt_res res = {
3966 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 };
3968 struct rpc_message msg = {
3969 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3970 .rpc_argp = &arg,
3971 .rpc_resp = &res,
3972 .rpc_cred = state->owner->so_cred,
3973 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 struct nfs4_lock_state *lsp;
3975 int status;
3976
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003977 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003978 status = nfs4_set_lock_state(state, request);
3979 if (status != 0)
3980 goto out;
3981 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003982 arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05003983 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003984 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003985 switch (status) {
3986 case 0:
3987 request->fl_type = F_UNLCK;
3988 break;
3989 case -NFS4ERR_DENIED:
3990 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003992 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003993out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 return status;
3995}
3996
3997static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3998{
3999 struct nfs4_exception exception = { };
4000 int err;
4001
4002 do {
4003 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4004 _nfs4_proc_getlk(state, cmd, request),
4005 &exception);
4006 } while (exception.retry);
4007 return err;
4008}
4009
4010static int do_vfs_lock(struct file *file, struct file_lock *fl)
4011{
4012 int res = 0;
4013 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4014 case FL_POSIX:
4015 res = posix_lock_file_wait(file, fl);
4016 break;
4017 case FL_FLOCK:
4018 res = flock_lock_file_wait(file, fl);
4019 break;
4020 default:
4021 BUG();
4022 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 return res;
4024}
4025
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004026struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004027 struct nfs_locku_args arg;
4028 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004029 struct nfs4_lock_state *lsp;
4030 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004031 struct file_lock fl;
4032 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004033 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004034};
4035
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004036static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4037 struct nfs_open_context *ctx,
4038 struct nfs4_lock_state *lsp,
4039 struct nfs_seqid *seqid)
4040{
4041 struct nfs4_unlockdata *p;
4042 struct inode *inode = lsp->ls_state->inode;
4043
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004044 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004045 if (p == NULL)
4046 return NULL;
4047 p->arg.fh = NFS_FH(inode);
4048 p->arg.fl = &p->fl;
4049 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004050 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004051 p->arg.stateid = &lsp->ls_stateid;
4052 p->lsp = lsp;
4053 atomic_inc(&lsp->ls_count);
4054 /* Ensure we don't close file until we're done freeing locks! */
4055 p->ctx = get_nfs_open_context(ctx);
4056 memcpy(&p->fl, fl, sizeof(p->fl));
4057 p->server = NFS_SERVER(inode);
4058 return p;
4059}
4060
Trond Myklebust06f814a2006-01-03 09:55:07 +01004061static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004062{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004063 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004064 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004065 nfs4_put_lock_state(calldata->lsp);
4066 put_nfs_open_context(calldata->ctx);
4067 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004068}
4069
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004070static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004071{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004072 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004073
Trond Myklebust14516c32010-07-31 14:29:06 -04004074 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4075 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004076 switch (task->tk_status) {
4077 case 0:
4078 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004079 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004080 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01004081 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004082 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004083 case -NFS4ERR_BAD_STATEID:
4084 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004085 case -NFS4ERR_STALE_STATEID:
4086 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004087 break;
4088 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004089 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebust0110ee12009-12-07 09:00:24 -05004090 nfs_restart_rpc(task,
Trond Myklebustd61e6122009-12-05 19:32:19 -05004091 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004092 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004093}
4094
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004095static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004096{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004097 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004099 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004100 return;
4101 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004102 /* Note: exit _without_ running nfs4_locku_done */
4103 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004104 return;
4105 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004106 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004107 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004108 &calldata->arg.seq_args,
4109 &calldata->res.seq_res, 1, task))
4110 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004111 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112}
4113
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004114static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004115 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004116 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004117 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004118};
4119
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004120static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4121 struct nfs_open_context *ctx,
4122 struct nfs4_lock_state *lsp,
4123 struct nfs_seqid *seqid)
4124{
4125 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004126 struct rpc_message msg = {
4127 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4128 .rpc_cred = ctx->cred,
4129 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004130 struct rpc_task_setup task_setup_data = {
4131 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004132 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004133 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004134 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004135 .flags = RPC_TASK_ASYNC,
4136 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004137
Frank Filz137d6ac2007-07-09 15:32:29 -07004138 /* Ensure this is an unlock - when canceling a lock, the
4139 * canceled lock is passed in, and it won't be an unlock.
4140 */
4141 fl->fl_type = F_UNLCK;
4142
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004143 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4144 if (data == NULL) {
4145 nfs_free_seqid(seqid);
4146 return ERR_PTR(-ENOMEM);
4147 }
4148
Trond Myklebust1174dd12010-12-21 10:52:24 -05004149 msg.rpc_argp = &data->arg;
4150 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004151 task_setup_data.callback_data = data;
4152 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004153}
4154
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4156{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004157 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004158 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004159 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004160 struct rpc_task *task;
4161 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004162 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163
Trond Myklebust9b073572006-06-29 16:38:34 -04004164 status = nfs4_set_lock_state(state, request);
4165 /* Unlock _before_ we do the RPC call */
4166 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004167 down_read(&nfsi->rwsem);
4168 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4169 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004170 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004171 }
4172 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004173 if (status != 0)
4174 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004175 /* Is this a delegated lock? */
4176 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004177 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004178 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004179 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004180 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004181 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004182 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004183 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004184 status = PTR_ERR(task);
4185 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004186 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004187 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004188 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004189out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004190 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004191 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192}
4193
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004194struct nfs4_lockdata {
4195 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004196 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004197 struct nfs4_lock_state *lsp;
4198 struct nfs_open_context *ctx;
4199 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004200 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004201 int rpc_status;
4202 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004203 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004204};
4205
4206static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004207 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4208 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004209{
4210 struct nfs4_lockdata *p;
4211 struct inode *inode = lsp->ls_state->inode;
4212 struct nfs_server *server = NFS_SERVER(inode);
4213
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004214 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004215 if (p == NULL)
4216 return NULL;
4217
4218 p->arg.fh = NFS_FH(inode);
4219 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004220 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004221 if (p->arg.open_seqid == NULL)
4222 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004223 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004224 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004225 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004226 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004227 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004228 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004229 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004230 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004231 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004232 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004233 atomic_inc(&lsp->ls_count);
4234 p->ctx = get_nfs_open_context(ctx);
4235 memcpy(&p->fl, fl, sizeof(p->fl));
4236 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004237out_free_seqid:
4238 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004239out_free:
4240 kfree(p);
4241 return NULL;
4242}
4243
4244static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4245{
4246 struct nfs4_lockdata *data = calldata;
4247 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248
Harvey Harrison3110ff82008-05-02 13:42:44 -07004249 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004250 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4251 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004252 /* Do we need to do an open_to_lock_owner? */
4253 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004254 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4255 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004256 data->arg.open_stateid = &state->stateid;
4257 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004258 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004259 } else
4260 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004261 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004262 if (nfs4_setup_sequence(data->server,
4263 &data->arg.seq_args,
Andy Adamson66179ef2009-04-01 09:22:22 -04004264 &data->res.seq_res, 1, task))
4265 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004266 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004267 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004268}
4269
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004270static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4271{
4272 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4273 nfs4_lock_prepare(task, calldata);
4274}
4275
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004276static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4277{
4278 struct nfs4_lockdata *data = calldata;
4279
Harvey Harrison3110ff82008-05-02 13:42:44 -07004280 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004281
Trond Myklebust14516c32010-07-31 14:29:06 -04004282 if (!nfs4_sequence_done(task, &data->res.seq_res))
4283 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004284
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004285 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004286 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004287 if (data->rpc_status == 0)
4288 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4289 else
4290 goto out;
4291 }
4292 if (data->rpc_status == 0) {
4293 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4294 sizeof(data->lsp->ls_stateid.data));
4295 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04004296 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004297 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004298out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004299 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004300}
4301
4302static void nfs4_lock_release(void *calldata)
4303{
4304 struct nfs4_lockdata *data = calldata;
4305
Harvey Harrison3110ff82008-05-02 13:42:44 -07004306 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004307 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004308 if (data->cancelled != 0) {
4309 struct rpc_task *task;
4310 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4311 data->arg.lock_seqid);
4312 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004313 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004314 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004315 } else
4316 nfs_free_seqid(data->arg.lock_seqid);
4317 nfs4_put_lock_state(data->lsp);
4318 put_nfs_open_context(data->ctx);
4319 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004320 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004321}
4322
4323static const struct rpc_call_ops nfs4_lock_ops = {
4324 .rpc_call_prepare = nfs4_lock_prepare,
4325 .rpc_call_done = nfs4_lock_done,
4326 .rpc_release = nfs4_lock_release,
4327};
4328
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004329static const struct rpc_call_ops nfs4_recover_lock_ops = {
4330 .rpc_call_prepare = nfs4_recover_lock_prepare,
4331 .rpc_call_done = nfs4_lock_done,
4332 .rpc_release = nfs4_lock_release,
4333};
4334
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004335static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4336{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004337 switch (error) {
4338 case -NFS4ERR_ADMIN_REVOKED:
4339 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004340 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004341 if (new_lock_owner != 0 ||
4342 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004343 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004344 break;
4345 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004346 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004347 case -NFS4ERR_EXPIRED:
4348 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004349 };
4350}
4351
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004352static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004353{
4354 struct nfs4_lockdata *data;
4355 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004356 struct rpc_message msg = {
4357 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4358 .rpc_cred = state->owner->so_cred,
4359 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004360 struct rpc_task_setup task_setup_data = {
4361 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004362 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004363 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004364 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004365 .flags = RPC_TASK_ASYNC,
4366 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004367 int ret;
4368
Harvey Harrison3110ff82008-05-02 13:42:44 -07004369 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004370 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004371 fl->fl_u.nfs4_fl.owner,
4372 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004373 if (data == NULL)
4374 return -ENOMEM;
4375 if (IS_SETLKW(cmd))
4376 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004377 if (recovery_type > NFS_LOCK_NEW) {
4378 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004379 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004380 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4381 }
Trond Myklebust1174dd12010-12-21 10:52:24 -05004382 msg.rpc_argp = &data->arg;
4383 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004384 task_setup_data.callback_data = data;
4385 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004386 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004387 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004388 ret = nfs4_wait_for_completion_rpc_task(task);
4389 if (ret == 0) {
4390 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004391 if (ret)
4392 nfs4_handle_setlk_error(data->server, data->lsp,
4393 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004394 } else
4395 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004396 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004397 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004398 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399}
4400
4401static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4402{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004403 struct nfs_server *server = NFS_SERVER(state->inode);
4404 struct nfs4_exception exception = { };
4405 int err;
4406
4407 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004408 /* Cache the lock if possible... */
4409 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4410 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004411 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004412 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004413 break;
4414 nfs4_handle_exception(server, err, &exception);
4415 } while (exception.retry);
4416 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417}
4418
4419static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4420{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004421 struct nfs_server *server = NFS_SERVER(state->inode);
4422 struct nfs4_exception exception = { };
4423 int err;
4424
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004425 err = nfs4_set_lock_state(state, request);
4426 if (err != 0)
4427 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004428 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004429 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4430 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004431 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004432 switch (err) {
4433 default:
4434 goto out;
4435 case -NFS4ERR_GRACE:
4436 case -NFS4ERR_DELAY:
4437 nfs4_handle_exception(server, err, &exception);
4438 err = 0;
4439 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004440 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004441out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004442 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443}
4444
4445static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4446{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004447 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004448 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004449 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004451 if ((fl_flags & FL_POSIX) &&
4452 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4453 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004454 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004455 status = nfs4_set_lock_state(state, request);
4456 if (status != 0)
4457 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004458 request->fl_flags |= FL_ACCESS;
4459 status = do_vfs_lock(request->fl_file, request);
4460 if (status < 0)
4461 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004462 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004463 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004464 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004465 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004466 request->fl_flags = fl_flags & ~FL_SLEEP;
4467 status = do_vfs_lock(request->fl_file, request);
4468 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004469 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004470 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004471 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004472 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004473 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004474 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004475 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004476 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004477out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004478 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004479out:
4480 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 return status;
4482}
4483
4484static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4485{
4486 struct nfs4_exception exception = { };
4487 int err;
4488
4489 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004490 err = _nfs4_proc_setlk(state, cmd, request);
4491 if (err == -NFS4ERR_DENIED)
4492 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004494 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 } while (exception.retry);
4496 return err;
4497}
4498
4499static int
4500nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4501{
4502 struct nfs_open_context *ctx;
4503 struct nfs4_state *state;
4504 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4505 int status;
4506
4507 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004508 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 state = ctx->state;
4510
4511 if (request->fl_start < 0 || request->fl_end < 0)
4512 return -EINVAL;
4513
Trond Myklebustd9531262009-07-21 19:22:38 -04004514 if (IS_GETLK(cmd)) {
4515 if (state != NULL)
4516 return nfs4_proc_getlk(state, F_GETLK, request);
4517 return 0;
4518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519
4520 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4521 return -EINVAL;
4522
Trond Myklebustd9531262009-07-21 19:22:38 -04004523 if (request->fl_type == F_UNLCK) {
4524 if (state != NULL)
4525 return nfs4_proc_unlck(state, cmd, request);
4526 return 0;
4527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528
Trond Myklebustd9531262009-07-21 19:22:38 -04004529 if (state == NULL)
4530 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 do {
4532 status = nfs4_proc_setlk(state, cmd, request);
4533 if ((status != -EAGAIN) || IS_SETLK(cmd))
4534 break;
4535 timeout = nfs4_set_lock_task_retry(timeout);
4536 status = -ERESTARTSYS;
4537 if (signalled())
4538 break;
4539 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 return status;
4541}
4542
Trond Myklebust888e6942005-11-04 15:38:11 -05004543int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4544{
4545 struct nfs_server *server = NFS_SERVER(state->inode);
4546 struct nfs4_exception exception = { };
4547 int err;
4548
4549 err = nfs4_set_lock_state(state, fl);
4550 if (err != 0)
4551 goto out;
4552 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004553 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004554 switch (err) {
4555 default:
4556 printk(KERN_ERR "%s: unhandled error %d.\n",
4557 __func__, err);
4558 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004559 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004560 goto out;
4561 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004562 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004563 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004564 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004565 nfs4_schedule_lease_recovery(server->nfs_client);
4566 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004567 case -NFS4ERR_BADSESSION:
4568 case -NFS4ERR_BADSLOT:
4569 case -NFS4ERR_BAD_HIGH_SLOT:
4570 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4571 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004572 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004573 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004574 case -ERESTARTSYS:
4575 /*
4576 * The show must go on: exit, but mark the
4577 * stateid as needing recovery.
4578 */
4579 case -NFS4ERR_ADMIN_REVOKED:
4580 case -NFS4ERR_BAD_STATEID:
4581 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004582 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004583 err = 0;
4584 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004585 case -EKEYEXPIRED:
4586 /*
4587 * User RPCSEC_GSS context has expired.
4588 * We cannot recover this stateid now, so
4589 * skip it and allow recovery thread to
4590 * proceed.
4591 */
4592 err = 0;
4593 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004594 case -ENOMEM:
4595 case -NFS4ERR_DENIED:
4596 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4597 err = 0;
4598 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004599 case -NFS4ERR_DELAY:
4600 break;
4601 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004602 err = nfs4_handle_exception(server, err, &exception);
4603 } while (exception.retry);
4604out:
4605 return err;
4606}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004607
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004608static void nfs4_release_lockowner_release(void *calldata)
4609{
4610 kfree(calldata);
4611}
4612
4613const struct rpc_call_ops nfs4_release_lockowner_ops = {
4614 .rpc_release = nfs4_release_lockowner_release,
4615};
4616
4617void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4618{
4619 struct nfs_server *server = lsp->ls_state->owner->so_server;
4620 struct nfs_release_lockowner_args *args;
4621 struct rpc_message msg = {
4622 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4623 };
4624
4625 if (server->nfs_client->cl_mvops->minor_version != 0)
4626 return;
4627 args = kmalloc(sizeof(*args), GFP_NOFS);
4628 if (!args)
4629 return;
4630 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4631 args->lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004632 args->lock_owner.s_dev = server->s_dev;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004633 msg.rpc_argp = args;
4634 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4635}
4636
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004637#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4638
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004639static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4640 const void *buf, size_t buflen,
4641 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004642{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004643 if (strcmp(key, "") != 0)
4644 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004645
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004646 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004647}
4648
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004649static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4650 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004651{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004652 if (strcmp(key, "") != 0)
4653 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004654
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004655 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004656}
4657
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004658static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4659 size_t list_len, const char *name,
4660 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004661{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004662 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004663
J. Bruce Fields096455a2006-03-20 23:23:42 -05004664 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4665 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004666
4667 if (list && len <= list_len)
4668 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004669 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004670}
4671
Andy Adamson533eb462011-06-13 18:25:56 -04004672/*
4673 * nfs_fhget will use either the mounted_on_fileid or the fileid
4674 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004675static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4676{
Andy Adamson533eb462011-06-13 18:25:56 -04004677 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4678 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4679 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4680 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004681 return;
4682
4683 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4684 NFS_ATTR_FATTR_NLINK;
4685 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4686 fattr->nlink = 2;
4687}
4688
Trond Myklebust56659e92007-07-17 21:52:39 -04004689int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004690 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004691{
4692 struct nfs_server *server = NFS_SERVER(dir);
4693 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004694 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004695 };
4696 struct nfs4_fs_locations_arg args = {
4697 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004698 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004699 .page = page,
4700 .bitmask = bitmask,
4701 };
Benny Halevy22958462009-04-01 09:22:02 -04004702 struct nfs4_fs_locations_res res = {
4703 .fs_locations = fs_locations,
4704 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004705 struct rpc_message msg = {
4706 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4707 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004708 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004709 };
4710 int status;
4711
Harvey Harrison3110ff82008-05-02 13:42:44 -07004712 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04004713
4714 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
4715 * is not supported */
4716 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
4717 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
4718 else
4719 bitmask[0] |= FATTR4_WORD0_FILEID;
4720
Trond Myklebustc228fd32007-01-13 02:28:11 -05004721 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004722 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004723 fs_locations->nlocations = 0;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004724 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004725 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004726 return status;
4727}
4728
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00004729static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4730{
4731 int status;
4732 struct nfs4_secinfo_arg args = {
4733 .dir_fh = NFS_FH(dir),
4734 .name = name,
4735 };
4736 struct nfs4_secinfo_res res = {
4737 .flavors = flavors,
4738 };
4739 struct rpc_message msg = {
4740 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
4741 .rpc_argp = &args,
4742 .rpc_resp = &res,
4743 };
4744
4745 dprintk("NFS call secinfo %s\n", name->name);
4746 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4747 dprintk("NFS reply secinfo: %d\n", status);
4748 return status;
4749}
4750
4751int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4752{
4753 struct nfs4_exception exception = { };
4754 int err;
4755 do {
4756 err = nfs4_handle_exception(NFS_SERVER(dir),
4757 _nfs4_proc_secinfo(dir, name, flavors),
4758 &exception);
4759 } while (exception.retry);
4760 return err;
4761}
4762
Andy Adamson557134a2009-04-01 09:21:53 -04004763#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004764/*
Andy Adamson357f54d2010-12-14 10:11:57 -05004765 * Check the exchange flags returned by the server for invalid flags, having
4766 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4767 * DS flags set.
4768 */
4769static int nfs4_check_cl_exchange_flags(u32 flags)
4770{
4771 if (flags & ~EXCHGID4_FLAG_MASK_R)
4772 goto out_inval;
4773 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4774 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4775 goto out_inval;
4776 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4777 goto out_inval;
4778 return NFS_OK;
4779out_inval:
4780 return -NFS4ERR_INVAL;
4781}
4782
4783/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04004784 * nfs4_proc_exchange_id()
4785 *
4786 * Since the clientid has expired, all compounds using sessions
4787 * associated with the stale clientid will be returning
4788 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4789 * be in some phase of session reset.
4790 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004791int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004792{
4793 nfs4_verifier verifier;
4794 struct nfs41_exchange_id_args args = {
4795 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05004796 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04004797 };
4798 struct nfs41_exchange_id_res res = {
4799 .client = clp,
4800 };
4801 int status;
4802 struct rpc_message msg = {
4803 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4804 .rpc_argp = &args,
4805 .rpc_resp = &res,
4806 .rpc_cred = cred,
4807 };
4808 __be32 *p;
4809
4810 dprintk("--> %s\n", __func__);
4811 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004812
Benny Halevy99fe60d2009-04-01 09:22:29 -04004813 p = (u32 *)verifier.data;
4814 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4815 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4816 args.verifier = &verifier;
4817
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004818 args.id_len = scnprintf(args.id, sizeof(args.id),
4819 "%s/%s.%s/%u",
4820 clp->cl_ipaddr,
4821 init_utsname()->nodename,
4822 init_utsname()->domainname,
4823 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004824
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004825 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004826 if (!status)
4827 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004828 dprintk("<-- %s status= %d\n", __func__, status);
4829 return status;
4830}
4831
Andy Adamson2050f0c2009-04-01 09:22:30 -04004832struct nfs4_get_lease_time_data {
4833 struct nfs4_get_lease_time_args *args;
4834 struct nfs4_get_lease_time_res *res;
4835 struct nfs_client *clp;
4836};
4837
4838static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4839 void *calldata)
4840{
4841 int ret;
4842 struct nfs4_get_lease_time_data *data =
4843 (struct nfs4_get_lease_time_data *)calldata;
4844
4845 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004846 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004847 /* just setup sequence, do not trigger session recovery
4848 since we're invoked within one */
4849 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004850 &data->args->la_seq_args,
4851 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004852
4853 BUG_ON(ret == -EAGAIN);
4854 rpc_call_start(task);
4855 dprintk("<-- %s\n", __func__);
4856}
4857
4858/*
4859 * Called from nfs4_state_manager thread for session setup, so don't recover
4860 * from sequence operation or clientid errors.
4861 */
4862static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4863{
4864 struct nfs4_get_lease_time_data *data =
4865 (struct nfs4_get_lease_time_data *)calldata;
4866
4867 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04004868 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4869 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04004870 switch (task->tk_status) {
4871 case -NFS4ERR_DELAY:
4872 case -NFS4ERR_GRACE:
4873 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4874 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4875 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004876 /* fall through */
4877 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust0110ee12009-12-07 09:00:24 -05004878 nfs_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004879 return;
4880 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004881 dprintk("<-- %s\n", __func__);
4882}
4883
4884struct rpc_call_ops nfs4_get_lease_time_ops = {
4885 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4886 .rpc_call_done = nfs4_get_lease_time_done,
4887};
4888
4889int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4890{
4891 struct rpc_task *task;
4892 struct nfs4_get_lease_time_args args;
4893 struct nfs4_get_lease_time_res res = {
4894 .lr_fsinfo = fsinfo,
4895 };
4896 struct nfs4_get_lease_time_data data = {
4897 .args = &args,
4898 .res = &res,
4899 .clp = clp,
4900 };
4901 struct rpc_message msg = {
4902 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4903 .rpc_argp = &args,
4904 .rpc_resp = &res,
4905 };
4906 struct rpc_task_setup task_setup = {
4907 .rpc_client = clp->cl_rpcclient,
4908 .rpc_message = &msg,
4909 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004910 .callback_data = &data,
4911 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04004912 };
4913 int status;
4914
Andy Adamson2050f0c2009-04-01 09:22:30 -04004915 dprintk("--> %s\n", __func__);
4916 task = rpc_run_task(&task_setup);
4917
4918 if (IS_ERR(task))
4919 status = PTR_ERR(task);
4920 else {
4921 status = task->tk_status;
4922 rpc_put_task(task);
4923 }
4924 dprintk("<-- %s return %d\n", __func__, status);
4925
4926 return status;
4927}
4928
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004929/*
4930 * Reset a slot table
4931 */
Andy Adamson104aeba2010-01-14 17:45:10 -05004932static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4933 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004934{
Andy Adamson104aeba2010-01-14 17:45:10 -05004935 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004936 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004937 int ret = 0;
4938
Andy Adamson104aeba2010-01-14 17:45:10 -05004939 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4940 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004941
Andy Adamson104aeba2010-01-14 17:45:10 -05004942 /* Does the newly negotiated max_reqs match the existing slot table? */
4943 if (max_reqs != tbl->max_slots) {
4944 ret = -ENOMEM;
4945 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004946 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05004947 if (!new)
4948 goto out;
4949 ret = 0;
4950 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004951 }
4952 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05004953 if (new) {
4954 tbl->slots = new;
4955 tbl->max_slots = max_reqs;
4956 }
4957 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004958 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004959 spin_unlock(&tbl->slot_tbl_lock);
4960 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4961 tbl, tbl->slots, tbl->max_slots);
4962out:
4963 dprintk("<-- %s: return %d\n", __func__, ret);
4964 return ret;
4965}
4966
Andy Adamsonfc931582009-04-01 09:22:31 -04004967/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004968 * Reset the forechannel and backchannel slot tables
4969 */
4970static int nfs4_reset_slot_tables(struct nfs4_session *session)
4971{
4972 int status;
4973
4974 status = nfs4_reset_slot_table(&session->fc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004975 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004976 if (status)
4977 return status;
4978
4979 status = nfs4_reset_slot_table(&session->bc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004980 session->bc_attrs.max_reqs, 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004981 return status;
4982}
4983
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004984/* Destroy the slot table */
4985static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4986{
4987 if (session->fc_slot_table.slots != NULL) {
4988 kfree(session->fc_slot_table.slots);
4989 session->fc_slot_table.slots = NULL;
4990 }
4991 if (session->bc_slot_table.slots != NULL) {
4992 kfree(session->bc_slot_table.slots);
4993 session->bc_slot_table.slots = NULL;
4994 }
4995 return;
4996}
4997
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004998/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004999 * Initialize slot table
5000 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005001static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
5002 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04005003{
Andy Adamsonfc931582009-04-01 09:22:31 -04005004 struct nfs4_slot *slot;
5005 int ret = -ENOMEM;
5006
5007 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
5008
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005009 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04005010
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005011 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04005012 if (!slot)
5013 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04005014 ret = 0;
5015
5016 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04005017 tbl->max_slots = max_slots;
5018 tbl->slots = slot;
5019 tbl->highest_used_slotid = -1; /* no slot is currently used */
5020 spin_unlock(&tbl->slot_tbl_lock);
5021 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5022 tbl, tbl->slots, tbl->max_slots);
5023out:
5024 dprintk("<-- %s: return %d\n", __func__, ret);
5025 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04005026}
5027
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005028/*
5029 * Initialize the forechannel and backchannel tables
5030 */
5031static int nfs4_init_slot_tables(struct nfs4_session *session)
5032{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005033 struct nfs4_slot_table *tbl;
5034 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005035
Trond Myklebustf26468f2009-12-05 19:32:11 -05005036 tbl = &session->fc_slot_table;
5037 if (tbl->slots == NULL) {
5038 status = nfs4_init_slot_table(tbl,
5039 session->fc_attrs.max_reqs, 1);
5040 if (status)
5041 return status;
5042 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005043
Trond Myklebustf26468f2009-12-05 19:32:11 -05005044 tbl = &session->bc_slot_table;
5045 if (tbl->slots == NULL) {
5046 status = nfs4_init_slot_table(tbl,
5047 session->bc_attrs.max_reqs, 0);
5048 if (status)
5049 nfs4_destroy_slot_tables(session);
5050 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005051
5052 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005053}
5054
5055struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5056{
5057 struct nfs4_session *session;
5058 struct nfs4_slot_table *tbl;
5059
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005060 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005061 if (!session)
5062 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005063
Andy Adamson557134a2009-04-01 09:21:53 -04005064 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05005065 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04005066 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005067 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005068 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005069
5070 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05005071 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005072 spin_lock_init(&tbl->slot_tbl_lock);
5073 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005074 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005075
Trond Myklebust1055d762010-06-16 09:52:27 -04005076 session->session_state = 1<<NFS4_SESSION_INITING;
5077
Andy Adamson557134a2009-04-01 09:21:53 -04005078 session->clp = clp;
5079 return session;
5080}
5081
5082void nfs4_destroy_session(struct nfs4_session *session)
5083{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005084 nfs4_proc_destroy_session(session);
5085 dprintk("%s Destroy backchannel for xprt %p\n",
5086 __func__, session->clp->cl_rpcclient->cl_xprt);
5087 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
5088 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005089 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005090 kfree(session);
5091}
5092
Andy Adamsonfc931582009-04-01 09:22:31 -04005093/*
5094 * Initialize the values to be used by the client in CREATE_SESSION
5095 * If nfs4_init_session set the fore channel request and response sizes,
5096 * use them.
5097 *
5098 * Set the back channel max_resp_sz_cached to zero to force the client to
5099 * always set csa_cachethis to FALSE because the current implementation
5100 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5101 */
5102static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5103{
5104 struct nfs4_session *session = args->client->cl_session;
5105 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5106 mxresp_sz = session->fc_attrs.max_resp_sz;
5107
5108 if (mxrqst_sz == 0)
5109 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5110 if (mxresp_sz == 0)
5111 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5112 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005113 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5114 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005115 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5116 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
5117
5118 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005119 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005120 __func__,
5121 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005122 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005123
5124 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005125 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5126 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5127 args->bc_attrs.max_resp_sz_cached = 0;
5128 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5129 args->bc_attrs.max_reqs = 1;
5130
5131 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5132 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5133 __func__,
5134 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5135 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5136 args->bc_attrs.max_reqs);
5137}
5138
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005139static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005140{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005141 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5142 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5143
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005144 if (rcvd->max_resp_sz > sent->max_resp_sz)
5145 return -EINVAL;
5146 /*
5147 * Our requested max_ops is the minimum we need; we're not
5148 * prepared to break up compounds into smaller pieces than that.
5149 * So, no point even trying to continue if the server won't
5150 * cooperate:
5151 */
5152 if (rcvd->max_ops < sent->max_ops)
5153 return -EINVAL;
5154 if (rcvd->max_reqs == 0)
5155 return -EINVAL;
5156 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005157}
5158
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005159static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5160{
5161 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5162 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005163
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005164 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5165 return -EINVAL;
5166 if (rcvd->max_resp_sz < sent->max_resp_sz)
5167 return -EINVAL;
5168 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5169 return -EINVAL;
5170 /* These would render the backchannel useless: */
5171 if (rcvd->max_ops == 0)
5172 return -EINVAL;
5173 if (rcvd->max_reqs == 0)
5174 return -EINVAL;
5175 return 0;
5176}
Andy Adamson8d353012009-04-01 09:22:32 -04005177
Andy Adamson8d353012009-04-01 09:22:32 -04005178static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5179 struct nfs4_session *session)
5180{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005181 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005182
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005183 ret = nfs4_verify_fore_channel_attrs(args, session);
5184 if (ret)
5185 return ret;
5186 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005187}
5188
Andy Adamsonfc931582009-04-01 09:22:31 -04005189static int _nfs4_proc_create_session(struct nfs_client *clp)
5190{
5191 struct nfs4_session *session = clp->cl_session;
5192 struct nfs41_create_session_args args = {
5193 .client = clp,
5194 .cb_program = NFS4_CALLBACK,
5195 };
5196 struct nfs41_create_session_res res = {
5197 .client = clp,
5198 };
5199 struct rpc_message msg = {
5200 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5201 .rpc_argp = &args,
5202 .rpc_resp = &res,
5203 };
5204 int status;
5205
5206 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005207 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005208
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005209 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005210
Andy Adamson8d353012009-04-01 09:22:32 -04005211 if (!status)
5212 /* Verify the session's negotiated channel_attrs values */
5213 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005214 if (!status) {
5215 /* Increment the clientid slot sequence id */
5216 clp->cl_seqid++;
5217 }
5218
5219 return status;
5220}
5221
5222/*
5223 * Issues a CREATE_SESSION operation to the server.
5224 * It is the responsibility of the caller to verify the session is
5225 * expired before calling this routine.
5226 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005227int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005228{
5229 int status;
5230 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005231 struct nfs4_session *session = clp->cl_session;
5232
5233 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5234
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005235 status = _nfs4_proc_create_session(clp);
Andy Adamsonfc931582009-04-01 09:22:31 -04005236 if (status)
5237 goto out;
5238
Trond Myklebustf26468f2009-12-05 19:32:11 -05005239 /* Init and reset the fore channel */
5240 status = nfs4_init_slot_tables(session);
5241 dprintk("slot table initialization returned %d\n", status);
5242 if (status)
5243 goto out;
5244 status = nfs4_reset_slot_tables(session);
5245 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005246 if (status)
5247 goto out;
5248
5249 ptr = (unsigned *)&session->sess_id.data[0];
5250 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5251 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005252out:
5253 dprintk("<-- %s\n", __func__);
5254 return status;
5255}
5256
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005257/*
5258 * Issue the over-the-wire RPC DESTROY_SESSION.
5259 * The caller must serialize access to this routine.
5260 */
5261int nfs4_proc_destroy_session(struct nfs4_session *session)
5262{
5263 int status = 0;
5264 struct rpc_message msg;
5265
5266 dprintk("--> nfs4_proc_destroy_session\n");
5267
5268 /* session is still being setup */
5269 if (session->clp->cl_cons_state != NFS_CS_READY)
5270 return status;
5271
5272 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5273 msg.rpc_argp = session;
5274 msg.rpc_resp = NULL;
5275 msg.rpc_cred = NULL;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005276 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005277
5278 if (status)
5279 printk(KERN_WARNING
5280 "Got error %d from the server on DESTROY_SESSION. "
5281 "Session has been destroyed regardless...\n", status);
5282
5283 dprintk("<-- nfs4_proc_destroy_session\n");
5284 return status;
5285}
5286
Trond Myklebustfccba802009-07-21 16:48:07 -04005287int nfs4_init_session(struct nfs_server *server)
5288{
5289 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005290 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005291 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005292 int ret;
5293
5294 if (!nfs4_has_session(clp))
5295 return 0;
5296
Trond Myklebust1055d762010-06-16 09:52:27 -04005297 session = clp->cl_session;
5298 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5299 return 0;
5300
Andy Adamson68bf05e2009-12-15 12:55:02 -05005301 rsize = server->rsize;
5302 if (rsize == 0)
5303 rsize = NFS_MAX_FILE_IO_SIZE;
5304 wsize = server->wsize;
5305 if (wsize == 0)
5306 wsize = NFS_MAX_FILE_IO_SIZE;
5307
Andy Adamson68bf05e2009-12-15 12:55:02 -05005308 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5309 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005310
Trond Myklebustfccba802009-07-21 16:48:07 -04005311 ret = nfs4_recover_expired_lease(server);
5312 if (!ret)
5313 ret = nfs4_check_client_ready(clp);
5314 return ret;
5315}
5316
Andy Adamsond83217c2011-03-01 01:34:17 +00005317int nfs4_init_ds_session(struct nfs_client *clp)
5318{
5319 struct nfs4_session *session = clp->cl_session;
5320 int ret;
5321
5322 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5323 return 0;
5324
5325 ret = nfs4_client_recover_expired_lease(clp);
5326 if (!ret)
5327 /* Test for the DS role */
5328 if (!is_ds_client(clp))
5329 ret = -ENODEV;
5330 if (!ret)
5331 ret = nfs4_check_client_ready(clp);
5332 return ret;
5333
5334}
5335EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5336
5337
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005338/*
5339 * Renew the cl_session lease.
5340 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005341struct nfs4_sequence_data {
5342 struct nfs_client *clp;
5343 struct nfs4_sequence_args args;
5344 struct nfs4_sequence_res res;
5345};
5346
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005347static void nfs41_sequence_release(void *data)
5348{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005349 struct nfs4_sequence_data *calldata = data;
5350 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005351
Alexandros Batsakis71358402010-02-05 03:45:05 -08005352 if (atomic_read(&clp->cl_count) > 1)
5353 nfs4_schedule_state_renewal(clp);
5354 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005355 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005356}
5357
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005358static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5359{
5360 switch(task->tk_status) {
5361 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005362 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5363 return -EAGAIN;
5364 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005365 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005366 }
5367 return 0;
5368}
5369
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005370static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005371{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005372 struct nfs4_sequence_data *calldata = data;
5373 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005374
Trond Myklebust14516c32010-07-31 14:29:06 -04005375 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5376 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005377
5378 if (task->tk_status < 0) {
5379 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005380 if (atomic_read(&clp->cl_count) == 1)
5381 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005382
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005383 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5384 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005385 return;
5386 }
5387 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005388 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005389out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005390 dprintk("<-- %s\n", __func__);
5391}
5392
5393static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5394{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005395 struct nfs4_sequence_data *calldata = data;
5396 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005397 struct nfs4_sequence_args *args;
5398 struct nfs4_sequence_res *res;
5399
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005400 args = task->tk_msg.rpc_argp;
5401 res = task->tk_msg.rpc_resp;
5402
Trond Myklebust035168a2010-06-16 09:52:26 -04005403 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005404 return;
5405 rpc_call_start(task);
5406}
5407
5408static const struct rpc_call_ops nfs41_sequence_ops = {
5409 .rpc_call_done = nfs41_sequence_call_done,
5410 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005411 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005412};
5413
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005414static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005415{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005416 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005417 struct rpc_message msg = {
5418 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5419 .rpc_cred = cred,
5420 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005421 struct rpc_task_setup task_setup_data = {
5422 .rpc_client = clp->cl_rpcclient,
5423 .rpc_message = &msg,
5424 .callback_ops = &nfs41_sequence_ops,
5425 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5426 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005427
Alexandros Batsakis71358402010-02-05 03:45:05 -08005428 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005429 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005430 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005431 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005432 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005433 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005434 }
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005435 msg.rpc_argp = &calldata->args;
5436 msg.rpc_resp = &calldata->res;
5437 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005438 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005439
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005440 return rpc_run_task(&task_setup_data);
5441}
5442
5443static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5444{
5445 struct rpc_task *task;
5446 int ret = 0;
5447
5448 task = _nfs41_proc_sequence(clp, cred);
5449 if (IS_ERR(task))
5450 ret = PTR_ERR(task);
5451 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005452 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005453 dprintk("<-- %s status=%d\n", __func__, ret);
5454 return ret;
5455}
5456
5457static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5458{
5459 struct rpc_task *task;
5460 int ret;
5461
5462 task = _nfs41_proc_sequence(clp, cred);
5463 if (IS_ERR(task)) {
5464 ret = PTR_ERR(task);
5465 goto out;
5466 }
5467 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005468 if (!ret) {
5469 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5470
5471 if (task->tk_status == 0)
5472 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005473 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005474 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005475 rpc_put_task(task);
5476out:
5477 dprintk("<-- %s status=%d\n", __func__, ret);
5478 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005479}
5480
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005481struct nfs4_reclaim_complete_data {
5482 struct nfs_client *clp;
5483 struct nfs41_reclaim_complete_args arg;
5484 struct nfs41_reclaim_complete_res res;
5485};
5486
5487static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5488{
5489 struct nfs4_reclaim_complete_data *calldata = data;
5490
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005491 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005492 if (nfs41_setup_sequence(calldata->clp->cl_session,
5493 &calldata->arg.seq_args,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005494 &calldata->res.seq_res, 0, task))
5495 return;
5496
5497 rpc_call_start(task);
5498}
5499
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005500static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5501{
5502 switch(task->tk_status) {
5503 case 0:
5504 case -NFS4ERR_COMPLETE_ALREADY:
5505 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5506 break;
5507 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005508 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005509 /* fall through */
5510 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005511 return -EAGAIN;
5512 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005513 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005514 }
5515 return 0;
5516}
5517
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005518static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5519{
5520 struct nfs4_reclaim_complete_data *calldata = data;
5521 struct nfs_client *clp = calldata->clp;
5522 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5523
5524 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005525 if (!nfs41_sequence_done(task, res))
5526 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005527
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005528 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5529 rpc_restart_call_prepare(task);
5530 return;
5531 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005532 dprintk("<-- %s\n", __func__);
5533}
5534
5535static void nfs4_free_reclaim_complete_data(void *data)
5536{
5537 struct nfs4_reclaim_complete_data *calldata = data;
5538
5539 kfree(calldata);
5540}
5541
5542static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5543 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5544 .rpc_call_done = nfs4_reclaim_complete_done,
5545 .rpc_release = nfs4_free_reclaim_complete_data,
5546};
5547
5548/*
5549 * Issue a global reclaim complete.
5550 */
5551static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5552{
5553 struct nfs4_reclaim_complete_data *calldata;
5554 struct rpc_task *task;
5555 struct rpc_message msg = {
5556 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5557 };
5558 struct rpc_task_setup task_setup_data = {
5559 .rpc_client = clp->cl_rpcclient,
5560 .rpc_message = &msg,
5561 .callback_ops = &nfs4_reclaim_complete_call_ops,
5562 .flags = RPC_TASK_ASYNC,
5563 };
5564 int status = -ENOMEM;
5565
5566 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005567 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005568 if (calldata == NULL)
5569 goto out;
5570 calldata->clp = clp;
5571 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005572
5573 msg.rpc_argp = &calldata->arg;
5574 msg.rpc_resp = &calldata->res;
5575 task_setup_data.callback_data = calldata;
5576 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005577 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005578 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005579 goto out;
5580 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005581 status = nfs4_wait_for_completion_rpc_task(task);
5582 if (status == 0)
5583 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005584 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005585 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005586out:
5587 dprintk("<-- %s status=%d\n", __func__, status);
5588 return status;
5589}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005590
5591static void
5592nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5593{
5594 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005595 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005596
5597 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005598 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5599 * right now covering the LAYOUTGET we are about to send.
5600 * However, that is not so catastrophic, and there seems
5601 * to be no way to prevent it completely.
5602 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005603 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5604 &lgp->res.seq_res, 0, task))
5605 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005606 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5607 NFS_I(lgp->args.inode)->layout,
5608 lgp->args.ctx->state)) {
5609 rpc_exit(task, NFS4_OK);
5610 return;
5611 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005612 rpc_call_start(task);
5613}
5614
5615static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5616{
5617 struct nfs4_layoutget *lgp = calldata;
5618 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5619
5620 dprintk("--> %s\n", __func__);
5621
5622 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5623 return;
5624
5625 switch (task->tk_status) {
5626 case 0:
5627 break;
5628 case -NFS4ERR_LAYOUTTRYLATER:
5629 case -NFS4ERR_RECALLCONFLICT:
5630 task->tk_status = -NFS4ERR_DELAY;
5631 /* Fall through */
5632 default:
5633 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5634 rpc_restart_call_prepare(task);
5635 return;
5636 }
5637 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005638 dprintk("<-- %s\n", __func__);
5639}
5640
5641static void nfs4_layoutget_release(void *calldata)
5642{
5643 struct nfs4_layoutget *lgp = calldata;
5644
5645 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005646 put_nfs_open_context(lgp->args.ctx);
5647 kfree(calldata);
5648 dprintk("<-- %s\n", __func__);
5649}
5650
5651static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5652 .rpc_call_prepare = nfs4_layoutget_prepare,
5653 .rpc_call_done = nfs4_layoutget_done,
5654 .rpc_release = nfs4_layoutget_release,
5655};
5656
5657int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5658{
5659 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5660 struct rpc_task *task;
5661 struct rpc_message msg = {
5662 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5663 .rpc_argp = &lgp->args,
5664 .rpc_resp = &lgp->res,
5665 };
5666 struct rpc_task_setup task_setup_data = {
5667 .rpc_client = server->client,
5668 .rpc_message = &msg,
5669 .callback_ops = &nfs4_layoutget_call_ops,
5670 .callback_data = lgp,
5671 .flags = RPC_TASK_ASYNC,
5672 };
5673 int status = 0;
5674
5675 dprintk("--> %s\n", __func__);
5676
Weston Andros Adamson35124a02011-03-24 16:48:21 -04005677 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005678 lgp->res.seq_res.sr_slot = NULL;
5679 task = rpc_run_task(&task_setup_data);
5680 if (IS_ERR(task))
5681 return PTR_ERR(task);
5682 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00005683 if (status == 0)
5684 status = task->tk_status;
5685 if (status == 0)
5686 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005687 rpc_put_task(task);
5688 dprintk("<-- %s status=%d\n", __func__, status);
5689 return status;
5690}
5691
Benny Halevycbe82602011-05-22 19:52:37 +03005692static void
5693nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
5694{
5695 struct nfs4_layoutreturn *lrp = calldata;
5696
5697 dprintk("--> %s\n", __func__);
5698 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
5699 &lrp->res.seq_res, 0, task))
5700 return;
5701 rpc_call_start(task);
5702}
5703
5704static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
5705{
5706 struct nfs4_layoutreturn *lrp = calldata;
5707 struct nfs_server *server;
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005708 struct pnfs_layout_hdr *lo = NFS_I(lrp->args.inode)->layout;
Benny Halevycbe82602011-05-22 19:52:37 +03005709
5710 dprintk("--> %s\n", __func__);
5711
5712 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
5713 return;
5714
5715 server = NFS_SERVER(lrp->args.inode);
5716 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5717 nfs_restart_rpc(task, lrp->clp);
5718 return;
5719 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005720 spin_lock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005721 if (task->tk_status == 0) {
Benny Halevycbe82602011-05-22 19:52:37 +03005722 if (lrp->res.lrs_present) {
Benny Halevycbe82602011-05-22 19:52:37 +03005723 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Benny Halevycbe82602011-05-22 19:52:37 +03005724 } else
5725 BUG_ON(!list_empty(&lo->plh_segs));
5726 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005727 lo->plh_block_lgets--;
5728 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005729 dprintk("<-- %s\n", __func__);
5730}
5731
5732static void nfs4_layoutreturn_release(void *calldata)
5733{
5734 struct nfs4_layoutreturn *lrp = calldata;
5735
5736 dprintk("--> %s\n", __func__);
5737 put_layout_hdr(NFS_I(lrp->args.inode)->layout);
5738 kfree(calldata);
5739 dprintk("<-- %s\n", __func__);
5740}
5741
5742static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
5743 .rpc_call_prepare = nfs4_layoutreturn_prepare,
5744 .rpc_call_done = nfs4_layoutreturn_done,
5745 .rpc_release = nfs4_layoutreturn_release,
5746};
5747
5748int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
5749{
5750 struct rpc_task *task;
5751 struct rpc_message msg = {
5752 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
5753 .rpc_argp = &lrp->args,
5754 .rpc_resp = &lrp->res,
5755 };
5756 struct rpc_task_setup task_setup_data = {
5757 .rpc_client = lrp->clp->cl_rpcclient,
5758 .rpc_message = &msg,
5759 .callback_ops = &nfs4_layoutreturn_call_ops,
5760 .callback_data = lrp,
5761 };
5762 int status;
5763
5764 dprintk("--> %s\n", __func__);
5765 task = rpc_run_task(&task_setup_data);
5766 if (IS_ERR(task))
5767 return PTR_ERR(task);
5768 status = task->tk_status;
5769 dprintk("<-- %s status=%d\n", __func__, status);
5770 rpc_put_task(task);
5771 return status;
5772}
5773
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005774static int
5775_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5776{
5777 struct nfs4_getdeviceinfo_args args = {
5778 .pdev = pdev,
5779 };
5780 struct nfs4_getdeviceinfo_res res = {
5781 .pdev = pdev,
5782 };
5783 struct rpc_message msg = {
5784 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5785 .rpc_argp = &args,
5786 .rpc_resp = &res,
5787 };
5788 int status;
5789
5790 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005791 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005792 dprintk("<-- %s status=%d\n", __func__, status);
5793
5794 return status;
5795}
5796
5797int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5798{
5799 struct nfs4_exception exception = { };
5800 int err;
5801
5802 do {
5803 err = nfs4_handle_exception(server,
5804 _nfs4_proc_getdeviceinfo(server, pdev),
5805 &exception);
5806 } while (exception.retry);
5807 return err;
5808}
5809EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5810
Andy Adamson863a3c62011-03-23 13:27:54 +00005811static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
5812{
5813 struct nfs4_layoutcommit_data *data = calldata;
5814 struct nfs_server *server = NFS_SERVER(data->args.inode);
5815
5816 if (nfs4_setup_sequence(server, &data->args.seq_args,
5817 &data->res.seq_res, 1, task))
5818 return;
5819 rpc_call_start(task);
5820}
5821
5822static void
5823nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
5824{
5825 struct nfs4_layoutcommit_data *data = calldata;
5826 struct nfs_server *server = NFS_SERVER(data->args.inode);
5827
5828 if (!nfs4_sequence_done(task, &data->res.seq_res))
5829 return;
5830
5831 switch (task->tk_status) { /* Just ignore these failures */
5832 case NFS4ERR_DELEG_REVOKED: /* layout was recalled */
5833 case NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
5834 case NFS4ERR_BADLAYOUT: /* no layout */
5835 case NFS4ERR_GRACE: /* loca_recalim always false */
5836 task->tk_status = 0;
5837 }
5838
5839 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5840 nfs_restart_rpc(task, server->nfs_client);
5841 return;
5842 }
5843
5844 if (task->tk_status == 0)
5845 nfs_post_op_update_inode_force_wcc(data->args.inode,
5846 data->res.fattr);
5847}
5848
5849static void nfs4_layoutcommit_release(void *calldata)
5850{
5851 struct nfs4_layoutcommit_data *data = calldata;
5852
5853 /* Matched by references in pnfs_set_layoutcommit */
5854 put_lseg(data->lseg);
5855 put_rpccred(data->cred);
5856 kfree(data);
5857}
5858
5859static const struct rpc_call_ops nfs4_layoutcommit_ops = {
5860 .rpc_call_prepare = nfs4_layoutcommit_prepare,
5861 .rpc_call_done = nfs4_layoutcommit_done,
5862 .rpc_release = nfs4_layoutcommit_release,
5863};
5864
5865int
Andy Adamsonef311532011-03-12 02:58:10 -05005866nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00005867{
5868 struct rpc_message msg = {
5869 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
5870 .rpc_argp = &data->args,
5871 .rpc_resp = &data->res,
5872 .rpc_cred = data->cred,
5873 };
5874 struct rpc_task_setup task_setup_data = {
5875 .task = &data->task,
5876 .rpc_client = NFS_CLIENT(data->args.inode),
5877 .rpc_message = &msg,
5878 .callback_ops = &nfs4_layoutcommit_ops,
5879 .callback_data = data,
5880 .flags = RPC_TASK_ASYNC,
5881 };
5882 struct rpc_task *task;
5883 int status = 0;
5884
5885 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
5886 "lbw: %llu inode %lu\n",
5887 data->task.tk_pid, sync,
5888 data->args.lastbytewritten,
5889 data->args.inode->i_ino);
5890
5891 task = rpc_run_task(&task_setup_data);
5892 if (IS_ERR(task))
5893 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05005894 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00005895 goto out;
5896 status = nfs4_wait_for_completion_rpc_task(task);
5897 if (status != 0)
5898 goto out;
5899 status = task->tk_status;
5900out:
5901 dprintk("%s: status %d\n", __func__, status);
5902 rpc_put_task(task);
5903 return status;
5904}
Andy Adamson557134a2009-04-01 09:21:53 -04005905#endif /* CONFIG_NFS_V4_1 */
5906
Andy Adamson591d71c2009-04-01 09:22:47 -04005907struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005908 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005909 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910 .recover_open = nfs4_open_reclaim,
5911 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04005912 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005913 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914};
5915
Andy Adamson591d71c2009-04-01 09:22:47 -04005916#if defined(CONFIG_NFS_V4_1)
5917struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5918 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5919 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5920 .recover_open = nfs4_open_reclaim,
5921 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05005922 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005923 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005924 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04005925};
5926#endif /* CONFIG_NFS_V4_1 */
5927
5928struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005929 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005930 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931 .recover_open = nfs4_open_expired,
5932 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04005933 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005934 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935};
5936
Andy Adamson591d71c2009-04-01 09:22:47 -04005937#if defined(CONFIG_NFS_V4_1)
5938struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5939 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5940 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5941 .recover_open = nfs4_open_expired,
5942 .recover_lock = nfs4_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05005943 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005944 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04005945};
5946#endif /* CONFIG_NFS_V4_1 */
5947
Benny Halevy29fba382009-04-01 09:22:44 -04005948struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5949 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04005950 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005951 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04005952};
5953
5954#if defined(CONFIG_NFS_V4_1)
5955struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5956 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04005957 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005958 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04005959};
5960#endif
5961
Trond Myklebust97dc1352010-06-16 09:52:26 -04005962static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
5963 .minor_version = 0,
5964 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04005965 .validate_stateid = nfs4_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005966 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
5967 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
5968 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005969};
5970
5971#if defined(CONFIG_NFS_V4_1)
5972static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
5973 .minor_version = 1,
5974 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04005975 .validate_stateid = nfs41_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005976 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
5977 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
5978 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005979};
5980#endif
5981
Trond Myklebust97dc1352010-06-16 09:52:26 -04005982const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
5983 [0] = &nfs_v4_0_minor_ops,
5984#if defined(CONFIG_NFS_V4_1)
5985 [1] = &nfs_v4_1_minor_ops,
5986#endif
5987};
5988
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005989static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005990 .permission = nfs_permission,
5991 .getattr = nfs_getattr,
5992 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005993 .getxattr = generic_getxattr,
5994 .setxattr = generic_setxattr,
5995 .listxattr = generic_listxattr,
5996 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005997};
5998
David Howells509de812006-08-22 20:06:11 -04005999const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006000 .version = 4, /* protocol version */
6001 .dentry_ops = &nfs4_dentry_operations,
6002 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006003 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006004 .getroot = nfs4_proc_get_root,
6005 .getattr = nfs4_proc_getattr,
6006 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04006007 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006008 .lookup = nfs4_proc_lookup,
6009 .access = nfs4_proc_access,
6010 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011 .create = nfs4_proc_create,
6012 .remove = nfs4_proc_remove,
6013 .unlink_setup = nfs4_proc_unlink_setup,
6014 .unlink_done = nfs4_proc_unlink_done,
6015 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006016 .rename_setup = nfs4_proc_rename_setup,
6017 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018 .link = nfs4_proc_link,
6019 .symlink = nfs4_proc_symlink,
6020 .mkdir = nfs4_proc_mkdir,
6021 .rmdir = nfs4_proc_remove,
6022 .readdir = nfs4_proc_readdir,
6023 .mknod = nfs4_proc_mknod,
6024 .statfs = nfs4_proc_statfs,
6025 .fsinfo = nfs4_proc_fsinfo,
6026 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006027 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028 .decode_dirent = nfs4_decode_dirent,
6029 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05006030 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006032 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006034 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006036 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006037 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006038 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00006039 .init_client = nfs4_init_client,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006040 .secinfo = nfs4_proc_secinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006041};
6042
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006043static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6044 .prefix = XATTR_NAME_NFSV4_ACL,
6045 .list = nfs4_xattr_list_nfs4_acl,
6046 .get = nfs4_xattr_get_nfs4_acl,
6047 .set = nfs4_xattr_set_nfs4_acl,
6048};
6049
6050const struct xattr_handler *nfs4_xattr_handlers[] = {
6051 &nfs4_xattr_nfs4_acl_handler,
6052 NULL
6053};
6054
Linus Torvalds1da177e2005-04-16 15:20:36 -07006055/*
6056 * Local variables:
6057 * c-basic-offset: 8
6058 * End:
6059 */