]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - fs/afs/dir.c
afs: Handle EIO from delivery function
[mirror_ubuntu-eoan-kernel.git] / fs / afs / dir.c
CommitLineData
1da177e4
LT
1/* dir.c: AFS filesystem directory handling
2 *
f3ddee8d 3 * Copyright (C) 2002, 2018 Red Hat, Inc. All Rights Reserved.
1da177e4
LT
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12#include <linux/kernel.h>
1da177e4 13#include <linux/fs.h>
34286d66 14#include <linux/namei.h>
1da177e4 15#include <linux/pagemap.h>
f3ddee8d 16#include <linux/swap.h>
00d3b7a4 17#include <linux/ctype.h>
e8edc6e0 18#include <linux/sched.h>
f3ddee8d 19#include <linux/task_io_accounting_ops.h>
1da177e4 20#include "internal.h"
4ea219a8 21#include "xdr_fs.h"
1da177e4 22
260a9803 23static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
00cd8dd3 24 unsigned int flags);
1da177e4 25static int afs_dir_open(struct inode *inode, struct file *file);
1bbae9f8 26static int afs_readdir(struct file *file, struct dir_context *ctx);
0b728e19 27static int afs_d_revalidate(struct dentry *dentry, unsigned int flags);
fe15ce44 28static int afs_d_delete(const struct dentry *dentry);
5cf9dd55 29static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name, int nlen,
afefdbb2 30 loff_t fpos, u64 ino, unsigned dtype);
5cf9dd55
DH
31static int afs_lookup_filldir(struct dir_context *ctx, const char *name, int nlen,
32 loff_t fpos, u64 ino, unsigned dtype);
4acdaf27 33static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
ebfc3b49 34 bool excl);
18bb1db3 35static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
260a9803
DH
36static int afs_rmdir(struct inode *dir, struct dentry *dentry);
37static int afs_unlink(struct inode *dir, struct dentry *dentry);
38static int afs_link(struct dentry *from, struct inode *dir,
39 struct dentry *dentry);
40static int afs_symlink(struct inode *dir, struct dentry *dentry,
41 const char *content);
42static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
1cd66c93
MS
43 struct inode *new_dir, struct dentry *new_dentry,
44 unsigned int flags);
f3ddee8d
DH
45static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags);
46static void afs_dir_invalidatepage(struct page *page, unsigned int offset,
47 unsigned int length);
48
49static int afs_dir_set_page_dirty(struct page *page)
50{
51 BUG(); /* This should never happen. */
52}
1da177e4 53
4b6f5d20 54const struct file_operations afs_dir_file_operations = {
1da177e4 55 .open = afs_dir_open,
00d3b7a4 56 .release = afs_release,
29884eff 57 .iterate_shared = afs_readdir,
e8d6c554 58 .lock = afs_lock,
3222a3e5 59 .llseek = generic_file_llseek,
1da177e4
LT
60};
61
754661f1 62const struct inode_operations afs_dir_inode_operations = {
260a9803
DH
63 .create = afs_create,
64 .lookup = afs_lookup,
65 .link = afs_link,
66 .unlink = afs_unlink,
67 .symlink = afs_symlink,
68 .mkdir = afs_mkdir,
69 .rmdir = afs_rmdir,
2773bf00 70 .rename = afs_rename,
00d3b7a4 71 .permission = afs_permission,
416351f2 72 .getattr = afs_getattr,
31143d5d 73 .setattr = afs_setattr,
d3e3b7ea 74 .listxattr = afs_listxattr,
1da177e4
LT
75};
76
f3ddee8d
DH
77const struct address_space_operations afs_dir_aops = {
78 .set_page_dirty = afs_dir_set_page_dirty,
79 .releasepage = afs_dir_releasepage,
80 .invalidatepage = afs_dir_invalidatepage,
81};
82
d61dcce2 83const struct dentry_operations afs_fs_dentry_operations = {
1da177e4
LT
84 .d_revalidate = afs_d_revalidate,
85 .d_delete = afs_d_delete,
260a9803 86 .d_release = afs_d_release,
d18610b0 87 .d_automount = afs_d_automount,
1da177e4
LT
88};
89
5cf9dd55
DH
90struct afs_lookup_one_cookie {
91 struct dir_context ctx;
92 struct qstr name;
93 bool found;
94 struct afs_fid fid;
95};
96
260a9803 97struct afs_lookup_cookie {
5cf9dd55
DH
98 struct dir_context ctx;
99 struct qstr name;
100 bool found;
101 bool one_only;
102 unsigned short nr_fids;
103 struct afs_file_status *statuses;
104 struct afs_callback *callbacks;
105 struct afs_fid fids[50];
1da177e4
LT
106};
107
1da177e4
LT
108/*
109 * check that a directory page is valid
110 */
f3ddee8d
DH
111static bool afs_dir_check_page(struct afs_vnode *dvnode, struct page *page,
112 loff_t i_size)
1da177e4 113{
00317636 114 struct afs_xdr_dir_page *dbuf;
f3ddee8d 115 loff_t latter, off;
1da177e4
LT
116 int tmp, qty;
117
dab17c1a
DH
118 /* Determine how many magic numbers there should be in this page, but
119 * we must take care because the directory may change size under us.
120 */
121 off = page_offset(page);
dab17c1a
DH
122 if (i_size <= off)
123 goto checked;
124
125 latter = i_size - off;
1da177e4
LT
126 if (latter >= PAGE_SIZE)
127 qty = PAGE_SIZE;
128 else
129 qty = latter;
00317636 130 qty /= sizeof(union afs_xdr_dir_block);
1da177e4
LT
131
132 /* check them */
63a4681f 133 dbuf = kmap(page);
1da177e4 134 for (tmp = 0; tmp < qty; tmp++) {
00317636 135 if (dbuf->blocks[tmp].hdr.magic != AFS_DIR_MAGIC) {
dab17c1a 136 printk("kAFS: %s(%lx): bad magic %d/%d is %04hx\n",
f3ddee8d 137 __func__, dvnode->vfs_inode.i_ino, tmp, qty,
00317636 138 ntohs(dbuf->blocks[tmp].hdr.magic));
f3ddee8d 139 trace_afs_dir_check_failed(dvnode, off, i_size);
63a4681f 140 kunmap(page);
1da177e4
LT
141 goto error;
142 }
63a4681f
DH
143
144 /* Make sure each block is NUL terminated so we can reasonably
145 * use string functions on it. The filenames in the page
146 * *should* be NUL-terminated anyway.
147 */
148 ((u8 *)&dbuf->blocks[tmp])[AFS_DIR_BLOCK_SIZE - 1] = 0;
1da177e4
LT
149 }
150
63a4681f
DH
151 kunmap(page);
152
dab17c1a 153checked:
f3ddee8d 154 afs_stat_v(dvnode, n_read_dir);
be5b82db 155 return true;
1da177e4 156
ec26815a 157error:
be5b82db 158 return false;
ec26815a 159}
1da177e4 160
1da177e4
LT
161/*
162 * open an AFS directory file
163 */
164static int afs_dir_open(struct inode *inode, struct file *file)
165{
166 _enter("{%lu}", inode->i_ino);
167
00317636
DH
168 BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
169 BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
1da177e4 170
08e0e7c8 171 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(inode)->flags))
1da177e4
LT
172 return -ENOENT;
173
00d3b7a4 174 return afs_open(inode, file);
ec26815a 175}
1da177e4 176
f3ddee8d
DH
177/*
178 * Read the directory into the pagecache in one go, scrubbing the previous
179 * contents. The list of pages is returned, pinning them so that they don't
180 * get reclaimed during the iteration.
181 */
182static struct afs_read *afs_read_dir(struct afs_vnode *dvnode, struct key *key)
b61f7dcf 183 __acquires(&dvnode->validate_lock)
f3ddee8d
DH
184{
185 struct afs_read *req;
186 loff_t i_size;
187 int nr_pages, nr_inline, i, n;
188 int ret = -ENOMEM;
189
190retry:
191 i_size = i_size_read(&dvnode->vfs_inode);
192 if (i_size < 2048)
193 return ERR_PTR(-EIO);
194 if (i_size > 2048 * 1024)
195 return ERR_PTR(-EFBIG);
196
197 _enter("%llu", i_size);
198
199 /* Get a request record to hold the page list. We want to hold it
200 * inline if we can, but we don't want to make an order 1 allocation.
201 */
202 nr_pages = (i_size + PAGE_SIZE - 1) / PAGE_SIZE;
203 nr_inline = nr_pages;
204 if (nr_inline > (PAGE_SIZE - sizeof(*req)) / sizeof(struct page *))
205 nr_inline = 0;
206
207 req = kzalloc(sizeof(*req) + sizeof(struct page *) * nr_inline,
208 GFP_KERNEL);
209 if (!req)
210 return ERR_PTR(-ENOMEM);
211
212 refcount_set(&req->usage, 1);
213 req->nr_pages = nr_pages;
214 req->actual_len = i_size; /* May change */
215 req->len = nr_pages * PAGE_SIZE; /* We can ask for more than there is */
216 req->data_version = dvnode->status.data_version; /* May change */
217 if (nr_inline > 0) {
218 req->pages = req->array;
219 } else {
220 req->pages = kcalloc(nr_pages, sizeof(struct page *),
221 GFP_KERNEL);
222 if (!req->pages)
223 goto error;
224 }
225
226 /* Get a list of all the pages that hold or will hold the directory
227 * content. We need to fill in any gaps that we might find where the
228 * memory reclaimer has been at work. If there are any gaps, we will
229 * need to reread the entire directory contents.
230 */
231 i = 0;
232 do {
233 n = find_get_pages_contig(dvnode->vfs_inode.i_mapping, i,
234 req->nr_pages - i,
235 req->pages + i);
236 _debug("find %u at %u/%u", n, i, req->nr_pages);
237 if (n == 0) {
238 gfp_t gfp = dvnode->vfs_inode.i_mapping->gfp_mask;
239
240 if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
241 afs_stat_v(dvnode, n_inval);
242
243 ret = -ENOMEM;
244 req->pages[i] = __page_cache_alloc(gfp);
245 if (!req->pages[i])
246 goto error;
247 ret = add_to_page_cache_lru(req->pages[i],
248 dvnode->vfs_inode.i_mapping,
249 i, gfp);
250 if (ret < 0)
251 goto error;
252
253 set_page_private(req->pages[i], 1);
254 SetPagePrivate(req->pages[i]);
255 unlock_page(req->pages[i]);
256 i++;
257 } else {
258 i += n;
259 }
260 } while (i < req->nr_pages);
261
262 /* If we're going to reload, we need to lock all the pages to prevent
263 * races.
264 */
b61f7dcf
DH
265 ret = -ERESTARTSYS;
266 if (down_read_killable(&dvnode->validate_lock) < 0)
267 goto error;
f3ddee8d 268
b61f7dcf
DH
269 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
270 goto success;
f3ddee8d 271
b61f7dcf
DH
272 up_read(&dvnode->validate_lock);
273 if (down_write_killable(&dvnode->validate_lock) < 0)
274 goto error;
275
276 if (!test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) {
f3ddee8d
DH
277 ret = afs_fetch_data(dvnode, key, req);
278 if (ret < 0)
b61f7dcf 279 goto error_unlock;
f3ddee8d
DH
280
281 task_io_account_read(PAGE_SIZE * req->nr_pages);
282
283 if (req->len < req->file_size)
284 goto content_has_grown;
285
286 /* Validate the data we just read. */
287 ret = -EIO;
288 for (i = 0; i < req->nr_pages; i++)
289 if (!afs_dir_check_page(dvnode, req->pages[i],
290 req->actual_len))
b61f7dcf 291 goto error_unlock;
f3ddee8d
DH
292
293 // TODO: Trim excess pages
294
295 set_bit(AFS_VNODE_DIR_VALID, &dvnode->flags);
296 }
297
b61f7dcf 298 downgrade_write(&dvnode->validate_lock);
f3ddee8d 299success:
f3ddee8d
DH
300 return req;
301
f3ddee8d 302error_unlock:
b61f7dcf 303 up_write(&dvnode->validate_lock);
f3ddee8d
DH
304error:
305 afs_put_read(req);
306 _leave(" = %d", ret);
307 return ERR_PTR(ret);
308
309content_has_grown:
b61f7dcf 310 up_write(&dvnode->validate_lock);
f3ddee8d
DH
311 afs_put_read(req);
312 goto retry;
313}
314
1da177e4
LT
315/*
316 * deal with one block in an AFS directory
317 */
1bbae9f8 318static int afs_dir_iterate_block(struct dir_context *ctx,
00317636 319 union afs_xdr_dir_block *block,
1bbae9f8 320 unsigned blkoff)
1da177e4 321{
00317636 322 union afs_xdr_dirent *dire;
1da177e4
LT
323 unsigned offset, next, curr;
324 size_t nlen;
1bbae9f8 325 int tmp;
1da177e4 326
1bbae9f8 327 _enter("%u,%x,%p,,",(unsigned)ctx->pos,blkoff,block);
1da177e4 328
00317636 329 curr = (ctx->pos - blkoff) / sizeof(union afs_xdr_dirent);
1da177e4
LT
330
331 /* walk through the block, an entry at a time */
4ea219a8
DH
332 for (offset = (blkoff == 0 ? AFS_DIR_RESV_BLOCKS0 : AFS_DIR_RESV_BLOCKS);
333 offset < AFS_DIR_SLOTS_PER_BLOCK;
1da177e4
LT
334 offset = next
335 ) {
336 next = offset + 1;
337
338 /* skip entries marked unused in the bitmap */
00317636 339 if (!(block->hdr.bitmap[offset / 8] &
1da177e4 340 (1 << (offset % 8)))) {
5b5e0928 341 _debug("ENT[%zu.%u]: unused",
00317636 342 blkoff / sizeof(union afs_xdr_dir_block), offset);
1da177e4 343 if (offset >= curr)
1bbae9f8 344 ctx->pos = blkoff +
00317636 345 next * sizeof(union afs_xdr_dirent);
1da177e4
LT
346 continue;
347 }
348
349 /* got a valid entry */
350 dire = &block->dirents[offset];
351 nlen = strnlen(dire->u.name,
352 sizeof(*block) -
00317636 353 offset * sizeof(union afs_xdr_dirent));
1da177e4 354
5b5e0928 355 _debug("ENT[%zu.%u]: %s %zu \"%s\"",
00317636 356 blkoff / sizeof(union afs_xdr_dir_block), offset,
1da177e4
LT
357 (offset < curr ? "skip" : "fill"),
358 nlen, dire->u.name);
359
360 /* work out where the next possible entry is */
00317636 361 for (tmp = nlen; tmp > 15; tmp -= sizeof(union afs_xdr_dirent)) {
4ea219a8 362 if (next >= AFS_DIR_SLOTS_PER_BLOCK) {
5b5e0928 363 _debug("ENT[%zu.%u]:"
1da177e4 364 " %u travelled beyond end dir block"
5b5e0928 365 " (len %u/%zu)",
00317636 366 blkoff / sizeof(union afs_xdr_dir_block),
1da177e4
LT
367 offset, next, tmp, nlen);
368 return -EIO;
369 }
00317636 370 if (!(block->hdr.bitmap[next / 8] &
1da177e4 371 (1 << (next % 8)))) {
5b5e0928
AD
372 _debug("ENT[%zu.%u]:"
373 " %u unmarked extension (len %u/%zu)",
00317636 374 blkoff / sizeof(union afs_xdr_dir_block),
1da177e4
LT
375 offset, next, tmp, nlen);
376 return -EIO;
377 }
378
5b5e0928 379 _debug("ENT[%zu.%u]: ext %u/%zu",
00317636 380 blkoff / sizeof(union afs_xdr_dir_block),
1da177e4
LT
381 next, tmp, nlen);
382 next++;
383 }
384
385 /* skip if starts before the current position */
386 if (offset < curr)
387 continue;
388
389 /* found the next entry */
1bbae9f8 390 if (!dir_emit(ctx, dire->u.name, nlen,
1da177e4 391 ntohl(dire->u.vnode),
5cf9dd55
DH
392 (ctx->actor == afs_lookup_filldir ||
393 ctx->actor == afs_lookup_one_filldir)?
1bbae9f8 394 ntohl(dire->u.unique) : DT_UNKNOWN)) {
1da177e4
LT
395 _leave(" = 0 [full]");
396 return 0;
397 }
398
00317636 399 ctx->pos = blkoff + next * sizeof(union afs_xdr_dirent);
1da177e4
LT
400 }
401
402 _leave(" = 1 [more]");
403 return 1;
ec26815a 404}
1da177e4 405
1da177e4 406/*
08e0e7c8 407 * iterate through the data blob that lists the contents of an AFS directory
1da177e4 408 */
1bbae9f8
AV
409static int afs_dir_iterate(struct inode *dir, struct dir_context *ctx,
410 struct key *key)
1da177e4 411{
f3ddee8d 412 struct afs_vnode *dvnode = AFS_FS_I(dir);
00317636
DH
413 struct afs_xdr_dir_page *dbuf;
414 union afs_xdr_dir_block *dblock;
f3ddee8d 415 struct afs_read *req;
1da177e4
LT
416 struct page *page;
417 unsigned blkoff, limit;
418 int ret;
419
1bbae9f8 420 _enter("{%lu},%u,,", dir->i_ino, (unsigned)ctx->pos);
1da177e4 421
08e0e7c8 422 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) {
1da177e4
LT
423 _leave(" = -ESTALE");
424 return -ESTALE;
425 }
426
f3ddee8d
DH
427 req = afs_read_dir(dvnode, key);
428 if (IS_ERR(req))
429 return PTR_ERR(req);
430
1da177e4 431 /* round the file position up to the next entry boundary */
00317636
DH
432 ctx->pos += sizeof(union afs_xdr_dirent) - 1;
433 ctx->pos &= ~(sizeof(union afs_xdr_dirent) - 1);
1da177e4
LT
434
435 /* walk through the blocks in sequence */
436 ret = 0;
f3ddee8d 437 while (ctx->pos < req->actual_len) {
00317636 438 blkoff = ctx->pos & ~(sizeof(union afs_xdr_dir_block) - 1);
1da177e4 439
f3ddee8d
DH
440 /* Fetch the appropriate page from the directory and re-add it
441 * to the LRU.
442 */
443 page = req->pages[blkoff / PAGE_SIZE];
444 if (!page) {
445 ret = -EIO;
1da177e4
LT
446 break;
447 }
f3ddee8d 448 mark_page_accessed(page);
1da177e4
LT
449
450 limit = blkoff & ~(PAGE_SIZE - 1);
451
f3ddee8d 452 dbuf = kmap(page);
1da177e4
LT
453
454 /* deal with the individual blocks stashed on this page */
455 do {
456 dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) /
00317636 457 sizeof(union afs_xdr_dir_block)];
1bbae9f8 458 ret = afs_dir_iterate_block(ctx, dblock, blkoff);
1da177e4 459 if (ret != 1) {
f3ddee8d 460 kunmap(page);
1da177e4
LT
461 goto out;
462 }
463
00317636 464 blkoff += sizeof(union afs_xdr_dir_block);
1da177e4 465
1bbae9f8 466 } while (ctx->pos < dir->i_size && blkoff < limit);
1da177e4 467
f3ddee8d 468 kunmap(page);
1da177e4
LT
469 ret = 0;
470 }
471
ec26815a 472out:
b61f7dcf 473 up_read(&dvnode->validate_lock);
f3ddee8d 474 afs_put_read(req);
1da177e4
LT
475 _leave(" = %d", ret);
476 return ret;
ec26815a 477}
1da177e4 478
1da177e4
LT
479/*
480 * read an AFS directory
481 */
1bbae9f8 482static int afs_readdir(struct file *file, struct dir_context *ctx)
1da177e4 483{
215804a9 484 return afs_dir_iterate(file_inode(file), ctx, afs_file_key(file));
ec26815a 485}
1da177e4 486
1da177e4 487/*
5cf9dd55 488 * Search the directory for a single name
1da177e4
LT
489 * - if afs_dir_iterate_block() spots this function, it'll pass the FID
490 * uniquifier through dtype
491 */
5cf9dd55
DH
492static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name,
493 int nlen, loff_t fpos, u64 ino, unsigned dtype)
1da177e4 494{
5cf9dd55
DH
495 struct afs_lookup_one_cookie *cookie =
496 container_of(ctx, struct afs_lookup_one_cookie, ctx);
1da177e4 497
1bbae9f8
AV
498 _enter("{%s,%u},%s,%u,,%llu,%u",
499 cookie->name.name, cookie->name.len, name, nlen,
ba3e0e1a 500 (unsigned long long) ino, dtype);
1da177e4 501
08e0e7c8 502 /* insanity checks first */
00317636
DH
503 BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
504 BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
08e0e7c8 505
1bbae9f8
AV
506 if (cookie->name.len != nlen ||
507 memcmp(cookie->name.name, name, nlen) != 0) {
1da177e4
LT
508 _leave(" = 0 [no]");
509 return 0;
510 }
511
512 cookie->fid.vnode = ino;
513 cookie->fid.unique = dtype;
514 cookie->found = 1;
515
516 _leave(" = -1 [found]");
517 return -1;
ec26815a 518}
1da177e4 519
1da177e4 520/*
5cf9dd55 521 * Do a lookup of a single name in a directory
260a9803 522 * - just returns the FID the dentry name maps to if found
1da177e4 523 */
5cf9dd55
DH
524static int afs_do_lookup_one(struct inode *dir, struct dentry *dentry,
525 struct afs_fid *fid, struct key *key)
1da177e4 526{
1bbae9f8 527 struct afs_super_info *as = dir->i_sb->s_fs_info;
5cf9dd55
DH
528 struct afs_lookup_one_cookie cookie = {
529 .ctx.actor = afs_lookup_one_filldir,
1bbae9f8
AV
530 .name = dentry->d_name,
531 .fid.vid = as->volume->vid
532 };
1da177e4
LT
533 int ret;
534
a455589f 535 _enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
1da177e4 536
1da177e4 537 /* search the directory */
1bbae9f8 538 ret = afs_dir_iterate(dir, &cookie.ctx, key);
1da177e4 539 if (ret < 0) {
08e0e7c8
DH
540 _leave(" = %d [iter]", ret);
541 return ret;
1da177e4
LT
542 }
543
544 ret = -ENOENT;
545 if (!cookie.found) {
08e0e7c8
DH
546 _leave(" = -ENOENT [not found]");
547 return -ENOENT;
1da177e4
LT
548 }
549
08e0e7c8
DH
550 *fid = cookie.fid;
551 _leave(" = 0 { vn=%u u=%u }", fid->vnode, fid->unique);
552 return 0;
553}
554
5cf9dd55
DH
555/*
556 * search the directory for a name
557 * - if afs_dir_iterate_block() spots this function, it'll pass the FID
558 * uniquifier through dtype
559 */
560static int afs_lookup_filldir(struct dir_context *ctx, const char *name,
561 int nlen, loff_t fpos, u64 ino, unsigned dtype)
562{
563 struct afs_lookup_cookie *cookie =
564 container_of(ctx, struct afs_lookup_cookie, ctx);
565 int ret;
566
567 _enter("{%s,%u},%s,%u,,%llu,%u",
568 cookie->name.name, cookie->name.len, name, nlen,
569 (unsigned long long) ino, dtype);
570
571 /* insanity checks first */
00317636
DH
572 BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
573 BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
5cf9dd55
DH
574
575 if (cookie->found) {
576 if (cookie->nr_fids < 50) {
577 cookie->fids[cookie->nr_fids].vnode = ino;
578 cookie->fids[cookie->nr_fids].unique = dtype;
579 cookie->nr_fids++;
580 }
581 } else if (cookie->name.len == nlen &&
582 memcmp(cookie->name.name, name, nlen) == 0) {
583 cookie->fids[0].vnode = ino;
584 cookie->fids[0].unique = dtype;
585 cookie->found = 1;
586 if (cookie->one_only)
587 return -1;
588 }
589
590 ret = cookie->nr_fids >= 50 ? -1 : 0;
591 _leave(" = %d", ret);
592 return ret;
593}
594
595/*
596 * Do a lookup in a directory. We make use of bulk lookup to query a slew of
597 * files in one go and create inodes for them. The inode of the file we were
598 * asked for is returned.
599 */
600static struct inode *afs_do_lookup(struct inode *dir, struct dentry *dentry,
601 struct key *key)
602{
603 struct afs_lookup_cookie *cookie;
604 struct afs_cb_interest *cbi = NULL;
605 struct afs_super_info *as = dir->i_sb->s_fs_info;
606 struct afs_iget_data data;
607 struct afs_fs_cursor fc;
608 struct afs_vnode *dvnode = AFS_FS_I(dir);
609 struct inode *inode = NULL;
610 int ret, i;
611
612 _enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
613
614 cookie = kzalloc(sizeof(struct afs_lookup_cookie), GFP_KERNEL);
615 if (!cookie)
616 return ERR_PTR(-ENOMEM);
617
618 cookie->ctx.actor = afs_lookup_filldir;
619 cookie->name = dentry->d_name;
620 cookie->nr_fids = 1; /* slot 0 is saved for the fid we actually want */
621
622 read_seqlock_excl(&dvnode->cb_lock);
623 if (dvnode->cb_interest &&
624 dvnode->cb_interest->server &&
625 test_bit(AFS_SERVER_FL_NO_IBULK, &dvnode->cb_interest->server->flags))
626 cookie->one_only = true;
627 read_sequnlock_excl(&dvnode->cb_lock);
628
629 for (i = 0; i < 50; i++)
630 cookie->fids[i].vid = as->volume->vid;
631
632 /* search the directory */
633 ret = afs_dir_iterate(dir, &cookie->ctx, key);
634 if (ret < 0) {
635 inode = ERR_PTR(ret);
636 goto out;
637 }
638
639 inode = ERR_PTR(-ENOENT);
640 if (!cookie->found)
641 goto out;
642
643 /* Check to see if we already have an inode for the primary fid. */
644 data.volume = dvnode->volume;
645 data.fid = cookie->fids[0];
646 inode = ilookup5(dir->i_sb, cookie->fids[0].vnode, afs_iget5_test, &data);
647 if (inode)
648 goto out;
649
650 /* Need space for examining all the selected files */
651 inode = ERR_PTR(-ENOMEM);
652 cookie->statuses = kcalloc(cookie->nr_fids, sizeof(struct afs_file_status),
653 GFP_KERNEL);
654 if (!cookie->statuses)
655 goto out;
656
657 cookie->callbacks = kcalloc(cookie->nr_fids, sizeof(struct afs_callback),
658 GFP_KERNEL);
659 if (!cookie->callbacks)
660 goto out_s;
661
662 /* Try FS.InlineBulkStatus first. Abort codes for the individual
663 * lookups contained therein are stored in the reply without aborting
664 * the whole operation.
665 */
666 if (cookie->one_only)
667 goto no_inline_bulk_status;
668
669 inode = ERR_PTR(-ERESTARTSYS);
670 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
671 while (afs_select_fileserver(&fc)) {
672 if (test_bit(AFS_SERVER_FL_NO_IBULK,
673 &fc.cbi->server->flags)) {
674 fc.ac.abort_code = RX_INVALID_OPERATION;
675 fc.ac.error = -ECONNABORTED;
676 break;
677 }
678 afs_fs_inline_bulk_status(&fc,
679 afs_v2net(dvnode),
680 cookie->fids,
681 cookie->statuses,
682 cookie->callbacks,
683 cookie->nr_fids, NULL);
684 }
685
686 if (fc.ac.error == 0)
687 cbi = afs_get_cb_interest(fc.cbi);
688 if (fc.ac.abort_code == RX_INVALID_OPERATION)
689 set_bit(AFS_SERVER_FL_NO_IBULK, &fc.cbi->server->flags);
690 inode = ERR_PTR(afs_end_vnode_operation(&fc));
691 }
692
693 if (!IS_ERR(inode))
694 goto success;
695 if (fc.ac.abort_code != RX_INVALID_OPERATION)
696 goto out_c;
697
698no_inline_bulk_status:
699 /* We could try FS.BulkStatus next, but this aborts the entire op if
700 * any of the lookups fails - so, for the moment, revert to
701 * FS.FetchStatus for just the primary fid.
702 */
703 cookie->nr_fids = 1;
704 inode = ERR_PTR(-ERESTARTSYS);
705 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
706 while (afs_select_fileserver(&fc)) {
707 afs_fs_fetch_status(&fc,
708 afs_v2net(dvnode),
709 cookie->fids,
710 cookie->statuses,
711 cookie->callbacks,
712 NULL);
713 }
714
715 if (fc.ac.error == 0)
716 cbi = afs_get_cb_interest(fc.cbi);
717 inode = ERR_PTR(afs_end_vnode_operation(&fc));
718 }
719
720 if (IS_ERR(inode))
721 goto out_c;
722
723 for (i = 0; i < cookie->nr_fids; i++)
724 cookie->statuses[i].abort_code = 0;
725
726success:
727 /* Turn all the files into inodes and save the first one - which is the
728 * one we actually want.
729 */
730 if (cookie->statuses[0].abort_code != 0)
731 inode = ERR_PTR(afs_abort_to_error(cookie->statuses[0].abort_code));
732
733 for (i = 0; i < cookie->nr_fids; i++) {
734 struct inode *ti;
735
736 if (cookie->statuses[i].abort_code != 0)
737 continue;
738
739 ti = afs_iget(dir->i_sb, key, &cookie->fids[i],
740 &cookie->statuses[i],
741 &cookie->callbacks[i],
742 cbi);
743 if (i == 0) {
744 inode = ti;
745 } else {
746 if (!IS_ERR(ti))
747 iput(ti);
748 }
749 }
750
751out_c:
752 afs_put_cb_interest(afs_v2net(dvnode), cbi);
753 kfree(cookie->callbacks);
754out_s:
755 kfree(cookie->statuses);
756out:
757 kfree(cookie);
758 return inode;
759}
760
6f8880d8
DH
761/*
762 * Look up an entry in a directory with @sys substitution.
763 */
764static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry,
765 struct key *key)
766{
767 struct afs_sysnames *subs;
768 struct afs_net *net = afs_i2net(dir);
769 struct dentry *ret;
770 char *buf, *p, *name;
771 int len, i;
772
773 _enter("");
774
775 ret = ERR_PTR(-ENOMEM);
776 p = buf = kmalloc(AFSNAMEMAX, GFP_KERNEL);
777 if (!buf)
778 goto out_p;
779 if (dentry->d_name.len > 4) {
780 memcpy(p, dentry->d_name.name, dentry->d_name.len - 4);
781 p += dentry->d_name.len - 4;
782 }
783
784 /* There is an ordered list of substitutes that we have to try. */
785 read_lock(&net->sysnames_lock);
786 subs = net->sysnames;
787 refcount_inc(&subs->usage);
788 read_unlock(&net->sysnames_lock);
789
790 for (i = 0; i < subs->nr; i++) {
791 name = subs->subs[i];
792 len = dentry->d_name.len - 4 + strlen(name);
793 if (len >= AFSNAMEMAX) {
794 ret = ERR_PTR(-ENAMETOOLONG);
795 goto out_s;
796 }
797
798 strcpy(p, name);
799 ret = lookup_one_len(buf, dentry->d_parent, len);
800 if (IS_ERR(ret) || d_is_positive(ret))
801 goto out_s;
802 dput(ret);
803 }
804
805 /* We don't want to d_add() the @sys dentry here as we don't want to
806 * the cached dentry to hide changes to the sysnames list.
807 */
808 ret = NULL;
809out_s:
810 afs_put_sysnames(subs);
811 kfree(buf);
812out_p:
813 key_put(key);
814 return ret;
815}
816
08e0e7c8
DH
817/*
818 * look up an entry in a directory
819 */
260a9803 820static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
00cd8dd3 821 unsigned int flags)
08e0e7c8 822{
5cf9dd55 823 struct afs_vnode *dvnode = AFS_FS_I(dir);
08e0e7c8 824 struct inode *inode;
34b2a88f 825 struct dentry *d;
00d3b7a4 826 struct key *key;
08e0e7c8
DH
827 int ret;
828
a455589f 829 _enter("{%x:%u},%p{%pd},",
5cf9dd55 830 dvnode->fid.vid, dvnode->fid.vnode, dentry, dentry);
260a9803 831
2b0143b5 832 ASSERTCMP(d_inode(dentry), ==, NULL);
08e0e7c8 833
45222b9e 834 if (dentry->d_name.len >= AFSNAMEMAX) {
08e0e7c8
DH
835 _leave(" = -ENAMETOOLONG");
836 return ERR_PTR(-ENAMETOOLONG);
837 }
838
5cf9dd55 839 if (test_bit(AFS_VNODE_DELETED, &dvnode->flags)) {
08e0e7c8
DH
840 _leave(" = -ESTALE");
841 return ERR_PTR(-ESTALE);
842 }
843
5cf9dd55 844 key = afs_request_key(dvnode->volume->cell);
00d3b7a4
DH
845 if (IS_ERR(key)) {
846 _leave(" = %ld [key]", PTR_ERR(key));
e231c2ee 847 return ERR_CAST(key);
00d3b7a4
DH
848 }
849
5cf9dd55 850 ret = afs_validate(dvnode, key);
260a9803
DH
851 if (ret < 0) {
852 key_put(key);
853 _leave(" = %d [val]", ret);
854 return ERR_PTR(ret);
855 }
856
6f8880d8
DH
857 if (dentry->d_name.len >= 4 &&
858 dentry->d_name.name[dentry->d_name.len - 4] == '@' &&
859 dentry->d_name.name[dentry->d_name.len - 3] == 's' &&
860 dentry->d_name.name[dentry->d_name.len - 2] == 'y' &&
861 dentry->d_name.name[dentry->d_name.len - 1] == 's')
862 return afs_lookup_atsys(dir, dentry, key);
863
d55b4da4 864 afs_stat_v(dvnode, n_lookup);
5cf9dd55 865 inode = afs_do_lookup(dir, dentry, key);
00d3b7a4 866 key_put(key);
34b2a88f
AV
867 if (inode == ERR_PTR(-ENOENT)) {
868 inode = afs_try_auto_mntpt(dentry, dir);
34b2a88f
AV
869 } else {
870 dentry->d_fsdata =
871 (void *)(unsigned long)dvnode->status.data_version;
08e0e7c8 872 }
34b2a88f
AV
873 d = d_splice_alias(inode, dentry);
874 if (!IS_ERR_OR_NULL(d))
875 d->d_fsdata = dentry->d_fsdata;
876 return d;
ec26815a 877}
1da177e4 878
1da177e4
LT
879/*
880 * check that a dentry lookup hit has found a valid entry
881 * - NOTE! the hit can be a negative hit too, so we can't assume we have an
882 * inode
1da177e4 883 */
0b728e19 884static int afs_d_revalidate(struct dentry *dentry, unsigned int flags)
1da177e4 885{
260a9803 886 struct afs_vnode *vnode, *dir;
dd0d9a46 887 struct afs_fid uninitialized_var(fid);
1da177e4 888 struct dentry *parent;
c435ee34 889 struct inode *inode;
00d3b7a4 890 struct key *key;
a4ff7401 891 long dir_version, de_version;
1da177e4
LT
892 int ret;
893
0b728e19 894 if (flags & LOOKUP_RCU)
34286d66
NP
895 return -ECHILD;
896
c435ee34
DH
897 if (d_really_is_positive(dentry)) {
898 vnode = AFS_FS_I(d_inode(dentry));
a455589f
AV
899 _enter("{v={%x:%u} n=%pd fl=%lx},",
900 vnode->fid.vid, vnode->fid.vnode, dentry,
260a9803 901 vnode->flags);
c435ee34 902 } else {
a455589f 903 _enter("{neg n=%pd}", dentry);
c435ee34 904 }
1da177e4 905
260a9803 906 key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell);
00d3b7a4
DH
907 if (IS_ERR(key))
908 key = NULL;
909
c435ee34
DH
910 if (d_really_is_positive(dentry)) {
911 inode = d_inode(dentry);
912 if (inode) {
913 vnode = AFS_FS_I(inode);
914 afs_validate(vnode, key);
915 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
916 goto out_bad;
917 }
918 }
919
1da177e4 920 /* lock down the parent dentry so we can peer at it */
08e0e7c8 921 parent = dget_parent(dentry);
2b0143b5 922 dir = AFS_FS_I(d_inode(parent));
1da177e4 923
260a9803 924 /* validate the parent directory */
c435ee34 925 afs_validate(dir, key);
260a9803
DH
926
927 if (test_bit(AFS_VNODE_DELETED, &dir->flags)) {
a455589f 928 _debug("%pd: parent dir deleted", dentry);
c435ee34 929 goto out_bad_parent;
1da177e4
LT
930 }
931
a4ff7401
DH
932 /* We only need to invalidate a dentry if the server's copy changed
933 * behind our back. If we made the change, it's no problem. Note that
934 * on a 32-bit system, we only have 32 bits in the dentry to store the
935 * version.
936 */
937 dir_version = (long)dir->status.data_version;
938 de_version = (long)dentry->d_fsdata;
939 if (de_version == dir_version)
940 goto out_valid;
941
942 dir_version = (long)dir->invalid_before;
943 if (de_version - dir_version >= 0)
944 goto out_valid;
1da177e4 945
260a9803 946 _debug("dir modified");
d55b4da4 947 afs_stat_v(dir, n_reval);
260a9803
DH
948
949 /* search the directory for this vnode */
5cf9dd55 950 ret = afs_do_lookup_one(&dir->vfs_inode, dentry, &fid, key);
260a9803
DH
951 switch (ret) {
952 case 0:
953 /* the filename maps to something */
2b0143b5 954 if (d_really_is_negative(dentry))
c435ee34
DH
955 goto out_bad_parent;
956 inode = d_inode(dentry);
957 if (is_bad_inode(inode)) {
a455589f
AV
958 printk("kAFS: afs_d_revalidate: %pd2 has bad inode\n",
959 dentry);
c435ee34 960 goto out_bad_parent;
1da177e4
LT
961 }
962
c435ee34
DH
963 vnode = AFS_FS_I(inode);
964
1da177e4
LT
965 /* if the vnode ID has changed, then the dirent points to a
966 * different file */
08e0e7c8 967 if (fid.vnode != vnode->fid.vnode) {
a455589f
AV
968 _debug("%pd: dirent changed [%u != %u]",
969 dentry, fid.vnode,
08e0e7c8 970 vnode->fid.vnode);
1da177e4
LT
971 goto not_found;
972 }
973
974 /* if the vnode ID uniqifier has changed, then the file has
260a9803
DH
975 * been deleted and replaced, and the original vnode ID has
976 * been reused */
08e0e7c8 977 if (fid.unique != vnode->fid.unique) {
a455589f
AV
978 _debug("%pd: file deleted (uq %u -> %u I:%u)",
979 dentry, fid.unique,
7a224228 980 vnode->fid.unique,
c435ee34
DH
981 vnode->vfs_inode.i_generation);
982 write_seqlock(&vnode->cb_lock);
08e0e7c8 983 set_bit(AFS_VNODE_DELETED, &vnode->flags);
c435ee34 984 write_sequnlock(&vnode->cb_lock);
260a9803 985 goto not_found;
1da177e4 986 }
260a9803 987 goto out_valid;
08e0e7c8 988
260a9803
DH
989 case -ENOENT:
990 /* the filename is unknown */
a455589f 991 _debug("%pd: dirent not found", dentry);
2b0143b5 992 if (d_really_is_positive(dentry))
260a9803
DH
993 goto not_found;
994 goto out_valid;
1da177e4 995
260a9803 996 default:
a455589f
AV
997 _debug("failed to iterate dir %pd: %d",
998 parent, ret);
c435ee34 999 goto out_bad_parent;
08e0e7c8
DH
1000 }
1001
ec26815a 1002out_valid:
a4ff7401 1003 dentry->d_fsdata = (void *)dir_version;
1da177e4 1004 dput(parent);
00d3b7a4 1005 key_put(key);
1da177e4
LT
1006 _leave(" = 1 [valid]");
1007 return 1;
1008
1009 /* the dirent, if it exists, now points to a different vnode */
ec26815a 1010not_found:
1da177e4
LT
1011 spin_lock(&dentry->d_lock);
1012 dentry->d_flags |= DCACHE_NFSFS_RENAMED;
1013 spin_unlock(&dentry->d_lock);
1014
c435ee34 1015out_bad_parent:
a455589f 1016 _debug("dropping dentry %pd2", dentry);
1da177e4 1017 dput(parent);
c435ee34 1018out_bad:
00d3b7a4 1019 key_put(key);
1da177e4
LT
1020
1021 _leave(" = 0 [bad]");
1022 return 0;
ec26815a 1023}
1da177e4 1024
1da177e4
LT
1025/*
1026 * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
1027 * sleep)
1028 * - called from dput() when d_count is going to 0.
1029 * - return 1 to request dentry be unhashed, 0 otherwise
1030 */
fe15ce44 1031static int afs_d_delete(const struct dentry *dentry)
1da177e4 1032{
a455589f 1033 _enter("%pd", dentry);
1da177e4
LT
1034
1035 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1036 goto zap;
1037
2b0143b5
DH
1038 if (d_really_is_positive(dentry) &&
1039 (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(d_inode(dentry))->flags) ||
1040 test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(d_inode(dentry))->flags)))
bec5eb61 1041 goto zap;
1da177e4
LT
1042
1043 _leave(" = 0 [keep]");
1044 return 0;
1045
ec26815a 1046zap:
1da177e4
LT
1047 _leave(" = 1 [zap]");
1048 return 1;
ec26815a 1049}
260a9803
DH
1050
1051/*
1052 * handle dentry release
1053 */
66c7e1d3 1054void afs_d_release(struct dentry *dentry)
260a9803 1055{
a455589f 1056 _enter("%pd", dentry);
260a9803
DH
1057}
1058
d2ddc776
DH
1059/*
1060 * Create a new inode for create/mkdir/symlink
1061 */
1062static void afs_vnode_new_inode(struct afs_fs_cursor *fc,
1063 struct dentry *new_dentry,
1064 struct afs_fid *newfid,
1065 struct afs_file_status *newstatus,
1066 struct afs_callback *newcb)
1067{
5a813276 1068 struct afs_vnode *vnode;
d2ddc776
DH
1069 struct inode *inode;
1070
1071 if (fc->ac.error < 0)
1072 return;
1073
bc1527dc
DH
1074 d_drop(new_dentry);
1075
d2ddc776
DH
1076 inode = afs_iget(fc->vnode->vfs_inode.i_sb, fc->key,
1077 newfid, newstatus, newcb, fc->cbi);
1078 if (IS_ERR(inode)) {
1079 /* ENOMEM or EINTR at a really inconvenient time - just abandon
1080 * the new directory on the server.
1081 */
1082 fc->ac.error = PTR_ERR(inode);
1083 return;
1084 }
1085
5a813276
DH
1086 vnode = AFS_FS_I(inode);
1087 set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
bc1527dc 1088 d_add(new_dentry, inode);
d2ddc776
DH
1089}
1090
260a9803
DH
1091/*
1092 * create a directory on an AFS filesystem
1093 */
18bb1db3 1094static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
260a9803 1095{
d2ddc776
DH
1096 struct afs_file_status newstatus;
1097 struct afs_fs_cursor fc;
1098 struct afs_callback newcb;
1099 struct afs_vnode *dvnode = AFS_FS_I(dir);
1100 struct afs_fid newfid;
260a9803 1101 struct key *key;
63a4681f 1102 u64 data_version = dvnode->status.data_version;
260a9803
DH
1103 int ret;
1104
d2ddc776 1105 mode |= S_IFDIR;
260a9803 1106
a455589f
AV
1107 _enter("{%x:%u},{%pd},%ho",
1108 dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
260a9803 1109
260a9803
DH
1110 key = afs_request_key(dvnode->volume->cell);
1111 if (IS_ERR(key)) {
1112 ret = PTR_ERR(key);
1113 goto error;
1114 }
1115
d2ddc776
DH
1116 ret = -ERESTARTSYS;
1117 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1118 while (afs_select_fileserver(&fc)) {
68251f0a 1119 fc.cb_break = afs_calc_vnode_cb_break(dvnode);
63a4681f 1120 afs_fs_create(&fc, dentry->d_name.name, mode, data_version,
d2ddc776
DH
1121 &newfid, &newstatus, &newcb);
1122 }
260a9803 1123
d2ddc776
DH
1124 afs_check_for_remote_deletion(&fc, fc.vnode);
1125 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1126 afs_vnode_new_inode(&fc, dentry, &newfid, &newstatus, &newcb);
1127 ret = afs_end_vnode_operation(&fc);
1128 if (ret < 0)
1129 goto error_key;
4433b691
DH
1130 } else {
1131 goto error_key;
260a9803
DH
1132 }
1133
63a4681f
DH
1134 if (ret == 0 &&
1135 test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1136 afs_edit_dir_add(dvnode, &dentry->d_name, &newfid,
1137 afs_edit_dir_for_create);
1138
260a9803
DH
1139 key_put(key);
1140 _leave(" = 0");
1141 return 0;
1142
d2ddc776 1143error_key:
260a9803
DH
1144 key_put(key);
1145error:
1146 d_drop(dentry);
1147 _leave(" = %d", ret);
1148 return ret;
1149}
1150
d2ddc776
DH
1151/*
1152 * Remove a subdir from a directory.
1153 */
1154static void afs_dir_remove_subdir(struct dentry *dentry)
1155{
1156 if (d_really_is_positive(dentry)) {
1157 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1158
1159 clear_nlink(&vnode->vfs_inode);
1160 set_bit(AFS_VNODE_DELETED, &vnode->flags);
1161 clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
63a4681f 1162 clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
d2ddc776
DH
1163 }
1164}
1165
260a9803
DH
1166/*
1167 * remove a directory from an AFS filesystem
1168 */
1169static int afs_rmdir(struct inode *dir, struct dentry *dentry)
1170{
d2ddc776
DH
1171 struct afs_fs_cursor fc;
1172 struct afs_vnode *dvnode = AFS_FS_I(dir);
260a9803 1173 struct key *key;
63a4681f 1174 u64 data_version = dvnode->status.data_version;
260a9803
DH
1175 int ret;
1176
a455589f
AV
1177 _enter("{%x:%u},{%pd}",
1178 dvnode->fid.vid, dvnode->fid.vnode, dentry);
260a9803 1179
260a9803
DH
1180 key = afs_request_key(dvnode->volume->cell);
1181 if (IS_ERR(key)) {
1182 ret = PTR_ERR(key);
1183 goto error;
1184 }
1185
d2ddc776
DH
1186 ret = -ERESTARTSYS;
1187 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1188 while (afs_select_fileserver(&fc)) {
68251f0a 1189 fc.cb_break = afs_calc_vnode_cb_break(dvnode);
63a4681f
DH
1190 afs_fs_remove(&fc, dentry->d_name.name, true,
1191 data_version);
d2ddc776 1192 }
260a9803 1193
d2ddc776
DH
1194 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1195 ret = afs_end_vnode_operation(&fc);
63a4681f 1196 if (ret == 0) {
d2ddc776 1197 afs_dir_remove_subdir(dentry);
63a4681f
DH
1198 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1199 afs_edit_dir_remove(dvnode, &dentry->d_name,
1200 afs_edit_dir_for_rmdir);
1201 }
260a9803
DH
1202 }
1203
1204 key_put(key);
260a9803 1205error:
260a9803
DH
1206 return ret;
1207}
1208
1209/*
d2ddc776
DH
1210 * Remove a link to a file or symlink from a directory.
1211 *
1212 * If the file was not deleted due to excess hard links, the fileserver will
1213 * break the callback promise on the file - if it had one - before it returns
1214 * to us, and if it was deleted, it won't
1215 *
1216 * However, if we didn't have a callback promise outstanding, or it was
1217 * outstanding on a different server, then it won't break it either...
1218 */
440fbc3a
DH
1219static int afs_dir_remove_link(struct dentry *dentry, struct key *key,
1220 unsigned long d_version_before,
1221 unsigned long d_version_after)
d2ddc776 1222{
440fbc3a 1223 bool dir_valid;
d2ddc776
DH
1224 int ret = 0;
1225
440fbc3a
DH
1226 /* There were no intervening changes on the server if the version
1227 * number we got back was incremented by exactly 1.
1228 */
1229 dir_valid = (d_version_after == d_version_before + 1);
1230
d2ddc776
DH
1231 if (d_really_is_positive(dentry)) {
1232 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1233
440fbc3a
DH
1234 if (dir_valid) {
1235 drop_nlink(&vnode->vfs_inode);
1236 if (vnode->vfs_inode.i_nlink == 0) {
1237 set_bit(AFS_VNODE_DELETED, &vnode->flags);
1238 clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
1239 }
d2ddc776 1240 ret = 0;
440fbc3a
DH
1241 } else {
1242 clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
1243
1244 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
1245 kdebug("AFS_VNODE_DELETED");
1246
1247 ret = afs_validate(vnode, key);
1248 if (ret == -ESTALE)
1249 ret = 0;
1250 }
d2ddc776
DH
1251 _debug("nlink %d [val %d]", vnode->vfs_inode.i_nlink, ret);
1252 }
1253
1254 return ret;
1255}
1256
1257/*
1258 * Remove a file or symlink from an AFS filesystem.
260a9803
DH
1259 */
1260static int afs_unlink(struct inode *dir, struct dentry *dentry)
1261{
d2ddc776
DH
1262 struct afs_fs_cursor fc;
1263 struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode;
260a9803 1264 struct key *key;
440fbc3a 1265 unsigned long d_version = (unsigned long)dentry->d_fsdata;
63a4681f 1266 u64 data_version = dvnode->status.data_version;
260a9803
DH
1267 int ret;
1268
a455589f
AV
1269 _enter("{%x:%u},{%pd}",
1270 dvnode->fid.vid, dvnode->fid.vnode, dentry);
260a9803 1271
45222b9e 1272 if (dentry->d_name.len >= AFSNAMEMAX)
d2ddc776 1273 return -ENAMETOOLONG;
260a9803
DH
1274
1275 key = afs_request_key(dvnode->volume->cell);
1276 if (IS_ERR(key)) {
1277 ret = PTR_ERR(key);
1278 goto error;
1279 }
1280
d2ddc776 1281 /* Try to make sure we have a callback promise on the victim. */
2b0143b5
DH
1282 if (d_really_is_positive(dentry)) {
1283 vnode = AFS_FS_I(d_inode(dentry));
260a9803
DH
1284 ret = afs_validate(vnode, key);
1285 if (ret < 0)
d2ddc776 1286 goto error_key;
260a9803
DH
1287 }
1288
d2ddc776
DH
1289 ret = -ERESTARTSYS;
1290 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1291 while (afs_select_fileserver(&fc)) {
68251f0a 1292 fc.cb_break = afs_calc_vnode_cb_break(dvnode);
63a4681f
DH
1293 afs_fs_remove(&fc, dentry->d_name.name, false,
1294 data_version);
d2ddc776 1295 }
260a9803 1296
d2ddc776
DH
1297 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1298 ret = afs_end_vnode_operation(&fc);
1299 if (ret == 0)
440fbc3a
DH
1300 ret = afs_dir_remove_link(
1301 dentry, key, d_version,
1302 (unsigned long)dvnode->status.data_version);
63a4681f
DH
1303 if (ret == 0 &&
1304 test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1305 afs_edit_dir_remove(dvnode, &dentry->d_name,
1306 afs_edit_dir_for_unlink);
260a9803
DH
1307 }
1308
d2ddc776 1309error_key:
260a9803
DH
1310 key_put(key);
1311error:
1312 _leave(" = %d", ret);
1313 return ret;
1314}
1315
1316/*
1317 * create a regular file on an AFS filesystem
1318 */
4acdaf27 1319static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
ebfc3b49 1320 bool excl)
260a9803 1321{
d2ddc776
DH
1322 struct afs_fs_cursor fc;
1323 struct afs_file_status newstatus;
1324 struct afs_callback newcb;
43dd388b 1325 struct afs_vnode *dvnode = AFS_FS_I(dir);
d2ddc776 1326 struct afs_fid newfid;
260a9803 1327 struct key *key;
63a4681f 1328 u64 data_version = dvnode->status.data_version;
260a9803
DH
1329 int ret;
1330
d2ddc776 1331 mode |= S_IFREG;
260a9803 1332
a455589f
AV
1333 _enter("{%x:%u},{%pd},%ho,",
1334 dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
260a9803 1335
d2ddc776
DH
1336 ret = -ENAMETOOLONG;
1337 if (dentry->d_name.len >= AFSNAMEMAX)
1338 goto error;
1339
260a9803
DH
1340 key = afs_request_key(dvnode->volume->cell);
1341 if (IS_ERR(key)) {
1342 ret = PTR_ERR(key);
1343 goto error;
1344 }
1345
d2ddc776
DH
1346 ret = -ERESTARTSYS;
1347 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1348 while (afs_select_fileserver(&fc)) {
68251f0a 1349 fc.cb_break = afs_calc_vnode_cb_break(dvnode);
63a4681f 1350 afs_fs_create(&fc, dentry->d_name.name, mode, data_version,
d2ddc776
DH
1351 &newfid, &newstatus, &newcb);
1352 }
260a9803 1353
d2ddc776
DH
1354 afs_check_for_remote_deletion(&fc, fc.vnode);
1355 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1356 afs_vnode_new_inode(&fc, dentry, &newfid, &newstatus, &newcb);
1357 ret = afs_end_vnode_operation(&fc);
1358 if (ret < 0)
1359 goto error_key;
4433b691
DH
1360 } else {
1361 goto error_key;
260a9803
DH
1362 }
1363
63a4681f
DH
1364 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1365 afs_edit_dir_add(dvnode, &dentry->d_name, &newfid,
1366 afs_edit_dir_for_create);
1367
260a9803
DH
1368 key_put(key);
1369 _leave(" = 0");
1370 return 0;
1371
d2ddc776 1372error_key:
260a9803
DH
1373 key_put(key);
1374error:
1375 d_drop(dentry);
1376 _leave(" = %d", ret);
1377 return ret;
1378}
1379
1380/*
1381 * create a hard link between files in an AFS filesystem
1382 */
1383static int afs_link(struct dentry *from, struct inode *dir,
1384 struct dentry *dentry)
1385{
d2ddc776 1386 struct afs_fs_cursor fc;
260a9803
DH
1387 struct afs_vnode *dvnode, *vnode;
1388 struct key *key;
63a4681f 1389 u64 data_version;
260a9803
DH
1390 int ret;
1391
2b0143b5 1392 vnode = AFS_FS_I(d_inode(from));
260a9803 1393 dvnode = AFS_FS_I(dir);
63a4681f 1394 data_version = dvnode->status.data_version;
260a9803 1395
a455589f 1396 _enter("{%x:%u},{%x:%u},{%pd}",
260a9803
DH
1397 vnode->fid.vid, vnode->fid.vnode,
1398 dvnode->fid.vid, dvnode->fid.vnode,
a455589f 1399 dentry);
260a9803 1400
d2ddc776
DH
1401 ret = -ENAMETOOLONG;
1402 if (dentry->d_name.len >= AFSNAMEMAX)
1403 goto error;
1404
260a9803
DH
1405 key = afs_request_key(dvnode->volume->cell);
1406 if (IS_ERR(key)) {
1407 ret = PTR_ERR(key);
1408 goto error;
1409 }
1410
d2ddc776
DH
1411 ret = -ERESTARTSYS;
1412 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1413 if (mutex_lock_interruptible_nested(&vnode->io_lock, 1) < 0) {
1414 afs_end_vnode_operation(&fc);
bc1527dc 1415 goto error_key;
d2ddc776
DH
1416 }
1417
1418 while (afs_select_fileserver(&fc)) {
68251f0a
DH
1419 fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1420 fc.cb_break_2 = afs_calc_vnode_cb_break(vnode);
63a4681f 1421 afs_fs_link(&fc, vnode, dentry->d_name.name, data_version);
d2ddc776
DH
1422 }
1423
1424 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1425 afs_vnode_commit_status(&fc, vnode, fc.cb_break_2);
1426 ihold(&vnode->vfs_inode);
1427 d_instantiate(dentry, &vnode->vfs_inode);
1428
1429 mutex_unlock(&vnode->io_lock);
1430 ret = afs_end_vnode_operation(&fc);
1431 if (ret < 0)
1432 goto error_key;
4433b691
DH
1433 } else {
1434 goto error_key;
d2ddc776 1435 }
260a9803 1436
63a4681f
DH
1437 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1438 afs_edit_dir_add(dvnode, &dentry->d_name, &vnode->fid,
1439 afs_edit_dir_for_link);
1440
260a9803
DH
1441 key_put(key);
1442 _leave(" = 0");
1443 return 0;
1444
d2ddc776 1445error_key:
260a9803
DH
1446 key_put(key);
1447error:
1448 d_drop(dentry);
1449 _leave(" = %d", ret);
1450 return ret;
1451}
1452
1453/*
1454 * create a symlink in an AFS filesystem
1455 */
1456static int afs_symlink(struct inode *dir, struct dentry *dentry,
1457 const char *content)
1458{
d2ddc776
DH
1459 struct afs_fs_cursor fc;
1460 struct afs_file_status newstatus;
1461 struct afs_vnode *dvnode = AFS_FS_I(dir);
1462 struct afs_fid newfid;
260a9803 1463 struct key *key;
63a4681f 1464 u64 data_version = dvnode->status.data_version;
260a9803
DH
1465 int ret;
1466
a455589f
AV
1467 _enter("{%x:%u},{%pd},%s",
1468 dvnode->fid.vid, dvnode->fid.vnode, dentry,
260a9803
DH
1469 content);
1470
d2ddc776
DH
1471 ret = -ENAMETOOLONG;
1472 if (dentry->d_name.len >= AFSNAMEMAX)
1473 goto error;
1474
260a9803 1475 ret = -EINVAL;
45222b9e 1476 if (strlen(content) >= AFSPATHMAX)
260a9803
DH
1477 goto error;
1478
1479 key = afs_request_key(dvnode->volume->cell);
1480 if (IS_ERR(key)) {
1481 ret = PTR_ERR(key);
1482 goto error;
1483 }
1484
d2ddc776
DH
1485 ret = -ERESTARTSYS;
1486 if (afs_begin_vnode_operation(&fc, dvnode, key)) {
1487 while (afs_select_fileserver(&fc)) {
68251f0a 1488 fc.cb_break = afs_calc_vnode_cb_break(dvnode);
63a4681f
DH
1489 afs_fs_symlink(&fc, dentry->d_name.name,
1490 content, data_version,
d2ddc776
DH
1491 &newfid, &newstatus);
1492 }
260a9803 1493
d2ddc776
DH
1494 afs_check_for_remote_deletion(&fc, fc.vnode);
1495 afs_vnode_commit_status(&fc, dvnode, fc.cb_break);
1496 afs_vnode_new_inode(&fc, dentry, &newfid, &newstatus, NULL);
1497 ret = afs_end_vnode_operation(&fc);
1498 if (ret < 0)
1499 goto error_key;
4433b691
DH
1500 } else {
1501 goto error_key;
260a9803
DH
1502 }
1503
63a4681f
DH
1504 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1505 afs_edit_dir_add(dvnode, &dentry->d_name, &newfid,
1506 afs_edit_dir_for_symlink);
1507
260a9803
DH
1508 key_put(key);
1509 _leave(" = 0");
1510 return 0;
1511
d2ddc776 1512error_key:
260a9803
DH
1513 key_put(key);
1514error:
1515 d_drop(dentry);
1516 _leave(" = %d", ret);
1517 return ret;
1518}
1519
1520/*
1521 * rename a file in an AFS filesystem and/or move it between directories
1522 */
1523static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
1cd66c93
MS
1524 struct inode *new_dir, struct dentry *new_dentry,
1525 unsigned int flags)
260a9803 1526{
d2ddc776 1527 struct afs_fs_cursor fc;
260a9803
DH
1528 struct afs_vnode *orig_dvnode, *new_dvnode, *vnode;
1529 struct key *key;
63a4681f
DH
1530 u64 orig_data_version, new_data_version;
1531 bool new_negative = d_is_negative(new_dentry);
260a9803
DH
1532 int ret;
1533
1cd66c93
MS
1534 if (flags)
1535 return -EINVAL;
1536
2b0143b5 1537 vnode = AFS_FS_I(d_inode(old_dentry));
260a9803
DH
1538 orig_dvnode = AFS_FS_I(old_dir);
1539 new_dvnode = AFS_FS_I(new_dir);
63a4681f
DH
1540 orig_data_version = orig_dvnode->status.data_version;
1541 new_data_version = new_dvnode->status.data_version;
260a9803 1542
a455589f 1543 _enter("{%x:%u},{%x:%u},{%x:%u},{%pd}",
260a9803
DH
1544 orig_dvnode->fid.vid, orig_dvnode->fid.vnode,
1545 vnode->fid.vid, vnode->fid.vnode,
1546 new_dvnode->fid.vid, new_dvnode->fid.vnode,
a455589f 1547 new_dentry);
260a9803 1548
260a9803
DH
1549 key = afs_request_key(orig_dvnode->volume->cell);
1550 if (IS_ERR(key)) {
1551 ret = PTR_ERR(key);
1552 goto error;
1553 }
1554
d2ddc776
DH
1555 ret = -ERESTARTSYS;
1556 if (afs_begin_vnode_operation(&fc, orig_dvnode, key)) {
1557 if (orig_dvnode != new_dvnode) {
1558 if (mutex_lock_interruptible_nested(&new_dvnode->io_lock, 1) < 0) {
1559 afs_end_vnode_operation(&fc);
bc1527dc 1560 goto error_key;
d2ddc776
DH
1561 }
1562 }
1563 while (afs_select_fileserver(&fc)) {
68251f0a
DH
1564 fc.cb_break = afs_calc_vnode_cb_break(orig_dvnode);
1565 fc.cb_break_2 = afs_calc_vnode_cb_break(new_dvnode);
d2ddc776 1566 afs_fs_rename(&fc, old_dentry->d_name.name,
63a4681f
DH
1567 new_dvnode, new_dentry->d_name.name,
1568 orig_data_version, new_data_version);
d2ddc776
DH
1569 }
1570
1571 afs_vnode_commit_status(&fc, orig_dvnode, fc.cb_break);
1572 afs_vnode_commit_status(&fc, new_dvnode, fc.cb_break_2);
1573 if (orig_dvnode != new_dvnode)
1574 mutex_unlock(&new_dvnode->io_lock);
1575 ret = afs_end_vnode_operation(&fc);
1576 if (ret < 0)
1577 goto error_key;
1578 }
1579
63a4681f
DH
1580 if (ret == 0) {
1581 if (test_bit(AFS_VNODE_DIR_VALID, &orig_dvnode->flags))
1582 afs_edit_dir_remove(orig_dvnode, &old_dentry->d_name,
1583 afs_edit_dir_for_rename);
1584
1585 if (!new_negative &&
1586 test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags))
1587 afs_edit_dir_remove(new_dvnode, &new_dentry->d_name,
1588 afs_edit_dir_for_rename);
1589
1590 if (test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags))
1591 afs_edit_dir_add(new_dvnode, &new_dentry->d_name,
1592 &vnode->fid, afs_edit_dir_for_rename);
1593 }
1594
d2ddc776 1595error_key:
260a9803
DH
1596 key_put(key);
1597error:
260a9803
DH
1598 _leave(" = %d", ret);
1599 return ret;
1600}
f3ddee8d
DH
1601
1602/*
1603 * Release a directory page and clean up its private state if it's not busy
1604 * - return true if the page can now be released, false if not
1605 */
1606static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags)
1607{
1608 struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host);
1609
1610 _enter("{{%x:%u}[%lu]}", dvnode->fid.vid, dvnode->fid.vnode, page->index);
1611
1612 set_page_private(page, 0);
1613 ClearPagePrivate(page);
1614
1615 /* The directory will need reloading. */
1616 if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1617 afs_stat_v(dvnode, n_relpg);
1618 return 1;
1619}
1620
1621/*
1622 * invalidate part or all of a page
1623 * - release a page and clean up its private data if offset is 0 (indicating
1624 * the entire page)
1625 */
1626static void afs_dir_invalidatepage(struct page *page, unsigned int offset,
1627 unsigned int length)
1628{
1629 struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host);
1630
1631 _enter("{%lu},%u,%u", page->index, offset, length);
1632
1633 BUG_ON(!PageLocked(page));
1634
1635 /* The directory will need reloading. */
1636 if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1637 afs_stat_v(dvnode, n_inval);
1638
1639 /* we clean up only if the entire page is being invalidated */
1640 if (offset == 0 && length == PAGE_SIZE) {
1641 set_page_private(page, 0);
1642 ClearPagePrivate(page);
1643 }
1644}