blob: fcc2408d7ab0ea8c40f6a4bd8f66e82cdbf0aa4f [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>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
Bryan Schumaker8f70e952011-03-24 17:12:31 +000046#include <linux/sunrpc/gss_api.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/nfs.h>
48#include <linux/nfs4.h>
49#include <linux/nfs_fs.h>
50#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040051#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070053#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040054#include <linux/module.h>
Andy Adamson5a0ffe52009-04-01 09:23:18 -040055#include <linux/sunrpc/bc_xprt.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000056#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050057#include <linux/utsname.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66#define NFSDBG_FACILITY NFSDBG_PROC
67
Trond Myklebust2066fe82006-09-15 08:30:46 -040068#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#define NFS4_POLL_RETRY_MAX (15*HZ)
70
Trond Myklebusta78cb572009-08-09 15:06:19 -040071#define NFS4_MAX_LOOP_ON_RECOVER (10)
72
Trond Myklebustcdd4e682006-01-03 09:55:12 +010073struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010074static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080075static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050077static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Trond Myklebust99367812007-07-17 21:52:41 -040078static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040079static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
80 struct nfs_fattr *fattr, struct iattr *sattr,
81 struct nfs4_state *state);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040082#ifdef CONFIG_NFS_V4_1
83static int nfs41_test_stateid(struct nfs_server *, struct nfs4_state *);
84static int nfs41_free_stateid(struct nfs_server *, struct nfs4_state *);
85#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070086/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050087static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Trond Myklebust52567b02009-10-23 14:46:42 -040089 if (err >= -1000)
90 return err;
91 switch (err) {
92 case -NFS4ERR_RESOURCE:
93 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +000094 case -NFS4ERR_WRONGSEC:
95 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -080096 case -NFS4ERR_BADOWNER:
97 case -NFS4ERR_BADNAME:
98 return -EINVAL;
Trond Myklebust52567b02009-10-23 14:46:42 -040099 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700101 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400102 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400104 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
107/*
108 * This is our standard bitmap for GETATTR requests.
109 */
110const u32 nfs4_fattr_bitmap[2] = {
111 FATTR4_WORD0_TYPE
112 | FATTR4_WORD0_CHANGE
113 | FATTR4_WORD0_SIZE
114 | FATTR4_WORD0_FSID
115 | FATTR4_WORD0_FILEID,
116 FATTR4_WORD1_MODE
117 | FATTR4_WORD1_NUMLINKS
118 | FATTR4_WORD1_OWNER
119 | FATTR4_WORD1_OWNER_GROUP
120 | FATTR4_WORD1_RAWDEV
121 | FATTR4_WORD1_SPACE_USED
122 | FATTR4_WORD1_TIME_ACCESS
123 | FATTR4_WORD1_TIME_METADATA
124 | FATTR4_WORD1_TIME_MODIFY
125};
126
127const u32 nfs4_statfs_bitmap[2] = {
128 FATTR4_WORD0_FILES_AVAIL
129 | FATTR4_WORD0_FILES_FREE
130 | FATTR4_WORD0_FILES_TOTAL,
131 FATTR4_WORD1_SPACE_AVAIL
132 | FATTR4_WORD1_SPACE_FREE
133 | FATTR4_WORD1_SPACE_TOTAL
134};
135
Trond Myklebust4ce79712005-06-22 17:16:21 +0000136const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 FATTR4_WORD0_MAXLINK
138 | FATTR4_WORD0_MAXNAME,
139 0
140};
141
Fred Isamandae100c2011-07-30 20:52:37 -0400142const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 | FATTR4_WORD0_MAXREAD
144 | FATTR4_WORD0_MAXWRITE
145 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700146 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400147 | FATTR4_WORD1_FS_LAYOUT_TYPES,
148 FATTR4_WORD2_LAYOUT_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149};
150
Manoj Naik830b8e32006-06-09 09:34:25 -0400151const u32 nfs4_fs_locations_bitmap[2] = {
152 FATTR4_WORD0_TYPE
153 | FATTR4_WORD0_CHANGE
154 | FATTR4_WORD0_SIZE
155 | FATTR4_WORD0_FSID
156 | FATTR4_WORD0_FILEID
157 | FATTR4_WORD0_FS_LOCATIONS,
158 FATTR4_WORD1_MODE
159 | FATTR4_WORD1_NUMLINKS
160 | FATTR4_WORD1_OWNER
161 | FATTR4_WORD1_OWNER_GROUP
162 | FATTR4_WORD1_RAWDEV
163 | FATTR4_WORD1_SPACE_USED
164 | FATTR4_WORD1_TIME_ACCESS
165 | FATTR4_WORD1_TIME_METADATA
166 | FATTR4_WORD1_TIME_MODIFY
167 | FATTR4_WORD1_MOUNTED_ON_FILEID
168};
169
Al Virobc4785c2006-10-19 23:28:51 -0700170static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 struct nfs4_readdir_arg *readdir)
172{
Al Viro0dbb4c62006-10-19 23:28:49 -0700173 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 BUG_ON(readdir->count < 80);
176 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000177 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
179 return;
180 }
181
182 readdir->cookie = 0;
183 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
184 if (cookie == 2)
185 return;
186
187 /*
188 * NFSv4 servers do not return entries for '.' and '..'
189 * Therefore, we fake these entries here. We let '.'
190 * have cookie 0 and '..' have cookie 1. Note that
191 * when talking to the server, we always send cookie 0
192 * instead of 1 or 2.
193 */
Al Viro0dbb4c62006-10-19 23:28:49 -0700194 start = p = kmap_atomic(*readdir->pages, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 if (cookie == 0) {
197 *p++ = xdr_one; /* next */
198 *p++ = xdr_zero; /* cookie, first word */
199 *p++ = xdr_one; /* cookie, second word */
200 *p++ = xdr_one; /* entry len */
201 memcpy(p, ".\0\0\0", 4); /* entry */
202 p++;
203 *p++ = xdr_one; /* bitmap length */
204 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
205 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400206 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
208
209 *p++ = xdr_one; /* next */
210 *p++ = xdr_zero; /* cookie, first word */
211 *p++ = xdr_two; /* cookie, second word */
212 *p++ = xdr_two; /* entry len */
213 memcpy(p, "..\0\0", 4); /* entry */
214 p++;
215 *p++ = xdr_one; /* bitmap length */
216 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
217 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400218 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 readdir->pgbase = (char *)p - (char *)start;
221 readdir->count -= readdir->pgbase;
222 kunmap_atomic(start, KM_USER0);
223}
224
Trond Myklebust65de8722008-12-23 15:21:44 -0500225static int nfs4_wait_clnt_recover(struct nfs_client *clp)
226{
227 int res;
228
229 might_sleep();
230
Trond Myklebuste005e802008-12-23 15:21:48 -0500231 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400232 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500233 return res;
234}
235
236static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
237{
238 int res = 0;
239
240 might_sleep();
241
242 if (*timeout <= 0)
243 *timeout = NFS4_POLL_RETRY_MIN;
244 if (*timeout > NFS4_POLL_RETRY_MAX)
245 *timeout = NFS4_POLL_RETRY_MAX;
246 schedule_timeout_killable(*timeout);
247 if (fatal_signal_pending(current))
248 res = -ERESTARTSYS;
249 *timeout <<= 1;
250 return res;
251}
252
253/* This is the error handling routine for processes that are allowed
254 * to sleep.
255 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800256static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500257{
258 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500259 struct nfs4_state *state = exception->state;
Trond Myklebust65de8722008-12-23 15:21:44 -0500260 int ret = errorcode;
261
262 exception->retry = 0;
263 switch(errorcode) {
264 case 0:
265 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500266 case -NFS4ERR_ADMIN_REVOKED:
267 case -NFS4ERR_BAD_STATEID:
268 case -NFS4ERR_OPENMODE:
269 if (state == NULL)
270 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500271 nfs4_schedule_stateid_recovery(server, state);
272 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -0400273 case -NFS4ERR_EXPIRED:
274 if (state != NULL)
275 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust65de8722008-12-23 15:21:44 -0500276 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500277 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500278 nfs4_schedule_lease_recovery(clp);
279 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500280#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400281 case -NFS4ERR_BADSESSION:
282 case -NFS4ERR_BADSLOT:
283 case -NFS4ERR_BAD_HIGH_SLOT:
284 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
285 case -NFS4ERR_DEADSESSION:
286 case -NFS4ERR_SEQ_FALSE_RETRY:
287 case -NFS4ERR_SEQ_MISORDERED:
288 dprintk("%s ERROR: %d Reset session\n", __func__,
289 errorcode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500290 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -0400291 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500292 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500293#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500294 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500295 if (exception->timeout > HZ) {
296 /* We have retried a decent amount, time to
297 * fail
298 */
299 ret = -EBUSY;
300 break;
301 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500302 case -NFS4ERR_GRACE:
303 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500304 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500305 ret = nfs4_delay(server->client, &exception->timeout);
306 if (ret != 0)
307 break;
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400308 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500309 case -NFS4ERR_OLD_STATEID:
310 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800311 break;
312 case -NFS4ERR_BADOWNER:
313 /* The following works around a Linux server bug! */
314 case -NFS4ERR_BADNAME:
315 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
316 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
317 exception->retry = 1;
318 printk(KERN_WARNING "NFS: v4 server %s "
319 "does not accept raw "
320 "uid/gids. "
321 "Reenabling the idmapper.\n",
322 server->nfs_client->cl_hostname);
323 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500324 }
325 /* We failed to handle the error */
326 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500327wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500328 ret = nfs4_wait_clnt_recover(clp);
329 if (ret == 0)
330 exception->retry = 1;
331 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500332}
333
334
Trond Myklebust452e9352010-07-31 14:29:06 -0400335static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 spin_lock(&clp->cl_lock);
338 if (time_before(clp->cl_last_renewal,timestamp))
339 clp->cl_last_renewal = timestamp;
340 spin_unlock(&clp->cl_lock);
341}
342
Trond Myklebust452e9352010-07-31 14:29:06 -0400343static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
344{
345 do_renew_lease(server->nfs_client, timestamp);
346}
347
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400348#if defined(CONFIG_NFS_V4_1)
349
Benny Halevy510b8172009-04-01 09:22:14 -0400350/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400351 * nfs4_free_slot - free a slot and efficiently update slot table.
352 *
353 * freeing a slot is trivially done by clearing its respective bit
354 * in the bitmap.
355 * If the freed slotid equals highest_used_slotid we want to update it
356 * so that the server would be able to size down the slot table if needed,
357 * otherwise we know that the highest_used_slotid is still in use.
358 * When updating highest_used_slotid there may be "holes" in the bitmap
359 * so we need to scan down from highest_used_slotid to 0 looking for the now
360 * highest slotid in use.
361 * If none found, highest_used_slotid is set to -1.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500362 *
363 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400364 */
365static void
Andy Adamsonaabd0b42011-11-09 13:58:22 -0500366nfs4_free_slot(struct nfs4_slot_table *tbl, u8 free_slotid)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400367{
368 int slotid = free_slotid;
369
Benny Halevydfb4f3092010-09-24 09:17:01 -0400370 BUG_ON(slotid < 0 || slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400371 /* clear used bit in bitmap */
372 __clear_bit(slotid, tbl->used_slots);
373
374 /* update highest_used_slotid when it is freed */
375 if (slotid == tbl->highest_used_slotid) {
376 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500377 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400378 tbl->highest_used_slotid = slotid;
379 else
380 tbl->highest_used_slotid = -1;
381 }
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400382 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
383 free_slotid, tbl->highest_used_slotid);
384}
385
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800386/*
Andy Adamson42acd022011-01-06 02:04:34 +0000387 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800388 */
Andy Adamson42acd022011-01-06 02:04:34 +0000389static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800390{
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800391 struct rpc_task *task;
392
Trond Myklebusta2118c32010-06-16 09:52:26 -0400393 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800394 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
395 if (task)
396 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800397 return;
398 }
399
400 if (ses->fc_slot_table.highest_used_slotid != -1)
401 return;
402
Andy Adamson42acd022011-01-06 02:04:34 +0000403 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
404 complete(&ses->fc_slot_table.complete);
405}
406
407/*
408 * Signal state manager thread if session back channel is drained
409 */
410void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
411{
412 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
413 ses->bc_slot_table.highest_used_slotid != -1)
414 return;
415 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
416 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800417}
418
Trond Myklebustd185a332010-06-16 09:52:25 -0400419static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400420{
421 struct nfs4_slot_table *tbl;
422
Trond Myklebustd185a332010-06-16 09:52:25 -0400423 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400424 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400425 /* just wake up the next guy waiting since
426 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500427 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500428 return;
Andy Adamson13615872009-04-01 09:22:17 -0400429 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500430
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500431 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonaabd0b42011-11-09 13:58:22 -0500432 nfs4_free_slot(tbl, res->sr_slot - tbl->slots);
Andy Adamson42acd022011-01-06 02:04:34 +0000433 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500434 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400435 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400436}
437
Trond Myklebust14516c32010-07-31 14:29:06 -0400438static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400439{
440 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400441 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400442
443 /*
444 * sr_status remains 1 if an RPC level error occurred. The server
445 * may or may not have processed the sequence operation..
446 * Proceed as if the server received and processed the sequence
447 * operation.
448 */
449 if (res->sr_status == 1)
450 res->sr_status = NFS_OK;
451
Bryan Schumaker468f8612011-04-18 15:57:32 -0400452 /* don't increment the sequence number if the task wasn't sent */
453 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400454 goto out;
455
Andy Adamson691daf32009-12-04 15:55:39 -0500456 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400457 switch (res->sr_status) {
458 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400459 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400460 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400461 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400462 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400463 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500464 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500465 if (res->sr_status_flags != 0)
466 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400467 break;
468 case -NFS4ERR_DELAY:
469 /* The server detected a resend of the RPC call and
470 * returned NFS4ERR_DELAY as per Section 2.10.6.2
471 * of RFC5661.
472 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200473 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400474 __func__,
475 res->sr_slot - res->sr_session->fc_slot_table.slots,
476 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400477 goto out_retry;
478 default:
479 /* Just update the slot sequence no. */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400480 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400481 }
482out:
483 /* The session may be reset by one of the error handlers. */
484 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400485 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400486 return 1;
487out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400488 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400489 goto out;
490 rpc_delay(task, NFS4_POLL_RETRY_MAX);
491 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400492}
493
Trond Myklebust14516c32010-07-31 14:29:06 -0400494static int nfs4_sequence_done(struct rpc_task *task,
495 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400496{
Trond Myklebust14516c32010-07-31 14:29:06 -0400497 if (res->sr_session == NULL)
498 return 1;
499 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400500}
501
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400502/*
Benny Halevy510b8172009-04-01 09:22:14 -0400503 * nfs4_find_slot - efficiently look for a free slot
504 *
505 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
506 * If found, we mark the slot as used, update the highest_used_slotid,
507 * and respectively set up the sequence operation args.
508 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400509 *
510 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400511 */
512static u8
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800513nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400514{
515 int slotid;
516 u8 ret_id = NFS4_MAX_SLOT_TABLE;
517 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
518
Benny Halevy510b8172009-04-01 09:22:14 -0400519 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
520 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
521 tbl->max_slots);
522 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
523 if (slotid >= tbl->max_slots)
524 goto out;
525 __set_bit(slotid, tbl->used_slots);
526 if (slotid > tbl->highest_used_slotid)
527 tbl->highest_used_slotid = slotid;
528 ret_id = slotid;
529out:
530 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
531 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400532 return ret_id;
533}
534
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000535int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400536 struct nfs4_sequence_args *args,
537 struct nfs4_sequence_res *res,
538 int cache_reply,
539 struct rpc_task *task)
540{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400541 struct nfs4_slot *slot;
542 struct nfs4_slot_table *tbl;
543 u8 slotid;
544
545 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400546 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400547 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400548 return 0;
549
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400550 tbl = &session->fc_slot_table;
551
552 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400553 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800554 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500555 /* The state manager will wait until the slot table is empty */
Andy Adamson05f0d232009-12-04 15:55:37 -0500556 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400557 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500558 dprintk("%s session is draining\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500559 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400560 }
561
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800562 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
563 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
564 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
565 spin_unlock(&tbl->slot_tbl_lock);
566 dprintk("%s enforce FIFO order\n", __func__);
567 return -EAGAIN;
568 }
569
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800570 slotid = nfs4_find_slot(tbl);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400571 if (slotid == NFS4_MAX_SLOT_TABLE) {
572 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
573 spin_unlock(&tbl->slot_tbl_lock);
574 dprintk("<-- %s: no free slots\n", __func__);
575 return -EAGAIN;
576 }
577 spin_unlock(&tbl->slot_tbl_lock);
578
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800579 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400580 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400581 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400582 args->sa_slotid = slotid;
583 args->sa_cache_this = cache_reply;
584
585 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
586
Benny Halevy99fe60d2009-04-01 09:22:29 -0400587 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400588 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400589 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400590 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400591 /*
592 * sr_status is only set in decode_sequence, and so will remain
593 * set to 1 if an rpc level failure occurs.
594 */
595 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400596 return 0;
597}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000598EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400599
Trond Myklebust035168a2010-06-16 09:52:26 -0400600int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400601 struct nfs4_sequence_args *args,
602 struct nfs4_sequence_res *res,
603 int cache_reply,
604 struct rpc_task *task)
605{
Trond Myklebust035168a2010-06-16 09:52:26 -0400606 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400607 int ret = 0;
608
Trond Myklebust035168a2010-06-16 09:52:26 -0400609 if (session == NULL) {
610 args->sa_session = NULL;
611 res->sr_session = NULL;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400612 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400613 }
614
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200615 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400616 __func__, session->clp, session, res->sr_slot ?
617 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400618
619 ret = nfs41_setup_sequence(session, args, res, cache_reply,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400620 task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400621out:
622 dprintk("<-- %s status=%d\n", __func__, ret);
623 return ret;
624}
625
626struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400627 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400628 struct nfs4_sequence_args *seq_args;
629 struct nfs4_sequence_res *seq_res;
630 int cache_reply;
631};
632
633static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
634{
635 struct nfs41_call_sync_data *data = calldata;
636
Trond Myklebust035168a2010-06-16 09:52:26 -0400637 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
638
639 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400640 data->seq_res, data->cache_reply, task))
641 return;
642 rpc_call_start(task);
643}
644
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800645static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
646{
647 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
648 nfs41_call_sync_prepare(task, calldata);
649}
650
Andy Adamson69ab40c2009-04-01 09:22:19 -0400651static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
652{
653 struct nfs41_call_sync_data *data = calldata;
654
Trond Myklebust14516c32010-07-31 14:29:06 -0400655 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400656}
657
Andy Adamsonce5039c2009-04-01 09:22:13 -0400658struct rpc_call_ops nfs41_call_sync_ops = {
659 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400660 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400661};
662
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800663struct rpc_call_ops nfs41_call_priv_sync_ops = {
664 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
665 .rpc_call_done = nfs41_call_sync_done,
666};
667
Bryan Schumaker7c513052011-03-24 17:12:24 +0000668static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
669 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400670 struct rpc_message *msg,
671 struct nfs4_sequence_args *args,
672 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800673 int cache_reply,
674 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400675{
676 int ret;
677 struct rpc_task *task;
678 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400679 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400680 .seq_args = args,
681 .seq_res = res,
682 .cache_reply = cache_reply,
683 };
684 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000685 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400686 .rpc_message = msg,
687 .callback_ops = &nfs41_call_sync_ops,
688 .callback_data = &data
689 };
690
Benny Halevydfb4f3092010-09-24 09:17:01 -0400691 res->sr_slot = NULL;
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800692 if (privileged)
693 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400694 task = rpc_run_task(&task_setup);
695 if (IS_ERR(task))
696 ret = PTR_ERR(task);
697 else {
698 ret = task->tk_status;
699 rpc_put_task(task);
700 }
701 return ret;
702}
703
Bryan Schumaker7c513052011-03-24 17:12:24 +0000704int _nfs4_call_sync_session(struct rpc_clnt *clnt,
705 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400706 struct rpc_message *msg,
707 struct nfs4_sequence_args *args,
708 struct nfs4_sequence_res *res,
709 int cache_reply)
710{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000711 return nfs4_call_sync_sequence(clnt, server, msg, args, res, cache_reply, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400712}
713
Trond Myklebustdf896452010-06-16 09:52:26 -0400714#else
Trond Myklebust14516c32010-07-31 14:29:06 -0400715static int nfs4_sequence_done(struct rpc_task *task,
716 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400717{
Trond Myklebust14516c32010-07-31 14:29:06 -0400718 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400719}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400720#endif /* CONFIG_NFS_V4_1 */
721
Bryan Schumaker7c513052011-03-24 17:12:24 +0000722int _nfs4_call_sync(struct rpc_clnt *clnt,
723 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400724 struct rpc_message *msg,
725 struct nfs4_sequence_args *args,
726 struct nfs4_sequence_res *res,
727 int cache_reply)
728{
Benny Halevyf3752972009-04-01 09:22:04 -0400729 args->sa_session = res->sr_session = NULL;
Bryan Schumaker7c513052011-03-24 17:12:24 +0000730 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400731}
732
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000733static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000734int nfs4_call_sync(struct rpc_clnt *clnt,
735 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000736 struct rpc_message *msg,
737 struct nfs4_sequence_args *args,
738 struct nfs4_sequence_res *res,
739 int cache_reply)
740{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000741 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
742 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000743}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400744
Trond Myklebust38478b22006-05-25 01:40:57 -0400745static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
Trond Myklebust38478b22006-05-25 01:40:57 -0400747 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Trond Myklebust38478b22006-05-25 01:40:57 -0400749 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400750 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700751 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400752 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700753 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400754 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100757struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400758 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100759 struct nfs_openargs o_arg;
760 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100761 struct nfs_open_confirmargs c_arg;
762 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100763 struct nfs_fattr f_attr;
764 struct nfs_fattr dir_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100765 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400766 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100767 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400768 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100769 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100770 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400771 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100772 int rpc_status;
773 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100774};
775
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400776
777static void nfs4_init_opendata_res(struct nfs4_opendata *p)
778{
779 p->o_res.f_attr = &p->f_attr;
780 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400781 p->o_res.seqid = p->o_arg.seqid;
782 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400783 p->o_res.server = p->o_arg.server;
784 nfs_fattr_init(&p->f_attr);
785 nfs_fattr_init(&p->dir_attr);
786}
787
Al Viro82a2c1b2011-06-22 18:30:55 -0400788static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500789 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400790 const struct iattr *attrs,
791 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100792{
Al Viro82a2c1b2011-06-22 18:30:55 -0400793 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100794 struct inode *dir = parent->d_inode;
795 struct nfs_server *server = NFS_SERVER(dir);
796 struct nfs4_opendata *p;
797
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400798 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100799 if (p == NULL)
800 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400801 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100802 if (p->o_arg.seqid == NULL)
803 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400804 nfs_sb_active(dentry->d_sb);
805 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100806 p->dir = parent;
807 p->owner = sp;
808 atomic_inc(&sp->so_count);
809 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500810 p->o_arg.open_flags = flags;
811 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400812 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400813 p->o_arg.id = sp->so_owner_id.id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400814 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100815 p->o_arg.server = server;
816 p->o_arg.bitmask = server->attr_bitmask;
817 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400818 if (flags & O_CREAT) {
819 u32 *s;
820
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100821 p->o_arg.u.attrs = &p->attrs;
822 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400823 s = (u32 *) p->o_arg.u.verifier.data;
824 s[0] = jiffies;
825 s[1] = current->pid;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100826 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100827 p->c_arg.fh = &p->o_res.fh;
828 p->c_arg.stateid = &p->o_res.stateid;
829 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400830 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400831 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100832 return p;
833err_free:
834 kfree(p);
835err:
836 dput(parent);
837 return NULL;
838}
839
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400840static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100841{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400842 struct nfs4_opendata *p = container_of(kref,
843 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400844 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400845
846 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400847 if (p->state != NULL)
848 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400849 nfs4_put_state_owner(p->owner);
850 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400851 dput(p->dentry);
852 nfs_sb_deactive(sb);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400853 kfree(p);
854}
855
856static void nfs4_opendata_put(struct nfs4_opendata *p)
857{
858 if (p != NULL)
859 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100860}
861
Trond Myklebust06f814a2006-01-03 09:55:07 +0100862static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
863{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100864 int ret;
865
Trond Myklebust06f814a2006-01-03 09:55:07 +0100866 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100867 return ret;
868}
869
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500870static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400871{
872 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500873
874 if (open_mode & O_EXCL)
875 goto out;
876 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400877 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500878 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
879 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400880 break;
881 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500882 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
883 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400884 break;
885 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500886 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
887 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400888 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500889out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400890 return ret;
891}
892
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500893static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400894{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500895 if (delegation == NULL)
896 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500897 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400898 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500899 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400900 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500901 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400902 return 1;
903}
904
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500905static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100906{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500907 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100908 case FMODE_WRITE:
909 state->n_wronly++;
910 break;
911 case FMODE_READ:
912 state->n_rdonly++;
913 break;
914 case FMODE_READ|FMODE_WRITE:
915 state->n_rdwr++;
916 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500917 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100918}
919
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500920static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400921{
922 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
923 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
924 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500925 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400926 case FMODE_READ:
927 set_bit(NFS_O_RDONLY_STATE, &state->flags);
928 break;
929 case FMODE_WRITE:
930 set_bit(NFS_O_WRONLY_STATE, &state->flags);
931 break;
932 case FMODE_READ|FMODE_WRITE:
933 set_bit(NFS_O_RDWR_STATE, &state->flags);
934 }
935}
936
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500937static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400938{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400939 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500940 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400941 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400942}
943
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500944static 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 -0700945{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400946 /*
947 * Protect the call to nfs4_state_set_mode_locked and
948 * serialise the stateid update
949 */
950 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400951 if (deleg_stateid != NULL) {
952 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
953 set_bit(NFS_DELEGATED_STATE, &state->flags);
954 }
955 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500956 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400957 write_sequnlock(&state->seqlock);
958 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500959 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700960 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961}
962
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500963static 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 -0500964{
965 struct nfs_inode *nfsi = NFS_I(state->inode);
966 struct nfs_delegation *deleg_cur;
967 int ret = 0;
968
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500969 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500970
971 rcu_read_lock();
972 deleg_cur = rcu_dereference(nfsi->delegation);
973 if (deleg_cur == NULL)
974 goto no_delegation;
975
976 spin_lock(&deleg_cur->lock);
977 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500978 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500979 goto no_delegation_unlock;
980
981 if (delegation == NULL)
982 delegation = &deleg_cur->stateid;
983 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
984 goto no_delegation_unlock;
985
Trond Myklebustb7391f42008-12-23 15:21:52 -0500986 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500987 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500988 ret = 1;
989no_delegation_unlock:
990 spin_unlock(&deleg_cur->lock);
991no_delegation:
992 rcu_read_unlock();
993
994 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500995 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500996 ret = 1;
997 }
998
999 return ret;
1000}
1001
1002
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001003static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001004{
1005 struct nfs_delegation *delegation;
1006
1007 rcu_read_lock();
1008 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001009 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001010 rcu_read_unlock();
1011 return;
1012 }
1013 rcu_read_unlock();
1014 nfs_inode_return_delegation(inode);
1015}
1016
Trond Myklebust6ee41262007-07-08 14:11:36 -04001017static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001018{
1019 struct nfs4_state *state = opendata->state;
1020 struct nfs_inode *nfsi = NFS_I(state->inode);
1021 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001022 int open_mode = opendata->o_arg.open_flags & O_EXCL;
1023 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001024 nfs4_stateid stateid;
1025 int ret = -EAGAIN;
1026
Trond Myklebustaac00a82007-07-05 19:02:21 -04001027 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001028 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001029 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001030 if (can_open_cached(state, fmode, open_mode)) {
1031 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001032 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001033 goto out_return_state;
1034 }
1035 spin_unlock(&state->owner->so_lock);
1036 }
Trond Myklebust34310432008-12-23 15:21:38 -05001037 rcu_read_lock();
1038 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001039 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001040 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001041 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001042 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001043 /* Save the delegation */
1044 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1045 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001046 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001047 if (ret != 0)
1048 goto out;
1049 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001050
1051 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001052 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001053 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001054 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001055out:
1056 return ERR_PTR(ret);
1057out_return_state:
1058 atomic_inc(&state->count);
1059 return state;
1060}
1061
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001062static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1063{
1064 struct inode *inode;
1065 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001066 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001067 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001068
Trond Myklebustaac00a82007-07-05 19:02:21 -04001069 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001070 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001071 goto out;
1072 }
1073
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001074 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001075 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001076 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001077 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001078 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001079 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001080 goto err;
1081 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001082 state = nfs4_get_open_state(inode, data->owner);
1083 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001084 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001085 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001086 int delegation_flags = 0;
1087
Trond Myklebust003707c2007-07-05 18:07:55 -04001088 rcu_read_lock();
1089 delegation = rcu_dereference(NFS_I(inode)->delegation);
1090 if (delegation)
1091 delegation_flags = delegation->flags;
1092 rcu_read_unlock();
Trond Myklebust652f89f2011-12-09 19:05:58 -05001093 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1094 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1095 "returning a delegation for "
1096 "OPEN(CLAIM_DELEGATE_CUR)\n",
1097 NFS_CLIENT(inode)->cl_server);
1098 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001099 nfs_inode_set_delegation(state->inode,
1100 data->owner->so_cred,
1101 &data->o_res);
1102 else
1103 nfs_inode_reclaim_delegation(state->inode,
1104 data->owner->so_cred,
1105 &data->o_res);
1106 }
Trond Myklebust34310432008-12-23 15:21:38 -05001107
1108 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001109 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001110 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001111out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001112 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001113err_put_inode:
1114 iput(inode);
1115err:
1116 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001117}
1118
Trond Myklebust864472e2006-01-03 09:55:15 +01001119static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1120{
1121 struct nfs_inode *nfsi = NFS_I(state->inode);
1122 struct nfs_open_context *ctx;
1123
1124 spin_lock(&state->inode->i_lock);
1125 list_for_each_entry(ctx, &nfsi->open_files, list) {
1126 if (ctx->state != state)
1127 continue;
1128 get_nfs_open_context(ctx);
1129 spin_unlock(&state->inode->i_lock);
1130 return ctx;
1131 }
1132 spin_unlock(&state->inode->i_lock);
1133 return ERR_PTR(-ENOENT);
1134}
1135
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001136static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1137{
1138 struct nfs4_opendata *opendata;
1139
Al Viro3d4ff432011-06-22 18:40:12 -04001140 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001141 if (opendata == NULL)
1142 return ERR_PTR(-ENOMEM);
1143 opendata->state = state;
1144 atomic_inc(&state->count);
1145 return opendata;
1146}
1147
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001148static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001149{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001150 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001151 int ret;
1152
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001153 opendata->o_arg.open_flags = 0;
1154 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001155 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1156 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1157 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001158 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001159 if (ret != 0)
1160 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001161 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001162 if (IS_ERR(newstate))
1163 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001164 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001165 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001166 return 0;
1167}
1168
1169static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1170{
Trond Myklebust864472e2006-01-03 09:55:15 +01001171 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001172 int ret;
1173
1174 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001175 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001176 smp_rmb();
1177 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001178 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001179 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001180 if (ret != 0)
1181 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001182 if (newstate != state)
1183 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001184 }
1185 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001186 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001187 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001188 if (ret != 0)
1189 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001190 if (newstate != state)
1191 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001192 }
1193 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001194 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001195 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001196 if (ret != 0)
1197 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001198 if (newstate != state)
1199 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001200 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001201 /*
1202 * We may have performed cached opens for all three recoveries.
1203 * Check if we need to update the current stateid.
1204 */
1205 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1206 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001207 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001208 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1209 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001210 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001211 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001212 return 0;
1213}
1214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215/*
1216 * OPEN_RECLAIM:
1217 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001219static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001221 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001222 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001223 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 int status;
1225
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001226 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1227 if (IS_ERR(opendata))
1228 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001229 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1230 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001231 rcu_read_lock();
1232 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001233 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001234 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001235 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001236 opendata->o_arg.u.delegation_type = delegation_type;
1237 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001238 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return status;
1240}
1241
Trond Myklebust539cd032007-06-05 11:46:42 -04001242static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
1244 struct nfs_server *server = NFS_SERVER(state->inode);
1245 struct nfs4_exception exception = { };
1246 int err;
1247 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001248 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001249 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001250 break;
1251 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 } while (exception.retry);
1253 return err;
1254}
1255
Trond Myklebust864472e2006-01-03 09:55:15 +01001256static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1257{
1258 struct nfs_open_context *ctx;
1259 int ret;
1260
1261 ctx = nfs4_state_find_open_context(state);
1262 if (IS_ERR(ctx))
1263 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001264 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001265 put_nfs_open_context(ctx);
1266 return ret;
1267}
1268
Trond Myklebust13437e12007-07-06 15:10:43 -04001269static 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 -07001270{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001271 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001272 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001274 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1275 if (IS_ERR(opendata))
1276 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001277 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001278 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001279 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001280 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001281 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001282 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283}
1284
Trond Myklebust13437e12007-07-06 15:10:43 -04001285int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
1287 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001288 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 int err;
1290 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001291 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 switch (err) {
1293 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001294 case -ENOENT:
1295 case -ESTALE:
1296 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001297 case -NFS4ERR_BADSESSION:
1298 case -NFS4ERR_BADSLOT:
1299 case -NFS4ERR_BAD_HIGH_SLOT:
1300 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1301 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001302 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001303 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 case -NFS4ERR_STALE_CLIENTID:
1305 case -NFS4ERR_STALE_STATEID:
1306 case -NFS4ERR_EXPIRED:
1307 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001308 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001309 goto out;
1310 case -ERESTARTSYS:
1311 /*
1312 * The show must go on: exit, but mark the
1313 * stateid as needing recovery.
1314 */
1315 case -NFS4ERR_ADMIN_REVOKED:
1316 case -NFS4ERR_BAD_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001317 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001318 case -EKEYEXPIRED:
1319 /*
1320 * User RPCSEC_GSS context has expired.
1321 * We cannot recover this stateid now, so
1322 * skip it and allow recovery thread to
1323 * proceed.
1324 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001325 case -ENOMEM:
1326 err = 0;
1327 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 }
1329 err = nfs4_handle_exception(server, err, &exception);
1330 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001331out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 return err;
1333}
1334
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001335static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1336{
1337 struct nfs4_opendata *data = calldata;
1338
1339 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001340 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001341 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1342 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001343 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001344 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001345 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001346 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001347}
1348
1349static void nfs4_open_confirm_release(void *calldata)
1350{
1351 struct nfs4_opendata *data = calldata;
1352 struct nfs4_state *state = NULL;
1353
1354 /* If this request hasn't been cancelled, do nothing */
1355 if (data->cancelled == 0)
1356 goto out_free;
1357 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001358 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001359 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001360 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001361 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001362 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001363out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001364 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001365}
1366
1367static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001368 .rpc_call_done = nfs4_open_confirm_done,
1369 .rpc_release = nfs4_open_confirm_release,
1370};
1371
1372/*
1373 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1374 */
1375static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1376{
1377 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1378 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001379 struct rpc_message msg = {
1380 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1381 .rpc_argp = &data->c_arg,
1382 .rpc_resp = &data->c_res,
1383 .rpc_cred = data->owner->so_cred,
1384 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001385 struct rpc_task_setup task_setup_data = {
1386 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001387 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001388 .callback_ops = &nfs4_open_confirm_ops,
1389 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001390 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001391 .flags = RPC_TASK_ASYNC,
1392 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 int status;
1394
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001395 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001396 data->rpc_done = 0;
1397 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001398 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001399 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001400 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001401 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001402 status = nfs4_wait_for_completion_rpc_task(task);
1403 if (status != 0) {
1404 data->cancelled = 1;
1405 smp_wmb();
1406 } else
1407 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001408 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 return status;
1410}
1411
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001412static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001414 struct nfs4_opendata *data = calldata;
1415 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001416
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001417 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1418 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001419 /*
1420 * Check if we still need to send an OPEN call, or if we can use
1421 * a delegation instead.
1422 */
1423 if (data->state != NULL) {
1424 struct nfs_delegation *delegation;
1425
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001426 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001427 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001428 rcu_read_lock();
1429 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001430 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1431 can_open_delegated(delegation, data->o_arg.fmode))
1432 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001433 rcu_read_unlock();
1434 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001435 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001436 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001437 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001438 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001439 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001440 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1441 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001442 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001443 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001444 &data->o_arg.seq_args,
1445 &data->o_res.seq_res, 1, task))
1446 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001447 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001448 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001449unlock_no_action:
1450 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001451out_no_action:
1452 task->tk_action = NULL;
1453
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001454}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001456static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1457{
1458 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1459 nfs4_open_prepare(task, calldata);
1460}
1461
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001462static void nfs4_open_done(struct rpc_task *task, void *calldata)
1463{
1464 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001466 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001467
Trond Myklebust14516c32010-07-31 14:29:06 -04001468 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1469 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001470
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001471 if (task->tk_status == 0) {
1472 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001473 case S_IFREG:
1474 break;
1475 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001476 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001477 break;
1478 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001479 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001480 break;
1481 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001482 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001483 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001484 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001485 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1486 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001487 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001488 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001489}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001490
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001491static void nfs4_open_release(void *calldata)
1492{
1493 struct nfs4_opendata *data = calldata;
1494 struct nfs4_state *state = NULL;
1495
1496 /* If this request hasn't been cancelled, do nothing */
1497 if (data->cancelled == 0)
1498 goto out_free;
1499 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001500 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001501 goto out_free;
1502 /* In case we need an open_confirm, no cleanup! */
1503 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1504 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001505 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001506 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001507 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001508out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001509 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001510}
1511
1512static const struct rpc_call_ops nfs4_open_ops = {
1513 .rpc_call_prepare = nfs4_open_prepare,
1514 .rpc_call_done = nfs4_open_done,
1515 .rpc_release = nfs4_open_release,
1516};
1517
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001518static const struct rpc_call_ops nfs4_recover_open_ops = {
1519 .rpc_call_prepare = nfs4_recover_open_prepare,
1520 .rpc_call_done = nfs4_open_done,
1521 .rpc_release = nfs4_open_release,
1522};
1523
1524static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001525{
1526 struct inode *dir = data->dir->d_inode;
1527 struct nfs_server *server = NFS_SERVER(dir);
1528 struct nfs_openargs *o_arg = &data->o_arg;
1529 struct nfs_openres *o_res = &data->o_res;
1530 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001531 struct rpc_message msg = {
1532 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1533 .rpc_argp = o_arg,
1534 .rpc_resp = o_res,
1535 .rpc_cred = data->owner->so_cred,
1536 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001537 struct rpc_task_setup task_setup_data = {
1538 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001539 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001540 .callback_ops = &nfs4_open_ops,
1541 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001542 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001543 .flags = RPC_TASK_ASYNC,
1544 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001545 int status;
1546
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001547 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001548 data->rpc_done = 0;
1549 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001550 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001551 if (isrecover)
1552 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001553 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001554 if (IS_ERR(task))
1555 return PTR_ERR(task);
1556 status = nfs4_wait_for_completion_rpc_task(task);
1557 if (status != 0) {
1558 data->cancelled = 1;
1559 smp_wmb();
1560 } else
1561 status = data->rpc_status;
1562 rpc_put_task(task);
1563
1564 return status;
1565}
1566
1567static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1568{
1569 struct inode *dir = data->dir->d_inode;
1570 struct nfs_openres *o_res = &data->o_res;
1571 int status;
1572
1573 status = nfs4_run_open_task(data, 1);
1574 if (status != 0 || !data->rpc_done)
1575 return status;
1576
1577 nfs_refresh_inode(dir, o_res->dir_attr);
1578
1579 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1580 status = _nfs4_proc_open_confirm(data);
1581 if (status != 0)
1582 return status;
1583 }
1584
1585 return status;
1586}
1587
1588/*
1589 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1590 */
1591static int _nfs4_proc_open(struct nfs4_opendata *data)
1592{
1593 struct inode *dir = data->dir->d_inode;
1594 struct nfs_server *server = NFS_SERVER(dir);
1595 struct nfs_openargs *o_arg = &data->o_arg;
1596 struct nfs_openres *o_res = &data->o_res;
1597 int status;
1598
1599 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001600 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001601 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001602 if (status != 0) {
1603 if (status == -NFS4ERR_BADNAME &&
1604 !(o_arg->open_flags & O_CREAT))
1605 return -ENOENT;
1606 return status;
1607 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001608
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001609 if (o_arg->open_flags & O_CREAT) {
1610 update_changeattr(dir, &o_res->cinfo);
1611 nfs_post_op_update_inode(dir, o_res->dir_attr);
1612 } else
1613 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001614 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1615 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001617 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001619 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 }
1621 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001622 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001623 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624}
1625
Andy Adamsond83217c2011-03-01 01:34:17 +00001626static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001627{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001628 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001629 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001630
Trond Myklebusta78cb572009-08-09 15:06:19 -04001631 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001632 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001633 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001634 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001635 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1636 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001637 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001638 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001639 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001640 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001641 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001642}
1643
Andy Adamsond83217c2011-03-01 01:34:17 +00001644static int nfs4_recover_expired_lease(struct nfs_server *server)
1645{
1646 return nfs4_client_recover_expired_lease(server->nfs_client);
1647}
1648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649/*
1650 * OPEN_EXPIRED:
1651 * reclaim state on the server after a network partition.
1652 * Assumes caller holds the appropriate lock
1653 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001654static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001656 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001657 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001659 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1660 if (IS_ERR(opendata))
1661 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001662 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001663 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001664 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001665 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001666 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667}
1668
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001669static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001670{
Trond Myklebust539cd032007-06-05 11:46:42 -04001671 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001672 struct nfs4_exception exception = { };
1673 int err;
1674
1675 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001676 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001677 switch (err) {
1678 default:
1679 goto out;
1680 case -NFS4ERR_GRACE:
1681 case -NFS4ERR_DELAY:
1682 nfs4_handle_exception(server, err, &exception);
1683 err = 0;
1684 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001685 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001686out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001687 return err;
1688}
1689
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1691{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001693 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Trond Myklebust864472e2006-01-03 09:55:15 +01001695 ctx = nfs4_state_find_open_context(state);
1696 if (IS_ERR(ctx))
1697 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001698 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001699 put_nfs_open_context(ctx);
1700 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701}
1702
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001703#if defined(CONFIG_NFS_V4_1)
1704static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1705{
1706 int status;
1707 struct nfs_server *server = NFS_SERVER(state->inode);
1708
1709 status = nfs41_test_stateid(server, state);
1710 if (status == NFS_OK)
1711 return 0;
1712 nfs41_free_stateid(server, state);
1713 return nfs4_open_expired(sp, state);
1714}
1715#endif
1716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001718 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1719 * fields corresponding to attributes that were used to store the verifier.
1720 * Make sure we clobber those fields in the later setattr call
1721 */
1722static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1723{
1724 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1725 !(sattr->ia_valid & ATTR_ATIME_SET))
1726 sattr->ia_valid |= ATTR_ATIME;
1727
1728 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1729 !(sattr->ia_valid & ATTR_MTIME_SET))
1730 sattr->ia_valid |= ATTR_MTIME;
1731}
1732
1733/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001734 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 */
Al Viro82a2c1b2011-06-22 18:30:55 -04001736static int _nfs4_do_open(struct inode *dir, struct dentry *dentry, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
1738 struct nfs4_state_owner *sp;
1739 struct nfs4_state *state = NULL;
1740 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001741 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001742 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
1744 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 status = -ENOMEM;
1746 if (!(sp = nfs4_get_state_owner(server, cred))) {
1747 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1748 goto out_err;
1749 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001750 status = nfs4_recover_expired_lease(server);
1751 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001752 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001753 if (dentry->d_inode != NULL)
1754 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001755 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001756 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001757 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001758 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
Al Viro82a2c1b2011-06-22 18:30:55 -04001760 if (dentry->d_inode != NULL)
1761 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001762
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001763 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001765 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001767 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001768 status = PTR_ERR(state);
1769 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001770 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001771 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001772 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001773
1774 if (opendata->o_arg.open_flags & O_EXCL) {
1775 nfs4_exclusive_attrset(opendata, sattr);
1776
1777 nfs_fattr_init(opendata->o_res.f_attr);
1778 status = nfs4_do_setattr(state->inode, cred,
1779 opendata->o_res.f_attr, sattr,
1780 state);
1781 if (status == 0)
1782 nfs_setattr_update_inode(state->inode, sattr);
1783 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1784 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001785 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 *res = state;
1788 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001789err_opendata_put:
1790 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001791err_put_state_owner:
1792 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 *res = NULL;
1795 return status;
1796}
1797
1798
Al Viro82a2c1b2011-06-22 18:30:55 -04001799static struct nfs4_state *nfs4_do_open(struct inode *dir, struct dentry *dentry, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800{
1801 struct nfs4_exception exception = { };
1802 struct nfs4_state *res;
1803 int status;
1804
1805 do {
Al Viro82a2c1b2011-06-22 18:30:55 -04001806 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 if (status == 0)
1808 break;
1809 /* NOTE: BAD_SEQID means the server and client disagree about the
1810 * book-keeping w.r.t. state-changing operations
1811 * (OPEN/CLOSE/LOCK/LOCKU...)
1812 * It is actually a sign of a bug on the client or on the server.
1813 *
1814 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001815 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 * have unhashed the old state_owner for us, and that we can
1817 * therefore safely retry using a new one. We should still warn
1818 * the user though...
1819 */
1820 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001821 printk(KERN_WARNING "NFS: v4 server %s "
1822 " returned a bad sequence-id error!\n",
1823 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 exception.retry = 1;
1825 continue;
1826 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001827 /*
1828 * BAD_STATEID on OPEN means that the server cancelled our
1829 * state before it received the OPEN_CONFIRM.
1830 * Recover by retrying the request as per the discussion
1831 * on Page 181 of RFC3530.
1832 */
1833 if (status == -NFS4ERR_BAD_STATEID) {
1834 exception.retry = 1;
1835 continue;
1836 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001837 if (status == -EAGAIN) {
1838 /* We must have found a delegation */
1839 exception.retry = 1;
1840 continue;
1841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1843 status, &exception));
1844 } while (exception.retry);
1845 return res;
1846}
1847
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001848static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1849 struct nfs_fattr *fattr, struct iattr *sattr,
1850 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001852 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001854 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 .iap = sattr,
1856 .server = server,
1857 .bitmask = server->attr_bitmask,
1858 };
1859 struct nfs_setattrres res = {
1860 .fattr = fattr,
1861 .server = server,
1862 };
1863 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001864 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1865 .rpc_argp = &arg,
1866 .rpc_resp = &res,
1867 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001869 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001870 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
Trond Myklebust0e574af2005-10-27 22:12:38 -04001872 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001874 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1875 /* Use that stateid */
1876 } else if (state != NULL) {
Trond Myklebust77041ed2010-07-01 12:49:11 -04001877 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001878 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1880
Bryan Schumaker7c513052011-03-24 17:12:24 +00001881 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001882 if (status == 0 && state != NULL)
1883 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001884 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885}
1886
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001887static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1888 struct nfs_fattr *fattr, struct iattr *sattr,
1889 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001891 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 struct nfs4_exception exception = { };
1893 int err;
1894 do {
1895 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001896 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 &exception);
1898 } while (exception.retry);
1899 return err;
1900}
1901
1902struct nfs4_closedata {
1903 struct inode *inode;
1904 struct nfs4_state *state;
1905 struct nfs_closeargs arg;
1906 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001907 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001908 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00001909 bool roc;
1910 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911};
1912
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001913static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001914{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001915 struct nfs4_closedata *calldata = data;
1916 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04001917 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07001918
Fred Isamanf7e89172011-01-06 11:36:32 +00001919 if (calldata->roc)
1920 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001921 nfs4_put_open_state(calldata->state);
1922 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001923 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04001924 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07001925 kfree(calldata);
1926}
1927
Trond Myklebust88069f72009-12-08 08:33:16 -05001928static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1929 fmode_t fmode)
1930{
1931 spin_lock(&state->owner->so_lock);
1932 if (!(fmode & FMODE_READ))
1933 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1934 if (!(fmode & FMODE_WRITE))
1935 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1936 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1937 spin_unlock(&state->owner->so_lock);
1938}
1939
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001940static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001942 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 struct nfs_server *server = NFS_SERVER(calldata->inode);
1945
Trond Myklebust14516c32010-07-31 14:29:06 -04001946 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1947 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 /* hmm. we are done with the inode, and in the process of freeing
1949 * the state_owner. we keep this around to process errors
1950 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 switch (task->tk_status) {
1952 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00001953 if (calldata->roc)
1954 pnfs_roc_set_barrier(state->inode,
1955 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04001956 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001957 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001958 nfs4_close_clear_stateid_flags(state,
1959 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 break;
1961 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001962 case -NFS4ERR_OLD_STATEID:
1963 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001965 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001966 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001968 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1969 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001971 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001972 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973}
1974
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001975static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001977 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001978 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001979 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001980
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001981 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001982 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001983
Trond Myklebust88069f72009-12-08 08:33:16 -05001984 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1985 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001986 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001987 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001988 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001989 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001990 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1991 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1992 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001993 }
1994 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001995 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1996 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1997 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001998 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001999 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002000 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002001
2002 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002003 /* Note: exit _without_ calling nfs4_close_done */
2004 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07002005 return;
2006 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002007
Fred Isamanf7e89172011-01-06 11:36:32 +00002008 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002009 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002010 if (calldata->roc &&
2011 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2012 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2013 task, NULL);
2014 return;
2015 }
2016 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002017
Trond Myklebust516a6af2005-10-27 22:12:41 -04002018 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002019 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002020 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Andy Adamson19ddab02009-04-01 09:22:20 -04002021 &calldata->arg.seq_args, &calldata->res.seq_res,
2022 1, task))
2023 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002024 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025}
2026
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002027static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002028 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002029 .rpc_call_done = nfs4_close_done,
2030 .rpc_release = nfs4_free_closedata,
2031};
2032
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033/*
2034 * It is possible for data to be read/written from a mem-mapped file
2035 * after the sys_close call (which hits the vfs layer as a flush).
2036 * This means that we can't safely call nfsv4 close on a file until
2037 * the inode is cleared. This in turn means that we are not good
2038 * NFSv4 citizens - we do not indicate to the server to update the file's
2039 * share state even when we are done with one of the three share
2040 * stateid's in the inode.
2041 *
2042 * NOTE: Caller must be holding the sp->so_owner semaphore!
2043 */
Al Viro643168c2011-06-22 18:20:23 -04002044int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002046 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002048 struct nfs4_state_owner *sp = state->owner;
2049 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002050 struct rpc_message msg = {
2051 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2052 .rpc_cred = state->owner->so_cred,
2053 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002054 struct rpc_task_setup task_setup_data = {
2055 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002056 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002057 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002058 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002059 .flags = RPC_TASK_ASYNC,
2060 };
Trond Myklebust95121352005-10-18 14:20:12 -07002061 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002063 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002065 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002066 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002068 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002069 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002071 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002072 if (calldata->arg.seqid == NULL)
2073 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002074 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002075 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002076 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002077 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002078 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002079 calldata->roc = roc;
Al Viro643168c2011-06-22 18:20:23 -04002080 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002081
Trond Myklebust1174dd12010-12-21 10:52:24 -05002082 msg.rpc_argp = &calldata->arg;
2083 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002084 task_setup_data.callback_data = calldata;
2085 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002086 if (IS_ERR(task))
2087 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002088 status = 0;
2089 if (wait)
2090 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002091 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002092 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002093out_free_calldata:
2094 kfree(calldata);
2095out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002096 if (roc)
2097 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002098 nfs4_put_open_state(state);
2099 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002100 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101}
2102
Trond Myklebust2b484292010-09-17 10:56:51 -04002103static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002104nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 struct nfs4_state *state;
2107
Trond Myklebust565277f2007-10-15 18:17:53 -04002108 /* Protect against concurrent sillydeletes */
Al Viro3d4ff432011-06-22 18:40:12 -04002109 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002110 if (IS_ERR(state))
2111 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002112 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002113 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114}
2115
Trond Myklebust1185a552009-12-03 15:54:02 -05002116static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002117{
2118 if (ctx->state == NULL)
2119 return;
2120 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002121 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002122 else
Al Viro643168c2011-06-22 18:20:23 -04002123 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002124}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
2126static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2127{
Benny Halevy43652ad2009-04-01 09:21:54 -04002128 struct nfs4_server_caps_arg args = {
2129 .fhandle = fhandle,
2130 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 struct nfs4_server_caps_res res = {};
2132 struct rpc_message msg = {
2133 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002134 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 .rpc_resp = &res,
2136 };
2137 int status;
2138
Bryan Schumaker7c513052011-03-24 17:12:24 +00002139 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 if (status == 0) {
2141 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002142 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2143 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2144 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2145 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2146 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2148 server->caps |= NFS_CAP_ACLS;
2149 if (res.has_links != 0)
2150 server->caps |= NFS_CAP_HARDLINKS;
2151 if (res.has_symlinks != 0)
2152 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002153 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2154 server->caps |= NFS_CAP_FILEID;
2155 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2156 server->caps |= NFS_CAP_MODE;
2157 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2158 server->caps |= NFS_CAP_NLINK;
2159 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2160 server->caps |= NFS_CAP_OWNER;
2161 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2162 server->caps |= NFS_CAP_OWNER_GROUP;
2163 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2164 server->caps |= NFS_CAP_ATIME;
2165 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2166 server->caps |= NFS_CAP_CTIME;
2167 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2168 server->caps |= NFS_CAP_MTIME;
2169
Trond Myklebusta65318b2009-03-11 14:10:28 -04002170 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2171 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2172 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 server->acl_bitmask = res.acl_bitmask;
2174 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002175
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 return status;
2177}
2178
Trond Myklebust55a97592006-06-09 09:34:19 -04002179int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180{
2181 struct nfs4_exception exception = { };
2182 int err;
2183 do {
2184 err = nfs4_handle_exception(server,
2185 _nfs4_server_capabilities(server, fhandle),
2186 &exception);
2187 } while (exception.retry);
2188 return err;
2189}
2190
2191static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2192 struct nfs_fsinfo *info)
2193{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 struct nfs4_lookup_root_arg args = {
2195 .bitmask = nfs4_fattr_bitmap,
2196 };
2197 struct nfs4_lookup_res res = {
2198 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002199 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 .fh = fhandle,
2201 };
2202 struct rpc_message msg = {
2203 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2204 .rpc_argp = &args,
2205 .rpc_resp = &res,
2206 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002207
Trond Myklebust0e574af2005-10-27 22:12:38 -04002208 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002209 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210}
2211
2212static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2213 struct nfs_fsinfo *info)
2214{
2215 struct nfs4_exception exception = { };
2216 int err;
2217 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002218 err = _nfs4_lookup_root(server, fhandle, info);
2219 switch (err) {
2220 case 0:
2221 case -NFS4ERR_WRONGSEC:
2222 break;
2223 default:
2224 err = nfs4_handle_exception(server, err, &exception);
2225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 } while (exception.retry);
2227 return err;
2228}
2229
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002230static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2231 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2232{
2233 struct rpc_auth *auth;
2234 int ret;
2235
2236 auth = rpcauth_create(flavor, server->client);
2237 if (!auth) {
2238 ret = -EIO;
2239 goto out;
2240 }
2241 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002242out:
2243 return ret;
2244}
2245
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002246static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002247 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002249 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002250 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002252 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2253 flav_array[len] = RPC_AUTH_NULL;
2254 len += 1;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002255
2256 for (i = 0; i < len; i++) {
2257 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002258 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002259 continue;
2260 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002261 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002262 /*
2263 * -EACCESS could mean that the user doesn't have correct permissions
2264 * to access the mount. It could also mean that we tried to mount
2265 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2266 * existing mount programs don't handle -EACCES very well so it should
2267 * be mapped to -EPERM instead.
2268 */
2269 if (status == -EACCES)
2270 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002271 return status;
2272}
2273
2274/*
2275 * get the file handle for the "/" directory on the server
2276 */
2277static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2278 struct nfs_fsinfo *info)
2279{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002280 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002281 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002282 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2283 /*
2284 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2285 * by nfs4_map_errors() as this function exits.
2286 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002287 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 if (status == 0)
2289 status = nfs4_server_capabilities(server, fhandle);
2290 if (status == 0)
2291 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002292 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293}
2294
Andy Adamson533eb462011-06-13 18:25:56 -04002295static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002296/*
2297 * Get locations and (maybe) other attributes of a referral.
2298 * Note that we'll actually follow the referral later when
2299 * we detect fsid mismatch in inode revalidation
2300 */
Andy Adamson533eb462011-06-13 18:25:56 -04002301static int nfs4_get_referral(struct inode *dir, const struct qstr *name,
2302 struct nfs_fattr *fattr, struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002303{
2304 int status = -ENOMEM;
2305 struct page *page = NULL;
2306 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002307
2308 page = alloc_page(GFP_KERNEL);
2309 if (page == NULL)
2310 goto out;
2311 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2312 if (locations == NULL)
2313 goto out;
2314
Trond Myklebustc228fd32007-01-13 02:28:11 -05002315 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002316 if (status != 0)
2317 goto out;
2318 /* Make sure server returned a different fsid for the referral */
2319 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002320 dprintk("%s: server did not return a different fsid for"
2321 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002322 status = -EIO;
2323 goto out;
2324 }
Andy Adamson533eb462011-06-13 18:25:56 -04002325 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2326 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002327
Andy Adamson533eb462011-06-13 18:25:56 -04002328 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002329 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002330 memset(fhandle, 0, sizeof(struct nfs_fh));
2331out:
2332 if (page)
2333 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002334 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002335 return status;
2336}
2337
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2339{
2340 struct nfs4_getattr_arg args = {
2341 .fh = fhandle,
2342 .bitmask = server->attr_bitmask,
2343 };
2344 struct nfs4_getattr_res res = {
2345 .fattr = fattr,
2346 .server = server,
2347 };
2348 struct rpc_message msg = {
2349 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2350 .rpc_argp = &args,
2351 .rpc_resp = &res,
2352 };
2353
Trond Myklebust0e574af2005-10-27 22:12:38 -04002354 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002355 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356}
2357
2358static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2359{
2360 struct nfs4_exception exception = { };
2361 int err;
2362 do {
2363 err = nfs4_handle_exception(server,
2364 _nfs4_proc_getattr(server, fhandle, fattr),
2365 &exception);
2366 } while (exception.retry);
2367 return err;
2368}
2369
2370/*
2371 * The file is not closed if it is opened due to the a request to change
2372 * the size of the file. The open call will not be needed once the
2373 * VFS layer lookup-intents are implemented.
2374 *
2375 * Close is called when the inode is destroyed.
2376 * If we haven't opened the file for O_WRONLY, we
2377 * need to in the size_change case to obtain a stateid.
2378 *
2379 * Got race?
2380 * Because OPEN is always done by name in nfsv4, it is
2381 * possible that we opened a different file by the same
2382 * name. We can recognize this race condition, but we
2383 * can't do anything about it besides returning an error.
2384 *
2385 * This will be fixed with VFS changes (lookup-intent).
2386 */
2387static int
2388nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2389 struct iattr *sattr)
2390{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002391 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002392 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002393 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 int status;
2395
Benny Halevy8a1636c2010-07-14 15:43:57 -04002396 if (pnfs_ld_layoutret_on_setattr(inode))
2397 pnfs_return_layout(inode);
2398
Trond Myklebust0e574af2005-10-27 22:12:38 -04002399 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
Trond Myklebustd5308382005-11-04 15:33:38 -05002401 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002402 if (sattr->ia_valid & ATTR_FILE) {
2403 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002404
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002405 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002406 if (ctx) {
2407 cred = ctx->cred;
2408 state = ctx->state;
2409 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002410 }
2411
2412 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002413 if (status == 0)
2414 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 return status;
2416}
2417
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002418static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2419 const struct qstr *name, struct nfs_fh *fhandle,
2420 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002421{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002422 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002423 int status;
2424 struct nfs4_lookup_arg args = {
2425 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002426 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002427 .name = name,
2428 };
2429 struct nfs4_lookup_res res = {
2430 .server = server,
2431 .fattr = fattr,
2432 .fh = fhandle,
2433 };
2434 struct rpc_message msg = {
2435 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2436 .rpc_argp = &args,
2437 .rpc_resp = &res,
2438 };
2439
2440 nfs_fattr_init(fattr);
2441
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002443 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 dprintk("NFS reply lookup: %d\n", status);
2445 return status;
2446}
2447
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002448void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2449{
2450 memset(fh, 0, sizeof(struct nfs_fh));
2451 fattr->fsid.major = 1;
2452 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2453 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2454 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2455 fattr->nlink = 2;
2456}
2457
Bryan Schumaker7c513052011-03-24 17:12:24 +00002458static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2459 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460{
2461 struct nfs4_exception exception = { };
2462 int err;
2463 do {
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002464 int status;
2465
2466 status = _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr);
2467 switch (status) {
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002468 case -NFS4ERR_BADNAME:
2469 return -ENOENT;
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002470 case -NFS4ERR_MOVED:
Trond Myklebusta6f498a2011-11-07 15:50:32 -05002471 return nfs4_get_referral(dir, name, fattr, fhandle);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002472 case -NFS4ERR_WRONGSEC:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002473 nfs_fixup_secinfo_attributes(fattr, fhandle);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002474 }
2475 err = nfs4_handle_exception(NFS_SERVER(dir),
2476 status, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 } while (exception.retry);
2478 return err;
2479}
2480
2481static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2482{
Trond Myklebust76b32992007-08-10 17:45:11 -04002483 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 struct nfs4_accessargs args = {
2485 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002486 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002488 struct nfs4_accessres res = {
2489 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002490 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 struct rpc_message msg = {
2492 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2493 .rpc_argp = &args,
2494 .rpc_resp = &res,
2495 .rpc_cred = entry->cred,
2496 };
2497 int mode = entry->mask;
2498 int status;
2499
2500 /*
2501 * Determine which access bits we want to ask for...
2502 */
2503 if (mode & MAY_READ)
2504 args.access |= NFS4_ACCESS_READ;
2505 if (S_ISDIR(inode->i_mode)) {
2506 if (mode & MAY_WRITE)
2507 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2508 if (mode & MAY_EXEC)
2509 args.access |= NFS4_ACCESS_LOOKUP;
2510 } else {
2511 if (mode & MAY_WRITE)
2512 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2513 if (mode & MAY_EXEC)
2514 args.access |= NFS4_ACCESS_EXECUTE;
2515 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002516
2517 res.fattr = nfs_alloc_fattr();
2518 if (res.fattr == NULL)
2519 return -ENOMEM;
2520
Bryan Schumaker7c513052011-03-24 17:12:24 +00002521 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 if (!status) {
2523 entry->mask = 0;
2524 if (res.access & NFS4_ACCESS_READ)
2525 entry->mask |= MAY_READ;
2526 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2527 entry->mask |= MAY_WRITE;
2528 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2529 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002530 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002532 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 return status;
2534}
2535
2536static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2537{
2538 struct nfs4_exception exception = { };
2539 int err;
2540 do {
2541 err = nfs4_handle_exception(NFS_SERVER(inode),
2542 _nfs4_proc_access(inode, entry),
2543 &exception);
2544 } while (exception.retry);
2545 return err;
2546}
2547
2548/*
2549 * TODO: For the time being, we don't try to get any attributes
2550 * along with any of the zero-copy operations READ, READDIR,
2551 * READLINK, WRITE.
2552 *
2553 * In the case of the first three, we want to put the GETATTR
2554 * after the read-type operation -- this is because it is hard
2555 * to predict the length of a GETATTR response in v4, and thus
2556 * align the READ data correctly. This means that the GETATTR
2557 * may end up partially falling into the page cache, and we should
2558 * shift it into the 'tail' of the xdr_buf before processing.
2559 * To do this efficiently, we need to know the total length
2560 * of data received, which doesn't seem to be available outside
2561 * of the RPC layer.
2562 *
2563 * In the case of WRITE, we also want to put the GETATTR after
2564 * the operation -- in this case because we want to make sure
2565 * we get the post-operation mtime and size. This means that
2566 * we can't use xdr_encode_pages() as written: we need a variant
2567 * of it which would leave room in the 'tail' iovec.
2568 *
2569 * Both of these changes to the XDR layer would in fact be quite
2570 * minor, but I decided to leave them for a subsequent patch.
2571 */
2572static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2573 unsigned int pgbase, unsigned int pglen)
2574{
2575 struct nfs4_readlink args = {
2576 .fh = NFS_FH(inode),
2577 .pgbase = pgbase,
2578 .pglen = pglen,
2579 .pages = &page,
2580 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002581 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 struct rpc_message msg = {
2583 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2584 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002585 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 };
2587
Bryan Schumaker7c513052011-03-24 17:12:24 +00002588 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 -07002589}
2590
2591static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2592 unsigned int pgbase, unsigned int pglen)
2593{
2594 struct nfs4_exception exception = { };
2595 int err;
2596 do {
2597 err = nfs4_handle_exception(NFS_SERVER(inode),
2598 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2599 &exception);
2600 } while (exception.retry);
2601 return err;
2602}
2603
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604/*
2605 * Got race?
2606 * We will need to arrange for the VFS layer to provide an atomic open.
2607 * Until then, this create/open method is prone to inefficiency and race
2608 * conditions due to the lookup, create, and open VFS calls from sys_open()
2609 * placed on the wire.
2610 *
2611 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2612 * The file will be opened again in the subsequent VFS open call
2613 * (nfs4_proc_file_open).
2614 *
2615 * The open for read will just hang around to be used by any process that
2616 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2617 */
2618
2619static int
2620nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002621 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622{
Al Viro82a2c1b2011-06-22 18:30:55 -04002623 struct dentry *de = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002625 struct rpc_cred *cred = NULL;
2626 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 int status = 0;
2628
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002629 if (ctx != NULL) {
2630 cred = ctx->cred;
Al Viro3d4ff432011-06-22 18:40:12 -04002631 de = ctx->dentry;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002632 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002634 sattr->ia_mode &= ~current_umask();
Al Viro82a2c1b2011-06-22 18:30:55 -04002635 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002636 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 if (IS_ERR(state)) {
2638 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002639 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002641 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002642 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002643 if (ctx != NULL)
2644 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002645 else
Al Viro643168c2011-06-22 18:20:23 -04002646 nfs4_close_sync(state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647out:
2648 return status;
2649}
2650
2651static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2652{
Trond Myklebust16e42952005-10-27 22:12:44 -04002653 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002654 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002656 .name.len = name->len,
2657 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002658 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002660 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002661 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002662 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002664 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2665 .rpc_argp = &args,
2666 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002668 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
Trond Myklebustd3468902010-04-16 16:22:50 -04002670 res.dir_attr = nfs_alloc_fattr();
2671 if (res.dir_attr == NULL)
2672 goto out;
2673
Bryan Schumaker7c513052011-03-24 17:12:24 +00002674 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002675 if (status == 0) {
2676 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002677 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002678 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002679 nfs_free_fattr(res.dir_attr);
2680out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 return status;
2682}
2683
2684static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2685{
2686 struct nfs4_exception exception = { };
2687 int err;
2688 do {
2689 err = nfs4_handle_exception(NFS_SERVER(dir),
2690 _nfs4_proc_remove(dir, name),
2691 &exception);
2692 } while (exception.retry);
2693 return err;
2694}
2695
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002696static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002698 struct nfs_server *server = NFS_SERVER(dir);
2699 struct nfs_removeargs *args = msg->rpc_argp;
2700 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701
Trond Myklebusta65318b2009-03-11 14:10:28 -04002702 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002703 res->server = server;
Benny Halevydfb4f3092010-09-24 09:17:01 -04002704 res->seq_res.sr_slot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706}
2707
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002708static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002710 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2711
Trond Myklebust14516c32010-07-31 14:29:06 -04002712 if (!nfs4_sequence_done(task, &res->seq_res))
2713 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002714 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002715 return 0;
2716 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002717 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002718 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719}
2720
Jeff Laytond3d41522010-09-17 17:31:57 -04002721static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2722{
2723 struct nfs_server *server = NFS_SERVER(dir);
2724 struct nfs_renameargs *arg = msg->rpc_argp;
2725 struct nfs_renameres *res = msg->rpc_resp;
2726
2727 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2728 arg->bitmask = server->attr_bitmask;
2729 res->server = server;
2730}
2731
2732static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2733 struct inode *new_dir)
2734{
2735 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2736
2737 if (!nfs4_sequence_done(task, &res->seq_res))
2738 return 0;
2739 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2740 return 0;
2741
2742 update_changeattr(old_dir, &res->old_cinfo);
2743 nfs_post_op_update_inode(old_dir, res->old_fattr);
2744 update_changeattr(new_dir, &res->new_cinfo);
2745 nfs_post_op_update_inode(new_dir, res->new_fattr);
2746 return 1;
2747}
2748
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2750 struct inode *new_dir, struct qstr *new_name)
2751{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002752 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002753 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 .old_dir = NFS_FH(old_dir),
2755 .new_dir = NFS_FH(new_dir),
2756 .old_name = old_name,
2757 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002758 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002760 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002761 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002762 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 struct rpc_message msg = {
2764 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2765 .rpc_argp = &arg,
2766 .rpc_resp = &res,
2767 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002768 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769
Trond Myklebust011fff72010-04-16 16:22:49 -04002770 res.old_fattr = nfs_alloc_fattr();
2771 res.new_fattr = nfs_alloc_fattr();
2772 if (res.old_fattr == NULL || res.new_fattr == NULL)
2773 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
Bryan Schumaker7c513052011-03-24 17:12:24 +00002775 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 if (!status) {
2777 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002778 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002780 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002782out:
2783 nfs_free_fattr(res.new_fattr);
2784 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 return status;
2786}
2787
2788static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2789 struct inode *new_dir, struct qstr *new_name)
2790{
2791 struct nfs4_exception exception = { };
2792 int err;
2793 do {
2794 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2795 _nfs4_proc_rename(old_dir, old_name,
2796 new_dir, new_name),
2797 &exception);
2798 } while (exception.retry);
2799 return err;
2800}
2801
2802static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2803{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002804 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 struct nfs4_link_arg arg = {
2806 .fh = NFS_FH(inode),
2807 .dir_fh = NFS_FH(dir),
2808 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002809 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002811 struct nfs4_link_res res = {
2812 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002813 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 struct rpc_message msg = {
2815 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2816 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002817 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002819 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
Trond Myklebust136f2622010-04-16 16:22:49 -04002821 res.fattr = nfs_alloc_fattr();
2822 res.dir_attr = nfs_alloc_fattr();
2823 if (res.fattr == NULL || res.dir_attr == NULL)
2824 goto out;
2825
Bryan Schumaker7c513052011-03-24 17:12:24 +00002826 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002827 if (!status) {
2828 update_changeattr(dir, &res.cinfo);
2829 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002830 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002831 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002832out:
2833 nfs_free_fattr(res.dir_attr);
2834 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 return status;
2836}
2837
2838static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2839{
2840 struct nfs4_exception exception = { };
2841 int err;
2842 do {
2843 err = nfs4_handle_exception(NFS_SERVER(inode),
2844 _nfs4_proc_link(inode, dir, name),
2845 &exception);
2846 } while (exception.retry);
2847 return err;
2848}
2849
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002850struct nfs4_createdata {
2851 struct rpc_message msg;
2852 struct nfs4_create_arg arg;
2853 struct nfs4_create_res res;
2854 struct nfs_fh fh;
2855 struct nfs_fattr fattr;
2856 struct nfs_fattr dir_fattr;
2857};
2858
2859static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2860 struct qstr *name, struct iattr *sattr, u32 ftype)
2861{
2862 struct nfs4_createdata *data;
2863
2864 data = kzalloc(sizeof(*data), GFP_KERNEL);
2865 if (data != NULL) {
2866 struct nfs_server *server = NFS_SERVER(dir);
2867
2868 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2869 data->msg.rpc_argp = &data->arg;
2870 data->msg.rpc_resp = &data->res;
2871 data->arg.dir_fh = NFS_FH(dir);
2872 data->arg.server = server;
2873 data->arg.name = name;
2874 data->arg.attrs = sattr;
2875 data->arg.ftype = ftype;
2876 data->arg.bitmask = server->attr_bitmask;
2877 data->res.server = server;
2878 data->res.fh = &data->fh;
2879 data->res.fattr = &data->fattr;
2880 data->res.dir_fattr = &data->dir_fattr;
2881 nfs_fattr_init(data->res.fattr);
2882 nfs_fattr_init(data->res.dir_fattr);
2883 }
2884 return data;
2885}
2886
2887static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2888{
Bryan Schumaker7c513052011-03-24 17:12:24 +00002889 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00002890 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002891 if (status == 0) {
2892 update_changeattr(dir, &data->res.dir_cinfo);
2893 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2894 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2895 }
2896 return status;
2897}
2898
2899static void nfs4_free_createdata(struct nfs4_createdata *data)
2900{
2901 kfree(data);
2902}
2903
Chuck Lever4f390c12006-08-22 20:06:22 -04002904static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002905 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002907 struct nfs4_createdata *data;
2908 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909
Chuck Lever94a6d752006-08-22 20:06:23 -04002910 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002911 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002912
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002913 status = -ENOMEM;
2914 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2915 if (data == NULL)
2916 goto out;
2917
2918 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2919 data->arg.u.symlink.pages = &page;
2920 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002922 status = nfs4_do_create(dir, dentry, data);
2923
2924 nfs4_free_createdata(data);
2925out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 return status;
2927}
2928
Chuck Lever4f390c12006-08-22 20:06:22 -04002929static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002930 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931{
2932 struct nfs4_exception exception = { };
2933 int err;
2934 do {
2935 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002936 _nfs4_proc_symlink(dir, dentry, page,
2937 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 &exception);
2939 } while (exception.retry);
2940 return err;
2941}
2942
2943static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2944 struct iattr *sattr)
2945{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002946 struct nfs4_createdata *data;
2947 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002949 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2950 if (data == NULL)
2951 goto out;
2952
2953 status = nfs4_do_create(dir, dentry, data);
2954
2955 nfs4_free_createdata(data);
2956out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 return status;
2958}
2959
2960static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2961 struct iattr *sattr)
2962{
2963 struct nfs4_exception exception = { };
2964 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002965
2966 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 do {
2968 err = nfs4_handle_exception(NFS_SERVER(dir),
2969 _nfs4_proc_mkdir(dir, dentry, sattr),
2970 &exception);
2971 } while (exception.retry);
2972 return err;
2973}
2974
2975static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002976 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977{
2978 struct inode *dir = dentry->d_inode;
2979 struct nfs4_readdir_arg args = {
2980 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002981 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 .pgbase = 0,
2983 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002984 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04002985 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 };
2987 struct nfs4_readdir_res res;
2988 struct rpc_message msg = {
2989 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2990 .rpc_argp = &args,
2991 .rpc_resp = &res,
2992 .rpc_cred = cred,
2993 };
2994 int status;
2995
Harvey Harrison3110ff82008-05-02 13:42:44 -07002996 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002997 dentry->d_parent->d_name.name,
2998 dentry->d_name.name,
2999 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3001 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003002 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 -05003003 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003005 status += args.pgbase;
3006 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003007
3008 nfs_invalidate_atime(dir);
3009
Harvey Harrison3110ff82008-05-02 13:42:44 -07003010 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 return status;
3012}
3013
3014static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003015 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016{
3017 struct nfs4_exception exception = { };
3018 int err;
3019 do {
3020 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3021 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003022 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 &exception);
3024 } while (exception.retry);
3025 return err;
3026}
3027
3028static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3029 struct iattr *sattr, dev_t rdev)
3030{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003031 struct nfs4_createdata *data;
3032 int mode = sattr->ia_mode;
3033 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034
3035 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3036 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003037
3038 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3039 if (data == NULL)
3040 goto out;
3041
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003043 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003045 data->arg.ftype = NF4BLK;
3046 data->arg.u.device.specdata1 = MAJOR(rdev);
3047 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 }
3049 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003050 data->arg.ftype = NF4CHR;
3051 data->arg.u.device.specdata1 = MAJOR(rdev);
3052 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003055 status = nfs4_do_create(dir, dentry, data);
3056
3057 nfs4_free_createdata(data);
3058out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 return status;
3060}
3061
3062static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3063 struct iattr *sattr, dev_t rdev)
3064{
3065 struct nfs4_exception exception = { };
3066 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003067
3068 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 do {
3070 err = nfs4_handle_exception(NFS_SERVER(dir),
3071 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3072 &exception);
3073 } while (exception.retry);
3074 return err;
3075}
3076
3077static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3078 struct nfs_fsstat *fsstat)
3079{
3080 struct nfs4_statfs_arg args = {
3081 .fh = fhandle,
3082 .bitmask = server->attr_bitmask,
3083 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003084 struct nfs4_statfs_res res = {
3085 .fsstat = fsstat,
3086 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 struct rpc_message msg = {
3088 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3089 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003090 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 };
3092
Trond Myklebust0e574af2005-10-27 22:12:38 -04003093 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003094 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095}
3096
3097static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3098{
3099 struct nfs4_exception exception = { };
3100 int err;
3101 do {
3102 err = nfs4_handle_exception(server,
3103 _nfs4_proc_statfs(server, fhandle, fsstat),
3104 &exception);
3105 } while (exception.retry);
3106 return err;
3107}
3108
3109static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3110 struct nfs_fsinfo *fsinfo)
3111{
3112 struct nfs4_fsinfo_arg args = {
3113 .fh = fhandle,
3114 .bitmask = server->attr_bitmask,
3115 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003116 struct nfs4_fsinfo_res res = {
3117 .fsinfo = fsinfo,
3118 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 struct rpc_message msg = {
3120 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3121 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003122 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 };
3124
Bryan Schumaker7c513052011-03-24 17:12:24 +00003125 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126}
3127
3128static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3129{
3130 struct nfs4_exception exception = { };
3131 int err;
3132
3133 do {
3134 err = nfs4_handle_exception(server,
3135 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3136 &exception);
3137 } while (exception.retry);
3138 return err;
3139}
3140
3141static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3142{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003143 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3145}
3146
3147static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3148 struct nfs_pathconf *pathconf)
3149{
3150 struct nfs4_pathconf_arg args = {
3151 .fh = fhandle,
3152 .bitmask = server->attr_bitmask,
3153 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003154 struct nfs4_pathconf_res res = {
3155 .pathconf = pathconf,
3156 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 struct rpc_message msg = {
3158 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3159 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003160 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 };
3162
3163 /* None of the pathconf attributes are mandatory to implement */
3164 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3165 memset(pathconf, 0, sizeof(*pathconf));
3166 return 0;
3167 }
3168
Trond Myklebust0e574af2005-10-27 22:12:38 -04003169 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003170 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171}
3172
3173static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3174 struct nfs_pathconf *pathconf)
3175{
3176 struct nfs4_exception exception = { };
3177 int err;
3178
3179 do {
3180 err = nfs4_handle_exception(server,
3181 _nfs4_proc_pathconf(server, fhandle, pathconf),
3182 &exception);
3183 } while (exception.retry);
3184 return err;
3185}
3186
Benny Halevyd20581a2011-05-22 19:52:03 +03003187void __nfs4_read_done_cb(struct nfs_read_data *data)
3188{
3189 nfs_invalidate_atime(data->inode);
3190}
3191
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003192static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193{
Trond Myklebustec06c092006-03-20 13:44:27 -05003194 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003196 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003197 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003198 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003200
Benny Halevyd20581a2011-05-22 19:52:03 +03003201 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003203 renew_lease(server, data->timestamp);
3204 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205}
3206
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003207static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3208{
3209
3210 dprintk("--> %s\n", __func__);
3211
3212 if (!nfs4_sequence_done(task, &data->res.seq_res))
3213 return -EAGAIN;
3214
Benny Halevyd20581a2011-05-22 19:52:03 +03003215 return data->read_done_cb ? data->read_done_cb(task, data) :
3216 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003217}
3218
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003219static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003222 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003223 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224}
3225
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003226/* Reset the the nfs_read_data to send the read to the MDS. */
3227void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3228{
3229 dprintk("%s Reset task for i/o through\n", __func__);
3230 put_lseg(data->lseg);
3231 data->lseg = NULL;
3232 /* offsets will differ in the dense stripe case */
3233 data->args.offset = data->mds_offset;
3234 data->ds_clp = NULL;
3235 data->args.fh = NFS_FH(data->inode);
3236 data->read_done_cb = nfs4_read_done_cb;
3237 task->tk_ops = data->mds_ops;
3238 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3239}
3240EXPORT_SYMBOL_GPL(nfs4_reset_read);
3241
Fred Isamanb029bc92011-03-03 15:13:42 +00003242static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 struct inode *inode = data->inode;
3245
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003246 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003247 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003248 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003250 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003252 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003253 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003254 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255}
3256
Fred Isamanb029bc92011-03-03 15:13:42 +00003257static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3258{
3259 if (!nfs4_sequence_done(task, &data->res.seq_res))
3260 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003261 return data->write_done_cb ? data->write_done_cb(task, data) :
3262 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003263}
3264
Fred Isamana69aef12011-03-03 15:13:47 +00003265/* Reset the the nfs_write_data to send the write to the MDS. */
3266void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3267{
3268 dprintk("%s Reset task for i/o through\n", __func__);
3269 put_lseg(data->lseg);
3270 data->lseg = NULL;
3271 data->ds_clp = NULL;
3272 data->write_done_cb = nfs4_write_done_cb;
3273 data->args.fh = NFS_FH(data->inode);
3274 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3275 data->args.offset = data->mds_offset;
3276 data->res.fattr = &data->fattr;
3277 task->tk_ops = data->mds_ops;
3278 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3279}
3280EXPORT_SYMBOL_GPL(nfs4_reset_write);
3281
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003282static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003284 struct nfs_server *server = NFS_SERVER(data->inode);
3285
Fred Isaman7ffd1062011-03-03 15:13:46 +00003286 if (data->lseg) {
3287 data->args.bitmask = NULL;
3288 data->res.fattr = NULL;
3289 } else
3290 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isamanb029bc92011-03-03 15:13:42 +00003291 if (!data->write_done_cb)
3292 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003293 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 data->timestamp = jiffies;
3295
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003296 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297}
3298
Fred Isaman5f452432011-03-23 13:27:46 +00003299static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003302
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003303 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003304 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003305 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003307 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003308 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309}
3310
Fred Isaman5f452432011-03-23 13:27:46 +00003311static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3312{
3313 if (!nfs4_sequence_done(task, &data->res.seq_res))
3314 return -EAGAIN;
3315 return data->write_done_cb(task, data);
3316}
3317
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003318static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003320 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003321
3322 if (data->lseg) {
3323 data->args.bitmask = NULL;
3324 data->res.fattr = NULL;
3325 } else
3326 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isaman5f452432011-03-23 13:27:46 +00003327 if (!data->write_done_cb)
3328 data->write_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003329 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003330 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331}
3332
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003333struct nfs4_renewdata {
3334 struct nfs_client *client;
3335 unsigned long timestamp;
3336};
3337
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338/*
3339 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3340 * standalone procedure for queueing an asynchronous RENEW.
3341 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003342static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003343{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003344 struct nfs4_renewdata *data = calldata;
3345 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003346
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003347 if (atomic_read(&clp->cl_count) > 1)
3348 nfs4_schedule_state_renewal(clp);
3349 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003350 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003351}
3352
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003353static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003355 struct nfs4_renewdata *data = calldata;
3356 struct nfs_client *clp = data->client;
3357 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358
3359 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003360 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003361 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3362 return;
3363 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003364 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003365 return;
3366 }
3367 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003369 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370}
3371
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003372static const struct rpc_call_ops nfs4_renew_ops = {
3373 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003374 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003375};
3376
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003377static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378{
3379 struct rpc_message msg = {
3380 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3381 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003382 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003384 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003386 if (renew_flags == 0)
3387 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003388 if (!atomic_inc_not_zero(&clp->cl_count))
3389 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003390 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003391 if (data == NULL)
3392 return -ENOMEM;
3393 data->client = clp;
3394 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003396 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397}
3398
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003399static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400{
3401 struct rpc_message msg = {
3402 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3403 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003404 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 };
3406 unsigned long now = jiffies;
3407 int status;
3408
3409 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3410 if (status < 0)
3411 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003412 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 return 0;
3414}
3415
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003416static inline int nfs4_server_supports_acls(struct nfs_server *server)
3417{
3418 return (server->caps & NFS_CAP_ACLS)
3419 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3420 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3421}
3422
3423/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3424 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3425 * the stack.
3426 */
3427#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3428
3429static void buf_to_pages(const void *buf, size_t buflen,
3430 struct page **pages, unsigned int *pgbase)
3431{
3432 const void *p = buf;
3433
3434 *pgbase = offset_in_page(buf);
3435 p -= *pgbase;
3436 while (p < buf + buflen) {
3437 *(pages++) = virt_to_page(p);
3438 p += PAGE_CACHE_SIZE;
3439 }
3440}
3441
Neil Hormane9e3d722011-03-04 19:26:03 -05003442static int buf_to_pages_noslab(const void *buf, size_t buflen,
3443 struct page **pages, unsigned int *pgbase)
3444{
3445 struct page *newpage, **spages;
3446 int rc = 0;
3447 size_t len;
3448 spages = pages;
3449
3450 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003451 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003452 newpage = alloc_page(GFP_KERNEL);
3453
3454 if (newpage == NULL)
3455 goto unwind;
3456 memcpy(page_address(newpage), buf, len);
3457 buf += len;
3458 buflen -= len;
3459 *pages++ = newpage;
3460 rc++;
3461 } while (buflen != 0);
3462
3463 return rc;
3464
3465unwind:
3466 for(; rc > 0; rc--)
3467 __free_page(spages[rc-1]);
3468 return -ENOMEM;
3469}
3470
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003471struct nfs4_cached_acl {
3472 int cached;
3473 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003474 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003475};
3476
3477static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003478{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003479 struct nfs_inode *nfsi = NFS_I(inode);
3480
3481 spin_lock(&inode->i_lock);
3482 kfree(nfsi->nfs4_acl);
3483 nfsi->nfs4_acl = acl;
3484 spin_unlock(&inode->i_lock);
3485}
3486
3487static void nfs4_zap_acl_attr(struct inode *inode)
3488{
3489 nfs4_set_cached_acl(inode, NULL);
3490}
3491
3492static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3493{
3494 struct nfs_inode *nfsi = NFS_I(inode);
3495 struct nfs4_cached_acl *acl;
3496 int ret = -ENOENT;
3497
3498 spin_lock(&inode->i_lock);
3499 acl = nfsi->nfs4_acl;
3500 if (acl == NULL)
3501 goto out;
3502 if (buf == NULL) /* user is just asking for length */
3503 goto out_len;
3504 if (acl->cached == 0)
3505 goto out;
3506 ret = -ERANGE; /* see getxattr(2) man page */
3507 if (acl->len > buflen)
3508 goto out;
3509 memcpy(buf, acl->data, acl->len);
3510out_len:
3511 ret = acl->len;
3512out:
3513 spin_unlock(&inode->i_lock);
3514 return ret;
3515}
3516
3517static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3518{
3519 struct nfs4_cached_acl *acl;
3520
3521 if (buf && acl_len <= PAGE_SIZE) {
3522 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3523 if (acl == NULL)
3524 goto out;
3525 acl->cached = 1;
3526 memcpy(acl->data, buf, acl_len);
3527 } else {
3528 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3529 if (acl == NULL)
3530 goto out;
3531 acl->cached = 0;
3532 }
3533 acl->len = acl_len;
3534out:
3535 nfs4_set_cached_acl(inode, acl);
3536}
3537
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003538static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003539{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003540 struct page *pages[NFS4ACL_MAXPAGES];
3541 struct nfs_getaclargs args = {
3542 .fh = NFS_FH(inode),
3543 .acl_pages = pages,
3544 .acl_len = buflen,
3545 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003546 struct nfs_getaclres res = {
3547 .acl_len = buflen,
3548 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003549 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003550 struct rpc_message msg = {
3551 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3552 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003553 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003554 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003555 struct page *localpage = NULL;
3556 int ret;
3557
3558 if (buflen < PAGE_SIZE) {
3559 /* As long as we're doing a round trip to the server anyway,
3560 * let's be prepared for a page of acl data. */
3561 localpage = alloc_page(GFP_KERNEL);
3562 resp_buf = page_address(localpage);
3563 if (localpage == NULL)
3564 return -ENOMEM;
3565 args.acl_pages[0] = localpage;
3566 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003567 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003568 } else {
3569 resp_buf = buf;
3570 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3571 }
Bryan Schumaker7c513052011-03-24 17:12:24 +00003572 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 +00003573 if (ret)
3574 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003575 if (res.acl_len > args.acl_len)
3576 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003577 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003578 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003579 if (buf) {
3580 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003581 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003582 goto out_free;
3583 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003584 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003585 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003586 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003587out_free:
3588 if (localpage)
3589 __free_page(localpage);
3590 return ret;
3591}
3592
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003593static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3594{
3595 struct nfs4_exception exception = { };
3596 ssize_t ret;
3597 do {
3598 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3599 if (ret >= 0)
3600 break;
3601 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3602 } while (exception.retry);
3603 return ret;
3604}
3605
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003606static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3607{
3608 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003609 int ret;
3610
3611 if (!nfs4_server_supports_acls(server))
3612 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003613 ret = nfs_revalidate_inode(server, inode);
3614 if (ret < 0)
3615 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003616 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3617 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003618 ret = nfs4_read_cached_acl(inode, buf, buflen);
3619 if (ret != -ENOENT)
3620 return ret;
3621 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003622}
3623
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003624static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003625{
3626 struct nfs_server *server = NFS_SERVER(inode);
3627 struct page *pages[NFS4ACL_MAXPAGES];
3628 struct nfs_setaclargs arg = {
3629 .fh = NFS_FH(inode),
3630 .acl_pages = pages,
3631 .acl_len = buflen,
3632 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003633 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003634 struct rpc_message msg = {
3635 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3636 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003637 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003638 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003639 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003640
3641 if (!nfs4_server_supports_acls(server))
3642 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003643 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3644 if (i < 0)
3645 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003646 nfs_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003647 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003648
3649 /*
3650 * Free each page after tx, so the only ref left is
3651 * held by the network stack
3652 */
3653 for (; i > 0; i--)
3654 put_page(pages[i-1]);
3655
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003656 /*
3657 * Acl update can result in inode attribute update.
3658 * so mark the attribute cache invalid.
3659 */
3660 spin_lock(&inode->i_lock);
3661 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3662 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003663 nfs_access_zap_cache(inode);
3664 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003665 return ret;
3666}
3667
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003668static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3669{
3670 struct nfs4_exception exception = { };
3671 int err;
3672 do {
3673 err = nfs4_handle_exception(NFS_SERVER(inode),
3674 __nfs4_proc_set_acl(inode, buf, buflen),
3675 &exception);
3676 } while (exception.retry);
3677 return err;
3678}
3679
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003681nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003683 struct nfs_client *clp = server->nfs_client;
3684
3685 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686 return 0;
3687 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003688 case -NFS4ERR_ADMIN_REVOKED:
3689 case -NFS4ERR_BAD_STATEID:
3690 case -NFS4ERR_OPENMODE:
3691 if (state == NULL)
3692 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003693 nfs4_schedule_stateid_recovery(server, state);
3694 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003695 case -NFS4ERR_EXPIRED:
3696 if (state != NULL)
3697 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003699 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003700 nfs4_schedule_lease_recovery(clp);
3701 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003702#if defined(CONFIG_NFS_V4_1)
3703 case -NFS4ERR_BADSESSION:
3704 case -NFS4ERR_BADSLOT:
3705 case -NFS4ERR_BAD_HIGH_SLOT:
3706 case -NFS4ERR_DEADSESSION:
3707 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3708 case -NFS4ERR_SEQ_FALSE_RETRY:
3709 case -NFS4ERR_SEQ_MISORDERED:
3710 dprintk("%s ERROR %d, Reset session\n", __func__,
3711 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003712 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -04003713 task->tk_status = 0;
3714 return -EAGAIN;
3715#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003717 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003718 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003719 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3721 task->tk_status = 0;
3722 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003723 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 case -NFS4ERR_OLD_STATEID:
3725 task->tk_status = 0;
3726 return -EAGAIN;
3727 }
3728 task->tk_status = nfs4_map_errors(task->tk_status);
3729 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003730wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003731 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003732 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3733 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3734 task->tk_status = 0;
3735 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736}
3737
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003738int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3739 unsigned short port, struct rpc_cred *cred,
3740 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741{
3742 nfs4_verifier sc_verifier;
3743 struct nfs4_setclientid setclientid = {
3744 .sc_verifier = &sc_verifier,
3745 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003746 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 };
3748 struct rpc_message msg = {
3749 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3750 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003751 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003752 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 };
Al Virobc4785c2006-10-19 23:28:51 -07003754 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 int loop = 0;
3756 int status;
3757
Al Virobc4785c2006-10-19 23:28:51 -07003758 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3760 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3761
3762 for(;;) {
3763 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003764 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003765 clp->cl_ipaddr,
3766 rpc_peeraddr2str(clp->cl_rpcclient,
3767 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003768 rpc_peeraddr2str(clp->cl_rpcclient,
3769 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003770 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 clp->cl_id_uniquifier);
3772 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003773 sizeof(setclientid.sc_netid),
3774 rpc_peeraddr2str(clp->cl_rpcclient,
3775 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003776 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003777 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 clp->cl_ipaddr, port >> 8, port & 255);
3779
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003780 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 if (status != -NFS4ERR_CLID_INUSE)
3782 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003783 if (loop != 0) {
3784 ++clp->cl_id_uniquifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003786 }
3787 ++loop;
3788 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 }
3790 return status;
3791}
3792
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003793int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003794 struct nfs4_setclientid_res *arg,
3795 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796{
3797 struct nfs_fsinfo fsinfo;
3798 struct rpc_message msg = {
3799 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003800 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003802 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 };
3804 unsigned long now;
3805 int status;
3806
3807 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003808 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 if (status == 0) {
3810 spin_lock(&clp->cl_lock);
3811 clp->cl_lease_time = fsinfo.lease_time * HZ;
3812 clp->cl_last_renewal = now;
3813 spin_unlock(&clp->cl_lock);
3814 }
3815 return status;
3816}
3817
Trond Myklebustfe650402006-01-03 09:55:18 +01003818struct nfs4_delegreturndata {
3819 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003820 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003821 struct nfs_fh fh;
3822 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003823 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003824 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003825 int rpc_status;
3826};
3827
Trond Myklebustfe650402006-01-03 09:55:18 +01003828static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3829{
3830 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003831
Trond Myklebust14516c32010-07-31 14:29:06 -04003832 if (!nfs4_sequence_done(task, &data->res.seq_res))
3833 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003834
Ricardo Labiaga79708862009-12-07 09:23:21 -05003835 switch (task->tk_status) {
3836 case -NFS4ERR_STALE_STATEID:
3837 case -NFS4ERR_EXPIRED:
3838 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003839 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003840 break;
3841 default:
3842 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3843 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003844 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003845 return;
3846 }
3847 }
3848 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003849}
3850
3851static void nfs4_delegreturn_release(void *calldata)
3852{
Trond Myklebustfe650402006-01-03 09:55:18 +01003853 kfree(calldata);
3854}
3855
Andy Adamson938e1012009-04-01 09:22:28 -04003856#if defined(CONFIG_NFS_V4_1)
3857static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3858{
3859 struct nfs4_delegreturndata *d_data;
3860
3861 d_data = (struct nfs4_delegreturndata *)data;
3862
Trond Myklebust035168a2010-06-16 09:52:26 -04003863 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003864 &d_data->args.seq_args,
3865 &d_data->res.seq_res, 1, task))
3866 return;
3867 rpc_call_start(task);
3868}
3869#endif /* CONFIG_NFS_V4_1 */
3870
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003871static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003872#if defined(CONFIG_NFS_V4_1)
3873 .rpc_call_prepare = nfs4_delegreturn_prepare,
3874#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003875 .rpc_call_done = nfs4_delegreturn_done,
3876 .rpc_release = nfs4_delegreturn_release,
3877};
3878
Trond Myklebuste6f81072008-01-24 18:14:34 -05003879static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003880{
3881 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003882 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003883 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003884 struct rpc_message msg = {
3885 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3886 .rpc_cred = cred,
3887 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003888 struct rpc_task_setup task_setup_data = {
3889 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003890 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003891 .callback_ops = &nfs4_delegreturn_ops,
3892 .flags = RPC_TASK_ASYNC,
3893 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003894 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003895
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003896 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003897 if (data == NULL)
3898 return -ENOMEM;
3899 data->args.fhandle = &data->fh;
3900 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003901 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003902 nfs_copy_fh(&data->fh, NFS_FH(inode));
3903 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003904 data->res.fattr = &data->fattr;
3905 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003906 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003907 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003908 data->rpc_status = 0;
3909
Trond Myklebustc970aa82007-07-14 15:39:59 -04003910 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05003911 msg.rpc_argp = &data->args;
3912 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003913 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003914 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003915 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003916 if (!issync)
3917 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003918 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003919 if (status != 0)
3920 goto out;
3921 status = data->rpc_status;
3922 if (status != 0)
3923 goto out;
3924 nfs_refresh_inode(inode, &data->fattr);
3925out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003926 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003927 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928}
3929
Trond Myklebuste6f81072008-01-24 18:14:34 -05003930int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931{
3932 struct nfs_server *server = NFS_SERVER(inode);
3933 struct nfs4_exception exception = { };
3934 int err;
3935 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003936 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 switch (err) {
3938 case -NFS4ERR_STALE_STATEID:
3939 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 case 0:
3941 return 0;
3942 }
3943 err = nfs4_handle_exception(server, err, &exception);
3944 } while (exception.retry);
3945 return err;
3946}
3947
3948#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3949#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3950
3951/*
3952 * sleep, with exponential backoff, and retry the LOCK operation.
3953 */
3954static unsigned long
3955nfs4_set_lock_task_retry(unsigned long timeout)
3956{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003957 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 timeout <<= 1;
3959 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3960 return NFS4_LOCK_MAXTIMEOUT;
3961 return timeout;
3962}
3963
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3965{
3966 struct inode *inode = state->inode;
3967 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003968 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003969 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003971 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003973 struct nfs_lockt_res res = {
3974 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 };
3976 struct rpc_message msg = {
3977 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3978 .rpc_argp = &arg,
3979 .rpc_resp = &res,
3980 .rpc_cred = state->owner->so_cred,
3981 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 struct nfs4_lock_state *lsp;
3983 int status;
3984
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003985 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003986 status = nfs4_set_lock_state(state, request);
3987 if (status != 0)
3988 goto out;
3989 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003990 arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05003991 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003992 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003993 switch (status) {
3994 case 0:
3995 request->fl_type = F_UNLCK;
3996 break;
3997 case -NFS4ERR_DENIED:
3998 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004000 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004001out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 return status;
4003}
4004
4005static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4006{
4007 struct nfs4_exception exception = { };
4008 int err;
4009
4010 do {
4011 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4012 _nfs4_proc_getlk(state, cmd, request),
4013 &exception);
4014 } while (exception.retry);
4015 return err;
4016}
4017
4018static int do_vfs_lock(struct file *file, struct file_lock *fl)
4019{
4020 int res = 0;
4021 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4022 case FL_POSIX:
4023 res = posix_lock_file_wait(file, fl);
4024 break;
4025 case FL_FLOCK:
4026 res = flock_lock_file_wait(file, fl);
4027 break;
4028 default:
4029 BUG();
4030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 return res;
4032}
4033
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004034struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004035 struct nfs_locku_args arg;
4036 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004037 struct nfs4_lock_state *lsp;
4038 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004039 struct file_lock fl;
4040 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004041 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004042};
4043
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004044static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4045 struct nfs_open_context *ctx,
4046 struct nfs4_lock_state *lsp,
4047 struct nfs_seqid *seqid)
4048{
4049 struct nfs4_unlockdata *p;
4050 struct inode *inode = lsp->ls_state->inode;
4051
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004052 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004053 if (p == NULL)
4054 return NULL;
4055 p->arg.fh = NFS_FH(inode);
4056 p->arg.fl = &p->fl;
4057 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004058 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004059 p->arg.stateid = &lsp->ls_stateid;
4060 p->lsp = lsp;
4061 atomic_inc(&lsp->ls_count);
4062 /* Ensure we don't close file until we're done freeing locks! */
4063 p->ctx = get_nfs_open_context(ctx);
4064 memcpy(&p->fl, fl, sizeof(p->fl));
4065 p->server = NFS_SERVER(inode);
4066 return p;
4067}
4068
Trond Myklebust06f814a2006-01-03 09:55:07 +01004069static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004070{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004071 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004072 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004073 nfs4_put_lock_state(calldata->lsp);
4074 put_nfs_open_context(calldata->ctx);
4075 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004076}
4077
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004078static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004079{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004080 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004081
Trond Myklebust14516c32010-07-31 14:29:06 -04004082 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4083 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004084 switch (task->tk_status) {
4085 case 0:
4086 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004087 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004088 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01004089 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004090 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004091 case -NFS4ERR_BAD_STATEID:
4092 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004093 case -NFS4ERR_STALE_STATEID:
4094 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004095 break;
4096 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004097 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004098 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004099 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004100}
4101
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004102static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004103{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004104 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004106 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004107 return;
4108 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004109 /* Note: exit _without_ running nfs4_locku_done */
4110 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004111 return;
4112 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004113 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004114 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004115 &calldata->arg.seq_args,
4116 &calldata->res.seq_res, 1, task))
4117 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004118 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119}
4120
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004121static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004122 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004123 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004124 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004125};
4126
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004127static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4128 struct nfs_open_context *ctx,
4129 struct nfs4_lock_state *lsp,
4130 struct nfs_seqid *seqid)
4131{
4132 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004133 struct rpc_message msg = {
4134 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4135 .rpc_cred = ctx->cred,
4136 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004137 struct rpc_task_setup task_setup_data = {
4138 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004139 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004140 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004141 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004142 .flags = RPC_TASK_ASYNC,
4143 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004144
Frank Filz137d6ac2007-07-09 15:32:29 -07004145 /* Ensure this is an unlock - when canceling a lock, the
4146 * canceled lock is passed in, and it won't be an unlock.
4147 */
4148 fl->fl_type = F_UNLCK;
4149
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004150 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4151 if (data == NULL) {
4152 nfs_free_seqid(seqid);
4153 return ERR_PTR(-ENOMEM);
4154 }
4155
Trond Myklebust1174dd12010-12-21 10:52:24 -05004156 msg.rpc_argp = &data->arg;
4157 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004158 task_setup_data.callback_data = data;
4159 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004160}
4161
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4163{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004164 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004165 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004166 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004167 struct rpc_task *task;
4168 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004169 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170
Trond Myklebust9b073572006-06-29 16:38:34 -04004171 status = nfs4_set_lock_state(state, request);
4172 /* Unlock _before_ we do the RPC call */
4173 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004174 down_read(&nfsi->rwsem);
4175 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4176 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004177 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004178 }
4179 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004180 if (status != 0)
4181 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004182 /* Is this a delegated lock? */
4183 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004184 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004185 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004186 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004187 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004188 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004189 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004190 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004191 status = PTR_ERR(task);
4192 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004193 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004194 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004195 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004196out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004197 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004198 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199}
4200
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004201struct nfs4_lockdata {
4202 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004203 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004204 struct nfs4_lock_state *lsp;
4205 struct nfs_open_context *ctx;
4206 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004207 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004208 int rpc_status;
4209 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004210 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004211};
4212
4213static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004214 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4215 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004216{
4217 struct nfs4_lockdata *p;
4218 struct inode *inode = lsp->ls_state->inode;
4219 struct nfs_server *server = NFS_SERVER(inode);
4220
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004221 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004222 if (p == NULL)
4223 return NULL;
4224
4225 p->arg.fh = NFS_FH(inode);
4226 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004227 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004228 if (p->arg.open_seqid == NULL)
4229 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004230 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004231 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004232 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004233 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004234 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004235 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004236 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004237 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004238 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004239 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004240 atomic_inc(&lsp->ls_count);
4241 p->ctx = get_nfs_open_context(ctx);
4242 memcpy(&p->fl, fl, sizeof(p->fl));
4243 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004244out_free_seqid:
4245 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004246out_free:
4247 kfree(p);
4248 return NULL;
4249}
4250
4251static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4252{
4253 struct nfs4_lockdata *data = calldata;
4254 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255
Harvey Harrison3110ff82008-05-02 13:42:44 -07004256 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004257 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4258 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004259 /* Do we need to do an open_to_lock_owner? */
4260 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004261 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4262 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004263 data->arg.open_stateid = &state->stateid;
4264 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004265 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004266 } else
4267 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004268 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004269 if (nfs4_setup_sequence(data->server,
4270 &data->arg.seq_args,
Andy Adamson66179ef2009-04-01 09:22:22 -04004271 &data->res.seq_res, 1, task))
4272 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004273 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004274 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004275}
4276
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004277static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4278{
4279 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4280 nfs4_lock_prepare(task, calldata);
4281}
4282
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004283static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4284{
4285 struct nfs4_lockdata *data = calldata;
4286
Harvey Harrison3110ff82008-05-02 13:42:44 -07004287 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004288
Trond Myklebust14516c32010-07-31 14:29:06 -04004289 if (!nfs4_sequence_done(task, &data->res.seq_res))
4290 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004291
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004292 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004293 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004294 if (data->rpc_status == 0)
4295 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4296 else
4297 goto out;
4298 }
4299 if (data->rpc_status == 0) {
4300 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4301 sizeof(data->lsp->ls_stateid.data));
4302 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Al Viro3d4ff432011-06-22 18:40:12 -04004303 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004304 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004305out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004306 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004307}
4308
4309static void nfs4_lock_release(void *calldata)
4310{
4311 struct nfs4_lockdata *data = calldata;
4312
Harvey Harrison3110ff82008-05-02 13:42:44 -07004313 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004314 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004315 if (data->cancelled != 0) {
4316 struct rpc_task *task;
4317 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4318 data->arg.lock_seqid);
4319 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004320 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004321 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004322 } else
4323 nfs_free_seqid(data->arg.lock_seqid);
4324 nfs4_put_lock_state(data->lsp);
4325 put_nfs_open_context(data->ctx);
4326 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004327 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004328}
4329
4330static const struct rpc_call_ops nfs4_lock_ops = {
4331 .rpc_call_prepare = nfs4_lock_prepare,
4332 .rpc_call_done = nfs4_lock_done,
4333 .rpc_release = nfs4_lock_release,
4334};
4335
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004336static const struct rpc_call_ops nfs4_recover_lock_ops = {
4337 .rpc_call_prepare = nfs4_recover_lock_prepare,
4338 .rpc_call_done = nfs4_lock_done,
4339 .rpc_release = nfs4_lock_release,
4340};
4341
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004342static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4343{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004344 switch (error) {
4345 case -NFS4ERR_ADMIN_REVOKED:
4346 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004347 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004348 if (new_lock_owner != 0 ||
4349 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004350 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004351 break;
4352 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004353 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004354 case -NFS4ERR_EXPIRED:
4355 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004356 };
4357}
4358
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004359static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004360{
4361 struct nfs4_lockdata *data;
4362 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004363 struct rpc_message msg = {
4364 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4365 .rpc_cred = state->owner->so_cred,
4366 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004367 struct rpc_task_setup task_setup_data = {
4368 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004369 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004370 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004371 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004372 .flags = RPC_TASK_ASYNC,
4373 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004374 int ret;
4375
Harvey Harrison3110ff82008-05-02 13:42:44 -07004376 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004377 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004378 fl->fl_u.nfs4_fl.owner,
4379 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004380 if (data == NULL)
4381 return -ENOMEM;
4382 if (IS_SETLKW(cmd))
4383 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004384 if (recovery_type > NFS_LOCK_NEW) {
4385 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004386 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004387 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4388 }
Trond Myklebust1174dd12010-12-21 10:52:24 -05004389 msg.rpc_argp = &data->arg;
4390 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004391 task_setup_data.callback_data = data;
4392 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004393 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004394 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004395 ret = nfs4_wait_for_completion_rpc_task(task);
4396 if (ret == 0) {
4397 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004398 if (ret)
4399 nfs4_handle_setlk_error(data->server, data->lsp,
4400 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004401 } else
4402 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004403 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004404 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004405 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406}
4407
4408static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4409{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004410 struct nfs_server *server = NFS_SERVER(state->inode);
4411 struct nfs4_exception exception = { };
4412 int err;
4413
4414 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004415 /* Cache the lock if possible... */
4416 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4417 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004418 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004419 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004420 break;
4421 nfs4_handle_exception(server, err, &exception);
4422 } while (exception.retry);
4423 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424}
4425
4426static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4427{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004428 struct nfs_server *server = NFS_SERVER(state->inode);
4429 struct nfs4_exception exception = { };
4430 int err;
4431
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004432 err = nfs4_set_lock_state(state, request);
4433 if (err != 0)
4434 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004435 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004436 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4437 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004438 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004439 switch (err) {
4440 default:
4441 goto out;
4442 case -NFS4ERR_GRACE:
4443 case -NFS4ERR_DELAY:
4444 nfs4_handle_exception(server, err, &exception);
4445 err = 0;
4446 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004447 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004448out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004449 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450}
4451
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004452#if defined(CONFIG_NFS_V4_1)
4453static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4454{
4455 int status;
4456 struct nfs_server *server = NFS_SERVER(state->inode);
4457
4458 status = nfs41_test_stateid(server, state);
4459 if (status == NFS_OK)
4460 return 0;
4461 nfs41_free_stateid(server, state);
4462 return nfs4_lock_expired(state, request);
4463}
4464#endif
4465
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4467{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004468 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004469 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004470 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004472 if ((fl_flags & FL_POSIX) &&
4473 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4474 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004475 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004476 status = nfs4_set_lock_state(state, request);
4477 if (status != 0)
4478 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004479 request->fl_flags |= FL_ACCESS;
4480 status = do_vfs_lock(request->fl_file, request);
4481 if (status < 0)
4482 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004483 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004484 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004485 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004486 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004487 request->fl_flags = fl_flags & ~FL_SLEEP;
4488 status = do_vfs_lock(request->fl_file, request);
4489 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004490 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004491 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004492 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004493 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004494 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004495 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004496 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004497 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004498out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004499 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004500out:
4501 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 return status;
4503}
4504
4505static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4506{
4507 struct nfs4_exception exception = { };
4508 int err;
4509
4510 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004511 err = _nfs4_proc_setlk(state, cmd, request);
4512 if (err == -NFS4ERR_DENIED)
4513 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004515 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 } while (exception.retry);
4517 return err;
4518}
4519
4520static int
4521nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4522{
4523 struct nfs_open_context *ctx;
4524 struct nfs4_state *state;
4525 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4526 int status;
4527
4528 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004529 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 state = ctx->state;
4531
4532 if (request->fl_start < 0 || request->fl_end < 0)
4533 return -EINVAL;
4534
Trond Myklebustd9531262009-07-21 19:22:38 -04004535 if (IS_GETLK(cmd)) {
4536 if (state != NULL)
4537 return nfs4_proc_getlk(state, F_GETLK, request);
4538 return 0;
4539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540
4541 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4542 return -EINVAL;
4543
Trond Myklebustd9531262009-07-21 19:22:38 -04004544 if (request->fl_type == F_UNLCK) {
4545 if (state != NULL)
4546 return nfs4_proc_unlck(state, cmd, request);
4547 return 0;
4548 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549
Trond Myklebustd9531262009-07-21 19:22:38 -04004550 if (state == NULL)
4551 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 do {
4553 status = nfs4_proc_setlk(state, cmd, request);
4554 if ((status != -EAGAIN) || IS_SETLK(cmd))
4555 break;
4556 timeout = nfs4_set_lock_task_retry(timeout);
4557 status = -ERESTARTSYS;
4558 if (signalled())
4559 break;
4560 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 return status;
4562}
4563
Trond Myklebust888e6942005-11-04 15:38:11 -05004564int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4565{
4566 struct nfs_server *server = NFS_SERVER(state->inode);
4567 struct nfs4_exception exception = { };
4568 int err;
4569
4570 err = nfs4_set_lock_state(state, fl);
4571 if (err != 0)
4572 goto out;
4573 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004574 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004575 switch (err) {
4576 default:
4577 printk(KERN_ERR "%s: unhandled error %d.\n",
4578 __func__, err);
4579 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004580 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004581 goto out;
4582 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004583 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004584 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004585 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004586 nfs4_schedule_lease_recovery(server->nfs_client);
4587 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004588 case -NFS4ERR_BADSESSION:
4589 case -NFS4ERR_BADSLOT:
4590 case -NFS4ERR_BAD_HIGH_SLOT:
4591 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4592 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004593 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004594 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004595 case -ERESTARTSYS:
4596 /*
4597 * The show must go on: exit, but mark the
4598 * stateid as needing recovery.
4599 */
4600 case -NFS4ERR_ADMIN_REVOKED:
4601 case -NFS4ERR_BAD_STATEID:
4602 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004603 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004604 err = 0;
4605 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004606 case -EKEYEXPIRED:
4607 /*
4608 * User RPCSEC_GSS context has expired.
4609 * We cannot recover this stateid now, so
4610 * skip it and allow recovery thread to
4611 * proceed.
4612 */
4613 err = 0;
4614 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004615 case -ENOMEM:
4616 case -NFS4ERR_DENIED:
4617 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4618 err = 0;
4619 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004620 case -NFS4ERR_DELAY:
4621 break;
4622 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004623 err = nfs4_handle_exception(server, err, &exception);
4624 } while (exception.retry);
4625out:
4626 return err;
4627}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004628
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004629static void nfs4_release_lockowner_release(void *calldata)
4630{
4631 kfree(calldata);
4632}
4633
4634const struct rpc_call_ops nfs4_release_lockowner_ops = {
4635 .rpc_release = nfs4_release_lockowner_release,
4636};
4637
4638void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4639{
4640 struct nfs_server *server = lsp->ls_state->owner->so_server;
4641 struct nfs_release_lockowner_args *args;
4642 struct rpc_message msg = {
4643 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4644 };
4645
4646 if (server->nfs_client->cl_mvops->minor_version != 0)
4647 return;
4648 args = kmalloc(sizeof(*args), GFP_NOFS);
4649 if (!args)
4650 return;
4651 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4652 args->lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004653 args->lock_owner.s_dev = server->s_dev;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004654 msg.rpc_argp = args;
4655 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4656}
4657
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004658#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4659
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004660static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4661 const void *buf, size_t buflen,
4662 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004663{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004664 if (strcmp(key, "") != 0)
4665 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004666
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004667 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004668}
4669
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004670static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4671 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004672{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004673 if (strcmp(key, "") != 0)
4674 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004675
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004676 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004677}
4678
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004679static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4680 size_t list_len, const char *name,
4681 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004682{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004683 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004684
J. Bruce Fields096455a2006-03-20 23:23:42 -05004685 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4686 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004687
4688 if (list && len <= list_len)
4689 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004690 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004691}
4692
Andy Adamson533eb462011-06-13 18:25:56 -04004693/*
4694 * nfs_fhget will use either the mounted_on_fileid or the fileid
4695 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004696static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4697{
Andy Adamson533eb462011-06-13 18:25:56 -04004698 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4699 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4700 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4701 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004702 return;
4703
4704 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4705 NFS_ATTR_FATTR_NLINK;
4706 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4707 fattr->nlink = 2;
4708}
4709
Trond Myklebust56659e92007-07-17 21:52:39 -04004710int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004711 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004712{
4713 struct nfs_server *server = NFS_SERVER(dir);
4714 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004715 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004716 };
4717 struct nfs4_fs_locations_arg args = {
4718 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004719 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004720 .page = page,
4721 .bitmask = bitmask,
4722 };
Benny Halevy22958462009-04-01 09:22:02 -04004723 struct nfs4_fs_locations_res res = {
4724 .fs_locations = fs_locations,
4725 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004726 struct rpc_message msg = {
4727 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4728 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004729 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004730 };
4731 int status;
4732
Harvey Harrison3110ff82008-05-02 13:42:44 -07004733 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04004734
4735 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
4736 * is not supported */
4737 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
4738 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
4739 else
4740 bitmask[0] |= FATTR4_WORD0_FILEID;
4741
Trond Myklebustc228fd32007-01-13 02:28:11 -05004742 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004743 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004744 fs_locations->nlocations = 0;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004745 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004746 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004747 return status;
4748}
4749
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00004750static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4751{
4752 int status;
4753 struct nfs4_secinfo_arg args = {
4754 .dir_fh = NFS_FH(dir),
4755 .name = name,
4756 };
4757 struct nfs4_secinfo_res res = {
4758 .flavors = flavors,
4759 };
4760 struct rpc_message msg = {
4761 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
4762 .rpc_argp = &args,
4763 .rpc_resp = &res,
4764 };
4765
4766 dprintk("NFS call secinfo %s\n", name->name);
4767 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4768 dprintk("NFS reply secinfo: %d\n", status);
4769 return status;
4770}
4771
4772int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4773{
4774 struct nfs4_exception exception = { };
4775 int err;
4776 do {
4777 err = nfs4_handle_exception(NFS_SERVER(dir),
4778 _nfs4_proc_secinfo(dir, name, flavors),
4779 &exception);
4780 } while (exception.retry);
4781 return err;
4782}
4783
Andy Adamson557134a2009-04-01 09:21:53 -04004784#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004785/*
Andy Adamson357f54d2010-12-14 10:11:57 -05004786 * Check the exchange flags returned by the server for invalid flags, having
4787 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4788 * DS flags set.
4789 */
4790static int nfs4_check_cl_exchange_flags(u32 flags)
4791{
4792 if (flags & ~EXCHGID4_FLAG_MASK_R)
4793 goto out_inval;
4794 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4795 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4796 goto out_inval;
4797 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4798 goto out_inval;
4799 return NFS_OK;
4800out_inval:
4801 return -NFS4ERR_INVAL;
4802}
4803
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004804static bool
4805nfs41_same_server_scope(struct server_scope *a, struct server_scope *b)
4806{
4807 if (a->server_scope_sz == b->server_scope_sz &&
4808 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
4809 return true;
4810
4811 return false;
4812}
4813
Andy Adamson357f54d2010-12-14 10:11:57 -05004814/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04004815 * nfs4_proc_exchange_id()
4816 *
4817 * Since the clientid has expired, all compounds using sessions
4818 * associated with the stale clientid will be returning
4819 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4820 * be in some phase of session reset.
4821 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004822int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004823{
4824 nfs4_verifier verifier;
4825 struct nfs41_exchange_id_args args = {
4826 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05004827 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04004828 };
4829 struct nfs41_exchange_id_res res = {
4830 .client = clp,
4831 };
4832 int status;
4833 struct rpc_message msg = {
4834 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4835 .rpc_argp = &args,
4836 .rpc_resp = &res,
4837 .rpc_cred = cred,
4838 };
4839 __be32 *p;
4840
4841 dprintk("--> %s\n", __func__);
4842 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004843
Benny Halevy99fe60d2009-04-01 09:22:29 -04004844 p = (u32 *)verifier.data;
4845 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4846 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4847 args.verifier = &verifier;
4848
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004849 args.id_len = scnprintf(args.id, sizeof(args.id),
4850 "%s/%s.%s/%u",
4851 clp->cl_ipaddr,
4852 init_utsname()->nodename,
4853 init_utsname()->domainname,
4854 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004855
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004856 res.server_scope = kzalloc(sizeof(struct server_scope), GFP_KERNEL);
4857 if (unlikely(!res.server_scope))
4858 return -ENOMEM;
4859
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004860 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004861 if (!status)
4862 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004863
4864 if (!status) {
4865 if (clp->server_scope &&
4866 !nfs41_same_server_scope(clp->server_scope,
4867 res.server_scope)) {
4868 dprintk("%s: server_scope mismatch detected\n",
4869 __func__);
4870 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
4871 kfree(clp->server_scope);
4872 clp->server_scope = NULL;
4873 }
4874
4875 if (!clp->server_scope)
4876 clp->server_scope = res.server_scope;
4877 else
4878 kfree(res.server_scope);
4879 }
4880
Benny Halevy99fe60d2009-04-01 09:22:29 -04004881 dprintk("<-- %s status= %d\n", __func__, status);
4882 return status;
4883}
4884
Andy Adamson2050f0c2009-04-01 09:22:30 -04004885struct nfs4_get_lease_time_data {
4886 struct nfs4_get_lease_time_args *args;
4887 struct nfs4_get_lease_time_res *res;
4888 struct nfs_client *clp;
4889};
4890
4891static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4892 void *calldata)
4893{
4894 int ret;
4895 struct nfs4_get_lease_time_data *data =
4896 (struct nfs4_get_lease_time_data *)calldata;
4897
4898 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004899 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004900 /* just setup sequence, do not trigger session recovery
4901 since we're invoked within one */
4902 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004903 &data->args->la_seq_args,
4904 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004905
4906 BUG_ON(ret == -EAGAIN);
4907 rpc_call_start(task);
4908 dprintk("<-- %s\n", __func__);
4909}
4910
4911/*
4912 * Called from nfs4_state_manager thread for session setup, so don't recover
4913 * from sequence operation or clientid errors.
4914 */
4915static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4916{
4917 struct nfs4_get_lease_time_data *data =
4918 (struct nfs4_get_lease_time_data *)calldata;
4919
4920 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04004921 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4922 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04004923 switch (task->tk_status) {
4924 case -NFS4ERR_DELAY:
4925 case -NFS4ERR_GRACE:
4926 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4927 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4928 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004929 /* fall through */
4930 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004931 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004932 return;
4933 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004934 dprintk("<-- %s\n", __func__);
4935}
4936
4937struct rpc_call_ops nfs4_get_lease_time_ops = {
4938 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4939 .rpc_call_done = nfs4_get_lease_time_done,
4940};
4941
4942int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4943{
4944 struct rpc_task *task;
4945 struct nfs4_get_lease_time_args args;
4946 struct nfs4_get_lease_time_res res = {
4947 .lr_fsinfo = fsinfo,
4948 };
4949 struct nfs4_get_lease_time_data data = {
4950 .args = &args,
4951 .res = &res,
4952 .clp = clp,
4953 };
4954 struct rpc_message msg = {
4955 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4956 .rpc_argp = &args,
4957 .rpc_resp = &res,
4958 };
4959 struct rpc_task_setup task_setup = {
4960 .rpc_client = clp->cl_rpcclient,
4961 .rpc_message = &msg,
4962 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004963 .callback_data = &data,
4964 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04004965 };
4966 int status;
4967
Andy Adamson2050f0c2009-04-01 09:22:30 -04004968 dprintk("--> %s\n", __func__);
4969 task = rpc_run_task(&task_setup);
4970
4971 if (IS_ERR(task))
4972 status = PTR_ERR(task);
4973 else {
4974 status = task->tk_status;
4975 rpc_put_task(task);
4976 }
4977 dprintk("<-- %s return %d\n", __func__, status);
4978
4979 return status;
4980}
4981
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004982/*
4983 * Reset a slot table
4984 */
Andy Adamson104aeba2010-01-14 17:45:10 -05004985static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4986 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004987{
Andy Adamson104aeba2010-01-14 17:45:10 -05004988 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004989 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004990 int ret = 0;
4991
Andy Adamson104aeba2010-01-14 17:45:10 -05004992 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4993 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004994
Andy Adamson104aeba2010-01-14 17:45:10 -05004995 /* Does the newly negotiated max_reqs match the existing slot table? */
4996 if (max_reqs != tbl->max_slots) {
4997 ret = -ENOMEM;
4998 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004999 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005000 if (!new)
5001 goto out;
5002 ret = 0;
5003 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005004 }
5005 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05005006 if (new) {
5007 tbl->slots = new;
5008 tbl->max_slots = max_reqs;
5009 }
5010 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005011 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005012 spin_unlock(&tbl->slot_tbl_lock);
5013 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5014 tbl, tbl->slots, tbl->max_slots);
5015out:
5016 dprintk("<-- %s: return %d\n", __func__, ret);
5017 return ret;
5018}
5019
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005020/* Destroy the slot table */
5021static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5022{
5023 if (session->fc_slot_table.slots != NULL) {
5024 kfree(session->fc_slot_table.slots);
5025 session->fc_slot_table.slots = NULL;
5026 }
5027 if (session->bc_slot_table.slots != NULL) {
5028 kfree(session->bc_slot_table.slots);
5029 session->bc_slot_table.slots = NULL;
5030 }
5031 return;
5032}
5033
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005034/*
Andy Adamsonfc931582009-04-01 09:22:31 -04005035 * Initialize slot table
5036 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005037static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
5038 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04005039{
Andy Adamsonfc931582009-04-01 09:22:31 -04005040 struct nfs4_slot *slot;
5041 int ret = -ENOMEM;
5042
5043 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
5044
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005045 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04005046
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005047 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04005048 if (!slot)
5049 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04005050 ret = 0;
5051
5052 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04005053 tbl->max_slots = max_slots;
5054 tbl->slots = slot;
5055 tbl->highest_used_slotid = -1; /* no slot is currently used */
5056 spin_unlock(&tbl->slot_tbl_lock);
5057 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5058 tbl, tbl->slots, tbl->max_slots);
5059out:
5060 dprintk("<-- %s: return %d\n", __func__, ret);
5061 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04005062}
5063
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005064/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005065 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005066 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005067static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005068{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005069 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005070 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005071
Andy Adamsonaacd5532011-11-09 13:58:21 -05005072 dprintk("--> %s\n", __func__);
5073 /* Fore channel */
5074 tbl = &ses->fc_slot_table;
Trond Myklebustf26468f2009-12-05 19:32:11 -05005075 if (tbl->slots == NULL) {
Andy Adamsonaacd5532011-11-09 13:58:21 -05005076 status = nfs4_init_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5077 if (status) /* -ENOMEM */
5078 return status;
5079 } else {
5080 status = nfs4_reset_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
Trond Myklebustf26468f2009-12-05 19:32:11 -05005081 if (status)
5082 return status;
5083 }
Andy Adamsonaacd5532011-11-09 13:58:21 -05005084 /* Back channel */
5085 tbl = &ses->bc_slot_table;
Trond Myklebustf26468f2009-12-05 19:32:11 -05005086 if (tbl->slots == NULL) {
Andy Adamsonaacd5532011-11-09 13:58:21 -05005087 status = nfs4_init_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
Trond Myklebustf26468f2009-12-05 19:32:11 -05005088 if (status)
Andy Adamsonaacd5532011-11-09 13:58:21 -05005089 /* Fore and back channel share a connection so get
5090 * both slot tables or neither */
5091 nfs4_destroy_slot_tables(ses);
5092 } else
5093 status = nfs4_reset_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005094 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005095}
5096
5097struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5098{
5099 struct nfs4_session *session;
5100 struct nfs4_slot_table *tbl;
5101
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005102 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005103 if (!session)
5104 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005105
Andy Adamson557134a2009-04-01 09:21:53 -04005106 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05005107 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04005108 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005109 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005110 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005111
5112 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05005113 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005114 spin_lock_init(&tbl->slot_tbl_lock);
5115 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005116 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005117
Trond Myklebust1055d762010-06-16 09:52:27 -04005118 session->session_state = 1<<NFS4_SESSION_INITING;
5119
Andy Adamson557134a2009-04-01 09:21:53 -04005120 session->clp = clp;
5121 return session;
5122}
5123
5124void nfs4_destroy_session(struct nfs4_session *session)
5125{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005126 nfs4_proc_destroy_session(session);
5127 dprintk("%s Destroy backchannel for xprt %p\n",
5128 __func__, session->clp->cl_rpcclient->cl_xprt);
5129 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
5130 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005131 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005132 kfree(session);
5133}
5134
Andy Adamsonfc931582009-04-01 09:22:31 -04005135/*
5136 * Initialize the values to be used by the client in CREATE_SESSION
5137 * If nfs4_init_session set the fore channel request and response sizes,
5138 * use them.
5139 *
5140 * Set the back channel max_resp_sz_cached to zero to force the client to
5141 * always set csa_cachethis to FALSE because the current implementation
5142 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5143 */
5144static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5145{
5146 struct nfs4_session *session = args->client->cl_session;
5147 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5148 mxresp_sz = session->fc_attrs.max_resp_sz;
5149
5150 if (mxrqst_sz == 0)
5151 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5152 if (mxresp_sz == 0)
5153 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5154 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005155 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5156 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005157 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5158 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
5159
5160 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005161 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005162 __func__,
5163 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005164 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005165
5166 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005167 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5168 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5169 args->bc_attrs.max_resp_sz_cached = 0;
5170 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5171 args->bc_attrs.max_reqs = 1;
5172
5173 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5174 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5175 __func__,
5176 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5177 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5178 args->bc_attrs.max_reqs);
5179}
5180
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005181static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005182{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005183 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5184 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5185
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005186 if (rcvd->max_resp_sz > sent->max_resp_sz)
5187 return -EINVAL;
5188 /*
5189 * Our requested max_ops is the minimum we need; we're not
5190 * prepared to break up compounds into smaller pieces than that.
5191 * So, no point even trying to continue if the server won't
5192 * cooperate:
5193 */
5194 if (rcvd->max_ops < sent->max_ops)
5195 return -EINVAL;
5196 if (rcvd->max_reqs == 0)
5197 return -EINVAL;
5198 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005199}
5200
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005201static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5202{
5203 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5204 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005205
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005206 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5207 return -EINVAL;
5208 if (rcvd->max_resp_sz < sent->max_resp_sz)
5209 return -EINVAL;
5210 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5211 return -EINVAL;
5212 /* These would render the backchannel useless: */
5213 if (rcvd->max_ops == 0)
5214 return -EINVAL;
5215 if (rcvd->max_reqs == 0)
5216 return -EINVAL;
5217 return 0;
5218}
Andy Adamson8d353012009-04-01 09:22:32 -04005219
Andy Adamson8d353012009-04-01 09:22:32 -04005220static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5221 struct nfs4_session *session)
5222{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005223 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005224
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005225 ret = nfs4_verify_fore_channel_attrs(args, session);
5226 if (ret)
5227 return ret;
5228 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005229}
5230
Andy Adamsonfc931582009-04-01 09:22:31 -04005231static int _nfs4_proc_create_session(struct nfs_client *clp)
5232{
5233 struct nfs4_session *session = clp->cl_session;
5234 struct nfs41_create_session_args args = {
5235 .client = clp,
5236 .cb_program = NFS4_CALLBACK,
5237 };
5238 struct nfs41_create_session_res res = {
5239 .client = clp,
5240 };
5241 struct rpc_message msg = {
5242 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5243 .rpc_argp = &args,
5244 .rpc_resp = &res,
5245 };
5246 int status;
5247
5248 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005249 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005250
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005251 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005252
Andy Adamson8d353012009-04-01 09:22:32 -04005253 if (!status)
5254 /* Verify the session's negotiated channel_attrs values */
5255 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005256 if (!status) {
5257 /* Increment the clientid slot sequence id */
5258 clp->cl_seqid++;
5259 }
5260
5261 return status;
5262}
5263
5264/*
5265 * Issues a CREATE_SESSION operation to the server.
5266 * It is the responsibility of the caller to verify the session is
5267 * expired before calling this routine.
5268 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005269int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005270{
5271 int status;
5272 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005273 struct nfs4_session *session = clp->cl_session;
5274
5275 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5276
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005277 status = _nfs4_proc_create_session(clp);
Andy Adamsonfc931582009-04-01 09:22:31 -04005278 if (status)
5279 goto out;
5280
Andy Adamsonaacd5532011-11-09 13:58:21 -05005281 /* Init or reset the session slot tables */
5282 status = nfs4_setup_session_slot_tables(session);
5283 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005284 if (status)
5285 goto out;
5286
5287 ptr = (unsigned *)&session->sess_id.data[0];
5288 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5289 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005290out:
5291 dprintk("<-- %s\n", __func__);
5292 return status;
5293}
5294
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005295/*
5296 * Issue the over-the-wire RPC DESTROY_SESSION.
5297 * The caller must serialize access to this routine.
5298 */
5299int nfs4_proc_destroy_session(struct nfs4_session *session)
5300{
5301 int status = 0;
5302 struct rpc_message msg;
5303
5304 dprintk("--> nfs4_proc_destroy_session\n");
5305
5306 /* session is still being setup */
5307 if (session->clp->cl_cons_state != NFS_CS_READY)
5308 return status;
5309
5310 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5311 msg.rpc_argp = session;
5312 msg.rpc_resp = NULL;
5313 msg.rpc_cred = NULL;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005314 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005315
5316 if (status)
5317 printk(KERN_WARNING
5318 "Got error %d from the server on DESTROY_SESSION. "
5319 "Session has been destroyed regardless...\n", status);
5320
5321 dprintk("<-- nfs4_proc_destroy_session\n");
5322 return status;
5323}
5324
Trond Myklebustfccba802009-07-21 16:48:07 -04005325int nfs4_init_session(struct nfs_server *server)
5326{
5327 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005328 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005329 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005330 int ret;
5331
5332 if (!nfs4_has_session(clp))
5333 return 0;
5334
Trond Myklebust1055d762010-06-16 09:52:27 -04005335 session = clp->cl_session;
5336 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5337 return 0;
5338
Andy Adamson68bf05e2009-12-15 12:55:02 -05005339 rsize = server->rsize;
5340 if (rsize == 0)
5341 rsize = NFS_MAX_FILE_IO_SIZE;
5342 wsize = server->wsize;
5343 if (wsize == 0)
5344 wsize = NFS_MAX_FILE_IO_SIZE;
5345
Andy Adamson68bf05e2009-12-15 12:55:02 -05005346 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5347 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005348
Trond Myklebustfccba802009-07-21 16:48:07 -04005349 ret = nfs4_recover_expired_lease(server);
5350 if (!ret)
5351 ret = nfs4_check_client_ready(clp);
5352 return ret;
5353}
5354
Andy Adamsond83217c2011-03-01 01:34:17 +00005355int nfs4_init_ds_session(struct nfs_client *clp)
5356{
5357 struct nfs4_session *session = clp->cl_session;
5358 int ret;
5359
5360 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5361 return 0;
5362
5363 ret = nfs4_client_recover_expired_lease(clp);
5364 if (!ret)
5365 /* Test for the DS role */
5366 if (!is_ds_client(clp))
5367 ret = -ENODEV;
5368 if (!ret)
5369 ret = nfs4_check_client_ready(clp);
5370 return ret;
5371
5372}
5373EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5374
5375
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005376/*
5377 * Renew the cl_session lease.
5378 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005379struct nfs4_sequence_data {
5380 struct nfs_client *clp;
5381 struct nfs4_sequence_args args;
5382 struct nfs4_sequence_res res;
5383};
5384
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005385static void nfs41_sequence_release(void *data)
5386{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005387 struct nfs4_sequence_data *calldata = data;
5388 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005389
Alexandros Batsakis71358402010-02-05 03:45:05 -08005390 if (atomic_read(&clp->cl_count) > 1)
5391 nfs4_schedule_state_renewal(clp);
5392 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005393 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005394}
5395
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005396static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5397{
5398 switch(task->tk_status) {
5399 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005400 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5401 return -EAGAIN;
5402 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005403 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005404 }
5405 return 0;
5406}
5407
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005408static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005409{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005410 struct nfs4_sequence_data *calldata = data;
5411 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005412
Trond Myklebust14516c32010-07-31 14:29:06 -04005413 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5414 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005415
5416 if (task->tk_status < 0) {
5417 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005418 if (atomic_read(&clp->cl_count) == 1)
5419 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005420
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005421 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5422 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005423 return;
5424 }
5425 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005426 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005427out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005428 dprintk("<-- %s\n", __func__);
5429}
5430
5431static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5432{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005433 struct nfs4_sequence_data *calldata = data;
5434 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005435 struct nfs4_sequence_args *args;
5436 struct nfs4_sequence_res *res;
5437
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005438 args = task->tk_msg.rpc_argp;
5439 res = task->tk_msg.rpc_resp;
5440
Trond Myklebust035168a2010-06-16 09:52:26 -04005441 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005442 return;
5443 rpc_call_start(task);
5444}
5445
5446static const struct rpc_call_ops nfs41_sequence_ops = {
5447 .rpc_call_done = nfs41_sequence_call_done,
5448 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005449 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005450};
5451
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005452static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005453{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005454 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005455 struct rpc_message msg = {
5456 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5457 .rpc_cred = cred,
5458 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005459 struct rpc_task_setup task_setup_data = {
5460 .rpc_client = clp->cl_rpcclient,
5461 .rpc_message = &msg,
5462 .callback_ops = &nfs41_sequence_ops,
5463 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5464 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005465
Alexandros Batsakis71358402010-02-05 03:45:05 -08005466 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005467 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005468 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005469 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005470 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005471 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005472 }
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005473 msg.rpc_argp = &calldata->args;
5474 msg.rpc_resp = &calldata->res;
5475 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005476 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005477
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005478 return rpc_run_task(&task_setup_data);
5479}
5480
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005481static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005482{
5483 struct rpc_task *task;
5484 int ret = 0;
5485
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005486 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5487 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005488 task = _nfs41_proc_sequence(clp, cred);
5489 if (IS_ERR(task))
5490 ret = PTR_ERR(task);
5491 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005492 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005493 dprintk("<-- %s status=%d\n", __func__, ret);
5494 return ret;
5495}
5496
5497static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5498{
5499 struct rpc_task *task;
5500 int ret;
5501
5502 task = _nfs41_proc_sequence(clp, cred);
5503 if (IS_ERR(task)) {
5504 ret = PTR_ERR(task);
5505 goto out;
5506 }
5507 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005508 if (!ret) {
5509 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5510
5511 if (task->tk_status == 0)
5512 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005513 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005514 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005515 rpc_put_task(task);
5516out:
5517 dprintk("<-- %s status=%d\n", __func__, ret);
5518 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005519}
5520
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005521struct nfs4_reclaim_complete_data {
5522 struct nfs_client *clp;
5523 struct nfs41_reclaim_complete_args arg;
5524 struct nfs41_reclaim_complete_res res;
5525};
5526
5527static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5528{
5529 struct nfs4_reclaim_complete_data *calldata = data;
5530
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005531 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005532 if (nfs41_setup_sequence(calldata->clp->cl_session,
5533 &calldata->arg.seq_args,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005534 &calldata->res.seq_res, 0, task))
5535 return;
5536
5537 rpc_call_start(task);
5538}
5539
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005540static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5541{
5542 switch(task->tk_status) {
5543 case 0:
5544 case -NFS4ERR_COMPLETE_ALREADY:
5545 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5546 break;
5547 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005548 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005549 /* fall through */
5550 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005551 return -EAGAIN;
5552 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005553 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005554 }
5555 return 0;
5556}
5557
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005558static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5559{
5560 struct nfs4_reclaim_complete_data *calldata = data;
5561 struct nfs_client *clp = calldata->clp;
5562 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5563
5564 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005565 if (!nfs41_sequence_done(task, res))
5566 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005567
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005568 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5569 rpc_restart_call_prepare(task);
5570 return;
5571 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005572 dprintk("<-- %s\n", __func__);
5573}
5574
5575static void nfs4_free_reclaim_complete_data(void *data)
5576{
5577 struct nfs4_reclaim_complete_data *calldata = data;
5578
5579 kfree(calldata);
5580}
5581
5582static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5583 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5584 .rpc_call_done = nfs4_reclaim_complete_done,
5585 .rpc_release = nfs4_free_reclaim_complete_data,
5586};
5587
5588/*
5589 * Issue a global reclaim complete.
5590 */
5591static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5592{
5593 struct nfs4_reclaim_complete_data *calldata;
5594 struct rpc_task *task;
5595 struct rpc_message msg = {
5596 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5597 };
5598 struct rpc_task_setup task_setup_data = {
5599 .rpc_client = clp->cl_rpcclient,
5600 .rpc_message = &msg,
5601 .callback_ops = &nfs4_reclaim_complete_call_ops,
5602 .flags = RPC_TASK_ASYNC,
5603 };
5604 int status = -ENOMEM;
5605
5606 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005607 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005608 if (calldata == NULL)
5609 goto out;
5610 calldata->clp = clp;
5611 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005612
5613 msg.rpc_argp = &calldata->arg;
5614 msg.rpc_resp = &calldata->res;
5615 task_setup_data.callback_data = calldata;
5616 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005617 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005618 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005619 goto out;
5620 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005621 status = nfs4_wait_for_completion_rpc_task(task);
5622 if (status == 0)
5623 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005624 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005625 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005626out:
5627 dprintk("<-- %s status=%d\n", __func__, status);
5628 return status;
5629}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005630
5631static void
5632nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5633{
5634 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005635 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005636
5637 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005638 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5639 * right now covering the LAYOUTGET we are about to send.
5640 * However, that is not so catastrophic, and there seems
5641 * to be no way to prevent it completely.
5642 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005643 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5644 &lgp->res.seq_res, 0, task))
5645 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005646 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5647 NFS_I(lgp->args.inode)->layout,
5648 lgp->args.ctx->state)) {
5649 rpc_exit(task, NFS4_OK);
5650 return;
5651 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005652 rpc_call_start(task);
5653}
5654
5655static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5656{
5657 struct nfs4_layoutget *lgp = calldata;
5658 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5659
5660 dprintk("--> %s\n", __func__);
5661
5662 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5663 return;
5664
5665 switch (task->tk_status) {
5666 case 0:
5667 break;
5668 case -NFS4ERR_LAYOUTTRYLATER:
5669 case -NFS4ERR_RECALLCONFLICT:
5670 task->tk_status = -NFS4ERR_DELAY;
5671 /* Fall through */
5672 default:
5673 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5674 rpc_restart_call_prepare(task);
5675 return;
5676 }
5677 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005678 dprintk("<-- %s\n", __func__);
5679}
5680
5681static void nfs4_layoutget_release(void *calldata)
5682{
5683 struct nfs4_layoutget *lgp = calldata;
5684
5685 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005686 put_nfs_open_context(lgp->args.ctx);
5687 kfree(calldata);
5688 dprintk("<-- %s\n", __func__);
5689}
5690
5691static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5692 .rpc_call_prepare = nfs4_layoutget_prepare,
5693 .rpc_call_done = nfs4_layoutget_done,
5694 .rpc_release = nfs4_layoutget_release,
5695};
5696
5697int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5698{
5699 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5700 struct rpc_task *task;
5701 struct rpc_message msg = {
5702 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5703 .rpc_argp = &lgp->args,
5704 .rpc_resp = &lgp->res,
5705 };
5706 struct rpc_task_setup task_setup_data = {
5707 .rpc_client = server->client,
5708 .rpc_message = &msg,
5709 .callback_ops = &nfs4_layoutget_call_ops,
5710 .callback_data = lgp,
5711 .flags = RPC_TASK_ASYNC,
5712 };
5713 int status = 0;
5714
5715 dprintk("--> %s\n", __func__);
5716
Weston Andros Adamson35124a02011-03-24 16:48:21 -04005717 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005718 lgp->res.seq_res.sr_slot = NULL;
5719 task = rpc_run_task(&task_setup_data);
5720 if (IS_ERR(task))
5721 return PTR_ERR(task);
5722 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00005723 if (status == 0)
5724 status = task->tk_status;
5725 if (status == 0)
5726 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005727 rpc_put_task(task);
5728 dprintk("<-- %s status=%d\n", __func__, status);
5729 return status;
5730}
5731
Benny Halevycbe82602011-05-22 19:52:37 +03005732static void
5733nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
5734{
5735 struct nfs4_layoutreturn *lrp = calldata;
5736
5737 dprintk("--> %s\n", __func__);
5738 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
5739 &lrp->res.seq_res, 0, task))
5740 return;
5741 rpc_call_start(task);
5742}
5743
5744static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
5745{
5746 struct nfs4_layoutreturn *lrp = calldata;
5747 struct nfs_server *server;
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005748 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03005749
5750 dprintk("--> %s\n", __func__);
5751
5752 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
5753 return;
5754
5755 server = NFS_SERVER(lrp->args.inode);
5756 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005757 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03005758 return;
5759 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005760 spin_lock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005761 if (task->tk_status == 0) {
Benny Halevycbe82602011-05-22 19:52:37 +03005762 if (lrp->res.lrs_present) {
Benny Halevycbe82602011-05-22 19:52:37 +03005763 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Benny Halevycbe82602011-05-22 19:52:37 +03005764 } else
5765 BUG_ON(!list_empty(&lo->plh_segs));
5766 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005767 lo->plh_block_lgets--;
5768 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005769 dprintk("<-- %s\n", __func__);
5770}
5771
5772static void nfs4_layoutreturn_release(void *calldata)
5773{
5774 struct nfs4_layoutreturn *lrp = calldata;
5775
5776 dprintk("--> %s\n", __func__);
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005777 put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03005778 kfree(calldata);
5779 dprintk("<-- %s\n", __func__);
5780}
5781
5782static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
5783 .rpc_call_prepare = nfs4_layoutreturn_prepare,
5784 .rpc_call_done = nfs4_layoutreturn_done,
5785 .rpc_release = nfs4_layoutreturn_release,
5786};
5787
5788int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
5789{
5790 struct rpc_task *task;
5791 struct rpc_message msg = {
5792 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
5793 .rpc_argp = &lrp->args,
5794 .rpc_resp = &lrp->res,
5795 };
5796 struct rpc_task_setup task_setup_data = {
5797 .rpc_client = lrp->clp->cl_rpcclient,
5798 .rpc_message = &msg,
5799 .callback_ops = &nfs4_layoutreturn_call_ops,
5800 .callback_data = lrp,
5801 };
5802 int status;
5803
5804 dprintk("--> %s\n", __func__);
5805 task = rpc_run_task(&task_setup_data);
5806 if (IS_ERR(task))
5807 return PTR_ERR(task);
5808 status = task->tk_status;
5809 dprintk("<-- %s status=%d\n", __func__, status);
5810 rpc_put_task(task);
5811 return status;
5812}
5813
Andy Adamson7f11d8d2011-07-30 20:52:35 -04005814/*
5815 * Retrieve the list of Data Server devices from the MDS.
5816 */
5817static int _nfs4_getdevicelist(struct nfs_server *server,
5818 const struct nfs_fh *fh,
5819 struct pnfs_devicelist *devlist)
5820{
5821 struct nfs4_getdevicelist_args args = {
5822 .fh = fh,
5823 .layoutclass = server->pnfs_curr_ld->id,
5824 };
5825 struct nfs4_getdevicelist_res res = {
5826 .devlist = devlist,
5827 };
5828 struct rpc_message msg = {
5829 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
5830 .rpc_argp = &args,
5831 .rpc_resp = &res,
5832 };
5833 int status;
5834
5835 dprintk("--> %s\n", __func__);
5836 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5837 &res.seq_res, 0);
5838 dprintk("<-- %s status=%d\n", __func__, status);
5839 return status;
5840}
5841
5842int nfs4_proc_getdevicelist(struct nfs_server *server,
5843 const struct nfs_fh *fh,
5844 struct pnfs_devicelist *devlist)
5845{
5846 struct nfs4_exception exception = { };
5847 int err;
5848
5849 do {
5850 err = nfs4_handle_exception(server,
5851 _nfs4_getdevicelist(server, fh, devlist),
5852 &exception);
5853 } while (exception.retry);
5854
5855 dprintk("%s: err=%d, num_devs=%u\n", __func__,
5856 err, devlist->num_devs);
5857
5858 return err;
5859}
5860EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
5861
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005862static int
5863_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5864{
5865 struct nfs4_getdeviceinfo_args args = {
5866 .pdev = pdev,
5867 };
5868 struct nfs4_getdeviceinfo_res res = {
5869 .pdev = pdev,
5870 };
5871 struct rpc_message msg = {
5872 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5873 .rpc_argp = &args,
5874 .rpc_resp = &res,
5875 };
5876 int status;
5877
5878 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005879 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005880 dprintk("<-- %s status=%d\n", __func__, status);
5881
5882 return status;
5883}
5884
5885int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5886{
5887 struct nfs4_exception exception = { };
5888 int err;
5889
5890 do {
5891 err = nfs4_handle_exception(server,
5892 _nfs4_proc_getdeviceinfo(server, pdev),
5893 &exception);
5894 } while (exception.retry);
5895 return err;
5896}
5897EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5898
Andy Adamson863a3c62011-03-23 13:27:54 +00005899static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
5900{
5901 struct nfs4_layoutcommit_data *data = calldata;
5902 struct nfs_server *server = NFS_SERVER(data->args.inode);
5903
5904 if (nfs4_setup_sequence(server, &data->args.seq_args,
5905 &data->res.seq_res, 1, task))
5906 return;
5907 rpc_call_start(task);
5908}
5909
5910static void
5911nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
5912{
5913 struct nfs4_layoutcommit_data *data = calldata;
5914 struct nfs_server *server = NFS_SERVER(data->args.inode);
5915
5916 if (!nfs4_sequence_done(task, &data->res.seq_res))
5917 return;
5918
5919 switch (task->tk_status) { /* Just ignore these failures */
5920 case NFS4ERR_DELEG_REVOKED: /* layout was recalled */
5921 case NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
5922 case NFS4ERR_BADLAYOUT: /* no layout */
5923 case NFS4ERR_GRACE: /* loca_recalim always false */
5924 task->tk_status = 0;
5925 }
5926
5927 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005928 rpc_restart_call_prepare(task);
Andy Adamson863a3c62011-03-23 13:27:54 +00005929 return;
5930 }
5931
5932 if (task->tk_status == 0)
5933 nfs_post_op_update_inode_force_wcc(data->args.inode,
5934 data->res.fattr);
5935}
5936
5937static void nfs4_layoutcommit_release(void *calldata)
5938{
5939 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04005940 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07005941 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00005942
Andy Adamsondb29c082011-07-30 20:52:38 -04005943 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00005944 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04005945 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
5946 list_del_init(&lseg->pls_lc_list);
5947 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
5948 &lseg->pls_flags))
5949 put_lseg(lseg);
5950 }
Peng Tao92407e72011-10-23 20:21:17 -07005951
5952 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
5953 smp_mb__after_clear_bit();
5954 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
5955
Andy Adamson863a3c62011-03-23 13:27:54 +00005956 put_rpccred(data->cred);
5957 kfree(data);
5958}
5959
5960static const struct rpc_call_ops nfs4_layoutcommit_ops = {
5961 .rpc_call_prepare = nfs4_layoutcommit_prepare,
5962 .rpc_call_done = nfs4_layoutcommit_done,
5963 .rpc_release = nfs4_layoutcommit_release,
5964};
5965
5966int
Andy Adamsonef311532011-03-12 02:58:10 -05005967nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00005968{
5969 struct rpc_message msg = {
5970 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
5971 .rpc_argp = &data->args,
5972 .rpc_resp = &data->res,
5973 .rpc_cred = data->cred,
5974 };
5975 struct rpc_task_setup task_setup_data = {
5976 .task = &data->task,
5977 .rpc_client = NFS_CLIENT(data->args.inode),
5978 .rpc_message = &msg,
5979 .callback_ops = &nfs4_layoutcommit_ops,
5980 .callback_data = data,
5981 .flags = RPC_TASK_ASYNC,
5982 };
5983 struct rpc_task *task;
5984 int status = 0;
5985
5986 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
5987 "lbw: %llu inode %lu\n",
5988 data->task.tk_pid, sync,
5989 data->args.lastbytewritten,
5990 data->args.inode->i_ino);
5991
5992 task = rpc_run_task(&task_setup_data);
5993 if (IS_ERR(task))
5994 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05005995 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00005996 goto out;
5997 status = nfs4_wait_for_completion_rpc_task(task);
5998 if (status != 0)
5999 goto out;
6000 status = task->tk_status;
6001out:
6002 dprintk("%s: status %d\n", __func__, status);
6003 rpc_put_task(task);
6004 return status;
6005}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006006
6007static int
6008_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6009 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6010{
6011 struct nfs41_secinfo_no_name_args args = {
6012 .style = SECINFO_STYLE_CURRENT_FH,
6013 };
6014 struct nfs4_secinfo_res res = {
6015 .flavors = flavors,
6016 };
6017 struct rpc_message msg = {
6018 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6019 .rpc_argp = &args,
6020 .rpc_resp = &res,
6021 };
6022 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6023}
6024
6025static int
6026nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6027 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6028{
6029 struct nfs4_exception exception = { };
6030 int err;
6031 do {
6032 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6033 switch (err) {
6034 case 0:
6035 case -NFS4ERR_WRONGSEC:
6036 case -NFS4ERR_NOTSUPP:
6037 break;
6038 default:
6039 err = nfs4_handle_exception(server, err, &exception);
6040 }
6041 } while (exception.retry);
6042 return err;
6043}
6044
6045static int
6046nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6047 struct nfs_fsinfo *info)
6048{
6049 int err;
6050 struct page *page;
6051 rpc_authflavor_t flavor;
6052 struct nfs4_secinfo_flavors *flavors;
6053
6054 page = alloc_page(GFP_KERNEL);
6055 if (!page) {
6056 err = -ENOMEM;
6057 goto out;
6058 }
6059
6060 flavors = page_address(page);
6061 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6062
6063 /*
6064 * Fall back on "guess and check" method if
6065 * the server doesn't support SECINFO_NO_NAME
6066 */
6067 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6068 err = nfs4_find_root_sec(server, fhandle, info);
6069 goto out_freepage;
6070 }
6071 if (err)
6072 goto out_freepage;
6073
6074 flavor = nfs_find_best_sec(flavors);
6075 if (err == 0)
6076 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6077
6078out_freepage:
6079 put_page(page);
6080 if (err == -EACCES)
6081 return -EPERM;
6082out:
6083 return err;
6084}
Bryan Schumaker7d974792011-06-02 14:59:08 -04006085static int _nfs41_test_stateid(struct nfs_server *server, struct nfs4_state *state)
6086{
6087 int status;
6088 struct nfs41_test_stateid_args args = {
6089 .stateid = &state->stateid,
6090 };
6091 struct nfs41_test_stateid_res res;
6092 struct rpc_message msg = {
6093 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6094 .rpc_argp = &args,
6095 .rpc_resp = &res,
6096 };
6097 args.seq_args.sa_session = res.seq_res.sr_session = NULL;
6098 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 0, 1);
6099 return status;
6100}
6101
6102static int nfs41_test_stateid(struct nfs_server *server, struct nfs4_state *state)
6103{
6104 struct nfs4_exception exception = { };
6105 int err;
6106 do {
6107 err = nfs4_handle_exception(server,
6108 _nfs41_test_stateid(server, state),
6109 &exception);
6110 } while (exception.retry);
6111 return err;
6112}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006113
6114static int _nfs4_free_stateid(struct nfs_server *server, struct nfs4_state *state)
6115{
6116 int status;
6117 struct nfs41_free_stateid_args args = {
6118 .stateid = &state->stateid,
6119 };
6120 struct nfs41_free_stateid_res res;
6121 struct rpc_message msg = {
6122 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6123 .rpc_argp = &args,
6124 .rpc_resp = &res,
6125 };
6126
6127 args.seq_args.sa_session = res.seq_res.sr_session = NULL;
6128 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 0, 1);
6129 return status;
6130}
6131
6132static int nfs41_free_stateid(struct nfs_server *server, struct nfs4_state *state)
6133{
6134 struct nfs4_exception exception = { };
6135 int err;
6136 do {
6137 err = nfs4_handle_exception(server,
6138 _nfs4_free_stateid(server, state),
6139 &exception);
6140 } while (exception.retry);
6141 return err;
6142}
Andy Adamson557134a2009-04-01 09:21:53 -04006143#endif /* CONFIG_NFS_V4_1 */
6144
Andy Adamson591d71c2009-04-01 09:22:47 -04006145struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006146 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006147 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006148 .recover_open = nfs4_open_reclaim,
6149 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006150 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006151 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006152};
6153
Andy Adamson591d71c2009-04-01 09:22:47 -04006154#if defined(CONFIG_NFS_V4_1)
6155struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6156 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6157 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6158 .recover_open = nfs4_open_reclaim,
6159 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006160 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006161 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006162 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006163};
6164#endif /* CONFIG_NFS_V4_1 */
6165
6166struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006167 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006168 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006169 .recover_open = nfs4_open_expired,
6170 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006171 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006172 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006173};
6174
Andy Adamson591d71c2009-04-01 09:22:47 -04006175#if defined(CONFIG_NFS_V4_1)
6176struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6177 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6178 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006179 .recover_open = nfs41_open_expired,
6180 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006181 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006182 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006183};
6184#endif /* CONFIG_NFS_V4_1 */
6185
Benny Halevy29fba382009-04-01 09:22:44 -04006186struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6187 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006188 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006189 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006190};
6191
6192#if defined(CONFIG_NFS_V4_1)
6193struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
6194 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006195 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006196 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006197};
6198#endif
6199
Trond Myklebust97dc1352010-06-16 09:52:26 -04006200static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6201 .minor_version = 0,
6202 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04006203 .validate_stateid = nfs4_validate_delegation_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006204 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006205 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6206 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6207 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006208};
6209
6210#if defined(CONFIG_NFS_V4_1)
6211static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6212 .minor_version = 1,
6213 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04006214 .validate_stateid = nfs41_validate_delegation_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006215 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006216 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6217 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6218 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006219};
6220#endif
6221
Trond Myklebust97dc1352010-06-16 09:52:26 -04006222const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6223 [0] = &nfs_v4_0_minor_ops,
6224#if defined(CONFIG_NFS_V4_1)
6225 [1] = &nfs_v4_1_minor_ops,
6226#endif
6227};
6228
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006229static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006230 .permission = nfs_permission,
6231 .getattr = nfs_getattr,
6232 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006233 .getxattr = generic_getxattr,
6234 .setxattr = generic_setxattr,
6235 .listxattr = generic_listxattr,
6236 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006237};
6238
David Howells509de812006-08-22 20:06:11 -04006239const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240 .version = 4, /* protocol version */
6241 .dentry_ops = &nfs4_dentry_operations,
6242 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006243 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006244 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245 .getroot = nfs4_proc_get_root,
6246 .getattr = nfs4_proc_getattr,
6247 .setattr = nfs4_proc_setattr,
6248 .lookup = nfs4_proc_lookup,
6249 .access = nfs4_proc_access,
6250 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006251 .create = nfs4_proc_create,
6252 .remove = nfs4_proc_remove,
6253 .unlink_setup = nfs4_proc_unlink_setup,
6254 .unlink_done = nfs4_proc_unlink_done,
6255 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006256 .rename_setup = nfs4_proc_rename_setup,
6257 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006258 .link = nfs4_proc_link,
6259 .symlink = nfs4_proc_symlink,
6260 .mkdir = nfs4_proc_mkdir,
6261 .rmdir = nfs4_proc_remove,
6262 .readdir = nfs4_proc_readdir,
6263 .mknod = nfs4_proc_mknod,
6264 .statfs = nfs4_proc_statfs,
6265 .fsinfo = nfs4_proc_fsinfo,
6266 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006267 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006268 .decode_dirent = nfs4_decode_dirent,
6269 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05006270 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006272 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006273 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006274 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006276 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006277 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006278 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00006279 .init_client = nfs4_init_client,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006280 .secinfo = nfs4_proc_secinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281};
6282
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006283static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6284 .prefix = XATTR_NAME_NFSV4_ACL,
6285 .list = nfs4_xattr_list_nfs4_acl,
6286 .get = nfs4_xattr_get_nfs4_acl,
6287 .set = nfs4_xattr_set_nfs4_acl,
6288};
6289
6290const struct xattr_handler *nfs4_xattr_handlers[] = {
6291 &nfs4_xattr_nfs4_acl_handler,
6292 NULL
6293};
6294
Linus Torvalds1da177e2005-04-16 15:20:36 -07006295/*
6296 * Local variables:
6297 * c-basic-offset: 8
6298 * End:
6299 */