]>
Commit | Line | Data |
---|---|---|
457c8996 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1da177e4 LT |
2 | /* |
3 | * linux/fs/nfs/dir.c | |
4 | * | |
5 | * Copyright (C) 1992 Rick Sladkey | |
6 | * | |
7 | * nfs directory handling functions | |
8 | * | |
9 | * 10 Apr 1996 Added silly rename for unlink --okir | |
10 | * 28 Sep 1996 Improved directory cache --okir | |
11 | * 23 Aug 1997 Claus Heine claus@momo.math.rwth-aachen.de | |
12 | * Re-implemented silly rename for unlink, newly implemented | |
13 | * silly rename for nfs_rename() following the suggestions | |
14 | * of Olaf Kirch (okir) found in this file. | |
15 | * Following Linus comments on my original hack, this version | |
16 | * depends only on the dcache stuff and doesn't touch the inode | |
17 | * layer (iput() and friends). | |
18 | * 6 Jun 1999 Cache readdir lookups in the page cache. -DaveM | |
19 | */ | |
20 | ||
ddda8e0a | 21 | #include <linux/module.h> |
1da177e4 LT |
22 | #include <linux/time.h> |
23 | #include <linux/errno.h> | |
24 | #include <linux/stat.h> | |
25 | #include <linux/fcntl.h> | |
26 | #include <linux/string.h> | |
27 | #include <linux/kernel.h> | |
28 | #include <linux/slab.h> | |
29 | #include <linux/mm.h> | |
30 | #include <linux/sunrpc/clnt.h> | |
31 | #include <linux/nfs_fs.h> | |
32 | #include <linux/nfs_mount.h> | |
33 | #include <linux/pagemap.h> | |
873101b3 | 34 | #include <linux/pagevec.h> |
1da177e4 | 35 | #include <linux/namei.h> |
54ceac45 | 36 | #include <linux/mount.h> |
a0b8cab3 | 37 | #include <linux/swap.h> |
e8edc6e0 | 38 | #include <linux/sched.h> |
04e4bd1c | 39 | #include <linux/kmemleak.h> |
64c2ce8b | 40 | #include <linux/xattr.h> |
1da177e4 LT |
41 | |
42 | #include "delegation.h" | |
91d5b470 | 43 | #include "iostat.h" |
4c30d56e | 44 | #include "internal.h" |
cd9a1c0e | 45 | #include "fscache.h" |
1da177e4 | 46 | |
f4ce1299 TM |
47 | #include "nfstrace.h" |
48 | ||
1da177e4 LT |
49 | /* #define NFS_DEBUG_VERBOSE 1 */ |
50 | ||
51 | static int nfs_opendir(struct inode *, struct file *); | |
480c2006 | 52 | static int nfs_closedir(struct inode *, struct file *); |
23db8620 | 53 | static int nfs_readdir(struct file *, struct dir_context *); |
02c24a82 | 54 | static int nfs_fsync_dir(struct file *, loff_t, loff_t, int); |
f0dd2136 | 55 | static loff_t nfs_llseek_dir(struct file *, loff_t, int); |
11de3b11 | 56 | static void nfs_readdir_clear_array(struct page*); |
1da177e4 | 57 | |
4b6f5d20 | 58 | const struct file_operations nfs_dir_operations = { |
f0dd2136 | 59 | .llseek = nfs_llseek_dir, |
1da177e4 | 60 | .read = generic_read_dir, |
93a6ab7b | 61 | .iterate_shared = nfs_readdir, |
1da177e4 | 62 | .open = nfs_opendir, |
480c2006 | 63 | .release = nfs_closedir, |
1da177e4 LT |
64 | .fsync = nfs_fsync_dir, |
65 | }; | |
66 | ||
11de3b11 TM |
67 | const struct address_space_operations nfs_dir_aops = { |
68 | .freepage = nfs_readdir_clear_array, | |
d1bacf9e BS |
69 | }; |
70 | ||
93b8959a | 71 | static struct nfs_open_dir_context *alloc_nfs_open_dir_context(struct inode *dir) |
480c2006 | 72 | { |
311324ad | 73 | struct nfs_inode *nfsi = NFS_I(dir); |
480c2006 BS |
74 | struct nfs_open_dir_context *ctx; |
75 | ctx = kmalloc(sizeof(*ctx), GFP_KERNEL); | |
76 | if (ctx != NULL) { | |
8ef2ce3e | 77 | ctx->duped = 0; |
311324ad | 78 | ctx->attr_gencount = nfsi->attr_gencount; |
480c2006 | 79 | ctx->dir_cookie = 0; |
8ef2ce3e | 80 | ctx->dup_cookie = 0; |
311324ad | 81 | spin_lock(&dir->i_lock); |
1c341b77 TM |
82 | if (list_empty(&nfsi->open_files) && |
83 | (nfsi->cache_validity & NFS_INO_DATA_INVAL_DEFER)) | |
84 | nfsi->cache_validity |= NFS_INO_INVALID_DATA | | |
85 | NFS_INO_REVAL_FORCED; | |
311324ad TM |
86 | list_add(&ctx->list, &nfsi->open_files); |
87 | spin_unlock(&dir->i_lock); | |
0c030806 TM |
88 | return ctx; |
89 | } | |
90 | return ERR_PTR(-ENOMEM); | |
480c2006 BS |
91 | } |
92 | ||
311324ad | 93 | static void put_nfs_open_dir_context(struct inode *dir, struct nfs_open_dir_context *ctx) |
480c2006 | 94 | { |
311324ad TM |
95 | spin_lock(&dir->i_lock); |
96 | list_del(&ctx->list); | |
97 | spin_unlock(&dir->i_lock); | |
480c2006 BS |
98 | kfree(ctx); |
99 | } | |
100 | ||
1da177e4 LT |
101 | /* |
102 | * Open file | |
103 | */ | |
104 | static int | |
105 | nfs_opendir(struct inode *inode, struct file *filp) | |
106 | { | |
480c2006 BS |
107 | int res = 0; |
108 | struct nfs_open_dir_context *ctx; | |
1da177e4 | 109 | |
6de1472f | 110 | dfprintk(FILE, "NFS: open dir(%pD2)\n", filp); |
cc0dd2d1 CL |
111 | |
112 | nfs_inc_stats(inode, NFSIOS_VFSOPEN); | |
1e7cb3dc | 113 | |
93b8959a | 114 | ctx = alloc_nfs_open_dir_context(inode); |
480c2006 BS |
115 | if (IS_ERR(ctx)) { |
116 | res = PTR_ERR(ctx); | |
117 | goto out; | |
118 | } | |
119 | filp->private_data = ctx; | |
480c2006 | 120 | out: |
1da177e4 LT |
121 | return res; |
122 | } | |
123 | ||
480c2006 BS |
124 | static int |
125 | nfs_closedir(struct inode *inode, struct file *filp) | |
126 | { | |
a455589f | 127 | put_nfs_open_dir_context(file_inode(filp), filp->private_data); |
480c2006 BS |
128 | return 0; |
129 | } | |
130 | ||
d1bacf9e BS |
131 | struct nfs_cache_array_entry { |
132 | u64 cookie; | |
133 | u64 ino; | |
a52a8a6a TM |
134 | const char *name; |
135 | unsigned int name_len; | |
0b26a0bf | 136 | unsigned char d_type; |
d1bacf9e BS |
137 | }; |
138 | ||
139 | struct nfs_cache_array { | |
d1bacf9e | 140 | u64 last_cookie; |
b1e21c97 TM |
141 | unsigned int size; |
142 | unsigned char page_full : 1, | |
143 | page_is_eof : 1; | |
5601cda8 | 144 | struct nfs_cache_array_entry array[]; |
d1bacf9e BS |
145 | }; |
146 | ||
6c981eff | 147 | struct nfs_readdir_descriptor { |
1da177e4 LT |
148 | struct file *file; |
149 | struct page *page; | |
23db8620 | 150 | struct dir_context *ctx; |
1f1d4aa4 | 151 | pgoff_t page_index; |
2e7a4641 | 152 | u64 dir_cookie; |
0aded708 | 153 | u64 last_cookie; |
2e7a4641 | 154 | u64 dup_cookie; |
f0dd2136 | 155 | loff_t current_index; |
59e356a9 | 156 | loff_t prev_index; |
d1bacf9e | 157 | |
a1147b82 | 158 | unsigned long dir_verifier; |
1f4eab7e | 159 | unsigned long timestamp; |
4704f0e2 | 160 | unsigned long gencount; |
2e7a4641 | 161 | unsigned long attr_gencount; |
d1bacf9e | 162 | unsigned int cache_entry_index; |
2e7a4641 | 163 | signed char duped; |
a7a3b1e9 BC |
164 | bool plus; |
165 | bool eof; | |
6c981eff | 166 | }; |
1da177e4 | 167 | |
1f1d4aa4 TM |
168 | static void nfs_readdir_array_init(struct nfs_cache_array *array) |
169 | { | |
170 | memset(array, 0, sizeof(struct nfs_cache_array)); | |
171 | } | |
172 | ||
173 | static void nfs_readdir_page_init_array(struct page *page, u64 last_cookie) | |
4b310319 TM |
174 | { |
175 | struct nfs_cache_array *array; | |
176 | ||
177 | array = kmap_atomic(page); | |
1f1d4aa4 TM |
178 | nfs_readdir_array_init(array); |
179 | array->last_cookie = last_cookie; | |
4b310319 TM |
180 | kunmap_atomic(array); |
181 | } | |
182 | ||
d1bacf9e BS |
183 | /* |
184 | * we are freeing strings created by nfs_add_to_readdir_array() | |
185 | */ | |
186 | static | |
11de3b11 | 187 | void nfs_readdir_clear_array(struct page *page) |
d1bacf9e | 188 | { |
11de3b11 | 189 | struct nfs_cache_array *array; |
d1bacf9e | 190 | int i; |
8cd51a0c | 191 | |
2b86ce2d | 192 | array = kmap_atomic(page); |
b044f645 | 193 | for (i = 0; i < array->size; i++) |
a52a8a6a | 194 | kfree(array->array[i].name); |
1f1d4aa4 | 195 | nfs_readdir_array_init(array); |
2b86ce2d | 196 | kunmap_atomic(array); |
d1bacf9e BS |
197 | } |
198 | ||
b1e21c97 TM |
199 | static void nfs_readdir_array_set_eof(struct nfs_cache_array *array) |
200 | { | |
201 | array->page_is_eof = 1; | |
202 | array->page_full = 1; | |
203 | } | |
204 | ||
205 | static bool nfs_readdir_array_is_full(struct nfs_cache_array *array) | |
206 | { | |
207 | return array->page_full; | |
208 | } | |
209 | ||
d1bacf9e BS |
210 | /* |
211 | * the caller is responsible for freeing qstr.name | |
212 | * when called by nfs_readdir_add_to_array, the strings will be freed in | |
213 | * nfs_clear_readdir_array() | |
214 | */ | |
a52a8a6a | 215 | static const char *nfs_readdir_copy_name(const char *name, unsigned int len) |
d1bacf9e | 216 | { |
a52a8a6a TM |
217 | const char *ret = kmemdup_nul(name, len, GFP_KERNEL); |
218 | ||
04e4bd1c CM |
219 | /* |
220 | * Avoid a kmemleak false positive. The pointer to the name is stored | |
221 | * in a page cache page which kmemleak does not scan. | |
222 | */ | |
a52a8a6a TM |
223 | if (ret != NULL) |
224 | kmemleak_not_leak(ret); | |
225 | return ret; | |
d1bacf9e BS |
226 | } |
227 | ||
b1e21c97 TM |
228 | /* |
229 | * Check that the next array entry lies entirely within the page bounds | |
230 | */ | |
231 | static int nfs_readdir_array_can_expand(struct nfs_cache_array *array) | |
232 | { | |
233 | struct nfs_cache_array_entry *cache_entry; | |
234 | ||
235 | if (array->page_full) | |
236 | return -ENOSPC; | |
237 | cache_entry = &array->array[array->size + 1]; | |
238 | if ((char *)cache_entry - (char *)array > PAGE_SIZE) { | |
239 | array->page_full = 1; | |
240 | return -ENOSPC; | |
241 | } | |
242 | return 0; | |
243 | } | |
244 | ||
d1bacf9e BS |
245 | static |
246 | int nfs_readdir_add_to_array(struct nfs_entry *entry, struct page *page) | |
247 | { | |
a52a8a6a | 248 | struct nfs_cache_array *array; |
4a201d6e | 249 | struct nfs_cache_array_entry *cache_entry; |
a52a8a6a | 250 | const char *name; |
4a201d6e TM |
251 | int ret; |
252 | ||
a52a8a6a TM |
253 | name = nfs_readdir_copy_name(entry->name, entry->len); |
254 | if (!name) | |
255 | return -ENOMEM; | |
256 | ||
257 | array = kmap_atomic(page); | |
b1e21c97 | 258 | ret = nfs_readdir_array_can_expand(array); |
a52a8a6a TM |
259 | if (ret) { |
260 | kfree(name); | |
4a201d6e | 261 | goto out; |
a52a8a6a | 262 | } |
d1bacf9e | 263 | |
b1e21c97 | 264 | cache_entry = &array->array[array->size]; |
4a201d6e TM |
265 | cache_entry->cookie = entry->prev_cookie; |
266 | cache_entry->ino = entry->ino; | |
0b26a0bf | 267 | cache_entry->d_type = entry->d_type; |
a52a8a6a TM |
268 | cache_entry->name_len = entry->len; |
269 | cache_entry->name = name; | |
d1bacf9e | 270 | array->last_cookie = entry->cookie; |
8cd51a0c | 271 | array->size++; |
47c716cb | 272 | if (entry->eof != 0) |
b1e21c97 | 273 | nfs_readdir_array_set_eof(array); |
4a201d6e | 274 | out: |
a52a8a6a | 275 | kunmap_atomic(array); |
4a201d6e | 276 | return ret; |
d1bacf9e BS |
277 | } |
278 | ||
1f1d4aa4 TM |
279 | static struct page *nfs_readdir_page_get_locked(struct address_space *mapping, |
280 | pgoff_t index, u64 last_cookie) | |
281 | { | |
282 | struct page *page; | |
283 | ||
284 | page = grab_cache_page(mapping, index); | |
285 | if (page && !PageUptodate(page)) { | |
286 | nfs_readdir_page_init_array(page, last_cookie); | |
287 | if (invalidate_inode_pages2_range(mapping, index + 1, -1) < 0) | |
288 | nfs_zap_mapping(mapping->host, mapping); | |
289 | SetPageUptodate(page); | |
290 | } | |
291 | ||
292 | return page; | |
293 | } | |
294 | ||
295 | static u64 nfs_readdir_page_last_cookie(struct page *page) | |
296 | { | |
297 | struct nfs_cache_array *array; | |
298 | u64 ret; | |
299 | ||
300 | array = kmap_atomic(page); | |
301 | ret = array->last_cookie; | |
302 | kunmap_atomic(array); | |
303 | return ret; | |
304 | } | |
305 | ||
306 | static bool nfs_readdir_page_needs_filling(struct page *page) | |
307 | { | |
308 | struct nfs_cache_array *array; | |
309 | bool ret; | |
310 | ||
311 | array = kmap_atomic(page); | |
312 | ret = !nfs_readdir_array_is_full(array); | |
313 | kunmap_atomic(array); | |
314 | return ret; | |
315 | } | |
316 | ||
b1e21c97 TM |
317 | static void nfs_readdir_page_set_eof(struct page *page) |
318 | { | |
319 | struct nfs_cache_array *array; | |
320 | ||
321 | array = kmap_atomic(page); | |
322 | nfs_readdir_array_set_eof(array); | |
323 | kunmap_atomic(array); | |
324 | } | |
325 | ||
3b2a09f1 TM |
326 | static void nfs_readdir_page_unlock_and_put(struct page *page) |
327 | { | |
328 | unlock_page(page); | |
329 | put_page(page); | |
330 | } | |
331 | ||
332 | static struct page *nfs_readdir_page_get_next(struct address_space *mapping, | |
333 | pgoff_t index, u64 cookie) | |
334 | { | |
335 | struct page *page; | |
336 | ||
337 | page = nfs_readdir_page_get_locked(mapping, index, cookie); | |
338 | if (page) { | |
339 | if (nfs_readdir_page_last_cookie(page) == cookie) | |
340 | return page; | |
341 | nfs_readdir_page_unlock_and_put(page); | |
342 | } | |
343 | return NULL; | |
344 | } | |
345 | ||
59e356a9 TM |
346 | static inline |
347 | int is_32bit_api(void) | |
348 | { | |
349 | #ifdef CONFIG_COMPAT | |
350 | return in_compat_syscall(); | |
351 | #else | |
352 | return (BITS_PER_LONG == 32); | |
353 | #endif | |
354 | } | |
355 | ||
356 | static | |
357 | bool nfs_readdir_use_cookie(const struct file *filp) | |
358 | { | |
359 | if ((filp->f_mode & FMODE_32BITHASH) || | |
360 | (!(filp->f_mode & FMODE_64BITHASH) && is_32bit_api())) | |
361 | return false; | |
362 | return true; | |
363 | } | |
364 | ||
6c981eff TM |
365 | static int nfs_readdir_search_for_pos(struct nfs_cache_array *array, |
366 | struct nfs_readdir_descriptor *desc) | |
d1bacf9e | 367 | { |
23db8620 | 368 | loff_t diff = desc->ctx->pos - desc->current_index; |
d1bacf9e BS |
369 | unsigned int index; |
370 | ||
371 | if (diff < 0) | |
372 | goto out_eof; | |
373 | if (diff >= array->size) { | |
b1e21c97 | 374 | if (array->page_is_eof) |
d1bacf9e | 375 | goto out_eof; |
d1bacf9e BS |
376 | return -EAGAIN; |
377 | } | |
378 | ||
379 | index = (unsigned int)diff; | |
2e7a4641 | 380 | desc->dir_cookie = array->array[index].cookie; |
d1bacf9e | 381 | desc->cache_entry_index = index; |
d1bacf9e BS |
382 | return 0; |
383 | out_eof: | |
6089dd0d | 384 | desc->eof = true; |
d1bacf9e BS |
385 | return -EBADCOOKIE; |
386 | } | |
387 | ||
4db72b40 JL |
388 | static bool |
389 | nfs_readdir_inode_mapping_valid(struct nfs_inode *nfsi) | |
390 | { | |
391 | if (nfsi->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA)) | |
392 | return false; | |
393 | smp_rmb(); | |
394 | return !test_bit(NFS_INO_INVALIDATING, &nfsi->flags); | |
395 | } | |
396 | ||
6c981eff TM |
397 | static int nfs_readdir_search_for_cookie(struct nfs_cache_array *array, |
398 | struct nfs_readdir_descriptor *desc) | |
d1bacf9e BS |
399 | { |
400 | int i; | |
8ef2ce3e | 401 | loff_t new_pos; |
d1bacf9e BS |
402 | int status = -EAGAIN; |
403 | ||
404 | for (i = 0; i < array->size; i++) { | |
2e7a4641 | 405 | if (array->array[i].cookie == desc->dir_cookie) { |
496ad9aa | 406 | struct nfs_inode *nfsi = NFS_I(file_inode(desc->file)); |
0c030806 | 407 | |
8ef2ce3e | 408 | new_pos = desc->current_index + i; |
2e7a4641 | 409 | if (desc->attr_gencount != nfsi->attr_gencount || |
4db72b40 | 410 | !nfs_readdir_inode_mapping_valid(nfsi)) { |
2e7a4641 TM |
411 | desc->duped = 0; |
412 | desc->attr_gencount = nfsi->attr_gencount; | |
59e356a9 | 413 | } else if (new_pos < desc->prev_index) { |
2e7a4641 TM |
414 | if (desc->duped > 0 |
415 | && desc->dup_cookie == desc->dir_cookie) { | |
0c030806 | 416 | if (printk_ratelimit()) { |
6de1472f | 417 | pr_notice("NFS: directory %pD2 contains a readdir loop." |
0c030806 | 418 | "Please contact your server vendor. " |
a52a8a6a TM |
419 | "The file: %s has duplicate cookie %llu\n", |
420 | desc->file, array->array[i].name, desc->dir_cookie); | |
0c030806 TM |
421 | } |
422 | status = -ELOOP; | |
423 | goto out; | |
424 | } | |
2e7a4641 TM |
425 | desc->dup_cookie = desc->dir_cookie; |
426 | desc->duped = -1; | |
8ef2ce3e | 427 | } |
59e356a9 | 428 | if (nfs_readdir_use_cookie(desc->file)) |
2e7a4641 | 429 | desc->ctx->pos = desc->dir_cookie; |
59e356a9 TM |
430 | else |
431 | desc->ctx->pos = new_pos; | |
432 | desc->prev_index = new_pos; | |
d1bacf9e | 433 | desc->cache_entry_index = i; |
47c716cb | 434 | return 0; |
d1bacf9e BS |
435 | } |
436 | } | |
b1e21c97 | 437 | if (array->page_is_eof) { |
8cd51a0c | 438 | status = -EBADCOOKIE; |
2e7a4641 | 439 | if (desc->dir_cookie == array->last_cookie) |
6089dd0d | 440 | desc->eof = true; |
8cd51a0c | 441 | } |
0c030806 | 442 | out: |
d1bacf9e BS |
443 | return status; |
444 | } | |
445 | ||
6c981eff | 446 | static int nfs_readdir_search_array(struct nfs_readdir_descriptor *desc) |
d1bacf9e BS |
447 | { |
448 | struct nfs_cache_array *array; | |
47c716cb | 449 | int status; |
d1bacf9e | 450 | |
ed09222d | 451 | array = kmap_atomic(desc->page); |
d1bacf9e | 452 | |
2e7a4641 | 453 | if (desc->dir_cookie == 0) |
d1bacf9e BS |
454 | status = nfs_readdir_search_for_pos(array, desc); |
455 | else | |
456 | status = nfs_readdir_search_for_cookie(array, desc); | |
457 | ||
47c716cb | 458 | if (status == -EAGAIN) { |
0aded708 | 459 | desc->last_cookie = array->last_cookie; |
e47c085a | 460 | desc->current_index += array->size; |
47c716cb TM |
461 | desc->page_index++; |
462 | } | |
ed09222d | 463 | kunmap_atomic(array); |
d1bacf9e BS |
464 | return status; |
465 | } | |
466 | ||
467 | /* Fill a page with xdr information before transferring to the cache page */ | |
93b8959a TM |
468 | static int nfs_readdir_xdr_filler(struct nfs_readdir_descriptor *desc, |
469 | u64 cookie, struct page **pages, | |
470 | size_t bufsize) | |
1da177e4 | 471 | { |
82e22a5e TM |
472 | struct inode *inode = file_inode(desc->file); |
473 | __be32 verf_res[2]; | |
474 | struct nfs_readdir_arg arg = { | |
475 | .dentry = file_dentry(desc->file), | |
476 | .cred = desc->file->f_cred, | |
477 | .verf = NFS_I(inode)->cookieverf, | |
478 | .cookie = cookie, | |
479 | .pages = pages, | |
480 | .page_len = bufsize, | |
481 | .plus = desc->plus, | |
482 | }; | |
483 | struct nfs_readdir_res res = { | |
484 | .verf = verf_res, | |
485 | }; | |
4704f0e2 | 486 | unsigned long timestamp, gencount; |
1da177e4 LT |
487 | int error; |
488 | ||
1da177e4 LT |
489 | again: |
490 | timestamp = jiffies; | |
4704f0e2 | 491 | gencount = nfs_inc_attr_generation_counter(); |
a1147b82 | 492 | desc->dir_verifier = nfs_save_change_attribute(inode); |
82e22a5e | 493 | error = NFS_PROTO(inode)->readdir(&arg, &res); |
1da177e4 LT |
494 | if (error < 0) { |
495 | /* We requested READDIRPLUS, but the server doesn't grok it */ | |
496 | if (error == -ENOTSUPP && desc->plus) { | |
497 | NFS_SERVER(inode)->caps &= ~NFS_CAP_READDIRPLUS; | |
3a10c30a | 498 | clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); |
82e22a5e | 499 | desc->plus = arg.plus = false; |
1da177e4 LT |
500 | goto again; |
501 | } | |
502 | goto error; | |
503 | } | |
1f4eab7e | 504 | desc->timestamp = timestamp; |
4704f0e2 | 505 | desc->gencount = gencount; |
82e22a5e TM |
506 | memcpy(NFS_I(inode)->cookieverf, res.verf, |
507 | sizeof(NFS_I(inode)->cookieverf)); | |
d1bacf9e BS |
508 | error: |
509 | return error; | |
1da177e4 LT |
510 | } |
511 | ||
6c981eff | 512 | static int xdr_decode(struct nfs_readdir_descriptor *desc, |
573c4e1e | 513 | struct nfs_entry *entry, struct xdr_stream *xdr) |
1da177e4 | 514 | { |
59e356a9 | 515 | struct inode *inode = file_inode(desc->file); |
573c4e1e | 516 | int error; |
1da177e4 | 517 | |
59e356a9 | 518 | error = NFS_PROTO(inode)->decode_dirent(xdr, entry, desc->plus); |
573c4e1e CL |
519 | if (error) |
520 | return error; | |
d1bacf9e BS |
521 | entry->fattr->time_start = desc->timestamp; |
522 | entry->fattr->gencount = desc->gencount; | |
523 | return 0; | |
1da177e4 LT |
524 | } |
525 | ||
fa923369 TM |
526 | /* Match file and dirent using either filehandle or fileid |
527 | * Note: caller is responsible for checking the fsid | |
528 | */ | |
d39ab9de BS |
529 | static |
530 | int nfs_same_file(struct dentry *dentry, struct nfs_entry *entry) | |
531 | { | |
d8fdb47f | 532 | struct inode *inode; |
fa923369 TM |
533 | struct nfs_inode *nfsi; |
534 | ||
2b0143b5 DH |
535 | if (d_really_is_negative(dentry)) |
536 | return 0; | |
fa923369 | 537 | |
d8fdb47f TM |
538 | inode = d_inode(dentry); |
539 | if (is_bad_inode(inode) || NFS_STALE(inode)) | |
540 | return 0; | |
541 | ||
542 | nfsi = NFS_I(inode); | |
7dc72d5f TM |
543 | if (entry->fattr->fileid != nfsi->fileid) |
544 | return 0; | |
545 | if (entry->fh->size && nfs_compare_fh(entry->fh, &nfsi->fh) != 0) | |
546 | return 0; | |
547 | return 1; | |
d39ab9de BS |
548 | } |
549 | ||
d69ee9b8 | 550 | static |
23db8620 | 551 | bool nfs_use_readdirplus(struct inode *dir, struct dir_context *ctx) |
d69ee9b8 TM |
552 | { |
553 | if (!nfs_server_capable(dir, NFS_CAP_READDIRPLUS)) | |
554 | return false; | |
555 | if (test_and_clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(dir)->flags)) | |
556 | return true; | |
23db8620 | 557 | if (ctx->pos == 0) |
d69ee9b8 TM |
558 | return true; |
559 | return false; | |
560 | } | |
561 | ||
562 | /* | |
63519fbc TM |
563 | * This function is called by the lookup and getattr code to request the |
564 | * use of readdirplus to accelerate any future lookups in the same | |
d69ee9b8 TM |
565 | * directory. |
566 | */ | |
d69ee9b8 TM |
567 | void nfs_advise_use_readdirplus(struct inode *dir) |
568 | { | |
63519fbc TM |
569 | struct nfs_inode *nfsi = NFS_I(dir); |
570 | ||
571 | if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) && | |
572 | !list_empty(&nfsi->open_files)) | |
573 | set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags); | |
d69ee9b8 TM |
574 | } |
575 | ||
311324ad TM |
576 | /* |
577 | * This function is mainly for use by nfs_getattr(). | |
578 | * | |
579 | * If this is an 'ls -l', we want to force use of readdirplus. | |
580 | * Do this by checking if there is an active file descriptor | |
581 | * and calling nfs_advise_use_readdirplus, then forcing a | |
582 | * cache flush. | |
583 | */ | |
584 | void nfs_force_use_readdirplus(struct inode *dir) | |
585 | { | |
63519fbc TM |
586 | struct nfs_inode *nfsi = NFS_I(dir); |
587 | ||
588 | if (nfs_server_capable(dir, NFS_CAP_READDIRPLUS) && | |
589 | !list_empty(&nfsi->open_files)) { | |
590 | set_bit(NFS_INO_ADVISE_RDPLUS, &nfsi->flags); | |
227823d2 DN |
591 | invalidate_mapping_pages(dir->i_mapping, |
592 | nfsi->page_index + 1, -1); | |
311324ad TM |
593 | } |
594 | } | |
595 | ||
d39ab9de | 596 | static |
a1147b82 TM |
597 | void nfs_prime_dcache(struct dentry *parent, struct nfs_entry *entry, |
598 | unsigned long dir_verifier) | |
d39ab9de | 599 | { |
26fe5750 | 600 | struct qstr filename = QSTR_INIT(entry->name, entry->len); |
9ac3d3e8 | 601 | DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); |
4a201d6e TM |
602 | struct dentry *dentry; |
603 | struct dentry *alias; | |
d39ab9de | 604 | struct inode *inode; |
aa9c2669 | 605 | int status; |
d39ab9de | 606 | |
fa923369 TM |
607 | if (!(entry->fattr->valid & NFS_ATTR_FATTR_FILEID)) |
608 | return; | |
6c441c25 TM |
609 | if (!(entry->fattr->valid & NFS_ATTR_FATTR_FSID)) |
610 | return; | |
78d04af4 TM |
611 | if (filename.len == 0) |
612 | return; | |
613 | /* Validate that the name doesn't contain any illegal '\0' */ | |
614 | if (strnlen(filename.name, filename.len) != filename.len) | |
615 | return; | |
616 | /* ...or '/' */ | |
617 | if (strnchr(filename.name, filename.len, '/')) | |
618 | return; | |
4a201d6e TM |
619 | if (filename.name[0] == '.') { |
620 | if (filename.len == 1) | |
621 | return; | |
622 | if (filename.len == 2 && filename.name[1] == '.') | |
623 | return; | |
624 | } | |
8387ff25 | 625 | filename.hash = full_name_hash(parent, filename.name, filename.len); |
d39ab9de | 626 | |
4a201d6e | 627 | dentry = d_lookup(parent, &filename); |
9ac3d3e8 AV |
628 | again: |
629 | if (!dentry) { | |
630 | dentry = d_alloc_parallel(parent, &filename, &wq); | |
631 | if (IS_ERR(dentry)) | |
632 | return; | |
633 | } | |
634 | if (!d_in_lookup(dentry)) { | |
6c441c25 TM |
635 | /* Is there a mountpoint here? If so, just exit */ |
636 | if (!nfs_fsid_equal(&NFS_SB(dentry->d_sb)->fsid, | |
637 | &entry->fattr->fsid)) | |
638 | goto out; | |
d39ab9de | 639 | if (nfs_same_file(dentry, entry)) { |
7dc72d5f TM |
640 | if (!entry->fh->size) |
641 | goto out; | |
a1147b82 | 642 | nfs_set_verifier(dentry, dir_verifier); |
2b0143b5 | 643 | status = nfs_refresh_inode(d_inode(dentry), entry->fattr); |
aa9c2669 | 644 | if (!status) |
2b0143b5 | 645 | nfs_setsecurity(d_inode(dentry), entry->fattr, entry->label); |
d39ab9de BS |
646 | goto out; |
647 | } else { | |
5542aa2f | 648 | d_invalidate(dentry); |
d39ab9de | 649 | dput(dentry); |
9ac3d3e8 AV |
650 | dentry = NULL; |
651 | goto again; | |
d39ab9de BS |
652 | } |
653 | } | |
7dc72d5f TM |
654 | if (!entry->fh->size) { |
655 | d_lookup_done(dentry); | |
656 | goto out; | |
657 | } | |
d39ab9de | 658 | |
1775fd3e | 659 | inode = nfs_fhget(dentry->d_sb, entry->fh, entry->fattr, entry->label); |
41d28bca | 660 | alias = d_splice_alias(inode, dentry); |
9ac3d3e8 AV |
661 | d_lookup_done(dentry); |
662 | if (alias) { | |
663 | if (IS_ERR(alias)) | |
664 | goto out; | |
665 | dput(dentry); | |
666 | dentry = alias; | |
667 | } | |
a1147b82 | 668 | nfs_set_verifier(dentry, dir_verifier); |
d39ab9de BS |
669 | out: |
670 | dput(dentry); | |
d39ab9de BS |
671 | } |
672 | ||
d1bacf9e | 673 | /* Perform conversion from xdr to cache array */ |
3b2a09f1 TM |
674 | static int nfs_readdir_page_filler(struct nfs_readdir_descriptor *desc, |
675 | struct nfs_entry *entry, | |
676 | struct page **xdr_pages, | |
677 | struct page *fillme, unsigned int buflen) | |
1da177e4 | 678 | { |
3b2a09f1 | 679 | struct address_space *mapping = desc->file->f_mapping; |
babddc72 | 680 | struct xdr_stream stream; |
f7da7a12 | 681 | struct xdr_buf buf; |
3b2a09f1 | 682 | struct page *scratch, *new, *page = fillme; |
5c346854 | 683 | int status; |
babddc72 | 684 | |
6650239a TM |
685 | scratch = alloc_page(GFP_KERNEL); |
686 | if (scratch == NULL) | |
687 | return -ENOMEM; | |
babddc72 | 688 | |
f7da7a12 | 689 | xdr_init_decode_pages(&stream, &buf, xdr_pages, buflen); |
6650239a | 690 | xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE); |
99424380 BS |
691 | |
692 | do { | |
d33030e2 JM |
693 | if (entry->label) |
694 | entry->label->len = NFS4_MAXLABELLEN; | |
695 | ||
99424380 | 696 | status = xdr_decode(desc, entry, &stream); |
972bcdf2 | 697 | if (status != 0) |
99424380 | 698 | break; |
5c346854 | 699 | |
a7a3b1e9 | 700 | if (desc->plus) |
a1147b82 TM |
701 | nfs_prime_dcache(file_dentry(desc->file), entry, |
702 | desc->dir_verifier); | |
8cd51a0c | 703 | |
db531db9 | 704 | status = nfs_readdir_add_to_array(entry, page); |
3b2a09f1 TM |
705 | if (status != -ENOSPC) |
706 | continue; | |
707 | ||
708 | if (page->mapping != mapping) | |
709 | break; | |
710 | new = nfs_readdir_page_get_next(mapping, page->index + 1, | |
711 | entry->prev_cookie); | |
712 | if (!new) | |
713 | break; | |
714 | if (page != fillme) | |
715 | nfs_readdir_page_unlock_and_put(page); | |
716 | page = new; | |
717 | status = nfs_readdir_add_to_array(entry, page); | |
972bcdf2 | 718 | } while (!status && !entry->eof); |
99424380 | 719 | |
972bcdf2 TM |
720 | switch (status) { |
721 | case -EBADCOOKIE: | |
722 | if (entry->eof) { | |
723 | nfs_readdir_page_set_eof(page); | |
724 | status = 0; | |
725 | } | |
726 | break; | |
727 | case -ENOSPC: | |
728 | case -EAGAIN: | |
0795bf83 | 729 | status = 0; |
972bcdf2 | 730 | break; |
1da177e4 | 731 | } |
6650239a | 732 | |
3b2a09f1 TM |
733 | if (page != fillme) |
734 | nfs_readdir_page_unlock_and_put(page); | |
735 | ||
6650239a | 736 | put_page(scratch); |
8cd51a0c | 737 | return status; |
56e4ebf8 BS |
738 | } |
739 | ||
1a34c8c9 | 740 | static void nfs_readdir_free_pages(struct page **pages, size_t npages) |
56e4ebf8 | 741 | { |
1a34c8c9 TM |
742 | while (npages--) |
743 | put_page(pages[npages]); | |
744 | kfree(pages); | |
56e4ebf8 BS |
745 | } |
746 | ||
56e4ebf8 | 747 | /* |
bf211ca1 | 748 | * nfs_readdir_alloc_pages() will allocate pages that must be freed with a call |
749 | * to nfs_readdir_free_pages() | |
56e4ebf8 | 750 | */ |
1a34c8c9 | 751 | static struct page **nfs_readdir_alloc_pages(size_t npages) |
56e4ebf8 | 752 | { |
1a34c8c9 TM |
753 | struct page **pages; |
754 | size_t i; | |
56e4ebf8 | 755 | |
1a34c8c9 TM |
756 | pages = kmalloc_array(npages, sizeof(*pages), GFP_KERNEL); |
757 | if (!pages) | |
758 | return NULL; | |
56e4ebf8 BS |
759 | for (i = 0; i < npages; i++) { |
760 | struct page *page = alloc_page(GFP_KERNEL); | |
761 | if (page == NULL) | |
762 | goto out_freepages; | |
763 | pages[i] = page; | |
764 | } | |
1a34c8c9 | 765 | return pages; |
56e4ebf8 | 766 | |
56e4ebf8 | 767 | out_freepages: |
c7e9668e | 768 | nfs_readdir_free_pages(pages, i); |
1a34c8c9 | 769 | return NULL; |
1da177e4 LT |
770 | } |
771 | ||
6c981eff TM |
772 | static int nfs_readdir_xdr_to_array(struct nfs_readdir_descriptor *desc, |
773 | struct page *page, struct inode *inode) | |
00a92642 | 774 | { |
1a34c8c9 | 775 | struct page **pages; |
6b75cf9e | 776 | struct nfs_entry *entry; |
1a34c8c9 TM |
777 | size_t array_size; |
778 | size_t dtsize = NFS_SERVER(inode)->dtsize; | |
8cd51a0c | 779 | int status = -ENOMEM; |
d1bacf9e | 780 | |
6b75cf9e TM |
781 | entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
782 | if (!entry) | |
783 | return -ENOMEM; | |
784 | entry->cookie = nfs_readdir_page_last_cookie(page); | |
785 | entry->fh = nfs_alloc_fhandle(); | |
786 | entry->fattr = nfs_alloc_fattr(); | |
787 | entry->server = NFS_SERVER(inode); | |
788 | if (entry->fh == NULL || entry->fattr == NULL) | |
d1bacf9e | 789 | goto out; |
00a92642 | 790 | |
6b75cf9e TM |
791 | entry->label = nfs4_label_alloc(NFS_SERVER(inode), GFP_NOWAIT); |
792 | if (IS_ERR(entry->label)) { | |
793 | status = PTR_ERR(entry->label); | |
14c43f76 DQ |
794 | goto out; |
795 | } | |
796 | ||
1a34c8c9 TM |
797 | array_size = (dtsize + PAGE_SIZE - 1) >> PAGE_SHIFT; |
798 | pages = nfs_readdir_alloc_pages(array_size); | |
799 | if (!pages) | |
e762a639 | 800 | goto out_release_label; |
1a34c8c9 | 801 | |
d1bacf9e | 802 | do { |
ac396128 | 803 | unsigned int pglen; |
6b75cf9e | 804 | status = nfs_readdir_xdr_filler(desc, entry->cookie, |
93b8959a | 805 | pages, dtsize); |
d1bacf9e | 806 | if (status < 0) |
00a92642 | 807 | break; |
972bcdf2 | 808 | |
ac396128 | 809 | pglen = status; |
972bcdf2 TM |
810 | if (pglen == 0) { |
811 | nfs_readdir_page_set_eof(page); | |
8cd51a0c TM |
812 | break; |
813 | } | |
972bcdf2 | 814 | |
6b75cf9e | 815 | status = nfs_readdir_page_filler(desc, entry, pages, page, pglen); |
e762a639 | 816 | } while (!status && nfs_readdir_page_needs_filling(page)); |
d1bacf9e | 817 | |
c7e9668e | 818 | nfs_readdir_free_pages(pages, array_size); |
e762a639 | 819 | out_release_label: |
6b75cf9e | 820 | nfs4_label_free(entry->label); |
d1bacf9e | 821 | out: |
6b75cf9e TM |
822 | nfs_free_fattr(entry->fattr); |
823 | nfs_free_fhandle(entry->fh); | |
824 | kfree(entry); | |
00a92642 OG |
825 | return status; |
826 | } | |
827 | ||
1f1d4aa4 | 828 | static void nfs_readdir_page_put(struct nfs_readdir_descriptor *desc) |
1da177e4 | 829 | { |
1f1d4aa4 TM |
830 | put_page(desc->page); |
831 | desc->page = NULL; | |
d1bacf9e | 832 | } |
1da177e4 | 833 | |
1f1d4aa4 TM |
834 | static void |
835 | nfs_readdir_page_unlock_and_put_cached(struct nfs_readdir_descriptor *desc) | |
d1bacf9e | 836 | { |
1f1d4aa4 TM |
837 | unlock_page(desc->page); |
838 | nfs_readdir_page_put(desc); | |
d1bacf9e BS |
839 | } |
840 | ||
1f1d4aa4 TM |
841 | static struct page * |
842 | nfs_readdir_page_get_cached(struct nfs_readdir_descriptor *desc) | |
d1bacf9e | 843 | { |
1f1d4aa4 TM |
844 | return nfs_readdir_page_get_locked(desc->file->f_mapping, |
845 | desc->page_index, | |
846 | desc->last_cookie); | |
1da177e4 LT |
847 | } |
848 | ||
849 | /* | |
d1bacf9e | 850 | * Returns 0 if desc->dir_cookie was found on page desc->page_index |
114de382 | 851 | * and locks the page to prevent removal from the page cache. |
1da177e4 | 852 | */ |
6c981eff | 853 | static int find_and_lock_cache_page(struct nfs_readdir_descriptor *desc) |
d1bacf9e | 854 | { |
227823d2 DN |
855 | struct inode *inode = file_inode(desc->file); |
856 | struct nfs_inode *nfsi = NFS_I(inode); | |
d1bacf9e BS |
857 | int res; |
858 | ||
1f1d4aa4 TM |
859 | desc->page = nfs_readdir_page_get_cached(desc); |
860 | if (!desc->page) | |
861 | return -ENOMEM; | |
862 | if (nfs_readdir_page_needs_filling(desc->page)) { | |
863 | res = nfs_readdir_xdr_to_array(desc, desc->page, inode); | |
9fff59ed TM |
864 | if (res < 0) { |
865 | nfs_readdir_page_unlock_and_put_cached(desc); | |
866 | if (res == -EBADCOOKIE || res == -ENOTSYNC) { | |
867 | invalidate_inode_pages2(desc->file->f_mapping); | |
868 | desc->page_index = 0; | |
869 | return -EAGAIN; | |
870 | } | |
871 | return res; | |
872 | } | |
1f1d4aa4 TM |
873 | } |
874 | res = nfs_readdir_search_array(desc); | |
875 | if (res == 0) { | |
876 | nfsi->page_index = desc->page_index; | |
877 | return 0; | |
114de382 | 878 | } |
1f1d4aa4 | 879 | nfs_readdir_page_unlock_and_put_cached(desc); |
d1bacf9e BS |
880 | return res; |
881 | } | |
882 | ||
883 | /* Search for desc->dir_cookie from the beginning of the page cache */ | |
6c981eff | 884 | static int readdir_search_pagecache(struct nfs_readdir_descriptor *desc) |
1da177e4 | 885 | { |
8cd51a0c | 886 | int res; |
d1bacf9e | 887 | |
47c716cb | 888 | do { |
9fff59ed TM |
889 | if (desc->page_index == 0) { |
890 | desc->current_index = 0; | |
891 | desc->prev_index = 0; | |
892 | desc->last_cookie = 0; | |
893 | } | |
114de382 | 894 | res = find_and_lock_cache_page(desc); |
47c716cb | 895 | } while (res == -EAGAIN); |
1da177e4 LT |
896 | return res; |
897 | } | |
898 | ||
1da177e4 LT |
899 | /* |
900 | * Once we've found the start of the dirent within a page: fill 'er up... | |
901 | */ | |
dbeaf8c9 | 902 | static void nfs_do_filldir(struct nfs_readdir_descriptor *desc) |
1da177e4 LT |
903 | { |
904 | struct file *file = desc->file; | |
dbeaf8c9 TM |
905 | struct nfs_cache_array *array; |
906 | unsigned int i = 0; | |
8ef2ce3e | 907 | |
0795bf83 | 908 | array = kmap(desc->page); |
d1bacf9e | 909 | for (i = desc->cache_entry_index; i < array->size; i++) { |
ece0b423 | 910 | struct nfs_cache_array_entry *ent; |
1da177e4 | 911 | |
ece0b423 | 912 | ent = &array->array[i]; |
a52a8a6a | 913 | if (!dir_emit(desc->ctx, ent->name, ent->name_len, |
23db8620 | 914 | nfs_compat_user_ino64(ent->ino), ent->d_type)) { |
6089dd0d | 915 | desc->eof = true; |
1da177e4 | 916 | break; |
ece0b423 | 917 | } |
d1bacf9e | 918 | if (i < (array->size-1)) |
2e7a4641 | 919 | desc->dir_cookie = array->array[i+1].cookie; |
d1bacf9e | 920 | else |
2e7a4641 | 921 | desc->dir_cookie = array->last_cookie; |
59e356a9 | 922 | if (nfs_readdir_use_cookie(file)) |
2e7a4641 | 923 | desc->ctx->pos = desc->dir_cookie; |
59e356a9 TM |
924 | else |
925 | desc->ctx->pos++; | |
2e7a4641 TM |
926 | if (desc->duped != 0) |
927 | desc->duped = 1; | |
1da177e4 | 928 | } |
b1e21c97 | 929 | if (array->page_is_eof) |
6089dd0d | 930 | desc->eof = true; |
d1bacf9e | 931 | |
0795bf83 | 932 | kunmap(desc->page); |
dbeaf8c9 TM |
933 | dfprintk(DIRCACHE, "NFS: nfs_do_filldir() filling ended @ cookie %llu\n", |
934 | (unsigned long long)desc->dir_cookie); | |
1da177e4 LT |
935 | } |
936 | ||
937 | /* | |
938 | * If we cannot find a cookie in our cache, we suspect that this is | |
939 | * because it points to a deleted file, so we ask the server to return | |
940 | * whatever it thinks is the next entry. We then feed this to filldir. | |
941 | * If all goes well, we should then be able to find our way round the | |
942 | * cache on the next call to readdir_search_pagecache(); | |
943 | * | |
944 | * NOTE: we cannot add the anonymous page to the pagecache because | |
945 | * the data it contains might not be page aligned. Besides, | |
946 | * we should already have a complete representation of the | |
947 | * directory in the page cache by the time we get here. | |
948 | */ | |
6c981eff | 949 | static int uncached_readdir(struct nfs_readdir_descriptor *desc) |
1da177e4 | 950 | { |
1da177e4 LT |
951 | struct page *page = NULL; |
952 | int status; | |
496ad9aa | 953 | struct inode *inode = file_inode(desc->file); |
1da177e4 | 954 | |
1e7cb3dc | 955 | dfprintk(DIRCACHE, "NFS: uncached_readdir() searching for cookie %Lu\n", |
2e7a4641 | 956 | (unsigned long long)desc->dir_cookie); |
1da177e4 LT |
957 | |
958 | page = alloc_page(GFP_HIGHUSER); | |
959 | if (!page) { | |
960 | status = -ENOMEM; | |
961 | goto out; | |
962 | } | |
d1bacf9e | 963 | |
7a8e1dc3 | 964 | desc->page_index = 0; |
2e7a4641 | 965 | desc->last_cookie = desc->dir_cookie; |
7a8e1dc3 | 966 | desc->page = page; |
2e7a4641 | 967 | desc->duped = 0; |
7a8e1dc3 | 968 | |
1f1d4aa4 | 969 | nfs_readdir_page_init_array(page, desc->dir_cookie); |
85f8607e TM |
970 | status = nfs_readdir_xdr_to_array(desc, page, inode); |
971 | if (status < 0) | |
1da177e4 LT |
972 | goto out_release; |
973 | ||
dbeaf8c9 | 974 | nfs_do_filldir(desc); |
1da177e4 | 975 | |
114de382 TM |
976 | out_release: |
977 | nfs_readdir_clear_array(desc->page); | |
1f1d4aa4 | 978 | nfs_readdir_page_put(desc); |
1da177e4 | 979 | out: |
1e7cb3dc | 980 | dfprintk(DIRCACHE, "NFS: %s: returns %d\n", |
3110ff80 | 981 | __func__, status); |
1da177e4 | 982 | return status; |
1da177e4 LT |
983 | } |
984 | ||
00a92642 OG |
985 | /* The file offset position represents the dirent entry number. A |
986 | last cookie cache takes care of the common case of reading the | |
987 | whole directory. | |
1da177e4 | 988 | */ |
23db8620 | 989 | static int nfs_readdir(struct file *file, struct dir_context *ctx) |
1da177e4 | 990 | { |
be62a1a8 | 991 | struct dentry *dentry = file_dentry(file); |
2b0143b5 | 992 | struct inode *inode = d_inode(dentry); |
23db8620 | 993 | struct nfs_open_dir_context *dir_ctx = file->private_data; |
6b75cf9e TM |
994 | struct nfs_readdir_descriptor *desc; |
995 | int res; | |
1da177e4 | 996 | |
6de1472f AV |
997 | dfprintk(FILE, "NFS: readdir(%pD2) starting at cookie %llu\n", |
998 | file, (long long)ctx->pos); | |
91d5b470 CL |
999 | nfs_inc_stats(inode, NFSIOS_VFSGETDENTS); |
1000 | ||
1da177e4 | 1001 | /* |
23db8620 | 1002 | * ctx->pos points to the dirent entry number. |
f0dd2136 | 1003 | * *desc->dir_cookie has the cookie for the next entry. We have |
00a92642 OG |
1004 | * to either find the entry with the appropriate number or |
1005 | * revalidate the cookie. | |
1da177e4 | 1006 | */ |
6b75cf9e | 1007 | if (ctx->pos == 0 || nfs_attribute_cache_expired(inode)) { |
07b5ce8e | 1008 | res = nfs_revalidate_mapping(inode, file->f_mapping); |
6b75cf9e TM |
1009 | if (res < 0) |
1010 | goto out; | |
1011 | } | |
1012 | ||
1013 | res = -ENOMEM; | |
1014 | desc = kzalloc(sizeof(*desc), GFP_KERNEL); | |
1015 | if (!desc) | |
fccca7fc | 1016 | goto out; |
6b75cf9e TM |
1017 | desc->file = file; |
1018 | desc->ctx = ctx; | |
1019 | desc->plus = nfs_use_readdirplus(inode, ctx); | |
fccca7fc | 1020 | |
2e7a4641 TM |
1021 | spin_lock(&file->f_lock); |
1022 | desc->dir_cookie = dir_ctx->dir_cookie; | |
1023 | desc->dup_cookie = dir_ctx->dup_cookie; | |
1024 | desc->duped = dir_ctx->duped; | |
1025 | desc->attr_gencount = dir_ctx->attr_gencount; | |
1026 | spin_unlock(&file->f_lock); | |
1027 | ||
47c716cb | 1028 | do { |
1da177e4 | 1029 | res = readdir_search_pagecache(desc); |
00a92642 | 1030 | |
1da177e4 | 1031 | if (res == -EBADCOOKIE) { |
ece0b423 | 1032 | res = 0; |
1da177e4 | 1033 | /* This means either end of directory */ |
2e7a4641 | 1034 | if (desc->dir_cookie && !desc->eof) { |
1da177e4 | 1035 | /* Or that the server has 'lost' a cookie */ |
23db8620 | 1036 | res = uncached_readdir(desc); |
ece0b423 | 1037 | if (res == 0) |
1da177e4 | 1038 | continue; |
9fff59ed TM |
1039 | if (res == -EBADCOOKIE || res == -ENOTSYNC) |
1040 | res = 0; | |
1da177e4 | 1041 | } |
1da177e4 LT |
1042 | break; |
1043 | } | |
1044 | if (res == -ETOOSMALL && desc->plus) { | |
3a10c30a | 1045 | clear_bit(NFS_INO_ADVISE_RDPLUS, &NFS_I(inode)->flags); |
1da177e4 | 1046 | nfs_zap_caches(inode); |
baf57a09 | 1047 | desc->page_index = 0; |
a7a3b1e9 BC |
1048 | desc->plus = false; |
1049 | desc->eof = false; | |
1da177e4 LT |
1050 | continue; |
1051 | } | |
1052 | if (res < 0) | |
1053 | break; | |
1054 | ||
dbeaf8c9 | 1055 | nfs_do_filldir(desc); |
1f1d4aa4 | 1056 | nfs_readdir_page_unlock_and_put_cached(desc); |
47c716cb | 1057 | } while (!desc->eof); |
2e7a4641 TM |
1058 | |
1059 | spin_lock(&file->f_lock); | |
1060 | dir_ctx->dir_cookie = desc->dir_cookie; | |
1061 | dir_ctx->dup_cookie = desc->dup_cookie; | |
1062 | dir_ctx->duped = desc->duped; | |
1063 | dir_ctx->attr_gencount = desc->attr_gencount; | |
1064 | spin_unlock(&file->f_lock); | |
1065 | ||
6b75cf9e TM |
1066 | kfree(desc); |
1067 | ||
fccca7fc | 1068 | out: |
6de1472f | 1069 | dfprintk(FILE, "NFS: readdir(%pD2) returns %d\n", file, res); |
1e7cb3dc | 1070 | return res; |
1da177e4 LT |
1071 | } |
1072 | ||
965c8e59 | 1073 | static loff_t nfs_llseek_dir(struct file *filp, loff_t offset, int whence) |
f0dd2136 | 1074 | { |
480c2006 | 1075 | struct nfs_open_dir_context *dir_ctx = filp->private_data; |
b84e06c5 | 1076 | |
6de1472f AV |
1077 | dfprintk(FILE, "NFS: llseek dir(%pD2, %lld, %d)\n", |
1078 | filp, offset, whence); | |
b84e06c5 | 1079 | |
965c8e59 | 1080 | switch (whence) { |
b2b1ff3d TM |
1081 | default: |
1082 | return -EINVAL; | |
1083 | case SEEK_SET: | |
1084 | if (offset < 0) | |
1085 | return -EINVAL; | |
83f2c45e | 1086 | spin_lock(&filp->f_lock); |
b2b1ff3d TM |
1087 | break; |
1088 | case SEEK_CUR: | |
1089 | if (offset == 0) | |
1090 | return filp->f_pos; | |
83f2c45e | 1091 | spin_lock(&filp->f_lock); |
b2b1ff3d TM |
1092 | offset += filp->f_pos; |
1093 | if (offset < 0) { | |
83f2c45e | 1094 | spin_unlock(&filp->f_lock); |
b2b1ff3d TM |
1095 | return -EINVAL; |
1096 | } | |
f0dd2136 TM |
1097 | } |
1098 | if (offset != filp->f_pos) { | |
1099 | filp->f_pos = offset; | |
59e356a9 TM |
1100 | if (nfs_readdir_use_cookie(filp)) |
1101 | dir_ctx->dir_cookie = offset; | |
1102 | else | |
1103 | dir_ctx->dir_cookie = 0; | |
8ef2ce3e | 1104 | dir_ctx->duped = 0; |
f0dd2136 | 1105 | } |
83f2c45e | 1106 | spin_unlock(&filp->f_lock); |
f0dd2136 TM |
1107 | return offset; |
1108 | } | |
1109 | ||
1da177e4 LT |
1110 | /* |
1111 | * All directory operations under NFS are synchronous, so fsync() | |
1112 | * is a dummy operation. | |
1113 | */ | |
02c24a82 JB |
1114 | static int nfs_fsync_dir(struct file *filp, loff_t start, loff_t end, |
1115 | int datasync) | |
1da177e4 | 1116 | { |
6de1472f | 1117 | dfprintk(FILE, "NFS: fsync dir(%pD2) datasync %d\n", filp, datasync); |
1e7cb3dc | 1118 | |
11decaf8 | 1119 | nfs_inc_stats(file_inode(filp), NFSIOS_VFSFSYNC); |
1da177e4 LT |
1120 | return 0; |
1121 | } | |
1122 | ||
bfc69a45 TM |
1123 | /** |
1124 | * nfs_force_lookup_revalidate - Mark the directory as having changed | |
302fad7b | 1125 | * @dir: pointer to directory inode |
bfc69a45 TM |
1126 | * |
1127 | * This forces the revalidation code in nfs_lookup_revalidate() to do a | |
1128 | * full lookup on all child dentries of 'dir' whenever a change occurs | |
1129 | * on the server that might have invalidated our dcache. | |
1130 | * | |
efeda80d TM |
1131 | * Note that we reserve bit '0' as a tag to let us know when a dentry |
1132 | * was revalidated while holding a delegation on its inode. | |
1133 | * | |
bfc69a45 TM |
1134 | * The caller should be holding dir->i_lock |
1135 | */ | |
1136 | void nfs_force_lookup_revalidate(struct inode *dir) | |
1137 | { | |
efeda80d | 1138 | NFS_I(dir)->cache_change_attribute += 2; |
bfc69a45 | 1139 | } |
89d77c8f | 1140 | EXPORT_SYMBOL_GPL(nfs_force_lookup_revalidate); |
bfc69a45 | 1141 | |
efeda80d TM |
1142 | /** |
1143 | * nfs_verify_change_attribute - Detects NFS remote directory changes | |
1144 | * @dir: pointer to parent directory inode | |
1145 | * @verf: previously saved change attribute | |
1146 | * | |
1147 | * Return "false" if the verifiers doesn't match the change attribute. | |
1148 | * This would usually indicate that the directory contents have changed on | |
1149 | * the server, and that any dentries need revalidating. | |
1150 | */ | |
1151 | static bool nfs_verify_change_attribute(struct inode *dir, unsigned long verf) | |
1152 | { | |
1153 | return (verf & ~1UL) == nfs_save_change_attribute(dir); | |
1154 | } | |
1155 | ||
1156 | static void nfs_set_verifier_delegated(unsigned long *verf) | |
1157 | { | |
1158 | *verf |= 1UL; | |
1159 | } | |
1160 | ||
1161 | #if IS_ENABLED(CONFIG_NFS_V4) | |
1162 | static void nfs_unset_verifier_delegated(unsigned long *verf) | |
1163 | { | |
1164 | *verf &= ~1UL; | |
1165 | } | |
1166 | #endif /* IS_ENABLED(CONFIG_NFS_V4) */ | |
1167 | ||
1168 | static bool nfs_test_verifier_delegated(unsigned long verf) | |
1169 | { | |
1170 | return verf & 1; | |
1171 | } | |
1172 | ||
1173 | static bool nfs_verifier_is_delegated(struct dentry *dentry) | |
1174 | { | |
1175 | return nfs_test_verifier_delegated(dentry->d_time); | |
1176 | } | |
1177 | ||
1178 | static void nfs_set_verifier_locked(struct dentry *dentry, unsigned long verf) | |
1179 | { | |
1180 | struct inode *inode = d_inode(dentry); | |
1181 | ||
1182 | if (!nfs_verifier_is_delegated(dentry) && | |
1183 | !nfs_verify_change_attribute(d_inode(dentry->d_parent), verf)) | |
1184 | goto out; | |
1185 | if (inode && NFS_PROTO(inode)->have_delegation(inode, FMODE_READ)) | |
1186 | nfs_set_verifier_delegated(&verf); | |
1187 | out: | |
1188 | dentry->d_time = verf; | |
1189 | } | |
1190 | ||
1191 | /** | |
1192 | * nfs_set_verifier - save a parent directory verifier in the dentry | |
1193 | * @dentry: pointer to dentry | |
1194 | * @verf: verifier to save | |
1195 | * | |
1196 | * Saves the parent directory verifier in @dentry. If the inode has | |
1197 | * a delegation, we also tag the dentry as having been revalidated | |
1198 | * while holding a delegation so that we know we don't have to | |
1199 | * look it up again after a directory change. | |
1200 | */ | |
1201 | void nfs_set_verifier(struct dentry *dentry, unsigned long verf) | |
1202 | { | |
1203 | ||
1204 | spin_lock(&dentry->d_lock); | |
1205 | nfs_set_verifier_locked(dentry, verf); | |
1206 | spin_unlock(&dentry->d_lock); | |
1207 | } | |
1208 | EXPORT_SYMBOL_GPL(nfs_set_verifier); | |
1209 | ||
1210 | #if IS_ENABLED(CONFIG_NFS_V4) | |
1211 | /** | |
1212 | * nfs_clear_verifier_delegated - clear the dir verifier delegation tag | |
1213 | * @inode: pointer to inode | |
1214 | * | |
1215 | * Iterates through the dentries in the inode alias list and clears | |
1216 | * the tag used to indicate that the dentry has been revalidated | |
1217 | * while holding a delegation. | |
1218 | * This function is intended for use when the delegation is being | |
1219 | * returned or revoked. | |
1220 | */ | |
1221 | void nfs_clear_verifier_delegated(struct inode *inode) | |
1222 | { | |
1223 | struct dentry *alias; | |
1224 | ||
1225 | if (!inode) | |
1226 | return; | |
1227 | spin_lock(&inode->i_lock); | |
1228 | hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) { | |
1229 | spin_lock(&alias->d_lock); | |
1230 | nfs_unset_verifier_delegated(&alias->d_time); | |
1231 | spin_unlock(&alias->d_lock); | |
1232 | } | |
1233 | spin_unlock(&inode->i_lock); | |
1234 | } | |
1235 | EXPORT_SYMBOL_GPL(nfs_clear_verifier_delegated); | |
1236 | #endif /* IS_ENABLED(CONFIG_NFS_V4) */ | |
1237 | ||
1da177e4 LT |
1238 | /* |
1239 | * A check for whether or not the parent directory has changed. | |
1240 | * In the case it has, we assume that the dentries are untrustworthy | |
1241 | * and may need to be looked up again. | |
912a108d | 1242 | * If rcu_walk prevents us from performing a full check, return 0. |
1da177e4 | 1243 | */ |
912a108d N |
1244 | static int nfs_check_verifier(struct inode *dir, struct dentry *dentry, |
1245 | int rcu_walk) | |
1da177e4 LT |
1246 | { |
1247 | if (IS_ROOT(dentry)) | |
1248 | return 1; | |
4eec952e TM |
1249 | if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONE) |
1250 | return 0; | |
f2c77f4e TM |
1251 | if (!nfs_verify_change_attribute(dir, dentry->d_time)) |
1252 | return 0; | |
1253 | /* Revalidate nfsi->cache_change_attribute before we declare a match */ | |
1cd9cb05 TM |
1254 | if (nfs_mapping_need_revalidate_inode(dir)) { |
1255 | if (rcu_walk) | |
1256 | return 0; | |
1257 | if (__nfs_revalidate_inode(NFS_SERVER(dir), dir) < 0) | |
1258 | return 0; | |
1259 | } | |
f2c77f4e TM |
1260 | if (!nfs_verify_change_attribute(dir, dentry->d_time)) |
1261 | return 0; | |
1262 | return 1; | |
1da177e4 LT |
1263 | } |
1264 | ||
a12802ca TM |
1265 | /* |
1266 | * Use intent information to check whether or not we're going to do | |
1267 | * an O_EXCL create using this path component. | |
1268 | */ | |
fa3c56bb | 1269 | static int nfs_is_exclusive_create(struct inode *dir, unsigned int flags) |
a12802ca TM |
1270 | { |
1271 | if (NFS_PROTO(dir)->version == 2) | |
1272 | return 0; | |
fa3c56bb | 1273 | return flags & LOOKUP_EXCL; |
a12802ca TM |
1274 | } |
1275 | ||
1d6757fb TM |
1276 | /* |
1277 | * Inode and filehandle revalidation for lookups. | |
1278 | * | |
1279 | * We force revalidation in the cases where the VFS sets LOOKUP_REVAL, | |
1280 | * or if the intent information indicates that we're about to open this | |
1281 | * particular file and the "nocto" mount flag is not set. | |
1282 | * | |
1283 | */ | |
65a0c149 | 1284 | static |
fa3c56bb | 1285 | int nfs_lookup_verify_inode(struct inode *inode, unsigned int flags) |
1da177e4 LT |
1286 | { |
1287 | struct nfs_server *server = NFS_SERVER(inode); | |
65a0c149 | 1288 | int ret; |
1da177e4 | 1289 | |
36d43a43 | 1290 | if (IS_AUTOMOUNT(inode)) |
4e99a1ff | 1291 | return 0; |
47921921 TM |
1292 | |
1293 | if (flags & LOOKUP_OPEN) { | |
1294 | switch (inode->i_mode & S_IFMT) { | |
1295 | case S_IFREG: | |
1296 | /* A NFSv4 OPEN will revalidate later */ | |
1297 | if (server->caps & NFS_CAP_ATOMIC_OPEN) | |
1298 | goto out; | |
df561f66 | 1299 | fallthrough; |
47921921 TM |
1300 | case S_IFDIR: |
1301 | if (server->flags & NFS_MOUNT_NOCTO) | |
1302 | break; | |
1303 | /* NFS close-to-open cache consistency validation */ | |
1304 | goto out_force; | |
1305 | } | |
1306 | } | |
1307 | ||
facc3530 | 1308 | /* VFS wants an on-the-wire revalidation */ |
fa3c56bb | 1309 | if (flags & LOOKUP_REVAL) |
facc3530 | 1310 | goto out_force; |
65a0c149 | 1311 | out: |
a61246c9 | 1312 | return (inode->i_nlink == 0) ? -ESTALE : 0; |
1da177e4 | 1313 | out_force: |
1fa1e384 N |
1314 | if (flags & LOOKUP_RCU) |
1315 | return -ECHILD; | |
65a0c149 TM |
1316 | ret = __nfs_revalidate_inode(server, inode); |
1317 | if (ret != 0) | |
1318 | return ret; | |
1319 | goto out; | |
1da177e4 LT |
1320 | } |
1321 | ||
1322 | /* | |
1323 | * We judge how long we want to trust negative | |
1324 | * dentries by looking at the parent inode mtime. | |
1325 | * | |
1326 | * If parent mtime has changed, we revalidate, else we wait for a | |
1327 | * period corresponding to the parent's attribute cache timeout value. | |
912a108d N |
1328 | * |
1329 | * If LOOKUP_RCU prevents us from performing a full check, return 1 | |
1330 | * suggesting a reval is needed. | |
9f6d44d4 TM |
1331 | * |
1332 | * Note that when creating a new file, or looking up a rename target, | |
1333 | * then it shouldn't be necessary to revalidate a negative dentry. | |
1da177e4 LT |
1334 | */ |
1335 | static inline | |
1336 | int nfs_neg_need_reval(struct inode *dir, struct dentry *dentry, | |
fa3c56bb | 1337 | unsigned int flags) |
1da177e4 | 1338 | { |
9f6d44d4 | 1339 | if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET)) |
1da177e4 | 1340 | return 0; |
4eec952e TM |
1341 | if (NFS_SERVER(dir)->flags & NFS_MOUNT_LOOKUP_CACHE_NONEG) |
1342 | return 1; | |
912a108d | 1343 | return !nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU); |
1da177e4 LT |
1344 | } |
1345 | ||
5ceb9d7f TM |
1346 | static int |
1347 | nfs_lookup_revalidate_done(struct inode *dir, struct dentry *dentry, | |
1348 | struct inode *inode, int error) | |
1349 | { | |
1350 | switch (error) { | |
1351 | case 1: | |
1352 | dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is valid\n", | |
1353 | __func__, dentry); | |
1354 | return 1; | |
1355 | case 0: | |
1356 | nfs_mark_for_revalidate(dir); | |
1357 | if (inode && S_ISDIR(inode->i_mode)) { | |
1358 | /* Purge readdir caches. */ | |
1359 | nfs_zap_caches(inode); | |
1360 | /* | |
1361 | * We can't d_drop the root of a disconnected tree: | |
1362 | * its d_hash is on the s_anon list and d_drop() would hide | |
1363 | * it from shrink_dcache_for_unmount(), leading to busy | |
1364 | * inodes on unmount and further oopses. | |
1365 | */ | |
1366 | if (IS_ROOT(dentry)) | |
1367 | return 1; | |
1368 | } | |
1369 | dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) is invalid\n", | |
1370 | __func__, dentry); | |
1371 | return 0; | |
1372 | } | |
1373 | dfprintk(LOOKUPCACHE, "NFS: %s(%pd2) lookup returned error %d\n", | |
1374 | __func__, dentry, error); | |
1375 | return error; | |
1376 | } | |
1377 | ||
1378 | static int | |
1379 | nfs_lookup_revalidate_negative(struct inode *dir, struct dentry *dentry, | |
1380 | unsigned int flags) | |
1381 | { | |
1382 | int ret = 1; | |
1383 | if (nfs_neg_need_reval(dir, dentry, flags)) { | |
1384 | if (flags & LOOKUP_RCU) | |
1385 | return -ECHILD; | |
1386 | ret = 0; | |
1387 | } | |
1388 | return nfs_lookup_revalidate_done(dir, dentry, NULL, ret); | |
1389 | } | |
1390 | ||
1391 | static int | |
1392 | nfs_lookup_revalidate_delegated(struct inode *dir, struct dentry *dentry, | |
1393 | struct inode *inode) | |
1394 | { | |
1395 | nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); | |
1396 | return nfs_lookup_revalidate_done(dir, dentry, inode, 1); | |
1397 | } | |
1398 | ||
1399 | static int | |
1400 | nfs_lookup_revalidate_dentry(struct inode *dir, struct dentry *dentry, | |
1401 | struct inode *inode) | |
1402 | { | |
1403 | struct nfs_fh *fhandle; | |
1404 | struct nfs_fattr *fattr; | |
1405 | struct nfs4_label *label; | |
a1147b82 | 1406 | unsigned long dir_verifier; |
5ceb9d7f TM |
1407 | int ret; |
1408 | ||
1409 | ret = -ENOMEM; | |
1410 | fhandle = nfs_alloc_fhandle(); | |
1411 | fattr = nfs_alloc_fattr(); | |
1412 | label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL); | |
1413 | if (fhandle == NULL || fattr == NULL || IS_ERR(label)) | |
1414 | goto out; | |
1415 | ||
a1147b82 | 1416 | dir_verifier = nfs_save_change_attribute(dir); |
f7b37b8b | 1417 | ret = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label); |
5ceb9d7f | 1418 | if (ret < 0) { |
f7b37b8b TM |
1419 | switch (ret) { |
1420 | case -ESTALE: | |
1421 | case -ENOENT: | |
5ceb9d7f | 1422 | ret = 0; |
f7b37b8b TM |
1423 | break; |
1424 | case -ETIMEDOUT: | |
1425 | if (NFS_SERVER(inode)->flags & NFS_MOUNT_SOFTREVAL) | |
1426 | ret = 1; | |
1427 | } | |
5ceb9d7f TM |
1428 | goto out; |
1429 | } | |
1430 | ret = 0; | |
1431 | if (nfs_compare_fh(NFS_FH(inode), fhandle)) | |
1432 | goto out; | |
1433 | if (nfs_refresh_inode(inode, fattr) < 0) | |
1434 | goto out; | |
1435 | ||
1436 | nfs_setsecurity(inode, fattr, label); | |
a1147b82 | 1437 | nfs_set_verifier(dentry, dir_verifier); |
5ceb9d7f TM |
1438 | |
1439 | /* set a readdirplus hint that we had a cache miss */ | |
1440 | nfs_force_use_readdirplus(dir); | |
1441 | ret = 1; | |
1442 | out: | |
1443 | nfs_free_fattr(fattr); | |
1444 | nfs_free_fhandle(fhandle); | |
1445 | nfs4_label_free(label); | |
1446 | return nfs_lookup_revalidate_done(dir, dentry, inode, ret); | |
1447 | } | |
1448 | ||
1da177e4 LT |
1449 | /* |
1450 | * This is called every time the dcache has a lookup hit, | |
1451 | * and we should check whether we can really trust that | |
1452 | * lookup. | |
1453 | * | |
1454 | * NOTE! The hit can be a negative hit too, don't assume | |
1455 | * we have an inode! | |
1456 | * | |
1457 | * If the parent directory is seen to have changed, we throw out the | |
1458 | * cached dentry and do a new lookup. | |
1459 | */ | |
5ceb9d7f TM |
1460 | static int |
1461 | nfs_do_lookup_revalidate(struct inode *dir, struct dentry *dentry, | |
1462 | unsigned int flags) | |
1da177e4 | 1463 | { |
1da177e4 | 1464 | struct inode *inode; |
1da177e4 | 1465 | int error; |
1da177e4 | 1466 | |
91d5b470 | 1467 | nfs_inc_stats(dir, NFSIOS_DENTRYREVALIDATE); |
2b0143b5 | 1468 | inode = d_inode(dentry); |
1da177e4 | 1469 | |
5ceb9d7f TM |
1470 | if (!inode) |
1471 | return nfs_lookup_revalidate_negative(dir, dentry, flags); | |
1da177e4 LT |
1472 | |
1473 | if (is_bad_inode(inode)) { | |
6de1472f AV |
1474 | dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", |
1475 | __func__, dentry); | |
1da177e4 LT |
1476 | goto out_bad; |
1477 | } | |
1478 | ||
efeda80d | 1479 | if (nfs_verifier_is_delegated(dentry)) |
5ceb9d7f | 1480 | return nfs_lookup_revalidate_delegated(dir, dentry, inode); |
15860ab1 | 1481 | |
1da177e4 | 1482 | /* Force a full look up iff the parent directory has changed */ |
73dd684a | 1483 | if (!(flags & (LOOKUP_EXCL | LOOKUP_REVAL)) && |
912a108d | 1484 | nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) { |
cc89684c N |
1485 | error = nfs_lookup_verify_inode(inode, flags); |
1486 | if (error) { | |
cc89684c | 1487 | if (error == -ESTALE) |
5ceb9d7f TM |
1488 | nfs_zap_caches(dir); |
1489 | goto out_bad; | |
1fa1e384 | 1490 | } |
63519fbc | 1491 | nfs_advise_use_readdirplus(dir); |
1da177e4 LT |
1492 | goto out_valid; |
1493 | } | |
1494 | ||
912a108d N |
1495 | if (flags & LOOKUP_RCU) |
1496 | return -ECHILD; | |
1497 | ||
1da177e4 LT |
1498 | if (NFS_STALE(inode)) |
1499 | goto out_bad; | |
1500 | ||
6e0d0be7 | 1501 | trace_nfs_lookup_revalidate_enter(dir, dentry, flags); |
5ceb9d7f | 1502 | error = nfs_lookup_revalidate_dentry(dir, dentry, inode); |
6e0d0be7 | 1503 | trace_nfs_lookup_revalidate_exit(dir, dentry, flags, error); |
5ceb9d7f TM |
1504 | return error; |
1505 | out_valid: | |
1506 | return nfs_lookup_revalidate_done(dir, dentry, inode, 1); | |
1507 | out_bad: | |
1508 | if (flags & LOOKUP_RCU) | |
1509 | return -ECHILD; | |
1510 | return nfs_lookup_revalidate_done(dir, dentry, inode, 0); | |
1511 | } | |
14c43f76 | 1512 | |
5ceb9d7f | 1513 | static int |
c7944ebb TM |
1514 | __nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags, |
1515 | int (*reval)(struct inode *, struct dentry *, unsigned int)) | |
5ceb9d7f TM |
1516 | { |
1517 | struct dentry *parent; | |
1518 | struct inode *dir; | |
1519 | int ret; | |
63519fbc | 1520 | |
d51ac1a8 | 1521 | if (flags & LOOKUP_RCU) { |
5ceb9d7f TM |
1522 | parent = READ_ONCE(dentry->d_parent); |
1523 | dir = d_inode_rcu(parent); | |
1524 | if (!dir) | |
1525 | return -ECHILD; | |
c7944ebb | 1526 | ret = reval(dir, dentry, flags); |
6aa7de05 | 1527 | if (parent != READ_ONCE(dentry->d_parent)) |
d51ac1a8 | 1528 | return -ECHILD; |
5ceb9d7f TM |
1529 | } else { |
1530 | parent = dget_parent(dentry); | |
c7944ebb | 1531 | ret = reval(d_inode(parent), dentry, flags); |
d51ac1a8 | 1532 | dput(parent); |
1da177e4 | 1533 | } |
5ceb9d7f | 1534 | return ret; |
1da177e4 LT |
1535 | } |
1536 | ||
c7944ebb TM |
1537 | static int nfs_lookup_revalidate(struct dentry *dentry, unsigned int flags) |
1538 | { | |
1539 | return __nfs_lookup_revalidate(dentry, flags, nfs_do_lookup_revalidate); | |
1540 | } | |
1541 | ||
ecf3d1f1 | 1542 | /* |
2b0143b5 | 1543 | * A weaker form of d_revalidate for revalidating just the d_inode(dentry) |
ecf3d1f1 JL |
1544 | * when we don't really care about the dentry name. This is called when a |
1545 | * pathwalk ends on a dentry that was not found via a normal lookup in the | |
1546 | * parent dir (e.g.: ".", "..", procfs symlinks or mountpoint traversals). | |
1547 | * | |
1548 | * In this situation, we just want to verify that the inode itself is OK | |
1549 | * since the dentry might have changed on the server. | |
1550 | */ | |
1551 | static int nfs_weak_revalidate(struct dentry *dentry, unsigned int flags) | |
1552 | { | |
2b0143b5 | 1553 | struct inode *inode = d_inode(dentry); |
9cdd1d3f | 1554 | int error = 0; |
ecf3d1f1 JL |
1555 | |
1556 | /* | |
1557 | * I believe we can only get a negative dentry here in the case of a | |
1558 | * procfs-style symlink. Just assume it's correct for now, but we may | |
1559 | * eventually need to do something more here. | |
1560 | */ | |
1561 | if (!inode) { | |
6de1472f AV |
1562 | dfprintk(LOOKUPCACHE, "%s: %pd2 has negative inode\n", |
1563 | __func__, dentry); | |
ecf3d1f1 JL |
1564 | return 1; |
1565 | } | |
1566 | ||
1567 | if (is_bad_inode(inode)) { | |
6de1472f AV |
1568 | dfprintk(LOOKUPCACHE, "%s: %pd2 has dud inode\n", |
1569 | __func__, dentry); | |
ecf3d1f1 JL |
1570 | return 0; |
1571 | } | |
1572 | ||
b688741c | 1573 | error = nfs_lookup_verify_inode(inode, flags); |
ecf3d1f1 JL |
1574 | dfprintk(LOOKUPCACHE, "NFS: %s: inode %lu is %s\n", |
1575 | __func__, inode->i_ino, error ? "invalid" : "valid"); | |
1576 | return !error; | |
1577 | } | |
1578 | ||
1da177e4 LT |
1579 | /* |
1580 | * This is called from dput() when d_count is going to 0. | |
1581 | */ | |
fe15ce44 | 1582 | static int nfs_dentry_delete(const struct dentry *dentry) |
1da177e4 | 1583 | { |
6de1472f AV |
1584 | dfprintk(VFS, "NFS: dentry_delete(%pd2, %x)\n", |
1585 | dentry, dentry->d_flags); | |
1da177e4 | 1586 | |
77f11192 | 1587 | /* Unhash any dentry with a stale inode */ |
2b0143b5 | 1588 | if (d_really_is_positive(dentry) && NFS_STALE(d_inode(dentry))) |
77f11192 TM |
1589 | return 1; |
1590 | ||
1da177e4 LT |
1591 | if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { |
1592 | /* Unhash it, so that ->d_iput() would be called */ | |
1593 | return 1; | |
1594 | } | |
1751e8a6 | 1595 | if (!(dentry->d_sb->s_flags & SB_ACTIVE)) { |
1da177e4 LT |
1596 | /* Unhash it, so that ancestors of killed async unlink |
1597 | * files will be cleaned up during umount */ | |
1598 | return 1; | |
1599 | } | |
1600 | return 0; | |
1601 | ||
1602 | } | |
1603 | ||
1f018458 | 1604 | /* Ensure that we revalidate inode->i_nlink */ |
1b83d707 TM |
1605 | static void nfs_drop_nlink(struct inode *inode) |
1606 | { | |
1607 | spin_lock(&inode->i_lock); | |
1f018458 | 1608 | /* drop the inode if we're reasonably sure this is the last link */ |
59a707b0 TM |
1609 | if (inode->i_nlink > 0) |
1610 | drop_nlink(inode); | |
1611 | NFS_I(inode)->attr_gencount = nfs_inc_attr_generation_counter(); | |
16e14375 TM |
1612 | NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE |
1613 | | NFS_INO_INVALID_CTIME | |
59a707b0 TM |
1614 | | NFS_INO_INVALID_OTHER |
1615 | | NFS_INO_REVAL_FORCED; | |
1b83d707 TM |
1616 | spin_unlock(&inode->i_lock); |
1617 | } | |
1618 | ||
1da177e4 LT |
1619 | /* |
1620 | * Called when the dentry loses inode. | |
1621 | * We use it to clean up silly-renamed files. | |
1622 | */ | |
1623 | static void nfs_dentry_iput(struct dentry *dentry, struct inode *inode) | |
1624 | { | |
83672d39 NB |
1625 | if (S_ISDIR(inode->i_mode)) |
1626 | /* drop any readdir cache as it could easily be old */ | |
1627 | NFS_I(inode)->cache_validity |= NFS_INO_INVALID_DATA; | |
1628 | ||
1da177e4 | 1629 | if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { |
e4eff1a6 | 1630 | nfs_complete_unlink(dentry, inode); |
1f018458 | 1631 | nfs_drop_nlink(inode); |
1da177e4 | 1632 | } |
1da177e4 LT |
1633 | iput(inode); |
1634 | } | |
1635 | ||
b1942c5f AV |
1636 | static void nfs_d_release(struct dentry *dentry) |
1637 | { | |
1638 | /* free cached devname value, if it survived that far */ | |
1639 | if (unlikely(dentry->d_fsdata)) { | |
1640 | if (dentry->d_flags & DCACHE_NFSFS_RENAMED) | |
1641 | WARN_ON(1); | |
1642 | else | |
1643 | kfree(dentry->d_fsdata); | |
1644 | } | |
1645 | } | |
1646 | ||
f786aa90 | 1647 | const struct dentry_operations nfs_dentry_operations = { |
1da177e4 | 1648 | .d_revalidate = nfs_lookup_revalidate, |
ecf3d1f1 | 1649 | .d_weak_revalidate = nfs_weak_revalidate, |
1da177e4 LT |
1650 | .d_delete = nfs_dentry_delete, |
1651 | .d_iput = nfs_dentry_iput, | |
36d43a43 | 1652 | .d_automount = nfs_d_automount, |
b1942c5f | 1653 | .d_release = nfs_d_release, |
1da177e4 | 1654 | }; |
ddda8e0a | 1655 | EXPORT_SYMBOL_GPL(nfs_dentry_operations); |
1da177e4 | 1656 | |
597d9289 | 1657 | struct dentry *nfs_lookup(struct inode *dir, struct dentry * dentry, unsigned int flags) |
1da177e4 LT |
1658 | { |
1659 | struct dentry *res; | |
1660 | struct inode *inode = NULL; | |
e1fb4d05 TM |
1661 | struct nfs_fh *fhandle = NULL; |
1662 | struct nfs_fattr *fattr = NULL; | |
1775fd3e | 1663 | struct nfs4_label *label = NULL; |
a1147b82 | 1664 | unsigned long dir_verifier; |
1da177e4 | 1665 | int error; |
1da177e4 | 1666 | |
6de1472f | 1667 | dfprintk(VFS, "NFS: lookup(%pd2)\n", dentry); |
91d5b470 | 1668 | nfs_inc_stats(dir, NFSIOS_VFSLOOKUP); |
1da177e4 | 1669 | |
130f9ab7 AV |
1670 | if (unlikely(dentry->d_name.len > NFS_SERVER(dir)->namelen)) |
1671 | return ERR_PTR(-ENAMETOOLONG); | |
1da177e4 | 1672 | |
fd684071 TM |
1673 | /* |
1674 | * If we're doing an exclusive create, optimize away the lookup | |
1675 | * but don't hash the dentry. | |
1676 | */ | |
9f6d44d4 | 1677 | if (nfs_is_exclusive_create(dir, flags) || flags & LOOKUP_RENAME_TARGET) |
130f9ab7 | 1678 | return NULL; |
1da177e4 | 1679 | |
e1fb4d05 TM |
1680 | res = ERR_PTR(-ENOMEM); |
1681 | fhandle = nfs_alloc_fhandle(); | |
1682 | fattr = nfs_alloc_fattr(); | |
1683 | if (fhandle == NULL || fattr == NULL) | |
1684 | goto out; | |
1685 | ||
14c43f76 DQ |
1686 | label = nfs4_label_alloc(NFS_SERVER(dir), GFP_NOWAIT); |
1687 | if (IS_ERR(label)) | |
1688 | goto out; | |
1689 | ||
a1147b82 | 1690 | dir_verifier = nfs_save_change_attribute(dir); |
6e0d0be7 | 1691 | trace_nfs_lookup_enter(dir, dentry, flags); |
f7b37b8b | 1692 | error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, label); |
1da177e4 LT |
1693 | if (error == -ENOENT) |
1694 | goto no_entry; | |
1695 | if (error < 0) { | |
1696 | res = ERR_PTR(error); | |
bf130914 | 1697 | goto out_label; |
1da177e4 | 1698 | } |
1775fd3e | 1699 | inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label); |
bf0c84f1 | 1700 | res = ERR_CAST(inode); |
03f28e3a | 1701 | if (IS_ERR(res)) |
bf130914 | 1702 | goto out_label; |
54ceac45 | 1703 | |
63519fbc TM |
1704 | /* Notify readdir to use READDIRPLUS */ |
1705 | nfs_force_use_readdirplus(dir); | |
d69ee9b8 | 1706 | |
1da177e4 | 1707 | no_entry: |
41d28bca | 1708 | res = d_splice_alias(inode, dentry); |
9eaef27b TM |
1709 | if (res != NULL) { |
1710 | if (IS_ERR(res)) | |
bf130914 | 1711 | goto out_label; |
1da177e4 | 1712 | dentry = res; |
9eaef27b | 1713 | } |
a1147b82 | 1714 | nfs_set_verifier(dentry, dir_verifier); |
bf130914 | 1715 | out_label: |
6e0d0be7 | 1716 | trace_nfs_lookup_exit(dir, dentry, flags, error); |
14c43f76 | 1717 | nfs4_label_free(label); |
1da177e4 | 1718 | out: |
e1fb4d05 TM |
1719 | nfs_free_fattr(fattr); |
1720 | nfs_free_fhandle(fhandle); | |
1da177e4 LT |
1721 | return res; |
1722 | } | |
ddda8e0a | 1723 | EXPORT_SYMBOL_GPL(nfs_lookup); |
1da177e4 | 1724 | |
89d77c8f | 1725 | #if IS_ENABLED(CONFIG_NFS_V4) |
0b728e19 | 1726 | static int nfs4_lookup_revalidate(struct dentry *, unsigned int); |
1da177e4 | 1727 | |
f786aa90 | 1728 | const struct dentry_operations nfs4_dentry_operations = { |
0ef97dcf | 1729 | .d_revalidate = nfs4_lookup_revalidate, |
b688741c | 1730 | .d_weak_revalidate = nfs_weak_revalidate, |
1da177e4 LT |
1731 | .d_delete = nfs_dentry_delete, |
1732 | .d_iput = nfs_dentry_iput, | |
36d43a43 | 1733 | .d_automount = nfs_d_automount, |
b1942c5f | 1734 | .d_release = nfs_d_release, |
1da177e4 | 1735 | }; |
89d77c8f | 1736 | EXPORT_SYMBOL_GPL(nfs4_dentry_operations); |
1da177e4 | 1737 | |
8a5e929d AV |
1738 | static fmode_t flags_to_mode(int flags) |
1739 | { | |
1740 | fmode_t res = (__force fmode_t)flags & FMODE_EXEC; | |
1741 | if ((flags & O_ACCMODE) != O_WRONLY) | |
1742 | res |= FMODE_READ; | |
1743 | if ((flags & O_ACCMODE) != O_RDONLY) | |
1744 | res |= FMODE_WRITE; | |
1745 | return res; | |
1746 | } | |
1747 | ||
532d4def | 1748 | static struct nfs_open_context *create_nfs_open_context(struct dentry *dentry, int open_flags, struct file *filp) |
cd9a1c0e | 1749 | { |
532d4def | 1750 | return alloc_nfs_open_context(dentry, flags_to_mode(open_flags), filp); |
cd9a1c0e TM |
1751 | } |
1752 | ||
1753 | static int do_open(struct inode *inode, struct file *filp) | |
1754 | { | |
f1fe29b4 | 1755 | nfs_fscache_open_file(inode, filp); |
cd9a1c0e TM |
1756 | return 0; |
1757 | } | |
1758 | ||
d9585277 AV |
1759 | static int nfs_finish_open(struct nfs_open_context *ctx, |
1760 | struct dentry *dentry, | |
b452a458 | 1761 | struct file *file, unsigned open_flags) |
cd9a1c0e | 1762 | { |
0dd2b474 MS |
1763 | int err; |
1764 | ||
be12af3e | 1765 | err = finish_open(file, dentry, do_open); |
30d90494 | 1766 | if (err) |
d9585277 | 1767 | goto out; |
eaa2b82c N |
1768 | if (S_ISREG(file->f_path.dentry->d_inode->i_mode)) |
1769 | nfs_file_set_open_context(file, ctx); | |
1770 | else | |
9821421a | 1771 | err = -EOPENSTALE; |
cd9a1c0e | 1772 | out: |
d9585277 | 1773 | return err; |
cd9a1c0e TM |
1774 | } |
1775 | ||
73a79706 BS |
1776 | int nfs_atomic_open(struct inode *dir, struct dentry *dentry, |
1777 | struct file *file, unsigned open_flags, | |
44907d79 | 1778 | umode_t mode) |
1da177e4 | 1779 | { |
c94c0953 | 1780 | DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); |
cd9a1c0e | 1781 | struct nfs_open_context *ctx; |
0dd2b474 MS |
1782 | struct dentry *res; |
1783 | struct iattr attr = { .ia_valid = ATTR_OPEN }; | |
f46e0bd3 | 1784 | struct inode *inode; |
1472b83e | 1785 | unsigned int lookup_flags = 0; |
c94c0953 | 1786 | bool switched = false; |
73a09dd9 | 1787 | int created = 0; |
898f635c | 1788 | int err; |
1da177e4 | 1789 | |
0dd2b474 | 1790 | /* Expect a negative dentry */ |
2b0143b5 | 1791 | BUG_ON(d_inode(dentry)); |
0dd2b474 | 1792 | |
1e8968c5 | 1793 | dfprintk(VFS, "NFS: atomic_open(%s/%lu), %pd\n", |
6de1472f | 1794 | dir->i_sb->s_id, dir->i_ino, dentry); |
1e7cb3dc | 1795 | |
9597c13b JL |
1796 | err = nfs_check_flags(open_flags); |
1797 | if (err) | |
1798 | return err; | |
1799 | ||
0dd2b474 MS |
1800 | /* NFS only supports OPEN on regular files */ |
1801 | if ((open_flags & O_DIRECTORY)) { | |
00699ad8 | 1802 | if (!d_in_lookup(dentry)) { |
0dd2b474 MS |
1803 | /* |
1804 | * Hashed negative dentry with O_DIRECTORY: dentry was | |
1805 | * revalidated and is fine, no need to perform lookup | |
1806 | * again | |
1807 | */ | |
d9585277 | 1808 | return -ENOENT; |
0dd2b474 | 1809 | } |
1472b83e | 1810 | lookup_flags = LOOKUP_OPEN|LOOKUP_DIRECTORY; |
1da177e4 | 1811 | goto no_open; |
02a913a7 | 1812 | } |
1da177e4 | 1813 | |
0dd2b474 | 1814 | if (dentry->d_name.len > NFS_SERVER(dir)->namelen) |
d9585277 | 1815 | return -ENAMETOOLONG; |
cd9a1c0e | 1816 | |
0dd2b474 | 1817 | if (open_flags & O_CREAT) { |
dff25ddb AG |
1818 | struct nfs_server *server = NFS_SERVER(dir); |
1819 | ||
1820 | if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK)) | |
1821 | mode &= ~current_umask(); | |
1822 | ||
536e43d1 | 1823 | attr.ia_valid |= ATTR_MODE; |
dff25ddb | 1824 | attr.ia_mode = mode; |
0dd2b474 | 1825 | } |
536e43d1 TM |
1826 | if (open_flags & O_TRUNC) { |
1827 | attr.ia_valid |= ATTR_SIZE; | |
1828 | attr.ia_size = 0; | |
cd9a1c0e TM |
1829 | } |
1830 | ||
c94c0953 AV |
1831 | if (!(open_flags & O_CREAT) && !d_in_lookup(dentry)) { |
1832 | d_drop(dentry); | |
1833 | switched = true; | |
1834 | dentry = d_alloc_parallel(dentry->d_parent, | |
1835 | &dentry->d_name, &wq); | |
1836 | if (IS_ERR(dentry)) | |
1837 | return PTR_ERR(dentry); | |
1838 | if (unlikely(!d_in_lookup(dentry))) | |
1839 | return finish_no_open(file, dentry); | |
1840 | } | |
1841 | ||
532d4def | 1842 | ctx = create_nfs_open_context(dentry, open_flags, file); |
0dd2b474 MS |
1843 | err = PTR_ERR(ctx); |
1844 | if (IS_ERR(ctx)) | |
d9585277 | 1845 | goto out; |
0dd2b474 | 1846 | |
6e0d0be7 | 1847 | trace_nfs_atomic_open_enter(dir, ctx, open_flags); |
73a09dd9 AV |
1848 | inode = NFS_PROTO(dir)->open_context(dir, ctx, open_flags, &attr, &created); |
1849 | if (created) | |
1850 | file->f_mode |= FMODE_CREATED; | |
f46e0bd3 | 1851 | if (IS_ERR(inode)) { |
0dd2b474 | 1852 | err = PTR_ERR(inode); |
6e0d0be7 | 1853 | trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); |
2d9db750 | 1854 | put_nfs_open_context(ctx); |
d20cb71d | 1855 | d_drop(dentry); |
0dd2b474 MS |
1856 | switch (err) { |
1857 | case -ENOENT: | |
774d9513 | 1858 | d_splice_alias(NULL, dentry); |
809fd143 | 1859 | nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); |
0dd2b474 MS |
1860 | break; |
1861 | case -EISDIR: | |
1862 | case -ENOTDIR: | |
1863 | goto no_open; | |
1864 | case -ELOOP: | |
1865 | if (!(open_flags & O_NOFOLLOW)) | |
6f926b5b | 1866 | goto no_open; |
0dd2b474 | 1867 | break; |
1da177e4 | 1868 | /* case -EINVAL: */ |
0dd2b474 MS |
1869 | default: |
1870 | break; | |
1da177e4 | 1871 | } |
d9585277 | 1872 | goto out; |
cd9a1c0e | 1873 | } |
0dd2b474 | 1874 | |
b452a458 | 1875 | err = nfs_finish_open(ctx, ctx->dentry, file, open_flags); |
6e0d0be7 | 1876 | trace_nfs_atomic_open_exit(dir, ctx, open_flags, err); |
2d9db750 | 1877 | put_nfs_open_context(ctx); |
d9585277 | 1878 | out: |
c94c0953 AV |
1879 | if (unlikely(switched)) { |
1880 | d_lookup_done(dentry); | |
1881 | dput(dentry); | |
1882 | } | |
d9585277 | 1883 | return err; |
0dd2b474 | 1884 | |
1da177e4 | 1885 | no_open: |
1472b83e | 1886 | res = nfs_lookup(dir, dentry, lookup_flags); |
c94c0953 AV |
1887 | if (switched) { |
1888 | d_lookup_done(dentry); | |
1889 | if (!res) | |
1890 | res = dentry; | |
1891 | else | |
1892 | dput(dentry); | |
1893 | } | |
0dd2b474 | 1894 | if (IS_ERR(res)) |
c94c0953 | 1895 | return PTR_ERR(res); |
e45198a6 | 1896 | return finish_no_open(file, res); |
1da177e4 | 1897 | } |
89d77c8f | 1898 | EXPORT_SYMBOL_GPL(nfs_atomic_open); |
1da177e4 | 1899 | |
c7944ebb TM |
1900 | static int |
1901 | nfs4_do_lookup_revalidate(struct inode *dir, struct dentry *dentry, | |
1902 | unsigned int flags) | |
1da177e4 | 1903 | { |
657e94b6 | 1904 | struct inode *inode; |
1da177e4 | 1905 | |
fa3c56bb | 1906 | if (!(flags & LOOKUP_OPEN) || (flags & LOOKUP_DIRECTORY)) |
c7944ebb | 1907 | goto full_reval; |
eda72afb | 1908 | if (d_mountpoint(dentry)) |
c7944ebb | 1909 | goto full_reval; |
2b484297 | 1910 | |
2b0143b5 | 1911 | inode = d_inode(dentry); |
2b484297 | 1912 | |
1da177e4 LT |
1913 | /* We can't create new files in nfs_open_revalidate(), so we |
1914 | * optimize away revalidation of negative dentries. | |
1915 | */ | |
c7944ebb TM |
1916 | if (inode == NULL) |
1917 | goto full_reval; | |
1918 | ||
efeda80d | 1919 | if (nfs_verifier_is_delegated(dentry)) |
c7944ebb | 1920 | return nfs_lookup_revalidate_delegated(dir, dentry, inode); |
216d5d06 | 1921 | |
1da177e4 LT |
1922 | /* NFS only supports OPEN on regular files */ |
1923 | if (!S_ISREG(inode->i_mode)) | |
c7944ebb TM |
1924 | goto full_reval; |
1925 | ||
1da177e4 | 1926 | /* We cannot do exclusive creation on a positive dentry */ |
c7944ebb TM |
1927 | if (flags & (LOOKUP_EXCL | LOOKUP_REVAL)) |
1928 | goto reval_dentry; | |
1929 | ||
1930 | /* Check if the directory changed */ | |
1931 | if (!nfs_check_verifier(dir, dentry, flags & LOOKUP_RCU)) | |
1932 | goto reval_dentry; | |
1da177e4 | 1933 | |
0ef97dcf | 1934 | /* Let f_op->open() actually open (and revalidate) the file */ |
c7944ebb TM |
1935 | return 1; |
1936 | reval_dentry: | |
1937 | if (flags & LOOKUP_RCU) | |
1938 | return -ECHILD; | |
42f72cf3 | 1939 | return nfs_lookup_revalidate_dentry(dir, dentry, inode); |
536e43d1 | 1940 | |
c7944ebb TM |
1941 | full_reval: |
1942 | return nfs_do_lookup_revalidate(dir, dentry, flags); | |
1943 | } | |
535918f1 | 1944 | |
c7944ebb TM |
1945 | static int nfs4_lookup_revalidate(struct dentry *dentry, unsigned int flags) |
1946 | { | |
1947 | return __nfs_lookup_revalidate(dentry, flags, | |
1948 | nfs4_do_lookup_revalidate); | |
c0204fd2 TM |
1949 | } |
1950 | ||
1da177e4 LT |
1951 | #endif /* CONFIG_NFSV4 */ |
1952 | ||
406cd915 BC |
1953 | struct dentry * |
1954 | nfs_add_or_obtain(struct dentry *dentry, struct nfs_fh *fhandle, | |
1775fd3e DQ |
1955 | struct nfs_fattr *fattr, |
1956 | struct nfs4_label *label) | |
1da177e4 | 1957 | { |
fab728e1 | 1958 | struct dentry *parent = dget_parent(dentry); |
2b0143b5 | 1959 | struct inode *dir = d_inode(parent); |
1da177e4 | 1960 | struct inode *inode; |
b0c6108e | 1961 | struct dentry *d; |
406cd915 | 1962 | int error; |
1da177e4 | 1963 | |
fab728e1 TM |
1964 | d_drop(dentry); |
1965 | ||
1da177e4 | 1966 | if (fhandle->size == 0) { |
f7b37b8b | 1967 | error = NFS_PROTO(dir)->lookup(dir, dentry, fhandle, fattr, NULL); |
1da177e4 | 1968 | if (error) |
fab728e1 | 1969 | goto out_error; |
1da177e4 | 1970 | } |
5724ab37 | 1971 | nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); |
1da177e4 LT |
1972 | if (!(fattr->valid & NFS_ATTR_FATTR)) { |
1973 | struct nfs_server *server = NFS_SB(dentry->d_sb); | |
a841b54d TM |
1974 | error = server->nfs_client->rpc_ops->getattr(server, fhandle, |
1975 | fattr, NULL, NULL); | |
1da177e4 | 1976 | if (error < 0) |
fab728e1 | 1977 | goto out_error; |
1da177e4 | 1978 | } |
1775fd3e | 1979 | inode = nfs_fhget(dentry->d_sb, fhandle, fattr, label); |
b0c6108e | 1980 | d = d_splice_alias(inode, dentry); |
fab728e1 TM |
1981 | out: |
1982 | dput(parent); | |
406cd915 | 1983 | return d; |
fab728e1 TM |
1984 | out_error: |
1985 | nfs_mark_for_revalidate(dir); | |
406cd915 BC |
1986 | d = ERR_PTR(error); |
1987 | goto out; | |
1988 | } | |
1989 | EXPORT_SYMBOL_GPL(nfs_add_or_obtain); | |
1990 | ||
1991 | /* | |
1992 | * Code common to create, mkdir, and mknod. | |
1993 | */ | |
1994 | int nfs_instantiate(struct dentry *dentry, struct nfs_fh *fhandle, | |
1995 | struct nfs_fattr *fattr, | |
1996 | struct nfs4_label *label) | |
1997 | { | |
1998 | struct dentry *d; | |
1999 | ||
2000 | d = nfs_add_or_obtain(dentry, fhandle, fattr, label); | |
2001 | if (IS_ERR(d)) | |
2002 | return PTR_ERR(d); | |
2003 | ||
2004 | /* Callers don't care */ | |
2005 | dput(d); | |
2006 | return 0; | |
1da177e4 | 2007 | } |
ddda8e0a | 2008 | EXPORT_SYMBOL_GPL(nfs_instantiate); |
1da177e4 LT |
2009 | |
2010 | /* | |
2011 | * Following a failed create operation, we drop the dentry rather | |
2012 | * than retain a negative dentry. This avoids a problem in the event | |
2013 | * that the operation succeeded on the server, but an error in the | |
2014 | * reply path made it appear to have failed. | |
2015 | */ | |
597d9289 | 2016 | int nfs_create(struct inode *dir, struct dentry *dentry, |
ebfc3b49 | 2017 | umode_t mode, bool excl) |
1da177e4 LT |
2018 | { |
2019 | struct iattr attr; | |
ebfc3b49 | 2020 | int open_flags = excl ? O_CREAT | O_EXCL : O_CREAT; |
1da177e4 | 2021 | int error; |
1da177e4 | 2022 | |
1e8968c5 | 2023 | dfprintk(VFS, "NFS: create(%s/%lu), %pd\n", |
6de1472f | 2024 | dir->i_sb->s_id, dir->i_ino, dentry); |
1da177e4 LT |
2025 | |
2026 | attr.ia_mode = mode; | |
2027 | attr.ia_valid = ATTR_MODE; | |
2028 | ||
8b0ad3d4 | 2029 | trace_nfs_create_enter(dir, dentry, open_flags); |
8867fe58 | 2030 | error = NFS_PROTO(dir)->create(dir, dentry, &attr, open_flags); |
8b0ad3d4 | 2031 | trace_nfs_create_exit(dir, dentry, open_flags, error); |
1da177e4 LT |
2032 | if (error != 0) |
2033 | goto out_err; | |
1da177e4 LT |
2034 | return 0; |
2035 | out_err: | |
1da177e4 LT |
2036 | d_drop(dentry); |
2037 | return error; | |
2038 | } | |
ddda8e0a | 2039 | EXPORT_SYMBOL_GPL(nfs_create); |
1da177e4 LT |
2040 | |
2041 | /* | |
2042 | * See comments for nfs_proc_create regarding failed operations. | |
2043 | */ | |
597d9289 | 2044 | int |
1a67aafb | 2045 | nfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t rdev) |
1da177e4 LT |
2046 | { |
2047 | struct iattr attr; | |
2048 | int status; | |
2049 | ||
1e8968c5 | 2050 | dfprintk(VFS, "NFS: mknod(%s/%lu), %pd\n", |
6de1472f | 2051 | dir->i_sb->s_id, dir->i_ino, dentry); |
1da177e4 | 2052 | |
1da177e4 LT |
2053 | attr.ia_mode = mode; |
2054 | attr.ia_valid = ATTR_MODE; | |
2055 | ||
1ca42382 | 2056 | trace_nfs_mknod_enter(dir, dentry); |
1da177e4 | 2057 | status = NFS_PROTO(dir)->mknod(dir, dentry, &attr, rdev); |
1ca42382 | 2058 | trace_nfs_mknod_exit(dir, dentry, status); |
1da177e4 LT |
2059 | if (status != 0) |
2060 | goto out_err; | |
1da177e4 LT |
2061 | return 0; |
2062 | out_err: | |
1da177e4 LT |
2063 | d_drop(dentry); |
2064 | return status; | |
2065 | } | |
ddda8e0a | 2066 | EXPORT_SYMBOL_GPL(nfs_mknod); |
1da177e4 LT |
2067 | |
2068 | /* | |
2069 | * See comments for nfs_proc_create regarding failed operations. | |
2070 | */ | |
597d9289 | 2071 | int nfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
1da177e4 LT |
2072 | { |
2073 | struct iattr attr; | |
2074 | int error; | |
2075 | ||
1e8968c5 | 2076 | dfprintk(VFS, "NFS: mkdir(%s/%lu), %pd\n", |
6de1472f | 2077 | dir->i_sb->s_id, dir->i_ino, dentry); |
1da177e4 LT |
2078 | |
2079 | attr.ia_valid = ATTR_MODE; | |
2080 | attr.ia_mode = mode | S_IFDIR; | |
2081 | ||
1ca42382 | 2082 | trace_nfs_mkdir_enter(dir, dentry); |
1da177e4 | 2083 | error = NFS_PROTO(dir)->mkdir(dir, dentry, &attr); |
1ca42382 | 2084 | trace_nfs_mkdir_exit(dir, dentry, error); |
1da177e4 LT |
2085 | if (error != 0) |
2086 | goto out_err; | |
1da177e4 LT |
2087 | return 0; |
2088 | out_err: | |
2089 | d_drop(dentry); | |
1da177e4 LT |
2090 | return error; |
2091 | } | |
ddda8e0a | 2092 | EXPORT_SYMBOL_GPL(nfs_mkdir); |
1da177e4 | 2093 | |
d45b9d8b TM |
2094 | static void nfs_dentry_handle_enoent(struct dentry *dentry) |
2095 | { | |
dc3f4198 | 2096 | if (simple_positive(dentry)) |
d45b9d8b TM |
2097 | d_delete(dentry); |
2098 | } | |
2099 | ||
597d9289 | 2100 | int nfs_rmdir(struct inode *dir, struct dentry *dentry) |
1da177e4 LT |
2101 | { |
2102 | int error; | |
2103 | ||
1e8968c5 | 2104 | dfprintk(VFS, "NFS: rmdir(%s/%lu), %pd\n", |
6de1472f | 2105 | dir->i_sb->s_id, dir->i_ino, dentry); |
1da177e4 | 2106 | |
1ca42382 | 2107 | trace_nfs_rmdir_enter(dir, dentry); |
2b0143b5 | 2108 | if (d_really_is_positive(dentry)) { |
884be175 | 2109 | down_write(&NFS_I(d_inode(dentry))->rmdir_sem); |
ba6c0592 TM |
2110 | error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); |
2111 | /* Ensure the VFS deletes this inode */ | |
2112 | switch (error) { | |
2113 | case 0: | |
2b0143b5 | 2114 | clear_nlink(d_inode(dentry)); |
ba6c0592 TM |
2115 | break; |
2116 | case -ENOENT: | |
2117 | nfs_dentry_handle_enoent(dentry); | |
2118 | } | |
884be175 | 2119 | up_write(&NFS_I(d_inode(dentry))->rmdir_sem); |
ba6c0592 TM |
2120 | } else |
2121 | error = NFS_PROTO(dir)->rmdir(dir, &dentry->d_name); | |
1ca42382 | 2122 | trace_nfs_rmdir_exit(dir, dentry, error); |
1da177e4 LT |
2123 | |
2124 | return error; | |
2125 | } | |
ddda8e0a | 2126 | EXPORT_SYMBOL_GPL(nfs_rmdir); |
1da177e4 | 2127 | |
1da177e4 LT |
2128 | /* |
2129 | * Remove a file after making sure there are no pending writes, | |
2130 | * and after checking that the file has only one user. | |
2131 | * | |
2132 | * We invalidate the attribute cache and free the inode prior to the operation | |
2133 | * to avoid possible races if the server reuses the inode. | |
2134 | */ | |
2135 | static int nfs_safe_remove(struct dentry *dentry) | |
2136 | { | |
2b0143b5 DH |
2137 | struct inode *dir = d_inode(dentry->d_parent); |
2138 | struct inode *inode = d_inode(dentry); | |
1da177e4 LT |
2139 | int error = -EBUSY; |
2140 | ||
6de1472f | 2141 | dfprintk(VFS, "NFS: safe_remove(%pd2)\n", dentry); |
1da177e4 LT |
2142 | |
2143 | /* If the dentry was sillyrenamed, we simply call d_delete() */ | |
2144 | if (dentry->d_flags & DCACHE_NFSFS_RENAMED) { | |
2145 | error = 0; | |
2146 | goto out; | |
2147 | } | |
2148 | ||
1ca42382 | 2149 | trace_nfs_remove_enter(dir, dentry); |
1da177e4 | 2150 | if (inode != NULL) { |
912678db | 2151 | error = NFS_PROTO(dir)->remove(dir, dentry); |
1da177e4 | 2152 | if (error == 0) |
1b83d707 | 2153 | nfs_drop_nlink(inode); |
1da177e4 | 2154 | } else |
912678db | 2155 | error = NFS_PROTO(dir)->remove(dir, dentry); |
d45b9d8b TM |
2156 | if (error == -ENOENT) |
2157 | nfs_dentry_handle_enoent(dentry); | |
1ca42382 | 2158 | trace_nfs_remove_exit(dir, dentry, error); |
1da177e4 LT |
2159 | out: |
2160 | return error; | |
2161 | } | |
2162 | ||
2163 | /* We do silly rename. In case sillyrename() returns -EBUSY, the inode | |
2164 | * belongs to an active ".nfs..." file and we return -EBUSY. | |
2165 | * | |
2166 | * If sillyrename() returns 0, we do nothing, otherwise we unlink. | |
2167 | */ | |
597d9289 | 2168 | int nfs_unlink(struct inode *dir, struct dentry *dentry) |
1da177e4 LT |
2169 | { |
2170 | int error; | |
2171 | int need_rehash = 0; | |
2172 | ||
1e8968c5 | 2173 | dfprintk(VFS, "NFS: unlink(%s/%lu, %pd)\n", dir->i_sb->s_id, |
6de1472f | 2174 | dir->i_ino, dentry); |
1da177e4 | 2175 | |
1ca42382 | 2176 | trace_nfs_unlink_enter(dir, dentry); |
1da177e4 | 2177 | spin_lock(&dentry->d_lock); |
84d08fa8 | 2178 | if (d_count(dentry) > 1) { |
1da177e4 | 2179 | spin_unlock(&dentry->d_lock); |
ccfeb506 | 2180 | /* Start asynchronous writeout of the inode */ |
2b0143b5 | 2181 | write_inode_now(d_inode(dentry), 0); |
1da177e4 | 2182 | error = nfs_sillyrename(dir, dentry); |
1ca42382 | 2183 | goto out; |
1da177e4 LT |
2184 | } |
2185 | if (!d_unhashed(dentry)) { | |
2186 | __d_drop(dentry); | |
2187 | need_rehash = 1; | |
2188 | } | |
2189 | spin_unlock(&dentry->d_lock); | |
1da177e4 | 2190 | error = nfs_safe_remove(dentry); |
d45b9d8b | 2191 | if (!error || error == -ENOENT) { |
1da177e4 LT |
2192 | nfs_set_verifier(dentry, nfs_save_change_attribute(dir)); |
2193 | } else if (need_rehash) | |
2194 | d_rehash(dentry); | |
1ca42382 TM |
2195 | out: |
2196 | trace_nfs_unlink_exit(dir, dentry, error); | |
1da177e4 LT |
2197 | return error; |
2198 | } | |
ddda8e0a | 2199 | EXPORT_SYMBOL_GPL(nfs_unlink); |
1da177e4 | 2200 | |
873101b3 CL |
2201 | /* |
2202 | * To create a symbolic link, most file systems instantiate a new inode, | |
2203 | * add a page to it containing the path, then write it out to the disk | |
2204 | * using prepare_write/commit_write. | |
2205 | * | |
2206 | * Unfortunately the NFS client can't create the in-core inode first | |
2207 | * because it needs a file handle to create an in-core inode (see | |
2208 | * fs/nfs/inode.c:nfs_fhget). We only have a file handle *after* the | |
2209 | * symlink request has completed on the server. | |
2210 | * | |
2211 | * So instead we allocate a raw page, copy the symname into it, then do | |
2212 | * the SYMLINK request with the page as the buffer. If it succeeds, we | |
2213 | * now have a new file handle and can instantiate an in-core NFS inode | |
2214 | * and move the raw page into its mapping. | |
2215 | */ | |
597d9289 | 2216 | int nfs_symlink(struct inode *dir, struct dentry *dentry, const char *symname) |
1da177e4 | 2217 | { |
873101b3 CL |
2218 | struct page *page; |
2219 | char *kaddr; | |
1da177e4 | 2220 | struct iattr attr; |
873101b3 | 2221 | unsigned int pathlen = strlen(symname); |
1da177e4 LT |
2222 | int error; |
2223 | ||
1e8968c5 | 2224 | dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s)\n", dir->i_sb->s_id, |
6de1472f | 2225 | dir->i_ino, dentry, symname); |
1da177e4 | 2226 | |
873101b3 CL |
2227 | if (pathlen > PAGE_SIZE) |
2228 | return -ENAMETOOLONG; | |
1da177e4 | 2229 | |
873101b3 CL |
2230 | attr.ia_mode = S_IFLNK | S_IRWXUGO; |
2231 | attr.ia_valid = ATTR_MODE; | |
1da177e4 | 2232 | |
e8ecde25 | 2233 | page = alloc_page(GFP_USER); |
76566991 | 2234 | if (!page) |
873101b3 | 2235 | return -ENOMEM; |
873101b3 | 2236 | |
e8ecde25 | 2237 | kaddr = page_address(page); |
873101b3 CL |
2238 | memcpy(kaddr, symname, pathlen); |
2239 | if (pathlen < PAGE_SIZE) | |
2240 | memset(kaddr + pathlen, 0, PAGE_SIZE - pathlen); | |
873101b3 | 2241 | |
1ca42382 | 2242 | trace_nfs_symlink_enter(dir, dentry); |
94a6d753 | 2243 | error = NFS_PROTO(dir)->symlink(dir, dentry, page, pathlen, &attr); |
1ca42382 | 2244 | trace_nfs_symlink_exit(dir, dentry, error); |
873101b3 | 2245 | if (error != 0) { |
1e8968c5 | 2246 | dfprintk(VFS, "NFS: symlink(%s/%lu, %pd, %s) error %d\n", |
873101b3 | 2247 | dir->i_sb->s_id, dir->i_ino, |
6de1472f | 2248 | dentry, symname, error); |
1da177e4 | 2249 | d_drop(dentry); |
873101b3 | 2250 | __free_page(page); |
873101b3 CL |
2251 | return error; |
2252 | } | |
2253 | ||
2254 | /* | |
2255 | * No big deal if we can't add this page to the page cache here. | |
2256 | * READLINK will get the missing page from the server if needed. | |
2257 | */ | |
2b0143b5 | 2258 | if (!add_to_page_cache_lru(page, d_inode(dentry)->i_mapping, 0, |
873101b3 | 2259 | GFP_KERNEL)) { |
873101b3 CL |
2260 | SetPageUptodate(page); |
2261 | unlock_page(page); | |
a0b54add RA |
2262 | /* |
2263 | * add_to_page_cache_lru() grabs an extra page refcount. | |
2264 | * Drop it here to avoid leaking this page later. | |
2265 | */ | |
09cbfeaf | 2266 | put_page(page); |
873101b3 CL |
2267 | } else |
2268 | __free_page(page); | |
2269 | ||
873101b3 | 2270 | return 0; |
1da177e4 | 2271 | } |
ddda8e0a | 2272 | EXPORT_SYMBOL_GPL(nfs_symlink); |
1da177e4 | 2273 | |
597d9289 | 2274 | int |
1da177e4 LT |
2275 | nfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) |
2276 | { | |
2b0143b5 | 2277 | struct inode *inode = d_inode(old_dentry); |
1da177e4 LT |
2278 | int error; |
2279 | ||
6de1472f AV |
2280 | dfprintk(VFS, "NFS: link(%pd2 -> %pd2)\n", |
2281 | old_dentry, dentry); | |
1da177e4 | 2282 | |
1fd1085b | 2283 | trace_nfs_link_enter(inode, dir, dentry); |
9697d234 | 2284 | d_drop(dentry); |
1da177e4 | 2285 | error = NFS_PROTO(dir)->link(inode, dir, &dentry->d_name); |
cf809556 | 2286 | if (error == 0) { |
7de9c6ee | 2287 | ihold(inode); |
9697d234 | 2288 | d_add(dentry, inode); |
cf809556 | 2289 | } |
1fd1085b | 2290 | trace_nfs_link_exit(inode, dir, dentry, error); |
1da177e4 LT |
2291 | return error; |
2292 | } | |
ddda8e0a | 2293 | EXPORT_SYMBOL_GPL(nfs_link); |
1da177e4 LT |
2294 | |
2295 | /* | |
2296 | * RENAME | |
2297 | * FIXME: Some nfsds, like the Linux user space nfsd, may generate a | |
2298 | * different file handle for the same inode after a rename (e.g. when | |
2299 | * moving to a different directory). A fail-safe method to do so would | |
2300 | * be to look up old_dir/old_name, create a link to new_dir/new_name and | |
2301 | * rename the old file using the sillyrename stuff. This way, the original | |
2302 | * file in old_dir will go away when the last process iput()s the inode. | |
2303 | * | |
2304 | * FIXED. | |
2305 | * | |
2306 | * It actually works quite well. One needs to have the possibility for | |
2307 | * at least one ".nfs..." file in each directory the file ever gets | |
2308 | * moved or linked to which happens automagically with the new | |
2309 | * implementation that only depends on the dcache stuff instead of | |
2310 | * using the inode layer | |
2311 | * | |
2312 | * Unfortunately, things are a little more complicated than indicated | |
2313 | * above. For a cross-directory move, we want to make sure we can get | |
2314 | * rid of the old inode after the operation. This means there must be | |
2315 | * no pending writes (if it's a file), and the use count must be 1. | |
2316 | * If these conditions are met, we can drop the dentries before doing | |
2317 | * the rename. | |
2318 | */ | |
597d9289 | 2319 | int nfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
1cd66c93 MS |
2320 | struct inode *new_dir, struct dentry *new_dentry, |
2321 | unsigned int flags) | |
1da177e4 | 2322 | { |
2b0143b5 DH |
2323 | struct inode *old_inode = d_inode(old_dentry); |
2324 | struct inode *new_inode = d_inode(new_dentry); | |
d9f29500 | 2325 | struct dentry *dentry = NULL, *rehash = NULL; |
80a491fd | 2326 | struct rpc_task *task; |
1da177e4 LT |
2327 | int error = -EBUSY; |
2328 | ||
1cd66c93 MS |
2329 | if (flags) |
2330 | return -EINVAL; | |
2331 | ||
6de1472f AV |
2332 | dfprintk(VFS, "NFS: rename(%pd2 -> %pd2, ct=%d)\n", |
2333 | old_dentry, new_dentry, | |
84d08fa8 | 2334 | d_count(new_dentry)); |
1da177e4 | 2335 | |
70ded201 | 2336 | trace_nfs_rename_enter(old_dir, old_dentry, new_dir, new_dentry); |
1da177e4 | 2337 | /* |
28f79a1a MS |
2338 | * For non-directories, check whether the target is busy and if so, |
2339 | * make a copy of the dentry and then do a silly-rename. If the | |
2340 | * silly-rename succeeds, the copied dentry is hashed and becomes | |
2341 | * the new target. | |
1da177e4 | 2342 | */ |
27226104 MS |
2343 | if (new_inode && !S_ISDIR(new_inode->i_mode)) { |
2344 | /* | |
2345 | * To prevent any new references to the target during the | |
2346 | * rename, we unhash the dentry in advance. | |
2347 | */ | |
d9f29500 | 2348 | if (!d_unhashed(new_dentry)) { |
27226104 | 2349 | d_drop(new_dentry); |
d9f29500 BC |
2350 | rehash = new_dentry; |
2351 | } | |
1da177e4 | 2352 | |
84d08fa8 | 2353 | if (d_count(new_dentry) > 2) { |
27226104 MS |
2354 | int err; |
2355 | ||
2356 | /* copy the target dentry's name */ | |
2357 | dentry = d_alloc(new_dentry->d_parent, | |
2358 | &new_dentry->d_name); | |
2359 | if (!dentry) | |
2360 | goto out; | |
2361 | ||
2362 | /* silly-rename the existing target ... */ | |
2363 | err = nfs_sillyrename(new_dir, new_dentry); | |
24e93025 | 2364 | if (err) |
27226104 | 2365 | goto out; |
24e93025 MS |
2366 | |
2367 | new_dentry = dentry; | |
d9f29500 | 2368 | rehash = NULL; |
24e93025 | 2369 | new_inode = NULL; |
27226104 | 2370 | } |
b1e4adf4 | 2371 | } |
1da177e4 | 2372 | |
d9f29500 | 2373 | task = nfs_async_rename(old_dir, new_dir, old_dentry, new_dentry, NULL); |
80a491fd JL |
2374 | if (IS_ERR(task)) { |
2375 | error = PTR_ERR(task); | |
2376 | goto out; | |
2377 | } | |
2378 | ||
2379 | error = rpc_wait_for_completion_task(task); | |
818a8dbe BC |
2380 | if (error != 0) { |
2381 | ((struct nfs_renamedata *)task->tk_calldata)->cancelled = 1; | |
2382 | /* Paired with the atomic_dec_and_test() barrier in rpc_do_put_task() */ | |
2383 | smp_wmb(); | |
2384 | } else | |
80a491fd JL |
2385 | error = task->tk_status; |
2386 | rpc_put_task(task); | |
59a707b0 TM |
2387 | /* Ensure the inode attributes are revalidated */ |
2388 | if (error == 0) { | |
2389 | spin_lock(&old_inode->i_lock); | |
2390 | NFS_I(old_inode)->attr_gencount = nfs_inc_attr_generation_counter(); | |
2391 | NFS_I(old_inode)->cache_validity |= NFS_INO_INVALID_CHANGE | |
2392 | | NFS_INO_INVALID_CTIME | |
2393 | | NFS_INO_REVAL_FORCED; | |
2394 | spin_unlock(&old_inode->i_lock); | |
2395 | } | |
1da177e4 | 2396 | out: |
d9f29500 BC |
2397 | if (rehash) |
2398 | d_rehash(rehash); | |
70ded201 TM |
2399 | trace_nfs_rename_exit(old_dir, old_dentry, |
2400 | new_dir, new_dentry, error); | |
d9f29500 BC |
2401 | if (!error) { |
2402 | if (new_inode != NULL) | |
2403 | nfs_drop_nlink(new_inode); | |
2404 | /* | |
2405 | * The d_move() should be here instead of in an async RPC completion | |
2406 | * handler because we need the proper locks to move the dentry. If | |
2407 | * we're interrupted by a signal, the async RPC completion handler | |
2408 | * should mark the directories for revalidation. | |
2409 | */ | |
2410 | d_move(old_dentry, new_dentry); | |
d803224c | 2411 | nfs_set_verifier(old_dentry, |
d9f29500 BC |
2412 | nfs_save_change_attribute(new_dir)); |
2413 | } else if (error == -ENOENT) | |
2414 | nfs_dentry_handle_enoent(old_dentry); | |
2415 | ||
1da177e4 LT |
2416 | /* new dentry created? */ |
2417 | if (dentry) | |
2418 | dput(dentry); | |
1da177e4 LT |
2419 | return error; |
2420 | } | |
ddda8e0a | 2421 | EXPORT_SYMBOL_GPL(nfs_rename); |
1da177e4 | 2422 | |
cfcea3e8 TM |
2423 | static DEFINE_SPINLOCK(nfs_access_lru_lock); |
2424 | static LIST_HEAD(nfs_access_lru_list); | |
2425 | static atomic_long_t nfs_access_nr_entries; | |
2426 | ||
a8b373ee | 2427 | static unsigned long nfs_access_max_cachesize = 4*1024*1024; |
3a505845 TM |
2428 | module_param(nfs_access_max_cachesize, ulong, 0644); |
2429 | MODULE_PARM_DESC(nfs_access_max_cachesize, "NFS access maximum total cache length"); | |
2430 | ||
1c3c07e9 TM |
2431 | static void nfs_access_free_entry(struct nfs_access_entry *entry) |
2432 | { | |
b68572e0 | 2433 | put_cred(entry->cred); |
f682a398 | 2434 | kfree_rcu(entry, rcu_head); |
4e857c58 | 2435 | smp_mb__before_atomic(); |
cfcea3e8 | 2436 | atomic_long_dec(&nfs_access_nr_entries); |
4e857c58 | 2437 | smp_mb__after_atomic(); |
1c3c07e9 TM |
2438 | } |
2439 | ||
1a81bb8a TM |
2440 | static void nfs_access_free_list(struct list_head *head) |
2441 | { | |
2442 | struct nfs_access_entry *cache; | |
2443 | ||
2444 | while (!list_empty(head)) { | |
2445 | cache = list_entry(head->next, struct nfs_access_entry, lru); | |
2446 | list_del(&cache->lru); | |
2447 | nfs_access_free_entry(cache); | |
2448 | } | |
2449 | } | |
2450 | ||
3a505845 TM |
2451 | static unsigned long |
2452 | nfs_do_access_cache_scan(unsigned int nr_to_scan) | |
979df72e TM |
2453 | { |
2454 | LIST_HEAD(head); | |
aa510da5 | 2455 | struct nfs_inode *nfsi, *next; |
979df72e | 2456 | struct nfs_access_entry *cache; |
1ab6c499 | 2457 | long freed = 0; |
979df72e | 2458 | |
a50f7951 | 2459 | spin_lock(&nfs_access_lru_lock); |
aa510da5 | 2460 | list_for_each_entry_safe(nfsi, next, &nfs_access_lru_list, access_cache_inode_lru) { |
979df72e TM |
2461 | struct inode *inode; |
2462 | ||
2463 | if (nr_to_scan-- == 0) | |
2464 | break; | |
9c7e7e23 | 2465 | inode = &nfsi->vfs_inode; |
979df72e TM |
2466 | spin_lock(&inode->i_lock); |
2467 | if (list_empty(&nfsi->access_cache_entry_lru)) | |
2468 | goto remove_lru_entry; | |
2469 | cache = list_entry(nfsi->access_cache_entry_lru.next, | |
2470 | struct nfs_access_entry, lru); | |
2471 | list_move(&cache->lru, &head); | |
2472 | rb_erase(&cache->rb_node, &nfsi->access_cache); | |
1ab6c499 | 2473 | freed++; |
979df72e TM |
2474 | if (!list_empty(&nfsi->access_cache_entry_lru)) |
2475 | list_move_tail(&nfsi->access_cache_inode_lru, | |
2476 | &nfs_access_lru_list); | |
2477 | else { | |
2478 | remove_lru_entry: | |
2479 | list_del_init(&nfsi->access_cache_inode_lru); | |
4e857c58 | 2480 | smp_mb__before_atomic(); |
979df72e | 2481 | clear_bit(NFS_INO_ACL_LRU_SET, &nfsi->flags); |
4e857c58 | 2482 | smp_mb__after_atomic(); |
979df72e | 2483 | } |
59844a9b | 2484 | spin_unlock(&inode->i_lock); |
979df72e TM |
2485 | } |
2486 | spin_unlock(&nfs_access_lru_lock); | |
1a81bb8a | 2487 | nfs_access_free_list(&head); |
1ab6c499 DC |
2488 | return freed; |
2489 | } | |
2490 | ||
3a505845 TM |
2491 | unsigned long |
2492 | nfs_access_cache_scan(struct shrinker *shrink, struct shrink_control *sc) | |
2493 | { | |
2494 | int nr_to_scan = sc->nr_to_scan; | |
2495 | gfp_t gfp_mask = sc->gfp_mask; | |
2496 | ||
2497 | if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL) | |
2498 | return SHRINK_STOP; | |
2499 | return nfs_do_access_cache_scan(nr_to_scan); | |
2500 | } | |
2501 | ||
2502 | ||
1ab6c499 DC |
2503 | unsigned long |
2504 | nfs_access_cache_count(struct shrinker *shrink, struct shrink_control *sc) | |
2505 | { | |
55f841ce | 2506 | return vfs_pressure_ratio(atomic_long_read(&nfs_access_nr_entries)); |
979df72e TM |
2507 | } |
2508 | ||
3a505845 TM |
2509 | static void |
2510 | nfs_access_cache_enforce_limit(void) | |
2511 | { | |
2512 | long nr_entries = atomic_long_read(&nfs_access_nr_entries); | |
2513 | unsigned long diff; | |
2514 | unsigned int nr_to_scan; | |
2515 | ||
2516 | if (nr_entries < 0 || nr_entries <= nfs_access_max_cachesize) | |
2517 | return; | |
2518 | nr_to_scan = 100; | |
2519 | diff = nr_entries - nfs_access_max_cachesize; | |
2520 | if (diff < nr_to_scan) | |
2521 | nr_to_scan = diff; | |
2522 | nfs_do_access_cache_scan(nr_to_scan); | |
2523 | } | |
2524 | ||
1a81bb8a | 2525 | static void __nfs_access_zap_cache(struct nfs_inode *nfsi, struct list_head *head) |
1da177e4 | 2526 | { |
1c3c07e9 | 2527 | struct rb_root *root_node = &nfsi->access_cache; |
1a81bb8a | 2528 | struct rb_node *n; |
1c3c07e9 TM |
2529 | struct nfs_access_entry *entry; |
2530 | ||
2531 | /* Unhook entries from the cache */ | |
2532 | while ((n = rb_first(root_node)) != NULL) { | |
2533 | entry = rb_entry(n, struct nfs_access_entry, rb_node); | |
2534 | rb_erase(n, root_node); | |
1a81bb8a | 2535 | list_move(&entry->lru, head); |
1c3c07e9 TM |
2536 | } |
2537 | nfsi->cache_validity &= ~NFS_INO_INVALID_ACCESS; | |
1da177e4 LT |
2538 | } |
2539 | ||
1c3c07e9 | 2540 | void nfs_access_zap_cache(struct inode *inode) |
1da177e4 | 2541 | { |
1a81bb8a TM |
2542 | LIST_HEAD(head); |
2543 | ||
2544 | if (test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags) == 0) | |
2545 | return; | |
cfcea3e8 | 2546 | /* Remove from global LRU init */ |
1a81bb8a TM |
2547 | spin_lock(&nfs_access_lru_lock); |
2548 | if (test_and_clear_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) | |
cfcea3e8 | 2549 | list_del_init(&NFS_I(inode)->access_cache_inode_lru); |
cfcea3e8 | 2550 | |
1c3c07e9 | 2551 | spin_lock(&inode->i_lock); |
1a81bb8a TM |
2552 | __nfs_access_zap_cache(NFS_I(inode), &head); |
2553 | spin_unlock(&inode->i_lock); | |
2554 | spin_unlock(&nfs_access_lru_lock); | |
2555 | nfs_access_free_list(&head); | |
1c3c07e9 | 2556 | } |
1c606fb7 | 2557 | EXPORT_SYMBOL_GPL(nfs_access_zap_cache); |
1da177e4 | 2558 | |
b68572e0 | 2559 | static struct nfs_access_entry *nfs_access_search_rbtree(struct inode *inode, const struct cred *cred) |
1c3c07e9 TM |
2560 | { |
2561 | struct rb_node *n = NFS_I(inode)->access_cache.rb_node; | |
1c3c07e9 TM |
2562 | |
2563 | while (n != NULL) { | |
b68572e0 N |
2564 | struct nfs_access_entry *entry = |
2565 | rb_entry(n, struct nfs_access_entry, rb_node); | |
2566 | int cmp = cred_fscmp(cred, entry->cred); | |
1c3c07e9 | 2567 | |
b68572e0 | 2568 | if (cmp < 0) |
1c3c07e9 | 2569 | n = n->rb_left; |
b68572e0 | 2570 | else if (cmp > 0) |
1c3c07e9 TM |
2571 | n = n->rb_right; |
2572 | else | |
2573 | return entry; | |
1da177e4 | 2574 | } |
1c3c07e9 TM |
2575 | return NULL; |
2576 | } | |
2577 | ||
d2ae4f8b | 2578 | static int nfs_access_get_cached_locked(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res, bool may_block) |
1c3c07e9 TM |
2579 | { |
2580 | struct nfs_inode *nfsi = NFS_I(inode); | |
2581 | struct nfs_access_entry *cache; | |
57b69181 TM |
2582 | bool retry = true; |
2583 | int err; | |
1c3c07e9 | 2584 | |
dc59250c | 2585 | spin_lock(&inode->i_lock); |
57b69181 TM |
2586 | for(;;) { |
2587 | if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) | |
2588 | goto out_zap; | |
2589 | cache = nfs_access_search_rbtree(inode, cred); | |
2590 | err = -ENOENT; | |
2591 | if (cache == NULL) | |
2592 | goto out; | |
2593 | /* Found an entry, is our attribute cache valid? */ | |
21c3ba7e | 2594 | if (!nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS)) |
57b69181 | 2595 | break; |
5c965db8 TM |
2596 | if (!retry) |
2597 | break; | |
57b69181 TM |
2598 | err = -ECHILD; |
2599 | if (!may_block) | |
2600 | goto out; | |
57b69181 TM |
2601 | spin_unlock(&inode->i_lock); |
2602 | err = __nfs_revalidate_inode(NFS_SERVER(inode), inode); | |
2603 | if (err) | |
2604 | return err; | |
2605 | spin_lock(&inode->i_lock); | |
2606 | retry = false; | |
2607 | } | |
1c3c07e9 TM |
2608 | res->cred = cache->cred; |
2609 | res->mask = cache->mask; | |
cfcea3e8 | 2610 | list_move_tail(&cache->lru, &nfsi->access_cache_entry_lru); |
1c3c07e9 TM |
2611 | err = 0; |
2612 | out: | |
2613 | spin_unlock(&inode->i_lock); | |
2614 | return err; | |
1c3c07e9 | 2615 | out_zap: |
1a81bb8a TM |
2616 | spin_unlock(&inode->i_lock); |
2617 | nfs_access_zap_cache(inode); | |
1c3c07e9 TM |
2618 | return -ENOENT; |
2619 | } | |
2620 | ||
b68572e0 | 2621 | static int nfs_access_get_cached_rcu(struct inode *inode, const struct cred *cred, struct nfs_access_entry *res) |
f682a398 N |
2622 | { |
2623 | /* Only check the most recently returned cache entry, | |
2624 | * but do it without locking. | |
2625 | */ | |
2626 | struct nfs_inode *nfsi = NFS_I(inode); | |
2627 | struct nfs_access_entry *cache; | |
2628 | int err = -ECHILD; | |
2629 | struct list_head *lh; | |
2630 | ||
2631 | rcu_read_lock(); | |
2632 | if (nfsi->cache_validity & NFS_INO_INVALID_ACCESS) | |
2633 | goto out; | |
9f01eb5d | 2634 | lh = rcu_dereference(list_tail_rcu(&nfsi->access_cache_entry_lru)); |
f682a398 N |
2635 | cache = list_entry(lh, struct nfs_access_entry, lru); |
2636 | if (lh == &nfsi->access_cache_entry_lru || | |
9a206de2 | 2637 | cred_fscmp(cred, cache->cred) != 0) |
f682a398 N |
2638 | cache = NULL; |
2639 | if (cache == NULL) | |
2640 | goto out; | |
21c3ba7e | 2641 | if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_ACCESS)) |
f682a398 | 2642 | goto out; |
f682a398 N |
2643 | res->cred = cache->cred; |
2644 | res->mask = cache->mask; | |
21c3ba7e | 2645 | err = 0; |
f682a398 N |
2646 | out: |
2647 | rcu_read_unlock(); | |
2648 | return err; | |
2649 | } | |
2650 | ||
d2ae4f8b FL |
2651 | int nfs_access_get_cached(struct inode *inode, const struct cred *cred, struct |
2652 | nfs_access_entry *res, bool may_block) | |
2653 | { | |
2654 | int status; | |
2655 | ||
2656 | status = nfs_access_get_cached_rcu(inode, cred, res); | |
2657 | if (status != 0) | |
2658 | status = nfs_access_get_cached_locked(inode, cred, res, | |
2659 | may_block); | |
2660 | ||
2661 | return status; | |
2662 | } | |
2663 | EXPORT_SYMBOL_GPL(nfs_access_get_cached); | |
2664 | ||
1c3c07e9 TM |
2665 | static void nfs_access_add_rbtree(struct inode *inode, struct nfs_access_entry *set) |
2666 | { | |
cfcea3e8 TM |
2667 | struct nfs_inode *nfsi = NFS_I(inode); |
2668 | struct rb_root *root_node = &nfsi->access_cache; | |
1c3c07e9 TM |
2669 | struct rb_node **p = &root_node->rb_node; |
2670 | struct rb_node *parent = NULL; | |
2671 | struct nfs_access_entry *entry; | |
b68572e0 | 2672 | int cmp; |
1c3c07e9 TM |
2673 | |
2674 | spin_lock(&inode->i_lock); | |
2675 | while (*p != NULL) { | |
2676 | parent = *p; | |
2677 | entry = rb_entry(parent, struct nfs_access_entry, rb_node); | |
b68572e0 | 2678 | cmp = cred_fscmp(set->cred, entry->cred); |
1c3c07e9 | 2679 | |
b68572e0 | 2680 | if (cmp < 0) |
1c3c07e9 | 2681 | p = &parent->rb_left; |
b68572e0 | 2682 | else if (cmp > 0) |
1c3c07e9 TM |
2683 | p = &parent->rb_right; |
2684 | else | |
2685 | goto found; | |
2686 | } | |
2687 | rb_link_node(&set->rb_node, parent, p); | |
2688 | rb_insert_color(&set->rb_node, root_node); | |
cfcea3e8 | 2689 | list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); |
dc59250c | 2690 | spin_unlock(&inode->i_lock); |
1c3c07e9 TM |
2691 | return; |
2692 | found: | |
2693 | rb_replace_node(parent, &set->rb_node, root_node); | |
cfcea3e8 TM |
2694 | list_add_tail(&set->lru, &nfsi->access_cache_entry_lru); |
2695 | list_del(&entry->lru); | |
1c3c07e9 TM |
2696 | spin_unlock(&inode->i_lock); |
2697 | nfs_access_free_entry(entry); | |
2698 | } | |
2699 | ||
6168f62c | 2700 | void nfs_access_add_cache(struct inode *inode, struct nfs_access_entry *set) |
1c3c07e9 TM |
2701 | { |
2702 | struct nfs_access_entry *cache = kmalloc(sizeof(*cache), GFP_KERNEL); | |
2703 | if (cache == NULL) | |
2704 | return; | |
2705 | RB_CLEAR_NODE(&cache->rb_node); | |
b68572e0 | 2706 | cache->cred = get_cred(set->cred); |
1da177e4 | 2707 | cache->mask = set->mask; |
1c3c07e9 | 2708 | |
f682a398 N |
2709 | /* The above field assignments must be visible |
2710 | * before this item appears on the lru. We cannot easily | |
2711 | * use rcu_assign_pointer, so just force the memory barrier. | |
2712 | */ | |
2713 | smp_wmb(); | |
1c3c07e9 | 2714 | nfs_access_add_rbtree(inode, cache); |
cfcea3e8 TM |
2715 | |
2716 | /* Update accounting */ | |
4e857c58 | 2717 | smp_mb__before_atomic(); |
cfcea3e8 | 2718 | atomic_long_inc(&nfs_access_nr_entries); |
4e857c58 | 2719 | smp_mb__after_atomic(); |
cfcea3e8 TM |
2720 | |
2721 | /* Add inode to global LRU list */ | |
1a81bb8a | 2722 | if (!test_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) { |
cfcea3e8 | 2723 | spin_lock(&nfs_access_lru_lock); |
1a81bb8a TM |
2724 | if (!test_and_set_bit(NFS_INO_ACL_LRU_SET, &NFS_I(inode)->flags)) |
2725 | list_add_tail(&NFS_I(inode)->access_cache_inode_lru, | |
2726 | &nfs_access_lru_list); | |
cfcea3e8 TM |
2727 | spin_unlock(&nfs_access_lru_lock); |
2728 | } | |
3a505845 | 2729 | nfs_access_cache_enforce_limit(); |
1da177e4 | 2730 | } |
6168f62c WAA |
2731 | EXPORT_SYMBOL_GPL(nfs_access_add_cache); |
2732 | ||
3c181827 AS |
2733 | #define NFS_MAY_READ (NFS_ACCESS_READ) |
2734 | #define NFS_MAY_WRITE (NFS_ACCESS_MODIFY | \ | |
2735 | NFS_ACCESS_EXTEND | \ | |
2736 | NFS_ACCESS_DELETE) | |
2737 | #define NFS_FILE_MAY_WRITE (NFS_ACCESS_MODIFY | \ | |
2738 | NFS_ACCESS_EXTEND) | |
ecbb903c | 2739 | #define NFS_DIR_MAY_WRITE NFS_MAY_WRITE |
3c181827 AS |
2740 | #define NFS_MAY_LOOKUP (NFS_ACCESS_LOOKUP) |
2741 | #define NFS_MAY_EXECUTE (NFS_ACCESS_EXECUTE) | |
15d4b73a | 2742 | static int |
ecbb903c | 2743 | nfs_access_calc_mask(u32 access_result, umode_t umode) |
15d4b73a TM |
2744 | { |
2745 | int mask = 0; | |
2746 | ||
2747 | if (access_result & NFS_MAY_READ) | |
2748 | mask |= MAY_READ; | |
ecbb903c TM |
2749 | if (S_ISDIR(umode)) { |
2750 | if ((access_result & NFS_DIR_MAY_WRITE) == NFS_DIR_MAY_WRITE) | |
2751 | mask |= MAY_WRITE; | |
2752 | if ((access_result & NFS_MAY_LOOKUP) == NFS_MAY_LOOKUP) | |
2753 | mask |= MAY_EXEC; | |
2754 | } else if (S_ISREG(umode)) { | |
2755 | if ((access_result & NFS_FILE_MAY_WRITE) == NFS_FILE_MAY_WRITE) | |
2756 | mask |= MAY_WRITE; | |
2757 | if ((access_result & NFS_MAY_EXECUTE) == NFS_MAY_EXECUTE) | |
2758 | mask |= MAY_EXEC; | |
2759 | } else if (access_result & NFS_MAY_WRITE) | |
2760 | mask |= MAY_WRITE; | |
15d4b73a TM |
2761 | return mask; |
2762 | } | |
2763 | ||
6168f62c WAA |
2764 | void nfs_access_set_mask(struct nfs_access_entry *entry, u32 access_result) |
2765 | { | |
bd8b2441 | 2766 | entry->mask = access_result; |
6168f62c WAA |
2767 | } |
2768 | EXPORT_SYMBOL_GPL(nfs_access_set_mask); | |
1da177e4 | 2769 | |
b68572e0 | 2770 | static int nfs_do_access(struct inode *inode, const struct cred *cred, int mask) |
1da177e4 LT |
2771 | { |
2772 | struct nfs_access_entry cache; | |
57b69181 | 2773 | bool may_block = (mask & MAY_NOT_BLOCK) == 0; |
e8194b7d | 2774 | int cache_mask = -1; |
1da177e4 LT |
2775 | int status; |
2776 | ||
f4ce1299 TM |
2777 | trace_nfs_access_enter(inode); |
2778 | ||
d2ae4f8b | 2779 | status = nfs_access_get_cached(inode, cred, &cache, may_block); |
1da177e4 | 2780 | if (status == 0) |
f4ce1299 | 2781 | goto out_cached; |
1da177e4 | 2782 | |
f3324a2a | 2783 | status = -ECHILD; |
57b69181 | 2784 | if (!may_block) |
f3324a2a N |
2785 | goto out; |
2786 | ||
1750d929 AS |
2787 | /* |
2788 | * Determine which access bits we want to ask for... | |
2789 | */ | |
2790 | cache.mask = NFS_ACCESS_READ | NFS_ACCESS_MODIFY | NFS_ACCESS_EXTEND; | |
72832a24 FL |
2791 | if (nfs_server_capable(inode, NFS_CAP_XATTR)) { |
2792 | cache.mask |= NFS_ACCESS_XAREAD | NFS_ACCESS_XAWRITE | | |
2793 | NFS_ACCESS_XALIST; | |
2794 | } | |
1750d929 AS |
2795 | if (S_ISDIR(inode->i_mode)) |
2796 | cache.mask |= NFS_ACCESS_DELETE | NFS_ACCESS_LOOKUP; | |
2797 | else | |
2798 | cache.mask |= NFS_ACCESS_EXECUTE; | |
1da177e4 | 2799 | cache.cred = cred; |
1da177e4 | 2800 | status = NFS_PROTO(inode)->access(inode, &cache); |
a71ee337 SJ |
2801 | if (status != 0) { |
2802 | if (status == -ESTALE) { | |
a71ee337 | 2803 | if (!S_ISDIR(inode->i_mode)) |
93ce4af7 TM |
2804 | nfs_set_inode_stale(inode); |
2805 | else | |
2806 | nfs_zap_caches(inode); | |
a71ee337 | 2807 | } |
f4ce1299 | 2808 | goto out; |
a71ee337 | 2809 | } |
1da177e4 | 2810 | nfs_access_add_cache(inode, &cache); |
f4ce1299 | 2811 | out_cached: |
ecbb903c | 2812 | cache_mask = nfs_access_calc_mask(cache.mask, inode->i_mode); |
bd8b2441 | 2813 | if ((mask & ~cache_mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) != 0) |
f4ce1299 | 2814 | status = -EACCES; |
1da177e4 | 2815 | out: |
e8194b7d | 2816 | trace_nfs_access_exit(inode, mask, cache_mask, status); |
f4ce1299 | 2817 | return status; |
1da177e4 LT |
2818 | } |
2819 | ||
af22f94a TM |
2820 | static int nfs_open_permission_mask(int openflags) |
2821 | { | |
2822 | int mask = 0; | |
2823 | ||
f8d9a897 WAA |
2824 | if (openflags & __FMODE_EXEC) { |
2825 | /* ONLY check exec rights */ | |
2826 | mask = MAY_EXEC; | |
2827 | } else { | |
2828 | if ((openflags & O_ACCMODE) != O_WRONLY) | |
2829 | mask |= MAY_READ; | |
2830 | if ((openflags & O_ACCMODE) != O_RDONLY) | |
2831 | mask |= MAY_WRITE; | |
2832 | } | |
2833 | ||
af22f94a TM |
2834 | return mask; |
2835 | } | |
2836 | ||
b68572e0 | 2837 | int nfs_may_open(struct inode *inode, const struct cred *cred, int openflags) |
af22f94a TM |
2838 | { |
2839 | return nfs_do_access(inode, cred, nfs_open_permission_mask(openflags)); | |
2840 | } | |
89d77c8f | 2841 | EXPORT_SYMBOL_GPL(nfs_may_open); |
af22f94a | 2842 | |
5c5fc09a TM |
2843 | static int nfs_execute_ok(struct inode *inode, int mask) |
2844 | { | |
2845 | struct nfs_server *server = NFS_SERVER(inode); | |
21c3ba7e | 2846 | int ret = 0; |
5c5fc09a | 2847 | |
3825827e TM |
2848 | if (S_ISDIR(inode->i_mode)) |
2849 | return 0; | |
cf834027 | 2850 | if (nfs_check_cache_invalid(inode, NFS_INO_INVALID_OTHER)) { |
21c3ba7e TM |
2851 | if (mask & MAY_NOT_BLOCK) |
2852 | return -ECHILD; | |
2853 | ret = __nfs_revalidate_inode(server, inode); | |
2854 | } | |
5c5fc09a TM |
2855 | if (ret == 0 && !execute_ok(inode)) |
2856 | ret = -EACCES; | |
2857 | return ret; | |
2858 | } | |
2859 | ||
10556cb2 | 2860 | int nfs_permission(struct inode *inode, int mask) |
1da177e4 | 2861 | { |
b68572e0 | 2862 | const struct cred *cred = current_cred(); |
1da177e4 LT |
2863 | int res = 0; |
2864 | ||
91d5b470 CL |
2865 | nfs_inc_stats(inode, NFSIOS_VFSACCESS); |
2866 | ||
e6305c43 | 2867 | if ((mask & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) |
1da177e4 LT |
2868 | goto out; |
2869 | /* Is this sys_access() ? */ | |
9cfcac81 | 2870 | if (mask & (MAY_ACCESS | MAY_CHDIR)) |
1da177e4 LT |
2871 | goto force_lookup; |
2872 | ||
2873 | switch (inode->i_mode & S_IFMT) { | |
2874 | case S_IFLNK: | |
2875 | goto out; | |
2876 | case S_IFREG: | |
762674f8 TM |
2877 | if ((mask & MAY_OPEN) && |
2878 | nfs_server_capable(inode, NFS_CAP_ATOMIC_OPEN)) | |
2879 | return 0; | |
1da177e4 LT |
2880 | break; |
2881 | case S_IFDIR: | |
2882 | /* | |
2883 | * Optimize away all write operations, since the server | |
2884 | * will check permissions when we perform the op. | |
2885 | */ | |
2886 | if ((mask & MAY_WRITE) && !(mask & MAY_READ)) | |
2887 | goto out; | |
2888 | } | |
2889 | ||
2890 | force_lookup: | |
1da177e4 LT |
2891 | if (!NFS_PROTO(inode)->access) |
2892 | goto out_notsup; | |
2893 | ||
eb095c14 | 2894 | res = nfs_do_access(inode, cred, mask); |
1da177e4 | 2895 | out: |
5c5fc09a TM |
2896 | if (!res && (mask & MAY_EXEC)) |
2897 | res = nfs_execute_ok(inode, mask); | |
f696a365 | 2898 | |
1e8968c5 | 2899 | dfprintk(VFS, "NFS: permission(%s/%lu), mask=0x%x, res=%d\n", |
1e7cb3dc | 2900 | inode->i_sb->s_id, inode->i_ino, mask, res); |
1da177e4 LT |
2901 | return res; |
2902 | out_notsup: | |
d51ac1a8 N |
2903 | if (mask & MAY_NOT_BLOCK) |
2904 | return -ECHILD; | |
2905 | ||
1da177e4 LT |
2906 | res = nfs_revalidate_inode(NFS_SERVER(inode), inode); |
2907 | if (res == 0) | |
2830ba7f | 2908 | res = generic_permission(inode, mask); |
1e7cb3dc | 2909 | goto out; |
1da177e4 | 2910 | } |
ddda8e0a | 2911 | EXPORT_SYMBOL_GPL(nfs_permission); |
1da177e4 LT |
2912 | |
2913 | /* | |
2914 | * Local variables: | |
2915 | * version-control: t | |
2916 | * kept-new-versions: 5 | |
2917 | * End: | |
2918 | */ |