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