blob: a64aa56e140a905cfd73ba9ce4b6723589d27ad6 [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
Benny Halevydfb4f3092010-09-24 09:17:01 -0400366nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *free_slot)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400367{
Benny Halevydfb4f3092010-09-24 09:17:01 -0400368 int free_slotid = free_slot - tbl->slots;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400369 int slotid = free_slotid;
370
Benny Halevydfb4f3092010-09-24 09:17:01 -0400371 BUG_ON(slotid < 0 || slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400372 /* clear used bit in bitmap */
373 __clear_bit(slotid, tbl->used_slots);
374
375 /* update highest_used_slotid when it is freed */
376 if (slotid == tbl->highest_used_slotid) {
377 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500378 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400379 tbl->highest_used_slotid = slotid;
380 else
381 tbl->highest_used_slotid = -1;
382 }
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400383 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
384 free_slotid, tbl->highest_used_slotid);
385}
386
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800387/*
Andy Adamson42acd022011-01-06 02:04:34 +0000388 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800389 */
Andy Adamson42acd022011-01-06 02:04:34 +0000390static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800391{
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800392 struct rpc_task *task;
393
Trond Myklebusta2118c32010-06-16 09:52:26 -0400394 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800395 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
396 if (task)
397 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800398 return;
399 }
400
401 if (ses->fc_slot_table.highest_used_slotid != -1)
402 return;
403
Andy Adamson42acd022011-01-06 02:04:34 +0000404 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
405 complete(&ses->fc_slot_table.complete);
406}
407
408/*
409 * Signal state manager thread if session back channel is drained
410 */
411void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
412{
413 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
414 ses->bc_slot_table.highest_used_slotid != -1)
415 return;
416 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
417 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800418}
419
Trond Myklebustd185a332010-06-16 09:52:25 -0400420static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400421{
422 struct nfs4_slot_table *tbl;
423
Trond Myklebustd185a332010-06-16 09:52:25 -0400424 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400425 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400426 /* just wake up the next guy waiting since
427 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500428 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500429 return;
Andy Adamson13615872009-04-01 09:22:17 -0400430 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500431
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500432 spin_lock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400433 nfs4_free_slot(tbl, res->sr_slot);
Andy Adamson42acd022011-01-06 02:04:34 +0000434 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500435 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400436 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400437}
438
Trond Myklebust14516c32010-07-31 14:29:06 -0400439static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400440{
441 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400442 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400443
444 /*
445 * sr_status remains 1 if an RPC level error occurred. The server
446 * may or may not have processed the sequence operation..
447 * Proceed as if the server received and processed the sequence
448 * operation.
449 */
450 if (res->sr_status == 1)
451 res->sr_status = NFS_OK;
452
Bryan Schumaker468f8612011-04-18 15:57:32 -0400453 /* don't increment the sequence number if the task wasn't sent */
454 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400455 goto out;
456
Andy Adamson691daf32009-12-04 15:55:39 -0500457 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400458 switch (res->sr_status) {
459 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400460 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400461 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400462 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400463 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400464 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500465 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500466 if (res->sr_status_flags != 0)
467 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400468 break;
469 case -NFS4ERR_DELAY:
470 /* The server detected a resend of the RPC call and
471 * returned NFS4ERR_DELAY as per Section 2.10.6.2
472 * of RFC5661.
473 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200474 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400475 __func__,
476 res->sr_slot - res->sr_session->fc_slot_table.slots,
477 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400478 goto out_retry;
479 default:
480 /* Just update the slot sequence no. */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400481 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400482 }
483out:
484 /* The session may be reset by one of the error handlers. */
485 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400486 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400487 return 1;
488out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400489 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400490 goto out;
491 rpc_delay(task, NFS4_POLL_RETRY_MAX);
492 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400493}
494
Trond Myklebust14516c32010-07-31 14:29:06 -0400495static int nfs4_sequence_done(struct rpc_task *task,
496 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400497{
Trond Myklebust14516c32010-07-31 14:29:06 -0400498 if (res->sr_session == NULL)
499 return 1;
500 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400501}
502
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400503/*
Benny Halevy510b8172009-04-01 09:22:14 -0400504 * nfs4_find_slot - efficiently look for a free slot
505 *
506 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
507 * If found, we mark the slot as used, update the highest_used_slotid,
508 * and respectively set up the sequence operation args.
509 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400510 *
511 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400512 */
513static u8
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800514nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400515{
516 int slotid;
517 u8 ret_id = NFS4_MAX_SLOT_TABLE;
518 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
519
Benny Halevy510b8172009-04-01 09:22:14 -0400520 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
521 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
522 tbl->max_slots);
523 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
524 if (slotid >= tbl->max_slots)
525 goto out;
526 __set_bit(slotid, tbl->used_slots);
527 if (slotid > tbl->highest_used_slotid)
528 tbl->highest_used_slotid = slotid;
529 ret_id = slotid;
530out:
531 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
532 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400533 return ret_id;
534}
535
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000536int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400537 struct nfs4_sequence_args *args,
538 struct nfs4_sequence_res *res,
539 int cache_reply,
540 struct rpc_task *task)
541{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400542 struct nfs4_slot *slot;
543 struct nfs4_slot_table *tbl;
544 u8 slotid;
545
546 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400547 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400548 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400549 return 0;
550
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400551 tbl = &session->fc_slot_table;
552
553 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400554 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800555 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamsonea028ac2009-12-04 15:55:38 -0500556 /*
557 * The state manager will wait until the slot table is empty.
558 * Schedule the reset thread
559 */
Andy Adamson05f0d232009-12-04 15:55:37 -0500560 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400561 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500562 dprintk("%s Schedule Session Reset\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500563 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400564 }
565
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800566 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
567 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
568 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
569 spin_unlock(&tbl->slot_tbl_lock);
570 dprintk("%s enforce FIFO order\n", __func__);
571 return -EAGAIN;
572 }
573
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800574 slotid = nfs4_find_slot(tbl);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400575 if (slotid == NFS4_MAX_SLOT_TABLE) {
576 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
577 spin_unlock(&tbl->slot_tbl_lock);
578 dprintk("<-- %s: no free slots\n", __func__);
579 return -EAGAIN;
580 }
581 spin_unlock(&tbl->slot_tbl_lock);
582
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800583 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400584 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400585 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400586 args->sa_slotid = slotid;
587 args->sa_cache_this = cache_reply;
588
589 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
590
Benny Halevy99fe60d2009-04-01 09:22:29 -0400591 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400592 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400593 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400594 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400595 /*
596 * sr_status is only set in decode_sequence, and so will remain
597 * set to 1 if an rpc level failure occurs.
598 */
599 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400600 return 0;
601}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000602EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400603
Trond Myklebust035168a2010-06-16 09:52:26 -0400604int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400605 struct nfs4_sequence_args *args,
606 struct nfs4_sequence_res *res,
607 int cache_reply,
608 struct rpc_task *task)
609{
Trond Myklebust035168a2010-06-16 09:52:26 -0400610 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400611 int ret = 0;
612
Trond Myklebust035168a2010-06-16 09:52:26 -0400613 if (session == NULL) {
614 args->sa_session = NULL;
615 res->sr_session = NULL;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400616 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400617 }
618
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200619 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400620 __func__, session->clp, session, res->sr_slot ?
621 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400622
623 ret = nfs41_setup_sequence(session, args, res, cache_reply,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400624 task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400625out:
626 dprintk("<-- %s status=%d\n", __func__, ret);
627 return ret;
628}
629
630struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400631 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400632 struct nfs4_sequence_args *seq_args;
633 struct nfs4_sequence_res *seq_res;
634 int cache_reply;
635};
636
637static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
638{
639 struct nfs41_call_sync_data *data = calldata;
640
Trond Myklebust035168a2010-06-16 09:52:26 -0400641 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
642
643 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400644 data->seq_res, data->cache_reply, task))
645 return;
646 rpc_call_start(task);
647}
648
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800649static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
650{
651 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
652 nfs41_call_sync_prepare(task, calldata);
653}
654
Andy Adamson69ab40c2009-04-01 09:22:19 -0400655static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
656{
657 struct nfs41_call_sync_data *data = calldata;
658
Trond Myklebust14516c32010-07-31 14:29:06 -0400659 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400660}
661
Andy Adamsonce5039c2009-04-01 09:22:13 -0400662struct rpc_call_ops nfs41_call_sync_ops = {
663 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400664 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400665};
666
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800667struct rpc_call_ops nfs41_call_priv_sync_ops = {
668 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
669 .rpc_call_done = nfs41_call_sync_done,
670};
671
Bryan Schumaker7c513052011-03-24 17:12:24 +0000672static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
673 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400674 struct rpc_message *msg,
675 struct nfs4_sequence_args *args,
676 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800677 int cache_reply,
678 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400679{
680 int ret;
681 struct rpc_task *task;
682 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400683 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400684 .seq_args = args,
685 .seq_res = res,
686 .cache_reply = cache_reply,
687 };
688 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000689 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400690 .rpc_message = msg,
691 .callback_ops = &nfs41_call_sync_ops,
692 .callback_data = &data
693 };
694
Benny Halevydfb4f3092010-09-24 09:17:01 -0400695 res->sr_slot = NULL;
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800696 if (privileged)
697 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400698 task = rpc_run_task(&task_setup);
699 if (IS_ERR(task))
700 ret = PTR_ERR(task);
701 else {
702 ret = task->tk_status;
703 rpc_put_task(task);
704 }
705 return ret;
706}
707
Bryan Schumaker7c513052011-03-24 17:12:24 +0000708int _nfs4_call_sync_session(struct rpc_clnt *clnt,
709 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400710 struct rpc_message *msg,
711 struct nfs4_sequence_args *args,
712 struct nfs4_sequence_res *res,
713 int cache_reply)
714{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000715 return nfs4_call_sync_sequence(clnt, server, msg, args, res, cache_reply, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400716}
717
Trond Myklebustdf896452010-06-16 09:52:26 -0400718#else
Trond Myklebust14516c32010-07-31 14:29:06 -0400719static int nfs4_sequence_done(struct rpc_task *task,
720 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400721{
Trond Myklebust14516c32010-07-31 14:29:06 -0400722 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400723}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400724#endif /* CONFIG_NFS_V4_1 */
725
Bryan Schumaker7c513052011-03-24 17:12:24 +0000726int _nfs4_call_sync(struct rpc_clnt *clnt,
727 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400728 struct rpc_message *msg,
729 struct nfs4_sequence_args *args,
730 struct nfs4_sequence_res *res,
731 int cache_reply)
732{
Benny Halevyf3752972009-04-01 09:22:04 -0400733 args->sa_session = res->sr_session = NULL;
Bryan Schumaker7c513052011-03-24 17:12:24 +0000734 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400735}
736
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000737static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000738int nfs4_call_sync(struct rpc_clnt *clnt,
739 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000740 struct rpc_message *msg,
741 struct nfs4_sequence_args *args,
742 struct nfs4_sequence_res *res,
743 int cache_reply)
744{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000745 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
746 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000747}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400748
Trond Myklebust38478b22006-05-25 01:40:57 -0400749static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
Trond Myklebust38478b22006-05-25 01:40:57 -0400751 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Trond Myklebust38478b22006-05-25 01:40:57 -0400753 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400754 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700755 if (!cinfo->atomic || cinfo->before != dir->i_version)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400756 nfs_force_lookup_revalidate(dir);
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700757 dir->i_version = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400758 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
760
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100761struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400762 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100763 struct nfs_openargs o_arg;
764 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100765 struct nfs_open_confirmargs c_arg;
766 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100767 struct nfs_fattr f_attr;
768 struct nfs_fattr dir_attr;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100769 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -0400770 struct dentry *dentry;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100771 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400772 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100773 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100774 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400775 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100776 int rpc_status;
777 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100778};
779
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400780
781static void nfs4_init_opendata_res(struct nfs4_opendata *p)
782{
783 p->o_res.f_attr = &p->f_attr;
784 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400785 p->o_res.seqid = p->o_arg.seqid;
786 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400787 p->o_res.server = p->o_arg.server;
788 nfs_fattr_init(&p->f_attr);
789 nfs_fattr_init(&p->dir_attr);
790}
791
Al Viro82a2c1b2011-06-22 18:30:55 -0400792static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500793 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400794 const struct iattr *attrs,
795 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100796{
Al Viro82a2c1b2011-06-22 18:30:55 -0400797 struct dentry *parent = dget_parent(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100798 struct inode *dir = parent->d_inode;
799 struct nfs_server *server = NFS_SERVER(dir);
800 struct nfs4_opendata *p;
801
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400802 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100803 if (p == NULL)
804 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400805 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100806 if (p->o_arg.seqid == NULL)
807 goto err_free;
Al Viro82a2c1b2011-06-22 18:30:55 -0400808 nfs_sb_active(dentry->d_sb);
809 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100810 p->dir = parent;
811 p->owner = sp;
812 atomic_inc(&sp->so_count);
813 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500814 p->o_arg.open_flags = flags;
815 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400816 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400817 p->o_arg.id = sp->so_owner_id.id;
Al Viro82a2c1b2011-06-22 18:30:55 -0400818 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100819 p->o_arg.server = server;
820 p->o_arg.bitmask = server->attr_bitmask;
821 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400822 if (flags & O_CREAT) {
823 u32 *s;
824
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100825 p->o_arg.u.attrs = &p->attrs;
826 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400827 s = (u32 *) p->o_arg.u.verifier.data;
828 s[0] = jiffies;
829 s[1] = current->pid;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100830 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100831 p->c_arg.fh = &p->o_res.fh;
832 p->c_arg.stateid = &p->o_res.stateid;
833 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400834 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400835 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100836 return p;
837err_free:
838 kfree(p);
839err:
840 dput(parent);
841 return NULL;
842}
843
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400844static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100845{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400846 struct nfs4_opendata *p = container_of(kref,
847 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -0400848 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400849
850 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400851 if (p->state != NULL)
852 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400853 nfs4_put_state_owner(p->owner);
854 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -0400855 dput(p->dentry);
856 nfs_sb_deactive(sb);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400857 kfree(p);
858}
859
860static void nfs4_opendata_put(struct nfs4_opendata *p)
861{
862 if (p != NULL)
863 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100864}
865
Trond Myklebust06f814a2006-01-03 09:55:07 +0100866static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
867{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100868 int ret;
869
Trond Myklebust06f814a2006-01-03 09:55:07 +0100870 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100871 return ret;
872}
873
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500874static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400875{
876 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500877
878 if (open_mode & O_EXCL)
879 goto out;
880 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400881 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500882 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
883 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400884 break;
885 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500886 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
887 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400888 break;
889 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500890 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
891 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400892 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500893out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400894 return ret;
895}
896
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500897static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400898{
Trond Myklebust652f89f2011-12-09 19:05:58 -0500899 if (delegation == NULL)
900 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500901 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400902 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500903 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400904 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500905 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400906 return 1;
907}
908
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500909static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100910{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500911 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100912 case FMODE_WRITE:
913 state->n_wronly++;
914 break;
915 case FMODE_READ:
916 state->n_rdonly++;
917 break;
918 case FMODE_READ|FMODE_WRITE:
919 state->n_rdwr++;
920 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500921 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100922}
923
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500924static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400925{
926 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
927 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
928 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500929 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400930 case FMODE_READ:
931 set_bit(NFS_O_RDONLY_STATE, &state->flags);
932 break;
933 case FMODE_WRITE:
934 set_bit(NFS_O_WRONLY_STATE, &state->flags);
935 break;
936 case FMODE_READ|FMODE_WRITE:
937 set_bit(NFS_O_RDWR_STATE, &state->flags);
938 }
939}
940
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500941static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400942{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400943 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500944 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400945 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400946}
947
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500948static 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 -0700949{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400950 /*
951 * Protect the call to nfs4_state_set_mode_locked and
952 * serialise the stateid update
953 */
954 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400955 if (deleg_stateid != NULL) {
956 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
957 set_bit(NFS_DELEGATED_STATE, &state->flags);
958 }
959 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500960 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400961 write_sequnlock(&state->seqlock);
962 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500963 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700964 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965}
966
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500967static 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 -0500968{
969 struct nfs_inode *nfsi = NFS_I(state->inode);
970 struct nfs_delegation *deleg_cur;
971 int ret = 0;
972
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500973 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500974
975 rcu_read_lock();
976 deleg_cur = rcu_dereference(nfsi->delegation);
977 if (deleg_cur == NULL)
978 goto no_delegation;
979
980 spin_lock(&deleg_cur->lock);
981 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500982 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500983 goto no_delegation_unlock;
984
985 if (delegation == NULL)
986 delegation = &deleg_cur->stateid;
987 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
988 goto no_delegation_unlock;
989
Trond Myklebustb7391f42008-12-23 15:21:52 -0500990 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500991 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500992 ret = 1;
993no_delegation_unlock:
994 spin_unlock(&deleg_cur->lock);
995no_delegation:
996 rcu_read_unlock();
997
998 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500999 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001000 ret = 1;
1001 }
1002
1003 return ret;
1004}
1005
1006
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001007static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001008{
1009 struct nfs_delegation *delegation;
1010
1011 rcu_read_lock();
1012 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001013 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001014 rcu_read_unlock();
1015 return;
1016 }
1017 rcu_read_unlock();
1018 nfs_inode_return_delegation(inode);
1019}
1020
Trond Myklebust6ee41262007-07-08 14:11:36 -04001021static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001022{
1023 struct nfs4_state *state = opendata->state;
1024 struct nfs_inode *nfsi = NFS_I(state->inode);
1025 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001026 int open_mode = opendata->o_arg.open_flags & O_EXCL;
1027 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001028 nfs4_stateid stateid;
1029 int ret = -EAGAIN;
1030
Trond Myklebustaac00a82007-07-05 19:02:21 -04001031 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001032 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001033 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001034 if (can_open_cached(state, fmode, open_mode)) {
1035 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001036 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001037 goto out_return_state;
1038 }
1039 spin_unlock(&state->owner->so_lock);
1040 }
Trond Myklebust34310432008-12-23 15:21:38 -05001041 rcu_read_lock();
1042 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001043 if (!can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001044 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001045 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001046 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001047 /* Save the delegation */
1048 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1049 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001050 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001051 if (ret != 0)
1052 goto out;
1053 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001054
1055 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001056 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001057 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001058 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001059out:
1060 return ERR_PTR(ret);
1061out_return_state:
1062 atomic_inc(&state->count);
1063 return state;
1064}
1065
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001066static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1067{
1068 struct inode *inode;
1069 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001070 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001071 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001072
Trond Myklebustaac00a82007-07-05 19:02:21 -04001073 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001074 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001075 goto out;
1076 }
1077
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001078 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001079 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001080 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001081 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001082 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001083 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001084 goto err;
1085 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001086 state = nfs4_get_open_state(inode, data->owner);
1087 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001088 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001089 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001090 int delegation_flags = 0;
1091
Trond Myklebust003707c2007-07-05 18:07:55 -04001092 rcu_read_lock();
1093 delegation = rcu_dereference(NFS_I(inode)->delegation);
1094 if (delegation)
1095 delegation_flags = delegation->flags;
1096 rcu_read_unlock();
Trond Myklebust652f89f2011-12-09 19:05:58 -05001097 if (data->o_arg.claim == NFS4_OPEN_CLAIM_DELEGATE_CUR) {
1098 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1099 "returning a delegation for "
1100 "OPEN(CLAIM_DELEGATE_CUR)\n",
1101 NFS_CLIENT(inode)->cl_server);
1102 } else if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001103 nfs_inode_set_delegation(state->inode,
1104 data->owner->so_cred,
1105 &data->o_res);
1106 else
1107 nfs_inode_reclaim_delegation(state->inode,
1108 data->owner->so_cred,
1109 &data->o_res);
1110 }
Trond Myklebust34310432008-12-23 15:21:38 -05001111
1112 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001113 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001114 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001115out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001116 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001117err_put_inode:
1118 iput(inode);
1119err:
1120 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001121}
1122
Trond Myklebust864472e2006-01-03 09:55:15 +01001123static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1124{
1125 struct nfs_inode *nfsi = NFS_I(state->inode);
1126 struct nfs_open_context *ctx;
1127
1128 spin_lock(&state->inode->i_lock);
1129 list_for_each_entry(ctx, &nfsi->open_files, list) {
1130 if (ctx->state != state)
1131 continue;
1132 get_nfs_open_context(ctx);
1133 spin_unlock(&state->inode->i_lock);
1134 return ctx;
1135 }
1136 spin_unlock(&state->inode->i_lock);
1137 return ERR_PTR(-ENOENT);
1138}
1139
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001140static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1141{
1142 struct nfs4_opendata *opendata;
1143
Al Viro3d4ff432011-06-22 18:40:12 -04001144 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001145 if (opendata == NULL)
1146 return ERR_PTR(-ENOMEM);
1147 opendata->state = state;
1148 atomic_inc(&state->count);
1149 return opendata;
1150}
1151
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001152static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001153{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001154 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001155 int ret;
1156
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001157 opendata->o_arg.open_flags = 0;
1158 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001159 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1160 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1161 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001162 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001163 if (ret != 0)
1164 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001165 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001166 if (IS_ERR(newstate))
1167 return PTR_ERR(newstate);
Al Viro643168c2011-06-22 18:20:23 -04001168 nfs4_close_state(newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001169 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001170 return 0;
1171}
1172
1173static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1174{
Trond Myklebust864472e2006-01-03 09:55:15 +01001175 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001176 int ret;
1177
1178 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001179 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001180 smp_rmb();
1181 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001182 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001183 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001184 if (ret != 0)
1185 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001186 if (newstate != state)
1187 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001188 }
1189 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001190 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001191 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001192 if (ret != 0)
1193 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001194 if (newstate != state)
1195 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001196 }
1197 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001198 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001199 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001200 if (ret != 0)
1201 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001202 if (newstate != state)
1203 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001204 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001205 /*
1206 * We may have performed cached opens for all three recoveries.
1207 * Check if we need to update the current stateid.
1208 */
1209 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1210 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001211 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001212 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1213 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001214 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001215 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001216 return 0;
1217}
1218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219/*
1220 * OPEN_RECLAIM:
1221 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001223static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001225 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001226 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001227 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 int status;
1229
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001230 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1231 if (IS_ERR(opendata))
1232 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001233 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1234 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001235 rcu_read_lock();
1236 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001237 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001238 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001239 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001240 opendata->o_arg.u.delegation_type = delegation_type;
1241 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001242 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 return status;
1244}
1245
Trond Myklebust539cd032007-06-05 11:46:42 -04001246static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
1248 struct nfs_server *server = NFS_SERVER(state->inode);
1249 struct nfs4_exception exception = { };
1250 int err;
1251 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001252 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001253 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001254 break;
1255 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 } while (exception.retry);
1257 return err;
1258}
1259
Trond Myklebust864472e2006-01-03 09:55:15 +01001260static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1261{
1262 struct nfs_open_context *ctx;
1263 int ret;
1264
1265 ctx = nfs4_state_find_open_context(state);
1266 if (IS_ERR(ctx))
1267 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001268 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001269 put_nfs_open_context(ctx);
1270 return ret;
1271}
1272
Trond Myklebust13437e12007-07-06 15:10:43 -04001273static 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 -07001274{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001275 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001276 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001278 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1279 if (IS_ERR(opendata))
1280 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001281 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001282 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001283 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001284 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001285 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001286 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287}
1288
Trond Myklebust13437e12007-07-06 15:10:43 -04001289int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
1291 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001292 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 int err;
1294 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001295 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 switch (err) {
1297 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001298 case -ENOENT:
1299 case -ESTALE:
1300 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001301 case -NFS4ERR_BADSESSION:
1302 case -NFS4ERR_BADSLOT:
1303 case -NFS4ERR_BAD_HIGH_SLOT:
1304 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1305 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001306 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001307 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 case -NFS4ERR_STALE_CLIENTID:
1309 case -NFS4ERR_STALE_STATEID:
1310 case -NFS4ERR_EXPIRED:
1311 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001312 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001313 goto out;
1314 case -ERESTARTSYS:
1315 /*
1316 * The show must go on: exit, but mark the
1317 * stateid as needing recovery.
1318 */
1319 case -NFS4ERR_ADMIN_REVOKED:
1320 case -NFS4ERR_BAD_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001321 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001322 case -EKEYEXPIRED:
1323 /*
1324 * User RPCSEC_GSS context has expired.
1325 * We cannot recover this stateid now, so
1326 * skip it and allow recovery thread to
1327 * proceed.
1328 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001329 case -ENOMEM:
1330 err = 0;
1331 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 }
1333 err = nfs4_handle_exception(server, err, &exception);
1334 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001335out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 return err;
1337}
1338
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001339static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1340{
1341 struct nfs4_opendata *data = calldata;
1342
1343 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001344 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001345 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1346 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001347 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001348 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001349 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001350 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001351}
1352
1353static void nfs4_open_confirm_release(void *calldata)
1354{
1355 struct nfs4_opendata *data = calldata;
1356 struct nfs4_state *state = NULL;
1357
1358 /* If this request hasn't been cancelled, do nothing */
1359 if (data->cancelled == 0)
1360 goto out_free;
1361 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001362 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001363 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001364 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001365 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001366 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001367out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001368 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001369}
1370
1371static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001372 .rpc_call_done = nfs4_open_confirm_done,
1373 .rpc_release = nfs4_open_confirm_release,
1374};
1375
1376/*
1377 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1378 */
1379static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1380{
1381 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1382 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001383 struct rpc_message msg = {
1384 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1385 .rpc_argp = &data->c_arg,
1386 .rpc_resp = &data->c_res,
1387 .rpc_cred = data->owner->so_cred,
1388 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001389 struct rpc_task_setup task_setup_data = {
1390 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001391 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001392 .callback_ops = &nfs4_open_confirm_ops,
1393 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001394 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001395 .flags = RPC_TASK_ASYNC,
1396 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 int status;
1398
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001399 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001400 data->rpc_done = 0;
1401 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001402 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001403 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001404 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001405 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001406 status = nfs4_wait_for_completion_rpc_task(task);
1407 if (status != 0) {
1408 data->cancelled = 1;
1409 smp_wmb();
1410 } else
1411 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001412 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 return status;
1414}
1415
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001416static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001418 struct nfs4_opendata *data = calldata;
1419 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001420
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001421 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1422 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001423 /*
1424 * Check if we still need to send an OPEN call, or if we can use
1425 * a delegation instead.
1426 */
1427 if (data->state != NULL) {
1428 struct nfs_delegation *delegation;
1429
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001430 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001431 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001432 rcu_read_lock();
1433 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust652f89f2011-12-09 19:05:58 -05001434 if (data->o_arg.claim != NFS4_OPEN_CLAIM_DELEGATE_CUR &&
1435 can_open_delegated(delegation, data->o_arg.fmode))
1436 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001437 rcu_read_unlock();
1438 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001439 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001440 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001441 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001442 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001443 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001444 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1445 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001446 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001447 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001448 &data->o_arg.seq_args,
1449 &data->o_res.seq_res, 1, task))
1450 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001451 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001452 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05001453unlock_no_action:
1454 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001455out_no_action:
1456 task->tk_action = NULL;
1457
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001458}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001460static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1461{
1462 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1463 nfs4_open_prepare(task, calldata);
1464}
1465
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001466static void nfs4_open_done(struct rpc_task *task, void *calldata)
1467{
1468 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001470 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001471
Trond Myklebust14516c32010-07-31 14:29:06 -04001472 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1473 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001474
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001475 if (task->tk_status == 0) {
1476 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001477 case S_IFREG:
1478 break;
1479 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001480 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001481 break;
1482 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001483 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001484 break;
1485 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001486 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001487 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001488 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001489 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1490 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001491 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001492 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001493}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001494
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001495static void nfs4_open_release(void *calldata)
1496{
1497 struct nfs4_opendata *data = calldata;
1498 struct nfs4_state *state = NULL;
1499
1500 /* If this request hasn't been cancelled, do nothing */
1501 if (data->cancelled == 0)
1502 goto out_free;
1503 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001504 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001505 goto out_free;
1506 /* In case we need an open_confirm, no cleanup! */
1507 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1508 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001509 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001510 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04001511 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001512out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001513 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001514}
1515
1516static const struct rpc_call_ops nfs4_open_ops = {
1517 .rpc_call_prepare = nfs4_open_prepare,
1518 .rpc_call_done = nfs4_open_done,
1519 .rpc_release = nfs4_open_release,
1520};
1521
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001522static const struct rpc_call_ops nfs4_recover_open_ops = {
1523 .rpc_call_prepare = nfs4_recover_open_prepare,
1524 .rpc_call_done = nfs4_open_done,
1525 .rpc_release = nfs4_open_release,
1526};
1527
1528static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001529{
1530 struct inode *dir = data->dir->d_inode;
1531 struct nfs_server *server = NFS_SERVER(dir);
1532 struct nfs_openargs *o_arg = &data->o_arg;
1533 struct nfs_openres *o_res = &data->o_res;
1534 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001535 struct rpc_message msg = {
1536 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1537 .rpc_argp = o_arg,
1538 .rpc_resp = o_res,
1539 .rpc_cred = data->owner->so_cred,
1540 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001541 struct rpc_task_setup task_setup_data = {
1542 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001543 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001544 .callback_ops = &nfs4_open_ops,
1545 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001546 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001547 .flags = RPC_TASK_ASYNC,
1548 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001549 int status;
1550
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001551 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001552 data->rpc_done = 0;
1553 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001554 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001555 if (isrecover)
1556 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001557 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001558 if (IS_ERR(task))
1559 return PTR_ERR(task);
1560 status = nfs4_wait_for_completion_rpc_task(task);
1561 if (status != 0) {
1562 data->cancelled = 1;
1563 smp_wmb();
1564 } else
1565 status = data->rpc_status;
1566 rpc_put_task(task);
1567
1568 return status;
1569}
1570
1571static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1572{
1573 struct inode *dir = data->dir->d_inode;
1574 struct nfs_openres *o_res = &data->o_res;
1575 int status;
1576
1577 status = nfs4_run_open_task(data, 1);
1578 if (status != 0 || !data->rpc_done)
1579 return status;
1580
1581 nfs_refresh_inode(dir, o_res->dir_attr);
1582
1583 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1584 status = _nfs4_proc_open_confirm(data);
1585 if (status != 0)
1586 return status;
1587 }
1588
1589 return status;
1590}
1591
1592/*
1593 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1594 */
1595static int _nfs4_proc_open(struct nfs4_opendata *data)
1596{
1597 struct inode *dir = data->dir->d_inode;
1598 struct nfs_server *server = NFS_SERVER(dir);
1599 struct nfs_openargs *o_arg = &data->o_arg;
1600 struct nfs_openres *o_res = &data->o_res;
1601 int status;
1602
1603 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001604 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001605 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07001606 if (status != 0) {
1607 if (status == -NFS4ERR_BADNAME &&
1608 !(o_arg->open_flags & O_CREAT))
1609 return -ENOENT;
1610 return status;
1611 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001612
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001613 if (o_arg->open_flags & O_CREAT) {
1614 update_changeattr(dir, &o_res->cinfo);
1615 nfs_post_op_update_inode(dir, o_res->dir_attr);
1616 } else
1617 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001618 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1619 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001621 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001623 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 }
1625 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001626 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001627 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628}
1629
Andy Adamsond83217c2011-03-01 01:34:17 +00001630static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001631{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001632 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001633 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001634
Trond Myklebusta78cb572009-08-09 15:06:19 -04001635 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001636 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001637 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001638 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001639 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1640 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001641 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001642 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001643 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001644 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001645 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001646}
1647
Andy Adamsond83217c2011-03-01 01:34:17 +00001648static int nfs4_recover_expired_lease(struct nfs_server *server)
1649{
1650 return nfs4_client_recover_expired_lease(server->nfs_client);
1651}
1652
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653/*
1654 * OPEN_EXPIRED:
1655 * reclaim state on the server after a network partition.
1656 * Assumes caller holds the appropriate lock
1657 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001658static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001660 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001661 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001663 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1664 if (IS_ERR(opendata))
1665 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001666 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001667 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04001668 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001669 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001670 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671}
1672
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001673static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001674{
Trond Myklebust539cd032007-06-05 11:46:42 -04001675 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001676 struct nfs4_exception exception = { };
1677 int err;
1678
1679 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001680 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001681 switch (err) {
1682 default:
1683 goto out;
1684 case -NFS4ERR_GRACE:
1685 case -NFS4ERR_DELAY:
1686 nfs4_handle_exception(server, err, &exception);
1687 err = 0;
1688 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001689 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001690out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001691 return err;
1692}
1693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1695{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001697 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Trond Myklebust864472e2006-01-03 09:55:15 +01001699 ctx = nfs4_state_find_open_context(state);
1700 if (IS_ERR(ctx))
1701 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001702 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001703 put_nfs_open_context(ctx);
1704 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705}
1706
Bryan Schumakerf062eb62011-06-02 14:59:10 -04001707#if defined(CONFIG_NFS_V4_1)
1708static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1709{
1710 int status;
1711 struct nfs_server *server = NFS_SERVER(state->inode);
1712
1713 status = nfs41_test_stateid(server, state);
1714 if (status == NFS_OK)
1715 return 0;
1716 nfs41_free_stateid(server, state);
1717 return nfs4_open_expired(sp, state);
1718}
1719#endif
1720
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001722 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1723 * fields corresponding to attributes that were used to store the verifier.
1724 * Make sure we clobber those fields in the later setattr call
1725 */
1726static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1727{
1728 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1729 !(sattr->ia_valid & ATTR_ATIME_SET))
1730 sattr->ia_valid |= ATTR_ATIME;
1731
1732 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1733 !(sattr->ia_valid & ATTR_MTIME_SET))
1734 sattr->ia_valid |= ATTR_MTIME;
1735}
1736
1737/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001738 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 */
Al Viro82a2c1b2011-06-22 18:30:55 -04001740static 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 -07001741{
1742 struct nfs4_state_owner *sp;
1743 struct nfs4_state *state = NULL;
1744 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001745 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001746 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
1748 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 status = -ENOMEM;
1750 if (!(sp = nfs4_get_state_owner(server, cred))) {
1751 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1752 goto out_err;
1753 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001754 status = nfs4_recover_expired_lease(server);
1755 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001756 goto err_put_state_owner;
Al Viro82a2c1b2011-06-22 18:30:55 -04001757 if (dentry->d_inode != NULL)
1758 nfs4_return_incompatible_delegation(dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001759 status = -ENOMEM;
Al Viro82a2c1b2011-06-22 18:30:55 -04001760 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001761 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001762 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
Al Viro82a2c1b2011-06-22 18:30:55 -04001764 if (dentry->d_inode != NULL)
1765 opendata->state = nfs4_get_open_state(dentry->d_inode, sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001766
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001767 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001769 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001771 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001772 status = PTR_ERR(state);
1773 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001774 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001775 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001776 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001777
1778 if (opendata->o_arg.open_flags & O_EXCL) {
1779 nfs4_exclusive_attrset(opendata, sattr);
1780
1781 nfs_fattr_init(opendata->o_res.f_attr);
1782 status = nfs4_do_setattr(state->inode, cred,
1783 opendata->o_res.f_attr, sattr,
1784 state);
1785 if (status == 0)
1786 nfs_setattr_update_inode(state->inode, sattr);
1787 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1788 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001789 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 *res = state;
1792 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001793err_opendata_put:
1794 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001795err_put_state_owner:
1796 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 *res = NULL;
1799 return status;
1800}
1801
1802
Al Viro82a2c1b2011-06-22 18:30:55 -04001803static 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 -07001804{
1805 struct nfs4_exception exception = { };
1806 struct nfs4_state *res;
1807 int status;
1808
1809 do {
Al Viro82a2c1b2011-06-22 18:30:55 -04001810 status = _nfs4_do_open(dir, dentry, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 if (status == 0)
1812 break;
1813 /* NOTE: BAD_SEQID means the server and client disagree about the
1814 * book-keeping w.r.t. state-changing operations
1815 * (OPEN/CLOSE/LOCK/LOCKU...)
1816 * It is actually a sign of a bug on the client or on the server.
1817 *
1818 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001819 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 * have unhashed the old state_owner for us, and that we can
1821 * therefore safely retry using a new one. We should still warn
1822 * the user though...
1823 */
1824 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001825 printk(KERN_WARNING "NFS: v4 server %s "
1826 " returned a bad sequence-id error!\n",
1827 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 exception.retry = 1;
1829 continue;
1830 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001831 /*
1832 * BAD_STATEID on OPEN means that the server cancelled our
1833 * state before it received the OPEN_CONFIRM.
1834 * Recover by retrying the request as per the discussion
1835 * on Page 181 of RFC3530.
1836 */
1837 if (status == -NFS4ERR_BAD_STATEID) {
1838 exception.retry = 1;
1839 continue;
1840 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001841 if (status == -EAGAIN) {
1842 /* We must have found a delegation */
1843 exception.retry = 1;
1844 continue;
1845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1847 status, &exception));
1848 } while (exception.retry);
1849 return res;
1850}
1851
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001852static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1853 struct nfs_fattr *fattr, struct iattr *sattr,
1854 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001856 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001858 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 .iap = sattr,
1860 .server = server,
1861 .bitmask = server->attr_bitmask,
1862 };
1863 struct nfs_setattrres res = {
1864 .fattr = fattr,
1865 .server = server,
1866 };
1867 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001868 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1869 .rpc_argp = &arg,
1870 .rpc_resp = &res,
1871 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001873 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001874 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Trond Myklebust0e574af2005-10-27 22:12:38 -04001876 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001878 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1879 /* Use that stateid */
1880 } else if (state != NULL) {
Trond Myklebust77041ed2010-07-01 12:49:11 -04001881 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001882 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1884
Bryan Schumaker7c513052011-03-24 17:12:24 +00001885 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001886 if (status == 0 && state != NULL)
1887 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001888 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889}
1890
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001891static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1892 struct nfs_fattr *fattr, struct iattr *sattr,
1893 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001895 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 struct nfs4_exception exception = { };
1897 int err;
1898 do {
1899 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001900 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 &exception);
1902 } while (exception.retry);
1903 return err;
1904}
1905
1906struct nfs4_closedata {
1907 struct inode *inode;
1908 struct nfs4_state *state;
1909 struct nfs_closeargs arg;
1910 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001911 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001912 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00001913 bool roc;
1914 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915};
1916
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001917static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001918{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001919 struct nfs4_closedata *calldata = data;
1920 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04001921 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07001922
Fred Isamanf7e89172011-01-06 11:36:32 +00001923 if (calldata->roc)
1924 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001925 nfs4_put_open_state(calldata->state);
1926 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001927 nfs4_put_state_owner(sp);
Al Viro643168c2011-06-22 18:20:23 -04001928 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07001929 kfree(calldata);
1930}
1931
Trond Myklebust88069f72009-12-08 08:33:16 -05001932static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1933 fmode_t fmode)
1934{
1935 spin_lock(&state->owner->so_lock);
1936 if (!(fmode & FMODE_READ))
1937 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1938 if (!(fmode & FMODE_WRITE))
1939 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1940 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1941 spin_unlock(&state->owner->so_lock);
1942}
1943
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001944static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001946 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 struct nfs_server *server = NFS_SERVER(calldata->inode);
1949
Trond Myklebust14516c32010-07-31 14:29:06 -04001950 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1951 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 /* hmm. we are done with the inode, and in the process of freeing
1953 * the state_owner. we keep this around to process errors
1954 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 switch (task->tk_status) {
1956 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00001957 if (calldata->roc)
1958 pnfs_roc_set_barrier(state->inode,
1959 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04001960 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001961 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001962 nfs4_close_clear_stateid_flags(state,
1963 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 break;
1965 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001966 case -NFS4ERR_OLD_STATEID:
1967 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001969 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001970 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001972 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1973 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001975 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001976 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977}
1978
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001979static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001981 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001982 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001983 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001984
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001985 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001986 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001987
Trond Myklebust88069f72009-12-08 08:33:16 -05001988 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1989 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001990 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001991 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001992 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001993 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001994 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1995 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1996 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001997 }
1998 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001999 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
2000 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
2001 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04002002 }
Trond Myklebuste7616922006-01-03 09:55:13 +01002003 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05002004 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05002005
2006 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002007 /* Note: exit _without_ calling nfs4_close_done */
2008 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07002009 return;
2010 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002011
Fred Isamanf7e89172011-01-06 11:36:32 +00002012 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05002013 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00002014 if (calldata->roc &&
2015 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
2016 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
2017 task, NULL);
2018 return;
2019 }
2020 }
Trond Myklebust88069f72009-12-08 08:33:16 -05002021
Trond Myklebust516a6af2005-10-27 22:12:41 -04002022 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002023 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04002024 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Andy Adamson19ddab02009-04-01 09:22:20 -04002025 &calldata->arg.seq_args, &calldata->res.seq_res,
2026 1, task))
2027 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002028 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029}
2030
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002031static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01002032 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01002033 .rpc_call_done = nfs4_close_done,
2034 .rpc_release = nfs4_free_closedata,
2035};
2036
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037/*
2038 * It is possible for data to be read/written from a mem-mapped file
2039 * after the sys_close call (which hits the vfs layer as a flush).
2040 * This means that we can't safely call nfsv4 close on a file until
2041 * the inode is cleared. This in turn means that we are not good
2042 * NFSv4 citizens - we do not indicate to the server to update the file's
2043 * share state even when we are done with one of the three share
2044 * stateid's in the inode.
2045 *
2046 * NOTE: Caller must be holding the sp->so_owner semaphore!
2047 */
Al Viro643168c2011-06-22 18:20:23 -04002048int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002050 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002052 struct nfs4_state_owner *sp = state->owner;
2053 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002054 struct rpc_message msg = {
2055 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2056 .rpc_cred = state->owner->so_cred,
2057 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002058 struct rpc_task_setup task_setup_data = {
2059 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002060 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002061 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002062 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002063 .flags = RPC_TASK_ASYNC,
2064 };
Trond Myklebust95121352005-10-18 14:20:12 -07002065 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002067 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002069 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002070 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002072 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002073 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002075 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002076 if (calldata->arg.seqid == NULL)
2077 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002078 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002079 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002080 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002081 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002082 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002083 calldata->roc = roc;
Al Viro643168c2011-06-22 18:20:23 -04002084 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07002085
Trond Myklebust1174dd12010-12-21 10:52:24 -05002086 msg.rpc_argp = &calldata->arg;
2087 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002088 task_setup_data.callback_data = calldata;
2089 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002090 if (IS_ERR(task))
2091 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002092 status = 0;
2093 if (wait)
2094 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002095 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002096 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002097out_free_calldata:
2098 kfree(calldata);
2099out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002100 if (roc)
2101 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002102 nfs4_put_open_state(state);
2103 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002104 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105}
2106
Trond Myklebust2b484292010-09-17 10:56:51 -04002107static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002108nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 struct nfs4_state *state;
2111
Trond Myklebust565277f2007-10-15 18:17:53 -04002112 /* Protect against concurrent sillydeletes */
Al Viro3d4ff432011-06-22 18:40:12 -04002113 state = nfs4_do_open(dir, ctx->dentry, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002114 if (IS_ERR(state))
2115 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002116 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002117 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118}
2119
Trond Myklebust1185a552009-12-03 15:54:02 -05002120static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002121{
2122 if (ctx->state == NULL)
2123 return;
2124 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04002125 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002126 else
Al Viro643168c2011-06-22 18:20:23 -04002127 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002128}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
2130static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2131{
Benny Halevy43652ad2009-04-01 09:21:54 -04002132 struct nfs4_server_caps_arg args = {
2133 .fhandle = fhandle,
2134 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 struct nfs4_server_caps_res res = {};
2136 struct rpc_message msg = {
2137 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002138 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 .rpc_resp = &res,
2140 };
2141 int status;
2142
Bryan Schumaker7c513052011-03-24 17:12:24 +00002143 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 if (status == 0) {
2145 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002146 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2147 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2148 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2149 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2150 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2152 server->caps |= NFS_CAP_ACLS;
2153 if (res.has_links != 0)
2154 server->caps |= NFS_CAP_HARDLINKS;
2155 if (res.has_symlinks != 0)
2156 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002157 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2158 server->caps |= NFS_CAP_FILEID;
2159 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2160 server->caps |= NFS_CAP_MODE;
2161 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2162 server->caps |= NFS_CAP_NLINK;
2163 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2164 server->caps |= NFS_CAP_OWNER;
2165 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2166 server->caps |= NFS_CAP_OWNER_GROUP;
2167 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2168 server->caps |= NFS_CAP_ATIME;
2169 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2170 server->caps |= NFS_CAP_CTIME;
2171 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2172 server->caps |= NFS_CAP_MTIME;
2173
Trond Myklebusta65318b2009-03-11 14:10:28 -04002174 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2175 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2176 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 server->acl_bitmask = res.acl_bitmask;
2178 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002179
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 return status;
2181}
2182
Trond Myklebust55a97592006-06-09 09:34:19 -04002183int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184{
2185 struct nfs4_exception exception = { };
2186 int err;
2187 do {
2188 err = nfs4_handle_exception(server,
2189 _nfs4_server_capabilities(server, fhandle),
2190 &exception);
2191 } while (exception.retry);
2192 return err;
2193}
2194
2195static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2196 struct nfs_fsinfo *info)
2197{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 struct nfs4_lookup_root_arg args = {
2199 .bitmask = nfs4_fattr_bitmap,
2200 };
2201 struct nfs4_lookup_res res = {
2202 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002203 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 .fh = fhandle,
2205 };
2206 struct rpc_message msg = {
2207 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2208 .rpc_argp = &args,
2209 .rpc_resp = &res,
2210 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002211
Trond Myklebust0e574af2005-10-27 22:12:38 -04002212 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002213 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214}
2215
2216static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2217 struct nfs_fsinfo *info)
2218{
2219 struct nfs4_exception exception = { };
2220 int err;
2221 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002222 err = _nfs4_lookup_root(server, fhandle, info);
2223 switch (err) {
2224 case 0:
2225 case -NFS4ERR_WRONGSEC:
2226 break;
2227 default:
2228 err = nfs4_handle_exception(server, err, &exception);
2229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 } while (exception.retry);
2231 return err;
2232}
2233
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002234static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2235 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2236{
2237 struct rpc_auth *auth;
2238 int ret;
2239
2240 auth = rpcauth_create(flavor, server->client);
2241 if (!auth) {
2242 ret = -EIO;
2243 goto out;
2244 }
2245 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002246out:
2247 return ret;
2248}
2249
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002250static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002251 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002253 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002254 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002256 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2257 flav_array[len] = RPC_AUTH_NULL;
2258 len += 1;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002259
2260 for (i = 0; i < len; i++) {
2261 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002262 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002263 continue;
2264 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002265 }
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002266 /*
2267 * -EACCESS could mean that the user doesn't have correct permissions
2268 * to access the mount. It could also mean that we tried to mount
2269 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
2270 * existing mount programs don't handle -EACCES very well so it should
2271 * be mapped to -EPERM instead.
2272 */
2273 if (status == -EACCES)
2274 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002275 return status;
2276}
2277
2278/*
2279 * get the file handle for the "/" directory on the server
2280 */
2281static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2282 struct nfs_fsinfo *info)
2283{
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002284 int minor_version = server->nfs_client->cl_minorversion;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002285 int status = nfs4_lookup_root(server, fhandle, info);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04002286 if ((status == -NFS4ERR_WRONGSEC) && !(server->flags & NFS_MOUNT_SECFLAVOUR))
2287 /*
2288 * A status of -NFS4ERR_WRONGSEC will be mapped to -EPERM
2289 * by nfs4_map_errors() as this function exits.
2290 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04002291 status = nfs_v4_minor_ops[minor_version]->find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 if (status == 0)
2293 status = nfs4_server_capabilities(server, fhandle);
2294 if (status == 0)
2295 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002296 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297}
2298
Andy Adamson533eb462011-06-13 18:25:56 -04002299static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002300/*
2301 * Get locations and (maybe) other attributes of a referral.
2302 * Note that we'll actually follow the referral later when
2303 * we detect fsid mismatch in inode revalidation
2304 */
Andy Adamson533eb462011-06-13 18:25:56 -04002305static int nfs4_get_referral(struct inode *dir, const struct qstr *name,
2306 struct nfs_fattr *fattr, struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002307{
2308 int status = -ENOMEM;
2309 struct page *page = NULL;
2310 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002311
2312 page = alloc_page(GFP_KERNEL);
2313 if (page == NULL)
2314 goto out;
2315 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2316 if (locations == NULL)
2317 goto out;
2318
Trond Myklebustc228fd32007-01-13 02:28:11 -05002319 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002320 if (status != 0)
2321 goto out;
2322 /* Make sure server returned a different fsid for the referral */
2323 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04002324 dprintk("%s: server did not return a different fsid for"
2325 " a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002326 status = -EIO;
2327 goto out;
2328 }
Andy Adamson533eb462011-06-13 18:25:56 -04002329 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
2330 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002331
Andy Adamson533eb462011-06-13 18:25:56 -04002332 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04002333 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04002334 memset(fhandle, 0, sizeof(struct nfs_fh));
2335out:
2336 if (page)
2337 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002338 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002339 return status;
2340}
2341
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2343{
2344 struct nfs4_getattr_arg args = {
2345 .fh = fhandle,
2346 .bitmask = server->attr_bitmask,
2347 };
2348 struct nfs4_getattr_res res = {
2349 .fattr = fattr,
2350 .server = server,
2351 };
2352 struct rpc_message msg = {
2353 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2354 .rpc_argp = &args,
2355 .rpc_resp = &res,
2356 };
2357
Trond Myklebust0e574af2005-10-27 22:12:38 -04002358 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002359 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360}
2361
2362static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2363{
2364 struct nfs4_exception exception = { };
2365 int err;
2366 do {
2367 err = nfs4_handle_exception(server,
2368 _nfs4_proc_getattr(server, fhandle, fattr),
2369 &exception);
2370 } while (exception.retry);
2371 return err;
2372}
2373
2374/*
2375 * The file is not closed if it is opened due to the a request to change
2376 * the size of the file. The open call will not be needed once the
2377 * VFS layer lookup-intents are implemented.
2378 *
2379 * Close is called when the inode is destroyed.
2380 * If we haven't opened the file for O_WRONLY, we
2381 * need to in the size_change case to obtain a stateid.
2382 *
2383 * Got race?
2384 * Because OPEN is always done by name in nfsv4, it is
2385 * possible that we opened a different file by the same
2386 * name. We can recognize this race condition, but we
2387 * can't do anything about it besides returning an error.
2388 *
2389 * This will be fixed with VFS changes (lookup-intent).
2390 */
2391static int
2392nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2393 struct iattr *sattr)
2394{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002395 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002396 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002397 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 int status;
2399
Benny Halevy8a1636c2010-07-14 15:43:57 -04002400 if (pnfs_ld_layoutret_on_setattr(inode))
2401 pnfs_return_layout(inode);
2402
Trond Myklebust0e574af2005-10-27 22:12:38 -04002403 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
Trond Myklebustd5308382005-11-04 15:33:38 -05002405 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002406 if (sattr->ia_valid & ATTR_FILE) {
2407 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002408
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002409 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002410 if (ctx) {
2411 cred = ctx->cred;
2412 state = ctx->state;
2413 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002414 }
2415
2416 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002417 if (status == 0)
2418 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 return status;
2420}
2421
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002422static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2423 const struct qstr *name, struct nfs_fh *fhandle,
2424 struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002425{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002426 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04002427 int status;
2428 struct nfs4_lookup_arg args = {
2429 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002430 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04002431 .name = name,
2432 };
2433 struct nfs4_lookup_res res = {
2434 .server = server,
2435 .fattr = fattr,
2436 .fh = fhandle,
2437 };
2438 struct rpc_message msg = {
2439 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2440 .rpc_argp = &args,
2441 .rpc_resp = &res,
2442 };
2443
2444 nfs_fattr_init(fattr);
2445
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002447 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 dprintk("NFS reply lookup: %d\n", status);
2449 return status;
2450}
2451
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002452void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2453{
2454 memset(fh, 0, sizeof(struct nfs_fh));
2455 fattr->fsid.major = 1;
2456 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2457 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2458 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2459 fattr->nlink = 2;
2460}
2461
Bryan Schumaker7c513052011-03-24 17:12:24 +00002462static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2463 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464{
2465 struct nfs4_exception exception = { };
2466 int err;
2467 do {
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002468 int status;
2469
2470 status = _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr);
2471 switch (status) {
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002472 case -NFS4ERR_BADNAME:
2473 return -ENOENT;
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002474 case -NFS4ERR_MOVED:
Trond Myklebusta6f498a2011-11-07 15:50:32 -05002475 return nfs4_get_referral(dir, name, fattr, fhandle);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002476 case -NFS4ERR_WRONGSEC:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002477 nfs_fixup_secinfo_attributes(fattr, fhandle);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07002478 }
2479 err = nfs4_handle_exception(NFS_SERVER(dir),
2480 status, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 } while (exception.retry);
2482 return err;
2483}
2484
2485static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2486{
Trond Myklebust76b32992007-08-10 17:45:11 -04002487 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 struct nfs4_accessargs args = {
2489 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002490 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002492 struct nfs4_accessres res = {
2493 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002494 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 struct rpc_message msg = {
2496 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2497 .rpc_argp = &args,
2498 .rpc_resp = &res,
2499 .rpc_cred = entry->cred,
2500 };
2501 int mode = entry->mask;
2502 int status;
2503
2504 /*
2505 * Determine which access bits we want to ask for...
2506 */
2507 if (mode & MAY_READ)
2508 args.access |= NFS4_ACCESS_READ;
2509 if (S_ISDIR(inode->i_mode)) {
2510 if (mode & MAY_WRITE)
2511 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2512 if (mode & MAY_EXEC)
2513 args.access |= NFS4_ACCESS_LOOKUP;
2514 } else {
2515 if (mode & MAY_WRITE)
2516 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2517 if (mode & MAY_EXEC)
2518 args.access |= NFS4_ACCESS_EXECUTE;
2519 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002520
2521 res.fattr = nfs_alloc_fattr();
2522 if (res.fattr == NULL)
2523 return -ENOMEM;
2524
Bryan Schumaker7c513052011-03-24 17:12:24 +00002525 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 if (!status) {
2527 entry->mask = 0;
2528 if (res.access & NFS4_ACCESS_READ)
2529 entry->mask |= MAY_READ;
2530 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2531 entry->mask |= MAY_WRITE;
2532 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2533 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002534 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002536 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 return status;
2538}
2539
2540static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2541{
2542 struct nfs4_exception exception = { };
2543 int err;
2544 do {
2545 err = nfs4_handle_exception(NFS_SERVER(inode),
2546 _nfs4_proc_access(inode, entry),
2547 &exception);
2548 } while (exception.retry);
2549 return err;
2550}
2551
2552/*
2553 * TODO: For the time being, we don't try to get any attributes
2554 * along with any of the zero-copy operations READ, READDIR,
2555 * READLINK, WRITE.
2556 *
2557 * In the case of the first three, we want to put the GETATTR
2558 * after the read-type operation -- this is because it is hard
2559 * to predict the length of a GETATTR response in v4, and thus
2560 * align the READ data correctly. This means that the GETATTR
2561 * may end up partially falling into the page cache, and we should
2562 * shift it into the 'tail' of the xdr_buf before processing.
2563 * To do this efficiently, we need to know the total length
2564 * of data received, which doesn't seem to be available outside
2565 * of the RPC layer.
2566 *
2567 * In the case of WRITE, we also want to put the GETATTR after
2568 * the operation -- in this case because we want to make sure
2569 * we get the post-operation mtime and size. This means that
2570 * we can't use xdr_encode_pages() as written: we need a variant
2571 * of it which would leave room in the 'tail' iovec.
2572 *
2573 * Both of these changes to the XDR layer would in fact be quite
2574 * minor, but I decided to leave them for a subsequent patch.
2575 */
2576static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2577 unsigned int pgbase, unsigned int pglen)
2578{
2579 struct nfs4_readlink args = {
2580 .fh = NFS_FH(inode),
2581 .pgbase = pgbase,
2582 .pglen = pglen,
2583 .pages = &page,
2584 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002585 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 struct rpc_message msg = {
2587 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2588 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002589 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 };
2591
Bryan Schumaker7c513052011-03-24 17:12:24 +00002592 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 -07002593}
2594
2595static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2596 unsigned int pgbase, unsigned int pglen)
2597{
2598 struct nfs4_exception exception = { };
2599 int err;
2600 do {
2601 err = nfs4_handle_exception(NFS_SERVER(inode),
2602 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2603 &exception);
2604 } while (exception.retry);
2605 return err;
2606}
2607
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608/*
2609 * Got race?
2610 * We will need to arrange for the VFS layer to provide an atomic open.
2611 * Until then, this create/open method is prone to inefficiency and race
2612 * conditions due to the lookup, create, and open VFS calls from sys_open()
2613 * placed on the wire.
2614 *
2615 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2616 * The file will be opened again in the subsequent VFS open call
2617 * (nfs4_proc_file_open).
2618 *
2619 * The open for read will just hang around to be used by any process that
2620 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2621 */
2622
2623static int
2624nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002625 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626{
Al Viro82a2c1b2011-06-22 18:30:55 -04002627 struct dentry *de = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002629 struct rpc_cred *cred = NULL;
2630 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 int status = 0;
2632
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002633 if (ctx != NULL) {
2634 cred = ctx->cred;
Al Viro3d4ff432011-06-22 18:40:12 -04002635 de = ctx->dentry;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002636 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002638 sattr->ia_mode &= ~current_umask();
Al Viro82a2c1b2011-06-22 18:30:55 -04002639 state = nfs4_do_open(dir, de, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002640 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 if (IS_ERR(state)) {
2642 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002643 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002645 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002646 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002647 if (ctx != NULL)
2648 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002649 else
Al Viro643168c2011-06-22 18:20:23 -04002650 nfs4_close_sync(state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651out:
2652 return status;
2653}
2654
2655static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2656{
Trond Myklebust16e42952005-10-27 22:12:44 -04002657 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002658 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002660 .name.len = name->len,
2661 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002662 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002664 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002665 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002666 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002668 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2669 .rpc_argp = &args,
2670 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002672 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673
Trond Myklebustd3468902010-04-16 16:22:50 -04002674 res.dir_attr = nfs_alloc_fattr();
2675 if (res.dir_attr == NULL)
2676 goto out;
2677
Bryan Schumaker7c513052011-03-24 17:12:24 +00002678 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002679 if (status == 0) {
2680 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002681 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002682 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002683 nfs_free_fattr(res.dir_attr);
2684out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 return status;
2686}
2687
2688static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2689{
2690 struct nfs4_exception exception = { };
2691 int err;
2692 do {
2693 err = nfs4_handle_exception(NFS_SERVER(dir),
2694 _nfs4_proc_remove(dir, name),
2695 &exception);
2696 } while (exception.retry);
2697 return err;
2698}
2699
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002700static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002702 struct nfs_server *server = NFS_SERVER(dir);
2703 struct nfs_removeargs *args = msg->rpc_argp;
2704 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705
Trond Myklebusta65318b2009-03-11 14:10:28 -04002706 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002707 res->server = server;
Benny Halevydfb4f3092010-09-24 09:17:01 -04002708 res->seq_res.sr_slot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710}
2711
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002712static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002714 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2715
Trond Myklebust14516c32010-07-31 14:29:06 -04002716 if (!nfs4_sequence_done(task, &res->seq_res))
2717 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002718 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002719 return 0;
2720 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002721 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002722 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723}
2724
Jeff Laytond3d41522010-09-17 17:31:57 -04002725static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2726{
2727 struct nfs_server *server = NFS_SERVER(dir);
2728 struct nfs_renameargs *arg = msg->rpc_argp;
2729 struct nfs_renameres *res = msg->rpc_resp;
2730
2731 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2732 arg->bitmask = server->attr_bitmask;
2733 res->server = server;
2734}
2735
2736static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2737 struct inode *new_dir)
2738{
2739 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2740
2741 if (!nfs4_sequence_done(task, &res->seq_res))
2742 return 0;
2743 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2744 return 0;
2745
2746 update_changeattr(old_dir, &res->old_cinfo);
2747 nfs_post_op_update_inode(old_dir, res->old_fattr);
2748 update_changeattr(new_dir, &res->new_cinfo);
2749 nfs_post_op_update_inode(new_dir, res->new_fattr);
2750 return 1;
2751}
2752
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2754 struct inode *new_dir, struct qstr *new_name)
2755{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002756 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002757 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 .old_dir = NFS_FH(old_dir),
2759 .new_dir = NFS_FH(new_dir),
2760 .old_name = old_name,
2761 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002762 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002764 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002765 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002766 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 struct rpc_message msg = {
2768 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2769 .rpc_argp = &arg,
2770 .rpc_resp = &res,
2771 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002772 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773
Trond Myklebust011fff72010-04-16 16:22:49 -04002774 res.old_fattr = nfs_alloc_fattr();
2775 res.new_fattr = nfs_alloc_fattr();
2776 if (res.old_fattr == NULL || res.new_fattr == NULL)
2777 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
Bryan Schumaker7c513052011-03-24 17:12:24 +00002779 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 if (!status) {
2781 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002782 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002784 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002786out:
2787 nfs_free_fattr(res.new_fattr);
2788 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 return status;
2790}
2791
2792static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2793 struct inode *new_dir, struct qstr *new_name)
2794{
2795 struct nfs4_exception exception = { };
2796 int err;
2797 do {
2798 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2799 _nfs4_proc_rename(old_dir, old_name,
2800 new_dir, new_name),
2801 &exception);
2802 } while (exception.retry);
2803 return err;
2804}
2805
2806static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2807{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002808 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 struct nfs4_link_arg arg = {
2810 .fh = NFS_FH(inode),
2811 .dir_fh = NFS_FH(dir),
2812 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002813 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002815 struct nfs4_link_res res = {
2816 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002817 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 struct rpc_message msg = {
2819 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2820 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002821 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002823 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824
Trond Myklebust136f2622010-04-16 16:22:49 -04002825 res.fattr = nfs_alloc_fattr();
2826 res.dir_attr = nfs_alloc_fattr();
2827 if (res.fattr == NULL || res.dir_attr == NULL)
2828 goto out;
2829
Bryan Schumaker7c513052011-03-24 17:12:24 +00002830 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002831 if (!status) {
2832 update_changeattr(dir, &res.cinfo);
2833 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002834 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002835 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002836out:
2837 nfs_free_fattr(res.dir_attr);
2838 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 return status;
2840}
2841
2842static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2843{
2844 struct nfs4_exception exception = { };
2845 int err;
2846 do {
2847 err = nfs4_handle_exception(NFS_SERVER(inode),
2848 _nfs4_proc_link(inode, dir, name),
2849 &exception);
2850 } while (exception.retry);
2851 return err;
2852}
2853
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002854struct nfs4_createdata {
2855 struct rpc_message msg;
2856 struct nfs4_create_arg arg;
2857 struct nfs4_create_res res;
2858 struct nfs_fh fh;
2859 struct nfs_fattr fattr;
2860 struct nfs_fattr dir_fattr;
2861};
2862
2863static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2864 struct qstr *name, struct iattr *sattr, u32 ftype)
2865{
2866 struct nfs4_createdata *data;
2867
2868 data = kzalloc(sizeof(*data), GFP_KERNEL);
2869 if (data != NULL) {
2870 struct nfs_server *server = NFS_SERVER(dir);
2871
2872 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2873 data->msg.rpc_argp = &data->arg;
2874 data->msg.rpc_resp = &data->res;
2875 data->arg.dir_fh = NFS_FH(dir);
2876 data->arg.server = server;
2877 data->arg.name = name;
2878 data->arg.attrs = sattr;
2879 data->arg.ftype = ftype;
2880 data->arg.bitmask = server->attr_bitmask;
2881 data->res.server = server;
2882 data->res.fh = &data->fh;
2883 data->res.fattr = &data->fattr;
2884 data->res.dir_fattr = &data->dir_fattr;
2885 nfs_fattr_init(data->res.fattr);
2886 nfs_fattr_init(data->res.dir_fattr);
2887 }
2888 return data;
2889}
2890
2891static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2892{
Bryan Schumaker7c513052011-03-24 17:12:24 +00002893 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00002894 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002895 if (status == 0) {
2896 update_changeattr(dir, &data->res.dir_cinfo);
2897 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2898 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2899 }
2900 return status;
2901}
2902
2903static void nfs4_free_createdata(struct nfs4_createdata *data)
2904{
2905 kfree(data);
2906}
2907
Chuck Lever4f390c12006-08-22 20:06:22 -04002908static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002909 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002911 struct nfs4_createdata *data;
2912 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913
Chuck Lever94a6d752006-08-22 20:06:23 -04002914 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002915 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002916
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002917 status = -ENOMEM;
2918 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2919 if (data == NULL)
2920 goto out;
2921
2922 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2923 data->arg.u.symlink.pages = &page;
2924 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002926 status = nfs4_do_create(dir, dentry, data);
2927
2928 nfs4_free_createdata(data);
2929out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 return status;
2931}
2932
Chuck Lever4f390c12006-08-22 20:06:22 -04002933static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002934 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935{
2936 struct nfs4_exception exception = { };
2937 int err;
2938 do {
2939 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002940 _nfs4_proc_symlink(dir, dentry, page,
2941 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 &exception);
2943 } while (exception.retry);
2944 return err;
2945}
2946
2947static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2948 struct iattr *sattr)
2949{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002950 struct nfs4_createdata *data;
2951 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002953 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2954 if (data == NULL)
2955 goto out;
2956
2957 status = nfs4_do_create(dir, dentry, data);
2958
2959 nfs4_free_createdata(data);
2960out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 return status;
2962}
2963
2964static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2965 struct iattr *sattr)
2966{
2967 struct nfs4_exception exception = { };
2968 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002969
2970 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 do {
2972 err = nfs4_handle_exception(NFS_SERVER(dir),
2973 _nfs4_proc_mkdir(dir, dentry, sattr),
2974 &exception);
2975 } while (exception.retry);
2976 return err;
2977}
2978
2979static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002980 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981{
2982 struct inode *dir = dentry->d_inode;
2983 struct nfs4_readdir_arg args = {
2984 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002985 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 .pgbase = 0,
2987 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002988 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04002989 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 };
2991 struct nfs4_readdir_res res;
2992 struct rpc_message msg = {
2993 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2994 .rpc_argp = &args,
2995 .rpc_resp = &res,
2996 .rpc_cred = cred,
2997 };
2998 int status;
2999
Harvey Harrison3110ff82008-05-02 13:42:44 -07003000 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00003001 dentry->d_parent->d_name.name,
3002 dentry->d_name.name,
3003 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
3005 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003006 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 -05003007 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05003009 status += args.pgbase;
3010 }
Trond Myklebustc4812992007-09-28 17:11:45 -04003011
3012 nfs_invalidate_atime(dir);
3013
Harvey Harrison3110ff82008-05-02 13:42:44 -07003014 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 return status;
3016}
3017
3018static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003019 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020{
3021 struct nfs4_exception exception = { };
3022 int err;
3023 do {
3024 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
3025 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04003026 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 &exception);
3028 } while (exception.retry);
3029 return err;
3030}
3031
3032static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3033 struct iattr *sattr, dev_t rdev)
3034{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003035 struct nfs4_createdata *data;
3036 int mode = sattr->ia_mode;
3037 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038
3039 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3040 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003041
3042 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3043 if (data == NULL)
3044 goto out;
3045
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003047 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003049 data->arg.ftype = NF4BLK;
3050 data->arg.u.device.specdata1 = MAJOR(rdev);
3051 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 }
3053 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003054 data->arg.ftype = NF4CHR;
3055 data->arg.u.device.specdata1 = MAJOR(rdev);
3056 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003059 status = nfs4_do_create(dir, dentry, data);
3060
3061 nfs4_free_createdata(data);
3062out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 return status;
3064}
3065
3066static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3067 struct iattr *sattr, dev_t rdev)
3068{
3069 struct nfs4_exception exception = { };
3070 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003071
3072 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 do {
3074 err = nfs4_handle_exception(NFS_SERVER(dir),
3075 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3076 &exception);
3077 } while (exception.retry);
3078 return err;
3079}
3080
3081static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3082 struct nfs_fsstat *fsstat)
3083{
3084 struct nfs4_statfs_arg args = {
3085 .fh = fhandle,
3086 .bitmask = server->attr_bitmask,
3087 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003088 struct nfs4_statfs_res res = {
3089 .fsstat = fsstat,
3090 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 struct rpc_message msg = {
3092 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3093 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003094 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 };
3096
Trond Myklebust0e574af2005-10-27 22:12:38 -04003097 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003098 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099}
3100
3101static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3102{
3103 struct nfs4_exception exception = { };
3104 int err;
3105 do {
3106 err = nfs4_handle_exception(server,
3107 _nfs4_proc_statfs(server, fhandle, fsstat),
3108 &exception);
3109 } while (exception.retry);
3110 return err;
3111}
3112
3113static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3114 struct nfs_fsinfo *fsinfo)
3115{
3116 struct nfs4_fsinfo_arg args = {
3117 .fh = fhandle,
3118 .bitmask = server->attr_bitmask,
3119 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003120 struct nfs4_fsinfo_res res = {
3121 .fsinfo = fsinfo,
3122 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 struct rpc_message msg = {
3124 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3125 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003126 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 };
3128
Bryan Schumaker7c513052011-03-24 17:12:24 +00003129 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130}
3131
3132static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3133{
3134 struct nfs4_exception exception = { };
3135 int err;
3136
3137 do {
3138 err = nfs4_handle_exception(server,
3139 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3140 &exception);
3141 } while (exception.retry);
3142 return err;
3143}
3144
3145static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3146{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003147 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3149}
3150
3151static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3152 struct nfs_pathconf *pathconf)
3153{
3154 struct nfs4_pathconf_arg args = {
3155 .fh = fhandle,
3156 .bitmask = server->attr_bitmask,
3157 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003158 struct nfs4_pathconf_res res = {
3159 .pathconf = pathconf,
3160 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 struct rpc_message msg = {
3162 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3163 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003164 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 };
3166
3167 /* None of the pathconf attributes are mandatory to implement */
3168 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3169 memset(pathconf, 0, sizeof(*pathconf));
3170 return 0;
3171 }
3172
Trond Myklebust0e574af2005-10-27 22:12:38 -04003173 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003174 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175}
3176
3177static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3178 struct nfs_pathconf *pathconf)
3179{
3180 struct nfs4_exception exception = { };
3181 int err;
3182
3183 do {
3184 err = nfs4_handle_exception(server,
3185 _nfs4_proc_pathconf(server, fhandle, pathconf),
3186 &exception);
3187 } while (exception.retry);
3188 return err;
3189}
3190
Benny Halevyd20581a2011-05-22 19:52:03 +03003191void __nfs4_read_done_cb(struct nfs_read_data *data)
3192{
3193 nfs_invalidate_atime(data->inode);
3194}
3195
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003196static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197{
Trond Myklebustec06c092006-03-20 13:44:27 -05003198 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003200 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003201 rpc_restart_call_prepare(task);
Trond Myklebustec06c092006-03-20 13:44:27 -05003202 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003204
Benny Halevyd20581a2011-05-22 19:52:03 +03003205 __nfs4_read_done_cb(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003207 renew_lease(server, data->timestamp);
3208 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209}
3210
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003211static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3212{
3213
3214 dprintk("--> %s\n", __func__);
3215
3216 if (!nfs4_sequence_done(task, &data->res.seq_res))
3217 return -EAGAIN;
3218
Benny Halevyd20581a2011-05-22 19:52:03 +03003219 return data->read_done_cb ? data->read_done_cb(task, data) :
3220 nfs4_read_done_cb(task, data);
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003221}
3222
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003223static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003226 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003227 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228}
3229
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003230/* Reset the the nfs_read_data to send the read to the MDS. */
3231void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3232{
3233 dprintk("%s Reset task for i/o through\n", __func__);
3234 put_lseg(data->lseg);
3235 data->lseg = NULL;
3236 /* offsets will differ in the dense stripe case */
3237 data->args.offset = data->mds_offset;
3238 data->ds_clp = NULL;
3239 data->args.fh = NFS_FH(data->inode);
3240 data->read_done_cb = nfs4_read_done_cb;
3241 task->tk_ops = data->mds_ops;
3242 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3243}
3244EXPORT_SYMBOL_GPL(nfs4_reset_read);
3245
Fred Isamanb029bc92011-03-03 15:13:42 +00003246static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 struct inode *inode = data->inode;
3249
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003250 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003251 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003252 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003254 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003256 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003257 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003258 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259}
3260
Fred Isamanb029bc92011-03-03 15:13:42 +00003261static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3262{
3263 if (!nfs4_sequence_done(task, &data->res.seq_res))
3264 return -EAGAIN;
Benny Halevyd20581a2011-05-22 19:52:03 +03003265 return data->write_done_cb ? data->write_done_cb(task, data) :
3266 nfs4_write_done_cb(task, data);
Fred Isamanb029bc92011-03-03 15:13:42 +00003267}
3268
Fred Isamana69aef12011-03-03 15:13:47 +00003269/* Reset the the nfs_write_data to send the write to the MDS. */
3270void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3271{
3272 dprintk("%s Reset task for i/o through\n", __func__);
3273 put_lseg(data->lseg);
3274 data->lseg = NULL;
3275 data->ds_clp = NULL;
3276 data->write_done_cb = nfs4_write_done_cb;
3277 data->args.fh = NFS_FH(data->inode);
3278 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3279 data->args.offset = data->mds_offset;
3280 data->res.fattr = &data->fattr;
3281 task->tk_ops = data->mds_ops;
3282 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3283}
3284EXPORT_SYMBOL_GPL(nfs4_reset_write);
3285
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003286static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003288 struct nfs_server *server = NFS_SERVER(data->inode);
3289
Fred Isaman7ffd1062011-03-03 15:13:46 +00003290 if (data->lseg) {
3291 data->args.bitmask = NULL;
3292 data->res.fattr = NULL;
3293 } else
3294 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isamanb029bc92011-03-03 15:13:42 +00003295 if (!data->write_done_cb)
3296 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003297 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 data->timestamp = jiffies;
3299
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003300 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301}
3302
Fred Isaman5f452432011-03-23 13:27:46 +00003303static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003306
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003307 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003308 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003309 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003311 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003312 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313}
3314
Fred Isaman5f452432011-03-23 13:27:46 +00003315static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3316{
3317 if (!nfs4_sequence_done(task, &data->res.seq_res))
3318 return -EAGAIN;
3319 return data->write_done_cb(task, data);
3320}
3321
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003322static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003324 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003325
3326 if (data->lseg) {
3327 data->args.bitmask = NULL;
3328 data->res.fattr = NULL;
3329 } else
3330 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isaman5f452432011-03-23 13:27:46 +00003331 if (!data->write_done_cb)
3332 data->write_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003333 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003334 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335}
3336
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003337struct nfs4_renewdata {
3338 struct nfs_client *client;
3339 unsigned long timestamp;
3340};
3341
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342/*
3343 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3344 * standalone procedure for queueing an asynchronous RENEW.
3345 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003346static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003347{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003348 struct nfs4_renewdata *data = calldata;
3349 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003350
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003351 if (atomic_read(&clp->cl_count) > 1)
3352 nfs4_schedule_state_renewal(clp);
3353 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003354 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003355}
3356
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003357static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003359 struct nfs4_renewdata *data = calldata;
3360 struct nfs_client *clp = data->client;
3361 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362
3363 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003364 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04003365 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
3366 return;
3367 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003368 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04003369 return;
3370 }
3371 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003373 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374}
3375
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003376static const struct rpc_call_ops nfs4_renew_ops = {
3377 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003378 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003379};
3380
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003381static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382{
3383 struct rpc_message msg = {
3384 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3385 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003386 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003388 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389
Trond Myklebust2f60ea62011-08-24 15:07:37 -04003390 if (renew_flags == 0)
3391 return 0;
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003392 if (!atomic_inc_not_zero(&clp->cl_count))
3393 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04003394 data = kmalloc(sizeof(*data), GFP_NOFS);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003395 if (data == NULL)
3396 return -ENOMEM;
3397 data->client = clp;
3398 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003400 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401}
3402
Trond Myklebust8534d4e2011-08-24 15:07:37 -04003403static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404{
3405 struct rpc_message msg = {
3406 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3407 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003408 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 };
3410 unsigned long now = jiffies;
3411 int status;
3412
3413 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3414 if (status < 0)
3415 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003416 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 return 0;
3418}
3419
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003420static inline int nfs4_server_supports_acls(struct nfs_server *server)
3421{
3422 return (server->caps & NFS_CAP_ACLS)
3423 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3424 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3425}
3426
3427/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3428 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3429 * the stack.
3430 */
3431#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3432
3433static void buf_to_pages(const void *buf, size_t buflen,
3434 struct page **pages, unsigned int *pgbase)
3435{
3436 const void *p = buf;
3437
3438 *pgbase = offset_in_page(buf);
3439 p -= *pgbase;
3440 while (p < buf + buflen) {
3441 *(pages++) = virt_to_page(p);
3442 p += PAGE_CACHE_SIZE;
3443 }
3444}
3445
Neil Hormane9e3d722011-03-04 19:26:03 -05003446static int buf_to_pages_noslab(const void *buf, size_t buflen,
3447 struct page **pages, unsigned int *pgbase)
3448{
3449 struct page *newpage, **spages;
3450 int rc = 0;
3451 size_t len;
3452 spages = pages;
3453
3454 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003455 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003456 newpage = alloc_page(GFP_KERNEL);
3457
3458 if (newpage == NULL)
3459 goto unwind;
3460 memcpy(page_address(newpage), buf, len);
3461 buf += len;
3462 buflen -= len;
3463 *pages++ = newpage;
3464 rc++;
3465 } while (buflen != 0);
3466
3467 return rc;
3468
3469unwind:
3470 for(; rc > 0; rc--)
3471 __free_page(spages[rc-1]);
3472 return -ENOMEM;
3473}
3474
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003475struct nfs4_cached_acl {
3476 int cached;
3477 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003478 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003479};
3480
3481static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003482{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003483 struct nfs_inode *nfsi = NFS_I(inode);
3484
3485 spin_lock(&inode->i_lock);
3486 kfree(nfsi->nfs4_acl);
3487 nfsi->nfs4_acl = acl;
3488 spin_unlock(&inode->i_lock);
3489}
3490
3491static void nfs4_zap_acl_attr(struct inode *inode)
3492{
3493 nfs4_set_cached_acl(inode, NULL);
3494}
3495
3496static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3497{
3498 struct nfs_inode *nfsi = NFS_I(inode);
3499 struct nfs4_cached_acl *acl;
3500 int ret = -ENOENT;
3501
3502 spin_lock(&inode->i_lock);
3503 acl = nfsi->nfs4_acl;
3504 if (acl == NULL)
3505 goto out;
3506 if (buf == NULL) /* user is just asking for length */
3507 goto out_len;
3508 if (acl->cached == 0)
3509 goto out;
3510 ret = -ERANGE; /* see getxattr(2) man page */
3511 if (acl->len > buflen)
3512 goto out;
3513 memcpy(buf, acl->data, acl->len);
3514out_len:
3515 ret = acl->len;
3516out:
3517 spin_unlock(&inode->i_lock);
3518 return ret;
3519}
3520
3521static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3522{
3523 struct nfs4_cached_acl *acl;
3524
3525 if (buf && acl_len <= PAGE_SIZE) {
3526 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3527 if (acl == NULL)
3528 goto out;
3529 acl->cached = 1;
3530 memcpy(acl->data, buf, acl_len);
3531 } else {
3532 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3533 if (acl == NULL)
3534 goto out;
3535 acl->cached = 0;
3536 }
3537 acl->len = acl_len;
3538out:
3539 nfs4_set_cached_acl(inode, acl);
3540}
3541
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003542static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003543{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003544 struct page *pages[NFS4ACL_MAXPAGES];
3545 struct nfs_getaclargs args = {
3546 .fh = NFS_FH(inode),
3547 .acl_pages = pages,
3548 .acl_len = buflen,
3549 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003550 struct nfs_getaclres res = {
3551 .acl_len = buflen,
3552 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003553 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003554 struct rpc_message msg = {
3555 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3556 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003557 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003558 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003559 struct page *localpage = NULL;
3560 int ret;
3561
3562 if (buflen < PAGE_SIZE) {
3563 /* As long as we're doing a round trip to the server anyway,
3564 * let's be prepared for a page of acl data. */
3565 localpage = alloc_page(GFP_KERNEL);
3566 resp_buf = page_address(localpage);
3567 if (localpage == NULL)
3568 return -ENOMEM;
3569 args.acl_pages[0] = localpage;
3570 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003571 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003572 } else {
3573 resp_buf = buf;
3574 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3575 }
Bryan Schumaker7c513052011-03-24 17:12:24 +00003576 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode), &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003577 if (ret)
3578 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003579 if (res.acl_len > args.acl_len)
3580 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003581 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003582 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003583 if (buf) {
3584 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003585 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003586 goto out_free;
3587 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003588 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003589 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003590 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003591out_free:
3592 if (localpage)
3593 __free_page(localpage);
3594 return ret;
3595}
3596
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003597static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3598{
3599 struct nfs4_exception exception = { };
3600 ssize_t ret;
3601 do {
3602 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3603 if (ret >= 0)
3604 break;
3605 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3606 } while (exception.retry);
3607 return ret;
3608}
3609
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003610static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3611{
3612 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003613 int ret;
3614
3615 if (!nfs4_server_supports_acls(server))
3616 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003617 ret = nfs_revalidate_inode(server, inode);
3618 if (ret < 0)
3619 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003620 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3621 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003622 ret = nfs4_read_cached_acl(inode, buf, buflen);
3623 if (ret != -ENOENT)
3624 return ret;
3625 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003626}
3627
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003628static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003629{
3630 struct nfs_server *server = NFS_SERVER(inode);
3631 struct page *pages[NFS4ACL_MAXPAGES];
3632 struct nfs_setaclargs arg = {
3633 .fh = NFS_FH(inode),
3634 .acl_pages = pages,
3635 .acl_len = buflen,
3636 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003637 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003638 struct rpc_message msg = {
3639 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3640 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003641 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003642 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003643 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003644
3645 if (!nfs4_server_supports_acls(server))
3646 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003647 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3648 if (i < 0)
3649 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003650 nfs_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003651 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003652
3653 /*
3654 * Free each page after tx, so the only ref left is
3655 * held by the network stack
3656 */
3657 for (; i > 0; i--)
3658 put_page(pages[i-1]);
3659
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003660 /*
3661 * Acl update can result in inode attribute update.
3662 * so mark the attribute cache invalid.
3663 */
3664 spin_lock(&inode->i_lock);
3665 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3666 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003667 nfs_access_zap_cache(inode);
3668 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003669 return ret;
3670}
3671
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003672static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3673{
3674 struct nfs4_exception exception = { };
3675 int err;
3676 do {
3677 err = nfs4_handle_exception(NFS_SERVER(inode),
3678 __nfs4_proc_set_acl(inode, buf, buflen),
3679 &exception);
3680 } while (exception.retry);
3681 return err;
3682}
3683
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003685nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003687 struct nfs_client *clp = server->nfs_client;
3688
3689 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 return 0;
3691 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003692 case -NFS4ERR_ADMIN_REVOKED:
3693 case -NFS4ERR_BAD_STATEID:
3694 case -NFS4ERR_OPENMODE:
3695 if (state == NULL)
3696 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003697 nfs4_schedule_stateid_recovery(server, state);
3698 goto wait_on_recovery;
Trond Myklebust0ced63d2011-05-26 14:26:35 -04003699 case -NFS4ERR_EXPIRED:
3700 if (state != NULL)
3701 nfs4_schedule_stateid_recovery(server, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003703 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003704 nfs4_schedule_lease_recovery(clp);
3705 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003706#if defined(CONFIG_NFS_V4_1)
3707 case -NFS4ERR_BADSESSION:
3708 case -NFS4ERR_BADSLOT:
3709 case -NFS4ERR_BAD_HIGH_SLOT:
3710 case -NFS4ERR_DEADSESSION:
3711 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3712 case -NFS4ERR_SEQ_FALSE_RETRY:
3713 case -NFS4ERR_SEQ_MISORDERED:
3714 dprintk("%s ERROR %d, Reset session\n", __func__,
3715 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003716 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -04003717 task->tk_status = 0;
3718 return -EAGAIN;
3719#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003721 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003722 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003723 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3725 task->tk_status = 0;
3726 return -EAGAIN;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04003727 case -NFS4ERR_RETRY_UNCACHED_REP:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 case -NFS4ERR_OLD_STATEID:
3729 task->tk_status = 0;
3730 return -EAGAIN;
3731 }
3732 task->tk_status = nfs4_map_errors(task->tk_status);
3733 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003734wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003735 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003736 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3737 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3738 task->tk_status = 0;
3739 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740}
3741
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003742int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3743 unsigned short port, struct rpc_cred *cred,
3744 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745{
3746 nfs4_verifier sc_verifier;
3747 struct nfs4_setclientid setclientid = {
3748 .sc_verifier = &sc_verifier,
3749 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003750 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 };
3752 struct rpc_message msg = {
3753 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3754 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003755 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003756 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 };
Al Virobc4785c2006-10-19 23:28:51 -07003758 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 int loop = 0;
3760 int status;
3761
Al Virobc4785c2006-10-19 23:28:51 -07003762 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3764 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3765
3766 for(;;) {
3767 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003768 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003769 clp->cl_ipaddr,
3770 rpc_peeraddr2str(clp->cl_rpcclient,
3771 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003772 rpc_peeraddr2str(clp->cl_rpcclient,
3773 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003774 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 clp->cl_id_uniquifier);
3776 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003777 sizeof(setclientid.sc_netid),
3778 rpc_peeraddr2str(clp->cl_rpcclient,
3779 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003781 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 clp->cl_ipaddr, port >> 8, port & 255);
3783
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003784 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 if (status != -NFS4ERR_CLID_INUSE)
3786 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003787 if (loop != 0) {
3788 ++clp->cl_id_uniquifier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 break;
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003790 }
3791 ++loop;
3792 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 }
3794 return status;
3795}
3796
Trond Myklebustfd954ae2011-04-24 14:28:18 -04003797int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003798 struct nfs4_setclientid_res *arg,
3799 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800{
3801 struct nfs_fsinfo fsinfo;
3802 struct rpc_message msg = {
3803 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003804 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003806 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 };
3808 unsigned long now;
3809 int status;
3810
3811 now = jiffies;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04003812 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 if (status == 0) {
3814 spin_lock(&clp->cl_lock);
3815 clp->cl_lease_time = fsinfo.lease_time * HZ;
3816 clp->cl_last_renewal = now;
3817 spin_unlock(&clp->cl_lock);
3818 }
3819 return status;
3820}
3821
Trond Myklebustfe650402006-01-03 09:55:18 +01003822struct nfs4_delegreturndata {
3823 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003824 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003825 struct nfs_fh fh;
3826 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003827 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003828 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003829 int rpc_status;
3830};
3831
Trond Myklebustfe650402006-01-03 09:55:18 +01003832static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3833{
3834 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003835
Trond Myklebust14516c32010-07-31 14:29:06 -04003836 if (!nfs4_sequence_done(task, &data->res.seq_res))
3837 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003838
Ricardo Labiaga79708862009-12-07 09:23:21 -05003839 switch (task->tk_status) {
3840 case -NFS4ERR_STALE_STATEID:
3841 case -NFS4ERR_EXPIRED:
3842 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003843 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003844 break;
3845 default:
3846 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3847 -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07003848 rpc_restart_call_prepare(task);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003849 return;
3850 }
3851 }
3852 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003853}
3854
3855static void nfs4_delegreturn_release(void *calldata)
3856{
Trond Myklebustfe650402006-01-03 09:55:18 +01003857 kfree(calldata);
3858}
3859
Andy Adamson938e1012009-04-01 09:22:28 -04003860#if defined(CONFIG_NFS_V4_1)
3861static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3862{
3863 struct nfs4_delegreturndata *d_data;
3864
3865 d_data = (struct nfs4_delegreturndata *)data;
3866
Trond Myklebust035168a2010-06-16 09:52:26 -04003867 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003868 &d_data->args.seq_args,
3869 &d_data->res.seq_res, 1, task))
3870 return;
3871 rpc_call_start(task);
3872}
3873#endif /* CONFIG_NFS_V4_1 */
3874
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003875static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003876#if defined(CONFIG_NFS_V4_1)
3877 .rpc_call_prepare = nfs4_delegreturn_prepare,
3878#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003879 .rpc_call_done = nfs4_delegreturn_done,
3880 .rpc_release = nfs4_delegreturn_release,
3881};
3882
Trond Myklebuste6f81072008-01-24 18:14:34 -05003883static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003884{
3885 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003886 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003887 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003888 struct rpc_message msg = {
3889 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3890 .rpc_cred = cred,
3891 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003892 struct rpc_task_setup task_setup_data = {
3893 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003894 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003895 .callback_ops = &nfs4_delegreturn_ops,
3896 .flags = RPC_TASK_ASYNC,
3897 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003898 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003899
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003900 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003901 if (data == NULL)
3902 return -ENOMEM;
3903 data->args.fhandle = &data->fh;
3904 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003905 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003906 nfs_copy_fh(&data->fh, NFS_FH(inode));
3907 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003908 data->res.fattr = &data->fattr;
3909 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003910 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003911 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003912 data->rpc_status = 0;
3913
Trond Myklebustc970aa82007-07-14 15:39:59 -04003914 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05003915 msg.rpc_argp = &data->args;
3916 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003917 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003918 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003919 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003920 if (!issync)
3921 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003922 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003923 if (status != 0)
3924 goto out;
3925 status = data->rpc_status;
3926 if (status != 0)
3927 goto out;
3928 nfs_refresh_inode(inode, &data->fattr);
3929out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003930 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003931 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932}
3933
Trond Myklebuste6f81072008-01-24 18:14:34 -05003934int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935{
3936 struct nfs_server *server = NFS_SERVER(inode);
3937 struct nfs4_exception exception = { };
3938 int err;
3939 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003940 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 switch (err) {
3942 case -NFS4ERR_STALE_STATEID:
3943 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 case 0:
3945 return 0;
3946 }
3947 err = nfs4_handle_exception(server, err, &exception);
3948 } while (exception.retry);
3949 return err;
3950}
3951
3952#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3953#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3954
3955/*
3956 * sleep, with exponential backoff, and retry the LOCK operation.
3957 */
3958static unsigned long
3959nfs4_set_lock_task_retry(unsigned long timeout)
3960{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003961 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 timeout <<= 1;
3963 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3964 return NFS4_LOCK_MAXTIMEOUT;
3965 return timeout;
3966}
3967
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3969{
3970 struct inode *inode = state->inode;
3971 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003972 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003973 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003975 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003977 struct nfs_lockt_res res = {
3978 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 };
3980 struct rpc_message msg = {
3981 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3982 .rpc_argp = &arg,
3983 .rpc_resp = &res,
3984 .rpc_cred = state->owner->so_cred,
3985 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 struct nfs4_lock_state *lsp;
3987 int status;
3988
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003989 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003990 status = nfs4_set_lock_state(state, request);
3991 if (status != 0)
3992 goto out;
3993 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003994 arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05003995 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003996 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003997 switch (status) {
3998 case 0:
3999 request->fl_type = F_UNLCK;
4000 break;
4001 case -NFS4ERR_DENIED:
4002 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05004004 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00004005out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 return status;
4007}
4008
4009static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4010{
4011 struct nfs4_exception exception = { };
4012 int err;
4013
4014 do {
4015 err = nfs4_handle_exception(NFS_SERVER(state->inode),
4016 _nfs4_proc_getlk(state, cmd, request),
4017 &exception);
4018 } while (exception.retry);
4019 return err;
4020}
4021
4022static int do_vfs_lock(struct file *file, struct file_lock *fl)
4023{
4024 int res = 0;
4025 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
4026 case FL_POSIX:
4027 res = posix_lock_file_wait(file, fl);
4028 break;
4029 case FL_FLOCK:
4030 res = flock_lock_file_wait(file, fl);
4031 break;
4032 default:
4033 BUG();
4034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 return res;
4036}
4037
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004038struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004039 struct nfs_locku_args arg;
4040 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004041 struct nfs4_lock_state *lsp;
4042 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004043 struct file_lock fl;
4044 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004045 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004046};
4047
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004048static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4049 struct nfs_open_context *ctx,
4050 struct nfs4_lock_state *lsp,
4051 struct nfs_seqid *seqid)
4052{
4053 struct nfs4_unlockdata *p;
4054 struct inode *inode = lsp->ls_state->inode;
4055
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004056 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004057 if (p == NULL)
4058 return NULL;
4059 p->arg.fh = NFS_FH(inode);
4060 p->arg.fl = &p->fl;
4061 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004062 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004063 p->arg.stateid = &lsp->ls_stateid;
4064 p->lsp = lsp;
4065 atomic_inc(&lsp->ls_count);
4066 /* Ensure we don't close file until we're done freeing locks! */
4067 p->ctx = get_nfs_open_context(ctx);
4068 memcpy(&p->fl, fl, sizeof(p->fl));
4069 p->server = NFS_SERVER(inode);
4070 return p;
4071}
4072
Trond Myklebust06f814a2006-01-03 09:55:07 +01004073static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004074{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004075 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004076 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004077 nfs4_put_lock_state(calldata->lsp);
4078 put_nfs_open_context(calldata->ctx);
4079 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004080}
4081
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004082static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004083{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004084 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004085
Trond Myklebust14516c32010-07-31 14:29:06 -04004086 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4087 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004088 switch (task->tk_status) {
4089 case 0:
4090 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004091 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004092 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01004093 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004094 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004095 case -NFS4ERR_BAD_STATEID:
4096 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004097 case -NFS4ERR_STALE_STATEID:
4098 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004099 break;
4100 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004101 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004102 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004103 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004104}
4105
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004106static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004107{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004108 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004110 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004111 return;
4112 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004113 /* Note: exit _without_ running nfs4_locku_done */
4114 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004115 return;
4116 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004117 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004118 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004119 &calldata->arg.seq_args,
4120 &calldata->res.seq_res, 1, task))
4121 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004122 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123}
4124
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004125static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004126 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004127 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004128 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004129};
4130
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004131static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4132 struct nfs_open_context *ctx,
4133 struct nfs4_lock_state *lsp,
4134 struct nfs_seqid *seqid)
4135{
4136 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004137 struct rpc_message msg = {
4138 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4139 .rpc_cred = ctx->cred,
4140 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004141 struct rpc_task_setup task_setup_data = {
4142 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004143 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004144 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004145 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004146 .flags = RPC_TASK_ASYNC,
4147 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004148
Frank Filz137d6ac2007-07-09 15:32:29 -07004149 /* Ensure this is an unlock - when canceling a lock, the
4150 * canceled lock is passed in, and it won't be an unlock.
4151 */
4152 fl->fl_type = F_UNLCK;
4153
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004154 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4155 if (data == NULL) {
4156 nfs_free_seqid(seqid);
4157 return ERR_PTR(-ENOMEM);
4158 }
4159
Trond Myklebust1174dd12010-12-21 10:52:24 -05004160 msg.rpc_argp = &data->arg;
4161 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004162 task_setup_data.callback_data = data;
4163 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004164}
4165
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4167{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004168 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004169 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004170 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004171 struct rpc_task *task;
4172 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004173 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174
Trond Myklebust9b073572006-06-29 16:38:34 -04004175 status = nfs4_set_lock_state(state, request);
4176 /* Unlock _before_ we do the RPC call */
4177 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004178 down_read(&nfsi->rwsem);
4179 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4180 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004181 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004182 }
4183 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004184 if (status != 0)
4185 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004186 /* Is this a delegated lock? */
4187 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004188 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004189 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004190 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004191 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004192 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004193 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004194 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004195 status = PTR_ERR(task);
4196 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004197 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004198 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004199 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004200out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004201 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004202 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203}
4204
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004205struct nfs4_lockdata {
4206 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004207 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004208 struct nfs4_lock_state *lsp;
4209 struct nfs_open_context *ctx;
4210 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004211 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004212 int rpc_status;
4213 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004214 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004215};
4216
4217static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004218 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4219 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004220{
4221 struct nfs4_lockdata *p;
4222 struct inode *inode = lsp->ls_state->inode;
4223 struct nfs_server *server = NFS_SERVER(inode);
4224
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004225 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004226 if (p == NULL)
4227 return NULL;
4228
4229 p->arg.fh = NFS_FH(inode);
4230 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004231 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004232 if (p->arg.open_seqid == NULL)
4233 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004234 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004235 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004236 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004237 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004238 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004239 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004240 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004241 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004242 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004243 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004244 atomic_inc(&lsp->ls_count);
4245 p->ctx = get_nfs_open_context(ctx);
4246 memcpy(&p->fl, fl, sizeof(p->fl));
4247 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004248out_free_seqid:
4249 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004250out_free:
4251 kfree(p);
4252 return NULL;
4253}
4254
4255static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4256{
4257 struct nfs4_lockdata *data = calldata;
4258 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259
Harvey Harrison3110ff82008-05-02 13:42:44 -07004260 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004261 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4262 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004263 /* Do we need to do an open_to_lock_owner? */
4264 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004265 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4266 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004267 data->arg.open_stateid = &state->stateid;
4268 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004269 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004270 } else
4271 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004272 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004273 if (nfs4_setup_sequence(data->server,
4274 &data->arg.seq_args,
Andy Adamson66179ef2009-04-01 09:22:22 -04004275 &data->res.seq_res, 1, task))
4276 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004277 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004278 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004279}
4280
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004281static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4282{
4283 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4284 nfs4_lock_prepare(task, calldata);
4285}
4286
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004287static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4288{
4289 struct nfs4_lockdata *data = calldata;
4290
Harvey Harrison3110ff82008-05-02 13:42:44 -07004291 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004292
Trond Myklebust14516c32010-07-31 14:29:06 -04004293 if (!nfs4_sequence_done(task, &data->res.seq_res))
4294 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004295
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004296 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004297 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004298 if (data->rpc_status == 0)
4299 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4300 else
4301 goto out;
4302 }
4303 if (data->rpc_status == 0) {
4304 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4305 sizeof(data->lsp->ls_stateid.data));
4306 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Al Viro3d4ff432011-06-22 18:40:12 -04004307 renew_lease(NFS_SERVER(data->ctx->dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004308 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004309out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004310 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004311}
4312
4313static void nfs4_lock_release(void *calldata)
4314{
4315 struct nfs4_lockdata *data = calldata;
4316
Harvey Harrison3110ff82008-05-02 13:42:44 -07004317 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004318 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004319 if (data->cancelled != 0) {
4320 struct rpc_task *task;
4321 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4322 data->arg.lock_seqid);
4323 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004324 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004325 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004326 } else
4327 nfs_free_seqid(data->arg.lock_seqid);
4328 nfs4_put_lock_state(data->lsp);
4329 put_nfs_open_context(data->ctx);
4330 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004331 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004332}
4333
4334static const struct rpc_call_ops nfs4_lock_ops = {
4335 .rpc_call_prepare = nfs4_lock_prepare,
4336 .rpc_call_done = nfs4_lock_done,
4337 .rpc_release = nfs4_lock_release,
4338};
4339
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004340static const struct rpc_call_ops nfs4_recover_lock_ops = {
4341 .rpc_call_prepare = nfs4_recover_lock_prepare,
4342 .rpc_call_done = nfs4_lock_done,
4343 .rpc_release = nfs4_lock_release,
4344};
4345
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004346static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4347{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004348 switch (error) {
4349 case -NFS4ERR_ADMIN_REVOKED:
4350 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004351 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004352 if (new_lock_owner != 0 ||
4353 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004354 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004355 break;
4356 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004357 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004358 case -NFS4ERR_EXPIRED:
4359 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004360 };
4361}
4362
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004363static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004364{
4365 struct nfs4_lockdata *data;
4366 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004367 struct rpc_message msg = {
4368 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4369 .rpc_cred = state->owner->so_cred,
4370 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004371 struct rpc_task_setup task_setup_data = {
4372 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004373 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004374 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004375 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004376 .flags = RPC_TASK_ASYNC,
4377 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004378 int ret;
4379
Harvey Harrison3110ff82008-05-02 13:42:44 -07004380 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004381 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004382 fl->fl_u.nfs4_fl.owner,
4383 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004384 if (data == NULL)
4385 return -ENOMEM;
4386 if (IS_SETLKW(cmd))
4387 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004388 if (recovery_type > NFS_LOCK_NEW) {
4389 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004390 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004391 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4392 }
Trond Myklebust1174dd12010-12-21 10:52:24 -05004393 msg.rpc_argp = &data->arg;
4394 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004395 task_setup_data.callback_data = data;
4396 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004397 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004398 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004399 ret = nfs4_wait_for_completion_rpc_task(task);
4400 if (ret == 0) {
4401 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004402 if (ret)
4403 nfs4_handle_setlk_error(data->server, data->lsp,
4404 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004405 } else
4406 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004407 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004408 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004409 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410}
4411
4412static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4413{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004414 struct nfs_server *server = NFS_SERVER(state->inode);
4415 struct nfs4_exception exception = { };
4416 int err;
4417
4418 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004419 /* Cache the lock if possible... */
4420 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4421 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004422 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004423 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004424 break;
4425 nfs4_handle_exception(server, err, &exception);
4426 } while (exception.retry);
4427 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428}
4429
4430static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4431{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004432 struct nfs_server *server = NFS_SERVER(state->inode);
4433 struct nfs4_exception exception = { };
4434 int err;
4435
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004436 err = nfs4_set_lock_state(state, request);
4437 if (err != 0)
4438 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004439 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004440 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4441 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004442 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004443 switch (err) {
4444 default:
4445 goto out;
4446 case -NFS4ERR_GRACE:
4447 case -NFS4ERR_DELAY:
4448 nfs4_handle_exception(server, err, &exception);
4449 err = 0;
4450 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004451 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004452out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004453 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454}
4455
Bryan Schumakerf062eb62011-06-02 14:59:10 -04004456#if defined(CONFIG_NFS_V4_1)
4457static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
4458{
4459 int status;
4460 struct nfs_server *server = NFS_SERVER(state->inode);
4461
4462 status = nfs41_test_stateid(server, state);
4463 if (status == NFS_OK)
4464 return 0;
4465 nfs41_free_stateid(server, state);
4466 return nfs4_lock_expired(state, request);
4467}
4468#endif
4469
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4471{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004472 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004473 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004474 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004476 if ((fl_flags & FL_POSIX) &&
4477 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4478 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004479 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004480 status = nfs4_set_lock_state(state, request);
4481 if (status != 0)
4482 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004483 request->fl_flags |= FL_ACCESS;
4484 status = do_vfs_lock(request->fl_file, request);
4485 if (status < 0)
4486 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004487 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004488 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004489 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004490 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004491 request->fl_flags = fl_flags & ~FL_SLEEP;
4492 status = do_vfs_lock(request->fl_file, request);
4493 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004494 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004495 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004496 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004497 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004498 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004499 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004500 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004501 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004502out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004503 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004504out:
4505 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 return status;
4507}
4508
4509static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4510{
4511 struct nfs4_exception exception = { };
4512 int err;
4513
4514 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004515 err = _nfs4_proc_setlk(state, cmd, request);
4516 if (err == -NFS4ERR_DENIED)
4517 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004519 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 } while (exception.retry);
4521 return err;
4522}
4523
4524static int
4525nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4526{
4527 struct nfs_open_context *ctx;
4528 struct nfs4_state *state;
4529 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4530 int status;
4531
4532 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004533 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 state = ctx->state;
4535
4536 if (request->fl_start < 0 || request->fl_end < 0)
4537 return -EINVAL;
4538
Trond Myklebustd9531262009-07-21 19:22:38 -04004539 if (IS_GETLK(cmd)) {
4540 if (state != NULL)
4541 return nfs4_proc_getlk(state, F_GETLK, request);
4542 return 0;
4543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544
4545 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4546 return -EINVAL;
4547
Trond Myklebustd9531262009-07-21 19:22:38 -04004548 if (request->fl_type == F_UNLCK) {
4549 if (state != NULL)
4550 return nfs4_proc_unlck(state, cmd, request);
4551 return 0;
4552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553
Trond Myklebustd9531262009-07-21 19:22:38 -04004554 if (state == NULL)
4555 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 do {
4557 status = nfs4_proc_setlk(state, cmd, request);
4558 if ((status != -EAGAIN) || IS_SETLK(cmd))
4559 break;
4560 timeout = nfs4_set_lock_task_retry(timeout);
4561 status = -ERESTARTSYS;
4562 if (signalled())
4563 break;
4564 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 return status;
4566}
4567
Trond Myklebust888e6942005-11-04 15:38:11 -05004568int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4569{
4570 struct nfs_server *server = NFS_SERVER(state->inode);
4571 struct nfs4_exception exception = { };
4572 int err;
4573
4574 err = nfs4_set_lock_state(state, fl);
4575 if (err != 0)
4576 goto out;
4577 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004578 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004579 switch (err) {
4580 default:
4581 printk(KERN_ERR "%s: unhandled error %d.\n",
4582 __func__, err);
4583 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004584 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004585 goto out;
4586 case -NFS4ERR_EXPIRED:
Trond Myklebust0ced63d2011-05-26 14:26:35 -04004587 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebustd5122202009-06-17 13:22:58 -07004588 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004589 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004590 nfs4_schedule_lease_recovery(server->nfs_client);
4591 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004592 case -NFS4ERR_BADSESSION:
4593 case -NFS4ERR_BADSLOT:
4594 case -NFS4ERR_BAD_HIGH_SLOT:
4595 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4596 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004597 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004598 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004599 case -ERESTARTSYS:
4600 /*
4601 * The show must go on: exit, but mark the
4602 * stateid as needing recovery.
4603 */
4604 case -NFS4ERR_ADMIN_REVOKED:
4605 case -NFS4ERR_BAD_STATEID:
4606 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004607 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004608 err = 0;
4609 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004610 case -EKEYEXPIRED:
4611 /*
4612 * User RPCSEC_GSS context has expired.
4613 * We cannot recover this stateid now, so
4614 * skip it and allow recovery thread to
4615 * proceed.
4616 */
4617 err = 0;
4618 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004619 case -ENOMEM:
4620 case -NFS4ERR_DENIED:
4621 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4622 err = 0;
4623 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004624 case -NFS4ERR_DELAY:
4625 break;
4626 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004627 err = nfs4_handle_exception(server, err, &exception);
4628 } while (exception.retry);
4629out:
4630 return err;
4631}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004632
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004633static void nfs4_release_lockowner_release(void *calldata)
4634{
4635 kfree(calldata);
4636}
4637
4638const struct rpc_call_ops nfs4_release_lockowner_ops = {
4639 .rpc_release = nfs4_release_lockowner_release,
4640};
4641
4642void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4643{
4644 struct nfs_server *server = lsp->ls_state->owner->so_server;
4645 struct nfs_release_lockowner_args *args;
4646 struct rpc_message msg = {
4647 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4648 };
4649
4650 if (server->nfs_client->cl_mvops->minor_version != 0)
4651 return;
4652 args = kmalloc(sizeof(*args), GFP_NOFS);
4653 if (!args)
4654 return;
4655 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4656 args->lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004657 args->lock_owner.s_dev = server->s_dev;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004658 msg.rpc_argp = args;
4659 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4660}
4661
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004662#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4663
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004664static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4665 const void *buf, size_t buflen,
4666 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004667{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004668 if (strcmp(key, "") != 0)
4669 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004670
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004671 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004672}
4673
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004674static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4675 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004676{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004677 if (strcmp(key, "") != 0)
4678 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004679
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004680 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004681}
4682
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004683static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4684 size_t list_len, const char *name,
4685 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004686{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004687 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004688
J. Bruce Fields096455a2006-03-20 23:23:42 -05004689 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4690 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004691
4692 if (list && len <= list_len)
4693 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004694 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004695}
4696
Andy Adamson533eb462011-06-13 18:25:56 -04004697/*
4698 * nfs_fhget will use either the mounted_on_fileid or the fileid
4699 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004700static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4701{
Andy Adamson533eb462011-06-13 18:25:56 -04004702 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
4703 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
4704 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4705 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004706 return;
4707
4708 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4709 NFS_ATTR_FATTR_NLINK;
4710 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4711 fattr->nlink = 2;
4712}
4713
Trond Myklebust56659e92007-07-17 21:52:39 -04004714int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004715 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004716{
4717 struct nfs_server *server = NFS_SERVER(dir);
4718 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004719 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004720 };
4721 struct nfs4_fs_locations_arg args = {
4722 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004723 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004724 .page = page,
4725 .bitmask = bitmask,
4726 };
Benny Halevy22958462009-04-01 09:22:02 -04004727 struct nfs4_fs_locations_res res = {
4728 .fs_locations = fs_locations,
4729 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004730 struct rpc_message msg = {
4731 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4732 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004733 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004734 };
4735 int status;
4736
Harvey Harrison3110ff82008-05-02 13:42:44 -07004737 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04004738
4739 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
4740 * is not supported */
4741 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
4742 bitmask[1] |= FATTR4_WORD1_MOUNTED_ON_FILEID;
4743 else
4744 bitmask[0] |= FATTR4_WORD0_FILEID;
4745
Trond Myklebustc228fd32007-01-13 02:28:11 -05004746 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004747 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004748 fs_locations->nlocations = 0;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004749 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004750 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004751 return status;
4752}
4753
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00004754static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4755{
4756 int status;
4757 struct nfs4_secinfo_arg args = {
4758 .dir_fh = NFS_FH(dir),
4759 .name = name,
4760 };
4761 struct nfs4_secinfo_res res = {
4762 .flavors = flavors,
4763 };
4764 struct rpc_message msg = {
4765 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
4766 .rpc_argp = &args,
4767 .rpc_resp = &res,
4768 };
4769
4770 dprintk("NFS call secinfo %s\n", name->name);
4771 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4772 dprintk("NFS reply secinfo: %d\n", status);
4773 return status;
4774}
4775
4776int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4777{
4778 struct nfs4_exception exception = { };
4779 int err;
4780 do {
4781 err = nfs4_handle_exception(NFS_SERVER(dir),
4782 _nfs4_proc_secinfo(dir, name, flavors),
4783 &exception);
4784 } while (exception.retry);
4785 return err;
4786}
4787
Andy Adamson557134a2009-04-01 09:21:53 -04004788#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004789/*
Andy Adamson357f54d2010-12-14 10:11:57 -05004790 * Check the exchange flags returned by the server for invalid flags, having
4791 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4792 * DS flags set.
4793 */
4794static int nfs4_check_cl_exchange_flags(u32 flags)
4795{
4796 if (flags & ~EXCHGID4_FLAG_MASK_R)
4797 goto out_inval;
4798 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4799 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4800 goto out_inval;
4801 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4802 goto out_inval;
4803 return NFS_OK;
4804out_inval:
4805 return -NFS4ERR_INVAL;
4806}
4807
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004808static bool
4809nfs41_same_server_scope(struct server_scope *a, struct server_scope *b)
4810{
4811 if (a->server_scope_sz == b->server_scope_sz &&
4812 memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0)
4813 return true;
4814
4815 return false;
4816}
4817
Andy Adamson357f54d2010-12-14 10:11:57 -05004818/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04004819 * nfs4_proc_exchange_id()
4820 *
4821 * Since the clientid has expired, all compounds using sessions
4822 * associated with the stale clientid will be returning
4823 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4824 * be in some phase of session reset.
4825 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004826int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004827{
4828 nfs4_verifier verifier;
4829 struct nfs41_exchange_id_args args = {
4830 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05004831 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04004832 };
4833 struct nfs41_exchange_id_res res = {
4834 .client = clp,
4835 };
4836 int status;
4837 struct rpc_message msg = {
4838 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4839 .rpc_argp = &args,
4840 .rpc_resp = &res,
4841 .rpc_cred = cred,
4842 };
4843 __be32 *p;
4844
4845 dprintk("--> %s\n", __func__);
4846 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004847
Benny Halevy99fe60d2009-04-01 09:22:29 -04004848 p = (u32 *)verifier.data;
4849 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4850 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4851 args.verifier = &verifier;
4852
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004853 args.id_len = scnprintf(args.id, sizeof(args.id),
4854 "%s/%s.%s/%u",
4855 clp->cl_ipaddr,
4856 init_utsname()->nodename,
4857 init_utsname()->domainname,
4858 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004859
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004860 res.server_scope = kzalloc(sizeof(struct server_scope), GFP_KERNEL);
4861 if (unlikely(!res.server_scope))
4862 return -ENOMEM;
4863
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004864 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004865 if (!status)
4866 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04004867
4868 if (!status) {
4869 if (clp->server_scope &&
4870 !nfs41_same_server_scope(clp->server_scope,
4871 res.server_scope)) {
4872 dprintk("%s: server_scope mismatch detected\n",
4873 __func__);
4874 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
4875 kfree(clp->server_scope);
4876 clp->server_scope = NULL;
4877 }
4878
4879 if (!clp->server_scope)
4880 clp->server_scope = res.server_scope;
4881 else
4882 kfree(res.server_scope);
4883 }
4884
Benny Halevy99fe60d2009-04-01 09:22:29 -04004885 dprintk("<-- %s status= %d\n", __func__, status);
4886 return status;
4887}
4888
Andy Adamson2050f0c2009-04-01 09:22:30 -04004889struct nfs4_get_lease_time_data {
4890 struct nfs4_get_lease_time_args *args;
4891 struct nfs4_get_lease_time_res *res;
4892 struct nfs_client *clp;
4893};
4894
4895static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4896 void *calldata)
4897{
4898 int ret;
4899 struct nfs4_get_lease_time_data *data =
4900 (struct nfs4_get_lease_time_data *)calldata;
4901
4902 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004903 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004904 /* just setup sequence, do not trigger session recovery
4905 since we're invoked within one */
4906 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004907 &data->args->la_seq_args,
4908 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004909
4910 BUG_ON(ret == -EAGAIN);
4911 rpc_call_start(task);
4912 dprintk("<-- %s\n", __func__);
4913}
4914
4915/*
4916 * Called from nfs4_state_manager thread for session setup, so don't recover
4917 * from sequence operation or clientid errors.
4918 */
4919static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4920{
4921 struct nfs4_get_lease_time_data *data =
4922 (struct nfs4_get_lease_time_data *)calldata;
4923
4924 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04004925 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4926 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04004927 switch (task->tk_status) {
4928 case -NFS4ERR_DELAY:
4929 case -NFS4ERR_GRACE:
4930 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4931 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4932 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04004933 /* fall through */
4934 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004935 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004936 return;
4937 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004938 dprintk("<-- %s\n", __func__);
4939}
4940
4941struct rpc_call_ops nfs4_get_lease_time_ops = {
4942 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4943 .rpc_call_done = nfs4_get_lease_time_done,
4944};
4945
4946int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4947{
4948 struct rpc_task *task;
4949 struct nfs4_get_lease_time_args args;
4950 struct nfs4_get_lease_time_res res = {
4951 .lr_fsinfo = fsinfo,
4952 };
4953 struct nfs4_get_lease_time_data data = {
4954 .args = &args,
4955 .res = &res,
4956 .clp = clp,
4957 };
4958 struct rpc_message msg = {
4959 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4960 .rpc_argp = &args,
4961 .rpc_resp = &res,
4962 };
4963 struct rpc_task_setup task_setup = {
4964 .rpc_client = clp->cl_rpcclient,
4965 .rpc_message = &msg,
4966 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04004967 .callback_data = &data,
4968 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04004969 };
4970 int status;
4971
Andy Adamson2050f0c2009-04-01 09:22:30 -04004972 dprintk("--> %s\n", __func__);
4973 task = rpc_run_task(&task_setup);
4974
4975 if (IS_ERR(task))
4976 status = PTR_ERR(task);
4977 else {
4978 status = task->tk_status;
4979 rpc_put_task(task);
4980 }
4981 dprintk("<-- %s return %d\n", __func__, status);
4982
4983 return status;
4984}
4985
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004986/*
4987 * Reset a slot table
4988 */
Andy Adamson104aeba2010-01-14 17:45:10 -05004989static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4990 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004991{
Andy Adamson104aeba2010-01-14 17:45:10 -05004992 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004993 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004994 int ret = 0;
4995
Andy Adamson104aeba2010-01-14 17:45:10 -05004996 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4997 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004998
Andy Adamson104aeba2010-01-14 17:45:10 -05004999 /* Does the newly negotiated max_reqs match the existing slot table? */
5000 if (max_reqs != tbl->max_slots) {
5001 ret = -ENOMEM;
5002 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005003 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05005004 if (!new)
5005 goto out;
5006 ret = 0;
5007 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005008 }
5009 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05005010 if (new) {
5011 tbl->slots = new;
5012 tbl->max_slots = max_reqs;
5013 }
5014 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005015 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04005016 spin_unlock(&tbl->slot_tbl_lock);
5017 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5018 tbl, tbl->slots, tbl->max_slots);
5019out:
5020 dprintk("<-- %s: return %d\n", __func__, ret);
5021 return ret;
5022}
5023
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005024/* Destroy the slot table */
5025static void nfs4_destroy_slot_tables(struct nfs4_session *session)
5026{
5027 if (session->fc_slot_table.slots != NULL) {
5028 kfree(session->fc_slot_table.slots);
5029 session->fc_slot_table.slots = NULL;
5030 }
5031 if (session->bc_slot_table.slots != NULL) {
5032 kfree(session->bc_slot_table.slots);
5033 session->bc_slot_table.slots = NULL;
5034 }
5035 return;
5036}
5037
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04005038/*
Andy Adamsonfc931582009-04-01 09:22:31 -04005039 * Initialize slot table
5040 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005041static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
5042 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04005043{
Andy Adamsonfc931582009-04-01 09:22:31 -04005044 struct nfs4_slot *slot;
5045 int ret = -ENOMEM;
5046
5047 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
5048
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005049 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04005050
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005051 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04005052 if (!slot)
5053 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04005054 ret = 0;
5055
5056 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04005057 tbl->max_slots = max_slots;
5058 tbl->slots = slot;
5059 tbl->highest_used_slotid = -1; /* no slot is currently used */
5060 spin_unlock(&tbl->slot_tbl_lock);
5061 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
5062 tbl, tbl->slots, tbl->max_slots);
5063out:
5064 dprintk("<-- %s: return %d\n", __func__, ret);
5065 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04005066}
5067
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005068/*
Andy Adamsonaacd5532011-11-09 13:58:21 -05005069 * Initialize or reset the forechannel and backchannel tables
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005070 */
Andy Adamsonaacd5532011-11-09 13:58:21 -05005071static int nfs4_setup_session_slot_tables(struct nfs4_session *ses)
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005072{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005073 struct nfs4_slot_table *tbl;
Andy Adamsonaacd5532011-11-09 13:58:21 -05005074 int status;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005075
Andy Adamsonaacd5532011-11-09 13:58:21 -05005076 dprintk("--> %s\n", __func__);
5077 /* Fore channel */
5078 tbl = &ses->fc_slot_table;
Trond Myklebustf26468f2009-12-05 19:32:11 -05005079 if (tbl->slots == NULL) {
Andy Adamsonaacd5532011-11-09 13:58:21 -05005080 status = nfs4_init_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
5081 if (status) /* -ENOMEM */
5082 return status;
5083 } else {
5084 status = nfs4_reset_slot_table(tbl, ses->fc_attrs.max_reqs, 1);
Trond Myklebustf26468f2009-12-05 19:32:11 -05005085 if (status)
5086 return status;
5087 }
Andy Adamsonaacd5532011-11-09 13:58:21 -05005088 /* Back channel */
5089 tbl = &ses->bc_slot_table;
Trond Myklebustf26468f2009-12-05 19:32:11 -05005090 if (tbl->slots == NULL) {
Andy Adamsonaacd5532011-11-09 13:58:21 -05005091 status = nfs4_init_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
Trond Myklebustf26468f2009-12-05 19:32:11 -05005092 if (status)
Andy Adamsonaacd5532011-11-09 13:58:21 -05005093 /* Fore and back channel share a connection so get
5094 * both slot tables or neither */
5095 nfs4_destroy_slot_tables(ses);
5096 } else
5097 status = nfs4_reset_slot_table(tbl, ses->bc_attrs.max_reqs, 0);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005098 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005099}
5100
5101struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5102{
5103 struct nfs4_session *session;
5104 struct nfs4_slot_table *tbl;
5105
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005106 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005107 if (!session)
5108 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005109
Andy Adamson557134a2009-04-01 09:21:53 -04005110 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05005111 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04005112 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005113 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005114 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005115
5116 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05005117 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005118 spin_lock_init(&tbl->slot_tbl_lock);
5119 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005120 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005121
Trond Myklebust1055d762010-06-16 09:52:27 -04005122 session->session_state = 1<<NFS4_SESSION_INITING;
5123
Andy Adamson557134a2009-04-01 09:21:53 -04005124 session->clp = clp;
5125 return session;
5126}
5127
5128void nfs4_destroy_session(struct nfs4_session *session)
5129{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005130 nfs4_proc_destroy_session(session);
5131 dprintk("%s Destroy backchannel for xprt %p\n",
5132 __func__, session->clp->cl_rpcclient->cl_xprt);
5133 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
5134 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005135 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005136 kfree(session);
5137}
5138
Andy Adamsonfc931582009-04-01 09:22:31 -04005139/*
5140 * Initialize the values to be used by the client in CREATE_SESSION
5141 * If nfs4_init_session set the fore channel request and response sizes,
5142 * use them.
5143 *
5144 * Set the back channel max_resp_sz_cached to zero to force the client to
5145 * always set csa_cachethis to FALSE because the current implementation
5146 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5147 */
5148static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5149{
5150 struct nfs4_session *session = args->client->cl_session;
5151 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5152 mxresp_sz = session->fc_attrs.max_resp_sz;
5153
5154 if (mxrqst_sz == 0)
5155 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5156 if (mxresp_sz == 0)
5157 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5158 /* Fore channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005159 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5160 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005161 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5162 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
5163
5164 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005165 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005166 __func__,
5167 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005168 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005169
5170 /* Back channel attributes */
Andy Adamsonfc931582009-04-01 09:22:31 -04005171 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5172 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5173 args->bc_attrs.max_resp_sz_cached = 0;
5174 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5175 args->bc_attrs.max_reqs = 1;
5176
5177 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5178 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5179 __func__,
5180 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5181 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5182 args->bc_attrs.max_reqs);
5183}
5184
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005185static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005186{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005187 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5188 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5189
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005190 if (rcvd->max_resp_sz > sent->max_resp_sz)
5191 return -EINVAL;
5192 /*
5193 * Our requested max_ops is the minimum we need; we're not
5194 * prepared to break up compounds into smaller pieces than that.
5195 * So, no point even trying to continue if the server won't
5196 * cooperate:
5197 */
5198 if (rcvd->max_ops < sent->max_ops)
5199 return -EINVAL;
5200 if (rcvd->max_reqs == 0)
5201 return -EINVAL;
5202 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005203}
5204
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005205static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5206{
5207 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5208 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005209
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005210 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5211 return -EINVAL;
5212 if (rcvd->max_resp_sz < sent->max_resp_sz)
5213 return -EINVAL;
5214 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5215 return -EINVAL;
5216 /* These would render the backchannel useless: */
5217 if (rcvd->max_ops == 0)
5218 return -EINVAL;
5219 if (rcvd->max_reqs == 0)
5220 return -EINVAL;
5221 return 0;
5222}
Andy Adamson8d353012009-04-01 09:22:32 -04005223
Andy Adamson8d353012009-04-01 09:22:32 -04005224static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5225 struct nfs4_session *session)
5226{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005227 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005228
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005229 ret = nfs4_verify_fore_channel_attrs(args, session);
5230 if (ret)
5231 return ret;
5232 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005233}
5234
Andy Adamsonfc931582009-04-01 09:22:31 -04005235static int _nfs4_proc_create_session(struct nfs_client *clp)
5236{
5237 struct nfs4_session *session = clp->cl_session;
5238 struct nfs41_create_session_args args = {
5239 .client = clp,
5240 .cb_program = NFS4_CALLBACK,
5241 };
5242 struct nfs41_create_session_res res = {
5243 .client = clp,
5244 };
5245 struct rpc_message msg = {
5246 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5247 .rpc_argp = &args,
5248 .rpc_resp = &res,
5249 };
5250 int status;
5251
5252 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005253 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005254
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005255 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamsonfc931582009-04-01 09:22:31 -04005256
Andy Adamson8d353012009-04-01 09:22:32 -04005257 if (!status)
5258 /* Verify the session's negotiated channel_attrs values */
5259 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005260 if (!status) {
5261 /* Increment the clientid slot sequence id */
5262 clp->cl_seqid++;
5263 }
5264
5265 return status;
5266}
5267
5268/*
5269 * Issues a CREATE_SESSION operation to the server.
5270 * It is the responsibility of the caller to verify the session is
5271 * expired before calling this routine.
5272 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005273int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005274{
5275 int status;
5276 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005277 struct nfs4_session *session = clp->cl_session;
5278
5279 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5280
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005281 status = _nfs4_proc_create_session(clp);
Andy Adamsonfc931582009-04-01 09:22:31 -04005282 if (status)
5283 goto out;
5284
Andy Adamsonaacd5532011-11-09 13:58:21 -05005285 /* Init or reset the session slot tables */
5286 status = nfs4_setup_session_slot_tables(session);
5287 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005288 if (status)
5289 goto out;
5290
5291 ptr = (unsigned *)&session->sess_id.data[0];
5292 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5293 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005294out:
5295 dprintk("<-- %s\n", __func__);
5296 return status;
5297}
5298
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005299/*
5300 * Issue the over-the-wire RPC DESTROY_SESSION.
5301 * The caller must serialize access to this routine.
5302 */
5303int nfs4_proc_destroy_session(struct nfs4_session *session)
5304{
5305 int status = 0;
5306 struct rpc_message msg;
5307
5308 dprintk("--> nfs4_proc_destroy_session\n");
5309
5310 /* session is still being setup */
5311 if (session->clp->cl_cons_state != NFS_CS_READY)
5312 return status;
5313
5314 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5315 msg.rpc_argp = session;
5316 msg.rpc_resp = NULL;
5317 msg.rpc_cred = NULL;
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005318 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005319
5320 if (status)
5321 printk(KERN_WARNING
5322 "Got error %d from the server on DESTROY_SESSION. "
5323 "Session has been destroyed regardless...\n", status);
5324
5325 dprintk("<-- nfs4_proc_destroy_session\n");
5326 return status;
5327}
5328
Trond Myklebustfccba802009-07-21 16:48:07 -04005329int nfs4_init_session(struct nfs_server *server)
5330{
5331 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005332 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005333 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005334 int ret;
5335
5336 if (!nfs4_has_session(clp))
5337 return 0;
5338
Trond Myklebust1055d762010-06-16 09:52:27 -04005339 session = clp->cl_session;
5340 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5341 return 0;
5342
Andy Adamson68bf05e2009-12-15 12:55:02 -05005343 rsize = server->rsize;
5344 if (rsize == 0)
5345 rsize = NFS_MAX_FILE_IO_SIZE;
5346 wsize = server->wsize;
5347 if (wsize == 0)
5348 wsize = NFS_MAX_FILE_IO_SIZE;
5349
Andy Adamson68bf05e2009-12-15 12:55:02 -05005350 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5351 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005352
Trond Myklebustfccba802009-07-21 16:48:07 -04005353 ret = nfs4_recover_expired_lease(server);
5354 if (!ret)
5355 ret = nfs4_check_client_ready(clp);
5356 return ret;
5357}
5358
Andy Adamsond83217c2011-03-01 01:34:17 +00005359int nfs4_init_ds_session(struct nfs_client *clp)
5360{
5361 struct nfs4_session *session = clp->cl_session;
5362 int ret;
5363
5364 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5365 return 0;
5366
5367 ret = nfs4_client_recover_expired_lease(clp);
5368 if (!ret)
5369 /* Test for the DS role */
5370 if (!is_ds_client(clp))
5371 ret = -ENODEV;
5372 if (!ret)
5373 ret = nfs4_check_client_ready(clp);
5374 return ret;
5375
5376}
5377EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5378
5379
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005380/*
5381 * Renew the cl_session lease.
5382 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005383struct nfs4_sequence_data {
5384 struct nfs_client *clp;
5385 struct nfs4_sequence_args args;
5386 struct nfs4_sequence_res res;
5387};
5388
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005389static void nfs41_sequence_release(void *data)
5390{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005391 struct nfs4_sequence_data *calldata = data;
5392 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005393
Alexandros Batsakis71358402010-02-05 03:45:05 -08005394 if (atomic_read(&clp->cl_count) > 1)
5395 nfs4_schedule_state_renewal(clp);
5396 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005397 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005398}
5399
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005400static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5401{
5402 switch(task->tk_status) {
5403 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005404 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5405 return -EAGAIN;
5406 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005407 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005408 }
5409 return 0;
5410}
5411
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005412static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005413{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005414 struct nfs4_sequence_data *calldata = data;
5415 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005416
Trond Myklebust14516c32010-07-31 14:29:06 -04005417 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5418 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005419
5420 if (task->tk_status < 0) {
5421 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005422 if (atomic_read(&clp->cl_count) == 1)
5423 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005424
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005425 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5426 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005427 return;
5428 }
5429 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005430 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005431out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005432 dprintk("<-- %s\n", __func__);
5433}
5434
5435static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5436{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005437 struct nfs4_sequence_data *calldata = data;
5438 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005439 struct nfs4_sequence_args *args;
5440 struct nfs4_sequence_res *res;
5441
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005442 args = task->tk_msg.rpc_argp;
5443 res = task->tk_msg.rpc_resp;
5444
Trond Myklebust035168a2010-06-16 09:52:26 -04005445 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005446 return;
5447 rpc_call_start(task);
5448}
5449
5450static const struct rpc_call_ops nfs41_sequence_ops = {
5451 .rpc_call_done = nfs41_sequence_call_done,
5452 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005453 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005454};
5455
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005456static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005457{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005458 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005459 struct rpc_message msg = {
5460 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5461 .rpc_cred = cred,
5462 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005463 struct rpc_task_setup task_setup_data = {
5464 .rpc_client = clp->cl_rpcclient,
5465 .rpc_message = &msg,
5466 .callback_ops = &nfs41_sequence_ops,
5467 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5468 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005469
Alexandros Batsakis71358402010-02-05 03:45:05 -08005470 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005471 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005472 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005473 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005474 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005475 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005476 }
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005477 msg.rpc_argp = &calldata->args;
5478 msg.rpc_resp = &calldata->res;
5479 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005480 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005481
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005482 return rpc_run_task(&task_setup_data);
5483}
5484
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005485static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005486{
5487 struct rpc_task *task;
5488 int ret = 0;
5489
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005490 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
5491 return 0;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005492 task = _nfs41_proc_sequence(clp, cred);
5493 if (IS_ERR(task))
5494 ret = PTR_ERR(task);
5495 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005496 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005497 dprintk("<-- %s status=%d\n", __func__, ret);
5498 return ret;
5499}
5500
5501static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5502{
5503 struct rpc_task *task;
5504 int ret;
5505
5506 task = _nfs41_proc_sequence(clp, cred);
5507 if (IS_ERR(task)) {
5508 ret = PTR_ERR(task);
5509 goto out;
5510 }
5511 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005512 if (!ret) {
5513 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5514
5515 if (task->tk_status == 0)
5516 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005517 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005518 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005519 rpc_put_task(task);
5520out:
5521 dprintk("<-- %s status=%d\n", __func__, ret);
5522 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005523}
5524
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005525struct nfs4_reclaim_complete_data {
5526 struct nfs_client *clp;
5527 struct nfs41_reclaim_complete_args arg;
5528 struct nfs41_reclaim_complete_res res;
5529};
5530
5531static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5532{
5533 struct nfs4_reclaim_complete_data *calldata = data;
5534
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005535 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005536 if (nfs41_setup_sequence(calldata->clp->cl_session,
5537 &calldata->arg.seq_args,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005538 &calldata->res.seq_res, 0, task))
5539 return;
5540
5541 rpc_call_start(task);
5542}
5543
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005544static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5545{
5546 switch(task->tk_status) {
5547 case 0:
5548 case -NFS4ERR_COMPLETE_ALREADY:
5549 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5550 break;
5551 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005552 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04005553 /* fall through */
5554 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005555 return -EAGAIN;
5556 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005557 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005558 }
5559 return 0;
5560}
5561
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005562static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5563{
5564 struct nfs4_reclaim_complete_data *calldata = data;
5565 struct nfs_client *clp = calldata->clp;
5566 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5567
5568 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005569 if (!nfs41_sequence_done(task, res))
5570 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005571
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005572 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5573 rpc_restart_call_prepare(task);
5574 return;
5575 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005576 dprintk("<-- %s\n", __func__);
5577}
5578
5579static void nfs4_free_reclaim_complete_data(void *data)
5580{
5581 struct nfs4_reclaim_complete_data *calldata = data;
5582
5583 kfree(calldata);
5584}
5585
5586static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5587 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5588 .rpc_call_done = nfs4_reclaim_complete_done,
5589 .rpc_release = nfs4_free_reclaim_complete_data,
5590};
5591
5592/*
5593 * Issue a global reclaim complete.
5594 */
5595static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5596{
5597 struct nfs4_reclaim_complete_data *calldata;
5598 struct rpc_task *task;
5599 struct rpc_message msg = {
5600 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5601 };
5602 struct rpc_task_setup task_setup_data = {
5603 .rpc_client = clp->cl_rpcclient,
5604 .rpc_message = &msg,
5605 .callback_ops = &nfs4_reclaim_complete_call_ops,
5606 .flags = RPC_TASK_ASYNC,
5607 };
5608 int status = -ENOMEM;
5609
5610 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005611 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005612 if (calldata == NULL)
5613 goto out;
5614 calldata->clp = clp;
5615 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005616
5617 msg.rpc_argp = &calldata->arg;
5618 msg.rpc_resp = &calldata->res;
5619 task_setup_data.callback_data = calldata;
5620 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005621 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005622 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005623 goto out;
5624 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005625 status = nfs4_wait_for_completion_rpc_task(task);
5626 if (status == 0)
5627 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005628 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005629 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005630out:
5631 dprintk("<-- %s status=%d\n", __func__, status);
5632 return status;
5633}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005634
5635static void
5636nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5637{
5638 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005639 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005640
5641 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005642 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5643 * right now covering the LAYOUTGET we are about to send.
5644 * However, that is not so catastrophic, and there seems
5645 * to be no way to prevent it completely.
5646 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005647 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5648 &lgp->res.seq_res, 0, task))
5649 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005650 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5651 NFS_I(lgp->args.inode)->layout,
5652 lgp->args.ctx->state)) {
5653 rpc_exit(task, NFS4_OK);
5654 return;
5655 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005656 rpc_call_start(task);
5657}
5658
5659static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5660{
5661 struct nfs4_layoutget *lgp = calldata;
5662 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5663
5664 dprintk("--> %s\n", __func__);
5665
5666 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5667 return;
5668
5669 switch (task->tk_status) {
5670 case 0:
5671 break;
5672 case -NFS4ERR_LAYOUTTRYLATER:
5673 case -NFS4ERR_RECALLCONFLICT:
5674 task->tk_status = -NFS4ERR_DELAY;
5675 /* Fall through */
5676 default:
5677 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5678 rpc_restart_call_prepare(task);
5679 return;
5680 }
5681 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005682 dprintk("<-- %s\n", __func__);
5683}
5684
5685static void nfs4_layoutget_release(void *calldata)
5686{
5687 struct nfs4_layoutget *lgp = calldata;
5688
5689 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005690 put_nfs_open_context(lgp->args.ctx);
5691 kfree(calldata);
5692 dprintk("<-- %s\n", __func__);
5693}
5694
5695static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5696 .rpc_call_prepare = nfs4_layoutget_prepare,
5697 .rpc_call_done = nfs4_layoutget_done,
5698 .rpc_release = nfs4_layoutget_release,
5699};
5700
5701int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5702{
5703 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5704 struct rpc_task *task;
5705 struct rpc_message msg = {
5706 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5707 .rpc_argp = &lgp->args,
5708 .rpc_resp = &lgp->res,
5709 };
5710 struct rpc_task_setup task_setup_data = {
5711 .rpc_client = server->client,
5712 .rpc_message = &msg,
5713 .callback_ops = &nfs4_layoutget_call_ops,
5714 .callback_data = lgp,
5715 .flags = RPC_TASK_ASYNC,
5716 };
5717 int status = 0;
5718
5719 dprintk("--> %s\n", __func__);
5720
Weston Andros Adamson35124a02011-03-24 16:48:21 -04005721 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005722 lgp->res.seq_res.sr_slot = NULL;
5723 task = rpc_run_task(&task_setup_data);
5724 if (IS_ERR(task))
5725 return PTR_ERR(task);
5726 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00005727 if (status == 0)
5728 status = task->tk_status;
5729 if (status == 0)
5730 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005731 rpc_put_task(task);
5732 dprintk("<-- %s status=%d\n", __func__, status);
5733 return status;
5734}
5735
Benny Halevycbe82602011-05-22 19:52:37 +03005736static void
5737nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
5738{
5739 struct nfs4_layoutreturn *lrp = calldata;
5740
5741 dprintk("--> %s\n", __func__);
5742 if (nfs41_setup_sequence(lrp->clp->cl_session, &lrp->args.seq_args,
5743 &lrp->res.seq_res, 0, task))
5744 return;
5745 rpc_call_start(task);
5746}
5747
5748static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
5749{
5750 struct nfs4_layoutreturn *lrp = calldata;
5751 struct nfs_server *server;
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005752 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03005753
5754 dprintk("--> %s\n", __func__);
5755
5756 if (!nfs4_sequence_done(task, &lrp->res.seq_res))
5757 return;
5758
5759 server = NFS_SERVER(lrp->args.inode);
5760 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005761 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03005762 return;
5763 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005764 spin_lock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005765 if (task->tk_status == 0) {
Benny Halevycbe82602011-05-22 19:52:37 +03005766 if (lrp->res.lrs_present) {
Benny Halevycbe82602011-05-22 19:52:37 +03005767 pnfs_set_layout_stateid(lo, &lrp->res.stateid, true);
Benny Halevycbe82602011-05-22 19:52:37 +03005768 } else
5769 BUG_ON(!list_empty(&lo->plh_segs));
5770 }
Fred Isamana2e1d4f2011-06-13 18:54:53 -04005771 lo->plh_block_lgets--;
5772 spin_unlock(&lo->plh_inode->i_lock);
Benny Halevycbe82602011-05-22 19:52:37 +03005773 dprintk("<-- %s\n", __func__);
5774}
5775
5776static void nfs4_layoutreturn_release(void *calldata)
5777{
5778 struct nfs4_layoutreturn *lrp = calldata;
5779
5780 dprintk("--> %s\n", __func__);
Trond Myklebusta56aaa02011-06-15 11:59:10 -04005781 put_layout_hdr(lrp->args.layout);
Benny Halevycbe82602011-05-22 19:52:37 +03005782 kfree(calldata);
5783 dprintk("<-- %s\n", __func__);
5784}
5785
5786static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
5787 .rpc_call_prepare = nfs4_layoutreturn_prepare,
5788 .rpc_call_done = nfs4_layoutreturn_done,
5789 .rpc_release = nfs4_layoutreturn_release,
5790};
5791
5792int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp)
5793{
5794 struct rpc_task *task;
5795 struct rpc_message msg = {
5796 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
5797 .rpc_argp = &lrp->args,
5798 .rpc_resp = &lrp->res,
5799 };
5800 struct rpc_task_setup task_setup_data = {
5801 .rpc_client = lrp->clp->cl_rpcclient,
5802 .rpc_message = &msg,
5803 .callback_ops = &nfs4_layoutreturn_call_ops,
5804 .callback_data = lrp,
5805 };
5806 int status;
5807
5808 dprintk("--> %s\n", __func__);
5809 task = rpc_run_task(&task_setup_data);
5810 if (IS_ERR(task))
5811 return PTR_ERR(task);
5812 status = task->tk_status;
5813 dprintk("<-- %s status=%d\n", __func__, status);
5814 rpc_put_task(task);
5815 return status;
5816}
5817
Andy Adamson7f11d8d2011-07-30 20:52:35 -04005818/*
5819 * Retrieve the list of Data Server devices from the MDS.
5820 */
5821static int _nfs4_getdevicelist(struct nfs_server *server,
5822 const struct nfs_fh *fh,
5823 struct pnfs_devicelist *devlist)
5824{
5825 struct nfs4_getdevicelist_args args = {
5826 .fh = fh,
5827 .layoutclass = server->pnfs_curr_ld->id,
5828 };
5829 struct nfs4_getdevicelist_res res = {
5830 .devlist = devlist,
5831 };
5832 struct rpc_message msg = {
5833 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICELIST],
5834 .rpc_argp = &args,
5835 .rpc_resp = &res,
5836 };
5837 int status;
5838
5839 dprintk("--> %s\n", __func__);
5840 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args,
5841 &res.seq_res, 0);
5842 dprintk("<-- %s status=%d\n", __func__, status);
5843 return status;
5844}
5845
5846int nfs4_proc_getdevicelist(struct nfs_server *server,
5847 const struct nfs_fh *fh,
5848 struct pnfs_devicelist *devlist)
5849{
5850 struct nfs4_exception exception = { };
5851 int err;
5852
5853 do {
5854 err = nfs4_handle_exception(server,
5855 _nfs4_getdevicelist(server, fh, devlist),
5856 &exception);
5857 } while (exception.retry);
5858
5859 dprintk("%s: err=%d, num_devs=%u\n", __func__,
5860 err, devlist->num_devs);
5861
5862 return err;
5863}
5864EXPORT_SYMBOL_GPL(nfs4_proc_getdevicelist);
5865
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005866static int
5867_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5868{
5869 struct nfs4_getdeviceinfo_args args = {
5870 .pdev = pdev,
5871 };
5872 struct nfs4_getdeviceinfo_res res = {
5873 .pdev = pdev,
5874 };
5875 struct rpc_message msg = {
5876 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5877 .rpc_argp = &args,
5878 .rpc_resp = &res,
5879 };
5880 int status;
5881
5882 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005883 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005884 dprintk("<-- %s status=%d\n", __func__, status);
5885
5886 return status;
5887}
5888
5889int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5890{
5891 struct nfs4_exception exception = { };
5892 int err;
5893
5894 do {
5895 err = nfs4_handle_exception(server,
5896 _nfs4_proc_getdeviceinfo(server, pdev),
5897 &exception);
5898 } while (exception.retry);
5899 return err;
5900}
5901EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5902
Andy Adamson863a3c62011-03-23 13:27:54 +00005903static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
5904{
5905 struct nfs4_layoutcommit_data *data = calldata;
5906 struct nfs_server *server = NFS_SERVER(data->args.inode);
5907
5908 if (nfs4_setup_sequence(server, &data->args.seq_args,
5909 &data->res.seq_res, 1, task))
5910 return;
5911 rpc_call_start(task);
5912}
5913
5914static void
5915nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
5916{
5917 struct nfs4_layoutcommit_data *data = calldata;
5918 struct nfs_server *server = NFS_SERVER(data->args.inode);
5919
5920 if (!nfs4_sequence_done(task, &data->res.seq_res))
5921 return;
5922
5923 switch (task->tk_status) { /* Just ignore these failures */
5924 case NFS4ERR_DELEG_REVOKED: /* layout was recalled */
5925 case NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
5926 case NFS4ERR_BADLAYOUT: /* no layout */
5927 case NFS4ERR_GRACE: /* loca_recalim always false */
5928 task->tk_status = 0;
5929 }
5930
5931 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005932 rpc_restart_call_prepare(task);
Andy Adamson863a3c62011-03-23 13:27:54 +00005933 return;
5934 }
5935
5936 if (task->tk_status == 0)
5937 nfs_post_op_update_inode_force_wcc(data->args.inode,
5938 data->res.fattr);
5939}
5940
5941static void nfs4_layoutcommit_release(void *calldata)
5942{
5943 struct nfs4_layoutcommit_data *data = calldata;
Peng Taoa9bae562011-07-30 20:52:33 -04005944 struct pnfs_layout_segment *lseg, *tmp;
Peng Tao92407e72011-10-23 20:21:17 -07005945 unsigned long *bitlock = &NFS_I(data->args.inode)->flags;
Andy Adamson863a3c62011-03-23 13:27:54 +00005946
Andy Adamsondb29c082011-07-30 20:52:38 -04005947 pnfs_cleanup_layoutcommit(data);
Andy Adamson863a3c62011-03-23 13:27:54 +00005948 /* Matched by references in pnfs_set_layoutcommit */
Peng Taoa9bae562011-07-30 20:52:33 -04005949 list_for_each_entry_safe(lseg, tmp, &data->lseg_list, pls_lc_list) {
5950 list_del_init(&lseg->pls_lc_list);
5951 if (test_and_clear_bit(NFS_LSEG_LAYOUTCOMMIT,
5952 &lseg->pls_flags))
5953 put_lseg(lseg);
5954 }
Peng Tao92407e72011-10-23 20:21:17 -07005955
5956 clear_bit_unlock(NFS_INO_LAYOUTCOMMITTING, bitlock);
5957 smp_mb__after_clear_bit();
5958 wake_up_bit(bitlock, NFS_INO_LAYOUTCOMMITTING);
5959
Andy Adamson863a3c62011-03-23 13:27:54 +00005960 put_rpccred(data->cred);
5961 kfree(data);
5962}
5963
5964static const struct rpc_call_ops nfs4_layoutcommit_ops = {
5965 .rpc_call_prepare = nfs4_layoutcommit_prepare,
5966 .rpc_call_done = nfs4_layoutcommit_done,
5967 .rpc_release = nfs4_layoutcommit_release,
5968};
5969
5970int
Andy Adamsonef311532011-03-12 02:58:10 -05005971nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00005972{
5973 struct rpc_message msg = {
5974 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
5975 .rpc_argp = &data->args,
5976 .rpc_resp = &data->res,
5977 .rpc_cred = data->cred,
5978 };
5979 struct rpc_task_setup task_setup_data = {
5980 .task = &data->task,
5981 .rpc_client = NFS_CLIENT(data->args.inode),
5982 .rpc_message = &msg,
5983 .callback_ops = &nfs4_layoutcommit_ops,
5984 .callback_data = data,
5985 .flags = RPC_TASK_ASYNC,
5986 };
5987 struct rpc_task *task;
5988 int status = 0;
5989
5990 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
5991 "lbw: %llu inode %lu\n",
5992 data->task.tk_pid, sync,
5993 data->args.lastbytewritten,
5994 data->args.inode->i_ino);
5995
5996 task = rpc_run_task(&task_setup_data);
5997 if (IS_ERR(task))
5998 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05005999 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00006000 goto out;
6001 status = nfs4_wait_for_completion_rpc_task(task);
6002 if (status != 0)
6003 goto out;
6004 status = task->tk_status;
6005out:
6006 dprintk("%s: status %d\n", __func__, status);
6007 rpc_put_task(task);
6008 return status;
6009}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006010
6011static int
6012_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6013 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6014{
6015 struct nfs41_secinfo_no_name_args args = {
6016 .style = SECINFO_STYLE_CURRENT_FH,
6017 };
6018 struct nfs4_secinfo_res res = {
6019 .flavors = flavors,
6020 };
6021 struct rpc_message msg = {
6022 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
6023 .rpc_argp = &args,
6024 .rpc_resp = &res,
6025 };
6026 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
6027}
6028
6029static int
6030nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
6031 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
6032{
6033 struct nfs4_exception exception = { };
6034 int err;
6035 do {
6036 err = _nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6037 switch (err) {
6038 case 0:
6039 case -NFS4ERR_WRONGSEC:
6040 case -NFS4ERR_NOTSUPP:
6041 break;
6042 default:
6043 err = nfs4_handle_exception(server, err, &exception);
6044 }
6045 } while (exception.retry);
6046 return err;
6047}
6048
6049static int
6050nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
6051 struct nfs_fsinfo *info)
6052{
6053 int err;
6054 struct page *page;
6055 rpc_authflavor_t flavor;
6056 struct nfs4_secinfo_flavors *flavors;
6057
6058 page = alloc_page(GFP_KERNEL);
6059 if (!page) {
6060 err = -ENOMEM;
6061 goto out;
6062 }
6063
6064 flavors = page_address(page);
6065 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
6066
6067 /*
6068 * Fall back on "guess and check" method if
6069 * the server doesn't support SECINFO_NO_NAME
6070 */
6071 if (err == -NFS4ERR_WRONGSEC || err == -NFS4ERR_NOTSUPP) {
6072 err = nfs4_find_root_sec(server, fhandle, info);
6073 goto out_freepage;
6074 }
6075 if (err)
6076 goto out_freepage;
6077
6078 flavor = nfs_find_best_sec(flavors);
6079 if (err == 0)
6080 err = nfs4_lookup_root_sec(server, fhandle, info, flavor);
6081
6082out_freepage:
6083 put_page(page);
6084 if (err == -EACCES)
6085 return -EPERM;
6086out:
6087 return err;
6088}
Bryan Schumaker7d974792011-06-02 14:59:08 -04006089static int _nfs41_test_stateid(struct nfs_server *server, struct nfs4_state *state)
6090{
6091 int status;
6092 struct nfs41_test_stateid_args args = {
6093 .stateid = &state->stateid,
6094 };
6095 struct nfs41_test_stateid_res res;
6096 struct rpc_message msg = {
6097 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
6098 .rpc_argp = &args,
6099 .rpc_resp = &res,
6100 };
6101 args.seq_args.sa_session = res.seq_res.sr_session = NULL;
6102 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 0, 1);
6103 return status;
6104}
6105
6106static int nfs41_test_stateid(struct nfs_server *server, struct nfs4_state *state)
6107{
6108 struct nfs4_exception exception = { };
6109 int err;
6110 do {
6111 err = nfs4_handle_exception(server,
6112 _nfs41_test_stateid(server, state),
6113 &exception);
6114 } while (exception.retry);
6115 return err;
6116}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04006117
6118static int _nfs4_free_stateid(struct nfs_server *server, struct nfs4_state *state)
6119{
6120 int status;
6121 struct nfs41_free_stateid_args args = {
6122 .stateid = &state->stateid,
6123 };
6124 struct nfs41_free_stateid_res res;
6125 struct rpc_message msg = {
6126 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
6127 .rpc_argp = &args,
6128 .rpc_resp = &res,
6129 };
6130
6131 args.seq_args.sa_session = res.seq_res.sr_session = NULL;
6132 status = nfs4_call_sync_sequence(server->client, server, &msg, &args.seq_args, &res.seq_res, 0, 1);
6133 return status;
6134}
6135
6136static int nfs41_free_stateid(struct nfs_server *server, struct nfs4_state *state)
6137{
6138 struct nfs4_exception exception = { };
6139 int err;
6140 do {
6141 err = nfs4_handle_exception(server,
6142 _nfs4_free_stateid(server, state),
6143 &exception);
6144 } while (exception.retry);
6145 return err;
6146}
Andy Adamson557134a2009-04-01 09:21:53 -04006147#endif /* CONFIG_NFS_V4_1 */
6148
Andy Adamson591d71c2009-04-01 09:22:47 -04006149struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006150 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006151 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006152 .recover_open = nfs4_open_reclaim,
6153 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04006154 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006155 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156};
6157
Andy Adamson591d71c2009-04-01 09:22:47 -04006158#if defined(CONFIG_NFS_V4_1)
6159struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
6160 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
6161 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
6162 .recover_open = nfs4_open_reclaim,
6163 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05006164 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006165 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05006166 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04006167};
6168#endif /* CONFIG_NFS_V4_1 */
6169
6170struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05006171 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05006172 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006173 .recover_open = nfs4_open_expired,
6174 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04006175 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04006176 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177};
6178
Andy Adamson591d71c2009-04-01 09:22:47 -04006179#if defined(CONFIG_NFS_V4_1)
6180struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
6181 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
6182 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006183 .recover_open = nfs41_open_expired,
6184 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05006185 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04006186 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04006187};
6188#endif /* CONFIG_NFS_V4_1 */
6189
Benny Halevy29fba382009-04-01 09:22:44 -04006190struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
6191 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04006192 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006193 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04006194};
6195
6196#if defined(CONFIG_NFS_V4_1)
6197struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
6198 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04006199 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04006200 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04006201};
6202#endif
6203
Trond Myklebust97dc1352010-06-16 09:52:26 -04006204static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
6205 .minor_version = 0,
6206 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04006207 .validate_stateid = nfs4_validate_delegation_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006208 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006209 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
6210 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
6211 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006212};
6213
6214#if defined(CONFIG_NFS_V4_1)
6215static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
6216 .minor_version = 1,
6217 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04006218 .validate_stateid = nfs41_validate_delegation_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04006219 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04006220 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
6221 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
6222 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04006223};
6224#endif
6225
Trond Myklebust97dc1352010-06-16 09:52:26 -04006226const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
6227 [0] = &nfs_v4_0_minor_ops,
6228#if defined(CONFIG_NFS_V4_1)
6229 [1] = &nfs_v4_1_minor_ops,
6230#endif
6231};
6232
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08006233static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006234 .permission = nfs_permission,
6235 .getattr = nfs_getattr,
6236 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006237 .getxattr = generic_getxattr,
6238 .setxattr = generic_setxattr,
6239 .listxattr = generic_listxattr,
6240 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006241};
6242
David Howells509de812006-08-22 20:06:11 -04006243const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006244 .version = 4, /* protocol version */
6245 .dentry_ops = &nfs4_dentry_operations,
6246 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006247 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04006248 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249 .getroot = nfs4_proc_get_root,
6250 .getattr = nfs4_proc_getattr,
6251 .setattr = nfs4_proc_setattr,
6252 .lookup = nfs4_proc_lookup,
6253 .access = nfs4_proc_access,
6254 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006255 .create = nfs4_proc_create,
6256 .remove = nfs4_proc_remove,
6257 .unlink_setup = nfs4_proc_unlink_setup,
6258 .unlink_done = nfs4_proc_unlink_done,
6259 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04006260 .rename_setup = nfs4_proc_rename_setup,
6261 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006262 .link = nfs4_proc_link,
6263 .symlink = nfs4_proc_symlink,
6264 .mkdir = nfs4_proc_mkdir,
6265 .rmdir = nfs4_proc_remove,
6266 .readdir = nfs4_proc_readdir,
6267 .mknod = nfs4_proc_mknod,
6268 .statfs = nfs4_proc_statfs,
6269 .fsinfo = nfs4_proc_fsinfo,
6270 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04006271 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006272 .decode_dirent = nfs4_decode_dirent,
6273 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05006274 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006276 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006277 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05006278 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006279 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00006280 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04006281 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04006282 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00006283 .init_client = nfs4_init_client,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00006284 .secinfo = nfs4_proc_secinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006285};
6286
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00006287static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
6288 .prefix = XATTR_NAME_NFSV4_ACL,
6289 .list = nfs4_xattr_list_nfs4_acl,
6290 .get = nfs4_xattr_get_nfs4_acl,
6291 .set = nfs4_xattr_set_nfs4_acl,
6292};
6293
6294const struct xattr_handler *nfs4_xattr_handlers[] = {
6295 &nfs4_xattr_nfs4_acl_handler,
6296 NULL
6297};
6298
Linus Torvalds1da177e2005-04-16 15:20:36 -07006299/*
6300 * Local variables:
6301 * c-basic-offset: 8
6302 * End:
6303 */