blob: 3b1080118452ba8d3b5fc860d9d99db8b900d8ad [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 Halevydfb4f302010-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 Halevydfb4f302010-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 Halevydfb4f302010-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 Halevydfb4f302010-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 Halevydfb4f302010-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 Halevydfb4f302010-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 Halevydfb4f302010-09-24 09:17:01 -0400547 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400548 return 0;
549
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400550 tbl = &session->fc_slot_table;
551
552 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400553 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800554 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500555 /* The state manager will wait until the slot table is empty */
Andy Adamson05f0d232009-12-04 15:55:37 -0500556 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400557 spin_unlock(&tbl->slot_tbl_lock);
Andy Adamson0b1c8fc2011-11-09 13:58:26 -0500558 dprintk("%s session is draining\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500559 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400560 }
561
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800562 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
563 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
564 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
565 spin_unlock(&tbl->slot_tbl_lock);
566 dprintk("%s enforce FIFO order\n", __func__);
567 return -EAGAIN;
568 }
569
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800570 slotid = nfs4_find_slot(tbl);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400571 if (slotid == NFS4_MAX_SLOT_TABLE) {
572 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
573 spin_unlock(&tbl->slot_tbl_lock);
574 dprintk("<-- %s: no free slots\n", __func__);
575 return -EAGAIN;
576 }
577 spin_unlock(&tbl->slot_tbl_lock);
578
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800579 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400580 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400581 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400582 args->sa_slotid = slotid;
583 args->sa_cache_this = cache_reply;
584
585 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
586
Benny Halevy99fe60d2009-04-01 09:22:29 -0400587 res->sr_session = session;
Benny Halevydfb4f302010-09-24 09:17:01 -0400588 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400589 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400590 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400591 /*
592 * sr_status is only set in decode_sequence, and so will remain
593 * set to 1 if an rpc level failure occurs.
594 */
595 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400596 return 0;
597}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000598EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400599
Trond Myklebust035168a2010-06-16 09:52:26 -0400600int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400601 struct nfs4_sequence_args *args,
602 struct nfs4_sequence_res *res,
603 int cache_reply,
604 struct rpc_task *task)
605{
Trond Myklebust035168a2010-06-16 09:52:26 -0400606 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400607 int ret = 0;
608
Trond Myklebust035168a2010-06-16 09:52:26 -0400609 if (session == NULL) {
610 args->sa_session = NULL;
611 res->sr_session = NULL;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400612 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400613 }
614
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200615 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f302010-09-24 09:17:01 -0400616 __func__, session->clp, session, res->sr_slot ?
617 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400618
619 ret = nfs41_setup_sequence(session, args, res, cache_reply,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400620 task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400621out:
622 dprintk("<-- %s status=%d\n", __func__, ret);
623 return ret;
624}
625
626struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400627 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400628 struct nfs4_sequence_args *seq_args;
629 struct nfs4_sequence_res *seq_res;
630 int cache_reply;
631};
632
633static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
634{
635 struct nfs41_call_sync_data *data = calldata;
636
Trond Myklebust035168a2010-06-16 09:52:26 -0400637 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
638
639 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400640 data->seq_res, data->cache_reply, task))
641 return;
642 rpc_call_start(task);
643}
644
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800645static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
646{
647 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
648 nfs41_call_sync_prepare(task, calldata);
649}
650
Andy Adamson69ab40c2009-04-01 09:22:19 -0400651static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
652{
653 struct nfs41_call_sync_data *data = calldata;
654
Trond Myklebust14516c32010-07-31 14:29:06 -0400655 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400656}
657
Andy Adamsonce5039c2009-04-01 09:22:13 -0400658struct rpc_call_ops nfs41_call_sync_ops = {
659 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400660 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400661};
662
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800663struct rpc_call_ops nfs41_call_priv_sync_ops = {
664 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
665 .rpc_call_done = nfs41_call_sync_done,
666};
667
Bryan Schumaker7c513052011-03-24 17:12:24 +0000668static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
669 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400670 struct rpc_message *msg,
671 struct nfs4_sequence_args *args,
672 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800673 int cache_reply,
674 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400675{
676 int ret;
677 struct rpc_task *task;
678 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400679 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400680 .seq_args = args,
681 .seq_res = res,
682 .cache_reply = cache_reply,
683 };
684 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000685 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400686 .rpc_message = msg,
687 .callback_ops = &nfs41_call_sync_ops,
688 .callback_data = &data
689 };
690
Benny Halevydfb4f302010-09-24 09:17:01 -0400691 res->sr_slot = NULL;
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800692 if (privileged)
693 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400694 task = rpc_run_task(&task_setup);
695 if (IS_ERR(task))
696 ret = PTR_ERR(task);
697 else {
698 ret = task->tk_status;
699 rpc_put_task(task);
700 }
701 return ret;
702}
703
Bryan Schumaker7c513052011-03-24 17:12:24 +0000704int _nfs4_call_sync_session(struct rpc_clnt *clnt,
705 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400706 struct rpc_message *msg,
707 struct nfs4_sequence_args *args,
708 struct nfs4_sequence_res *res,
709 int cache_reply)
710{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000711 return nfs4_call_sync_sequence(clnt, server, msg, args, res, cache_reply, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400712}
713
Trond Myklebustdf896452010-06-16 09:52:26 -0400714#else
Trond Myklebust14516c32010-07-31 14:29:06 -0400715static int nfs4_sequence_done(struct rpc_task *task,
716 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400717{
Trond Myklebust14516c32010-07-31 14:29:06 -0400718 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400719}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400720#endif /* CONFIG_NFS_V4_1 */
721
Bryan Schumaker7c513052011-03-24 17:12:24 +0000722int _nfs4_call_sync(struct rpc_clnt *clnt,
723 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400724 struct rpc_message *msg,
725 struct nfs4_sequence_args *args,
726 struct nfs4_sequence_res *res,
727 int cache_reply)
728{
Benny Halevyf3752972009-04-01 09:22:04 -0400729 args->sa_session = res->sr_session = NULL;
Bryan Schumaker7c513052011-03-24 17:12:24 +0000730 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400731}
732
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000733static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000734int nfs4_call_sync(struct rpc_clnt *clnt,
735 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000736 struct rpc_message *msg,
737 struct nfs4_sequence_args *args,
738 struct nfs4_sequence_res *res,
739 int cache_reply)
740{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000741 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
742 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000743}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400744
Trond Myklebust38478b22006-05-25 01:40:57 -0400745static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
Trond Myklebust38478b22006-05-25 01:40:57 -0400747 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Trond Myklebust38478b22006-05-25 01:40:57 -0400749 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400750 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700751 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400752 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700753 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400754 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100757struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400758 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100759 struct nfs_openargs o_arg;
760 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100761 struct nfs_open_confirmargs c_arg;
762 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100763 struct nfs_fattr f_attr;
764 struct nfs_fattr dir_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100765 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400766 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100767 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400768 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100769 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100770 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400771 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100772 int rpc_status;
773 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100774};
775
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400776
777static void nfs4_init_opendata_res(struct nfs4_opendata *p)
778{
779 p->o_res.f_attr = &p->f_attr;
780 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400781 p->o_res.seqid = p->o_arg.seqid;
782 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400783 p->o_res.server = p->o_arg.server;
784 nfs_fattr_init(&p->f_attr);
785 nfs_fattr_init(&p->dir_attr);
786}
787
Al Viro82a2c1b2011-06-22 18:30:55 -0400788static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500789 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400790 const struct iattr *attrs,
791 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100792{
Al Viro82a2c1b2011-06-22 18:30:55 -0400793 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100794 struct inode *dir = parent->d_inode;
795 struct nfs_server *server = NFS_SERVER(dir);
796 struct nfs4_opendata *p;
797
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400798 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100799 if (p == NULL)
800 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400801 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100802 if (p->o_arg.seqid == NULL)
803 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400804 nfs_sb_active(dentry->d_sb);
805 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100806 p->dir = parent;
807 p->owner = sp;
808 atomic_inc(&sp->so_count);
809 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500810 p->o_arg.open_flags = flags;
811 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400812 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400813 p->o_arg.id = sp->so_owner_id.id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400814 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100815 p->o_arg.server = server;
816 p->o_arg.bitmask = server->attr_bitmask;
817 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400818 if (flags & O_CREAT) {
819 u32 *s;
820
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100821 p->o_arg.u.attrs = &p->attrs;
822 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400823 s = (u32 *) p->o_arg.u.verifier.data;
824 s[0] = jiffies;
825 s[1] = current->pid;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100826 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100827 p->c_arg.fh = &p->o_res.fh;
828 p->c_arg.stateid = &p->o_res.stateid;
829 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400830 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400831 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100832 return p;
833err_free:
834 kfree(p);
835err:
836 dput(parent);
837 return NULL;
838}
839
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400840static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100841{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400842 struct nfs4_opendata *p = container_of(kref,
843 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400844 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400845
846 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400847 if (p->state != NULL)
848 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400849 nfs4_put_state_owner(p->owner);
850 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400851 dput(p->dentry);
852 nfs_sb_deactive(sb);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400853 kfree(p);
854}
855
856static void nfs4_opendata_put(struct nfs4_opendata *p)
857{
858 if (p != NULL)
859 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100860}
861
Trond Myklebust06f814a2006-01-03 09:55:07 +0100862static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
863{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100864 int ret;
865
Trond Myklebust06f814a2006-01-03 09:55:07 +0100866 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100867 return ret;
868}
869
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500870static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400871{
872 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500873
874 if (open_mode & O_EXCL)
875 goto out;
876 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400877 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500878 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
879 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400880 break;
881 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500882 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
883 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400884 break;
885 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500886 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
887 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400888 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500889out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400890 return ret;
891}
892
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500893static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400894{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500895 if (delegation == NULL)
896 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500897 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400898 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500899 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400900 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500901 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400902 return 1;
903}
904
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500905static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100906{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500907 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100908 case FMODE_WRITE:
909 state->n_wronly++;
910 break;
911 case FMODE_READ:
912 state->n_rdonly++;
913 break;
914 case FMODE_READ|FMODE_WRITE:
915 state->n_rdwr++;
916 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500917 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100918}
919
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500920static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400921{
922 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
923 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
924 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500925 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400926 case FMODE_READ:
927 set_bit(NFS_O_RDONLY_STATE, &state->flags);
928 break;
929 case FMODE_WRITE:
930 set_bit(NFS_O_WRONLY_STATE, &state->flags);
931 break;
932 case FMODE_READ|FMODE_WRITE:
933 set_bit(NFS_O_RDWR_STATE, &state->flags);
934 }
935}
936
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500937static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400938{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400939 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500940 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400941 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400942}
943
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500944static void __update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, const nfs4_stateid *deleg_stateid, fmode_t fmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400946 /*
947 * Protect the call to nfs4_state_set_mode_locked and
948 * serialise the stateid update
949 */
950 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400951 if (deleg_stateid != NULL) {
952 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
953 set_bit(NFS_DELEGATED_STATE, &state->flags);
954 }
955 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500956 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400957 write_sequnlock(&state->seqlock);
958 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500959 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700960 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961}
962
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500963static int update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *delegation, fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500964{
965 struct nfs_inode *nfsi = NFS_I(state->inode);
966 struct nfs_delegation *deleg_cur;
967 int ret = 0;
968
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500969 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500970
971 rcu_read_lock();
972 deleg_cur = rcu_dereference(nfsi->delegation);
973 if (deleg_cur == NULL)
974 goto no_delegation;
975
976 spin_lock(&deleg_cur->lock);
977 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500978 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500979 goto no_delegation_unlock;
980
981 if (delegation == NULL)
982 delegation = &deleg_cur->stateid;
983 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
984 goto no_delegation_unlock;
985
Trond Myklebustb7391f42008-12-23 15:21:52 -0500986 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500987 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500988 ret = 1;
989no_delegation_unlock:
990 spin_unlock(&deleg_cur->lock);
991no_delegation:
992 rcu_read_unlock();
993
994 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500995 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500996 ret = 1;
997 }
998
999 return ret;
1000}
1001
1002
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001003static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001004{
1005 struct nfs_delegation *delegation;
1006
1007 rcu_read_lock();
1008 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001009 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001010 rcu_read_unlock();
1011 return;
1012 }
1013 rcu_read_unlock();
1014 nfs_inode_return_delegation(inode);
1015}
1016
Trond Myklebust6ee41262007-07-08 14:11:36 -04001017static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001018{
1019 struct nfs4_state *state = opendata->state;
1020 struct nfs_inode *nfsi = NFS_I(state->inode);
1021 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001022 int open_mode = opendata->o_arg.open_flags & O_EXCL;
1023 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001024 nfs4_stateid stateid;
1025 int ret = -EAGAIN;
1026
Trond Myklebustaac00a82007-07-05 19:02:21 -04001027 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001028 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001029 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001030 if (can_open_cached(state, fmode, open_mode)) {
1031 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001032 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001033 goto out_return_state;
1034 }
1035 spin_unlock(&state->owner->so_lock);
1036 }
Trond Myklebust34310432008-12-23 15:21:38 -05001037 rcu_read_lock();
1038 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001039 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001040 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001041 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001042 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001043 /* Save the delegation */
1044 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1045 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001046 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001047 if (ret != 0)
1048 goto out;
1049 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001050
1051 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001052 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001053 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001054 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001055out:
1056 return ERR_PTR(ret);
1057out_return_state:
1058 atomic_inc(&state->count);
1059 return state;
1060}
1061
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001062static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1063{
1064 struct inode *inode;
1065 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001066 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001067 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001068
Trond Myklebustaac00a82007-07-05 19:02:21 -04001069 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001070 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001071 goto out;
1072 }
1073
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001074 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001075 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001076 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001077 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001078 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001079 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001080 goto err;
1081 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001082 state = nfs4_get_open_state(inode, data->owner);
1083 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001084 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001085 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001086 int delegation_flags = 0;
1087
Trond Myklebust003707c2007-07-05 18:07:55 -04001088 rcu_read_lock();
1089 delegation = rcu_dereference(NFS_I(inode)->delegation);
1090 if (delegation)
1091 delegation_flags = delegation->flags;
1092 rcu_read_unlock();
Trond Myklebust652f89f2011-12-09 19:05:58 -05001093 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1094 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1095 "returning a delegation for "
1096 "OPEN(CLAIM_DELEGATE_CUR)\n",
1097 NFS_CLIENT(inode)->cl_server);
1098 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001099 nfs_inode_set_delegation(state->inode,
1100 data->owner->so_cred,
1101 &data->o_res);
1102 else
1103 nfs_inode_reclaim_delegation(state->inode,
1104 data->owner->so_cred,
1105 &data->o_res);
1106 }
Trond Myklebust34310432008-12-23 15:21:38 -05001107
1108 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001109 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001110 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001111out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001112 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001113err_put_inode:
1114 iput(inode);
1115err:
1116 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001117}
1118
Trond Myklebust864472e2006-01-03 09:55:15 +01001119static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1120{
1121 struct nfs_inode *nfsi = NFS_I(state->inode);
1122 struct nfs_open_context *ctx;
1123
1124 spin_lock(&state->inode->i_lock);
1125 list_for_each_entry(ctx, &nfsi->open_files, list) {
1126 if (ctx->state != state)
1127 continue;
1128 get_nfs_open_context(ctx);
1129 spin_unlock(&state->inode->i_lock);
1130 return ctx;
1131 }
1132 spin_unlock(&state->inode->i_lock);
1133 return ERR_PTR(-ENOENT);
1134}
1135
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001136static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1137{
1138 struct nfs4_opendata *opendata;
1139
Al Viro3d4ff432011-06-22 18:40:12 -04001140 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001141 if (opendata == NULL)
1142 return ERR_PTR(-ENOMEM);
1143 opendata->state = state;
1144 atomic_inc(&state->count);
1145 return opendata;
1146}
1147
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001148static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001149{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001150 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001151 int ret;
1152
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001153 opendata->o_arg.open_flags = 0;
1154 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001155 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1156 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1157 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001158 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001159 if (ret != 0)
1160 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001161 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001162 if (IS_ERR(newstate))
1163 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001164 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001165 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001166 return 0;
1167}
1168
1169static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1170{
Trond Myklebust864472e2006-01-03 09:55:15 +01001171 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001172 int ret;
1173
1174 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001175 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001176 smp_rmb();
1177 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001178 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001179 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001180 if (ret != 0)
1181 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001182 if (newstate != state)
1183 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001184 }
1185 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001186 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001187 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001188 if (ret != 0)
1189 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001190 if (newstate != state)
1191 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001192 }
1193 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001194 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001195 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001196 if (ret != 0)
1197 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001198 if (newstate != state)
1199 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001200 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001201 /*
1202 * We may have performed cached opens for all three recoveries.
1203 * Check if we need to update the current stateid.
1204 */
1205 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1206 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001207 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001208 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1209 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001210 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001211 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001212 return 0;
1213}
1214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215/*
1216 * OPEN_RECLAIM:
1217 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001219static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001221 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001222 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001223 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 int status;
1225
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001226 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1227 if (IS_ERR(opendata))
1228 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001229 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1230 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001231 rcu_read_lock();
1232 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001233 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001234 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001235 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001236 opendata->o_arg.u.delegation_type = delegation_type;
1237 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001238 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return status;
1240}
1241
Trond Myklebust539cd032007-06-05 11:46:42 -04001242static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
1244 struct nfs_server *server = NFS_SERVER(state->inode);
1245 struct nfs4_exception exception = { };
1246 int err;
1247 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001248 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001249 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001250 break;
1251 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 } while (exception.retry);
1253 return err;
1254}
1255
Trond Myklebust864472e2006-01-03 09:55:15 +01001256static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1257{
1258 struct nfs_open_context *ctx;
1259 int ret;
1260
1261 ctx = nfs4_state_find_open_context(state);
1262 if (IS_ERR(ctx))
1263 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001264 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001265 put_nfs_open_context(ctx);
1266 return ret;
1267}
1268
Trond Myklebust13437e12007-07-06 15:10:43 -04001269static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001271 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001272 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001274 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1275 if (IS_ERR(opendata))
1276 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001277 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001278 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001279 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001280 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001281 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001282 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283}
1284
Trond Myklebust13437e12007-07-06 15:10:43 -04001285int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
1287 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001288 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 int err;
1290 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001291 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 switch (err) {
1293 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001294 case -ENOENT:
1295 case -ESTALE:
1296 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001297 case -NFS4ERR_BADSESSION:
1298 case -NFS4ERR_BADSLOT:
1299 case -NFS4ERR_BAD_HIGH_SLOT:
1300 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1301 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001302 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001303 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 case -NFS4ERR_STALE_CLIENTID:
1305 case -NFS4ERR_STALE_STATEID:
1306 case -NFS4ERR_EXPIRED:
1307 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001308 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001309 goto out;
1310 case -ERESTARTSYS:
1311 /*
1312 * The show must go on: exit, but mark the
1313 * stateid as needing recovery.
1314 */
1315 case -NFS4ERR_ADMIN_REVOKED:
1316 case -NFS4ERR_BAD_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001317 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001318 case -EKEYEXPIRED:
1319 /*
1320 * User RPCSEC_GSS context has expired.
1321 * We cannot recover this stateid now, so
1322 * skip it and allow recovery thread to
1323 * proceed.
1324 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001325 case -ENOMEM:
1326 err = 0;
1327 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 }
1329 err = nfs4_handle_exception(server, err, &exception);
1330 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001331out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 return err;
1333}
1334
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001335static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1336{
1337 struct nfs4_opendata *data = calldata;
1338
1339 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001340 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001341 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1342 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001343 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001344 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001345 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001346 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001347}
1348
1349static void nfs4_open_confirm_release(void *calldata)
1350{
1351 struct nfs4_opendata *data = calldata;
1352 struct nfs4_state *state = NULL;
1353
1354 /* If this request hasn't been cancelled, do nothing */
1355 if (data->cancelled == 0)
1356 goto out_free;
1357 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001358 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001359 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001360 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001361 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001362 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001363out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001364 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001365}
1366
1367static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001368 .rpc_call_done = nfs4_open_confirm_done,
1369 .rpc_release = nfs4_open_confirm_release,
1370};
1371
1372/*
1373 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1374 */
1375static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1376{
1377 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1378 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001379 struct rpc_message msg = {
1380 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1381 .rpc_argp = &data->c_arg,
1382 .rpc_resp = &data->c_res,
1383 .rpc_cred = data->owner->so_cred,
1384 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001385 struct rpc_task_setup task_setup_data = {
1386 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001387 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001388 .callback_ops = &nfs4_open_confirm_ops,
1389 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001390 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001391 .flags = RPC_TASK_ASYNC,
1392 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 int status;
1394
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001395 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001396 data->rpc_done = 0;
1397 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001398 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001399 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001400 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001401 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001402 status = nfs4_wait_for_completion_rpc_task(task);
1403 if (status != 0) {
1404 data->cancelled = 1;
1405 smp_wmb();
1406 } else
1407 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001408 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 return status;
1410}
1411
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001412static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001414 struct nfs4_opendata *data = calldata;
1415 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001416
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001417 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1418 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001419 /*
1420 * Check if we still need to send an OPEN call, or if we can use
1421 * a delegation instead.
1422 */
1423 if (data->state != NULL) {
1424 struct nfs_delegation *delegation;
1425
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001426 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001427 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001428 rcu_read_lock();
1429 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001430 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1431 can_open_delegated(delegation, data->o_arg.fmode))
1432 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001433 rcu_read_unlock();
1434 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001435 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001436 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001437 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001438 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001439 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001440 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1441 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001442 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001443 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001444 &data->o_arg.seq_args,
1445 &data->o_res.seq_res, 1, task))
1446 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001447 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001448 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001449unlock_no_action:
1450 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001451out_no_action:
1452 task->tk_action = NULL;
1453
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001454}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001456static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1457{
1458 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1459 nfs4_open_prepare(task, calldata);
1460}
1461
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001462static void nfs4_open_done(struct rpc_task *task, void *calldata)
1463{
1464 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001466 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001467
Trond Myklebust14516c32010-07-31 14:29:06 -04001468 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1469 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001470
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001471 if (task->tk_status == 0) {
1472 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001473 case S_IFREG:
1474 break;
1475 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001476 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001477 break;
1478 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001479 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001480 break;
1481 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001482 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001483 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001484 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001485 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1486 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001487 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001488 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001489}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001490
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001491static void nfs4_open_release(void *calldata)
1492{
1493 struct nfs4_opendata *data = calldata;
1494 struct nfs4_state *state = NULL;
1495
1496 /* If this request hasn't been cancelled, do nothing */
1497 if (data->cancelled == 0)
1498 goto out_free;
1499 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001500 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001501 goto out_free;
1502 /* In case we need an open_confirm, no cleanup! */
1503 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1504 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001505 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001506 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001507 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001508out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001509 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001510}
1511
1512static const struct rpc_call_ops nfs4_open_ops = {
1513 .rpc_call_prepare = nfs4_open_prepare,
1514 .rpc_call_done = nfs4_open_done,
1515 .rpc_release = nfs4_open_release,
1516};
1517
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001518static const struct rpc_call_ops nfs4_recover_open_ops = {
1519 .rpc_call_prepare = nfs4_recover_open_prepare,
1520 .rpc_call_done = nfs4_open_done,
1521 .rpc_release = nfs4_open_release,
1522};
1523
1524static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001525{
1526 struct inode *dir = data->dir->d_inode;
1527 struct nfs_server *server = NFS_SERVER(dir);
1528 struct nfs_openargs *o_arg = &data->o_arg;
1529 struct nfs_openres *o_res = &data->o_res;
1530 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001531 struct rpc_message msg = {
1532 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1533 .rpc_argp = o_arg,
1534 .rpc_resp = o_res,
1535 .rpc_cred = data->owner->so_cred,
1536 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001537 struct rpc_task_setup task_setup_data = {
1538 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001539 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001540 .callback_ops = &nfs4_open_ops,
1541 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001542 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001543 .flags = RPC_TASK_ASYNC,
1544 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001545 int status;
1546
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001547 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001548 data->rpc_done = 0;
1549 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001550 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001551 if (isrecover)
1552 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001553 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001554 if (IS_ERR(task))
1555 return PTR_ERR(task);
1556 status = nfs4_wait_for_completion_rpc_task(task);
1557 if (status != 0) {
1558 data->cancelled = 1;
1559 smp_wmb();
1560 } else
1561 status = data->rpc_status;
1562 rpc_put_task(task);
1563
1564 return status;
1565}
1566
1567static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1568{
1569 struct inode *dir = data->dir->d_inode;
1570 struct nfs_openres *o_res = &data->o_res;
1571 int status;
1572
1573 status = nfs4_run_open_task(data, 1);
1574 if (status != 0 || !data->rpc_done)
1575 return status;
1576
1577 nfs_refresh_inode(dir, o_res->dir_attr);
1578
1579 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1580 status = _nfs4_proc_open_confirm(data);
1581 if (status != 0)
1582 return status;
1583 }
1584
1585 return status;
1586}
1587
1588/*
1589 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1590 */
1591static int _nfs4_proc_open(struct nfs4_opendata *data)
1592{
1593 struct inode *dir = data->dir->d_inode;
1594 struct nfs_server *server = NFS_SERVER(dir);
1595 struct nfs_openargs *o_arg = &data->o_arg;
1596 struct nfs_openres *o_res = &data->o_res;
1597 int status;
1598
1599 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001600 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001601 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001602 if (status != 0) {
1603 if (status == -NFS4ERR_BADNAME &&
1604 !(o_arg->open_flags & O_CREAT))
1605 return -ENOENT;
1606 return status;
1607 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001608
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001609 if (o_arg->open_flags & O_CREAT) {
1610 update_changeattr(dir, &o_res->cinfo);
1611 nfs_post_op_update_inode(dir, o_res->dir_attr);
1612 } else
1613 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001614 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1615 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001617 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001619 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 }
1621 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001622 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001623 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624}
1625
Andy Adamsond83217c2011-03-01 01:34:17 +00001626static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001627{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001628 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001629 int ret;
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001630
Trond Myklebusta78cb572009-08-09 15:06:19 -04001631 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001632 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001633 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001634 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001635 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1636 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001637 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001638 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001639 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001640 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001641 return ret;
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001642}
1643
Andy Adamsond83217c2011-03-01 01:34:17 +00001644static int nfs4_recover_expired_lease(struct nfs_server *server)
1645{
1646 return nfs4_client_recover_expired_lease(server->nfs_client);
1647}
1648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649/*
1650 * OPEN_EXPIRED:
1651 * reclaim state on the server after a network partition.
1652 * Assumes caller holds the appropriate lock
1653 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001654static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001656 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001657 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001659 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1660 if (IS_ERR(opendata))
1661 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001662 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001663 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001664 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001665 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001666 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667}
1668
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001669static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001670{
Trond Myklebust539cd032007-06-05 11:46:42 -04001671 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001672 struct nfs4_exception exception = { };
1673 int err;
1674
1675 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001676 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001677 switch (err) {
1678 default:
1679 goto out;
1680 case -NFS4ERR_GRACE:
1681 case -NFS4ERR_DELAY:
1682 nfs4_handle_exception(server, err, &exception);
1683 err = 0;
1684 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001685 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001686out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001687 return err;
1688}
1689
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1691{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001693 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Trond Myklebust864472e2006-01-03 09:55:15 +01001695 ctx = nfs4_state_find_open_context(state);
1696 if (IS_ERR(ctx))
1697 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001698 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001699 put_nfs_open_context(ctx);
1700 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701}
1702
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001703#if defined(CONFIG_NFS_V4_1)
1704static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1705{
1706 int status;
1707 struct nfs_server *server = NFS_SERVER(state->inode);
1708
1709 status = nfs41_test_stateid(server, state);
1710 if (status == NFS_OK)
1711 return 0;
1712 nfs41_free_stateid(server, state);
1713 return nfs4_open_expired(sp, state);
1714}
1715#endif
1716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001718 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1719 * fields corresponding to attributes that were used to store the verifier.
1720 * Make sure we clobber those fields in the later setattr call
1721 */
1722static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1723{
1724 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1725 !(sattr->ia_valid & ATTR_ATIME_SET))
1726 sattr->ia_valid |= ATTR_ATIME;
1727
1728 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1729 !(sattr->ia_valid & ATTR_MTIME_SET))
1730 sattr->ia_valid |= ATTR_MTIME;
1731}
1732
1733/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001734 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 */
Al Viro82a2c1b2011-06-22 18:30:55 -04001736static int _nfs4_do_open(struct inode *dir, struct dentry *dentry, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
1738 struct nfs4_state_owner *sp;
1739 struct nfs4_state *state = NULL;
1740 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001741 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001742 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
1744 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 status = -ENOMEM;
1746 if (!(sp = nfs4_get_state_owner(server, cred))) {
1747 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1748 goto out_err;
1749 }
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001750 status = nfs4_recover_expired_lease(server);
1751 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001752 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001753 if (dentry->d_inode != NULL)
1754 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d9714a2006-01-03 09:55:24 +01001755 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001756 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001757 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001758 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
Al Viro82a2c1b2011-06-22 18:30:55 -04001760 if (dentry->d_inode != NULL)
1761 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001762
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001763 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001765 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001767 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001768 status = PTR_ERR(state);
1769 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001770 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001771 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001772 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001773
1774 if (opendata->o_arg.open_flags & O_EXCL) {
1775 nfs4_exclusive_attrset(opendata, sattr);
1776
1777 nfs_fattr_init(opendata->o_res.f_attr);
1778 status = nfs4_do_setattr(state->inode, cred,
1779 opendata->o_res.f_attr, sattr,
1780 state);
1781 if (status == 0)
1782 nfs_setattr_update_inode(state->inode, sattr);
1783 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1784 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001785 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 *res = state;
1788 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001789err_opendata_put:
1790 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001791err_put_state_owner:
1792 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 *res = NULL;
1795 return status;
1796}
1797
1798
Al Viro82a2c1b2011-06-22 18:30:55 -04001799static struct nfs4_state *nfs4_do_open(struct inode *dir, struct dentry *dentry, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800{
1801 struct nfs4_exception exception = { };
1802 struct nfs4_state *res;
1803 int status;
1804
1805 do {
Al Viro82a2c1b2011-06-22 18:30:55 -04001806 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 if (status == 0)
1808 break;
1809 /* NOTE: BAD_SEQID means the server and client disagree about the
1810 * book-keeping w.r.t. state-changing operations
1811 * (OPEN/CLOSE/LOCK/LOCKU...)
1812 * It is actually a sign of a bug on the client or on the server.
1813 *
1814 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001815 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 * have unhashed the old state_owner for us, and that we can
1817 * therefore safely retry using a new one. We should still warn
1818 * the user though...
1819 */
1820 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001821 printk(KERN_WARNING "NFS: v4 server %s "
1822 " returned a bad sequence-id error!\n",
1823 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 exception.retry = 1;
1825 continue;
1826 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001827 /*
1828 * BAD_STATEID on OPEN means that the server cancelled our
1829 * state before it received the OPEN_CONFIRM.
1830 * Recover by retrying the request as per the discussion
1831 * on Page 181 of RFC3530.
1832 */
1833 if (status == -NFS4ERR_BAD_STATEID) {
1834 exception.retry = 1;
1835 continue;
1836 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001837 if (status == -EAGAIN) {
1838 /* We must have found a delegation */
1839 exception.retry = 1;
1840 continue;
1841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1843 status, &exception));
1844 } while (exception.retry);
1845 return res;
1846}
1847
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001848static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1849 struct nfs_fattr *fattr, struct iattr *sattr,
1850 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001852 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001854 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 .iap = sattr,
1856 .server = server,
1857 .bitmask = server->attr_bitmask,
1858 };
1859 struct nfs_setattrres res = {
1860 .fattr = fattr,
1861 .server = server,
1862 };
1863 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001864 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1865 .rpc_argp = &arg,
1866 .rpc_resp = &res,
1867 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001869 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001870 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
Trond Myklebust0e574af2005-10-27 22:12:38 -04001872 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001874 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1875 /* Use that stateid */
1876 } else if (state != NULL) {
Trond Myklebust77041ed2010-07-01 12:49:11 -04001877 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001878 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1880
Bryan Schumaker7c513052011-03-24 17:12:24 +00001881 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001882 if (status == 0 && state != NULL)
1883 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001884 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885}
1886
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001887static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1888 struct nfs_fattr *fattr, struct iattr *sattr,
1889 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001891 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 struct nfs4_exception exception = { };
1893 int err;
1894 do {
1895 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001896 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 &exception);
1898 } while (exception.retry);
1899 return err;
1900}
1901
1902struct nfs4_closedata {
1903 struct inode *inode;
1904 struct nfs4_state *state;
1905 struct nfs_closeargs arg;
1906 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001907 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001908 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00001909 bool roc;
1910 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911};
1912
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001913static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001914{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001915 struct nfs4_closedata *calldata = data;
1916 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04001917 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07001918
Fred Isamanf7e89172011-01-06 11:36:32 +00001919 if (calldata->roc)
1920 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001921 nfs4_put_open_state(calldata->state);
1922 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001923 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04001924 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07001925 kfree(calldata);
1926}
1927
Trond Myklebust88069f72009-12-08 08:33:16 -05001928static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1929 fmode_t fmode)
1930{
1931 spin_lock(&state->owner->so_lock);
1932 if (!(fmode & FMODE_READ))
1933 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1934 if (!(fmode & FMODE_WRITE))
1935 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1936 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1937 spin_unlock(&state->owner->so_lock);
1938}
1939
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001940static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001942 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 struct nfs_server *server = NFS_SERVER(calldata->inode);
1945
Trond Myklebust14516c32010-07-31 14:29:06 -04001946 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1947 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 /* hmm. we are done with the inode, and in the process of freeing
1949 * the state_owner. we keep this around to process errors
1950 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 switch (task->tk_status) {
1952 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00001953 if (calldata->roc)
1954 pnfs_roc_set_barrier(state->inode,
1955 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04001956 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001957 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001958 nfs4_close_clear_stateid_flags(state,
1959 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 break;
1961 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001962 case -NFS4ERR_OLD_STATEID:
1963 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001965 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001966 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001968 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1969 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001971 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001972 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973}
1974
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001975static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001977 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001978 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001979 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001980
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001981 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001982 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001983
Trond Myklebust88069f72009-12-08 08:33:16 -05001984 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1985 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001986 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001987 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001988 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001989 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001990 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1991 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1992 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001993 }
1994 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001995 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1996 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1997 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001998 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001999 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002000 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002001
2002 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002003 /* Note: exit _without_ calling nfs4_close_done */
2004 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07002005 return;
2006 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002007
Fred Isamanf7e89172011-01-06 11:36:32 +00002008 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002009 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002010 if (calldata->roc &&
2011 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2012 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2013 task, NULL);
2014 return;
2015 }
2016 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002017
Trond Myklebust516a6af2005-10-27 22:12:41 -04002018 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002019 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002020 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Andy Adamson19ddab02009-04-01 09:22:20 -04002021 &calldata->arg.seq_args, &calldata->res.seq_res,
2022 1, task))
2023 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002024 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025}
2026
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002027static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002028 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002029 .rpc_call_done = nfs4_close_done,
2030 .rpc_release = nfs4_free_closedata,
2031};
2032
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033/*
2034 * It is possible for data to be read/written from a mem-mapped file
2035 * after the sys_close call (which hits the vfs layer as a flush).
2036 * This means that we can't safely call nfsv4 close on a file until
2037 * the inode is cleared. This in turn means that we are not good
2038 * NFSv4 citizens - we do not indicate to the server to update the file's
2039 * share state even when we are done with one of the three share
2040 * stateid's in the inode.
2041 *
2042 * NOTE: Caller must be holding the sp->so_owner semaphore!
2043 */
Al Viro643168c2011-06-22 18:20:23 -04002044int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002046 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002048 struct nfs4_state_owner *sp = state->owner;
2049 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002050 struct rpc_message msg = {
2051 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2052 .rpc_cred = state->owner->so_cred,
2053 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002054 struct rpc_task_setup task_setup_data = {
2055 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002056 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002057 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002058 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002059 .flags = RPC_TASK_ASYNC,
2060 };
Trond Myklebust95121352005-10-18 14:20:12 -07002061 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002063 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002065 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002066 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002068 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002069 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002071 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002072 if (calldata->arg.seqid == NULL)
2073 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002074 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002075 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002076 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002077 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002078 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002079 calldata->roc = roc;
Al Viro643168c2011-06-22 18:20:23 -04002080 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002081
Trond Myklebust1174dd12010-12-21 10:52:24 -05002082 msg.rpc_argp = &calldata->arg;
2083 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002084 task_setup_data.callback_data = calldata;
2085 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002086 if (IS_ERR(task))
2087 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002088 status = 0;
2089 if (wait)
2090 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002091 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002092 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002093out_free_calldata:
2094 kfree(calldata);
2095out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002096 if (roc)
2097 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002098 nfs4_put_open_state(state);
2099 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002100 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101}
2102
Trond Myklebust2b484292010-09-17 10:56:51 -04002103static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002104nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 struct nfs4_state *state;
2107
Trond Myklebust565277f2007-10-15 18:17:53 -04002108 /* Protect against concurrent sillydeletes */
Al Viro3d4ff432011-06-22 18:40:12 -04002109 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002110 if (IS_ERR(state))
2111 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002112 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002113 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114}
2115
Trond Myklebust1185a552009-12-03 15:54:02 -05002116static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002117{
2118 if (ctx->state == NULL)
2119 return;
2120 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002121 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002122 else
Al Viro643168c2011-06-22 18:20:23 -04002123 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002124}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
2126static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2127{
Benny Halevy43652ad2009-04-01 09:21:54 -04002128 struct nfs4_server_caps_arg args = {
2129 .fhandle = fhandle,
2130 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 struct nfs4_server_caps_res res = {};
2132 struct rpc_message msg = {
2133 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002134 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 .rpc_resp = &res,
2136 };
2137 int status;
2138
Bryan Schumaker7c513052011-03-24 17:12:24 +00002139 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 if (status == 0) {
2141 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04002142 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2143 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2144 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2145 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2146 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2148 server->caps |= NFS_CAP_ACLS;
2149 if (res.has_links != 0)
2150 server->caps |= NFS_CAP_HARDLINKS;
2151 if (res.has_symlinks != 0)
2152 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04002153 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2154 server->caps |= NFS_CAP_FILEID;
2155 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2156 server->caps |= NFS_CAP_MODE;
2157 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2158 server->caps |= NFS_CAP_NLINK;
2159 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2160 server->caps |= NFS_CAP_OWNER;
2161 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2162 server->caps |= NFS_CAP_OWNER_GROUP;
2163 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2164 server->caps |= NFS_CAP_ATIME;
2165 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2166 server->caps |= NFS_CAP_CTIME;
2167 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2168 server->caps |= NFS_CAP_MTIME;
2169
Trond Myklebusta65318b2009-03-11 14:10:28 -04002170 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2171 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2172 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 server->acl_bitmask = res.acl_bitmask;
2174 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002175
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 return status;
2177}
2178
Trond Myklebust55a97592006-06-09 09:34:19 -04002179int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180{
2181 struct nfs4_exception exception = { };
2182 int err;
2183 do {
2184 err = nfs4_handle_exception(server,
2185 _nfs4_server_capabilities(server, fhandle),
2186 &exception);
2187 } while (exception.retry);
2188 return err;
2189}
2190
2191static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2192 struct nfs_fsinfo *info)
2193{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 struct nfs4_lookup_root_arg args = {
2195 .bitmask = nfs4_fattr_bitmap,
2196 };
2197 struct nfs4_lookup_res res = {
2198 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002199 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 .fh = fhandle,
2201 };
2202 struct rpc_message msg = {
2203 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2204 .rpc_argp = &args,
2205 .rpc_resp = &res,
2206 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002207
Trond Myklebust0e574af2005-10-27 22:12:38 -04002208 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002209 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210}
2211
2212static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2213 struct nfs_fsinfo *info)
2214{
2215 struct nfs4_exception exception = { };
2216 int err;
2217 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002218 err = _nfs4_lookup_root(server, fhandle, info);
2219 switch (err) {
2220 case 0:
2221 case -NFS4ERR_WRONGSEC:
2222 break;
2223 default:
2224 err = nfs4_handle_exception(server, err, &exception);
2225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 } while (exception.retry);
2227 return err;
2228}
2229
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002230static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2231 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2232{
2233 struct rpc_auth *auth;
2234 int ret;
2235
2236 auth = rpcauth_create(flavor, server->client);
2237 if (!auth) {
2238 ret = -EIO;
2239 goto out;
2240 }
2241 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002242out:
2243 return ret;
2244}
2245
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002246static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002247 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002249 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002250 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002252 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2253 flav_array[len] = RPC_AUTH_NULL;
2254 len += 1;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002255
2256 for (i = 0; i < len; i++) {
2257 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002258 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002259 continue;
2260 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002261 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002262 /*
2263 * -EACCESS could mean that the user doesn't have correct permissions
2264 * to access the mount. It could also mean that we tried to mount
2265 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2266 * existing mount programs don't handle -EACCES very well so it should
2267 * be mapped to -EPERM instead.
2268 */
2269 if (status == -EACCES)
2270 status = -EPERM;
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002271 return status;
2272}
2273
2274/*
2275 * get the file handle for the "/" directory on the server
2276 */
2277static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2278 struct nfs_fsinfo *info)
2279{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002280 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16d2011-04-13 14:31:30 -04002281 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002282 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2283 /*
2284 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2285 * by nfs4_map_errors() as this function exits.
2286 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002287 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 if (status == 0)
2289 status = nfs4_server_capabilities(server, fhandle);
2290 if (status == 0)
2291 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002292 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293}
2294
Andy Adamson533eb462011-06-13 18:25:56 -04002295static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002296/*
2297 * Get locations and (maybe) other attributes of a referral.
2298 * Note that we'll actually follow the referral later when
2299 * we detect fsid mismatch in inode revalidation
2300 */
Andy Adamson533eb462011-06-13 18:25:56 -04002301static int nfs4_get_referral(struct inode *dir, const struct qstr *name,
2302 struct nfs_fattr *fattr, struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002303{
2304 int status = -ENOMEM;
2305 struct page *page = NULL;
2306 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002307
2308 page = alloc_page(GFP_KERNEL);
2309 if (page == NULL)
2310 goto out;
2311 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2312 if (locations == NULL)
2313 goto out;
2314
Trond Myklebustc228fd32007-01-13 02:28:11 -05002315 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002316 if (status != 0)
2317 goto out;
2318 /* Make sure server returned a different fsid for the referral */
2319 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002320 dprintk("%s: server did not return a different fsid for"
2321 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002322 status = -EIO;
2323 goto out;
2324 }
Andy Adamson533eb462011-06-13 18:25:56 -04002325 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2326 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002327
Andy Adamson533eb462011-06-13 18:25:56 -04002328 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002329 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002330 memset(fhandle, 0, sizeof(struct nfs_fh));
2331out:
2332 if (page)
2333 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002334 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002335 return status;
2336}
2337
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2339{
2340 struct nfs4_getattr_arg args = {
2341 .fh = fhandle,
2342 .bitmask = server->attr_bitmask,
2343 };
2344 struct nfs4_getattr_res res = {
2345 .fattr = fattr,
2346 .server = server,
2347 };
2348 struct rpc_message msg = {
2349 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2350 .rpc_argp = &args,
2351 .rpc_resp = &res,
2352 };
2353
Trond Myklebust0e574af2005-10-27 22:12:38 -04002354 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002355 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356}
2357
2358static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2359{
2360 struct nfs4_exception exception = { };
2361 int err;
2362 do {
2363 err = nfs4_handle_exception(server,
2364 _nfs4_proc_getattr(server, fhandle, fattr),
2365 &exception);
2366 } while (exception.retry);
2367 return err;
2368}
2369
2370/*
2371 * The file is not closed if it is opened due to the a request to change
2372 * the size of the file. The open call will not be needed once the
2373 * VFS layer lookup-intents are implemented.
2374 *
2375 * Close is called when the inode is destroyed.
2376 * If we haven't opened the file for O_WRONLY, we
2377 * need to in the size_change case to obtain a stateid.
2378 *
2379 * Got race?
2380 * Because OPEN is always done by name in nfsv4, it is
2381 * possible that we opened a different file by the same
2382 * name. We can recognize this race condition, but we
2383 * can't do anything about it besides returning an error.
2384 *
2385 * This will be fixed with VFS changes (lookup-intent).
2386 */
2387static int
2388nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2389 struct iattr *sattr)
2390{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002391 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002392 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002393 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 int status;
2395
Benny Halevy8a1636c2010-07-14 15:43:57 -04002396 if (pnfs_ld_layoutret_on_setattr(inode))
2397 pnfs_return_layout(inode);
2398
Trond Myklebust0e574af2005-10-27 22:12:38 -04002399 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
Trond Myklebustd5308382005-11-04 15:33:38 -05002401 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002402 if (sattr->ia_valid & ATTR_FILE) {
2403 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002404
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002405 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002406 if (ctx) {
2407 cred = ctx->cred;
2408 state = ctx->state;
2409 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002410 }
2411
2412 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002413 if (status == 0)
2414 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 return status;
2416}
2417
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002418static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2419 const struct qstr *name, struct nfs_fh *fhandle,
2420 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002421{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002422 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002423 int status;
2424 struct nfs4_lookup_arg args = {
2425 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002426 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002427 .name = name,
2428 };
2429 struct nfs4_lookup_res res = {
2430 .server = server,
2431 .fattr = fattr,
2432 .fh = fhandle,
2433 };
2434 struct rpc_message msg = {
2435 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2436 .rpc_argp = &args,
2437 .rpc_resp = &res,
2438 };
2439
2440 nfs_fattr_init(fattr);
2441
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002443 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 dprintk("NFS reply lookup: %d\n", status);
2445 return status;
2446}
2447
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002448void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2449{
2450 memset(fh, 0, sizeof(struct nfs_fh));
2451 fattr->fsid.major = 1;
2452 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2453 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2454 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2455 fattr->nlink = 2;
2456}
2457
Bryan Schumaker7c513052011-03-24 17:12:24 +00002458static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2459 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460{
2461 struct nfs4_exception exception = { };
2462 int err;
2463 do {
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002464 int status;
2465
2466 status = _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr);
2467 switch (status) {
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002468 case -NFS4ERR_BADNAME:
2469 return -ENOENT;
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002470 case -NFS4ERR_MOVED:
Trond Myklebusta6f498a2011-11-07 15:50:32 -05002471 return nfs4_get_referral(dir, name, fattr, fhandle);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002472 case -NFS4ERR_WRONGSEC:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002473 nfs_fixup_secinfo_attributes(fattr, fhandle);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002474 }
2475 err = nfs4_handle_exception(NFS_SERVER(dir),
2476 status, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 } while (exception.retry);
2478 return err;
2479}
2480
2481static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2482{
Trond Myklebust76b32992007-08-10 17:45:11 -04002483 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 struct nfs4_accessargs args = {
2485 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002486 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002488 struct nfs4_accessres res = {
2489 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002490 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 struct rpc_message msg = {
2492 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2493 .rpc_argp = &args,
2494 .rpc_resp = &res,
2495 .rpc_cred = entry->cred,
2496 };
2497 int mode = entry->mask;
2498 int status;
2499
2500 /*
2501 * Determine which access bits we want to ask for...
2502 */
2503 if (mode & MAY_READ)
2504 args.access |= NFS4_ACCESS_READ;
2505 if (S_ISDIR(inode->i_mode)) {
2506 if (mode & MAY_WRITE)
2507 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2508 if (mode & MAY_EXEC)
2509 args.access |= NFS4_ACCESS_LOOKUP;
2510 } else {
2511 if (mode & MAY_WRITE)
2512 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2513 if (mode & MAY_EXEC)
2514 args.access |= NFS4_ACCESS_EXECUTE;
2515 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002516
2517 res.fattr = nfs_alloc_fattr();
2518 if (res.fattr == NULL)
2519 return -ENOMEM;
2520
Bryan Schumaker7c513052011-03-24 17:12:24 +00002521 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 if (!status) {
2523 entry->mask = 0;
2524 if (res.access & NFS4_ACCESS_READ)
2525 entry->mask |= MAY_READ;
2526 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2527 entry->mask |= MAY_WRITE;
2528 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2529 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002530 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002532 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 return status;
2534}
2535
2536static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2537{
2538 struct nfs4_exception exception = { };
2539 int err;
2540 do {
2541 err = nfs4_handle_exception(NFS_SERVER(inode),
2542 _nfs4_proc_access(inode, entry),
2543 &exception);
2544 } while (exception.retry);
2545 return err;
2546}
2547
2548/*
2549 * TODO: For the time being, we don't try to get any attributes
2550 * along with any of the zero-copy operations READ, READDIR,
2551 * READLINK, WRITE.
2552 *
2553 * In the case of the first three, we want to put the GETATTR
2554 * after the read-type operation -- this is because it is hard
2555 * to predict the length of a GETATTR response in v4, and thus
2556 * align the READ data correctly. This means that the GETATTR
2557 * may end up partially falling into the page cache, and we should
2558 * shift it into the 'tail' of the xdr_buf before processing.
2559 * To do this efficiently, we need to know the total length
2560 * of data received, which doesn't seem to be available outside
2561 * of the RPC layer.
2562 *
2563 * In the case of WRITE, we also want to put the GETATTR after
2564 * the operation -- in this case because we want to make sure
2565 * we get the post-operation mtime and size. This means that
2566 * we can't use xdr_encode_pages() as written: we need a variant
2567 * of it which would leave room in the 'tail' iovec.
2568 *
2569 * Both of these changes to the XDR layer would in fact be quite
2570 * minor, but I decided to leave them for a subsequent patch.
2571 */
2572static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2573 unsigned int pgbase, unsigned int pglen)
2574{
2575 struct nfs4_readlink args = {
2576 .fh = NFS_FH(inode),
2577 .pgbase = pgbase,
2578 .pglen = pglen,
2579 .pages = &page,
2580 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002581 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 struct rpc_message msg = {
2583 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2584 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002585 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 };
2587
Bryan Schumaker7c513052011-03-24 17:12:24 +00002588 return nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589}
2590
2591static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2592 unsigned int pgbase, unsigned int pglen)
2593{
2594 struct nfs4_exception exception = { };
2595 int err;
2596 do {
2597 err = nfs4_handle_exception(NFS_SERVER(inode),
2598 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2599 &exception);
2600 } while (exception.retry);
2601 return err;
2602}
2603
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604/*
2605 * Got race?
2606 * We will need to arrange for the VFS layer to provide an atomic open.
2607 * Until then, this create/open method is prone to inefficiency and race
2608 * conditions due to the lookup, create, and open VFS calls from sys_open()
2609 * placed on the wire.
2610 *
2611 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2612 * The file will be opened again in the subsequent VFS open call
2613 * (nfs4_proc_file_open).
2614 *
2615 * The open for read will just hang around to be used by any process that
2616 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2617 */
2618
2619static int
2620nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002621 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622{
Al Viro82a2c1b2011-06-22 18:30:55 -04002623 struct dentry *de = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002625 struct rpc_cred *cred = NULL;
2626 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 int status = 0;
2628
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002629 if (ctx != NULL) {
2630 cred = ctx->cred;
Al Viro3d4ff432011-06-22 18:40:12 -04002631 de = ctx->dentry;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002632 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002634 sattr->ia_mode &= ~current_umask();
Al Viro82a2c1b2011-06-22 18:30:55 -04002635 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002636 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 if (IS_ERR(state)) {
2638 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002639 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002641 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002642 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002643 if (ctx != NULL)
2644 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002645 else
Al Viro643168c2011-06-22 18:20:23 -04002646 nfs4_close_sync(state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647out:
2648 return status;
2649}
2650
2651static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2652{
Trond Myklebust16e42952005-10-27 22:12:44 -04002653 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002654 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002656 .name.len = name->len,
2657 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002658 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002660 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002661 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002662 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002664 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2665 .rpc_argp = &args,
2666 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002668 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
Trond Myklebustd3468902010-04-16 16:22:50 -04002670 res.dir_attr = nfs_alloc_fattr();
2671 if (res.dir_attr == NULL)
2672 goto out;
2673
Bryan Schumaker7c513052011-03-24 17:12:24 +00002674 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002675 if (status == 0) {
2676 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002677 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002678 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002679 nfs_free_fattr(res.dir_attr);
2680out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 return status;
2682}
2683
2684static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2685{
2686 struct nfs4_exception exception = { };
2687 int err;
2688 do {
2689 err = nfs4_handle_exception(NFS_SERVER(dir),
2690 _nfs4_proc_remove(dir, name),
2691 &exception);
2692 } while (exception.retry);
2693 return err;
2694}
2695
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002696static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002698 struct nfs_server *server = NFS_SERVER(dir);
2699 struct nfs_removeargs *args = msg->rpc_argp;
2700 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701
Trond Myklebusta65318b2009-03-11 14:10:28 -04002702 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002703 res->server = server;
Benny Halevydfb4f302010-09-24 09:17:01 -04002704 res->seq_res.sr_slot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706}
2707
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002708static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002710 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2711
Trond Myklebust14516c32010-07-31 14:29:06 -04002712 if (!nfs4_sequence_done(task, &res->seq_res))
2713 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002714 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002715 return 0;
2716 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002717 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002718 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719}
2720
Jeff Laytond3d41522010-09-17 17:31:57 -04002721static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2722{
2723 struct nfs_server *server = NFS_SERVER(dir);
2724 struct nfs_renameargs *arg = msg->rpc_argp;
2725 struct nfs_renameres *res = msg->rpc_resp;
2726
2727 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2728 arg->bitmask = server->attr_bitmask;
2729 res->server = server;
2730}
2731
2732static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2733 struct inode *new_dir)
2734{
2735 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2736
2737 if (!nfs4_sequence_done(task, &res->seq_res))
2738 return 0;
2739 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2740 return 0;
2741
2742 update_changeattr(old_dir, &res->old_cinfo);
2743 nfs_post_op_update_inode(old_dir, res->old_fattr);
2744 update_changeattr(new_dir, &res->new_cinfo);
2745 nfs_post_op_update_inode(new_dir, res->new_fattr);
2746 return 1;
2747}
2748
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2750 struct inode *new_dir, struct qstr *new_name)
2751{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002752 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002753 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 .old_dir = NFS_FH(old_dir),
2755 .new_dir = NFS_FH(new_dir),
2756 .old_name = old_name,
2757 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002758 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002760 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002761 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002762 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 struct rpc_message msg = {
2764 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2765 .rpc_argp = &arg,
2766 .rpc_resp = &res,
2767 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002768 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769
Trond Myklebust011fff72010-04-16 16:22:49 -04002770 res.old_fattr = nfs_alloc_fattr();
2771 res.new_fattr = nfs_alloc_fattr();
2772 if (res.old_fattr == NULL || res.new_fattr == NULL)
2773 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
Bryan Schumaker7c513052011-03-24 17:12:24 +00002775 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 if (!status) {
2777 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002778 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002780 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002782out:
2783 nfs_free_fattr(res.new_fattr);
2784 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 return status;
2786}
2787
2788static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2789 struct inode *new_dir, struct qstr *new_name)
2790{
2791 struct nfs4_exception exception = { };
2792 int err;
2793 do {
2794 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2795 _nfs4_proc_rename(old_dir, old_name,
2796 new_dir, new_name),
2797 &exception);
2798 } while (exception.retry);
2799 return err;
2800}
2801
2802static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2803{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002804 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 struct nfs4_link_arg arg = {
2806 .fh = NFS_FH(inode),
2807 .dir_fh = NFS_FH(dir),
2808 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002809 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002811 struct nfs4_link_res res = {
2812 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002813 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 struct rpc_message msg = {
2815 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2816 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002817 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002819 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
Trond Myklebust136f2622010-04-16 16:22:49 -04002821 res.fattr = nfs_alloc_fattr();
2822 res.dir_attr = nfs_alloc_fattr();
2823 if (res.fattr == NULL || res.dir_attr == NULL)
2824 goto out;
2825
Bryan Schumaker7c513052011-03-24 17:12:24 +00002826 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002827 if (!status) {
2828 update_changeattr(dir, &res.cinfo);
2829 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002830 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002831 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002832out:
2833 nfs_free_fattr(res.dir_attr);
2834 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 return status;
2836}
2837
2838static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2839{
2840 struct nfs4_exception exception = { };
2841 int err;
2842 do {
2843 err = nfs4_handle_exception(NFS_SERVER(inode),
2844 _nfs4_proc_link(inode, dir, name),
2845 &exception);
2846 } while (exception.retry);
2847 return err;
2848}
2849
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002850struct nfs4_createdata {
2851 struct rpc_message msg;
2852 struct nfs4_create_arg arg;
2853 struct nfs4_create_res res;
2854 struct nfs_fh fh;
2855 struct nfs_fattr fattr;
2856 struct nfs_fattr dir_fattr;
2857};
2858
2859static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2860 struct qstr *name, struct iattr *sattr, u32 ftype)
2861{
2862 struct nfs4_createdata *data;
2863
2864 data = kzalloc(sizeof(*data), GFP_KERNEL);
2865 if (data != NULL) {
2866 struct nfs_server *server = NFS_SERVER(dir);
2867
2868 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2869 data->msg.rpc_argp = &data->arg;
2870 data->msg.rpc_resp = &data->res;
2871 data->arg.dir_fh = NFS_FH(dir);
2872 data->arg.server = server;
2873 data->arg.name = name;
2874 data->arg.attrs = sattr;
2875 data->arg.ftype = ftype;
2876 data->arg.bitmask = server->attr_bitmask;
2877 data->res.server = server;
2878 data->res.fh = &data->fh;
2879 data->res.fattr = &data->fattr;
2880 data->res.dir_fattr = &data->dir_fattr;
2881 nfs_fattr_init(data->res.fattr);
2882 nfs_fattr_init(data->res.dir_fattr);
2883 }
2884 return data;
2885}
2886
2887static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2888{
Bryan Schumaker7c513052011-03-24 17:12:24 +00002889 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00002890 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002891 if (status == 0) {
2892 update_changeattr(dir, &data->res.dir_cinfo);
2893 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2894 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2895 }
2896 return status;
2897}
2898
2899static void nfs4_free_createdata(struct nfs4_createdata *data)
2900{
2901 kfree(data);
2902}
2903
Chuck Lever4f390c12006-08-22 20:06:22 -04002904static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002905 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002907 struct nfs4_createdata *data;
2908 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909
Chuck Lever94a6d752006-08-22 20:06:23 -04002910 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002911 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002912
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002913 status = -ENOMEM;
2914 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2915 if (data == NULL)
2916 goto out;
2917
2918 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2919 data->arg.u.symlink.pages = &page;
2920 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002922 status = nfs4_do_create(dir, dentry, data);
2923
2924 nfs4_free_createdata(data);
2925out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 return status;
2927}
2928
Chuck Lever4f390c12006-08-22 20:06:22 -04002929static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002930 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931{
2932 struct nfs4_exception exception = { };
2933 int err;
2934 do {
2935 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002936 _nfs4_proc_symlink(dir, dentry, page,
2937 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 &exception);
2939 } while (exception.retry);
2940 return err;
2941}
2942
2943static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2944 struct iattr *sattr)
2945{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002946 struct nfs4_createdata *data;
2947 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002949 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2950 if (data == NULL)
2951 goto out;
2952
2953 status = nfs4_do_create(dir, dentry, data);
2954
2955 nfs4_free_createdata(data);
2956out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 return status;
2958}
2959
2960static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2961 struct iattr *sattr)
2962{
2963 struct nfs4_exception exception = { };
2964 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002965
2966 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 do {
2968 err = nfs4_handle_exception(NFS_SERVER(dir),
2969 _nfs4_proc_mkdir(dir, dentry, sattr),
2970 &exception);
2971 } while (exception.retry);
2972 return err;
2973}
2974
2975static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002976 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977{
2978 struct inode *dir = dentry->d_inode;
2979 struct nfs4_readdir_arg args = {
2980 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002981 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 .pgbase = 0,
2983 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002984 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04002985 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 };
2987 struct nfs4_readdir_res res;
2988 struct rpc_message msg = {
2989 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2990 .rpc_argp = &args,
2991 .rpc_resp = &res,
2992 .rpc_cred = cred,
2993 };
2994 int status;
2995
Harvey Harrison3110ff82008-05-02 13:42:44 -07002996 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002997 dentry->d_parent->d_name.name,
2998 dentry->d_name.name,
2999 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3001 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003002 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebustac396122010-11-15 20:26:22 -05003003 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003005 status += args.pgbase;
3006 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003007
3008 nfs_invalidate_atime(dir);
3009
Harvey Harrison3110ff82008-05-02 13:42:44 -07003010 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 return status;
3012}
3013
3014static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003015 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016{
3017 struct nfs4_exception exception = { };
3018 int err;
3019 do {
3020 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3021 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003022 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 &exception);
3024 } while (exception.retry);
3025 return err;
3026}
3027
3028static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3029 struct iattr *sattr, dev_t rdev)
3030{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003031 struct nfs4_createdata *data;
3032 int mode = sattr->ia_mode;
3033 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034
3035 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3036 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003037
3038 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3039 if (data == NULL)
3040 goto out;
3041
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003043 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003045 data->arg.ftype = NF4BLK;
3046 data->arg.u.device.specdata1 = MAJOR(rdev);
3047 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 }
3049 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003050 data->arg.ftype = NF4CHR;
3051 data->arg.u.device.specdata1 = MAJOR(rdev);
3052 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003055 status = nfs4_do_create(dir, dentry, data);
3056
3057 nfs4_free_createdata(data);
3058out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 return status;
3060}
3061
3062static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3063 struct iattr *sattr, dev_t rdev)
3064{
3065 struct nfs4_exception exception = { };
3066 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003067
3068 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 do {
3070 err = nfs4_handle_exception(NFS_SERVER(dir),
3071 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3072 &exception);
3073 } while (exception.retry);
3074 return err;
3075}
3076
3077static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3078 struct nfs_fsstat *fsstat)
3079{
3080 struct nfs4_statfs_arg args = {
3081 .fh = fhandle,
3082 .bitmask = server->attr_bitmask,
3083 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003084 struct nfs4_statfs_res res = {
3085 .fsstat = fsstat,
3086 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 struct rpc_message msg = {
3088 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3089 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003090 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 };
3092
Trond Myklebust0e574af2005-10-27 22:12:38 -04003093 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003094 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095}
3096
3097static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3098{
3099 struct nfs4_exception exception = { };
3100 int err;
3101 do {
3102 err = nfs4_handle_exception(server,
3103 _nfs4_proc_statfs(server, fhandle, fsstat),
3104 &exception);
3105 } while (exception.retry);
3106 return err;
3107}
3108
3109static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3110 struct nfs_fsinfo *fsinfo)
3111{
3112 struct nfs4_fsinfo_arg args = {
3113 .fh = fhandle,
3114 .bitmask = server->attr_bitmask,
3115 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003116 struct nfs4_fsinfo_res res = {
3117 .fsinfo = fsinfo,
3118 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 struct rpc_message msg = {
3120 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3121 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003122 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 };
3124
Bryan Schumaker7c513052011-03-24 17:12:24 +00003125 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126}
3127
3128static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3129{
3130 struct nfs4_exception exception = { };
3131 int err;
3132
3133 do {
3134 err = nfs4_handle_exception(server,
3135 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3136 &exception);
3137 } while (exception.retry);
3138 return err;
3139}
3140
3141static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3142{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003143 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3145}
3146
3147static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3148 struct nfs_pathconf *pathconf)
3149{
3150 struct nfs4_pathconf_arg args = {
3151 .fh = fhandle,
3152 .bitmask = server->attr_bitmask,
3153 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003154 struct nfs4_pathconf_res res = {
3155 .pathconf = pathconf,
3156 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 struct rpc_message msg = {
3158 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3159 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003160 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 };
3162
3163 /* None of the pathconf attributes are mandatory to implement */
3164 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3165 memset(pathconf, 0, sizeof(*pathconf));
3166 return 0;
3167 }
3168
Trond Myklebust0e574af2005-10-27 22:12:38 -04003169 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003170 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171}
3172
3173static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3174 struct nfs_pathconf *pathconf)
3175{
3176 struct nfs4_exception exception = { };
3177 int err;
3178
3179 do {
3180 err = nfs4_handle_exception(server,
3181 _nfs4_proc_pathconf(server, fhandle, pathconf),
3182 &exception);
3183 } while (exception.retry);
3184 return err;
3185}
3186
Benny Halevyd20581a2011-05-22 19:52:03 +03003187void __nfs4_read_done_cb(struct nfs_read_data *data)
3188{
3189 nfs_invalidate_atime(data->inode);
3190}
3191
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003192static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193{
Trond Myklebustec06c092006-03-20 13:44:27 -05003194 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003196 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003197 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003198 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003200
Benny Halevyd20581a2011-05-22 19:52:03 +03003201 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003203 renew_lease(server, data->timestamp);
3204 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205}
3206
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003207static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3208{
3209
3210 dprintk("--> %s\n", __func__);
3211
3212 if (!nfs4_sequence_done(task, &data->res.seq_res))
3213 return -EAGAIN;
3214
Benny Halevyd20581a2011-05-22 19:52:03 +03003215 return data->read_done_cb ? data->read_done_cb(task, data) :
3216 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003217}
3218
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003219static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 data->timestamp = jiffies;
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003222 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003223 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224}
3225
Andy Adamsoncbdabc72011-03-01 01:34:20 +00003226/* Reset the the nfs_read_data to send the read to the MDS. */
3227void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3228{
3229 dprintk("%s Reset task for i/o through\n", __func__);
3230 put_lseg(data->lseg);
3231 data->lseg = NULL;
3232 /* offsets will differ in the dense stripe case */
3233 data->args.offset = data->mds_offset;
3234 data->ds_clp = NULL;
3235 data->args.fh = NFS_FH(data->inode);
3236 data->read_done_cb = nfs4_read_done_cb;
3237 task->tk_ops = data->mds_ops;
3238 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3239}
3240EXPORT_SYMBOL_GPL(nfs4_reset_read);
3241
Fred Isamanb029bc92011-03-03 15:13:42 +00003242static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 struct inode *inode = data->inode;
3245
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003246 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003247 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003248 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003250 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003252 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003253 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003254 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255}
3256
Fred Isamanb029bc92011-03-03 15:13:42 +00003257static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3258{
3259 if (!nfs4_sequence_done(task, &data->res.seq_res))
3260 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003261 return data->write_done_cb ? data->write_done_cb(task, data) :
3262 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003263}
3264
Fred Isamana69aef12011-03-03 15:13:47 +00003265/* Reset the the nfs_write_data to send the write to the MDS. */
3266void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3267{
3268 dprintk("%s Reset task for i/o through\n", __func__);
3269 put_lseg(data->lseg);
3270 data->lseg = NULL;
3271 data->ds_clp = NULL;
3272 data->write_done_cb = nfs4_write_done_cb;
3273 data->args.fh = NFS_FH(data->inode);
3274 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3275 data->args.offset = data->mds_offset;
3276 data->res.fattr = &data->fattr;
3277 task->tk_ops = data->mds_ops;
3278 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3279}
3280EXPORT_SYMBOL_GPL(nfs4_reset_write);
3281
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003282static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003284 struct nfs_server *server = NFS_SERVER(data->inode);
3285
Fred Isaman7ffd1062011-03-03 15:13:46 +00003286 if (data->lseg) {
3287 data->args.bitmask = NULL;
3288 data->res.fattr = NULL;
3289 } else
3290 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isamanb029bc92011-03-03 15:13:42 +00003291 if (!data->write_done_cb)
3292 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003293 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 data->timestamp = jiffies;
3295
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003296 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297}
3298
Fred Isaman5f452432011-03-23 13:27:46 +00003299static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003302
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003303 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003304 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003305 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003307 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003308 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309}
3310
Fred Isaman5f452432011-03-23 13:27:46 +00003311static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3312{
3313 if (!nfs4_sequence_done(task, &data->res.seq_res))
3314 return -EAGAIN;
3315 return data->write_done_cb(task, data);
3316}
3317
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003318static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003320 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003321
3322 if (data->lseg) {
3323 data->args.bitmask = NULL;
3324 data->res.fattr = NULL;
3325 } else
3326 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isaman5f452432011-03-23 13:27:46 +00003327 if (!data->write_done_cb)
3328 data->write_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003329 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003330 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331}
3332
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003333struct nfs4_renewdata {
3334 struct nfs_client *client;
3335 unsigned long timestamp;
3336};
3337
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338/*
3339 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3340 * standalone procedure for queueing an asynchronous RENEW.
3341 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003342static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003343{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003344 struct nfs4_renewdata *data = calldata;
3345 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003346
Alexandros Batsakis0851de02010-02-05 03:45:06 -08003347 if (atomic_read(&clp->cl_count) > 1)
3348 nfs4_schedule_state_renewal(clp);
3349 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003350 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003351}
3352
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003353static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003355 struct nfs4_renewdata *data = calldata;
3356 struct nfs_client *clp = data->client;
3357 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358
3359 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003360 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003361 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3362 return;
3363 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003364 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003365 return;
3366 }
3367 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003369 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370}
3371
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003372static const struct rpc_call_ops nfs4_renew_ops = {
3373 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003374 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003375};
3376
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003377static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378{
3379 struct rpc_message msg = {
3380 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3381 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003382 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003384 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003386 if (renew_flags == 0)
3387 return 0;
Alexandros Batsakis0851de02010-02-05 03:45:06 -08003388 if (!atomic_inc_not_zero(&clp->cl_count))
3389 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003390 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003391 if (data == NULL)
3392 return -ENOMEM;
3393 data->client = clp;
3394 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003396 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397}
3398
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003399static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400{
3401 struct rpc_message msg = {
3402 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3403 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003404 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 };
3406 unsigned long now = jiffies;
3407 int status;
3408
3409 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3410 if (status < 0)
3411 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003412 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 return 0;
3414}
3415
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003416static inline int nfs4_server_supports_acls(struct nfs_server *server)
3417{
3418 return (server->caps & NFS_CAP_ACLS)
3419 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3420 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3421}
3422
3423/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3424 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3425 * the stack.
3426 */
3427#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3428
Neil Hormane9e3d722011-03-04 19:26:03 -05003429static int buf_to_pages_noslab(const void *buf, size_t buflen,
3430 struct page **pages, unsigned int *pgbase)
3431{
3432 struct page *newpage, **spages;
3433 int rc = 0;
3434 size_t len;
3435 spages = pages;
3436
3437 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003438 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003439 newpage = alloc_page(GFP_KERNEL);
3440
3441 if (newpage == NULL)
3442 goto unwind;
3443 memcpy(page_address(newpage), buf, len);
3444 buf += len;
3445 buflen -= len;
3446 *pages++ = newpage;
3447 rc++;
3448 } while (buflen != 0);
3449
3450 return rc;
3451
3452unwind:
3453 for(; rc > 0; rc--)
3454 __free_page(spages[rc-1]);
3455 return -ENOMEM;
3456}
3457
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003458struct nfs4_cached_acl {
3459 int cached;
3460 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003461 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003462};
3463
3464static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003465{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003466 struct nfs_inode *nfsi = NFS_I(inode);
3467
3468 spin_lock(&inode->i_lock);
3469 kfree(nfsi->nfs4_acl);
3470 nfsi->nfs4_acl = acl;
3471 spin_unlock(&inode->i_lock);
3472}
3473
3474static void nfs4_zap_acl_attr(struct inode *inode)
3475{
3476 nfs4_set_cached_acl(inode, NULL);
3477}
3478
3479static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3480{
3481 struct nfs_inode *nfsi = NFS_I(inode);
3482 struct nfs4_cached_acl *acl;
3483 int ret = -ENOENT;
3484
3485 spin_lock(&inode->i_lock);
3486 acl = nfsi->nfs4_acl;
3487 if (acl == NULL)
3488 goto out;
3489 if (buf == NULL) /* user is just asking for length */
3490 goto out_len;
3491 if (acl->cached == 0)
3492 goto out;
3493 ret = -ERANGE; /* see getxattr(2) man page */
3494 if (acl->len > buflen)
3495 goto out;
3496 memcpy(buf, acl->data, acl->len);
3497out_len:
3498 ret = acl->len;
3499out:
3500 spin_unlock(&inode->i_lock);
3501 return ret;
3502}
3503
3504static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3505{
3506 struct nfs4_cached_acl *acl;
3507
3508 if (buf && acl_len <= PAGE_SIZE) {
3509 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3510 if (acl == NULL)
3511 goto out;
3512 acl->cached = 1;
3513 memcpy(acl->data, buf, acl_len);
3514 } else {
3515 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3516 if (acl == NULL)
3517 goto out;
3518 acl->cached = 0;
3519 }
3520 acl->len = acl_len;
3521out:
3522 nfs4_set_cached_acl(inode, acl);
3523}
3524
Andy Adamsonbf118a32011-12-07 11:55:27 -05003525/*
3526 * The getxattr API returns the required buffer length when called with a
3527 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
3528 * the required buf. On a NULL buf, we send a page of data to the server
3529 * guessing that the ACL request can be serviced by a page. If so, we cache
3530 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
3531 * the cache. If not so, we throw away the page, and cache the required
3532 * length. The next getxattr call will then produce another round trip to
3533 * the server, this time with the input buf of the required size.
3534 */
Trond Myklebust16b42892006-08-24 12:27:15 -04003535static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003536{
Andy Adamsonbf118a32011-12-07 11:55:27 -05003537 struct page *pages[NFS4ACL_MAXPAGES] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003538 struct nfs_getaclargs args = {
3539 .fh = NFS_FH(inode),
3540 .acl_pages = pages,
3541 .acl_len = buflen,
3542 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003543 struct nfs_getaclres res = {
3544 .acl_len = buflen,
3545 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003546 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003547 struct rpc_message msg = {
3548 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3549 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003550 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003551 };
Andy Adamsonbf118a32011-12-07 11:55:27 -05003552 int ret = -ENOMEM, npages, i, acl_len = 0;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003553
Andy Adamsonbf118a32011-12-07 11:55:27 -05003554 npages = (buflen + PAGE_SIZE - 1) >> PAGE_SHIFT;
3555 /* As long as we're doing a round trip to the server anyway,
3556 * let's be prepared for a page of acl data. */
3557 if (npages == 0)
3558 npages = 1;
3559
3560 for (i = 0; i < npages; i++) {
3561 pages[i] = alloc_page(GFP_KERNEL);
3562 if (!pages[i])
3563 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003564 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003565 if (npages > 1) {
3566 /* for decoding across pages */
3567 args.acl_scratch = alloc_page(GFP_KERNEL);
3568 if (!args.acl_scratch)
3569 goto out_free;
3570 }
3571 args.acl_len = npages * PAGE_SIZE;
3572 args.acl_pgbase = 0;
3573 /* Let decode_getfacl know not to fail if the ACL data is larger than
3574 * the page we send as a guess */
3575 if (buf == NULL)
3576 res.acl_flags |= NFS4_ACL_LEN_REQUEST;
3577 resp_buf = page_address(pages[0]);
3578
3579 dprintk("%s buf %p buflen %ld npages %d args.acl_len %ld\n",
3580 __func__, buf, buflen, npages, args.acl_len);
3581 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
3582 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003583 if (ret)
3584 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003585
3586 acl_len = res.acl_len - res.acl_data_offset;
3587 if (acl_len > args.acl_len)
3588 nfs4_write_cached_acl(inode, NULL, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003589 else
Andy Adamsonbf118a32011-12-07 11:55:27 -05003590 nfs4_write_cached_acl(inode, resp_buf + res.acl_data_offset,
3591 acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003592 if (buf) {
3593 ret = -ERANGE;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003594 if (acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003595 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05003596 _copy_from_pages(buf, pages, res.acl_data_offset,
3597 res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003598 }
Andy Adamsonbf118a32011-12-07 11:55:27 -05003599 ret = acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003600out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05003601 for (i = 0; i < npages; i++)
3602 if (pages[i])
3603 __free_page(pages[i]);
3604 if (args.acl_scratch)
3605 __free_page(args.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003606 return ret;
3607}
3608
Trond Myklebust16b42892006-08-24 12:27:15 -04003609static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3610{
3611 struct nfs4_exception exception = { };
3612 ssize_t ret;
3613 do {
3614 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3615 if (ret >= 0)
3616 break;
3617 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3618 } while (exception.retry);
3619 return ret;
3620}
3621
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003622static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3623{
3624 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003625 int ret;
3626
3627 if (!nfs4_server_supports_acls(server))
3628 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003629 ret = nfs_revalidate_inode(server, inode);
3630 if (ret < 0)
3631 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003632 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3633 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003634 ret = nfs4_read_cached_acl(inode, buf, buflen);
3635 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05003636 /* -ENOENT is returned if there is no ACL or if there is an ACL
3637 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003638 return ret;
3639 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003640}
3641
Trond Myklebust16b42892006-08-24 12:27:15 -04003642static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003643{
3644 struct nfs_server *server = NFS_SERVER(inode);
3645 struct page *pages[NFS4ACL_MAXPAGES];
3646 struct nfs_setaclargs arg = {
3647 .fh = NFS_FH(inode),
3648 .acl_pages = pages,
3649 .acl_len = buflen,
3650 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003651 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003652 struct rpc_message msg = {
3653 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3654 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003655 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003656 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003657 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003658
3659 if (!nfs4_server_supports_acls(server))
3660 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003661 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3662 if (i < 0)
3663 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003664 nfs_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003665 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003666
3667 /*
3668 * Free each page after tx, so the only ref left is
3669 * held by the network stack
3670 */
3671 for (; i > 0; i--)
3672 put_page(pages[i-1]);
3673
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003674 /*
3675 * Acl update can result in inode attribute update.
3676 * so mark the attribute cache invalid.
3677 */
3678 spin_lock(&inode->i_lock);
3679 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3680 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003681 nfs_access_zap_cache(inode);
3682 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003683 return ret;
3684}
3685
Trond Myklebust16b42892006-08-24 12:27:15 -04003686static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3687{
3688 struct nfs4_exception exception = { };
3689 int err;
3690 do {
3691 err = nfs4_handle_exception(NFS_SERVER(inode),
3692 __nfs4_proc_set_acl(inode, buf, buflen),
3693 &exception);
3694 } while (exception.retry);
3695 return err;
3696}
3697
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003699nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003701 struct nfs_client *clp = server->nfs_client;
3702
3703 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 return 0;
3705 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003706 case -NFS4ERR_ADMIN_REVOKED:
3707 case -NFS4ERR_BAD_STATEID:
3708 case -NFS4ERR_OPENMODE:
3709 if (state == NULL)
3710 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003711 nfs4_schedule_stateid_recovery(server, state);
3712 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003713 case -NFS4ERR_EXPIRED:
3714 if (state != NULL)
3715 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003717 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003718 nfs4_schedule_lease_recovery(clp);
3719 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003720#if defined(CONFIG_NFS_V4_1)
3721 case -NFS4ERR_BADSESSION:
3722 case -NFS4ERR_BADSLOT:
3723 case -NFS4ERR_BAD_HIGH_SLOT:
3724 case -NFS4ERR_DEADSESSION:
3725 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3726 case -NFS4ERR_SEQ_FALSE_RETRY:
3727 case -NFS4ERR_SEQ_MISORDERED:
3728 dprintk("%s ERROR %d, Reset session\n", __func__,
3729 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003730 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -04003731 task->tk_status = 0;
3732 return -EAGAIN;
3733#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003735 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003736 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003737 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3739 task->tk_status = 0;
3740 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003741 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 case -NFS4ERR_OLD_STATEID:
3743 task->tk_status = 0;
3744 return -EAGAIN;
3745 }
3746 task->tk_status = nfs4_map_errors(task->tk_status);
3747 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003748wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003749 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003750 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3751 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3752 task->tk_status = 0;
3753 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754}
3755
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003756int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3757 unsigned short port, struct rpc_cred *cred,
3758 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759{
3760 nfs4_verifier sc_verifier;
3761 struct nfs4_setclientid setclientid = {
3762 .sc_verifier = &sc_verifier,
3763 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003764 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 };
3766 struct rpc_message msg = {
3767 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3768 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003769 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003770 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 };
Al Virobc4785c2006-10-19 23:28:51 -07003772 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 int loop = 0;
3774 int status;
3775
Al Virobc4785c2006-10-19 23:28:51 -07003776 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3778 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3779
3780 for(;;) {
3781 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003782 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003783 clp->cl_ipaddr,
3784 rpc_peeraddr2str(clp->cl_rpcclient,
3785 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003786 rpc_peeraddr2str(clp->cl_rpcclient,
3787 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003788 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 clp->cl_id_uniquifier);
3790 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003791 sizeof(setclientid.sc_netid),
3792 rpc_peeraddr2str(clp->cl_rpcclient,
3793 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003795 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 clp->cl_ipaddr, port >> 8, port & 255);
3797
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003798 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 if (status != -NFS4ERR_CLID_INUSE)
3800 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003801 if (loop != 0) {
3802 ++clp->cl_id_uniquifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003804 }
3805 ++loop;
3806 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 }
3808 return status;
3809}
3810
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003811int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003812 struct nfs4_setclientid_res *arg,
3813 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814{
3815 struct nfs_fsinfo fsinfo;
3816 struct rpc_message msg = {
3817 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003818 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003820 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 };
3822 unsigned long now;
3823 int status;
3824
3825 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003826 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 if (status == 0) {
3828 spin_lock(&clp->cl_lock);
3829 clp->cl_lease_time = fsinfo.lease_time * HZ;
3830 clp->cl_last_renewal = now;
3831 spin_unlock(&clp->cl_lock);
3832 }
3833 return status;
3834}
3835
Trond Myklebustfe650402006-01-03 09:55:18 +01003836struct nfs4_delegreturndata {
3837 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003838 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003839 struct nfs_fh fh;
3840 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003841 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003842 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003843 int rpc_status;
3844};
3845
Trond Myklebustfe650402006-01-03 09:55:18 +01003846static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3847{
3848 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003849
Trond Myklebust14516c32010-07-31 14:29:06 -04003850 if (!nfs4_sequence_done(task, &data->res.seq_res))
3851 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003852
Ricardo Labiaga79708862009-12-07 09:23:21 -05003853 switch (task->tk_status) {
3854 case -NFS4ERR_STALE_STATEID:
3855 case -NFS4ERR_EXPIRED:
3856 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003857 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003858 break;
3859 default:
3860 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3861 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003862 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003863 return;
3864 }
3865 }
3866 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003867}
3868
3869static void nfs4_delegreturn_release(void *calldata)
3870{
Trond Myklebustfe650402006-01-03 09:55:18 +01003871 kfree(calldata);
3872}
3873
Andy Adamson938e1012009-04-01 09:22:28 -04003874#if defined(CONFIG_NFS_V4_1)
3875static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3876{
3877 struct nfs4_delegreturndata *d_data;
3878
3879 d_data = (struct nfs4_delegreturndata *)data;
3880
Trond Myklebust035168a2010-06-16 09:52:26 -04003881 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003882 &d_data->args.seq_args,
3883 &d_data->res.seq_res, 1, task))
3884 return;
3885 rpc_call_start(task);
3886}
3887#endif /* CONFIG_NFS_V4_1 */
3888
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003889static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003890#if defined(CONFIG_NFS_V4_1)
3891 .rpc_call_prepare = nfs4_delegreturn_prepare,
3892#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003893 .rpc_call_done = nfs4_delegreturn_done,
3894 .rpc_release = nfs4_delegreturn_release,
3895};
3896
Trond Myklebuste6f81072008-01-24 18:14:34 -05003897static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003898{
3899 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003900 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003901 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003902 struct rpc_message msg = {
3903 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3904 .rpc_cred = cred,
3905 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003906 struct rpc_task_setup task_setup_data = {
3907 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003908 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003909 .callback_ops = &nfs4_delegreturn_ops,
3910 .flags = RPC_TASK_ASYNC,
3911 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003912 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003913
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003914 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003915 if (data == NULL)
3916 return -ENOMEM;
3917 data->args.fhandle = &data->fh;
3918 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003919 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003920 nfs_copy_fh(&data->fh, NFS_FH(inode));
3921 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003922 data->res.fattr = &data->fattr;
3923 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003924 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003925 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003926 data->rpc_status = 0;
3927
Trond Myklebustc970aa82007-07-14 15:39:59 -04003928 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05003929 msg.rpc_argp = &data->args;
3930 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003931 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003932 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003933 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003934 if (!issync)
3935 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003936 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003937 if (status != 0)
3938 goto out;
3939 status = data->rpc_status;
3940 if (status != 0)
3941 goto out;
3942 nfs_refresh_inode(inode, &data->fattr);
3943out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003944 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003945 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946}
3947
Trond Myklebuste6f81072008-01-24 18:14:34 -05003948int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949{
3950 struct nfs_server *server = NFS_SERVER(inode);
3951 struct nfs4_exception exception = { };
3952 int err;
3953 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003954 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 switch (err) {
3956 case -NFS4ERR_STALE_STATEID:
3957 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 case 0:
3959 return 0;
3960 }
3961 err = nfs4_handle_exception(server, err, &exception);
3962 } while (exception.retry);
3963 return err;
3964}
3965
3966#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3967#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3968
3969/*
3970 * sleep, with exponential backoff, and retry the LOCK operation.
3971 */
3972static unsigned long
3973nfs4_set_lock_task_retry(unsigned long timeout)
3974{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003975 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 timeout <<= 1;
3977 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3978 return NFS4_LOCK_MAXTIMEOUT;
3979 return timeout;
3980}
3981
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3983{
3984 struct inode *inode = state->inode;
3985 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003986 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003987 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003989 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003991 struct nfs_lockt_res res = {
3992 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 };
3994 struct rpc_message msg = {
3995 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3996 .rpc_argp = &arg,
3997 .rpc_resp = &res,
3998 .rpc_cred = state->owner->so_cred,
3999 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 struct nfs4_lock_state *lsp;
4001 int status;
4002
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004003 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004004 status = nfs4_set_lock_state(state, request);
4005 if (status != 0)
4006 goto out;
4007 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004008 arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004009 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004010 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004011 switch (status) {
4012 case 0:
4013 request->fl_type = F_UNLCK;
4014 break;
4015 case -NFS4ERR_DENIED:
4016 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004018 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004019out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020 return status;
4021}
4022
4023static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4024{
4025 struct nfs4_exception exception = { };
4026 int err;
4027
4028 do {
4029 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4030 _nfs4_proc_getlk(state, cmd, request),
4031 &exception);
4032 } while (exception.retry);
4033 return err;
4034}
4035
4036static int do_vfs_lock(struct file *file, struct file_lock *fl)
4037{
4038 int res = 0;
4039 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4040 case FL_POSIX:
4041 res = posix_lock_file_wait(file, fl);
4042 break;
4043 case FL_FLOCK:
4044 res = flock_lock_file_wait(file, fl);
4045 break;
4046 default:
4047 BUG();
4048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 return res;
4050}
4051
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004052struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004053 struct nfs_locku_args arg;
4054 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004055 struct nfs4_lock_state *lsp;
4056 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004057 struct file_lock fl;
4058 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004059 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004060};
4061
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004062static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4063 struct nfs_open_context *ctx,
4064 struct nfs4_lock_state *lsp,
4065 struct nfs_seqid *seqid)
4066{
4067 struct nfs4_unlockdata *p;
4068 struct inode *inode = lsp->ls_state->inode;
4069
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004070 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004071 if (p == NULL)
4072 return NULL;
4073 p->arg.fh = NFS_FH(inode);
4074 p->arg.fl = &p->fl;
4075 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004076 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004077 p->arg.stateid = &lsp->ls_stateid;
4078 p->lsp = lsp;
4079 atomic_inc(&lsp->ls_count);
4080 /* Ensure we don't close file until we're done freeing locks! */
4081 p->ctx = get_nfs_open_context(ctx);
4082 memcpy(&p->fl, fl, sizeof(p->fl));
4083 p->server = NFS_SERVER(inode);
4084 return p;
4085}
4086
Trond Myklebust06f814a2006-01-03 09:55:07 +01004087static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004088{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004089 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004090 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004091 nfs4_put_lock_state(calldata->lsp);
4092 put_nfs_open_context(calldata->ctx);
4093 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004094}
4095
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004096static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004097{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004098 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004099
Trond Myklebust14516c32010-07-31 14:29:06 -04004100 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4101 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004102 switch (task->tk_status) {
4103 case 0:
4104 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004105 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004106 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01004107 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004108 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004109 case -NFS4ERR_BAD_STATEID:
4110 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004111 case -NFS4ERR_STALE_STATEID:
4112 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004113 break;
4114 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004115 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004116 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004117 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004118}
4119
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004120static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004121{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004122 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004124 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004125 return;
4126 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004127 /* Note: exit _without_ running nfs4_locku_done */
4128 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004129 return;
4130 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004131 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004132 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004133 &calldata->arg.seq_args,
4134 &calldata->res.seq_res, 1, task))
4135 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004136 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137}
4138
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004139static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004140 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004141 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004142 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004143};
4144
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004145static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4146 struct nfs_open_context *ctx,
4147 struct nfs4_lock_state *lsp,
4148 struct nfs_seqid *seqid)
4149{
4150 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004151 struct rpc_message msg = {
4152 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4153 .rpc_cred = ctx->cred,
4154 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004155 struct rpc_task_setup task_setup_data = {
4156 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004157 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004158 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004159 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004160 .flags = RPC_TASK_ASYNC,
4161 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004162
Frank Filz137d6ac2007-07-09 15:32:29 -07004163 /* Ensure this is an unlock - when canceling a lock, the
4164 * canceled lock is passed in, and it won't be an unlock.
4165 */
4166 fl->fl_type = F_UNLCK;
4167
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004168 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4169 if (data == NULL) {
4170 nfs_free_seqid(seqid);
4171 return ERR_PTR(-ENOMEM);
4172 }
4173
Trond Myklebust1174dd12010-12-21 10:52:24 -05004174 msg.rpc_argp = &data->arg;
4175 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004176 task_setup_data.callback_data = data;
4177 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004178}
4179
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4181{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004182 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004183 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004184 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004185 struct rpc_task *task;
4186 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004187 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188
Trond Myklebust9b073572006-06-29 16:38:34 -04004189 status = nfs4_set_lock_state(state, request);
4190 /* Unlock _before_ we do the RPC call */
4191 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004192 down_read(&nfsi->rwsem);
4193 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4194 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004195 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004196 }
4197 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004198 if (status != 0)
4199 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004200 /* Is this a delegated lock? */
4201 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004202 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004203 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004204 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004205 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004206 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004207 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004208 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004209 status = PTR_ERR(task);
4210 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004211 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004212 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004213 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004214out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004215 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004216 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217}
4218
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004219struct nfs4_lockdata {
4220 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004221 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004222 struct nfs4_lock_state *lsp;
4223 struct nfs_open_context *ctx;
4224 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004225 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004226 int rpc_status;
4227 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004228 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004229};
4230
4231static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004232 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4233 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004234{
4235 struct nfs4_lockdata *p;
4236 struct inode *inode = lsp->ls_state->inode;
4237 struct nfs_server *server = NFS_SERVER(inode);
4238
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004239 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004240 if (p == NULL)
4241 return NULL;
4242
4243 p->arg.fh = NFS_FH(inode);
4244 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004245 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004246 if (p->arg.open_seqid == NULL)
4247 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004248 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004249 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004250 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004251 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004252 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004253 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004254 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004255 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004256 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004257 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004258 atomic_inc(&lsp->ls_count);
4259 p->ctx = get_nfs_open_context(ctx);
4260 memcpy(&p->fl, fl, sizeof(p->fl));
4261 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004262out_free_seqid:
4263 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004264out_free:
4265 kfree(p);
4266 return NULL;
4267}
4268
4269static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4270{
4271 struct nfs4_lockdata *data = calldata;
4272 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273
Harvey Harrison3110ff82008-05-02 13:42:44 -07004274 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004275 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4276 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004277 /* Do we need to do an open_to_lock_owner? */
4278 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004279 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4280 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004281 data->arg.open_stateid = &state->stateid;
4282 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004283 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004284 } else
4285 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004286 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004287 if (nfs4_setup_sequence(data->server,
4288 &data->arg.seq_args,
Andy Adamson66179ef2009-04-01 09:22:22 -04004289 &data->res.seq_res, 1, task))
4290 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004291 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004292 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004293}
4294
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004295static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4296{
4297 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4298 nfs4_lock_prepare(task, calldata);
4299}
4300
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004301static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4302{
4303 struct nfs4_lockdata *data = calldata;
4304
Harvey Harrison3110ff82008-05-02 13:42:44 -07004305 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004306
Trond Myklebust14516c32010-07-31 14:29:06 -04004307 if (!nfs4_sequence_done(task, &data->res.seq_res))
4308 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004309
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004310 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004311 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004312 if (data->rpc_status == 0)
4313 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4314 else
4315 goto out;
4316 }
4317 if (data->rpc_status == 0) {
4318 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4319 sizeof(data->lsp->ls_stateid.data));
4320 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Al Viro3d4ff432011-06-22 18:40:12 -04004321 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004322 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004323out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004324 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004325}
4326
4327static void nfs4_lock_release(void *calldata)
4328{
4329 struct nfs4_lockdata *data = calldata;
4330
Harvey Harrison3110ff82008-05-02 13:42:44 -07004331 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004332 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004333 if (data->cancelled != 0) {
4334 struct rpc_task *task;
4335 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4336 data->arg.lock_seqid);
4337 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004338 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004339 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004340 } else
4341 nfs_free_seqid(data->arg.lock_seqid);
4342 nfs4_put_lock_state(data->lsp);
4343 put_nfs_open_context(data->ctx);
4344 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004345 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004346}
4347
4348static const struct rpc_call_ops nfs4_lock_ops = {
4349 .rpc_call_prepare = nfs4_lock_prepare,
4350 .rpc_call_done = nfs4_lock_done,
4351 .rpc_release = nfs4_lock_release,
4352};
4353
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004354static const struct rpc_call_ops nfs4_recover_lock_ops = {
4355 .rpc_call_prepare = nfs4_recover_lock_prepare,
4356 .rpc_call_done = nfs4_lock_done,
4357 .rpc_release = nfs4_lock_release,
4358};
4359
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004360static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4361{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004362 switch (error) {
4363 case -NFS4ERR_ADMIN_REVOKED:
4364 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004365 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004366 if (new_lock_owner != 0 ||
4367 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004368 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004369 break;
4370 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004371 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004372 case -NFS4ERR_EXPIRED:
4373 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004374 };
4375}
4376
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004377static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004378{
4379 struct nfs4_lockdata *data;
4380 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004381 struct rpc_message msg = {
4382 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4383 .rpc_cred = state->owner->so_cred,
4384 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004385 struct rpc_task_setup task_setup_data = {
4386 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004387 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004388 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004389 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004390 .flags = RPC_TASK_ASYNC,
4391 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004392 int ret;
4393
Harvey Harrison3110ff82008-05-02 13:42:44 -07004394 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004395 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004396 fl->fl_u.nfs4_fl.owner,
4397 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004398 if (data == NULL)
4399 return -ENOMEM;
4400 if (IS_SETLKW(cmd))
4401 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004402 if (recovery_type > NFS_LOCK_NEW) {
4403 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004404 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004405 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4406 }
Trond Myklebust1174dd12010-12-21 10:52:24 -05004407 msg.rpc_argp = &data->arg;
4408 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004409 task_setup_data.callback_data = data;
4410 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004411 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004412 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004413 ret = nfs4_wait_for_completion_rpc_task(task);
4414 if (ret == 0) {
4415 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004416 if (ret)
4417 nfs4_handle_setlk_error(data->server, data->lsp,
4418 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004419 } else
4420 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004421 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004422 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004423 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424}
4425
4426static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4427{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004428 struct nfs_server *server = NFS_SERVER(state->inode);
4429 struct nfs4_exception exception = { };
4430 int err;
4431
4432 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004433 /* Cache the lock if possible... */
4434 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4435 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004436 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004437 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004438 break;
4439 nfs4_handle_exception(server, err, &exception);
4440 } while (exception.retry);
4441 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442}
4443
4444static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4445{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004446 struct nfs_server *server = NFS_SERVER(state->inode);
4447 struct nfs4_exception exception = { };
4448 int err;
4449
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004450 err = nfs4_set_lock_state(state, request);
4451 if (err != 0)
4452 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004453 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004454 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4455 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004456 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004457 switch (err) {
4458 default:
4459 goto out;
4460 case -NFS4ERR_GRACE:
4461 case -NFS4ERR_DELAY:
4462 nfs4_handle_exception(server, err, &exception);
4463 err = 0;
4464 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004465 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004466out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004467 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468}
4469
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004470#if defined(CONFIG_NFS_V4_1)
4471static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4472{
4473 int status;
4474 struct nfs_server *server = NFS_SERVER(state->inode);
4475
4476 status = nfs41_test_stateid(server, state);
4477 if (status == NFS_OK)
4478 return 0;
4479 nfs41_free_stateid(server, state);
4480 return nfs4_lock_expired(state, request);
4481}
4482#endif
4483
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4485{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004486 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004487 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004488 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004490 if ((fl_flags & FL_POSIX) &&
4491 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4492 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004493 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004494 status = nfs4_set_lock_state(state, request);
4495 if (status != 0)
4496 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004497 request->fl_flags |= FL_ACCESS;
4498 status = do_vfs_lock(request->fl_file, request);
4499 if (status < 0)
4500 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004501 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004502 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004503 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004504 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004505 request->fl_flags = fl_flags & ~FL_SLEEP;
4506 status = do_vfs_lock(request->fl_file, request);
4507 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004508 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004509 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004510 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004511 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004512 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004513 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004514 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004515 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004516out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004517 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004518out:
4519 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 return status;
4521}
4522
4523static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4524{
4525 struct nfs4_exception exception = { };
4526 int err;
4527
4528 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004529 err = _nfs4_proc_setlk(state, cmd, request);
4530 if (err == -NFS4ERR_DENIED)
4531 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004533 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 } while (exception.retry);
4535 return err;
4536}
4537
4538static int
4539nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4540{
4541 struct nfs_open_context *ctx;
4542 struct nfs4_state *state;
4543 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4544 int status;
4545
4546 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004547 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 state = ctx->state;
4549
4550 if (request->fl_start < 0 || request->fl_end < 0)
4551 return -EINVAL;
4552
Trond Myklebustd9531262009-07-21 19:22:38 -04004553 if (IS_GETLK(cmd)) {
4554 if (state != NULL)
4555 return nfs4_proc_getlk(state, F_GETLK, request);
4556 return 0;
4557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558
4559 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4560 return -EINVAL;
4561
Trond Myklebustd9531262009-07-21 19:22:38 -04004562 if (request->fl_type == F_UNLCK) {
4563 if (state != NULL)
4564 return nfs4_proc_unlck(state, cmd, request);
4565 return 0;
4566 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567
Trond Myklebustd9531262009-07-21 19:22:38 -04004568 if (state == NULL)
4569 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 do {
4571 status = nfs4_proc_setlk(state, cmd, request);
4572 if ((status != -EAGAIN) || IS_SETLK(cmd))
4573 break;
4574 timeout = nfs4_set_lock_task_retry(timeout);
4575 status = -ERESTARTSYS;
4576 if (signalled())
4577 break;
4578 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 return status;
4580}
4581
Trond Myklebust888e6942005-11-04 15:38:11 -05004582int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4583{
4584 struct nfs_server *server = NFS_SERVER(state->inode);
4585 struct nfs4_exception exception = { };
4586 int err;
4587
4588 err = nfs4_set_lock_state(state, fl);
4589 if (err != 0)
4590 goto out;
4591 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004592 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004593 switch (err) {
4594 default:
4595 printk(KERN_ERR "%s: unhandled error %d.\n",
4596 __func__, err);
4597 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004598 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004599 goto out;
4600 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004601 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004602 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004603 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004604 nfs4_schedule_lease_recovery(server->nfs_client);
4605 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004606 case -NFS4ERR_BADSESSION:
4607 case -NFS4ERR_BADSLOT:
4608 case -NFS4ERR_BAD_HIGH_SLOT:
4609 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4610 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004611 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004612 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004613 case -ERESTARTSYS:
4614 /*
4615 * The show must go on: exit, but mark the
4616 * stateid as needing recovery.
4617 */
4618 case -NFS4ERR_ADMIN_REVOKED:
4619 case -NFS4ERR_BAD_STATEID:
4620 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004621 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004622 err = 0;
4623 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004624 case -EKEYEXPIRED:
4625 /*
4626 * User RPCSEC_GSS context has expired.
4627 * We cannot recover this stateid now, so
4628 * skip it and allow recovery thread to
4629 * proceed.
4630 */
4631 err = 0;
4632 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004633 case -ENOMEM:
4634 case -NFS4ERR_DENIED:
4635 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4636 err = 0;
4637 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004638 case -NFS4ERR_DELAY:
4639 break;
4640 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004641 err = nfs4_handle_exception(server, err, &exception);
4642 } while (exception.retry);
4643out:
4644 return err;
4645}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004646
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004647static void nfs4_release_lockowner_release(void *calldata)
4648{
4649 kfree(calldata);
4650}
4651
4652const struct rpc_call_ops nfs4_release_lockowner_ops = {
4653 .rpc_release = nfs4_release_lockowner_release,
4654};
4655
4656void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4657{
4658 struct nfs_server *server = lsp->ls_state->owner->so_server;
4659 struct nfs_release_lockowner_args *args;
4660 struct rpc_message msg = {
4661 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4662 };
4663
4664 if (server->nfs_client->cl_mvops->minor_version != 0)
4665 return;
4666 args = kmalloc(sizeof(*args), GFP_NOFS);
4667 if (!args)
4668 return;
4669 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4670 args->lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004671 args->lock_owner.s_dev = server->s_dev;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004672 msg.rpc_argp = args;
4673 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4674}
4675
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004676#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4677
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004678static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4679 const void *buf, size_t buflen,
4680 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004681{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004682 if (strcmp(key, "") != 0)
4683 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004684
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004685 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004686}
4687
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004688static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4689 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004690{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004691 if (strcmp(key, "") != 0)
4692 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004693
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004694 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004695}
4696
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004697static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4698 size_t list_len, const char *name,
4699 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004700{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004701 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004702
J. Bruce Fields096455a2006-03-20 23:23:42 -05004703 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4704 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004705
4706 if (list && len <= list_len)
4707 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004708 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004709}
4710
Andy Adamson533eb462011-06-13 18:25:56 -04004711/*
4712 * nfs_fhget will use either the mounted_on_fileid or the fileid
4713 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004714static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4715{
Andy Adamson533eb462011-06-13 18:25:56 -04004716 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4717 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4718 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4719 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004720 return;
4721
4722 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4723 NFS_ATTR_FATTR_NLINK;
4724 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4725 fattr->nlink = 2;
4726}
4727
Trond Myklebust56659e92007-07-17 21:52:39 -04004728int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004729 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004730{
4731 struct nfs_server *server = NFS_SERVER(dir);
4732 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004733 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004734 };
4735 struct nfs4_fs_locations_arg args = {
4736 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004737 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004738 .page = page,
4739 .bitmask = bitmask,
4740 };
Benny Halevy22958462009-04-01 09:22:02 -04004741 struct nfs4_fs_locations_res res = {
4742 .fs_locations = fs_locations,
4743 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004744 struct rpc_message msg = {
4745 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4746 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004747 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004748 };
4749 int status;
4750
Harvey Harrison3110ff82008-05-02 13:42:44 -07004751 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04004752
4753 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
4754 * is not supported */
4755 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
4756 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
4757 else
4758 bitmask[0] |= FATTR4_WORD0_FILEID;
4759
Trond Myklebustc228fd32007-01-13 02:28:11 -05004760 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004761 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004762 fs_locations->nlocations = 0;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004763 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004764 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004765 return status;
4766}
4767
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00004768static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4769{
4770 int status;
4771 struct nfs4_secinfo_arg args = {
4772 .dir_fh = NFS_FH(dir),
4773 .name = name,
4774 };
4775 struct nfs4_secinfo_res res = {
4776 .flavors = flavors,
4777 };
4778 struct rpc_message msg = {
4779 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
4780 .rpc_argp = &args,
4781 .rpc_resp = &res,
4782 };
4783
4784 dprintk("NFS call secinfo %s\n", name->name);
4785 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4786 dprintk("NFS reply secinfo: %d\n", status);
4787 return status;
4788}
4789
4790int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4791{
4792 struct nfs4_exception exception = { };
4793 int err;
4794 do {
4795 err = nfs4_handle_exception(NFS_SERVER(dir),
4796 _nfs4_proc_secinfo(dir, name, flavors),
4797 &exception);
4798 } while (exception.retry);
4799 return err;
4800}
4801
Andy Adamson557134a2009-04-01 09:21:53 -04004802#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004803/*
Andy Adamson357f54d2010-12-14 10:11:57 -05004804 * Check the exchange flags returned by the server for invalid flags, having
4805 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4806 * DS flags set.
4807 */
4808static int nfs4_check_cl_exchange_flags(u32 flags)
4809{
4810 if (flags & ~EXCHGID4_FLAG_MASK_R)
4811 goto out_inval;
4812 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4813 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4814 goto out_inval;
4815 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4816 goto out_inval;
4817 return NFS_OK;
4818out_inval:
4819 return -NFS4ERR_INVAL;
4820}
4821
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004822static bool
4823nfs41_same_server_scope(struct server_scope *a, struct server_scope *b)
4824{
4825 if (a->server_scope_sz == b->server_scope_sz &&
4826 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
4827 return true;
4828
4829 return false;
4830}
4831
Andy Adamson357f54d2010-12-14 10:11:57 -05004832/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04004833 * nfs4_proc_exchange_id()
4834 *
4835 * Since the clientid has expired, all compounds using sessions
4836 * associated with the stale clientid will be returning
4837 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4838 * be in some phase of session reset.
4839 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004840int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004841{
4842 nfs4_verifier verifier;
4843 struct nfs41_exchange_id_args args = {
4844 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05004845 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04004846 };
4847 struct nfs41_exchange_id_res res = {
4848 .client = clp,
4849 };
4850 int status;
4851 struct rpc_message msg = {
4852 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4853 .rpc_argp = &args,
4854 .rpc_resp = &res,
4855 .rpc_cred = cred,
4856 };
4857 __be32 *p;
4858
4859 dprintk("--> %s\n", __func__);
4860 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004861
Benny Halevy99fe60d2009-04-01 09:22:29 -04004862 p = (u32 *)verifier.data;
4863 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4864 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4865 args.verifier = &verifier;
4866
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004867 args.id_len = scnprintf(args.id, sizeof(args.id),
4868 "%s/%s.%s/%u",
4869 clp->cl_ipaddr,
4870 init_utsname()->nodename,
4871 init_utsname()->domainname,
4872 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004873
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004874 res.server_scope = kzalloc(sizeof(struct server_scope), GFP_KERNEL);
4875 if (unlikely(!res.server_scope))
4876 return -ENOMEM;
4877
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004878 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004879 if (!status)
4880 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004881
4882 if (!status) {
4883 if (clp->server_scope &&
4884 !nfs41_same_server_scope(clp->server_scope,
4885 res.server_scope)) {
4886 dprintk("%s: server_scope mismatch detected\n",
4887 __func__);
4888 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
4889 kfree(clp->server_scope);
4890 clp->server_scope = NULL;
4891 }
4892
4893 if (!clp->server_scope)
4894 clp->server_scope = res.server_scope;
4895 else
4896 kfree(res.server_scope);
4897 }
4898
Benny Halevy99fe60d2009-04-01 09:22:29 -04004899 dprintk("<-- %s status= %d\n", __func__, status);
4900 return status;
4901}
4902
Andy Adamson2050f0c2009-04-01 09:22:30 -04004903struct nfs4_get_lease_time_data {
4904 struct nfs4_get_lease_time_args *args;
4905 struct nfs4_get_lease_time_res *res;
4906 struct nfs_client *clp;
4907};
4908
4909static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4910 void *calldata)
4911{
4912 int ret;
4913 struct nfs4_get_lease_time_data *data =
4914 (struct nfs4_get_lease_time_data *)calldata;
4915
4916 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004917 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004918 /* just setup sequence, do not trigger session recovery
4919 since we're invoked within one */
4920 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004921 &data->args->la_seq_args,
4922 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004923
4924 BUG_ON(ret == -EAGAIN);
4925 rpc_call_start(task);
4926 dprintk("<-- %s\n", __func__);
4927}
4928
4929/*
4930 * Called from nfs4_state_manager thread for session setup, so don't recover
4931 * from sequence operation or clientid errors.
4932 */
4933static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4934{
4935 struct nfs4_get_lease_time_data *data =
4936 (struct nfs4_get_lease_time_data *)calldata;
4937
4938 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04004939 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4940 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04004941 switch (task->tk_status) {
4942 case -NFS4ERR_DELAY:
4943 case -NFS4ERR_GRACE:
4944 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4945 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4946 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004947 /* fall through */
4948 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004949 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004950 return;
4951 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004952 dprintk("<-- %s\n", __func__);
4953}
4954
4955struct rpc_call_ops nfs4_get_lease_time_ops = {
4956 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4957 .rpc_call_done = nfs4_get_lease_time_done,
4958};
4959
4960int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4961{
4962 struct rpc_task *task;
4963 struct nfs4_get_lease_time_args args;
4964 struct nfs4_get_lease_time_res res = {
4965 .lr_fsinfo = fsinfo,
4966 };
4967 struct nfs4_get_lease_time_data data = {
4968 .args = &args,
4969 .res = &res,
4970 .clp = clp,
4971 };
4972 struct rpc_message msg = {
4973 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4974 .rpc_argp = &args,
4975 .rpc_resp = &res,
4976 };
4977 struct rpc_task_setup task_setup = {
4978 .rpc_client = clp->cl_rpcclient,
4979 .rpc_message = &msg,
4980 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004981 .callback_data = &data,
4982 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04004983 };
4984 int status;
4985
Andy Adamson2050f0c2009-04-01 09:22:30 -04004986 dprintk("--> %s\n", __func__);
4987 task = rpc_run_task(&task_setup);
4988
4989 if (IS_ERR(task))
4990 status = PTR_ERR(task);
4991 else {
4992 status = task->tk_status;
4993 rpc_put_task(task);
4994 }
4995 dprintk("<-- %s return %d\n", __func__, status);
4996
4997 return status;
4998}
4999
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005000/*
5001 * Reset a slot table
5002 */
Andy Adamson104aeba2010-01-14 17:45:10 -05005003static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
5004 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005005{
Andy Adamson104aeba2010-01-14 17:45:10 -05005006 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005007 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005008 int ret = 0;
5009
Andy Adamson104aeba2010-01-14 17:45:10 -05005010 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
5011 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005012
Andy Adamson104aeba2010-01-14 17:45:10 -05005013 /* Does the newly negotiated max_reqs match the existing slot table? */
5014 if (max_reqs != tbl->max_slots) {
5015 ret = -ENOMEM;
5016 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005017 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005018 if (!new)
5019 goto out;
5020 ret = 0;
5021 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005022 }
5023 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05005024 if (new) {
5025 tbl->slots = new;
5026 tbl->max_slots = max_reqs;
5027 }
5028 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005029 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005030 spin_unlock(&tbl->slot_tbl_lock);
5031 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5032 tbl, tbl->slots, tbl->max_slots);
5033out:
5034 dprintk("<-- %s: return %d\n", __func__, ret);
5035 return ret;
5036}
5037
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005038/* Destroy the slot table */
5039static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5040{
5041 if (session->fc_slot_table.slots != NULL) {
5042 kfree(session->fc_slot_table.slots);
5043 session->fc_slot_table.slots = NULL;
5044 }
5045 if (session->bc_slot_table.slots != NULL) {
5046 kfree(session->bc_slot_table.slots);
5047 session->bc_slot_table.slots = NULL;
5048 }
5049 return;
5050}
5051
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005052/*
Andy Adamsonfc931582009-04-01 09:22:31 -04005053 * Initialize slot table
5054 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005055static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
5056 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04005057{
Andy Adamsonfc931582009-04-01 09:22:31 -04005058 struct nfs4_slot *slot;
5059 int ret = -ENOMEM;
5060
5061 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
5062
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005063 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04005064
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005065 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04005066 if (!slot)
5067 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04005068 ret = 0;
5069
5070 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04005071 tbl->max_slots = max_slots;
5072 tbl->slots = slot;
5073 tbl->highest_used_slotid = -1; /* no slot is currently used */
5074 spin_unlock(&tbl->slot_tbl_lock);
5075 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5076 tbl, tbl->slots, tbl->max_slots);
5077out:
5078 dprintk("<-- %s: return %d\n", __func__, ret);
5079 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04005080}
5081
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005082/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005083 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005084 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005085static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005086{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005087 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005088 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005089
Andy Adamsonaacd5532011-11-09 13:58:21 -05005090 dprintk("--> %s\n", __func__);
5091 /* Fore channel */
5092 tbl = &ses->fc_slot_table;
Trond Myklebustf26468f2009-12-05 19:32:11 -05005093 if (tbl->slots == NULL) {
Andy Adamsonaacd5532011-11-09 13:58:21 -05005094 status = nfs4_init_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5095 if (status) /* -ENOMEM */
5096 return status;
5097 } else {
5098 status = nfs4_reset_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
Trond Myklebustf26468f2009-12-05 19:32:11 -05005099 if (status)
5100 return status;
5101 }
Andy Adamsonaacd5532011-11-09 13:58:21 -05005102 /* Back channel */
5103 tbl = &ses->bc_slot_table;
Trond Myklebustf26468f2009-12-05 19:32:11 -05005104 if (tbl->slots == NULL) {
Andy Adamsonaacd5532011-11-09 13:58:21 -05005105 status = nfs4_init_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
Trond Myklebustf26468f2009-12-05 19:32:11 -05005106 if (status)
Andy Adamsonaacd5532011-11-09 13:58:21 -05005107 /* Fore and back channel share a connection so get
5108 * both slot tables or neither */
5109 nfs4_destroy_slot_tables(ses);
5110 } else
5111 status = nfs4_reset_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005112 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005113}
5114
5115struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5116{
5117 struct nfs4_session *session;
5118 struct nfs4_slot_table *tbl;
5119
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005120 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005121 if (!session)
5122 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005123
Andy Adamson557134a2009-04-01 09:21:53 -04005124 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05005125 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04005126 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005127 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005128 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005129
5130 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05005131 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005132 spin_lock_init(&tbl->slot_tbl_lock);
5133 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005134 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005135
Trond Myklebust1055d762010-06-16 09:52:27 -04005136 session->session_state = 1<<NFS4_SESSION_INITING;
5137
Andy Adamson557134a2009-04-01 09:21:53 -04005138 session->clp = clp;
5139 return session;
5140}
5141
5142void nfs4_destroy_session(struct nfs4_session *session)
5143{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005144 nfs4_proc_destroy_session(session);
5145 dprintk("%s Destroy backchannel for xprt %p\n",
5146 __func__, session->clp->cl_rpcclient->cl_xprt);
5147 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
5148 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005149 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005150 kfree(session);
5151}
5152
Andy Adamsonfc931582009-04-01 09:22:31 -04005153/*
5154 * Initialize the values to be used by the client in CREATE_SESSION
5155 * If nfs4_init_session set the fore channel request and response sizes,
5156 * use them.
5157 *
5158 * Set the back channel max_resp_sz_cached to zero to force the client to
5159 * always set csa_cachethis to FALSE because the current implementation
5160 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5161 */
5162static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5163{
5164 struct nfs4_session *session = args->client->cl_session;
5165 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5166 mxresp_sz = session->fc_attrs.max_resp_sz;
5167
5168 if (mxrqst_sz == 0)
5169 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5170 if (mxresp_sz == 0)
5171 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5172 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005173 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5174 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005175 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5176 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
5177
5178 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005179 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005180 __func__,
5181 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005182 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005183
5184 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005185 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5186 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5187 args->bc_attrs.max_resp_sz_cached = 0;
5188 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5189 args->bc_attrs.max_reqs = 1;
5190
5191 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5192 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5193 __func__,
5194 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5195 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5196 args->bc_attrs.max_reqs);
5197}
5198
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005199static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005200{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005201 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5202 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5203
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005204 if (rcvd->max_resp_sz > sent->max_resp_sz)
5205 return -EINVAL;
5206 /*
5207 * Our requested max_ops is the minimum we need; we're not
5208 * prepared to break up compounds into smaller pieces than that.
5209 * So, no point even trying to continue if the server won't
5210 * cooperate:
5211 */
5212 if (rcvd->max_ops < sent->max_ops)
5213 return -EINVAL;
5214 if (rcvd->max_reqs == 0)
5215 return -EINVAL;
5216 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005217}
5218
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005219static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5220{
5221 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5222 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005223
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005224 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5225 return -EINVAL;
5226 if (rcvd->max_resp_sz < sent->max_resp_sz)
5227 return -EINVAL;
5228 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5229 return -EINVAL;
5230 /* These would render the backchannel useless: */
5231 if (rcvd->max_ops == 0)
5232 return -EINVAL;
5233 if (rcvd->max_reqs == 0)
5234 return -EINVAL;
5235 return 0;
5236}
Andy Adamson8d353012009-04-01 09:22:32 -04005237
Andy Adamson8d353012009-04-01 09:22:32 -04005238static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5239 struct nfs4_session *session)
5240{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005241 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005242
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005243 ret = nfs4_verify_fore_channel_attrs(args, session);
5244 if (ret)
5245 return ret;
5246 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005247}
5248
Andy Adamsonfc931582009-04-01 09:22:31 -04005249static int _nfs4_proc_create_session(struct nfs_client *clp)
5250{
5251 struct nfs4_session *session = clp->cl_session;
5252 struct nfs41_create_session_args args = {
5253 .client = clp,
5254 .cb_program = NFS4_CALLBACK,
5255 };
5256 struct nfs41_create_session_res res = {
5257 .client = clp,
5258 };
5259 struct rpc_message msg = {
5260 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5261 .rpc_argp = &args,
5262 .rpc_resp = &res,
5263 };
5264 int status;
5265
5266 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005267 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005268
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005269 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005270
Andy Adamson8d353012009-04-01 09:22:32 -04005271 if (!status)
5272 /* Verify the session's negotiated channel_attrs values */
5273 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005274 if (!status) {
5275 /* Increment the clientid slot sequence id */
5276 clp->cl_seqid++;
5277 }
5278
5279 return status;
5280}
5281
5282/*
5283 * Issues a CREATE_SESSION operation to the server.
5284 * It is the responsibility of the caller to verify the session is
5285 * expired before calling this routine.
5286 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005287int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005288{
5289 int status;
5290 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005291 struct nfs4_session *session = clp->cl_session;
5292
5293 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5294
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005295 status = _nfs4_proc_create_session(clp);
Andy Adamsonfc931582009-04-01 09:22:31 -04005296 if (status)
5297 goto out;
5298
Andy Adamsonaacd5532011-11-09 13:58:21 -05005299 /* Init or reset the session slot tables */
5300 status = nfs4_setup_session_slot_tables(session);
5301 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005302 if (status)
5303 goto out;
5304
5305 ptr = (unsigned *)&session->sess_id.data[0];
5306 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5307 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005308out:
5309 dprintk("<-- %s\n", __func__);
5310 return status;
5311}
5312
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005313/*
5314 * Issue the over-the-wire RPC DESTROY_SESSION.
5315 * The caller must serialize access to this routine.
5316 */
5317int nfs4_proc_destroy_session(struct nfs4_session *session)
5318{
5319 int status = 0;
5320 struct rpc_message msg;
5321
5322 dprintk("--> nfs4_proc_destroy_session\n");
5323
5324 /* session is still being setup */
5325 if (session->clp->cl_cons_state != NFS_CS_READY)
5326 return status;
5327
5328 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5329 msg.rpc_argp = session;
5330 msg.rpc_resp = NULL;
5331 msg.rpc_cred = NULL;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005332 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005333
5334 if (status)
5335 printk(KERN_WARNING
5336 "Got error %d from the server on DESTROY_SESSION. "
5337 "Session has been destroyed regardless...\n", status);
5338
5339 dprintk("<-- nfs4_proc_destroy_session\n");
5340 return status;
5341}
5342
Trond Myklebustfccba802009-07-21 16:48:07 -04005343int nfs4_init_session(struct nfs_server *server)
5344{
5345 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005346 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005347 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005348 int ret;
5349
5350 if (!nfs4_has_session(clp))
5351 return 0;
5352
Trond Myklebust1055d762010-06-16 09:52:27 -04005353 session = clp->cl_session;
5354 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5355 return 0;
5356
Andy Adamson68bf05e2009-12-15 12:55:02 -05005357 rsize = server->rsize;
5358 if (rsize == 0)
5359 rsize = NFS_MAX_FILE_IO_SIZE;
5360 wsize = server->wsize;
5361 if (wsize == 0)
5362 wsize = NFS_MAX_FILE_IO_SIZE;
5363
Andy Adamson68bf05e2009-12-15 12:55:02 -05005364 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5365 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005366
Trond Myklebustfccba802009-07-21 16:48:07 -04005367 ret = nfs4_recover_expired_lease(server);
5368 if (!ret)
5369 ret = nfs4_check_client_ready(clp);
5370 return ret;
5371}
5372
Andy Adamsond83217c2011-03-01 01:34:17 +00005373int nfs4_init_ds_session(struct nfs_client *clp)
5374{
5375 struct nfs4_session *session = clp->cl_session;
5376 int ret;
5377
5378 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5379 return 0;
5380
5381 ret = nfs4_client_recover_expired_lease(clp);
5382 if (!ret)
5383 /* Test for the DS role */
5384 if (!is_ds_client(clp))
5385 ret = -ENODEV;
5386 if (!ret)
5387 ret = nfs4_check_client_ready(clp);
5388 return ret;
5389
5390}
5391EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5392
5393
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005394/*
5395 * Renew the cl_session lease.
5396 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005397struct nfs4_sequence_data {
5398 struct nfs_client *clp;
5399 struct nfs4_sequence_args args;
5400 struct nfs4_sequence_res res;
5401};
5402
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005403static void nfs41_sequence_release(void *data)
5404{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005405 struct nfs4_sequence_data *calldata = data;
5406 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005407
Alexandros Batsakis71358402010-02-05 03:45:05 -08005408 if (atomic_read(&clp->cl_count) > 1)
5409 nfs4_schedule_state_renewal(clp);
5410 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005411 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005412}
5413
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005414static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5415{
5416 switch(task->tk_status) {
5417 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005418 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5419 return -EAGAIN;
5420 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005421 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005422 }
5423 return 0;
5424}
5425
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005426static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005427{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005428 struct nfs4_sequence_data *calldata = data;
5429 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005430
Trond Myklebust14516c32010-07-31 14:29:06 -04005431 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5432 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005433
5434 if (task->tk_status < 0) {
5435 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005436 if (atomic_read(&clp->cl_count) == 1)
5437 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005438
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005439 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5440 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005441 return;
5442 }
5443 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005444 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005445out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005446 dprintk("<-- %s\n", __func__);
5447}
5448
5449static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5450{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005451 struct nfs4_sequence_data *calldata = data;
5452 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005453 struct nfs4_sequence_args *args;
5454 struct nfs4_sequence_res *res;
5455
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005456 args = task->tk_msg.rpc_argp;
5457 res = task->tk_msg.rpc_resp;
5458
Trond Myklebust035168a2010-06-16 09:52:26 -04005459 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005460 return;
5461 rpc_call_start(task);
5462}
5463
5464static const struct rpc_call_ops nfs41_sequence_ops = {
5465 .rpc_call_done = nfs41_sequence_call_done,
5466 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005467 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005468};
5469
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005470static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005471{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005472 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005473 struct rpc_message msg = {
5474 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5475 .rpc_cred = cred,
5476 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005477 struct rpc_task_setup task_setup_data = {
5478 .rpc_client = clp->cl_rpcclient,
5479 .rpc_message = &msg,
5480 .callback_ops = &nfs41_sequence_ops,
5481 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5482 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005483
Alexandros Batsakis71358402010-02-05 03:45:05 -08005484 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005485 return ERR_PTR(-EIO);
Benny Halevydfb4f302010-09-24 09:17:01 -04005486 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005487 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005488 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005489 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005490 }
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005491 msg.rpc_argp = &calldata->args;
5492 msg.rpc_resp = &calldata->res;
5493 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005494 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005495
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005496 return rpc_run_task(&task_setup_data);
5497}
5498
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005499static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005500{
5501 struct rpc_task *task;
5502 int ret = 0;
5503
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005504 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5505 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005506 task = _nfs41_proc_sequence(clp, cred);
5507 if (IS_ERR(task))
5508 ret = PTR_ERR(task);
5509 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005510 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005511 dprintk("<-- %s status=%d\n", __func__, ret);
5512 return ret;
5513}
5514
5515static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5516{
5517 struct rpc_task *task;
5518 int ret;
5519
5520 task = _nfs41_proc_sequence(clp, cred);
5521 if (IS_ERR(task)) {
5522 ret = PTR_ERR(task);
5523 goto out;
5524 }
5525 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005526 if (!ret) {
5527 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5528
5529 if (task->tk_status == 0)
5530 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005531 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005532 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005533 rpc_put_task(task);
5534out:
5535 dprintk("<-- %s status=%d\n", __func__, ret);
5536 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005537}
5538
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005539struct nfs4_reclaim_complete_data {
5540 struct nfs_client *clp;
5541 struct nfs41_reclaim_complete_args arg;
5542 struct nfs41_reclaim_complete_res res;
5543};
5544
5545static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5546{
5547 struct nfs4_reclaim_complete_data *calldata = data;
5548
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005549 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005550 if (nfs41_setup_sequence(calldata->clp->cl_session,
5551 &calldata->arg.seq_args,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005552 &calldata->res.seq_res, 0, task))
5553 return;
5554
5555 rpc_call_start(task);
5556}
5557
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005558static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5559{
5560 switch(task->tk_status) {
5561 case 0:
5562 case -NFS4ERR_COMPLETE_ALREADY:
5563 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5564 break;
5565 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005566 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005567 /* fall through */
5568 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005569 return -EAGAIN;
5570 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005571 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005572 }
5573 return 0;
5574}
5575
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005576static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5577{
5578 struct nfs4_reclaim_complete_data *calldata = data;
5579 struct nfs_client *clp = calldata->clp;
5580 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5581
5582 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005583 if (!nfs41_sequence_done(task, res))
5584 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005585
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005586 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5587 rpc_restart_call_prepare(task);
5588 return;
5589 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005590 dprintk("<-- %s\n", __func__);
5591}
5592
5593static void nfs4_free_reclaim_complete_data(void *data)
5594{
5595 struct nfs4_reclaim_complete_data *calldata = data;
5596
5597 kfree(calldata);
5598}
5599
5600static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5601 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5602 .rpc_call_done = nfs4_reclaim_complete_done,
5603 .rpc_release = nfs4_free_reclaim_complete_data,
5604};
5605
5606/*
5607 * Issue a global reclaim complete.
5608 */
5609static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5610{
5611 struct nfs4_reclaim_complete_data *calldata;
5612 struct rpc_task *task;
5613 struct rpc_message msg = {
5614 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5615 };
5616 struct rpc_task_setup task_setup_data = {
5617 .rpc_client = clp->cl_rpcclient,
5618 .rpc_message = &msg,
5619 .callback_ops = &nfs4_reclaim_complete_call_ops,
5620 .flags = RPC_TASK_ASYNC,
5621 };
5622 int status = -ENOMEM;
5623
5624 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005625 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005626 if (calldata == NULL)
5627 goto out;
5628 calldata->clp = clp;
5629 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005630
5631 msg.rpc_argp = &calldata->arg;
5632 msg.rpc_resp = &calldata->res;
5633 task_setup_data.callback_data = calldata;
5634 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005635 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005636 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005637 goto out;
5638 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005639 status = nfs4_wait_for_completion_rpc_task(task);
5640 if (status == 0)
5641 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005642 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005643 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005644out:
5645 dprintk("<-- %s status=%d\n", __func__, status);
5646 return status;
5647}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005648
5649static void
5650nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5651{
5652 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005653 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005654
5655 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005656 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5657 * right now covering the LAYOUTGET we are about to send.
5658 * However, that is not so catastrophic, and there seems
5659 * to be no way to prevent it completely.
5660 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005661 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5662 &lgp->res.seq_res, 0, task))
5663 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005664 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5665 NFS_I(lgp->args.inode)->layout,
5666 lgp->args.ctx->state)) {
5667 rpc_exit(task, NFS4_OK);
5668 return;
5669 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005670 rpc_call_start(task);
5671}
5672
5673static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5674{
5675 struct nfs4_layoutget *lgp = calldata;
5676 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5677
5678 dprintk("--> %s\n", __func__);
5679
5680 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5681 return;
5682
5683 switch (task->tk_status) {
5684 case 0:
5685 break;
5686 case -NFS4ERR_LAYOUTTRYLATER:
5687 case -NFS4ERR_RECALLCONFLICT:
5688 task->tk_status = -NFS4ERR_DELAY;
5689 /* Fall through */
5690 default:
5691 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5692 rpc_restart_call_prepare(task);
5693 return;
5694 }
5695 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005696 dprintk("<-- %s\n", __func__);
5697}
5698
5699static void nfs4_layoutget_release(void *calldata)
5700{
5701 struct nfs4_layoutget *lgp = calldata;
5702
5703 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005704 put_nfs_open_context(lgp->args.ctx);
5705 kfree(calldata);
5706 dprintk("<-- %s\n", __func__);
5707}
5708
5709static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5710 .rpc_call_prepare = nfs4_layoutget_prepare,
5711 .rpc_call_done = nfs4_layoutget_done,
5712 .rpc_release = nfs4_layoutget_release,
5713};
5714
5715int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5716{
5717 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5718 struct rpc_task *task;
5719 struct rpc_message msg = {
5720 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5721 .rpc_argp = &lgp->args,
5722 .rpc_resp = &lgp->res,
5723 };
5724 struct rpc_task_setup task_setup_data = {
5725 .rpc_client = server->client,
5726 .rpc_message = &msg,
5727 .callback_ops = &nfs4_layoutget_call_ops,
5728 .callback_data = lgp,
5729 .flags = RPC_TASK_ASYNC,
5730 };
5731 int status = 0;
5732
5733 dprintk("--> %s\n", __func__);
5734
Weston Andros Adamson35124a02011-03-24 16:48:21 -04005735 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005736 lgp->res.seq_res.sr_slot = NULL;
5737 task = rpc_run_task(&task_setup_data);
5738 if (IS_ERR(task))
5739 return PTR_ERR(task);
5740 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00005741 if (status == 0)
5742 status = task->tk_status;
5743 if (status == 0)
5744 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005745 rpc_put_task(task);
5746 dprintk("<-- %s status=%d\n", __func__, status);
5747 return status;
5748}
5749
Benny Halevycbe82602011-05-22 19:52:37 +03005750static void
5751nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
5752{
5753 struct nfs4_layoutreturn *lrp = calldata;
5754
5755 dprintk("--> %s\n", __func__);
5756 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
5757 &lrp->res.seq_res, 0, task))
5758 return;
5759 rpc_call_start(task);
5760}
5761
5762static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
5763{
5764 struct nfs4_layoutreturn *lrp = calldata;
5765 struct nfs_server *server;
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005766 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03005767
5768 dprintk("--> %s\n", __func__);
5769
5770 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
5771 return;
5772
5773 server = NFS_SERVER(lrp->args.inode);
5774 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005775 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03005776 return;
5777 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005778 spin_lock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005779 if (task->tk_status == 0) {
Benny Halevycbe82602011-05-22 19:52:37 +03005780 if (lrp->res.lrs_present) {
Benny Halevycbe82602011-05-22 19:52:37 +03005781 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Benny Halevycbe82602011-05-22 19:52:37 +03005782 } else
5783 BUG_ON(!list_empty(&lo->plh_segs));
5784 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005785 lo->plh_block_lgets--;
5786 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005787 dprintk("<-- %s\n", __func__);
5788}
5789
5790static void nfs4_layoutreturn_release(void *calldata)
5791{
5792 struct nfs4_layoutreturn *lrp = calldata;
5793
5794 dprintk("--> %s\n", __func__);
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005795 put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03005796 kfree(calldata);
5797 dprintk("<-- %s\n", __func__);
5798}
5799
5800static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
5801 .rpc_call_prepare = nfs4_layoutreturn_prepare,
5802 .rpc_call_done = nfs4_layoutreturn_done,
5803 .rpc_release = nfs4_layoutreturn_release,
5804};
5805
5806int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
5807{
5808 struct rpc_task *task;
5809 struct rpc_message msg = {
5810 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
5811 .rpc_argp = &lrp->args,
5812 .rpc_resp = &lrp->res,
5813 };
5814 struct rpc_task_setup task_setup_data = {
5815 .rpc_client = lrp->clp->cl_rpcclient,
5816 .rpc_message = &msg,
5817 .callback_ops = &nfs4_layoutreturn_call_ops,
5818 .callback_data = lrp,
5819 };
5820 int status;
5821
5822 dprintk("--> %s\n", __func__);
5823 task = rpc_run_task(&task_setup_data);
5824 if (IS_ERR(task))
5825 return PTR_ERR(task);
5826 status = task->tk_status;
5827 dprintk("<-- %s status=%d\n", __func__, status);
5828 rpc_put_task(task);
5829 return status;
5830}
5831
Andy Adamson7f11d8d2011-07-30 20:52:35 -04005832/*
5833 * Retrieve the list of Data Server devices from the MDS.
5834 */
5835static int _nfs4_getdevicelist(struct nfs_server *server,
5836 const struct nfs_fh *fh,
5837 struct pnfs_devicelist *devlist)
5838{
5839 struct nfs4_getdevicelist_args args = {
5840 .fh = fh,
5841 .layoutclass = server->pnfs_curr_ld->id,
5842 };
5843 struct nfs4_getdevicelist_res res = {
5844 .devlist = devlist,
5845 };
5846 struct rpc_message msg = {
5847 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
5848 .rpc_argp = &args,
5849 .rpc_resp = &res,
5850 };
5851 int status;
5852
5853 dprintk("--> %s\n", __func__);
5854 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5855 &res.seq_res, 0);
5856 dprintk("<-- %s status=%d\n", __func__, status);
5857 return status;
5858}
5859
5860int nfs4_proc_getdevicelist(struct nfs_server *server,
5861 const struct nfs_fh *fh,
5862 struct pnfs_devicelist *devlist)
5863{
5864 struct nfs4_exception exception = { };
5865 int err;
5866
5867 do {
5868 err = nfs4_handle_exception(server,
5869 _nfs4_getdevicelist(server, fh, devlist),
5870 &exception);
5871 } while (exception.retry);
5872
5873 dprintk("%s: err=%d, num_devs=%u\n", __func__,
5874 err, devlist->num_devs);
5875
5876 return err;
5877}
5878EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
5879
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005880static int
5881_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5882{
5883 struct nfs4_getdeviceinfo_args args = {
5884 .pdev = pdev,
5885 };
5886 struct nfs4_getdeviceinfo_res res = {
5887 .pdev = pdev,
5888 };
5889 struct rpc_message msg = {
5890 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5891 .rpc_argp = &args,
5892 .rpc_resp = &res,
5893 };
5894 int status;
5895
5896 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005897 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005898 dprintk("<-- %s status=%d\n", __func__, status);
5899
5900 return status;
5901}
5902
5903int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5904{
5905 struct nfs4_exception exception = { };
5906 int err;
5907
5908 do {
5909 err = nfs4_handle_exception(server,
5910 _nfs4_proc_getdeviceinfo(server, pdev),
5911 &exception);
5912 } while (exception.retry);
5913 return err;
5914}
5915EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5916
Andy Adamson863a3c62011-03-23 13:27:54 +00005917static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
5918{
5919 struct nfs4_layoutcommit_data *data = calldata;
5920 struct nfs_server *server = NFS_SERVER(data->args.inode);
5921
5922 if (nfs4_setup_sequence(server, &data->args.seq_args,
5923 &data->res.seq_res, 1, task))
5924 return;
5925 rpc_call_start(task);
5926}
5927
5928static void
5929nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
5930{
5931 struct nfs4_layoutcommit_data *data = calldata;
5932 struct nfs_server *server = NFS_SERVER(data->args.inode);
5933
5934 if (!nfs4_sequence_done(task, &data->res.seq_res))
5935 return;
5936
5937 switch (task->tk_status) { /* Just ignore these failures */
5938 case NFS4ERR_DELEG_REVOKED: /* layout was recalled */
5939 case NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
5940 case NFS4ERR_BADLAYOUT: /* no layout */
5941 case NFS4ERR_GRACE: /* loca_recalim always false */
5942 task->tk_status = 0;
5943 }
5944
5945 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005946 rpc_restart_call_prepare(task);
Andy Adamson863a3c62011-03-23 13:27:54 +00005947 return;
5948 }
5949
5950 if (task->tk_status == 0)
5951 nfs_post_op_update_inode_force_wcc(data->args.inode,
5952 data->res.fattr);
5953}
5954
5955static void nfs4_layoutcommit_release(void *calldata)
5956{
5957 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04005958 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07005959 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00005960
Andy Adamsondb29c082011-07-30 20:52:38 -04005961 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00005962 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04005963 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
5964 list_del_init(&lseg->pls_lc_list);
5965 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
5966 &lseg->pls_flags))
5967 put_lseg(lseg);
5968 }
Peng Tao92407e72011-10-23 20:21:17 -07005969
5970 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
5971 smp_mb__after_clear_bit();
5972 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
5973
Andy Adamson863a3c62011-03-23 13:27:54 +00005974 put_rpccred(data->cred);
5975 kfree(data);
5976}
5977
5978static const struct rpc_call_ops nfs4_layoutcommit_ops = {
5979 .rpc_call_prepare = nfs4_layoutcommit_prepare,
5980 .rpc_call_done = nfs4_layoutcommit_done,
5981 .rpc_release = nfs4_layoutcommit_release,
5982};
5983
5984int
Andy Adamsonef311532011-03-12 02:58:10 -05005985nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00005986{
5987 struct rpc_message msg = {
5988 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
5989 .rpc_argp = &data->args,
5990 .rpc_resp = &data->res,
5991 .rpc_cred = data->cred,
5992 };
5993 struct rpc_task_setup task_setup_data = {
5994 .task = &data->task,
5995 .rpc_client = NFS_CLIENT(data->args.inode),
5996 .rpc_message = &msg,
5997 .callback_ops = &nfs4_layoutcommit_ops,
5998 .callback_data = data,
5999 .flags = RPC_TASK_ASYNC,
6000 };
6001 struct rpc_task *task;
6002 int status = 0;
6003
6004 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
6005 "lbw: %llu inode %lu\n",
6006 data->task.tk_pid, sync,
6007 data->args.lastbytewritten,
6008 data->args.inode->i_ino);
6009
6010 task = rpc_run_task(&task_setup_data);
6011 if (IS_ERR(task))
6012 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05006013 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006014 goto out;
6015 status = nfs4_wait_for_completion_rpc_task(task);
6016 if (status != 0)
6017 goto out;
6018 status = task->tk_status;
6019out:
6020 dprintk("%s: status %d\n", __func__, status);
6021 rpc_put_task(task);
6022 return status;
6023}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006024
6025static int
6026_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6027 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6028{
6029 struct nfs41_secinfo_no_name_args args = {
6030 .style = SECINFO_STYLE_CURRENT_FH,
6031 };
6032 struct nfs4_secinfo_res res = {
6033 .flavors = flavors,
6034 };
6035 struct rpc_message msg = {
6036 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6037 .rpc_argp = &args,
6038 .rpc_resp = &res,
6039 };
6040 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6041}
6042
6043static int
6044nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6045 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6046{
6047 struct nfs4_exception exception = { };
6048 int err;
6049 do {
6050 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6051 switch (err) {
6052 case 0:
6053 case -NFS4ERR_WRONGSEC:
6054 case -NFS4ERR_NOTSUPP:
6055 break;
6056 default:
6057 err = nfs4_handle_exception(server, err, &exception);
6058 }
6059 } while (exception.retry);
6060 return err;
6061}
6062
6063static int
6064nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6065 struct nfs_fsinfo *info)
6066{
6067 int err;
6068 struct page *page;
6069 rpc_authflavor_t flavor;
6070 struct nfs4_secinfo_flavors *flavors;
6071
6072 page = alloc_page(GFP_KERNEL);
6073 if (!page) {
6074 err = -ENOMEM;
6075 goto out;
6076 }
6077
6078 flavors = page_address(page);
6079 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6080
6081 /*
6082 * Fall back on "guess and check" method if
6083 * the server doesn't support SECINFO_NO_NAME
6084 */
6085 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6086 err = nfs4_find_root_sec(server, fhandle, info);
6087 goto out_freepage;
6088 }
6089 if (err)
6090 goto out_freepage;
6091
6092 flavor = nfs_find_best_sec(flavors);
6093 if (err == 0)
6094 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6095
6096out_freepage:
6097 put_page(page);
6098 if (err == -EACCES)
6099 return -EPERM;
6100out:
6101 return err;
6102}
Bryan Schumaker7d974792011-06-02 14:59:08 -04006103static int _nfs41_test_stateid(struct nfs_server *server, struct nfs4_state *state)
6104{
6105 int status;
6106 struct nfs41_test_stateid_args args = {
6107 .stateid = &state->stateid,
6108 };
6109 struct nfs41_test_stateid_res res;
6110 struct rpc_message msg = {
6111 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6112 .rpc_argp = &args,
6113 .rpc_resp = &res,
6114 };
6115 args.seq_args.sa_session = res.seq_res.sr_session = NULL;
6116 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 0, 1);
6117 return status;
6118}
6119
6120static int nfs41_test_stateid(struct nfs_server *server, struct nfs4_state *state)
6121{
6122 struct nfs4_exception exception = { };
6123 int err;
6124 do {
6125 err = nfs4_handle_exception(server,
6126 _nfs41_test_stateid(server, state),
6127 &exception);
6128 } while (exception.retry);
6129 return err;
6130}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006131
6132static int _nfs4_free_stateid(struct nfs_server *server, struct nfs4_state *state)
6133{
6134 int status;
6135 struct nfs41_free_stateid_args args = {
6136 .stateid = &state->stateid,
6137 };
6138 struct nfs41_free_stateid_res res;
6139 struct rpc_message msg = {
6140 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6141 .rpc_argp = &args,
6142 .rpc_resp = &res,
6143 };
6144
6145 args.seq_args.sa_session = res.seq_res.sr_session = NULL;
6146 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 0, 1);
6147 return status;
6148}
6149
6150static int nfs41_free_stateid(struct nfs_server *server, struct nfs4_state *state)
6151{
6152 struct nfs4_exception exception = { };
6153 int err;
6154 do {
6155 err = nfs4_handle_exception(server,
6156 _nfs4_free_stateid(server, state),
6157 &exception);
6158 } while (exception.retry);
6159 return err;
6160}
Andy Adamson557134a2009-04-01 09:21:53 -04006161#endif /* CONFIG_NFS_V4_1 */
6162
Andy Adamson591d71c2009-04-01 09:22:47 -04006163struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006164 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006165 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006166 .recover_open = nfs4_open_reclaim,
6167 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006168 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006169 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170};
6171
Andy Adamson591d71c2009-04-01 09:22:47 -04006172#if defined(CONFIG_NFS_V4_1)
6173struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6174 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6175 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6176 .recover_open = nfs4_open_reclaim,
6177 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006178 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006179 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006180 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006181};
6182#endif /* CONFIG_NFS_V4_1 */
6183
6184struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006185 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006186 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187 .recover_open = nfs4_open_expired,
6188 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006189 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006190 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006191};
6192
Andy Adamson591d71c2009-04-01 09:22:47 -04006193#if defined(CONFIG_NFS_V4_1)
6194struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6195 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6196 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006197 .recover_open = nfs41_open_expired,
6198 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006199 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006200 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006201};
6202#endif /* CONFIG_NFS_V4_1 */
6203
Benny Halevy29fba382009-04-01 09:22:44 -04006204struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6205 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006206 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006207 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006208};
6209
6210#if defined(CONFIG_NFS_V4_1)
6211struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
6212 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006213 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006214 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006215};
6216#endif
6217
Trond Myklebust97dc1352010-06-16 09:52:26 -04006218static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6219 .minor_version = 0,
6220 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04006221 .validate_stateid = nfs4_validate_delegation_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006222 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006223 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6224 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6225 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006226};
6227
6228#if defined(CONFIG_NFS_V4_1)
6229static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6230 .minor_version = 1,
6231 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04006232 .validate_stateid = nfs41_validate_delegation_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006233 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006234 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6235 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6236 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006237};
6238#endif
6239
Trond Myklebust97dc1352010-06-16 09:52:26 -04006240const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6241 [0] = &nfs_v4_0_minor_ops,
6242#if defined(CONFIG_NFS_V4_1)
6243 [1] = &nfs_v4_1_minor_ops,
6244#endif
6245};
6246
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006247static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006248 .permission = nfs_permission,
6249 .getattr = nfs_getattr,
6250 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006251 .getxattr = generic_getxattr,
6252 .setxattr = generic_setxattr,
6253 .listxattr = generic_listxattr,
6254 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006255};
6256
David Howells509de812006-08-22 20:06:11 -04006257const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006258 .version = 4, /* protocol version */
6259 .dentry_ops = &nfs4_dentry_operations,
6260 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006261 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006262 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006263 .getroot = nfs4_proc_get_root,
6264 .getattr = nfs4_proc_getattr,
6265 .setattr = nfs4_proc_setattr,
6266 .lookup = nfs4_proc_lookup,
6267 .access = nfs4_proc_access,
6268 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006269 .create = nfs4_proc_create,
6270 .remove = nfs4_proc_remove,
6271 .unlink_setup = nfs4_proc_unlink_setup,
6272 .unlink_done = nfs4_proc_unlink_done,
6273 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006274 .rename_setup = nfs4_proc_rename_setup,
6275 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006276 .link = nfs4_proc_link,
6277 .symlink = nfs4_proc_symlink,
6278 .mkdir = nfs4_proc_mkdir,
6279 .rmdir = nfs4_proc_remove,
6280 .readdir = nfs4_proc_readdir,
6281 .mknod = nfs4_proc_mknod,
6282 .statfs = nfs4_proc_statfs,
6283 .fsinfo = nfs4_proc_fsinfo,
6284 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006285 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006286 .decode_dirent = nfs4_decode_dirent,
6287 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05006288 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006289 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006290 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006292 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006293 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006294 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006295 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006296 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00006297 .init_client = nfs4_init_client,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006298 .secinfo = nfs4_proc_secinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006299};
6300
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006301static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6302 .prefix = XATTR_NAME_NFSV4_ACL,
6303 .list = nfs4_xattr_list_nfs4_acl,
6304 .get = nfs4_xattr_get_nfs4_acl,
6305 .set = nfs4_xattr_set_nfs4_acl,
6306};
6307
6308const struct xattr_handler *nfs4_xattr_handlers[] = {
6309 &nfs4_xattr_nfs4_acl_handler,
6310 NULL
6311};
6312
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313/*
6314 * Local variables:
6315 * c-basic-offset: 8
6316 * End:
6317 */