blob: b2104461ed4fa0d4559236d6a8f88be330282f2f [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 Adamsonea028ac2009-12-04 15:55:38 -0500555 /*
556 * The state manager will wait until the slot table is empty.
557 * Schedule the reset thread
558 */
Andy Adamson05f0d232009-12-04 15:55:37 -0500559 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400560 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500561 dprintk("%s Schedule Session Reset\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500562 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400563 }
564
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800565 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
566 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
567 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
568 spin_unlock(&tbl->slot_tbl_lock);
569 dprintk("%s enforce FIFO order\n", __func__);
570 return -EAGAIN;
571 }
572
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800573 slotid = nfs4_find_slot(tbl);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400574 if (slotid == NFS4_MAX_SLOT_TABLE) {
575 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
576 spin_unlock(&tbl->slot_tbl_lock);
577 dprintk("<-- %s: no free slots\n", __func__);
578 return -EAGAIN;
579 }
580 spin_unlock(&tbl->slot_tbl_lock);
581
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800582 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400583 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400584 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400585 args->sa_slotid = slotid;
586 args->sa_cache_this = cache_reply;
587
588 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
589
Benny Halevy99fe60d2009-04-01 09:22:29 -0400590 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400591 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400592 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400593 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400594 /*
595 * sr_status is only set in decode_sequence, and so will remain
596 * set to 1 if an rpc level failure occurs.
597 */
598 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400599 return 0;
600}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000601EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400602
Trond Myklebust035168a2010-06-16 09:52:26 -0400603int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400604 struct nfs4_sequence_args *args,
605 struct nfs4_sequence_res *res,
606 int cache_reply,
607 struct rpc_task *task)
608{
Trond Myklebust035168a2010-06-16 09:52:26 -0400609 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400610 int ret = 0;
611
Trond Myklebust035168a2010-06-16 09:52:26 -0400612 if (session == NULL) {
613 args->sa_session = NULL;
614 res->sr_session = NULL;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400615 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400616 }
617
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200618 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400619 __func__, session->clp, session, res->sr_slot ?
620 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400621
622 ret = nfs41_setup_sequence(session, args, res, cache_reply,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400623 task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400624out:
625 dprintk("<-- %s status=%d\n", __func__, ret);
626 return ret;
627}
628
629struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400630 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400631 struct nfs4_sequence_args *seq_args;
632 struct nfs4_sequence_res *seq_res;
633 int cache_reply;
634};
635
636static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
637{
638 struct nfs41_call_sync_data *data = calldata;
639
Trond Myklebust035168a2010-06-16 09:52:26 -0400640 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
641
642 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400643 data->seq_res, data->cache_reply, task))
644 return;
645 rpc_call_start(task);
646}
647
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800648static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
649{
650 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
651 nfs41_call_sync_prepare(task, calldata);
652}
653
Andy Adamson69ab40c2009-04-01 09:22:19 -0400654static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
655{
656 struct nfs41_call_sync_data *data = calldata;
657
Trond Myklebust14516c32010-07-31 14:29:06 -0400658 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400659}
660
Andy Adamsonce5039c2009-04-01 09:22:13 -0400661struct rpc_call_ops nfs41_call_sync_ops = {
662 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400663 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400664};
665
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800666struct rpc_call_ops nfs41_call_priv_sync_ops = {
667 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
668 .rpc_call_done = nfs41_call_sync_done,
669};
670
Bryan Schumaker7c513052011-03-24 17:12:24 +0000671static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
672 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400673 struct rpc_message *msg,
674 struct nfs4_sequence_args *args,
675 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800676 int cache_reply,
677 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400678{
679 int ret;
680 struct rpc_task *task;
681 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400682 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400683 .seq_args = args,
684 .seq_res = res,
685 .cache_reply = cache_reply,
686 };
687 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000688 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400689 .rpc_message = msg,
690 .callback_ops = &nfs41_call_sync_ops,
691 .callback_data = &data
692 };
693
Benny Halevydfb4f3092010-09-24 09:17:01 -0400694 res->sr_slot = NULL;
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800695 if (privileged)
696 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400697 task = rpc_run_task(&task_setup);
698 if (IS_ERR(task))
699 ret = PTR_ERR(task);
700 else {
701 ret = task->tk_status;
702 rpc_put_task(task);
703 }
704 return ret;
705}
706
Bryan Schumaker7c513052011-03-24 17:12:24 +0000707int _nfs4_call_sync_session(struct rpc_clnt *clnt,
708 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400709 struct rpc_message *msg,
710 struct nfs4_sequence_args *args,
711 struct nfs4_sequence_res *res,
712 int cache_reply)
713{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000714 return nfs4_call_sync_sequence(clnt, server, msg, args, res, cache_reply, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400715}
716
Trond Myklebustdf896452010-06-16 09:52:26 -0400717#else
Trond Myklebust14516c32010-07-31 14:29:06 -0400718static int nfs4_sequence_done(struct rpc_task *task,
719 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400720{
Trond Myklebust14516c32010-07-31 14:29:06 -0400721 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400722}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400723#endif /* CONFIG_NFS_V4_1 */
724
Bryan Schumaker7c513052011-03-24 17:12:24 +0000725int _nfs4_call_sync(struct rpc_clnt *clnt,
726 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400727 struct rpc_message *msg,
728 struct nfs4_sequence_args *args,
729 struct nfs4_sequence_res *res,
730 int cache_reply)
731{
Benny Halevyf3752972009-04-01 09:22:04 -0400732 args->sa_session = res->sr_session = NULL;
Bryan Schumaker7c513052011-03-24 17:12:24 +0000733 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400734}
735
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000736static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000737int nfs4_call_sync(struct rpc_clnt *clnt,
738 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000739 struct rpc_message *msg,
740 struct nfs4_sequence_args *args,
741 struct nfs4_sequence_res *res,
742 int cache_reply)
743{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000744 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
745 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000746}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400747
Trond Myklebust38478b22006-05-25 01:40:57 -0400748static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
Trond Myklebust38478b22006-05-25 01:40:57 -0400750 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Trond Myklebust38478b22006-05-25 01:40:57 -0400752 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400753 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700754 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400755 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700756 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400757 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758}
759
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100760struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400761 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100762 struct nfs_openargs o_arg;
763 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100764 struct nfs_open_confirmargs c_arg;
765 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100766 struct nfs_fattr f_attr;
767 struct nfs_fattr dir_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100768 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400769 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100770 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400771 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100772 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100773 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400774 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100775 int rpc_status;
776 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100777};
778
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400779
780static void nfs4_init_opendata_res(struct nfs4_opendata *p)
781{
782 p->o_res.f_attr = &p->f_attr;
783 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400784 p->o_res.seqid = p->o_arg.seqid;
785 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400786 p->o_res.server = p->o_arg.server;
787 nfs_fattr_init(&p->f_attr);
788 nfs_fattr_init(&p->dir_attr);
789}
790
Al Viro82a2c1b2011-06-22 18:30:55 -0400791static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500792 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400793 const struct iattr *attrs,
794 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100795{
Al Viro82a2c1b2011-06-22 18:30:55 -0400796 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100797 struct inode *dir = parent->d_inode;
798 struct nfs_server *server = NFS_SERVER(dir);
799 struct nfs4_opendata *p;
800
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400801 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100802 if (p == NULL)
803 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400804 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100805 if (p->o_arg.seqid == NULL)
806 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400807 nfs_sb_active(dentry->d_sb);
808 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100809 p->dir = parent;
810 p->owner = sp;
811 atomic_inc(&sp->so_count);
812 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500813 p->o_arg.open_flags = flags;
814 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400815 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400816 p->o_arg.id = sp->so_owner_id.id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400817 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100818 p->o_arg.server = server;
819 p->o_arg.bitmask = server->attr_bitmask;
820 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400821 if (flags & O_CREAT) {
822 u32 *s;
823
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100824 p->o_arg.u.attrs = &p->attrs;
825 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400826 s = (u32 *) p->o_arg.u.verifier.data;
827 s[0] = jiffies;
828 s[1] = current->pid;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100829 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100830 p->c_arg.fh = &p->o_res.fh;
831 p->c_arg.stateid = &p->o_res.stateid;
832 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400833 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400834 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100835 return p;
836err_free:
837 kfree(p);
838err:
839 dput(parent);
840 return NULL;
841}
842
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400843static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100844{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400845 struct nfs4_opendata *p = container_of(kref,
846 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400847 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400848
849 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400850 if (p->state != NULL)
851 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400852 nfs4_put_state_owner(p->owner);
853 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400854 dput(p->dentry);
855 nfs_sb_deactive(sb);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400856 kfree(p);
857}
858
859static void nfs4_opendata_put(struct nfs4_opendata *p)
860{
861 if (p != NULL)
862 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100863}
864
Trond Myklebust06f814a2006-01-03 09:55:07 +0100865static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
866{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100867 int ret;
868
Trond Myklebust06f814a2006-01-03 09:55:07 +0100869 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100870 return ret;
871}
872
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500873static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400874{
875 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500876
877 if (open_mode & O_EXCL)
878 goto out;
879 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400880 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500881 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
882 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400883 break;
884 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500885 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
886 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400887 break;
888 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500889 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
890 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400891 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500892out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400893 return ret;
894}
895
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500896static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400897{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500898 if (delegation == NULL)
899 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500900 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400901 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500902 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400903 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500904 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400905 return 1;
906}
907
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500908static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100909{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500910 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100911 case FMODE_WRITE:
912 state->n_wronly++;
913 break;
914 case FMODE_READ:
915 state->n_rdonly++;
916 break;
917 case FMODE_READ|FMODE_WRITE:
918 state->n_rdwr++;
919 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500920 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100921}
922
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500923static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400924{
925 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
926 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
927 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500928 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400929 case FMODE_READ:
930 set_bit(NFS_O_RDONLY_STATE, &state->flags);
931 break;
932 case FMODE_WRITE:
933 set_bit(NFS_O_WRONLY_STATE, &state->flags);
934 break;
935 case FMODE_READ|FMODE_WRITE:
936 set_bit(NFS_O_RDWR_STATE, &state->flags);
937 }
938}
939
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500940static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400941{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400942 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500943 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400944 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400945}
946
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500947static 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 -0700948{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400949 /*
950 * Protect the call to nfs4_state_set_mode_locked and
951 * serialise the stateid update
952 */
953 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400954 if (deleg_stateid != NULL) {
955 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
956 set_bit(NFS_DELEGATED_STATE, &state->flags);
957 }
958 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500959 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400960 write_sequnlock(&state->seqlock);
961 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500962 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700963 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964}
965
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500966static 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 -0500967{
968 struct nfs_inode *nfsi = NFS_I(state->inode);
969 struct nfs_delegation *deleg_cur;
970 int ret = 0;
971
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500972 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500973
974 rcu_read_lock();
975 deleg_cur = rcu_dereference(nfsi->delegation);
976 if (deleg_cur == NULL)
977 goto no_delegation;
978
979 spin_lock(&deleg_cur->lock);
980 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500981 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500982 goto no_delegation_unlock;
983
984 if (delegation == NULL)
985 delegation = &deleg_cur->stateid;
986 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
987 goto no_delegation_unlock;
988
Trond Myklebustb7391f42008-12-23 15:21:52 -0500989 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500990 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500991 ret = 1;
992no_delegation_unlock:
993 spin_unlock(&deleg_cur->lock);
994no_delegation:
995 rcu_read_unlock();
996
997 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500998 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500999 ret = 1;
1000 }
1001
1002 return ret;
1003}
1004
1005
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001006static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001007{
1008 struct nfs_delegation *delegation;
1009
1010 rcu_read_lock();
1011 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001012 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001013 rcu_read_unlock();
1014 return;
1015 }
1016 rcu_read_unlock();
1017 nfs_inode_return_delegation(inode);
1018}
1019
Trond Myklebust6ee41262007-07-08 14:11:36 -04001020static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001021{
1022 struct nfs4_state *state = opendata->state;
1023 struct nfs_inode *nfsi = NFS_I(state->inode);
1024 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001025 int open_mode = opendata->o_arg.open_flags & O_EXCL;
1026 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001027 nfs4_stateid stateid;
1028 int ret = -EAGAIN;
1029
Trond Myklebustaac00a82007-07-05 19:02:21 -04001030 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001031 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001032 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001033 if (can_open_cached(state, fmode, open_mode)) {
1034 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001035 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001036 goto out_return_state;
1037 }
1038 spin_unlock(&state->owner->so_lock);
1039 }
Trond Myklebust34310432008-12-23 15:21:38 -05001040 rcu_read_lock();
1041 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001042 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001043 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001044 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001045 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001046 /* Save the delegation */
1047 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1048 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001049 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001050 if (ret != 0)
1051 goto out;
1052 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001053
1054 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001055 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001056 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001057 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001058out:
1059 return ERR_PTR(ret);
1060out_return_state:
1061 atomic_inc(&state->count);
1062 return state;
1063}
1064
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001065static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1066{
1067 struct inode *inode;
1068 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001069 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001070 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001071
Trond Myklebustaac00a82007-07-05 19:02:21 -04001072 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001073 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001074 goto out;
1075 }
1076
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001077 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001078 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001079 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001080 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001081 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001082 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001083 goto err;
1084 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001085 state = nfs4_get_open_state(inode, data->owner);
1086 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001087 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001088 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001089 int delegation_flags = 0;
1090
Trond Myklebust003707c2007-07-05 18:07:55 -04001091 rcu_read_lock();
1092 delegation = rcu_dereference(NFS_I(inode)->delegation);
1093 if (delegation)
1094 delegation_flags = delegation->flags;
1095 rcu_read_unlock();
Trond Myklebust652f89f2011-12-09 19:05:58 -05001096 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1097 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1098 "returning a delegation for "
1099 "OPEN(CLAIM_DELEGATE_CUR)\n",
1100 NFS_CLIENT(inode)->cl_server);
1101 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001102 nfs_inode_set_delegation(state->inode,
1103 data->owner->so_cred,
1104 &data->o_res);
1105 else
1106 nfs_inode_reclaim_delegation(state->inode,
1107 data->owner->so_cred,
1108 &data->o_res);
1109 }
Trond Myklebust34310432008-12-23 15:21:38 -05001110
1111 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001112 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001113 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001114out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001115 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001116err_put_inode:
1117 iput(inode);
1118err:
1119 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001120}
1121
Trond Myklebust864472e2006-01-03 09:55:15 +01001122static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1123{
1124 struct nfs_inode *nfsi = NFS_I(state->inode);
1125 struct nfs_open_context *ctx;
1126
1127 spin_lock(&state->inode->i_lock);
1128 list_for_each_entry(ctx, &nfsi->open_files, list) {
1129 if (ctx->state != state)
1130 continue;
1131 get_nfs_open_context(ctx);
1132 spin_unlock(&state->inode->i_lock);
1133 return ctx;
1134 }
1135 spin_unlock(&state->inode->i_lock);
1136 return ERR_PTR(-ENOENT);
1137}
1138
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001139static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1140{
1141 struct nfs4_opendata *opendata;
1142
Al Viro3d4ff432011-06-22 18:40:12 -04001143 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001144 if (opendata == NULL)
1145 return ERR_PTR(-ENOMEM);
1146 opendata->state = state;
1147 atomic_inc(&state->count);
1148 return opendata;
1149}
1150
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001151static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001152{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001153 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001154 int ret;
1155
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001156 opendata->o_arg.open_flags = 0;
1157 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001158 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1159 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1160 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001161 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001162 if (ret != 0)
1163 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001164 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001165 if (IS_ERR(newstate))
1166 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001167 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001168 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001169 return 0;
1170}
1171
1172static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1173{
Trond Myklebust864472e2006-01-03 09:55:15 +01001174 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001175 int ret;
1176
1177 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001178 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001179 smp_rmb();
1180 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001181 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001182 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001183 if (ret != 0)
1184 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001185 if (newstate != state)
1186 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001187 }
1188 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001189 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001190 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001191 if (ret != 0)
1192 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001193 if (newstate != state)
1194 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001195 }
1196 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001197 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001198 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001199 if (ret != 0)
1200 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001201 if (newstate != state)
1202 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001203 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001204 /*
1205 * We may have performed cached opens for all three recoveries.
1206 * Check if we need to update the current stateid.
1207 */
1208 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1209 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001210 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001211 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1212 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001213 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001214 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001215 return 0;
1216}
1217
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218/*
1219 * OPEN_RECLAIM:
1220 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001222static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001224 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001225 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001226 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 int status;
1228
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001229 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1230 if (IS_ERR(opendata))
1231 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001232 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1233 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001234 rcu_read_lock();
1235 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001236 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001237 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001238 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001239 opendata->o_arg.u.delegation_type = delegation_type;
1240 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001241 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 return status;
1243}
1244
Trond Myklebust539cd032007-06-05 11:46:42 -04001245static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
1247 struct nfs_server *server = NFS_SERVER(state->inode);
1248 struct nfs4_exception exception = { };
1249 int err;
1250 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001251 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001252 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001253 break;
1254 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 } while (exception.retry);
1256 return err;
1257}
1258
Trond Myklebust864472e2006-01-03 09:55:15 +01001259static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1260{
1261 struct nfs_open_context *ctx;
1262 int ret;
1263
1264 ctx = nfs4_state_find_open_context(state);
1265 if (IS_ERR(ctx))
1266 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001267 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001268 put_nfs_open_context(ctx);
1269 return ret;
1270}
1271
Trond Myklebust13437e12007-07-06 15:10:43 -04001272static 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 -07001273{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001274 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001275 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001277 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1278 if (IS_ERR(opendata))
1279 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001280 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001281 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001282 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001283 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001284 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001285 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286}
1287
Trond Myklebust13437e12007-07-06 15:10:43 -04001288int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289{
1290 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001291 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 int err;
1293 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001294 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 switch (err) {
1296 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001297 case -ENOENT:
1298 case -ESTALE:
1299 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001300 case -NFS4ERR_BADSESSION:
1301 case -NFS4ERR_BADSLOT:
1302 case -NFS4ERR_BAD_HIGH_SLOT:
1303 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1304 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001305 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001306 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 case -NFS4ERR_STALE_CLIENTID:
1308 case -NFS4ERR_STALE_STATEID:
1309 case -NFS4ERR_EXPIRED:
1310 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001311 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001312 goto out;
1313 case -ERESTARTSYS:
1314 /*
1315 * The show must go on: exit, but mark the
1316 * stateid as needing recovery.
1317 */
1318 case -NFS4ERR_ADMIN_REVOKED:
1319 case -NFS4ERR_BAD_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001320 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001321 case -EKEYEXPIRED:
1322 /*
1323 * User RPCSEC_GSS context has expired.
1324 * We cannot recover this stateid now, so
1325 * skip it and allow recovery thread to
1326 * proceed.
1327 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001328 case -ENOMEM:
1329 err = 0;
1330 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 }
1332 err = nfs4_handle_exception(server, err, &exception);
1333 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001334out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 return err;
1336}
1337
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001338static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1339{
1340 struct nfs4_opendata *data = calldata;
1341
1342 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001343 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001344 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1345 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001346 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001347 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001348 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001349 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001350}
1351
1352static void nfs4_open_confirm_release(void *calldata)
1353{
1354 struct nfs4_opendata *data = calldata;
1355 struct nfs4_state *state = NULL;
1356
1357 /* If this request hasn't been cancelled, do nothing */
1358 if (data->cancelled == 0)
1359 goto out_free;
1360 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001361 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001362 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001363 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001364 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001365 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001366out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001367 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001368}
1369
1370static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001371 .rpc_call_done = nfs4_open_confirm_done,
1372 .rpc_release = nfs4_open_confirm_release,
1373};
1374
1375/*
1376 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1377 */
1378static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1379{
1380 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1381 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001382 struct rpc_message msg = {
1383 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1384 .rpc_argp = &data->c_arg,
1385 .rpc_resp = &data->c_res,
1386 .rpc_cred = data->owner->so_cred,
1387 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001388 struct rpc_task_setup task_setup_data = {
1389 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001390 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001391 .callback_ops = &nfs4_open_confirm_ops,
1392 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001393 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001394 .flags = RPC_TASK_ASYNC,
1395 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 int status;
1397
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001398 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001399 data->rpc_done = 0;
1400 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001401 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001402 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001403 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001404 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001405 status = nfs4_wait_for_completion_rpc_task(task);
1406 if (status != 0) {
1407 data->cancelled = 1;
1408 smp_wmb();
1409 } else
1410 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001411 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 return status;
1413}
1414
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001415static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001417 struct nfs4_opendata *data = calldata;
1418 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001419
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001420 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1421 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001422 /*
1423 * Check if we still need to send an OPEN call, or if we can use
1424 * a delegation instead.
1425 */
1426 if (data->state != NULL) {
1427 struct nfs_delegation *delegation;
1428
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001429 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001430 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001431 rcu_read_lock();
1432 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001433 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1434 can_open_delegated(delegation, data->o_arg.fmode))
1435 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001436 rcu_read_unlock();
1437 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001438 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001439 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001440 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001441 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001442 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001443 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1444 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001445 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001446 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001447 &data->o_arg.seq_args,
1448 &data->o_res.seq_res, 1, task))
1449 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001450 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001451 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001452unlock_no_action:
1453 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001454out_no_action:
1455 task->tk_action = NULL;
1456
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001457}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001459static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1460{
1461 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1462 nfs4_open_prepare(task, calldata);
1463}
1464
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001465static void nfs4_open_done(struct rpc_task *task, void *calldata)
1466{
1467 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001469 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001470
Trond Myklebust14516c32010-07-31 14:29:06 -04001471 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1472 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001473
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001474 if (task->tk_status == 0) {
1475 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001476 case S_IFREG:
1477 break;
1478 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001479 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001480 break;
1481 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001482 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001483 break;
1484 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001485 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001486 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001487 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001488 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1489 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001490 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001491 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001492}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001493
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001494static void nfs4_open_release(void *calldata)
1495{
1496 struct nfs4_opendata *data = calldata;
1497 struct nfs4_state *state = NULL;
1498
1499 /* If this request hasn't been cancelled, do nothing */
1500 if (data->cancelled == 0)
1501 goto out_free;
1502 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001503 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001504 goto out_free;
1505 /* In case we need an open_confirm, no cleanup! */
1506 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1507 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001508 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001509 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001510 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001511out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001512 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001513}
1514
1515static const struct rpc_call_ops nfs4_open_ops = {
1516 .rpc_call_prepare = nfs4_open_prepare,
1517 .rpc_call_done = nfs4_open_done,
1518 .rpc_release = nfs4_open_release,
1519};
1520
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001521static const struct rpc_call_ops nfs4_recover_open_ops = {
1522 .rpc_call_prepare = nfs4_recover_open_prepare,
1523 .rpc_call_done = nfs4_open_done,
1524 .rpc_release = nfs4_open_release,
1525};
1526
1527static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001528{
1529 struct inode *dir = data->dir->d_inode;
1530 struct nfs_server *server = NFS_SERVER(dir);
1531 struct nfs_openargs *o_arg = &data->o_arg;
1532 struct nfs_openres *o_res = &data->o_res;
1533 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001534 struct rpc_message msg = {
1535 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1536 .rpc_argp = o_arg,
1537 .rpc_resp = o_res,
1538 .rpc_cred = data->owner->so_cred,
1539 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001540 struct rpc_task_setup task_setup_data = {
1541 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001542 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001543 .callback_ops = &nfs4_open_ops,
1544 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001545 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001546 .flags = RPC_TASK_ASYNC,
1547 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001548 int status;
1549
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001550 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001551 data->rpc_done = 0;
1552 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001553 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001554 if (isrecover)
1555 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001556 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001557 if (IS_ERR(task))
1558 return PTR_ERR(task);
1559 status = nfs4_wait_for_completion_rpc_task(task);
1560 if (status != 0) {
1561 data->cancelled = 1;
1562 smp_wmb();
1563 } else
1564 status = data->rpc_status;
1565 rpc_put_task(task);
1566
1567 return status;
1568}
1569
1570static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1571{
1572 struct inode *dir = data->dir->d_inode;
1573 struct nfs_openres *o_res = &data->o_res;
1574 int status;
1575
1576 status = nfs4_run_open_task(data, 1);
1577 if (status != 0 || !data->rpc_done)
1578 return status;
1579
1580 nfs_refresh_inode(dir, o_res->dir_attr);
1581
1582 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1583 status = _nfs4_proc_open_confirm(data);
1584 if (status != 0)
1585 return status;
1586 }
1587
1588 return status;
1589}
1590
1591/*
1592 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1593 */
1594static int _nfs4_proc_open(struct nfs4_opendata *data)
1595{
1596 struct inode *dir = data->dir->d_inode;
1597 struct nfs_server *server = NFS_SERVER(dir);
1598 struct nfs_openargs *o_arg = &data->o_arg;
1599 struct nfs_openres *o_res = &data->o_res;
1600 int status;
1601
1602 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001603 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001604 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001605 if (status != 0) {
1606 if (status == -NFS4ERR_BADNAME &&
1607 !(o_arg->open_flags & O_CREAT))
1608 return -ENOENT;
1609 return status;
1610 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001611
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001612 if (o_arg->open_flags & O_CREAT) {
1613 update_changeattr(dir, &o_res->cinfo);
1614 nfs_post_op_update_inode(dir, o_res->dir_attr);
1615 } else
1616 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001617 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1618 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001620 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001622 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 }
1624 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001625 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001626 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627}
1628
Andy Adamsond83217c2011-03-01 01:34:17 +00001629static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001630{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001631 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001632 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001633
Trond Myklebusta78cb572009-08-09 15:06:19 -04001634 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001635 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001636 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001637 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001638 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1639 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001640 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001641 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001642 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001643 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001644 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001645}
1646
Andy Adamsond83217c2011-03-01 01:34:17 +00001647static int nfs4_recover_expired_lease(struct nfs_server *server)
1648{
1649 return nfs4_client_recover_expired_lease(server->nfs_client);
1650}
1651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652/*
1653 * OPEN_EXPIRED:
1654 * reclaim state on the server after a network partition.
1655 * Assumes caller holds the appropriate lock
1656 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001657static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001659 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001660 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001662 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1663 if (IS_ERR(opendata))
1664 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001665 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001666 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001667 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001668 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001669 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670}
1671
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001672static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001673{
Trond Myklebust539cd032007-06-05 11:46:42 -04001674 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001675 struct nfs4_exception exception = { };
1676 int err;
1677
1678 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001679 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001680 switch (err) {
1681 default:
1682 goto out;
1683 case -NFS4ERR_GRACE:
1684 case -NFS4ERR_DELAY:
1685 nfs4_handle_exception(server, err, &exception);
1686 err = 0;
1687 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001688 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001689out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001690 return err;
1691}
1692
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1694{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001696 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Trond Myklebust864472e2006-01-03 09:55:15 +01001698 ctx = nfs4_state_find_open_context(state);
1699 if (IS_ERR(ctx))
1700 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001701 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001702 put_nfs_open_context(ctx);
1703 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704}
1705
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001706#if defined(CONFIG_NFS_V4_1)
1707static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1708{
1709 int status;
1710 struct nfs_server *server = NFS_SERVER(state->inode);
1711
1712 status = nfs41_test_stateid(server, state);
1713 if (status == NFS_OK)
1714 return 0;
1715 nfs41_free_stateid(server, state);
1716 return nfs4_open_expired(sp, state);
1717}
1718#endif
1719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001721 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1722 * fields corresponding to attributes that were used to store the verifier.
1723 * Make sure we clobber those fields in the later setattr call
1724 */
1725static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1726{
1727 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1728 !(sattr->ia_valid & ATTR_ATIME_SET))
1729 sattr->ia_valid |= ATTR_ATIME;
1730
1731 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1732 !(sattr->ia_valid & ATTR_MTIME_SET))
1733 sattr->ia_valid |= ATTR_MTIME;
1734}
1735
1736/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001737 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 */
Al Viro82a2c1b2011-06-22 18:30:55 -04001739static 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 -07001740{
1741 struct nfs4_state_owner *sp;
1742 struct nfs4_state *state = NULL;
1743 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001744 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001745 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
1747 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 status = -ENOMEM;
1749 if (!(sp = nfs4_get_state_owner(server, cred))) {
1750 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1751 goto out_err;
1752 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001753 status = nfs4_recover_expired_lease(server);
1754 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001755 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001756 if (dentry->d_inode != NULL)
1757 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001758 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001759 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001760 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001761 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
Al Viro82a2c1b2011-06-22 18:30:55 -04001763 if (dentry->d_inode != NULL)
1764 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001765
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001766 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001768 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001770 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001771 status = PTR_ERR(state);
1772 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001773 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001774 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001775 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001776
1777 if (opendata->o_arg.open_flags & O_EXCL) {
1778 nfs4_exclusive_attrset(opendata, sattr);
1779
1780 nfs_fattr_init(opendata->o_res.f_attr);
1781 status = nfs4_do_setattr(state->inode, cred,
1782 opendata->o_res.f_attr, sattr,
1783 state);
1784 if (status == 0)
1785 nfs_setattr_update_inode(state->inode, sattr);
1786 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1787 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001788 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 *res = state;
1791 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001792err_opendata_put:
1793 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001794err_put_state_owner:
1795 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 *res = NULL;
1798 return status;
1799}
1800
1801
Al Viro82a2c1b2011-06-22 18:30:55 -04001802static 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 -07001803{
1804 struct nfs4_exception exception = { };
1805 struct nfs4_state *res;
1806 int status;
1807
1808 do {
Al Viro82a2c1b2011-06-22 18:30:55 -04001809 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 if (status == 0)
1811 break;
1812 /* NOTE: BAD_SEQID means the server and client disagree about the
1813 * book-keeping w.r.t. state-changing operations
1814 * (OPEN/CLOSE/LOCK/LOCKU...)
1815 * It is actually a sign of a bug on the client or on the server.
1816 *
1817 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001818 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 * have unhashed the old state_owner for us, and that we can
1820 * therefore safely retry using a new one. We should still warn
1821 * the user though...
1822 */
1823 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001824 printk(KERN_WARNING "NFS: v4 server %s "
1825 " returned a bad sequence-id error!\n",
1826 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 exception.retry = 1;
1828 continue;
1829 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001830 /*
1831 * BAD_STATEID on OPEN means that the server cancelled our
1832 * state before it received the OPEN_CONFIRM.
1833 * Recover by retrying the request as per the discussion
1834 * on Page 181 of RFC3530.
1835 */
1836 if (status == -NFS4ERR_BAD_STATEID) {
1837 exception.retry = 1;
1838 continue;
1839 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001840 if (status == -EAGAIN) {
1841 /* We must have found a delegation */
1842 exception.retry = 1;
1843 continue;
1844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1846 status, &exception));
1847 } while (exception.retry);
1848 return res;
1849}
1850
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001851static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1852 struct nfs_fattr *fattr, struct iattr *sattr,
1853 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001855 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001857 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 .iap = sattr,
1859 .server = server,
1860 .bitmask = server->attr_bitmask,
1861 };
1862 struct nfs_setattrres res = {
1863 .fattr = fattr,
1864 .server = server,
1865 };
1866 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001867 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1868 .rpc_argp = &arg,
1869 .rpc_resp = &res,
1870 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001872 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001873 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
Trond Myklebust0e574af2005-10-27 22:12:38 -04001875 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001877 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1878 /* Use that stateid */
1879 } else if (state != NULL) {
Trond Myklebust77041ed2010-07-01 12:49:11 -04001880 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001881 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1883
Bryan Schumaker7c513052011-03-24 17:12:24 +00001884 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001885 if (status == 0 && state != NULL)
1886 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001887 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888}
1889
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001890static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1891 struct nfs_fattr *fattr, struct iattr *sattr,
1892 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001894 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 struct nfs4_exception exception = { };
1896 int err;
1897 do {
1898 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001899 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 &exception);
1901 } while (exception.retry);
1902 return err;
1903}
1904
1905struct nfs4_closedata {
1906 struct inode *inode;
1907 struct nfs4_state *state;
1908 struct nfs_closeargs arg;
1909 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001910 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001911 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00001912 bool roc;
1913 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914};
1915
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001916static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001917{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001918 struct nfs4_closedata *calldata = data;
1919 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04001920 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07001921
Fred Isamanf7e89172011-01-06 11:36:32 +00001922 if (calldata->roc)
1923 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001924 nfs4_put_open_state(calldata->state);
1925 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001926 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04001927 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07001928 kfree(calldata);
1929}
1930
Trond Myklebust88069f72009-12-08 08:33:16 -05001931static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1932 fmode_t fmode)
1933{
1934 spin_lock(&state->owner->so_lock);
1935 if (!(fmode & FMODE_READ))
1936 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1937 if (!(fmode & FMODE_WRITE))
1938 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1939 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1940 spin_unlock(&state->owner->so_lock);
1941}
1942
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001943static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001945 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 struct nfs_server *server = NFS_SERVER(calldata->inode);
1948
Trond Myklebust14516c32010-07-31 14:29:06 -04001949 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1950 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 /* hmm. we are done with the inode, and in the process of freeing
1952 * the state_owner. we keep this around to process errors
1953 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 switch (task->tk_status) {
1955 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00001956 if (calldata->roc)
1957 pnfs_roc_set_barrier(state->inode,
1958 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04001959 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001960 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001961 nfs4_close_clear_stateid_flags(state,
1962 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 break;
1964 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001965 case -NFS4ERR_OLD_STATEID:
1966 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001968 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001969 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001971 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1972 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001974 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001975 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976}
1977
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001978static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001980 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001981 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001982 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001983
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001984 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001985 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001986
Trond Myklebust88069f72009-12-08 08:33:16 -05001987 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1988 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001989 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001990 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001991 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001992 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001993 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1994 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1995 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001996 }
1997 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001998 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1999 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2000 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002001 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002002 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002003 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002004
2005 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002006 /* Note: exit _without_ calling nfs4_close_done */
2007 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07002008 return;
2009 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002010
Fred Isamanf7e89172011-01-06 11:36:32 +00002011 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002012 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002013 if (calldata->roc &&
2014 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2015 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2016 task, NULL);
2017 return;
2018 }
2019 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002020
Trond Myklebust516a6af2005-10-27 22:12:41 -04002021 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002022 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002023 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Andy Adamson19ddab02009-04-01 09:22:20 -04002024 &calldata->arg.seq_args, &calldata->res.seq_res,
2025 1, task))
2026 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002027 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028}
2029
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002030static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002031 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002032 .rpc_call_done = nfs4_close_done,
2033 .rpc_release = nfs4_free_closedata,
2034};
2035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036/*
2037 * It is possible for data to be read/written from a mem-mapped file
2038 * after the sys_close call (which hits the vfs layer as a flush).
2039 * This means that we can't safely call nfsv4 close on a file until
2040 * the inode is cleared. This in turn means that we are not good
2041 * NFSv4 citizens - we do not indicate to the server to update the file's
2042 * share state even when we are done with one of the three share
2043 * stateid's in the inode.
2044 *
2045 * NOTE: Caller must be holding the sp->so_owner semaphore!
2046 */
Al Viro643168c2011-06-22 18:20:23 -04002047int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002049 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002051 struct nfs4_state_owner *sp = state->owner;
2052 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002053 struct rpc_message msg = {
2054 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2055 .rpc_cred = state->owner->so_cred,
2056 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002057 struct rpc_task_setup task_setup_data = {
2058 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002059 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002060 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002061 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002062 .flags = RPC_TASK_ASYNC,
2063 };
Trond Myklebust95121352005-10-18 14:20:12 -07002064 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002066 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002068 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002069 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002071 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002072 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002074 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002075 if (calldata->arg.seqid == NULL)
2076 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002077 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002078 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002079 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002080 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002081 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002082 calldata->roc = roc;
Al Viro643168c2011-06-22 18:20:23 -04002083 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002084
Trond Myklebust1174dd12010-12-21 10:52:24 -05002085 msg.rpc_argp = &calldata->arg;
2086 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002087 task_setup_data.callback_data = calldata;
2088 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002089 if (IS_ERR(task))
2090 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002091 status = 0;
2092 if (wait)
2093 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002094 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002095 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002096out_free_calldata:
2097 kfree(calldata);
2098out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002099 if (roc)
2100 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002101 nfs4_put_open_state(state);
2102 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002103 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104}
2105
Trond Myklebust2b484292010-09-17 10:56:51 -04002106static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002107nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 struct nfs4_state *state;
2110
Trond Myklebust565277f2007-10-15 18:17:53 -04002111 /* Protect against concurrent sillydeletes */
Al Viro3d4ff432011-06-22 18:40:12 -04002112 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002113 if (IS_ERR(state))
2114 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002115 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002116 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117}
2118
Trond Myklebust1185a552009-12-03 15:54:02 -05002119static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002120{
2121 if (ctx->state == NULL)
2122 return;
2123 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002124 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002125 else
Al Viro643168c2011-06-22 18:20:23 -04002126 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002127}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
2129static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2130{
Benny Halevy43652ad2009-04-01 09:21:54 -04002131 struct nfs4_server_caps_arg args = {
2132 .fhandle = fhandle,
2133 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 struct nfs4_server_caps_res res = {};
2135 struct rpc_message msg = {
2136 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002137 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 .rpc_resp = &res,
2139 };
2140 int status;
2141
Bryan Schumaker7c513052011-03-24 17:12:24 +00002142 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 if (status == 0) {
2144 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002145 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2146 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2147 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2148 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2149 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2151 server->caps |= NFS_CAP_ACLS;
2152 if (res.has_links != 0)
2153 server->caps |= NFS_CAP_HARDLINKS;
2154 if (res.has_symlinks != 0)
2155 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002156 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2157 server->caps |= NFS_CAP_FILEID;
2158 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2159 server->caps |= NFS_CAP_MODE;
2160 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2161 server->caps |= NFS_CAP_NLINK;
2162 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2163 server->caps |= NFS_CAP_OWNER;
2164 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2165 server->caps |= NFS_CAP_OWNER_GROUP;
2166 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2167 server->caps |= NFS_CAP_ATIME;
2168 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2169 server->caps |= NFS_CAP_CTIME;
2170 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2171 server->caps |= NFS_CAP_MTIME;
2172
Trond Myklebusta65318b2009-03-11 14:10:28 -04002173 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2174 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2175 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 server->acl_bitmask = res.acl_bitmask;
2177 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002178
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 return status;
2180}
2181
Trond Myklebust55a97592006-06-09 09:34:19 -04002182int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183{
2184 struct nfs4_exception exception = { };
2185 int err;
2186 do {
2187 err = nfs4_handle_exception(server,
2188 _nfs4_server_capabilities(server, fhandle),
2189 &exception);
2190 } while (exception.retry);
2191 return err;
2192}
2193
2194static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2195 struct nfs_fsinfo *info)
2196{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 struct nfs4_lookup_root_arg args = {
2198 .bitmask = nfs4_fattr_bitmap,
2199 };
2200 struct nfs4_lookup_res res = {
2201 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002202 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 .fh = fhandle,
2204 };
2205 struct rpc_message msg = {
2206 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2207 .rpc_argp = &args,
2208 .rpc_resp = &res,
2209 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002210
Trond Myklebust0e574af2005-10-27 22:12:38 -04002211 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002212 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213}
2214
2215static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2216 struct nfs_fsinfo *info)
2217{
2218 struct nfs4_exception exception = { };
2219 int err;
2220 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002221 err = _nfs4_lookup_root(server, fhandle, info);
2222 switch (err) {
2223 case 0:
2224 case -NFS4ERR_WRONGSEC:
2225 break;
2226 default:
2227 err = nfs4_handle_exception(server, err, &exception);
2228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 } while (exception.retry);
2230 return err;
2231}
2232
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002233static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2234 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2235{
2236 struct rpc_auth *auth;
2237 int ret;
2238
2239 auth = rpcauth_create(flavor, server->client);
2240 if (!auth) {
2241 ret = -EIO;
2242 goto out;
2243 }
2244 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002245out:
2246 return ret;
2247}
2248
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002249static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002250 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002252 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002253 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002255 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2256 flav_array[len] = RPC_AUTH_NULL;
2257 len += 1;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002258
2259 for (i = 0; i < len; i++) {
2260 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002261 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002262 continue;
2263 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002264 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002265 /*
2266 * -EACCESS could mean that the user doesn't have correct permissions
2267 * to access the mount. It could also mean that we tried to mount
2268 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2269 * existing mount programs don't handle -EACCES very well so it should
2270 * be mapped to -EPERM instead.
2271 */
2272 if (status == -EACCES)
2273 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002274 return status;
2275}
2276
2277/*
2278 * get the file handle for the "/" directory on the server
2279 */
2280static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2281 struct nfs_fsinfo *info)
2282{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002283 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002284 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002285 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2286 /*
2287 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2288 * by nfs4_map_errors() as this function exits.
2289 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002290 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 if (status == 0)
2292 status = nfs4_server_capabilities(server, fhandle);
2293 if (status == 0)
2294 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002295 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296}
2297
Andy Adamson533eb462011-06-13 18:25:56 -04002298static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002299/*
2300 * Get locations and (maybe) other attributes of a referral.
2301 * Note that we'll actually follow the referral later when
2302 * we detect fsid mismatch in inode revalidation
2303 */
Andy Adamson533eb462011-06-13 18:25:56 -04002304static int nfs4_get_referral(struct inode *dir, const struct qstr *name,
2305 struct nfs_fattr *fattr, struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002306{
2307 int status = -ENOMEM;
2308 struct page *page = NULL;
2309 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002310
2311 page = alloc_page(GFP_KERNEL);
2312 if (page == NULL)
2313 goto out;
2314 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2315 if (locations == NULL)
2316 goto out;
2317
Trond Myklebustc228fd32007-01-13 02:28:11 -05002318 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002319 if (status != 0)
2320 goto out;
2321 /* Make sure server returned a different fsid for the referral */
2322 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002323 dprintk("%s: server did not return a different fsid for"
2324 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002325 status = -EIO;
2326 goto out;
2327 }
Andy Adamson533eb462011-06-13 18:25:56 -04002328 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2329 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002330
Andy Adamson533eb462011-06-13 18:25:56 -04002331 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002332 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002333 memset(fhandle, 0, sizeof(struct nfs_fh));
2334out:
2335 if (page)
2336 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002337 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002338 return status;
2339}
2340
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2342{
2343 struct nfs4_getattr_arg args = {
2344 .fh = fhandle,
2345 .bitmask = server->attr_bitmask,
2346 };
2347 struct nfs4_getattr_res res = {
2348 .fattr = fattr,
2349 .server = server,
2350 };
2351 struct rpc_message msg = {
2352 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2353 .rpc_argp = &args,
2354 .rpc_resp = &res,
2355 };
2356
Trond Myklebust0e574af2005-10-27 22:12:38 -04002357 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002358 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359}
2360
2361static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2362{
2363 struct nfs4_exception exception = { };
2364 int err;
2365 do {
2366 err = nfs4_handle_exception(server,
2367 _nfs4_proc_getattr(server, fhandle, fattr),
2368 &exception);
2369 } while (exception.retry);
2370 return err;
2371}
2372
2373/*
2374 * The file is not closed if it is opened due to the a request to change
2375 * the size of the file. The open call will not be needed once the
2376 * VFS layer lookup-intents are implemented.
2377 *
2378 * Close is called when the inode is destroyed.
2379 * If we haven't opened the file for O_WRONLY, we
2380 * need to in the size_change case to obtain a stateid.
2381 *
2382 * Got race?
2383 * Because OPEN is always done by name in nfsv4, it is
2384 * possible that we opened a different file by the same
2385 * name. We can recognize this race condition, but we
2386 * can't do anything about it besides returning an error.
2387 *
2388 * This will be fixed with VFS changes (lookup-intent).
2389 */
2390static int
2391nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2392 struct iattr *sattr)
2393{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002394 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002395 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002396 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 int status;
2398
Benny Halevy8a1636c2010-07-14 15:43:57 -04002399 if (pnfs_ld_layoutret_on_setattr(inode))
2400 pnfs_return_layout(inode);
2401
Trond Myklebust0e574af2005-10-27 22:12:38 -04002402 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
Trond Myklebustd5308382005-11-04 15:33:38 -05002404 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002405 if (sattr->ia_valid & ATTR_FILE) {
2406 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002407
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002408 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002409 if (ctx) {
2410 cred = ctx->cred;
2411 state = ctx->state;
2412 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002413 }
2414
2415 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002416 if (status == 0)
2417 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 return status;
2419}
2420
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002421static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2422 const struct qstr *name, struct nfs_fh *fhandle,
2423 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002424{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002425 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002426 int status;
2427 struct nfs4_lookup_arg args = {
2428 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002429 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002430 .name = name,
2431 };
2432 struct nfs4_lookup_res res = {
2433 .server = server,
2434 .fattr = fattr,
2435 .fh = fhandle,
2436 };
2437 struct rpc_message msg = {
2438 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2439 .rpc_argp = &args,
2440 .rpc_resp = &res,
2441 };
2442
2443 nfs_fattr_init(fattr);
2444
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002446 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 dprintk("NFS reply lookup: %d\n", status);
2448 return status;
2449}
2450
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002451void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2452{
2453 memset(fh, 0, sizeof(struct nfs_fh));
2454 fattr->fsid.major = 1;
2455 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2456 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2457 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2458 fattr->nlink = 2;
2459}
2460
Bryan Schumaker7c513052011-03-24 17:12:24 +00002461static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2462 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463{
2464 struct nfs4_exception exception = { };
2465 int err;
2466 do {
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002467 int status;
2468
2469 status = _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr);
2470 switch (status) {
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002471 case -NFS4ERR_BADNAME:
2472 return -ENOENT;
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002473 case -NFS4ERR_MOVED:
Trond Myklebusta6f498a2011-11-07 15:50:32 -05002474 return nfs4_get_referral(dir, name, fattr, fhandle);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002475 case -NFS4ERR_WRONGSEC:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002476 nfs_fixup_secinfo_attributes(fattr, fhandle);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002477 }
2478 err = nfs4_handle_exception(NFS_SERVER(dir),
2479 status, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 } while (exception.retry);
2481 return err;
2482}
2483
2484static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2485{
Trond Myklebust76b32992007-08-10 17:45:11 -04002486 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 struct nfs4_accessargs args = {
2488 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002489 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002491 struct nfs4_accessres res = {
2492 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002493 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 struct rpc_message msg = {
2495 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2496 .rpc_argp = &args,
2497 .rpc_resp = &res,
2498 .rpc_cred = entry->cred,
2499 };
2500 int mode = entry->mask;
2501 int status;
2502
2503 /*
2504 * Determine which access bits we want to ask for...
2505 */
2506 if (mode & MAY_READ)
2507 args.access |= NFS4_ACCESS_READ;
2508 if (S_ISDIR(inode->i_mode)) {
2509 if (mode & MAY_WRITE)
2510 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2511 if (mode & MAY_EXEC)
2512 args.access |= NFS4_ACCESS_LOOKUP;
2513 } else {
2514 if (mode & MAY_WRITE)
2515 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2516 if (mode & MAY_EXEC)
2517 args.access |= NFS4_ACCESS_EXECUTE;
2518 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002519
2520 res.fattr = nfs_alloc_fattr();
2521 if (res.fattr == NULL)
2522 return -ENOMEM;
2523
Bryan Schumaker7c513052011-03-24 17:12:24 +00002524 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 if (!status) {
2526 entry->mask = 0;
2527 if (res.access & NFS4_ACCESS_READ)
2528 entry->mask |= MAY_READ;
2529 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2530 entry->mask |= MAY_WRITE;
2531 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2532 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002533 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002535 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 return status;
2537}
2538
2539static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2540{
2541 struct nfs4_exception exception = { };
2542 int err;
2543 do {
2544 err = nfs4_handle_exception(NFS_SERVER(inode),
2545 _nfs4_proc_access(inode, entry),
2546 &exception);
2547 } while (exception.retry);
2548 return err;
2549}
2550
2551/*
2552 * TODO: For the time being, we don't try to get any attributes
2553 * along with any of the zero-copy operations READ, READDIR,
2554 * READLINK, WRITE.
2555 *
2556 * In the case of the first three, we want to put the GETATTR
2557 * after the read-type operation -- this is because it is hard
2558 * to predict the length of a GETATTR response in v4, and thus
2559 * align the READ data correctly. This means that the GETATTR
2560 * may end up partially falling into the page cache, and we should
2561 * shift it into the 'tail' of the xdr_buf before processing.
2562 * To do this efficiently, we need to know the total length
2563 * of data received, which doesn't seem to be available outside
2564 * of the RPC layer.
2565 *
2566 * In the case of WRITE, we also want to put the GETATTR after
2567 * the operation -- in this case because we want to make sure
2568 * we get the post-operation mtime and size. This means that
2569 * we can't use xdr_encode_pages() as written: we need a variant
2570 * of it which would leave room in the 'tail' iovec.
2571 *
2572 * Both of these changes to the XDR layer would in fact be quite
2573 * minor, but I decided to leave them for a subsequent patch.
2574 */
2575static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2576 unsigned int pgbase, unsigned int pglen)
2577{
2578 struct nfs4_readlink args = {
2579 .fh = NFS_FH(inode),
2580 .pgbase = pgbase,
2581 .pglen = pglen,
2582 .pages = &page,
2583 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002584 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 struct rpc_message msg = {
2586 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2587 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002588 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 };
2590
Bryan Schumaker7c513052011-03-24 17:12:24 +00002591 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 -07002592}
2593
2594static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2595 unsigned int pgbase, unsigned int pglen)
2596{
2597 struct nfs4_exception exception = { };
2598 int err;
2599 do {
2600 err = nfs4_handle_exception(NFS_SERVER(inode),
2601 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2602 &exception);
2603 } while (exception.retry);
2604 return err;
2605}
2606
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607/*
2608 * Got race?
2609 * We will need to arrange for the VFS layer to provide an atomic open.
2610 * Until then, this create/open method is prone to inefficiency and race
2611 * conditions due to the lookup, create, and open VFS calls from sys_open()
2612 * placed on the wire.
2613 *
2614 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2615 * The file will be opened again in the subsequent VFS open call
2616 * (nfs4_proc_file_open).
2617 *
2618 * The open for read will just hang around to be used by any process that
2619 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2620 */
2621
2622static int
2623nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002624 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625{
Al Viro82a2c1b2011-06-22 18:30:55 -04002626 struct dentry *de = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002628 struct rpc_cred *cred = NULL;
2629 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 int status = 0;
2631
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002632 if (ctx != NULL) {
2633 cred = ctx->cred;
Al Viro3d4ff432011-06-22 18:40:12 -04002634 de = ctx->dentry;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002635 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002637 sattr->ia_mode &= ~current_umask();
Al Viro82a2c1b2011-06-22 18:30:55 -04002638 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002639 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 if (IS_ERR(state)) {
2641 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002642 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002644 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002645 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002646 if (ctx != NULL)
2647 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002648 else
Al Viro643168c2011-06-22 18:20:23 -04002649 nfs4_close_sync(state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650out:
2651 return status;
2652}
2653
2654static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2655{
Trond Myklebust16e42952005-10-27 22:12:44 -04002656 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002657 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002659 .name.len = name->len,
2660 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002661 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002663 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002664 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002665 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002667 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2668 .rpc_argp = &args,
2669 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002671 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
Trond Myklebustd3468902010-04-16 16:22:50 -04002673 res.dir_attr = nfs_alloc_fattr();
2674 if (res.dir_attr == NULL)
2675 goto out;
2676
Bryan Schumaker7c513052011-03-24 17:12:24 +00002677 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002678 if (status == 0) {
2679 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002680 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002681 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002682 nfs_free_fattr(res.dir_attr);
2683out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 return status;
2685}
2686
2687static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2688{
2689 struct nfs4_exception exception = { };
2690 int err;
2691 do {
2692 err = nfs4_handle_exception(NFS_SERVER(dir),
2693 _nfs4_proc_remove(dir, name),
2694 &exception);
2695 } while (exception.retry);
2696 return err;
2697}
2698
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002699static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002701 struct nfs_server *server = NFS_SERVER(dir);
2702 struct nfs_removeargs *args = msg->rpc_argp;
2703 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
Trond Myklebusta65318b2009-03-11 14:10:28 -04002705 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002706 res->server = server;
Benny Halevydfb4f3092010-09-24 09:17:01 -04002707 res->seq_res.sr_slot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709}
2710
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002711static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002713 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2714
Trond Myklebust14516c32010-07-31 14:29:06 -04002715 if (!nfs4_sequence_done(task, &res->seq_res))
2716 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002717 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002718 return 0;
2719 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002720 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002721 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722}
2723
Jeff Laytond3d41522010-09-17 17:31:57 -04002724static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2725{
2726 struct nfs_server *server = NFS_SERVER(dir);
2727 struct nfs_renameargs *arg = msg->rpc_argp;
2728 struct nfs_renameres *res = msg->rpc_resp;
2729
2730 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2731 arg->bitmask = server->attr_bitmask;
2732 res->server = server;
2733}
2734
2735static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2736 struct inode *new_dir)
2737{
2738 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2739
2740 if (!nfs4_sequence_done(task, &res->seq_res))
2741 return 0;
2742 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2743 return 0;
2744
2745 update_changeattr(old_dir, &res->old_cinfo);
2746 nfs_post_op_update_inode(old_dir, res->old_fattr);
2747 update_changeattr(new_dir, &res->new_cinfo);
2748 nfs_post_op_update_inode(new_dir, res->new_fattr);
2749 return 1;
2750}
2751
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2753 struct inode *new_dir, struct qstr *new_name)
2754{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002755 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002756 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 .old_dir = NFS_FH(old_dir),
2758 .new_dir = NFS_FH(new_dir),
2759 .old_name = old_name,
2760 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002761 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002763 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002764 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002765 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 struct rpc_message msg = {
2767 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2768 .rpc_argp = &arg,
2769 .rpc_resp = &res,
2770 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002771 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772
Trond Myklebust011fff72010-04-16 16:22:49 -04002773 res.old_fattr = nfs_alloc_fattr();
2774 res.new_fattr = nfs_alloc_fattr();
2775 if (res.old_fattr == NULL || res.new_fattr == NULL)
2776 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777
Bryan Schumaker7c513052011-03-24 17:12:24 +00002778 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 if (!status) {
2780 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002781 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002783 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002785out:
2786 nfs_free_fattr(res.new_fattr);
2787 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 return status;
2789}
2790
2791static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2792 struct inode *new_dir, struct qstr *new_name)
2793{
2794 struct nfs4_exception exception = { };
2795 int err;
2796 do {
2797 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2798 _nfs4_proc_rename(old_dir, old_name,
2799 new_dir, new_name),
2800 &exception);
2801 } while (exception.retry);
2802 return err;
2803}
2804
2805static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2806{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002807 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 struct nfs4_link_arg arg = {
2809 .fh = NFS_FH(inode),
2810 .dir_fh = NFS_FH(dir),
2811 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002812 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002814 struct nfs4_link_res res = {
2815 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002816 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 struct rpc_message msg = {
2818 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2819 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002820 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002822 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
Trond Myklebust136f2622010-04-16 16:22:49 -04002824 res.fattr = nfs_alloc_fattr();
2825 res.dir_attr = nfs_alloc_fattr();
2826 if (res.fattr == NULL || res.dir_attr == NULL)
2827 goto out;
2828
Bryan Schumaker7c513052011-03-24 17:12:24 +00002829 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002830 if (!status) {
2831 update_changeattr(dir, &res.cinfo);
2832 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002833 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002834 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002835out:
2836 nfs_free_fattr(res.dir_attr);
2837 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 return status;
2839}
2840
2841static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2842{
2843 struct nfs4_exception exception = { };
2844 int err;
2845 do {
2846 err = nfs4_handle_exception(NFS_SERVER(inode),
2847 _nfs4_proc_link(inode, dir, name),
2848 &exception);
2849 } while (exception.retry);
2850 return err;
2851}
2852
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002853struct nfs4_createdata {
2854 struct rpc_message msg;
2855 struct nfs4_create_arg arg;
2856 struct nfs4_create_res res;
2857 struct nfs_fh fh;
2858 struct nfs_fattr fattr;
2859 struct nfs_fattr dir_fattr;
2860};
2861
2862static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2863 struct qstr *name, struct iattr *sattr, u32 ftype)
2864{
2865 struct nfs4_createdata *data;
2866
2867 data = kzalloc(sizeof(*data), GFP_KERNEL);
2868 if (data != NULL) {
2869 struct nfs_server *server = NFS_SERVER(dir);
2870
2871 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2872 data->msg.rpc_argp = &data->arg;
2873 data->msg.rpc_resp = &data->res;
2874 data->arg.dir_fh = NFS_FH(dir);
2875 data->arg.server = server;
2876 data->arg.name = name;
2877 data->arg.attrs = sattr;
2878 data->arg.ftype = ftype;
2879 data->arg.bitmask = server->attr_bitmask;
2880 data->res.server = server;
2881 data->res.fh = &data->fh;
2882 data->res.fattr = &data->fattr;
2883 data->res.dir_fattr = &data->dir_fattr;
2884 nfs_fattr_init(data->res.fattr);
2885 nfs_fattr_init(data->res.dir_fattr);
2886 }
2887 return data;
2888}
2889
2890static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2891{
Bryan Schumaker7c513052011-03-24 17:12:24 +00002892 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00002893 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002894 if (status == 0) {
2895 update_changeattr(dir, &data->res.dir_cinfo);
2896 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2897 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2898 }
2899 return status;
2900}
2901
2902static void nfs4_free_createdata(struct nfs4_createdata *data)
2903{
2904 kfree(data);
2905}
2906
Chuck Lever4f390c12006-08-22 20:06:22 -04002907static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002908 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002910 struct nfs4_createdata *data;
2911 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912
Chuck Lever94a6d752006-08-22 20:06:23 -04002913 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002914 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002915
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002916 status = -ENOMEM;
2917 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2918 if (data == NULL)
2919 goto out;
2920
2921 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2922 data->arg.u.symlink.pages = &page;
2923 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002925 status = nfs4_do_create(dir, dentry, data);
2926
2927 nfs4_free_createdata(data);
2928out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 return status;
2930}
2931
Chuck Lever4f390c12006-08-22 20:06:22 -04002932static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002933 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934{
2935 struct nfs4_exception exception = { };
2936 int err;
2937 do {
2938 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002939 _nfs4_proc_symlink(dir, dentry, page,
2940 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 &exception);
2942 } while (exception.retry);
2943 return err;
2944}
2945
2946static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2947 struct iattr *sattr)
2948{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002949 struct nfs4_createdata *data;
2950 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002952 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2953 if (data == NULL)
2954 goto out;
2955
2956 status = nfs4_do_create(dir, dentry, data);
2957
2958 nfs4_free_createdata(data);
2959out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 return status;
2961}
2962
2963static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2964 struct iattr *sattr)
2965{
2966 struct nfs4_exception exception = { };
2967 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002968
2969 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 do {
2971 err = nfs4_handle_exception(NFS_SERVER(dir),
2972 _nfs4_proc_mkdir(dir, dentry, sattr),
2973 &exception);
2974 } while (exception.retry);
2975 return err;
2976}
2977
2978static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002979 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980{
2981 struct inode *dir = dentry->d_inode;
2982 struct nfs4_readdir_arg args = {
2983 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002984 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 .pgbase = 0,
2986 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002987 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04002988 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 };
2990 struct nfs4_readdir_res res;
2991 struct rpc_message msg = {
2992 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2993 .rpc_argp = &args,
2994 .rpc_resp = &res,
2995 .rpc_cred = cred,
2996 };
2997 int status;
2998
Harvey Harrison3110ff82008-05-02 13:42:44 -07002999 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003000 dentry->d_parent->d_name.name,
3001 dentry->d_name.name,
3002 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3004 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003005 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 -05003006 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003008 status += args.pgbase;
3009 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003010
3011 nfs_invalidate_atime(dir);
3012
Harvey Harrison3110ff82008-05-02 13:42:44 -07003013 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 return status;
3015}
3016
3017static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003018 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019{
3020 struct nfs4_exception exception = { };
3021 int err;
3022 do {
3023 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3024 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003025 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 &exception);
3027 } while (exception.retry);
3028 return err;
3029}
3030
3031static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3032 struct iattr *sattr, dev_t rdev)
3033{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003034 struct nfs4_createdata *data;
3035 int mode = sattr->ia_mode;
3036 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037
3038 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3039 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003040
3041 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3042 if (data == NULL)
3043 goto out;
3044
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003046 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003048 data->arg.ftype = NF4BLK;
3049 data->arg.u.device.specdata1 = MAJOR(rdev);
3050 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 }
3052 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003053 data->arg.ftype = NF4CHR;
3054 data->arg.u.device.specdata1 = MAJOR(rdev);
3055 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003058 status = nfs4_do_create(dir, dentry, data);
3059
3060 nfs4_free_createdata(data);
3061out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 return status;
3063}
3064
3065static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3066 struct iattr *sattr, dev_t rdev)
3067{
3068 struct nfs4_exception exception = { };
3069 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003070
3071 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 do {
3073 err = nfs4_handle_exception(NFS_SERVER(dir),
3074 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3075 &exception);
3076 } while (exception.retry);
3077 return err;
3078}
3079
3080static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3081 struct nfs_fsstat *fsstat)
3082{
3083 struct nfs4_statfs_arg args = {
3084 .fh = fhandle,
3085 .bitmask = server->attr_bitmask,
3086 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003087 struct nfs4_statfs_res res = {
3088 .fsstat = fsstat,
3089 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 struct rpc_message msg = {
3091 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3092 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003093 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 };
3095
Trond Myklebust0e574af2005-10-27 22:12:38 -04003096 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003097 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098}
3099
3100static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3101{
3102 struct nfs4_exception exception = { };
3103 int err;
3104 do {
3105 err = nfs4_handle_exception(server,
3106 _nfs4_proc_statfs(server, fhandle, fsstat),
3107 &exception);
3108 } while (exception.retry);
3109 return err;
3110}
3111
3112static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3113 struct nfs_fsinfo *fsinfo)
3114{
3115 struct nfs4_fsinfo_arg args = {
3116 .fh = fhandle,
3117 .bitmask = server->attr_bitmask,
3118 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003119 struct nfs4_fsinfo_res res = {
3120 .fsinfo = fsinfo,
3121 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 struct rpc_message msg = {
3123 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3124 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003125 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 };
3127
Bryan Schumaker7c513052011-03-24 17:12:24 +00003128 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129}
3130
3131static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3132{
3133 struct nfs4_exception exception = { };
3134 int err;
3135
3136 do {
3137 err = nfs4_handle_exception(server,
3138 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3139 &exception);
3140 } while (exception.retry);
3141 return err;
3142}
3143
3144static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3145{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003146 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3148}
3149
3150static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3151 struct nfs_pathconf *pathconf)
3152{
3153 struct nfs4_pathconf_arg args = {
3154 .fh = fhandle,
3155 .bitmask = server->attr_bitmask,
3156 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003157 struct nfs4_pathconf_res res = {
3158 .pathconf = pathconf,
3159 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 struct rpc_message msg = {
3161 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3162 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003163 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 };
3165
3166 /* None of the pathconf attributes are mandatory to implement */
3167 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3168 memset(pathconf, 0, sizeof(*pathconf));
3169 return 0;
3170 }
3171
Trond Myklebust0e574af2005-10-27 22:12:38 -04003172 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003173 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174}
3175
3176static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3177 struct nfs_pathconf *pathconf)
3178{
3179 struct nfs4_exception exception = { };
3180 int err;
3181
3182 do {
3183 err = nfs4_handle_exception(server,
3184 _nfs4_proc_pathconf(server, fhandle, pathconf),
3185 &exception);
3186 } while (exception.retry);
3187 return err;
3188}
3189
Benny Halevyd20581a2011-05-22 19:52:03 +03003190void __nfs4_read_done_cb(struct nfs_read_data *data)
3191{
3192 nfs_invalidate_atime(data->inode);
3193}
3194
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003195static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196{
Trond Myklebustec06c092006-03-20 13:44:27 -05003197 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003199 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003200 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003201 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003203
Benny Halevyd20581a2011-05-22 19:52:03 +03003204 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003206 renew_lease(server, data->timestamp);
3207 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208}
3209
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003210static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3211{
3212
3213 dprintk("--> %s\n", __func__);
3214
3215 if (!nfs4_sequence_done(task, &data->res.seq_res))
3216 return -EAGAIN;
3217
Benny Halevyd20581a2011-05-22 19:52:03 +03003218 return data->read_done_cb ? data->read_done_cb(task, data) :
3219 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003220}
3221
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003222static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003225 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003226 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227}
3228
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003229/* Reset the the nfs_read_data to send the read to the MDS. */
3230void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3231{
3232 dprintk("%s Reset task for i/o through\n", __func__);
3233 put_lseg(data->lseg);
3234 data->lseg = NULL;
3235 /* offsets will differ in the dense stripe case */
3236 data->args.offset = data->mds_offset;
3237 data->ds_clp = NULL;
3238 data->args.fh = NFS_FH(data->inode);
3239 data->read_done_cb = nfs4_read_done_cb;
3240 task->tk_ops = data->mds_ops;
3241 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3242}
3243EXPORT_SYMBOL_GPL(nfs4_reset_read);
3244
Fred Isamanb029bc92011-03-03 15:13:42 +00003245static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 struct inode *inode = data->inode;
3248
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003249 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003250 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003251 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003253 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003255 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003256 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003257 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258}
3259
Fred Isamanb029bc92011-03-03 15:13:42 +00003260static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3261{
3262 if (!nfs4_sequence_done(task, &data->res.seq_res))
3263 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003264 return data->write_done_cb ? data->write_done_cb(task, data) :
3265 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003266}
3267
Fred Isamana69aef12011-03-03 15:13:47 +00003268/* Reset the the nfs_write_data to send the write to the MDS. */
3269void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3270{
3271 dprintk("%s Reset task for i/o through\n", __func__);
3272 put_lseg(data->lseg);
3273 data->lseg = NULL;
3274 data->ds_clp = NULL;
3275 data->write_done_cb = nfs4_write_done_cb;
3276 data->args.fh = NFS_FH(data->inode);
3277 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3278 data->args.offset = data->mds_offset;
3279 data->res.fattr = &data->fattr;
3280 task->tk_ops = data->mds_ops;
3281 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3282}
3283EXPORT_SYMBOL_GPL(nfs4_reset_write);
3284
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003285static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003287 struct nfs_server *server = NFS_SERVER(data->inode);
3288
Fred Isaman7ffd1062011-03-03 15:13:46 +00003289 if (data->lseg) {
3290 data->args.bitmask = NULL;
3291 data->res.fattr = NULL;
3292 } else
3293 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isamanb029bc92011-03-03 15:13:42 +00003294 if (!data->write_done_cb)
3295 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003296 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 data->timestamp = jiffies;
3298
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003299 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300}
3301
Fred Isaman5f452432011-03-23 13:27:46 +00003302static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003305
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003306 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003307 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003308 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003310 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003311 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312}
3313
Fred Isaman5f452432011-03-23 13:27:46 +00003314static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3315{
3316 if (!nfs4_sequence_done(task, &data->res.seq_res))
3317 return -EAGAIN;
3318 return data->write_done_cb(task, data);
3319}
3320
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003321static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003323 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003324
3325 if (data->lseg) {
3326 data->args.bitmask = NULL;
3327 data->res.fattr = NULL;
3328 } else
3329 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isaman5f452432011-03-23 13:27:46 +00003330 if (!data->write_done_cb)
3331 data->write_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003332 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003333 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334}
3335
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003336struct nfs4_renewdata {
3337 struct nfs_client *client;
3338 unsigned long timestamp;
3339};
3340
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341/*
3342 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3343 * standalone procedure for queueing an asynchronous RENEW.
3344 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003345static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003346{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003347 struct nfs4_renewdata *data = calldata;
3348 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003349
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003350 if (atomic_read(&clp->cl_count) > 1)
3351 nfs4_schedule_state_renewal(clp);
3352 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003353 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003354}
3355
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003356static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003358 struct nfs4_renewdata *data = calldata;
3359 struct nfs_client *clp = data->client;
3360 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361
3362 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003363 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003364 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3365 return;
3366 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003367 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003368 return;
3369 }
3370 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003372 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373}
3374
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003375static const struct rpc_call_ops nfs4_renew_ops = {
3376 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003377 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003378};
3379
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003380static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381{
3382 struct rpc_message msg = {
3383 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3384 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003385 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003387 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003389 if (renew_flags == 0)
3390 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003391 if (!atomic_inc_not_zero(&clp->cl_count))
3392 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003393 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003394 if (data == NULL)
3395 return -ENOMEM;
3396 data->client = clp;
3397 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003399 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400}
3401
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003402static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403{
3404 struct rpc_message msg = {
3405 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3406 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003407 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 };
3409 unsigned long now = jiffies;
3410 int status;
3411
3412 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3413 if (status < 0)
3414 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003415 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 return 0;
3417}
3418
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003419static inline int nfs4_server_supports_acls(struct nfs_server *server)
3420{
3421 return (server->caps & NFS_CAP_ACLS)
3422 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3423 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3424}
3425
3426/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3427 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3428 * the stack.
3429 */
3430#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3431
3432static void buf_to_pages(const void *buf, size_t buflen,
3433 struct page **pages, unsigned int *pgbase)
3434{
3435 const void *p = buf;
3436
3437 *pgbase = offset_in_page(buf);
3438 p -= *pgbase;
3439 while (p < buf + buflen) {
3440 *(pages++) = virt_to_page(p);
3441 p += PAGE_CACHE_SIZE;
3442 }
3443}
3444
Neil Hormane9e3d722011-03-04 19:26:03 -05003445static int buf_to_pages_noslab(const void *buf, size_t buflen,
3446 struct page **pages, unsigned int *pgbase)
3447{
3448 struct page *newpage, **spages;
3449 int rc = 0;
3450 size_t len;
3451 spages = pages;
3452
3453 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003454 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003455 newpage = alloc_page(GFP_KERNEL);
3456
3457 if (newpage == NULL)
3458 goto unwind;
3459 memcpy(page_address(newpage), buf, len);
3460 buf += len;
3461 buflen -= len;
3462 *pages++ = newpage;
3463 rc++;
3464 } while (buflen != 0);
3465
3466 return rc;
3467
3468unwind:
3469 for(; rc > 0; rc--)
3470 __free_page(spages[rc-1]);
3471 return -ENOMEM;
3472}
3473
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003474struct nfs4_cached_acl {
3475 int cached;
3476 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003477 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003478};
3479
3480static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003481{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003482 struct nfs_inode *nfsi = NFS_I(inode);
3483
3484 spin_lock(&inode->i_lock);
3485 kfree(nfsi->nfs4_acl);
3486 nfsi->nfs4_acl = acl;
3487 spin_unlock(&inode->i_lock);
3488}
3489
3490static void nfs4_zap_acl_attr(struct inode *inode)
3491{
3492 nfs4_set_cached_acl(inode, NULL);
3493}
3494
3495static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3496{
3497 struct nfs_inode *nfsi = NFS_I(inode);
3498 struct nfs4_cached_acl *acl;
3499 int ret = -ENOENT;
3500
3501 spin_lock(&inode->i_lock);
3502 acl = nfsi->nfs4_acl;
3503 if (acl == NULL)
3504 goto out;
3505 if (buf == NULL) /* user is just asking for length */
3506 goto out_len;
3507 if (acl->cached == 0)
3508 goto out;
3509 ret = -ERANGE; /* see getxattr(2) man page */
3510 if (acl->len > buflen)
3511 goto out;
3512 memcpy(buf, acl->data, acl->len);
3513out_len:
3514 ret = acl->len;
3515out:
3516 spin_unlock(&inode->i_lock);
3517 return ret;
3518}
3519
3520static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3521{
3522 struct nfs4_cached_acl *acl;
3523
3524 if (buf && acl_len <= PAGE_SIZE) {
3525 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3526 if (acl == NULL)
3527 goto out;
3528 acl->cached = 1;
3529 memcpy(acl->data, buf, acl_len);
3530 } else {
3531 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3532 if (acl == NULL)
3533 goto out;
3534 acl->cached = 0;
3535 }
3536 acl->len = acl_len;
3537out:
3538 nfs4_set_cached_acl(inode, acl);
3539}
3540
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003541static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003542{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003543 struct page *pages[NFS4ACL_MAXPAGES];
3544 struct nfs_getaclargs args = {
3545 .fh = NFS_FH(inode),
3546 .acl_pages = pages,
3547 .acl_len = buflen,
3548 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003549 struct nfs_getaclres res = {
3550 .acl_len = buflen,
3551 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003552 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003553 struct rpc_message msg = {
3554 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3555 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003556 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003557 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003558 struct page *localpage = NULL;
3559 int ret;
3560
3561 if (buflen < PAGE_SIZE) {
3562 /* As long as we're doing a round trip to the server anyway,
3563 * let's be prepared for a page of acl data. */
3564 localpage = alloc_page(GFP_KERNEL);
3565 resp_buf = page_address(localpage);
3566 if (localpage == NULL)
3567 return -ENOMEM;
3568 args.acl_pages[0] = localpage;
3569 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003570 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003571 } else {
3572 resp_buf = buf;
3573 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3574 }
Bryan Schumaker7c513052011-03-24 17:12:24 +00003575 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 +00003576 if (ret)
3577 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003578 if (res.acl_len > args.acl_len)
3579 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003580 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003581 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003582 if (buf) {
3583 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003584 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003585 goto out_free;
3586 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003587 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003588 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003589 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003590out_free:
3591 if (localpage)
3592 __free_page(localpage);
3593 return ret;
3594}
3595
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003596static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3597{
3598 struct nfs4_exception exception = { };
3599 ssize_t ret;
3600 do {
3601 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3602 if (ret >= 0)
3603 break;
3604 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3605 } while (exception.retry);
3606 return ret;
3607}
3608
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003609static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3610{
3611 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003612 int ret;
3613
3614 if (!nfs4_server_supports_acls(server))
3615 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003616 ret = nfs_revalidate_inode(server, inode);
3617 if (ret < 0)
3618 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003619 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3620 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003621 ret = nfs4_read_cached_acl(inode, buf, buflen);
3622 if (ret != -ENOENT)
3623 return ret;
3624 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003625}
3626
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003627static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003628{
3629 struct nfs_server *server = NFS_SERVER(inode);
3630 struct page *pages[NFS4ACL_MAXPAGES];
3631 struct nfs_setaclargs arg = {
3632 .fh = NFS_FH(inode),
3633 .acl_pages = pages,
3634 .acl_len = buflen,
3635 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003636 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003637 struct rpc_message msg = {
3638 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3639 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003640 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003641 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003642 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003643
3644 if (!nfs4_server_supports_acls(server))
3645 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003646 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3647 if (i < 0)
3648 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003649 nfs_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003650 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003651
3652 /*
3653 * Free each page after tx, so the only ref left is
3654 * held by the network stack
3655 */
3656 for (; i > 0; i--)
3657 put_page(pages[i-1]);
3658
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003659 /*
3660 * Acl update can result in inode attribute update.
3661 * so mark the attribute cache invalid.
3662 */
3663 spin_lock(&inode->i_lock);
3664 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3665 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003666 nfs_access_zap_cache(inode);
3667 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003668 return ret;
3669}
3670
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003671static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3672{
3673 struct nfs4_exception exception = { };
3674 int err;
3675 do {
3676 err = nfs4_handle_exception(NFS_SERVER(inode),
3677 __nfs4_proc_set_acl(inode, buf, buflen),
3678 &exception);
3679 } while (exception.retry);
3680 return err;
3681}
3682
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003684nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003686 struct nfs_client *clp = server->nfs_client;
3687
3688 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 return 0;
3690 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003691 case -NFS4ERR_ADMIN_REVOKED:
3692 case -NFS4ERR_BAD_STATEID:
3693 case -NFS4ERR_OPENMODE:
3694 if (state == NULL)
3695 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003696 nfs4_schedule_stateid_recovery(server, state);
3697 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003698 case -NFS4ERR_EXPIRED:
3699 if (state != NULL)
3700 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003702 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003703 nfs4_schedule_lease_recovery(clp);
3704 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003705#if defined(CONFIG_NFS_V4_1)
3706 case -NFS4ERR_BADSESSION:
3707 case -NFS4ERR_BADSLOT:
3708 case -NFS4ERR_BAD_HIGH_SLOT:
3709 case -NFS4ERR_DEADSESSION:
3710 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3711 case -NFS4ERR_SEQ_FALSE_RETRY:
3712 case -NFS4ERR_SEQ_MISORDERED:
3713 dprintk("%s ERROR %d, Reset session\n", __func__,
3714 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003715 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -04003716 task->tk_status = 0;
3717 return -EAGAIN;
3718#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003720 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003721 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003722 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3724 task->tk_status = 0;
3725 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003726 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 case -NFS4ERR_OLD_STATEID:
3728 task->tk_status = 0;
3729 return -EAGAIN;
3730 }
3731 task->tk_status = nfs4_map_errors(task->tk_status);
3732 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003733wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003734 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003735 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3736 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3737 task->tk_status = 0;
3738 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739}
3740
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003741int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3742 unsigned short port, struct rpc_cred *cred,
3743 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744{
3745 nfs4_verifier sc_verifier;
3746 struct nfs4_setclientid setclientid = {
3747 .sc_verifier = &sc_verifier,
3748 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003749 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 };
3751 struct rpc_message msg = {
3752 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3753 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003754 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003755 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 };
Al Virobc4785c2006-10-19 23:28:51 -07003757 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758 int loop = 0;
3759 int status;
3760
Al Virobc4785c2006-10-19 23:28:51 -07003761 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3763 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3764
3765 for(;;) {
3766 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003767 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003768 clp->cl_ipaddr,
3769 rpc_peeraddr2str(clp->cl_rpcclient,
3770 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003771 rpc_peeraddr2str(clp->cl_rpcclient,
3772 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003773 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 clp->cl_id_uniquifier);
3775 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003776 sizeof(setclientid.sc_netid),
3777 rpc_peeraddr2str(clp->cl_rpcclient,
3778 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003780 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 clp->cl_ipaddr, port >> 8, port & 255);
3782
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003783 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 if (status != -NFS4ERR_CLID_INUSE)
3785 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003786 if (loop != 0) {
3787 ++clp->cl_id_uniquifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003789 }
3790 ++loop;
3791 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 }
3793 return status;
3794}
3795
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003796int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003797 struct nfs4_setclientid_res *arg,
3798 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799{
3800 struct nfs_fsinfo fsinfo;
3801 struct rpc_message msg = {
3802 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003803 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003805 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 };
3807 unsigned long now;
3808 int status;
3809
3810 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003811 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 if (status == 0) {
3813 spin_lock(&clp->cl_lock);
3814 clp->cl_lease_time = fsinfo.lease_time * HZ;
3815 clp->cl_last_renewal = now;
3816 spin_unlock(&clp->cl_lock);
3817 }
3818 return status;
3819}
3820
Trond Myklebustfe650402006-01-03 09:55:18 +01003821struct nfs4_delegreturndata {
3822 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003823 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003824 struct nfs_fh fh;
3825 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003826 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003827 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003828 int rpc_status;
3829};
3830
Trond Myklebustfe650402006-01-03 09:55:18 +01003831static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3832{
3833 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003834
Trond Myklebust14516c32010-07-31 14:29:06 -04003835 if (!nfs4_sequence_done(task, &data->res.seq_res))
3836 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003837
Ricardo Labiaga79708862009-12-07 09:23:21 -05003838 switch (task->tk_status) {
3839 case -NFS4ERR_STALE_STATEID:
3840 case -NFS4ERR_EXPIRED:
3841 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003842 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003843 break;
3844 default:
3845 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3846 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003847 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003848 return;
3849 }
3850 }
3851 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003852}
3853
3854static void nfs4_delegreturn_release(void *calldata)
3855{
Trond Myklebustfe650402006-01-03 09:55:18 +01003856 kfree(calldata);
3857}
3858
Andy Adamson938e1012009-04-01 09:22:28 -04003859#if defined(CONFIG_NFS_V4_1)
3860static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3861{
3862 struct nfs4_delegreturndata *d_data;
3863
3864 d_data = (struct nfs4_delegreturndata *)data;
3865
Trond Myklebust035168a2010-06-16 09:52:26 -04003866 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003867 &d_data->args.seq_args,
3868 &d_data->res.seq_res, 1, task))
3869 return;
3870 rpc_call_start(task);
3871}
3872#endif /* CONFIG_NFS_V4_1 */
3873
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003874static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003875#if defined(CONFIG_NFS_V4_1)
3876 .rpc_call_prepare = nfs4_delegreturn_prepare,
3877#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003878 .rpc_call_done = nfs4_delegreturn_done,
3879 .rpc_release = nfs4_delegreturn_release,
3880};
3881
Trond Myklebuste6f81072008-01-24 18:14:34 -05003882static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003883{
3884 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003885 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003886 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003887 struct rpc_message msg = {
3888 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3889 .rpc_cred = cred,
3890 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003891 struct rpc_task_setup task_setup_data = {
3892 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003893 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003894 .callback_ops = &nfs4_delegreturn_ops,
3895 .flags = RPC_TASK_ASYNC,
3896 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003897 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003898
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003899 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003900 if (data == NULL)
3901 return -ENOMEM;
3902 data->args.fhandle = &data->fh;
3903 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003904 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003905 nfs_copy_fh(&data->fh, NFS_FH(inode));
3906 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003907 data->res.fattr = &data->fattr;
3908 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003909 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003910 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003911 data->rpc_status = 0;
3912
Trond Myklebustc970aa82007-07-14 15:39:59 -04003913 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05003914 msg.rpc_argp = &data->args;
3915 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003916 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003917 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003918 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003919 if (!issync)
3920 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003921 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003922 if (status != 0)
3923 goto out;
3924 status = data->rpc_status;
3925 if (status != 0)
3926 goto out;
3927 nfs_refresh_inode(inode, &data->fattr);
3928out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003929 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003930 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931}
3932
Trond Myklebuste6f81072008-01-24 18:14:34 -05003933int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934{
3935 struct nfs_server *server = NFS_SERVER(inode);
3936 struct nfs4_exception exception = { };
3937 int err;
3938 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003939 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 switch (err) {
3941 case -NFS4ERR_STALE_STATEID:
3942 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 case 0:
3944 return 0;
3945 }
3946 err = nfs4_handle_exception(server, err, &exception);
3947 } while (exception.retry);
3948 return err;
3949}
3950
3951#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3952#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3953
3954/*
3955 * sleep, with exponential backoff, and retry the LOCK operation.
3956 */
3957static unsigned long
3958nfs4_set_lock_task_retry(unsigned long timeout)
3959{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003960 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961 timeout <<= 1;
3962 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3963 return NFS4_LOCK_MAXTIMEOUT;
3964 return timeout;
3965}
3966
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3968{
3969 struct inode *inode = state->inode;
3970 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003971 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003972 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003974 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003976 struct nfs_lockt_res res = {
3977 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 };
3979 struct rpc_message msg = {
3980 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3981 .rpc_argp = &arg,
3982 .rpc_resp = &res,
3983 .rpc_cred = state->owner->so_cred,
3984 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 struct nfs4_lock_state *lsp;
3986 int status;
3987
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003988 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003989 status = nfs4_set_lock_state(state, request);
3990 if (status != 0)
3991 goto out;
3992 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003993 arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05003994 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003995 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003996 switch (status) {
3997 case 0:
3998 request->fl_type = F_UNLCK;
3999 break;
4000 case -NFS4ERR_DENIED:
4001 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004003 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004004out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005 return status;
4006}
4007
4008static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4009{
4010 struct nfs4_exception exception = { };
4011 int err;
4012
4013 do {
4014 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4015 _nfs4_proc_getlk(state, cmd, request),
4016 &exception);
4017 } while (exception.retry);
4018 return err;
4019}
4020
4021static int do_vfs_lock(struct file *file, struct file_lock *fl)
4022{
4023 int res = 0;
4024 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4025 case FL_POSIX:
4026 res = posix_lock_file_wait(file, fl);
4027 break;
4028 case FL_FLOCK:
4029 res = flock_lock_file_wait(file, fl);
4030 break;
4031 default:
4032 BUG();
4033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 return res;
4035}
4036
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004037struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004038 struct nfs_locku_args arg;
4039 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004040 struct nfs4_lock_state *lsp;
4041 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004042 struct file_lock fl;
4043 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004044 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004045};
4046
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004047static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4048 struct nfs_open_context *ctx,
4049 struct nfs4_lock_state *lsp,
4050 struct nfs_seqid *seqid)
4051{
4052 struct nfs4_unlockdata *p;
4053 struct inode *inode = lsp->ls_state->inode;
4054
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004055 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004056 if (p == NULL)
4057 return NULL;
4058 p->arg.fh = NFS_FH(inode);
4059 p->arg.fl = &p->fl;
4060 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004061 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004062 p->arg.stateid = &lsp->ls_stateid;
4063 p->lsp = lsp;
4064 atomic_inc(&lsp->ls_count);
4065 /* Ensure we don't close file until we're done freeing locks! */
4066 p->ctx = get_nfs_open_context(ctx);
4067 memcpy(&p->fl, fl, sizeof(p->fl));
4068 p->server = NFS_SERVER(inode);
4069 return p;
4070}
4071
Trond Myklebust06f814a2006-01-03 09:55:07 +01004072static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004073{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004074 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004075 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004076 nfs4_put_lock_state(calldata->lsp);
4077 put_nfs_open_context(calldata->ctx);
4078 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004079}
4080
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004081static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004082{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004083 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004084
Trond Myklebust14516c32010-07-31 14:29:06 -04004085 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4086 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004087 switch (task->tk_status) {
4088 case 0:
4089 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004090 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004091 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01004092 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004093 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004094 case -NFS4ERR_BAD_STATEID:
4095 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004096 case -NFS4ERR_STALE_STATEID:
4097 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004098 break;
4099 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004100 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004101 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004102 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004103}
4104
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004105static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004106{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004107 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004109 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004110 return;
4111 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004112 /* Note: exit _without_ running nfs4_locku_done */
4113 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004114 return;
4115 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004116 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004117 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004118 &calldata->arg.seq_args,
4119 &calldata->res.seq_res, 1, task))
4120 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004121 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122}
4123
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004124static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004125 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004126 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004127 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004128};
4129
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004130static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4131 struct nfs_open_context *ctx,
4132 struct nfs4_lock_state *lsp,
4133 struct nfs_seqid *seqid)
4134{
4135 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004136 struct rpc_message msg = {
4137 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4138 .rpc_cred = ctx->cred,
4139 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004140 struct rpc_task_setup task_setup_data = {
4141 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004142 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004143 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004144 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004145 .flags = RPC_TASK_ASYNC,
4146 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004147
Frank Filz137d6ac2007-07-09 15:32:29 -07004148 /* Ensure this is an unlock - when canceling a lock, the
4149 * canceled lock is passed in, and it won't be an unlock.
4150 */
4151 fl->fl_type = F_UNLCK;
4152
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004153 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4154 if (data == NULL) {
4155 nfs_free_seqid(seqid);
4156 return ERR_PTR(-ENOMEM);
4157 }
4158
Trond Myklebust1174dd12010-12-21 10:52:24 -05004159 msg.rpc_argp = &data->arg;
4160 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004161 task_setup_data.callback_data = data;
4162 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004163}
4164
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4166{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004167 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004168 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004169 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004170 struct rpc_task *task;
4171 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004172 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173
Trond Myklebust9b073572006-06-29 16:38:34 -04004174 status = nfs4_set_lock_state(state, request);
4175 /* Unlock _before_ we do the RPC call */
4176 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004177 down_read(&nfsi->rwsem);
4178 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4179 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004180 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004181 }
4182 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004183 if (status != 0)
4184 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004185 /* Is this a delegated lock? */
4186 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004187 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004188 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004189 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004190 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004191 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004192 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004193 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004194 status = PTR_ERR(task);
4195 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004196 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004197 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004198 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004199out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004200 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004201 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202}
4203
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004204struct nfs4_lockdata {
4205 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004206 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004207 struct nfs4_lock_state *lsp;
4208 struct nfs_open_context *ctx;
4209 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004210 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004211 int rpc_status;
4212 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004213 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004214};
4215
4216static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004217 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4218 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004219{
4220 struct nfs4_lockdata *p;
4221 struct inode *inode = lsp->ls_state->inode;
4222 struct nfs_server *server = NFS_SERVER(inode);
4223
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004224 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004225 if (p == NULL)
4226 return NULL;
4227
4228 p->arg.fh = NFS_FH(inode);
4229 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004230 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004231 if (p->arg.open_seqid == NULL)
4232 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004233 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004234 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004235 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004236 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004237 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004238 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004239 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004240 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004241 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004242 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004243 atomic_inc(&lsp->ls_count);
4244 p->ctx = get_nfs_open_context(ctx);
4245 memcpy(&p->fl, fl, sizeof(p->fl));
4246 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004247out_free_seqid:
4248 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004249out_free:
4250 kfree(p);
4251 return NULL;
4252}
4253
4254static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4255{
4256 struct nfs4_lockdata *data = calldata;
4257 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258
Harvey Harrison3110ff82008-05-02 13:42:44 -07004259 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004260 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4261 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004262 /* Do we need to do an open_to_lock_owner? */
4263 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004264 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4265 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004266 data->arg.open_stateid = &state->stateid;
4267 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004268 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004269 } else
4270 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004271 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004272 if (nfs4_setup_sequence(data->server,
4273 &data->arg.seq_args,
Andy Adamson66179ef2009-04-01 09:22:22 -04004274 &data->res.seq_res, 1, task))
4275 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004276 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004277 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004278}
4279
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004280static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4281{
4282 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4283 nfs4_lock_prepare(task, calldata);
4284}
4285
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004286static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4287{
4288 struct nfs4_lockdata *data = calldata;
4289
Harvey Harrison3110ff82008-05-02 13:42:44 -07004290 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004291
Trond Myklebust14516c32010-07-31 14:29:06 -04004292 if (!nfs4_sequence_done(task, &data->res.seq_res))
4293 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004294
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004295 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004296 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004297 if (data->rpc_status == 0)
4298 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4299 else
4300 goto out;
4301 }
4302 if (data->rpc_status == 0) {
4303 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4304 sizeof(data->lsp->ls_stateid.data));
4305 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Al Viro3d4ff432011-06-22 18:40:12 -04004306 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004307 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004308out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004309 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004310}
4311
4312static void nfs4_lock_release(void *calldata)
4313{
4314 struct nfs4_lockdata *data = calldata;
4315
Harvey Harrison3110ff82008-05-02 13:42:44 -07004316 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004317 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004318 if (data->cancelled != 0) {
4319 struct rpc_task *task;
4320 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4321 data->arg.lock_seqid);
4322 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004323 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004324 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004325 } else
4326 nfs_free_seqid(data->arg.lock_seqid);
4327 nfs4_put_lock_state(data->lsp);
4328 put_nfs_open_context(data->ctx);
4329 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004330 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004331}
4332
4333static const struct rpc_call_ops nfs4_lock_ops = {
4334 .rpc_call_prepare = nfs4_lock_prepare,
4335 .rpc_call_done = nfs4_lock_done,
4336 .rpc_release = nfs4_lock_release,
4337};
4338
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004339static const struct rpc_call_ops nfs4_recover_lock_ops = {
4340 .rpc_call_prepare = nfs4_recover_lock_prepare,
4341 .rpc_call_done = nfs4_lock_done,
4342 .rpc_release = nfs4_lock_release,
4343};
4344
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004345static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4346{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004347 switch (error) {
4348 case -NFS4ERR_ADMIN_REVOKED:
4349 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004350 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004351 if (new_lock_owner != 0 ||
4352 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004353 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004354 break;
4355 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004356 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004357 case -NFS4ERR_EXPIRED:
4358 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004359 };
4360}
4361
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004362static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004363{
4364 struct nfs4_lockdata *data;
4365 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004366 struct rpc_message msg = {
4367 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4368 .rpc_cred = state->owner->so_cred,
4369 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004370 struct rpc_task_setup task_setup_data = {
4371 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004372 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004373 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004374 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004375 .flags = RPC_TASK_ASYNC,
4376 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004377 int ret;
4378
Harvey Harrison3110ff82008-05-02 13:42:44 -07004379 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004380 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004381 fl->fl_u.nfs4_fl.owner,
4382 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004383 if (data == NULL)
4384 return -ENOMEM;
4385 if (IS_SETLKW(cmd))
4386 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004387 if (recovery_type > NFS_LOCK_NEW) {
4388 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004389 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004390 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4391 }
Trond Myklebust1174dd12010-12-21 10:52:24 -05004392 msg.rpc_argp = &data->arg;
4393 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004394 task_setup_data.callback_data = data;
4395 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004396 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004397 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004398 ret = nfs4_wait_for_completion_rpc_task(task);
4399 if (ret == 0) {
4400 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004401 if (ret)
4402 nfs4_handle_setlk_error(data->server, data->lsp,
4403 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004404 } else
4405 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004406 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004407 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004408 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409}
4410
4411static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4412{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004413 struct nfs_server *server = NFS_SERVER(state->inode);
4414 struct nfs4_exception exception = { };
4415 int err;
4416
4417 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004418 /* Cache the lock if possible... */
4419 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4420 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004421 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004422 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004423 break;
4424 nfs4_handle_exception(server, err, &exception);
4425 } while (exception.retry);
4426 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427}
4428
4429static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4430{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004431 struct nfs_server *server = NFS_SERVER(state->inode);
4432 struct nfs4_exception exception = { };
4433 int err;
4434
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004435 err = nfs4_set_lock_state(state, request);
4436 if (err != 0)
4437 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004438 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004439 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4440 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004441 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004442 switch (err) {
4443 default:
4444 goto out;
4445 case -NFS4ERR_GRACE:
4446 case -NFS4ERR_DELAY:
4447 nfs4_handle_exception(server, err, &exception);
4448 err = 0;
4449 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004450 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004451out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004452 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453}
4454
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004455#if defined(CONFIG_NFS_V4_1)
4456static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4457{
4458 int status;
4459 struct nfs_server *server = NFS_SERVER(state->inode);
4460
4461 status = nfs41_test_stateid(server, state);
4462 if (status == NFS_OK)
4463 return 0;
4464 nfs41_free_stateid(server, state);
4465 return nfs4_lock_expired(state, request);
4466}
4467#endif
4468
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4470{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004471 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004472 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004473 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004475 if ((fl_flags & FL_POSIX) &&
4476 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4477 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004478 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004479 status = nfs4_set_lock_state(state, request);
4480 if (status != 0)
4481 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004482 request->fl_flags |= FL_ACCESS;
4483 status = do_vfs_lock(request->fl_file, request);
4484 if (status < 0)
4485 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004486 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004487 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004488 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004489 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004490 request->fl_flags = fl_flags & ~FL_SLEEP;
4491 status = do_vfs_lock(request->fl_file, request);
4492 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004493 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004494 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004495 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004496 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004497 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004498 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004499 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004500 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004501out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004502 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004503out:
4504 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 return status;
4506}
4507
4508static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4509{
4510 struct nfs4_exception exception = { };
4511 int err;
4512
4513 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004514 err = _nfs4_proc_setlk(state, cmd, request);
4515 if (err == -NFS4ERR_DENIED)
4516 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004518 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 } while (exception.retry);
4520 return err;
4521}
4522
4523static int
4524nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4525{
4526 struct nfs_open_context *ctx;
4527 struct nfs4_state *state;
4528 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4529 int status;
4530
4531 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004532 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 state = ctx->state;
4534
4535 if (request->fl_start < 0 || request->fl_end < 0)
4536 return -EINVAL;
4537
Trond Myklebustd9531262009-07-21 19:22:38 -04004538 if (IS_GETLK(cmd)) {
4539 if (state != NULL)
4540 return nfs4_proc_getlk(state, F_GETLK, request);
4541 return 0;
4542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543
4544 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4545 return -EINVAL;
4546
Trond Myklebustd9531262009-07-21 19:22:38 -04004547 if (request->fl_type == F_UNLCK) {
4548 if (state != NULL)
4549 return nfs4_proc_unlck(state, cmd, request);
4550 return 0;
4551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552
Trond Myklebustd9531262009-07-21 19:22:38 -04004553 if (state == NULL)
4554 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 do {
4556 status = nfs4_proc_setlk(state, cmd, request);
4557 if ((status != -EAGAIN) || IS_SETLK(cmd))
4558 break;
4559 timeout = nfs4_set_lock_task_retry(timeout);
4560 status = -ERESTARTSYS;
4561 if (signalled())
4562 break;
4563 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 return status;
4565}
4566
Trond Myklebust888e6942005-11-04 15:38:11 -05004567int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4568{
4569 struct nfs_server *server = NFS_SERVER(state->inode);
4570 struct nfs4_exception exception = { };
4571 int err;
4572
4573 err = nfs4_set_lock_state(state, fl);
4574 if (err != 0)
4575 goto out;
4576 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004577 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004578 switch (err) {
4579 default:
4580 printk(KERN_ERR "%s: unhandled error %d.\n",
4581 __func__, err);
4582 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004583 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004584 goto out;
4585 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004586 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004587 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004588 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004589 nfs4_schedule_lease_recovery(server->nfs_client);
4590 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004591 case -NFS4ERR_BADSESSION:
4592 case -NFS4ERR_BADSLOT:
4593 case -NFS4ERR_BAD_HIGH_SLOT:
4594 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4595 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004596 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004597 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004598 case -ERESTARTSYS:
4599 /*
4600 * The show must go on: exit, but mark the
4601 * stateid as needing recovery.
4602 */
4603 case -NFS4ERR_ADMIN_REVOKED:
4604 case -NFS4ERR_BAD_STATEID:
4605 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004606 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004607 err = 0;
4608 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004609 case -EKEYEXPIRED:
4610 /*
4611 * User RPCSEC_GSS context has expired.
4612 * We cannot recover this stateid now, so
4613 * skip it and allow recovery thread to
4614 * proceed.
4615 */
4616 err = 0;
4617 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004618 case -ENOMEM:
4619 case -NFS4ERR_DENIED:
4620 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4621 err = 0;
4622 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004623 case -NFS4ERR_DELAY:
4624 break;
4625 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004626 err = nfs4_handle_exception(server, err, &exception);
4627 } while (exception.retry);
4628out:
4629 return err;
4630}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004631
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004632static void nfs4_release_lockowner_release(void *calldata)
4633{
4634 kfree(calldata);
4635}
4636
4637const struct rpc_call_ops nfs4_release_lockowner_ops = {
4638 .rpc_release = nfs4_release_lockowner_release,
4639};
4640
4641void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4642{
4643 struct nfs_server *server = lsp->ls_state->owner->so_server;
4644 struct nfs_release_lockowner_args *args;
4645 struct rpc_message msg = {
4646 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4647 };
4648
4649 if (server->nfs_client->cl_mvops->minor_version != 0)
4650 return;
4651 args = kmalloc(sizeof(*args), GFP_NOFS);
4652 if (!args)
4653 return;
4654 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4655 args->lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004656 args->lock_owner.s_dev = server->s_dev;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004657 msg.rpc_argp = args;
4658 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4659}
4660
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004661#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4662
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004663static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4664 const void *buf, size_t buflen,
4665 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004666{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004667 if (strcmp(key, "") != 0)
4668 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004669
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004670 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004671}
4672
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004673static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4674 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004675{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004676 if (strcmp(key, "") != 0)
4677 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004678
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004679 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004680}
4681
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004682static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4683 size_t list_len, const char *name,
4684 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004685{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004686 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004687
J. Bruce Fields096455a2006-03-20 23:23:42 -05004688 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4689 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004690
4691 if (list && len <= list_len)
4692 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004693 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004694}
4695
Andy Adamson533eb462011-06-13 18:25:56 -04004696/*
4697 * nfs_fhget will use either the mounted_on_fileid or the fileid
4698 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004699static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4700{
Andy Adamson533eb462011-06-13 18:25:56 -04004701 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4702 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4703 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4704 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004705 return;
4706
4707 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4708 NFS_ATTR_FATTR_NLINK;
4709 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4710 fattr->nlink = 2;
4711}
4712
Trond Myklebust56659e92007-07-17 21:52:39 -04004713int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004714 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004715{
4716 struct nfs_server *server = NFS_SERVER(dir);
4717 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004718 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004719 };
4720 struct nfs4_fs_locations_arg args = {
4721 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004722 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004723 .page = page,
4724 .bitmask = bitmask,
4725 };
Benny Halevy22958462009-04-01 09:22:02 -04004726 struct nfs4_fs_locations_res res = {
4727 .fs_locations = fs_locations,
4728 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004729 struct rpc_message msg = {
4730 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4731 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004732 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004733 };
4734 int status;
4735
Harvey Harrison3110ff82008-05-02 13:42:44 -07004736 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04004737
4738 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
4739 * is not supported */
4740 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
4741 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
4742 else
4743 bitmask[0] |= FATTR4_WORD0_FILEID;
4744
Trond Myklebustc228fd32007-01-13 02:28:11 -05004745 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004746 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004747 fs_locations->nlocations = 0;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004748 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004749 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004750 return status;
4751}
4752
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00004753static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4754{
4755 int status;
4756 struct nfs4_secinfo_arg args = {
4757 .dir_fh = NFS_FH(dir),
4758 .name = name,
4759 };
4760 struct nfs4_secinfo_res res = {
4761 .flavors = flavors,
4762 };
4763 struct rpc_message msg = {
4764 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
4765 .rpc_argp = &args,
4766 .rpc_resp = &res,
4767 };
4768
4769 dprintk("NFS call secinfo %s\n", name->name);
4770 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4771 dprintk("NFS reply secinfo: %d\n", status);
4772 return status;
4773}
4774
4775int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4776{
4777 struct nfs4_exception exception = { };
4778 int err;
4779 do {
4780 err = nfs4_handle_exception(NFS_SERVER(dir),
4781 _nfs4_proc_secinfo(dir, name, flavors),
4782 &exception);
4783 } while (exception.retry);
4784 return err;
4785}
4786
Andy Adamson557134a2009-04-01 09:21:53 -04004787#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004788/*
Andy Adamson357f54d2010-12-14 10:11:57 -05004789 * Check the exchange flags returned by the server for invalid flags, having
4790 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4791 * DS flags set.
4792 */
4793static int nfs4_check_cl_exchange_flags(u32 flags)
4794{
4795 if (flags & ~EXCHGID4_FLAG_MASK_R)
4796 goto out_inval;
4797 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4798 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4799 goto out_inval;
4800 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4801 goto out_inval;
4802 return NFS_OK;
4803out_inval:
4804 return -NFS4ERR_INVAL;
4805}
4806
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004807static bool
4808nfs41_same_server_scope(struct server_scope *a, struct server_scope *b)
4809{
4810 if (a->server_scope_sz == b->server_scope_sz &&
4811 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
4812 return true;
4813
4814 return false;
4815}
4816
Andy Adamson357f54d2010-12-14 10:11:57 -05004817/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04004818 * nfs4_proc_exchange_id()
4819 *
4820 * Since the clientid has expired, all compounds using sessions
4821 * associated with the stale clientid will be returning
4822 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4823 * be in some phase of session reset.
4824 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004825int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004826{
4827 nfs4_verifier verifier;
4828 struct nfs41_exchange_id_args args = {
4829 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05004830 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04004831 };
4832 struct nfs41_exchange_id_res res = {
4833 .client = clp,
4834 };
4835 int status;
4836 struct rpc_message msg = {
4837 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4838 .rpc_argp = &args,
4839 .rpc_resp = &res,
4840 .rpc_cred = cred,
4841 };
4842 __be32 *p;
4843
4844 dprintk("--> %s\n", __func__);
4845 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004846
Benny Halevy99fe60d2009-04-01 09:22:29 -04004847 p = (u32 *)verifier.data;
4848 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4849 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4850 args.verifier = &verifier;
4851
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004852 args.id_len = scnprintf(args.id, sizeof(args.id),
4853 "%s/%s.%s/%u",
4854 clp->cl_ipaddr,
4855 init_utsname()->nodename,
4856 init_utsname()->domainname,
4857 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004858
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004859 res.server_scope = kzalloc(sizeof(struct server_scope), GFP_KERNEL);
4860 if (unlikely(!res.server_scope))
4861 return -ENOMEM;
4862
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004863 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004864 if (!status)
4865 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004866
4867 if (!status) {
4868 if (clp->server_scope &&
4869 !nfs41_same_server_scope(clp->server_scope,
4870 res.server_scope)) {
4871 dprintk("%s: server_scope mismatch detected\n",
4872 __func__);
4873 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
4874 kfree(clp->server_scope);
4875 clp->server_scope = NULL;
4876 }
4877
4878 if (!clp->server_scope)
4879 clp->server_scope = res.server_scope;
4880 else
4881 kfree(res.server_scope);
4882 }
4883
Benny Halevy99fe60d2009-04-01 09:22:29 -04004884 dprintk("<-- %s status= %d\n", __func__, status);
4885 return status;
4886}
4887
Andy Adamson2050f0c2009-04-01 09:22:30 -04004888struct nfs4_get_lease_time_data {
4889 struct nfs4_get_lease_time_args *args;
4890 struct nfs4_get_lease_time_res *res;
4891 struct nfs_client *clp;
4892};
4893
4894static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4895 void *calldata)
4896{
4897 int ret;
4898 struct nfs4_get_lease_time_data *data =
4899 (struct nfs4_get_lease_time_data *)calldata;
4900
4901 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004902 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004903 /* just setup sequence, do not trigger session recovery
4904 since we're invoked within one */
4905 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004906 &data->args->la_seq_args,
4907 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004908
4909 BUG_ON(ret == -EAGAIN);
4910 rpc_call_start(task);
4911 dprintk("<-- %s\n", __func__);
4912}
4913
4914/*
4915 * Called from nfs4_state_manager thread for session setup, so don't recover
4916 * from sequence operation or clientid errors.
4917 */
4918static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4919{
4920 struct nfs4_get_lease_time_data *data =
4921 (struct nfs4_get_lease_time_data *)calldata;
4922
4923 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04004924 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4925 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04004926 switch (task->tk_status) {
4927 case -NFS4ERR_DELAY:
4928 case -NFS4ERR_GRACE:
4929 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4930 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4931 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004932 /* fall through */
4933 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004934 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004935 return;
4936 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004937 dprintk("<-- %s\n", __func__);
4938}
4939
4940struct rpc_call_ops nfs4_get_lease_time_ops = {
4941 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4942 .rpc_call_done = nfs4_get_lease_time_done,
4943};
4944
4945int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4946{
4947 struct rpc_task *task;
4948 struct nfs4_get_lease_time_args args;
4949 struct nfs4_get_lease_time_res res = {
4950 .lr_fsinfo = fsinfo,
4951 };
4952 struct nfs4_get_lease_time_data data = {
4953 .args = &args,
4954 .res = &res,
4955 .clp = clp,
4956 };
4957 struct rpc_message msg = {
4958 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4959 .rpc_argp = &args,
4960 .rpc_resp = &res,
4961 };
4962 struct rpc_task_setup task_setup = {
4963 .rpc_client = clp->cl_rpcclient,
4964 .rpc_message = &msg,
4965 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004966 .callback_data = &data,
4967 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04004968 };
4969 int status;
4970
Andy Adamson2050f0c2009-04-01 09:22:30 -04004971 dprintk("--> %s\n", __func__);
4972 task = rpc_run_task(&task_setup);
4973
4974 if (IS_ERR(task))
4975 status = PTR_ERR(task);
4976 else {
4977 status = task->tk_status;
4978 rpc_put_task(task);
4979 }
4980 dprintk("<-- %s return %d\n", __func__, status);
4981
4982 return status;
4983}
4984
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004985/*
4986 * Reset a slot table
4987 */
Andy Adamson104aeba2010-01-14 17:45:10 -05004988static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4989 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004990{
Andy Adamson104aeba2010-01-14 17:45:10 -05004991 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004992 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004993 int ret = 0;
4994
Andy Adamson104aeba2010-01-14 17:45:10 -05004995 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4996 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004997
Andy Adamson104aeba2010-01-14 17:45:10 -05004998 /* Does the newly negotiated max_reqs match the existing slot table? */
4999 if (max_reqs != tbl->max_slots) {
5000 ret = -ENOMEM;
5001 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005002 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005003 if (!new)
5004 goto out;
5005 ret = 0;
5006 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005007 }
5008 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05005009 if (new) {
5010 tbl->slots = new;
5011 tbl->max_slots = max_reqs;
5012 }
5013 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005014 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005015 spin_unlock(&tbl->slot_tbl_lock);
5016 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5017 tbl, tbl->slots, tbl->max_slots);
5018out:
5019 dprintk("<-- %s: return %d\n", __func__, ret);
5020 return ret;
5021}
5022
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005023/* Destroy the slot table */
5024static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5025{
5026 if (session->fc_slot_table.slots != NULL) {
5027 kfree(session->fc_slot_table.slots);
5028 session->fc_slot_table.slots = NULL;
5029 }
5030 if (session->bc_slot_table.slots != NULL) {
5031 kfree(session->bc_slot_table.slots);
5032 session->bc_slot_table.slots = NULL;
5033 }
5034 return;
5035}
5036
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005037/*
Andy Adamsonfc931582009-04-01 09:22:31 -04005038 * Initialize slot table
5039 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005040static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
5041 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04005042{
Andy Adamsonfc931582009-04-01 09:22:31 -04005043 struct nfs4_slot *slot;
5044 int ret = -ENOMEM;
5045
5046 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
5047
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005048 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04005049
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005050 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04005051 if (!slot)
5052 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04005053 ret = 0;
5054
5055 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04005056 tbl->max_slots = max_slots;
5057 tbl->slots = slot;
5058 tbl->highest_used_slotid = -1; /* no slot is currently used */
5059 spin_unlock(&tbl->slot_tbl_lock);
5060 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5061 tbl, tbl->slots, tbl->max_slots);
5062out:
5063 dprintk("<-- %s: return %d\n", __func__, ret);
5064 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04005065}
5066
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005067/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005068 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005069 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005070static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005071{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005072 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005073 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005074
Andy Adamsonaacd5532011-11-09 13:58:21 -05005075 dprintk("--> %s\n", __func__);
5076 /* Fore channel */
5077 tbl = &ses->fc_slot_table;
Trond Myklebustf26468f2009-12-05 19:32:11 -05005078 if (tbl->slots == NULL) {
Andy Adamsonaacd5532011-11-09 13:58:21 -05005079 status = nfs4_init_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5080 if (status) /* -ENOMEM */
5081 return status;
5082 } else {
5083 status = nfs4_reset_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
Trond Myklebustf26468f2009-12-05 19:32:11 -05005084 if (status)
5085 return status;
5086 }
Andy Adamsonaacd5532011-11-09 13:58:21 -05005087 /* Back channel */
5088 tbl = &ses->bc_slot_table;
Trond Myklebustf26468f2009-12-05 19:32:11 -05005089 if (tbl->slots == NULL) {
Andy Adamsonaacd5532011-11-09 13:58:21 -05005090 status = nfs4_init_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
Trond Myklebustf26468f2009-12-05 19:32:11 -05005091 if (status)
Andy Adamsonaacd5532011-11-09 13:58:21 -05005092 /* Fore and back channel share a connection so get
5093 * both slot tables or neither */
5094 nfs4_destroy_slot_tables(ses);
5095 } else
5096 status = nfs4_reset_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005097 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005098}
5099
5100struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5101{
5102 struct nfs4_session *session;
5103 struct nfs4_slot_table *tbl;
5104
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005105 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005106 if (!session)
5107 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005108
Andy Adamson557134a2009-04-01 09:21:53 -04005109 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05005110 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04005111 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005112 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005113 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005114
5115 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05005116 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005117 spin_lock_init(&tbl->slot_tbl_lock);
5118 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005119 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005120
Trond Myklebust1055d762010-06-16 09:52:27 -04005121 session->session_state = 1<<NFS4_SESSION_INITING;
5122
Andy Adamson557134a2009-04-01 09:21:53 -04005123 session->clp = clp;
5124 return session;
5125}
5126
5127void nfs4_destroy_session(struct nfs4_session *session)
5128{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005129 nfs4_proc_destroy_session(session);
5130 dprintk("%s Destroy backchannel for xprt %p\n",
5131 __func__, session->clp->cl_rpcclient->cl_xprt);
5132 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
5133 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005134 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005135 kfree(session);
5136}
5137
Andy Adamsonfc931582009-04-01 09:22:31 -04005138/*
5139 * Initialize the values to be used by the client in CREATE_SESSION
5140 * If nfs4_init_session set the fore channel request and response sizes,
5141 * use them.
5142 *
5143 * Set the back channel max_resp_sz_cached to zero to force the client to
5144 * always set csa_cachethis to FALSE because the current implementation
5145 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5146 */
5147static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5148{
5149 struct nfs4_session *session = args->client->cl_session;
5150 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5151 mxresp_sz = session->fc_attrs.max_resp_sz;
5152
5153 if (mxrqst_sz == 0)
5154 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5155 if (mxresp_sz == 0)
5156 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5157 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005158 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5159 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005160 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5161 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
5162
5163 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005164 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005165 __func__,
5166 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005167 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005168
5169 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005170 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5171 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5172 args->bc_attrs.max_resp_sz_cached = 0;
5173 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5174 args->bc_attrs.max_reqs = 1;
5175
5176 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5177 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5178 __func__,
5179 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5180 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5181 args->bc_attrs.max_reqs);
5182}
5183
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005184static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005185{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005186 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5187 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5188
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005189 if (rcvd->max_resp_sz > sent->max_resp_sz)
5190 return -EINVAL;
5191 /*
5192 * Our requested max_ops is the minimum we need; we're not
5193 * prepared to break up compounds into smaller pieces than that.
5194 * So, no point even trying to continue if the server won't
5195 * cooperate:
5196 */
5197 if (rcvd->max_ops < sent->max_ops)
5198 return -EINVAL;
5199 if (rcvd->max_reqs == 0)
5200 return -EINVAL;
5201 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005202}
5203
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005204static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5205{
5206 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5207 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005208
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005209 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5210 return -EINVAL;
5211 if (rcvd->max_resp_sz < sent->max_resp_sz)
5212 return -EINVAL;
5213 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5214 return -EINVAL;
5215 /* These would render the backchannel useless: */
5216 if (rcvd->max_ops == 0)
5217 return -EINVAL;
5218 if (rcvd->max_reqs == 0)
5219 return -EINVAL;
5220 return 0;
5221}
Andy Adamson8d353012009-04-01 09:22:32 -04005222
Andy Adamson8d353012009-04-01 09:22:32 -04005223static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5224 struct nfs4_session *session)
5225{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005226 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005227
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005228 ret = nfs4_verify_fore_channel_attrs(args, session);
5229 if (ret)
5230 return ret;
5231 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005232}
5233
Andy Adamsonfc931582009-04-01 09:22:31 -04005234static int _nfs4_proc_create_session(struct nfs_client *clp)
5235{
5236 struct nfs4_session *session = clp->cl_session;
5237 struct nfs41_create_session_args args = {
5238 .client = clp,
5239 .cb_program = NFS4_CALLBACK,
5240 };
5241 struct nfs41_create_session_res res = {
5242 .client = clp,
5243 };
5244 struct rpc_message msg = {
5245 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5246 .rpc_argp = &args,
5247 .rpc_resp = &res,
5248 };
5249 int status;
5250
5251 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005252 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005253
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005254 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005255
Andy Adamson8d353012009-04-01 09:22:32 -04005256 if (!status)
5257 /* Verify the session's negotiated channel_attrs values */
5258 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005259 if (!status) {
5260 /* Increment the clientid slot sequence id */
5261 clp->cl_seqid++;
5262 }
5263
5264 return status;
5265}
5266
5267/*
5268 * Issues a CREATE_SESSION operation to the server.
5269 * It is the responsibility of the caller to verify the session is
5270 * expired before calling this routine.
5271 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005272int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005273{
5274 int status;
5275 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005276 struct nfs4_session *session = clp->cl_session;
5277
5278 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5279
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005280 status = _nfs4_proc_create_session(clp);
Andy Adamsonfc931582009-04-01 09:22:31 -04005281 if (status)
5282 goto out;
5283
Andy Adamsonaacd5532011-11-09 13:58:21 -05005284 /* Init or reset the session slot tables */
5285 status = nfs4_setup_session_slot_tables(session);
5286 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005287 if (status)
5288 goto out;
5289
5290 ptr = (unsigned *)&session->sess_id.data[0];
5291 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5292 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005293out:
5294 dprintk("<-- %s\n", __func__);
5295 return status;
5296}
5297
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005298/*
5299 * Issue the over-the-wire RPC DESTROY_SESSION.
5300 * The caller must serialize access to this routine.
5301 */
5302int nfs4_proc_destroy_session(struct nfs4_session *session)
5303{
5304 int status = 0;
5305 struct rpc_message msg;
5306
5307 dprintk("--> nfs4_proc_destroy_session\n");
5308
5309 /* session is still being setup */
5310 if (session->clp->cl_cons_state != NFS_CS_READY)
5311 return status;
5312
5313 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5314 msg.rpc_argp = session;
5315 msg.rpc_resp = NULL;
5316 msg.rpc_cred = NULL;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005317 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005318
5319 if (status)
5320 printk(KERN_WARNING
5321 "Got error %d from the server on DESTROY_SESSION. "
5322 "Session has been destroyed regardless...\n", status);
5323
5324 dprintk("<-- nfs4_proc_destroy_session\n");
5325 return status;
5326}
5327
Trond Myklebustfccba802009-07-21 16:48:07 -04005328int nfs4_init_session(struct nfs_server *server)
5329{
5330 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005331 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005332 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005333 int ret;
5334
5335 if (!nfs4_has_session(clp))
5336 return 0;
5337
Trond Myklebust1055d762010-06-16 09:52:27 -04005338 session = clp->cl_session;
5339 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5340 return 0;
5341
Andy Adamson68bf05e2009-12-15 12:55:02 -05005342 rsize = server->rsize;
5343 if (rsize == 0)
5344 rsize = NFS_MAX_FILE_IO_SIZE;
5345 wsize = server->wsize;
5346 if (wsize == 0)
5347 wsize = NFS_MAX_FILE_IO_SIZE;
5348
Andy Adamson68bf05e2009-12-15 12:55:02 -05005349 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5350 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005351
Trond Myklebustfccba802009-07-21 16:48:07 -04005352 ret = nfs4_recover_expired_lease(server);
5353 if (!ret)
5354 ret = nfs4_check_client_ready(clp);
5355 return ret;
5356}
5357
Andy Adamsond83217c2011-03-01 01:34:17 +00005358int nfs4_init_ds_session(struct nfs_client *clp)
5359{
5360 struct nfs4_session *session = clp->cl_session;
5361 int ret;
5362
5363 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5364 return 0;
5365
5366 ret = nfs4_client_recover_expired_lease(clp);
5367 if (!ret)
5368 /* Test for the DS role */
5369 if (!is_ds_client(clp))
5370 ret = -ENODEV;
5371 if (!ret)
5372 ret = nfs4_check_client_ready(clp);
5373 return ret;
5374
5375}
5376EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5377
5378
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005379/*
5380 * Renew the cl_session lease.
5381 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005382struct nfs4_sequence_data {
5383 struct nfs_client *clp;
5384 struct nfs4_sequence_args args;
5385 struct nfs4_sequence_res res;
5386};
5387
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005388static void nfs41_sequence_release(void *data)
5389{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005390 struct nfs4_sequence_data *calldata = data;
5391 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005392
Alexandros Batsakis71358402010-02-05 03:45:05 -08005393 if (atomic_read(&clp->cl_count) > 1)
5394 nfs4_schedule_state_renewal(clp);
5395 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005396 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005397}
5398
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005399static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5400{
5401 switch(task->tk_status) {
5402 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005403 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5404 return -EAGAIN;
5405 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005406 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005407 }
5408 return 0;
5409}
5410
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005411static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005412{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005413 struct nfs4_sequence_data *calldata = data;
5414 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005415
Trond Myklebust14516c32010-07-31 14:29:06 -04005416 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5417 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005418
5419 if (task->tk_status < 0) {
5420 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005421 if (atomic_read(&clp->cl_count) == 1)
5422 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005423
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005424 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5425 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005426 return;
5427 }
5428 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005429 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005430out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005431 dprintk("<-- %s\n", __func__);
5432}
5433
5434static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5435{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005436 struct nfs4_sequence_data *calldata = data;
5437 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005438 struct nfs4_sequence_args *args;
5439 struct nfs4_sequence_res *res;
5440
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005441 args = task->tk_msg.rpc_argp;
5442 res = task->tk_msg.rpc_resp;
5443
Trond Myklebust035168a2010-06-16 09:52:26 -04005444 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005445 return;
5446 rpc_call_start(task);
5447}
5448
5449static const struct rpc_call_ops nfs41_sequence_ops = {
5450 .rpc_call_done = nfs41_sequence_call_done,
5451 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005452 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005453};
5454
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005455static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005456{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005457 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005458 struct rpc_message msg = {
5459 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5460 .rpc_cred = cred,
5461 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005462 struct rpc_task_setup task_setup_data = {
5463 .rpc_client = clp->cl_rpcclient,
5464 .rpc_message = &msg,
5465 .callback_ops = &nfs41_sequence_ops,
5466 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5467 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005468
Alexandros Batsakis71358402010-02-05 03:45:05 -08005469 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005470 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005471 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005472 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005473 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005474 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005475 }
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005476 msg.rpc_argp = &calldata->args;
5477 msg.rpc_resp = &calldata->res;
5478 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005479 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005480
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005481 return rpc_run_task(&task_setup_data);
5482}
5483
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005484static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005485{
5486 struct rpc_task *task;
5487 int ret = 0;
5488
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005489 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5490 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005491 task = _nfs41_proc_sequence(clp, cred);
5492 if (IS_ERR(task))
5493 ret = PTR_ERR(task);
5494 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005495 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005496 dprintk("<-- %s status=%d\n", __func__, ret);
5497 return ret;
5498}
5499
5500static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5501{
5502 struct rpc_task *task;
5503 int ret;
5504
5505 task = _nfs41_proc_sequence(clp, cred);
5506 if (IS_ERR(task)) {
5507 ret = PTR_ERR(task);
5508 goto out;
5509 }
5510 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005511 if (!ret) {
5512 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5513
5514 if (task->tk_status == 0)
5515 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005516 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005517 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005518 rpc_put_task(task);
5519out:
5520 dprintk("<-- %s status=%d\n", __func__, ret);
5521 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005522}
5523
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005524struct nfs4_reclaim_complete_data {
5525 struct nfs_client *clp;
5526 struct nfs41_reclaim_complete_args arg;
5527 struct nfs41_reclaim_complete_res res;
5528};
5529
5530static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5531{
5532 struct nfs4_reclaim_complete_data *calldata = data;
5533
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005534 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005535 if (nfs41_setup_sequence(calldata->clp->cl_session,
5536 &calldata->arg.seq_args,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005537 &calldata->res.seq_res, 0, task))
5538 return;
5539
5540 rpc_call_start(task);
5541}
5542
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005543static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5544{
5545 switch(task->tk_status) {
5546 case 0:
5547 case -NFS4ERR_COMPLETE_ALREADY:
5548 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5549 break;
5550 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005551 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005552 /* fall through */
5553 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005554 return -EAGAIN;
5555 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005556 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005557 }
5558 return 0;
5559}
5560
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005561static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5562{
5563 struct nfs4_reclaim_complete_data *calldata = data;
5564 struct nfs_client *clp = calldata->clp;
5565 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5566
5567 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005568 if (!nfs41_sequence_done(task, res))
5569 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005570
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005571 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5572 rpc_restart_call_prepare(task);
5573 return;
5574 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005575 dprintk("<-- %s\n", __func__);
5576}
5577
5578static void nfs4_free_reclaim_complete_data(void *data)
5579{
5580 struct nfs4_reclaim_complete_data *calldata = data;
5581
5582 kfree(calldata);
5583}
5584
5585static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5586 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5587 .rpc_call_done = nfs4_reclaim_complete_done,
5588 .rpc_release = nfs4_free_reclaim_complete_data,
5589};
5590
5591/*
5592 * Issue a global reclaim complete.
5593 */
5594static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5595{
5596 struct nfs4_reclaim_complete_data *calldata;
5597 struct rpc_task *task;
5598 struct rpc_message msg = {
5599 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5600 };
5601 struct rpc_task_setup task_setup_data = {
5602 .rpc_client = clp->cl_rpcclient,
5603 .rpc_message = &msg,
5604 .callback_ops = &nfs4_reclaim_complete_call_ops,
5605 .flags = RPC_TASK_ASYNC,
5606 };
5607 int status = -ENOMEM;
5608
5609 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005610 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005611 if (calldata == NULL)
5612 goto out;
5613 calldata->clp = clp;
5614 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005615
5616 msg.rpc_argp = &calldata->arg;
5617 msg.rpc_resp = &calldata->res;
5618 task_setup_data.callback_data = calldata;
5619 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005620 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005621 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005622 goto out;
5623 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005624 status = nfs4_wait_for_completion_rpc_task(task);
5625 if (status == 0)
5626 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005627 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005628 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005629out:
5630 dprintk("<-- %s status=%d\n", __func__, status);
5631 return status;
5632}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005633
5634static void
5635nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5636{
5637 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005638 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005639
5640 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005641 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5642 * right now covering the LAYOUTGET we are about to send.
5643 * However, that is not so catastrophic, and there seems
5644 * to be no way to prevent it completely.
5645 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005646 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5647 &lgp->res.seq_res, 0, task))
5648 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005649 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5650 NFS_I(lgp->args.inode)->layout,
5651 lgp->args.ctx->state)) {
5652 rpc_exit(task, NFS4_OK);
5653 return;
5654 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005655 rpc_call_start(task);
5656}
5657
5658static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5659{
5660 struct nfs4_layoutget *lgp = calldata;
5661 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5662
5663 dprintk("--> %s\n", __func__);
5664
5665 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5666 return;
5667
5668 switch (task->tk_status) {
5669 case 0:
5670 break;
5671 case -NFS4ERR_LAYOUTTRYLATER:
5672 case -NFS4ERR_RECALLCONFLICT:
5673 task->tk_status = -NFS4ERR_DELAY;
5674 /* Fall through */
5675 default:
5676 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5677 rpc_restart_call_prepare(task);
5678 return;
5679 }
5680 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005681 dprintk("<-- %s\n", __func__);
5682}
5683
5684static void nfs4_layoutget_release(void *calldata)
5685{
5686 struct nfs4_layoutget *lgp = calldata;
5687
5688 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005689 put_nfs_open_context(lgp->args.ctx);
5690 kfree(calldata);
5691 dprintk("<-- %s\n", __func__);
5692}
5693
5694static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5695 .rpc_call_prepare = nfs4_layoutget_prepare,
5696 .rpc_call_done = nfs4_layoutget_done,
5697 .rpc_release = nfs4_layoutget_release,
5698};
5699
5700int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5701{
5702 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5703 struct rpc_task *task;
5704 struct rpc_message msg = {
5705 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5706 .rpc_argp = &lgp->args,
5707 .rpc_resp = &lgp->res,
5708 };
5709 struct rpc_task_setup task_setup_data = {
5710 .rpc_client = server->client,
5711 .rpc_message = &msg,
5712 .callback_ops = &nfs4_layoutget_call_ops,
5713 .callback_data = lgp,
5714 .flags = RPC_TASK_ASYNC,
5715 };
5716 int status = 0;
5717
5718 dprintk("--> %s\n", __func__);
5719
Weston Andros Adamson35124a02011-03-24 16:48:21 -04005720 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005721 lgp->res.seq_res.sr_slot = NULL;
5722 task = rpc_run_task(&task_setup_data);
5723 if (IS_ERR(task))
5724 return PTR_ERR(task);
5725 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00005726 if (status == 0)
5727 status = task->tk_status;
5728 if (status == 0)
5729 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005730 rpc_put_task(task);
5731 dprintk("<-- %s status=%d\n", __func__, status);
5732 return status;
5733}
5734
Benny Halevycbe82602011-05-22 19:52:37 +03005735static void
5736nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
5737{
5738 struct nfs4_layoutreturn *lrp = calldata;
5739
5740 dprintk("--> %s\n", __func__);
5741 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
5742 &lrp->res.seq_res, 0, task))
5743 return;
5744 rpc_call_start(task);
5745}
5746
5747static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
5748{
5749 struct nfs4_layoutreturn *lrp = calldata;
5750 struct nfs_server *server;
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005751 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03005752
5753 dprintk("--> %s\n", __func__);
5754
5755 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
5756 return;
5757
5758 server = NFS_SERVER(lrp->args.inode);
5759 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005760 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03005761 return;
5762 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005763 spin_lock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005764 if (task->tk_status == 0) {
Benny Halevycbe82602011-05-22 19:52:37 +03005765 if (lrp->res.lrs_present) {
Benny Halevycbe82602011-05-22 19:52:37 +03005766 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Benny Halevycbe82602011-05-22 19:52:37 +03005767 } else
5768 BUG_ON(!list_empty(&lo->plh_segs));
5769 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005770 lo->plh_block_lgets--;
5771 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005772 dprintk("<-- %s\n", __func__);
5773}
5774
5775static void nfs4_layoutreturn_release(void *calldata)
5776{
5777 struct nfs4_layoutreturn *lrp = calldata;
5778
5779 dprintk("--> %s\n", __func__);
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005780 put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03005781 kfree(calldata);
5782 dprintk("<-- %s\n", __func__);
5783}
5784
5785static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
5786 .rpc_call_prepare = nfs4_layoutreturn_prepare,
5787 .rpc_call_done = nfs4_layoutreturn_done,
5788 .rpc_release = nfs4_layoutreturn_release,
5789};
5790
5791int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
5792{
5793 struct rpc_task *task;
5794 struct rpc_message msg = {
5795 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
5796 .rpc_argp = &lrp->args,
5797 .rpc_resp = &lrp->res,
5798 };
5799 struct rpc_task_setup task_setup_data = {
5800 .rpc_client = lrp->clp->cl_rpcclient,
5801 .rpc_message = &msg,
5802 .callback_ops = &nfs4_layoutreturn_call_ops,
5803 .callback_data = lrp,
5804 };
5805 int status;
5806
5807 dprintk("--> %s\n", __func__);
5808 task = rpc_run_task(&task_setup_data);
5809 if (IS_ERR(task))
5810 return PTR_ERR(task);
5811 status = task->tk_status;
5812 dprintk("<-- %s status=%d\n", __func__, status);
5813 rpc_put_task(task);
5814 return status;
5815}
5816
Andy Adamson7f11d8d2011-07-30 20:52:35 -04005817/*
5818 * Retrieve the list of Data Server devices from the MDS.
5819 */
5820static int _nfs4_getdevicelist(struct nfs_server *server,
5821 const struct nfs_fh *fh,
5822 struct pnfs_devicelist *devlist)
5823{
5824 struct nfs4_getdevicelist_args args = {
5825 .fh = fh,
5826 .layoutclass = server->pnfs_curr_ld->id,
5827 };
5828 struct nfs4_getdevicelist_res res = {
5829 .devlist = devlist,
5830 };
5831 struct rpc_message msg = {
5832 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
5833 .rpc_argp = &args,
5834 .rpc_resp = &res,
5835 };
5836 int status;
5837
5838 dprintk("--> %s\n", __func__);
5839 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5840 &res.seq_res, 0);
5841 dprintk("<-- %s status=%d\n", __func__, status);
5842 return status;
5843}
5844
5845int nfs4_proc_getdevicelist(struct nfs_server *server,
5846 const struct nfs_fh *fh,
5847 struct pnfs_devicelist *devlist)
5848{
5849 struct nfs4_exception exception = { };
5850 int err;
5851
5852 do {
5853 err = nfs4_handle_exception(server,
5854 _nfs4_getdevicelist(server, fh, devlist),
5855 &exception);
5856 } while (exception.retry);
5857
5858 dprintk("%s: err=%d, num_devs=%u\n", __func__,
5859 err, devlist->num_devs);
5860
5861 return err;
5862}
5863EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
5864
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005865static int
5866_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5867{
5868 struct nfs4_getdeviceinfo_args args = {
5869 .pdev = pdev,
5870 };
5871 struct nfs4_getdeviceinfo_res res = {
5872 .pdev = pdev,
5873 };
5874 struct rpc_message msg = {
5875 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5876 .rpc_argp = &args,
5877 .rpc_resp = &res,
5878 };
5879 int status;
5880
5881 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005882 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005883 dprintk("<-- %s status=%d\n", __func__, status);
5884
5885 return status;
5886}
5887
5888int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5889{
5890 struct nfs4_exception exception = { };
5891 int err;
5892
5893 do {
5894 err = nfs4_handle_exception(server,
5895 _nfs4_proc_getdeviceinfo(server, pdev),
5896 &exception);
5897 } while (exception.retry);
5898 return err;
5899}
5900EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5901
Andy Adamson863a3c62011-03-23 13:27:54 +00005902static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
5903{
5904 struct nfs4_layoutcommit_data *data = calldata;
5905 struct nfs_server *server = NFS_SERVER(data->args.inode);
5906
5907 if (nfs4_setup_sequence(server, &data->args.seq_args,
5908 &data->res.seq_res, 1, task))
5909 return;
5910 rpc_call_start(task);
5911}
5912
5913static void
5914nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
5915{
5916 struct nfs4_layoutcommit_data *data = calldata;
5917 struct nfs_server *server = NFS_SERVER(data->args.inode);
5918
5919 if (!nfs4_sequence_done(task, &data->res.seq_res))
5920 return;
5921
5922 switch (task->tk_status) { /* Just ignore these failures */
5923 case NFS4ERR_DELEG_REVOKED: /* layout was recalled */
5924 case NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
5925 case NFS4ERR_BADLAYOUT: /* no layout */
5926 case NFS4ERR_GRACE: /* loca_recalim always false */
5927 task->tk_status = 0;
5928 }
5929
5930 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005931 rpc_restart_call_prepare(task);
Andy Adamson863a3c62011-03-23 13:27:54 +00005932 return;
5933 }
5934
5935 if (task->tk_status == 0)
5936 nfs_post_op_update_inode_force_wcc(data->args.inode,
5937 data->res.fattr);
5938}
5939
5940static void nfs4_layoutcommit_release(void *calldata)
5941{
5942 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04005943 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07005944 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00005945
Andy Adamsondb29c082011-07-30 20:52:38 -04005946 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00005947 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04005948 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
5949 list_del_init(&lseg->pls_lc_list);
5950 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
5951 &lseg->pls_flags))
5952 put_lseg(lseg);
5953 }
Peng Tao92407e72011-10-23 20:21:17 -07005954
5955 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
5956 smp_mb__after_clear_bit();
5957 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
5958
Andy Adamson863a3c62011-03-23 13:27:54 +00005959 put_rpccred(data->cred);
5960 kfree(data);
5961}
5962
5963static const struct rpc_call_ops nfs4_layoutcommit_ops = {
5964 .rpc_call_prepare = nfs4_layoutcommit_prepare,
5965 .rpc_call_done = nfs4_layoutcommit_done,
5966 .rpc_release = nfs4_layoutcommit_release,
5967};
5968
5969int
Andy Adamsonef311532011-03-12 02:58:10 -05005970nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00005971{
5972 struct rpc_message msg = {
5973 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
5974 .rpc_argp = &data->args,
5975 .rpc_resp = &data->res,
5976 .rpc_cred = data->cred,
5977 };
5978 struct rpc_task_setup task_setup_data = {
5979 .task = &data->task,
5980 .rpc_client = NFS_CLIENT(data->args.inode),
5981 .rpc_message = &msg,
5982 .callback_ops = &nfs4_layoutcommit_ops,
5983 .callback_data = data,
5984 .flags = RPC_TASK_ASYNC,
5985 };
5986 struct rpc_task *task;
5987 int status = 0;
5988
5989 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
5990 "lbw: %llu inode %lu\n",
5991 data->task.tk_pid, sync,
5992 data->args.lastbytewritten,
5993 data->args.inode->i_ino);
5994
5995 task = rpc_run_task(&task_setup_data);
5996 if (IS_ERR(task))
5997 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05005998 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00005999 goto out;
6000 status = nfs4_wait_for_completion_rpc_task(task);
6001 if (status != 0)
6002 goto out;
6003 status = task->tk_status;
6004out:
6005 dprintk("%s: status %d\n", __func__, status);
6006 rpc_put_task(task);
6007 return status;
6008}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006009
6010static int
6011_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6012 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6013{
6014 struct nfs41_secinfo_no_name_args args = {
6015 .style = SECINFO_STYLE_CURRENT_FH,
6016 };
6017 struct nfs4_secinfo_res res = {
6018 .flavors = flavors,
6019 };
6020 struct rpc_message msg = {
6021 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6022 .rpc_argp = &args,
6023 .rpc_resp = &res,
6024 };
6025 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6026}
6027
6028static int
6029nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6030 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6031{
6032 struct nfs4_exception exception = { };
6033 int err;
6034 do {
6035 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6036 switch (err) {
6037 case 0:
6038 case -NFS4ERR_WRONGSEC:
6039 case -NFS4ERR_NOTSUPP:
6040 break;
6041 default:
6042 err = nfs4_handle_exception(server, err, &exception);
6043 }
6044 } while (exception.retry);
6045 return err;
6046}
6047
6048static int
6049nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6050 struct nfs_fsinfo *info)
6051{
6052 int err;
6053 struct page *page;
6054 rpc_authflavor_t flavor;
6055 struct nfs4_secinfo_flavors *flavors;
6056
6057 page = alloc_page(GFP_KERNEL);
6058 if (!page) {
6059 err = -ENOMEM;
6060 goto out;
6061 }
6062
6063 flavors = page_address(page);
6064 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6065
6066 /*
6067 * Fall back on "guess and check" method if
6068 * the server doesn't support SECINFO_NO_NAME
6069 */
6070 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6071 err = nfs4_find_root_sec(server, fhandle, info);
6072 goto out_freepage;
6073 }
6074 if (err)
6075 goto out_freepage;
6076
6077 flavor = nfs_find_best_sec(flavors);
6078 if (err == 0)
6079 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6080
6081out_freepage:
6082 put_page(page);
6083 if (err == -EACCES)
6084 return -EPERM;
6085out:
6086 return err;
6087}
Bryan Schumaker7d974792011-06-02 14:59:08 -04006088static int _nfs41_test_stateid(struct nfs_server *server, struct nfs4_state *state)
6089{
6090 int status;
6091 struct nfs41_test_stateid_args args = {
6092 .stateid = &state->stateid,
6093 };
6094 struct nfs41_test_stateid_res res;
6095 struct rpc_message msg = {
6096 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6097 .rpc_argp = &args,
6098 .rpc_resp = &res,
6099 };
6100 args.seq_args.sa_session = res.seq_res.sr_session = NULL;
6101 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 0, 1);
6102 return status;
6103}
6104
6105static int nfs41_test_stateid(struct nfs_server *server, struct nfs4_state *state)
6106{
6107 struct nfs4_exception exception = { };
6108 int err;
6109 do {
6110 err = nfs4_handle_exception(server,
6111 _nfs41_test_stateid(server, state),
6112 &exception);
6113 } while (exception.retry);
6114 return err;
6115}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006116
6117static int _nfs4_free_stateid(struct nfs_server *server, struct nfs4_state *state)
6118{
6119 int status;
6120 struct nfs41_free_stateid_args args = {
6121 .stateid = &state->stateid,
6122 };
6123 struct nfs41_free_stateid_res res;
6124 struct rpc_message msg = {
6125 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6126 .rpc_argp = &args,
6127 .rpc_resp = &res,
6128 };
6129
6130 args.seq_args.sa_session = res.seq_res.sr_session = NULL;
6131 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 0, 1);
6132 return status;
6133}
6134
6135static int nfs41_free_stateid(struct nfs_server *server, struct nfs4_state *state)
6136{
6137 struct nfs4_exception exception = { };
6138 int err;
6139 do {
6140 err = nfs4_handle_exception(server,
6141 _nfs4_free_stateid(server, state),
6142 &exception);
6143 } while (exception.retry);
6144 return err;
6145}
Andy Adamson557134a2009-04-01 09:21:53 -04006146#endif /* CONFIG_NFS_V4_1 */
6147
Andy Adamson591d71c2009-04-01 09:22:47 -04006148struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006149 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006150 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006151 .recover_open = nfs4_open_reclaim,
6152 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006153 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006154 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006155};
6156
Andy Adamson591d71c2009-04-01 09:22:47 -04006157#if defined(CONFIG_NFS_V4_1)
6158struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6159 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6160 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6161 .recover_open = nfs4_open_reclaim,
6162 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006163 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006164 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006165 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006166};
6167#endif /* CONFIG_NFS_V4_1 */
6168
6169struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006170 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006171 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006172 .recover_open = nfs4_open_expired,
6173 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006174 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006175 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006176};
6177
Andy Adamson591d71c2009-04-01 09:22:47 -04006178#if defined(CONFIG_NFS_V4_1)
6179struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6180 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6181 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006182 .recover_open = nfs41_open_expired,
6183 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006184 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006185 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006186};
6187#endif /* CONFIG_NFS_V4_1 */
6188
Benny Halevy29fba382009-04-01 09:22:44 -04006189struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6190 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006191 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006192 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006193};
6194
6195#if defined(CONFIG_NFS_V4_1)
6196struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
6197 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006198 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006199 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006200};
6201#endif
6202
Trond Myklebust97dc1352010-06-16 09:52:26 -04006203static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6204 .minor_version = 0,
6205 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04006206 .validate_stateid = nfs4_validate_delegation_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006207 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006208 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6209 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6210 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006211};
6212
6213#if defined(CONFIG_NFS_V4_1)
6214static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6215 .minor_version = 1,
6216 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04006217 .validate_stateid = nfs41_validate_delegation_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006218 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006219 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6220 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6221 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006222};
6223#endif
6224
Trond Myklebust97dc1352010-06-16 09:52:26 -04006225const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6226 [0] = &nfs_v4_0_minor_ops,
6227#if defined(CONFIG_NFS_V4_1)
6228 [1] = &nfs_v4_1_minor_ops,
6229#endif
6230};
6231
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006232static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006233 .permission = nfs_permission,
6234 .getattr = nfs_getattr,
6235 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006236 .getxattr = generic_getxattr,
6237 .setxattr = generic_setxattr,
6238 .listxattr = generic_listxattr,
6239 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006240};
6241
David Howells509de812006-08-22 20:06:11 -04006242const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006243 .version = 4, /* protocol version */
6244 .dentry_ops = &nfs4_dentry_operations,
6245 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006246 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006247 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006248 .getroot = nfs4_proc_get_root,
6249 .getattr = nfs4_proc_getattr,
6250 .setattr = nfs4_proc_setattr,
6251 .lookup = nfs4_proc_lookup,
6252 .access = nfs4_proc_access,
6253 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006254 .create = nfs4_proc_create,
6255 .remove = nfs4_proc_remove,
6256 .unlink_setup = nfs4_proc_unlink_setup,
6257 .unlink_done = nfs4_proc_unlink_done,
6258 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006259 .rename_setup = nfs4_proc_rename_setup,
6260 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006261 .link = nfs4_proc_link,
6262 .symlink = nfs4_proc_symlink,
6263 .mkdir = nfs4_proc_mkdir,
6264 .rmdir = nfs4_proc_remove,
6265 .readdir = nfs4_proc_readdir,
6266 .mknod = nfs4_proc_mknod,
6267 .statfs = nfs4_proc_statfs,
6268 .fsinfo = nfs4_proc_fsinfo,
6269 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006270 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006271 .decode_dirent = nfs4_decode_dirent,
6272 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05006273 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006274 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006275 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006276 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006277 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006278 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006279 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006280 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006281 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00006282 .init_client = nfs4_init_client,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006283 .secinfo = nfs4_proc_secinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006284};
6285
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006286static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6287 .prefix = XATTR_NAME_NFSV4_ACL,
6288 .list = nfs4_xattr_list_nfs4_acl,
6289 .get = nfs4_xattr_get_nfs4_acl,
6290 .set = nfs4_xattr_set_nfs4_acl,
6291};
6292
6293const struct xattr_handler *nfs4_xattr_handlers[] = {
6294 &nfs4_xattr_nfs4_acl_handler,
6295 NULL
6296};
6297
Linus Torvalds1da177e2005-04-16 15:20:36 -07006298/*
6299 * Local variables:
6300 * c-basic-offset: 8
6301 * End:
6302 */