blob: 563463777d9d22d0319795d0a6b16adcfb4dc028 [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>
44#include <linux/nfs.h>
45#include <linux/nfs4.h>
46#include <linux/nfs_fs.h>
47#include <linux/nfs_page.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070049#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040050#include <linux/module.h>
Andy Adamson5a0ffe52009-04-01 09:23:18 -040051#include <linux/sunrpc/bc_xprt.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000052#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050053#include <linux/utsname.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Trond Myklebust4ce79712005-06-22 17:16:21 +000055#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050057#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050058#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040059#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040060#include "pnfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62#define NFSDBG_FACILITY NFSDBG_PROC
63
Trond Myklebust2066fe82006-09-15 08:30:46 -040064#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#define NFS4_POLL_RETRY_MAX (15*HZ)
66
Trond Myklebusta78cb572009-08-09 15:06:19 -040067#define NFS4_MAX_LOOP_ON_RECOVER (10)
68
Trond Myklebustcdd4e682006-01-03 09:55:12 +010069struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010070static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080071static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050073static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Bryan Schumaker7c513052011-03-24 17:12:24 +000074static int _nfs4_proc_lookup(struct rpc_clnt *client, struct inode *dir,
75 const struct qstr *name, struct nfs_fh *fhandle,
76 struct nfs_fattr *fattr);
Trond Myklebust99367812007-07-17 21:52:41 -040077static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040078static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
79 struct nfs_fattr *fattr, struct iattr *sattr,
80 struct nfs4_state *state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Linus Torvalds1da177e2005-04-16 15:20:36 -070082/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050083static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
Trond Myklebust52567b02009-10-23 14:46:42 -040085 if (err >= -1000)
86 return err;
87 switch (err) {
88 case -NFS4ERR_RESOURCE:
89 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +000090 case -NFS4ERR_WRONGSEC:
91 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -080092 case -NFS4ERR_BADOWNER:
93 case -NFS4ERR_BADNAME:
94 return -EINVAL;
Trond Myklebust52567b02009-10-23 14:46:42 -040095 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -070097 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -040098 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400100 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
103/*
104 * This is our standard bitmap for GETATTR requests.
105 */
106const u32 nfs4_fattr_bitmap[2] = {
107 FATTR4_WORD0_TYPE
108 | FATTR4_WORD0_CHANGE
109 | FATTR4_WORD0_SIZE
110 | FATTR4_WORD0_FSID
111 | FATTR4_WORD0_FILEID,
112 FATTR4_WORD1_MODE
113 | FATTR4_WORD1_NUMLINKS
114 | FATTR4_WORD1_OWNER
115 | FATTR4_WORD1_OWNER_GROUP
116 | FATTR4_WORD1_RAWDEV
117 | FATTR4_WORD1_SPACE_USED
118 | FATTR4_WORD1_TIME_ACCESS
119 | FATTR4_WORD1_TIME_METADATA
120 | FATTR4_WORD1_TIME_MODIFY
121};
122
123const u32 nfs4_statfs_bitmap[2] = {
124 FATTR4_WORD0_FILES_AVAIL
125 | FATTR4_WORD0_FILES_FREE
126 | FATTR4_WORD0_FILES_TOTAL,
127 FATTR4_WORD1_SPACE_AVAIL
128 | FATTR4_WORD1_SPACE_FREE
129 | FATTR4_WORD1_SPACE_TOTAL
130};
131
Trond Myklebust4ce79712005-06-22 17:16:21 +0000132const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 FATTR4_WORD0_MAXLINK
134 | FATTR4_WORD0_MAXNAME,
135 0
136};
137
138const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
139 | FATTR4_WORD0_MAXREAD
140 | FATTR4_WORD0_MAXWRITE
141 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700142 FATTR4_WORD1_TIME_DELTA
Andy Adamson504913f2010-10-20 00:17:57 -0400143 | FATTR4_WORD1_FS_LAYOUT_TYPES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144};
145
Manoj Naik830b8e32006-06-09 09:34:25 -0400146const u32 nfs4_fs_locations_bitmap[2] = {
147 FATTR4_WORD0_TYPE
148 | FATTR4_WORD0_CHANGE
149 | FATTR4_WORD0_SIZE
150 | FATTR4_WORD0_FSID
151 | FATTR4_WORD0_FILEID
152 | FATTR4_WORD0_FS_LOCATIONS,
153 FATTR4_WORD1_MODE
154 | FATTR4_WORD1_NUMLINKS
155 | FATTR4_WORD1_OWNER
156 | FATTR4_WORD1_OWNER_GROUP
157 | FATTR4_WORD1_RAWDEV
158 | FATTR4_WORD1_SPACE_USED
159 | FATTR4_WORD1_TIME_ACCESS
160 | FATTR4_WORD1_TIME_METADATA
161 | FATTR4_WORD1_TIME_MODIFY
162 | FATTR4_WORD1_MOUNTED_ON_FILEID
163};
164
Al Virobc4785c2006-10-19 23:28:51 -0700165static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 struct nfs4_readdir_arg *readdir)
167{
Al Viro0dbb4c62006-10-19 23:28:49 -0700168 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 BUG_ON(readdir->count < 80);
171 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000172 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
174 return;
175 }
176
177 readdir->cookie = 0;
178 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
179 if (cookie == 2)
180 return;
181
182 /*
183 * NFSv4 servers do not return entries for '.' and '..'
184 * Therefore, we fake these entries here. We let '.'
185 * have cookie 0 and '..' have cookie 1. Note that
186 * when talking to the server, we always send cookie 0
187 * instead of 1 or 2.
188 */
Al Viro0dbb4c62006-10-19 23:28:49 -0700189 start = p = kmap_atomic(*readdir->pages, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 if (cookie == 0) {
192 *p++ = xdr_one; /* next */
193 *p++ = xdr_zero; /* cookie, first word */
194 *p++ = xdr_one; /* cookie, second word */
195 *p++ = xdr_one; /* entry len */
196 memcpy(p, ".\0\0\0", 4); /* entry */
197 p++;
198 *p++ = xdr_one; /* bitmap length */
199 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
200 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400201 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
203
204 *p++ = xdr_one; /* next */
205 *p++ = xdr_zero; /* cookie, first word */
206 *p++ = xdr_two; /* cookie, second word */
207 *p++ = xdr_two; /* entry len */
208 memcpy(p, "..\0\0", 4); /* entry */
209 p++;
210 *p++ = xdr_one; /* bitmap length */
211 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
212 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400213 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 readdir->pgbase = (char *)p - (char *)start;
216 readdir->count -= readdir->pgbase;
217 kunmap_atomic(start, KM_USER0);
218}
219
Trond Myklebust65de8722008-12-23 15:21:44 -0500220static int nfs4_wait_clnt_recover(struct nfs_client *clp)
221{
222 int res;
223
224 might_sleep();
225
Trond Myklebuste005e802008-12-23 15:21:48 -0500226 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400227 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500228 return res;
229}
230
231static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
232{
233 int res = 0;
234
235 might_sleep();
236
237 if (*timeout <= 0)
238 *timeout = NFS4_POLL_RETRY_MIN;
239 if (*timeout > NFS4_POLL_RETRY_MAX)
240 *timeout = NFS4_POLL_RETRY_MAX;
241 schedule_timeout_killable(*timeout);
242 if (fatal_signal_pending(current))
243 res = -ERESTARTSYS;
244 *timeout <<= 1;
245 return res;
246}
247
248/* This is the error handling routine for processes that are allowed
249 * to sleep.
250 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800251static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500252{
253 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500254 struct nfs4_state *state = exception->state;
Trond Myklebust65de8722008-12-23 15:21:44 -0500255 int ret = errorcode;
256
257 exception->retry = 0;
258 switch(errorcode) {
259 case 0:
260 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500261 case -NFS4ERR_ADMIN_REVOKED:
262 case -NFS4ERR_BAD_STATEID:
263 case -NFS4ERR_OPENMODE:
264 if (state == NULL)
265 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500266 nfs4_schedule_stateid_recovery(server, state);
267 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500268 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500269 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust65de8722008-12-23 15:21:44 -0500270 case -NFS4ERR_EXPIRED:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500271 nfs4_schedule_lease_recovery(clp);
272 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500273#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400274 case -NFS4ERR_BADSESSION:
275 case -NFS4ERR_BADSLOT:
276 case -NFS4ERR_BAD_HIGH_SLOT:
277 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
278 case -NFS4ERR_DEADSESSION:
279 case -NFS4ERR_SEQ_FALSE_RETRY:
280 case -NFS4ERR_SEQ_MISORDERED:
281 dprintk("%s ERROR: %d Reset session\n", __func__,
282 errorcode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500283 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -0400284 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500285 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500286#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500287 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500288 if (exception->timeout > HZ) {
289 /* We have retried a decent amount, time to
290 * fail
291 */
292 ret = -EBUSY;
293 break;
294 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500295 case -NFS4ERR_GRACE:
296 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500297 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500298 ret = nfs4_delay(server->client, &exception->timeout);
299 if (ret != 0)
300 break;
301 case -NFS4ERR_OLD_STATEID:
302 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800303 break;
304 case -NFS4ERR_BADOWNER:
305 /* The following works around a Linux server bug! */
306 case -NFS4ERR_BADNAME:
307 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
308 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
309 exception->retry = 1;
310 printk(KERN_WARNING "NFS: v4 server %s "
311 "does not accept raw "
312 "uid/gids. "
313 "Reenabling the idmapper.\n",
314 server->nfs_client->cl_hostname);
315 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500316 }
317 /* We failed to handle the error */
318 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500319wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500320 ret = nfs4_wait_clnt_recover(clp);
321 if (ret == 0)
322 exception->retry = 1;
323 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500324}
325
326
Trond Myklebust452e9352010-07-31 14:29:06 -0400327static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 spin_lock(&clp->cl_lock);
330 if (time_before(clp->cl_last_renewal,timestamp))
331 clp->cl_last_renewal = timestamp;
332 spin_unlock(&clp->cl_lock);
333}
334
Trond Myklebust452e9352010-07-31 14:29:06 -0400335static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
336{
337 do_renew_lease(server->nfs_client, timestamp);
338}
339
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400340#if defined(CONFIG_NFS_V4_1)
341
Benny Halevy510b8172009-04-01 09:22:14 -0400342/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400343 * nfs4_free_slot - free a slot and efficiently update slot table.
344 *
345 * freeing a slot is trivially done by clearing its respective bit
346 * in the bitmap.
347 * If the freed slotid equals highest_used_slotid we want to update it
348 * so that the server would be able to size down the slot table if needed,
349 * otherwise we know that the highest_used_slotid is still in use.
350 * When updating highest_used_slotid there may be "holes" in the bitmap
351 * so we need to scan down from highest_used_slotid to 0 looking for the now
352 * highest slotid in use.
353 * If none found, highest_used_slotid is set to -1.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500354 *
355 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400356 */
357static void
Benny Halevydfb4f302010-09-24 09:17:01 -0400358nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *free_slot)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400359{
Benny Halevydfb4f302010-09-24 09:17:01 -0400360 int free_slotid = free_slot - tbl->slots;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400361 int slotid = free_slotid;
362
Benny Halevydfb4f302010-09-24 09:17:01 -0400363 BUG_ON(slotid < 0 || slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400364 /* clear used bit in bitmap */
365 __clear_bit(slotid, tbl->used_slots);
366
367 /* update highest_used_slotid when it is freed */
368 if (slotid == tbl->highest_used_slotid) {
369 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500370 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400371 tbl->highest_used_slotid = slotid;
372 else
373 tbl->highest_used_slotid = -1;
374 }
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400375 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
376 free_slotid, tbl->highest_used_slotid);
377}
378
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800379/*
Andy Adamson42acd022011-01-06 02:04:34 +0000380 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800381 */
Andy Adamson42acd022011-01-06 02:04:34 +0000382static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800383{
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800384 struct rpc_task *task;
385
Trond Myklebusta2118c32010-06-16 09:52:26 -0400386 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800387 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
388 if (task)
389 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800390 return;
391 }
392
393 if (ses->fc_slot_table.highest_used_slotid != -1)
394 return;
395
Andy Adamson42acd022011-01-06 02:04:34 +0000396 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
397 complete(&ses->fc_slot_table.complete);
398}
399
400/*
401 * Signal state manager thread if session back channel is drained
402 */
403void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
404{
405 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
406 ses->bc_slot_table.highest_used_slotid != -1)
407 return;
408 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
409 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800410}
411
Trond Myklebustd185a332010-06-16 09:52:25 -0400412static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400413{
414 struct nfs4_slot_table *tbl;
415
Trond Myklebustd185a332010-06-16 09:52:25 -0400416 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f302010-09-24 09:17:01 -0400417 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400418 /* just wake up the next guy waiting since
419 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500420 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500421 return;
Andy Adamson13615872009-04-01 09:22:17 -0400422 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500423
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500424 spin_lock(&tbl->slot_tbl_lock);
Benny Halevydfb4f302010-09-24 09:17:01 -0400425 nfs4_free_slot(tbl, res->sr_slot);
Andy Adamson42acd022011-01-06 02:04:34 +0000426 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500427 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f302010-09-24 09:17:01 -0400428 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400429}
430
Trond Myklebust14516c32010-07-31 14:29:06 -0400431static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400432{
433 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400434 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400435
436 /*
437 * sr_status remains 1 if an RPC level error occurred. The server
438 * may or may not have processed the sequence operation..
439 * Proceed as if the server received and processed the sequence
440 * operation.
441 */
442 if (res->sr_status == 1)
443 res->sr_status = NFS_OK;
444
445 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
Benny Halevydfb4f302010-09-24 09:17:01 -0400446 if (!res->sr_slot)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400447 goto out;
448
Andy Adamson691daf32009-12-04 15:55:39 -0500449 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400450 switch (res->sr_status) {
451 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400452 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f302010-09-24 09:17:01 -0400453 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400454 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400455 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400456 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500457 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500458 if (res->sr_status_flags != 0)
459 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400460 break;
461 case -NFS4ERR_DELAY:
462 /* The server detected a resend of the RPC call and
463 * returned NFS4ERR_DELAY as per Section 2.10.6.2
464 * of RFC5661.
465 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200466 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f302010-09-24 09:17:01 -0400467 __func__,
468 res->sr_slot - res->sr_session->fc_slot_table.slots,
469 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400470 goto out_retry;
471 default:
472 /* Just update the slot sequence no. */
Benny Halevydfb4f302010-09-24 09:17:01 -0400473 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400474 }
475out:
476 /* The session may be reset by one of the error handlers. */
477 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400478 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400479 return 1;
480out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400481 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400482 goto out;
483 rpc_delay(task, NFS4_POLL_RETRY_MAX);
484 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400485}
486
Trond Myklebust14516c32010-07-31 14:29:06 -0400487static int nfs4_sequence_done(struct rpc_task *task,
488 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400489{
Trond Myklebust14516c32010-07-31 14:29:06 -0400490 if (res->sr_session == NULL)
491 return 1;
492 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400493}
494
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400495/*
Benny Halevy510b8172009-04-01 09:22:14 -0400496 * nfs4_find_slot - efficiently look for a free slot
497 *
498 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
499 * If found, we mark the slot as used, update the highest_used_slotid,
500 * and respectively set up the sequence operation args.
501 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400502 *
503 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400504 */
505static u8
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800506nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400507{
508 int slotid;
509 u8 ret_id = NFS4_MAX_SLOT_TABLE;
510 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
511
Benny Halevy510b8172009-04-01 09:22:14 -0400512 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
513 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
514 tbl->max_slots);
515 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
516 if (slotid >= tbl->max_slots)
517 goto out;
518 __set_bit(slotid, tbl->used_slots);
519 if (slotid > tbl->highest_used_slotid)
520 tbl->highest_used_slotid = slotid;
521 ret_id = slotid;
522out:
523 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
524 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400525 return ret_id;
526}
527
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000528int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400529 struct nfs4_sequence_args *args,
530 struct nfs4_sequence_res *res,
531 int cache_reply,
532 struct rpc_task *task)
533{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400534 struct nfs4_slot *slot;
535 struct nfs4_slot_table *tbl;
536 u8 slotid;
537
538 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400539 /* slot already allocated? */
Benny Halevydfb4f302010-09-24 09:17:01 -0400540 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400541 return 0;
542
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400543 tbl = &session->fc_slot_table;
544
545 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400546 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800547 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamsonea028ac2009-12-04 15:55:38 -0500548 /*
549 * The state manager will wait until the slot table is empty.
550 * Schedule the reset thread
551 */
Andy Adamson05f0d232009-12-04 15:55:37 -0500552 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400553 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500554 dprintk("%s Schedule Session Reset\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500555 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400556 }
557
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800558 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
559 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
560 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
561 spin_unlock(&tbl->slot_tbl_lock);
562 dprintk("%s enforce FIFO order\n", __func__);
563 return -EAGAIN;
564 }
565
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800566 slotid = nfs4_find_slot(tbl);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400567 if (slotid == NFS4_MAX_SLOT_TABLE) {
568 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
569 spin_unlock(&tbl->slot_tbl_lock);
570 dprintk("<-- %s: no free slots\n", __func__);
571 return -EAGAIN;
572 }
573 spin_unlock(&tbl->slot_tbl_lock);
574
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800575 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400576 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400577 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400578 args->sa_slotid = slotid;
579 args->sa_cache_this = cache_reply;
580
581 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
582
Benny Halevy99fe60d2009-04-01 09:22:29 -0400583 res->sr_session = session;
Benny Halevydfb4f302010-09-24 09:17:01 -0400584 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400585 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400586 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400587 /*
588 * sr_status is only set in decode_sequence, and so will remain
589 * set to 1 if an rpc level failure occurs.
590 */
591 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400592 return 0;
593}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000594EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400595
Trond Myklebust035168a2010-06-16 09:52:26 -0400596int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400597 struct nfs4_sequence_args *args,
598 struct nfs4_sequence_res *res,
599 int cache_reply,
600 struct rpc_task *task)
601{
Trond Myklebust035168a2010-06-16 09:52:26 -0400602 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400603 int ret = 0;
604
Trond Myklebust035168a2010-06-16 09:52:26 -0400605 if (session == NULL) {
606 args->sa_session = NULL;
607 res->sr_session = NULL;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400608 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400609 }
610
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200611 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f302010-09-24 09:17:01 -0400612 __func__, session->clp, session, res->sr_slot ?
613 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400614
615 ret = nfs41_setup_sequence(session, args, res, cache_reply,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400616 task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400617out:
618 dprintk("<-- %s status=%d\n", __func__, ret);
619 return ret;
620}
621
622struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400623 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400624 struct nfs4_sequence_args *seq_args;
625 struct nfs4_sequence_res *seq_res;
626 int cache_reply;
627};
628
629static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
630{
631 struct nfs41_call_sync_data *data = calldata;
632
Trond Myklebust035168a2010-06-16 09:52:26 -0400633 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
634
635 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400636 data->seq_res, data->cache_reply, task))
637 return;
638 rpc_call_start(task);
639}
640
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800641static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
642{
643 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
644 nfs41_call_sync_prepare(task, calldata);
645}
646
Andy Adamson69ab40c2009-04-01 09:22:19 -0400647static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
648{
649 struct nfs41_call_sync_data *data = calldata;
650
Trond Myklebust14516c32010-07-31 14:29:06 -0400651 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400652}
653
Andy Adamsonce5039c2009-04-01 09:22:13 -0400654struct rpc_call_ops nfs41_call_sync_ops = {
655 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400656 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400657};
658
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800659struct rpc_call_ops nfs41_call_priv_sync_ops = {
660 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
661 .rpc_call_done = nfs41_call_sync_done,
662};
663
Bryan Schumaker7c513052011-03-24 17:12:24 +0000664static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
665 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400666 struct rpc_message *msg,
667 struct nfs4_sequence_args *args,
668 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800669 int cache_reply,
670 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400671{
672 int ret;
673 struct rpc_task *task;
674 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400675 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400676 .seq_args = args,
677 .seq_res = res,
678 .cache_reply = cache_reply,
679 };
680 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000681 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400682 .rpc_message = msg,
683 .callback_ops = &nfs41_call_sync_ops,
684 .callback_data = &data
685 };
686
Benny Halevydfb4f302010-09-24 09:17:01 -0400687 res->sr_slot = NULL;
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800688 if (privileged)
689 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400690 task = rpc_run_task(&task_setup);
691 if (IS_ERR(task))
692 ret = PTR_ERR(task);
693 else {
694 ret = task->tk_status;
695 rpc_put_task(task);
696 }
697 return ret;
698}
699
Bryan Schumaker7c513052011-03-24 17:12:24 +0000700int _nfs4_call_sync_session(struct rpc_clnt *clnt,
701 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400702 struct rpc_message *msg,
703 struct nfs4_sequence_args *args,
704 struct nfs4_sequence_res *res,
705 int cache_reply)
706{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000707 return nfs4_call_sync_sequence(clnt, server, msg, args, res, cache_reply, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400708}
709
Trond Myklebustdf896452010-06-16 09:52:26 -0400710#else
Trond Myklebust14516c32010-07-31 14:29:06 -0400711static int nfs4_sequence_done(struct rpc_task *task,
712 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400713{
Trond Myklebust14516c32010-07-31 14:29:06 -0400714 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400715}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400716#endif /* CONFIG_NFS_V4_1 */
717
Bryan Schumaker7c513052011-03-24 17:12:24 +0000718int _nfs4_call_sync(struct rpc_clnt *clnt,
719 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400720 struct rpc_message *msg,
721 struct nfs4_sequence_args *args,
722 struct nfs4_sequence_res *res,
723 int cache_reply)
724{
Benny Halevyf3752972009-04-01 09:22:04 -0400725 args->sa_session = res->sr_session = NULL;
Bryan Schumaker7c513052011-03-24 17:12:24 +0000726 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400727}
728
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000729static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000730int nfs4_call_sync(struct rpc_clnt *clnt,
731 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000732 struct rpc_message *msg,
733 struct nfs4_sequence_args *args,
734 struct nfs4_sequence_res *res,
735 int cache_reply)
736{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000737 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
738 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000739}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400740
Trond Myklebust38478b22006-05-25 01:40:57 -0400741static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
Trond Myklebust38478b22006-05-25 01:40:57 -0400743 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Trond Myklebust38478b22006-05-25 01:40:57 -0400745 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400746 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
747 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400748 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400749 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400750 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751}
752
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100753struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400754 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100755 struct nfs_openargs o_arg;
756 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100757 struct nfs_open_confirmargs c_arg;
758 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100759 struct nfs_fattr f_attr;
760 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400761 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100762 struct dentry *dir;
763 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400764 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100765 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100766 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400767 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100768 int rpc_status;
769 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100770};
771
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400772
773static void nfs4_init_opendata_res(struct nfs4_opendata *p)
774{
775 p->o_res.f_attr = &p->f_attr;
776 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400777 p->o_res.seqid = p->o_arg.seqid;
778 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400779 p->o_res.server = p->o_arg.server;
780 nfs_fattr_init(&p->f_attr);
781 nfs_fattr_init(&p->dir_attr);
782}
783
Trond Myklebustad389da2007-06-05 12:30:00 -0400784static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500785 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400786 const struct iattr *attrs,
787 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100788{
Trond Myklebustad389da2007-06-05 12:30:00 -0400789 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100790 struct inode *dir = parent->d_inode;
791 struct nfs_server *server = NFS_SERVER(dir);
792 struct nfs4_opendata *p;
793
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400794 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100795 if (p == NULL)
796 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400797 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100798 if (p->o_arg.seqid == NULL)
799 goto err_free;
Al Virof6948692010-01-30 13:51:04 -0500800 path_get(path);
801 p->path = *path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100802 p->dir = parent;
803 p->owner = sp;
804 atomic_inc(&sp->so_count);
805 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500806 p->o_arg.open_flags = flags;
807 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400808 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400809 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400810 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100811 p->o_arg.server = server;
812 p->o_arg.bitmask = server->attr_bitmask;
813 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400814 if (flags & O_CREAT) {
815 u32 *s;
816
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100817 p->o_arg.u.attrs = &p->attrs;
818 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400819 s = (u32 *) p->o_arg.u.verifier.data;
820 s[0] = jiffies;
821 s[1] = current->pid;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100822 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100823 p->c_arg.fh = &p->o_res.fh;
824 p->c_arg.stateid = &p->o_res.stateid;
825 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400826 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400827 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100828 return p;
829err_free:
830 kfree(p);
831err:
832 dput(parent);
833 return NULL;
834}
835
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400836static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100837{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400838 struct nfs4_opendata *p = container_of(kref,
839 struct nfs4_opendata, kref);
840
841 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400842 if (p->state != NULL)
843 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400844 nfs4_put_state_owner(p->owner);
845 dput(p->dir);
Jan Blunck31f31db2008-05-02 13:42:45 -0700846 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400847 kfree(p);
848}
849
850static void nfs4_opendata_put(struct nfs4_opendata *p)
851{
852 if (p != NULL)
853 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100854}
855
Trond Myklebust06f814a2006-01-03 09:55:07 +0100856static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
857{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100858 int ret;
859
Trond Myklebust06f814a2006-01-03 09:55:07 +0100860 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100861 return ret;
862}
863
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500864static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400865{
866 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500867
868 if (open_mode & O_EXCL)
869 goto out;
870 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400871 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500872 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
873 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400874 break;
875 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500876 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
877 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400878 break;
879 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500880 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
881 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400882 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500883out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400884 return ret;
885}
886
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500887static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400888{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500889 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400890 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500891 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400892 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500893 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400894 return 1;
895}
896
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500897static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100898{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500899 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100900 case FMODE_WRITE:
901 state->n_wronly++;
902 break;
903 case FMODE_READ:
904 state->n_rdonly++;
905 break;
906 case FMODE_READ|FMODE_WRITE:
907 state->n_rdwr++;
908 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500909 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100910}
911
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500912static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400913{
914 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
915 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
916 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500917 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400918 case FMODE_READ:
919 set_bit(NFS_O_RDONLY_STATE, &state->flags);
920 break;
921 case FMODE_WRITE:
922 set_bit(NFS_O_WRONLY_STATE, &state->flags);
923 break;
924 case FMODE_READ|FMODE_WRITE:
925 set_bit(NFS_O_RDWR_STATE, &state->flags);
926 }
927}
928
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500929static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400930{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400931 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500932 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400933 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400934}
935
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500936static 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 -0700937{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400938 /*
939 * Protect the call to nfs4_state_set_mode_locked and
940 * serialise the stateid update
941 */
942 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400943 if (deleg_stateid != NULL) {
944 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
945 set_bit(NFS_DELEGATED_STATE, &state->flags);
946 }
947 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500948 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400949 write_sequnlock(&state->seqlock);
950 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500951 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700952 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953}
954
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500955static 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 -0500956{
957 struct nfs_inode *nfsi = NFS_I(state->inode);
958 struct nfs_delegation *deleg_cur;
959 int ret = 0;
960
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500961 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500962
963 rcu_read_lock();
964 deleg_cur = rcu_dereference(nfsi->delegation);
965 if (deleg_cur == NULL)
966 goto no_delegation;
967
968 spin_lock(&deleg_cur->lock);
969 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500970 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500971 goto no_delegation_unlock;
972
973 if (delegation == NULL)
974 delegation = &deleg_cur->stateid;
975 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
976 goto no_delegation_unlock;
977
Trond Myklebustb7391f42008-12-23 15:21:52 -0500978 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500979 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500980 ret = 1;
981no_delegation_unlock:
982 spin_unlock(&deleg_cur->lock);
983no_delegation:
984 rcu_read_unlock();
985
986 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500987 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500988 ret = 1;
989 }
990
991 return ret;
992}
993
994
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500995static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400996{
997 struct nfs_delegation *delegation;
998
999 rcu_read_lock();
1000 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001001 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001002 rcu_read_unlock();
1003 return;
1004 }
1005 rcu_read_unlock();
1006 nfs_inode_return_delegation(inode);
1007}
1008
Trond Myklebust6ee41262007-07-08 14:11:36 -04001009static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001010{
1011 struct nfs4_state *state = opendata->state;
1012 struct nfs_inode *nfsi = NFS_I(state->inode);
1013 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001014 int open_mode = opendata->o_arg.open_flags & O_EXCL;
1015 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001016 nfs4_stateid stateid;
1017 int ret = -EAGAIN;
1018
Trond Myklebustaac00a82007-07-05 19:02:21 -04001019 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001020 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001021 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001022 if (can_open_cached(state, fmode, open_mode)) {
1023 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001024 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001025 goto out_return_state;
1026 }
1027 spin_unlock(&state->owner->so_lock);
1028 }
Trond Myklebust34310432008-12-23 15:21:38 -05001029 rcu_read_lock();
1030 delegation = rcu_dereference(nfsi->delegation);
1031 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001032 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001033 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001034 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001035 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001036 /* Save the delegation */
1037 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1038 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001039 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001040 if (ret != 0)
1041 goto out;
1042 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001043
1044 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001045 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001046 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001047 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001048out:
1049 return ERR_PTR(ret);
1050out_return_state:
1051 atomic_inc(&state->count);
1052 return state;
1053}
1054
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001055static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1056{
1057 struct inode *inode;
1058 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001059 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001060 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001061
Trond Myklebustaac00a82007-07-05 19:02:21 -04001062 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001063 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001064 goto out;
1065 }
1066
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001067 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001068 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001069 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001070 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001071 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001072 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001073 goto err;
1074 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001075 state = nfs4_get_open_state(inode, data->owner);
1076 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001077 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001078 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001079 int delegation_flags = 0;
1080
Trond Myklebust003707c2007-07-05 18:07:55 -04001081 rcu_read_lock();
1082 delegation = rcu_dereference(NFS_I(inode)->delegation);
1083 if (delegation)
1084 delegation_flags = delegation->flags;
1085 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -05001086 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001087 nfs_inode_set_delegation(state->inode,
1088 data->owner->so_cred,
1089 &data->o_res);
1090 else
1091 nfs_inode_reclaim_delegation(state->inode,
1092 data->owner->so_cred,
1093 &data->o_res);
1094 }
Trond Myklebust34310432008-12-23 15:21:38 -05001095
1096 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001097 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001098 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001099out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001100 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001101err_put_inode:
1102 iput(inode);
1103err:
1104 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001105}
1106
Trond Myklebust864472e2006-01-03 09:55:15 +01001107static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1108{
1109 struct nfs_inode *nfsi = NFS_I(state->inode);
1110 struct nfs_open_context *ctx;
1111
1112 spin_lock(&state->inode->i_lock);
1113 list_for_each_entry(ctx, &nfsi->open_files, list) {
1114 if (ctx->state != state)
1115 continue;
1116 get_nfs_open_context(ctx);
1117 spin_unlock(&state->inode->i_lock);
1118 return ctx;
1119 }
1120 spin_unlock(&state->inode->i_lock);
1121 return ERR_PTR(-ENOENT);
1122}
1123
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001124static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1125{
1126 struct nfs4_opendata *opendata;
1127
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001128 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001129 if (opendata == NULL)
1130 return ERR_PTR(-ENOMEM);
1131 opendata->state = state;
1132 atomic_inc(&state->count);
1133 return opendata;
1134}
1135
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001136static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001137{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001138 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001139 int ret;
1140
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001141 opendata->o_arg.open_flags = 0;
1142 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001143 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1144 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1145 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001146 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001147 if (ret != 0)
1148 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001149 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001150 if (IS_ERR(newstate))
1151 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001152 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001153 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001154 return 0;
1155}
1156
1157static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1158{
Trond Myklebust864472e2006-01-03 09:55:15 +01001159 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001160 int ret;
1161
1162 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001163 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001164 smp_rmb();
1165 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001166 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001167 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001168 if (ret != 0)
1169 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001170 if (newstate != state)
1171 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001172 }
1173 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001174 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001175 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001176 if (ret != 0)
1177 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001178 if (newstate != state)
1179 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001180 }
1181 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001182 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001183 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001184 if (ret != 0)
1185 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001186 if (newstate != state)
1187 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001188 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001189 /*
1190 * We may have performed cached opens for all three recoveries.
1191 * Check if we need to update the current stateid.
1192 */
1193 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1194 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001195 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001196 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1197 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001198 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001199 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001200 return 0;
1201}
1202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203/*
1204 * OPEN_RECLAIM:
1205 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001207static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001209 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001210 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001211 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 int status;
1213
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001214 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1215 if (IS_ERR(opendata))
1216 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001217 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1218 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001219 rcu_read_lock();
1220 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001221 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001222 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001223 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001224 opendata->o_arg.u.delegation_type = delegation_type;
1225 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001226 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 return status;
1228}
1229
Trond Myklebust539cd032007-06-05 11:46:42 -04001230static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231{
1232 struct nfs_server *server = NFS_SERVER(state->inode);
1233 struct nfs4_exception exception = { };
1234 int err;
1235 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001236 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001237 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001238 break;
1239 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 } while (exception.retry);
1241 return err;
1242}
1243
Trond Myklebust864472e2006-01-03 09:55:15 +01001244static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1245{
1246 struct nfs_open_context *ctx;
1247 int ret;
1248
1249 ctx = nfs4_state_find_open_context(state);
1250 if (IS_ERR(ctx))
1251 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001252 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001253 put_nfs_open_context(ctx);
1254 return ret;
1255}
1256
Trond Myklebust13437e12007-07-06 15:10:43 -04001257static 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 -07001258{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001259 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001260 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001262 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1263 if (IS_ERR(opendata))
1264 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001265 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001266 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001267 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001268 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001269 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001270 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271}
1272
Trond Myklebust13437e12007-07-06 15:10:43 -04001273int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
1275 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001276 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 int err;
1278 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001279 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 switch (err) {
1281 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001282 case -ENOENT:
1283 case -ESTALE:
1284 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001285 case -NFS4ERR_BADSESSION:
1286 case -NFS4ERR_BADSLOT:
1287 case -NFS4ERR_BAD_HIGH_SLOT:
1288 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1289 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001290 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001291 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 case -NFS4ERR_STALE_CLIENTID:
1293 case -NFS4ERR_STALE_STATEID:
1294 case -NFS4ERR_EXPIRED:
1295 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001296 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001297 goto out;
1298 case -ERESTARTSYS:
1299 /*
1300 * The show must go on: exit, but mark the
1301 * stateid as needing recovery.
1302 */
1303 case -NFS4ERR_ADMIN_REVOKED:
1304 case -NFS4ERR_BAD_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001305 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001306 case -EKEYEXPIRED:
1307 /*
1308 * User RPCSEC_GSS context has expired.
1309 * We cannot recover this stateid now, so
1310 * skip it and allow recovery thread to
1311 * proceed.
1312 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001313 case -ENOMEM:
1314 err = 0;
1315 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 }
1317 err = nfs4_handle_exception(server, err, &exception);
1318 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001319out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 return err;
1321}
1322
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001323static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1324{
1325 struct nfs4_opendata *data = calldata;
1326
1327 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001328 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001329 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1330 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001331 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001332 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001333 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001334 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001335}
1336
1337static void nfs4_open_confirm_release(void *calldata)
1338{
1339 struct nfs4_opendata *data = calldata;
1340 struct nfs4_state *state = NULL;
1341
1342 /* If this request hasn't been cancelled, do nothing */
1343 if (data->cancelled == 0)
1344 goto out_free;
1345 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001346 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001347 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001348 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001349 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001350 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001351out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001352 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001353}
1354
1355static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001356 .rpc_call_done = nfs4_open_confirm_done,
1357 .rpc_release = nfs4_open_confirm_release,
1358};
1359
1360/*
1361 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1362 */
1363static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1364{
1365 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1366 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001367 struct rpc_message msg = {
1368 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1369 .rpc_argp = &data->c_arg,
1370 .rpc_resp = &data->c_res,
1371 .rpc_cred = data->owner->so_cred,
1372 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001373 struct rpc_task_setup task_setup_data = {
1374 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001375 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001376 .callback_ops = &nfs4_open_confirm_ops,
1377 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001378 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001379 .flags = RPC_TASK_ASYNC,
1380 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 int status;
1382
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001383 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001384 data->rpc_done = 0;
1385 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001386 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001387 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001388 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001389 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001390 status = nfs4_wait_for_completion_rpc_task(task);
1391 if (status != 0) {
1392 data->cancelled = 1;
1393 smp_wmb();
1394 } else
1395 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001396 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 return status;
1398}
1399
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001400static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001402 struct nfs4_opendata *data = calldata;
1403 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001404
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001405 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1406 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001407 /*
1408 * Check if we still need to send an OPEN call, or if we can use
1409 * a delegation instead.
1410 */
1411 if (data->state != NULL) {
1412 struct nfs_delegation *delegation;
1413
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001414 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001415 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001416 rcu_read_lock();
1417 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1418 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001419 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001420 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001421 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001422 }
1423 rcu_read_unlock();
1424 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001425 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001426 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001427 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001428 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001429 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001430 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1431 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001432 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001433 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001434 &data->o_arg.seq_args,
1435 &data->o_res.seq_res, 1, task))
1436 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001437 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001438 return;
1439out_no_action:
1440 task->tk_action = NULL;
1441
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001442}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001444static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1445{
1446 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1447 nfs4_open_prepare(task, calldata);
1448}
1449
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001450static void nfs4_open_done(struct rpc_task *task, void *calldata)
1451{
1452 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001454 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001455
Trond Myklebust14516c32010-07-31 14:29:06 -04001456 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1457 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001458
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001459 if (task->tk_status == 0) {
1460 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001461 case S_IFREG:
1462 break;
1463 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001464 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001465 break;
1466 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001467 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001468 break;
1469 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001470 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001471 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001472 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001473 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1474 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001475 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001476 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001477}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001478
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001479static void nfs4_open_release(void *calldata)
1480{
1481 struct nfs4_opendata *data = calldata;
1482 struct nfs4_state *state = NULL;
1483
1484 /* If this request hasn't been cancelled, do nothing */
1485 if (data->cancelled == 0)
1486 goto out_free;
1487 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001488 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001489 goto out_free;
1490 /* In case we need an open_confirm, no cleanup! */
1491 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1492 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001493 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001494 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001495 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001496out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001497 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001498}
1499
1500static const struct rpc_call_ops nfs4_open_ops = {
1501 .rpc_call_prepare = nfs4_open_prepare,
1502 .rpc_call_done = nfs4_open_done,
1503 .rpc_release = nfs4_open_release,
1504};
1505
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001506static const struct rpc_call_ops nfs4_recover_open_ops = {
1507 .rpc_call_prepare = nfs4_recover_open_prepare,
1508 .rpc_call_done = nfs4_open_done,
1509 .rpc_release = nfs4_open_release,
1510};
1511
1512static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001513{
1514 struct inode *dir = data->dir->d_inode;
1515 struct nfs_server *server = NFS_SERVER(dir);
1516 struct nfs_openargs *o_arg = &data->o_arg;
1517 struct nfs_openres *o_res = &data->o_res;
1518 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001519 struct rpc_message msg = {
1520 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1521 .rpc_argp = o_arg,
1522 .rpc_resp = o_res,
1523 .rpc_cred = data->owner->so_cred,
1524 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001525 struct rpc_task_setup task_setup_data = {
1526 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001527 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001528 .callback_ops = &nfs4_open_ops,
1529 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001530 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001531 .flags = RPC_TASK_ASYNC,
1532 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001533 int status;
1534
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001535 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001536 data->rpc_done = 0;
1537 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001538 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001539 if (isrecover)
1540 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001541 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001542 if (IS_ERR(task))
1543 return PTR_ERR(task);
1544 status = nfs4_wait_for_completion_rpc_task(task);
1545 if (status != 0) {
1546 data->cancelled = 1;
1547 smp_wmb();
1548 } else
1549 status = data->rpc_status;
1550 rpc_put_task(task);
1551
1552 return status;
1553}
1554
1555static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1556{
1557 struct inode *dir = data->dir->d_inode;
1558 struct nfs_openres *o_res = &data->o_res;
1559 int status;
1560
1561 status = nfs4_run_open_task(data, 1);
1562 if (status != 0 || !data->rpc_done)
1563 return status;
1564
1565 nfs_refresh_inode(dir, o_res->dir_attr);
1566
1567 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1568 status = _nfs4_proc_open_confirm(data);
1569 if (status != 0)
1570 return status;
1571 }
1572
1573 return status;
1574}
1575
1576/*
1577 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1578 */
1579static int _nfs4_proc_open(struct nfs4_opendata *data)
1580{
1581 struct inode *dir = data->dir->d_inode;
1582 struct nfs_server *server = NFS_SERVER(dir);
1583 struct nfs_openargs *o_arg = &data->o_arg;
1584 struct nfs_openres *o_res = &data->o_res;
1585 int status;
1586
1587 status = nfs4_run_open_task(data, 0);
Trond Myklebust3e309912007-07-07 13:19:59 -04001588 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001589 return status;
1590
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001591 if (o_arg->open_flags & O_CREAT) {
1592 update_changeattr(dir, &o_res->cinfo);
1593 nfs_post_op_update_inode(dir, o_res->dir_attr);
1594 } else
1595 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001596 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1597 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001599 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001601 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 }
1603 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001604 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001605 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606}
1607
Andy Adamsond83217c2011-03-01 01:34:17 +00001608static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001609{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001610 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001611 int ret;
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001612
Trond Myklebusta78cb572009-08-09 15:06:19 -04001613 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001614 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001615 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001616 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001617 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1618 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001619 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001620 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001621 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001622 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001623 return ret;
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001624}
1625
Andy Adamsond83217c2011-03-01 01:34:17 +00001626static int nfs4_recover_expired_lease(struct nfs_server *server)
1627{
1628 return nfs4_client_recover_expired_lease(server->nfs_client);
1629}
1630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631/*
1632 * OPEN_EXPIRED:
1633 * reclaim state on the server after a network partition.
1634 * Assumes caller holds the appropriate lock
1635 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001636static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001638 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001639 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001641 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1642 if (IS_ERR(opendata))
1643 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001644 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001645 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001646 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001647 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001648 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649}
1650
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001651static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001652{
Trond Myklebust539cd032007-06-05 11:46:42 -04001653 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001654 struct nfs4_exception exception = { };
1655 int err;
1656
1657 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001658 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001659 switch (err) {
1660 default:
1661 goto out;
1662 case -NFS4ERR_GRACE:
1663 case -NFS4ERR_DELAY:
1664 nfs4_handle_exception(server, err, &exception);
1665 err = 0;
1666 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001667 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001668out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001669 return err;
1670}
1671
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1673{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001675 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Trond Myklebust864472e2006-01-03 09:55:15 +01001677 ctx = nfs4_state_find_open_context(state);
1678 if (IS_ERR(ctx))
1679 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001680 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001681 put_nfs_open_context(ctx);
1682 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683}
1684
1685/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001686 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1687 * fields corresponding to attributes that were used to store the verifier.
1688 * Make sure we clobber those fields in the later setattr call
1689 */
1690static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1691{
1692 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1693 !(sattr->ia_valid & ATTR_ATIME_SET))
1694 sattr->ia_valid |= ATTR_ATIME;
1695
1696 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1697 !(sattr->ia_valid & ATTR_MTIME_SET))
1698 sattr->ia_valid |= ATTR_MTIME;
1699}
1700
1701/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001702 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001704static 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 -07001705{
1706 struct nfs4_state_owner *sp;
1707 struct nfs4_state *state = NULL;
1708 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001709 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001710 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
1712 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 status = -ENOMEM;
1714 if (!(sp = nfs4_get_state_owner(server, cred))) {
1715 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1716 goto out_err;
1717 }
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001718 status = nfs4_recover_expired_lease(server);
1719 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001720 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001721 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001722 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001723 status = -ENOMEM;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001724 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001725 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001726 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
Trond Myklebustaac00a82007-07-05 19:02:21 -04001728 if (path->dentry->d_inode != NULL)
1729 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1730
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001731 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001733 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001735 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001736 status = PTR_ERR(state);
1737 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001738 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001739 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001740 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001741
1742 if (opendata->o_arg.open_flags & O_EXCL) {
1743 nfs4_exclusive_attrset(opendata, sattr);
1744
1745 nfs_fattr_init(opendata->o_res.f_attr);
1746 status = nfs4_do_setattr(state->inode, cred,
1747 opendata->o_res.f_attr, sattr,
1748 state);
1749 if (status == 0)
1750 nfs_setattr_update_inode(state->inode, sattr);
1751 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1752 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001753 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 *res = state;
1756 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001757err_opendata_put:
1758 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001759err_put_state_owner:
1760 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 *res = NULL;
1763 return status;
1764}
1765
1766
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001767static 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 -07001768{
1769 struct nfs4_exception exception = { };
1770 struct nfs4_state *res;
1771 int status;
1772
1773 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001774 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 if (status == 0)
1776 break;
1777 /* NOTE: BAD_SEQID means the server and client disagree about the
1778 * book-keeping w.r.t. state-changing operations
1779 * (OPEN/CLOSE/LOCK/LOCKU...)
1780 * It is actually a sign of a bug on the client or on the server.
1781 *
1782 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001783 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 * have unhashed the old state_owner for us, and that we can
1785 * therefore safely retry using a new one. We should still warn
1786 * the user though...
1787 */
1788 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001789 printk(KERN_WARNING "NFS: v4 server %s "
1790 " returned a bad sequence-id error!\n",
1791 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 exception.retry = 1;
1793 continue;
1794 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001795 /*
1796 * BAD_STATEID on OPEN means that the server cancelled our
1797 * state before it received the OPEN_CONFIRM.
1798 * Recover by retrying the request as per the discussion
1799 * on Page 181 of RFC3530.
1800 */
1801 if (status == -NFS4ERR_BAD_STATEID) {
1802 exception.retry = 1;
1803 continue;
1804 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001805 if (status == -EAGAIN) {
1806 /* We must have found a delegation */
1807 exception.retry = 1;
1808 continue;
1809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1811 status, &exception));
1812 } while (exception.retry);
1813 return res;
1814}
1815
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001816static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1817 struct nfs_fattr *fattr, struct iattr *sattr,
1818 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001820 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001822 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 .iap = sattr,
1824 .server = server,
1825 .bitmask = server->attr_bitmask,
1826 };
1827 struct nfs_setattrres res = {
1828 .fattr = fattr,
1829 .server = server,
1830 };
1831 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001832 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1833 .rpc_argp = &arg,
1834 .rpc_resp = &res,
1835 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001837 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001838 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Trond Myklebust0e574af2005-10-27 22:12:38 -04001840 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001842 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1843 /* Use that stateid */
1844 } else if (state != NULL) {
Trond Myklebust77041ed2010-07-01 12:49:11 -04001845 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001846 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1848
Bryan Schumaker7c513052011-03-24 17:12:24 +00001849 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001850 if (status == 0 && state != NULL)
1851 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001852 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853}
1854
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001855static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1856 struct nfs_fattr *fattr, struct iattr *sattr,
1857 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001859 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 struct nfs4_exception exception = { };
1861 int err;
1862 do {
1863 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001864 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 &exception);
1866 } while (exception.retry);
1867 return err;
1868}
1869
1870struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001871 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 struct inode *inode;
1873 struct nfs4_state *state;
1874 struct nfs_closeargs arg;
1875 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001876 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001877 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00001878 bool roc;
1879 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880};
1881
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001882static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001883{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001884 struct nfs4_closedata *calldata = data;
1885 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001886
Fred Isamanf7e89172011-01-06 11:36:32 +00001887 if (calldata->roc)
1888 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001889 nfs4_put_open_state(calldata->state);
1890 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001891 nfs4_put_state_owner(sp);
Jan Blunck31f31db2008-05-02 13:42:45 -07001892 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001893 kfree(calldata);
1894}
1895
Trond Myklebust88069f72009-12-08 08:33:16 -05001896static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1897 fmode_t fmode)
1898{
1899 spin_lock(&state->owner->so_lock);
1900 if (!(fmode & FMODE_READ))
1901 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1902 if (!(fmode & FMODE_WRITE))
1903 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1904 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1905 spin_unlock(&state->owner->so_lock);
1906}
1907
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001908static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001910 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 struct nfs_server *server = NFS_SERVER(calldata->inode);
1913
Trond Myklebust14516c32010-07-31 14:29:06 -04001914 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1915 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 /* hmm. we are done with the inode, and in the process of freeing
1917 * the state_owner. we keep this around to process errors
1918 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 switch (task->tk_status) {
1920 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00001921 if (calldata->roc)
1922 pnfs_roc_set_barrier(state->inode,
1923 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04001924 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001925 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001926 nfs4_close_clear_stateid_flags(state,
1927 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 break;
1929 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001930 case -NFS4ERR_OLD_STATEID:
1931 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001933 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001934 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001936 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1937 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001939 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001940 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941}
1942
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001943static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001945 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001946 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001947 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001948
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001949 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001950 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001951
Trond Myklebust88069f72009-12-08 08:33:16 -05001952 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1953 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001954 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001955 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001956 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001957 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001958 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1959 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1960 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001961 }
1962 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001963 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1964 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1965 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001966 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001967 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001968 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05001969
1970 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001971 /* Note: exit _without_ calling nfs4_close_done */
1972 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001973 return;
1974 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001975
Fred Isamanf7e89172011-01-06 11:36:32 +00001976 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001977 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00001978 if (calldata->roc &&
1979 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
1980 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
1981 task, NULL);
1982 return;
1983 }
1984 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001985
Trond Myklebust516a6af2005-10-27 22:12:41 -04001986 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001987 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001988 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Andy Adamson19ddab02009-04-01 09:22:20 -04001989 &calldata->arg.seq_args, &calldata->res.seq_res,
1990 1, task))
1991 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001992 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993}
1994
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001995static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001996 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001997 .rpc_call_done = nfs4_close_done,
1998 .rpc_release = nfs4_free_closedata,
1999};
2000
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001/*
2002 * It is possible for data to be read/written from a mem-mapped file
2003 * after the sys_close call (which hits the vfs layer as a flush).
2004 * This means that we can't safely call nfsv4 close on a file until
2005 * the inode is cleared. This in turn means that we are not good
2006 * NFSv4 citizens - we do not indicate to the server to update the file's
2007 * share state even when we are done with one of the three share
2008 * stateid's in the inode.
2009 *
2010 * NOTE: Caller must be holding the sp->so_owner semaphore!
2011 */
Fred Isamanf7e89172011-01-06 11:36:32 +00002012int 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 -07002013{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002014 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002016 struct nfs4_state_owner *sp = state->owner;
2017 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002018 struct rpc_message msg = {
2019 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2020 .rpc_cred = state->owner->so_cred,
2021 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002022 struct rpc_task_setup task_setup_data = {
2023 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002024 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002025 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002026 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002027 .flags = RPC_TASK_ASYNC,
2028 };
Trond Myklebust95121352005-10-18 14:20:12 -07002029 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002031 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002033 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002034 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002036 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002037 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002039 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002040 if (calldata->arg.seqid == NULL)
2041 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002042 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002043 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002044 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002045 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002046 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002047 calldata->roc = roc;
Al Virof6948692010-01-30 13:51:04 -05002048 path_get(path);
2049 calldata->path = *path;
Trond Myklebust95121352005-10-18 14:20:12 -07002050
Trond Myklebust1174dd12010-12-21 10:52:24 -05002051 msg.rpc_argp = &calldata->arg;
2052 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002053 task_setup_data.callback_data = calldata;
2054 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002055 if (IS_ERR(task))
2056 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002057 status = 0;
2058 if (wait)
2059 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002060 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002061 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002062out_free_calldata:
2063 kfree(calldata);
2064out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002065 if (roc)
2066 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002067 nfs4_put_open_state(state);
2068 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002069 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070}
2071
Trond Myklebust2b484292010-09-17 10:56:51 -04002072static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002073nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 struct nfs4_state *state;
2076
Trond Myklebust565277f2007-10-15 18:17:53 -04002077 /* Protect against concurrent sillydeletes */
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002078 state = nfs4_do_open(dir, &ctx->path, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002079 if (IS_ERR(state))
2080 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002081 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002082 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083}
2084
Trond Myklebust1185a552009-12-03 15:54:02 -05002085static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002086{
2087 if (ctx->state == NULL)
2088 return;
2089 if (is_sync)
2090 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2091 else
2092 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2093}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
2095static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2096{
Benny Halevy43652ad2009-04-01 09:21:54 -04002097 struct nfs4_server_caps_arg args = {
2098 .fhandle = fhandle,
2099 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 struct nfs4_server_caps_res res = {};
2101 struct rpc_message msg = {
2102 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002103 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 .rpc_resp = &res,
2105 };
2106 int status;
2107
Bryan Schumaker7c513052011-03-24 17:12:24 +00002108 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 if (status == 0) {
2110 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002111 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2112 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2113 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2114 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2115 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2117 server->caps |= NFS_CAP_ACLS;
2118 if (res.has_links != 0)
2119 server->caps |= NFS_CAP_HARDLINKS;
2120 if (res.has_symlinks != 0)
2121 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002122 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2123 server->caps |= NFS_CAP_FILEID;
2124 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2125 server->caps |= NFS_CAP_MODE;
2126 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2127 server->caps |= NFS_CAP_NLINK;
2128 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2129 server->caps |= NFS_CAP_OWNER;
2130 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2131 server->caps |= NFS_CAP_OWNER_GROUP;
2132 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2133 server->caps |= NFS_CAP_ATIME;
2134 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2135 server->caps |= NFS_CAP_CTIME;
2136 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2137 server->caps |= NFS_CAP_MTIME;
2138
Trond Myklebusta65318b2009-03-11 14:10:28 -04002139 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2140 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2141 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 server->acl_bitmask = res.acl_bitmask;
2143 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002144
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 return status;
2146}
2147
Trond Myklebust55a97592006-06-09 09:34:19 -04002148int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149{
2150 struct nfs4_exception exception = { };
2151 int err;
2152 do {
2153 err = nfs4_handle_exception(server,
2154 _nfs4_server_capabilities(server, fhandle),
2155 &exception);
2156 } while (exception.retry);
2157 return err;
2158}
2159
2160static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2161 struct nfs_fsinfo *info)
2162{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 struct nfs4_lookup_root_arg args = {
2164 .bitmask = nfs4_fattr_bitmap,
2165 };
2166 struct nfs4_lookup_res res = {
2167 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002168 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 .fh = fhandle,
2170 };
2171 struct rpc_message msg = {
2172 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2173 .rpc_argp = &args,
2174 .rpc_resp = &res,
2175 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002176
Trond Myklebust0e574af2005-10-27 22:12:38 -04002177 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002178 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179}
2180
2181static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2182 struct nfs_fsinfo *info)
2183{
2184 struct nfs4_exception exception = { };
2185 int err;
2186 do {
2187 err = nfs4_handle_exception(server,
2188 _nfs4_lookup_root(server, fhandle, info),
2189 &exception);
2190 } while (exception.retry);
2191 return err;
2192}
2193
David Howells54ceac42006-08-22 20:06:13 -04002194/*
2195 * get the file handle for the "/" directory on the server
2196 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002198 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 int status;
2201
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 status = nfs4_lookup_root(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 if (status == 0)
2204 status = nfs4_server_capabilities(server, fhandle);
2205 if (status == 0)
2206 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002207 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208}
2209
Manoj Naik6b97fd32006-06-09 09:34:29 -04002210/*
2211 * Get locations and (maybe) other attributes of a referral.
2212 * Note that we'll actually follow the referral later when
2213 * we detect fsid mismatch in inode revalidation
2214 */
Trond Myklebust56659e92007-07-17 21:52:39 -04002215static 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 -04002216{
2217 int status = -ENOMEM;
2218 struct page *page = NULL;
2219 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002220
2221 page = alloc_page(GFP_KERNEL);
2222 if (page == NULL)
2223 goto out;
2224 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2225 if (locations == NULL)
2226 goto out;
2227
Trond Myklebustc228fd32007-01-13 02:28:11 -05002228 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002229 if (status != 0)
2230 goto out;
2231 /* Make sure server returned a different fsid for the referral */
2232 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07002233 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 -04002234 status = -EIO;
2235 goto out;
2236 }
2237
2238 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2239 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2240 if (!fattr->mode)
2241 fattr->mode = S_IFDIR;
2242 memset(fhandle, 0, sizeof(struct nfs_fh));
2243out:
2244 if (page)
2245 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002246 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002247 return status;
2248}
2249
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2251{
2252 struct nfs4_getattr_arg args = {
2253 .fh = fhandle,
2254 .bitmask = server->attr_bitmask,
2255 };
2256 struct nfs4_getattr_res res = {
2257 .fattr = fattr,
2258 .server = server,
2259 };
2260 struct rpc_message msg = {
2261 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2262 .rpc_argp = &args,
2263 .rpc_resp = &res,
2264 };
2265
Trond Myklebust0e574af2005-10-27 22:12:38 -04002266 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002267 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268}
2269
2270static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2271{
2272 struct nfs4_exception exception = { };
2273 int err;
2274 do {
2275 err = nfs4_handle_exception(server,
2276 _nfs4_proc_getattr(server, fhandle, fattr),
2277 &exception);
2278 } while (exception.retry);
2279 return err;
2280}
2281
2282/*
2283 * The file is not closed if it is opened due to the a request to change
2284 * the size of the file. The open call will not be needed once the
2285 * VFS layer lookup-intents are implemented.
2286 *
2287 * Close is called when the inode is destroyed.
2288 * If we haven't opened the file for O_WRONLY, we
2289 * need to in the size_change case to obtain a stateid.
2290 *
2291 * Got race?
2292 * Because OPEN is always done by name in nfsv4, it is
2293 * possible that we opened a different file by the same
2294 * name. We can recognize this race condition, but we
2295 * can't do anything about it besides returning an error.
2296 *
2297 * This will be fixed with VFS changes (lookup-intent).
2298 */
2299static int
2300nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2301 struct iattr *sattr)
2302{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002303 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002304 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002305 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 int status;
2307
Trond Myklebust0e574af2005-10-27 22:12:38 -04002308 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
Trond Myklebustd5308382005-11-04 15:33:38 -05002310 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002311 if (sattr->ia_valid & ATTR_FILE) {
2312 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002313
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002314 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002315 if (ctx) {
2316 cred = ctx->cred;
2317 state = ctx->state;
2318 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002319 }
2320
2321 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002322 if (status == 0)
2323 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 return status;
2325}
2326
Bryan Schumaker7c513052011-03-24 17:12:24 +00002327static int _nfs4_proc_lookupfh(struct rpc_clnt *clnt, struct nfs_server *server,
2328 const struct nfs_fh *dirfh, const struct qstr *name,
2329 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002330{
2331 int status;
2332 struct nfs4_lookup_arg args = {
2333 .bitmask = server->attr_bitmask,
2334 .dir_fh = dirfh,
2335 .name = name,
2336 };
2337 struct nfs4_lookup_res res = {
2338 .server = server,
2339 .fattr = fattr,
2340 .fh = fhandle,
2341 };
2342 struct rpc_message msg = {
2343 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2344 .rpc_argp = &args,
2345 .rpc_resp = &res,
2346 };
2347
2348 nfs_fattr_init(fattr);
2349
2350 dprintk("NFS call lookupfh %s\n", name->name);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002351 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002352 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002353 return status;
2354}
2355
2356static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2357 struct qstr *name, struct nfs_fh *fhandle,
2358 struct nfs_fattr *fattr)
2359{
2360 struct nfs4_exception exception = { };
2361 int err;
2362 do {
Bryan Schumaker7c513052011-03-24 17:12:24 +00002363 err = _nfs4_proc_lookupfh(server->client, server, dirfh, name, fhandle, fattr);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002364 /* FIXME: !!!! */
2365 if (err == -NFS4ERR_MOVED) {
2366 err = -EREMOTE;
2367 break;
2368 }
2369 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002370 } while (exception.retry);
2371 return err;
2372}
2373
Bryan Schumaker7c513052011-03-24 17:12:24 +00002374static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2375 const struct qstr *name, struct nfs_fh *fhandle,
2376 struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002378 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
2380 dprintk("NFS call lookup %s\n", name->name);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002381 status = _nfs4_proc_lookupfh(clnt, NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002382 if (status == -NFS4ERR_MOVED)
2383 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 dprintk("NFS reply lookup: %d\n", status);
2385 return status;
2386}
2387
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002388void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2389{
2390 memset(fh, 0, sizeof(struct nfs_fh));
2391 fattr->fsid.major = 1;
2392 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2393 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2394 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2395 fattr->nlink = 2;
2396}
2397
Bryan Schumaker7c513052011-03-24 17:12:24 +00002398static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2399 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400{
2401 struct nfs4_exception exception = { };
2402 int err;
2403 do {
2404 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumaker7c513052011-03-24 17:12:24 +00002405 _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 &exception);
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002407 if (err == -EPERM)
2408 nfs_fixup_secinfo_attributes(fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 } while (exception.retry);
2410 return err;
2411}
2412
2413static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2414{
Trond Myklebust76b32992007-08-10 17:45:11 -04002415 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 struct nfs4_accessargs args = {
2417 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002418 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002420 struct nfs4_accessres res = {
2421 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002422 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 struct rpc_message msg = {
2424 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2425 .rpc_argp = &args,
2426 .rpc_resp = &res,
2427 .rpc_cred = entry->cred,
2428 };
2429 int mode = entry->mask;
2430 int status;
2431
2432 /*
2433 * Determine which access bits we want to ask for...
2434 */
2435 if (mode & MAY_READ)
2436 args.access |= NFS4_ACCESS_READ;
2437 if (S_ISDIR(inode->i_mode)) {
2438 if (mode & MAY_WRITE)
2439 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2440 if (mode & MAY_EXEC)
2441 args.access |= NFS4_ACCESS_LOOKUP;
2442 } else {
2443 if (mode & MAY_WRITE)
2444 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2445 if (mode & MAY_EXEC)
2446 args.access |= NFS4_ACCESS_EXECUTE;
2447 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002448
2449 res.fattr = nfs_alloc_fattr();
2450 if (res.fattr == NULL)
2451 return -ENOMEM;
2452
Bryan Schumaker7c513052011-03-24 17:12:24 +00002453 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 if (!status) {
2455 entry->mask = 0;
2456 if (res.access & NFS4_ACCESS_READ)
2457 entry->mask |= MAY_READ;
2458 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2459 entry->mask |= MAY_WRITE;
2460 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2461 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002462 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002464 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 return status;
2466}
2467
2468static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2469{
2470 struct nfs4_exception exception = { };
2471 int err;
2472 do {
2473 err = nfs4_handle_exception(NFS_SERVER(inode),
2474 _nfs4_proc_access(inode, entry),
2475 &exception);
2476 } while (exception.retry);
2477 return err;
2478}
2479
2480/*
2481 * TODO: For the time being, we don't try to get any attributes
2482 * along with any of the zero-copy operations READ, READDIR,
2483 * READLINK, WRITE.
2484 *
2485 * In the case of the first three, we want to put the GETATTR
2486 * after the read-type operation -- this is because it is hard
2487 * to predict the length of a GETATTR response in v4, and thus
2488 * align the READ data correctly. This means that the GETATTR
2489 * may end up partially falling into the page cache, and we should
2490 * shift it into the 'tail' of the xdr_buf before processing.
2491 * To do this efficiently, we need to know the total length
2492 * of data received, which doesn't seem to be available outside
2493 * of the RPC layer.
2494 *
2495 * In the case of WRITE, we also want to put the GETATTR after
2496 * the operation -- in this case because we want to make sure
2497 * we get the post-operation mtime and size. This means that
2498 * we can't use xdr_encode_pages() as written: we need a variant
2499 * of it which would leave room in the 'tail' iovec.
2500 *
2501 * Both of these changes to the XDR layer would in fact be quite
2502 * minor, but I decided to leave them for a subsequent patch.
2503 */
2504static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2505 unsigned int pgbase, unsigned int pglen)
2506{
2507 struct nfs4_readlink args = {
2508 .fh = NFS_FH(inode),
2509 .pgbase = pgbase,
2510 .pglen = pglen,
2511 .pages = &page,
2512 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002513 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 struct rpc_message msg = {
2515 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2516 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002517 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 };
2519
Bryan Schumaker7c513052011-03-24 17:12:24 +00002520 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 -07002521}
2522
2523static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2524 unsigned int pgbase, unsigned int pglen)
2525{
2526 struct nfs4_exception exception = { };
2527 int err;
2528 do {
2529 err = nfs4_handle_exception(NFS_SERVER(inode),
2530 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2531 &exception);
2532 } while (exception.retry);
2533 return err;
2534}
2535
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536/*
2537 * Got race?
2538 * We will need to arrange for the VFS layer to provide an atomic open.
2539 * Until then, this create/open method is prone to inefficiency and race
2540 * conditions due to the lookup, create, and open VFS calls from sys_open()
2541 * placed on the wire.
2542 *
2543 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2544 * The file will be opened again in the subsequent VFS open call
2545 * (nfs4_proc_file_open).
2546 *
2547 * The open for read will just hang around to be used by any process that
2548 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2549 */
2550
2551static int
2552nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002553 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554{
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002555 struct path my_path = {
Trond Myklebustad389da2007-06-05 12:30:00 -04002556 .dentry = dentry,
2557 };
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002558 struct path *path = &my_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002560 struct rpc_cred *cred = NULL;
2561 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 int status = 0;
2563
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002564 if (ctx != NULL) {
2565 cred = ctx->cred;
2566 path = &ctx->path;
2567 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002569 sattr->ia_mode &= ~current_umask();
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002570 state = nfs4_do_open(dir, path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002571 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 if (IS_ERR(state)) {
2573 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002574 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002576 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002577 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002578 if (ctx != NULL)
2579 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002580 else
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002581 nfs4_close_sync(path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582out:
2583 return status;
2584}
2585
2586static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2587{
Trond Myklebust16e42952005-10-27 22:12:44 -04002588 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002589 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002591 .name.len = name->len,
2592 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002593 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002595 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002596 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002597 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002599 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2600 .rpc_argp = &args,
2601 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002603 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
Trond Myklebustd3468902010-04-16 16:22:50 -04002605 res.dir_attr = nfs_alloc_fattr();
2606 if (res.dir_attr == NULL)
2607 goto out;
2608
Bryan Schumaker7c513052011-03-24 17:12:24 +00002609 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002610 if (status == 0) {
2611 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002612 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002613 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002614 nfs_free_fattr(res.dir_attr);
2615out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 return status;
2617}
2618
2619static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2620{
2621 struct nfs4_exception exception = { };
2622 int err;
2623 do {
2624 err = nfs4_handle_exception(NFS_SERVER(dir),
2625 _nfs4_proc_remove(dir, name),
2626 &exception);
2627 } while (exception.retry);
2628 return err;
2629}
2630
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002631static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002633 struct nfs_server *server = NFS_SERVER(dir);
2634 struct nfs_removeargs *args = msg->rpc_argp;
2635 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636
Trond Myklebusta65318b2009-03-11 14:10:28 -04002637 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002638 res->server = server;
Benny Halevydfb4f302010-09-24 09:17:01 -04002639 res->seq_res.sr_slot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641}
2642
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002643static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002645 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2646
Trond Myklebust14516c32010-07-31 14:29:06 -04002647 if (!nfs4_sequence_done(task, &res->seq_res))
2648 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002649 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002650 return 0;
2651 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002652 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002653 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654}
2655
Jeff Laytond3d41522010-09-17 17:31:57 -04002656static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2657{
2658 struct nfs_server *server = NFS_SERVER(dir);
2659 struct nfs_renameargs *arg = msg->rpc_argp;
2660 struct nfs_renameres *res = msg->rpc_resp;
2661
2662 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2663 arg->bitmask = server->attr_bitmask;
2664 res->server = server;
2665}
2666
2667static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2668 struct inode *new_dir)
2669{
2670 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2671
2672 if (!nfs4_sequence_done(task, &res->seq_res))
2673 return 0;
2674 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2675 return 0;
2676
2677 update_changeattr(old_dir, &res->old_cinfo);
2678 nfs_post_op_update_inode(old_dir, res->old_fattr);
2679 update_changeattr(new_dir, &res->new_cinfo);
2680 nfs_post_op_update_inode(new_dir, res->new_fattr);
2681 return 1;
2682}
2683
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2685 struct inode *new_dir, struct qstr *new_name)
2686{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002687 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002688 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 .old_dir = NFS_FH(old_dir),
2690 .new_dir = NFS_FH(new_dir),
2691 .old_name = old_name,
2692 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002693 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002695 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002696 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002697 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 struct rpc_message msg = {
2699 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2700 .rpc_argp = &arg,
2701 .rpc_resp = &res,
2702 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002703 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
Trond Myklebust011fff72010-04-16 16:22:49 -04002705 res.old_fattr = nfs_alloc_fattr();
2706 res.new_fattr = nfs_alloc_fattr();
2707 if (res.old_fattr == NULL || res.new_fattr == NULL)
2708 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709
Bryan Schumaker7c513052011-03-24 17:12:24 +00002710 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 if (!status) {
2712 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002713 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002715 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002717out:
2718 nfs_free_fattr(res.new_fattr);
2719 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 return status;
2721}
2722
2723static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2724 struct inode *new_dir, struct qstr *new_name)
2725{
2726 struct nfs4_exception exception = { };
2727 int err;
2728 do {
2729 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2730 _nfs4_proc_rename(old_dir, old_name,
2731 new_dir, new_name),
2732 &exception);
2733 } while (exception.retry);
2734 return err;
2735}
2736
2737static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2738{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002739 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 struct nfs4_link_arg arg = {
2741 .fh = NFS_FH(inode),
2742 .dir_fh = NFS_FH(dir),
2743 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002744 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002746 struct nfs4_link_res res = {
2747 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002748 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 struct rpc_message msg = {
2750 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2751 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002752 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002754 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755
Trond Myklebust136f2622010-04-16 16:22:49 -04002756 res.fattr = nfs_alloc_fattr();
2757 res.dir_attr = nfs_alloc_fattr();
2758 if (res.fattr == NULL || res.dir_attr == NULL)
2759 goto out;
2760
Bryan Schumaker7c513052011-03-24 17:12:24 +00002761 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002762 if (!status) {
2763 update_changeattr(dir, &res.cinfo);
2764 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002765 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002766 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002767out:
2768 nfs_free_fattr(res.dir_attr);
2769 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 return status;
2771}
2772
2773static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2774{
2775 struct nfs4_exception exception = { };
2776 int err;
2777 do {
2778 err = nfs4_handle_exception(NFS_SERVER(inode),
2779 _nfs4_proc_link(inode, dir, name),
2780 &exception);
2781 } while (exception.retry);
2782 return err;
2783}
2784
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002785struct nfs4_createdata {
2786 struct rpc_message msg;
2787 struct nfs4_create_arg arg;
2788 struct nfs4_create_res res;
2789 struct nfs_fh fh;
2790 struct nfs_fattr fattr;
2791 struct nfs_fattr dir_fattr;
2792};
2793
2794static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2795 struct qstr *name, struct iattr *sattr, u32 ftype)
2796{
2797 struct nfs4_createdata *data;
2798
2799 data = kzalloc(sizeof(*data), GFP_KERNEL);
2800 if (data != NULL) {
2801 struct nfs_server *server = NFS_SERVER(dir);
2802
2803 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2804 data->msg.rpc_argp = &data->arg;
2805 data->msg.rpc_resp = &data->res;
2806 data->arg.dir_fh = NFS_FH(dir);
2807 data->arg.server = server;
2808 data->arg.name = name;
2809 data->arg.attrs = sattr;
2810 data->arg.ftype = ftype;
2811 data->arg.bitmask = server->attr_bitmask;
2812 data->res.server = server;
2813 data->res.fh = &data->fh;
2814 data->res.fattr = &data->fattr;
2815 data->res.dir_fattr = &data->dir_fattr;
2816 nfs_fattr_init(data->res.fattr);
2817 nfs_fattr_init(data->res.dir_fattr);
2818 }
2819 return data;
2820}
2821
2822static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2823{
Bryan Schumaker7c513052011-03-24 17:12:24 +00002824 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00002825 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002826 if (status == 0) {
2827 update_changeattr(dir, &data->res.dir_cinfo);
2828 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2829 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2830 }
2831 return status;
2832}
2833
2834static void nfs4_free_createdata(struct nfs4_createdata *data)
2835{
2836 kfree(data);
2837}
2838
Chuck Lever4f390c12006-08-22 20:06:22 -04002839static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002840 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002842 struct nfs4_createdata *data;
2843 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844
Chuck Lever94a6d752006-08-22 20:06:23 -04002845 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002846 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002847
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002848 status = -ENOMEM;
2849 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2850 if (data == NULL)
2851 goto out;
2852
2853 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2854 data->arg.u.symlink.pages = &page;
2855 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002857 status = nfs4_do_create(dir, dentry, data);
2858
2859 nfs4_free_createdata(data);
2860out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 return status;
2862}
2863
Chuck Lever4f390c12006-08-22 20:06:22 -04002864static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002865 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866{
2867 struct nfs4_exception exception = { };
2868 int err;
2869 do {
2870 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002871 _nfs4_proc_symlink(dir, dentry, page,
2872 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 &exception);
2874 } while (exception.retry);
2875 return err;
2876}
2877
2878static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2879 struct iattr *sattr)
2880{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002881 struct nfs4_createdata *data;
2882 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002884 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2885 if (data == NULL)
2886 goto out;
2887
2888 status = nfs4_do_create(dir, dentry, data);
2889
2890 nfs4_free_createdata(data);
2891out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 return status;
2893}
2894
2895static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2896 struct iattr *sattr)
2897{
2898 struct nfs4_exception exception = { };
2899 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002900
2901 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 do {
2903 err = nfs4_handle_exception(NFS_SERVER(dir),
2904 _nfs4_proc_mkdir(dir, dentry, sattr),
2905 &exception);
2906 } while (exception.retry);
2907 return err;
2908}
2909
2910static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002911 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912{
2913 struct inode *dir = dentry->d_inode;
2914 struct nfs4_readdir_arg args = {
2915 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002916 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 .pgbase = 0,
2918 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002919 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04002920 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 };
2922 struct nfs4_readdir_res res;
2923 struct rpc_message msg = {
2924 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2925 .rpc_argp = &args,
2926 .rpc_resp = &res,
2927 .rpc_cred = cred,
2928 };
2929 int status;
2930
Harvey Harrison3110ff82008-05-02 13:42:44 -07002931 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002932 dentry->d_parent->d_name.name,
2933 dentry->d_name.name,
2934 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2936 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00002937 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 -05002938 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05002940 status += args.pgbase;
2941 }
Trond Myklebustc4812992007-09-28 17:11:45 -04002942
2943 nfs_invalidate_atime(dir);
2944
Harvey Harrison3110ff82008-05-02 13:42:44 -07002945 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 return status;
2947}
2948
2949static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002950 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951{
2952 struct nfs4_exception exception = { };
2953 int err;
2954 do {
2955 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2956 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002957 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 &exception);
2959 } while (exception.retry);
2960 return err;
2961}
2962
2963static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2964 struct iattr *sattr, dev_t rdev)
2965{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002966 struct nfs4_createdata *data;
2967 int mode = sattr->ia_mode;
2968 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969
2970 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
2971 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002972
2973 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
2974 if (data == NULL)
2975 goto out;
2976
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002978 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002980 data->arg.ftype = NF4BLK;
2981 data->arg.u.device.specdata1 = MAJOR(rdev);
2982 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 }
2984 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002985 data->arg.ftype = NF4CHR;
2986 data->arg.u.device.specdata1 = MAJOR(rdev);
2987 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002990 status = nfs4_do_create(dir, dentry, data);
2991
2992 nfs4_free_createdata(data);
2993out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 return status;
2995}
2996
2997static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
2998 struct iattr *sattr, dev_t rdev)
2999{
3000 struct nfs4_exception exception = { };
3001 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003002
3003 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 do {
3005 err = nfs4_handle_exception(NFS_SERVER(dir),
3006 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3007 &exception);
3008 } while (exception.retry);
3009 return err;
3010}
3011
3012static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3013 struct nfs_fsstat *fsstat)
3014{
3015 struct nfs4_statfs_arg args = {
3016 .fh = fhandle,
3017 .bitmask = server->attr_bitmask,
3018 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003019 struct nfs4_statfs_res res = {
3020 .fsstat = fsstat,
3021 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 struct rpc_message msg = {
3023 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3024 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003025 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 };
3027
Trond Myklebust0e574af2005-10-27 22:12:38 -04003028 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003029 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030}
3031
3032static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3033{
3034 struct nfs4_exception exception = { };
3035 int err;
3036 do {
3037 err = nfs4_handle_exception(server,
3038 _nfs4_proc_statfs(server, fhandle, fsstat),
3039 &exception);
3040 } while (exception.retry);
3041 return err;
3042}
3043
3044static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3045 struct nfs_fsinfo *fsinfo)
3046{
3047 struct nfs4_fsinfo_arg args = {
3048 .fh = fhandle,
3049 .bitmask = server->attr_bitmask,
3050 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003051 struct nfs4_fsinfo_res res = {
3052 .fsinfo = fsinfo,
3053 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 struct rpc_message msg = {
3055 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3056 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003057 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 };
3059
Bryan Schumaker7c513052011-03-24 17:12:24 +00003060 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061}
3062
3063static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3064{
3065 struct nfs4_exception exception = { };
3066 int err;
3067
3068 do {
3069 err = nfs4_handle_exception(server,
3070 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3071 &exception);
3072 } while (exception.retry);
3073 return err;
3074}
3075
3076static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3077{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003078 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3080}
3081
3082static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3083 struct nfs_pathconf *pathconf)
3084{
3085 struct nfs4_pathconf_arg args = {
3086 .fh = fhandle,
3087 .bitmask = server->attr_bitmask,
3088 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003089 struct nfs4_pathconf_res res = {
3090 .pathconf = pathconf,
3091 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 struct rpc_message msg = {
3093 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3094 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003095 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 };
3097
3098 /* None of the pathconf attributes are mandatory to implement */
3099 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3100 memset(pathconf, 0, sizeof(*pathconf));
3101 return 0;
3102 }
3103
Trond Myklebust0e574af2005-10-27 22:12:38 -04003104 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003105 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106}
3107
3108static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3109 struct nfs_pathconf *pathconf)
3110{
3111 struct nfs4_exception exception = { };
3112 int err;
3113
3114 do {
3115 err = nfs4_handle_exception(server,
3116 _nfs4_proc_pathconf(server, fhandle, pathconf),
3117 &exception);
3118 } while (exception.retry);
3119 return err;
3120}
3121
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003122static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123{
Trond Myklebustec06c092006-03-20 13:44:27 -05003124 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003126 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003127 nfs_restart_rpc(task, server->nfs_client);
Trond Myklebustec06c092006-03-20 13:44:27 -05003128 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003130
3131 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003133 renew_lease(server, data->timestamp);
3134 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135}
3136
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003137static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3138{
3139
3140 dprintk("--> %s\n", __func__);
3141
3142 if (!nfs4_sequence_done(task, &data->res.seq_res))
3143 return -EAGAIN;
3144
3145 return data->read_done_cb(task, data);
3146}
3147
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003148static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003151 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003152 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153}
3154
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003155/* Reset the the nfs_read_data to send the read to the MDS. */
3156void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3157{
3158 dprintk("%s Reset task for i/o through\n", __func__);
3159 put_lseg(data->lseg);
3160 data->lseg = NULL;
3161 /* offsets will differ in the dense stripe case */
3162 data->args.offset = data->mds_offset;
3163 data->ds_clp = NULL;
3164 data->args.fh = NFS_FH(data->inode);
3165 data->read_done_cb = nfs4_read_done_cb;
3166 task->tk_ops = data->mds_ops;
3167 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3168}
3169EXPORT_SYMBOL_GPL(nfs4_reset_read);
3170
Fred Isamanb029bc92011-03-03 15:13:42 +00003171static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 struct inode *inode = data->inode;
3174
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003175 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003176 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003177 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003179 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003181 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003182 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003183 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184}
3185
Fred Isamanb029bc92011-03-03 15:13:42 +00003186static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3187{
3188 if (!nfs4_sequence_done(task, &data->res.seq_res))
3189 return -EAGAIN;
3190 return data->write_done_cb(task, data);
3191}
3192
Fred Isamana69aef12011-03-03 15:13:47 +00003193/* Reset the the nfs_write_data to send the write to the MDS. */
3194void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3195{
3196 dprintk("%s Reset task for i/o through\n", __func__);
3197 put_lseg(data->lseg);
3198 data->lseg = NULL;
3199 data->ds_clp = NULL;
3200 data->write_done_cb = nfs4_write_done_cb;
3201 data->args.fh = NFS_FH(data->inode);
3202 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3203 data->args.offset = data->mds_offset;
3204 data->res.fattr = &data->fattr;
3205 task->tk_ops = data->mds_ops;
3206 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3207}
3208EXPORT_SYMBOL_GPL(nfs4_reset_write);
3209
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003210static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003212 struct nfs_server *server = NFS_SERVER(data->inode);
3213
Fred Isaman7ffd1062011-03-03 15:13:46 +00003214 if (data->lseg) {
3215 data->args.bitmask = NULL;
3216 data->res.fattr = NULL;
3217 } else
3218 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isamanb029bc92011-03-03 15:13:42 +00003219 if (!data->write_done_cb)
3220 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003221 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 data->timestamp = jiffies;
3223
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003224 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225}
3226
Trond Myklebust788e7a82006-03-20 13:44:27 -05003227static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 struct inode *inode = data->inode;
3230
Trond Myklebust14516c32010-07-31 14:29:06 -04003231 if (!nfs4_sequence_done(task, &data->res.seq_res))
3232 return -EAGAIN;
3233
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003234 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003235 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003236 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003238 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003239 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240}
3241
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003242static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003244 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245
Trond Myklebusta65318b2009-03-11 14:10:28 -04003246 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003247 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003248 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249}
3250
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003251struct nfs4_renewdata {
3252 struct nfs_client *client;
3253 unsigned long timestamp;
3254};
3255
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256/*
3257 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3258 * standalone procedure for queueing an asynchronous RENEW.
3259 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003260static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003261{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003262 struct nfs4_renewdata *data = calldata;
3263 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003264
Alexandros Batsakis0851de02010-02-05 03:45:06 -08003265 if (atomic_read(&clp->cl_count) > 1)
3266 nfs4_schedule_state_renewal(clp);
3267 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003268 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003269}
3270
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003271static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003273 struct nfs4_renewdata *data = calldata;
3274 struct nfs_client *clp = data->client;
3275 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276
3277 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003278 /* Unless we're shutting down, schedule state recovery! */
3279 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003280 nfs4_schedule_lease_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 return;
3282 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003283 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284}
3285
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003286static const struct rpc_call_ops nfs4_renew_ops = {
3287 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003288 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003289};
3290
David Howellsadfa6f92006-08-22 20:06:08 -04003291int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292{
3293 struct rpc_message msg = {
3294 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3295 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003296 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003298 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299
Alexandros Batsakis0851de02010-02-05 03:45:06 -08003300 if (!atomic_inc_not_zero(&clp->cl_count))
3301 return -EIO;
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003302 data = kmalloc(sizeof(*data), GFP_KERNEL);
3303 if (data == NULL)
3304 return -ENOMEM;
3305 data->client = clp;
3306 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003308 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309}
3310
David Howellsadfa6f92006-08-22 20:06:08 -04003311int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312{
3313 struct rpc_message msg = {
3314 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3315 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003316 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 };
3318 unsigned long now = jiffies;
3319 int status;
3320
3321 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3322 if (status < 0)
3323 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003324 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 return 0;
3326}
3327
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003328static inline int nfs4_server_supports_acls(struct nfs_server *server)
3329{
3330 return (server->caps & NFS_CAP_ACLS)
3331 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3332 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3333}
3334
3335/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3336 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3337 * the stack.
3338 */
3339#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3340
3341static void buf_to_pages(const void *buf, size_t buflen,
3342 struct page **pages, unsigned int *pgbase)
3343{
3344 const void *p = buf;
3345
3346 *pgbase = offset_in_page(buf);
3347 p -= *pgbase;
3348 while (p < buf + buflen) {
3349 *(pages++) = virt_to_page(p);
3350 p += PAGE_CACHE_SIZE;
3351 }
3352}
3353
Neil Hormane9e3d722011-03-04 19:26:03 -05003354static int buf_to_pages_noslab(const void *buf, size_t buflen,
3355 struct page **pages, unsigned int *pgbase)
3356{
3357 struct page *newpage, **spages;
3358 int rc = 0;
3359 size_t len;
3360 spages = pages;
3361
3362 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003363 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003364 newpage = alloc_page(GFP_KERNEL);
3365
3366 if (newpage == NULL)
3367 goto unwind;
3368 memcpy(page_address(newpage), buf, len);
3369 buf += len;
3370 buflen -= len;
3371 *pages++ = newpage;
3372 rc++;
3373 } while (buflen != 0);
3374
3375 return rc;
3376
3377unwind:
3378 for(; rc > 0; rc--)
3379 __free_page(spages[rc-1]);
3380 return -ENOMEM;
3381}
3382
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003383struct nfs4_cached_acl {
3384 int cached;
3385 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003386 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003387};
3388
3389static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003390{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003391 struct nfs_inode *nfsi = NFS_I(inode);
3392
3393 spin_lock(&inode->i_lock);
3394 kfree(nfsi->nfs4_acl);
3395 nfsi->nfs4_acl = acl;
3396 spin_unlock(&inode->i_lock);
3397}
3398
3399static void nfs4_zap_acl_attr(struct inode *inode)
3400{
3401 nfs4_set_cached_acl(inode, NULL);
3402}
3403
3404static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3405{
3406 struct nfs_inode *nfsi = NFS_I(inode);
3407 struct nfs4_cached_acl *acl;
3408 int ret = -ENOENT;
3409
3410 spin_lock(&inode->i_lock);
3411 acl = nfsi->nfs4_acl;
3412 if (acl == NULL)
3413 goto out;
3414 if (buf == NULL) /* user is just asking for length */
3415 goto out_len;
3416 if (acl->cached == 0)
3417 goto out;
3418 ret = -ERANGE; /* see getxattr(2) man page */
3419 if (acl->len > buflen)
3420 goto out;
3421 memcpy(buf, acl->data, acl->len);
3422out_len:
3423 ret = acl->len;
3424out:
3425 spin_unlock(&inode->i_lock);
3426 return ret;
3427}
3428
3429static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3430{
3431 struct nfs4_cached_acl *acl;
3432
3433 if (buf && acl_len <= PAGE_SIZE) {
3434 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3435 if (acl == NULL)
3436 goto out;
3437 acl->cached = 1;
3438 memcpy(acl->data, buf, acl_len);
3439 } else {
3440 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3441 if (acl == NULL)
3442 goto out;
3443 acl->cached = 0;
3444 }
3445 acl->len = acl_len;
3446out:
3447 nfs4_set_cached_acl(inode, acl);
3448}
3449
Trond Myklebust16b42892006-08-24 12:27:15 -04003450static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003451{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003452 struct page *pages[NFS4ACL_MAXPAGES];
3453 struct nfs_getaclargs args = {
3454 .fh = NFS_FH(inode),
3455 .acl_pages = pages,
3456 .acl_len = buflen,
3457 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003458 struct nfs_getaclres res = {
3459 .acl_len = buflen,
3460 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003461 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003462 struct rpc_message msg = {
3463 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3464 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003465 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003466 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003467 struct page *localpage = NULL;
3468 int ret;
3469
3470 if (buflen < PAGE_SIZE) {
3471 /* As long as we're doing a round trip to the server anyway,
3472 * let's be prepared for a page of acl data. */
3473 localpage = alloc_page(GFP_KERNEL);
3474 resp_buf = page_address(localpage);
3475 if (localpage == NULL)
3476 return -ENOMEM;
3477 args.acl_pages[0] = localpage;
3478 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003479 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003480 } else {
3481 resp_buf = buf;
3482 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3483 }
Bryan Schumaker7c513052011-03-24 17:12:24 +00003484 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 +00003485 if (ret)
3486 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003487 if (res.acl_len > args.acl_len)
3488 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003489 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003490 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003491 if (buf) {
3492 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003493 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003494 goto out_free;
3495 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003496 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003497 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003498 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003499out_free:
3500 if (localpage)
3501 __free_page(localpage);
3502 return ret;
3503}
3504
Trond Myklebust16b42892006-08-24 12:27:15 -04003505static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3506{
3507 struct nfs4_exception exception = { };
3508 ssize_t ret;
3509 do {
3510 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3511 if (ret >= 0)
3512 break;
3513 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3514 } while (exception.retry);
3515 return ret;
3516}
3517
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003518static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3519{
3520 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003521 int ret;
3522
3523 if (!nfs4_server_supports_acls(server))
3524 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003525 ret = nfs_revalidate_inode(server, inode);
3526 if (ret < 0)
3527 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003528 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3529 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003530 ret = nfs4_read_cached_acl(inode, buf, buflen);
3531 if (ret != -ENOENT)
3532 return ret;
3533 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003534}
3535
Trond Myklebust16b42892006-08-24 12:27:15 -04003536static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003537{
3538 struct nfs_server *server = NFS_SERVER(inode);
3539 struct page *pages[NFS4ACL_MAXPAGES];
3540 struct nfs_setaclargs arg = {
3541 .fh = NFS_FH(inode),
3542 .acl_pages = pages,
3543 .acl_len = buflen,
3544 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003545 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003546 struct rpc_message msg = {
3547 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3548 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003549 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003550 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003551 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003552
3553 if (!nfs4_server_supports_acls(server))
3554 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003555 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3556 if (i < 0)
3557 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003558 nfs_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003559 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003560
3561 /*
3562 * Free each page after tx, so the only ref left is
3563 * held by the network stack
3564 */
3565 for (; i > 0; i--)
3566 put_page(pages[i-1]);
3567
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003568 /*
3569 * Acl update can result in inode attribute update.
3570 * so mark the attribute cache invalid.
3571 */
3572 spin_lock(&inode->i_lock);
3573 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3574 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003575 nfs_access_zap_cache(inode);
3576 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003577 return ret;
3578}
3579
Trond Myklebust16b42892006-08-24 12:27:15 -04003580static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3581{
3582 struct nfs4_exception exception = { };
3583 int err;
3584 do {
3585 err = nfs4_handle_exception(NFS_SERVER(inode),
3586 __nfs4_proc_set_acl(inode, buf, buflen),
3587 &exception);
3588 } while (exception.retry);
3589 return err;
3590}
3591
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003593nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003595 struct nfs_client *clp = server->nfs_client;
3596
3597 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 return 0;
3599 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003600 case -NFS4ERR_ADMIN_REVOKED:
3601 case -NFS4ERR_BAD_STATEID:
3602 case -NFS4ERR_OPENMODE:
3603 if (state == NULL)
3604 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003605 nfs4_schedule_stateid_recovery(server, state);
3606 goto wait_on_recovery;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003608 case -NFS4ERR_STALE_CLIENTID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 case -NFS4ERR_EXPIRED:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003610 nfs4_schedule_lease_recovery(clp);
3611 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003612#if defined(CONFIG_NFS_V4_1)
3613 case -NFS4ERR_BADSESSION:
3614 case -NFS4ERR_BADSLOT:
3615 case -NFS4ERR_BAD_HIGH_SLOT:
3616 case -NFS4ERR_DEADSESSION:
3617 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3618 case -NFS4ERR_SEQ_FALSE_RETRY:
3619 case -NFS4ERR_SEQ_MISORDERED:
3620 dprintk("%s ERROR %d, Reset session\n", __func__,
3621 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003622 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -04003623 task->tk_status = 0;
3624 return -EAGAIN;
3625#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003627 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003628 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003629 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3631 task->tk_status = 0;
3632 return -EAGAIN;
3633 case -NFS4ERR_OLD_STATEID:
3634 task->tk_status = 0;
3635 return -EAGAIN;
3636 }
3637 task->tk_status = nfs4_map_errors(task->tk_status);
3638 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003639wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003640 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003641 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3642 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3643 task->tk_status = 0;
3644 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645}
3646
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003647int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3648 unsigned short port, struct rpc_cred *cred,
3649 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650{
3651 nfs4_verifier sc_verifier;
3652 struct nfs4_setclientid setclientid = {
3653 .sc_verifier = &sc_verifier,
3654 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003655 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 };
3657 struct rpc_message msg = {
3658 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3659 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003660 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003661 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 };
Al Virobc4785c2006-10-19 23:28:51 -07003663 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 int loop = 0;
3665 int status;
3666
Al Virobc4785c2006-10-19 23:28:51 -07003667 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3669 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3670
3671 for(;;) {
3672 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003673 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003674 clp->cl_ipaddr,
3675 rpc_peeraddr2str(clp->cl_rpcclient,
3676 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003677 rpc_peeraddr2str(clp->cl_rpcclient,
3678 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003679 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 clp->cl_id_uniquifier);
3681 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003682 sizeof(setclientid.sc_netid),
3683 rpc_peeraddr2str(clp->cl_rpcclient,
3684 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003686 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 clp->cl_ipaddr, port >> 8, port & 255);
3688
3689 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3690 if (status != -NFS4ERR_CLID_INUSE)
3691 break;
3692 if (signalled())
3693 break;
3694 if (loop++ & 1)
J. Bruce Fields611c96c2010-12-13 19:05:46 -05003695 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 else
3697 if (++clp->cl_id_uniquifier == 0)
3698 break;
3699 }
3700 return status;
3701}
3702
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003703static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3704 struct nfs4_setclientid_res *arg,
3705 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706{
3707 struct nfs_fsinfo fsinfo;
3708 struct rpc_message msg = {
3709 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003710 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003712 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 };
3714 unsigned long now;
3715 int status;
3716
3717 now = jiffies;
3718 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3719 if (status == 0) {
3720 spin_lock(&clp->cl_lock);
3721 clp->cl_lease_time = fsinfo.lease_time * HZ;
3722 clp->cl_last_renewal = now;
3723 spin_unlock(&clp->cl_lock);
3724 }
3725 return status;
3726}
3727
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003728int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3729 struct nfs4_setclientid_res *arg,
3730 struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003731{
Benny Halevy77e03672008-06-24 20:25:57 +03003732 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003733 int err;
3734 do {
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003735 err = _nfs4_proc_setclientid_confirm(clp, arg, cred);
Trond Myklebust51581f32006-03-20 13:44:47 -05003736 switch (err) {
3737 case 0:
3738 return err;
3739 case -NFS4ERR_RESOURCE:
3740 /* The IBM lawyers misread another document! */
3741 case -NFS4ERR_DELAY:
3742 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3743 }
3744 } while (err == 0);
3745 return err;
3746}
3747
Trond Myklebustfe650402006-01-03 09:55:18 +01003748struct nfs4_delegreturndata {
3749 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003750 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003751 struct nfs_fh fh;
3752 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003753 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003754 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003755 int rpc_status;
3756};
3757
Trond Myklebustfe650402006-01-03 09:55:18 +01003758static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3759{
3760 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003761
Trond Myklebust14516c32010-07-31 14:29:06 -04003762 if (!nfs4_sequence_done(task, &data->res.seq_res))
3763 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003764
Ricardo Labiaga79708862009-12-07 09:23:21 -05003765 switch (task->tk_status) {
3766 case -NFS4ERR_STALE_STATEID:
3767 case -NFS4ERR_EXPIRED:
3768 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003769 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003770 break;
3771 default:
3772 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3773 -EAGAIN) {
3774 nfs_restart_rpc(task, data->res.server->nfs_client);
3775 return;
3776 }
3777 }
3778 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003779}
3780
3781static void nfs4_delegreturn_release(void *calldata)
3782{
Trond Myklebustfe650402006-01-03 09:55:18 +01003783 kfree(calldata);
3784}
3785
Andy Adamson938e1012009-04-01 09:22:28 -04003786#if defined(CONFIG_NFS_V4_1)
3787static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3788{
3789 struct nfs4_delegreturndata *d_data;
3790
3791 d_data = (struct nfs4_delegreturndata *)data;
3792
Trond Myklebust035168a2010-06-16 09:52:26 -04003793 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003794 &d_data->args.seq_args,
3795 &d_data->res.seq_res, 1, task))
3796 return;
3797 rpc_call_start(task);
3798}
3799#endif /* CONFIG_NFS_V4_1 */
3800
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003801static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003802#if defined(CONFIG_NFS_V4_1)
3803 .rpc_call_prepare = nfs4_delegreturn_prepare,
3804#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003805 .rpc_call_done = nfs4_delegreturn_done,
3806 .rpc_release = nfs4_delegreturn_release,
3807};
3808
Trond Myklebuste6f81072008-01-24 18:14:34 -05003809static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003810{
3811 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003812 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003813 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003814 struct rpc_message msg = {
3815 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3816 .rpc_cred = cred,
3817 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003818 struct rpc_task_setup task_setup_data = {
3819 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003820 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003821 .callback_ops = &nfs4_delegreturn_ops,
3822 .flags = RPC_TASK_ASYNC,
3823 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003824 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003825
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003826 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003827 if (data == NULL)
3828 return -ENOMEM;
3829 data->args.fhandle = &data->fh;
3830 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003831 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003832 nfs_copy_fh(&data->fh, NFS_FH(inode));
3833 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003834 data->res.fattr = &data->fattr;
3835 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003836 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003837 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003838 data->rpc_status = 0;
3839
Trond Myklebustc970aa82007-07-14 15:39:59 -04003840 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05003841 msg.rpc_argp = &data->args;
3842 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003843 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003844 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003845 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003846 if (!issync)
3847 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003848 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003849 if (status != 0)
3850 goto out;
3851 status = data->rpc_status;
3852 if (status != 0)
3853 goto out;
3854 nfs_refresh_inode(inode, &data->fattr);
3855out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003856 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003857 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858}
3859
Trond Myklebuste6f81072008-01-24 18:14:34 -05003860int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861{
3862 struct nfs_server *server = NFS_SERVER(inode);
3863 struct nfs4_exception exception = { };
3864 int err;
3865 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003866 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 switch (err) {
3868 case -NFS4ERR_STALE_STATEID:
3869 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 case 0:
3871 return 0;
3872 }
3873 err = nfs4_handle_exception(server, err, &exception);
3874 } while (exception.retry);
3875 return err;
3876}
3877
3878#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3879#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3880
3881/*
3882 * sleep, with exponential backoff, and retry the LOCK operation.
3883 */
3884static unsigned long
3885nfs4_set_lock_task_retry(unsigned long timeout)
3886{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003887 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 timeout <<= 1;
3889 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3890 return NFS4_LOCK_MAXTIMEOUT;
3891 return timeout;
3892}
3893
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3895{
3896 struct inode *inode = state->inode;
3897 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003898 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003899 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003901 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003903 struct nfs_lockt_res res = {
3904 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 };
3906 struct rpc_message msg = {
3907 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3908 .rpc_argp = &arg,
3909 .rpc_resp = &res,
3910 .rpc_cred = state->owner->so_cred,
3911 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 struct nfs4_lock_state *lsp;
3913 int status;
3914
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003915 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003916 status = nfs4_set_lock_state(state, request);
3917 if (status != 0)
3918 goto out;
3919 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003920 arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05003921 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003922 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003923 switch (status) {
3924 case 0:
3925 request->fl_type = F_UNLCK;
3926 break;
3927 case -NFS4ERR_DENIED:
3928 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003930 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003931out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 return status;
3933}
3934
3935static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3936{
3937 struct nfs4_exception exception = { };
3938 int err;
3939
3940 do {
3941 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3942 _nfs4_proc_getlk(state, cmd, request),
3943 &exception);
3944 } while (exception.retry);
3945 return err;
3946}
3947
3948static int do_vfs_lock(struct file *file, struct file_lock *fl)
3949{
3950 int res = 0;
3951 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3952 case FL_POSIX:
3953 res = posix_lock_file_wait(file, fl);
3954 break;
3955 case FL_FLOCK:
3956 res = flock_lock_file_wait(file, fl);
3957 break;
3958 default:
3959 BUG();
3960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 return res;
3962}
3963
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003964struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003965 struct nfs_locku_args arg;
3966 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003967 struct nfs4_lock_state *lsp;
3968 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003969 struct file_lock fl;
3970 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003971 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07003972};
3973
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003974static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
3975 struct nfs_open_context *ctx,
3976 struct nfs4_lock_state *lsp,
3977 struct nfs_seqid *seqid)
3978{
3979 struct nfs4_unlockdata *p;
3980 struct inode *inode = lsp->ls_state->inode;
3981
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003982 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003983 if (p == NULL)
3984 return NULL;
3985 p->arg.fh = NFS_FH(inode);
3986 p->arg.fl = &p->fl;
3987 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003988 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003989 p->arg.stateid = &lsp->ls_stateid;
3990 p->lsp = lsp;
3991 atomic_inc(&lsp->ls_count);
3992 /* Ensure we don't close file until we're done freeing locks! */
3993 p->ctx = get_nfs_open_context(ctx);
3994 memcpy(&p->fl, fl, sizeof(p->fl));
3995 p->server = NFS_SERVER(inode);
3996 return p;
3997}
3998
Trond Myklebust06f814a2006-01-03 09:55:07 +01003999static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004000{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004001 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004002 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004003 nfs4_put_lock_state(calldata->lsp);
4004 put_nfs_open_context(calldata->ctx);
4005 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004006}
4007
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004008static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004009{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004010 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004011
Trond Myklebust14516c32010-07-31 14:29:06 -04004012 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4013 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004014 switch (task->tk_status) {
4015 case 0:
4016 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004017 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004018 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01004019 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004020 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004021 case -NFS4ERR_BAD_STATEID:
4022 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004023 case -NFS4ERR_STALE_STATEID:
4024 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004025 break;
4026 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004027 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebust0110ee12009-12-07 09:00:24 -05004028 nfs_restart_rpc(task,
Trond Myklebustd61e6122009-12-05 19:32:19 -05004029 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004030 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004031}
4032
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004033static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004034{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004035 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004037 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004038 return;
4039 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004040 /* Note: exit _without_ running nfs4_locku_done */
4041 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004042 return;
4043 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004044 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004045 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004046 &calldata->arg.seq_args,
4047 &calldata->res.seq_res, 1, task))
4048 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004049 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050}
4051
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004052static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004053 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004054 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004055 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004056};
4057
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004058static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4059 struct nfs_open_context *ctx,
4060 struct nfs4_lock_state *lsp,
4061 struct nfs_seqid *seqid)
4062{
4063 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004064 struct rpc_message msg = {
4065 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4066 .rpc_cred = ctx->cred,
4067 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004068 struct rpc_task_setup task_setup_data = {
4069 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004070 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004071 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004072 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004073 .flags = RPC_TASK_ASYNC,
4074 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004075
Frank Filz137d6ac2007-07-09 15:32:29 -07004076 /* Ensure this is an unlock - when canceling a lock, the
4077 * canceled lock is passed in, and it won't be an unlock.
4078 */
4079 fl->fl_type = F_UNLCK;
4080
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004081 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4082 if (data == NULL) {
4083 nfs_free_seqid(seqid);
4084 return ERR_PTR(-ENOMEM);
4085 }
4086
Trond Myklebust1174dd12010-12-21 10:52:24 -05004087 msg.rpc_argp = &data->arg;
4088 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004089 task_setup_data.callback_data = data;
4090 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004091}
4092
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4094{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004095 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004096 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004097 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004098 struct rpc_task *task;
4099 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004100 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101
Trond Myklebust9b073572006-06-29 16:38:34 -04004102 status = nfs4_set_lock_state(state, request);
4103 /* Unlock _before_ we do the RPC call */
4104 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004105 down_read(&nfsi->rwsem);
4106 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4107 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004108 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004109 }
4110 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004111 if (status != 0)
4112 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004113 /* Is this a delegated lock? */
4114 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004115 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004116 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004117 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004118 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004119 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004120 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004121 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004122 status = PTR_ERR(task);
4123 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004124 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004125 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004126 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004127out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004128 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004129 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130}
4131
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004132struct nfs4_lockdata {
4133 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004134 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004135 struct nfs4_lock_state *lsp;
4136 struct nfs_open_context *ctx;
4137 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004138 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004139 int rpc_status;
4140 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004141 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004142};
4143
4144static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004145 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4146 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004147{
4148 struct nfs4_lockdata *p;
4149 struct inode *inode = lsp->ls_state->inode;
4150 struct nfs_server *server = NFS_SERVER(inode);
4151
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004152 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004153 if (p == NULL)
4154 return NULL;
4155
4156 p->arg.fh = NFS_FH(inode);
4157 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004158 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004159 if (p->arg.open_seqid == NULL)
4160 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004161 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004162 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004163 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004164 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004165 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004166 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004167 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004168 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004169 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004170 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004171 atomic_inc(&lsp->ls_count);
4172 p->ctx = get_nfs_open_context(ctx);
4173 memcpy(&p->fl, fl, sizeof(p->fl));
4174 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004175out_free_seqid:
4176 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004177out_free:
4178 kfree(p);
4179 return NULL;
4180}
4181
4182static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4183{
4184 struct nfs4_lockdata *data = calldata;
4185 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186
Harvey Harrison3110ff82008-05-02 13:42:44 -07004187 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004188 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4189 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004190 /* Do we need to do an open_to_lock_owner? */
4191 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004192 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4193 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004194 data->arg.open_stateid = &state->stateid;
4195 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004196 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004197 } else
4198 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004199 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004200 if (nfs4_setup_sequence(data->server,
4201 &data->arg.seq_args,
Andy Adamson66179ef2009-04-01 09:22:22 -04004202 &data->res.seq_res, 1, task))
4203 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004204 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004205 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004206}
4207
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004208static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4209{
4210 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4211 nfs4_lock_prepare(task, calldata);
4212}
4213
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004214static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4215{
4216 struct nfs4_lockdata *data = calldata;
4217
Harvey Harrison3110ff82008-05-02 13:42:44 -07004218 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004219
Trond Myklebust14516c32010-07-31 14:29:06 -04004220 if (!nfs4_sequence_done(task, &data->res.seq_res))
4221 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004222
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004223 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004224 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004225 if (data->rpc_status == 0)
4226 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4227 else
4228 goto out;
4229 }
4230 if (data->rpc_status == 0) {
4231 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4232 sizeof(data->lsp->ls_stateid.data));
4233 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04004234 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004235 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004236out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004237 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004238}
4239
4240static void nfs4_lock_release(void *calldata)
4241{
4242 struct nfs4_lockdata *data = calldata;
4243
Harvey Harrison3110ff82008-05-02 13:42:44 -07004244 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004245 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004246 if (data->cancelled != 0) {
4247 struct rpc_task *task;
4248 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4249 data->arg.lock_seqid);
4250 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004251 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004252 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004253 } else
4254 nfs_free_seqid(data->arg.lock_seqid);
4255 nfs4_put_lock_state(data->lsp);
4256 put_nfs_open_context(data->ctx);
4257 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004258 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004259}
4260
4261static const struct rpc_call_ops nfs4_lock_ops = {
4262 .rpc_call_prepare = nfs4_lock_prepare,
4263 .rpc_call_done = nfs4_lock_done,
4264 .rpc_release = nfs4_lock_release,
4265};
4266
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004267static const struct rpc_call_ops nfs4_recover_lock_ops = {
4268 .rpc_call_prepare = nfs4_recover_lock_prepare,
4269 .rpc_call_done = nfs4_lock_done,
4270 .rpc_release = nfs4_lock_release,
4271};
4272
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004273static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4274{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004275 switch (error) {
4276 case -NFS4ERR_ADMIN_REVOKED:
4277 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004278 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004279 if (new_lock_owner != 0 ||
4280 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004281 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004282 break;
4283 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004284 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004285 case -NFS4ERR_EXPIRED:
4286 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004287 };
4288}
4289
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004290static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004291{
4292 struct nfs4_lockdata *data;
4293 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004294 struct rpc_message msg = {
4295 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4296 .rpc_cred = state->owner->so_cred,
4297 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004298 struct rpc_task_setup task_setup_data = {
4299 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004300 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004301 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004302 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004303 .flags = RPC_TASK_ASYNC,
4304 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004305 int ret;
4306
Harvey Harrison3110ff82008-05-02 13:42:44 -07004307 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004308 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004309 fl->fl_u.nfs4_fl.owner,
4310 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004311 if (data == NULL)
4312 return -ENOMEM;
4313 if (IS_SETLKW(cmd))
4314 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004315 if (recovery_type > NFS_LOCK_NEW) {
4316 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004317 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004318 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4319 }
Trond Myklebust1174dd12010-12-21 10:52:24 -05004320 msg.rpc_argp = &data->arg;
4321 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004322 task_setup_data.callback_data = data;
4323 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004324 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004325 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004326 ret = nfs4_wait_for_completion_rpc_task(task);
4327 if (ret == 0) {
4328 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004329 if (ret)
4330 nfs4_handle_setlk_error(data->server, data->lsp,
4331 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004332 } else
4333 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004334 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004335 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004336 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337}
4338
4339static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4340{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004341 struct nfs_server *server = NFS_SERVER(state->inode);
4342 struct nfs4_exception exception = { };
4343 int err;
4344
4345 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004346 /* Cache the lock if possible... */
4347 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4348 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004349 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004350 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004351 break;
4352 nfs4_handle_exception(server, err, &exception);
4353 } while (exception.retry);
4354 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355}
4356
4357static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4358{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004359 struct nfs_server *server = NFS_SERVER(state->inode);
4360 struct nfs4_exception exception = { };
4361 int err;
4362
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004363 err = nfs4_set_lock_state(state, request);
4364 if (err != 0)
4365 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004366 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004367 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4368 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004369 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004370 switch (err) {
4371 default:
4372 goto out;
4373 case -NFS4ERR_GRACE:
4374 case -NFS4ERR_DELAY:
4375 nfs4_handle_exception(server, err, &exception);
4376 err = 0;
4377 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004378 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004379out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004380 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381}
4382
4383static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4384{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004385 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004386 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004387 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004389 if ((fl_flags & FL_POSIX) &&
4390 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4391 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004392 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004393 status = nfs4_set_lock_state(state, request);
4394 if (status != 0)
4395 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004396 request->fl_flags |= FL_ACCESS;
4397 status = do_vfs_lock(request->fl_file, request);
4398 if (status < 0)
4399 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004400 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004401 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004402 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004403 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004404 request->fl_flags = fl_flags & ~FL_SLEEP;
4405 status = do_vfs_lock(request->fl_file, request);
4406 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004407 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004408 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004409 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004410 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004411 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004412 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004413 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004414 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004415out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004416 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004417out:
4418 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 return status;
4420}
4421
4422static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4423{
4424 struct nfs4_exception exception = { };
4425 int err;
4426
4427 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004428 err = _nfs4_proc_setlk(state, cmd, request);
4429 if (err == -NFS4ERR_DENIED)
4430 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004432 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 } while (exception.retry);
4434 return err;
4435}
4436
4437static int
4438nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4439{
4440 struct nfs_open_context *ctx;
4441 struct nfs4_state *state;
4442 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4443 int status;
4444
4445 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004446 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 state = ctx->state;
4448
4449 if (request->fl_start < 0 || request->fl_end < 0)
4450 return -EINVAL;
4451
Trond Myklebustd9531262009-07-21 19:22:38 -04004452 if (IS_GETLK(cmd)) {
4453 if (state != NULL)
4454 return nfs4_proc_getlk(state, F_GETLK, request);
4455 return 0;
4456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457
4458 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4459 return -EINVAL;
4460
Trond Myklebustd9531262009-07-21 19:22:38 -04004461 if (request->fl_type == F_UNLCK) {
4462 if (state != NULL)
4463 return nfs4_proc_unlck(state, cmd, request);
4464 return 0;
4465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466
Trond Myklebustd9531262009-07-21 19:22:38 -04004467 if (state == NULL)
4468 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 do {
4470 status = nfs4_proc_setlk(state, cmd, request);
4471 if ((status != -EAGAIN) || IS_SETLK(cmd))
4472 break;
4473 timeout = nfs4_set_lock_task_retry(timeout);
4474 status = -ERESTARTSYS;
4475 if (signalled())
4476 break;
4477 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 return status;
4479}
4480
Trond Myklebust888e6942005-11-04 15:38:11 -05004481int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4482{
4483 struct nfs_server *server = NFS_SERVER(state->inode);
4484 struct nfs4_exception exception = { };
4485 int err;
4486
4487 err = nfs4_set_lock_state(state, fl);
4488 if (err != 0)
4489 goto out;
4490 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004491 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004492 switch (err) {
4493 default:
4494 printk(KERN_ERR "%s: unhandled error %d.\n",
4495 __func__, err);
4496 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004497 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004498 goto out;
4499 case -NFS4ERR_EXPIRED:
4500 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004501 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004502 nfs4_schedule_lease_recovery(server->nfs_client);
4503 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004504 case -NFS4ERR_BADSESSION:
4505 case -NFS4ERR_BADSLOT:
4506 case -NFS4ERR_BAD_HIGH_SLOT:
4507 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4508 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004509 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004510 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004511 case -ERESTARTSYS:
4512 /*
4513 * The show must go on: exit, but mark the
4514 * stateid as needing recovery.
4515 */
4516 case -NFS4ERR_ADMIN_REVOKED:
4517 case -NFS4ERR_BAD_STATEID:
4518 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004519 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004520 err = 0;
4521 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004522 case -EKEYEXPIRED:
4523 /*
4524 * User RPCSEC_GSS context has expired.
4525 * We cannot recover this stateid now, so
4526 * skip it and allow recovery thread to
4527 * proceed.
4528 */
4529 err = 0;
4530 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004531 case -ENOMEM:
4532 case -NFS4ERR_DENIED:
4533 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4534 err = 0;
4535 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004536 case -NFS4ERR_DELAY:
4537 break;
4538 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004539 err = nfs4_handle_exception(server, err, &exception);
4540 } while (exception.retry);
4541out:
4542 return err;
4543}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004544
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004545static void nfs4_release_lockowner_release(void *calldata)
4546{
4547 kfree(calldata);
4548}
4549
4550const struct rpc_call_ops nfs4_release_lockowner_ops = {
4551 .rpc_release = nfs4_release_lockowner_release,
4552};
4553
4554void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4555{
4556 struct nfs_server *server = lsp->ls_state->owner->so_server;
4557 struct nfs_release_lockowner_args *args;
4558 struct rpc_message msg = {
4559 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4560 };
4561
4562 if (server->nfs_client->cl_mvops->minor_version != 0)
4563 return;
4564 args = kmalloc(sizeof(*args), GFP_NOFS);
4565 if (!args)
4566 return;
4567 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4568 args->lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004569 args->lock_owner.s_dev = server->s_dev;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004570 msg.rpc_argp = args;
4571 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4572}
4573
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004574#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4575
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004576static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4577 const void *buf, size_t buflen,
4578 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004579{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004580 if (strcmp(key, "") != 0)
4581 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004582
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004583 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004584}
4585
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004586static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4587 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004588{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004589 if (strcmp(key, "") != 0)
4590 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004591
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004592 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004593}
4594
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004595static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4596 size_t list_len, const char *name,
4597 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004598{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004599 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004600
J. Bruce Fields096455a2006-03-20 23:23:42 -05004601 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4602 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004603
4604 if (list && len <= list_len)
4605 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004606 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004607}
4608
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004609static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4610{
4611 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4612 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4613 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4614 return;
4615
4616 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4617 NFS_ATTR_FATTR_NLINK;
4618 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4619 fattr->nlink = 2;
4620}
4621
Trond Myklebust56659e92007-07-17 21:52:39 -04004622int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004623 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004624{
4625 struct nfs_server *server = NFS_SERVER(dir);
4626 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004627 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4628 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004629 };
4630 struct nfs4_fs_locations_arg args = {
4631 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004632 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004633 .page = page,
4634 .bitmask = bitmask,
4635 };
Benny Halevy22958462009-04-01 09:22:02 -04004636 struct nfs4_fs_locations_res res = {
4637 .fs_locations = fs_locations,
4638 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004639 struct rpc_message msg = {
4640 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4641 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004642 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004643 };
4644 int status;
4645
Harvey Harrison3110ff82008-05-02 13:42:44 -07004646 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004647 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004648 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004649 fs_locations->nlocations = 0;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004650 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004651 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004652 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004653 return status;
4654}
4655
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00004656static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4657{
4658 int status;
4659 struct nfs4_secinfo_arg args = {
4660 .dir_fh = NFS_FH(dir),
4661 .name = name,
4662 };
4663 struct nfs4_secinfo_res res = {
4664 .flavors = flavors,
4665 };
4666 struct rpc_message msg = {
4667 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
4668 .rpc_argp = &args,
4669 .rpc_resp = &res,
4670 };
4671
4672 dprintk("NFS call secinfo %s\n", name->name);
4673 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4674 dprintk("NFS reply secinfo: %d\n", status);
4675 return status;
4676}
4677
4678int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4679{
4680 struct nfs4_exception exception = { };
4681 int err;
4682 do {
4683 err = nfs4_handle_exception(NFS_SERVER(dir),
4684 _nfs4_proc_secinfo(dir, name, flavors),
4685 &exception);
4686 } while (exception.retry);
4687 return err;
4688}
4689
Andy Adamson557134a2009-04-01 09:21:53 -04004690#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004691/*
Andy Adamson357f54d2010-12-14 10:11:57 -05004692 * Check the exchange flags returned by the server for invalid flags, having
4693 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4694 * DS flags set.
4695 */
4696static int nfs4_check_cl_exchange_flags(u32 flags)
4697{
4698 if (flags & ~EXCHGID4_FLAG_MASK_R)
4699 goto out_inval;
4700 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4701 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4702 goto out_inval;
4703 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4704 goto out_inval;
4705 return NFS_OK;
4706out_inval:
4707 return -NFS4ERR_INVAL;
4708}
4709
4710/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04004711 * nfs4_proc_exchange_id()
4712 *
4713 * Since the clientid has expired, all compounds using sessions
4714 * associated with the stale clientid will be returning
4715 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4716 * be in some phase of session reset.
4717 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004718int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004719{
4720 nfs4_verifier verifier;
4721 struct nfs41_exchange_id_args args = {
4722 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05004723 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04004724 };
4725 struct nfs41_exchange_id_res res = {
4726 .client = clp,
4727 };
4728 int status;
4729 struct rpc_message msg = {
4730 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4731 .rpc_argp = &args,
4732 .rpc_resp = &res,
4733 .rpc_cred = cred,
4734 };
4735 __be32 *p;
4736
4737 dprintk("--> %s\n", __func__);
4738 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004739
Benny Halevy99fe60d2009-04-01 09:22:29 -04004740 p = (u32 *)verifier.data;
4741 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4742 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4743 args.verifier = &verifier;
4744
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004745 args.id_len = scnprintf(args.id, sizeof(args.id),
4746 "%s/%s.%s/%u",
4747 clp->cl_ipaddr,
4748 init_utsname()->nodename,
4749 init_utsname()->domainname,
4750 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004751
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004752 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4753 if (!status)
4754 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004755 dprintk("<-- %s status= %d\n", __func__, status);
4756 return status;
4757}
4758
Andy Adamson2050f0c2009-04-01 09:22:30 -04004759struct nfs4_get_lease_time_data {
4760 struct nfs4_get_lease_time_args *args;
4761 struct nfs4_get_lease_time_res *res;
4762 struct nfs_client *clp;
4763};
4764
4765static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4766 void *calldata)
4767{
4768 int ret;
4769 struct nfs4_get_lease_time_data *data =
4770 (struct nfs4_get_lease_time_data *)calldata;
4771
4772 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004773 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004774 /* just setup sequence, do not trigger session recovery
4775 since we're invoked within one */
4776 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004777 &data->args->la_seq_args,
4778 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004779
4780 BUG_ON(ret == -EAGAIN);
4781 rpc_call_start(task);
4782 dprintk("<-- %s\n", __func__);
4783}
4784
4785/*
4786 * Called from nfs4_state_manager thread for session setup, so don't recover
4787 * from sequence operation or clientid errors.
4788 */
4789static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4790{
4791 struct nfs4_get_lease_time_data *data =
4792 (struct nfs4_get_lease_time_data *)calldata;
4793
4794 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04004795 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4796 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04004797 switch (task->tk_status) {
4798 case -NFS4ERR_DELAY:
4799 case -NFS4ERR_GRACE:
4800 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4801 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4802 task->tk_status = 0;
Trond Myklebust0110ee12009-12-07 09:00:24 -05004803 nfs_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004804 return;
4805 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004806 dprintk("<-- %s\n", __func__);
4807}
4808
4809struct rpc_call_ops nfs4_get_lease_time_ops = {
4810 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4811 .rpc_call_done = nfs4_get_lease_time_done,
4812};
4813
4814int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4815{
4816 struct rpc_task *task;
4817 struct nfs4_get_lease_time_args args;
4818 struct nfs4_get_lease_time_res res = {
4819 .lr_fsinfo = fsinfo,
4820 };
4821 struct nfs4_get_lease_time_data data = {
4822 .args = &args,
4823 .res = &res,
4824 .clp = clp,
4825 };
4826 struct rpc_message msg = {
4827 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4828 .rpc_argp = &args,
4829 .rpc_resp = &res,
4830 };
4831 struct rpc_task_setup task_setup = {
4832 .rpc_client = clp->cl_rpcclient,
4833 .rpc_message = &msg,
4834 .callback_ops = &nfs4_get_lease_time_ops,
4835 .callback_data = &data
4836 };
4837 int status;
4838
Andy Adamson2050f0c2009-04-01 09:22:30 -04004839 dprintk("--> %s\n", __func__);
4840 task = rpc_run_task(&task_setup);
4841
4842 if (IS_ERR(task))
4843 status = PTR_ERR(task);
4844 else {
4845 status = task->tk_status;
4846 rpc_put_task(task);
4847 }
4848 dprintk("<-- %s return %d\n", __func__, status);
4849
4850 return status;
4851}
4852
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004853/*
4854 * Reset a slot table
4855 */
Andy Adamson104aeba2010-01-14 17:45:10 -05004856static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4857 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004858{
Andy Adamson104aeba2010-01-14 17:45:10 -05004859 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004860 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004861 int ret = 0;
4862
Andy Adamson104aeba2010-01-14 17:45:10 -05004863 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4864 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004865
Andy Adamson104aeba2010-01-14 17:45:10 -05004866 /* Does the newly negotiated max_reqs match the existing slot table? */
4867 if (max_reqs != tbl->max_slots) {
4868 ret = -ENOMEM;
4869 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004870 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05004871 if (!new)
4872 goto out;
4873 ret = 0;
4874 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004875 }
4876 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05004877 if (new) {
4878 tbl->slots = new;
4879 tbl->max_slots = max_reqs;
4880 }
4881 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004882 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004883 spin_unlock(&tbl->slot_tbl_lock);
4884 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4885 tbl, tbl->slots, tbl->max_slots);
4886out:
4887 dprintk("<-- %s: return %d\n", __func__, ret);
4888 return ret;
4889}
4890
Andy Adamsonfc931582009-04-01 09:22:31 -04004891/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004892 * Reset the forechannel and backchannel slot tables
4893 */
4894static int nfs4_reset_slot_tables(struct nfs4_session *session)
4895{
4896 int status;
4897
4898 status = nfs4_reset_slot_table(&session->fc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004899 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004900 if (status)
4901 return status;
4902
4903 status = nfs4_reset_slot_table(&session->bc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004904 session->bc_attrs.max_reqs, 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004905 return status;
4906}
4907
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004908/* Destroy the slot table */
4909static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4910{
4911 if (session->fc_slot_table.slots != NULL) {
4912 kfree(session->fc_slot_table.slots);
4913 session->fc_slot_table.slots = NULL;
4914 }
4915 if (session->bc_slot_table.slots != NULL) {
4916 kfree(session->bc_slot_table.slots);
4917 session->bc_slot_table.slots = NULL;
4918 }
4919 return;
4920}
4921
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004922/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004923 * Initialize slot table
4924 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004925static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4926 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04004927{
Andy Adamsonfc931582009-04-01 09:22:31 -04004928 struct nfs4_slot *slot;
4929 int ret = -ENOMEM;
4930
4931 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4932
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004933 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04004934
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004935 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04004936 if (!slot)
4937 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04004938 ret = 0;
4939
4940 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04004941 tbl->max_slots = max_slots;
4942 tbl->slots = slot;
4943 tbl->highest_used_slotid = -1; /* no slot is currently used */
4944 spin_unlock(&tbl->slot_tbl_lock);
4945 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4946 tbl, tbl->slots, tbl->max_slots);
4947out:
4948 dprintk("<-- %s: return %d\n", __func__, ret);
4949 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04004950}
4951
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004952/*
4953 * Initialize the forechannel and backchannel tables
4954 */
4955static int nfs4_init_slot_tables(struct nfs4_session *session)
4956{
Trond Myklebustf26468f2009-12-05 19:32:11 -05004957 struct nfs4_slot_table *tbl;
4958 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004959
Trond Myklebustf26468f2009-12-05 19:32:11 -05004960 tbl = &session->fc_slot_table;
4961 if (tbl->slots == NULL) {
4962 status = nfs4_init_slot_table(tbl,
4963 session->fc_attrs.max_reqs, 1);
4964 if (status)
4965 return status;
4966 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004967
Trond Myklebustf26468f2009-12-05 19:32:11 -05004968 tbl = &session->bc_slot_table;
4969 if (tbl->slots == NULL) {
4970 status = nfs4_init_slot_table(tbl,
4971 session->bc_attrs.max_reqs, 0);
4972 if (status)
4973 nfs4_destroy_slot_tables(session);
4974 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004975
4976 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04004977}
4978
4979struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
4980{
4981 struct nfs4_session *session;
4982 struct nfs4_slot_table *tbl;
4983
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004984 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04004985 if (!session)
4986 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04004987
Andy Adamson557134a2009-04-01 09:21:53 -04004988 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004989 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04004990 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004991 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00004992 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004993
4994 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05004995 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004996 spin_lock_init(&tbl->slot_tbl_lock);
4997 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00004998 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004999
Trond Myklebust1055d762010-06-16 09:52:27 -04005000 session->session_state = 1<<NFS4_SESSION_INITING;
5001
Andy Adamson557134a2009-04-01 09:21:53 -04005002 session->clp = clp;
5003 return session;
5004}
5005
5006void nfs4_destroy_session(struct nfs4_session *session)
5007{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005008 nfs4_proc_destroy_session(session);
5009 dprintk("%s Destroy backchannel for xprt %p\n",
5010 __func__, session->clp->cl_rpcclient->cl_xprt);
5011 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
5012 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005013 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005014 kfree(session);
5015}
5016
Andy Adamsonfc931582009-04-01 09:22:31 -04005017/*
5018 * Initialize the values to be used by the client in CREATE_SESSION
5019 * If nfs4_init_session set the fore channel request and response sizes,
5020 * use them.
5021 *
5022 * Set the back channel max_resp_sz_cached to zero to force the client to
5023 * always set csa_cachethis to FALSE because the current implementation
5024 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5025 */
5026static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5027{
5028 struct nfs4_session *session = args->client->cl_session;
5029 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5030 mxresp_sz = session->fc_attrs.max_resp_sz;
5031
5032 if (mxrqst_sz == 0)
5033 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5034 if (mxresp_sz == 0)
5035 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5036 /* Fore channel attributes */
5037 args->fc_attrs.headerpadsz = 0;
5038 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5039 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005040 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5041 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
5042
5043 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005044 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005045 __func__,
5046 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005047 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005048
5049 /* Back channel attributes */
5050 args->bc_attrs.headerpadsz = 0;
5051 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5052 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5053 args->bc_attrs.max_resp_sz_cached = 0;
5054 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5055 args->bc_attrs.max_reqs = 1;
5056
5057 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5058 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5059 __func__,
5060 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5061 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5062 args->bc_attrs.max_reqs);
5063}
5064
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005065static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005066{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005067 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5068 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5069
5070 if (rcvd->headerpadsz > sent->headerpadsz)
5071 return -EINVAL;
5072 if (rcvd->max_resp_sz > sent->max_resp_sz)
5073 return -EINVAL;
5074 /*
5075 * Our requested max_ops is the minimum we need; we're not
5076 * prepared to break up compounds into smaller pieces than that.
5077 * So, no point even trying to continue if the server won't
5078 * cooperate:
5079 */
5080 if (rcvd->max_ops < sent->max_ops)
5081 return -EINVAL;
5082 if (rcvd->max_reqs == 0)
5083 return -EINVAL;
5084 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005085}
5086
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005087static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5088{
5089 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5090 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005091
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005092 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5093 return -EINVAL;
5094 if (rcvd->max_resp_sz < sent->max_resp_sz)
5095 return -EINVAL;
5096 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5097 return -EINVAL;
5098 /* These would render the backchannel useless: */
5099 if (rcvd->max_ops == 0)
5100 return -EINVAL;
5101 if (rcvd->max_reqs == 0)
5102 return -EINVAL;
5103 return 0;
5104}
Andy Adamson8d353012009-04-01 09:22:32 -04005105
Andy Adamson8d353012009-04-01 09:22:32 -04005106static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5107 struct nfs4_session *session)
5108{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005109 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005110
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005111 ret = nfs4_verify_fore_channel_attrs(args, session);
5112 if (ret)
5113 return ret;
5114 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005115}
5116
Andy Adamsonfc931582009-04-01 09:22:31 -04005117static int _nfs4_proc_create_session(struct nfs_client *clp)
5118{
5119 struct nfs4_session *session = clp->cl_session;
5120 struct nfs41_create_session_args args = {
5121 .client = clp,
5122 .cb_program = NFS4_CALLBACK,
5123 };
5124 struct nfs41_create_session_res res = {
5125 .client = clp,
5126 };
5127 struct rpc_message msg = {
5128 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5129 .rpc_argp = &args,
5130 .rpc_resp = &res,
5131 };
5132 int status;
5133
5134 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005135 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005136
5137 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5138
Andy Adamson8d353012009-04-01 09:22:32 -04005139 if (!status)
5140 /* Verify the session's negotiated channel_attrs values */
5141 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005142 if (!status) {
5143 /* Increment the clientid slot sequence id */
5144 clp->cl_seqid++;
5145 }
5146
5147 return status;
5148}
5149
5150/*
5151 * Issues a CREATE_SESSION operation to the server.
5152 * It is the responsibility of the caller to verify the session is
5153 * expired before calling this routine.
5154 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005155int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005156{
5157 int status;
5158 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005159 struct nfs4_session *session = clp->cl_session;
Ricardo Labiaga7d6d63d2011-03-09 13:13:44 -05005160 long timeout = 0;
5161 int err;
Andy Adamsonfc931582009-04-01 09:22:31 -04005162
5163 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5164
Ricardo Labiaga7d6d63d2011-03-09 13:13:44 -05005165 do {
5166 status = _nfs4_proc_create_session(clp);
5167 if (status == -NFS4ERR_DELAY) {
5168 err = nfs4_delay(clp->cl_rpcclient, &timeout);
5169 if (err)
5170 status = err;
5171 }
5172 } while (status == -NFS4ERR_DELAY);
5173
Andy Adamsonfc931582009-04-01 09:22:31 -04005174 if (status)
5175 goto out;
5176
Trond Myklebustf26468f2009-12-05 19:32:11 -05005177 /* Init and reset the fore channel */
5178 status = nfs4_init_slot_tables(session);
5179 dprintk("slot table initialization returned %d\n", status);
5180 if (status)
5181 goto out;
5182 status = nfs4_reset_slot_tables(session);
5183 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005184 if (status)
5185 goto out;
5186
5187 ptr = (unsigned *)&session->sess_id.data[0];
5188 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5189 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005190out:
5191 dprintk("<-- %s\n", __func__);
5192 return status;
5193}
5194
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005195/*
5196 * Issue the over-the-wire RPC DESTROY_SESSION.
5197 * The caller must serialize access to this routine.
5198 */
5199int nfs4_proc_destroy_session(struct nfs4_session *session)
5200{
5201 int status = 0;
5202 struct rpc_message msg;
5203
5204 dprintk("--> nfs4_proc_destroy_session\n");
5205
5206 /* session is still being setup */
5207 if (session->clp->cl_cons_state != NFS_CS_READY)
5208 return status;
5209
5210 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5211 msg.rpc_argp = session;
5212 msg.rpc_resp = NULL;
5213 msg.rpc_cred = NULL;
5214 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5215
5216 if (status)
5217 printk(KERN_WARNING
5218 "Got error %d from the server on DESTROY_SESSION. "
5219 "Session has been destroyed regardless...\n", status);
5220
5221 dprintk("<-- nfs4_proc_destroy_session\n");
5222 return status;
5223}
5224
Trond Myklebustfccba802009-07-21 16:48:07 -04005225int nfs4_init_session(struct nfs_server *server)
5226{
5227 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005228 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005229 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005230 int ret;
5231
5232 if (!nfs4_has_session(clp))
5233 return 0;
5234
Trond Myklebust1055d762010-06-16 09:52:27 -04005235 session = clp->cl_session;
5236 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5237 return 0;
5238
Andy Adamson68bf05e2009-12-15 12:55:02 -05005239 rsize = server->rsize;
5240 if (rsize == 0)
5241 rsize = NFS_MAX_FILE_IO_SIZE;
5242 wsize = server->wsize;
5243 if (wsize == 0)
5244 wsize = NFS_MAX_FILE_IO_SIZE;
5245
Andy Adamson68bf05e2009-12-15 12:55:02 -05005246 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5247 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005248
Trond Myklebustfccba802009-07-21 16:48:07 -04005249 ret = nfs4_recover_expired_lease(server);
5250 if (!ret)
5251 ret = nfs4_check_client_ready(clp);
5252 return ret;
5253}
5254
Andy Adamsond83217c2011-03-01 01:34:17 +00005255int nfs4_init_ds_session(struct nfs_client *clp)
5256{
5257 struct nfs4_session *session = clp->cl_session;
5258 int ret;
5259
5260 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5261 return 0;
5262
5263 ret = nfs4_client_recover_expired_lease(clp);
5264 if (!ret)
5265 /* Test for the DS role */
5266 if (!is_ds_client(clp))
5267 ret = -ENODEV;
5268 if (!ret)
5269 ret = nfs4_check_client_ready(clp);
5270 return ret;
5271
5272}
5273EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5274
5275
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005276/*
5277 * Renew the cl_session lease.
5278 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005279struct nfs4_sequence_data {
5280 struct nfs_client *clp;
5281 struct nfs4_sequence_args args;
5282 struct nfs4_sequence_res res;
5283};
5284
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005285static void nfs41_sequence_release(void *data)
5286{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005287 struct nfs4_sequence_data *calldata = data;
5288 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005289
Alexandros Batsakis71358402010-02-05 03:45:05 -08005290 if (atomic_read(&clp->cl_count) > 1)
5291 nfs4_schedule_state_renewal(clp);
5292 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005293 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005294}
5295
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005296static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5297{
5298 switch(task->tk_status) {
5299 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005300 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5301 return -EAGAIN;
5302 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005303 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005304 }
5305 return 0;
5306}
5307
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005308static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005309{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005310 struct nfs4_sequence_data *calldata = data;
5311 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005312
Trond Myklebust14516c32010-07-31 14:29:06 -04005313 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5314 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005315
5316 if (task->tk_status < 0) {
5317 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005318 if (atomic_read(&clp->cl_count) == 1)
5319 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005320
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005321 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5322 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005323 return;
5324 }
5325 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005326 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005327out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005328 dprintk("<-- %s\n", __func__);
5329}
5330
5331static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5332{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005333 struct nfs4_sequence_data *calldata = data;
5334 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005335 struct nfs4_sequence_args *args;
5336 struct nfs4_sequence_res *res;
5337
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005338 args = task->tk_msg.rpc_argp;
5339 res = task->tk_msg.rpc_resp;
5340
Trond Myklebust035168a2010-06-16 09:52:26 -04005341 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005342 return;
5343 rpc_call_start(task);
5344}
5345
5346static const struct rpc_call_ops nfs41_sequence_ops = {
5347 .rpc_call_done = nfs41_sequence_call_done,
5348 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005349 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005350};
5351
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005352static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005353{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005354 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005355 struct rpc_message msg = {
5356 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5357 .rpc_cred = cred,
5358 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005359 struct rpc_task_setup task_setup_data = {
5360 .rpc_client = clp->cl_rpcclient,
5361 .rpc_message = &msg,
5362 .callback_ops = &nfs41_sequence_ops,
5363 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5364 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005365
Alexandros Batsakis71358402010-02-05 03:45:05 -08005366 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005367 return ERR_PTR(-EIO);
Benny Halevydfb4f302010-09-24 09:17:01 -04005368 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005369 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005370 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005371 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005372 }
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005373 msg.rpc_argp = &calldata->args;
5374 msg.rpc_resp = &calldata->res;
5375 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005376 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005377
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005378 return rpc_run_task(&task_setup_data);
5379}
5380
5381static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5382{
5383 struct rpc_task *task;
5384 int ret = 0;
5385
5386 task = _nfs41_proc_sequence(clp, cred);
5387 if (IS_ERR(task))
5388 ret = PTR_ERR(task);
5389 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005390 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005391 dprintk("<-- %s status=%d\n", __func__, ret);
5392 return ret;
5393}
5394
5395static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5396{
5397 struct rpc_task *task;
5398 int ret;
5399
5400 task = _nfs41_proc_sequence(clp, cred);
5401 if (IS_ERR(task)) {
5402 ret = PTR_ERR(task);
5403 goto out;
5404 }
5405 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005406 if (!ret) {
5407 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5408
5409 if (task->tk_status == 0)
5410 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005411 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005412 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005413 rpc_put_task(task);
5414out:
5415 dprintk("<-- %s status=%d\n", __func__, ret);
5416 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005417}
5418
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005419struct nfs4_reclaim_complete_data {
5420 struct nfs_client *clp;
5421 struct nfs41_reclaim_complete_args arg;
5422 struct nfs41_reclaim_complete_res res;
5423};
5424
5425static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5426{
5427 struct nfs4_reclaim_complete_data *calldata = data;
5428
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005429 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005430 if (nfs41_setup_sequence(calldata->clp->cl_session,
5431 &calldata->arg.seq_args,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005432 &calldata->res.seq_res, 0, task))
5433 return;
5434
5435 rpc_call_start(task);
5436}
5437
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005438static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5439{
5440 switch(task->tk_status) {
5441 case 0:
5442 case -NFS4ERR_COMPLETE_ALREADY:
5443 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5444 break;
5445 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005446 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5447 return -EAGAIN;
5448 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005449 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005450 }
5451 return 0;
5452}
5453
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005454static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5455{
5456 struct nfs4_reclaim_complete_data *calldata = data;
5457 struct nfs_client *clp = calldata->clp;
5458 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5459
5460 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005461 if (!nfs41_sequence_done(task, res))
5462 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005463
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005464 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5465 rpc_restart_call_prepare(task);
5466 return;
5467 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005468 dprintk("<-- %s\n", __func__);
5469}
5470
5471static void nfs4_free_reclaim_complete_data(void *data)
5472{
5473 struct nfs4_reclaim_complete_data *calldata = data;
5474
5475 kfree(calldata);
5476}
5477
5478static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5479 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5480 .rpc_call_done = nfs4_reclaim_complete_done,
5481 .rpc_release = nfs4_free_reclaim_complete_data,
5482};
5483
5484/*
5485 * Issue a global reclaim complete.
5486 */
5487static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5488{
5489 struct nfs4_reclaim_complete_data *calldata;
5490 struct rpc_task *task;
5491 struct rpc_message msg = {
5492 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5493 };
5494 struct rpc_task_setup task_setup_data = {
5495 .rpc_client = clp->cl_rpcclient,
5496 .rpc_message = &msg,
5497 .callback_ops = &nfs4_reclaim_complete_call_ops,
5498 .flags = RPC_TASK_ASYNC,
5499 };
5500 int status = -ENOMEM;
5501
5502 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005503 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005504 if (calldata == NULL)
5505 goto out;
5506 calldata->clp = clp;
5507 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005508
5509 msg.rpc_argp = &calldata->arg;
5510 msg.rpc_resp = &calldata->res;
5511 task_setup_data.callback_data = calldata;
5512 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005513 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005514 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005515 goto out;
5516 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005517 status = nfs4_wait_for_completion_rpc_task(task);
5518 if (status == 0)
5519 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005520 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005521 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005522out:
5523 dprintk("<-- %s status=%d\n", __func__, status);
5524 return status;
5525}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005526
5527static void
5528nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5529{
5530 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005531 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005532
5533 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005534 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5535 * right now covering the LAYOUTGET we are about to send.
5536 * However, that is not so catastrophic, and there seems
5537 * to be no way to prevent it completely.
5538 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005539 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5540 &lgp->res.seq_res, 0, task))
5541 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005542 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5543 NFS_I(lgp->args.inode)->layout,
5544 lgp->args.ctx->state)) {
5545 rpc_exit(task, NFS4_OK);
5546 return;
5547 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005548 rpc_call_start(task);
5549}
5550
5551static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5552{
5553 struct nfs4_layoutget *lgp = calldata;
5554 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5555
5556 dprintk("--> %s\n", __func__);
5557
5558 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5559 return;
5560
5561 switch (task->tk_status) {
5562 case 0:
5563 break;
5564 case -NFS4ERR_LAYOUTTRYLATER:
5565 case -NFS4ERR_RECALLCONFLICT:
5566 task->tk_status = -NFS4ERR_DELAY;
5567 /* Fall through */
5568 default:
5569 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5570 rpc_restart_call_prepare(task);
5571 return;
5572 }
5573 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005574 dprintk("<-- %s\n", __func__);
5575}
5576
5577static void nfs4_layoutget_release(void *calldata)
5578{
5579 struct nfs4_layoutget *lgp = calldata;
5580
5581 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005582 if (lgp->res.layout.buf != NULL)
5583 free_page((unsigned long) lgp->res.layout.buf);
5584 put_nfs_open_context(lgp->args.ctx);
5585 kfree(calldata);
5586 dprintk("<-- %s\n", __func__);
5587}
5588
5589static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5590 .rpc_call_prepare = nfs4_layoutget_prepare,
5591 .rpc_call_done = nfs4_layoutget_done,
5592 .rpc_release = nfs4_layoutget_release,
5593};
5594
5595int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5596{
5597 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5598 struct rpc_task *task;
5599 struct rpc_message msg = {
5600 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5601 .rpc_argp = &lgp->args,
5602 .rpc_resp = &lgp->res,
5603 };
5604 struct rpc_task_setup task_setup_data = {
5605 .rpc_client = server->client,
5606 .rpc_message = &msg,
5607 .callback_ops = &nfs4_layoutget_call_ops,
5608 .callback_data = lgp,
5609 .flags = RPC_TASK_ASYNC,
5610 };
5611 int status = 0;
5612
5613 dprintk("--> %s\n", __func__);
5614
5615 lgp->res.layout.buf = (void *)__get_free_page(GFP_NOFS);
5616 if (lgp->res.layout.buf == NULL) {
5617 nfs4_layoutget_release(lgp);
5618 return -ENOMEM;
5619 }
5620
5621 lgp->res.seq_res.sr_slot = NULL;
5622 task = rpc_run_task(&task_setup_data);
5623 if (IS_ERR(task))
5624 return PTR_ERR(task);
5625 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00005626 if (status == 0)
5627 status = task->tk_status;
5628 if (status == 0)
5629 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005630 rpc_put_task(task);
5631 dprintk("<-- %s status=%d\n", __func__, status);
5632 return status;
5633}
5634
5635static int
5636_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5637{
5638 struct nfs4_getdeviceinfo_args args = {
5639 .pdev = pdev,
5640 };
5641 struct nfs4_getdeviceinfo_res res = {
5642 .pdev = pdev,
5643 };
5644 struct rpc_message msg = {
5645 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5646 .rpc_argp = &args,
5647 .rpc_resp = &res,
5648 };
5649 int status;
5650
5651 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005652 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005653 dprintk("<-- %s status=%d\n", __func__, status);
5654
5655 return status;
5656}
5657
5658int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5659{
5660 struct nfs4_exception exception = { };
5661 int err;
5662
5663 do {
5664 err = nfs4_handle_exception(server,
5665 _nfs4_proc_getdeviceinfo(server, pdev),
5666 &exception);
5667 } while (exception.retry);
5668 return err;
5669}
5670EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5671
Andy Adamson557134a2009-04-01 09:21:53 -04005672#endif /* CONFIG_NFS_V4_1 */
5673
Andy Adamson591d71c2009-04-01 09:22:47 -04005674struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005675 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005676 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677 .recover_open = nfs4_open_reclaim,
5678 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04005679 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005680 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681};
5682
Andy Adamson591d71c2009-04-01 09:22:47 -04005683#if defined(CONFIG_NFS_V4_1)
5684struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5685 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5686 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5687 .recover_open = nfs4_open_reclaim,
5688 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05005689 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005690 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005691 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04005692};
5693#endif /* CONFIG_NFS_V4_1 */
5694
5695struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005696 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005697 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698 .recover_open = nfs4_open_expired,
5699 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04005700 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005701 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702};
5703
Andy Adamson591d71c2009-04-01 09:22:47 -04005704#if defined(CONFIG_NFS_V4_1)
5705struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5706 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5707 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5708 .recover_open = nfs4_open_expired,
5709 .recover_lock = nfs4_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05005710 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005711 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04005712};
5713#endif /* CONFIG_NFS_V4_1 */
5714
Benny Halevy29fba382009-04-01 09:22:44 -04005715struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5716 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04005717 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005718 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04005719};
5720
5721#if defined(CONFIG_NFS_V4_1)
5722struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5723 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04005724 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005725 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04005726};
5727#endif
5728
Trond Myklebust97dc1352010-06-16 09:52:26 -04005729static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
5730 .minor_version = 0,
5731 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04005732 .validate_stateid = nfs4_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005733 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
5734 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
5735 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005736};
5737
5738#if defined(CONFIG_NFS_V4_1)
5739static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
5740 .minor_version = 1,
5741 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04005742 .validate_stateid = nfs41_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005743 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
5744 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
5745 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005746};
5747#endif
5748
Trond Myklebust97dc1352010-06-16 09:52:26 -04005749const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
5750 [0] = &nfs_v4_0_minor_ops,
5751#if defined(CONFIG_NFS_V4_1)
5752 [1] = &nfs_v4_1_minor_ops,
5753#endif
5754};
5755
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005756static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005757 .permission = nfs_permission,
5758 .getattr = nfs_getattr,
5759 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005760 .getxattr = generic_getxattr,
5761 .setxattr = generic_setxattr,
5762 .listxattr = generic_listxattr,
5763 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005764};
5765
David Howells509de812006-08-22 20:06:11 -04005766const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767 .version = 4, /* protocol version */
5768 .dentry_ops = &nfs4_dentry_operations,
5769 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005770 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771 .getroot = nfs4_proc_get_root,
5772 .getattr = nfs4_proc_getattr,
5773 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04005774 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775 .lookup = nfs4_proc_lookup,
5776 .access = nfs4_proc_access,
5777 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778 .create = nfs4_proc_create,
5779 .remove = nfs4_proc_remove,
5780 .unlink_setup = nfs4_proc_unlink_setup,
5781 .unlink_done = nfs4_proc_unlink_done,
5782 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04005783 .rename_setup = nfs4_proc_rename_setup,
5784 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785 .link = nfs4_proc_link,
5786 .symlink = nfs4_proc_symlink,
5787 .mkdir = nfs4_proc_mkdir,
5788 .rmdir = nfs4_proc_remove,
5789 .readdir = nfs4_proc_readdir,
5790 .mknod = nfs4_proc_mknod,
5791 .statfs = nfs4_proc_statfs,
5792 .fsinfo = nfs4_proc_fsinfo,
5793 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04005794 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795 .decode_dirent = nfs4_decode_dirent,
5796 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05005797 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005799 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005801 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005803 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04005804 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04005805 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00005806 .init_client = nfs4_init_client,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005807 .secinfo = nfs4_proc_secinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808};
5809
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005810static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
5811 .prefix = XATTR_NAME_NFSV4_ACL,
5812 .list = nfs4_xattr_list_nfs4_acl,
5813 .get = nfs4_xattr_get_nfs4_acl,
5814 .set = nfs4_xattr_set_nfs4_acl,
5815};
5816
5817const struct xattr_handler *nfs4_xattr_handlers[] = {
5818 &nfs4_xattr_nfs4_acl_handler,
5819 NULL
5820};
5821
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822/*
5823 * Local variables:
5824 * c-basic-offset: 8
5825 * End:
5826 */