blob: 7e27ebf5be5d10ae28f600b7ce313d17a3886b6a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/sunrpc/clnt.h>
Bryan Schumaker8f70e952011-03-24 17:12:31 +000044#include <linux/sunrpc/gss_api.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/nfs.h>
46#include <linux/nfs4.h>
47#include <linux/nfs_fs.h>
48#include <linux/nfs_page.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070050#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040051#include <linux/module.h>
Andy Adamson5a0ffe52009-04-01 09:23:18 -040052#include <linux/sunrpc/bc_xprt.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000053#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050054#include <linux/utsname.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Trond Myklebust4ce79712005-06-22 17:16:21 +000056#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070057#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050058#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050059#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040060#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040061#include "pnfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63#define NFSDBG_FACILITY NFSDBG_PROC
64
Trond Myklebust2066fe82006-09-15 08:30:46 -040065#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#define NFS4_POLL_RETRY_MAX (15*HZ)
67
Trond Myklebusta78cb572009-08-09 15:06:19 -040068#define NFS4_MAX_LOOP_ON_RECOVER (10)
69
Trond Myklebustcdd4e682006-01-03 09:55:12 +010070struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010071static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080072static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Trond Myklebust9e33bed2008-12-23 15:21:46 -050074static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *, struct nfs4_state *);
Bryan Schumaker7c513052011-03-24 17:12:24 +000075static int _nfs4_proc_lookup(struct rpc_clnt *client, struct inode *dir,
76 const struct qstr *name, struct nfs_fh *fhandle,
77 struct nfs_fattr *fattr);
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -050084static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Trond Myklebust52567b02009-10-23 14:46:42 -040086 if (err >= -1000)
87 return err;
88 switch (err) {
89 case -NFS4ERR_RESOURCE:
90 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +000091 case -NFS4ERR_WRONGSEC:
92 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -080093 case -NFS4ERR_BADOWNER:
94 case -NFS4ERR_BADNAME:
95 return -EINVAL;
Trond Myklebust52567b02009-10-23 14:46:42 -040096 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -070098 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -040099 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400101 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
104/*
105 * This is our standard bitmap for GETATTR requests.
106 */
107const u32 nfs4_fattr_bitmap[2] = {
108 FATTR4_WORD0_TYPE
109 | FATTR4_WORD0_CHANGE
110 | FATTR4_WORD0_SIZE
111 | FATTR4_WORD0_FSID
112 | FATTR4_WORD0_FILEID,
113 FATTR4_WORD1_MODE
114 | FATTR4_WORD1_NUMLINKS
115 | FATTR4_WORD1_OWNER
116 | FATTR4_WORD1_OWNER_GROUP
117 | FATTR4_WORD1_RAWDEV
118 | FATTR4_WORD1_SPACE_USED
119 | FATTR4_WORD1_TIME_ACCESS
120 | FATTR4_WORD1_TIME_METADATA
121 | FATTR4_WORD1_TIME_MODIFY
122};
123
124const u32 nfs4_statfs_bitmap[2] = {
125 FATTR4_WORD0_FILES_AVAIL
126 | FATTR4_WORD0_FILES_FREE
127 | FATTR4_WORD0_FILES_TOTAL,
128 FATTR4_WORD1_SPACE_AVAIL
129 | FATTR4_WORD1_SPACE_FREE
130 | FATTR4_WORD1_SPACE_TOTAL
131};
132
Trond Myklebust4ce79712005-06-22 17:16:21 +0000133const u32 nfs4_pathconf_bitmap[2] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 FATTR4_WORD0_MAXLINK
135 | FATTR4_WORD0_MAXNAME,
136 0
137};
138
139const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE
140 | FATTR4_WORD0_MAXREAD
141 | FATTR4_WORD0_MAXWRITE
142 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700143 FATTR4_WORD1_TIME_DELTA
Andy Adamson504913f2010-10-20 00:17:57 -0400144 | FATTR4_WORD1_FS_LAYOUT_TYPES
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145};
146
Manoj Naik830b8e32006-06-09 09:34:25 -0400147const u32 nfs4_fs_locations_bitmap[2] = {
148 FATTR4_WORD0_TYPE
149 | FATTR4_WORD0_CHANGE
150 | FATTR4_WORD0_SIZE
151 | FATTR4_WORD0_FSID
152 | FATTR4_WORD0_FILEID
153 | FATTR4_WORD0_FS_LOCATIONS,
154 FATTR4_WORD1_MODE
155 | FATTR4_WORD1_NUMLINKS
156 | FATTR4_WORD1_OWNER
157 | FATTR4_WORD1_OWNER_GROUP
158 | FATTR4_WORD1_RAWDEV
159 | FATTR4_WORD1_SPACE_USED
160 | FATTR4_WORD1_TIME_ACCESS
161 | FATTR4_WORD1_TIME_METADATA
162 | FATTR4_WORD1_TIME_MODIFY
163 | FATTR4_WORD1_MOUNTED_ON_FILEID
164};
165
Al Virobc4785c2006-10-19 23:28:51 -0700166static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 struct nfs4_readdir_arg *readdir)
168{
Al Viro0dbb4c62006-10-19 23:28:49 -0700169 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 BUG_ON(readdir->count < 80);
172 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000173 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
175 return;
176 }
177
178 readdir->cookie = 0;
179 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
180 if (cookie == 2)
181 return;
182
183 /*
184 * NFSv4 servers do not return entries for '.' and '..'
185 * Therefore, we fake these entries here. We let '.'
186 * have cookie 0 and '..' have cookie 1. Note that
187 * when talking to the server, we always send cookie 0
188 * instead of 1 or 2.
189 */
Al Viro0dbb4c62006-10-19 23:28:49 -0700190 start = p = kmap_atomic(*readdir->pages, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 if (cookie == 0) {
193 *p++ = xdr_one; /* next */
194 *p++ = xdr_zero; /* cookie, first word */
195 *p++ = xdr_one; /* cookie, second word */
196 *p++ = xdr_one; /* entry len */
197 memcpy(p, ".\0\0\0", 4); /* entry */
198 p++;
199 *p++ = xdr_one; /* bitmap length */
200 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
201 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400202 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 }
204
205 *p++ = xdr_one; /* next */
206 *p++ = xdr_zero; /* cookie, first word */
207 *p++ = xdr_two; /* cookie, second word */
208 *p++ = xdr_two; /* entry len */
209 memcpy(p, "..\0\0", 4); /* entry */
210 p++;
211 *p++ = xdr_one; /* bitmap length */
212 *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */
213 *p++ = htonl(8); /* attribute buffer length */
Peter Staubach4e769b92007-08-03 15:07:10 -0400214 p = xdr_encode_hyper(p, NFS_FILEID(dentry->d_parent->d_inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 readdir->pgbase = (char *)p - (char *)start;
217 readdir->count -= readdir->pgbase;
218 kunmap_atomic(start, KM_USER0);
219}
220
Trond Myklebust65de8722008-12-23 15:21:44 -0500221static int nfs4_wait_clnt_recover(struct nfs_client *clp)
222{
223 int res;
224
225 might_sleep();
226
Trond Myklebuste005e802008-12-23 15:21:48 -0500227 res = wait_on_bit(&clp->cl_state, NFS4CLNT_MANAGER_RUNNING,
Trond Myklebust72cb77f2009-03-11 14:10:30 -0400228 nfs_wait_bit_killable, TASK_KILLABLE);
Trond Myklebust65de8722008-12-23 15:21:44 -0500229 return res;
230}
231
232static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
233{
234 int res = 0;
235
236 might_sleep();
237
238 if (*timeout <= 0)
239 *timeout = NFS4_POLL_RETRY_MIN;
240 if (*timeout > NFS4_POLL_RETRY_MAX)
241 *timeout = NFS4_POLL_RETRY_MAX;
242 schedule_timeout_killable(*timeout);
243 if (fatal_signal_pending(current))
244 res = -ERESTARTSYS;
245 *timeout <<= 1;
246 return res;
247}
248
249/* This is the error handling routine for processes that are allowed
250 * to sleep.
251 */
Trond Myklebustb064eca22011-02-22 15:44:32 -0800252static int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500253{
254 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500255 struct nfs4_state *state = exception->state;
Trond Myklebust65de8722008-12-23 15:21:44 -0500256 int ret = errorcode;
257
258 exception->retry = 0;
259 switch(errorcode) {
260 case 0:
261 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500262 case -NFS4ERR_ADMIN_REVOKED:
263 case -NFS4ERR_BAD_STATEID:
264 case -NFS4ERR_OPENMODE:
265 if (state == NULL)
266 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500267 nfs4_schedule_stateid_recovery(server, state);
268 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500269 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500270 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust65de8722008-12-23 15:21:44 -0500271 case -NFS4ERR_EXPIRED:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500272 nfs4_schedule_lease_recovery(clp);
273 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500274#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400275 case -NFS4ERR_BADSESSION:
276 case -NFS4ERR_BADSLOT:
277 case -NFS4ERR_BAD_HIGH_SLOT:
278 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
279 case -NFS4ERR_DEADSESSION:
280 case -NFS4ERR_SEQ_FALSE_RETRY:
281 case -NFS4ERR_SEQ_MISORDERED:
282 dprintk("%s ERROR: %d Reset session\n", __func__,
283 errorcode);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500284 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -0400285 exception->retry = 1;
Andy Adamsonb9179232009-12-04 15:55:32 -0500286 break;
Trond Myklebust03391692010-01-26 15:42:38 -0500287#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500288 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500289 if (exception->timeout > HZ) {
290 /* We have retried a decent amount, time to
291 * fail
292 */
293 ret = -EBUSY;
294 break;
295 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500296 case -NFS4ERR_GRACE:
297 case -NFS4ERR_DELAY:
Jeff Layton2c643482010-01-07 09:42:03 -0500298 case -EKEYEXPIRED:
Trond Myklebust65de8722008-12-23 15:21:44 -0500299 ret = nfs4_delay(server->client, &exception->timeout);
300 if (ret != 0)
301 break;
302 case -NFS4ERR_OLD_STATEID:
303 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800304 break;
305 case -NFS4ERR_BADOWNER:
306 /* The following works around a Linux server bug! */
307 case -NFS4ERR_BADNAME:
308 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
309 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
310 exception->retry = 1;
311 printk(KERN_WARNING "NFS: v4 server %s "
312 "does not accept raw "
313 "uid/gids. "
314 "Reenabling the idmapper.\n",
315 server->nfs_client->cl_hostname);
316 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500317 }
318 /* We failed to handle the error */
319 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500320wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500321 ret = nfs4_wait_clnt_recover(clp);
322 if (ret == 0)
323 exception->retry = 1;
324 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500325}
326
327
Trond Myklebust452e9352010-07-31 14:29:06 -0400328static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 spin_lock(&clp->cl_lock);
331 if (time_before(clp->cl_last_renewal,timestamp))
332 clp->cl_last_renewal = timestamp;
333 spin_unlock(&clp->cl_lock);
334}
335
Trond Myklebust452e9352010-07-31 14:29:06 -0400336static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
337{
338 do_renew_lease(server->nfs_client, timestamp);
339}
340
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400341#if defined(CONFIG_NFS_V4_1)
342
Benny Halevy510b8172009-04-01 09:22:14 -0400343/*
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400344 * nfs4_free_slot - free a slot and efficiently update slot table.
345 *
346 * freeing a slot is trivially done by clearing its respective bit
347 * in the bitmap.
348 * If the freed slotid equals highest_used_slotid we want to update it
349 * so that the server would be able to size down the slot table if needed,
350 * otherwise we know that the highest_used_slotid is still in use.
351 * When updating highest_used_slotid there may be "holes" in the bitmap
352 * so we need to scan down from highest_used_slotid to 0 looking for the now
353 * highest slotid in use.
354 * If none found, highest_used_slotid is set to -1.
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500355 *
356 * Must be called while holding tbl->slot_tbl_lock
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400357 */
358static void
Benny Halevydfb4f3092010-09-24 09:17:01 -0400359nfs4_free_slot(struct nfs4_slot_table *tbl, struct nfs4_slot *free_slot)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400360{
Benny Halevydfb4f3092010-09-24 09:17:01 -0400361 int free_slotid = free_slot - tbl->slots;
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400362 int slotid = free_slotid;
363
Benny Halevydfb4f3092010-09-24 09:17:01 -0400364 BUG_ON(slotid < 0 || slotid >= NFS4_MAX_SLOT_TABLE);
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400365 /* clear used bit in bitmap */
366 __clear_bit(slotid, tbl->used_slots);
367
368 /* update highest_used_slotid when it is freed */
369 if (slotid == tbl->highest_used_slotid) {
370 slotid = find_last_bit(tbl->used_slots, tbl->max_slots);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500371 if (slotid < tbl->max_slots)
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400372 tbl->highest_used_slotid = slotid;
373 else
374 tbl->highest_used_slotid = -1;
375 }
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400376 dprintk("%s: free_slotid %u highest_used_slotid %d\n", __func__,
377 free_slotid, tbl->highest_used_slotid);
378}
379
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800380/*
Andy Adamson42acd022011-01-06 02:04:34 +0000381 * Signal state manager thread if session fore channel is drained
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800382 */
Andy Adamson42acd022011-01-06 02:04:34 +0000383static void nfs4_check_drain_fc_complete(struct nfs4_session *ses)
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800384{
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800385 struct rpc_task *task;
386
Trond Myklebusta2118c32010-06-16 09:52:26 -0400387 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state)) {
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800388 task = rpc_wake_up_next(&ses->fc_slot_table.slot_tbl_waitq);
389 if (task)
390 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800391 return;
392 }
393
394 if (ses->fc_slot_table.highest_used_slotid != -1)
395 return;
396
Andy Adamson42acd022011-01-06 02:04:34 +0000397 dprintk("%s COMPLETE: Session Fore Channel Drained\n", __func__);
398 complete(&ses->fc_slot_table.complete);
399}
400
401/*
402 * Signal state manager thread if session back channel is drained
403 */
404void nfs4_check_drain_bc_complete(struct nfs4_session *ses)
405{
406 if (!test_bit(NFS4_SESSION_DRAINING, &ses->session_state) ||
407 ses->bc_slot_table.highest_used_slotid != -1)
408 return;
409 dprintk("%s COMPLETE: Session Back Channel Drained\n", __func__);
410 complete(&ses->bc_slot_table.complete);
Alexandros Batsakis3bfb0fc2009-12-09 01:50:11 -0800411}
412
Trond Myklebustd185a332010-06-16 09:52:25 -0400413static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
Andy Adamson13615872009-04-01 09:22:17 -0400414{
415 struct nfs4_slot_table *tbl;
416
Trond Myklebustd185a332010-06-16 09:52:25 -0400417 tbl = &res->sr_session->fc_slot_table;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400418 if (!res->sr_slot) {
Andy Adamson13615872009-04-01 09:22:17 -0400419 /* just wake up the next guy waiting since
420 * we may have not consumed a slot after all */
Andy Adamson691daf32009-12-04 15:55:39 -0500421 dprintk("%s: No slot\n", __func__);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500422 return;
Andy Adamson13615872009-04-01 09:22:17 -0400423 }
Andy Adamsonea028ac2009-12-04 15:55:38 -0500424
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500425 spin_lock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400426 nfs4_free_slot(tbl, res->sr_slot);
Andy Adamson42acd022011-01-06 02:04:34 +0000427 nfs4_check_drain_fc_complete(res->sr_session);
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500428 spin_unlock(&tbl->slot_tbl_lock);
Benny Halevydfb4f3092010-09-24 09:17:01 -0400429 res->sr_slot = NULL;
Andy Adamson13615872009-04-01 09:22:17 -0400430}
431
Trond Myklebust14516c32010-07-31 14:29:06 -0400432static int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400433{
434 unsigned long timestamp;
Trond Myklebust14516c32010-07-31 14:29:06 -0400435 struct nfs_client *clp;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400436
437 /*
438 * sr_status remains 1 if an RPC level error occurred. The server
439 * may or may not have processed the sequence operation..
440 * Proceed as if the server received and processed the sequence
441 * operation.
442 */
443 if (res->sr_status == 1)
444 res->sr_status = NFS_OK;
445
446 /* -ERESTARTSYS can result in skipping nfs41_sequence_setup */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400447 if (!res->sr_slot)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400448 goto out;
449
Andy Adamson691daf32009-12-04 15:55:39 -0500450 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400451 switch (res->sr_status) {
452 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400453 /* Update the slot's sequence and clientid lease timer */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400454 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400455 timestamp = res->sr_renewal_time;
Trond Myklebust14516c32010-07-31 14:29:06 -0400456 clp = res->sr_session->clp;
Trond Myklebust452e9352010-07-31 14:29:06 -0400457 do_renew_lease(clp, timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500458 /* Check sequence flags */
Trond Myklebustb4410c22011-03-09 16:00:55 -0500459 if (res->sr_status_flags != 0)
460 nfs4_schedule_lease_recovery(clp);
Trond Myklebust14516c32010-07-31 14:29:06 -0400461 break;
462 case -NFS4ERR_DELAY:
463 /* The server detected a resend of the RPC call and
464 * returned NFS4ERR_DELAY as per Section 2.10.6.2
465 * of RFC5661.
466 */
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200467 dprintk("%s: slot=%td seq=%d: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400468 __func__,
469 res->sr_slot - res->sr_session->fc_slot_table.slots,
470 res->sr_slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400471 goto out_retry;
472 default:
473 /* Just update the slot sequence no. */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400474 ++res->sr_slot->seq_nr;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400475 }
476out:
477 /* The session may be reset by one of the error handlers. */
478 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebustd185a332010-06-16 09:52:25 -0400479 nfs41_sequence_free_slot(res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400480 return 1;
481out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400482 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400483 goto out;
484 rpc_delay(task, NFS4_POLL_RETRY_MAX);
485 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400486}
487
Trond Myklebust14516c32010-07-31 14:29:06 -0400488static int nfs4_sequence_done(struct rpc_task *task,
489 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400490{
Trond Myklebust14516c32010-07-31 14:29:06 -0400491 if (res->sr_session == NULL)
492 return 1;
493 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400494}
495
Andy Adamsone2c4ab32009-04-01 09:22:16 -0400496/*
Benny Halevy510b8172009-04-01 09:22:14 -0400497 * nfs4_find_slot - efficiently look for a free slot
498 *
499 * nfs4_find_slot looks for an unset bit in the used_slots bitmap.
500 * If found, we mark the slot as used, update the highest_used_slotid,
501 * and respectively set up the sequence operation args.
502 * The slot number is returned if found, or NFS4_MAX_SLOT_TABLE otherwise.
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400503 *
504 * Note: must be called with under the slot_tbl_lock.
Benny Halevy510b8172009-04-01 09:22:14 -0400505 */
506static u8
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800507nfs4_find_slot(struct nfs4_slot_table *tbl)
Benny Halevy510b8172009-04-01 09:22:14 -0400508{
509 int slotid;
510 u8 ret_id = NFS4_MAX_SLOT_TABLE;
511 BUILD_BUG_ON((u8)NFS4_MAX_SLOT_TABLE != (int)NFS4_MAX_SLOT_TABLE);
512
Benny Halevy510b8172009-04-01 09:22:14 -0400513 dprintk("--> %s used_slots=%04lx highest_used=%d max_slots=%d\n",
514 __func__, tbl->used_slots[0], tbl->highest_used_slotid,
515 tbl->max_slots);
516 slotid = find_first_zero_bit(tbl->used_slots, tbl->max_slots);
517 if (slotid >= tbl->max_slots)
518 goto out;
519 __set_bit(slotid, tbl->used_slots);
520 if (slotid > tbl->highest_used_slotid)
521 tbl->highest_used_slotid = slotid;
522 ret_id = slotid;
523out:
524 dprintk("<-- %s used_slots=%04lx highest_used=%d slotid=%d \n",
525 __func__, tbl->used_slots[0], tbl->highest_used_slotid, ret_id);
Benny Halevy510b8172009-04-01 09:22:14 -0400526 return ret_id;
527}
528
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000529int nfs41_setup_sequence(struct nfs4_session *session,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400530 struct nfs4_sequence_args *args,
531 struct nfs4_sequence_res *res,
532 int cache_reply,
533 struct rpc_task *task)
534{
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400535 struct nfs4_slot *slot;
536 struct nfs4_slot_table *tbl;
537 u8 slotid;
538
539 dprintk("--> %s\n", __func__);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400540 /* slot already allocated? */
Benny Halevydfb4f3092010-09-24 09:17:01 -0400541 if (res->sr_slot != NULL)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400542 return 0;
543
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400544 tbl = &session->fc_slot_table;
545
546 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebusta2118c32010-06-16 09:52:26 -0400547 if (test_bit(NFS4_SESSION_DRAINING, &session->session_state) &&
Alexandros Batsakis5601a002009-12-14 21:27:58 -0800548 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
Andy Adamsonea028ac2009-12-04 15:55:38 -0500549 /*
550 * The state manager will wait until the slot table is empty.
551 * Schedule the reset thread
552 */
Andy Adamson05f0d232009-12-04 15:55:37 -0500553 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
Andy Adamsonb069d942009-04-01 09:22:43 -0400554 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustbcb56162009-12-05 19:32:19 -0500555 dprintk("%s Schedule Session Reset\n", __func__);
Andy Adamson05f0d232009-12-04 15:55:37 -0500556 return -EAGAIN;
Andy Adamsonb069d942009-04-01 09:22:43 -0400557 }
558
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800559 if (!rpc_queue_empty(&tbl->slot_tbl_waitq) &&
560 !rpc_task_has_priority(task, RPC_PRIORITY_PRIVILEGED)) {
561 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
562 spin_unlock(&tbl->slot_tbl_lock);
563 dprintk("%s enforce FIFO order\n", __func__);
564 return -EAGAIN;
565 }
566
Alexandros Batsakis40ead582009-12-14 21:27:54 -0800567 slotid = nfs4_find_slot(tbl);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400568 if (slotid == NFS4_MAX_SLOT_TABLE) {
569 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
570 spin_unlock(&tbl->slot_tbl_lock);
571 dprintk("<-- %s: no free slots\n", __func__);
572 return -EAGAIN;
573 }
574 spin_unlock(&tbl->slot_tbl_lock);
575
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -0800576 rpc_task_set_priority(task, RPC_PRIORITY_NORMAL);
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400577 slot = tbl->slots + slotid;
Benny Halevy99fe60d2009-04-01 09:22:29 -0400578 args->sa_session = session;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400579 args->sa_slotid = slotid;
580 args->sa_cache_this = cache_reply;
581
582 dprintk("<-- %s slotid=%d seqid=%d\n", __func__, slotid, slot->seq_nr);
583
Benny Halevy99fe60d2009-04-01 09:22:29 -0400584 res->sr_session = session;
Benny Halevydfb4f3092010-09-24 09:17:01 -0400585 res->sr_slot = slot;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400586 res->sr_renewal_time = jiffies;
Trond Myklebust2a6e26c2010-06-16 09:52:25 -0400587 res->sr_status_flags = 0;
Andy Adamsonfbcd4ab2009-04-01 09:22:15 -0400588 /*
589 * sr_status is only set in decode_sequence, and so will remain
590 * set to 1 if an rpc level failure occurs.
591 */
592 res->sr_status = 1;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400593 return 0;
594}
Andy Adamsondc70d7b2011-03-01 01:34:19 +0000595EXPORT_SYMBOL_GPL(nfs41_setup_sequence);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400596
Trond Myklebust035168a2010-06-16 09:52:26 -0400597int nfs4_setup_sequence(const struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400598 struct nfs4_sequence_args *args,
599 struct nfs4_sequence_res *res,
600 int cache_reply,
601 struct rpc_task *task)
602{
Trond Myklebust035168a2010-06-16 09:52:26 -0400603 struct nfs4_session *session = nfs4_get_session(server);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400604 int ret = 0;
605
Trond Myklebust035168a2010-06-16 09:52:26 -0400606 if (session == NULL) {
607 args->sa_session = NULL;
608 res->sr_session = NULL;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400609 goto out;
Trond Myklebust035168a2010-06-16 09:52:26 -0400610 }
611
Geert Uytterhoeven12364a42010-10-28 20:06:19 +0200612 dprintk("--> %s clp %p session %p sr_slot %td\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400613 __func__, session->clp, session, res->sr_slot ?
614 res->sr_slot - session->fc_slot_table.slots : -1);
Trond Myklebust035168a2010-06-16 09:52:26 -0400615
616 ret = nfs41_setup_sequence(session, args, res, cache_reply,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400617 task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400618out:
619 dprintk("<-- %s status=%d\n", __func__, ret);
620 return ret;
621}
622
623struct nfs41_call_sync_data {
Trond Myklebust035168a2010-06-16 09:52:26 -0400624 const struct nfs_server *seq_server;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400625 struct nfs4_sequence_args *seq_args;
626 struct nfs4_sequence_res *seq_res;
627 int cache_reply;
628};
629
630static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
631{
632 struct nfs41_call_sync_data *data = calldata;
633
Trond Myklebust035168a2010-06-16 09:52:26 -0400634 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
635
636 if (nfs4_setup_sequence(data->seq_server, data->seq_args,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400637 data->seq_res, data->cache_reply, task))
638 return;
639 rpc_call_start(task);
640}
641
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800642static void nfs41_call_priv_sync_prepare(struct rpc_task *task, void *calldata)
643{
644 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
645 nfs41_call_sync_prepare(task, calldata);
646}
647
Andy Adamson69ab40c2009-04-01 09:22:19 -0400648static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
649{
650 struct nfs41_call_sync_data *data = calldata;
651
Trond Myklebust14516c32010-07-31 14:29:06 -0400652 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400653}
654
Andy Adamsonce5039c2009-04-01 09:22:13 -0400655struct rpc_call_ops nfs41_call_sync_ops = {
656 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400657 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400658};
659
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800660struct rpc_call_ops nfs41_call_priv_sync_ops = {
661 .rpc_call_prepare = nfs41_call_priv_sync_prepare,
662 .rpc_call_done = nfs41_call_sync_done,
663};
664
Bryan Schumaker7c513052011-03-24 17:12:24 +0000665static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
666 struct nfs_server *server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400667 struct rpc_message *msg,
668 struct nfs4_sequence_args *args,
669 struct nfs4_sequence_res *res,
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800670 int cache_reply,
671 int privileged)
Andy Adamsonce5039c2009-04-01 09:22:13 -0400672{
673 int ret;
674 struct rpc_task *task;
675 struct nfs41_call_sync_data data = {
Trond Myklebust035168a2010-06-16 09:52:26 -0400676 .seq_server = server,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400677 .seq_args = args,
678 .seq_res = res,
679 .cache_reply = cache_reply,
680 };
681 struct rpc_task_setup task_setup = {
Bryan Schumaker7c513052011-03-24 17:12:24 +0000682 .rpc_client = clnt,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400683 .rpc_message = msg,
684 .callback_ops = &nfs41_call_sync_ops,
685 .callback_data = &data
686 };
687
Benny Halevydfb4f3092010-09-24 09:17:01 -0400688 res->sr_slot = NULL;
Alexandros Batsakisb2579572009-12-14 21:27:57 -0800689 if (privileged)
690 task_setup.callback_ops = &nfs41_call_priv_sync_ops;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400691 task = rpc_run_task(&task_setup);
692 if (IS_ERR(task))
693 ret = PTR_ERR(task);
694 else {
695 ret = task->tk_status;
696 rpc_put_task(task);
697 }
698 return ret;
699}
700
Bryan Schumaker7c513052011-03-24 17:12:24 +0000701int _nfs4_call_sync_session(struct rpc_clnt *clnt,
702 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400703 struct rpc_message *msg,
704 struct nfs4_sequence_args *args,
705 struct nfs4_sequence_res *res,
706 int cache_reply)
707{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000708 return nfs4_call_sync_sequence(clnt, server, msg, args, res, cache_reply, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400709}
710
Trond Myklebustdf896452010-06-16 09:52:26 -0400711#else
Trond Myklebust14516c32010-07-31 14:29:06 -0400712static int nfs4_sequence_done(struct rpc_task *task,
713 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400714{
Trond Myklebust14516c32010-07-31 14:29:06 -0400715 return 1;
Trond Myklebustdf896452010-06-16 09:52:26 -0400716}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400717#endif /* CONFIG_NFS_V4_1 */
718
Bryan Schumaker7c513052011-03-24 17:12:24 +0000719int _nfs4_call_sync(struct rpc_clnt *clnt,
720 struct nfs_server *server,
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400721 struct rpc_message *msg,
722 struct nfs4_sequence_args *args,
723 struct nfs4_sequence_res *res,
724 int cache_reply)
725{
Benny Halevyf3752972009-04-01 09:22:04 -0400726 args->sa_session = res->sr_session = NULL;
Bryan Schumaker7c513052011-03-24 17:12:24 +0000727 return rpc_call_sync(clnt, msg, 0);
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400728}
729
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000730static inline
Bryan Schumaker7c513052011-03-24 17:12:24 +0000731int nfs4_call_sync(struct rpc_clnt *clnt,
732 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000733 struct rpc_message *msg,
734 struct nfs4_sequence_args *args,
735 struct nfs4_sequence_res *res,
736 int cache_reply)
737{
Bryan Schumaker7c513052011-03-24 17:12:24 +0000738 return server->nfs_client->cl_mvops->call_sync(clnt, server, msg,
739 args, res, cache_reply);
Bryan Schumakere73b83f2011-03-24 17:12:23 +0000740}
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400741
Trond Myklebust38478b22006-05-25 01:40:57 -0400742static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
Trond Myklebust38478b22006-05-25 01:40:57 -0400744 struct nfs_inode *nfsi = NFS_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Trond Myklebust38478b22006-05-25 01:40:57 -0400746 spin_lock(&dir->i_lock);
Trond Myklebust40d24702007-10-08 09:24:22 -0400747 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA;
748 if (!cinfo->atomic || cinfo->before != nfsi->change_attr)
Trond Myklebustbfc69a42007-10-15 18:18:29 -0400749 nfs_force_lookup_revalidate(dir);
Trond Myklebust40d24702007-10-08 09:24:22 -0400750 nfsi->change_attr = cinfo->after;
Trond Myklebust38478b22006-05-25 01:40:57 -0400751 spin_unlock(&dir->i_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100754struct nfs4_opendata {
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400755 struct kref kref;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100756 struct nfs_openargs o_arg;
757 struct nfs_openres o_res;
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100758 struct nfs_open_confirmargs c_arg;
759 struct nfs_open_confirmres c_res;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100760 struct nfs_fattr f_attr;
761 struct nfs_fattr dir_attr;
Trond Myklebustad389da2007-06-05 12:30:00 -0400762 struct path path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100763 struct dentry *dir;
764 struct nfs4_state_owner *owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -0400765 struct nfs4_state *state;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100766 struct iattr attrs;
Trond Myklebust26e976a2006-01-03 09:55:21 +0100767 unsigned long timestamp;
Trond Myklebust3e309912007-07-07 13:19:59 -0400768 unsigned int rpc_done : 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +0100769 int rpc_status;
770 int cancelled;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100771};
772
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400773
774static void nfs4_init_opendata_res(struct nfs4_opendata *p)
775{
776 p->o_res.f_attr = &p->f_attr;
777 p->o_res.dir_attr = &p->dir_attr;
Trond Myklebustc1d51932008-04-07 13:20:54 -0400778 p->o_res.seqid = p->o_arg.seqid;
779 p->c_res.seqid = p->c_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400780 p->o_res.server = p->o_arg.server;
781 nfs_fattr_init(&p->f_attr);
782 nfs_fattr_init(&p->dir_attr);
783}
784
Trond Myklebustad389da2007-06-05 12:30:00 -0400785static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500786 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400787 const struct iattr *attrs,
788 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100789{
Trond Myklebustad389da2007-06-05 12:30:00 -0400790 struct dentry *parent = dget_parent(path->dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100791 struct inode *dir = parent->d_inode;
792 struct nfs_server *server = NFS_SERVER(dir);
793 struct nfs4_opendata *p;
794
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400795 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100796 if (p == NULL)
797 goto err;
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400798 p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100799 if (p->o_arg.seqid == NULL)
800 goto err_free;
Al Virof6948692010-01-30 13:51:04 -0500801 path_get(path);
802 p->path = *path;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100803 p->dir = parent;
804 p->owner = sp;
805 atomic_inc(&sp->so_count);
806 p->o_arg.fh = NFS_FH(dir);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500807 p->o_arg.open_flags = flags;
808 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
David Howells7539bba2006-08-22 20:06:09 -0400809 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400810 p->o_arg.id = sp->so_owner_id.id;
Trond Myklebustad389da2007-06-05 12:30:00 -0400811 p->o_arg.name = &p->path.dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100812 p->o_arg.server = server;
813 p->o_arg.bitmask = server->attr_bitmask;
814 p->o_arg.claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400815 if (flags & O_CREAT) {
816 u32 *s;
817
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100818 p->o_arg.u.attrs = &p->attrs;
819 memcpy(&p->attrs, attrs, sizeof(p->attrs));
Trond Myklebustd77d76f2010-06-16 09:52:27 -0400820 s = (u32 *) p->o_arg.u.verifier.data;
821 s[0] = jiffies;
822 s[1] = current->pid;
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100823 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +0100824 p->c_arg.fh = &p->o_res.fh;
825 p->c_arg.stateid = &p->o_res.stateid;
826 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400827 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400828 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100829 return p;
830err_free:
831 kfree(p);
832err:
833 dput(parent);
834 return NULL;
835}
836
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400837static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100838{
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400839 struct nfs4_opendata *p = container_of(kref,
840 struct nfs4_opendata, kref);
841
842 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400843 if (p->state != NULL)
844 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400845 nfs4_put_state_owner(p->owner);
846 dput(p->dir);
Jan Blunck31f31db12008-05-02 13:42:45 -0700847 path_put(&p->path);
Trond Myklebustc6d00e62007-06-17 16:02:44 -0400848 kfree(p);
849}
850
851static void nfs4_opendata_put(struct nfs4_opendata *p)
852{
853 if (p != NULL)
854 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100855}
856
Trond Myklebust06f814a2006-01-03 09:55:07 +0100857static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task)
858{
Trond Myklebust06f814a2006-01-03 09:55:07 +0100859 int ret;
860
Trond Myklebust06f814a2006-01-03 09:55:07 +0100861 ret = rpc_wait_for_completion_task(task);
Trond Myklebust06f814a2006-01-03 09:55:07 +0100862 return ret;
863}
864
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500865static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -0400866{
867 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500868
869 if (open_mode & O_EXCL)
870 goto out;
871 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -0400872 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -0500873 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
874 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400875 break;
876 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500877 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
878 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400879 break;
880 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -0500881 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
882 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -0400883 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500884out:
Trond Myklebust6ee41262007-07-08 14:11:36 -0400885 return ret;
886}
887
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500888static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400889{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500890 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400891 return 0;
Trond Myklebust15c831b2008-12-23 15:21:39 -0500892 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
Trond Myklebustaac00a82007-07-05 19:02:21 -0400893 return 0;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500894 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -0400895 return 1;
896}
897
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500898static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +0100899{
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500900 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +0100901 case FMODE_WRITE:
902 state->n_wronly++;
903 break;
904 case FMODE_READ:
905 state->n_rdonly++;
906 break;
907 case FMODE_READ|FMODE_WRITE:
908 state->n_rdwr++;
909 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500910 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +0100911}
912
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500913static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400914{
915 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
916 memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data));
917 memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data));
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500918 switch (fmode) {
Trond Myklebust003707c2007-07-05 18:07:55 -0400919 case FMODE_READ:
920 set_bit(NFS_O_RDONLY_STATE, &state->flags);
921 break;
922 case FMODE_WRITE:
923 set_bit(NFS_O_WRONLY_STATE, &state->flags);
924 break;
925 case FMODE_READ|FMODE_WRITE:
926 set_bit(NFS_O_RDWR_STATE, &state->flags);
927 }
928}
929
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500930static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust003707c2007-07-05 18:07:55 -0400931{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400932 write_seqlock(&state->seqlock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500933 nfs_set_open_stateid_locked(state, stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400934 write_sequnlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400935}
936
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500937static 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 -0700938{
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400939 /*
940 * Protect the call to nfs4_state_set_mode_locked and
941 * serialise the stateid update
942 */
943 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -0400944 if (deleg_stateid != NULL) {
945 memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data));
946 set_bit(NFS_DELEGATED_STATE, &state->flags);
947 }
948 if (open_stateid != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500949 nfs_set_open_stateid_locked(state, open_stateid, fmode);
Trond Myklebust8bda4e42007-07-09 10:45:42 -0400950 write_sequnlock(&state->seqlock);
951 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500952 update_open_stateflags(state, fmode);
Trond Myklebustec073422005-10-20 14:22:47 -0700953 spin_unlock(&state->owner->so_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954}
955
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500956static 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 -0500957{
958 struct nfs_inode *nfsi = NFS_I(state->inode);
959 struct nfs_delegation *deleg_cur;
960 int ret = 0;
961
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500962 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -0500963
964 rcu_read_lock();
965 deleg_cur = rcu_dereference(nfsi->delegation);
966 if (deleg_cur == NULL)
967 goto no_delegation;
968
969 spin_lock(&deleg_cur->lock);
970 if (nfsi->delegation != deleg_cur ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500971 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -0500972 goto no_delegation_unlock;
973
974 if (delegation == NULL)
975 delegation = &deleg_cur->stateid;
976 else if (memcmp(deleg_cur->stateid.data, delegation->data, NFS4_STATEID_SIZE) != 0)
977 goto no_delegation_unlock;
978
Trond Myklebustb7391f42008-12-23 15:21:52 -0500979 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500980 __update_open_stateid(state, open_stateid, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500981 ret = 1;
982no_delegation_unlock:
983 spin_unlock(&deleg_cur->lock);
984no_delegation:
985 rcu_read_unlock();
986
987 if (!ret && open_stateid != NULL) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500988 __update_open_stateid(state, open_stateid, NULL, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -0500989 ret = 1;
990 }
991
992 return ret;
993}
994
995
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500996static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -0400997{
998 struct nfs_delegation *delegation;
999
1000 rcu_read_lock();
1001 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001002 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001003 rcu_read_unlock();
1004 return;
1005 }
1006 rcu_read_unlock();
1007 nfs_inode_return_delegation(inode);
1008}
1009
Trond Myklebust6ee41262007-07-08 14:11:36 -04001010static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001011{
1012 struct nfs4_state *state = opendata->state;
1013 struct nfs_inode *nfsi = NFS_I(state->inode);
1014 struct nfs_delegation *delegation;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001015 int open_mode = opendata->o_arg.open_flags & O_EXCL;
1016 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001017 nfs4_stateid stateid;
1018 int ret = -EAGAIN;
1019
Trond Myklebustaac00a82007-07-05 19:02:21 -04001020 for (;;) {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001021 if (can_open_cached(state, fmode, open_mode)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001022 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001023 if (can_open_cached(state, fmode, open_mode)) {
1024 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001025 spin_unlock(&state->owner->so_lock);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001026 goto out_return_state;
1027 }
1028 spin_unlock(&state->owner->so_lock);
1029 }
Trond Myklebust34310432008-12-23 15:21:38 -05001030 rcu_read_lock();
1031 delegation = rcu_dereference(nfsi->delegation);
1032 if (delegation == NULL ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001033 !can_open_delegated(delegation, fmode)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001034 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001035 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001036 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001037 /* Save the delegation */
1038 memcpy(stateid.data, delegation->stateid.data, sizeof(stateid.data));
1039 rcu_read_unlock();
Trond Myklebustaf22f942007-08-10 17:45:10 -04001040 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001041 if (ret != 0)
1042 goto out;
1043 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001044
1045 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001046 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001047 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001048 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001049out:
1050 return ERR_PTR(ret);
1051out_return_state:
1052 atomic_inc(&state->count);
1053 return state;
1054}
1055
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001056static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1057{
1058 struct inode *inode;
1059 struct nfs4_state *state = NULL;
Trond Myklebust003707c2007-07-05 18:07:55 -04001060 struct nfs_delegation *delegation;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001061 int ret;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001062
Trond Myklebustaac00a82007-07-05 19:02:21 -04001063 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001064 state = nfs4_try_open_cached(data);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001065 goto out;
1066 }
1067
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001068 ret = -EAGAIN;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001069 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001070 goto err;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001071 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001072 ret = PTR_ERR(inode);
Trond Myklebust03f28e32006-03-20 13:44:48 -05001073 if (IS_ERR(inode))
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001074 goto err;
1075 ret = -ENOMEM;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001076 state = nfs4_get_open_state(inode, data->owner);
1077 if (state == NULL)
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001078 goto err_put_inode;
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001079 if (data->o_res.delegation_type != 0) {
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001080 int delegation_flags = 0;
1081
Trond Myklebust003707c2007-07-05 18:07:55 -04001082 rcu_read_lock();
1083 delegation = rcu_dereference(NFS_I(inode)->delegation);
1084 if (delegation)
1085 delegation_flags = delegation->flags;
1086 rcu_read_unlock();
Trond Myklebust15c831b2008-12-23 15:21:39 -05001087 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Trond Myklebust549d6ed2007-07-03 16:42:45 -04001088 nfs_inode_set_delegation(state->inode,
1089 data->owner->so_cred,
1090 &data->o_res);
1091 else
1092 nfs_inode_reclaim_delegation(state->inode,
1093 data->owner->so_cred,
1094 &data->o_res);
1095 }
Trond Myklebust34310432008-12-23 15:21:38 -05001096
1097 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001098 data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001099 iput(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001100out:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001101 return state;
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001102err_put_inode:
1103 iput(inode);
1104err:
1105 return ERR_PTR(ret);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001106}
1107
Trond Myklebust864472e2006-01-03 09:55:15 +01001108static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1109{
1110 struct nfs_inode *nfsi = NFS_I(state->inode);
1111 struct nfs_open_context *ctx;
1112
1113 spin_lock(&state->inode->i_lock);
1114 list_for_each_entry(ctx, &nfsi->open_files, list) {
1115 if (ctx->state != state)
1116 continue;
1117 get_nfs_open_context(ctx);
1118 spin_unlock(&state->inode->i_lock);
1119 return ctx;
1120 }
1121 spin_unlock(&state->inode->i_lock);
1122 return ERR_PTR(-ENOENT);
1123}
1124
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001125static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx, struct nfs4_state *state)
1126{
1127 struct nfs4_opendata *opendata;
1128
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001129 opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, 0, NULL, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001130 if (opendata == NULL)
1131 return ERR_PTR(-ENOMEM);
1132 opendata->state = state;
1133 atomic_inc(&state->count);
1134 return opendata;
1135}
1136
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001137static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, fmode_t fmode, struct nfs4_state **res)
Trond Myklebust864472e2006-01-03 09:55:15 +01001138{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001139 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001140 int ret;
1141
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001142 opendata->o_arg.open_flags = 0;
1143 opendata->o_arg.fmode = fmode;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001144 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1145 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1146 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001147 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001148 if (ret != 0)
1149 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001150 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001151 if (IS_ERR(newstate))
1152 return PTR_ERR(newstate);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001153 nfs4_close_state(&opendata->path, newstate, fmode);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001154 *res = newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001155 return 0;
1156}
1157
1158static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1159{
Trond Myklebust864472e2006-01-03 09:55:15 +01001160 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001161 int ret;
1162
1163 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001164 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001165 smp_rmb();
1166 if (state->n_rdwr != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001167 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001168 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001169 if (ret != 0)
1170 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001171 if (newstate != state)
1172 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001173 }
1174 if (state->n_wronly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001175 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001176 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001177 if (ret != 0)
1178 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001179 if (newstate != state)
1180 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001181 }
1182 if (state->n_rdonly != 0) {
Trond Myklebustb0ed9db2010-10-04 17:59:08 -04001183 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001184 ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate);
Trond Myklebust864472e2006-01-03 09:55:15 +01001185 if (ret != 0)
1186 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001187 if (newstate != state)
1188 return -ESTALE;
Trond Myklebust864472e2006-01-03 09:55:15 +01001189 }
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001190 /*
1191 * We may have performed cached opens for all three recoveries.
1192 * Check if we need to update the current stateid.
1193 */
1194 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
1195 memcmp(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data)) != 0) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001196 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001197 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1198 memcpy(state->stateid.data, state->open_stateid.data, sizeof(state->stateid.data));
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001199 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001200 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001201 return 0;
1202}
1203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204/*
1205 * OPEN_RECLAIM:
1206 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001208static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001210 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001211 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001212 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 int status;
1214
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001215 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1216 if (IS_ERR(opendata))
1217 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001218 opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS;
1219 opendata->o_arg.fh = NFS_FH(state->inode);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001220 rcu_read_lock();
1221 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001222 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001223 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001224 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001225 opendata->o_arg.u.delegation_type = delegation_type;
1226 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001227 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 return status;
1229}
1230
Trond Myklebust539cd032007-06-05 11:46:42 -04001231static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
1233 struct nfs_server *server = NFS_SERVER(state->inode);
1234 struct nfs4_exception exception = { };
1235 int err;
1236 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001237 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001238 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001239 break;
1240 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 } while (exception.retry);
1242 return err;
1243}
1244
Trond Myklebust864472e2006-01-03 09:55:15 +01001245static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
1246{
1247 struct nfs_open_context *ctx;
1248 int ret;
1249
1250 ctx = nfs4_state_find_open_context(state);
1251 if (IS_ERR(ctx))
1252 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001253 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001254 put_nfs_open_context(ctx);
1255 return ret;
1256}
1257
Trond Myklebust13437e12007-07-06 15:10:43 -04001258static 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 -07001259{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001260 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001261 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001263 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1264 if (IS_ERR(opendata))
1265 return PTR_ERR(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001266 opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR;
Trond Myklebust13437e12007-07-06 15:10:43 -04001267 memcpy(opendata->o_arg.u.delegation.data, stateid->data,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001268 sizeof(opendata->o_arg.u.delegation.data));
Trond Myklebust864472e2006-01-03 09:55:15 +01001269 ret = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001270 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001271 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272}
1273
Trond Myklebust13437e12007-07-06 15:10:43 -04001274int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
1276 struct nfs4_exception exception = { };
Trond Myklebust539cd032007-06-05 11:46:42 -04001277 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 int err;
1279 do {
Trond Myklebust13437e12007-07-06 15:10:43 -04001280 err = _nfs4_open_delegation_recall(ctx, state, stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 switch (err) {
1282 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07001283 case -ENOENT:
1284 case -ESTALE:
1285 goto out;
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001286 case -NFS4ERR_BADSESSION:
1287 case -NFS4ERR_BADSLOT:
1288 case -NFS4ERR_BAD_HIGH_SLOT:
1289 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
1290 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001291 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Ricardo Labiagabcfa49f2009-12-07 09:22:29 -05001292 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 case -NFS4ERR_STALE_CLIENTID:
1294 case -NFS4ERR_STALE_STATEID:
1295 case -NFS4ERR_EXPIRED:
1296 /* Don't recall a delegation if it was lost */
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001297 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001298 goto out;
1299 case -ERESTARTSYS:
1300 /*
1301 * The show must go on: exit, but mark the
1302 * stateid as needing recovery.
1303 */
1304 case -NFS4ERR_ADMIN_REVOKED:
1305 case -NFS4ERR_BAD_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001306 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust168667c2010-10-19 19:47:49 -04001307 case -EKEYEXPIRED:
1308 /*
1309 * User RPCSEC_GSS context has expired.
1310 * We cannot recover this stateid now, so
1311 * skip it and allow recovery thread to
1312 * proceed.
1313 */
Trond Myklebust965b5d62009-06-17 13:22:59 -07001314 case -ENOMEM:
1315 err = 0;
1316 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 }
1318 err = nfs4_handle_exception(server, err, &exception);
1319 } while (exception.retry);
Trond Myklebust965b5d62009-06-17 13:22:59 -07001320out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 return err;
1322}
1323
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001324static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
1325{
1326 struct nfs4_opendata *data = calldata;
1327
1328 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001329 if (data->rpc_status == 0) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001330 memcpy(data->o_res.stateid.data, data->c_res.stateid.data,
1331 sizeof(data->o_res.stateid.data));
Trond Myklebustbb226292008-01-02 15:19:18 -05001332 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001333 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust3e309912007-07-07 13:19:59 -04001334 data->rpc_done = 1;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001335 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001336}
1337
1338static void nfs4_open_confirm_release(void *calldata)
1339{
1340 struct nfs4_opendata *data = calldata;
1341 struct nfs4_state *state = NULL;
1342
1343 /* If this request hasn't been cancelled, do nothing */
1344 if (data->cancelled == 0)
1345 goto out_free;
1346 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001347 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001348 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001349 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001350 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001351 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001352out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001353 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001354}
1355
1356static const struct rpc_call_ops nfs4_open_confirm_ops = {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001357 .rpc_call_done = nfs4_open_confirm_done,
1358 .rpc_release = nfs4_open_confirm_release,
1359};
1360
1361/*
1362 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
1363 */
1364static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
1365{
1366 struct nfs_server *server = NFS_SERVER(data->dir->d_inode);
1367 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001368 struct rpc_message msg = {
1369 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
1370 .rpc_argp = &data->c_arg,
1371 .rpc_resp = &data->c_res,
1372 .rpc_cred = data->owner->so_cred,
1373 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001374 struct rpc_task_setup task_setup_data = {
1375 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001376 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001377 .callback_ops = &nfs4_open_confirm_ops,
1378 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001379 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001380 .flags = RPC_TASK_ASYNC,
1381 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 int status;
1383
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001384 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001385 data->rpc_done = 0;
1386 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001387 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001388 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05001389 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001390 return PTR_ERR(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001391 status = nfs4_wait_for_completion_rpc_task(task);
1392 if (status != 0) {
1393 data->cancelled = 1;
1394 smp_wmb();
1395 } else
1396 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05001397 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 return status;
1399}
1400
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001401static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001403 struct nfs4_opendata *data = calldata;
1404 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001405
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001406 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
1407 return;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001408 /*
1409 * Check if we still need to send an OPEN call, or if we can use
1410 * a delegation instead.
1411 */
1412 if (data->state != NULL) {
1413 struct nfs_delegation *delegation;
1414
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001415 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04001416 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001417 rcu_read_lock();
1418 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
1419 if (delegation != NULL &&
Trond Myklebust15c831b2008-12-23 15:21:39 -05001420 test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001421 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001422 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001423 }
1424 rcu_read_unlock();
1425 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001426 /* Update sequence id. */
Trond Myklebust9f958ab2007-07-02 13:58:33 -04001427 data->o_arg.id = sp->so_owner_id.id;
Trond Myklebust1f0e8902010-06-24 15:11:43 -04001428 data->o_arg.clientid = sp->so_server->nfs_client->cl_clientid;
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001429 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) {
Trond Myklebust5138fde2007-07-14 15:40:01 -04001430 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001431 nfs_copy_fh(&data->o_res.fh, data->o_arg.fh);
1432 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001433 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001434 if (nfs4_setup_sequence(data->o_arg.server,
Andy Adamsond8985282009-04-01 09:22:21 -04001435 &data->o_arg.seq_args,
1436 &data->o_res.seq_res, 1, task))
1437 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001438 rpc_call_start(task);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001439 return;
1440out_no_action:
1441 task->tk_action = NULL;
1442
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001443}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001445static void nfs4_recover_open_prepare(struct rpc_task *task, void *calldata)
1446{
1447 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
1448 nfs4_open_prepare(task, calldata);
1449}
1450
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001451static void nfs4_open_done(struct rpc_task *task, void *calldata)
1452{
1453 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001455 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04001456
Trond Myklebust14516c32010-07-31 14:29:06 -04001457 if (!nfs4_sequence_done(task, &data->o_res.seq_res))
1458 return;
Andy Adamsond8985282009-04-01 09:22:21 -04001459
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001460 if (task->tk_status == 0) {
1461 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07001462 case S_IFREG:
1463 break;
1464 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001465 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001466 break;
1467 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001468 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001469 break;
1470 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001471 data->rpc_status = -ENOTDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07001472 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01001473 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04001474 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
1475 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07001476 }
Trond Myklebust3e309912007-07-07 13:19:59 -04001477 data->rpc_done = 1;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001478}
Trond Myklebust6f926b52005-10-18 14:20:18 -07001479
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001480static void nfs4_open_release(void *calldata)
1481{
1482 struct nfs4_opendata *data = calldata;
1483 struct nfs4_state *state = NULL;
1484
1485 /* If this request hasn't been cancelled, do nothing */
1486 if (data->cancelled == 0)
1487 goto out_free;
1488 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04001489 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001490 goto out_free;
1491 /* In case we need an open_confirm, no cleanup! */
1492 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
1493 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001494 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001495 if (!IS_ERR(state))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001496 nfs4_close_state(&data->path, state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001497out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001498 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001499}
1500
1501static const struct rpc_call_ops nfs4_open_ops = {
1502 .rpc_call_prepare = nfs4_open_prepare,
1503 .rpc_call_done = nfs4_open_done,
1504 .rpc_release = nfs4_open_release,
1505};
1506
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001507static const struct rpc_call_ops nfs4_recover_open_ops = {
1508 .rpc_call_prepare = nfs4_recover_open_prepare,
1509 .rpc_call_done = nfs4_open_done,
1510 .rpc_release = nfs4_open_release,
1511};
1512
1513static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001514{
1515 struct inode *dir = data->dir->d_inode;
1516 struct nfs_server *server = NFS_SERVER(dir);
1517 struct nfs_openargs *o_arg = &data->o_arg;
1518 struct nfs_openres *o_res = &data->o_res;
1519 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001520 struct rpc_message msg = {
1521 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
1522 .rpc_argp = o_arg,
1523 .rpc_resp = o_res,
1524 .rpc_cred = data->owner->so_cred,
1525 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04001526 struct rpc_task_setup task_setup_data = {
1527 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04001528 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001529 .callback_ops = &nfs4_open_ops,
1530 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05001531 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04001532 .flags = RPC_TASK_ASYNC,
1533 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001534 int status;
1535
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001536 kref_get(&data->kref);
Trond Myklebust3e309912007-07-07 13:19:59 -04001537 data->rpc_done = 0;
1538 data->rpc_status = 0;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001539 data->cancelled = 0;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001540 if (isrecover)
1541 task_setup_data.callback_ops = &nfs4_recover_open_ops;
Trond Myklebustc970aa82007-07-14 15:39:59 -04001542 task = rpc_run_task(&task_setup_data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001543 if (IS_ERR(task))
1544 return PTR_ERR(task);
1545 status = nfs4_wait_for_completion_rpc_task(task);
1546 if (status != 0) {
1547 data->cancelled = 1;
1548 smp_wmb();
1549 } else
1550 status = data->rpc_status;
1551 rpc_put_task(task);
1552
1553 return status;
1554}
1555
1556static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
1557{
1558 struct inode *dir = data->dir->d_inode;
1559 struct nfs_openres *o_res = &data->o_res;
1560 int status;
1561
1562 status = nfs4_run_open_task(data, 1);
1563 if (status != 0 || !data->rpc_done)
1564 return status;
1565
1566 nfs_refresh_inode(dir, o_res->dir_attr);
1567
1568 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
1569 status = _nfs4_proc_open_confirm(data);
1570 if (status != 0)
1571 return status;
1572 }
1573
1574 return status;
1575}
1576
1577/*
1578 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
1579 */
1580static int _nfs4_proc_open(struct nfs4_opendata *data)
1581{
1582 struct inode *dir = data->dir->d_inode;
1583 struct nfs_server *server = NFS_SERVER(dir);
1584 struct nfs_openargs *o_arg = &data->o_arg;
1585 struct nfs_openres *o_res = &data->o_res;
1586 int status;
1587
1588 status = nfs4_run_open_task(data, 0);
Trond Myklebust3e309912007-07-07 13:19:59 -04001589 if (status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001590 return status;
1591
Trond Myklebust56ae19f2005-10-27 22:12:40 -04001592 if (o_arg->open_flags & O_CREAT) {
1593 update_changeattr(dir, &o_res->cinfo);
1594 nfs_post_op_update_inode(dir, o_res->dir_attr);
1595 } else
1596 nfs_refresh_inode(dir, o_res->dir_attr);
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001597 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
1598 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001600 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001602 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 }
1604 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR))
Trond Myklebust99367812007-07-17 21:52:41 -04001605 _nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001606 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607}
1608
Andy Adamsond83217c2011-03-01 01:34:17 +00001609static int nfs4_client_recover_expired_lease(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +01001610{
Trond Myklebusta78cb572009-08-09 15:06:19 -04001611 unsigned int loop;
Trond Myklebust6b309542006-09-14 14:03:14 -04001612 int ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001613
Trond Myklebusta78cb572009-08-09 15:06:19 -04001614 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
Trond Myklebust65de8722008-12-23 15:21:44 -05001615 ret = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b309542006-09-14 14:03:14 -04001616 if (ret != 0)
Trond Myklebusta78cb572009-08-09 15:06:19 -04001617 break;
Trond Myklebuste598d842008-12-23 15:21:42 -05001618 if (!test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) &&
1619 !test_bit(NFS4CLNT_CHECK_LEASE,&clp->cl_state))
Trond Myklebust6b309542006-09-14 14:03:14 -04001620 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05001621 nfs4_schedule_state_manager(clp);
Trond Myklebusta78cb572009-08-09 15:06:19 -04001622 ret = -EIO;
Trond Myklebust6b309542006-09-14 14:03:14 -04001623 }
Trond Myklebusta78cb572009-08-09 15:06:19 -04001624 return ret;
Trond Myklebust58d97142006-01-03 09:55:24 +01001625}
1626
Andy Adamsond83217c2011-03-01 01:34:17 +00001627static int nfs4_recover_expired_lease(struct nfs_server *server)
1628{
1629 return nfs4_client_recover_expired_lease(server->nfs_client);
1630}
1631
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632/*
1633 * OPEN_EXPIRED:
1634 * reclaim state on the server after a network partition.
1635 * Assumes caller holds the appropriate lock
1636 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001637static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001639 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01001640 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001642 opendata = nfs4_open_recoverdata_alloc(ctx, state);
1643 if (IS_ERR(opendata))
1644 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001645 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04001646 if (ret == -ESTALE)
Trond Myklebust539cd032007-06-05 11:46:42 -04001647 d_drop(ctx->path.dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001648 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001649 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650}
1651
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001652static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001653{
Trond Myklebust539cd032007-06-05 11:46:42 -04001654 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00001655 struct nfs4_exception exception = { };
1656 int err;
1657
1658 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001659 err = _nfs4_open_expired(ctx, state);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001660 switch (err) {
1661 default:
1662 goto out;
1663 case -NFS4ERR_GRACE:
1664 case -NFS4ERR_DELAY:
1665 nfs4_handle_exception(server, err, &exception);
1666 err = 0;
1667 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00001668 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05001669out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00001670 return err;
1671}
1672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
1674{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01001676 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Trond Myklebust864472e2006-01-03 09:55:15 +01001678 ctx = nfs4_state_find_open_context(state);
1679 if (IS_ERR(ctx))
1680 return PTR_ERR(ctx);
Trond Myklebust539cd032007-06-05 11:46:42 -04001681 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01001682 put_nfs_open_context(ctx);
1683 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684}
1685
1686/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04001687 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
1688 * fields corresponding to attributes that were used to store the verifier.
1689 * Make sure we clobber those fields in the later setattr call
1690 */
1691static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr)
1692{
1693 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
1694 !(sattr->ia_valid & ATTR_ATIME_SET))
1695 sattr->ia_valid |= ATTR_ATIME;
1696
1697 if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
1698 !(sattr->ia_valid & ATTR_MTIME_SET))
1699 sattr->ia_valid |= ATTR_MTIME;
1700}
1701
1702/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001703 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001705static int _nfs4_do_open(struct inode *dir, struct path *path, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706{
1707 struct nfs4_state_owner *sp;
1708 struct nfs4_state *state = NULL;
1709 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001710 struct nfs4_opendata *opendata;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001711 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712
1713 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 status = -ENOMEM;
1715 if (!(sp = nfs4_get_state_owner(server, cred))) {
1716 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
1717 goto out_err;
1718 }
Trond Myklebust58d97142006-01-03 09:55:24 +01001719 status = nfs4_recover_expired_lease(server);
1720 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01001721 goto err_put_state_owner;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001722 if (path->dentry->d_inode != NULL)
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001723 nfs4_return_incompatible_delegation(path->dentry->d_inode, fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01001724 status = -ENOMEM;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001725 opendata = nfs4_opendata_alloc(path, sp, fmode, flags, sattr, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001726 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05001727 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Trond Myklebustaac00a82007-07-05 19:02:21 -04001729 if (path->dentry->d_inode != NULL)
1730 opendata->state = nfs4_get_open_state(path->dentry->d_inode, sp);
1731
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001732 status = _nfs4_proc_open(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 if (status != 0)
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001734 goto err_opendata_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001736 state = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001737 status = PTR_ERR(state);
1738 if (IS_ERR(state))
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001739 goto err_opendata_put;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001740 if (server->caps & NFS_CAP_POSIX_LOCK)
Trond Myklebust8e469eb2010-01-26 15:42:30 -05001741 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Trond Myklebust0ab64e02010-04-16 16:22:51 -04001742
1743 if (opendata->o_arg.open_flags & O_EXCL) {
1744 nfs4_exclusive_attrset(opendata, sattr);
1745
1746 nfs_fattr_init(opendata->o_res.f_attr);
1747 status = nfs4_do_setattr(state->inode, cred,
1748 opendata->o_res.f_attr, sattr,
1749 state);
1750 if (status == 0)
1751 nfs_setattr_update_inode(state->inode, sattr);
1752 nfs_post_op_update_inode(state->inode, opendata->o_res.f_attr);
1753 }
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001754 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 *res = state;
1757 return 0;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001758err_opendata_put:
1759 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001760err_put_state_owner:
1761 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 *res = NULL;
1764 return status;
1765}
1766
1767
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001768static struct nfs4_state *nfs4_do_open(struct inode *dir, struct path *path, fmode_t fmode, int flags, struct iattr *sattr, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769{
1770 struct nfs4_exception exception = { };
1771 struct nfs4_state *res;
1772 int status;
1773
1774 do {
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001775 status = _nfs4_do_open(dir, path, fmode, flags, sattr, cred, &res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 if (status == 0)
1777 break;
1778 /* NOTE: BAD_SEQID means the server and client disagree about the
1779 * book-keeping w.r.t. state-changing operations
1780 * (OPEN/CLOSE/LOCK/LOCKU...)
1781 * It is actually a sign of a bug on the client or on the server.
1782 *
1783 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07001784 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 * have unhashed the old state_owner for us, and that we can
1786 * therefore safely retry using a new one. We should still warn
1787 * the user though...
1788 */
1789 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04001790 printk(KERN_WARNING "NFS: v4 server %s "
1791 " returned a bad sequence-id error!\n",
1792 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 exception.retry = 1;
1794 continue;
1795 }
Trond Myklebust550f5742005-10-18 14:20:21 -07001796 /*
1797 * BAD_STATEID on OPEN means that the server cancelled our
1798 * state before it received the OPEN_CONFIRM.
1799 * Recover by retrying the request as per the discussion
1800 * on Page 181 of RFC3530.
1801 */
1802 if (status == -NFS4ERR_BAD_STATEID) {
1803 exception.retry = 1;
1804 continue;
1805 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001806 if (status == -EAGAIN) {
1807 /* We must have found a delegation */
1808 exception.retry = 1;
1809 continue;
1810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir),
1812 status, &exception));
1813 } while (exception.retry);
1814 return res;
1815}
1816
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001817static int _nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1818 struct nfs_fattr *fattr, struct iattr *sattr,
1819 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001821 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 struct nfs_setattrargs arg = {
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001823 .fh = NFS_FH(inode),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 .iap = sattr,
1825 .server = server,
1826 .bitmask = server->attr_bitmask,
1827 };
1828 struct nfs_setattrres res = {
1829 .fattr = fattr,
1830 .server = server,
1831 };
1832 struct rpc_message msg = {
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001833 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
1834 .rpc_argp = &arg,
1835 .rpc_resp = &res,
1836 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 };
Trond Myklebust26e976a2006-01-03 09:55:21 +01001838 unsigned long timestamp = jiffies;
Trond Myklebust65e43082005-08-16 11:49:44 -04001839 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Trond Myklebust0e574af2005-10-27 22:12:38 -04001841 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001843 if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) {
1844 /* Use that stateid */
1845 } else if (state != NULL) {
Trond Myklebust77041ed2010-07-01 12:49:11 -04001846 nfs4_copy_stateid(&arg.stateid, state, current->files, current->tgid);
Trond Myklebust08e9eac2005-06-22 17:16:29 +00001847 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid));
1849
Bryan Schumaker7c513052011-03-24 17:12:24 +00001850 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001851 if (status == 0 && state != NULL)
1852 renew_lease(server, timestamp);
Trond Myklebust65e43082005-08-16 11:49:44 -04001853 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854}
1855
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001856static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
1857 struct nfs_fattr *fattr, struct iattr *sattr,
1858 struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001860 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 struct nfs4_exception exception = { };
1862 int err;
1863 do {
1864 err = nfs4_handle_exception(server,
Trond Myklebust659bfcd2008-06-10 19:39:41 -04001865 _nfs4_do_setattr(inode, cred, fattr, sattr, state),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 &exception);
1867 } while (exception.retry);
1868 return err;
1869}
1870
1871struct nfs4_closedata {
Trond Myklebust4a35bd42007-06-05 10:31:33 -04001872 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 struct inode *inode;
1874 struct nfs4_state *state;
1875 struct nfs_closeargs arg;
1876 struct nfs_closeres res;
Trond Myklebust516a6af2005-10-27 22:12:41 -04001877 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01001878 unsigned long timestamp;
Fred Isamanf7e89172011-01-06 11:36:32 +00001879 bool roc;
1880 u32 roc_barrier;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881};
1882
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001883static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07001884{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001885 struct nfs4_closedata *calldata = data;
1886 struct nfs4_state_owner *sp = calldata->state->owner;
Trond Myklebust95121352005-10-18 14:20:12 -07001887
Fred Isamanf7e89172011-01-06 11:36:32 +00001888 if (calldata->roc)
1889 pnfs_roc_release(calldata->state->inode);
Trond Myklebust95121352005-10-18 14:20:12 -07001890 nfs4_put_open_state(calldata->state);
1891 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07001892 nfs4_put_state_owner(sp);
Jan Blunck31f31db12008-05-02 13:42:45 -07001893 path_put(&calldata->path);
Trond Myklebust95121352005-10-18 14:20:12 -07001894 kfree(calldata);
1895}
1896
Trond Myklebust88069f72009-12-08 08:33:16 -05001897static void nfs4_close_clear_stateid_flags(struct nfs4_state *state,
1898 fmode_t fmode)
1899{
1900 spin_lock(&state->owner->so_lock);
1901 if (!(fmode & FMODE_READ))
1902 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1903 if (!(fmode & FMODE_WRITE))
1904 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1905 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1906 spin_unlock(&state->owner->so_lock);
1907}
1908
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001909static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001911 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 struct nfs_server *server = NFS_SERVER(calldata->inode);
1914
Trond Myklebust14516c32010-07-31 14:29:06 -04001915 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
1916 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 /* hmm. we are done with the inode, and in the process of freeing
1918 * the state_owner. we keep this around to process errors
1919 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 switch (task->tk_status) {
1921 case 0:
Fred Isamanf7e89172011-01-06 11:36:32 +00001922 if (calldata->roc)
1923 pnfs_roc_set_barrier(state->inode,
1924 calldata->roc_barrier);
Trond Myklebust45328c32007-07-26 17:47:34 -04001925 nfs_set_open_stateid(state, &calldata->res.stateid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001926 renew_lease(server, calldata->timestamp);
Trond Myklebust88069f72009-12-08 08:33:16 -05001927 nfs4_close_clear_stateid_flags(state,
1928 calldata->arg.fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 break;
1930 case -NFS4ERR_STALE_STATEID:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001931 case -NFS4ERR_OLD_STATEID:
1932 case -NFS4ERR_BAD_STATEID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 case -NFS4ERR_EXPIRED:
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001934 if (calldata->arg.fmode == 0)
Trond Myklebust9e33bed2008-12-23 15:21:46 -05001935 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 default:
Trond Myklebust72211db2009-12-15 14:47:36 -05001937 if (nfs4_async_handle_error(task, server, state) == -EAGAIN)
1938 rpc_restart_call_prepare(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 }
Trond Myklebust72211db2009-12-15 14:47:36 -05001940 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebust516a6af2005-10-27 22:12:41 -04001941 nfs_refresh_inode(calldata->inode, calldata->res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942}
1943
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001944static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001946 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07001947 struct nfs4_state *state = calldata->state;
Trond Myklebust88069f72009-12-08 08:33:16 -05001948 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07001949
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001950 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebust95121352005-10-18 14:20:12 -07001951 return;
Trond Myklebust003707c2007-07-05 18:07:55 -04001952
Trond Myklebust88069f72009-12-08 08:33:16 -05001953 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
1954 calldata->arg.fmode = FMODE_READ|FMODE_WRITE;
Trond Myklebust4cecb762005-11-04 15:32:58 -05001955 spin_lock(&state->owner->so_lock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001956 /* Calculate the change in open mode */
Trond Myklebuste7616922006-01-03 09:55:13 +01001957 if (state->n_rdwr == 0) {
Trond Myklebust003707c2007-07-05 18:07:55 -04001958 if (state->n_rdonly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001959 call_close |= test_bit(NFS_O_RDONLY_STATE, &state->flags);
1960 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1961 calldata->arg.fmode &= ~FMODE_READ;
Trond Myklebust003707c2007-07-05 18:07:55 -04001962 }
1963 if (state->n_wronly == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001964 call_close |= test_bit(NFS_O_WRONLY_STATE, &state->flags);
1965 call_close |= test_bit(NFS_O_RDWR_STATE, &state->flags);
1966 calldata->arg.fmode &= ~FMODE_WRITE;
Trond Myklebust003707c2007-07-05 18:07:55 -04001967 }
Trond Myklebuste7616922006-01-03 09:55:13 +01001968 }
Trond Myklebust4cecb762005-11-04 15:32:58 -05001969 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05001970
1971 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001972 /* Note: exit _without_ calling nfs4_close_done */
1973 task->tk_action = NULL;
Trond Myklebust95121352005-10-18 14:20:12 -07001974 return;
1975 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001976
Fred Isamanf7e89172011-01-06 11:36:32 +00001977 if (calldata->arg.fmode == 0) {
Trond Myklebust88069f72009-12-08 08:33:16 -05001978 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Fred Isamanf7e89172011-01-06 11:36:32 +00001979 if (calldata->roc &&
1980 pnfs_roc_drain(calldata->inode, &calldata->roc_barrier)) {
1981 rpc_sleep_on(&NFS_SERVER(calldata->inode)->roc_rpcwaitq,
1982 task, NULL);
1983 return;
1984 }
1985 }
Trond Myklebust88069f72009-12-08 08:33:16 -05001986
Trond Myklebust516a6af2005-10-27 22:12:41 -04001987 nfs_fattr_init(calldata->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01001988 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04001989 if (nfs4_setup_sequence(NFS_SERVER(calldata->inode),
Andy Adamson19ddab02009-04-01 09:22:20 -04001990 &calldata->arg.seq_args, &calldata->res.seq_res,
1991 1, task))
1992 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04001993 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994}
1995
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001996static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01001997 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001998 .rpc_call_done = nfs4_close_done,
1999 .rpc_release = nfs4_free_closedata,
2000};
2001
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002/*
2003 * It is possible for data to be read/written from a mem-mapped file
2004 * after the sys_close call (which hits the vfs layer as a flush).
2005 * This means that we can't safely call nfsv4 close on a file until
2006 * the inode is cleared. This in turn means that we are not good
2007 * NFSv4 citizens - we do not indicate to the server to update the file's
2008 * share state even when we are done with one of the three share
2009 * stateid's in the inode.
2010 *
2011 * NOTE: Caller must be holding the sp->so_owner semaphore!
2012 */
Fred Isamanf7e89172011-01-06 11:36:32 +00002013int nfs4_do_close(struct path *path, struct nfs4_state *state, gfp_t gfp_mask, int wait, bool roc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002015 struct nfs_server *server = NFS_SERVER(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04002017 struct nfs4_state_owner *sp = state->owner;
2018 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002019 struct rpc_message msg = {
2020 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
2021 .rpc_cred = state->owner->so_cred,
2022 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002023 struct rpc_task_setup task_setup_data = {
2024 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002025 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002026 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05002027 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002028 .flags = RPC_TASK_ASYNC,
2029 };
Trond Myklebust95121352005-10-18 14:20:12 -07002030 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002032 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07002034 goto out;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002035 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04002037 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebust003707c2007-07-05 18:07:55 -04002038 calldata->arg.stateid = &state->open_stateid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 /* Serialization for the sequence id */
Trond Myklebust8535b2b2010-05-13 12:51:01 -04002040 calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebust95121352005-10-18 14:20:12 -07002041 if (calldata->arg.seqid == NULL)
2042 goto out_free_calldata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002043 calldata->arg.fmode = 0;
Trond Myklebusta65318b2009-03-11 14:10:28 -04002044 calldata->arg.bitmask = server->cache_consistency_bitmask;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002045 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04002046 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04002047 calldata->res.server = server;
Fred Isamanf7e89172011-01-06 11:36:32 +00002048 calldata->roc = roc;
Al Virof6948692010-01-30 13:51:04 -05002049 path_get(path);
2050 calldata->path = *path;
Trond Myklebust95121352005-10-18 14:20:12 -07002051
Trond Myklebust1174dd12010-12-21 10:52:24 -05002052 msg.rpc_argp = &calldata->arg;
2053 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002054 task_setup_data.callback_data = calldata;
2055 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002056 if (IS_ERR(task))
2057 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002058 status = 0;
2059 if (wait)
2060 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002061 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04002062 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07002063out_free_calldata:
2064 kfree(calldata);
2065out:
Fred Isamanf7e89172011-01-06 11:36:32 +00002066 if (roc)
2067 pnfs_roc_release(state->inode);
Trond Myklebustb39e6252007-06-11 23:05:07 -04002068 nfs4_put_open_state(state);
2069 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07002070 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071}
2072
Trond Myklebust2b484292010-09-17 10:56:51 -04002073static struct inode *
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002074nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx, int open_flags, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 struct nfs4_state *state;
2077
Trond Myklebust565277f2007-10-15 18:17:53 -04002078 /* Protect against concurrent sillydeletes */
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002079 state = nfs4_do_open(dir, &ctx->path, ctx->mode, open_flags, attr, ctx->cred);
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002080 if (IS_ERR(state))
2081 return ERR_CAST(state);
Trond Myklebustcd9a1c02010-09-17 10:56:50 -04002082 ctx->state = state;
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04002083 return igrab(state->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084}
2085
Trond Myklebust1185a552009-12-03 15:54:02 -05002086static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04002087{
2088 if (ctx->state == NULL)
2089 return;
2090 if (is_sync)
2091 nfs4_close_sync(&ctx->path, ctx->state, ctx->mode);
2092 else
2093 nfs4_close_state(&ctx->path, ctx->state, ctx->mode);
2094}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
2096static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
2097{
Benny Halevy43652ad2009-04-01 09:21:54 -04002098 struct nfs4_server_caps_arg args = {
2099 .fhandle = fhandle,
2100 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 struct nfs4_server_caps_res res = {};
2102 struct rpc_message msg = {
2103 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04002104 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 .rpc_resp = &res,
2106 };
2107 int status;
2108
Bryan Schumaker7c513052011-03-24 17:12:24 +00002109 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 if (status == 0) {
2111 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002112 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
2113 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
2114 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
2115 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
2116 NFS_CAP_CTIME|NFS_CAP_MTIME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL)
2118 server->caps |= NFS_CAP_ACLS;
2119 if (res.has_links != 0)
2120 server->caps |= NFS_CAP_HARDLINKS;
2121 if (res.has_symlinks != 0)
2122 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04002123 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
2124 server->caps |= NFS_CAP_FILEID;
2125 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
2126 server->caps |= NFS_CAP_MODE;
2127 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
2128 server->caps |= NFS_CAP_NLINK;
2129 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
2130 server->caps |= NFS_CAP_OWNER;
2131 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
2132 server->caps |= NFS_CAP_OWNER_GROUP;
2133 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
2134 server->caps |= NFS_CAP_ATIME;
2135 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
2136 server->caps |= NFS_CAP_CTIME;
2137 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
2138 server->caps |= NFS_CAP_MTIME;
2139
Trond Myklebusta65318b2009-03-11 14:10:28 -04002140 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
2141 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
2142 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 server->acl_bitmask = res.acl_bitmask;
2144 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04002145
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 return status;
2147}
2148
Trond Myklebust55a97592006-06-09 09:34:19 -04002149int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150{
2151 struct nfs4_exception exception = { };
2152 int err;
2153 do {
2154 err = nfs4_handle_exception(server,
2155 _nfs4_server_capabilities(server, fhandle),
2156 &exception);
2157 } while (exception.retry);
2158 return err;
2159}
2160
2161static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2162 struct nfs_fsinfo *info)
2163{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 struct nfs4_lookup_root_arg args = {
2165 .bitmask = nfs4_fattr_bitmap,
2166 };
2167 struct nfs4_lookup_res res = {
2168 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04002169 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 .fh = fhandle,
2171 };
2172 struct rpc_message msg = {
2173 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
2174 .rpc_argp = &args,
2175 .rpc_resp = &res,
2176 };
Benny Halevy008f55d2009-04-01 09:22:50 -04002177
Trond Myklebust0e574af2005-10-27 22:12:38 -04002178 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002179 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180}
2181
2182static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
2183 struct nfs_fsinfo *info)
2184{
2185 struct nfs4_exception exception = { };
2186 int err;
2187 do {
2188 err = nfs4_handle_exception(server,
2189 _nfs4_lookup_root(server, fhandle, info),
2190 &exception);
2191 } while (exception.retry);
2192 return err;
2193}
2194
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002195static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
2196 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
2197{
2198 struct rpc_auth *auth;
2199 int ret;
2200
2201 auth = rpcauth_create(flavor, server->client);
2202 if (!auth) {
2203 ret = -EIO;
2204 goto out;
2205 }
2206 ret = nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002207out:
2208 return ret;
2209}
2210
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002211static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04002212 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213{
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002214 int i, len, status = 0;
Bryan Schumaker0fabee22011-04-13 14:31:29 -04002215 rpc_authflavor_t flav_array[NFS_MAX_SECFLAVORS];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002217 len = gss_mech_list_pseudoflavors(&flav_array[0]);
2218 flav_array[len] = RPC_AUTH_NULL;
2219 len += 1;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002220
2221 for (i = 0; i < len; i++) {
2222 status = nfs4_lookup_root_sec(server, fhandle, info, flav_array[i]);
Bryan Schumakerd1a80162011-04-13 14:31:28 -04002223 if (status == -EPERM || status == -EACCES)
2224 continue;
2225 break;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00002226 }
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04002227 return status;
2228}
2229
2230/*
2231 * get the file handle for the "/" directory on the server
2232 */
2233static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle,
2234 struct nfs_fsinfo *info)
2235{
2236 int status = nfs4_lookup_root(server, fhandle, info);
2237 if (status == -EPERM)
2238 status = nfs4_find_root_sec(server, fhandle, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 if (status == 0)
2240 status = nfs4_server_capabilities(server, fhandle);
2241 if (status == 0)
2242 status = nfs4_do_fsinfo(server, fhandle, info);
Trond Myklebustc12e87f2006-03-13 21:20:47 -08002243 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244}
2245
Manoj Naik6b97fd32006-06-09 09:34:29 -04002246/*
2247 * Get locations and (maybe) other attributes of a referral.
2248 * Note that we'll actually follow the referral later when
2249 * we detect fsid mismatch in inode revalidation
2250 */
Trond Myklebust56659e92007-07-17 21:52:39 -04002251static int nfs4_get_referral(struct inode *dir, const struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04002252{
2253 int status = -ENOMEM;
2254 struct page *page = NULL;
2255 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04002256
2257 page = alloc_page(GFP_KERNEL);
2258 if (page == NULL)
2259 goto out;
2260 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
2261 if (locations == NULL)
2262 goto out;
2263
Trond Myklebustc228fd32007-01-13 02:28:11 -05002264 status = nfs4_proc_fs_locations(dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002265 if (status != 0)
2266 goto out;
2267 /* Make sure server returned a different fsid for the referral */
2268 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -07002269 dprintk("%s: server did not return a different fsid for a referral at %s\n", __func__, name->name);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002270 status = -EIO;
2271 goto out;
2272 }
2273
2274 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
2275 fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL;
2276 if (!fattr->mode)
2277 fattr->mode = S_IFDIR;
2278 memset(fhandle, 0, sizeof(struct nfs_fh));
2279out:
2280 if (page)
2281 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04002282 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002283 return status;
2284}
2285
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2287{
2288 struct nfs4_getattr_arg args = {
2289 .fh = fhandle,
2290 .bitmask = server->attr_bitmask,
2291 };
2292 struct nfs4_getattr_res res = {
2293 .fattr = fattr,
2294 .server = server,
2295 };
2296 struct rpc_message msg = {
2297 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
2298 .rpc_argp = &args,
2299 .rpc_resp = &res,
2300 };
2301
Trond Myklebust0e574af2005-10-27 22:12:38 -04002302 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002303 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304}
2305
2306static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr)
2307{
2308 struct nfs4_exception exception = { };
2309 int err;
2310 do {
2311 err = nfs4_handle_exception(server,
2312 _nfs4_proc_getattr(server, fhandle, fattr),
2313 &exception);
2314 } while (exception.retry);
2315 return err;
2316}
2317
2318/*
2319 * The file is not closed if it is opened due to the a request to change
2320 * the size of the file. The open call will not be needed once the
2321 * VFS layer lookup-intents are implemented.
2322 *
2323 * Close is called when the inode is destroyed.
2324 * If we haven't opened the file for O_WRONLY, we
2325 * need to in the size_change case to obtain a stateid.
2326 *
2327 * Got race?
2328 * Because OPEN is always done by name in nfsv4, it is
2329 * possible that we opened a different file by the same
2330 * name. We can recognize this race condition, but we
2331 * can't do anything about it besides returning an error.
2332 *
2333 * This will be fixed with VFS changes (lookup-intent).
2334 */
2335static int
2336nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
2337 struct iattr *sattr)
2338{
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002339 struct inode *inode = dentry->d_inode;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002340 struct rpc_cred *cred = NULL;
Trond Myklebustd5308382005-11-04 15:33:38 -05002341 struct nfs4_state *state = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 int status;
2343
Trond Myklebust0e574af2005-10-27 22:12:38 -04002344 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
Trond Myklebustd5308382005-11-04 15:33:38 -05002346 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002347 if (sattr->ia_valid & ATTR_FILE) {
2348 struct nfs_open_context *ctx;
Trond Myklebust08e9eac2005-06-22 17:16:29 +00002349
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002350 ctx = nfs_file_open_context(sattr->ia_file);
Neil Brown504e5182008-10-16 14:15:16 +11002351 if (ctx) {
2352 cred = ctx->cred;
2353 state = ctx->state;
2354 }
Trond Myklebust659bfcd2008-06-10 19:39:41 -04002355 }
2356
2357 status = nfs4_do_setattr(inode, cred, fattr, sattr, state);
Trond Myklebust65e43082005-08-16 11:49:44 -04002358 if (status == 0)
2359 nfs_setattr_update_inode(inode, sattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 return status;
2361}
2362
Bryan Schumaker7c513052011-03-24 17:12:24 +00002363static int _nfs4_proc_lookupfh(struct rpc_clnt *clnt, struct nfs_server *server,
2364 const struct nfs_fh *dirfh, const struct qstr *name,
2365 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
David Howells2b3de442006-08-22 20:06:09 -04002366{
2367 int status;
2368 struct nfs4_lookup_arg args = {
2369 .bitmask = server->attr_bitmask,
2370 .dir_fh = dirfh,
2371 .name = name,
2372 };
2373 struct nfs4_lookup_res res = {
2374 .server = server,
2375 .fattr = fattr,
2376 .fh = fhandle,
2377 };
2378 struct rpc_message msg = {
2379 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
2380 .rpc_argp = &args,
2381 .rpc_resp = &res,
2382 };
2383
2384 nfs_fattr_init(fattr);
2385
2386 dprintk("NFS call lookupfh %s\n", name->name);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002387 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
David Howells2b3de442006-08-22 20:06:09 -04002388 dprintk("NFS reply lookupfh: %d\n", status);
David Howells2b3de442006-08-22 20:06:09 -04002389 return status;
2390}
2391
2392static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh,
2393 struct qstr *name, struct nfs_fh *fhandle,
2394 struct nfs_fattr *fattr)
2395{
2396 struct nfs4_exception exception = { };
2397 int err;
2398 do {
Bryan Schumaker7c513052011-03-24 17:12:24 +00002399 err = _nfs4_proc_lookupfh(server->client, server, dirfh, name, fhandle, fattr);
Trond Myklebust4e56e082007-07-01 18:13:52 -04002400 /* FIXME: !!!! */
2401 if (err == -NFS4ERR_MOVED) {
2402 err = -EREMOTE;
2403 break;
2404 }
2405 err = nfs4_handle_exception(server, err, &exception);
David Howells2b3de442006-08-22 20:06:09 -04002406 } while (exception.retry);
2407 return err;
2408}
2409
Bryan Schumaker7c513052011-03-24 17:12:24 +00002410static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
2411 const struct qstr *name, struct nfs_fh *fhandle,
2412 struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413{
Trond Myklebust4e56e082007-07-01 18:13:52 -04002414 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415
2416 dprintk("NFS call lookup %s\n", name->name);
Bryan Schumaker7c513052011-03-24 17:12:24 +00002417 status = _nfs4_proc_lookupfh(clnt, NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04002418 if (status == -NFS4ERR_MOVED)
2419 status = nfs4_get_referral(dir, name, fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 dprintk("NFS reply lookup: %d\n", status);
2421 return status;
2422}
2423
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002424void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr, struct nfs_fh *fh)
2425{
2426 memset(fh, 0, sizeof(struct nfs_fh));
2427 fattr->fsid.major = 1;
2428 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
2429 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_FSID | NFS_ATTR_FATTR_MOUNTPOINT;
2430 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
2431 fattr->nlink = 2;
2432}
2433
Bryan Schumaker7c513052011-03-24 17:12:24 +00002434static int nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir, struct qstr *name,
2435 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436{
2437 struct nfs4_exception exception = { };
2438 int err;
2439 do {
2440 err = nfs4_handle_exception(NFS_SERVER(dir),
Bryan Schumaker7c513052011-03-24 17:12:24 +00002441 _nfs4_proc_lookup(clnt, dir, name, fhandle, fattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 &exception);
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00002443 if (err == -EPERM)
2444 nfs_fixup_secinfo_attributes(fattr, fhandle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 } while (exception.retry);
2446 return err;
2447}
2448
2449static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2450{
Trond Myklebust76b32992007-08-10 17:45:11 -04002451 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 struct nfs4_accessargs args = {
2453 .fh = NFS_FH(inode),
Trond Myklebust76b32992007-08-10 17:45:11 -04002454 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 };
Trond Myklebust76b32992007-08-10 17:45:11 -04002456 struct nfs4_accessres res = {
2457 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04002458 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 struct rpc_message msg = {
2460 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
2461 .rpc_argp = &args,
2462 .rpc_resp = &res,
2463 .rpc_cred = entry->cred,
2464 };
2465 int mode = entry->mask;
2466 int status;
2467
2468 /*
2469 * Determine which access bits we want to ask for...
2470 */
2471 if (mode & MAY_READ)
2472 args.access |= NFS4_ACCESS_READ;
2473 if (S_ISDIR(inode->i_mode)) {
2474 if (mode & MAY_WRITE)
2475 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE;
2476 if (mode & MAY_EXEC)
2477 args.access |= NFS4_ACCESS_LOOKUP;
2478 } else {
2479 if (mode & MAY_WRITE)
2480 args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND;
2481 if (mode & MAY_EXEC)
2482 args.access |= NFS4_ACCESS_EXECUTE;
2483 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002484
2485 res.fattr = nfs_alloc_fattr();
2486 if (res.fattr == NULL)
2487 return -ENOMEM;
2488
Bryan Schumaker7c513052011-03-24 17:12:24 +00002489 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 if (!status) {
2491 entry->mask = 0;
2492 if (res.access & NFS4_ACCESS_READ)
2493 entry->mask |= MAY_READ;
2494 if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE))
2495 entry->mask |= MAY_WRITE;
2496 if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE))
2497 entry->mask |= MAY_EXEC;
Trond Myklebustc407d412010-04-16 16:22:48 -04002498 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 }
Trond Myklebustc407d412010-04-16 16:22:48 -04002500 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 return status;
2502}
2503
2504static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
2505{
2506 struct nfs4_exception exception = { };
2507 int err;
2508 do {
2509 err = nfs4_handle_exception(NFS_SERVER(inode),
2510 _nfs4_proc_access(inode, entry),
2511 &exception);
2512 } while (exception.retry);
2513 return err;
2514}
2515
2516/*
2517 * TODO: For the time being, we don't try to get any attributes
2518 * along with any of the zero-copy operations READ, READDIR,
2519 * READLINK, WRITE.
2520 *
2521 * In the case of the first three, we want to put the GETATTR
2522 * after the read-type operation -- this is because it is hard
2523 * to predict the length of a GETATTR response in v4, and thus
2524 * align the READ data correctly. This means that the GETATTR
2525 * may end up partially falling into the page cache, and we should
2526 * shift it into the 'tail' of the xdr_buf before processing.
2527 * To do this efficiently, we need to know the total length
2528 * of data received, which doesn't seem to be available outside
2529 * of the RPC layer.
2530 *
2531 * In the case of WRITE, we also want to put the GETATTR after
2532 * the operation -- in this case because we want to make sure
2533 * we get the post-operation mtime and size. This means that
2534 * we can't use xdr_encode_pages() as written: we need a variant
2535 * of it which would leave room in the 'tail' iovec.
2536 *
2537 * Both of these changes to the XDR layer would in fact be quite
2538 * minor, but I decided to leave them for a subsequent patch.
2539 */
2540static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
2541 unsigned int pgbase, unsigned int pglen)
2542{
2543 struct nfs4_readlink args = {
2544 .fh = NFS_FH(inode),
2545 .pgbase = pgbase,
2546 .pglen = pglen,
2547 .pages = &page,
2548 };
Benny Halevyf50c7002009-04-01 09:21:55 -04002549 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 struct rpc_message msg = {
2551 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
2552 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04002553 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 };
2555
Bryan Schumaker7c513052011-03-24 17:12:24 +00002556 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 -07002557}
2558
2559static int nfs4_proc_readlink(struct inode *inode, struct page *page,
2560 unsigned int pgbase, unsigned int pglen)
2561{
2562 struct nfs4_exception exception = { };
2563 int err;
2564 do {
2565 err = nfs4_handle_exception(NFS_SERVER(inode),
2566 _nfs4_proc_readlink(inode, page, pgbase, pglen),
2567 &exception);
2568 } while (exception.retry);
2569 return err;
2570}
2571
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572/*
2573 * Got race?
2574 * We will need to arrange for the VFS layer to provide an atomic open.
2575 * Until then, this create/open method is prone to inefficiency and race
2576 * conditions due to the lookup, create, and open VFS calls from sys_open()
2577 * placed on the wire.
2578 *
2579 * Given the above sorry state of affairs, I'm simply sending an OPEN.
2580 * The file will be opened again in the subsequent VFS open call
2581 * (nfs4_proc_file_open).
2582 *
2583 * The open for read will just hang around to be used by any process that
2584 * opens the file O_RDONLY. This will all be resolved with the VFS changes.
2585 */
2586
2587static int
2588nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002589 int flags, struct nfs_open_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590{
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002591 struct path my_path = {
Trond Myklebustad389da2007-06-05 12:30:00 -04002592 .dentry = dentry,
2593 };
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002594 struct path *path = &my_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 struct nfs4_state *state;
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002596 struct rpc_cred *cred = NULL;
2597 fmode_t fmode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 int status = 0;
2599
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002600 if (ctx != NULL) {
2601 cred = ctx->cred;
2602 path = &ctx->path;
2603 fmode = ctx->mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 }
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002605 sattr->ia_mode &= ~current_umask();
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002606 state = nfs4_do_open(dir, path, fmode, flags, sattr, cred);
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002607 d_drop(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 if (IS_ERR(state)) {
2609 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002610 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 }
Trond Myklebustd4d9cdc2007-10-02 18:38:53 -04002612 d_add(dentry, igrab(state->inode));
Trond Myklebustd75340c2007-10-01 21:42:01 -04002613 nfs_set_verifier(dentry, nfs_save_change_attribute(dir));
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002614 if (ctx != NULL)
2615 ctx->state = state;
Trond Myklebust02a913a2005-10-18 14:20:17 -07002616 else
Trond Myklebustc0204fd2010-09-17 10:56:51 -04002617 nfs4_close_sync(path, state, fmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618out:
2619 return status;
2620}
2621
2622static int _nfs4_proc_remove(struct inode *dir, struct qstr *name)
2623{
Trond Myklebust16e42952005-10-27 22:12:44 -04002624 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002625 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 .fh = NFS_FH(dir),
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002627 .name.len = name->len,
2628 .name.name = name->name,
Trond Myklebust16e42952005-10-27 22:12:44 -04002629 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002631 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04002632 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04002633 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04002635 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
2636 .rpc_argp = &args,
2637 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 };
Trond Myklebustd3468902010-04-16 16:22:50 -04002639 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
Trond Myklebustd3468902010-04-16 16:22:50 -04002641 res.dir_attr = nfs_alloc_fattr();
2642 if (res.dir_attr == NULL)
2643 goto out;
2644
Bryan Schumaker7c513052011-03-24 17:12:24 +00002645 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust16e42952005-10-27 22:12:44 -04002646 if (status == 0) {
2647 update_changeattr(dir, &res.cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002648 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust16e42952005-10-27 22:12:44 -04002649 }
Trond Myklebustd3468902010-04-16 16:22:50 -04002650 nfs_free_fattr(res.dir_attr);
2651out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 return status;
2653}
2654
2655static int nfs4_proc_remove(struct inode *dir, struct qstr *name)
2656{
2657 struct nfs4_exception exception = { };
2658 int err;
2659 do {
2660 err = nfs4_handle_exception(NFS_SERVER(dir),
2661 _nfs4_proc_remove(dir, name),
2662 &exception);
2663 } while (exception.retry);
2664 return err;
2665}
2666
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002667static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002669 struct nfs_server *server = NFS_SERVER(dir);
2670 struct nfs_removeargs *args = msg->rpc_argp;
2671 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
Trond Myklebusta65318b2009-03-11 14:10:28 -04002673 args->bitmask = server->cache_consistency_bitmask;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002674 res->server = server;
Benny Halevydfb4f3092010-09-24 09:17:01 -04002675 res->seq_res.sr_slot = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677}
2678
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002679static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002681 struct nfs_removeres *res = task->tk_msg.rpc_resp;
2682
Trond Myklebust14516c32010-07-31 14:29:06 -04002683 if (!nfs4_sequence_done(task, &res->seq_res))
2684 return 0;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05002685 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002686 return 0;
2687 update_changeattr(dir, &res->cinfo);
Trond Myklebustd3468902010-04-16 16:22:50 -04002688 nfs_post_op_update_inode(dir, res->dir_attr);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04002689 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690}
2691
Jeff Laytond3d41522010-09-17 17:31:57 -04002692static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
2693{
2694 struct nfs_server *server = NFS_SERVER(dir);
2695 struct nfs_renameargs *arg = msg->rpc_argp;
2696 struct nfs_renameres *res = msg->rpc_resp;
2697
2698 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
2699 arg->bitmask = server->attr_bitmask;
2700 res->server = server;
2701}
2702
2703static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
2704 struct inode *new_dir)
2705{
2706 struct nfs_renameres *res = task->tk_msg.rpc_resp;
2707
2708 if (!nfs4_sequence_done(task, &res->seq_res))
2709 return 0;
2710 if (nfs4_async_handle_error(task, res->server, NULL) == -EAGAIN)
2711 return 0;
2712
2713 update_changeattr(old_dir, &res->old_cinfo);
2714 nfs_post_op_update_inode(old_dir, res->old_fattr);
2715 update_changeattr(new_dir, &res->new_cinfo);
2716 nfs_post_op_update_inode(new_dir, res->new_fattr);
2717 return 1;
2718}
2719
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2721 struct inode *new_dir, struct qstr *new_name)
2722{
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002723 struct nfs_server *server = NFS_SERVER(old_dir);
Jeff Layton920769f2010-09-17 17:30:25 -04002724 struct nfs_renameargs arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 .old_dir = NFS_FH(old_dir),
2726 .new_dir = NFS_FH(new_dir),
2727 .old_name = old_name,
2728 .new_name = new_name,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002729 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 };
Jeff Laytone8582a82010-09-17 17:31:06 -04002731 struct nfs_renameres res = {
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002732 .server = server,
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002733 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 struct rpc_message msg = {
2735 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME],
2736 .rpc_argp = &arg,
2737 .rpc_resp = &res,
2738 };
Trond Myklebust011fff72010-04-16 16:22:49 -04002739 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
Trond Myklebust011fff72010-04-16 16:22:49 -04002741 res.old_fattr = nfs_alloc_fattr();
2742 res.new_fattr = nfs_alloc_fattr();
2743 if (res.old_fattr == NULL || res.new_fattr == NULL)
2744 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745
Bryan Schumaker7c513052011-03-24 17:12:24 +00002746 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 if (!status) {
2748 update_changeattr(old_dir, &res.old_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002749 nfs_post_op_update_inode(old_dir, res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 update_changeattr(new_dir, &res.new_cinfo);
Trond Myklebust6caf2c82005-10-27 22:12:43 -04002751 nfs_post_op_update_inode(new_dir, res.new_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 }
Trond Myklebust011fff72010-04-16 16:22:49 -04002753out:
2754 nfs_free_fattr(res.new_fattr);
2755 nfs_free_fattr(res.old_fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 return status;
2757}
2758
2759static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name,
2760 struct inode *new_dir, struct qstr *new_name)
2761{
2762 struct nfs4_exception exception = { };
2763 int err;
2764 do {
2765 err = nfs4_handle_exception(NFS_SERVER(old_dir),
2766 _nfs4_proc_rename(old_dir, old_name,
2767 new_dir, new_name),
2768 &exception);
2769 } while (exception.retry);
2770 return err;
2771}
2772
2773static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2774{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002775 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 struct nfs4_link_arg arg = {
2777 .fh = NFS_FH(inode),
2778 .dir_fh = NFS_FH(dir),
2779 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002780 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002782 struct nfs4_link_res res = {
2783 .server = server,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002784 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 struct rpc_message msg = {
2786 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
2787 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002788 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 };
Trond Myklebust136f2622010-04-16 16:22:49 -04002790 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791
Trond Myklebust136f2622010-04-16 16:22:49 -04002792 res.fattr = nfs_alloc_fattr();
2793 res.dir_attr = nfs_alloc_fattr();
2794 if (res.fattr == NULL || res.dir_attr == NULL)
2795 goto out;
2796
Bryan Schumaker7c513052011-03-24 17:12:24 +00002797 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002798 if (!status) {
2799 update_changeattr(dir, &res.cinfo);
2800 nfs_post_op_update_inode(dir, res.dir_attr);
Trond Myklebust73a3d072006-05-25 01:40:47 -04002801 nfs_post_op_update_inode(inode, res.fattr);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04002802 }
Trond Myklebust136f2622010-04-16 16:22:49 -04002803out:
2804 nfs_free_fattr(res.dir_attr);
2805 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 return status;
2807}
2808
2809static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name)
2810{
2811 struct nfs4_exception exception = { };
2812 int err;
2813 do {
2814 err = nfs4_handle_exception(NFS_SERVER(inode),
2815 _nfs4_proc_link(inode, dir, name),
2816 &exception);
2817 } while (exception.retry);
2818 return err;
2819}
2820
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002821struct nfs4_createdata {
2822 struct rpc_message msg;
2823 struct nfs4_create_arg arg;
2824 struct nfs4_create_res res;
2825 struct nfs_fh fh;
2826 struct nfs_fattr fattr;
2827 struct nfs_fattr dir_fattr;
2828};
2829
2830static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
2831 struct qstr *name, struct iattr *sattr, u32 ftype)
2832{
2833 struct nfs4_createdata *data;
2834
2835 data = kzalloc(sizeof(*data), GFP_KERNEL);
2836 if (data != NULL) {
2837 struct nfs_server *server = NFS_SERVER(dir);
2838
2839 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
2840 data->msg.rpc_argp = &data->arg;
2841 data->msg.rpc_resp = &data->res;
2842 data->arg.dir_fh = NFS_FH(dir);
2843 data->arg.server = server;
2844 data->arg.name = name;
2845 data->arg.attrs = sattr;
2846 data->arg.ftype = ftype;
2847 data->arg.bitmask = server->attr_bitmask;
2848 data->res.server = server;
2849 data->res.fh = &data->fh;
2850 data->res.fattr = &data->fattr;
2851 data->res.dir_fattr = &data->dir_fattr;
2852 nfs_fattr_init(data->res.fattr);
2853 nfs_fattr_init(data->res.dir_fattr);
2854 }
2855 return data;
2856}
2857
2858static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
2859{
Bryan Schumaker7c513052011-03-24 17:12:24 +00002860 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00002861 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002862 if (status == 0) {
2863 update_changeattr(dir, &data->res.dir_cinfo);
2864 nfs_post_op_update_inode(dir, data->res.dir_fattr);
2865 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr);
2866 }
2867 return status;
2868}
2869
2870static void nfs4_free_createdata(struct nfs4_createdata *data)
2871{
2872 kfree(data);
2873}
2874
Chuck Lever4f390c12006-08-22 20:06:22 -04002875static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002876 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002878 struct nfs4_createdata *data;
2879 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880
Chuck Lever94a6d752006-08-22 20:06:23 -04002881 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002882 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04002883
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002884 status = -ENOMEM;
2885 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
2886 if (data == NULL)
2887 goto out;
2888
2889 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
2890 data->arg.u.symlink.pages = &page;
2891 data->arg.u.symlink.len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002893 status = nfs4_do_create(dir, dentry, data);
2894
2895 nfs4_free_createdata(data);
2896out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 return status;
2898}
2899
Chuck Lever4f390c12006-08-22 20:06:22 -04002900static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04002901 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902{
2903 struct nfs4_exception exception = { };
2904 int err;
2905 do {
2906 err = nfs4_handle_exception(NFS_SERVER(dir),
Chuck Lever94a6d752006-08-22 20:06:23 -04002907 _nfs4_proc_symlink(dir, dentry, page,
2908 len, sattr),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 &exception);
2910 } while (exception.retry);
2911 return err;
2912}
2913
2914static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2915 struct iattr *sattr)
2916{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002917 struct nfs4_createdata *data;
2918 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919
Trond Myklebust57dc9a52008-06-20 15:35:32 -04002920 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
2921 if (data == NULL)
2922 goto out;
2923
2924 status = nfs4_do_create(dir, dentry, data);
2925
2926 nfs4_free_createdata(data);
2927out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 return status;
2929}
2930
2931static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
2932 struct iattr *sattr)
2933{
2934 struct nfs4_exception exception = { };
2935 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00002936
2937 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 do {
2939 err = nfs4_handle_exception(NFS_SERVER(dir),
2940 _nfs4_proc_mkdir(dir, dentry, sattr),
2941 &exception);
2942 } while (exception.retry);
2943 return err;
2944}
2945
2946static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002947 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948{
2949 struct inode *dir = dentry->d_inode;
2950 struct nfs4_readdir_arg args = {
2951 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002952 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 .pgbase = 0,
2954 .count = count,
Trond Myklebust96d25e52009-11-11 16:15:42 +09002955 .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04002956 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 };
2958 struct nfs4_readdir_res res;
2959 struct rpc_message msg = {
2960 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
2961 .rpc_argp = &args,
2962 .rpc_resp = &res,
2963 .rpc_cred = cred,
2964 };
2965 int status;
2966
Harvey Harrison3110ff82008-05-02 13:42:44 -07002967 dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __func__,
Trond Myklebusteadf4592005-06-22 17:16:39 +00002968 dentry->d_parent->d_name.name,
2969 dentry->d_name.name,
2970 (unsigned long long)cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args);
2972 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00002973 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 -05002974 if (status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05002976 status += args.pgbase;
2977 }
Trond Myklebustc4812992007-09-28 17:11:45 -04002978
2979 nfs_invalidate_atime(dir);
2980
Harvey Harrison3110ff82008-05-02 13:42:44 -07002981 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 return status;
2983}
2984
2985static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002986 u64 cookie, struct page **pages, unsigned int count, int plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987{
2988 struct nfs4_exception exception = { };
2989 int err;
2990 do {
2991 err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode),
2992 _nfs4_proc_readdir(dentry, cred, cookie,
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04002993 pages, count, plus),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 &exception);
2995 } while (exception.retry);
2996 return err;
2997}
2998
2999static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3000 struct iattr *sattr, dev_t rdev)
3001{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003002 struct nfs4_createdata *data;
3003 int mode = sattr->ia_mode;
3004 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005
3006 BUG_ON(!(sattr->ia_valid & ATTR_MODE));
3007 BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode));
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003008
3009 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
3010 if (data == NULL)
3011 goto out;
3012
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003014 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003016 data->arg.ftype = NF4BLK;
3017 data->arg.u.device.specdata1 = MAJOR(rdev);
3018 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 }
3020 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003021 data->arg.ftype = NF4CHR;
3022 data->arg.u.device.specdata1 = MAJOR(rdev);
3023 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025
Trond Myklebust57dc9a52008-06-20 15:35:32 -04003026 status = nfs4_do_create(dir, dentry, data);
3027
3028 nfs4_free_createdata(data);
3029out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 return status;
3031}
3032
3033static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
3034 struct iattr *sattr, dev_t rdev)
3035{
3036 struct nfs4_exception exception = { };
3037 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00003038
3039 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 do {
3041 err = nfs4_handle_exception(NFS_SERVER(dir),
3042 _nfs4_proc_mknod(dir, dentry, sattr, rdev),
3043 &exception);
3044 } while (exception.retry);
3045 return err;
3046}
3047
3048static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
3049 struct nfs_fsstat *fsstat)
3050{
3051 struct nfs4_statfs_arg args = {
3052 .fh = fhandle,
3053 .bitmask = server->attr_bitmask,
3054 };
Benny Halevy24ad1482009-04-01 09:21:56 -04003055 struct nfs4_statfs_res res = {
3056 .fsstat = fsstat,
3057 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 struct rpc_message msg = {
3059 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
3060 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04003061 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 };
3063
Trond Myklebust0e574af2005-10-27 22:12:38 -04003064 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003065 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066}
3067
3068static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
3069{
3070 struct nfs4_exception exception = { };
3071 int err;
3072 do {
3073 err = nfs4_handle_exception(server,
3074 _nfs4_proc_statfs(server, fhandle, fsstat),
3075 &exception);
3076 } while (exception.retry);
3077 return err;
3078}
3079
3080static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
3081 struct nfs_fsinfo *fsinfo)
3082{
3083 struct nfs4_fsinfo_arg args = {
3084 .fh = fhandle,
3085 .bitmask = server->attr_bitmask,
3086 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04003087 struct nfs4_fsinfo_res res = {
3088 .fsinfo = fsinfo,
3089 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 struct rpc_message msg = {
3091 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
3092 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04003093 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 };
3095
Bryan Schumaker7c513052011-03-24 17:12:24 +00003096 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097}
3098
3099static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3100{
3101 struct nfs4_exception exception = { };
3102 int err;
3103
3104 do {
3105 err = nfs4_handle_exception(server,
3106 _nfs4_do_fsinfo(server, fhandle, fsinfo),
3107 &exception);
3108 } while (exception.retry);
3109 return err;
3110}
3111
3112static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
3113{
Trond Myklebust0e574af2005-10-27 22:12:38 -04003114 nfs_fattr_init(fsinfo->fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 return nfs4_do_fsinfo(server, fhandle, fsinfo);
3116}
3117
3118static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3119 struct nfs_pathconf *pathconf)
3120{
3121 struct nfs4_pathconf_arg args = {
3122 .fh = fhandle,
3123 .bitmask = server->attr_bitmask,
3124 };
Benny Halevyd45b2982009-04-01 09:21:58 -04003125 struct nfs4_pathconf_res res = {
3126 .pathconf = pathconf,
3127 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 struct rpc_message msg = {
3129 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
3130 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04003131 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 };
3133
3134 /* None of the pathconf attributes are mandatory to implement */
3135 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
3136 memset(pathconf, 0, sizeof(*pathconf));
3137 return 0;
3138 }
3139
Trond Myklebust0e574af2005-10-27 22:12:38 -04003140 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003141 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142}
3143
3144static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
3145 struct nfs_pathconf *pathconf)
3146{
3147 struct nfs4_exception exception = { };
3148 int err;
3149
3150 do {
3151 err = nfs4_handle_exception(server,
3152 _nfs4_proc_pathconf(server, fhandle, pathconf),
3153 &exception);
3154 } while (exception.retry);
3155 return err;
3156}
3157
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003158static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_read_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159{
Trond Myklebustec06c092006-03-20 13:44:27 -05003160 struct nfs_server *server = NFS_SERVER(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003162 if (nfs4_async_handle_error(task, server, data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003163 nfs_restart_rpc(task, server->nfs_client);
Trond Myklebustec06c092006-03-20 13:44:27 -05003164 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 }
Trond Myklebust8850df92007-09-28 17:20:07 -04003166
3167 nfs_invalidate_atime(data->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 if (task->tk_status > 0)
Trond Myklebustec06c092006-03-20 13:44:27 -05003169 renew_lease(server, data->timestamp);
3170 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171}
3172
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003173static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data)
3174{
3175
3176 dprintk("--> %s\n", __func__);
3177
3178 if (!nfs4_sequence_done(task, &data->res.seq_res))
3179 return -EAGAIN;
3180
3181 return data->read_done_cb(task, data);
3182}
3183
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003184static void nfs4_proc_read_setup(struct nfs_read_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 data->timestamp = jiffies;
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003187 data->read_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003188 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189}
3190
Andy Adamsoncbdabc7f2011-03-01 01:34:20 +00003191/* Reset the the nfs_read_data to send the read to the MDS. */
3192void nfs4_reset_read(struct rpc_task *task, struct nfs_read_data *data)
3193{
3194 dprintk("%s Reset task for i/o through\n", __func__);
3195 put_lseg(data->lseg);
3196 data->lseg = NULL;
3197 /* offsets will differ in the dense stripe case */
3198 data->args.offset = data->mds_offset;
3199 data->ds_clp = NULL;
3200 data->args.fh = NFS_FH(data->inode);
3201 data->read_done_cb = nfs4_read_done_cb;
3202 task->tk_ops = data->mds_ops;
3203 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3204}
3205EXPORT_SYMBOL_GPL(nfs4_reset_read);
3206
Fred Isamanb029bc92011-03-03 15:13:42 +00003207static int nfs4_write_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 struct inode *inode = data->inode;
3210
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003211 if (nfs4_async_handle_error(task, NFS_SERVER(inode), data->args.context->state) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003212 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003213 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003215 if (task->tk_status >= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 renew_lease(NFS_SERVER(inode), data->timestamp);
Trond Myklebust70ca8852007-09-30 15:21:24 -04003217 nfs_post_op_update_inode_force_wcc(inode, data->res.fattr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003218 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05003219 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220}
3221
Fred Isamanb029bc92011-03-03 15:13:42 +00003222static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data)
3223{
3224 if (!nfs4_sequence_done(task, &data->res.seq_res))
3225 return -EAGAIN;
3226 return data->write_done_cb(task, data);
3227}
3228
Fred Isamana69aef12011-03-03 15:13:47 +00003229/* Reset the the nfs_write_data to send the write to the MDS. */
3230void nfs4_reset_write(struct rpc_task *task, struct nfs_write_data *data)
3231{
3232 dprintk("%s Reset task for i/o through\n", __func__);
3233 put_lseg(data->lseg);
3234 data->lseg = NULL;
3235 data->ds_clp = NULL;
3236 data->write_done_cb = nfs4_write_done_cb;
3237 data->args.fh = NFS_FH(data->inode);
3238 data->args.bitmask = data->res.server->cache_consistency_bitmask;
3239 data->args.offset = data->mds_offset;
3240 data->res.fattr = &data->fattr;
3241 task->tk_ops = data->mds_ops;
3242 rpc_task_reset_client(task, NFS_CLIENT(data->inode));
3243}
3244EXPORT_SYMBOL_GPL(nfs4_reset_write);
3245
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003246static void nfs4_proc_write_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247{
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003248 struct nfs_server *server = NFS_SERVER(data->inode);
3249
Fred Isaman7ffd1062011-03-03 15:13:46 +00003250 if (data->lseg) {
3251 data->args.bitmask = NULL;
3252 data->res.fattr = NULL;
3253 } else
3254 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isamanb029bc92011-03-03 15:13:42 +00003255 if (!data->write_done_cb)
3256 data->write_done_cb = nfs4_write_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003257 data->res.server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 data->timestamp = jiffies;
3259
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003260 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261}
3262
Fred Isaman5f452432011-03-23 13:27:46 +00003263static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_write_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04003266
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003267 if (nfs4_async_handle_error(task, NFS_SERVER(inode), NULL) == -EAGAIN) {
Trond Myklebust0110ee12009-12-07 09:00:24 -05003268 nfs_restart_rpc(task, NFS_SERVER(inode)->nfs_client);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003269 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 }
Trond Myklebust9e08a3c2007-10-08 14:10:31 -04003271 nfs_refresh_inode(inode, data->res.fattr);
Trond Myklebust788e7a82006-03-20 13:44:27 -05003272 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273}
3274
Fred Isaman5f452432011-03-23 13:27:46 +00003275static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data)
3276{
3277 if (!nfs4_sequence_done(task, &data->res.seq_res))
3278 return -EAGAIN;
3279 return data->write_done_cb(task, data);
3280}
3281
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003282static void nfs4_proc_commit_setup(struct nfs_write_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283{
Trond Myklebust788e7a82006-03-20 13:44:27 -05003284 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00003285
3286 if (data->lseg) {
3287 data->args.bitmask = NULL;
3288 data->res.fattr = NULL;
3289 } else
3290 data->args.bitmask = server->cache_consistency_bitmask;
Fred Isaman5f452432011-03-23 13:27:46 +00003291 if (!data->write_done_cb)
3292 data->write_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04003293 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04003294 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295}
3296
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003297struct nfs4_renewdata {
3298 struct nfs_client *client;
3299 unsigned long timestamp;
3300};
3301
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302/*
3303 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
3304 * standalone procedure for queueing an asynchronous RENEW.
3305 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003306static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003307{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003308 struct nfs4_renewdata *data = calldata;
3309 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003310
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003311 if (atomic_read(&clp->cl_count) > 1)
3312 nfs4_schedule_state_renewal(clp);
3313 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003314 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003315}
3316
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003317static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003319 struct nfs4_renewdata *data = calldata;
3320 struct nfs_client *clp = data->client;
3321 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322
3323 if (task->tk_status < 0) {
Trond Myklebust95baa252009-05-26 14:51:00 -04003324 /* Unless we're shutting down, schedule state recovery! */
3325 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) != 0)
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003326 nfs4_schedule_lease_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 return;
3328 }
Trond Myklebust452e9352010-07-31 14:29:06 -04003329 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330}
3331
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003332static const struct rpc_call_ops nfs4_renew_ops = {
3333 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08003334 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003335};
3336
David Howellsadfa6f92006-08-22 20:06:08 -04003337int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338{
3339 struct rpc_message msg = {
3340 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3341 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003342 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003344 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345
Alexandros Batsakis0851de062010-02-05 03:45:06 -08003346 if (!atomic_inc_not_zero(&clp->cl_count))
3347 return -EIO;
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003348 data = kmalloc(sizeof(*data), GFP_KERNEL);
3349 if (data == NULL)
3350 return -ENOMEM;
3351 data->client = clp;
3352 data->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04003354 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355}
3356
David Howellsadfa6f92006-08-22 20:06:08 -04003357int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358{
3359 struct rpc_message msg = {
3360 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
3361 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003362 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 };
3364 unsigned long now = jiffies;
3365 int status;
3366
3367 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3368 if (status < 0)
3369 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04003370 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 return 0;
3372}
3373
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003374static inline int nfs4_server_supports_acls(struct nfs_server *server)
3375{
3376 return (server->caps & NFS_CAP_ACLS)
3377 && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
3378 && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL);
3379}
3380
3381/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that
3382 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on
3383 * the stack.
3384 */
3385#define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT)
3386
3387static void buf_to_pages(const void *buf, size_t buflen,
3388 struct page **pages, unsigned int *pgbase)
3389{
3390 const void *p = buf;
3391
3392 *pgbase = offset_in_page(buf);
3393 p -= *pgbase;
3394 while (p < buf + buflen) {
3395 *(pages++) = virt_to_page(p);
3396 p += PAGE_CACHE_SIZE;
3397 }
3398}
3399
Neil Hormane9e3d722011-03-04 19:26:03 -05003400static int buf_to_pages_noslab(const void *buf, size_t buflen,
3401 struct page **pages, unsigned int *pgbase)
3402{
3403 struct page *newpage, **spages;
3404 int rc = 0;
3405 size_t len;
3406 spages = pages;
3407
3408 do {
Jovi Zhang43b7c3f2011-03-02 23:19:37 +00003409 len = min_t(size_t, PAGE_CACHE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05003410 newpage = alloc_page(GFP_KERNEL);
3411
3412 if (newpage == NULL)
3413 goto unwind;
3414 memcpy(page_address(newpage), buf, len);
3415 buf += len;
3416 buflen -= len;
3417 *pages++ = newpage;
3418 rc++;
3419 } while (buflen != 0);
3420
3421 return rc;
3422
3423unwind:
3424 for(; rc > 0; rc--)
3425 __free_page(spages[rc-1]);
3426 return -ENOMEM;
3427}
3428
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003429struct nfs4_cached_acl {
3430 int cached;
3431 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00003432 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003433};
3434
3435static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003436{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003437 struct nfs_inode *nfsi = NFS_I(inode);
3438
3439 spin_lock(&inode->i_lock);
3440 kfree(nfsi->nfs4_acl);
3441 nfsi->nfs4_acl = acl;
3442 spin_unlock(&inode->i_lock);
3443}
3444
3445static void nfs4_zap_acl_attr(struct inode *inode)
3446{
3447 nfs4_set_cached_acl(inode, NULL);
3448}
3449
3450static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
3451{
3452 struct nfs_inode *nfsi = NFS_I(inode);
3453 struct nfs4_cached_acl *acl;
3454 int ret = -ENOENT;
3455
3456 spin_lock(&inode->i_lock);
3457 acl = nfsi->nfs4_acl;
3458 if (acl == NULL)
3459 goto out;
3460 if (buf == NULL) /* user is just asking for length */
3461 goto out_len;
3462 if (acl->cached == 0)
3463 goto out;
3464 ret = -ERANGE; /* see getxattr(2) man page */
3465 if (acl->len > buflen)
3466 goto out;
3467 memcpy(buf, acl->data, acl->len);
3468out_len:
3469 ret = acl->len;
3470out:
3471 spin_unlock(&inode->i_lock);
3472 return ret;
3473}
3474
3475static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len)
3476{
3477 struct nfs4_cached_acl *acl;
3478
3479 if (buf && acl_len <= PAGE_SIZE) {
3480 acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL);
3481 if (acl == NULL)
3482 goto out;
3483 acl->cached = 1;
3484 memcpy(acl->data, buf, acl_len);
3485 } else {
3486 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
3487 if (acl == NULL)
3488 goto out;
3489 acl->cached = 0;
3490 }
3491 acl->len = acl_len;
3492out:
3493 nfs4_set_cached_acl(inode, acl);
3494}
3495
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003496static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003497{
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003498 struct page *pages[NFS4ACL_MAXPAGES];
3499 struct nfs_getaclargs args = {
3500 .fh = NFS_FH(inode),
3501 .acl_pages = pages,
3502 .acl_len = buflen,
3503 };
Benny Halevy663c79b2009-04-01 09:21:59 -04003504 struct nfs_getaclres res = {
3505 .acl_len = buflen,
3506 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003507 void *resp_buf;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003508 struct rpc_message msg = {
3509 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
3510 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04003511 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003512 };
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003513 struct page *localpage = NULL;
3514 int ret;
3515
3516 if (buflen < PAGE_SIZE) {
3517 /* As long as we're doing a round trip to the server anyway,
3518 * let's be prepared for a page of acl data. */
3519 localpage = alloc_page(GFP_KERNEL);
3520 resp_buf = page_address(localpage);
3521 if (localpage == NULL)
3522 return -ENOMEM;
3523 args.acl_pages[0] = localpage;
3524 args.acl_pgbase = 0;
Benny Halevy663c79b2009-04-01 09:21:59 -04003525 args.acl_len = PAGE_SIZE;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003526 } else {
3527 resp_buf = buf;
3528 buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase);
3529 }
Bryan Schumaker7c513052011-03-24 17:12:24 +00003530 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 +00003531 if (ret)
3532 goto out_free;
Benny Halevy663c79b2009-04-01 09:21:59 -04003533 if (res.acl_len > args.acl_len)
3534 nfs4_write_cached_acl(inode, NULL, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003535 else
Benny Halevy663c79b2009-04-01 09:21:59 -04003536 nfs4_write_cached_acl(inode, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003537 if (buf) {
3538 ret = -ERANGE;
Benny Halevy663c79b2009-04-01 09:21:59 -04003539 if (res.acl_len > buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003540 goto out_free;
3541 if (localpage)
Benny Halevy663c79b2009-04-01 09:21:59 -04003542 memcpy(buf, resp_buf, res.acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003543 }
Benny Halevy663c79b2009-04-01 09:21:59 -04003544 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003545out_free:
3546 if (localpage)
3547 __free_page(localpage);
3548 return ret;
3549}
3550
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003551static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
3552{
3553 struct nfs4_exception exception = { };
3554 ssize_t ret;
3555 do {
3556 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
3557 if (ret >= 0)
3558 break;
3559 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
3560 } while (exception.retry);
3561 return ret;
3562}
3563
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003564static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
3565{
3566 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003567 int ret;
3568
3569 if (!nfs4_server_supports_acls(server))
3570 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003571 ret = nfs_revalidate_inode(server, inode);
3572 if (ret < 0)
3573 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003574 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
3575 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00003576 ret = nfs4_read_cached_acl(inode, buf, buflen);
3577 if (ret != -ENOENT)
3578 return ret;
3579 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00003580}
3581
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003582static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003583{
3584 struct nfs_server *server = NFS_SERVER(inode);
3585 struct page *pages[NFS4ACL_MAXPAGES];
3586 struct nfs_setaclargs arg = {
3587 .fh = NFS_FH(inode),
3588 .acl_pages = pages,
3589 .acl_len = buflen,
3590 };
Benny Halevy73c403a2009-04-01 09:22:01 -04003591 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003592 struct rpc_message msg = {
3593 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
3594 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04003595 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003596 };
Neil Hormane9e3d722011-03-04 19:26:03 -05003597 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003598
3599 if (!nfs4_server_supports_acls(server))
3600 return -EOPNOTSUPP;
Neil Hormane9e3d722011-03-04 19:26:03 -05003601 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages, &arg.acl_pgbase);
3602 if (i < 0)
3603 return i;
Trond Myklebust642ac542005-10-18 14:20:19 -07003604 nfs_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003605 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05003606
3607 /*
3608 * Free each page after tx, so the only ref left is
3609 * held by the network stack
3610 */
3611 for (; i > 0; i--)
3612 put_page(pages[i-1]);
3613
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00003614 /*
3615 * Acl update can result in inode attribute update.
3616 * so mark the attribute cache invalid.
3617 */
3618 spin_lock(&inode->i_lock);
3619 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
3620 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04003621 nfs_access_zap_cache(inode);
3622 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00003623 return ret;
3624}
3625
Trond Myklebust16b4289c2006-08-24 12:27:15 -04003626static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
3627{
3628 struct nfs4_exception exception = { };
3629 int err;
3630 do {
3631 err = nfs4_handle_exception(NFS_SERVER(inode),
3632 __nfs4_proc_set_acl(inode, buf, buflen),
3633 &exception);
3634 } while (exception.retry);
3635 return err;
3636}
3637
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638static int
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003639nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640{
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003641 struct nfs_client *clp = server->nfs_client;
3642
3643 if (task->tk_status >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644 return 0;
3645 switch(task->tk_status) {
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003646 case -NFS4ERR_ADMIN_REVOKED:
3647 case -NFS4ERR_BAD_STATEID:
3648 case -NFS4ERR_OPENMODE:
3649 if (state == NULL)
3650 break;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003651 nfs4_schedule_stateid_recovery(server, state);
3652 goto wait_on_recovery;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003654 case -NFS4ERR_STALE_CLIENTID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 case -NFS4ERR_EXPIRED:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003656 nfs4_schedule_lease_recovery(clp);
3657 goto wait_on_recovery;
Andy Adamson4745e312009-04-01 09:22:42 -04003658#if defined(CONFIG_NFS_V4_1)
3659 case -NFS4ERR_BADSESSION:
3660 case -NFS4ERR_BADSLOT:
3661 case -NFS4ERR_BAD_HIGH_SLOT:
3662 case -NFS4ERR_DEADSESSION:
3663 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
3664 case -NFS4ERR_SEQ_FALSE_RETRY:
3665 case -NFS4ERR_SEQ_MISORDERED:
3666 dprintk("%s ERROR %d, Reset session\n", __func__,
3667 task->tk_status);
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003668 nfs4_schedule_session_recovery(clp->cl_session);
Andy Adamson4745e312009-04-01 09:22:42 -04003669 task->tk_status = 0;
3670 return -EAGAIN;
3671#endif /* CONFIG_NFS_V4_1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04003673 nfs_inc_server_stats(server, NFSIOS_DELAY);
Chuck Lever006ea732006-03-20 13:44:14 -05003674 case -NFS4ERR_GRACE:
Jeff Layton2c643482010-01-07 09:42:03 -05003675 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 rpc_delay(task, NFS4_POLL_RETRY_MAX);
3677 task->tk_status = 0;
3678 return -EAGAIN;
3679 case -NFS4ERR_OLD_STATEID:
3680 task->tk_status = 0;
3681 return -EAGAIN;
3682 }
3683 task->tk_status = nfs4_map_errors(task->tk_status);
3684 return 0;
Trond Myklebust0400a6b2011-03-09 16:00:53 -05003685wait_on_recovery:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003686 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05003687 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
3688 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
3689 task->tk_status = 0;
3690 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691}
3692
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003693int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
3694 unsigned short port, struct rpc_cred *cred,
3695 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696{
3697 nfs4_verifier sc_verifier;
3698 struct nfs4_setclientid setclientid = {
3699 .sc_verifier = &sc_verifier,
3700 .sc_prog = program,
Andy Adamsonf4eecd52011-01-06 02:04:30 +00003701 .sc_cb_ident = clp->cl_cb_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 };
3703 struct rpc_message msg = {
3704 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
3705 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003706 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003707 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 };
Al Virobc4785c2006-10-19 23:28:51 -07003709 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 int loop = 0;
3711 int status;
3712
Al Virobc4785c2006-10-19 23:28:51 -07003713 p = (__be32*)sc_verifier.data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
3715 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
3716
3717 for(;;) {
3718 setclientid.sc_name_len = scnprintf(setclientid.sc_name,
Trond Myklebust69dd7162007-12-14 14:56:07 -05003719 sizeof(setclientid.sc_name), "%s/%s %s %s %u",
Chuck Leverd4d3c502007-12-10 14:57:09 -05003720 clp->cl_ipaddr,
3721 rpc_peeraddr2str(clp->cl_rpcclient,
3722 RPC_DISPLAY_ADDR),
Trond Myklebust69dd7162007-12-14 14:56:07 -05003723 rpc_peeraddr2str(clp->cl_rpcclient,
3724 RPC_DISPLAY_PROTO),
Trond Myklebust78ea3232008-04-07 20:49:28 -04003725 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 clp->cl_id_uniquifier);
3727 setclientid.sc_netid_len = scnprintf(setclientid.sc_netid,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003728 sizeof(setclientid.sc_netid),
3729 rpc_peeraddr2str(clp->cl_rpcclient,
3730 RPC_DISPLAY_NETID));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05003732 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 clp->cl_ipaddr, port >> 8, port & 255);
3734
3735 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3736 if (status != -NFS4ERR_CLID_INUSE)
3737 break;
3738 if (signalled())
3739 break;
3740 if (loop++ & 1)
J. Bruce Fields611c96c2010-12-13 19:05:46 -05003741 ssleep(clp->cl_lease_time / HZ + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 else
3743 if (++clp->cl_id_uniquifier == 0)
3744 break;
3745 }
3746 return status;
3747}
3748
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003749static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3750 struct nfs4_setclientid_res *arg,
3751 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752{
3753 struct nfs_fsinfo fsinfo;
3754 struct rpc_message msg = {
3755 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003756 .rpc_argp = arg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 .rpc_resp = &fsinfo,
Trond Myklebust286d7d62006-01-03 09:55:26 +01003758 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 };
3760 unsigned long now;
3761 int status;
3762
3763 now = jiffies;
3764 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
3765 if (status == 0) {
3766 spin_lock(&clp->cl_lock);
3767 clp->cl_lease_time = fsinfo.lease_time * HZ;
3768 clp->cl_last_renewal = now;
3769 spin_unlock(&clp->cl_lock);
3770 }
3771 return status;
3772}
3773
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003774int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
3775 struct nfs4_setclientid_res *arg,
3776 struct rpc_cred *cred)
Trond Myklebust51581f32006-03-20 13:44:47 -05003777{
Benny Halevy77e03672008-06-24 20:25:57 +03003778 long timeout = 0;
Trond Myklebust51581f32006-03-20 13:44:47 -05003779 int err;
3780 do {
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04003781 err = _nfs4_proc_setclientid_confirm(clp, arg, cred);
Trond Myklebust51581f32006-03-20 13:44:47 -05003782 switch (err) {
3783 case 0:
3784 return err;
3785 case -NFS4ERR_RESOURCE:
3786 /* The IBM lawyers misread another document! */
3787 case -NFS4ERR_DELAY:
3788 err = nfs4_delay(clp->cl_rpcclient, &timeout);
3789 }
3790 } while (err == 0);
3791 return err;
3792}
3793
Trond Myklebustfe650402006-01-03 09:55:18 +01003794struct nfs4_delegreturndata {
3795 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003796 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01003797 struct nfs_fh fh;
3798 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003799 unsigned long timestamp;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003800 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01003801 int rpc_status;
3802};
3803
Trond Myklebustfe650402006-01-03 09:55:18 +01003804static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
3805{
3806 struct nfs4_delegreturndata *data = calldata;
Andy Adamson938e1012009-04-01 09:22:28 -04003807
Trond Myklebust14516c32010-07-31 14:29:06 -04003808 if (!nfs4_sequence_done(task, &data->res.seq_res))
3809 return;
Andy Adamson938e1012009-04-01 09:22:28 -04003810
Ricardo Labiaga79708862009-12-07 09:23:21 -05003811 switch (task->tk_status) {
3812 case -NFS4ERR_STALE_STATEID:
3813 case -NFS4ERR_EXPIRED:
3814 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01003815 renew_lease(data->res.server, data->timestamp);
Ricardo Labiaga79708862009-12-07 09:23:21 -05003816 break;
3817 default:
3818 if (nfs4_async_handle_error(task, data->res.server, NULL) ==
3819 -EAGAIN) {
3820 nfs_restart_rpc(task, data->res.server->nfs_client);
3821 return;
3822 }
3823 }
3824 data->rpc_status = task->tk_status;
Trond Myklebustfe650402006-01-03 09:55:18 +01003825}
3826
3827static void nfs4_delegreturn_release(void *calldata)
3828{
Trond Myklebustfe650402006-01-03 09:55:18 +01003829 kfree(calldata);
3830}
3831
Andy Adamson938e1012009-04-01 09:22:28 -04003832#if defined(CONFIG_NFS_V4_1)
3833static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
3834{
3835 struct nfs4_delegreturndata *d_data;
3836
3837 d_data = (struct nfs4_delegreturndata *)data;
3838
Trond Myklebust035168a2010-06-16 09:52:26 -04003839 if (nfs4_setup_sequence(d_data->res.server,
Andy Adamson938e1012009-04-01 09:22:28 -04003840 &d_data->args.seq_args,
3841 &d_data->res.seq_res, 1, task))
3842 return;
3843 rpc_call_start(task);
3844}
3845#endif /* CONFIG_NFS_V4_1 */
3846
Jesper Juhlc8d149f2006-03-20 13:44:07 -05003847static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04003848#if defined(CONFIG_NFS_V4_1)
3849 .rpc_call_prepare = nfs4_delegreturn_prepare,
3850#endif /* CONFIG_NFS_V4_1 */
Trond Myklebustfe650402006-01-03 09:55:18 +01003851 .rpc_call_done = nfs4_delegreturn_done,
3852 .rpc_release = nfs4_delegreturn_release,
3853};
3854
Trond Myklebuste6f81072008-01-24 18:14:34 -05003855static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01003856{
3857 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003858 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01003859 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003860 struct rpc_message msg = {
3861 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
3862 .rpc_cred = cred,
3863 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003864 struct rpc_task_setup task_setup_data = {
3865 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003866 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003867 .callback_ops = &nfs4_delegreturn_ops,
3868 .flags = RPC_TASK_ASYNC,
3869 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05003870 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01003871
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003872 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01003873 if (data == NULL)
3874 return -ENOMEM;
3875 data->args.fhandle = &data->fh;
3876 data->args.stateid = &data->stateid;
Trond Myklebustfa178f22006-01-03 09:55:38 +01003877 data->args.bitmask = server->attr_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01003878 nfs_copy_fh(&data->fh, NFS_FH(inode));
3879 memcpy(&data->stateid, stateid, sizeof(data->stateid));
Trond Myklebustfa178f22006-01-03 09:55:38 +01003880 data->res.fattr = &data->fattr;
3881 data->res.server = server;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003882 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01003883 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01003884 data->rpc_status = 0;
3885
Trond Myklebustc970aa82007-07-14 15:39:59 -04003886 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05003887 msg.rpc_argp = &data->args;
3888 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003889 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05003890 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01003891 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003892 if (!issync)
3893 goto out;
Trond Myklebustfe650402006-01-03 09:55:18 +01003894 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05003895 if (status != 0)
3896 goto out;
3897 status = data->rpc_status;
3898 if (status != 0)
3899 goto out;
3900 nfs_refresh_inode(inode, &data->fattr);
3901out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05003902 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01003903 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904}
3905
Trond Myklebuste6f81072008-01-24 18:14:34 -05003906int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907{
3908 struct nfs_server *server = NFS_SERVER(inode);
3909 struct nfs4_exception exception = { };
3910 int err;
3911 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05003912 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 switch (err) {
3914 case -NFS4ERR_STALE_STATEID:
3915 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 case 0:
3917 return 0;
3918 }
3919 err = nfs4_handle_exception(server, err, &exception);
3920 } while (exception.retry);
3921 return err;
3922}
3923
3924#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
3925#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
3926
3927/*
3928 * sleep, with exponential backoff, and retry the LOCK operation.
3929 */
3930static unsigned long
3931nfs4_set_lock_task_retry(unsigned long timeout)
3932{
Matthew Wilcox150030b2007-12-06 16:24:39 -05003933 schedule_timeout_killable(timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 timeout <<= 1;
3935 if (timeout > NFS4_LOCK_MAXTIMEOUT)
3936 return NFS4_LOCK_MAXTIMEOUT;
3937 return timeout;
3938}
3939
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3941{
3942 struct inode *inode = state->inode;
3943 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04003944 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003945 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003947 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003949 struct nfs_lockt_res res = {
3950 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 };
3952 struct rpc_message msg = {
3953 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
3954 .rpc_argp = &arg,
3955 .rpc_resp = &res,
3956 .rpc_cred = state->owner->so_cred,
3957 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 struct nfs4_lock_state *lsp;
3959 int status;
3960
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003961 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003962 status = nfs4_set_lock_state(state, request);
3963 if (status != 0)
3964 goto out;
3965 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04003966 arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05003967 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00003968 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01003969 switch (status) {
3970 case 0:
3971 request->fl_type = F_UNLCK;
3972 break;
3973 case -NFS4ERR_DENIED:
3974 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05003976 request->fl_ops->fl_release_private(request);
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00003977out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 return status;
3979}
3980
3981static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
3982{
3983 struct nfs4_exception exception = { };
3984 int err;
3985
3986 do {
3987 err = nfs4_handle_exception(NFS_SERVER(state->inode),
3988 _nfs4_proc_getlk(state, cmd, request),
3989 &exception);
3990 } while (exception.retry);
3991 return err;
3992}
3993
3994static int do_vfs_lock(struct file *file, struct file_lock *fl)
3995{
3996 int res = 0;
3997 switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
3998 case FL_POSIX:
3999 res = posix_lock_file_wait(file, fl);
4000 break;
4001 case FL_FLOCK:
4002 res = flock_lock_file_wait(file, fl);
4003 break;
4004 default:
4005 BUG();
4006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 return res;
4008}
4009
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004010struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004011 struct nfs_locku_args arg;
4012 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004013 struct nfs4_lock_state *lsp;
4014 struct nfs_open_context *ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004015 struct file_lock fl;
4016 const struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004017 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004018};
4019
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004020static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
4021 struct nfs_open_context *ctx,
4022 struct nfs4_lock_state *lsp,
4023 struct nfs_seqid *seqid)
4024{
4025 struct nfs4_unlockdata *p;
4026 struct inode *inode = lsp->ls_state->inode;
4027
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004028 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004029 if (p == NULL)
4030 return NULL;
4031 p->arg.fh = NFS_FH(inode);
4032 p->arg.fl = &p->fl;
4033 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004034 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004035 p->arg.stateid = &lsp->ls_stateid;
4036 p->lsp = lsp;
4037 atomic_inc(&lsp->ls_count);
4038 /* Ensure we don't close file until we're done freeing locks! */
4039 p->ctx = get_nfs_open_context(ctx);
4040 memcpy(&p->fl, fl, sizeof(p->fl));
4041 p->server = NFS_SERVER(inode);
4042 return p;
4043}
4044
Trond Myklebust06f814a2006-01-03 09:55:07 +01004045static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004046{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004047 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004048 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01004049 nfs4_put_lock_state(calldata->lsp);
4050 put_nfs_open_context(calldata->ctx);
4051 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004052}
4053
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004054static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004055{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004056 struct nfs4_unlockdata *calldata = data;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004057
Trond Myklebust14516c32010-07-31 14:29:06 -04004058 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
4059 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004060 switch (task->tk_status) {
4061 case 0:
4062 memcpy(calldata->lsp->ls_stateid.data,
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004063 calldata->res.stateid.data,
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004064 sizeof(calldata->lsp->ls_stateid.data));
Trond Myklebust26e976a2006-01-03 09:55:21 +01004065 renew_lease(calldata->server, calldata->timestamp);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004066 break;
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004067 case -NFS4ERR_BAD_STATEID:
4068 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004069 case -NFS4ERR_STALE_STATEID:
4070 case -NFS4ERR_EXPIRED:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004071 break;
4072 default:
Trond Myklebust9e33bed2008-12-23 15:21:46 -05004073 if (nfs4_async_handle_error(task, calldata->server, NULL) == -EAGAIN)
Trond Myklebust0110ee12009-12-07 09:00:24 -05004074 nfs_restart_rpc(task,
Trond Myklebustd61e6122009-12-05 19:32:19 -05004075 calldata->server->nfs_client);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004076 }
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004077}
4078
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004079static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004080{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004081 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004083 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004084 return;
4085 if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004086 /* Note: exit _without_ running nfs4_locku_done */
4087 task->tk_action = NULL;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004088 return;
4089 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01004090 calldata->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004091 if (nfs4_setup_sequence(calldata->server,
Andy Adamsona8936932009-04-01 09:22:23 -04004092 &calldata->arg.seq_args,
4093 &calldata->res.seq_res, 1, task))
4094 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004095 rpc_call_start(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096}
4097
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004098static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01004099 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004100 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01004101 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01004102};
4103
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004104static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
4105 struct nfs_open_context *ctx,
4106 struct nfs4_lock_state *lsp,
4107 struct nfs_seqid *seqid)
4108{
4109 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004110 struct rpc_message msg = {
4111 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
4112 .rpc_cred = ctx->cred,
4113 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004114 struct rpc_task_setup task_setup_data = {
4115 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004116 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004117 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004118 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004119 .flags = RPC_TASK_ASYNC,
4120 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004121
Frank Filz137d6ac2007-07-09 15:32:29 -07004122 /* Ensure this is an unlock - when canceling a lock, the
4123 * canceled lock is passed in, and it won't be an unlock.
4124 */
4125 fl->fl_type = F_UNLCK;
4126
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004127 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
4128 if (data == NULL) {
4129 nfs_free_seqid(seqid);
4130 return ERR_PTR(-ENOMEM);
4131 }
4132
Trond Myklebust1174dd12010-12-21 10:52:24 -05004133 msg.rpc_argp = &data->arg;
4134 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004135 task_setup_data.callback_data = data;
4136 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004137}
4138
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
4140{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004141 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004142 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004143 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01004144 struct rpc_task *task;
4145 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004146 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147
Trond Myklebust9b073572006-06-29 16:38:34 -04004148 status = nfs4_set_lock_state(state, request);
4149 /* Unlock _before_ we do the RPC call */
4150 request->fl_flags |= FL_EXISTS;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004151 down_read(&nfsi->rwsem);
4152 if (do_vfs_lock(request->fl_file, request) == -ENOENT) {
4153 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004154 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004155 }
4156 up_read(&nfsi->rwsem);
Trond Myklebust9b073572006-06-29 16:38:34 -04004157 if (status != 0)
4158 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004159 /* Is this a delegated lock? */
4160 if (test_bit(NFS_DELEGATED_STATE, &state->flags))
Trond Myklebust9b073572006-06-29 16:38:34 -04004161 goto out;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004162 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004163 seqid = nfs_alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04004164 status = -ENOMEM;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004165 if (seqid == NULL)
Trond Myklebust9b073572006-06-29 16:38:34 -04004166 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004167 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004168 status = PTR_ERR(task);
4169 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04004170 goto out;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004171 status = nfs4_wait_for_completion_rpc_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004172 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04004173out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04004174 request->fl_flags = fl_flags;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07004175 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176}
4177
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004178struct nfs4_lockdata {
4179 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01004180 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004181 struct nfs4_lock_state *lsp;
4182 struct nfs_open_context *ctx;
4183 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004184 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004185 int rpc_status;
4186 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04004187 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004188};
4189
4190static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004191 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
4192 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004193{
4194 struct nfs4_lockdata *p;
4195 struct inode *inode = lsp->ls_state->inode;
4196 struct nfs_server *server = NFS_SERVER(inode);
4197
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004198 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004199 if (p == NULL)
4200 return NULL;
4201
4202 p->arg.fh = NFS_FH(inode);
4203 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004204 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004205 if (p->arg.open_seqid == NULL)
4206 goto out_free;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004207 p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004208 if (p->arg.lock_seqid == NULL)
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004209 goto out_free_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004210 p->arg.lock_stateid = &lsp->ls_stateid;
David Howells7539bba2006-08-22 20:06:09 -04004211 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust9f958ab2007-07-02 13:58:33 -04004212 p->arg.lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004213 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004214 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004215 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04004216 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004217 atomic_inc(&lsp->ls_count);
4218 p->ctx = get_nfs_open_context(ctx);
4219 memcpy(&p->fl, fl, sizeof(p->fl));
4220 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004221out_free_seqid:
4222 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004223out_free:
4224 kfree(p);
4225 return NULL;
4226}
4227
4228static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
4229{
4230 struct nfs4_lockdata *data = calldata;
4231 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232
Harvey Harrison3110ff82008-05-02 13:42:44 -07004233 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004234 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
4235 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004236 /* Do we need to do an open_to_lock_owner? */
4237 if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) {
Trond Myklebuste6e21972008-01-02 16:27:16 -05004238 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0)
4239 return;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004240 data->arg.open_stateid = &state->stateid;
4241 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04004242 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004243 } else
4244 data->arg.new_lock_owner = 0;
Trond Myklebust26e976a2006-01-03 09:55:21 +01004245 data->timestamp = jiffies;
Trond Myklebust035168a2010-06-16 09:52:26 -04004246 if (nfs4_setup_sequence(data->server,
4247 &data->arg.seq_args,
Andy Adamson66179ef2009-04-01 09:22:22 -04004248 &data->res.seq_res, 1, task))
4249 return;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004250 rpc_call_start(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004251 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004252}
4253
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004254static void nfs4_recover_lock_prepare(struct rpc_task *task, void *calldata)
4255{
4256 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
4257 nfs4_lock_prepare(task, calldata);
4258}
4259
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004260static void nfs4_lock_done(struct rpc_task *task, void *calldata)
4261{
4262 struct nfs4_lockdata *data = calldata;
4263
Harvey Harrison3110ff82008-05-02 13:42:44 -07004264 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004265
Trond Myklebust14516c32010-07-31 14:29:06 -04004266 if (!nfs4_sequence_done(task, &data->res.seq_res))
4267 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04004268
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004269 data->rpc_status = task->tk_status;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004270 if (data->arg.new_lock_owner != 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004271 if (data->rpc_status == 0)
4272 nfs_confirm_seqid(&data->lsp->ls_seqid, 0);
4273 else
4274 goto out;
4275 }
4276 if (data->rpc_status == 0) {
4277 memcpy(data->lsp->ls_stateid.data, data->res.stateid.data,
4278 sizeof(data->lsp->ls_stateid.data));
4279 data->lsp->ls_flags |= NFS_LOCK_INITIALIZED;
Trond Myklebust88be9f92007-06-05 10:42:27 -04004280 renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004281 }
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004282out:
Harvey Harrison3110ff82008-05-02 13:42:44 -07004283 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004284}
4285
4286static void nfs4_lock_release(void *calldata)
4287{
4288 struct nfs4_lockdata *data = calldata;
4289
Harvey Harrison3110ff82008-05-02 13:42:44 -07004290 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05004291 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004292 if (data->cancelled != 0) {
4293 struct rpc_task *task;
4294 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
4295 data->arg.lock_seqid);
4296 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08004297 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004298 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004299 } else
4300 nfs_free_seqid(data->arg.lock_seqid);
4301 nfs4_put_lock_state(data->lsp);
4302 put_nfs_open_context(data->ctx);
4303 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004304 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004305}
4306
4307static const struct rpc_call_ops nfs4_lock_ops = {
4308 .rpc_call_prepare = nfs4_lock_prepare,
4309 .rpc_call_done = nfs4_lock_done,
4310 .rpc_release = nfs4_lock_release,
4311};
4312
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004313static const struct rpc_call_ops nfs4_recover_lock_ops = {
4314 .rpc_call_prepare = nfs4_recover_lock_prepare,
4315 .rpc_call_done = nfs4_lock_done,
4316 .rpc_release = nfs4_lock_release,
4317};
4318
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004319static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
4320{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004321 switch (error) {
4322 case -NFS4ERR_ADMIN_REVOKED:
4323 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05004324 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004325 if (new_lock_owner != 0 ||
4326 (lsp->ls_flags & NFS_LOCK_INITIALIZED) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05004327 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004328 break;
4329 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05004330 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05004331 case -NFS4ERR_EXPIRED:
4332 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004333 };
4334}
4335
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004336static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004337{
4338 struct nfs4_lockdata *data;
4339 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04004340 struct rpc_message msg = {
4341 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
4342 .rpc_cred = state->owner->so_cred,
4343 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04004344 struct rpc_task_setup task_setup_data = {
4345 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04004346 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004347 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05004348 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04004349 .flags = RPC_TASK_ASYNC,
4350 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004351 int ret;
4352
Harvey Harrison3110ff82008-05-02 13:42:44 -07004353 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004354 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004355 fl->fl_u.nfs4_fl.owner,
4356 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004357 if (data == NULL)
4358 return -ENOMEM;
4359 if (IS_SETLKW(cmd))
4360 data->arg.block = 1;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004361 if (recovery_type > NFS_LOCK_NEW) {
4362 if (recovery_type == NFS_LOCK_RECLAIM)
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004363 data->arg.reclaim = NFS_LOCK_RECLAIM;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08004364 task_setup_data.callback_ops = &nfs4_recover_lock_ops;
4365 }
Trond Myklebust1174dd12010-12-21 10:52:24 -05004366 msg.rpc_argp = &data->arg;
4367 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04004368 task_setup_data.callback_data = data;
4369 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05004370 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004371 return PTR_ERR(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004372 ret = nfs4_wait_for_completion_rpc_task(task);
4373 if (ret == 0) {
4374 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05004375 if (ret)
4376 nfs4_handle_setlk_error(data->server, data->lsp,
4377 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004378 } else
4379 data->cancelled = 1;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05004380 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004381 dprintk("%s: done, ret = %d!\n", __func__, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01004382 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383}
4384
4385static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
4386{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004387 struct nfs_server *server = NFS_SERVER(state->inode);
4388 struct nfs4_exception exception = { };
4389 int err;
4390
4391 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004392 /* Cache the lock if possible... */
4393 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4394 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004395 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04004396 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00004397 break;
4398 nfs4_handle_exception(server, err, &exception);
4399 } while (exception.retry);
4400 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401}
4402
4403static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
4404{
Trond Myklebust202b50d2005-06-22 17:16:29 +00004405 struct nfs_server *server = NFS_SERVER(state->inode);
4406 struct nfs4_exception exception = { };
4407 int err;
4408
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004409 err = nfs4_set_lock_state(state, request);
4410 if (err != 0)
4411 return err;
Trond Myklebust202b50d2005-06-22 17:16:29 +00004412 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04004413 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
4414 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004415 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004416 switch (err) {
4417 default:
4418 goto out;
4419 case -NFS4ERR_GRACE:
4420 case -NFS4ERR_DELAY:
4421 nfs4_handle_exception(server, err, &exception);
4422 err = 0;
4423 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00004424 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05004425out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00004426 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427}
4428
4429static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4430{
Trond Myklebust19e03c52008-12-23 15:21:44 -05004431 struct nfs_inode *nfsi = NFS_I(state->inode);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004432 unsigned char fl_flags = request->fl_flags;
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004433 int status = -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434
Trond Myklebust8e469eb2010-01-26 15:42:30 -05004435 if ((fl_flags & FL_POSIX) &&
4436 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
4437 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004438 /* Is this a delegated open? */
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004439 status = nfs4_set_lock_state(state, request);
4440 if (status != 0)
4441 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004442 request->fl_flags |= FL_ACCESS;
4443 status = do_vfs_lock(request->fl_file, request);
4444 if (status < 0)
4445 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05004446 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004447 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04004448 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004449 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05004450 request->fl_flags = fl_flags & ~FL_SLEEP;
4451 status = do_vfs_lock(request->fl_file, request);
4452 goto out_unlock;
Trond Myklebust01c3b862006-06-29 16:38:39 -04004453 }
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004454 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004455 if (status != 0)
Trond Myklebust01c3b862006-06-29 16:38:39 -04004456 goto out_unlock;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004457 /* Note: we always want to sleep here! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04004458 request->fl_flags = fl_flags | FL_SLEEP;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05004459 if (do_vfs_lock(request->fl_file, request) < 0)
Harvey Harrison3110ff82008-05-02 13:42:44 -07004460 printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __func__);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004461out_unlock:
Trond Myklebust19e03c52008-12-23 15:21:44 -05004462 up_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04004463out:
4464 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 return status;
4466}
4467
4468static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
4469{
4470 struct nfs4_exception exception = { };
4471 int err;
4472
4473 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07004474 err = _nfs4_proc_setlk(state, cmd, request);
4475 if (err == -NFS4ERR_DENIED)
4476 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07004478 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 } while (exception.retry);
4480 return err;
4481}
4482
4483static int
4484nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
4485{
4486 struct nfs_open_context *ctx;
4487 struct nfs4_state *state;
4488 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
4489 int status;
4490
4491 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04004492 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 state = ctx->state;
4494
4495 if (request->fl_start < 0 || request->fl_end < 0)
4496 return -EINVAL;
4497
Trond Myklebustd9531262009-07-21 19:22:38 -04004498 if (IS_GETLK(cmd)) {
4499 if (state != NULL)
4500 return nfs4_proc_getlk(state, F_GETLK, request);
4501 return 0;
4502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503
4504 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
4505 return -EINVAL;
4506
Trond Myklebustd9531262009-07-21 19:22:38 -04004507 if (request->fl_type == F_UNLCK) {
4508 if (state != NULL)
4509 return nfs4_proc_unlck(state, cmd, request);
4510 return 0;
4511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512
Trond Myklebustd9531262009-07-21 19:22:38 -04004513 if (state == NULL)
4514 return -ENOLCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 do {
4516 status = nfs4_proc_setlk(state, cmd, request);
4517 if ((status != -EAGAIN) || IS_SETLK(cmd))
4518 break;
4519 timeout = nfs4_set_lock_task_retry(timeout);
4520 status = -ERESTARTSYS;
4521 if (signalled())
4522 break;
4523 } while(status < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 return status;
4525}
4526
Trond Myklebust888e6942005-11-04 15:38:11 -05004527int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl)
4528{
4529 struct nfs_server *server = NFS_SERVER(state->inode);
4530 struct nfs4_exception exception = { };
4531 int err;
4532
4533 err = nfs4_set_lock_state(state, fl);
4534 if (err != 0)
4535 goto out;
4536 do {
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08004537 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
Trond Myklebustd5122202009-06-17 13:22:58 -07004538 switch (err) {
4539 default:
4540 printk(KERN_ERR "%s: unhandled error %d.\n",
4541 __func__, err);
4542 case 0:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004543 case -ESTALE:
Trond Myklebustd5122202009-06-17 13:22:58 -07004544 goto out;
4545 case -NFS4ERR_EXPIRED:
4546 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust965b5d62009-06-17 13:22:59 -07004547 case -NFS4ERR_STALE_STATEID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004548 nfs4_schedule_lease_recovery(server->nfs_client);
4549 goto out;
Ricardo Labiaga74e7bb72009-12-07 09:48:30 -05004550 case -NFS4ERR_BADSESSION:
4551 case -NFS4ERR_BADSLOT:
4552 case -NFS4ERR_BAD_HIGH_SLOT:
4553 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
4554 case -NFS4ERR_DEADSESSION:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004555 nfs4_schedule_session_recovery(server->nfs_client->cl_session);
Trond Myklebustd5122202009-06-17 13:22:58 -07004556 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004557 case -ERESTARTSYS:
4558 /*
4559 * The show must go on: exit, but mark the
4560 * stateid as needing recovery.
4561 */
4562 case -NFS4ERR_ADMIN_REVOKED:
4563 case -NFS4ERR_BAD_STATEID:
4564 case -NFS4ERR_OPENMODE:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05004565 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust965b5d62009-06-17 13:22:59 -07004566 err = 0;
4567 goto out;
Trond Myklebust168667c2010-10-19 19:47:49 -04004568 case -EKEYEXPIRED:
4569 /*
4570 * User RPCSEC_GSS context has expired.
4571 * We cannot recover this stateid now, so
4572 * skip it and allow recovery thread to
4573 * proceed.
4574 */
4575 err = 0;
4576 goto out;
Trond Myklebust965b5d62009-06-17 13:22:59 -07004577 case -ENOMEM:
4578 case -NFS4ERR_DENIED:
4579 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
4580 err = 0;
4581 goto out;
Trond Myklebustd5122202009-06-17 13:22:58 -07004582 case -NFS4ERR_DELAY:
4583 break;
4584 }
Trond Myklebust888e6942005-11-04 15:38:11 -05004585 err = nfs4_handle_exception(server, err, &exception);
4586 } while (exception.retry);
4587out:
4588 return err;
4589}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004590
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004591static void nfs4_release_lockowner_release(void *calldata)
4592{
4593 kfree(calldata);
4594}
4595
4596const struct rpc_call_ops nfs4_release_lockowner_ops = {
4597 .rpc_release = nfs4_release_lockowner_release,
4598};
4599
4600void nfs4_release_lockowner(const struct nfs4_lock_state *lsp)
4601{
4602 struct nfs_server *server = lsp->ls_state->owner->so_server;
4603 struct nfs_release_lockowner_args *args;
4604 struct rpc_message msg = {
4605 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
4606 };
4607
4608 if (server->nfs_client->cl_mvops->minor_version != 0)
4609 return;
4610 args = kmalloc(sizeof(*args), GFP_NOFS);
4611 if (!args)
4612 return;
4613 args->lock_owner.clientid = server->nfs_client->cl_clientid;
4614 args->lock_owner.id = lsp->ls_id.id;
Trond Myklebustd035c362010-12-21 10:45:27 -05004615 args->lock_owner.s_dev = server->s_dev;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04004616 msg.rpc_argp = args;
4617 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, args);
4618}
4619
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004620#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
4621
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004622static int nfs4_xattr_set_nfs4_acl(struct dentry *dentry, const char *key,
4623 const void *buf, size_t buflen,
4624 int flags, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004625{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004626 if (strcmp(key, "") != 0)
4627 return -EINVAL;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00004628
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004629 return nfs4_proc_set_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004630}
4631
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004632static int nfs4_xattr_get_nfs4_acl(struct dentry *dentry, const char *key,
4633 void *buf, size_t buflen, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004634{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004635 if (strcmp(key, "") != 0)
4636 return -EINVAL;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004637
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004638 return nfs4_proc_get_acl(dentry->d_inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004639}
4640
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004641static size_t nfs4_xattr_list_nfs4_acl(struct dentry *dentry, char *list,
4642 size_t list_len, const char *name,
4643 size_t name_len, int type)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004644{
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004645 size_t len = sizeof(XATTR_NAME_NFSV4_ACL);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004646
J. Bruce Fields096455a2006-03-20 23:23:42 -05004647 if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode)))
4648 return 0;
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00004649
4650 if (list && len <= list_len)
4651 memcpy(list, XATTR_NAME_NFSV4_ACL, len);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00004652 return len;
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00004653}
4654
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004655static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
4656{
4657 if (!((fattr->valid & NFS_ATTR_FATTR_FILEID) &&
4658 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
4659 (fattr->valid & NFS_ATTR_FATTR_V4_REFERRAL)))
4660 return;
4661
4662 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
4663 NFS_ATTR_FATTR_NLINK;
4664 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4665 fattr->nlink = 2;
4666}
4667
Trond Myklebust56659e92007-07-17 21:52:39 -04004668int nfs4_proc_fs_locations(struct inode *dir, const struct qstr *name,
Manoj Naik7aaa0b32006-06-09 09:34:23 -04004669 struct nfs4_fs_locations *fs_locations, struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04004670{
4671 struct nfs_server *server = NFS_SERVER(dir);
4672 u32 bitmask[2] = {
Manoj Naik361e6242006-06-09 09:34:24 -04004673 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
4674 [1] = FATTR4_WORD1_MOUNTED_ON_FILEID,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004675 };
4676 struct nfs4_fs_locations_arg args = {
4677 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05004678 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004679 .page = page,
4680 .bitmask = bitmask,
4681 };
Benny Halevy22958462009-04-01 09:22:02 -04004682 struct nfs4_fs_locations_res res = {
4683 .fs_locations = fs_locations,
4684 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04004685 struct rpc_message msg = {
4686 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
4687 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04004688 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04004689 };
4690 int status;
4691
Harvey Harrison3110ff82008-05-02 13:42:44 -07004692 dprintk("%s: start\n", __func__);
Trond Myklebustc228fd32007-01-13 02:28:11 -05004693 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004694 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04004695 fs_locations->nlocations = 0;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004696 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust69aaaae2009-03-11 14:10:28 -04004697 nfs_fixup_referral_attributes(&fs_locations->fattr);
Harvey Harrison3110ff82008-05-02 13:42:44 -07004698 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04004699 return status;
4700}
4701
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00004702static int _nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4703{
4704 int status;
4705 struct nfs4_secinfo_arg args = {
4706 .dir_fh = NFS_FH(dir),
4707 .name = name,
4708 };
4709 struct nfs4_secinfo_res res = {
4710 .flavors = flavors,
4711 };
4712 struct rpc_message msg = {
4713 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
4714 .rpc_argp = &args,
4715 .rpc_resp = &res,
4716 };
4717
4718 dprintk("NFS call secinfo %s\n", name->name);
4719 status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &msg, &args.seq_args, &res.seq_res, 0);
4720 dprintk("NFS reply secinfo: %d\n", status);
4721 return status;
4722}
4723
4724int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name, struct nfs4_secinfo_flavors *flavors)
4725{
4726 struct nfs4_exception exception = { };
4727 int err;
4728 do {
4729 err = nfs4_handle_exception(NFS_SERVER(dir),
4730 _nfs4_proc_secinfo(dir, name, flavors),
4731 &exception);
4732 } while (exception.retry);
4733 return err;
4734}
4735
Andy Adamson557134a2009-04-01 09:21:53 -04004736#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04004737/*
Andy Adamson357f54d2010-12-14 10:11:57 -05004738 * Check the exchange flags returned by the server for invalid flags, having
4739 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
4740 * DS flags set.
4741 */
4742static int nfs4_check_cl_exchange_flags(u32 flags)
4743{
4744 if (flags & ~EXCHGID4_FLAG_MASK_R)
4745 goto out_inval;
4746 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
4747 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
4748 goto out_inval;
4749 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
4750 goto out_inval;
4751 return NFS_OK;
4752out_inval:
4753 return -NFS4ERR_INVAL;
4754}
4755
4756/*
Benny Halevy99fe60d2009-04-01 09:22:29 -04004757 * nfs4_proc_exchange_id()
4758 *
4759 * Since the clientid has expired, all compounds using sessions
4760 * associated with the stale clientid will be returning
4761 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
4762 * be in some phase of session reset.
4763 */
Andy Adamson4d643d12009-12-04 15:52:24 -05004764int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
Benny Halevy99fe60d2009-04-01 09:22:29 -04004765{
4766 nfs4_verifier verifier;
4767 struct nfs41_exchange_id_args args = {
4768 .client = clp,
Andy Adamson357f54d2010-12-14 10:11:57 -05004769 .flags = EXCHGID4_FLAG_SUPP_MOVED_REFER,
Benny Halevy99fe60d2009-04-01 09:22:29 -04004770 };
4771 struct nfs41_exchange_id_res res = {
4772 .client = clp,
4773 };
4774 int status;
4775 struct rpc_message msg = {
4776 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
4777 .rpc_argp = &args,
4778 .rpc_resp = &res,
4779 .rpc_cred = cred,
4780 };
4781 __be32 *p;
4782
4783 dprintk("--> %s\n", __func__);
4784 BUG_ON(clp == NULL);
Andy Adamsona7b72102009-04-01 09:22:46 -04004785
Benny Halevy99fe60d2009-04-01 09:22:29 -04004786 p = (u32 *)verifier.data;
4787 *p++ = htonl((u32)clp->cl_boot_time.tv_sec);
4788 *p = htonl((u32)clp->cl_boot_time.tv_nsec);
4789 args.verifier = &verifier;
4790
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004791 args.id_len = scnprintf(args.id, sizeof(args.id),
4792 "%s/%s.%s/%u",
4793 clp->cl_ipaddr,
4794 init_utsname()->nodename,
4795 init_utsname()->domainname,
4796 clp->cl_rpcclient->cl_auth->au_flavor);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004797
Andy Adamsonc7a360b2011-01-25 19:15:32 -05004798 status = rpc_call_sync(clp->cl_rpcclient, &msg, 0);
4799 if (!status)
4800 status = nfs4_check_cl_exchange_flags(clp->cl_exchange_flags);
Benny Halevy99fe60d2009-04-01 09:22:29 -04004801 dprintk("<-- %s status= %d\n", __func__, status);
4802 return status;
4803}
4804
Andy Adamson2050f0c2009-04-01 09:22:30 -04004805struct nfs4_get_lease_time_data {
4806 struct nfs4_get_lease_time_args *args;
4807 struct nfs4_get_lease_time_res *res;
4808 struct nfs_client *clp;
4809};
4810
4811static void nfs4_get_lease_time_prepare(struct rpc_task *task,
4812 void *calldata)
4813{
4814 int ret;
4815 struct nfs4_get_lease_time_data *data =
4816 (struct nfs4_get_lease_time_data *)calldata;
4817
4818 dprintk("--> %s\n", __func__);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004819 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004820 /* just setup sequence, do not trigger session recovery
4821 since we're invoked within one */
4822 ret = nfs41_setup_sequence(data->clp->cl_session,
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08004823 &data->args->la_seq_args,
4824 &data->res->lr_seq_res, 0, task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004825
4826 BUG_ON(ret == -EAGAIN);
4827 rpc_call_start(task);
4828 dprintk("<-- %s\n", __func__);
4829}
4830
4831/*
4832 * Called from nfs4_state_manager thread for session setup, so don't recover
4833 * from sequence operation or clientid errors.
4834 */
4835static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
4836{
4837 struct nfs4_get_lease_time_data *data =
4838 (struct nfs4_get_lease_time_data *)calldata;
4839
4840 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04004841 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
4842 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04004843 switch (task->tk_status) {
4844 case -NFS4ERR_DELAY:
4845 case -NFS4ERR_GRACE:
4846 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
4847 rpc_delay(task, NFS4_POLL_RETRY_MIN);
4848 task->tk_status = 0;
Trond Myklebust0110ee12009-12-07 09:00:24 -05004849 nfs_restart_rpc(task, data->clp);
Andy Adamson2050f0c2009-04-01 09:22:30 -04004850 return;
4851 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04004852 dprintk("<-- %s\n", __func__);
4853}
4854
4855struct rpc_call_ops nfs4_get_lease_time_ops = {
4856 .rpc_call_prepare = nfs4_get_lease_time_prepare,
4857 .rpc_call_done = nfs4_get_lease_time_done,
4858};
4859
4860int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
4861{
4862 struct rpc_task *task;
4863 struct nfs4_get_lease_time_args args;
4864 struct nfs4_get_lease_time_res res = {
4865 .lr_fsinfo = fsinfo,
4866 };
4867 struct nfs4_get_lease_time_data data = {
4868 .args = &args,
4869 .res = &res,
4870 .clp = clp,
4871 };
4872 struct rpc_message msg = {
4873 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
4874 .rpc_argp = &args,
4875 .rpc_resp = &res,
4876 };
4877 struct rpc_task_setup task_setup = {
4878 .rpc_client = clp->cl_rpcclient,
4879 .rpc_message = &msg,
4880 .callback_ops = &nfs4_get_lease_time_ops,
4881 .callback_data = &data
4882 };
4883 int status;
4884
Andy Adamson2050f0c2009-04-01 09:22:30 -04004885 dprintk("--> %s\n", __func__);
4886 task = rpc_run_task(&task_setup);
4887
4888 if (IS_ERR(task))
4889 status = PTR_ERR(task);
4890 else {
4891 status = task->tk_status;
4892 rpc_put_task(task);
4893 }
4894 dprintk("<-- %s return %d\n", __func__, status);
4895
4896 return status;
4897}
4898
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004899/*
4900 * Reset a slot table
4901 */
Andy Adamson104aeba2010-01-14 17:45:10 -05004902static int nfs4_reset_slot_table(struct nfs4_slot_table *tbl, u32 max_reqs,
4903 int ivalue)
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004904{
Andy Adamson104aeba2010-01-14 17:45:10 -05004905 struct nfs4_slot *new = NULL;
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004906 int i;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004907 int ret = 0;
4908
Andy Adamson104aeba2010-01-14 17:45:10 -05004909 dprintk("--> %s: max_reqs=%u, tbl->max_slots %d\n", __func__,
4910 max_reqs, tbl->max_slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004911
Andy Adamson104aeba2010-01-14 17:45:10 -05004912 /* Does the newly negotiated max_reqs match the existing slot table? */
4913 if (max_reqs != tbl->max_slots) {
4914 ret = -ENOMEM;
4915 new = kmalloc(max_reqs * sizeof(struct nfs4_slot),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004916 GFP_NOFS);
Andy Adamson104aeba2010-01-14 17:45:10 -05004917 if (!new)
4918 goto out;
4919 ret = 0;
4920 kfree(tbl->slots);
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004921 }
4922 spin_lock(&tbl->slot_tbl_lock);
Andy Adamson104aeba2010-01-14 17:45:10 -05004923 if (new) {
4924 tbl->slots = new;
4925 tbl->max_slots = max_reqs;
4926 }
4927 for (i = 0; i < tbl->max_slots; ++i)
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004928 tbl->slots[i].seq_nr = ivalue;
Andy Adamsonac72b7b2009-04-01 09:22:37 -04004929 spin_unlock(&tbl->slot_tbl_lock);
4930 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4931 tbl, tbl->slots, tbl->max_slots);
4932out:
4933 dprintk("<-- %s: return %d\n", __func__, ret);
4934 return ret;
4935}
4936
Andy Adamsonfc931582009-04-01 09:22:31 -04004937/*
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004938 * Reset the forechannel and backchannel slot tables
4939 */
4940static int nfs4_reset_slot_tables(struct nfs4_session *session)
4941{
4942 int status;
4943
4944 status = nfs4_reset_slot_table(&session->fc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004945 session->fc_attrs.max_reqs, 1);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004946 if (status)
4947 return status;
4948
4949 status = nfs4_reset_slot_table(&session->bc_slot_table,
Andy Adamson104aeba2010-01-14 17:45:10 -05004950 session->bc_attrs.max_reqs, 0);
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004951 return status;
4952}
4953
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04004954/* Destroy the slot table */
4955static void nfs4_destroy_slot_tables(struct nfs4_session *session)
4956{
4957 if (session->fc_slot_table.slots != NULL) {
4958 kfree(session->fc_slot_table.slots);
4959 session->fc_slot_table.slots = NULL;
4960 }
4961 if (session->bc_slot_table.slots != NULL) {
4962 kfree(session->bc_slot_table.slots);
4963 session->bc_slot_table.slots = NULL;
4964 }
4965 return;
4966}
4967
Ricardo Labiagab73dafa2009-04-01 09:23:31 -04004968/*
Andy Adamsonfc931582009-04-01 09:22:31 -04004969 * Initialize slot table
4970 */
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004971static int nfs4_init_slot_table(struct nfs4_slot_table *tbl,
4972 int max_slots, int ivalue)
Andy Adamsonfc931582009-04-01 09:22:31 -04004973{
Andy Adamsonfc931582009-04-01 09:22:31 -04004974 struct nfs4_slot *slot;
4975 int ret = -ENOMEM;
4976
4977 BUG_ON(max_slots > NFS4_MAX_SLOT_TABLE);
4978
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004979 dprintk("--> %s: max_reqs=%u\n", __func__, max_slots);
Andy Adamsonfc931582009-04-01 09:22:31 -04004980
Trond Myklebust8535b2b2010-05-13 12:51:01 -04004981 slot = kcalloc(max_slots, sizeof(struct nfs4_slot), GFP_NOFS);
Andy Adamsonfc931582009-04-01 09:22:31 -04004982 if (!slot)
4983 goto out;
Andy Adamsonfc931582009-04-01 09:22:31 -04004984 ret = 0;
4985
4986 spin_lock(&tbl->slot_tbl_lock);
Andy Adamsonfc931582009-04-01 09:22:31 -04004987 tbl->max_slots = max_slots;
4988 tbl->slots = slot;
4989 tbl->highest_used_slotid = -1; /* no slot is currently used */
4990 spin_unlock(&tbl->slot_tbl_lock);
4991 dprintk("%s: tbl=%p slots=%p max_slots=%d\n", __func__,
4992 tbl, tbl->slots, tbl->max_slots);
4993out:
4994 dprintk("<-- %s: return %d\n", __func__, ret);
4995 return ret;
Andy Adamsonfc931582009-04-01 09:22:31 -04004996}
4997
Ricardo Labiaga050047c2009-04-01 09:23:32 -04004998/*
4999 * Initialize the forechannel and backchannel tables
5000 */
5001static int nfs4_init_slot_tables(struct nfs4_session *session)
5002{
Trond Myklebustf26468f2009-12-05 19:32:11 -05005003 struct nfs4_slot_table *tbl;
5004 int status = 0;
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005005
Trond Myklebustf26468f2009-12-05 19:32:11 -05005006 tbl = &session->fc_slot_table;
5007 if (tbl->slots == NULL) {
5008 status = nfs4_init_slot_table(tbl,
5009 session->fc_attrs.max_reqs, 1);
5010 if (status)
5011 return status;
5012 }
Ricardo Labiaga050047c2009-04-01 09:23:32 -04005013
Trond Myklebustf26468f2009-12-05 19:32:11 -05005014 tbl = &session->bc_slot_table;
5015 if (tbl->slots == NULL) {
5016 status = nfs4_init_slot_table(tbl,
5017 session->bc_attrs.max_reqs, 0);
5018 if (status)
5019 nfs4_destroy_slot_tables(session);
5020 }
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005021
5022 return status;
Andy Adamson557134a2009-04-01 09:21:53 -04005023}
5024
5025struct nfs4_session *nfs4_alloc_session(struct nfs_client *clp)
5026{
5027 struct nfs4_session *session;
5028 struct nfs4_slot_table *tbl;
5029
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005030 session = kzalloc(sizeof(struct nfs4_session), GFP_NOFS);
Andy Adamson557134a2009-04-01 09:21:53 -04005031 if (!session)
5032 return NULL;
Andy Adamson76db6d92009-04-01 09:22:38 -04005033
Andy Adamson557134a2009-04-01 09:21:53 -04005034 tbl = &session->fc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05005035 tbl->highest_used_slotid = -1;
Andy Adamson557134a2009-04-01 09:21:53 -04005036 spin_lock_init(&tbl->slot_tbl_lock);
Alexandros Batsakis689cf5c2009-12-14 21:27:56 -08005037 rpc_init_priority_wait_queue(&tbl->slot_tbl_waitq, "ForeChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005038 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005039
5040 tbl = &session->bc_slot_table;
Ricardo Labiaga9dfdf402009-12-06 12:57:34 -05005041 tbl->highest_used_slotid = -1;
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005042 spin_lock_init(&tbl->slot_tbl_lock);
5043 rpc_init_wait_queue(&tbl->slot_tbl_waitq, "BackChannel Slot table");
Andy Adamson42acd022011-01-06 02:04:34 +00005044 init_completion(&tbl->complete);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005045
Trond Myklebust1055d762010-06-16 09:52:27 -04005046 session->session_state = 1<<NFS4_SESSION_INITING;
5047
Andy Adamson557134a2009-04-01 09:21:53 -04005048 session->clp = clp;
5049 return session;
5050}
5051
5052void nfs4_destroy_session(struct nfs4_session *session)
5053{
Andy Adamson5a0ffe52009-04-01 09:23:18 -04005054 nfs4_proc_destroy_session(session);
5055 dprintk("%s Destroy backchannel for xprt %p\n",
5056 __func__, session->clp->cl_rpcclient->cl_xprt);
5057 xprt_destroy_backchannel(session->clp->cl_rpcclient->cl_xprt,
5058 NFS41_BC_MIN_CALLBACKS);
Ricardo Labiagaf8625a62009-04-01 09:23:33 -04005059 nfs4_destroy_slot_tables(session);
Andy Adamson557134a2009-04-01 09:21:53 -04005060 kfree(session);
5061}
5062
Andy Adamsonfc931582009-04-01 09:22:31 -04005063/*
5064 * Initialize the values to be used by the client in CREATE_SESSION
5065 * If nfs4_init_session set the fore channel request and response sizes,
5066 * use them.
5067 *
5068 * Set the back channel max_resp_sz_cached to zero to force the client to
5069 * always set csa_cachethis to FALSE because the current implementation
5070 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
5071 */
5072static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args)
5073{
5074 struct nfs4_session *session = args->client->cl_session;
5075 unsigned int mxrqst_sz = session->fc_attrs.max_rqst_sz,
5076 mxresp_sz = session->fc_attrs.max_resp_sz;
5077
5078 if (mxrqst_sz == 0)
5079 mxrqst_sz = NFS_MAX_FILE_IO_SIZE;
5080 if (mxresp_sz == 0)
5081 mxresp_sz = NFS_MAX_FILE_IO_SIZE;
5082 /* Fore channel attributes */
5083 args->fc_attrs.headerpadsz = 0;
5084 args->fc_attrs.max_rqst_sz = mxrqst_sz;
5085 args->fc_attrs.max_resp_sz = mxresp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04005086 args->fc_attrs.max_ops = NFS4_MAX_OPS;
5087 args->fc_attrs.max_reqs = session->clp->cl_rpcclient->cl_xprt->max_reqs;
5088
5089 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05005090 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04005091 __func__,
5092 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05005093 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04005094
5095 /* Back channel attributes */
5096 args->bc_attrs.headerpadsz = 0;
5097 args->bc_attrs.max_rqst_sz = PAGE_SIZE;
5098 args->bc_attrs.max_resp_sz = PAGE_SIZE;
5099 args->bc_attrs.max_resp_sz_cached = 0;
5100 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
5101 args->bc_attrs.max_reqs = 1;
5102
5103 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
5104 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
5105 __func__,
5106 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
5107 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
5108 args->bc_attrs.max_reqs);
5109}
5110
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005111static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
Andy Adamson8d353012009-04-01 09:22:32 -04005112{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005113 struct nfs4_channel_attrs *sent = &args->fc_attrs;
5114 struct nfs4_channel_attrs *rcvd = &session->fc_attrs;
5115
5116 if (rcvd->headerpadsz > sent->headerpadsz)
5117 return -EINVAL;
5118 if (rcvd->max_resp_sz > sent->max_resp_sz)
5119 return -EINVAL;
5120 /*
5121 * Our requested max_ops is the minimum we need; we're not
5122 * prepared to break up compounds into smaller pieces than that.
5123 * So, no point even trying to continue if the server won't
5124 * cooperate:
5125 */
5126 if (rcvd->max_ops < sent->max_ops)
5127 return -EINVAL;
5128 if (rcvd->max_reqs == 0)
5129 return -EINVAL;
5130 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04005131}
5132
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005133static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args, struct nfs4_session *session)
5134{
5135 struct nfs4_channel_attrs *sent = &args->bc_attrs;
5136 struct nfs4_channel_attrs *rcvd = &session->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04005137
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005138 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
5139 return -EINVAL;
5140 if (rcvd->max_resp_sz < sent->max_resp_sz)
5141 return -EINVAL;
5142 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
5143 return -EINVAL;
5144 /* These would render the backchannel useless: */
5145 if (rcvd->max_ops == 0)
5146 return -EINVAL;
5147 if (rcvd->max_reqs == 0)
5148 return -EINVAL;
5149 return 0;
5150}
Andy Adamson8d353012009-04-01 09:22:32 -04005151
Andy Adamson8d353012009-04-01 09:22:32 -04005152static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
5153 struct nfs4_session *session)
5154{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005155 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04005156
J. Bruce Fields43c2e882010-10-02 15:19:01 -04005157 ret = nfs4_verify_fore_channel_attrs(args, session);
5158 if (ret)
5159 return ret;
5160 return nfs4_verify_back_channel_attrs(args, session);
Andy Adamson8d353012009-04-01 09:22:32 -04005161}
5162
Andy Adamsonfc931582009-04-01 09:22:31 -04005163static int _nfs4_proc_create_session(struct nfs_client *clp)
5164{
5165 struct nfs4_session *session = clp->cl_session;
5166 struct nfs41_create_session_args args = {
5167 .client = clp,
5168 .cb_program = NFS4_CALLBACK,
5169 };
5170 struct nfs41_create_session_res res = {
5171 .client = clp,
5172 };
5173 struct rpc_message msg = {
5174 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
5175 .rpc_argp = &args,
5176 .rpc_resp = &res,
5177 };
5178 int status;
5179
5180 nfs4_init_channel_attrs(&args);
Andy Adamson0f914212009-04-01 09:23:16 -04005181 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04005182
5183 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5184
Andy Adamson8d353012009-04-01 09:22:32 -04005185 if (!status)
5186 /* Verify the session's negotiated channel_attrs values */
5187 status = nfs4_verify_channel_attrs(&args, session);
Andy Adamsonfc931582009-04-01 09:22:31 -04005188 if (!status) {
5189 /* Increment the clientid slot sequence id */
5190 clp->cl_seqid++;
5191 }
5192
5193 return status;
5194}
5195
5196/*
5197 * Issues a CREATE_SESSION operation to the server.
5198 * It is the responsibility of the caller to verify the session is
5199 * expired before calling this routine.
5200 */
Trond Myklebustf26468f2009-12-05 19:32:11 -05005201int nfs4_proc_create_session(struct nfs_client *clp)
Andy Adamsonfc931582009-04-01 09:22:31 -04005202{
5203 int status;
5204 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04005205 struct nfs4_session *session = clp->cl_session;
Ricardo Labiaga7d6d63d2011-03-09 13:13:44 -05005206 long timeout = 0;
5207 int err;
Andy Adamsonfc931582009-04-01 09:22:31 -04005208
5209 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
5210
Ricardo Labiaga7d6d63d2011-03-09 13:13:44 -05005211 do {
5212 status = _nfs4_proc_create_session(clp);
5213 if (status == -NFS4ERR_DELAY) {
5214 err = nfs4_delay(clp->cl_rpcclient, &timeout);
5215 if (err)
5216 status = err;
5217 }
5218 } while (status == -NFS4ERR_DELAY);
5219
Andy Adamsonfc931582009-04-01 09:22:31 -04005220 if (status)
5221 goto out;
5222
Trond Myklebustf26468f2009-12-05 19:32:11 -05005223 /* Init and reset the fore channel */
5224 status = nfs4_init_slot_tables(session);
5225 dprintk("slot table initialization returned %d\n", status);
5226 if (status)
5227 goto out;
5228 status = nfs4_reset_slot_tables(session);
5229 dprintk("slot table reset returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04005230 if (status)
5231 goto out;
5232
5233 ptr = (unsigned *)&session->sess_id.data[0];
5234 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
5235 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04005236out:
5237 dprintk("<-- %s\n", __func__);
5238 return status;
5239}
5240
Andy Adamson0f3e66c2009-04-01 09:22:34 -04005241/*
5242 * Issue the over-the-wire RPC DESTROY_SESSION.
5243 * The caller must serialize access to this routine.
5244 */
5245int nfs4_proc_destroy_session(struct nfs4_session *session)
5246{
5247 int status = 0;
5248 struct rpc_message msg;
5249
5250 dprintk("--> nfs4_proc_destroy_session\n");
5251
5252 /* session is still being setup */
5253 if (session->clp->cl_cons_state != NFS_CS_READY)
5254 return status;
5255
5256 msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION];
5257 msg.rpc_argp = session;
5258 msg.rpc_resp = NULL;
5259 msg.rpc_cred = NULL;
5260 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, 0);
5261
5262 if (status)
5263 printk(KERN_WARNING
5264 "Got error %d from the server on DESTROY_SESSION. "
5265 "Session has been destroyed regardless...\n", status);
5266
5267 dprintk("<-- nfs4_proc_destroy_session\n");
5268 return status;
5269}
5270
Trond Myklebustfccba802009-07-21 16:48:07 -04005271int nfs4_init_session(struct nfs_server *server)
5272{
5273 struct nfs_client *clp = server->nfs_client;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005274 struct nfs4_session *session;
Andy Adamson68bf05e2009-12-15 12:55:02 -05005275 unsigned int rsize, wsize;
Trond Myklebustfccba802009-07-21 16:48:07 -04005276 int ret;
5277
5278 if (!nfs4_has_session(clp))
5279 return 0;
5280
Trond Myklebust1055d762010-06-16 09:52:27 -04005281 session = clp->cl_session;
5282 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5283 return 0;
5284
Andy Adamson68bf05e2009-12-15 12:55:02 -05005285 rsize = server->rsize;
5286 if (rsize == 0)
5287 rsize = NFS_MAX_FILE_IO_SIZE;
5288 wsize = server->wsize;
5289 if (wsize == 0)
5290 wsize = NFS_MAX_FILE_IO_SIZE;
5291
Andy Adamson68bf05e2009-12-15 12:55:02 -05005292 session->fc_attrs.max_rqst_sz = wsize + nfs41_maxwrite_overhead;
5293 session->fc_attrs.max_resp_sz = rsize + nfs41_maxread_overhead;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05005294
Trond Myklebustfccba802009-07-21 16:48:07 -04005295 ret = nfs4_recover_expired_lease(server);
5296 if (!ret)
5297 ret = nfs4_check_client_ready(clp);
5298 return ret;
5299}
5300
Andy Adamsond83217c2011-03-01 01:34:17 +00005301int nfs4_init_ds_session(struct nfs_client *clp)
5302{
5303 struct nfs4_session *session = clp->cl_session;
5304 int ret;
5305
5306 if (!test_and_clear_bit(NFS4_SESSION_INITING, &session->session_state))
5307 return 0;
5308
5309 ret = nfs4_client_recover_expired_lease(clp);
5310 if (!ret)
5311 /* Test for the DS role */
5312 if (!is_ds_client(clp))
5313 ret = -ENODEV;
5314 if (!ret)
5315 ret = nfs4_check_client_ready(clp);
5316 return ret;
5317
5318}
5319EXPORT_SYMBOL_GPL(nfs4_init_ds_session);
5320
5321
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005322/*
5323 * Renew the cl_session lease.
5324 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005325struct nfs4_sequence_data {
5326 struct nfs_client *clp;
5327 struct nfs4_sequence_args args;
5328 struct nfs4_sequence_res res;
5329};
5330
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005331static void nfs41_sequence_release(void *data)
5332{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005333 struct nfs4_sequence_data *calldata = data;
5334 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005335
Alexandros Batsakis71358402010-02-05 03:45:05 -08005336 if (atomic_read(&clp->cl_count) > 1)
5337 nfs4_schedule_state_renewal(clp);
5338 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005339 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005340}
5341
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005342static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5343{
5344 switch(task->tk_status) {
5345 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005346 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5347 return -EAGAIN;
5348 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005349 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005350 }
5351 return 0;
5352}
5353
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005354static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005355{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005356 struct nfs4_sequence_data *calldata = data;
5357 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005358
Trond Myklebust14516c32010-07-31 14:29:06 -04005359 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
5360 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005361
5362 if (task->tk_status < 0) {
5363 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005364 if (atomic_read(&clp->cl_count) == 1)
5365 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005366
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005367 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
5368 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005369 return;
5370 }
5371 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005372 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08005373out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005374 dprintk("<-- %s\n", __func__);
5375}
5376
5377static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
5378{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005379 struct nfs4_sequence_data *calldata = data;
5380 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005381 struct nfs4_sequence_args *args;
5382 struct nfs4_sequence_res *res;
5383
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005384 args = task->tk_msg.rpc_argp;
5385 res = task->tk_msg.rpc_resp;
5386
Trond Myklebust035168a2010-06-16 09:52:26 -04005387 if (nfs41_setup_sequence(clp->cl_session, args, res, 0, task))
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005388 return;
5389 rpc_call_start(task);
5390}
5391
5392static const struct rpc_call_ops nfs41_sequence_ops = {
5393 .rpc_call_done = nfs41_sequence_call_done,
5394 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005395 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005396};
5397
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005398static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005399{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005400 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005401 struct rpc_message msg = {
5402 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
5403 .rpc_cred = cred,
5404 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005405 struct rpc_task_setup task_setup_data = {
5406 .rpc_client = clp->cl_rpcclient,
5407 .rpc_message = &msg,
5408 .callback_ops = &nfs41_sequence_ops,
5409 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFT,
5410 };
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005411
Alexandros Batsakis71358402010-02-05 03:45:05 -08005412 if (!atomic_inc_not_zero(&clp->cl_count))
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005413 return ERR_PTR(-EIO);
Benny Halevydfb4f3092010-09-24 09:17:01 -04005414 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005415 if (calldata == NULL) {
Alexandros Batsakis71358402010-02-05 03:45:05 -08005416 nfs_put_client(clp);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005417 return ERR_PTR(-ENOMEM);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005418 }
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04005419 msg.rpc_argp = &calldata->args;
5420 msg.rpc_resp = &calldata->res;
5421 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005422 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005423
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005424 return rpc_run_task(&task_setup_data);
5425}
5426
5427static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5428{
5429 struct rpc_task *task;
5430 int ret = 0;
5431
5432 task = _nfs41_proc_sequence(clp, cred);
5433 if (IS_ERR(task))
5434 ret = PTR_ERR(task);
5435 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08005436 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005437 dprintk("<-- %s status=%d\n", __func__, ret);
5438 return ret;
5439}
5440
5441static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
5442{
5443 struct rpc_task *task;
5444 int ret;
5445
5446 task = _nfs41_proc_sequence(clp, cred);
5447 if (IS_ERR(task)) {
5448 ret = PTR_ERR(task);
5449 goto out;
5450 }
5451 ret = rpc_wait_for_completion_task(task);
Trond Myklebustb4410c22011-03-09 16:00:55 -05005452 if (!ret) {
5453 struct nfs4_sequence_res *res = task->tk_msg.rpc_resp;
5454
5455 if (task->tk_status == 0)
5456 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005457 ret = task->tk_status;
Trond Myklebustb4410c22011-03-09 16:00:55 -05005458 }
Trond Myklebust71ac6da2010-06-16 09:52:26 -04005459 rpc_put_task(task);
5460out:
5461 dprintk("<-- %s status=%d\n", __func__, ret);
5462 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04005463}
5464
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005465struct nfs4_reclaim_complete_data {
5466 struct nfs_client *clp;
5467 struct nfs41_reclaim_complete_args arg;
5468 struct nfs41_reclaim_complete_res res;
5469};
5470
5471static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
5472{
5473 struct nfs4_reclaim_complete_data *calldata = data;
5474
Alexandros Batsakisb2579572009-12-14 21:27:57 -08005475 rpc_task_set_priority(task, RPC_PRIORITY_PRIVILEGED);
Trond Myklebust035168a2010-06-16 09:52:26 -04005476 if (nfs41_setup_sequence(calldata->clp->cl_session,
5477 &calldata->arg.seq_args,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005478 &calldata->res.seq_res, 0, task))
5479 return;
5480
5481 rpc_call_start(task);
5482}
5483
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005484static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
5485{
5486 switch(task->tk_status) {
5487 case 0:
5488 case -NFS4ERR_COMPLETE_ALREADY:
5489 case -NFS4ERR_WRONG_CRED: /* What to do here? */
5490 break;
5491 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005492 rpc_delay(task, NFS4_POLL_RETRY_MAX);
5493 return -EAGAIN;
5494 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005495 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005496 }
5497 return 0;
5498}
5499
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005500static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
5501{
5502 struct nfs4_reclaim_complete_data *calldata = data;
5503 struct nfs_client *clp = calldata->clp;
5504 struct nfs4_sequence_res *res = &calldata->res.seq_res;
5505
5506 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04005507 if (!nfs41_sequence_done(task, res))
5508 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005509
Trond Myklebustaa5190d2010-06-16 09:52:25 -04005510 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
5511 rpc_restart_call_prepare(task);
5512 return;
5513 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005514 dprintk("<-- %s\n", __func__);
5515}
5516
5517static void nfs4_free_reclaim_complete_data(void *data)
5518{
5519 struct nfs4_reclaim_complete_data *calldata = data;
5520
5521 kfree(calldata);
5522}
5523
5524static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
5525 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
5526 .rpc_call_done = nfs4_reclaim_complete_done,
5527 .rpc_release = nfs4_free_reclaim_complete_data,
5528};
5529
5530/*
5531 * Issue a global reclaim complete.
5532 */
5533static int nfs41_proc_reclaim_complete(struct nfs_client *clp)
5534{
5535 struct nfs4_reclaim_complete_data *calldata;
5536 struct rpc_task *task;
5537 struct rpc_message msg = {
5538 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
5539 };
5540 struct rpc_task_setup task_setup_data = {
5541 .rpc_client = clp->cl_rpcclient,
5542 .rpc_message = &msg,
5543 .callback_ops = &nfs4_reclaim_complete_call_ops,
5544 .flags = RPC_TASK_ASYNC,
5545 };
5546 int status = -ENOMEM;
5547
5548 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005549 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005550 if (calldata == NULL)
5551 goto out;
5552 calldata->clp = clp;
5553 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005554
5555 msg.rpc_argp = &calldata->arg;
5556 msg.rpc_resp = &calldata->res;
5557 task_setup_data.callback_data = calldata;
5558 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005559 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005560 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005561 goto out;
5562 }
Andy Adamsonc34c32e2011-03-09 13:13:46 -05005563 status = nfs4_wait_for_completion_rpc_task(task);
5564 if (status == 0)
5565 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005566 rpc_put_task(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02005567 return 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005568out:
5569 dprintk("<-- %s status=%d\n", __func__, status);
5570 return status;
5571}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005572
5573static void
5574nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
5575{
5576 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00005577 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005578
5579 dprintk("--> %s\n", __func__);
Fred Isamanc31663d2011-01-06 11:36:24 +00005580 /* Note the is a race here, where a CB_LAYOUTRECALL can come in
5581 * right now covering the LAYOUTGET we are about to send.
5582 * However, that is not so catastrophic, and there seems
5583 * to be no way to prevent it completely.
5584 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005585 if (nfs4_setup_sequence(server, &lgp->args.seq_args,
5586 &lgp->res.seq_res, 0, task))
5587 return;
Fred Isamancf7d63f2011-01-06 11:36:25 +00005588 if (pnfs_choose_layoutget_stateid(&lgp->args.stateid,
5589 NFS_I(lgp->args.inode)->layout,
5590 lgp->args.ctx->state)) {
5591 rpc_exit(task, NFS4_OK);
5592 return;
5593 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005594 rpc_call_start(task);
5595}
5596
5597static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
5598{
5599 struct nfs4_layoutget *lgp = calldata;
5600 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5601
5602 dprintk("--> %s\n", __func__);
5603
5604 if (!nfs4_sequence_done(task, &lgp->res.seq_res))
5605 return;
5606
5607 switch (task->tk_status) {
5608 case 0:
5609 break;
5610 case -NFS4ERR_LAYOUTTRYLATER:
5611 case -NFS4ERR_RECALLCONFLICT:
5612 task->tk_status = -NFS4ERR_DELAY;
5613 /* Fall through */
5614 default:
5615 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5616 rpc_restart_call_prepare(task);
5617 return;
5618 }
5619 }
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005620 dprintk("<-- %s\n", __func__);
5621}
5622
5623static void nfs4_layoutget_release(void *calldata)
5624{
5625 struct nfs4_layoutget *lgp = calldata;
5626
5627 dprintk("--> %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005628 put_nfs_open_context(lgp->args.ctx);
5629 kfree(calldata);
5630 dprintk("<-- %s\n", __func__);
5631}
5632
5633static const struct rpc_call_ops nfs4_layoutget_call_ops = {
5634 .rpc_call_prepare = nfs4_layoutget_prepare,
5635 .rpc_call_done = nfs4_layoutget_done,
5636 .rpc_release = nfs4_layoutget_release,
5637};
5638
5639int nfs4_proc_layoutget(struct nfs4_layoutget *lgp)
5640{
5641 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
5642 struct rpc_task *task;
5643 struct rpc_message msg = {
5644 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
5645 .rpc_argp = &lgp->args,
5646 .rpc_resp = &lgp->res,
5647 };
5648 struct rpc_task_setup task_setup_data = {
5649 .rpc_client = server->client,
5650 .rpc_message = &msg,
5651 .callback_ops = &nfs4_layoutget_call_ops,
5652 .callback_data = lgp,
5653 .flags = RPC_TASK_ASYNC,
5654 };
5655 int status = 0;
5656
5657 dprintk("--> %s\n", __func__);
5658
Weston Andros Adamson35124a02011-03-24 16:48:21 -04005659 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005660 lgp->res.seq_res.sr_slot = NULL;
5661 task = rpc_run_task(&task_setup_data);
5662 if (IS_ERR(task))
5663 return PTR_ERR(task);
5664 status = nfs4_wait_for_completion_rpc_task(task);
Fred Isamanc31663d2011-01-06 11:36:24 +00005665 if (status == 0)
5666 status = task->tk_status;
5667 if (status == 0)
5668 status = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005669 rpc_put_task(task);
5670 dprintk("<-- %s status=%d\n", __func__, status);
5671 return status;
5672}
5673
5674static int
5675_nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5676{
5677 struct nfs4_getdeviceinfo_args args = {
5678 .pdev = pdev,
5679 };
5680 struct nfs4_getdeviceinfo_res res = {
5681 .pdev = pdev,
5682 };
5683 struct rpc_message msg = {
5684 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
5685 .rpc_argp = &args,
5686 .rpc_resp = &res,
5687 };
5688 int status;
5689
5690 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005691 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04005692 dprintk("<-- %s status=%d\n", __func__, status);
5693
5694 return status;
5695}
5696
5697int nfs4_proc_getdeviceinfo(struct nfs_server *server, struct pnfs_device *pdev)
5698{
5699 struct nfs4_exception exception = { };
5700 int err;
5701
5702 do {
5703 err = nfs4_handle_exception(server,
5704 _nfs4_proc_getdeviceinfo(server, pdev),
5705 &exception);
5706 } while (exception.retry);
5707 return err;
5708}
5709EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
5710
Andy Adamson863a3c62011-03-23 13:27:54 +00005711static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
5712{
5713 struct nfs4_layoutcommit_data *data = calldata;
5714 struct nfs_server *server = NFS_SERVER(data->args.inode);
5715
5716 if (nfs4_setup_sequence(server, &data->args.seq_args,
5717 &data->res.seq_res, 1, task))
5718 return;
5719 rpc_call_start(task);
5720}
5721
5722static void
5723nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
5724{
5725 struct nfs4_layoutcommit_data *data = calldata;
5726 struct nfs_server *server = NFS_SERVER(data->args.inode);
5727
5728 if (!nfs4_sequence_done(task, &data->res.seq_res))
5729 return;
5730
5731 switch (task->tk_status) { /* Just ignore these failures */
5732 case NFS4ERR_DELEG_REVOKED: /* layout was recalled */
5733 case NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
5734 case NFS4ERR_BADLAYOUT: /* no layout */
5735 case NFS4ERR_GRACE: /* loca_recalim always false */
5736 task->tk_status = 0;
5737 }
5738
5739 if (nfs4_async_handle_error(task, server, NULL) == -EAGAIN) {
5740 nfs_restart_rpc(task, server->nfs_client);
5741 return;
5742 }
5743
5744 if (task->tk_status == 0)
5745 nfs_post_op_update_inode_force_wcc(data->args.inode,
5746 data->res.fattr);
5747}
5748
5749static void nfs4_layoutcommit_release(void *calldata)
5750{
5751 struct nfs4_layoutcommit_data *data = calldata;
5752
5753 /* Matched by references in pnfs_set_layoutcommit */
5754 put_lseg(data->lseg);
5755 put_rpccred(data->cred);
5756 kfree(data);
5757}
5758
5759static const struct rpc_call_ops nfs4_layoutcommit_ops = {
5760 .rpc_call_prepare = nfs4_layoutcommit_prepare,
5761 .rpc_call_done = nfs4_layoutcommit_done,
5762 .rpc_release = nfs4_layoutcommit_release,
5763};
5764
5765int
Andy Adamsonef311532011-03-12 02:58:10 -05005766nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00005767{
5768 struct rpc_message msg = {
5769 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
5770 .rpc_argp = &data->args,
5771 .rpc_resp = &data->res,
5772 .rpc_cred = data->cred,
5773 };
5774 struct rpc_task_setup task_setup_data = {
5775 .task = &data->task,
5776 .rpc_client = NFS_CLIENT(data->args.inode),
5777 .rpc_message = &msg,
5778 .callback_ops = &nfs4_layoutcommit_ops,
5779 .callback_data = data,
5780 .flags = RPC_TASK_ASYNC,
5781 };
5782 struct rpc_task *task;
5783 int status = 0;
5784
5785 dprintk("NFS: %4d initiating layoutcommit call. sync %d "
5786 "lbw: %llu inode %lu\n",
5787 data->task.tk_pid, sync,
5788 data->args.lastbytewritten,
5789 data->args.inode->i_ino);
5790
5791 task = rpc_run_task(&task_setup_data);
5792 if (IS_ERR(task))
5793 return PTR_ERR(task);
Andy Adamsonef311532011-03-12 02:58:10 -05005794 if (sync == false)
Andy Adamson863a3c62011-03-23 13:27:54 +00005795 goto out;
5796 status = nfs4_wait_for_completion_rpc_task(task);
5797 if (status != 0)
5798 goto out;
5799 status = task->tk_status;
5800out:
5801 dprintk("%s: status %d\n", __func__, status);
5802 rpc_put_task(task);
5803 return status;
5804}
Andy Adamson557134a2009-04-01 09:21:53 -04005805#endif /* CONFIG_NFS_V4_1 */
5806
Andy Adamson591d71c2009-04-01 09:22:47 -04005807struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005808 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005809 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810 .recover_open = nfs4_open_reclaim,
5811 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04005812 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005813 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814};
5815
Andy Adamson591d71c2009-04-01 09:22:47 -04005816#if defined(CONFIG_NFS_V4_1)
5817struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
5818 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
5819 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
5820 .recover_open = nfs4_open_reclaim,
5821 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05005822 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005823 .get_clid_cred = nfs4_get_exchange_id_cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05005824 .reclaim_complete = nfs41_proc_reclaim_complete,
Andy Adamson591d71c2009-04-01 09:22:47 -04005825};
5826#endif /* CONFIG_NFS_V4_1 */
5827
5828struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05005829 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05005830 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831 .recover_open = nfs4_open_expired,
5832 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04005833 .establish_clid = nfs4_init_clientid,
Andy Adamson90a16612009-04-01 09:22:48 -04005834 .get_clid_cred = nfs4_get_setclientid_cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835};
5836
Andy Adamson591d71c2009-04-01 09:22:47 -04005837#if defined(CONFIG_NFS_V4_1)
5838struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
5839 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
5840 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
5841 .recover_open = nfs4_open_expired,
5842 .recover_lock = nfs4_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05005843 .establish_clid = nfs41_init_clientid,
Andy Adamsonb4b82602009-04-01 09:22:49 -04005844 .get_clid_cred = nfs4_get_exchange_id_cred,
Andy Adamson591d71c2009-04-01 09:22:47 -04005845};
5846#endif /* CONFIG_NFS_V4_1 */
5847
Benny Halevy29fba382009-04-01 09:22:44 -04005848struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
5849 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04005850 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005851 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04005852};
5853
5854#if defined(CONFIG_NFS_V4_1)
5855struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
5856 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04005857 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04005858 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04005859};
5860#endif
5861
Trond Myklebust97dc1352010-06-16 09:52:26 -04005862static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
5863 .minor_version = 0,
5864 .call_sync = _nfs4_call_sync,
Trond Myklebuste047a102010-06-16 09:52:27 -04005865 .validate_stateid = nfs4_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005866 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
5867 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
5868 .state_renewal_ops = &nfs40_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005869};
5870
5871#if defined(CONFIG_NFS_V4_1)
5872static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
5873 .minor_version = 1,
5874 .call_sync = _nfs4_call_sync_session,
Trond Myklebuste047a102010-06-16 09:52:27 -04005875 .validate_stateid = nfs41_validate_delegation_stateid,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04005876 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
5877 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
5878 .state_renewal_ops = &nfs41_state_renewal_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04005879};
5880#endif
5881
Trond Myklebust97dc1352010-06-16 09:52:26 -04005882const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
5883 [0] = &nfs_v4_0_minor_ops,
5884#if defined(CONFIG_NFS_V4_1)
5885 [1] = &nfs_v4_1_minor_ops,
5886#endif
5887};
5888
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08005889static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005890 .permission = nfs_permission,
5891 .getattr = nfs_getattr,
5892 .setattr = nfs_setattr,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005893 .getxattr = generic_getxattr,
5894 .setxattr = generic_setxattr,
5895 .listxattr = generic_listxattr,
5896 .removexattr = generic_removexattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005897};
5898
David Howells509de812006-08-22 20:06:11 -04005899const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900 .version = 4, /* protocol version */
5901 .dentry_ops = &nfs4_dentry_operations,
5902 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00005903 .file_inode_ops = &nfs4_file_inode_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904 .getroot = nfs4_proc_get_root,
5905 .getattr = nfs4_proc_getattr,
5906 .setattr = nfs4_proc_setattr,
David Howells2b3de442006-08-22 20:06:09 -04005907 .lookupfh = nfs4_proc_lookupfh,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908 .lookup = nfs4_proc_lookup,
5909 .access = nfs4_proc_access,
5910 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911 .create = nfs4_proc_create,
5912 .remove = nfs4_proc_remove,
5913 .unlink_setup = nfs4_proc_unlink_setup,
5914 .unlink_done = nfs4_proc_unlink_done,
5915 .rename = nfs4_proc_rename,
Jeff Laytond3d41522010-09-17 17:31:57 -04005916 .rename_setup = nfs4_proc_rename_setup,
5917 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918 .link = nfs4_proc_link,
5919 .symlink = nfs4_proc_symlink,
5920 .mkdir = nfs4_proc_mkdir,
5921 .rmdir = nfs4_proc_remove,
5922 .readdir = nfs4_proc_readdir,
5923 .mknod = nfs4_proc_mknod,
5924 .statfs = nfs4_proc_statfs,
5925 .fsinfo = nfs4_proc_fsinfo,
5926 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04005927 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005928 .decode_dirent = nfs4_decode_dirent,
5929 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05005930 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005932 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933 .commit_setup = nfs4_proc_commit_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05005934 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005936 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04005937 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04005938 .open_context = nfs4_atomic_open,
Andy Adamson45a52a02011-03-01 01:34:08 +00005939 .init_client = nfs4_init_client,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00005940 .secinfo = nfs4_proc_secinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941};
5942
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00005943static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
5944 .prefix = XATTR_NAME_NFSV4_ACL,
5945 .list = nfs4_xattr_list_nfs4_acl,
5946 .get = nfs4_xattr_get_nfs4_acl,
5947 .set = nfs4_xattr_set_nfs4_acl,
5948};
5949
5950const struct xattr_handler *nfs4_xattr_handlers[] = {
5951 &nfs4_xattr_nfs4_acl_handler,
5952 NULL
5953};
5954
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955/*
5956 * Local variables:
5957 * c-basic-offset: 8
5958 * End:
5959 */