blob: e7e2077eebd9f623ce28a9714cb3f31988b2a941 [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 Myklebust65de8722008-12-23 15:21:44 -0500270 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500271 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust65de8722008-12-23 15:21:44 -0500272 case -NFS4ERR_EXPIRED:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500273 nfs4_schedule_lease_recovery(clp);
274 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500275#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400276 case -NFS4ERR_BADSESSION:
277 case -NFS4ERR_BADSLOT:
278 case -NFS4ERR_BAD_HIGH_SLOT:
279 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
280 case -NFS4ERR_DEADSESSION:
281 case -NFS4ERR_SEQ_FALSE_RETRY:
282 case -NFS4ERR_SEQ_MISORDERED:
283 dprintk("%s ERROR: %d Reset session\n", __func__,
284 errorcode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500285 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -0400286 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500287 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500288#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500289 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500290 if (exception->timeout > HZ) {
291 /* We have retried a decent amount, time to
292 * fail
293 */
294 ret = -EBUSY;
295 break;
296 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500297 case -NFS4ERR_GRACE:
298 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500299 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500300 ret = nfs4_delay(server->client, &exception->timeout);
301 if (ret != 0)
302 break;
303 case -NFS4ERR_OLD_STATEID:
304 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800305 break;
306 case -NFS4ERR_BADOWNER:
307 /* The following works around a Linux server bug! */
308 case -NFS4ERR_BADNAME:
309 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
310 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
311 exception->retry = 1;
312 printk(KERN_WARNING "NFS: v4 server %s "
313 "does not accept raw "
314 "uid/gids. "
315 "Reenabling the idmapper.\n",
316 server->nfs_client->cl_hostname);
317 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500318 }
319 /* We failed to handle the error */
320 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500321wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500322 ret = nfs4_wait_clnt_recover(clp);
323 if (ret == 0)
324 exception->retry = 1;
325 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500326}
327
328
Trond Myklebust452e9352010-07-31 14:29:06 -0400329static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 spin_lock(&clp->cl_lock);
332 if (time_before(clp->cl_last_renewal,timestamp))
333 clp->cl_last_renewal = timestamp;
334 spin_unlock(&clp->cl_lock);
335}
336
Trond Myklebust452e9352010-07-31 14:29:06 -0400337static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
338{
339 do_renew_lease(server->nfs_client, timestamp);
340}
341
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400342#if defined(CONFIG_NFS_V4_1)
343
Benny Halevy510b8172009-04-01 09:22:14 -0400344/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400345 * nfs4_free_slot - free a slot and efficiently update slot table.
346 *
347 * freeing a slot is trivially done by clearing its respective bit
348 * in the bitmap.
349 * If the freed slotid equals highest_used_slotid we want to update it
350 * so that the server would be able to size down the slot table if needed,
351 * otherwise we know that the highest_used_slotid is still in use.
352 * When updating highest_used_slotid there may be "holes" in the bitmap
353 * so we need to scan down from highest_used_slotid to 0 looking for the now
354 * highest slotid in use.
355 * If none found, highest_used_slotid is set to -1.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500356 *
357 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400358 */
359static void
Benny Halevydfb4f3092010-09-24 09:17:01 -0400360nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *free_slot)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400361{
Benny Halevydfb4f3092010-09-24 09:17:01 -0400362 int free_slotid = free_slot - tbl->slots;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400363 int slotid = free_slotid;
364
Benny Halevydfb4f3092010-09-24 09:17:01 -0400365 BUG_ON(slotid < 0 || slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400366 /* clear used bit in bitmap */
367 __clear_bit(slotid, tbl->used_slots);
368
369 /* update highest_used_slotid when it is freed */
370 if (slotid == tbl->highest_used_slotid) {
371 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500372 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400373 tbl->highest_used_slotid = slotid;
374 else
375 tbl->highest_used_slotid = -1;
376 }
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400377 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
378 free_slotid, tbl->highest_used_slotid);
379}
380
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800381/*
Andy Adamson42acd022011-01-06 02:04:34 +0000382 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800383 */
Andy Adamson42acd022011-01-06 02:04:34 +0000384static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800385{
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800386 struct rpc_task *task;
387
Trond Myklebusta2118c32010-06-16 09:52:26 -0400388 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800389 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
390 if (task)
391 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800392 return;
393 }
394
395 if (ses->fc_slot_table.highest_used_slotid != -1)
396 return;
397
Andy Adamson42acd022011-01-06 02:04:34 +0000398 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
399 complete(&ses->fc_slot_table.complete);
400}
401
402/*
403 * Signal state manager thread if session back channel is drained
404 */
405void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
406{
407 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
408 ses->bc_slot_table.highest_used_slotid != -1)
409 return;
410 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
411 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800412}
413
Trond Myklebustd185a332010-06-16 09:52:25 -0400414static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400415{
416 struct nfs4_slot_table *tbl;
417
Trond Myklebustd185a332010-06-16 09:52:25 -0400418 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400419 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400420 /* just wake up the next guy waiting since
421 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500422 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500423 return;
Andy Adamson13615872009-04-01 09:22:17 -0400424 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500425
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500426 spin_lock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400427 nfs4_free_slot(tbl, res->sr_slot);
Andy Adamson42acd022011-01-06 02:04:34 +0000428 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500429 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400430 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400431}
432
Trond Myklebust14516c32010-07-31 14:29:06 -0400433static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400434{
435 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400436 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400437
438 /*
439 * sr_status remains 1 if an RPC level error occurred. The server
440 * may or may not have processed the sequence operation..
441 * Proceed as if the server received and processed the sequence
442 * operation.
443 */
444 if (res->sr_status == 1)
445 res->sr_status = NFS_OK;
446
Bryan Schumaker468f8612011-04-18 15:57:32 -0400447 /* don't increment the sequence number if the task wasn't sent */
448 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400449 goto out;
450
Andy Adamson691daf32009-12-04 15:55:39 -0500451 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400452 switch (res->sr_status) {
453 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400454 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400455 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400456 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400457 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400458 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500459 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500460 if (res->sr_status_flags != 0)
461 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400462 break;
463 case -NFS4ERR_DELAY:
464 /* The server detected a resend of the RPC call and
465 * returned NFS4ERR_DELAY as per Section 2.10.6.2
466 * of RFC5661.
467 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200468 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400469 __func__,
470 res->sr_slot - res->sr_session->fc_slot_table.slots,
471 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400472 goto out_retry;
473 default:
474 /* Just update the slot sequence no. */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400475 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400476 }
477out:
478 /* The session may be reset by one of the error handlers. */
479 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400480 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400481 return 1;
482out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400483 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400484 goto out;
485 rpc_delay(task, NFS4_POLL_RETRY_MAX);
486 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400487}
488
Trond Myklebust14516c32010-07-31 14:29:06 -0400489static int nfs4_sequence_done(struct rpc_task *task,
490 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400491{
Trond Myklebust14516c32010-07-31 14:29:06 -0400492 if (res->sr_session == NULL)
493 return 1;
494 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400495}
496
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400497/*
Benny Halevy510b8172009-04-01 09:22:14 -0400498 * nfs4_find_slot - efficiently look for a free slot
499 *
500 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
501 * If found, we mark the slot as used, update the highest_used_slotid,
502 * and respectively set up the sequence operation args.
503 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400504 *
505 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400506 */
507static u8
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800508nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400509{
510 int slotid;
511 u8 ret_id = NFS4_MAX_SLOT_TABLE;
512 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
513
Benny Halevy510b8172009-04-01 09:22:14 -0400514 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
515 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
516 tbl->max_slots);
517 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
518 if (slotid >= tbl->max_slots)
519 goto out;
520 __set_bit(slotid, tbl->used_slots);
521 if (slotid > tbl->highest_used_slotid)
522 tbl->highest_used_slotid = slotid;
523 ret_id = slotid;
524out:
525 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
526 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400527 return ret_id;
528}
529
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000530int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400531 struct nfs4_sequence_args *args,
532 struct nfs4_sequence_res *res,
533 int cache_reply,
534 struct rpc_task *task)
535{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400536 struct nfs4_slot *slot;
537 struct nfs4_slot_table *tbl;
538 u8 slotid;
539
540 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400541 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400542 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400543 return 0;
544
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400545 tbl = &session->fc_slot_table;
546
547 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400548 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800549 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamsonea028ac2009-12-04 15:55:38 -0500550 /*
551 * The state manager will wait until the slot table is empty.
552 * Schedule the reset thread
553 */
Andy Adamson05f0d232009-12-04 15:55:37 -0500554 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400555 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500556 dprintk("%s Schedule Session Reset\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500557 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400558 }
559
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800560 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
561 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
562 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
563 spin_unlock(&tbl->slot_tbl_lock);
564 dprintk("%s enforce FIFO order\n", __func__);
565 return -EAGAIN;
566 }
567
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800568 slotid = nfs4_find_slot(tbl);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400569 if (slotid == NFS4_MAX_SLOT_TABLE) {
570 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
571 spin_unlock(&tbl->slot_tbl_lock);
572 dprintk("<-- %s: no free slots\n", __func__);
573 return -EAGAIN;
574 }
575 spin_unlock(&tbl->slot_tbl_lock);
576
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800577 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400578 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400579 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400580 args->sa_slotid = slotid;
581 args->sa_cache_this = cache_reply;
582
583 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
584
Benny Halevy99fe60d2009-04-01 09:22:29 -0400585 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400586 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400587 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400588 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400589 /*
590 * sr_status is only set in decode_sequence, and so will remain
591 * set to 1 if an rpc level failure occurs.
592 */
593 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400594 return 0;
595}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000596EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400597
Trond Myklebust035168a2010-06-16 09:52:26 -0400598int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400599 struct nfs4_sequence_args *args,
600 struct nfs4_sequence_res *res,
601 int cache_reply,
602 struct rpc_task *task)
603{
Trond Myklebust035168a2010-06-16 09:52:26 -0400604 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400605 int ret = 0;
606
Trond Myklebust035168a2010-06-16 09:52:26 -0400607 if (session == NULL) {
608 args->sa_session = NULL;
609 res->sr_session = NULL;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400610 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400611 }
612
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200613 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400614 __func__, session->clp, session, res->sr_slot ?
615 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400616
617 ret = nfs41_setup_sequence(session, args, res, cache_reply,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400618 task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400619out:
620 dprintk("<-- %s status=%d\n", __func__, ret);
621 return ret;
622}
623
624struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400625 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400626 struct nfs4_sequence_args *seq_args;
627 struct nfs4_sequence_res *seq_res;
628 int cache_reply;
629};
630
631static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
632{
633 struct nfs41_call_sync_data *data = calldata;
634
Trond Myklebust035168a2010-06-16 09:52:26 -0400635 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
636
637 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400638 data->seq_res, data->cache_reply, task))
639 return;
640 rpc_call_start(task);
641}
642
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800643static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
644{
645 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
646 nfs41_call_sync_prepare(task, calldata);
647}
648
Andy Adamson69ab40c2009-04-01 09:22:19 -0400649static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
650{
651 struct nfs41_call_sync_data *data = calldata;
652
Trond Myklebust14516c32010-07-31 14:29:06 -0400653 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400654}
655
Andy Adamsonce5039c2009-04-01 09:22:13 -0400656struct rpc_call_ops nfs41_call_sync_ops = {
657 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400658 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400659};
660
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800661struct rpc_call_ops nfs41_call_priv_sync_ops = {
662 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
663 .rpc_call_done = nfs41_call_sync_done,
664};
665
Bryan Schumaker7c513052011-03-24 17:12:24 +0000666static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
667 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400668 struct rpc_message *msg,
669 struct nfs4_sequence_args *args,
670 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800671 int cache_reply,
672 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400673{
674 int ret;
675 struct rpc_task *task;
676 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400677 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400678 .seq_args = args,
679 .seq_res = res,
680 .cache_reply = cache_reply,
681 };
682 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000683 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400684 .rpc_message = msg,
685 .callback_ops = &nfs41_call_sync_ops,
686 .callback_data = &data
687 };
688
Benny Halevydfb4f3092010-09-24 09:17:01 -0400689 res->sr_slot = NULL;
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800690 if (privileged)
691 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400692 task = rpc_run_task(&task_setup);
693 if (IS_ERR(task))
694 ret = PTR_ERR(task);
695 else {
696 ret = task->tk_status;
697 rpc_put_task(task);
698 }
699 return ret;
700}
701
Bryan Schumaker7c513052011-03-24 17:12:24 +0000702int _nfs4_call_sync_session(struct rpc_clnt *clnt,
703 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400704 struct rpc_message *msg,
705 struct nfs4_sequence_args *args,
706 struct nfs4_sequence_res *res,
707 int cache_reply)
708{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000709 return nfs4_call_sync_sequence(clnt, server, msg, args, res, cache_reply, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400710}
711
Trond Myklebustdf896452010-06-16 09:52:26 -0400712#else
Trond Myklebust14516c32010-07-31 14:29:06 -0400713static int nfs4_sequence_done(struct rpc_task *task,
714 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400715{
Trond Myklebust14516c32010-07-31 14:29:06 -0400716 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400717}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400718#endif /* CONFIG_NFS_V4_1 */
719
Bryan Schumaker7c513052011-03-24 17:12:24 +0000720int _nfs4_call_sync(struct rpc_clnt *clnt,
721 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400722 struct rpc_message *msg,
723 struct nfs4_sequence_args *args,
724 struct nfs4_sequence_res *res,
725 int cache_reply)
726{
Benny Halevyf3752972009-04-01 09:22:04 -0400727 args->sa_session = res->sr_session = NULL;
Bryan Schumaker7c513052011-03-24 17:12:24 +0000728 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400729}
730
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000731static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000732int nfs4_call_sync(struct rpc_clnt *clnt,
733 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000734 struct rpc_message *msg,
735 struct nfs4_sequence_args *args,
736 struct nfs4_sequence_res *res,
737 int cache_reply)
738{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000739 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
740 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000741}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400742
Trond Myklebust38478b22006-05-25 01:40:57 -0400743static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
Trond Myklebust38478b22006-05-25 01:40:57 -0400745 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Trond Myklebust38478b22006-05-25 01:40:57 -0400747 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400748 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
749 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400750 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400751 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400752 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100755struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400756 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100757 struct nfs_openargs o_arg;
758 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100759 struct nfs_open_confirmargs c_arg;
760 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100761 struct nfs_fattr f_attr;
762 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400763 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100764 struct dentry *dir;
765 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400766 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100767 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100768 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400769 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100770 int rpc_status;
771 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100772};
773
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400774
775static void nfs4_init_opendata_res(struct nfs4_opendata *p)
776{
777 p->o_res.f_attr = &p->f_attr;
778 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400779 p->o_res.seqid = p->o_arg.seqid;
780 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400781 p->o_res.server = p->o_arg.server;
782 nfs_fattr_init(&p->f_attr);
783 nfs_fattr_init(&p->dir_attr);
784}
785
Trond Myklebustad389da2007-06-05 12:30:00 -0400786static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500787 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400788 const struct iattr *attrs,
789 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100790{
Trond Myklebustad389da2007-06-05 12:30:00 -0400791 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100792 struct inode *dir = parent->d_inode;
793 struct nfs_server *server = NFS_SERVER(dir);
794 struct nfs4_opendata *p;
795
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400796 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100797 if (p == NULL)
798 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400799 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100800 if (p->o_arg.seqid == NULL)
801 goto err_free;
Al Virof6948692010-01-30 13:51:04 -0500802 path_get(path);
803 p->path = *path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100804 p->dir = parent;
805 p->owner = sp;
806 atomic_inc(&sp->so_count);
807 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500808 p->o_arg.open_flags = flags;
809 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400810 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400811 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400812 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100813 p->o_arg.server = server;
814 p->o_arg.bitmask = server->attr_bitmask;
815 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400816 if (flags & O_CREAT) {
817 u32 *s;
818
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100819 p->o_arg.u.attrs = &p->attrs;
820 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400821 s = (u32 *) p->o_arg.u.verifier.data;
822 s[0] = jiffies;
823 s[1] = current->pid;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100824 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100825 p->c_arg.fh = &p->o_res.fh;
826 p->c_arg.stateid = &p->o_res.stateid;
827 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400828 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400829 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100830 return p;
831err_free:
832 kfree(p);
833err:
834 dput(parent);
835 return NULL;
836}
837
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400838static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100839{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400840 struct nfs4_opendata *p = container_of(kref,
841 struct nfs4_opendata, kref);
842
843 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400844 if (p->state != NULL)
845 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400846 nfs4_put_state_owner(p->owner);
847 dput(p->dir);
Jan Blunck31f31db12008-05-02 13:42:45 -0700848 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400849 kfree(p);
850}
851
852static void nfs4_opendata_put(struct nfs4_opendata *p)
853{
854 if (p != NULL)
855 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100856}
857
Trond Myklebust06f814a2006-01-03 09:55:07 +0100858static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
859{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100860 int ret;
861
Trond Myklebust06f814a2006-01-03 09:55:07 +0100862 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100863 return ret;
864}
865
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500866static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400867{
868 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500869
870 if (open_mode & O_EXCL)
871 goto out;
872 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400873 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500874 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
875 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400876 break;
877 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500878 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
879 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400880 break;
881 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500882 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
883 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400884 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500885out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400886 return ret;
887}
888
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500889static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400890{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500891 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400892 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500893 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400894 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500895 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400896 return 1;
897}
898
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500899static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100900{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500901 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100902 case FMODE_WRITE:
903 state->n_wronly++;
904 break;
905 case FMODE_READ:
906 state->n_rdonly++;
907 break;
908 case FMODE_READ|FMODE_WRITE:
909 state->n_rdwr++;
910 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500911 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100912}
913
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500914static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400915{
916 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
917 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
918 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500919 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400920 case FMODE_READ:
921 set_bit(NFS_O_RDONLY_STATE, &state->flags);
922 break;
923 case FMODE_WRITE:
924 set_bit(NFS_O_WRONLY_STATE, &state->flags);
925 break;
926 case FMODE_READ|FMODE_WRITE:
927 set_bit(NFS_O_RDWR_STATE, &state->flags);
928 }
929}
930
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500931static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400932{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400933 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500934 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400935 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400936}
937
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500938static 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 -0700939{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400940 /*
941 * Protect the call to nfs4_state_set_mode_locked and
942 * serialise the stateid update
943 */
944 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400945 if (deleg_stateid != NULL) {
946 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
947 set_bit(NFS_DELEGATED_STATE, &state->flags);
948 }
949 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500950 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400951 write_sequnlock(&state->seqlock);
952 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500953 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700954 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955}
956
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500957static 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 -0500958{
959 struct nfs_inode *nfsi = NFS_I(state->inode);
960 struct nfs_delegation *deleg_cur;
961 int ret = 0;
962
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500963 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500964
965 rcu_read_lock();
966 deleg_cur = rcu_dereference(nfsi->delegation);
967 if (deleg_cur == NULL)
968 goto no_delegation;
969
970 spin_lock(&deleg_cur->lock);
971 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500972 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500973 goto no_delegation_unlock;
974
975 if (delegation == NULL)
976 delegation = &deleg_cur->stateid;
977 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
978 goto no_delegation_unlock;
979
Trond Myklebustb7391f42008-12-23 15:21:52 -0500980 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500981 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500982 ret = 1;
983no_delegation_unlock:
984 spin_unlock(&deleg_cur->lock);
985no_delegation:
986 rcu_read_unlock();
987
988 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500989 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500990 ret = 1;
991 }
992
993 return ret;
994}
995
996
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500997static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400998{
999 struct nfs_delegation *delegation;
1000
1001 rcu_read_lock();
1002 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001003 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001004 rcu_read_unlock();
1005 return;
1006 }
1007 rcu_read_unlock();
1008 nfs_inode_return_delegation(inode);
1009}
1010
Trond Myklebust6ee41262007-07-08 14:11:36 -04001011static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001012{
1013 struct nfs4_state *state = opendata->state;
1014 struct nfs_inode *nfsi = NFS_I(state->inode);
1015 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001016 int open_mode = opendata->o_arg.open_flags & O_EXCL;
1017 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001018 nfs4_stateid stateid;
1019 int ret = -EAGAIN;
1020
Trond Myklebustaac00a82007-07-05 19:02:21 -04001021 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001022 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001023 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001024 if (can_open_cached(state, fmode, open_mode)) {
1025 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001026 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001027 goto out_return_state;
1028 }
1029 spin_unlock(&state->owner->so_lock);
1030 }
Trond Myklebust34310432008-12-23 15:21:38 -05001031 rcu_read_lock();
1032 delegation = rcu_dereference(nfsi->delegation);
1033 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001034 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001035 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001036 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001037 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001038 /* Save the delegation */
1039 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1040 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001041 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001042 if (ret != 0)
1043 goto out;
1044 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001045
1046 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001047 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001048 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001049 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001050out:
1051 return ERR_PTR(ret);
1052out_return_state:
1053 atomic_inc(&state->count);
1054 return state;
1055}
1056
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001057static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1058{
1059 struct inode *inode;
1060 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001061 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001062 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001063
Trond Myklebustaac00a82007-07-05 19:02:21 -04001064 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001065 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001066 goto out;
1067 }
1068
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001069 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001070 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001071 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001072 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001073 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001074 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001075 goto err;
1076 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001077 state = nfs4_get_open_state(inode, data->owner);
1078 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001079 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001080 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001081 int delegation_flags = 0;
1082
Trond Myklebust003707c2007-07-05 18:07:55 -04001083 rcu_read_lock();
1084 delegation = rcu_dereference(NFS_I(inode)->delegation);
1085 if (delegation)
1086 delegation_flags = delegation->flags;
1087 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -05001088 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001089 nfs_inode_set_delegation(state->inode,
1090 data->owner->so_cred,
1091 &data->o_res);
1092 else
1093 nfs_inode_reclaim_delegation(state->inode,
1094 data->owner->so_cred,
1095 &data->o_res);
1096 }
Trond Myklebust34310432008-12-23 15:21:38 -05001097
1098 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001099 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001100 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001101out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001102 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001103err_put_inode:
1104 iput(inode);
1105err:
1106 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001107}
1108
Trond Myklebust864472e2006-01-03 09:55:15 +01001109static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1110{
1111 struct nfs_inode *nfsi = NFS_I(state->inode);
1112 struct nfs_open_context *ctx;
1113
1114 spin_lock(&state->inode->i_lock);
1115 list_for_each_entry(ctx, &nfsi->open_files, list) {
1116 if (ctx->state != state)
1117 continue;
1118 get_nfs_open_context(ctx);
1119 spin_unlock(&state->inode->i_lock);
1120 return ctx;
1121 }
1122 spin_unlock(&state->inode->i_lock);
1123 return ERR_PTR(-ENOENT);
1124}
1125
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001126static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1127{
1128 struct nfs4_opendata *opendata;
1129
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001130 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001131 if (opendata == NULL)
1132 return ERR_PTR(-ENOMEM);
1133 opendata->state = state;
1134 atomic_inc(&state->count);
1135 return opendata;
1136}
1137
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001138static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001139{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001140 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001141 int ret;
1142
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001143 opendata->o_arg.open_flags = 0;
1144 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001145 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1146 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1147 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001148 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001149 if (ret != 0)
1150 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001151 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001152 if (IS_ERR(newstate))
1153 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001154 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001155 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001156 return 0;
1157}
1158
1159static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1160{
Trond Myklebust864472e2006-01-03 09:55:15 +01001161 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001162 int ret;
1163
1164 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001165 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001166 smp_rmb();
1167 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001168 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001169 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001170 if (ret != 0)
1171 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001172 if (newstate != state)
1173 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001174 }
1175 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001176 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001177 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001178 if (ret != 0)
1179 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001180 if (newstate != state)
1181 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001182 }
1183 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001184 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001185 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001186 if (ret != 0)
1187 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001188 if (newstate != state)
1189 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001190 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001191 /*
1192 * We may have performed cached opens for all three recoveries.
1193 * Check if we need to update the current stateid.
1194 */
1195 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1196 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001197 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001198 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1199 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001200 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001201 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001202 return 0;
1203}
1204
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205/*
1206 * OPEN_RECLAIM:
1207 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001209static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001211 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001212 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001213 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 int status;
1215
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001216 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1217 if (IS_ERR(opendata))
1218 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001219 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1220 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001221 rcu_read_lock();
1222 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001223 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001224 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001225 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001226 opendata->o_arg.u.delegation_type = delegation_type;
1227 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001228 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 return status;
1230}
1231
Trond Myklebust539cd032007-06-05 11:46:42 -04001232static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233{
1234 struct nfs_server *server = NFS_SERVER(state->inode);
1235 struct nfs4_exception exception = { };
1236 int err;
1237 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001238 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001239 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001240 break;
1241 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 } while (exception.retry);
1243 return err;
1244}
1245
Trond Myklebust864472e2006-01-03 09:55:15 +01001246static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1247{
1248 struct nfs_open_context *ctx;
1249 int ret;
1250
1251 ctx = nfs4_state_find_open_context(state);
1252 if (IS_ERR(ctx))
1253 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001254 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001255 put_nfs_open_context(ctx);
1256 return ret;
1257}
1258
Trond Myklebust13437e12007-07-06 15:10:43 -04001259static 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 -07001260{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001261 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001262 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001264 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1265 if (IS_ERR(opendata))
1266 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001267 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001268 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001269 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001270 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001271 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001272 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273}
1274
Trond Myklebust13437e12007-07-06 15:10:43 -04001275int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276{
1277 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001278 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 int err;
1280 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001281 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 switch (err) {
1283 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001284 case -ENOENT:
1285 case -ESTALE:
1286 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001287 case -NFS4ERR_BADSESSION:
1288 case -NFS4ERR_BADSLOT:
1289 case -NFS4ERR_BAD_HIGH_SLOT:
1290 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1291 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001292 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001293 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 case -NFS4ERR_STALE_CLIENTID:
1295 case -NFS4ERR_STALE_STATEID:
1296 case -NFS4ERR_EXPIRED:
1297 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001298 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001299 goto out;
1300 case -ERESTARTSYS:
1301 /*
1302 * The show must go on: exit, but mark the
1303 * stateid as needing recovery.
1304 */
1305 case -NFS4ERR_ADMIN_REVOKED:
1306 case -NFS4ERR_BAD_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001307 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001308 case -EKEYEXPIRED:
1309 /*
1310 * User RPCSEC_GSS context has expired.
1311 * We cannot recover this stateid now, so
1312 * skip it and allow recovery thread to
1313 * proceed.
1314 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001315 case -ENOMEM:
1316 err = 0;
1317 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 }
1319 err = nfs4_handle_exception(server, err, &exception);
1320 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001321out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 return err;
1323}
1324
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001325static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1326{
1327 struct nfs4_opendata *data = calldata;
1328
1329 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001330 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001331 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1332 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001333 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001334 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001335 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001336 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001337}
1338
1339static void nfs4_open_confirm_release(void *calldata)
1340{
1341 struct nfs4_opendata *data = calldata;
1342 struct nfs4_state *state = NULL;
1343
1344 /* If this request hasn't been cancelled, do nothing */
1345 if (data->cancelled == 0)
1346 goto out_free;
1347 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001348 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001349 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001350 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001351 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001352 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001353out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001354 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001355}
1356
1357static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001358 .rpc_call_done = nfs4_open_confirm_done,
1359 .rpc_release = nfs4_open_confirm_release,
1360};
1361
1362/*
1363 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1364 */
1365static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1366{
1367 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1368 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001369 struct rpc_message msg = {
1370 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1371 .rpc_argp = &data->c_arg,
1372 .rpc_resp = &data->c_res,
1373 .rpc_cred = data->owner->so_cred,
1374 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001375 struct rpc_task_setup task_setup_data = {
1376 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001377 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001378 .callback_ops = &nfs4_open_confirm_ops,
1379 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001380 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001381 .flags = RPC_TASK_ASYNC,
1382 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 int status;
1384
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001385 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001386 data->rpc_done = 0;
1387 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001388 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001389 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001390 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001391 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001392 status = nfs4_wait_for_completion_rpc_task(task);
1393 if (status != 0) {
1394 data->cancelled = 1;
1395 smp_wmb();
1396 } else
1397 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001398 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 return status;
1400}
1401
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001402static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001404 struct nfs4_opendata *data = calldata;
1405 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001406
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001407 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1408 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001409 /*
1410 * Check if we still need to send an OPEN call, or if we can use
1411 * a delegation instead.
1412 */
1413 if (data->state != NULL) {
1414 struct nfs_delegation *delegation;
1415
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001416 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001417 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001418 rcu_read_lock();
1419 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1420 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001421 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001422 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001423 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001424 }
1425 rcu_read_unlock();
1426 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001427 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001428 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001429 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001430 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001431 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001432 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1433 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001434 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001435 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001436 &data->o_arg.seq_args,
1437 &data->o_res.seq_res, 1, task))
1438 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001439 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001440 return;
1441out_no_action:
1442 task->tk_action = NULL;
1443
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001444}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001446static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1447{
1448 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1449 nfs4_open_prepare(task, calldata);
1450}
1451
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001452static void nfs4_open_done(struct rpc_task *task, void *calldata)
1453{
1454 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001456 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001457
Trond Myklebust14516c32010-07-31 14:29:06 -04001458 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1459 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001460
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001461 if (task->tk_status == 0) {
1462 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001463 case S_IFREG:
1464 break;
1465 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001466 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001467 break;
1468 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001469 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001470 break;
1471 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001472 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001473 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001474 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001475 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1476 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001477 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001478 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001479}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001480
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001481static void nfs4_open_release(void *calldata)
1482{
1483 struct nfs4_opendata *data = calldata;
1484 struct nfs4_state *state = NULL;
1485
1486 /* If this request hasn't been cancelled, do nothing */
1487 if (data->cancelled == 0)
1488 goto out_free;
1489 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001490 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001491 goto out_free;
1492 /* In case we need an open_confirm, no cleanup! */
1493 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1494 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001495 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001496 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001497 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001498out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001499 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001500}
1501
1502static const struct rpc_call_ops nfs4_open_ops = {
1503 .rpc_call_prepare = nfs4_open_prepare,
1504 .rpc_call_done = nfs4_open_done,
1505 .rpc_release = nfs4_open_release,
1506};
1507
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001508static const struct rpc_call_ops nfs4_recover_open_ops = {
1509 .rpc_call_prepare = nfs4_recover_open_prepare,
1510 .rpc_call_done = nfs4_open_done,
1511 .rpc_release = nfs4_open_release,
1512};
1513
1514static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001515{
1516 struct inode *dir = data->dir->d_inode;
1517 struct nfs_server *server = NFS_SERVER(dir);
1518 struct nfs_openargs *o_arg = &data->o_arg;
1519 struct nfs_openres *o_res = &data->o_res;
1520 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001521 struct rpc_message msg = {
1522 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1523 .rpc_argp = o_arg,
1524 .rpc_resp = o_res,
1525 .rpc_cred = data->owner->so_cred,
1526 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001527 struct rpc_task_setup task_setup_data = {
1528 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001529 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001530 .callback_ops = &nfs4_open_ops,
1531 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001532 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001533 .flags = RPC_TASK_ASYNC,
1534 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001535 int status;
1536
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001537 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001538 data->rpc_done = 0;
1539 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001540 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001541 if (isrecover)
1542 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001543 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001544 if (IS_ERR(task))
1545 return PTR_ERR(task);
1546 status = nfs4_wait_for_completion_rpc_task(task);
1547 if (status != 0) {
1548 data->cancelled = 1;
1549 smp_wmb();
1550 } else
1551 status = data->rpc_status;
1552 rpc_put_task(task);
1553
1554 return status;
1555}
1556
1557static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1558{
1559 struct inode *dir = data->dir->d_inode;
1560 struct nfs_openres *o_res = &data->o_res;
1561 int status;
1562
1563 status = nfs4_run_open_task(data, 1);
1564 if (status != 0 || !data->rpc_done)
1565 return status;
1566
1567 nfs_refresh_inode(dir, o_res->dir_attr);
1568
1569 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1570 status = _nfs4_proc_open_confirm(data);
1571 if (status != 0)
1572 return status;
1573 }
1574
1575 return status;
1576}
1577
1578/*
1579 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1580 */
1581static int _nfs4_proc_open(struct nfs4_opendata *data)
1582{
1583 struct inode *dir = data->dir->d_inode;
1584 struct nfs_server *server = NFS_SERVER(dir);
1585 struct nfs_openargs *o_arg = &data->o_arg;
1586 struct nfs_openres *o_res = &data->o_res;
1587 int status;
1588
1589 status = nfs4_run_open_task(data, 0);
Trond Myklebust3e309912007-07-07 13:19:59 -04001590 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001591 return status;
1592
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001593 if (o_arg->open_flags & O_CREAT) {
1594 update_changeattr(dir, &o_res->cinfo);
1595 nfs_post_op_update_inode(dir, o_res->dir_attr);
1596 } else
1597 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001598 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1599 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001601 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001603 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 }
1605 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001606 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001607 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608}
1609
Andy Adamsond83217c2011-03-01 01:34:17 +00001610static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001611{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001612 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001613 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001614
Trond Myklebusta78cb572009-08-09 15:06:19 -04001615 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001616 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001617 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001618 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001619 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1620 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001621 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001622 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001623 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001624 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001625 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001626}
1627
Andy Adamsond83217c2011-03-01 01:34:17 +00001628static int nfs4_recover_expired_lease(struct nfs_server *server)
1629{
1630 return nfs4_client_recover_expired_lease(server->nfs_client);
1631}
1632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633/*
1634 * OPEN_EXPIRED:
1635 * reclaim state on the server after a network partition.
1636 * Assumes caller holds the appropriate lock
1637 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001638static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001640 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001641 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001643 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1644 if (IS_ERR(opendata))
1645 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001646 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001647 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001648 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001649 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001650 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651}
1652
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001653static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001654{
Trond Myklebust539cd032007-06-05 11:46:42 -04001655 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001656 struct nfs4_exception exception = { };
1657 int err;
1658
1659 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001660 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001661 switch (err) {
1662 default:
1663 goto out;
1664 case -NFS4ERR_GRACE:
1665 case -NFS4ERR_DELAY:
1666 nfs4_handle_exception(server, err, &exception);
1667 err = 0;
1668 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001669 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001670out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001671 return err;
1672}
1673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1675{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001677 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Trond Myklebust864472e2006-01-03 09:55:15 +01001679 ctx = nfs4_state_find_open_context(state);
1680 if (IS_ERR(ctx))
1681 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001682 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001683 put_nfs_open_context(ctx);
1684 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685}
1686
1687/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001688 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1689 * fields corresponding to attributes that were used to store the verifier.
1690 * Make sure we clobber those fields in the later setattr call
1691 */
1692static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1693{
1694 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1695 !(sattr->ia_valid & ATTR_ATIME_SET))
1696 sattr->ia_valid |= ATTR_ATIME;
1697
1698 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1699 !(sattr->ia_valid & ATTR_MTIME_SET))
1700 sattr->ia_valid |= ATTR_MTIME;
1701}
1702
1703/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001704 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001706static 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 -07001707{
1708 struct nfs4_state_owner *sp;
1709 struct nfs4_state *state = NULL;
1710 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001711 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001712 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
1714 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 status = -ENOMEM;
1716 if (!(sp = nfs4_get_state_owner(server, cred))) {
1717 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1718 goto out_err;
1719 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001720 status = nfs4_recover_expired_lease(server);
1721 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001722 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001723 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001724 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001725 status = -ENOMEM;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001726 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001727 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001728 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Trond Myklebustaac00a82007-07-05 19:02:21 -04001730 if (path->dentry->d_inode != NULL)
1731 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1732
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001733 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001735 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001737 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001738 status = PTR_ERR(state);
1739 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001740 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001741 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001742 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001743
1744 if (opendata->o_arg.open_flags & O_EXCL) {
1745 nfs4_exclusive_attrset(opendata, sattr);
1746
1747 nfs_fattr_init(opendata->o_res.f_attr);
1748 status = nfs4_do_setattr(state->inode, cred,
1749 opendata->o_res.f_attr, sattr,
1750 state);
1751 if (status == 0)
1752 nfs_setattr_update_inode(state->inode, sattr);
1753 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1754 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001755 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 *res = state;
1758 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001759err_opendata_put:
1760 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001761err_put_state_owner:
1762 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 *res = NULL;
1765 return status;
1766}
1767
1768
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001769static 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 -07001770{
1771 struct nfs4_exception exception = { };
1772 struct nfs4_state *res;
1773 int status;
1774
1775 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001776 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 if (status == 0)
1778 break;
1779 /* NOTE: BAD_SEQID means the server and client disagree about the
1780 * book-keeping w.r.t. state-changing operations
1781 * (OPEN/CLOSE/LOCK/LOCKU...)
1782 * It is actually a sign of a bug on the client or on the server.
1783 *
1784 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001785 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 * have unhashed the old state_owner for us, and that we can
1787 * therefore safely retry using a new one. We should still warn
1788 * the user though...
1789 */
1790 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001791 printk(KERN_WARNING "NFS: v4 server %s "
1792 " returned a bad sequence-id error!\n",
1793 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 exception.retry = 1;
1795 continue;
1796 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001797 /*
1798 * BAD_STATEID on OPEN means that the server cancelled our
1799 * state before it received the OPEN_CONFIRM.
1800 * Recover by retrying the request as per the discussion
1801 * on Page 181 of RFC3530.
1802 */
1803 if (status == -NFS4ERR_BAD_STATEID) {
1804 exception.retry = 1;
1805 continue;
1806 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001807 if (status == -EAGAIN) {
1808 /* We must have found a delegation */
1809 exception.retry = 1;
1810 continue;
1811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1813 status, &exception));
1814 } while (exception.retry);
1815 return res;
1816}
1817
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001818static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1819 struct nfs_fattr *fattr, struct iattr *sattr,
1820 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001822 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001824 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 .iap = sattr,
1826 .server = server,
1827 .bitmask = server->attr_bitmask,
1828 };
1829 struct nfs_setattrres res = {
1830 .fattr = fattr,
1831 .server = server,
1832 };
1833 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001834 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1835 .rpc_argp = &arg,
1836 .rpc_resp = &res,
1837 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001839 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001840 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
Trond Myklebust0e574af2005-10-27 22:12:38 -04001842 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001844 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1845 /* Use that stateid */
1846 } else if (state != NULL) {
Trond Myklebust77041ed2010-07-01 12:49:11 -04001847 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001848 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1850
Bryan Schumaker7c513052011-03-24 17:12:24 +00001851 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001852 if (status == 0 && state != NULL)
1853 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001854 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855}
1856
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001857static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1858 struct nfs_fattr *fattr, struct iattr *sattr,
1859 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001861 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 struct nfs4_exception exception = { };
1863 int err;
1864 do {
1865 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001866 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 &exception);
1868 } while (exception.retry);
1869 return err;
1870}
1871
1872struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001873 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 struct inode *inode;
1875 struct nfs4_state *state;
1876 struct nfs_closeargs arg;
1877 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001878 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001879 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00001880 bool roc;
1881 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882};
1883
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001884static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001885{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001886 struct nfs4_closedata *calldata = data;
1887 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001888
Fred Isamanf7e89172011-01-06 11:36:32 +00001889 if (calldata->roc)
1890 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001891 nfs4_put_open_state(calldata->state);
1892 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001893 nfs4_put_state_owner(sp);
Jan Blunck31f31db12008-05-02 13:42:45 -07001894 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001895 kfree(calldata);
1896}
1897
Trond Myklebust88069f72009-12-08 08:33:16 -05001898static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1899 fmode_t fmode)
1900{
1901 spin_lock(&state->owner->so_lock);
1902 if (!(fmode & FMODE_READ))
1903 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1904 if (!(fmode & FMODE_WRITE))
1905 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1906 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1907 spin_unlock(&state->owner->so_lock);
1908}
1909
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001910static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001912 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 struct nfs_server *server = NFS_SERVER(calldata->inode);
1915
Trond Myklebust14516c32010-07-31 14:29:06 -04001916 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1917 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 /* hmm. we are done with the inode, and in the process of freeing
1919 * the state_owner. we keep this around to process errors
1920 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 switch (task->tk_status) {
1922 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00001923 if (calldata->roc)
1924 pnfs_roc_set_barrier(state->inode,
1925 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04001926 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001927 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001928 nfs4_close_clear_stateid_flags(state,
1929 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 break;
1931 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001932 case -NFS4ERR_OLD_STATEID:
1933 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001935 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001936 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001938 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1939 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001941 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001942 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943}
1944
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001945static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001947 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001948 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001949 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001950
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001951 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001952 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001953
Trond Myklebust88069f72009-12-08 08:33:16 -05001954 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1955 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001956 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001957 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001958 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001959 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001960 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1961 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1962 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001963 }
1964 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001965 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1966 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1967 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001968 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001969 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001970 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05001971
1972 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001973 /* Note: exit _without_ calling nfs4_close_done */
1974 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001975 return;
1976 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001977
Fred Isamanf7e89172011-01-06 11:36:32 +00001978 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001979 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00001980 if (calldata->roc &&
1981 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
1982 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
1983 task, NULL);
1984 return;
1985 }
1986 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001987
Trond Myklebust516a6af2005-10-27 22:12:41 -04001988 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001989 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001990 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Andy Adamson19ddab02009-04-01 09:22:20 -04001991 &calldata->arg.seq_args, &calldata->res.seq_res,
1992 1, task))
1993 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001994 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995}
1996
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001997static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001998 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001999 .rpc_call_done = nfs4_close_done,
2000 .rpc_release = nfs4_free_closedata,
2001};
2002
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003/*
2004 * It is possible for data to be read/written from a mem-mapped file
2005 * after the sys_close call (which hits the vfs layer as a flush).
2006 * This means that we can't safely call nfsv4 close on a file until
2007 * the inode is cleared. This in turn means that we are not good
2008 * NFSv4 citizens - we do not indicate to the server to update the file's
2009 * share state even when we are done with one of the three share
2010 * stateid's in the inode.
2011 *
2012 * NOTE: Caller must be holding the sp->so_owner semaphore!
2013 */
Fred Isamanf7e89172011-01-06 11:36:32 +00002014int nfs4_do_close(struct path *path, struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002016 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002018 struct nfs4_state_owner *sp = state->owner;
2019 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002020 struct rpc_message msg = {
2021 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2022 .rpc_cred = state->owner->so_cred,
2023 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002024 struct rpc_task_setup task_setup_data = {
2025 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002026 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002027 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002028 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002029 .flags = RPC_TASK_ASYNC,
2030 };
Trond Myklebust95121352005-10-18 14:20:12 -07002031 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002033 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002035 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002036 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002038 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002039 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002041 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002042 if (calldata->arg.seqid == NULL)
2043 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002044 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002045 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002046 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002047 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002048 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002049 calldata->roc = roc;
Al Virof6948692010-01-30 13:51:04 -05002050 path_get(path);
2051 calldata->path = *path;
Trond Myklebust95121352005-10-18 14:20:12 -07002052
Trond Myklebust1174dd12010-12-21 10:52:24 -05002053 msg.rpc_argp = &calldata->arg;
2054 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002055 task_setup_data.callback_data = calldata;
2056 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002057 if (IS_ERR(task))
2058 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002059 status = 0;
2060 if (wait)
2061 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002062 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002063 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002064out_free_calldata:
2065 kfree(calldata);
2066out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002067 if (roc)
2068 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002069 nfs4_put_open_state(state);
2070 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002071 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072}
2073
Trond Myklebust2b484292010-09-17 10:56:51 -04002074static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002075nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 struct nfs4_state *state;
2078
Trond Myklebust565277f2007-10-15 18:17:53 -04002079 /* Protect against concurrent sillydeletes */
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002080 state = nfs4_do_open(dir, &ctx->path, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002081 if (IS_ERR(state))
2082 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002083 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002084 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085}
2086
Trond Myklebust1185a552009-12-03 15:54:02 -05002087static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002088{
2089 if (ctx->state == NULL)
2090 return;
2091 if (is_sync)
2092 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2093 else
2094 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2095}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
2097static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2098{
Benny Halevy43652ad2009-04-01 09:21:54 -04002099 struct nfs4_server_caps_arg args = {
2100 .fhandle = fhandle,
2101 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 struct nfs4_server_caps_res res = {};
2103 struct rpc_message msg = {
2104 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002105 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 .rpc_resp = &res,
2107 };
2108 int status;
2109
Bryan Schumaker7c513052011-03-24 17:12:24 +00002110 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 if (status == 0) {
2112 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002113 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2114 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2115 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2116 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2117 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2119 server->caps |= NFS_CAP_ACLS;
2120 if (res.has_links != 0)
2121 server->caps |= NFS_CAP_HARDLINKS;
2122 if (res.has_symlinks != 0)
2123 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002124 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2125 server->caps |= NFS_CAP_FILEID;
2126 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2127 server->caps |= NFS_CAP_MODE;
2128 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2129 server->caps |= NFS_CAP_NLINK;
2130 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2131 server->caps |= NFS_CAP_OWNER;
2132 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2133 server->caps |= NFS_CAP_OWNER_GROUP;
2134 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2135 server->caps |= NFS_CAP_ATIME;
2136 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2137 server->caps |= NFS_CAP_CTIME;
2138 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2139 server->caps |= NFS_CAP_MTIME;
2140
Trond Myklebusta65318b2009-03-11 14:10:28 -04002141 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2142 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2143 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 server->acl_bitmask = res.acl_bitmask;
2145 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002146
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 return status;
2148}
2149
Trond Myklebust55a97592006-06-09 09:34:19 -04002150int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151{
2152 struct nfs4_exception exception = { };
2153 int err;
2154 do {
2155 err = nfs4_handle_exception(server,
2156 _nfs4_server_capabilities(server, fhandle),
2157 &exception);
2158 } while (exception.retry);
2159 return err;
2160}
2161
2162static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2163 struct nfs_fsinfo *info)
2164{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 struct nfs4_lookup_root_arg args = {
2166 .bitmask = nfs4_fattr_bitmap,
2167 };
2168 struct nfs4_lookup_res res = {
2169 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002170 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 .fh = fhandle,
2172 };
2173 struct rpc_message msg = {
2174 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2175 .rpc_argp = &args,
2176 .rpc_resp = &res,
2177 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002178
Trond Myklebust0e574af2005-10-27 22:12:38 -04002179 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002180 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181}
2182
2183static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2184 struct nfs_fsinfo *info)
2185{
2186 struct nfs4_exception exception = { };
2187 int err;
2188 do {
2189 err = nfs4_handle_exception(server,
2190 _nfs4_lookup_root(server, fhandle, info),
2191 &exception);
2192 } while (exception.retry);
2193 return err;
2194}
2195
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002196static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2197 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2198{
2199 struct rpc_auth *auth;
2200 int ret;
2201
2202 auth = rpcauth_create(flavor, server->client);
2203 if (!auth) {
2204 ret = -EIO;
2205 goto out;
2206 }
2207 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002208out:
2209 return ret;
2210}
2211
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002212static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002213 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002215 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002216 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002218 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2219 flav_array[len] = RPC_AUTH_NULL;
2220 len += 1;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002221
2222 for (i = 0; i < len; i++) {
2223 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002224 if (status == -EPERM || status == -EACCES)
2225 continue;
2226 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002227 }
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002228 return status;
2229}
2230
2231/*
2232 * get the file handle for the "/" directory on the server
2233 */
2234static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2235 struct nfs_fsinfo *info)
2236{
2237 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker9b7160c2011-04-13 14:31:30 -04002238 if ((status == -EPERM) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002239 status = nfs4_find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 if (status == 0)
2241 status = nfs4_server_capabilities(server, fhandle);
2242 if (status == 0)
2243 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002244 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245}
2246
Manoj Naik6b97fd32006-06-09 09:34:29 -04002247/*
2248 * Get locations and (maybe) other attributes of a referral.
2249 * Note that we'll actually follow the referral later when
2250 * we detect fsid mismatch in inode revalidation
2251 */
Trond Myklebust56659e92007-07-17 21:52:39 -04002252static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002253{
2254 int status = -ENOMEM;
2255 struct page *page = NULL;
2256 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002257
2258 page = alloc_page(GFP_KERNEL);
2259 if (page == NULL)
2260 goto out;
2261 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2262 if (locations == NULL)
2263 goto out;
2264
Trond Myklebustc228fd32007-01-13 02:28:11 -05002265 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002266 if (status != 0)
2267 goto out;
2268 /* Make sure server returned a different fsid for the referral */
2269 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07002270 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002271 status = -EIO;
2272 goto out;
2273 }
2274
2275 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2276 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2277 if (!fattr->mode)
2278 fattr->mode = S_IFDIR;
2279 memset(fhandle, 0, sizeof(struct nfs_fh));
2280out:
2281 if (page)
2282 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002283 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002284 return status;
2285}
2286
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2288{
2289 struct nfs4_getattr_arg args = {
2290 .fh = fhandle,
2291 .bitmask = server->attr_bitmask,
2292 };
2293 struct nfs4_getattr_res res = {
2294 .fattr = fattr,
2295 .server = server,
2296 };
2297 struct rpc_message msg = {
2298 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2299 .rpc_argp = &args,
2300 .rpc_resp = &res,
2301 };
2302
Trond Myklebust0e574af2005-10-27 22:12:38 -04002303 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002304 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305}
2306
2307static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2308{
2309 struct nfs4_exception exception = { };
2310 int err;
2311 do {
2312 err = nfs4_handle_exception(server,
2313 _nfs4_proc_getattr(server, fhandle, fattr),
2314 &exception);
2315 } while (exception.retry);
2316 return err;
2317}
2318
2319/*
2320 * The file is not closed if it is opened due to the a request to change
2321 * the size of the file. The open call will not be needed once the
2322 * VFS layer lookup-intents are implemented.
2323 *
2324 * Close is called when the inode is destroyed.
2325 * If we haven't opened the file for O_WRONLY, we
2326 * need to in the size_change case to obtain a stateid.
2327 *
2328 * Got race?
2329 * Because OPEN is always done by name in nfsv4, it is
2330 * possible that we opened a different file by the same
2331 * name. We can recognize this race condition, but we
2332 * can't do anything about it besides returning an error.
2333 *
2334 * This will be fixed with VFS changes (lookup-intent).
2335 */
2336static int
2337nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2338 struct iattr *sattr)
2339{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002340 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002341 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002342 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 int status;
2344
Trond Myklebust0e574af2005-10-27 22:12:38 -04002345 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
Trond Myklebustd5308382005-11-04 15:33:38 -05002347 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002348 if (sattr->ia_valid & ATTR_FILE) {
2349 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002350
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002351 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002352 if (ctx) {
2353 cred = ctx->cred;
2354 state = ctx->state;
2355 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002356 }
2357
2358 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002359 if (status == 0)
2360 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 return status;
2362}
2363
Bryan Schumaker7c513052011-03-24 17:12:24 +00002364static int _nfs4_proc_lookupfh(struct rpc_clnt *clnt, struct nfs_server *server,
2365 const struct nfs_fh *dirfh, const struct qstr *name,
2366 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002367{
2368 int status;
2369 struct nfs4_lookup_arg args = {
2370 .bitmask = server->attr_bitmask,
2371 .dir_fh = dirfh,
2372 .name = name,
2373 };
2374 struct nfs4_lookup_res res = {
2375 .server = server,
2376 .fattr = fattr,
2377 .fh = fhandle,
2378 };
2379 struct rpc_message msg = {
2380 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2381 .rpc_argp = &args,
2382 .rpc_resp = &res,
2383 };
2384
2385 nfs_fattr_init(fattr);
2386
2387 dprintk("NFS call lookupfh %s\n", name->name);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002388 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002389 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002390 return status;
2391}
2392
2393static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2394 struct qstr *name, struct nfs_fh *fhandle,
2395 struct nfs_fattr *fattr)
2396{
2397 struct nfs4_exception exception = { };
2398 int err;
2399 do {
Bryan Schumaker7c513052011-03-24 17:12:24 +00002400 err = _nfs4_proc_lookupfh(server->client, server, dirfh, name, fhandle, fattr);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002401 /* FIXME: !!!! */
2402 if (err == -NFS4ERR_MOVED) {
2403 err = -EREMOTE;
2404 break;
2405 }
2406 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002407 } while (exception.retry);
2408 return err;
2409}
2410
Bryan Schumaker7c513052011-03-24 17:12:24 +00002411static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2412 const struct qstr *name, struct nfs_fh *fhandle,
2413 struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002415 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
2417 dprintk("NFS call lookup %s\n", name->name);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002418 status = _nfs4_proc_lookupfh(clnt, NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002419 if (status == -NFS4ERR_MOVED)
2420 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 dprintk("NFS reply lookup: %d\n", status);
2422 return status;
2423}
2424
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002425void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2426{
2427 memset(fh, 0, sizeof(struct nfs_fh));
2428 fattr->fsid.major = 1;
2429 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2430 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2431 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2432 fattr->nlink = 2;
2433}
2434
Bryan Schumaker7c513052011-03-24 17:12:24 +00002435static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2436 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437{
2438 struct nfs4_exception exception = { };
2439 int err;
2440 do {
2441 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumaker7c513052011-03-24 17:12:24 +00002442 _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 &exception);
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002444 if (err == -EPERM)
2445 nfs_fixup_secinfo_attributes(fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 } while (exception.retry);
2447 return err;
2448}
2449
2450static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2451{
Trond Myklebust76b32992007-08-10 17:45:11 -04002452 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 struct nfs4_accessargs args = {
2454 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002455 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002457 struct nfs4_accessres res = {
2458 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002459 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 struct rpc_message msg = {
2461 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2462 .rpc_argp = &args,
2463 .rpc_resp = &res,
2464 .rpc_cred = entry->cred,
2465 };
2466 int mode = entry->mask;
2467 int status;
2468
2469 /*
2470 * Determine which access bits we want to ask for...
2471 */
2472 if (mode & MAY_READ)
2473 args.access |= NFS4_ACCESS_READ;
2474 if (S_ISDIR(inode->i_mode)) {
2475 if (mode & MAY_WRITE)
2476 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2477 if (mode & MAY_EXEC)
2478 args.access |= NFS4_ACCESS_LOOKUP;
2479 } else {
2480 if (mode & MAY_WRITE)
2481 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2482 if (mode & MAY_EXEC)
2483 args.access |= NFS4_ACCESS_EXECUTE;
2484 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002485
2486 res.fattr = nfs_alloc_fattr();
2487 if (res.fattr == NULL)
2488 return -ENOMEM;
2489
Bryan Schumaker7c513052011-03-24 17:12:24 +00002490 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 if (!status) {
2492 entry->mask = 0;
2493 if (res.access & NFS4_ACCESS_READ)
2494 entry->mask |= MAY_READ;
2495 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2496 entry->mask |= MAY_WRITE;
2497 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2498 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002499 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002501 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 return status;
2503}
2504
2505static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2506{
2507 struct nfs4_exception exception = { };
2508 int err;
2509 do {
2510 err = nfs4_handle_exception(NFS_SERVER(inode),
2511 _nfs4_proc_access(inode, entry),
2512 &exception);
2513 } while (exception.retry);
2514 return err;
2515}
2516
2517/*
2518 * TODO: For the time being, we don't try to get any attributes
2519 * along with any of the zero-copy operations READ, READDIR,
2520 * READLINK, WRITE.
2521 *
2522 * In the case of the first three, we want to put the GETATTR
2523 * after the read-type operation -- this is because it is hard
2524 * to predict the length of a GETATTR response in v4, and thus
2525 * align the READ data correctly. This means that the GETATTR
2526 * may end up partially falling into the page cache, and we should
2527 * shift it into the 'tail' of the xdr_buf before processing.
2528 * To do this efficiently, we need to know the total length
2529 * of data received, which doesn't seem to be available outside
2530 * of the RPC layer.
2531 *
2532 * In the case of WRITE, we also want to put the GETATTR after
2533 * the operation -- in this case because we want to make sure
2534 * we get the post-operation mtime and size. This means that
2535 * we can't use xdr_encode_pages() as written: we need a variant
2536 * of it which would leave room in the 'tail' iovec.
2537 *
2538 * Both of these changes to the XDR layer would in fact be quite
2539 * minor, but I decided to leave them for a subsequent patch.
2540 */
2541static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2542 unsigned int pgbase, unsigned int pglen)
2543{
2544 struct nfs4_readlink args = {
2545 .fh = NFS_FH(inode),
2546 .pgbase = pgbase,
2547 .pglen = pglen,
2548 .pages = &page,
2549 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002550 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 struct rpc_message msg = {
2552 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2553 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002554 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 };
2556
Bryan Schumaker7c513052011-03-24 17:12:24 +00002557 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 -07002558}
2559
2560static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2561 unsigned int pgbase, unsigned int pglen)
2562{
2563 struct nfs4_exception exception = { };
2564 int err;
2565 do {
2566 err = nfs4_handle_exception(NFS_SERVER(inode),
2567 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2568 &exception);
2569 } while (exception.retry);
2570 return err;
2571}
2572
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573/*
2574 * Got race?
2575 * We will need to arrange for the VFS layer to provide an atomic open.
2576 * Until then, this create/open method is prone to inefficiency and race
2577 * conditions due to the lookup, create, and open VFS calls from sys_open()
2578 * placed on the wire.
2579 *
2580 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2581 * The file will be opened again in the subsequent VFS open call
2582 * (nfs4_proc_file_open).
2583 *
2584 * The open for read will just hang around to be used by any process that
2585 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2586 */
2587
2588static int
2589nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002590 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591{
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002592 struct path my_path = {
Trond Myklebustad389da2007-06-05 12:30:00 -04002593 .dentry = dentry,
2594 };
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002595 struct path *path = &my_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002597 struct rpc_cred *cred = NULL;
2598 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 int status = 0;
2600
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002601 if (ctx != NULL) {
2602 cred = ctx->cred;
2603 path = &ctx->path;
2604 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002606 sattr->ia_mode &= ~current_umask();
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002607 state = nfs4_do_open(dir, path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002608 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 if (IS_ERR(state)) {
2610 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002611 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002613 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002614 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002615 if (ctx != NULL)
2616 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002617 else
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002618 nfs4_close_sync(path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619out:
2620 return status;
2621}
2622
2623static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2624{
Trond Myklebust16e42952005-10-27 22:12:44 -04002625 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002626 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002628 .name.len = name->len,
2629 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002630 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002632 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002633 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002634 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002636 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2637 .rpc_argp = &args,
2638 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002640 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
Trond Myklebustd3468902010-04-16 16:22:50 -04002642 res.dir_attr = nfs_alloc_fattr();
2643 if (res.dir_attr == NULL)
2644 goto out;
2645
Bryan Schumaker7c513052011-03-24 17:12:24 +00002646 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002647 if (status == 0) {
2648 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002649 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002650 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002651 nfs_free_fattr(res.dir_attr);
2652out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 return status;
2654}
2655
2656static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2657{
2658 struct nfs4_exception exception = { };
2659 int err;
2660 do {
2661 err = nfs4_handle_exception(NFS_SERVER(dir),
2662 _nfs4_proc_remove(dir, name),
2663 &exception);
2664 } while (exception.retry);
2665 return err;
2666}
2667
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002668static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002670 struct nfs_server *server = NFS_SERVER(dir);
2671 struct nfs_removeargs *args = msg->rpc_argp;
2672 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673
Trond Myklebusta65318b2009-03-11 14:10:28 -04002674 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002675 res->server = server;
Benny Halevydfb4f3092010-09-24 09:17:01 -04002676 res->seq_res.sr_slot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678}
2679
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002680static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002682 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2683
Trond Myklebust14516c32010-07-31 14:29:06 -04002684 if (!nfs4_sequence_done(task, &res->seq_res))
2685 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002686 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002687 return 0;
2688 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002689 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002690 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691}
2692
Jeff Laytond3d41522010-09-17 17:31:57 -04002693static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2694{
2695 struct nfs_server *server = NFS_SERVER(dir);
2696 struct nfs_renameargs *arg = msg->rpc_argp;
2697 struct nfs_renameres *res = msg->rpc_resp;
2698
2699 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2700 arg->bitmask = server->attr_bitmask;
2701 res->server = server;
2702}
2703
2704static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2705 struct inode *new_dir)
2706{
2707 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2708
2709 if (!nfs4_sequence_done(task, &res->seq_res))
2710 return 0;
2711 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2712 return 0;
2713
2714 update_changeattr(old_dir, &res->old_cinfo);
2715 nfs_post_op_update_inode(old_dir, res->old_fattr);
2716 update_changeattr(new_dir, &res->new_cinfo);
2717 nfs_post_op_update_inode(new_dir, res->new_fattr);
2718 return 1;
2719}
2720
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2722 struct inode *new_dir, struct qstr *new_name)
2723{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002724 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002725 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 .old_dir = NFS_FH(old_dir),
2727 .new_dir = NFS_FH(new_dir),
2728 .old_name = old_name,
2729 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002730 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002732 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002733 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002734 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 struct rpc_message msg = {
2736 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2737 .rpc_argp = &arg,
2738 .rpc_resp = &res,
2739 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002740 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741
Trond Myklebust011fff72010-04-16 16:22:49 -04002742 res.old_fattr = nfs_alloc_fattr();
2743 res.new_fattr = nfs_alloc_fattr();
2744 if (res.old_fattr == NULL || res.new_fattr == NULL)
2745 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746
Bryan Schumaker7c513052011-03-24 17:12:24 +00002747 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 if (!status) {
2749 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002750 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002752 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002754out:
2755 nfs_free_fattr(res.new_fattr);
2756 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 return status;
2758}
2759
2760static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2761 struct inode *new_dir, struct qstr *new_name)
2762{
2763 struct nfs4_exception exception = { };
2764 int err;
2765 do {
2766 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2767 _nfs4_proc_rename(old_dir, old_name,
2768 new_dir, new_name),
2769 &exception);
2770 } while (exception.retry);
2771 return err;
2772}
2773
2774static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2775{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002776 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 struct nfs4_link_arg arg = {
2778 .fh = NFS_FH(inode),
2779 .dir_fh = NFS_FH(dir),
2780 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002781 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002783 struct nfs4_link_res res = {
2784 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002785 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 struct rpc_message msg = {
2787 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2788 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002789 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002791 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792
Trond Myklebust136f2622010-04-16 16:22:49 -04002793 res.fattr = nfs_alloc_fattr();
2794 res.dir_attr = nfs_alloc_fattr();
2795 if (res.fattr == NULL || res.dir_attr == NULL)
2796 goto out;
2797
Bryan Schumaker7c513052011-03-24 17:12:24 +00002798 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002799 if (!status) {
2800 update_changeattr(dir, &res.cinfo);
2801 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002802 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002803 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002804out:
2805 nfs_free_fattr(res.dir_attr);
2806 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 return status;
2808}
2809
2810static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2811{
2812 struct nfs4_exception exception = { };
2813 int err;
2814 do {
2815 err = nfs4_handle_exception(NFS_SERVER(inode),
2816 _nfs4_proc_link(inode, dir, name),
2817 &exception);
2818 } while (exception.retry);
2819 return err;
2820}
2821
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002822struct nfs4_createdata {
2823 struct rpc_message msg;
2824 struct nfs4_create_arg arg;
2825 struct nfs4_create_res res;
2826 struct nfs_fh fh;
2827 struct nfs_fattr fattr;
2828 struct nfs_fattr dir_fattr;
2829};
2830
2831static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2832 struct qstr *name, struct iattr *sattr, u32 ftype)
2833{
2834 struct nfs4_createdata *data;
2835
2836 data = kzalloc(sizeof(*data), GFP_KERNEL);
2837 if (data != NULL) {
2838 struct nfs_server *server = NFS_SERVER(dir);
2839
2840 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2841 data->msg.rpc_argp = &data->arg;
2842 data->msg.rpc_resp = &data->res;
2843 data->arg.dir_fh = NFS_FH(dir);
2844 data->arg.server = server;
2845 data->arg.name = name;
2846 data->arg.attrs = sattr;
2847 data->arg.ftype = ftype;
2848 data->arg.bitmask = server->attr_bitmask;
2849 data->res.server = server;
2850 data->res.fh = &data->fh;
2851 data->res.fattr = &data->fattr;
2852 data->res.dir_fattr = &data->dir_fattr;
2853 nfs_fattr_init(data->res.fattr);
2854 nfs_fattr_init(data->res.dir_fattr);
2855 }
2856 return data;
2857}
2858
2859static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2860{
Bryan Schumaker7c513052011-03-24 17:12:24 +00002861 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00002862 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002863 if (status == 0) {
2864 update_changeattr(dir, &data->res.dir_cinfo);
2865 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2866 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2867 }
2868 return status;
2869}
2870
2871static void nfs4_free_createdata(struct nfs4_createdata *data)
2872{
2873 kfree(data);
2874}
2875
Chuck Lever4f390c12006-08-22 20:06:22 -04002876static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002877 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002879 struct nfs4_createdata *data;
2880 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881
Chuck Lever94a6d752006-08-22 20:06:23 -04002882 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002883 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002884
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002885 status = -ENOMEM;
2886 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2887 if (data == NULL)
2888 goto out;
2889
2890 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2891 data->arg.u.symlink.pages = &page;
2892 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002894 status = nfs4_do_create(dir, dentry, data);
2895
2896 nfs4_free_createdata(data);
2897out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 return status;
2899}
2900
Chuck Lever4f390c12006-08-22 20:06:22 -04002901static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002902 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903{
2904 struct nfs4_exception exception = { };
2905 int err;
2906 do {
2907 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002908 _nfs4_proc_symlink(dir, dentry, page,
2909 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 &exception);
2911 } while (exception.retry);
2912 return err;
2913}
2914
2915static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2916 struct iattr *sattr)
2917{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002918 struct nfs4_createdata *data;
2919 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002921 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2922 if (data == NULL)
2923 goto out;
2924
2925 status = nfs4_do_create(dir, dentry, data);
2926
2927 nfs4_free_createdata(data);
2928out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 return status;
2930}
2931
2932static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2933 struct iattr *sattr)
2934{
2935 struct nfs4_exception exception = { };
2936 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002937
2938 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 do {
2940 err = nfs4_handle_exception(NFS_SERVER(dir),
2941 _nfs4_proc_mkdir(dir, dentry, sattr),
2942 &exception);
2943 } while (exception.retry);
2944 return err;
2945}
2946
2947static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002948 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949{
2950 struct inode *dir = dentry->d_inode;
2951 struct nfs4_readdir_arg args = {
2952 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002953 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 .pgbase = 0,
2955 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002956 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04002957 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 };
2959 struct nfs4_readdir_res res;
2960 struct rpc_message msg = {
2961 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2962 .rpc_argp = &args,
2963 .rpc_resp = &res,
2964 .rpc_cred = cred,
2965 };
2966 int status;
2967
Harvey Harrison3110ff82008-05-02 13:42:44 -07002968 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002969 dentry->d_parent->d_name.name,
2970 dentry->d_name.name,
2971 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2973 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00002974 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 -05002975 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05002977 status += args.pgbase;
2978 }
Trond Myklebustc4812992007-09-28 17:11:45 -04002979
2980 nfs_invalidate_atime(dir);
2981
Harvey Harrison3110ff82008-05-02 13:42:44 -07002982 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 return status;
2984}
2985
2986static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002987 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988{
2989 struct nfs4_exception exception = { };
2990 int err;
2991 do {
2992 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2993 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002994 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 &exception);
2996 } while (exception.retry);
2997 return err;
2998}
2999
3000static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3001 struct iattr *sattr, dev_t rdev)
3002{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003003 struct nfs4_createdata *data;
3004 int mode = sattr->ia_mode;
3005 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
3007 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3008 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003009
3010 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3011 if (data == NULL)
3012 goto out;
3013
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003015 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003017 data->arg.ftype = NF4BLK;
3018 data->arg.u.device.specdata1 = MAJOR(rdev);
3019 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 }
3021 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003022 data->arg.ftype = NF4CHR;
3023 data->arg.u.device.specdata1 = MAJOR(rdev);
3024 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003027 status = nfs4_do_create(dir, dentry, data);
3028
3029 nfs4_free_createdata(data);
3030out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 return status;
3032}
3033
3034static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3035 struct iattr *sattr, dev_t rdev)
3036{
3037 struct nfs4_exception exception = { };
3038 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003039
3040 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 do {
3042 err = nfs4_handle_exception(NFS_SERVER(dir),
3043 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3044 &exception);
3045 } while (exception.retry);
3046 return err;
3047}
3048
3049static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3050 struct nfs_fsstat *fsstat)
3051{
3052 struct nfs4_statfs_arg args = {
3053 .fh = fhandle,
3054 .bitmask = server->attr_bitmask,
3055 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003056 struct nfs4_statfs_res res = {
3057 .fsstat = fsstat,
3058 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 struct rpc_message msg = {
3060 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3061 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003062 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 };
3064
Trond Myklebust0e574af2005-10-27 22:12:38 -04003065 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003066 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067}
3068
3069static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3070{
3071 struct nfs4_exception exception = { };
3072 int err;
3073 do {
3074 err = nfs4_handle_exception(server,
3075 _nfs4_proc_statfs(server, fhandle, fsstat),
3076 &exception);
3077 } while (exception.retry);
3078 return err;
3079}
3080
3081static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3082 struct nfs_fsinfo *fsinfo)
3083{
3084 struct nfs4_fsinfo_arg args = {
3085 .fh = fhandle,
3086 .bitmask = server->attr_bitmask,
3087 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003088 struct nfs4_fsinfo_res res = {
3089 .fsinfo = fsinfo,
3090 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 struct rpc_message msg = {
3092 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3093 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003094 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 };
3096
Bryan Schumaker7c513052011-03-24 17:12:24 +00003097 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098}
3099
3100static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3101{
3102 struct nfs4_exception exception = { };
3103 int err;
3104
3105 do {
3106 err = nfs4_handle_exception(server,
3107 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3108 &exception);
3109 } while (exception.retry);
3110 return err;
3111}
3112
3113static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3114{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003115 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3117}
3118
3119static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3120 struct nfs_pathconf *pathconf)
3121{
3122 struct nfs4_pathconf_arg args = {
3123 .fh = fhandle,
3124 .bitmask = server->attr_bitmask,
3125 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003126 struct nfs4_pathconf_res res = {
3127 .pathconf = pathconf,
3128 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 struct rpc_message msg = {
3130 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3131 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003132 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 };
3134
3135 /* None of the pathconf attributes are mandatory to implement */
3136 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3137 memset(pathconf, 0, sizeof(*pathconf));
3138 return 0;
3139 }
3140
Trond Myklebust0e574af2005-10-27 22:12:38 -04003141 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003142 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143}
3144
3145static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3146 struct nfs_pathconf *pathconf)
3147{
3148 struct nfs4_exception exception = { };
3149 int err;
3150
3151 do {
3152 err = nfs4_handle_exception(server,
3153 _nfs4_proc_pathconf(server, fhandle, pathconf),
3154 &exception);
3155 } while (exception.retry);
3156 return err;
3157}
3158
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003159static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160{
Trond Myklebustec06c092006-03-20 13:44:27 -05003161 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003163 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003164 nfs_restart_rpc(task, server->nfs_client);
Trond Myklebustec06c092006-03-20 13:44:27 -05003165 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003167
3168 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003170 renew_lease(server, data->timestamp);
3171 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172}
3173
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003174static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3175{
3176
3177 dprintk("--> %s\n", __func__);
3178
3179 if (!nfs4_sequence_done(task, &data->res.seq_res))
3180 return -EAGAIN;
3181
3182 return data->read_done_cb(task, data);
3183}
3184
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003185static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003188 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003189 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190}
3191
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003192/* Reset the the nfs_read_data to send the read to the MDS. */
3193void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3194{
3195 dprintk("%s Reset task for i/o through\n", __func__);
3196 put_lseg(data->lseg);
3197 data->lseg = NULL;
3198 /* offsets will differ in the dense stripe case */
3199 data->args.offset = data->mds_offset;
3200 data->ds_clp = NULL;
3201 data->args.fh = NFS_FH(data->inode);
3202 data->read_done_cb = nfs4_read_done_cb;
3203 task->tk_ops = data->mds_ops;
3204 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3205}
3206EXPORT_SYMBOL_GPL(nfs4_reset_read);
3207
Fred Isamanb029bc92011-03-03 15:13:42 +00003208static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 struct inode *inode = data->inode;
3211
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003212 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003213 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003214 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003216 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003218 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003219 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003220 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221}
3222
Fred Isamanb029bc92011-03-03 15:13:42 +00003223static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3224{
3225 if (!nfs4_sequence_done(task, &data->res.seq_res))
3226 return -EAGAIN;
3227 return data->write_done_cb(task, data);
3228}
3229
Fred Isamana69aef12011-03-03 15:13:47 +00003230/* Reset the the nfs_write_data to send the write to the MDS. */
3231void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3232{
3233 dprintk("%s Reset task for i/o through\n", __func__);
3234 put_lseg(data->lseg);
3235 data->lseg = NULL;
3236 data->ds_clp = NULL;
3237 data->write_done_cb = nfs4_write_done_cb;
3238 data->args.fh = NFS_FH(data->inode);
3239 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3240 data->args.offset = data->mds_offset;
3241 data->res.fattr = &data->fattr;
3242 task->tk_ops = data->mds_ops;
3243 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3244}
3245EXPORT_SYMBOL_GPL(nfs4_reset_write);
3246
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003247static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003249 struct nfs_server *server = NFS_SERVER(data->inode);
3250
Fred Isaman7ffd1062011-03-03 15:13:46 +00003251 if (data->lseg) {
3252 data->args.bitmask = NULL;
3253 data->res.fattr = NULL;
3254 } else
3255 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isamanb029bc92011-03-03 15:13:42 +00003256 if (!data->write_done_cb)
3257 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003258 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 data->timestamp = jiffies;
3260
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003261 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262}
3263
Fred Isaman5f452432011-03-23 13:27:46 +00003264static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003267
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003268 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003269 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003270 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003272 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003273 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274}
3275
Fred Isaman5f452432011-03-23 13:27:46 +00003276static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3277{
3278 if (!nfs4_sequence_done(task, &data->res.seq_res))
3279 return -EAGAIN;
3280 return data->write_done_cb(task, data);
3281}
3282
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003283static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003285 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003286
3287 if (data->lseg) {
3288 data->args.bitmask = NULL;
3289 data->res.fattr = NULL;
3290 } else
3291 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isaman5f452432011-03-23 13:27:46 +00003292 if (!data->write_done_cb)
3293 data->write_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003294 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003295 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296}
3297
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003298struct nfs4_renewdata {
3299 struct nfs_client *client;
3300 unsigned long timestamp;
3301};
3302
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303/*
3304 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3305 * standalone procedure for queueing an asynchronous RENEW.
3306 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003307static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003308{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003309 struct nfs4_renewdata *data = calldata;
3310 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003311
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003312 if (atomic_read(&clp->cl_count) > 1)
3313 nfs4_schedule_state_renewal(clp);
3314 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003315 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003316}
3317
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003318static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003320 struct nfs4_renewdata *data = calldata;
3321 struct nfs_client *clp = data->client;
3322 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323
3324 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003325 /* Unless we're shutting down, schedule state recovery! */
3326 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003327 nfs4_schedule_lease_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 return;
3329 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003330 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331}
3332
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003333static const struct rpc_call_ops nfs4_renew_ops = {
3334 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003335 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003336};
3337
David Howellsadfa6f92006-08-22 20:06:08 -04003338int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339{
3340 struct rpc_message msg = {
3341 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3342 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003343 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003345 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003347 if (!atomic_inc_not_zero(&clp->cl_count))
3348 return -EIO;
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003349 data = kmalloc(sizeof(*data), GFP_KERNEL);
3350 if (data == NULL)
3351 return -ENOMEM;
3352 data->client = clp;
3353 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003355 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356}
3357
David Howellsadfa6f92006-08-22 20:06:08 -04003358int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359{
3360 struct rpc_message msg = {
3361 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3362 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003363 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 };
3365 unsigned long now = jiffies;
3366 int status;
3367
3368 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3369 if (status < 0)
3370 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003371 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 return 0;
3373}
3374
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003375static inline int nfs4_server_supports_acls(struct nfs_server *server)
3376{
3377 return (server->caps & NFS_CAP_ACLS)
3378 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3379 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3380}
3381
3382/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3383 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3384 * the stack.
3385 */
3386#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3387
3388static void buf_to_pages(const void *buf, size_t buflen,
3389 struct page **pages, unsigned int *pgbase)
3390{
3391 const void *p = buf;
3392
3393 *pgbase = offset_in_page(buf);
3394 p -= *pgbase;
3395 while (p < buf + buflen) {
3396 *(pages++) = virt_to_page(p);
3397 p += PAGE_CACHE_SIZE;
3398 }
3399}
3400
Neil Hormane9e3d722011-03-04 19:26:03 -05003401static int buf_to_pages_noslab(const void *buf, size_t buflen,
3402 struct page **pages, unsigned int *pgbase)
3403{
3404 struct page *newpage, **spages;
3405 int rc = 0;
3406 size_t len;
3407 spages = pages;
3408
3409 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003410 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003411 newpage = alloc_page(GFP_KERNEL);
3412
3413 if (newpage == NULL)
3414 goto unwind;
3415 memcpy(page_address(newpage), buf, len);
3416 buf += len;
3417 buflen -= len;
3418 *pages++ = newpage;
3419 rc++;
3420 } while (buflen != 0);
3421
3422 return rc;
3423
3424unwind:
3425 for(; rc > 0; rc--)
3426 __free_page(spages[rc-1]);
3427 return -ENOMEM;
3428}
3429
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003430struct nfs4_cached_acl {
3431 int cached;
3432 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003433 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003434};
3435
3436static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003437{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003438 struct nfs_inode *nfsi = NFS_I(inode);
3439
3440 spin_lock(&inode->i_lock);
3441 kfree(nfsi->nfs4_acl);
3442 nfsi->nfs4_acl = acl;
3443 spin_unlock(&inode->i_lock);
3444}
3445
3446static void nfs4_zap_acl_attr(struct inode *inode)
3447{
3448 nfs4_set_cached_acl(inode, NULL);
3449}
3450
3451static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3452{
3453 struct nfs_inode *nfsi = NFS_I(inode);
3454 struct nfs4_cached_acl *acl;
3455 int ret = -ENOENT;
3456
3457 spin_lock(&inode->i_lock);
3458 acl = nfsi->nfs4_acl;
3459 if (acl == NULL)
3460 goto out;
3461 if (buf == NULL) /* user is just asking for length */
3462 goto out_len;
3463 if (acl->cached == 0)
3464 goto out;
3465 ret = -ERANGE; /* see getxattr(2) man page */
3466 if (acl->len > buflen)
3467 goto out;
3468 memcpy(buf, acl->data, acl->len);
3469out_len:
3470 ret = acl->len;
3471out:
3472 spin_unlock(&inode->i_lock);
3473 return ret;
3474}
3475
3476static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3477{
3478 struct nfs4_cached_acl *acl;
3479
3480 if (buf && acl_len <= PAGE_SIZE) {
3481 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3482 if (acl == NULL)
3483 goto out;
3484 acl->cached = 1;
3485 memcpy(acl->data, buf, acl_len);
3486 } else {
3487 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3488 if (acl == NULL)
3489 goto out;
3490 acl->cached = 0;
3491 }
3492 acl->len = acl_len;
3493out:
3494 nfs4_set_cached_acl(inode, acl);
3495}
3496
Trond Myklebust16b42892006-08-24 12:27:15 -04003497static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003498{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003499 struct page *pages[NFS4ACL_MAXPAGES];
3500 struct nfs_getaclargs args = {
3501 .fh = NFS_FH(inode),
3502 .acl_pages = pages,
3503 .acl_len = buflen,
3504 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003505 struct nfs_getaclres res = {
3506 .acl_len = buflen,
3507 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003508 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003509 struct rpc_message msg = {
3510 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3511 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003512 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003513 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003514 struct page *localpage = NULL;
3515 int ret;
3516
3517 if (buflen < PAGE_SIZE) {
3518 /* As long as we're doing a round trip to the server anyway,
3519 * let's be prepared for a page of acl data. */
3520 localpage = alloc_page(GFP_KERNEL);
3521 resp_buf = page_address(localpage);
3522 if (localpage == NULL)
3523 return -ENOMEM;
3524 args.acl_pages[0] = localpage;
3525 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003526 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003527 } else {
3528 resp_buf = buf;
3529 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3530 }
Bryan Schumaker7c513052011-03-24 17:12:24 +00003531 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 +00003532 if (ret)
3533 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003534 if (res.acl_len > args.acl_len)
3535 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003536 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003537 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003538 if (buf) {
3539 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003540 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003541 goto out_free;
3542 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003543 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003544 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003545 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003546out_free:
3547 if (localpage)
3548 __free_page(localpage);
3549 return ret;
3550}
3551
Trond Myklebust16b42892006-08-24 12:27:15 -04003552static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3553{
3554 struct nfs4_exception exception = { };
3555 ssize_t ret;
3556 do {
3557 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3558 if (ret >= 0)
3559 break;
3560 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3561 } while (exception.retry);
3562 return ret;
3563}
3564
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003565static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3566{
3567 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003568 int ret;
3569
3570 if (!nfs4_server_supports_acls(server))
3571 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003572 ret = nfs_revalidate_inode(server, inode);
3573 if (ret < 0)
3574 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003575 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3576 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003577 ret = nfs4_read_cached_acl(inode, buf, buflen);
3578 if (ret != -ENOENT)
3579 return ret;
3580 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003581}
3582
Trond Myklebust16b42892006-08-24 12:27:15 -04003583static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003584{
3585 struct nfs_server *server = NFS_SERVER(inode);
3586 struct page *pages[NFS4ACL_MAXPAGES];
3587 struct nfs_setaclargs arg = {
3588 .fh = NFS_FH(inode),
3589 .acl_pages = pages,
3590 .acl_len = buflen,
3591 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003592 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003593 struct rpc_message msg = {
3594 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3595 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003596 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003597 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003598 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003599
3600 if (!nfs4_server_supports_acls(server))
3601 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003602 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3603 if (i < 0)
3604 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003605 nfs_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003606 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003607
3608 /*
3609 * Free each page after tx, so the only ref left is
3610 * held by the network stack
3611 */
3612 for (; i > 0; i--)
3613 put_page(pages[i-1]);
3614
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003615 /*
3616 * Acl update can result in inode attribute update.
3617 * so mark the attribute cache invalid.
3618 */
3619 spin_lock(&inode->i_lock);
3620 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3621 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003622 nfs_access_zap_cache(inode);
3623 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003624 return ret;
3625}
3626
Trond Myklebust16b42892006-08-24 12:27:15 -04003627static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3628{
3629 struct nfs4_exception exception = { };
3630 int err;
3631 do {
3632 err = nfs4_handle_exception(NFS_SERVER(inode),
3633 __nfs4_proc_set_acl(inode, buf, buflen),
3634 &exception);
3635 } while (exception.retry);
3636 return err;
3637}
3638
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003640nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003642 struct nfs_client *clp = server->nfs_client;
3643
3644 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 return 0;
3646 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003647 case -NFS4ERR_ADMIN_REVOKED:
3648 case -NFS4ERR_BAD_STATEID:
3649 case -NFS4ERR_OPENMODE:
3650 if (state == NULL)
3651 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003652 nfs4_schedule_stateid_recovery(server, state);
3653 goto wait_on_recovery;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003655 case -NFS4ERR_STALE_CLIENTID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 case -NFS4ERR_EXPIRED:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003657 nfs4_schedule_lease_recovery(clp);
3658 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003659#if defined(CONFIG_NFS_V4_1)
3660 case -NFS4ERR_BADSESSION:
3661 case -NFS4ERR_BADSLOT:
3662 case -NFS4ERR_BAD_HIGH_SLOT:
3663 case -NFS4ERR_DEADSESSION:
3664 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3665 case -NFS4ERR_SEQ_FALSE_RETRY:
3666 case -NFS4ERR_SEQ_MISORDERED:
3667 dprintk("%s ERROR %d, Reset session\n", __func__,
3668 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003669 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -04003670 task->tk_status = 0;
3671 return -EAGAIN;
3672#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003674 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003675 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003676 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3678 task->tk_status = 0;
3679 return -EAGAIN;
3680 case -NFS4ERR_OLD_STATEID:
3681 task->tk_status = 0;
3682 return -EAGAIN;
3683 }
3684 task->tk_status = nfs4_map_errors(task->tk_status);
3685 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003686wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003687 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003688 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3689 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3690 task->tk_status = 0;
3691 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692}
3693
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003694int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3695 unsigned short port, struct rpc_cred *cred,
3696 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697{
3698 nfs4_verifier sc_verifier;
3699 struct nfs4_setclientid setclientid = {
3700 .sc_verifier = &sc_verifier,
3701 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003702 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 };
3704 struct rpc_message msg = {
3705 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3706 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003707 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003708 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 };
Al Virobc4785c2006-10-19 23:28:51 -07003710 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 int loop = 0;
3712 int status;
3713
Al Virobc4785c2006-10-19 23:28:51 -07003714 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3716 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3717
3718 for(;;) {
3719 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003720 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003721 clp->cl_ipaddr,
3722 rpc_peeraddr2str(clp->cl_rpcclient,
3723 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003724 rpc_peeraddr2str(clp->cl_rpcclient,
3725 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003726 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 clp->cl_id_uniquifier);
3728 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003729 sizeof(setclientid.sc_netid),
3730 rpc_peeraddr2str(clp->cl_rpcclient,
3731 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003733 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 clp->cl_ipaddr, port >> 8, port & 255);
3735
3736 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3737 if (status != -NFS4ERR_CLID_INUSE)
3738 break;
3739 if (signalled())
3740 break;
3741 if (loop++ & 1)
J. Bruce Fields611c96c2010-12-13 19:05:46 -05003742 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 else
3744 if (++clp->cl_id_uniquifier == 0)
3745 break;
3746 }
3747 return status;
3748}
3749
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003750static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3751 struct nfs4_setclientid_res *arg,
3752 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753{
3754 struct nfs_fsinfo fsinfo;
3755 struct rpc_message msg = {
3756 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003757 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003759 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 };
3761 unsigned long now;
3762 int status;
3763
3764 now = jiffies;
3765 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3766 if (status == 0) {
3767 spin_lock(&clp->cl_lock);
3768 clp->cl_lease_time = fsinfo.lease_time * HZ;
3769 clp->cl_last_renewal = now;
3770 spin_unlock(&clp->cl_lock);
3771 }
3772 return status;
3773}
3774
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003775int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3776 struct nfs4_setclientid_res *arg,
3777 struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003778{
Benny Halevy77e03672008-06-24 20:25:57 +03003779 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003780 int err;
3781 do {
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003782 err = _nfs4_proc_setclientid_confirm(clp, arg, cred);
Trond Myklebust51581f32006-03-20 13:44:47 -05003783 switch (err) {
3784 case 0:
3785 return err;
3786 case -NFS4ERR_RESOURCE:
3787 /* The IBM lawyers misread another document! */
3788 case -NFS4ERR_DELAY:
3789 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3790 }
3791 } while (err == 0);
3792 return err;
3793}
3794
Trond Myklebustfe650402006-01-03 09:55:18 +01003795struct nfs4_delegreturndata {
3796 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003797 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003798 struct nfs_fh fh;
3799 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003800 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003801 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003802 int rpc_status;
3803};
3804
Trond Myklebustfe650402006-01-03 09:55:18 +01003805static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3806{
3807 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003808
Trond Myklebust14516c32010-07-31 14:29:06 -04003809 if (!nfs4_sequence_done(task, &data->res.seq_res))
3810 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003811
Ricardo Labiaga79708862009-12-07 09:23:21 -05003812 switch (task->tk_status) {
3813 case -NFS4ERR_STALE_STATEID:
3814 case -NFS4ERR_EXPIRED:
3815 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003816 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003817 break;
3818 default:
3819 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3820 -EAGAIN) {
3821 nfs_restart_rpc(task, data->res.server->nfs_client);
3822 return;
3823 }
3824 }
3825 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003826}
3827
3828static void nfs4_delegreturn_release(void *calldata)
3829{
Trond Myklebustfe650402006-01-03 09:55:18 +01003830 kfree(calldata);
3831}
3832
Andy Adamson938e1012009-04-01 09:22:28 -04003833#if defined(CONFIG_NFS_V4_1)
3834static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3835{
3836 struct nfs4_delegreturndata *d_data;
3837
3838 d_data = (struct nfs4_delegreturndata *)data;
3839
Trond Myklebust035168a2010-06-16 09:52:26 -04003840 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003841 &d_data->args.seq_args,
3842 &d_data->res.seq_res, 1, task))
3843 return;
3844 rpc_call_start(task);
3845}
3846#endif /* CONFIG_NFS_V4_1 */
3847
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003848static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003849#if defined(CONFIG_NFS_V4_1)
3850 .rpc_call_prepare = nfs4_delegreturn_prepare,
3851#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003852 .rpc_call_done = nfs4_delegreturn_done,
3853 .rpc_release = nfs4_delegreturn_release,
3854};
3855
Trond Myklebuste6f81072008-01-24 18:14:34 -05003856static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003857{
3858 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003859 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003860 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003861 struct rpc_message msg = {
3862 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3863 .rpc_cred = cred,
3864 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003865 struct rpc_task_setup task_setup_data = {
3866 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003867 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003868 .callback_ops = &nfs4_delegreturn_ops,
3869 .flags = RPC_TASK_ASYNC,
3870 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003871 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003872
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003873 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003874 if (data == NULL)
3875 return -ENOMEM;
3876 data->args.fhandle = &data->fh;
3877 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003878 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003879 nfs_copy_fh(&data->fh, NFS_FH(inode));
3880 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003881 data->res.fattr = &data->fattr;
3882 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003883 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003884 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003885 data->rpc_status = 0;
3886
Trond Myklebustc970aa82007-07-14 15:39:59 -04003887 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05003888 msg.rpc_argp = &data->args;
3889 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003890 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003891 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003892 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003893 if (!issync)
3894 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003895 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003896 if (status != 0)
3897 goto out;
3898 status = data->rpc_status;
3899 if (status != 0)
3900 goto out;
3901 nfs_refresh_inode(inode, &data->fattr);
3902out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003903 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003904 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905}
3906
Trond Myklebuste6f81072008-01-24 18:14:34 -05003907int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908{
3909 struct nfs_server *server = NFS_SERVER(inode);
3910 struct nfs4_exception exception = { };
3911 int err;
3912 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003913 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 switch (err) {
3915 case -NFS4ERR_STALE_STATEID:
3916 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 case 0:
3918 return 0;
3919 }
3920 err = nfs4_handle_exception(server, err, &exception);
3921 } while (exception.retry);
3922 return err;
3923}
3924
3925#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3926#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3927
3928/*
3929 * sleep, with exponential backoff, and retry the LOCK operation.
3930 */
3931static unsigned long
3932nfs4_set_lock_task_retry(unsigned long timeout)
3933{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003934 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 timeout <<= 1;
3936 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3937 return NFS4_LOCK_MAXTIMEOUT;
3938 return timeout;
3939}
3940
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3942{
3943 struct inode *inode = state->inode;
3944 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003945 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003946 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003948 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003950 struct nfs_lockt_res res = {
3951 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 };
3953 struct rpc_message msg = {
3954 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3955 .rpc_argp = &arg,
3956 .rpc_resp = &res,
3957 .rpc_cred = state->owner->so_cred,
3958 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 struct nfs4_lock_state *lsp;
3960 int status;
3961
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003962 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003963 status = nfs4_set_lock_state(state, request);
3964 if (status != 0)
3965 goto out;
3966 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003967 arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05003968 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003969 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003970 switch (status) {
3971 case 0:
3972 request->fl_type = F_UNLCK;
3973 break;
3974 case -NFS4ERR_DENIED:
3975 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003977 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003978out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 return status;
3980}
3981
3982static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3983{
3984 struct nfs4_exception exception = { };
3985 int err;
3986
3987 do {
3988 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3989 _nfs4_proc_getlk(state, cmd, request),
3990 &exception);
3991 } while (exception.retry);
3992 return err;
3993}
3994
3995static int do_vfs_lock(struct file *file, struct file_lock *fl)
3996{
3997 int res = 0;
3998 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3999 case FL_POSIX:
4000 res = posix_lock_file_wait(file, fl);
4001 break;
4002 case FL_FLOCK:
4003 res = flock_lock_file_wait(file, fl);
4004 break;
4005 default:
4006 BUG();
4007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 return res;
4009}
4010
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004011struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004012 struct nfs_locku_args arg;
4013 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004014 struct nfs4_lock_state *lsp;
4015 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004016 struct file_lock fl;
4017 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004018 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004019};
4020
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004021static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4022 struct nfs_open_context *ctx,
4023 struct nfs4_lock_state *lsp,
4024 struct nfs_seqid *seqid)
4025{
4026 struct nfs4_unlockdata *p;
4027 struct inode *inode = lsp->ls_state->inode;
4028
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004029 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004030 if (p == NULL)
4031 return NULL;
4032 p->arg.fh = NFS_FH(inode);
4033 p->arg.fl = &p->fl;
4034 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004035 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004036 p->arg.stateid = &lsp->ls_stateid;
4037 p->lsp = lsp;
4038 atomic_inc(&lsp->ls_count);
4039 /* Ensure we don't close file until we're done freeing locks! */
4040 p->ctx = get_nfs_open_context(ctx);
4041 memcpy(&p->fl, fl, sizeof(p->fl));
4042 p->server = NFS_SERVER(inode);
4043 return p;
4044}
4045
Trond Myklebust06f814a2006-01-03 09:55:07 +01004046static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004047{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004048 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004049 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004050 nfs4_put_lock_state(calldata->lsp);
4051 put_nfs_open_context(calldata->ctx);
4052 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004053}
4054
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004055static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004056{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004057 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004058
Trond Myklebust14516c32010-07-31 14:29:06 -04004059 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4060 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004061 switch (task->tk_status) {
4062 case 0:
4063 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004064 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004065 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01004066 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004067 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004068 case -NFS4ERR_BAD_STATEID:
4069 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004070 case -NFS4ERR_STALE_STATEID:
4071 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004072 break;
4073 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004074 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebust0110ee12009-12-07 09:00:24 -05004075 nfs_restart_rpc(task,
Trond Myklebustd61e6122009-12-05 19:32:19 -05004076 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004077 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004078}
4079
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004080static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004081{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004082 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004084 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004085 return;
4086 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004087 /* Note: exit _without_ running nfs4_locku_done */
4088 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004089 return;
4090 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004091 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004092 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004093 &calldata->arg.seq_args,
4094 &calldata->res.seq_res, 1, task))
4095 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004096 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097}
4098
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004099static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004100 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004101 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004102 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004103};
4104
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004105static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4106 struct nfs_open_context *ctx,
4107 struct nfs4_lock_state *lsp,
4108 struct nfs_seqid *seqid)
4109{
4110 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004111 struct rpc_message msg = {
4112 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4113 .rpc_cred = ctx->cred,
4114 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004115 struct rpc_task_setup task_setup_data = {
4116 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004117 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004118 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004119 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004120 .flags = RPC_TASK_ASYNC,
4121 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004122
Frank Filz137d6ac2007-07-09 15:32:29 -07004123 /* Ensure this is an unlock - when canceling a lock, the
4124 * canceled lock is passed in, and it won't be an unlock.
4125 */
4126 fl->fl_type = F_UNLCK;
4127
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004128 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4129 if (data == NULL) {
4130 nfs_free_seqid(seqid);
4131 return ERR_PTR(-ENOMEM);
4132 }
4133
Trond Myklebust1174dd12010-12-21 10:52:24 -05004134 msg.rpc_argp = &data->arg;
4135 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004136 task_setup_data.callback_data = data;
4137 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004138}
4139
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4141{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004142 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004143 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004144 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004145 struct rpc_task *task;
4146 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004147 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148
Trond Myklebust9b073572006-06-29 16:38:34 -04004149 status = nfs4_set_lock_state(state, request);
4150 /* Unlock _before_ we do the RPC call */
4151 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004152 down_read(&nfsi->rwsem);
4153 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4154 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004155 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004156 }
4157 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004158 if (status != 0)
4159 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004160 /* Is this a delegated lock? */
4161 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004162 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004163 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004164 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004165 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004166 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004167 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004168 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004169 status = PTR_ERR(task);
4170 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004171 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004172 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004173 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004174out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004175 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004176 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177}
4178
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004179struct nfs4_lockdata {
4180 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004181 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004182 struct nfs4_lock_state *lsp;
4183 struct nfs_open_context *ctx;
4184 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004185 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004186 int rpc_status;
4187 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004188 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004189};
4190
4191static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004192 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4193 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004194{
4195 struct nfs4_lockdata *p;
4196 struct inode *inode = lsp->ls_state->inode;
4197 struct nfs_server *server = NFS_SERVER(inode);
4198
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004199 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004200 if (p == NULL)
4201 return NULL;
4202
4203 p->arg.fh = NFS_FH(inode);
4204 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004205 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004206 if (p->arg.open_seqid == NULL)
4207 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004208 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004209 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004210 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004211 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004212 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004213 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004214 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004215 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004216 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004217 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004218 atomic_inc(&lsp->ls_count);
4219 p->ctx = get_nfs_open_context(ctx);
4220 memcpy(&p->fl, fl, sizeof(p->fl));
4221 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004222out_free_seqid:
4223 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004224out_free:
4225 kfree(p);
4226 return NULL;
4227}
4228
4229static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4230{
4231 struct nfs4_lockdata *data = calldata;
4232 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233
Harvey Harrison3110ff82008-05-02 13:42:44 -07004234 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004235 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4236 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004237 /* Do we need to do an open_to_lock_owner? */
4238 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004239 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4240 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004241 data->arg.open_stateid = &state->stateid;
4242 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004243 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004244 } else
4245 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004246 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004247 if (nfs4_setup_sequence(data->server,
4248 &data->arg.seq_args,
Andy Adamson66179ef2009-04-01 09:22:22 -04004249 &data->res.seq_res, 1, task))
4250 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004251 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004252 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004253}
4254
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004255static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4256{
4257 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4258 nfs4_lock_prepare(task, calldata);
4259}
4260
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004261static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4262{
4263 struct nfs4_lockdata *data = calldata;
4264
Harvey Harrison3110ff82008-05-02 13:42:44 -07004265 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004266
Trond Myklebust14516c32010-07-31 14:29:06 -04004267 if (!nfs4_sequence_done(task, &data->res.seq_res))
4268 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004269
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004270 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004271 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004272 if (data->rpc_status == 0)
4273 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4274 else
4275 goto out;
4276 }
4277 if (data->rpc_status == 0) {
4278 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4279 sizeof(data->lsp->ls_stateid.data));
4280 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04004281 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004282 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004283out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004284 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004285}
4286
4287static void nfs4_lock_release(void *calldata)
4288{
4289 struct nfs4_lockdata *data = calldata;
4290
Harvey Harrison3110ff82008-05-02 13:42:44 -07004291 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004292 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004293 if (data->cancelled != 0) {
4294 struct rpc_task *task;
4295 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4296 data->arg.lock_seqid);
4297 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004298 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004299 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004300 } else
4301 nfs_free_seqid(data->arg.lock_seqid);
4302 nfs4_put_lock_state(data->lsp);
4303 put_nfs_open_context(data->ctx);
4304 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004305 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004306}
4307
4308static const struct rpc_call_ops nfs4_lock_ops = {
4309 .rpc_call_prepare = nfs4_lock_prepare,
4310 .rpc_call_done = nfs4_lock_done,
4311 .rpc_release = nfs4_lock_release,
4312};
4313
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004314static const struct rpc_call_ops nfs4_recover_lock_ops = {
4315 .rpc_call_prepare = nfs4_recover_lock_prepare,
4316 .rpc_call_done = nfs4_lock_done,
4317 .rpc_release = nfs4_lock_release,
4318};
4319
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004320static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4321{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004322 switch (error) {
4323 case -NFS4ERR_ADMIN_REVOKED:
4324 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004325 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004326 if (new_lock_owner != 0 ||
4327 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004328 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004329 break;
4330 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004331 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004332 case -NFS4ERR_EXPIRED:
4333 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004334 };
4335}
4336
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004337static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004338{
4339 struct nfs4_lockdata *data;
4340 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004341 struct rpc_message msg = {
4342 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4343 .rpc_cred = state->owner->so_cred,
4344 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004345 struct rpc_task_setup task_setup_data = {
4346 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004347 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004348 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004349 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004350 .flags = RPC_TASK_ASYNC,
4351 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004352 int ret;
4353
Harvey Harrison3110ff82008-05-02 13:42:44 -07004354 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004355 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004356 fl->fl_u.nfs4_fl.owner,
4357 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004358 if (data == NULL)
4359 return -ENOMEM;
4360 if (IS_SETLKW(cmd))
4361 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004362 if (recovery_type > NFS_LOCK_NEW) {
4363 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004364 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004365 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4366 }
Trond Myklebust1174dd12010-12-21 10:52:24 -05004367 msg.rpc_argp = &data->arg;
4368 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004369 task_setup_data.callback_data = data;
4370 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004371 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004372 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004373 ret = nfs4_wait_for_completion_rpc_task(task);
4374 if (ret == 0) {
4375 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004376 if (ret)
4377 nfs4_handle_setlk_error(data->server, data->lsp,
4378 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004379 } else
4380 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004381 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004382 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004383 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384}
4385
4386static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4387{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004388 struct nfs_server *server = NFS_SERVER(state->inode);
4389 struct nfs4_exception exception = { };
4390 int err;
4391
4392 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004393 /* Cache the lock if possible... */
4394 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4395 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004396 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004397 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004398 break;
4399 nfs4_handle_exception(server, err, &exception);
4400 } while (exception.retry);
4401 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402}
4403
4404static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4405{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004406 struct nfs_server *server = NFS_SERVER(state->inode);
4407 struct nfs4_exception exception = { };
4408 int err;
4409
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004410 err = nfs4_set_lock_state(state, request);
4411 if (err != 0)
4412 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004413 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004414 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4415 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004416 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004417 switch (err) {
4418 default:
4419 goto out;
4420 case -NFS4ERR_GRACE:
4421 case -NFS4ERR_DELAY:
4422 nfs4_handle_exception(server, err, &exception);
4423 err = 0;
4424 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004425 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004426out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004427 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428}
4429
4430static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4431{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004432 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004433 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004434 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004436 if ((fl_flags & FL_POSIX) &&
4437 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4438 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004439 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004440 status = nfs4_set_lock_state(state, request);
4441 if (status != 0)
4442 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004443 request->fl_flags |= FL_ACCESS;
4444 status = do_vfs_lock(request->fl_file, request);
4445 if (status < 0)
4446 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004447 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004448 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004449 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004450 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004451 request->fl_flags = fl_flags & ~FL_SLEEP;
4452 status = do_vfs_lock(request->fl_file, request);
4453 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004454 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004455 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004456 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004457 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004458 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004459 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004460 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004461 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004462out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004463 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004464out:
4465 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 return status;
4467}
4468
4469static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4470{
4471 struct nfs4_exception exception = { };
4472 int err;
4473
4474 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004475 err = _nfs4_proc_setlk(state, cmd, request);
4476 if (err == -NFS4ERR_DENIED)
4477 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004479 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 } while (exception.retry);
4481 return err;
4482}
4483
4484static int
4485nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4486{
4487 struct nfs_open_context *ctx;
4488 struct nfs4_state *state;
4489 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4490 int status;
4491
4492 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004493 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 state = ctx->state;
4495
4496 if (request->fl_start < 0 || request->fl_end < 0)
4497 return -EINVAL;
4498
Trond Myklebustd9531262009-07-21 19:22:38 -04004499 if (IS_GETLK(cmd)) {
4500 if (state != NULL)
4501 return nfs4_proc_getlk(state, F_GETLK, request);
4502 return 0;
4503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504
4505 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4506 return -EINVAL;
4507
Trond Myklebustd9531262009-07-21 19:22:38 -04004508 if (request->fl_type == F_UNLCK) {
4509 if (state != NULL)
4510 return nfs4_proc_unlck(state, cmd, request);
4511 return 0;
4512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513
Trond Myklebustd9531262009-07-21 19:22:38 -04004514 if (state == NULL)
4515 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 do {
4517 status = nfs4_proc_setlk(state, cmd, request);
4518 if ((status != -EAGAIN) || IS_SETLK(cmd))
4519 break;
4520 timeout = nfs4_set_lock_task_retry(timeout);
4521 status = -ERESTARTSYS;
4522 if (signalled())
4523 break;
4524 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 return status;
4526}
4527
Trond Myklebust888e6942005-11-04 15:38:11 -05004528int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4529{
4530 struct nfs_server *server = NFS_SERVER(state->inode);
4531 struct nfs4_exception exception = { };
4532 int err;
4533
4534 err = nfs4_set_lock_state(state, fl);
4535 if (err != 0)
4536 goto out;
4537 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004538 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004539 switch (err) {
4540 default:
4541 printk(KERN_ERR "%s: unhandled error %d.\n",
4542 __func__, err);
4543 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004544 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004545 goto out;
4546 case -NFS4ERR_EXPIRED:
4547 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004548 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004549 nfs4_schedule_lease_recovery(server->nfs_client);
4550 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004551 case -NFS4ERR_BADSESSION:
4552 case -NFS4ERR_BADSLOT:
4553 case -NFS4ERR_BAD_HIGH_SLOT:
4554 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4555 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004556 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004557 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004558 case -ERESTARTSYS:
4559 /*
4560 * The show must go on: exit, but mark the
4561 * stateid as needing recovery.
4562 */
4563 case -NFS4ERR_ADMIN_REVOKED:
4564 case -NFS4ERR_BAD_STATEID:
4565 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004566 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004567 err = 0;
4568 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004569 case -EKEYEXPIRED:
4570 /*
4571 * User RPCSEC_GSS context has expired.
4572 * We cannot recover this stateid now, so
4573 * skip it and allow recovery thread to
4574 * proceed.
4575 */
4576 err = 0;
4577 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004578 case -ENOMEM:
4579 case -NFS4ERR_DENIED:
4580 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4581 err = 0;
4582 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004583 case -NFS4ERR_DELAY:
4584 break;
4585 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004586 err = nfs4_handle_exception(server, err, &exception);
4587 } while (exception.retry);
4588out:
4589 return err;
4590}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004591
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004592static void nfs4_release_lockowner_release(void *calldata)
4593{
4594 kfree(calldata);
4595}
4596
4597const struct rpc_call_ops nfs4_release_lockowner_ops = {
4598 .rpc_release = nfs4_release_lockowner_release,
4599};
4600
4601void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4602{
4603 struct nfs_server *server = lsp->ls_state->owner->so_server;
4604 struct nfs_release_lockowner_args *args;
4605 struct rpc_message msg = {
4606 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4607 };
4608
4609 if (server->nfs_client->cl_mvops->minor_version != 0)
4610 return;
4611 args = kmalloc(sizeof(*args), GFP_NOFS);
4612 if (!args)
4613 return;
4614 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4615 args->lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004616 args->lock_owner.s_dev = server->s_dev;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004617 msg.rpc_argp = args;
4618 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4619}
4620
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004621#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4622
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004623static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4624 const void *buf, size_t buflen,
4625 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004626{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004627 if (strcmp(key, "") != 0)
4628 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004629
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004630 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004631}
4632
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004633static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4634 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004635{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004636 if (strcmp(key, "") != 0)
4637 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004638
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004639 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004640}
4641
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004642static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4643 size_t list_len, const char *name,
4644 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004645{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004646 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004647
J. Bruce Fields096455a2006-03-20 23:23:42 -05004648 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4649 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004650
4651 if (list && len <= list_len)
4652 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004653 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004654}
4655
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004656static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4657{
4658 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4659 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4660 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4661 return;
4662
4663 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4664 NFS_ATTR_FATTR_NLINK;
4665 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4666 fattr->nlink = 2;
4667}
4668
Trond Myklebust56659e92007-07-17 21:52:39 -04004669int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004670 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004671{
4672 struct nfs_server *server = NFS_SERVER(dir);
4673 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004674 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4675 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004676 };
4677 struct nfs4_fs_locations_arg args = {
4678 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004679 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004680 .page = page,
4681 .bitmask = bitmask,
4682 };
Benny Halevy22958462009-04-01 09:22:02 -04004683 struct nfs4_fs_locations_res res = {
4684 .fs_locations = fs_locations,
4685 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004686 struct rpc_message msg = {
4687 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4688 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004689 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004690 };
4691 int status;
4692
Harvey Harrison3110ff82008-05-02 13:42:44 -07004693 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004694 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004695 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004696 fs_locations->nlocations = 0;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004697 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004698 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004699 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004700 return status;
4701}
4702
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00004703static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4704{
4705 int status;
4706 struct nfs4_secinfo_arg args = {
4707 .dir_fh = NFS_FH(dir),
4708 .name = name,
4709 };
4710 struct nfs4_secinfo_res res = {
4711 .flavors = flavors,
4712 };
4713 struct rpc_message msg = {
4714 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
4715 .rpc_argp = &args,
4716 .rpc_resp = &res,
4717 };
4718
4719 dprintk("NFS call secinfo %s\n", name->name);
4720 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4721 dprintk("NFS reply secinfo: %d\n", status);
4722 return status;
4723}
4724
4725int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4726{
4727 struct nfs4_exception exception = { };
4728 int err;
4729 do {
4730 err = nfs4_handle_exception(NFS_SERVER(dir),
4731 _nfs4_proc_secinfo(dir, name, flavors),
4732 &exception);
4733 } while (exception.retry);
4734 return err;
4735}
4736
Andy Adamson557134a2009-04-01 09:21:53 -04004737#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004738/*
Andy Adamson357f54d2010-12-14 10:11:57 -05004739 * Check the exchange flags returned by the server for invalid flags, having
4740 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4741 * DS flags set.
4742 */
4743static int nfs4_check_cl_exchange_flags(u32 flags)
4744{
4745 if (flags & ~EXCHGID4_FLAG_MASK_R)
4746 goto out_inval;
4747 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4748 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4749 goto out_inval;
4750 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4751 goto out_inval;
4752 return NFS_OK;
4753out_inval:
4754 return -NFS4ERR_INVAL;
4755}
4756
4757/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04004758 * nfs4_proc_exchange_id()
4759 *
4760 * Since the clientid has expired, all compounds using sessions
4761 * associated with the stale clientid will be returning
4762 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4763 * be in some phase of session reset.
4764 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004765int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004766{
4767 nfs4_verifier verifier;
4768 struct nfs41_exchange_id_args args = {
4769 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05004770 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04004771 };
4772 struct nfs41_exchange_id_res res = {
4773 .client = clp,
4774 };
4775 int status;
4776 struct rpc_message msg = {
4777 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4778 .rpc_argp = &args,
4779 .rpc_resp = &res,
4780 .rpc_cred = cred,
4781 };
4782 __be32 *p;
4783
4784 dprintk("--> %s\n", __func__);
4785 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004786
Benny Halevy99fe60d2009-04-01 09:22:29 -04004787 p = (u32 *)verifier.data;
4788 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4789 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4790 args.verifier = &verifier;
4791
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004792 args.id_len = scnprintf(args.id, sizeof(args.id),
4793 "%s/%s.%s/%u",
4794 clp->cl_ipaddr,
4795 init_utsname()->nodename,
4796 init_utsname()->domainname,
4797 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004798
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004799 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4800 if (!status)
4801 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004802 dprintk("<-- %s status= %d\n", __func__, status);
4803 return status;
4804}
4805
Andy Adamson2050f0c2009-04-01 09:22:30 -04004806struct nfs4_get_lease_time_data {
4807 struct nfs4_get_lease_time_args *args;
4808 struct nfs4_get_lease_time_res *res;
4809 struct nfs_client *clp;
4810};
4811
4812static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4813 void *calldata)
4814{
4815 int ret;
4816 struct nfs4_get_lease_time_data *data =
4817 (struct nfs4_get_lease_time_data *)calldata;
4818
4819 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004820 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004821 /* just setup sequence, do not trigger session recovery
4822 since we're invoked within one */
4823 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004824 &data->args->la_seq_args,
4825 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004826
4827 BUG_ON(ret == -EAGAIN);
4828 rpc_call_start(task);
4829 dprintk("<-- %s\n", __func__);
4830}
4831
4832/*
4833 * Called from nfs4_state_manager thread for session setup, so don't recover
4834 * from sequence operation or clientid errors.
4835 */
4836static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4837{
4838 struct nfs4_get_lease_time_data *data =
4839 (struct nfs4_get_lease_time_data *)calldata;
4840
4841 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04004842 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4843 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04004844 switch (task->tk_status) {
4845 case -NFS4ERR_DELAY:
4846 case -NFS4ERR_GRACE:
4847 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4848 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4849 task->tk_status = 0;
Trond Myklebust0110ee12009-12-07 09:00:24 -05004850 nfs_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004851 return;
4852 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004853 dprintk("<-- %s\n", __func__);
4854}
4855
4856struct rpc_call_ops nfs4_get_lease_time_ops = {
4857 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4858 .rpc_call_done = nfs4_get_lease_time_done,
4859};
4860
4861int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4862{
4863 struct rpc_task *task;
4864 struct nfs4_get_lease_time_args args;
4865 struct nfs4_get_lease_time_res res = {
4866 .lr_fsinfo = fsinfo,
4867 };
4868 struct nfs4_get_lease_time_data data = {
4869 .args = &args,
4870 .res = &res,
4871 .clp = clp,
4872 };
4873 struct rpc_message msg = {
4874 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4875 .rpc_argp = &args,
4876 .rpc_resp = &res,
4877 };
4878 struct rpc_task_setup task_setup = {
4879 .rpc_client = clp->cl_rpcclient,
4880 .rpc_message = &msg,
4881 .callback_ops = &nfs4_get_lease_time_ops,
4882 .callback_data = &data
4883 };
4884 int status;
4885
Andy Adamson2050f0c2009-04-01 09:22:30 -04004886 dprintk("--> %s\n", __func__);
4887 task = rpc_run_task(&task_setup);
4888
4889 if (IS_ERR(task))
4890 status = PTR_ERR(task);
4891 else {
4892 status = task->tk_status;
4893 rpc_put_task(task);
4894 }
4895 dprintk("<-- %s return %d\n", __func__, status);
4896
4897 return status;
4898}
4899
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004900/*
4901 * Reset a slot table
4902 */
Andy Adamson104aeba2010-01-14 17:45:10 -05004903static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4904 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004905{
Andy Adamson104aeba2010-01-14 17:45:10 -05004906 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004907 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004908 int ret = 0;
4909
Andy Adamson104aeba2010-01-14 17:45:10 -05004910 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4911 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004912
Andy Adamson104aeba2010-01-14 17:45:10 -05004913 /* Does the newly negotiated max_reqs match the existing slot table? */
4914 if (max_reqs != tbl->max_slots) {
4915 ret = -ENOMEM;
4916 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004917 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05004918 if (!new)
4919 goto out;
4920 ret = 0;
4921 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004922 }
4923 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05004924 if (new) {
4925 tbl->slots = new;
4926 tbl->max_slots = max_reqs;
4927 }
4928 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004929 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004930 spin_unlock(&tbl->slot_tbl_lock);
4931 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4932 tbl, tbl->slots, tbl->max_slots);
4933out:
4934 dprintk("<-- %s: return %d\n", __func__, ret);
4935 return ret;
4936}
4937
Andy Adamsonfc931582009-04-01 09:22:31 -04004938/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004939 * Reset the forechannel and backchannel slot tables
4940 */
4941static int nfs4_reset_slot_tables(struct nfs4_session *session)
4942{
4943 int status;
4944
4945 status = nfs4_reset_slot_table(&session->fc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004946 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004947 if (status)
4948 return status;
4949
4950 status = nfs4_reset_slot_table(&session->bc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004951 session->bc_attrs.max_reqs, 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004952 return status;
4953}
4954
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004955/* Destroy the slot table */
4956static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4957{
4958 if (session->fc_slot_table.slots != NULL) {
4959 kfree(session->fc_slot_table.slots);
4960 session->fc_slot_table.slots = NULL;
4961 }
4962 if (session->bc_slot_table.slots != NULL) {
4963 kfree(session->bc_slot_table.slots);
4964 session->bc_slot_table.slots = NULL;
4965 }
4966 return;
4967}
4968
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004969/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004970 * Initialize slot table
4971 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004972static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4973 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04004974{
Andy Adamsonfc931582009-04-01 09:22:31 -04004975 struct nfs4_slot *slot;
4976 int ret = -ENOMEM;
4977
4978 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4979
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004980 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04004981
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004982 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04004983 if (!slot)
4984 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04004985 ret = 0;
4986
4987 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04004988 tbl->max_slots = max_slots;
4989 tbl->slots = slot;
4990 tbl->highest_used_slotid = -1; /* no slot is currently used */
4991 spin_unlock(&tbl->slot_tbl_lock);
4992 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4993 tbl, tbl->slots, tbl->max_slots);
4994out:
4995 dprintk("<-- %s: return %d\n", __func__, ret);
4996 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04004997}
4998
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004999/*
5000 * Initialize the forechannel and backchannel tables
5001 */
5002static int nfs4_init_slot_tables(struct nfs4_session *session)
5003{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005004 struct nfs4_slot_table *tbl;
5005 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005006
Trond Myklebustf26468f2009-12-05 19:32:11 -05005007 tbl = &session->fc_slot_table;
5008 if (tbl->slots == NULL) {
5009 status = nfs4_init_slot_table(tbl,
5010 session->fc_attrs.max_reqs, 1);
5011 if (status)
5012 return status;
5013 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005014
Trond Myklebustf26468f2009-12-05 19:32:11 -05005015 tbl = &session->bc_slot_table;
5016 if (tbl->slots == NULL) {
5017 status = nfs4_init_slot_table(tbl,
5018 session->bc_attrs.max_reqs, 0);
5019 if (status)
5020 nfs4_destroy_slot_tables(session);
5021 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005022
5023 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005024}
5025
5026struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5027{
5028 struct nfs4_session *session;
5029 struct nfs4_slot_table *tbl;
5030
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005031 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005032 if (!session)
5033 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005034
Andy Adamson557134a2009-04-01 09:21:53 -04005035 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05005036 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04005037 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005038 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005039 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005040
5041 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05005042 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005043 spin_lock_init(&tbl->slot_tbl_lock);
5044 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005045 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005046
Trond Myklebust1055d762010-06-16 09:52:27 -04005047 session->session_state = 1<<NFS4_SESSION_INITING;
5048
Andy Adamson557134a2009-04-01 09:21:53 -04005049 session->clp = clp;
5050 return session;
5051}
5052
5053void nfs4_destroy_session(struct nfs4_session *session)
5054{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005055 nfs4_proc_destroy_session(session);
5056 dprintk("%s Destroy backchannel for xprt %p\n",
5057 __func__, session->clp->cl_rpcclient->cl_xprt);
5058 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
5059 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005060 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005061 kfree(session);
5062}
5063
Andy Adamsonfc931582009-04-01 09:22:31 -04005064/*
5065 * Initialize the values to be used by the client in CREATE_SESSION
5066 * If nfs4_init_session set the fore channel request and response sizes,
5067 * use them.
5068 *
5069 * Set the back channel max_resp_sz_cached to zero to force the client to
5070 * always set csa_cachethis to FALSE because the current implementation
5071 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5072 */
5073static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5074{
5075 struct nfs4_session *session = args->client->cl_session;
5076 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5077 mxresp_sz = session->fc_attrs.max_resp_sz;
5078
5079 if (mxrqst_sz == 0)
5080 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5081 if (mxresp_sz == 0)
5082 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5083 /* Fore channel attributes */
5084 args->fc_attrs.headerpadsz = 0;
5085 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5086 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005087 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5088 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
5089
5090 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005091 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005092 __func__,
5093 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005094 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005095
5096 /* Back channel attributes */
5097 args->bc_attrs.headerpadsz = 0;
5098 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5099 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5100 args->bc_attrs.max_resp_sz_cached = 0;
5101 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5102 args->bc_attrs.max_reqs = 1;
5103
5104 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5105 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5106 __func__,
5107 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5108 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5109 args->bc_attrs.max_reqs);
5110}
5111
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005112static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005113{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005114 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5115 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5116
5117 if (rcvd->headerpadsz > sent->headerpadsz)
5118 return -EINVAL;
5119 if (rcvd->max_resp_sz > sent->max_resp_sz)
5120 return -EINVAL;
5121 /*
5122 * Our requested max_ops is the minimum we need; we're not
5123 * prepared to break up compounds into smaller pieces than that.
5124 * So, no point even trying to continue if the server won't
5125 * cooperate:
5126 */
5127 if (rcvd->max_ops < sent->max_ops)
5128 return -EINVAL;
5129 if (rcvd->max_reqs == 0)
5130 return -EINVAL;
5131 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005132}
5133
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005134static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5135{
5136 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5137 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005138
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005139 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5140 return -EINVAL;
5141 if (rcvd->max_resp_sz < sent->max_resp_sz)
5142 return -EINVAL;
5143 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5144 return -EINVAL;
5145 /* These would render the backchannel useless: */
5146 if (rcvd->max_ops == 0)
5147 return -EINVAL;
5148 if (rcvd->max_reqs == 0)
5149 return -EINVAL;
5150 return 0;
5151}
Andy Adamson8d353012009-04-01 09:22:32 -04005152
Andy Adamson8d353012009-04-01 09:22:32 -04005153static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5154 struct nfs4_session *session)
5155{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005156 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005157
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005158 ret = nfs4_verify_fore_channel_attrs(args, session);
5159 if (ret)
5160 return ret;
5161 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005162}
5163
Andy Adamsonfc931582009-04-01 09:22:31 -04005164static int _nfs4_proc_create_session(struct nfs_client *clp)
5165{
5166 struct nfs4_session *session = clp->cl_session;
5167 struct nfs41_create_session_args args = {
5168 .client = clp,
5169 .cb_program = NFS4_CALLBACK,
5170 };
5171 struct nfs41_create_session_res res = {
5172 .client = clp,
5173 };
5174 struct rpc_message msg = {
5175 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5176 .rpc_argp = &args,
5177 .rpc_resp = &res,
5178 };
5179 int status;
5180
5181 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005182 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005183
5184 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5185
Andy Adamson8d353012009-04-01 09:22:32 -04005186 if (!status)
5187 /* Verify the session's negotiated channel_attrs values */
5188 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005189 if (!status) {
5190 /* Increment the clientid slot sequence id */
5191 clp->cl_seqid++;
5192 }
5193
5194 return status;
5195}
5196
5197/*
5198 * Issues a CREATE_SESSION operation to the server.
5199 * It is the responsibility of the caller to verify the session is
5200 * expired before calling this routine.
5201 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005202int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005203{
5204 int status;
5205 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005206 struct nfs4_session *session = clp->cl_session;
Ricardo Labiaga7d6d63d2011-03-09 13:13:44 -05005207 long timeout = 0;
5208 int err;
Andy Adamsonfc931582009-04-01 09:22:31 -04005209
5210 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5211
Ricardo Labiaga7d6d63d2011-03-09 13:13:44 -05005212 do {
5213 status = _nfs4_proc_create_session(clp);
5214 if (status == -NFS4ERR_DELAY) {
5215 err = nfs4_delay(clp->cl_rpcclient, &timeout);
5216 if (err)
5217 status = err;
5218 }
5219 } while (status == -NFS4ERR_DELAY);
5220
Andy Adamsonfc931582009-04-01 09:22:31 -04005221 if (status)
5222 goto out;
5223
Trond Myklebustf26468f2009-12-05 19:32:11 -05005224 /* Init and reset the fore channel */
5225 status = nfs4_init_slot_tables(session);
5226 dprintk("slot table initialization returned %d\n", status);
5227 if (status)
5228 goto out;
5229 status = nfs4_reset_slot_tables(session);
5230 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005231 if (status)
5232 goto out;
5233
5234 ptr = (unsigned *)&session->sess_id.data[0];
5235 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5236 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005237out:
5238 dprintk("<-- %s\n", __func__);
5239 return status;
5240}
5241
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005242/*
5243 * Issue the over-the-wire RPC DESTROY_SESSION.
5244 * The caller must serialize access to this routine.
5245 */
5246int nfs4_proc_destroy_session(struct nfs4_session *session)
5247{
5248 int status = 0;
5249 struct rpc_message msg;
5250
5251 dprintk("--> nfs4_proc_destroy_session\n");
5252
5253 /* session is still being setup */
5254 if (session->clp->cl_cons_state != NFS_CS_READY)
5255 return status;
5256
5257 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5258 msg.rpc_argp = session;
5259 msg.rpc_resp = NULL;
5260 msg.rpc_cred = NULL;
5261 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5262
5263 if (status)
5264 printk(KERN_WARNING
5265 "Got error %d from the server on DESTROY_SESSION. "
5266 "Session has been destroyed regardless...\n", status);
5267
5268 dprintk("<-- nfs4_proc_destroy_session\n");
5269 return status;
5270}
5271
Trond Myklebustfccba802009-07-21 16:48:07 -04005272int nfs4_init_session(struct nfs_server *server)
5273{
5274 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005275 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005276 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005277 int ret;
5278
5279 if (!nfs4_has_session(clp))
5280 return 0;
5281
Trond Myklebust1055d762010-06-16 09:52:27 -04005282 session = clp->cl_session;
5283 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5284 return 0;
5285
Andy Adamson68bf05e2009-12-15 12:55:02 -05005286 rsize = server->rsize;
5287 if (rsize == 0)
5288 rsize = NFS_MAX_FILE_IO_SIZE;
5289 wsize = server->wsize;
5290 if (wsize == 0)
5291 wsize = NFS_MAX_FILE_IO_SIZE;
5292
Andy Adamson68bf05e2009-12-15 12:55:02 -05005293 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5294 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005295
Trond Myklebustfccba802009-07-21 16:48:07 -04005296 ret = nfs4_recover_expired_lease(server);
5297 if (!ret)
5298 ret = nfs4_check_client_ready(clp);
5299 return ret;
5300}
5301
Andy Adamsond83217c2011-03-01 01:34:17 +00005302int nfs4_init_ds_session(struct nfs_client *clp)
5303{
5304 struct nfs4_session *session = clp->cl_session;
5305 int ret;
5306
5307 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5308 return 0;
5309
5310 ret = nfs4_client_recover_expired_lease(clp);
5311 if (!ret)
5312 /* Test for the DS role */
5313 if (!is_ds_client(clp))
5314 ret = -ENODEV;
5315 if (!ret)
5316 ret = nfs4_check_client_ready(clp);
5317 return ret;
5318
5319}
5320EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5321
5322
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005323/*
5324 * Renew the cl_session lease.
5325 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005326struct nfs4_sequence_data {
5327 struct nfs_client *clp;
5328 struct nfs4_sequence_args args;
5329 struct nfs4_sequence_res res;
5330};
5331
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005332static void nfs41_sequence_release(void *data)
5333{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005334 struct nfs4_sequence_data *calldata = data;
5335 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005336
Alexandros Batsakis71358402010-02-05 03:45:05 -08005337 if (atomic_read(&clp->cl_count) > 1)
5338 nfs4_schedule_state_renewal(clp);
5339 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005340 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005341}
5342
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005343static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5344{
5345 switch(task->tk_status) {
5346 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005347 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5348 return -EAGAIN;
5349 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005350 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005351 }
5352 return 0;
5353}
5354
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005355static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005356{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005357 struct nfs4_sequence_data *calldata = data;
5358 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005359
Trond Myklebust14516c32010-07-31 14:29:06 -04005360 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5361 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005362
5363 if (task->tk_status < 0) {
5364 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005365 if (atomic_read(&clp->cl_count) == 1)
5366 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005367
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005368 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5369 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005370 return;
5371 }
5372 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005373 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005374out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005375 dprintk("<-- %s\n", __func__);
5376}
5377
5378static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5379{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005380 struct nfs4_sequence_data *calldata = data;
5381 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005382 struct nfs4_sequence_args *args;
5383 struct nfs4_sequence_res *res;
5384
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005385 args = task->tk_msg.rpc_argp;
5386 res = task->tk_msg.rpc_resp;
5387
Trond Myklebust035168a2010-06-16 09:52:26 -04005388 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005389 return;
5390 rpc_call_start(task);
5391}
5392
5393static const struct rpc_call_ops nfs41_sequence_ops = {
5394 .rpc_call_done = nfs41_sequence_call_done,
5395 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005396 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005397};
5398
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005399static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005400{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005401 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005402 struct rpc_message msg = {
5403 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5404 .rpc_cred = cred,
5405 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005406 struct rpc_task_setup task_setup_data = {
5407 .rpc_client = clp->cl_rpcclient,
5408 .rpc_message = &msg,
5409 .callback_ops = &nfs41_sequence_ops,
5410 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5411 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005412
Alexandros Batsakis71358402010-02-05 03:45:05 -08005413 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005414 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005415 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005416 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005417 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005418 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005419 }
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005420 msg.rpc_argp = &calldata->args;
5421 msg.rpc_resp = &calldata->res;
5422 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005423 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005424
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005425 return rpc_run_task(&task_setup_data);
5426}
5427
5428static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5429{
5430 struct rpc_task *task;
5431 int ret = 0;
5432
5433 task = _nfs41_proc_sequence(clp, cred);
5434 if (IS_ERR(task))
5435 ret = PTR_ERR(task);
5436 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005437 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005438 dprintk("<-- %s status=%d\n", __func__, ret);
5439 return ret;
5440}
5441
5442static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5443{
5444 struct rpc_task *task;
5445 int ret;
5446
5447 task = _nfs41_proc_sequence(clp, cred);
5448 if (IS_ERR(task)) {
5449 ret = PTR_ERR(task);
5450 goto out;
5451 }
5452 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005453 if (!ret) {
5454 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5455
5456 if (task->tk_status == 0)
5457 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005458 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005459 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005460 rpc_put_task(task);
5461out:
5462 dprintk("<-- %s status=%d\n", __func__, ret);
5463 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005464}
5465
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005466struct nfs4_reclaim_complete_data {
5467 struct nfs_client *clp;
5468 struct nfs41_reclaim_complete_args arg;
5469 struct nfs41_reclaim_complete_res res;
5470};
5471
5472static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5473{
5474 struct nfs4_reclaim_complete_data *calldata = data;
5475
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005476 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005477 if (nfs41_setup_sequence(calldata->clp->cl_session,
5478 &calldata->arg.seq_args,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005479 &calldata->res.seq_res, 0, task))
5480 return;
5481
5482 rpc_call_start(task);
5483}
5484
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005485static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5486{
5487 switch(task->tk_status) {
5488 case 0:
5489 case -NFS4ERR_COMPLETE_ALREADY:
5490 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5491 break;
5492 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005493 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5494 return -EAGAIN;
5495 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005496 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005497 }
5498 return 0;
5499}
5500
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005501static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5502{
5503 struct nfs4_reclaim_complete_data *calldata = data;
5504 struct nfs_client *clp = calldata->clp;
5505 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5506
5507 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005508 if (!nfs41_sequence_done(task, res))
5509 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005510
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005511 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5512 rpc_restart_call_prepare(task);
5513 return;
5514 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005515 dprintk("<-- %s\n", __func__);
5516}
5517
5518static void nfs4_free_reclaim_complete_data(void *data)
5519{
5520 struct nfs4_reclaim_complete_data *calldata = data;
5521
5522 kfree(calldata);
5523}
5524
5525static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5526 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5527 .rpc_call_done = nfs4_reclaim_complete_done,
5528 .rpc_release = nfs4_free_reclaim_complete_data,
5529};
5530
5531/*
5532 * Issue a global reclaim complete.
5533 */
5534static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5535{
5536 struct nfs4_reclaim_complete_data *calldata;
5537 struct rpc_task *task;
5538 struct rpc_message msg = {
5539 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5540 };
5541 struct rpc_task_setup task_setup_data = {
5542 .rpc_client = clp->cl_rpcclient,
5543 .rpc_message = &msg,
5544 .callback_ops = &nfs4_reclaim_complete_call_ops,
5545 .flags = RPC_TASK_ASYNC,
5546 };
5547 int status = -ENOMEM;
5548
5549 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005550 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005551 if (calldata == NULL)
5552 goto out;
5553 calldata->clp = clp;
5554 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005555
5556 msg.rpc_argp = &calldata->arg;
5557 msg.rpc_resp = &calldata->res;
5558 task_setup_data.callback_data = calldata;
5559 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005560 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005561 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005562 goto out;
5563 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005564 status = nfs4_wait_for_completion_rpc_task(task);
5565 if (status == 0)
5566 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005567 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005568 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005569out:
5570 dprintk("<-- %s status=%d\n", __func__, status);
5571 return status;
5572}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005573
5574static void
5575nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5576{
5577 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005578 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005579
5580 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005581 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5582 * right now covering the LAYOUTGET we are about to send.
5583 * However, that is not so catastrophic, and there seems
5584 * to be no way to prevent it completely.
5585 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005586 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5587 &lgp->res.seq_res, 0, task))
5588 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005589 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5590 NFS_I(lgp->args.inode)->layout,
5591 lgp->args.ctx->state)) {
5592 rpc_exit(task, NFS4_OK);
5593 return;
5594 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005595 rpc_call_start(task);
5596}
5597
5598static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5599{
5600 struct nfs4_layoutget *lgp = calldata;
5601 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5602
5603 dprintk("--> %s\n", __func__);
5604
5605 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5606 return;
5607
5608 switch (task->tk_status) {
5609 case 0:
5610 break;
5611 case -NFS4ERR_LAYOUTTRYLATER:
5612 case -NFS4ERR_RECALLCONFLICT:
5613 task->tk_status = -NFS4ERR_DELAY;
5614 /* Fall through */
5615 default:
5616 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5617 rpc_restart_call_prepare(task);
5618 return;
5619 }
5620 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005621 dprintk("<-- %s\n", __func__);
5622}
5623
5624static void nfs4_layoutget_release(void *calldata)
5625{
5626 struct nfs4_layoutget *lgp = calldata;
5627
5628 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005629 put_nfs_open_context(lgp->args.ctx);
5630 kfree(calldata);
5631 dprintk("<-- %s\n", __func__);
5632}
5633
5634static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5635 .rpc_call_prepare = nfs4_layoutget_prepare,
5636 .rpc_call_done = nfs4_layoutget_done,
5637 .rpc_release = nfs4_layoutget_release,
5638};
5639
5640int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5641{
5642 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5643 struct rpc_task *task;
5644 struct rpc_message msg = {
5645 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5646 .rpc_argp = &lgp->args,
5647 .rpc_resp = &lgp->res,
5648 };
5649 struct rpc_task_setup task_setup_data = {
5650 .rpc_client = server->client,
5651 .rpc_message = &msg,
5652 .callback_ops = &nfs4_layoutget_call_ops,
5653 .callback_data = lgp,
5654 .flags = RPC_TASK_ASYNC,
5655 };
5656 int status = 0;
5657
5658 dprintk("--> %s\n", __func__);
5659
Weston Andros Adamson35124a02011-03-24 16:48:21 -04005660 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005661 lgp->res.seq_res.sr_slot = NULL;
5662 task = rpc_run_task(&task_setup_data);
5663 if (IS_ERR(task))
5664 return PTR_ERR(task);
5665 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00005666 if (status == 0)
5667 status = task->tk_status;
5668 if (status == 0)
5669 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005670 rpc_put_task(task);
5671 dprintk("<-- %s status=%d\n", __func__, status);
5672 return status;
5673}
5674
5675static int
5676_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5677{
5678 struct nfs4_getdeviceinfo_args args = {
5679 .pdev = pdev,
5680 };
5681 struct nfs4_getdeviceinfo_res res = {
5682 .pdev = pdev,
5683 };
5684 struct rpc_message msg = {
5685 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5686 .rpc_argp = &args,
5687 .rpc_resp = &res,
5688 };
5689 int status;
5690
5691 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005692 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005693 dprintk("<-- %s status=%d\n", __func__, status);
5694
5695 return status;
5696}
5697
5698int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5699{
5700 struct nfs4_exception exception = { };
5701 int err;
5702
5703 do {
5704 err = nfs4_handle_exception(server,
5705 _nfs4_proc_getdeviceinfo(server, pdev),
5706 &exception);
5707 } while (exception.retry);
5708 return err;
5709}
5710EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5711
Andy Adamson863a3c62011-03-23 13:27:54 +00005712static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
5713{
5714 struct nfs4_layoutcommit_data *data = calldata;
5715 struct nfs_server *server = NFS_SERVER(data->args.inode);
5716
5717 if (nfs4_setup_sequence(server, &data->args.seq_args,
5718 &data->res.seq_res, 1, task))
5719 return;
5720 rpc_call_start(task);
5721}
5722
5723static void
5724nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
5725{
5726 struct nfs4_layoutcommit_data *data = calldata;
5727 struct nfs_server *server = NFS_SERVER(data->args.inode);
5728
5729 if (!nfs4_sequence_done(task, &data->res.seq_res))
5730 return;
5731
5732 switch (task->tk_status) { /* Just ignore these failures */
5733 case NFS4ERR_DELEG_REVOKED: /* layout was recalled */
5734 case NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
5735 case NFS4ERR_BADLAYOUT: /* no layout */
5736 case NFS4ERR_GRACE: /* loca_recalim always false */
5737 task->tk_status = 0;
5738 }
5739
5740 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5741 nfs_restart_rpc(task, server->nfs_client);
5742 return;
5743 }
5744
5745 if (task->tk_status == 0)
5746 nfs_post_op_update_inode_force_wcc(data->args.inode,
5747 data->res.fattr);
5748}
5749
5750static void nfs4_layoutcommit_release(void *calldata)
5751{
5752 struct nfs4_layoutcommit_data *data = calldata;
5753
5754 /* Matched by references in pnfs_set_layoutcommit */
5755 put_lseg(data->lseg);
5756 put_rpccred(data->cred);
5757 kfree(data);
5758}
5759
5760static const struct rpc_call_ops nfs4_layoutcommit_ops = {
5761 .rpc_call_prepare = nfs4_layoutcommit_prepare,
5762 .rpc_call_done = nfs4_layoutcommit_done,
5763 .rpc_release = nfs4_layoutcommit_release,
5764};
5765
5766int
Andy Adamsonef311532011-03-12 02:58:10 -05005767nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00005768{
5769 struct rpc_message msg = {
5770 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
5771 .rpc_argp = &data->args,
5772 .rpc_resp = &data->res,
5773 .rpc_cred = data->cred,
5774 };
5775 struct rpc_task_setup task_setup_data = {
5776 .task = &data->task,
5777 .rpc_client = NFS_CLIENT(data->args.inode),
5778 .rpc_message = &msg,
5779 .callback_ops = &nfs4_layoutcommit_ops,
5780 .callback_data = data,
5781 .flags = RPC_TASK_ASYNC,
5782 };
5783 struct rpc_task *task;
5784 int status = 0;
5785
5786 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
5787 "lbw: %llu inode %lu\n",
5788 data->task.tk_pid, sync,
5789 data->args.lastbytewritten,
5790 data->args.inode->i_ino);
5791
5792 task = rpc_run_task(&task_setup_data);
5793 if (IS_ERR(task))
5794 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05005795 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00005796 goto out;
5797 status = nfs4_wait_for_completion_rpc_task(task);
5798 if (status != 0)
5799 goto out;
5800 status = task->tk_status;
5801out:
5802 dprintk("%s: status %d\n", __func__, status);
5803 rpc_put_task(task);
5804 return status;
5805}
Andy Adamson557134a2009-04-01 09:21:53 -04005806#endif /* CONFIG_NFS_V4_1 */
5807
Andy Adamson591d71c2009-04-01 09:22:47 -04005808struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005809 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005810 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005811 .recover_open = nfs4_open_reclaim,
5812 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04005813 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005814 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005815};
5816
Andy Adamson591d71c2009-04-01 09:22:47 -04005817#if defined(CONFIG_NFS_V4_1)
5818struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5819 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5820 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5821 .recover_open = nfs4_open_reclaim,
5822 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05005823 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005824 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005825 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04005826};
5827#endif /* CONFIG_NFS_V4_1 */
5828
5829struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005830 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005831 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832 .recover_open = nfs4_open_expired,
5833 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04005834 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005835 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836};
5837
Andy Adamson591d71c2009-04-01 09:22:47 -04005838#if defined(CONFIG_NFS_V4_1)
5839struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5840 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5841 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5842 .recover_open = nfs4_open_expired,
5843 .recover_lock = nfs4_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05005844 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005845 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04005846};
5847#endif /* CONFIG_NFS_V4_1 */
5848
Benny Halevy29fba382009-04-01 09:22:44 -04005849struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5850 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04005851 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005852 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04005853};
5854
5855#if defined(CONFIG_NFS_V4_1)
5856struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5857 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04005858 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005859 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04005860};
5861#endif
5862
Trond Myklebust97dc1352010-06-16 09:52:26 -04005863static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
5864 .minor_version = 0,
5865 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04005866 .validate_stateid = nfs4_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005867 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
5868 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
5869 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005870};
5871
5872#if defined(CONFIG_NFS_V4_1)
5873static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
5874 .minor_version = 1,
5875 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04005876 .validate_stateid = nfs41_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005877 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
5878 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
5879 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005880};
5881#endif
5882
Trond Myklebust97dc1352010-06-16 09:52:26 -04005883const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
5884 [0] = &nfs_v4_0_minor_ops,
5885#if defined(CONFIG_NFS_V4_1)
5886 [1] = &nfs_v4_1_minor_ops,
5887#endif
5888};
5889
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005890static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005891 .permission = nfs_permission,
5892 .getattr = nfs_getattr,
5893 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005894 .getxattr = generic_getxattr,
5895 .setxattr = generic_setxattr,
5896 .listxattr = generic_listxattr,
5897 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005898};
5899
David Howells509de812006-08-22 20:06:11 -04005900const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901 .version = 4, /* protocol version */
5902 .dentry_ops = &nfs4_dentry_operations,
5903 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005904 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905 .getroot = nfs4_proc_get_root,
5906 .getattr = nfs4_proc_getattr,
5907 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04005908 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909 .lookup = nfs4_proc_lookup,
5910 .access = nfs4_proc_access,
5911 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912 .create = nfs4_proc_create,
5913 .remove = nfs4_proc_remove,
5914 .unlink_setup = nfs4_proc_unlink_setup,
5915 .unlink_done = nfs4_proc_unlink_done,
5916 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04005917 .rename_setup = nfs4_proc_rename_setup,
5918 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919 .link = nfs4_proc_link,
5920 .symlink = nfs4_proc_symlink,
5921 .mkdir = nfs4_proc_mkdir,
5922 .rmdir = nfs4_proc_remove,
5923 .readdir = nfs4_proc_readdir,
5924 .mknod = nfs4_proc_mknod,
5925 .statfs = nfs4_proc_statfs,
5926 .fsinfo = nfs4_proc_fsinfo,
5927 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04005928 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005929 .decode_dirent = nfs4_decode_dirent,
5930 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05005931 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005932 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005933 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005935 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005937 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04005938 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04005939 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00005940 .init_client = nfs4_init_client,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005941 .secinfo = nfs4_proc_secinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005942};
5943
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005944static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
5945 .prefix = XATTR_NAME_NFSV4_ACL,
5946 .list = nfs4_xattr_list_nfs4_acl,
5947 .get = nfs4_xattr_get_nfs4_acl,
5948 .set = nfs4_xattr_set_nfs4_acl,
5949};
5950
5951const struct xattr_handler *nfs4_xattr_handlers[] = {
5952 &nfs4_xattr_nfs4_acl_handler,
5953 NULL
5954};
5955
Linus Torvalds1da177e2005-04-16 15:20:36 -07005956/*
5957 * Local variables:
5958 * c-basic-offset: 8
5959 * End:
5960 */