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