Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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> | |
39 | #include <linux/utsname.h> | |
40 | #include <linux/delay.h> | |
41 | #include <linux/errno.h> | |
42 | #include <linux/string.h> | |
43 | #include <linux/sunrpc/clnt.h> | |
44 | #include <linux/nfs.h> | |
45 | #include <linux/nfs4.h> | |
46 | #include <linux/nfs_fs.h> | |
47 | #include <linux/nfs_page.h> | |
48 | #include <linux/smp_lock.h> | |
49 | #include <linux/namei.h> | |
02a913a7 | 50 | #include <linux/mount.h> |
1da177e4 | 51 | |
4ce79717 | 52 | #include "nfs4_fs.h" |
1da177e4 | 53 | #include "delegation.h" |
006ea73e | 54 | #include "iostat.h" |
1da177e4 LT |
55 | |
56 | #define NFSDBG_FACILITY NFSDBG_PROC | |
57 | ||
2066fe89 | 58 | #define NFS4_POLL_RETRY_MIN (HZ/10) |
1da177e4 LT |
59 | #define NFS4_POLL_RETRY_MAX (15*HZ) |
60 | ||
cdd4e68b | 61 | struct nfs4_opendata; |
864472e9 | 62 | static int _nfs4_proc_open(struct nfs4_opendata *data); |
1da177e4 | 63 | static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *); |
faf5f49c | 64 | static int nfs4_async_handle_error(struct rpc_task *, const struct nfs_server *); |
1da177e4 | 65 | static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry); |
faf5f49c | 66 | static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception); |
adfa6f98 | 67 | static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs_client *clp); |
1da177e4 | 68 | |
1da177e4 LT |
69 | /* Prevent leaks of NFSv4 errors into userland */ |
70 | int nfs4_map_errors(int err) | |
71 | { | |
72 | if (err < -1000) { | |
73 | dprintk("%s could not handle NFSv4 error %d\n", | |
74 | __FUNCTION__, -err); | |
75 | return -EIO; | |
76 | } | |
77 | return err; | |
78 | } | |
79 | ||
80 | /* | |
81 | * This is our standard bitmap for GETATTR requests. | |
82 | */ | |
83 | const u32 nfs4_fattr_bitmap[2] = { | |
84 | FATTR4_WORD0_TYPE | |
85 | | FATTR4_WORD0_CHANGE | |
86 | | FATTR4_WORD0_SIZE | |
87 | | FATTR4_WORD0_FSID | |
88 | | FATTR4_WORD0_FILEID, | |
89 | FATTR4_WORD1_MODE | |
90 | | FATTR4_WORD1_NUMLINKS | |
91 | | FATTR4_WORD1_OWNER | |
92 | | FATTR4_WORD1_OWNER_GROUP | |
93 | | FATTR4_WORD1_RAWDEV | |
94 | | FATTR4_WORD1_SPACE_USED | |
95 | | FATTR4_WORD1_TIME_ACCESS | |
96 | | FATTR4_WORD1_TIME_METADATA | |
97 | | FATTR4_WORD1_TIME_MODIFY | |
98 | }; | |
99 | ||
100 | const u32 nfs4_statfs_bitmap[2] = { | |
101 | FATTR4_WORD0_FILES_AVAIL | |
102 | | FATTR4_WORD0_FILES_FREE | |
103 | | FATTR4_WORD0_FILES_TOTAL, | |
104 | FATTR4_WORD1_SPACE_AVAIL | |
105 | | FATTR4_WORD1_SPACE_FREE | |
106 | | FATTR4_WORD1_SPACE_TOTAL | |
107 | }; | |
108 | ||
4ce79717 | 109 | const u32 nfs4_pathconf_bitmap[2] = { |
1da177e4 LT |
110 | FATTR4_WORD0_MAXLINK |
111 | | FATTR4_WORD0_MAXNAME, | |
112 | 0 | |
113 | }; | |
114 | ||
115 | const u32 nfs4_fsinfo_bitmap[2] = { FATTR4_WORD0_MAXFILESIZE | |
116 | | FATTR4_WORD0_MAXREAD | |
117 | | FATTR4_WORD0_MAXWRITE | |
118 | | FATTR4_WORD0_LEASE_TIME, | |
119 | 0 | |
120 | }; | |
121 | ||
830b8e33 MN |
122 | const u32 nfs4_fs_locations_bitmap[2] = { |
123 | FATTR4_WORD0_TYPE | |
124 | | FATTR4_WORD0_CHANGE | |
125 | | FATTR4_WORD0_SIZE | |
126 | | FATTR4_WORD0_FSID | |
127 | | FATTR4_WORD0_FILEID | |
128 | | FATTR4_WORD0_FS_LOCATIONS, | |
129 | FATTR4_WORD1_MODE | |
130 | | FATTR4_WORD1_NUMLINKS | |
131 | | FATTR4_WORD1_OWNER | |
132 | | FATTR4_WORD1_OWNER_GROUP | |
133 | | FATTR4_WORD1_RAWDEV | |
134 | | FATTR4_WORD1_SPACE_USED | |
135 | | FATTR4_WORD1_TIME_ACCESS | |
136 | | FATTR4_WORD1_TIME_METADATA | |
137 | | FATTR4_WORD1_TIME_MODIFY | |
138 | | FATTR4_WORD1_MOUNTED_ON_FILEID | |
139 | }; | |
140 | ||
bc4785cd | 141 | static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry, |
1da177e4 LT |
142 | struct nfs4_readdir_arg *readdir) |
143 | { | |
0dbb4c67 | 144 | __be32 *start, *p; |
1da177e4 LT |
145 | |
146 | BUG_ON(readdir->count < 80); | |
147 | if (cookie > 2) { | |
b7ef1956 | 148 | readdir->cookie = cookie; |
1da177e4 LT |
149 | memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier)); |
150 | return; | |
151 | } | |
152 | ||
153 | readdir->cookie = 0; | |
154 | memset(&readdir->verifier, 0, sizeof(readdir->verifier)); | |
155 | if (cookie == 2) | |
156 | return; | |
157 | ||
158 | /* | |
159 | * NFSv4 servers do not return entries for '.' and '..' | |
160 | * Therefore, we fake these entries here. We let '.' | |
161 | * have cookie 0 and '..' have cookie 1. Note that | |
162 | * when talking to the server, we always send cookie 0 | |
163 | * instead of 1 or 2. | |
164 | */ | |
0dbb4c67 | 165 | start = p = kmap_atomic(*readdir->pages, KM_USER0); |
1da177e4 LT |
166 | |
167 | if (cookie == 0) { | |
168 | *p++ = xdr_one; /* next */ | |
169 | *p++ = xdr_zero; /* cookie, first word */ | |
170 | *p++ = xdr_one; /* cookie, second word */ | |
171 | *p++ = xdr_one; /* entry len */ | |
172 | memcpy(p, ".\0\0\0", 4); /* entry */ | |
173 | p++; | |
174 | *p++ = xdr_one; /* bitmap length */ | |
175 | *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */ | |
176 | *p++ = htonl(8); /* attribute buffer length */ | |
177 | p = xdr_encode_hyper(p, dentry->d_inode->i_ino); | |
178 | } | |
179 | ||
180 | *p++ = xdr_one; /* next */ | |
181 | *p++ = xdr_zero; /* cookie, first word */ | |
182 | *p++ = xdr_two; /* cookie, second word */ | |
183 | *p++ = xdr_two; /* entry len */ | |
184 | memcpy(p, "..\0\0", 4); /* entry */ | |
185 | p++; | |
186 | *p++ = xdr_one; /* bitmap length */ | |
187 | *p++ = htonl(FATTR4_WORD0_FILEID); /* bitmap */ | |
188 | *p++ = htonl(8); /* attribute buffer length */ | |
189 | p = xdr_encode_hyper(p, dentry->d_parent->d_inode->i_ino); | |
190 | ||
191 | readdir->pgbase = (char *)p - (char *)start; | |
192 | readdir->count -= readdir->pgbase; | |
193 | kunmap_atomic(start, KM_USER0); | |
194 | } | |
195 | ||
26e976a8 | 196 | static void renew_lease(const struct nfs_server *server, unsigned long timestamp) |
1da177e4 | 197 | { |
7539bbab | 198 | struct nfs_client *clp = server->nfs_client; |
1da177e4 LT |
199 | spin_lock(&clp->cl_lock); |
200 | if (time_before(clp->cl_last_renewal,timestamp)) | |
201 | clp->cl_last_renewal = timestamp; | |
202 | spin_unlock(&clp->cl_lock); | |
203 | } | |
204 | ||
38478b24 | 205 | static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo) |
1da177e4 | 206 | { |
38478b24 | 207 | struct nfs_inode *nfsi = NFS_I(dir); |
1da177e4 | 208 | |
38478b24 TM |
209 | spin_lock(&dir->i_lock); |
210 | nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA; | |
1da177e4 LT |
211 | if (cinfo->before == nfsi->change_attr && cinfo->atomic) |
212 | nfsi->change_attr = cinfo->after; | |
38478b24 | 213 | spin_unlock(&dir->i_lock); |
1da177e4 LT |
214 | } |
215 | ||
e56e0b78 | 216 | struct nfs4_opendata { |
c6d00e63 | 217 | struct kref kref; |
e56e0b78 TM |
218 | struct nfs_openargs o_arg; |
219 | struct nfs_openres o_res; | |
cdd4e68b TM |
220 | struct nfs_open_confirmargs c_arg; |
221 | struct nfs_open_confirmres c_res; | |
e56e0b78 TM |
222 | struct nfs_fattr f_attr; |
223 | struct nfs_fattr dir_attr; | |
ad389da7 | 224 | struct path path; |
e56e0b78 TM |
225 | struct dentry *dir; |
226 | struct nfs4_state_owner *owner; | |
227 | struct iattr attrs; | |
26e976a8 | 228 | unsigned long timestamp; |
3e309914 | 229 | unsigned int rpc_done : 1; |
24ac23ab TM |
230 | int rpc_status; |
231 | int cancelled; | |
e56e0b78 TM |
232 | }; |
233 | ||
2ced46c2 TM |
234 | |
235 | static void nfs4_init_opendata_res(struct nfs4_opendata *p) | |
236 | { | |
237 | p->o_res.f_attr = &p->f_attr; | |
238 | p->o_res.dir_attr = &p->dir_attr; | |
239 | p->o_res.server = p->o_arg.server; | |
240 | nfs_fattr_init(&p->f_attr); | |
241 | nfs_fattr_init(&p->dir_attr); | |
242 | } | |
243 | ||
ad389da7 | 244 | static struct nfs4_opendata *nfs4_opendata_alloc(struct path *path, |
e56e0b78 TM |
245 | struct nfs4_state_owner *sp, int flags, |
246 | const struct iattr *attrs) | |
247 | { | |
ad389da7 | 248 | struct dentry *parent = dget_parent(path->dentry); |
e56e0b78 TM |
249 | struct inode *dir = parent->d_inode; |
250 | struct nfs_server *server = NFS_SERVER(dir); | |
251 | struct nfs4_opendata *p; | |
252 | ||
253 | p = kzalloc(sizeof(*p), GFP_KERNEL); | |
254 | if (p == NULL) | |
255 | goto err; | |
256 | p->o_arg.seqid = nfs_alloc_seqid(&sp->so_seqid); | |
257 | if (p->o_arg.seqid == NULL) | |
258 | goto err_free; | |
ad389da7 TM |
259 | p->path.mnt = mntget(path->mnt); |
260 | p->path.dentry = dget(path->dentry); | |
e56e0b78 TM |
261 | p->dir = parent; |
262 | p->owner = sp; | |
263 | atomic_inc(&sp->so_count); | |
264 | p->o_arg.fh = NFS_FH(dir); | |
265 | p->o_arg.open_flags = flags, | |
7539bbab | 266 | p->o_arg.clientid = server->nfs_client->cl_clientid; |
9f958ab8 | 267 | p->o_arg.id = sp->so_owner_id.id; |
ad389da7 | 268 | p->o_arg.name = &p->path.dentry->d_name; |
e56e0b78 TM |
269 | p->o_arg.server = server; |
270 | p->o_arg.bitmask = server->attr_bitmask; | |
271 | p->o_arg.claim = NFS4_OPEN_CLAIM_NULL; | |
e56e0b78 TM |
272 | if (flags & O_EXCL) { |
273 | u32 *s = (u32 *) p->o_arg.u.verifier.data; | |
274 | s[0] = jiffies; | |
275 | s[1] = current->pid; | |
276 | } else if (flags & O_CREAT) { | |
277 | p->o_arg.u.attrs = &p->attrs; | |
278 | memcpy(&p->attrs, attrs, sizeof(p->attrs)); | |
279 | } | |
cdd4e68b TM |
280 | p->c_arg.fh = &p->o_res.fh; |
281 | p->c_arg.stateid = &p->o_res.stateid; | |
282 | p->c_arg.seqid = p->o_arg.seqid; | |
2ced46c2 | 283 | nfs4_init_opendata_res(p); |
c6d00e63 | 284 | kref_init(&p->kref); |
e56e0b78 TM |
285 | return p; |
286 | err_free: | |
287 | kfree(p); | |
288 | err: | |
289 | dput(parent); | |
290 | return NULL; | |
291 | } | |
292 | ||
c6d00e63 | 293 | static void nfs4_opendata_free(struct kref *kref) |
e56e0b78 | 294 | { |
c6d00e63 TM |
295 | struct nfs4_opendata *p = container_of(kref, |
296 | struct nfs4_opendata, kref); | |
297 | ||
298 | nfs_free_seqid(p->o_arg.seqid); | |
299 | nfs4_put_state_owner(p->owner); | |
300 | dput(p->dir); | |
301 | dput(p->path.dentry); | |
302 | mntput(p->path.mnt); | |
303 | kfree(p); | |
304 | } | |
305 | ||
306 | static void nfs4_opendata_put(struct nfs4_opendata *p) | |
307 | { | |
308 | if (p != NULL) | |
309 | kref_put(&p->kref, nfs4_opendata_free); | |
e56e0b78 TM |
310 | } |
311 | ||
06f814a3 TM |
312 | static int nfs4_wait_for_completion_rpc_task(struct rpc_task *task) |
313 | { | |
314 | sigset_t oldset; | |
315 | int ret; | |
316 | ||
317 | rpc_clnt_sigmask(task->tk_client, &oldset); | |
318 | ret = rpc_wait_for_completion_task(task); | |
319 | rpc_clnt_sigunmask(task->tk_client, &oldset); | |
320 | return ret; | |
321 | } | |
322 | ||
003707c7 | 323 | static void update_open_stateflags(struct nfs4_state *state, mode_t open_flags) |
e7616923 TM |
324 | { |
325 | switch (open_flags) { | |
326 | case FMODE_WRITE: | |
327 | state->n_wronly++; | |
328 | break; | |
329 | case FMODE_READ: | |
330 | state->n_rdonly++; | |
331 | break; | |
332 | case FMODE_READ|FMODE_WRITE: | |
333 | state->n_rdwr++; | |
334 | } | |
003707c7 TM |
335 | nfs4_state_set_mode_locked(state, state->state | open_flags); |
336 | } | |
337 | ||
338 | static void nfs_set_open_stateid_locked(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags) | |
339 | { | |
340 | if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) | |
341 | memcpy(state->stateid.data, stateid->data, sizeof(state->stateid.data)); | |
342 | memcpy(state->open_stateid.data, stateid->data, sizeof(state->open_stateid.data)); | |
343 | switch (open_flags) { | |
344 | case FMODE_READ: | |
345 | set_bit(NFS_O_RDONLY_STATE, &state->flags); | |
346 | break; | |
347 | case FMODE_WRITE: | |
348 | set_bit(NFS_O_WRONLY_STATE, &state->flags); | |
349 | break; | |
350 | case FMODE_READ|FMODE_WRITE: | |
351 | set_bit(NFS_O_RDWR_STATE, &state->flags); | |
352 | } | |
e7616923 TM |
353 | } |
354 | ||
003707c7 TM |
355 | static void nfs_set_open_stateid(struct nfs4_state *state, nfs4_stateid *stateid, int open_flags) |
356 | { | |
357 | spin_lock(&state->owner->so_lock); | |
358 | spin_lock(&state->inode->i_lock); | |
359 | nfs_set_open_stateid_locked(state, stateid, open_flags); | |
360 | spin_unlock(&state->inode->i_lock); | |
361 | spin_unlock(&state->owner->so_lock); | |
362 | } | |
363 | ||
364 | static void update_open_stateid(struct nfs4_state *state, nfs4_stateid *open_stateid, nfs4_stateid *deleg_stateid, int open_flags) | |
1da177e4 LT |
365 | { |
366 | struct inode *inode = state->inode; | |
367 | ||
368 | open_flags &= (FMODE_READ|FMODE_WRITE); | |
d530838b | 369 | /* Protect against nfs4_find_state_byowner() */ |
ec073428 | 370 | spin_lock(&state->owner->so_lock); |
1da177e4 | 371 | spin_lock(&inode->i_lock); |
003707c7 TM |
372 | if (deleg_stateid != NULL) { |
373 | memcpy(state->stateid.data, deleg_stateid->data, sizeof(state->stateid.data)); | |
374 | set_bit(NFS_DELEGATED_STATE, &state->flags); | |
375 | } | |
376 | if (open_stateid != NULL) | |
377 | nfs_set_open_stateid_locked(state, open_stateid, open_flags); | |
e7616923 | 378 | update_open_stateflags(state, open_flags); |
1da177e4 | 379 | spin_unlock(&inode->i_lock); |
ec073428 | 380 | spin_unlock(&state->owner->so_lock); |
1da177e4 LT |
381 | } |
382 | ||
24ac23ab TM |
383 | static struct nfs4_state *nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data) |
384 | { | |
385 | struct inode *inode; | |
386 | struct nfs4_state *state = NULL; | |
003707c7 TM |
387 | struct nfs_delegation *delegation; |
388 | nfs4_stateid *deleg_stateid = NULL; | |
1b370bc2 | 389 | int ret; |
24ac23ab | 390 | |
1b370bc2 | 391 | ret = -EAGAIN; |
24ac23ab | 392 | if (!(data->f_attr.valid & NFS_ATTR_FATTR)) |
1b370bc2 | 393 | goto err; |
24ac23ab | 394 | inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh, &data->f_attr); |
1b370bc2 | 395 | ret = PTR_ERR(inode); |
03f28e3a | 396 | if (IS_ERR(inode)) |
1b370bc2 TM |
397 | goto err; |
398 | ret = -ENOMEM; | |
24ac23ab TM |
399 | state = nfs4_get_open_state(inode, data->owner); |
400 | if (state == NULL) | |
1b370bc2 | 401 | goto err_put_inode; |
549d6ed5 | 402 | if (data->o_res.delegation_type != 0) { |
549d6ed5 TM |
403 | int delegation_flags = 0; |
404 | ||
003707c7 TM |
405 | rcu_read_lock(); |
406 | delegation = rcu_dereference(NFS_I(inode)->delegation); | |
407 | if (delegation) | |
408 | delegation_flags = delegation->flags; | |
409 | rcu_read_unlock(); | |
549d6ed5 TM |
410 | if (!(delegation_flags & NFS_DELEGATION_NEED_RECLAIM)) |
411 | nfs_inode_set_delegation(state->inode, | |
412 | data->owner->so_cred, | |
413 | &data->o_res); | |
414 | else | |
415 | nfs_inode_reclaim_delegation(state->inode, | |
416 | data->owner->so_cred, | |
417 | &data->o_res); | |
418 | } | |
003707c7 TM |
419 | rcu_read_lock(); |
420 | delegation = rcu_dereference(NFS_I(inode)->delegation); | |
421 | if (delegation != NULL) | |
422 | deleg_stateid = &delegation->stateid; | |
423 | update_open_stateid(state, &data->o_res.stateid, deleg_stateid, data->o_arg.open_flags); | |
424 | rcu_read_unlock(); | |
24ac23ab | 425 | iput(inode); |
24ac23ab | 426 | return state; |
1b370bc2 TM |
427 | err_put_inode: |
428 | iput(inode); | |
429 | err: | |
430 | return ERR_PTR(ret); | |
24ac23ab TM |
431 | } |
432 | ||
864472e9 TM |
433 | static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state) |
434 | { | |
435 | struct nfs_inode *nfsi = NFS_I(state->inode); | |
436 | struct nfs_open_context *ctx; | |
437 | ||
438 | spin_lock(&state->inode->i_lock); | |
439 | list_for_each_entry(ctx, &nfsi->open_files, list) { | |
440 | if (ctx->state != state) | |
441 | continue; | |
442 | get_nfs_open_context(ctx); | |
443 | spin_unlock(&state->inode->i_lock); | |
444 | return ctx; | |
445 | } | |
446 | spin_unlock(&state->inode->i_lock); | |
447 | return ERR_PTR(-ENOENT); | |
448 | } | |
449 | ||
2ced46c2 | 450 | static int nfs4_open_recover_helper(struct nfs4_opendata *opendata, mode_t openflags, struct nfs4_state **res) |
864472e9 | 451 | { |
2ced46c2 | 452 | struct nfs4_state *newstate; |
864472e9 TM |
453 | int ret; |
454 | ||
455 | opendata->o_arg.open_flags = openflags; | |
2ced46c2 TM |
456 | memset(&opendata->o_res, 0, sizeof(opendata->o_res)); |
457 | memset(&opendata->c_res, 0, sizeof(opendata->c_res)); | |
458 | nfs4_init_opendata_res(opendata); | |
864472e9 TM |
459 | ret = _nfs4_proc_open(opendata); |
460 | if (ret != 0) | |
461 | return ret; | |
2ced46c2 | 462 | newstate = nfs4_opendata_to_nfs4_state(opendata); |
1b370bc2 TM |
463 | if (IS_ERR(newstate)) |
464 | return PTR_ERR(newstate); | |
465 | nfs4_close_state(&opendata->path, newstate, openflags); | |
2ced46c2 | 466 | *res = newstate; |
864472e9 TM |
467 | return 0; |
468 | } | |
469 | ||
470 | static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state) | |
471 | { | |
864472e9 | 472 | struct nfs4_state *newstate; |
864472e9 TM |
473 | int ret; |
474 | ||
475 | /* memory barrier prior to reading state->n_* */ | |
2ced46c2 | 476 | clear_bit(NFS_DELEGATED_STATE, &state->flags); |
864472e9 TM |
477 | smp_rmb(); |
478 | if (state->n_rdwr != 0) { | |
2ced46c2 | 479 | ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE, &newstate); |
864472e9 TM |
480 | if (ret != 0) |
481 | return ret; | |
2ced46c2 TM |
482 | if (newstate != state) |
483 | return -ESTALE; | |
864472e9 TM |
484 | } |
485 | if (state->n_wronly != 0) { | |
2ced46c2 | 486 | ret = nfs4_open_recover_helper(opendata, FMODE_WRITE, &newstate); |
864472e9 TM |
487 | if (ret != 0) |
488 | return ret; | |
2ced46c2 TM |
489 | if (newstate != state) |
490 | return -ESTALE; | |
864472e9 TM |
491 | } |
492 | if (state->n_rdonly != 0) { | |
2ced46c2 | 493 | ret = nfs4_open_recover_helper(opendata, FMODE_READ, &newstate); |
864472e9 TM |
494 | if (ret != 0) |
495 | return ret; | |
2ced46c2 TM |
496 | if (newstate != state) |
497 | return -ESTALE; | |
864472e9 | 498 | } |
864472e9 TM |
499 | return 0; |
500 | } | |
501 | ||
1da177e4 LT |
502 | /* |
503 | * OPEN_RECLAIM: | |
504 | * reclaim state on the server after a reboot. | |
1da177e4 | 505 | */ |
539cd03a | 506 | static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state) |
1da177e4 | 507 | { |
864472e9 TM |
508 | struct nfs_delegation *delegation = NFS_I(state->inode)->delegation; |
509 | struct nfs4_opendata *opendata; | |
510 | int delegation_type = 0; | |
1da177e4 LT |
511 | int status; |
512 | ||
513 | if (delegation != NULL) { | |
514 | if (!(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) { | |
515 | memcpy(&state->stateid, &delegation->stateid, | |
516 | sizeof(state->stateid)); | |
517 | set_bit(NFS_DELEGATED_STATE, &state->flags); | |
518 | return 0; | |
519 | } | |
864472e9 | 520 | delegation_type = delegation->type; |
1da177e4 | 521 | } |
ad389da7 | 522 | opendata = nfs4_opendata_alloc(&ctx->path, state->owner, 0, NULL); |
864472e9 | 523 | if (opendata == NULL) |
cee54fc9 | 524 | return -ENOMEM; |
864472e9 TM |
525 | opendata->o_arg.claim = NFS4_OPEN_CLAIM_PREVIOUS; |
526 | opendata->o_arg.fh = NFS_FH(state->inode); | |
527 | nfs_copy_fh(&opendata->o_res.fh, opendata->o_arg.fh); | |
528 | opendata->o_arg.u.delegation_type = delegation_type; | |
529 | status = nfs4_open_recover(opendata, state); | |
c6d00e63 | 530 | nfs4_opendata_put(opendata); |
1da177e4 LT |
531 | return status; |
532 | } | |
533 | ||
539cd03a | 534 | static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state) |
1da177e4 LT |
535 | { |
536 | struct nfs_server *server = NFS_SERVER(state->inode); | |
537 | struct nfs4_exception exception = { }; | |
538 | int err; | |
539 | do { | |
539cd03a | 540 | err = _nfs4_do_open_reclaim(ctx, state); |
202b50dc TM |
541 | if (err != -NFS4ERR_DELAY) |
542 | break; | |
543 | nfs4_handle_exception(server, err, &exception); | |
1da177e4 LT |
544 | } while (exception.retry); |
545 | return err; | |
546 | } | |
547 | ||
864472e9 TM |
548 | static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state) |
549 | { | |
550 | struct nfs_open_context *ctx; | |
551 | int ret; | |
552 | ||
553 | ctx = nfs4_state_find_open_context(state); | |
554 | if (IS_ERR(ctx)) | |
555 | return PTR_ERR(ctx); | |
539cd03a | 556 | ret = nfs4_do_open_reclaim(ctx, state); |
864472e9 TM |
557 | put_nfs_open_context(ctx); |
558 | return ret; | |
559 | } | |
560 | ||
13437e12 | 561 | static int _nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid) |
1da177e4 LT |
562 | { |
563 | struct nfs4_state_owner *sp = state->owner; | |
e56e0b78 | 564 | struct nfs4_opendata *opendata; |
864472e9 | 565 | int ret; |
1da177e4 | 566 | |
ad389da7 | 567 | opendata = nfs4_opendata_alloc(&ctx->path, sp, 0, NULL); |
e56e0b78 | 568 | if (opendata == NULL) |
864472e9 | 569 | return -ENOMEM; |
e56e0b78 | 570 | opendata->o_arg.claim = NFS4_OPEN_CLAIM_DELEGATE_CUR; |
13437e12 | 571 | memcpy(opendata->o_arg.u.delegation.data, stateid->data, |
e56e0b78 | 572 | sizeof(opendata->o_arg.u.delegation.data)); |
864472e9 | 573 | ret = nfs4_open_recover(opendata, state); |
c6d00e63 | 574 | nfs4_opendata_put(opendata); |
864472e9 | 575 | return ret; |
1da177e4 LT |
576 | } |
577 | ||
13437e12 | 578 | int nfs4_open_delegation_recall(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid) |
1da177e4 LT |
579 | { |
580 | struct nfs4_exception exception = { }; | |
539cd03a | 581 | struct nfs_server *server = NFS_SERVER(state->inode); |
1da177e4 LT |
582 | int err; |
583 | do { | |
13437e12 | 584 | err = _nfs4_open_delegation_recall(ctx, state, stateid); |
1da177e4 LT |
585 | switch (err) { |
586 | case 0: | |
587 | return err; | |
588 | case -NFS4ERR_STALE_CLIENTID: | |
589 | case -NFS4ERR_STALE_STATEID: | |
590 | case -NFS4ERR_EXPIRED: | |
591 | /* Don't recall a delegation if it was lost */ | |
7539bbab | 592 | nfs4_schedule_state_recovery(server->nfs_client); |
1da177e4 LT |
593 | return err; |
594 | } | |
595 | err = nfs4_handle_exception(server, err, &exception); | |
596 | } while (exception.retry); | |
597 | return err; | |
598 | } | |
599 | ||
cdd4e68b | 600 | static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata) |
1da177e4 | 601 | { |
cdd4e68b TM |
602 | struct nfs4_opendata *data = calldata; |
603 | struct rpc_message msg = { | |
604 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM], | |
605 | .rpc_argp = &data->c_arg, | |
606 | .rpc_resp = &data->c_res, | |
607 | .rpc_cred = data->owner->so_cred, | |
1da177e4 | 608 | }; |
26e976a8 | 609 | data->timestamp = jiffies; |
cdd4e68b TM |
610 | rpc_call_setup(task, &msg, 0); |
611 | } | |
612 | ||
613 | static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata) | |
614 | { | |
615 | struct nfs4_opendata *data = calldata; | |
616 | ||
617 | data->rpc_status = task->tk_status; | |
618 | if (RPC_ASSASSINATED(task)) | |
619 | return; | |
26e976a8 | 620 | if (data->rpc_status == 0) { |
cdd4e68b TM |
621 | memcpy(data->o_res.stateid.data, data->c_res.stateid.data, |
622 | sizeof(data->o_res.stateid.data)); | |
26e976a8 | 623 | renew_lease(data->o_res.server, data->timestamp); |
3e309914 | 624 | data->rpc_done = 1; |
26e976a8 | 625 | } |
cdd4e68b | 626 | nfs_confirm_seqid(&data->owner->so_seqid, data->rpc_status); |
0f9f95e0 | 627 | nfs_increment_open_seqid(data->rpc_status, data->c_arg.seqid); |
cdd4e68b TM |
628 | } |
629 | ||
630 | static void nfs4_open_confirm_release(void *calldata) | |
631 | { | |
632 | struct nfs4_opendata *data = calldata; | |
633 | struct nfs4_state *state = NULL; | |
634 | ||
635 | /* If this request hasn't been cancelled, do nothing */ | |
636 | if (data->cancelled == 0) | |
637 | goto out_free; | |
638 | /* In case of error, no cleanup! */ | |
3e309914 | 639 | if (!data->rpc_done) |
cdd4e68b TM |
640 | goto out_free; |
641 | nfs_confirm_seqid(&data->owner->so_seqid, 0); | |
642 | state = nfs4_opendata_to_nfs4_state(data); | |
1b370bc2 | 643 | if (!IS_ERR(state)) |
4a35bd41 | 644 | nfs4_close_state(&data->path, state, data->o_arg.open_flags); |
cdd4e68b | 645 | out_free: |
c6d00e63 | 646 | nfs4_opendata_put(data); |
cdd4e68b TM |
647 | } |
648 | ||
649 | static const struct rpc_call_ops nfs4_open_confirm_ops = { | |
650 | .rpc_call_prepare = nfs4_open_confirm_prepare, | |
651 | .rpc_call_done = nfs4_open_confirm_done, | |
652 | .rpc_release = nfs4_open_confirm_release, | |
653 | }; | |
654 | ||
655 | /* | |
656 | * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata | |
657 | */ | |
658 | static int _nfs4_proc_open_confirm(struct nfs4_opendata *data) | |
659 | { | |
660 | struct nfs_server *server = NFS_SERVER(data->dir->d_inode); | |
661 | struct rpc_task *task; | |
1da177e4 LT |
662 | int status; |
663 | ||
c6d00e63 | 664 | kref_get(&data->kref); |
3e309914 TM |
665 | data->rpc_done = 0; |
666 | data->rpc_status = 0; | |
cdd4e68b | 667 | task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_open_confirm_ops, data); |
7a1218a2 | 668 | if (IS_ERR(task)) |
cdd4e68b | 669 | return PTR_ERR(task); |
cdd4e68b TM |
670 | status = nfs4_wait_for_completion_rpc_task(task); |
671 | if (status != 0) { | |
672 | data->cancelled = 1; | |
673 | smp_wmb(); | |
674 | } else | |
675 | status = data->rpc_status; | |
e6b3c4db | 676 | rpc_put_task(task); |
1da177e4 LT |
677 | return status; |
678 | } | |
679 | ||
24ac23ab | 680 | static void nfs4_open_prepare(struct rpc_task *task, void *calldata) |
1da177e4 | 681 | { |
24ac23ab TM |
682 | struct nfs4_opendata *data = calldata; |
683 | struct nfs4_state_owner *sp = data->owner; | |
1da177e4 LT |
684 | struct rpc_message msg = { |
685 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN], | |
24ac23ab TM |
686 | .rpc_argp = &data->o_arg, |
687 | .rpc_resp = &data->o_res, | |
1da177e4 LT |
688 | .rpc_cred = sp->so_cred, |
689 | }; | |
24ac23ab TM |
690 | |
691 | if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0) | |
692 | return; | |
693 | /* Update sequence id. */ | |
9f958ab8 | 694 | data->o_arg.id = sp->so_owner_id.id; |
24ac23ab | 695 | data->o_arg.clientid = sp->so_client->cl_clientid; |
864472e9 TM |
696 | if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS) |
697 | msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR]; | |
26e976a8 | 698 | data->timestamp = jiffies; |
24ac23ab TM |
699 | rpc_call_setup(task, &msg, 0); |
700 | } | |
1da177e4 | 701 | |
24ac23ab TM |
702 | static void nfs4_open_done(struct rpc_task *task, void *calldata) |
703 | { | |
704 | struct nfs4_opendata *data = calldata; | |
1da177e4 | 705 | |
24ac23ab TM |
706 | data->rpc_status = task->tk_status; |
707 | if (RPC_ASSASSINATED(task)) | |
708 | return; | |
709 | if (task->tk_status == 0) { | |
710 | switch (data->o_res.f_attr->mode & S_IFMT) { | |
6f926b5b TM |
711 | case S_IFREG: |
712 | break; | |
713 | case S_IFLNK: | |
24ac23ab | 714 | data->rpc_status = -ELOOP; |
6f926b5b TM |
715 | break; |
716 | case S_IFDIR: | |
24ac23ab | 717 | data->rpc_status = -EISDIR; |
6f926b5b TM |
718 | break; |
719 | default: | |
24ac23ab | 720 | data->rpc_status = -ENOTDIR; |
6f926b5b | 721 | } |
26e976a8 | 722 | renew_lease(data->o_res.server, data->timestamp); |
0f9f95e0 TM |
723 | if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)) |
724 | nfs_confirm_seqid(&data->owner->so_seqid, 0); | |
6f926b5b | 725 | } |
24ac23ab | 726 | nfs_increment_open_seqid(data->rpc_status, data->o_arg.seqid); |
3e309914 | 727 | data->rpc_done = 1; |
24ac23ab | 728 | } |
6f926b5b | 729 | |
24ac23ab TM |
730 | static void nfs4_open_release(void *calldata) |
731 | { | |
732 | struct nfs4_opendata *data = calldata; | |
733 | struct nfs4_state *state = NULL; | |
734 | ||
735 | /* If this request hasn't been cancelled, do nothing */ | |
736 | if (data->cancelled == 0) | |
737 | goto out_free; | |
738 | /* In case of error, no cleanup! */ | |
3e309914 | 739 | if (data->rpc_status != 0 || !data->rpc_done) |
24ac23ab TM |
740 | goto out_free; |
741 | /* In case we need an open_confirm, no cleanup! */ | |
742 | if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM) | |
743 | goto out_free; | |
744 | nfs_confirm_seqid(&data->owner->so_seqid, 0); | |
745 | state = nfs4_opendata_to_nfs4_state(data); | |
1b370bc2 | 746 | if (!IS_ERR(state)) |
4a35bd41 | 747 | nfs4_close_state(&data->path, state, data->o_arg.open_flags); |
24ac23ab | 748 | out_free: |
c6d00e63 | 749 | nfs4_opendata_put(data); |
24ac23ab TM |
750 | } |
751 | ||
752 | static const struct rpc_call_ops nfs4_open_ops = { | |
753 | .rpc_call_prepare = nfs4_open_prepare, | |
754 | .rpc_call_done = nfs4_open_done, | |
755 | .rpc_release = nfs4_open_release, | |
756 | }; | |
757 | ||
758 | /* | |
759 | * Note: On error, nfs4_proc_open will free the struct nfs4_opendata | |
760 | */ | |
761 | static int _nfs4_proc_open(struct nfs4_opendata *data) | |
762 | { | |
763 | struct inode *dir = data->dir->d_inode; | |
764 | struct nfs_server *server = NFS_SERVER(dir); | |
765 | struct nfs_openargs *o_arg = &data->o_arg; | |
766 | struct nfs_openres *o_res = &data->o_res; | |
767 | struct rpc_task *task; | |
768 | int status; | |
769 | ||
c6d00e63 | 770 | kref_get(&data->kref); |
3e309914 TM |
771 | data->rpc_done = 0; |
772 | data->rpc_status = 0; | |
2ced46c2 | 773 | data->cancelled = 0; |
24ac23ab | 774 | task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_open_ops, data); |
7a1218a2 | 775 | if (IS_ERR(task)) |
24ac23ab | 776 | return PTR_ERR(task); |
24ac23ab TM |
777 | status = nfs4_wait_for_completion_rpc_task(task); |
778 | if (status != 0) { | |
779 | data->cancelled = 1; | |
780 | smp_wmb(); | |
781 | } else | |
782 | status = data->rpc_status; | |
e6b3c4db | 783 | rpc_put_task(task); |
3e309914 | 784 | if (status != 0 || !data->rpc_done) |
24ac23ab TM |
785 | return status; |
786 | ||
56ae19f3 TM |
787 | if (o_arg->open_flags & O_CREAT) { |
788 | update_changeattr(dir, &o_res->cinfo); | |
789 | nfs_post_op_update_inode(dir, o_res->dir_attr); | |
790 | } else | |
791 | nfs_refresh_inode(dir, o_res->dir_attr); | |
1da177e4 | 792 | if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) { |
cdd4e68b | 793 | status = _nfs4_proc_open_confirm(data); |
1da177e4 | 794 | if (status != 0) |
24ac23ab | 795 | return status; |
1da177e4 LT |
796 | } |
797 | if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) | |
8fa5c000 | 798 | return server->nfs_client->rpc_ops->getattr(server, &o_res->fh, o_res->f_attr); |
24ac23ab | 799 | return 0; |
1da177e4 LT |
800 | } |
801 | ||
802 | static int _nfs4_do_access(struct inode *inode, struct rpc_cred *cred, int openflags) | |
803 | { | |
804 | struct nfs_access_entry cache; | |
805 | int mask = 0; | |
806 | int status; | |
807 | ||
808 | if (openflags & FMODE_READ) | |
809 | mask |= MAY_READ; | |
810 | if (openflags & FMODE_WRITE) | |
811 | mask |= MAY_WRITE; | |
1b45c46c TM |
812 | if (openflags & FMODE_EXEC) |
813 | mask |= MAY_EXEC; | |
1da177e4 LT |
814 | status = nfs_access_get_cached(inode, cred, &cache); |
815 | if (status == 0) | |
816 | goto out; | |
817 | ||
818 | /* Be clever: ask server to check for all possible rights */ | |
819 | cache.mask = MAY_EXEC | MAY_WRITE | MAY_READ; | |
820 | cache.cred = cred; | |
821 | cache.jiffies = jiffies; | |
822 | status = _nfs4_proc_access(inode, &cache); | |
823 | if (status != 0) | |
824 | return status; | |
825 | nfs_access_add_cache(inode, &cache); | |
826 | out: | |
827 | if ((cache.mask & mask) == mask) | |
828 | return 0; | |
829 | return -EACCES; | |
830 | } | |
831 | ||
10afec90 | 832 | static int nfs4_recover_expired_lease(struct nfs_server *server) |
58d9714a | 833 | { |
7539bbab | 834 | struct nfs_client *clp = server->nfs_client; |
6b30954e | 835 | int ret; |
58d9714a | 836 | |
6b30954e TM |
837 | for (;;) { |
838 | ret = nfs4_wait_clnt_recover(server->client, clp); | |
839 | if (ret != 0) | |
840 | return ret; | |
841 | if (!test_and_clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state)) | |
842 | break; | |
58d9714a | 843 | nfs4_schedule_state_recovery(clp); |
6b30954e TM |
844 | } |
845 | return 0; | |
58d9714a TM |
846 | } |
847 | ||
1da177e4 LT |
848 | /* |
849 | * OPEN_EXPIRED: | |
850 | * reclaim state on the server after a network partition. | |
851 | * Assumes caller holds the appropriate lock | |
852 | */ | |
539cd03a | 853 | static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state) |
1da177e4 | 854 | { |
1da177e4 | 855 | struct inode *inode = state->inode; |
1da177e4 | 856 | struct nfs_delegation *delegation = NFS_I(inode)->delegation; |
e56e0b78 | 857 | struct nfs4_opendata *opendata; |
24ac23ab | 858 | int openflags = state->state & (FMODE_READ|FMODE_WRITE); |
864472e9 | 859 | int ret; |
1da177e4 LT |
860 | |
861 | if (delegation != NULL && !(delegation->flags & NFS_DELEGATION_NEED_RECLAIM)) { | |
539cd03a | 862 | ret = _nfs4_do_access(inode, ctx->cred, openflags); |
864472e9 TM |
863 | if (ret < 0) |
864 | return ret; | |
1da177e4 LT |
865 | memcpy(&state->stateid, &delegation->stateid, sizeof(state->stateid)); |
866 | set_bit(NFS_DELEGATED_STATE, &state->flags); | |
864472e9 | 867 | return 0; |
1da177e4 | 868 | } |
ad389da7 | 869 | opendata = nfs4_opendata_alloc(&ctx->path, state->owner, openflags, NULL); |
e56e0b78 | 870 | if (opendata == NULL) |
864472e9 TM |
871 | return -ENOMEM; |
872 | ret = nfs4_open_recover(opendata, state); | |
873 | if (ret == -ESTALE) { | |
874 | /* Invalidate the state owner so we don't ever use it again */ | |
539cd03a TM |
875 | nfs4_drop_state_owner(state->owner); |
876 | d_drop(ctx->path.dentry); | |
1da177e4 | 877 | } |
c6d00e63 | 878 | nfs4_opendata_put(opendata); |
864472e9 | 879 | return ret; |
1da177e4 LT |
880 | } |
881 | ||
539cd03a | 882 | static inline int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state) |
202b50dc | 883 | { |
539cd03a | 884 | struct nfs_server *server = NFS_SERVER(state->inode); |
202b50dc TM |
885 | struct nfs4_exception exception = { }; |
886 | int err; | |
887 | ||
888 | do { | |
539cd03a | 889 | err = _nfs4_open_expired(ctx, state); |
202b50dc TM |
890 | if (err == -NFS4ERR_DELAY) |
891 | nfs4_handle_exception(server, err, &exception); | |
892 | } while (exception.retry); | |
893 | return err; | |
894 | } | |
895 | ||
1da177e4 LT |
896 | static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state) |
897 | { | |
1da177e4 | 898 | struct nfs_open_context *ctx; |
864472e9 | 899 | int ret; |
1da177e4 | 900 | |
864472e9 TM |
901 | ctx = nfs4_state_find_open_context(state); |
902 | if (IS_ERR(ctx)) | |
903 | return PTR_ERR(ctx); | |
539cd03a | 904 | ret = nfs4_do_open_expired(ctx, state); |
864472e9 TM |
905 | put_nfs_open_context(ctx); |
906 | return ret; | |
1da177e4 LT |
907 | } |
908 | ||
909 | /* | |
24ac23ab | 910 | * Returns a referenced nfs4_state if there is an open delegation on the file |
1da177e4 LT |
911 | */ |
912 | static int _nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred, struct nfs4_state **res) | |
913 | { | |
914 | struct nfs_delegation *delegation; | |
915 | struct nfs_server *server = NFS_SERVER(inode); | |
7539bbab | 916 | struct nfs_client *clp = server->nfs_client; |
1da177e4 LT |
917 | struct nfs_inode *nfsi = NFS_I(inode); |
918 | struct nfs4_state_owner *sp = NULL; | |
919 | struct nfs4_state *state = NULL; | |
920 | int open_flags = flags & (FMODE_READ|FMODE_WRITE); | |
921 | int err; | |
922 | ||
b4454fe1 TM |
923 | err = -ENOMEM; |
924 | if (!(sp = nfs4_get_state_owner(server, cred))) { | |
925 | dprintk("%s: nfs4_get_state_owner failed!\n", __FUNCTION__); | |
926 | return err; | |
927 | } | |
58d9714a TM |
928 | err = nfs4_recover_expired_lease(server); |
929 | if (err != 0) | |
b4454fe1 | 930 | goto out_put_state_owner; |
1da177e4 LT |
931 | /* Protect against reboot recovery - NOTE ORDER! */ |
932 | down_read(&clp->cl_sem); | |
933 | /* Protect against delegation recall */ | |
934 | down_read(&nfsi->rwsem); | |
935 | delegation = NFS_I(inode)->delegation; | |
936 | err = -ENOENT; | |
937 | if (delegation == NULL || (delegation->type & open_flags) != open_flags) | |
938 | goto out_err; | |
939 | err = -ENOMEM; | |
1da177e4 LT |
940 | state = nfs4_get_open_state(inode, sp); |
941 | if (state == NULL) | |
942 | goto out_err; | |
943 | ||
944 | err = -ENOENT; | |
945 | if ((state->state & open_flags) == open_flags) { | |
946 | spin_lock(&inode->i_lock); | |
e7616923 | 947 | update_open_stateflags(state, open_flags); |
1da177e4 LT |
948 | spin_unlock(&inode->i_lock); |
949 | goto out_ok; | |
950 | } else if (state->state != 0) | |
b4454fe1 | 951 | goto out_put_open_state; |
1da177e4 LT |
952 | |
953 | lock_kernel(); | |
954 | err = _nfs4_do_access(inode, cred, open_flags); | |
955 | unlock_kernel(); | |
956 | if (err != 0) | |
b4454fe1 | 957 | goto out_put_open_state; |
003707c7 | 958 | update_open_stateid(state, NULL, &delegation->stateid, open_flags); |
1da177e4 | 959 | out_ok: |
1da177e4 LT |
960 | nfs4_put_state_owner(sp); |
961 | up_read(&nfsi->rwsem); | |
962 | up_read(&clp->cl_sem); | |
1da177e4 | 963 | *res = state; |
b4454fe1 TM |
964 | return 0; |
965 | out_put_open_state: | |
966 | nfs4_put_open_state(state); | |
1da177e4 | 967 | out_err: |
1da177e4 LT |
968 | up_read(&nfsi->rwsem); |
969 | up_read(&clp->cl_sem); | |
b8e5c4c2 TM |
970 | if (err != -EACCES) |
971 | nfs_inode_return_delegation(inode); | |
b4454fe1 TM |
972 | out_put_state_owner: |
973 | nfs4_put_state_owner(sp); | |
1da177e4 LT |
974 | return err; |
975 | } | |
976 | ||
977 | static struct nfs4_state *nfs4_open_delegated(struct inode *inode, int flags, struct rpc_cred *cred) | |
978 | { | |
979 | struct nfs4_exception exception = { }; | |
a162a6b8 | 980 | struct nfs4_state *res = ERR_PTR(-EIO); |
1da177e4 LT |
981 | int err; |
982 | ||
983 | do { | |
984 | err = _nfs4_open_delegated(inode, flags, cred, &res); | |
985 | if (err == 0) | |
986 | break; | |
987 | res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(inode), | |
988 | err, &exception)); | |
989 | } while (exception.retry); | |
990 | return res; | |
991 | } | |
992 | ||
aa53ed54 JL |
993 | /* |
994 | * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-* | |
995 | * fields corresponding to attributes that were used to store the verifier. | |
996 | * Make sure we clobber those fields in the later setattr call | |
997 | */ | |
998 | static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata, struct iattr *sattr) | |
999 | { | |
1000 | if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_ACCESS) && | |
1001 | !(sattr->ia_valid & ATTR_ATIME_SET)) | |
1002 | sattr->ia_valid |= ATTR_ATIME; | |
1003 | ||
1004 | if ((opendata->o_res.attrset[1] & FATTR4_WORD1_TIME_MODIFY) && | |
1005 | !(sattr->ia_valid & ATTR_MTIME_SET)) | |
1006 | sattr->ia_valid |= ATTR_MTIME; | |
1007 | } | |
1008 | ||
1da177e4 | 1009 | /* |
24ac23ab | 1010 | * Returns a referenced nfs4_state |
1da177e4 | 1011 | */ |
ad389da7 | 1012 | static int _nfs4_do_open(struct inode *dir, struct path *path, int flags, struct iattr *sattr, struct rpc_cred *cred, struct nfs4_state **res) |
1da177e4 LT |
1013 | { |
1014 | struct nfs4_state_owner *sp; | |
1015 | struct nfs4_state *state = NULL; | |
1016 | struct nfs_server *server = NFS_SERVER(dir); | |
7539bbab | 1017 | struct nfs_client *clp = server->nfs_client; |
e56e0b78 | 1018 | struct nfs4_opendata *opendata; |
1da177e4 | 1019 | int status; |
1da177e4 LT |
1020 | |
1021 | /* Protect against reboot recovery conflicts */ | |
1da177e4 LT |
1022 | status = -ENOMEM; |
1023 | if (!(sp = nfs4_get_state_owner(server, cred))) { | |
1024 | dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n"); | |
1025 | goto out_err; | |
1026 | } | |
58d9714a TM |
1027 | status = nfs4_recover_expired_lease(server); |
1028 | if (status != 0) | |
b4454fe1 | 1029 | goto err_put_state_owner; |
58d9714a TM |
1030 | down_read(&clp->cl_sem); |
1031 | status = -ENOMEM; | |
ad389da7 | 1032 | opendata = nfs4_opendata_alloc(path, sp, flags, sattr); |
e56e0b78 | 1033 | if (opendata == NULL) |
76723de0 | 1034 | goto err_release_rwsem; |
1da177e4 | 1035 | |
24ac23ab | 1036 | status = _nfs4_proc_open(opendata); |
1da177e4 | 1037 | if (status != 0) |
c6d00e63 | 1038 | goto err_opendata_put; |
1da177e4 | 1039 | |
aa53ed54 JL |
1040 | if (opendata->o_arg.open_flags & O_EXCL) |
1041 | nfs4_exclusive_attrset(opendata, sattr); | |
1042 | ||
24ac23ab | 1043 | state = nfs4_opendata_to_nfs4_state(opendata); |
1b370bc2 TM |
1044 | status = PTR_ERR(state); |
1045 | if (IS_ERR(state)) | |
c6d00e63 | 1046 | goto err_opendata_put; |
c6d00e63 | 1047 | nfs4_opendata_put(opendata); |
1da177e4 LT |
1048 | nfs4_put_state_owner(sp); |
1049 | up_read(&clp->cl_sem); | |
1050 | *res = state; | |
1051 | return 0; | |
c6d00e63 TM |
1052 | err_opendata_put: |
1053 | nfs4_opendata_put(opendata); | |
76723de0 TM |
1054 | err_release_rwsem: |
1055 | up_read(&clp->cl_sem); | |
e56e0b78 TM |
1056 | err_put_state_owner: |
1057 | nfs4_put_state_owner(sp); | |
1da177e4 | 1058 | out_err: |
1da177e4 LT |
1059 | *res = NULL; |
1060 | return status; | |
1061 | } | |
1062 | ||
1063 | ||
ad389da7 | 1064 | static struct nfs4_state *nfs4_do_open(struct inode *dir, struct path *path, int flags, struct iattr *sattr, struct rpc_cred *cred) |
1da177e4 LT |
1065 | { |
1066 | struct nfs4_exception exception = { }; | |
1067 | struct nfs4_state *res; | |
1068 | int status; | |
1069 | ||
1070 | do { | |
ad389da7 | 1071 | status = _nfs4_do_open(dir, path, flags, sattr, cred, &res); |
1da177e4 LT |
1072 | if (status == 0) |
1073 | break; | |
1074 | /* NOTE: BAD_SEQID means the server and client disagree about the | |
1075 | * book-keeping w.r.t. state-changing operations | |
1076 | * (OPEN/CLOSE/LOCK/LOCKU...) | |
1077 | * It is actually a sign of a bug on the client or on the server. | |
1078 | * | |
1079 | * If we receive a BAD_SEQID error in the particular case of | |
cee54fc9 | 1080 | * doing an OPEN, we assume that nfs_increment_open_seqid() will |
1da177e4 LT |
1081 | * have unhashed the old state_owner for us, and that we can |
1082 | * therefore safely retry using a new one. We should still warn | |
1083 | * the user though... | |
1084 | */ | |
1085 | if (status == -NFS4ERR_BAD_SEQID) { | |
6f43ddcc TM |
1086 | printk(KERN_WARNING "NFS: v4 server %s " |
1087 | " returned a bad sequence-id error!\n", | |
1088 | NFS_SERVER(dir)->nfs_client->cl_hostname); | |
1da177e4 LT |
1089 | exception.retry = 1; |
1090 | continue; | |
1091 | } | |
550f5747 TM |
1092 | /* |
1093 | * BAD_STATEID on OPEN means that the server cancelled our | |
1094 | * state before it received the OPEN_CONFIRM. | |
1095 | * Recover by retrying the request as per the discussion | |
1096 | * on Page 181 of RFC3530. | |
1097 | */ | |
1098 | if (status == -NFS4ERR_BAD_STATEID) { | |
1099 | exception.retry = 1; | |
1100 | continue; | |
1101 | } | |
1da177e4 LT |
1102 | res = ERR_PTR(nfs4_handle_exception(NFS_SERVER(dir), |
1103 | status, &exception)); | |
1104 | } while (exception.retry); | |
1105 | return res; | |
1106 | } | |
1107 | ||
3e4f6290 TM |
1108 | static int _nfs4_do_setattr(struct inode *inode, struct nfs_fattr *fattr, |
1109 | struct iattr *sattr, struct nfs4_state *state) | |
1da177e4 | 1110 | { |
3e4f6290 | 1111 | struct nfs_server *server = NFS_SERVER(inode); |
1da177e4 | 1112 | struct nfs_setattrargs arg = { |
3e4f6290 | 1113 | .fh = NFS_FH(inode), |
1da177e4 LT |
1114 | .iap = sattr, |
1115 | .server = server, | |
1116 | .bitmask = server->attr_bitmask, | |
1117 | }; | |
1118 | struct nfs_setattrres res = { | |
1119 | .fattr = fattr, | |
1120 | .server = server, | |
1121 | }; | |
1122 | struct rpc_message msg = { | |
1123 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR], | |
1124 | .rpc_argp = &arg, | |
1125 | .rpc_resp = &res, | |
1126 | }; | |
26e976a8 | 1127 | unsigned long timestamp = jiffies; |
65e4308d | 1128 | int status; |
1da177e4 | 1129 | |
0e574af1 | 1130 | nfs_fattr_init(fattr); |
1da177e4 | 1131 | |
3e4f6290 TM |
1132 | if (nfs4_copy_delegation_stateid(&arg.stateid, inode)) { |
1133 | /* Use that stateid */ | |
1134 | } else if (state != NULL) { | |
1da177e4 | 1135 | msg.rpc_cred = state->owner->so_cred; |
08e9eac4 TM |
1136 | nfs4_copy_stateid(&arg.stateid, state, current->files); |
1137 | } else | |
1da177e4 LT |
1138 | memcpy(&arg.stateid, &zero_stateid, sizeof(arg.stateid)); |
1139 | ||
65e4308d | 1140 | status = rpc_call_sync(server->client, &msg, 0); |
26e976a8 TM |
1141 | if (status == 0 && state != NULL) |
1142 | renew_lease(server, timestamp); | |
65e4308d | 1143 | return status; |
1da177e4 LT |
1144 | } |
1145 | ||
3e4f6290 TM |
1146 | static int nfs4_do_setattr(struct inode *inode, struct nfs_fattr *fattr, |
1147 | struct iattr *sattr, struct nfs4_state *state) | |
1da177e4 | 1148 | { |
3e4f6290 | 1149 | struct nfs_server *server = NFS_SERVER(inode); |
1da177e4 LT |
1150 | struct nfs4_exception exception = { }; |
1151 | int err; | |
1152 | do { | |
1153 | err = nfs4_handle_exception(server, | |
3e4f6290 | 1154 | _nfs4_do_setattr(inode, fattr, sattr, state), |
1da177e4 LT |
1155 | &exception); |
1156 | } while (exception.retry); | |
1157 | return err; | |
1158 | } | |
1159 | ||
1160 | struct nfs4_closedata { | |
4a35bd41 | 1161 | struct path path; |
1da177e4 LT |
1162 | struct inode *inode; |
1163 | struct nfs4_state *state; | |
1164 | struct nfs_closeargs arg; | |
1165 | struct nfs_closeres res; | |
516a6af6 | 1166 | struct nfs_fattr fattr; |
26e976a8 | 1167 | unsigned long timestamp; |
1da177e4 LT |
1168 | }; |
1169 | ||
963d8fe5 | 1170 | static void nfs4_free_closedata(void *data) |
9512135d | 1171 | { |
963d8fe5 TM |
1172 | struct nfs4_closedata *calldata = data; |
1173 | struct nfs4_state_owner *sp = calldata->state->owner; | |
9512135d TM |
1174 | |
1175 | nfs4_put_open_state(calldata->state); | |
1176 | nfs_free_seqid(calldata->arg.seqid); | |
9512135d | 1177 | nfs4_put_state_owner(sp); |
4a35bd41 TM |
1178 | dput(calldata->path.dentry); |
1179 | mntput(calldata->path.mnt); | |
9512135d TM |
1180 | kfree(calldata); |
1181 | } | |
1182 | ||
963d8fe5 | 1183 | static void nfs4_close_done(struct rpc_task *task, void *data) |
1da177e4 | 1184 | { |
963d8fe5 | 1185 | struct nfs4_closedata *calldata = data; |
1da177e4 | 1186 | struct nfs4_state *state = calldata->state; |
1da177e4 LT |
1187 | struct nfs_server *server = NFS_SERVER(calldata->inode); |
1188 | ||
4ce70ada TM |
1189 | if (RPC_ASSASSINATED(task)) |
1190 | return; | |
1da177e4 LT |
1191 | /* hmm. we are done with the inode, and in the process of freeing |
1192 | * the state_owner. we keep this around to process errors | |
1193 | */ | |
cee54fc9 | 1194 | nfs_increment_open_seqid(task->tk_status, calldata->arg.seqid); |
1da177e4 LT |
1195 | switch (task->tk_status) { |
1196 | case 0: | |
003707c7 | 1197 | nfs_set_open_stateid(state, &calldata->res.stateid, calldata->arg.open_flags); |
26e976a8 | 1198 | renew_lease(server, calldata->timestamp); |
1da177e4 LT |
1199 | break; |
1200 | case -NFS4ERR_STALE_STATEID: | |
1201 | case -NFS4ERR_EXPIRED: | |
1da177e4 LT |
1202 | break; |
1203 | default: | |
1204 | if (nfs4_async_handle_error(task, server) == -EAGAIN) { | |
1205 | rpc_restart_call(task); | |
1206 | return; | |
1207 | } | |
1208 | } | |
516a6af6 | 1209 | nfs_refresh_inode(calldata->inode, calldata->res.fattr); |
1da177e4 LT |
1210 | } |
1211 | ||
4ce70ada | 1212 | static void nfs4_close_prepare(struct rpc_task *task, void *data) |
1da177e4 | 1213 | { |
4ce70ada | 1214 | struct nfs4_closedata *calldata = data; |
9512135d | 1215 | struct nfs4_state *state = calldata->state; |
1da177e4 LT |
1216 | struct rpc_message msg = { |
1217 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE], | |
1218 | .rpc_argp = &calldata->arg, | |
1219 | .rpc_resp = &calldata->res, | |
9512135d | 1220 | .rpc_cred = state->owner->so_cred, |
1da177e4 | 1221 | }; |
003707c7 TM |
1222 | int clear_rd, clear_wr, clear_rdwr; |
1223 | int mode; | |
9512135d | 1224 | |
963d8fe5 | 1225 | if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0) |
9512135d | 1226 | return; |
003707c7 TM |
1227 | |
1228 | mode = FMODE_READ|FMODE_WRITE; | |
1229 | clear_rd = clear_wr = clear_rdwr = 0; | |
4cecb76f TM |
1230 | spin_lock(&state->owner->so_lock); |
1231 | spin_lock(&calldata->inode->i_lock); | |
003707c7 | 1232 | /* Calculate the change in open mode */ |
e7616923 | 1233 | if (state->n_rdwr == 0) { |
003707c7 | 1234 | if (state->n_rdonly == 0) { |
e7616923 | 1235 | mode &= ~FMODE_READ; |
003707c7 TM |
1236 | clear_rd |= test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags); |
1237 | clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags); | |
1238 | } | |
1239 | if (state->n_wronly == 0) { | |
e7616923 | 1240 | mode &= ~FMODE_WRITE; |
003707c7 TM |
1241 | clear_wr |= test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags); |
1242 | clear_rdwr |= test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags); | |
1243 | } | |
e7616923 | 1244 | } |
4cecb76f TM |
1245 | spin_unlock(&calldata->inode->i_lock); |
1246 | spin_unlock(&state->owner->so_lock); | |
003707c7 | 1247 | if (!clear_rd && !clear_wr && !clear_rdwr) { |
963d8fe5 TM |
1248 | /* Note: exit _without_ calling nfs4_close_done */ |
1249 | task->tk_action = NULL; | |
9512135d TM |
1250 | return; |
1251 | } | |
516a6af6 | 1252 | nfs_fattr_init(calldata->res.fattr); |
9512135d | 1253 | if (mode != 0) |
1da177e4 | 1254 | msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE]; |
9512135d | 1255 | calldata->arg.open_flags = mode; |
26e976a8 | 1256 | calldata->timestamp = jiffies; |
9512135d | 1257 | rpc_call_setup(task, &msg, 0); |
1da177e4 LT |
1258 | } |
1259 | ||
963d8fe5 | 1260 | static const struct rpc_call_ops nfs4_close_ops = { |
4ce70ada | 1261 | .rpc_call_prepare = nfs4_close_prepare, |
963d8fe5 TM |
1262 | .rpc_call_done = nfs4_close_done, |
1263 | .rpc_release = nfs4_free_closedata, | |
1264 | }; | |
1265 | ||
1da177e4 LT |
1266 | /* |
1267 | * It is possible for data to be read/written from a mem-mapped file | |
1268 | * after the sys_close call (which hits the vfs layer as a flush). | |
1269 | * This means that we can't safely call nfsv4 close on a file until | |
1270 | * the inode is cleared. This in turn means that we are not good | |
1271 | * NFSv4 citizens - we do not indicate to the server to update the file's | |
1272 | * share state even when we are done with one of the three share | |
1273 | * stateid's in the inode. | |
1274 | * | |
1275 | * NOTE: Caller must be holding the sp->so_owner semaphore! | |
1276 | */ | |
4a35bd41 | 1277 | int nfs4_do_close(struct path *path, struct nfs4_state *state) |
1da177e4 | 1278 | { |
4a35bd41 | 1279 | struct nfs_server *server = NFS_SERVER(state->inode); |
1da177e4 | 1280 | struct nfs4_closedata *calldata; |
b39e625b TM |
1281 | struct nfs4_state_owner *sp = state->owner; |
1282 | struct rpc_task *task; | |
9512135d | 1283 | int status = -ENOMEM; |
1da177e4 | 1284 | |
9512135d | 1285 | calldata = kmalloc(sizeof(*calldata), GFP_KERNEL); |
1da177e4 | 1286 | if (calldata == NULL) |
9512135d | 1287 | goto out; |
4a35bd41 | 1288 | calldata->inode = state->inode; |
1da177e4 | 1289 | calldata->state = state; |
4a35bd41 | 1290 | calldata->arg.fh = NFS_FH(state->inode); |
003707c7 | 1291 | calldata->arg.stateid = &state->open_stateid; |
1da177e4 | 1292 | /* Serialization for the sequence id */ |
cee54fc9 | 1293 | calldata->arg.seqid = nfs_alloc_seqid(&state->owner->so_seqid); |
9512135d TM |
1294 | if (calldata->arg.seqid == NULL) |
1295 | goto out_free_calldata; | |
516a6af6 TM |
1296 | calldata->arg.bitmask = server->attr_bitmask; |
1297 | calldata->res.fattr = &calldata->fattr; | |
1298 | calldata->res.server = server; | |
4a35bd41 TM |
1299 | calldata->path.mnt = mntget(path->mnt); |
1300 | calldata->path.dentry = dget(path->dentry); | |
9512135d | 1301 | |
b39e625b TM |
1302 | task = rpc_run_task(server->client, RPC_TASK_ASYNC, &nfs4_close_ops, calldata); |
1303 | if (IS_ERR(task)) | |
1304 | return PTR_ERR(task); | |
1305 | rpc_put_task(task); | |
1306 | return 0; | |
9512135d TM |
1307 | out_free_calldata: |
1308 | kfree(calldata); | |
1309 | out: | |
b39e625b TM |
1310 | nfs4_put_open_state(state); |
1311 | nfs4_put_state_owner(sp); | |
9512135d | 1312 | return status; |
1da177e4 LT |
1313 | } |
1314 | ||
4a35bd41 | 1315 | static int nfs4_intent_set_file(struct nameidata *nd, struct path *path, struct nfs4_state *state) |
02a913a7 TM |
1316 | { |
1317 | struct file *filp; | |
1b45c46c | 1318 | int ret; |
02a913a7 | 1319 | |
1b45c46c TM |
1320 | /* If the open_intent is for execute, we have an extra check to make */ |
1321 | if (nd->intent.open.flags & FMODE_EXEC) { | |
1322 | ret = _nfs4_do_access(state->inode, | |
1323 | state->owner->so_cred, | |
1324 | nd->intent.open.flags); | |
1325 | if (ret < 0) | |
1326 | goto out_close; | |
1327 | } | |
4a35bd41 | 1328 | filp = lookup_instantiate_filp(nd, path->dentry, NULL); |
02a913a7 TM |
1329 | if (!IS_ERR(filp)) { |
1330 | struct nfs_open_context *ctx; | |
1331 | ctx = (struct nfs_open_context *)filp->private_data; | |
1332 | ctx->state = state; | |
95cf959b TM |
1333 | return 0; |
1334 | } | |
1b45c46c TM |
1335 | ret = PTR_ERR(filp); |
1336 | out_close: | |
4a35bd41 | 1337 | nfs4_close_state(path, state, nd->intent.open.flags); |
1b45c46c | 1338 | return ret; |
02a913a7 TM |
1339 | } |
1340 | ||
1341 | struct dentry * | |
1da177e4 LT |
1342 | nfs4_atomic_open(struct inode *dir, struct dentry *dentry, struct nameidata *nd) |
1343 | { | |
ad389da7 TM |
1344 | struct path path = { |
1345 | .mnt = nd->mnt, | |
1346 | .dentry = dentry, | |
1347 | }; | |
1da177e4 LT |
1348 | struct iattr attr; |
1349 | struct rpc_cred *cred; | |
1350 | struct nfs4_state *state; | |
02a913a7 | 1351 | struct dentry *res; |
1da177e4 LT |
1352 | |
1353 | if (nd->flags & LOOKUP_CREATE) { | |
1354 | attr.ia_mode = nd->intent.open.create_mode; | |
1355 | attr.ia_valid = ATTR_MODE; | |
1356 | if (!IS_POSIXACL(dir)) | |
1357 | attr.ia_mode &= ~current->fs->umask; | |
1358 | } else { | |
1359 | attr.ia_valid = 0; | |
1360 | BUG_ON(nd->intent.open.flags & O_CREAT); | |
1361 | } | |
1362 | ||
1f163415 | 1363 | cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0); |
1da177e4 | 1364 | if (IS_ERR(cred)) |
02a913a7 | 1365 | return (struct dentry *)cred; |
ad389da7 | 1366 | state = nfs4_do_open(dir, &path, nd->intent.open.flags, &attr, cred); |
1da177e4 | 1367 | put_rpccred(cred); |
02a913a7 TM |
1368 | if (IS_ERR(state)) { |
1369 | if (PTR_ERR(state) == -ENOENT) | |
1370 | d_add(dentry, NULL); | |
1371 | return (struct dentry *)state; | |
1372 | } | |
24ac23ab | 1373 | res = d_add_unique(dentry, igrab(state->inode)); |
02a913a7 TM |
1374 | if (res != NULL) |
1375 | dentry = res; | |
4a35bd41 | 1376 | nfs4_intent_set_file(nd, &path, state); |
02a913a7 | 1377 | return res; |
1da177e4 LT |
1378 | } |
1379 | ||
1380 | int | |
02a913a7 | 1381 | nfs4_open_revalidate(struct inode *dir, struct dentry *dentry, int openflags, struct nameidata *nd) |
1da177e4 | 1382 | { |
ad389da7 TM |
1383 | struct path path = { |
1384 | .mnt = nd->mnt, | |
1385 | .dentry = dentry, | |
1386 | }; | |
1da177e4 LT |
1387 | struct rpc_cred *cred; |
1388 | struct nfs4_state *state; | |
1da177e4 | 1389 | |
1f163415 | 1390 | cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0); |
1da177e4 LT |
1391 | if (IS_ERR(cred)) |
1392 | return PTR_ERR(cred); | |
1393 | state = nfs4_open_delegated(dentry->d_inode, openflags, cred); | |
1394 | if (IS_ERR(state)) | |
ad389da7 | 1395 | state = nfs4_do_open(dir, &path, openflags, NULL, cred); |
1da177e4 | 1396 | put_rpccred(cred); |
02a913a7 TM |
1397 | if (IS_ERR(state)) { |
1398 | switch (PTR_ERR(state)) { | |
1399 | case -EPERM: | |
1400 | case -EACCES: | |
1401 | case -EDQUOT: | |
1402 | case -ENOSPC: | |
1403 | case -EROFS: | |
1404 | lookup_instantiate_filp(nd, (struct dentry *)state, NULL); | |
1405 | return 1; | |
b87c0adf CL |
1406 | default: |
1407 | goto out_drop; | |
02a913a7 | 1408 | } |
02a913a7 | 1409 | } |
24ac23ab | 1410 | if (state->inode == dentry->d_inode) { |
4a35bd41 | 1411 | nfs4_intent_set_file(nd, &path, state); |
1da177e4 LT |
1412 | return 1; |
1413 | } | |
4a35bd41 | 1414 | nfs4_close_state(&path, state, openflags); |
02a913a7 TM |
1415 | out_drop: |
1416 | d_drop(dentry); | |
1da177e4 LT |
1417 | return 0; |
1418 | } | |
1419 | ||
1420 | ||
1421 | static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle) | |
1422 | { | |
1423 | struct nfs4_server_caps_res res = {}; | |
1424 | struct rpc_message msg = { | |
1425 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS], | |
1426 | .rpc_argp = fhandle, | |
1427 | .rpc_resp = &res, | |
1428 | }; | |
1429 | int status; | |
1430 | ||
1431 | status = rpc_call_sync(server->client, &msg, 0); | |
1432 | if (status == 0) { | |
1433 | memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask)); | |
1434 | if (res.attr_bitmask[0] & FATTR4_WORD0_ACL) | |
1435 | server->caps |= NFS_CAP_ACLS; | |
1436 | if (res.has_links != 0) | |
1437 | server->caps |= NFS_CAP_HARDLINKS; | |
1438 | if (res.has_symlinks != 0) | |
1439 | server->caps |= NFS_CAP_SYMLINKS; | |
1440 | server->acl_bitmask = res.acl_bitmask; | |
1441 | } | |
1442 | return status; | |
1443 | } | |
1444 | ||
55a97593 | 1445 | int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle) |
1da177e4 LT |
1446 | { |
1447 | struct nfs4_exception exception = { }; | |
1448 | int err; | |
1449 | do { | |
1450 | err = nfs4_handle_exception(server, | |
1451 | _nfs4_server_capabilities(server, fhandle), | |
1452 | &exception); | |
1453 | } while (exception.retry); | |
1454 | return err; | |
1455 | } | |
1456 | ||
1457 | static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle, | |
1458 | struct nfs_fsinfo *info) | |
1459 | { | |
1da177e4 LT |
1460 | struct nfs4_lookup_root_arg args = { |
1461 | .bitmask = nfs4_fattr_bitmap, | |
1462 | }; | |
1463 | struct nfs4_lookup_res res = { | |
1464 | .server = server, | |
0e574af1 | 1465 | .fattr = info->fattr, |
1da177e4 LT |
1466 | .fh = fhandle, |
1467 | }; | |
1468 | struct rpc_message msg = { | |
1469 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT], | |
1470 | .rpc_argp = &args, | |
1471 | .rpc_resp = &res, | |
1472 | }; | |
0e574af1 | 1473 | nfs_fattr_init(info->fattr); |
1da177e4 LT |
1474 | return rpc_call_sync(server->client, &msg, 0); |
1475 | } | |
1476 | ||
1477 | static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle, | |
1478 | struct nfs_fsinfo *info) | |
1479 | { | |
1480 | struct nfs4_exception exception = { }; | |
1481 | int err; | |
1482 | do { | |
1483 | err = nfs4_handle_exception(server, | |
1484 | _nfs4_lookup_root(server, fhandle, info), | |
1485 | &exception); | |
1486 | } while (exception.retry); | |
1487 | return err; | |
1488 | } | |
1489 | ||
54ceac45 DH |
1490 | /* |
1491 | * get the file handle for the "/" directory on the server | |
1492 | */ | |
1da177e4 | 1493 | static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *fhandle, |
54ceac45 | 1494 | struct nfs_fsinfo *info) |
1da177e4 | 1495 | { |
1da177e4 LT |
1496 | int status; |
1497 | ||
1da177e4 | 1498 | status = nfs4_lookup_root(server, fhandle, info); |
1da177e4 LT |
1499 | if (status == 0) |
1500 | status = nfs4_server_capabilities(server, fhandle); | |
1501 | if (status == 0) | |
1502 | status = nfs4_do_fsinfo(server, fhandle, info); | |
c12e87f4 | 1503 | return nfs4_map_errors(status); |
1da177e4 LT |
1504 | } |
1505 | ||
6b97fd3d MN |
1506 | /* |
1507 | * Get locations and (maybe) other attributes of a referral. | |
1508 | * Note that we'll actually follow the referral later when | |
1509 | * we detect fsid mismatch in inode revalidation | |
1510 | */ | |
1511 | static int nfs4_get_referral(struct inode *dir, struct qstr *name, struct nfs_fattr *fattr, struct nfs_fh *fhandle) | |
1512 | { | |
1513 | int status = -ENOMEM; | |
1514 | struct page *page = NULL; | |
1515 | struct nfs4_fs_locations *locations = NULL; | |
6b97fd3d MN |
1516 | |
1517 | page = alloc_page(GFP_KERNEL); | |
1518 | if (page == NULL) | |
1519 | goto out; | |
1520 | locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL); | |
1521 | if (locations == NULL) | |
1522 | goto out; | |
1523 | ||
c228fd3a | 1524 | status = nfs4_proc_fs_locations(dir, name, locations, page); |
6b97fd3d MN |
1525 | if (status != 0) |
1526 | goto out; | |
1527 | /* Make sure server returned a different fsid for the referral */ | |
1528 | if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) { | |
1529 | dprintk("%s: server did not return a different fsid for a referral at %s\n", __FUNCTION__, name->name); | |
1530 | status = -EIO; | |
1531 | goto out; | |
1532 | } | |
1533 | ||
1534 | memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr)); | |
1535 | fattr->valid |= NFS_ATTR_FATTR_V4_REFERRAL; | |
1536 | if (!fattr->mode) | |
1537 | fattr->mode = S_IFDIR; | |
1538 | memset(fhandle, 0, sizeof(struct nfs_fh)); | |
1539 | out: | |
1540 | if (page) | |
1541 | __free_page(page); | |
1542 | if (locations) | |
1543 | kfree(locations); | |
1544 | return status; | |
1545 | } | |
1546 | ||
1da177e4 LT |
1547 | static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr) |
1548 | { | |
1549 | struct nfs4_getattr_arg args = { | |
1550 | .fh = fhandle, | |
1551 | .bitmask = server->attr_bitmask, | |
1552 | }; | |
1553 | struct nfs4_getattr_res res = { | |
1554 | .fattr = fattr, | |
1555 | .server = server, | |
1556 | }; | |
1557 | struct rpc_message msg = { | |
1558 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR], | |
1559 | .rpc_argp = &args, | |
1560 | .rpc_resp = &res, | |
1561 | }; | |
1562 | ||
0e574af1 | 1563 | nfs_fattr_init(fattr); |
1da177e4 LT |
1564 | return rpc_call_sync(server->client, &msg, 0); |
1565 | } | |
1566 | ||
1567 | static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr) | |
1568 | { | |
1569 | struct nfs4_exception exception = { }; | |
1570 | int err; | |
1571 | do { | |
1572 | err = nfs4_handle_exception(server, | |
1573 | _nfs4_proc_getattr(server, fhandle, fattr), | |
1574 | &exception); | |
1575 | } while (exception.retry); | |
1576 | return err; | |
1577 | } | |
1578 | ||
1579 | /* | |
1580 | * The file is not closed if it is opened due to the a request to change | |
1581 | * the size of the file. The open call will not be needed once the | |
1582 | * VFS layer lookup-intents are implemented. | |
1583 | * | |
1584 | * Close is called when the inode is destroyed. | |
1585 | * If we haven't opened the file for O_WRONLY, we | |
1586 | * need to in the size_change case to obtain a stateid. | |
1587 | * | |
1588 | * Got race? | |
1589 | * Because OPEN is always done by name in nfsv4, it is | |
1590 | * possible that we opened a different file by the same | |
1591 | * name. We can recognize this race condition, but we | |
1592 | * can't do anything about it besides returning an error. | |
1593 | * | |
1594 | * This will be fixed with VFS changes (lookup-intent). | |
1595 | */ | |
1596 | static int | |
1597 | nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr, | |
1598 | struct iattr *sattr) | |
1599 | { | |
08e9eac4 TM |
1600 | struct rpc_cred *cred; |
1601 | struct inode *inode = dentry->d_inode; | |
d530838b TM |
1602 | struct nfs_open_context *ctx; |
1603 | struct nfs4_state *state = NULL; | |
1da177e4 LT |
1604 | int status; |
1605 | ||
0e574af1 | 1606 | nfs_fattr_init(fattr); |
1da177e4 | 1607 | |
1f163415 | 1608 | cred = rpcauth_lookupcred(NFS_CLIENT(inode)->cl_auth, 0); |
08e9eac4 TM |
1609 | if (IS_ERR(cred)) |
1610 | return PTR_ERR(cred); | |
d530838b TM |
1611 | |
1612 | /* Search for an existing open(O_WRITE) file */ | |
1613 | ctx = nfs_find_open_context(inode, cred, FMODE_WRITE); | |
1614 | if (ctx != NULL) | |
1615 | state = ctx->state; | |
08e9eac4 | 1616 | |
3e4f6290 | 1617 | status = nfs4_do_setattr(inode, fattr, sattr, state); |
65e4308d TM |
1618 | if (status == 0) |
1619 | nfs_setattr_update_inode(inode, sattr); | |
d530838b TM |
1620 | if (ctx != NULL) |
1621 | put_nfs_open_context(ctx); | |
08e9eac4 | 1622 | put_rpccred(cred); |
1da177e4 LT |
1623 | return status; |
1624 | } | |
1625 | ||
2b3de441 DH |
1626 | static int _nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh, |
1627 | struct qstr *name, struct nfs_fh *fhandle, | |
1628 | struct nfs_fattr *fattr) | |
1629 | { | |
1630 | int status; | |
1631 | struct nfs4_lookup_arg args = { | |
1632 | .bitmask = server->attr_bitmask, | |
1633 | .dir_fh = dirfh, | |
1634 | .name = name, | |
1635 | }; | |
1636 | struct nfs4_lookup_res res = { | |
1637 | .server = server, | |
1638 | .fattr = fattr, | |
1639 | .fh = fhandle, | |
1640 | }; | |
1641 | struct rpc_message msg = { | |
1642 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP], | |
1643 | .rpc_argp = &args, | |
1644 | .rpc_resp = &res, | |
1645 | }; | |
1646 | ||
1647 | nfs_fattr_init(fattr); | |
1648 | ||
1649 | dprintk("NFS call lookupfh %s\n", name->name); | |
1650 | status = rpc_call_sync(server->client, &msg, 0); | |
1651 | dprintk("NFS reply lookupfh: %d\n", status); | |
2b3de441 DH |
1652 | return status; |
1653 | } | |
1654 | ||
1655 | static int nfs4_proc_lookupfh(struct nfs_server *server, struct nfs_fh *dirfh, | |
1656 | struct qstr *name, struct nfs_fh *fhandle, | |
1657 | struct nfs_fattr *fattr) | |
1658 | { | |
1659 | struct nfs4_exception exception = { }; | |
1660 | int err; | |
1661 | do { | |
4e56e082 TM |
1662 | err = _nfs4_proc_lookupfh(server, dirfh, name, fhandle, fattr); |
1663 | /* FIXME: !!!! */ | |
1664 | if (err == -NFS4ERR_MOVED) { | |
1665 | err = -EREMOTE; | |
1666 | break; | |
1667 | } | |
1668 | err = nfs4_handle_exception(server, err, &exception); | |
2b3de441 DH |
1669 | } while (exception.retry); |
1670 | return err; | |
1671 | } | |
1672 | ||
1da177e4 LT |
1673 | static int _nfs4_proc_lookup(struct inode *dir, struct qstr *name, |
1674 | struct nfs_fh *fhandle, struct nfs_fattr *fattr) | |
1675 | { | |
4e56e082 | 1676 | int status; |
1da177e4 LT |
1677 | |
1678 | dprintk("NFS call lookup %s\n", name->name); | |
4e56e082 | 1679 | status = _nfs4_proc_lookupfh(NFS_SERVER(dir), NFS_FH(dir), name, fhandle, fattr); |
6b97fd3d MN |
1680 | if (status == -NFS4ERR_MOVED) |
1681 | status = nfs4_get_referral(dir, name, fattr, fhandle); | |
1da177e4 LT |
1682 | dprintk("NFS reply lookup: %d\n", status); |
1683 | return status; | |
1684 | } | |
1685 | ||
1686 | static int nfs4_proc_lookup(struct inode *dir, struct qstr *name, struct nfs_fh *fhandle, struct nfs_fattr *fattr) | |
1687 | { | |
1688 | struct nfs4_exception exception = { }; | |
1689 | int err; | |
1690 | do { | |
1691 | err = nfs4_handle_exception(NFS_SERVER(dir), | |
1692 | _nfs4_proc_lookup(dir, name, fhandle, fattr), | |
1693 | &exception); | |
1694 | } while (exception.retry); | |
1695 | return err; | |
1696 | } | |
1697 | ||
1698 | static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry) | |
1699 | { | |
1700 | struct nfs4_accessargs args = { | |
1701 | .fh = NFS_FH(inode), | |
1702 | }; | |
1703 | struct nfs4_accessres res = { 0 }; | |
1704 | struct rpc_message msg = { | |
1705 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS], | |
1706 | .rpc_argp = &args, | |
1707 | .rpc_resp = &res, | |
1708 | .rpc_cred = entry->cred, | |
1709 | }; | |
1710 | int mode = entry->mask; | |
1711 | int status; | |
1712 | ||
1713 | /* | |
1714 | * Determine which access bits we want to ask for... | |
1715 | */ | |
1716 | if (mode & MAY_READ) | |
1717 | args.access |= NFS4_ACCESS_READ; | |
1718 | if (S_ISDIR(inode->i_mode)) { | |
1719 | if (mode & MAY_WRITE) | |
1720 | args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE; | |
1721 | if (mode & MAY_EXEC) | |
1722 | args.access |= NFS4_ACCESS_LOOKUP; | |
1723 | } else { | |
1724 | if (mode & MAY_WRITE) | |
1725 | args.access |= NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND; | |
1726 | if (mode & MAY_EXEC) | |
1727 | args.access |= NFS4_ACCESS_EXECUTE; | |
1728 | } | |
1729 | status = rpc_call_sync(NFS_CLIENT(inode), &msg, 0); | |
1730 | if (!status) { | |
1731 | entry->mask = 0; | |
1732 | if (res.access & NFS4_ACCESS_READ) | |
1733 | entry->mask |= MAY_READ; | |
1734 | if (res.access & (NFS4_ACCESS_MODIFY | NFS4_ACCESS_EXTEND | NFS4_ACCESS_DELETE)) | |
1735 | entry->mask |= MAY_WRITE; | |
1736 | if (res.access & (NFS4_ACCESS_LOOKUP|NFS4_ACCESS_EXECUTE)) | |
1737 | entry->mask |= MAY_EXEC; | |
1738 | } | |
1739 | return status; | |
1740 | } | |
1741 | ||
1742 | static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry) | |
1743 | { | |
1744 | struct nfs4_exception exception = { }; | |
1745 | int err; | |
1746 | do { | |
1747 | err = nfs4_handle_exception(NFS_SERVER(inode), | |
1748 | _nfs4_proc_access(inode, entry), | |
1749 | &exception); | |
1750 | } while (exception.retry); | |
1751 | return err; | |
1752 | } | |
1753 | ||
1754 | /* | |
1755 | * TODO: For the time being, we don't try to get any attributes | |
1756 | * along with any of the zero-copy operations READ, READDIR, | |
1757 | * READLINK, WRITE. | |
1758 | * | |
1759 | * In the case of the first three, we want to put the GETATTR | |
1760 | * after the read-type operation -- this is because it is hard | |
1761 | * to predict the length of a GETATTR response in v4, and thus | |
1762 | * align the READ data correctly. This means that the GETATTR | |
1763 | * may end up partially falling into the page cache, and we should | |
1764 | * shift it into the 'tail' of the xdr_buf before processing. | |
1765 | * To do this efficiently, we need to know the total length | |
1766 | * of data received, which doesn't seem to be available outside | |
1767 | * of the RPC layer. | |
1768 | * | |
1769 | * In the case of WRITE, we also want to put the GETATTR after | |
1770 | * the operation -- in this case because we want to make sure | |
1771 | * we get the post-operation mtime and size. This means that | |
1772 | * we can't use xdr_encode_pages() as written: we need a variant | |
1773 | * of it which would leave room in the 'tail' iovec. | |
1774 | * | |
1775 | * Both of these changes to the XDR layer would in fact be quite | |
1776 | * minor, but I decided to leave them for a subsequent patch. | |
1777 | */ | |
1778 | static int _nfs4_proc_readlink(struct inode *inode, struct page *page, | |
1779 | unsigned int pgbase, unsigned int pglen) | |
1780 | { | |
1781 | struct nfs4_readlink args = { | |
1782 | .fh = NFS_FH(inode), | |
1783 | .pgbase = pgbase, | |
1784 | .pglen = pglen, | |
1785 | .pages = &page, | |
1786 | }; | |
1787 | struct rpc_message msg = { | |
1788 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK], | |
1789 | .rpc_argp = &args, | |
1790 | .rpc_resp = NULL, | |
1791 | }; | |
1792 | ||
1793 | return rpc_call_sync(NFS_CLIENT(inode), &msg, 0); | |
1794 | } | |
1795 | ||
1796 | static int nfs4_proc_readlink(struct inode *inode, struct page *page, | |
1797 | unsigned int pgbase, unsigned int pglen) | |
1798 | { | |
1799 | struct nfs4_exception exception = { }; | |
1800 | int err; | |
1801 | do { | |
1802 | err = nfs4_handle_exception(NFS_SERVER(inode), | |
1803 | _nfs4_proc_readlink(inode, page, pgbase, pglen), | |
1804 | &exception); | |
1805 | } while (exception.retry); | |
1806 | return err; | |
1807 | } | |
1808 | ||
1da177e4 LT |
1809 | /* |
1810 | * Got race? | |
1811 | * We will need to arrange for the VFS layer to provide an atomic open. | |
1812 | * Until then, this create/open method is prone to inefficiency and race | |
1813 | * conditions due to the lookup, create, and open VFS calls from sys_open() | |
1814 | * placed on the wire. | |
1815 | * | |
1816 | * Given the above sorry state of affairs, I'm simply sending an OPEN. | |
1817 | * The file will be opened again in the subsequent VFS open call | |
1818 | * (nfs4_proc_file_open). | |
1819 | * | |
1820 | * The open for read will just hang around to be used by any process that | |
1821 | * opens the file O_RDONLY. This will all be resolved with the VFS changes. | |
1822 | */ | |
1823 | ||
1824 | static int | |
1825 | nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr, | |
02a913a7 | 1826 | int flags, struct nameidata *nd) |
1da177e4 | 1827 | { |
ad389da7 TM |
1828 | struct path path = { |
1829 | .mnt = nd->mnt, | |
1830 | .dentry = dentry, | |
1831 | }; | |
1da177e4 LT |
1832 | struct nfs4_state *state; |
1833 | struct rpc_cred *cred; | |
1834 | int status = 0; | |
1835 | ||
1f163415 | 1836 | cred = rpcauth_lookupcred(NFS_CLIENT(dir)->cl_auth, 0); |
1da177e4 LT |
1837 | if (IS_ERR(cred)) { |
1838 | status = PTR_ERR(cred); | |
1839 | goto out; | |
1840 | } | |
ad389da7 | 1841 | state = nfs4_do_open(dir, &path, flags, sattr, cred); |
1da177e4 LT |
1842 | put_rpccred(cred); |
1843 | if (IS_ERR(state)) { | |
1844 | status = PTR_ERR(state); | |
1845 | goto out; | |
1846 | } | |
24ac23ab | 1847 | d_instantiate(dentry, igrab(state->inode)); |
1da177e4 LT |
1848 | if (flags & O_EXCL) { |
1849 | struct nfs_fattr fattr; | |
3e4f6290 | 1850 | status = nfs4_do_setattr(state->inode, &fattr, sattr, state); |
02a913a7 | 1851 | if (status == 0) |
65e4308d | 1852 | nfs_setattr_update_inode(state->inode, sattr); |
aa53ed54 | 1853 | nfs_post_op_update_inode(state->inode, &fattr); |
02a913a7 | 1854 | } |
ad389da7 | 1855 | if (status == 0 && (nd->flags & LOOKUP_OPEN) != 0) |
4a35bd41 | 1856 | status = nfs4_intent_set_file(nd, &path, state); |
02a913a7 | 1857 | else |
4a35bd41 | 1858 | nfs4_close_state(&path, state, flags); |
1da177e4 LT |
1859 | out: |
1860 | return status; | |
1861 | } | |
1862 | ||
1863 | static int _nfs4_proc_remove(struct inode *dir, struct qstr *name) | |
1864 | { | |
16e42959 | 1865 | struct nfs_server *server = NFS_SERVER(dir); |
1da177e4 LT |
1866 | struct nfs4_remove_arg args = { |
1867 | .fh = NFS_FH(dir), | |
1868 | .name = name, | |
16e42959 TM |
1869 | .bitmask = server->attr_bitmask, |
1870 | }; | |
1871 | struct nfs_fattr dir_attr; | |
1872 | struct nfs4_remove_res res = { | |
1873 | .server = server, | |
1874 | .dir_attr = &dir_attr, | |
1da177e4 | 1875 | }; |
1da177e4 LT |
1876 | struct rpc_message msg = { |
1877 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE], | |
1878 | .rpc_argp = &args, | |
1879 | .rpc_resp = &res, | |
1880 | }; | |
1881 | int status; | |
1882 | ||
16e42959 TM |
1883 | nfs_fattr_init(res.dir_attr); |
1884 | status = rpc_call_sync(server->client, &msg, 0); | |
1885 | if (status == 0) { | |
1886 | update_changeattr(dir, &res.cinfo); | |
1887 | nfs_post_op_update_inode(dir, res.dir_attr); | |
1888 | } | |
1da177e4 LT |
1889 | return status; |
1890 | } | |
1891 | ||
1892 | static int nfs4_proc_remove(struct inode *dir, struct qstr *name) | |
1893 | { | |
1894 | struct nfs4_exception exception = { }; | |
1895 | int err; | |
1896 | do { | |
1897 | err = nfs4_handle_exception(NFS_SERVER(dir), | |
1898 | _nfs4_proc_remove(dir, name), | |
1899 | &exception); | |
1900 | } while (exception.retry); | |
1901 | return err; | |
1902 | } | |
1903 | ||
1904 | struct unlink_desc { | |
1905 | struct nfs4_remove_arg args; | |
16e42959 TM |
1906 | struct nfs4_remove_res res; |
1907 | struct nfs_fattr dir_attr; | |
1da177e4 LT |
1908 | }; |
1909 | ||
1910 | static int nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dir, | |
1911 | struct qstr *name) | |
1912 | { | |
16e42959 | 1913 | struct nfs_server *server = NFS_SERVER(dir->d_inode); |
1da177e4 LT |
1914 | struct unlink_desc *up; |
1915 | ||
5cbded58 | 1916 | up = kmalloc(sizeof(*up), GFP_KERNEL); |
1da177e4 LT |
1917 | if (!up) |
1918 | return -ENOMEM; | |
1919 | ||
1920 | up->args.fh = NFS_FH(dir->d_inode); | |
1921 | up->args.name = name; | |
16e42959 TM |
1922 | up->args.bitmask = server->attr_bitmask; |
1923 | up->res.server = server; | |
1924 | up->res.dir_attr = &up->dir_attr; | |
1da177e4 LT |
1925 | |
1926 | msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE]; | |
1927 | msg->rpc_argp = &up->args; | |
1928 | msg->rpc_resp = &up->res; | |
1929 | return 0; | |
1930 | } | |
1931 | ||
1932 | static int nfs4_proc_unlink_done(struct dentry *dir, struct rpc_task *task) | |
1933 | { | |
1934 | struct rpc_message *msg = &task->tk_msg; | |
1935 | struct unlink_desc *up; | |
1936 | ||
1937 | if (msg->rpc_resp != NULL) { | |
1938 | up = container_of(msg->rpc_resp, struct unlink_desc, res); | |
16e42959 TM |
1939 | update_changeattr(dir->d_inode, &up->res.cinfo); |
1940 | nfs_post_op_update_inode(dir->d_inode, up->res.dir_attr); | |
1da177e4 LT |
1941 | kfree(up); |
1942 | msg->rpc_resp = NULL; | |
1943 | msg->rpc_argp = NULL; | |
1944 | } | |
1945 | return 0; | |
1946 | } | |
1947 | ||
1948 | static int _nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name, | |
1949 | struct inode *new_dir, struct qstr *new_name) | |
1950 | { | |
6caf2c82 | 1951 | struct nfs_server *server = NFS_SERVER(old_dir); |
1da177e4 LT |
1952 | struct nfs4_rename_arg arg = { |
1953 | .old_dir = NFS_FH(old_dir), | |
1954 | .new_dir = NFS_FH(new_dir), | |
1955 | .old_name = old_name, | |
1956 | .new_name = new_name, | |
6caf2c82 TM |
1957 | .bitmask = server->attr_bitmask, |
1958 | }; | |
1959 | struct nfs_fattr old_fattr, new_fattr; | |
1960 | struct nfs4_rename_res res = { | |
1961 | .server = server, | |
1962 | .old_fattr = &old_fattr, | |
1963 | .new_fattr = &new_fattr, | |
1da177e4 | 1964 | }; |
1da177e4 LT |
1965 | struct rpc_message msg = { |
1966 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME], | |
1967 | .rpc_argp = &arg, | |
1968 | .rpc_resp = &res, | |
1969 | }; | |
1970 | int status; | |
1971 | ||
6caf2c82 TM |
1972 | nfs_fattr_init(res.old_fattr); |
1973 | nfs_fattr_init(res.new_fattr); | |
1974 | status = rpc_call_sync(server->client, &msg, 0); | |
1da177e4 LT |
1975 | |
1976 | if (!status) { | |
1977 | update_changeattr(old_dir, &res.old_cinfo); | |
6caf2c82 | 1978 | nfs_post_op_update_inode(old_dir, res.old_fattr); |
1da177e4 | 1979 | update_changeattr(new_dir, &res.new_cinfo); |
6caf2c82 | 1980 | nfs_post_op_update_inode(new_dir, res.new_fattr); |
1da177e4 LT |
1981 | } |
1982 | return status; | |
1983 | } | |
1984 | ||
1985 | static int nfs4_proc_rename(struct inode *old_dir, struct qstr *old_name, | |
1986 | struct inode *new_dir, struct qstr *new_name) | |
1987 | { | |
1988 | struct nfs4_exception exception = { }; | |
1989 | int err; | |
1990 | do { | |
1991 | err = nfs4_handle_exception(NFS_SERVER(old_dir), | |
1992 | _nfs4_proc_rename(old_dir, old_name, | |
1993 | new_dir, new_name), | |
1994 | &exception); | |
1995 | } while (exception.retry); | |
1996 | return err; | |
1997 | } | |
1998 | ||
1999 | static int _nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name) | |
2000 | { | |
91ba2eee | 2001 | struct nfs_server *server = NFS_SERVER(inode); |
1da177e4 LT |
2002 | struct nfs4_link_arg arg = { |
2003 | .fh = NFS_FH(inode), | |
2004 | .dir_fh = NFS_FH(dir), | |
2005 | .name = name, | |
91ba2eee TM |
2006 | .bitmask = server->attr_bitmask, |
2007 | }; | |
2008 | struct nfs_fattr fattr, dir_attr; | |
2009 | struct nfs4_link_res res = { | |
2010 | .server = server, | |
2011 | .fattr = &fattr, | |
2012 | .dir_attr = &dir_attr, | |
1da177e4 | 2013 | }; |
1da177e4 LT |
2014 | struct rpc_message msg = { |
2015 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK], | |
2016 | .rpc_argp = &arg, | |
91ba2eee | 2017 | .rpc_resp = &res, |
1da177e4 LT |
2018 | }; |
2019 | int status; | |
2020 | ||
91ba2eee TM |
2021 | nfs_fattr_init(res.fattr); |
2022 | nfs_fattr_init(res.dir_attr); | |
2023 | status = rpc_call_sync(server->client, &msg, 0); | |
2024 | if (!status) { | |
2025 | update_changeattr(dir, &res.cinfo); | |
2026 | nfs_post_op_update_inode(dir, res.dir_attr); | |
73a3d07c | 2027 | nfs_post_op_update_inode(inode, res.fattr); |
91ba2eee | 2028 | } |
1da177e4 LT |
2029 | |
2030 | return status; | |
2031 | } | |
2032 | ||
2033 | static int nfs4_proc_link(struct inode *inode, struct inode *dir, struct qstr *name) | |
2034 | { | |
2035 | struct nfs4_exception exception = { }; | |
2036 | int err; | |
2037 | do { | |
2038 | err = nfs4_handle_exception(NFS_SERVER(inode), | |
2039 | _nfs4_proc_link(inode, dir, name), | |
2040 | &exception); | |
2041 | } while (exception.retry); | |
2042 | return err; | |
2043 | } | |
2044 | ||
4f390c15 | 2045 | static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry, |
94a6d753 | 2046 | struct page *page, unsigned int len, struct iattr *sattr) |
1da177e4 LT |
2047 | { |
2048 | struct nfs_server *server = NFS_SERVER(dir); | |
4f390c15 CL |
2049 | struct nfs_fh fhandle; |
2050 | struct nfs_fattr fattr, dir_fattr; | |
1da177e4 LT |
2051 | struct nfs4_create_arg arg = { |
2052 | .dir_fh = NFS_FH(dir), | |
2053 | .server = server, | |
4f390c15 | 2054 | .name = &dentry->d_name, |
1da177e4 LT |
2055 | .attrs = sattr, |
2056 | .ftype = NF4LNK, | |
2057 | .bitmask = server->attr_bitmask, | |
2058 | }; | |
2059 | struct nfs4_create_res res = { | |
2060 | .server = server, | |
4f390c15 CL |
2061 | .fh = &fhandle, |
2062 | .fattr = &fattr, | |
56ae19f3 | 2063 | .dir_fattr = &dir_fattr, |
1da177e4 LT |
2064 | }; |
2065 | struct rpc_message msg = { | |
2066 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK], | |
2067 | .rpc_argp = &arg, | |
2068 | .rpc_resp = &res, | |
2069 | }; | |
2070 | int status; | |
2071 | ||
94a6d753 | 2072 | if (len > NFS4_MAXPATHLEN) |
1da177e4 | 2073 | return -ENAMETOOLONG; |
4f390c15 | 2074 | |
94a6d753 CL |
2075 | arg.u.symlink.pages = &page; |
2076 | arg.u.symlink.len = len; | |
4f390c15 | 2077 | nfs_fattr_init(&fattr); |
56ae19f3 | 2078 | nfs_fattr_init(&dir_fattr); |
1da177e4 LT |
2079 | |
2080 | status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0); | |
4f390c15 | 2081 | if (!status) { |
1da177e4 | 2082 | update_changeattr(dir, &res.dir_cinfo); |
4f390c15 CL |
2083 | nfs_post_op_update_inode(dir, res.dir_fattr); |
2084 | status = nfs_instantiate(dentry, &fhandle, &fattr); | |
2085 | } | |
1da177e4 LT |
2086 | return status; |
2087 | } | |
2088 | ||
4f390c15 | 2089 | static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry, |
94a6d753 | 2090 | struct page *page, unsigned int len, struct iattr *sattr) |
1da177e4 LT |
2091 | { |
2092 | struct nfs4_exception exception = { }; | |
2093 | int err; | |
2094 | do { | |
2095 | err = nfs4_handle_exception(NFS_SERVER(dir), | |
94a6d753 CL |
2096 | _nfs4_proc_symlink(dir, dentry, page, |
2097 | len, sattr), | |
1da177e4 LT |
2098 | &exception); |
2099 | } while (exception.retry); | |
2100 | return err; | |
2101 | } | |
2102 | ||
2103 | static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry, | |
2104 | struct iattr *sattr) | |
2105 | { | |
2106 | struct nfs_server *server = NFS_SERVER(dir); | |
2107 | struct nfs_fh fhandle; | |
56ae19f3 | 2108 | struct nfs_fattr fattr, dir_fattr; |
1da177e4 LT |
2109 | struct nfs4_create_arg arg = { |
2110 | .dir_fh = NFS_FH(dir), | |
2111 | .server = server, | |
2112 | .name = &dentry->d_name, | |
2113 | .attrs = sattr, | |
2114 | .ftype = NF4DIR, | |
2115 | .bitmask = server->attr_bitmask, | |
2116 | }; | |
2117 | struct nfs4_create_res res = { | |
2118 | .server = server, | |
2119 | .fh = &fhandle, | |
2120 | .fattr = &fattr, | |
56ae19f3 | 2121 | .dir_fattr = &dir_fattr, |
1da177e4 LT |
2122 | }; |
2123 | struct rpc_message msg = { | |
2124 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE], | |
2125 | .rpc_argp = &arg, | |
2126 | .rpc_resp = &res, | |
2127 | }; | |
2128 | int status; | |
2129 | ||
0e574af1 | 2130 | nfs_fattr_init(&fattr); |
56ae19f3 | 2131 | nfs_fattr_init(&dir_fattr); |
1da177e4 LT |
2132 | |
2133 | status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0); | |
2134 | if (!status) { | |
2135 | update_changeattr(dir, &res.dir_cinfo); | |
56ae19f3 | 2136 | nfs_post_op_update_inode(dir, res.dir_fattr); |
1da177e4 LT |
2137 | status = nfs_instantiate(dentry, &fhandle, &fattr); |
2138 | } | |
2139 | return status; | |
2140 | } | |
2141 | ||
2142 | static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry, | |
2143 | struct iattr *sattr) | |
2144 | { | |
2145 | struct nfs4_exception exception = { }; | |
2146 | int err; | |
2147 | do { | |
2148 | err = nfs4_handle_exception(NFS_SERVER(dir), | |
2149 | _nfs4_proc_mkdir(dir, dentry, sattr), | |
2150 | &exception); | |
2151 | } while (exception.retry); | |
2152 | return err; | |
2153 | } | |
2154 | ||
2155 | static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred, | |
2156 | u64 cookie, struct page *page, unsigned int count, int plus) | |
2157 | { | |
2158 | struct inode *dir = dentry->d_inode; | |
2159 | struct nfs4_readdir_arg args = { | |
2160 | .fh = NFS_FH(dir), | |
2161 | .pages = &page, | |
2162 | .pgbase = 0, | |
2163 | .count = count, | |
2164 | .bitmask = NFS_SERVER(dentry->d_inode)->attr_bitmask, | |
2165 | }; | |
2166 | struct nfs4_readdir_res res; | |
2167 | struct rpc_message msg = { | |
2168 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR], | |
2169 | .rpc_argp = &args, | |
2170 | .rpc_resp = &res, | |
2171 | .rpc_cred = cred, | |
2172 | }; | |
2173 | int status; | |
2174 | ||
eadf4598 TM |
2175 | dprintk("%s: dentry = %s/%s, cookie = %Lu\n", __FUNCTION__, |
2176 | dentry->d_parent->d_name.name, | |
2177 | dentry->d_name.name, | |
2178 | (unsigned long long)cookie); | |
1da177e4 LT |
2179 | nfs4_setup_readdir(cookie, NFS_COOKIEVERF(dir), dentry, &args); |
2180 | res.pgbase = args.pgbase; | |
2181 | status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0); | |
2182 | if (status == 0) | |
2183 | memcpy(NFS_COOKIEVERF(dir), res.verifier.data, NFS4_VERIFIER_SIZE); | |
eadf4598 | 2184 | dprintk("%s: returns %d\n", __FUNCTION__, status); |
1da177e4 LT |
2185 | return status; |
2186 | } | |
2187 | ||
2188 | static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred, | |
2189 | u64 cookie, struct page *page, unsigned int count, int plus) | |
2190 | { | |
2191 | struct nfs4_exception exception = { }; | |
2192 | int err; | |
2193 | do { | |
2194 | err = nfs4_handle_exception(NFS_SERVER(dentry->d_inode), | |
2195 | _nfs4_proc_readdir(dentry, cred, cookie, | |
2196 | page, count, plus), | |
2197 | &exception); | |
2198 | } while (exception.retry); | |
2199 | return err; | |
2200 | } | |
2201 | ||
2202 | static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry, | |
2203 | struct iattr *sattr, dev_t rdev) | |
2204 | { | |
2205 | struct nfs_server *server = NFS_SERVER(dir); | |
2206 | struct nfs_fh fh; | |
56ae19f3 | 2207 | struct nfs_fattr fattr, dir_fattr; |
1da177e4 LT |
2208 | struct nfs4_create_arg arg = { |
2209 | .dir_fh = NFS_FH(dir), | |
2210 | .server = server, | |
2211 | .name = &dentry->d_name, | |
2212 | .attrs = sattr, | |
2213 | .bitmask = server->attr_bitmask, | |
2214 | }; | |
2215 | struct nfs4_create_res res = { | |
2216 | .server = server, | |
2217 | .fh = &fh, | |
2218 | .fattr = &fattr, | |
56ae19f3 | 2219 | .dir_fattr = &dir_fattr, |
1da177e4 LT |
2220 | }; |
2221 | struct rpc_message msg = { | |
2222 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE], | |
2223 | .rpc_argp = &arg, | |
2224 | .rpc_resp = &res, | |
2225 | }; | |
2226 | int status; | |
2227 | int mode = sattr->ia_mode; | |
2228 | ||
0e574af1 | 2229 | nfs_fattr_init(&fattr); |
56ae19f3 | 2230 | nfs_fattr_init(&dir_fattr); |
1da177e4 LT |
2231 | |
2232 | BUG_ON(!(sattr->ia_valid & ATTR_MODE)); | |
2233 | BUG_ON(!S_ISFIFO(mode) && !S_ISBLK(mode) && !S_ISCHR(mode) && !S_ISSOCK(mode)); | |
2234 | if (S_ISFIFO(mode)) | |
2235 | arg.ftype = NF4FIFO; | |
2236 | else if (S_ISBLK(mode)) { | |
2237 | arg.ftype = NF4BLK; | |
2238 | arg.u.device.specdata1 = MAJOR(rdev); | |
2239 | arg.u.device.specdata2 = MINOR(rdev); | |
2240 | } | |
2241 | else if (S_ISCHR(mode)) { | |
2242 | arg.ftype = NF4CHR; | |
2243 | arg.u.device.specdata1 = MAJOR(rdev); | |
2244 | arg.u.device.specdata2 = MINOR(rdev); | |
2245 | } | |
2246 | else | |
2247 | arg.ftype = NF4SOCK; | |
2248 | ||
2249 | status = rpc_call_sync(NFS_CLIENT(dir), &msg, 0); | |
2250 | if (status == 0) { | |
2251 | update_changeattr(dir, &res.dir_cinfo); | |
56ae19f3 | 2252 | nfs_post_op_update_inode(dir, res.dir_fattr); |
1da177e4 LT |
2253 | status = nfs_instantiate(dentry, &fh, &fattr); |
2254 | } | |
2255 | return status; | |
2256 | } | |
2257 | ||
2258 | static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry, | |
2259 | struct iattr *sattr, dev_t rdev) | |
2260 | { | |
2261 | struct nfs4_exception exception = { }; | |
2262 | int err; | |
2263 | do { | |
2264 | err = nfs4_handle_exception(NFS_SERVER(dir), | |
2265 | _nfs4_proc_mknod(dir, dentry, sattr, rdev), | |
2266 | &exception); | |
2267 | } while (exception.retry); | |
2268 | return err; | |
2269 | } | |
2270 | ||
2271 | static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, | |
2272 | struct nfs_fsstat *fsstat) | |
2273 | { | |
2274 | struct nfs4_statfs_arg args = { | |
2275 | .fh = fhandle, | |
2276 | .bitmask = server->attr_bitmask, | |
2277 | }; | |
2278 | struct rpc_message msg = { | |
2279 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS], | |
2280 | .rpc_argp = &args, | |
2281 | .rpc_resp = fsstat, | |
2282 | }; | |
2283 | ||
0e574af1 | 2284 | nfs_fattr_init(fsstat->fattr); |
1da177e4 LT |
2285 | return rpc_call_sync(server->client, &msg, 0); |
2286 | } | |
2287 | ||
2288 | static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat) | |
2289 | { | |
2290 | struct nfs4_exception exception = { }; | |
2291 | int err; | |
2292 | do { | |
2293 | err = nfs4_handle_exception(server, | |
2294 | _nfs4_proc_statfs(server, fhandle, fsstat), | |
2295 | &exception); | |
2296 | } while (exception.retry); | |
2297 | return err; | |
2298 | } | |
2299 | ||
2300 | static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, | |
2301 | struct nfs_fsinfo *fsinfo) | |
2302 | { | |
2303 | struct nfs4_fsinfo_arg args = { | |
2304 | .fh = fhandle, | |
2305 | .bitmask = server->attr_bitmask, | |
2306 | }; | |
2307 | struct rpc_message msg = { | |
2308 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO], | |
2309 | .rpc_argp = &args, | |
2310 | .rpc_resp = fsinfo, | |
2311 | }; | |
2312 | ||
2313 | return rpc_call_sync(server->client, &msg, 0); | |
2314 | } | |
2315 | ||
2316 | static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo) | |
2317 | { | |
2318 | struct nfs4_exception exception = { }; | |
2319 | int err; | |
2320 | ||
2321 | do { | |
2322 | err = nfs4_handle_exception(server, | |
2323 | _nfs4_do_fsinfo(server, fhandle, fsinfo), | |
2324 | &exception); | |
2325 | } while (exception.retry); | |
2326 | return err; | |
2327 | } | |
2328 | ||
2329 | static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo) | |
2330 | { | |
0e574af1 | 2331 | nfs_fattr_init(fsinfo->fattr); |
1da177e4 LT |
2332 | return nfs4_do_fsinfo(server, fhandle, fsinfo); |
2333 | } | |
2334 | ||
2335 | static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle, | |
2336 | struct nfs_pathconf *pathconf) | |
2337 | { | |
2338 | struct nfs4_pathconf_arg args = { | |
2339 | .fh = fhandle, | |
2340 | .bitmask = server->attr_bitmask, | |
2341 | }; | |
2342 | struct rpc_message msg = { | |
2343 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF], | |
2344 | .rpc_argp = &args, | |
2345 | .rpc_resp = pathconf, | |
2346 | }; | |
2347 | ||
2348 | /* None of the pathconf attributes are mandatory to implement */ | |
2349 | if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) { | |
2350 | memset(pathconf, 0, sizeof(*pathconf)); | |
2351 | return 0; | |
2352 | } | |
2353 | ||
0e574af1 | 2354 | nfs_fattr_init(pathconf->fattr); |
1da177e4 LT |
2355 | return rpc_call_sync(server->client, &msg, 0); |
2356 | } | |
2357 | ||
2358 | static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle, | |
2359 | struct nfs_pathconf *pathconf) | |
2360 | { | |
2361 | struct nfs4_exception exception = { }; | |
2362 | int err; | |
2363 | ||
2364 | do { | |
2365 | err = nfs4_handle_exception(server, | |
2366 | _nfs4_proc_pathconf(server, fhandle, pathconf), | |
2367 | &exception); | |
2368 | } while (exception.retry); | |
2369 | return err; | |
2370 | } | |
2371 | ||
ec06c096 | 2372 | static int nfs4_read_done(struct rpc_task *task, struct nfs_read_data *data) |
1da177e4 | 2373 | { |
ec06c096 | 2374 | struct nfs_server *server = NFS_SERVER(data->inode); |
1da177e4 | 2375 | |
ec06c096 | 2376 | if (nfs4_async_handle_error(task, server) == -EAGAIN) { |
1da177e4 | 2377 | rpc_restart_call(task); |
ec06c096 | 2378 | return -EAGAIN; |
1da177e4 LT |
2379 | } |
2380 | if (task->tk_status > 0) | |
ec06c096 TM |
2381 | renew_lease(server, data->timestamp); |
2382 | return 0; | |
1da177e4 LT |
2383 | } |
2384 | ||
ec06c096 | 2385 | static void nfs4_proc_read_setup(struct nfs_read_data *data) |
1da177e4 | 2386 | { |
1da177e4 LT |
2387 | struct rpc_message msg = { |
2388 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ], | |
2389 | .rpc_argp = &data->args, | |
2390 | .rpc_resp = &data->res, | |
2391 | .rpc_cred = data->cred, | |
2392 | }; | |
1da177e4 LT |
2393 | |
2394 | data->timestamp = jiffies; | |
2395 | ||
ec06c096 | 2396 | rpc_call_setup(&data->task, &msg, 0); |
1da177e4 LT |
2397 | } |
2398 | ||
788e7a89 | 2399 | static int nfs4_write_done(struct rpc_task *task, struct nfs_write_data *data) |
1da177e4 | 2400 | { |
1da177e4 LT |
2401 | struct inode *inode = data->inode; |
2402 | ||
2403 | if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) { | |
2404 | rpc_restart_call(task); | |
788e7a89 | 2405 | return -EAGAIN; |
1da177e4 | 2406 | } |
4f9838c7 | 2407 | if (task->tk_status >= 0) { |
1da177e4 | 2408 | renew_lease(NFS_SERVER(inode), data->timestamp); |
4f9838c7 TM |
2409 | nfs_post_op_update_inode(inode, data->res.fattr); |
2410 | } | |
788e7a89 | 2411 | return 0; |
1da177e4 LT |
2412 | } |
2413 | ||
788e7a89 | 2414 | static void nfs4_proc_write_setup(struct nfs_write_data *data, int how) |
1da177e4 | 2415 | { |
1da177e4 LT |
2416 | struct rpc_message msg = { |
2417 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE], | |
2418 | .rpc_argp = &data->args, | |
2419 | .rpc_resp = &data->res, | |
2420 | .rpc_cred = data->cred, | |
2421 | }; | |
2422 | struct inode *inode = data->inode; | |
4f9838c7 | 2423 | struct nfs_server *server = NFS_SERVER(inode); |
1da177e4 | 2424 | int stable; |
1da177e4 LT |
2425 | |
2426 | if (how & FLUSH_STABLE) { | |
2427 | if (!NFS_I(inode)->ncommit) | |
2428 | stable = NFS_FILE_SYNC; | |
2429 | else | |
2430 | stable = NFS_DATA_SYNC; | |
2431 | } else | |
2432 | stable = NFS_UNSTABLE; | |
2433 | data->args.stable = stable; | |
4f9838c7 TM |
2434 | data->args.bitmask = server->attr_bitmask; |
2435 | data->res.server = server; | |
1da177e4 LT |
2436 | |
2437 | data->timestamp = jiffies; | |
2438 | ||
1da177e4 | 2439 | /* Finalize the task. */ |
788e7a89 | 2440 | rpc_call_setup(&data->task, &msg, 0); |
1da177e4 LT |
2441 | } |
2442 | ||
788e7a89 | 2443 | static int nfs4_commit_done(struct rpc_task *task, struct nfs_write_data *data) |
1da177e4 | 2444 | { |
1da177e4 LT |
2445 | struct inode *inode = data->inode; |
2446 | ||
2447 | if (nfs4_async_handle_error(task, NFS_SERVER(inode)) == -EAGAIN) { | |
2448 | rpc_restart_call(task); | |
788e7a89 | 2449 | return -EAGAIN; |
1da177e4 | 2450 | } |
4f9838c7 TM |
2451 | if (task->tk_status >= 0) |
2452 | nfs_post_op_update_inode(inode, data->res.fattr); | |
788e7a89 | 2453 | return 0; |
1da177e4 LT |
2454 | } |
2455 | ||
788e7a89 | 2456 | static void nfs4_proc_commit_setup(struct nfs_write_data *data, int how) |
1da177e4 | 2457 | { |
1da177e4 LT |
2458 | struct rpc_message msg = { |
2459 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT], | |
2460 | .rpc_argp = &data->args, | |
2461 | .rpc_resp = &data->res, | |
2462 | .rpc_cred = data->cred, | |
2463 | }; | |
788e7a89 | 2464 | struct nfs_server *server = NFS_SERVER(data->inode); |
1da177e4 | 2465 | |
4f9838c7 TM |
2466 | data->args.bitmask = server->attr_bitmask; |
2467 | data->res.server = server; | |
2468 | ||
788e7a89 | 2469 | rpc_call_setup(&data->task, &msg, 0); |
1da177e4 LT |
2470 | } |
2471 | ||
2472 | /* | |
2473 | * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special | |
2474 | * standalone procedure for queueing an asynchronous RENEW. | |
2475 | */ | |
963d8fe5 | 2476 | static void nfs4_renew_done(struct rpc_task *task, void *data) |
1da177e4 | 2477 | { |
adfa6f98 | 2478 | struct nfs_client *clp = (struct nfs_client *)task->tk_msg.rpc_argp; |
963d8fe5 | 2479 | unsigned long timestamp = (unsigned long)data; |
1da177e4 LT |
2480 | |
2481 | if (task->tk_status < 0) { | |
2482 | switch (task->tk_status) { | |
2483 | case -NFS4ERR_STALE_CLIENTID: | |
2484 | case -NFS4ERR_EXPIRED: | |
2485 | case -NFS4ERR_CB_PATH_DOWN: | |
2486 | nfs4_schedule_state_recovery(clp); | |
2487 | } | |
2488 | return; | |
2489 | } | |
2490 | spin_lock(&clp->cl_lock); | |
2491 | if (time_before(clp->cl_last_renewal,timestamp)) | |
2492 | clp->cl_last_renewal = timestamp; | |
2493 | spin_unlock(&clp->cl_lock); | |
2494 | } | |
2495 | ||
963d8fe5 TM |
2496 | static const struct rpc_call_ops nfs4_renew_ops = { |
2497 | .rpc_call_done = nfs4_renew_done, | |
2498 | }; | |
2499 | ||
adfa6f98 | 2500 | int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred) |
1da177e4 LT |
2501 | { |
2502 | struct rpc_message msg = { | |
2503 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW], | |
2504 | .rpc_argp = clp, | |
b4454fe1 | 2505 | .rpc_cred = cred, |
1da177e4 LT |
2506 | }; |
2507 | ||
2508 | return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_SOFT, | |
963d8fe5 | 2509 | &nfs4_renew_ops, (void *)jiffies); |
1da177e4 LT |
2510 | } |
2511 | ||
adfa6f98 | 2512 | int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred) |
1da177e4 LT |
2513 | { |
2514 | struct rpc_message msg = { | |
2515 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW], | |
2516 | .rpc_argp = clp, | |
b4454fe1 | 2517 | .rpc_cred = cred, |
1da177e4 LT |
2518 | }; |
2519 | unsigned long now = jiffies; | |
2520 | int status; | |
2521 | ||
2522 | status = rpc_call_sync(clp->cl_rpcclient, &msg, 0); | |
2523 | if (status < 0) | |
2524 | return status; | |
2525 | spin_lock(&clp->cl_lock); | |
2526 | if (time_before(clp->cl_last_renewal,now)) | |
2527 | clp->cl_last_renewal = now; | |
2528 | spin_unlock(&clp->cl_lock); | |
2529 | return 0; | |
2530 | } | |
2531 | ||
aa1870af BF |
2532 | static inline int nfs4_server_supports_acls(struct nfs_server *server) |
2533 | { | |
2534 | return (server->caps & NFS_CAP_ACLS) | |
2535 | && (server->acl_bitmask & ACL4_SUPPORT_ALLOW_ACL) | |
2536 | && (server->acl_bitmask & ACL4_SUPPORT_DENY_ACL); | |
2537 | } | |
2538 | ||
2539 | /* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_CACHE_SIZE, and that | |
2540 | * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_CACHE_SIZE) bytes on | |
2541 | * the stack. | |
2542 | */ | |
2543 | #define NFS4ACL_MAXPAGES (XATTR_SIZE_MAX >> PAGE_CACHE_SHIFT) | |
2544 | ||
2545 | static void buf_to_pages(const void *buf, size_t buflen, | |
2546 | struct page **pages, unsigned int *pgbase) | |
2547 | { | |
2548 | const void *p = buf; | |
2549 | ||
2550 | *pgbase = offset_in_page(buf); | |
2551 | p -= *pgbase; | |
2552 | while (p < buf + buflen) { | |
2553 | *(pages++) = virt_to_page(p); | |
2554 | p += PAGE_CACHE_SIZE; | |
2555 | } | |
2556 | } | |
2557 | ||
e50a1c2e BF |
2558 | struct nfs4_cached_acl { |
2559 | int cached; | |
2560 | size_t len; | |
3e9d4154 | 2561 | char data[0]; |
e50a1c2e BF |
2562 | }; |
2563 | ||
2564 | static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl) | |
2565 | { | |
2566 | struct nfs_inode *nfsi = NFS_I(inode); | |
2567 | ||
2568 | spin_lock(&inode->i_lock); | |
2569 | kfree(nfsi->nfs4_acl); | |
2570 | nfsi->nfs4_acl = acl; | |
2571 | spin_unlock(&inode->i_lock); | |
2572 | } | |
2573 | ||
2574 | static void nfs4_zap_acl_attr(struct inode *inode) | |
2575 | { | |
2576 | nfs4_set_cached_acl(inode, NULL); | |
2577 | } | |
2578 | ||
2579 | static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen) | |
2580 | { | |
2581 | struct nfs_inode *nfsi = NFS_I(inode); | |
2582 | struct nfs4_cached_acl *acl; | |
2583 | int ret = -ENOENT; | |
2584 | ||
2585 | spin_lock(&inode->i_lock); | |
2586 | acl = nfsi->nfs4_acl; | |
2587 | if (acl == NULL) | |
2588 | goto out; | |
2589 | if (buf == NULL) /* user is just asking for length */ | |
2590 | goto out_len; | |
2591 | if (acl->cached == 0) | |
2592 | goto out; | |
2593 | ret = -ERANGE; /* see getxattr(2) man page */ | |
2594 | if (acl->len > buflen) | |
2595 | goto out; | |
2596 | memcpy(buf, acl->data, acl->len); | |
2597 | out_len: | |
2598 | ret = acl->len; | |
2599 | out: | |
2600 | spin_unlock(&inode->i_lock); | |
2601 | return ret; | |
2602 | } | |
2603 | ||
2604 | static void nfs4_write_cached_acl(struct inode *inode, const char *buf, size_t acl_len) | |
2605 | { | |
2606 | struct nfs4_cached_acl *acl; | |
2607 | ||
2608 | if (buf && acl_len <= PAGE_SIZE) { | |
2609 | acl = kmalloc(sizeof(*acl) + acl_len, GFP_KERNEL); | |
2610 | if (acl == NULL) | |
2611 | goto out; | |
2612 | acl->cached = 1; | |
2613 | memcpy(acl->data, buf, acl_len); | |
2614 | } else { | |
2615 | acl = kmalloc(sizeof(*acl), GFP_KERNEL); | |
2616 | if (acl == NULL) | |
2617 | goto out; | |
2618 | acl->cached = 0; | |
2619 | } | |
2620 | acl->len = acl_len; | |
2621 | out: | |
2622 | nfs4_set_cached_acl(inode, acl); | |
2623 | } | |
2624 | ||
16b4289c | 2625 | static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen) |
aa1870af | 2626 | { |
aa1870af BF |
2627 | struct page *pages[NFS4ACL_MAXPAGES]; |
2628 | struct nfs_getaclargs args = { | |
2629 | .fh = NFS_FH(inode), | |
2630 | .acl_pages = pages, | |
2631 | .acl_len = buflen, | |
2632 | }; | |
2633 | size_t resp_len = buflen; | |
e50a1c2e | 2634 | void *resp_buf; |
aa1870af BF |
2635 | struct rpc_message msg = { |
2636 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL], | |
2637 | .rpc_argp = &args, | |
2638 | .rpc_resp = &resp_len, | |
2639 | }; | |
e50a1c2e | 2640 | struct page *localpage = NULL; |
aa1870af BF |
2641 | int ret; |
2642 | ||
e50a1c2e BF |
2643 | if (buflen < PAGE_SIZE) { |
2644 | /* As long as we're doing a round trip to the server anyway, | |
2645 | * let's be prepared for a page of acl data. */ | |
2646 | localpage = alloc_page(GFP_KERNEL); | |
2647 | resp_buf = page_address(localpage); | |
2648 | if (localpage == NULL) | |
2649 | return -ENOMEM; | |
2650 | args.acl_pages[0] = localpage; | |
2651 | args.acl_pgbase = 0; | |
1d95db8e | 2652 | resp_len = args.acl_len = PAGE_SIZE; |
e50a1c2e BF |
2653 | } else { |
2654 | resp_buf = buf; | |
2655 | buf_to_pages(buf, buflen, args.acl_pages, &args.acl_pgbase); | |
2656 | } | |
aa1870af | 2657 | ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0); |
e50a1c2e BF |
2658 | if (ret) |
2659 | goto out_free; | |
2660 | if (resp_len > args.acl_len) | |
2661 | nfs4_write_cached_acl(inode, NULL, resp_len); | |
2662 | else | |
2663 | nfs4_write_cached_acl(inode, resp_buf, resp_len); | |
2664 | if (buf) { | |
2665 | ret = -ERANGE; | |
2666 | if (resp_len > buflen) | |
2667 | goto out_free; | |
2668 | if (localpage) | |
2669 | memcpy(buf, resp_buf, resp_len); | |
2670 | } | |
2671 | ret = resp_len; | |
2672 | out_free: | |
2673 | if (localpage) | |
2674 | __free_page(localpage); | |
aa1870af BF |
2675 | return ret; |
2676 | } | |
2677 | ||
16b4289c TM |
2678 | static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen) |
2679 | { | |
2680 | struct nfs4_exception exception = { }; | |
2681 | ssize_t ret; | |
2682 | do { | |
2683 | ret = __nfs4_get_acl_uncached(inode, buf, buflen); | |
2684 | if (ret >= 0) | |
2685 | break; | |
2686 | ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception); | |
2687 | } while (exception.retry); | |
2688 | return ret; | |
2689 | } | |
2690 | ||
e50a1c2e BF |
2691 | static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen) |
2692 | { | |
2693 | struct nfs_server *server = NFS_SERVER(inode); | |
2694 | int ret; | |
2695 | ||
2696 | if (!nfs4_server_supports_acls(server)) | |
2697 | return -EOPNOTSUPP; | |
2698 | ret = nfs_revalidate_inode(server, inode); | |
2699 | if (ret < 0) | |
2700 | return ret; | |
2701 | ret = nfs4_read_cached_acl(inode, buf, buflen); | |
2702 | if (ret != -ENOENT) | |
2703 | return ret; | |
2704 | return nfs4_get_acl_uncached(inode, buf, buflen); | |
2705 | } | |
2706 | ||
16b4289c | 2707 | static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen) |
4b580ee3 BF |
2708 | { |
2709 | struct nfs_server *server = NFS_SERVER(inode); | |
2710 | struct page *pages[NFS4ACL_MAXPAGES]; | |
2711 | struct nfs_setaclargs arg = { | |
2712 | .fh = NFS_FH(inode), | |
2713 | .acl_pages = pages, | |
2714 | .acl_len = buflen, | |
2715 | }; | |
2716 | struct rpc_message msg = { | |
2717 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL], | |
2718 | .rpc_argp = &arg, | |
2719 | .rpc_resp = NULL, | |
2720 | }; | |
2721 | int ret; | |
2722 | ||
2723 | if (!nfs4_server_supports_acls(server)) | |
2724 | return -EOPNOTSUPP; | |
642ac549 | 2725 | nfs_inode_return_delegation(inode); |
4b580ee3 | 2726 | buf_to_pages(buf, buflen, arg.acl_pages, &arg.acl_pgbase); |
1f163415 | 2727 | ret = rpc_call_sync(NFS_CLIENT(inode), &msg, 0); |
08efa202 | 2728 | nfs_zap_caches(inode); |
4b580ee3 BF |
2729 | return ret; |
2730 | } | |
2731 | ||
16b4289c TM |
2732 | static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen) |
2733 | { | |
2734 | struct nfs4_exception exception = { }; | |
2735 | int err; | |
2736 | do { | |
2737 | err = nfs4_handle_exception(NFS_SERVER(inode), | |
2738 | __nfs4_proc_set_acl(inode, buf, buflen), | |
2739 | &exception); | |
2740 | } while (exception.retry); | |
2741 | return err; | |
2742 | } | |
2743 | ||
1da177e4 | 2744 | static int |
faf5f49c | 2745 | nfs4_async_handle_error(struct rpc_task *task, const struct nfs_server *server) |
1da177e4 | 2746 | { |
7539bbab | 2747 | struct nfs_client *clp = server->nfs_client; |
1da177e4 LT |
2748 | |
2749 | if (!clp || task->tk_status >= 0) | |
2750 | return 0; | |
2751 | switch(task->tk_status) { | |
2752 | case -NFS4ERR_STALE_CLIENTID: | |
2753 | case -NFS4ERR_STALE_STATEID: | |
2754 | case -NFS4ERR_EXPIRED: | |
2755 | rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL, NULL); | |
2756 | nfs4_schedule_state_recovery(clp); | |
433fbe4c | 2757 | if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) == 0) |
1da177e4 LT |
2758 | rpc_wake_up_task(task); |
2759 | task->tk_status = 0; | |
2760 | return -EAGAIN; | |
1da177e4 | 2761 | case -NFS4ERR_DELAY: |
006ea73e CL |
2762 | nfs_inc_server_stats((struct nfs_server *) server, |
2763 | NFSIOS_DELAY); | |
2764 | case -NFS4ERR_GRACE: | |
1da177e4 LT |
2765 | rpc_delay(task, NFS4_POLL_RETRY_MAX); |
2766 | task->tk_status = 0; | |
2767 | return -EAGAIN; | |
2768 | case -NFS4ERR_OLD_STATEID: | |
2769 | task->tk_status = 0; | |
2770 | return -EAGAIN; | |
2771 | } | |
2772 | task->tk_status = nfs4_map_errors(task->tk_status); | |
2773 | return 0; | |
2774 | } | |
2775 | ||
433fbe4c TM |
2776 | static int nfs4_wait_bit_interruptible(void *word) |
2777 | { | |
2778 | if (signal_pending(current)) | |
2779 | return -ERESTARTSYS; | |
2780 | schedule(); | |
2781 | return 0; | |
2782 | } | |
2783 | ||
adfa6f98 | 2784 | static int nfs4_wait_clnt_recover(struct rpc_clnt *clnt, struct nfs_client *clp) |
1da177e4 | 2785 | { |
1da177e4 | 2786 | sigset_t oldset; |
433fbe4c | 2787 | int res; |
1da177e4 LT |
2788 | |
2789 | might_sleep(); | |
2790 | ||
e148582e TM |
2791 | rwsem_acquire(&clp->cl_sem.dep_map, 0, 0, _RET_IP_); |
2792 | ||
1da177e4 | 2793 | rpc_clnt_sigmask(clnt, &oldset); |
433fbe4c TM |
2794 | res = wait_on_bit(&clp->cl_state, NFS4CLNT_STATE_RECOVER, |
2795 | nfs4_wait_bit_interruptible, | |
2796 | TASK_INTERRUPTIBLE); | |
1da177e4 | 2797 | rpc_clnt_sigunmask(clnt, &oldset); |
e148582e TM |
2798 | |
2799 | rwsem_release(&clp->cl_sem.dep_map, 1, _RET_IP_); | |
1da177e4 LT |
2800 | return res; |
2801 | } | |
2802 | ||
2803 | static int nfs4_delay(struct rpc_clnt *clnt, long *timeout) | |
2804 | { | |
2805 | sigset_t oldset; | |
2806 | int res = 0; | |
2807 | ||
2808 | might_sleep(); | |
2809 | ||
2810 | if (*timeout <= 0) | |
2811 | *timeout = NFS4_POLL_RETRY_MIN; | |
2812 | if (*timeout > NFS4_POLL_RETRY_MAX) | |
2813 | *timeout = NFS4_POLL_RETRY_MAX; | |
2814 | rpc_clnt_sigmask(clnt, &oldset); | |
2815 | if (clnt->cl_intr) { | |
041e0e3b | 2816 | schedule_timeout_interruptible(*timeout); |
1da177e4 LT |
2817 | if (signalled()) |
2818 | res = -ERESTARTSYS; | |
041e0e3b NA |
2819 | } else |
2820 | schedule_timeout_uninterruptible(*timeout); | |
1da177e4 LT |
2821 | rpc_clnt_sigunmask(clnt, &oldset); |
2822 | *timeout <<= 1; | |
2823 | return res; | |
2824 | } | |
2825 | ||
2826 | /* This is the error handling routine for processes that are allowed | |
2827 | * to sleep. | |
2828 | */ | |
10afec90 | 2829 | static int nfs4_handle_exception(const struct nfs_server *server, int errorcode, struct nfs4_exception *exception) |
1da177e4 | 2830 | { |
7539bbab | 2831 | struct nfs_client *clp = server->nfs_client; |
1da177e4 LT |
2832 | int ret = errorcode; |
2833 | ||
2834 | exception->retry = 0; | |
2835 | switch(errorcode) { | |
2836 | case 0: | |
2837 | return 0; | |
2838 | case -NFS4ERR_STALE_CLIENTID: | |
2839 | case -NFS4ERR_STALE_STATEID: | |
2840 | case -NFS4ERR_EXPIRED: | |
433fbe4c | 2841 | nfs4_schedule_state_recovery(clp); |
1da177e4 LT |
2842 | ret = nfs4_wait_clnt_recover(server->client, clp); |
2843 | if (ret == 0) | |
2844 | exception->retry = 1; | |
2845 | break; | |
c514983d | 2846 | case -NFS4ERR_FILE_OPEN: |
1da177e4 LT |
2847 | case -NFS4ERR_GRACE: |
2848 | case -NFS4ERR_DELAY: | |
2849 | ret = nfs4_delay(server->client, &exception->timeout); | |
2c56617d TM |
2850 | if (ret != 0) |
2851 | break; | |
1da177e4 | 2852 | case -NFS4ERR_OLD_STATEID: |
2c56617d | 2853 | exception->retry = 1; |
1da177e4 LT |
2854 | } |
2855 | /* We failed to handle the error */ | |
2856 | return nfs4_map_errors(ret); | |
2857 | } | |
2858 | ||
adfa6f98 | 2859 | int nfs4_proc_setclientid(struct nfs_client *clp, u32 program, unsigned short port, struct rpc_cred *cred) |
1da177e4 LT |
2860 | { |
2861 | nfs4_verifier sc_verifier; | |
2862 | struct nfs4_setclientid setclientid = { | |
2863 | .sc_verifier = &sc_verifier, | |
2864 | .sc_prog = program, | |
2865 | }; | |
2866 | struct rpc_message msg = { | |
2867 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID], | |
2868 | .rpc_argp = &setclientid, | |
2869 | .rpc_resp = clp, | |
286d7d6a | 2870 | .rpc_cred = cred, |
1da177e4 | 2871 | }; |
bc4785cd | 2872 | __be32 *p; |
1da177e4 LT |
2873 | int loop = 0; |
2874 | int status; | |
2875 | ||
bc4785cd | 2876 | p = (__be32*)sc_verifier.data; |
1da177e4 LT |
2877 | *p++ = htonl((u32)clp->cl_boot_time.tv_sec); |
2878 | *p = htonl((u32)clp->cl_boot_time.tv_nsec); | |
2879 | ||
2880 | for(;;) { | |
2881 | setclientid.sc_name_len = scnprintf(setclientid.sc_name, | |
2882 | sizeof(setclientid.sc_name), "%s/%u.%u.%u.%u %s %u", | |
24c8dbbb | 2883 | clp->cl_ipaddr, NIPQUAD(clp->cl_addr.sin_addr), |
286d7d6a | 2884 | cred->cr_ops->cr_name, |
1da177e4 LT |
2885 | clp->cl_id_uniquifier); |
2886 | setclientid.sc_netid_len = scnprintf(setclientid.sc_netid, | |
2887 | sizeof(setclientid.sc_netid), "tcp"); | |
2888 | setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr, | |
2889 | sizeof(setclientid.sc_uaddr), "%s.%d.%d", | |
2890 | clp->cl_ipaddr, port >> 8, port & 255); | |
2891 | ||
2892 | status = rpc_call_sync(clp->cl_rpcclient, &msg, 0); | |
2893 | if (status != -NFS4ERR_CLID_INUSE) | |
2894 | break; | |
2895 | if (signalled()) | |
2896 | break; | |
2897 | if (loop++ & 1) | |
2898 | ssleep(clp->cl_lease_time + 1); | |
2899 | else | |
2900 | if (++clp->cl_id_uniquifier == 0) | |
2901 | break; | |
2902 | } | |
2903 | return status; | |
2904 | } | |
2905 | ||
adfa6f98 | 2906 | static int _nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred) |
1da177e4 LT |
2907 | { |
2908 | struct nfs_fsinfo fsinfo; | |
2909 | struct rpc_message msg = { | |
2910 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM], | |
2911 | .rpc_argp = clp, | |
2912 | .rpc_resp = &fsinfo, | |
286d7d6a | 2913 | .rpc_cred = cred, |
1da177e4 LT |
2914 | }; |
2915 | unsigned long now; | |
2916 | int status; | |
2917 | ||
2918 | now = jiffies; | |
2919 | status = rpc_call_sync(clp->cl_rpcclient, &msg, 0); | |
2920 | if (status == 0) { | |
2921 | spin_lock(&clp->cl_lock); | |
2922 | clp->cl_lease_time = fsinfo.lease_time * HZ; | |
2923 | clp->cl_last_renewal = now; | |
58d9714a | 2924 | clear_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state); |
1da177e4 LT |
2925 | spin_unlock(&clp->cl_lock); |
2926 | } | |
2927 | return status; | |
2928 | } | |
2929 | ||
adfa6f98 | 2930 | int nfs4_proc_setclientid_confirm(struct nfs_client *clp, struct rpc_cred *cred) |
51581f3b TM |
2931 | { |
2932 | long timeout; | |
2933 | int err; | |
2934 | do { | |
2935 | err = _nfs4_proc_setclientid_confirm(clp, cred); | |
2936 | switch (err) { | |
2937 | case 0: | |
2938 | return err; | |
2939 | case -NFS4ERR_RESOURCE: | |
2940 | /* The IBM lawyers misread another document! */ | |
2941 | case -NFS4ERR_DELAY: | |
2942 | err = nfs4_delay(clp->cl_rpcclient, &timeout); | |
2943 | } | |
2944 | } while (err == 0); | |
2945 | return err; | |
2946 | } | |
2947 | ||
fe650407 TM |
2948 | struct nfs4_delegreturndata { |
2949 | struct nfs4_delegreturnargs args; | |
fa178f29 | 2950 | struct nfs4_delegreturnres res; |
fe650407 TM |
2951 | struct nfs_fh fh; |
2952 | nfs4_stateid stateid; | |
2953 | struct rpc_cred *cred; | |
26e976a8 | 2954 | unsigned long timestamp; |
fa178f29 | 2955 | struct nfs_fattr fattr; |
fe650407 TM |
2956 | int rpc_status; |
2957 | }; | |
2958 | ||
2959 | static void nfs4_delegreturn_prepare(struct rpc_task *task, void *calldata) | |
1da177e4 | 2960 | { |
fe650407 | 2961 | struct nfs4_delegreturndata *data = calldata; |
1da177e4 LT |
2962 | struct rpc_message msg = { |
2963 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN], | |
fe650407 | 2964 | .rpc_argp = &data->args, |
fa178f29 | 2965 | .rpc_resp = &data->res, |
fe650407 | 2966 | .rpc_cred = data->cred, |
1da177e4 | 2967 | }; |
fa178f29 | 2968 | nfs_fattr_init(data->res.fattr); |
fe650407 TM |
2969 | rpc_call_setup(task, &msg, 0); |
2970 | } | |
1da177e4 | 2971 | |
fe650407 TM |
2972 | static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata) |
2973 | { | |
2974 | struct nfs4_delegreturndata *data = calldata; | |
2975 | data->rpc_status = task->tk_status; | |
26e976a8 | 2976 | if (data->rpc_status == 0) |
fa178f29 | 2977 | renew_lease(data->res.server, data->timestamp); |
fe650407 TM |
2978 | } |
2979 | ||
2980 | static void nfs4_delegreturn_release(void *calldata) | |
2981 | { | |
2982 | struct nfs4_delegreturndata *data = calldata; | |
2983 | ||
2984 | put_rpccred(data->cred); | |
2985 | kfree(calldata); | |
2986 | } | |
2987 | ||
c8d149f3 | 2988 | static const struct rpc_call_ops nfs4_delegreturn_ops = { |
fe650407 TM |
2989 | .rpc_call_prepare = nfs4_delegreturn_prepare, |
2990 | .rpc_call_done = nfs4_delegreturn_done, | |
2991 | .rpc_release = nfs4_delegreturn_release, | |
2992 | }; | |
2993 | ||
2994 | static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid) | |
2995 | { | |
2996 | struct nfs4_delegreturndata *data; | |
fa178f29 | 2997 | struct nfs_server *server = NFS_SERVER(inode); |
fe650407 TM |
2998 | struct rpc_task *task; |
2999 | int status; | |
3000 | ||
3001 | data = kmalloc(sizeof(*data), GFP_KERNEL); | |
3002 | if (data == NULL) | |
3003 | return -ENOMEM; | |
3004 | data->args.fhandle = &data->fh; | |
3005 | data->args.stateid = &data->stateid; | |
fa178f29 | 3006 | data->args.bitmask = server->attr_bitmask; |
fe650407 TM |
3007 | nfs_copy_fh(&data->fh, NFS_FH(inode)); |
3008 | memcpy(&data->stateid, stateid, sizeof(data->stateid)); | |
fa178f29 TM |
3009 | data->res.fattr = &data->fattr; |
3010 | data->res.server = server; | |
fe650407 | 3011 | data->cred = get_rpccred(cred); |
26e976a8 | 3012 | data->timestamp = jiffies; |
fe650407 TM |
3013 | data->rpc_status = 0; |
3014 | ||
3015 | task = rpc_run_task(NFS_CLIENT(inode), RPC_TASK_ASYNC, &nfs4_delegreturn_ops, data); | |
7a1218a2 | 3016 | if (IS_ERR(task)) |
fe650407 | 3017 | return PTR_ERR(task); |
fe650407 | 3018 | status = nfs4_wait_for_completion_rpc_task(task); |
fa178f29 | 3019 | if (status == 0) { |
fe650407 | 3020 | status = data->rpc_status; |
fa178f29 TM |
3021 | if (status == 0) |
3022 | nfs_post_op_update_inode(inode, &data->fattr); | |
3023 | } | |
e6b3c4db | 3024 | rpc_put_task(task); |
fe650407 | 3025 | return status; |
1da177e4 LT |
3026 | } |
3027 | ||
3028 | int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid) | |
3029 | { | |
3030 | struct nfs_server *server = NFS_SERVER(inode); | |
3031 | struct nfs4_exception exception = { }; | |
3032 | int err; | |
3033 | do { | |
3034 | err = _nfs4_proc_delegreturn(inode, cred, stateid); | |
3035 | switch (err) { | |
3036 | case -NFS4ERR_STALE_STATEID: | |
3037 | case -NFS4ERR_EXPIRED: | |
1da177e4 LT |
3038 | case 0: |
3039 | return 0; | |
3040 | } | |
3041 | err = nfs4_handle_exception(server, err, &exception); | |
3042 | } while (exception.retry); | |
3043 | return err; | |
3044 | } | |
3045 | ||
3046 | #define NFS4_LOCK_MINTIMEOUT (1 * HZ) | |
3047 | #define NFS4_LOCK_MAXTIMEOUT (30 * HZ) | |
3048 | ||
3049 | /* | |
3050 | * sleep, with exponential backoff, and retry the LOCK operation. | |
3051 | */ | |
3052 | static unsigned long | |
3053 | nfs4_set_lock_task_retry(unsigned long timeout) | |
3054 | { | |
041e0e3b | 3055 | schedule_timeout_interruptible(timeout); |
1da177e4 LT |
3056 | timeout <<= 1; |
3057 | if (timeout > NFS4_LOCK_MAXTIMEOUT) | |
3058 | return NFS4_LOCK_MAXTIMEOUT; | |
3059 | return timeout; | |
3060 | } | |
3061 | ||
1da177e4 LT |
3062 | static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request) |
3063 | { | |
3064 | struct inode *inode = state->inode; | |
3065 | struct nfs_server *server = NFS_SERVER(inode); | |
7539bbab | 3066 | struct nfs_client *clp = server->nfs_client; |
911d1aaf | 3067 | struct nfs_lockt_args arg = { |
1da177e4 | 3068 | .fh = NFS_FH(inode), |
911d1aaf | 3069 | .fl = request, |
1da177e4 | 3070 | }; |
911d1aaf TM |
3071 | struct nfs_lockt_res res = { |
3072 | .denied = request, | |
1da177e4 LT |
3073 | }; |
3074 | struct rpc_message msg = { | |
3075 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT], | |
3076 | .rpc_argp = &arg, | |
3077 | .rpc_resp = &res, | |
3078 | .rpc_cred = state->owner->so_cred, | |
3079 | }; | |
1da177e4 LT |
3080 | struct nfs4_lock_state *lsp; |
3081 | int status; | |
3082 | ||
3083 | down_read(&clp->cl_sem); | |
911d1aaf | 3084 | arg.lock_owner.clientid = clp->cl_clientid; |
8d0a8a9d TM |
3085 | status = nfs4_set_lock_state(state, request); |
3086 | if (status != 0) | |
3087 | goto out; | |
3088 | lsp = request->fl_u.nfs4_fl.owner; | |
9f958ab8 | 3089 | arg.lock_owner.id = lsp->ls_id.id; |
1da177e4 | 3090 | status = rpc_call_sync(server->client, &msg, 0); |
911d1aaf TM |
3091 | switch (status) { |
3092 | case 0: | |
3093 | request->fl_type = F_UNLCK; | |
3094 | break; | |
3095 | case -NFS4ERR_DENIED: | |
3096 | status = 0; | |
1da177e4 | 3097 | } |
70cc6487 | 3098 | request->fl_ops->fl_release_private(request); |
8d0a8a9d | 3099 | out: |
1da177e4 LT |
3100 | up_read(&clp->cl_sem); |
3101 | return status; | |
3102 | } | |
3103 | ||
3104 | static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request) | |
3105 | { | |
3106 | struct nfs4_exception exception = { }; | |
3107 | int err; | |
3108 | ||
3109 | do { | |
3110 | err = nfs4_handle_exception(NFS_SERVER(state->inode), | |
3111 | _nfs4_proc_getlk(state, cmd, request), | |
3112 | &exception); | |
3113 | } while (exception.retry); | |
3114 | return err; | |
3115 | } | |
3116 | ||
3117 | static int do_vfs_lock(struct file *file, struct file_lock *fl) | |
3118 | { | |
3119 | int res = 0; | |
3120 | switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) { | |
3121 | case FL_POSIX: | |
3122 | res = posix_lock_file_wait(file, fl); | |
3123 | break; | |
3124 | case FL_FLOCK: | |
3125 | res = flock_lock_file_wait(file, fl); | |
3126 | break; | |
3127 | default: | |
3128 | BUG(); | |
3129 | } | |
1da177e4 LT |
3130 | return res; |
3131 | } | |
3132 | ||
faf5f49c | 3133 | struct nfs4_unlockdata { |
911d1aaf TM |
3134 | struct nfs_locku_args arg; |
3135 | struct nfs_locku_res res; | |
faf5f49c TM |
3136 | struct nfs4_lock_state *lsp; |
3137 | struct nfs_open_context *ctx; | |
911d1aaf TM |
3138 | struct file_lock fl; |
3139 | const struct nfs_server *server; | |
26e976a8 | 3140 | unsigned long timestamp; |
faf5f49c TM |
3141 | }; |
3142 | ||
911d1aaf TM |
3143 | static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl, |
3144 | struct nfs_open_context *ctx, | |
3145 | struct nfs4_lock_state *lsp, | |
3146 | struct nfs_seqid *seqid) | |
3147 | { | |
3148 | struct nfs4_unlockdata *p; | |
3149 | struct inode *inode = lsp->ls_state->inode; | |
3150 | ||
3151 | p = kmalloc(sizeof(*p), GFP_KERNEL); | |
3152 | if (p == NULL) | |
3153 | return NULL; | |
3154 | p->arg.fh = NFS_FH(inode); | |
3155 | p->arg.fl = &p->fl; | |
3156 | p->arg.seqid = seqid; | |
3157 | p->arg.stateid = &lsp->ls_stateid; | |
3158 | p->lsp = lsp; | |
3159 | atomic_inc(&lsp->ls_count); | |
3160 | /* Ensure we don't close file until we're done freeing locks! */ | |
3161 | p->ctx = get_nfs_open_context(ctx); | |
3162 | memcpy(&p->fl, fl, sizeof(p->fl)); | |
3163 | p->server = NFS_SERVER(inode); | |
3164 | return p; | |
3165 | } | |
3166 | ||
06f814a3 | 3167 | static void nfs4_locku_release_calldata(void *data) |
faf5f49c | 3168 | { |
963d8fe5 | 3169 | struct nfs4_unlockdata *calldata = data; |
911d1aaf | 3170 | nfs_free_seqid(calldata->arg.seqid); |
06f814a3 TM |
3171 | nfs4_put_lock_state(calldata->lsp); |
3172 | put_nfs_open_context(calldata->ctx); | |
3173 | kfree(calldata); | |
faf5f49c TM |
3174 | } |
3175 | ||
963d8fe5 | 3176 | static void nfs4_locku_done(struct rpc_task *task, void *data) |
faf5f49c | 3177 | { |
963d8fe5 | 3178 | struct nfs4_unlockdata *calldata = data; |
faf5f49c | 3179 | |
06f814a3 TM |
3180 | if (RPC_ASSASSINATED(task)) |
3181 | return; | |
911d1aaf | 3182 | nfs_increment_lock_seqid(task->tk_status, calldata->arg.seqid); |
faf5f49c TM |
3183 | switch (task->tk_status) { |
3184 | case 0: | |
3185 | memcpy(calldata->lsp->ls_stateid.data, | |
911d1aaf | 3186 | calldata->res.stateid.data, |
faf5f49c | 3187 | sizeof(calldata->lsp->ls_stateid.data)); |
26e976a8 | 3188 | renew_lease(calldata->server, calldata->timestamp); |
faf5f49c TM |
3189 | break; |
3190 | case -NFS4ERR_STALE_STATEID: | |
3191 | case -NFS4ERR_EXPIRED: | |
faf5f49c TM |
3192 | break; |
3193 | default: | |
a6a352e9 | 3194 | if (nfs4_async_handle_error(task, calldata->server) == -EAGAIN) |
faf5f49c | 3195 | rpc_restart_call(task); |
faf5f49c | 3196 | } |
faf5f49c TM |
3197 | } |
3198 | ||
4ce70ada | 3199 | static void nfs4_locku_prepare(struct rpc_task *task, void *data) |
faf5f49c | 3200 | { |
4ce70ada | 3201 | struct nfs4_unlockdata *calldata = data; |
1da177e4 LT |
3202 | struct rpc_message msg = { |
3203 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU], | |
faf5f49c TM |
3204 | .rpc_argp = &calldata->arg, |
3205 | .rpc_resp = &calldata->res, | |
3206 | .rpc_cred = calldata->lsp->ls_state->owner->so_cred, | |
1da177e4 | 3207 | }; |
faf5f49c | 3208 | |
911d1aaf | 3209 | if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0) |
faf5f49c TM |
3210 | return; |
3211 | if ((calldata->lsp->ls_flags & NFS_LOCK_INITIALIZED) == 0) { | |
963d8fe5 TM |
3212 | /* Note: exit _without_ running nfs4_locku_done */ |
3213 | task->tk_action = NULL; | |
faf5f49c TM |
3214 | return; |
3215 | } | |
26e976a8 | 3216 | calldata->timestamp = jiffies; |
faf5f49c TM |
3217 | rpc_call_setup(task, &msg, 0); |
3218 | } | |
3219 | ||
963d8fe5 | 3220 | static const struct rpc_call_ops nfs4_locku_ops = { |
4ce70ada | 3221 | .rpc_call_prepare = nfs4_locku_prepare, |
963d8fe5 | 3222 | .rpc_call_done = nfs4_locku_done, |
06f814a3 | 3223 | .rpc_release = nfs4_locku_release_calldata, |
963d8fe5 TM |
3224 | }; |
3225 | ||
a5d16a4d TM |
3226 | static struct rpc_task *nfs4_do_unlck(struct file_lock *fl, |
3227 | struct nfs_open_context *ctx, | |
3228 | struct nfs4_lock_state *lsp, | |
3229 | struct nfs_seqid *seqid) | |
3230 | { | |
3231 | struct nfs4_unlockdata *data; | |
a5d16a4d TM |
3232 | |
3233 | data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid); | |
3234 | if (data == NULL) { | |
3235 | nfs_free_seqid(seqid); | |
3236 | return ERR_PTR(-ENOMEM); | |
3237 | } | |
3238 | ||
7a1218a2 | 3239 | return rpc_run_task(NFS_CLIENT(lsp->ls_state->inode), RPC_TASK_ASYNC, &nfs4_locku_ops, data); |
a5d16a4d TM |
3240 | } |
3241 | ||
faf5f49c TM |
3242 | static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request) |
3243 | { | |
911d1aaf | 3244 | struct nfs_seqid *seqid; |
1da177e4 | 3245 | struct nfs4_lock_state *lsp; |
06f814a3 TM |
3246 | struct rpc_task *task; |
3247 | int status = 0; | |
faf5f49c | 3248 | |
8d0a8a9d | 3249 | status = nfs4_set_lock_state(state, request); |
9b073574 TM |
3250 | /* Unlock _before_ we do the RPC call */ |
3251 | request->fl_flags |= FL_EXISTS; | |
3252 | if (do_vfs_lock(request->fl_file, request) == -ENOENT) | |
3253 | goto out; | |
8d0a8a9d | 3254 | if (status != 0) |
9b073574 TM |
3255 | goto out; |
3256 | /* Is this a delegated lock? */ | |
3257 | if (test_bit(NFS_DELEGATED_STATE, &state->flags)) | |
3258 | goto out; | |
8d0a8a9d | 3259 | lsp = request->fl_u.nfs4_fl.owner; |
911d1aaf | 3260 | seqid = nfs_alloc_seqid(&lsp->ls_seqid); |
9b073574 | 3261 | status = -ENOMEM; |
911d1aaf | 3262 | if (seqid == NULL) |
9b073574 | 3263 | goto out; |
a5d16a4d TM |
3264 | task = nfs4_do_unlck(request, request->fl_file->private_data, lsp, seqid); |
3265 | status = PTR_ERR(task); | |
3266 | if (IS_ERR(task)) | |
9b073574 | 3267 | goto out; |
a5d16a4d | 3268 | status = nfs4_wait_for_completion_rpc_task(task); |
e6b3c4db | 3269 | rpc_put_task(task); |
9b073574 | 3270 | out: |
1da177e4 LT |
3271 | return status; |
3272 | } | |
3273 | ||
a5d16a4d TM |
3274 | struct nfs4_lockdata { |
3275 | struct nfs_lock_args arg; | |
3276 | struct nfs_lock_res res; | |
3277 | struct nfs4_lock_state *lsp; | |
3278 | struct nfs_open_context *ctx; | |
3279 | struct file_lock fl; | |
26e976a8 | 3280 | unsigned long timestamp; |
a5d16a4d TM |
3281 | int rpc_status; |
3282 | int cancelled; | |
3283 | }; | |
3284 | ||
3285 | static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl, | |
3286 | struct nfs_open_context *ctx, struct nfs4_lock_state *lsp) | |
1da177e4 | 3287 | { |
a5d16a4d TM |
3288 | struct nfs4_lockdata *p; |
3289 | struct inode *inode = lsp->ls_state->inode; | |
1da177e4 | 3290 | struct nfs_server *server = NFS_SERVER(inode); |
a5d16a4d TM |
3291 | |
3292 | p = kzalloc(sizeof(*p), GFP_KERNEL); | |
3293 | if (p == NULL) | |
3294 | return NULL; | |
3295 | ||
3296 | p->arg.fh = NFS_FH(inode); | |
3297 | p->arg.fl = &p->fl; | |
3298 | p->arg.lock_seqid = nfs_alloc_seqid(&lsp->ls_seqid); | |
3299 | if (p->arg.lock_seqid == NULL) | |
3300 | goto out_free; | |
3301 | p->arg.lock_stateid = &lsp->ls_stateid; | |
7539bbab | 3302 | p->arg.lock_owner.clientid = server->nfs_client->cl_clientid; |
9f958ab8 | 3303 | p->arg.lock_owner.id = lsp->ls_id.id; |
a5d16a4d TM |
3304 | p->lsp = lsp; |
3305 | atomic_inc(&lsp->ls_count); | |
3306 | p->ctx = get_nfs_open_context(ctx); | |
3307 | memcpy(&p->fl, fl, sizeof(p->fl)); | |
3308 | return p; | |
3309 | out_free: | |
3310 | kfree(p); | |
3311 | return NULL; | |
3312 | } | |
3313 | ||
3314 | static void nfs4_lock_prepare(struct rpc_task *task, void *calldata) | |
3315 | { | |
3316 | struct nfs4_lockdata *data = calldata; | |
3317 | struct nfs4_state *state = data->lsp->ls_state; | |
3318 | struct nfs4_state_owner *sp = state->owner; | |
1da177e4 | 3319 | struct rpc_message msg = { |
a5d16a4d TM |
3320 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK], |
3321 | .rpc_argp = &data->arg, | |
3322 | .rpc_resp = &data->res, | |
3323 | .rpc_cred = sp->so_cred, | |
1da177e4 | 3324 | }; |
06735b34 | 3325 | |
a5d16a4d TM |
3326 | if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0) |
3327 | return; | |
3328 | dprintk("%s: begin!\n", __FUNCTION__); | |
3329 | /* Do we need to do an open_to_lock_owner? */ | |
3330 | if (!(data->arg.lock_seqid->sequence->flags & NFS_SEQID_CONFIRMED)) { | |
3331 | data->arg.open_seqid = nfs_alloc_seqid(&sp->so_seqid); | |
3332 | if (data->arg.open_seqid == NULL) { | |
3333 | data->rpc_status = -ENOMEM; | |
3334 | task->tk_action = NULL; | |
06735b34 | 3335 | goto out; |
8d0a8a9d | 3336 | } |
a5d16a4d TM |
3337 | data->arg.open_stateid = &state->stateid; |
3338 | data->arg.new_lock_owner = 1; | |
3339 | } | |
26e976a8 | 3340 | data->timestamp = jiffies; |
a5d16a4d | 3341 | rpc_call_setup(task, &msg, 0); |
06735b34 | 3342 | out: |
a5d16a4d TM |
3343 | dprintk("%s: done!, ret = %d\n", __FUNCTION__, data->rpc_status); |
3344 | } | |
3345 | ||
3346 | static void nfs4_lock_done(struct rpc_task *task, void *calldata) | |
3347 | { | |
3348 | struct nfs4_lockdata *data = calldata; | |
3349 | ||
3350 | dprintk("%s: begin!\n", __FUNCTION__); | |
3351 | ||
3352 | data->rpc_status = task->tk_status; | |
3353 | if (RPC_ASSASSINATED(task)) | |
3354 | goto out; | |
3355 | if (data->arg.new_lock_owner != 0) { | |
3356 | nfs_increment_open_seqid(data->rpc_status, data->arg.open_seqid); | |
3357 | if (data->rpc_status == 0) | |
3358 | nfs_confirm_seqid(&data->lsp->ls_seqid, 0); | |
3359 | else | |
3360 | goto out; | |
3361 | } | |
3362 | if (data->rpc_status == 0) { | |
3363 | memcpy(data->lsp->ls_stateid.data, data->res.stateid.data, | |
3364 | sizeof(data->lsp->ls_stateid.data)); | |
3365 | data->lsp->ls_flags |= NFS_LOCK_INITIALIZED; | |
88be9f99 | 3366 | renew_lease(NFS_SERVER(data->ctx->path.dentry->d_inode), data->timestamp); |
a5d16a4d TM |
3367 | } |
3368 | nfs_increment_lock_seqid(data->rpc_status, data->arg.lock_seqid); | |
3369 | out: | |
3370 | dprintk("%s: done, ret = %d!\n", __FUNCTION__, data->rpc_status); | |
3371 | } | |
3372 | ||
3373 | static void nfs4_lock_release(void *calldata) | |
3374 | { | |
3375 | struct nfs4_lockdata *data = calldata; | |
3376 | ||
3377 | dprintk("%s: begin!\n", __FUNCTION__); | |
3378 | if (data->arg.open_seqid != NULL) | |
3379 | nfs_free_seqid(data->arg.open_seqid); | |
3380 | if (data->cancelled != 0) { | |
3381 | struct rpc_task *task; | |
3382 | task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp, | |
3383 | data->arg.lock_seqid); | |
3384 | if (!IS_ERR(task)) | |
e6b3c4db | 3385 | rpc_put_task(task); |
a5d16a4d TM |
3386 | dprintk("%s: cancelling lock!\n", __FUNCTION__); |
3387 | } else | |
3388 | nfs_free_seqid(data->arg.lock_seqid); | |
3389 | nfs4_put_lock_state(data->lsp); | |
3390 | put_nfs_open_context(data->ctx); | |
3391 | kfree(data); | |
3392 | dprintk("%s: done!\n", __FUNCTION__); | |
3393 | } | |
3394 | ||
3395 | static const struct rpc_call_ops nfs4_lock_ops = { | |
3396 | .rpc_call_prepare = nfs4_lock_prepare, | |
3397 | .rpc_call_done = nfs4_lock_done, | |
3398 | .rpc_release = nfs4_lock_release, | |
3399 | }; | |
3400 | ||
3401 | static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int reclaim) | |
3402 | { | |
3403 | struct nfs4_lockdata *data; | |
3404 | struct rpc_task *task; | |
3405 | int ret; | |
3406 | ||
3407 | dprintk("%s: begin!\n", __FUNCTION__); | |
3408 | data = nfs4_alloc_lockdata(fl, fl->fl_file->private_data, | |
3409 | fl->fl_u.nfs4_fl.owner); | |
3410 | if (data == NULL) | |
3411 | return -ENOMEM; | |
3412 | if (IS_SETLKW(cmd)) | |
3413 | data->arg.block = 1; | |
3414 | if (reclaim != 0) | |
3415 | data->arg.reclaim = 1; | |
3416 | task = rpc_run_task(NFS_CLIENT(state->inode), RPC_TASK_ASYNC, | |
3417 | &nfs4_lock_ops, data); | |
7a1218a2 | 3418 | if (IS_ERR(task)) |
a5d16a4d | 3419 | return PTR_ERR(task); |
a5d16a4d TM |
3420 | ret = nfs4_wait_for_completion_rpc_task(task); |
3421 | if (ret == 0) { | |
3422 | ret = data->rpc_status; | |
3423 | if (ret == -NFS4ERR_DENIED) | |
3424 | ret = -EAGAIN; | |
3425 | } else | |
3426 | data->cancelled = 1; | |
e6b3c4db | 3427 | rpc_put_task(task); |
a5d16a4d TM |
3428 | dprintk("%s: done, ret = %d!\n", __FUNCTION__, ret); |
3429 | return ret; | |
1da177e4 LT |
3430 | } |
3431 | ||
3432 | static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request) | |
3433 | { | |
202b50dc TM |
3434 | struct nfs_server *server = NFS_SERVER(state->inode); |
3435 | struct nfs4_exception exception = { }; | |
3436 | int err; | |
3437 | ||
3438 | do { | |
42a2d13e TM |
3439 | /* Cache the lock if possible... */ |
3440 | if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0) | |
3441 | return 0; | |
202b50dc TM |
3442 | err = _nfs4_do_setlk(state, F_SETLK, request, 1); |
3443 | if (err != -NFS4ERR_DELAY) | |
3444 | break; | |
3445 | nfs4_handle_exception(server, err, &exception); | |
3446 | } while (exception.retry); | |
3447 | return err; | |
1da177e4 LT |
3448 | } |
3449 | ||
3450 | static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request) | |
3451 | { | |
202b50dc TM |
3452 | struct nfs_server *server = NFS_SERVER(state->inode); |
3453 | struct nfs4_exception exception = { }; | |
3454 | int err; | |
3455 | ||
6bfc93ef TM |
3456 | err = nfs4_set_lock_state(state, request); |
3457 | if (err != 0) | |
3458 | return err; | |
202b50dc | 3459 | do { |
42a2d13e TM |
3460 | if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0) |
3461 | return 0; | |
202b50dc TM |
3462 | err = _nfs4_do_setlk(state, F_SETLK, request, 0); |
3463 | if (err != -NFS4ERR_DELAY) | |
3464 | break; | |
3465 | nfs4_handle_exception(server, err, &exception); | |
3466 | } while (exception.retry); | |
3467 | return err; | |
1da177e4 LT |
3468 | } |
3469 | ||
3470 | static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request) | |
3471 | { | |
adfa6f98 | 3472 | struct nfs_client *clp = state->owner->so_client; |
01c3b861 | 3473 | unsigned char fl_flags = request->fl_flags; |
1da177e4 LT |
3474 | int status; |
3475 | ||
6bfc93ef | 3476 | /* Is this a delegated open? */ |
6bfc93ef TM |
3477 | status = nfs4_set_lock_state(state, request); |
3478 | if (status != 0) | |
3479 | goto out; | |
01c3b861 TM |
3480 | request->fl_flags |= FL_ACCESS; |
3481 | status = do_vfs_lock(request->fl_file, request); | |
3482 | if (status < 0) | |
3483 | goto out; | |
3484 | down_read(&clp->cl_sem); | |
3485 | if (test_bit(NFS_DELEGATED_STATE, &state->flags)) { | |
3486 | struct nfs_inode *nfsi = NFS_I(state->inode); | |
3487 | /* Yes: cache locks! */ | |
3488 | down_read(&nfsi->rwsem); | |
3489 | /* ...but avoid races with delegation recall... */ | |
3490 | if (test_bit(NFS_DELEGATED_STATE, &state->flags)) { | |
3491 | request->fl_flags = fl_flags & ~FL_SLEEP; | |
3492 | status = do_vfs_lock(request->fl_file, request); | |
3493 | up_read(&nfsi->rwsem); | |
3494 | goto out_unlock; | |
3495 | } | |
3496 | up_read(&nfsi->rwsem); | |
3497 | } | |
6bfc93ef TM |
3498 | status = _nfs4_do_setlk(state, cmd, request, 0); |
3499 | if (status != 0) | |
01c3b861 | 3500 | goto out_unlock; |
6bfc93ef | 3501 | /* Note: we always want to sleep here! */ |
01c3b861 | 3502 | request->fl_flags = fl_flags | FL_SLEEP; |
6bfc93ef TM |
3503 | if (do_vfs_lock(request->fl_file, request) < 0) |
3504 | printk(KERN_WARNING "%s: VFS is out of sync with lock manager!\n", __FUNCTION__); | |
01c3b861 | 3505 | out_unlock: |
1da177e4 | 3506 | up_read(&clp->cl_sem); |
01c3b861 TM |
3507 | out: |
3508 | request->fl_flags = fl_flags; | |
1da177e4 LT |
3509 | return status; |
3510 | } | |
3511 | ||
3512 | static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request) | |
3513 | { | |
3514 | struct nfs4_exception exception = { }; | |
3515 | int err; | |
3516 | ||
3517 | do { | |
3518 | err = nfs4_handle_exception(NFS_SERVER(state->inode), | |
3519 | _nfs4_proc_setlk(state, cmd, request), | |
3520 | &exception); | |
3521 | } while (exception.retry); | |
3522 | return err; | |
3523 | } | |
3524 | ||
3525 | static int | |
3526 | nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request) | |
3527 | { | |
3528 | struct nfs_open_context *ctx; | |
3529 | struct nfs4_state *state; | |
3530 | unsigned long timeout = NFS4_LOCK_MINTIMEOUT; | |
3531 | int status; | |
3532 | ||
3533 | /* verify open state */ | |
3534 | ctx = (struct nfs_open_context *)filp->private_data; | |
3535 | state = ctx->state; | |
3536 | ||
3537 | if (request->fl_start < 0 || request->fl_end < 0) | |
3538 | return -EINVAL; | |
3539 | ||
3540 | if (IS_GETLK(cmd)) | |
3541 | return nfs4_proc_getlk(state, F_GETLK, request); | |
3542 | ||
3543 | if (!(IS_SETLK(cmd) || IS_SETLKW(cmd))) | |
3544 | return -EINVAL; | |
3545 | ||
3546 | if (request->fl_type == F_UNLCK) | |
3547 | return nfs4_proc_unlck(state, cmd, request); | |
3548 | ||
3549 | do { | |
3550 | status = nfs4_proc_setlk(state, cmd, request); | |
3551 | if ((status != -EAGAIN) || IS_SETLK(cmd)) | |
3552 | break; | |
3553 | timeout = nfs4_set_lock_task_retry(timeout); | |
3554 | status = -ERESTARTSYS; | |
3555 | if (signalled()) | |
3556 | break; | |
3557 | } while(status < 0); | |
1da177e4 LT |
3558 | return status; |
3559 | } | |
3560 | ||
888e694c TM |
3561 | int nfs4_lock_delegation_recall(struct nfs4_state *state, struct file_lock *fl) |
3562 | { | |
3563 | struct nfs_server *server = NFS_SERVER(state->inode); | |
3564 | struct nfs4_exception exception = { }; | |
3565 | int err; | |
3566 | ||
3567 | err = nfs4_set_lock_state(state, fl); | |
3568 | if (err != 0) | |
3569 | goto out; | |
3570 | do { | |
3571 | err = _nfs4_do_setlk(state, F_SETLK, fl, 0); | |
3572 | if (err != -NFS4ERR_DELAY) | |
3573 | break; | |
3574 | err = nfs4_handle_exception(server, err, &exception); | |
3575 | } while (exception.retry); | |
3576 | out: | |
3577 | return err; | |
3578 | } | |
6b3b5496 | 3579 | |
aa1870af BF |
3580 | #define XATTR_NAME_NFSV4_ACL "system.nfs4_acl" |
3581 | ||
6b3b5496 BF |
3582 | int nfs4_setxattr(struct dentry *dentry, const char *key, const void *buf, |
3583 | size_t buflen, int flags) | |
3584 | { | |
4b580ee3 BF |
3585 | struct inode *inode = dentry->d_inode; |
3586 | ||
3587 | if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0) | |
3588 | return -EOPNOTSUPP; | |
3589 | ||
3590 | if (!S_ISREG(inode->i_mode) && | |
3591 | (!S_ISDIR(inode->i_mode) || inode->i_mode & S_ISVTX)) | |
3592 | return -EPERM; | |
3593 | ||
3594 | return nfs4_proc_set_acl(inode, buf, buflen); | |
6b3b5496 BF |
3595 | } |
3596 | ||
3597 | /* The getxattr man page suggests returning -ENODATA for unknown attributes, | |
3598 | * and that's what we'll do for e.g. user attributes that haven't been set. | |
3599 | * But we'll follow ext2/ext3's lead by returning -EOPNOTSUPP for unsupported | |
3600 | * attributes in kernel-managed attribute namespaces. */ | |
3601 | ssize_t nfs4_getxattr(struct dentry *dentry, const char *key, void *buf, | |
3602 | size_t buflen) | |
3603 | { | |
aa1870af BF |
3604 | struct inode *inode = dentry->d_inode; |
3605 | ||
3606 | if (strcmp(key, XATTR_NAME_NFSV4_ACL) != 0) | |
3607 | return -EOPNOTSUPP; | |
3608 | ||
3609 | return nfs4_proc_get_acl(inode, buf, buflen); | |
6b3b5496 BF |
3610 | } |
3611 | ||
3612 | ssize_t nfs4_listxattr(struct dentry *dentry, char *buf, size_t buflen) | |
3613 | { | |
aa1870af | 3614 | size_t len = strlen(XATTR_NAME_NFSV4_ACL) + 1; |
6b3b5496 | 3615 | |
096455a2 BF |
3616 | if (!nfs4_server_supports_acls(NFS_SERVER(dentry->d_inode))) |
3617 | return 0; | |
6b3b5496 BF |
3618 | if (buf && buflen < len) |
3619 | return -ERANGE; | |
3620 | if (buf) | |
aa1870af BF |
3621 | memcpy(buf, XATTR_NAME_NFSV4_ACL, len); |
3622 | return len; | |
6b3b5496 BF |
3623 | } |
3624 | ||
c228fd3a | 3625 | int nfs4_proc_fs_locations(struct inode *dir, struct qstr *name, |
7aaa0b3b | 3626 | struct nfs4_fs_locations *fs_locations, struct page *page) |
683b57b4 TM |
3627 | { |
3628 | struct nfs_server *server = NFS_SERVER(dir); | |
3629 | u32 bitmask[2] = { | |
361e624f MN |
3630 | [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS, |
3631 | [1] = FATTR4_WORD1_MOUNTED_ON_FILEID, | |
683b57b4 TM |
3632 | }; |
3633 | struct nfs4_fs_locations_arg args = { | |
3634 | .dir_fh = NFS_FH(dir), | |
c228fd3a | 3635 | .name = name, |
683b57b4 TM |
3636 | .page = page, |
3637 | .bitmask = bitmask, | |
3638 | }; | |
3639 | struct rpc_message msg = { | |
3640 | .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS], | |
3641 | .rpc_argp = &args, | |
7aaa0b3b | 3642 | .rpc_resp = fs_locations, |
683b57b4 TM |
3643 | }; |
3644 | int status; | |
3645 | ||
3646 | dprintk("%s: start\n", __FUNCTION__); | |
c228fd3a | 3647 | nfs_fattr_init(&fs_locations->fattr); |
683b57b4 | 3648 | fs_locations->server = server; |
830b8e33 | 3649 | fs_locations->nlocations = 0; |
683b57b4 TM |
3650 | status = rpc_call_sync(server->client, &msg, 0); |
3651 | dprintk("%s: returned status = %d\n", __FUNCTION__, status); | |
3652 | return status; | |
3653 | } | |
3654 | ||
1da177e4 LT |
3655 | struct nfs4_state_recovery_ops nfs4_reboot_recovery_ops = { |
3656 | .recover_open = nfs4_open_reclaim, | |
3657 | .recover_lock = nfs4_lock_reclaim, | |
3658 | }; | |
3659 | ||
3660 | struct nfs4_state_recovery_ops nfs4_network_partition_recovery_ops = { | |
3661 | .recover_open = nfs4_open_expired, | |
3662 | .recover_lock = nfs4_lock_expired, | |
3663 | }; | |
3664 | ||
92e1d5be | 3665 | static const struct inode_operations nfs4_file_inode_operations = { |
6b3b5496 BF |
3666 | .permission = nfs_permission, |
3667 | .getattr = nfs_getattr, | |
3668 | .setattr = nfs_setattr, | |
3669 | .getxattr = nfs4_getxattr, | |
3670 | .setxattr = nfs4_setxattr, | |
3671 | .listxattr = nfs4_listxattr, | |
3672 | }; | |
3673 | ||
509de811 | 3674 | const struct nfs_rpc_ops nfs_v4_clientops = { |
1da177e4 LT |
3675 | .version = 4, /* protocol version */ |
3676 | .dentry_ops = &nfs4_dentry_operations, | |
3677 | .dir_inode_ops = &nfs4_dir_inode_operations, | |
6b3b5496 | 3678 | .file_inode_ops = &nfs4_file_inode_operations, |
1da177e4 LT |
3679 | .getroot = nfs4_proc_get_root, |
3680 | .getattr = nfs4_proc_getattr, | |
3681 | .setattr = nfs4_proc_setattr, | |
2b3de441 | 3682 | .lookupfh = nfs4_proc_lookupfh, |
1da177e4 LT |
3683 | .lookup = nfs4_proc_lookup, |
3684 | .access = nfs4_proc_access, | |
3685 | .readlink = nfs4_proc_readlink, | |
1da177e4 LT |
3686 | .create = nfs4_proc_create, |
3687 | .remove = nfs4_proc_remove, | |
3688 | .unlink_setup = nfs4_proc_unlink_setup, | |
3689 | .unlink_done = nfs4_proc_unlink_done, | |
3690 | .rename = nfs4_proc_rename, | |
3691 | .link = nfs4_proc_link, | |
3692 | .symlink = nfs4_proc_symlink, | |
3693 | .mkdir = nfs4_proc_mkdir, | |
3694 | .rmdir = nfs4_proc_remove, | |
3695 | .readdir = nfs4_proc_readdir, | |
3696 | .mknod = nfs4_proc_mknod, | |
3697 | .statfs = nfs4_proc_statfs, | |
3698 | .fsinfo = nfs4_proc_fsinfo, | |
3699 | .pathconf = nfs4_proc_pathconf, | |
e9326dca | 3700 | .set_capabilities = nfs4_server_capabilities, |
1da177e4 LT |
3701 | .decode_dirent = nfs4_decode_dirent, |
3702 | .read_setup = nfs4_proc_read_setup, | |
ec06c096 | 3703 | .read_done = nfs4_read_done, |
1da177e4 | 3704 | .write_setup = nfs4_proc_write_setup, |
788e7a89 | 3705 | .write_done = nfs4_write_done, |
1da177e4 | 3706 | .commit_setup = nfs4_proc_commit_setup, |
788e7a89 | 3707 | .commit_done = nfs4_commit_done, |
02a913a7 TM |
3708 | .file_open = nfs_open, |
3709 | .file_release = nfs_release, | |
1da177e4 | 3710 | .lock = nfs4_proc_lock, |
e50a1c2e | 3711 | .clear_acl_cache = nfs4_zap_acl_attr, |
1da177e4 LT |
3712 | }; |
3713 | ||
3714 | /* | |
3715 | * Local variables: | |
3716 | * c-basic-offset: 8 | |
3717 | * End: | |
3718 | */ |