mtd: rawnand: ingenic: Fix ingenic_ecc dependency
[linux/fpc-iii.git] / fs / afs / dir.c
blobda9563d62b32717a5b03e6cd1d7506b356410974
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* dir.c: AFS filesystem directory handling
4 * Copyright (C) 2002, 2018 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 */
8 #include <linux/kernel.h>
9 #include <linux/fs.h>
10 #include <linux/namei.h>
11 #include <linux/pagemap.h>
12 #include <linux/swap.h>
13 #include <linux/ctype.h>
14 #include <linux/sched.h>
15 #include <linux/task_io_accounting_ops.h>
16 #include "internal.h"
17 #include "afs_fs.h"
18 #include "xdr_fs.h"
20 static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
21 unsigned int flags);
22 static int afs_dir_open(struct inode *inode, struct file *file);
23 static int afs_readdir(struct file *file, struct dir_context *ctx);
24 static int afs_d_revalidate(struct dentry *dentry, unsigned int flags);
25 static int afs_d_delete(const struct dentry *dentry);
26 static void afs_d_iput(struct dentry *dentry, struct inode *inode);
27 static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name, int nlen,
28 loff_t fpos, u64 ino, unsigned dtype);
29 static int afs_lookup_filldir(struct dir_context *ctx, const char *name, int nlen,
30 loff_t fpos, u64 ino, unsigned dtype);
31 static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
32 bool excl);
33 static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
34 static int afs_rmdir(struct inode *dir, struct dentry *dentry);
35 static int afs_unlink(struct inode *dir, struct dentry *dentry);
36 static int afs_link(struct dentry *from, struct inode *dir,
37 struct dentry *dentry);
38 static int afs_symlink(struct inode *dir, struct dentry *dentry,
39 const char *content);
40 static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
41 struct inode *new_dir, struct dentry *new_dentry,
42 unsigned int flags);
43 static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags);
44 static void afs_dir_invalidatepage(struct page *page, unsigned int offset,
45 unsigned int length);
47 static int afs_dir_set_page_dirty(struct page *page)
49 BUG(); /* This should never happen. */
52 const struct file_operations afs_dir_file_operations = {
53 .open = afs_dir_open,
54 .release = afs_release,
55 .iterate_shared = afs_readdir,
56 .lock = afs_lock,
57 .llseek = generic_file_llseek,
60 const struct inode_operations afs_dir_inode_operations = {
61 .create = afs_create,
62 .lookup = afs_lookup,
63 .link = afs_link,
64 .unlink = afs_unlink,
65 .symlink = afs_symlink,
66 .mkdir = afs_mkdir,
67 .rmdir = afs_rmdir,
68 .rename = afs_rename,
69 .permission = afs_permission,
70 .getattr = afs_getattr,
71 .setattr = afs_setattr,
72 .listxattr = afs_listxattr,
75 const struct address_space_operations afs_dir_aops = {
76 .set_page_dirty = afs_dir_set_page_dirty,
77 .releasepage = afs_dir_releasepage,
78 .invalidatepage = afs_dir_invalidatepage,
81 const struct dentry_operations afs_fs_dentry_operations = {
82 .d_revalidate = afs_d_revalidate,
83 .d_delete = afs_d_delete,
84 .d_release = afs_d_release,
85 .d_automount = afs_d_automount,
86 .d_iput = afs_d_iput,
89 struct afs_lookup_one_cookie {
90 struct dir_context ctx;
91 struct qstr name;
92 bool found;
93 struct afs_fid fid;
96 struct afs_lookup_cookie {
97 struct dir_context ctx;
98 struct qstr name;
99 bool found;
100 bool one_only;
101 unsigned short nr_fids;
102 struct inode **inodes;
103 struct afs_status_cb *statuses;
104 struct afs_fid fids[50];
108 * check that a directory page is valid
110 static bool afs_dir_check_page(struct afs_vnode *dvnode, struct page *page,
111 loff_t i_size)
113 struct afs_xdr_dir_page *dbuf;
114 loff_t latter, off;
115 int tmp, qty;
117 /* Determine how many magic numbers there should be in this page, but
118 * we must take care because the directory may change size under us.
120 off = page_offset(page);
121 if (i_size <= off)
122 goto checked;
124 latter = i_size - off;
125 if (latter >= PAGE_SIZE)
126 qty = PAGE_SIZE;
127 else
128 qty = latter;
129 qty /= sizeof(union afs_xdr_dir_block);
131 /* check them */
132 dbuf = kmap(page);
133 for (tmp = 0; tmp < qty; tmp++) {
134 if (dbuf->blocks[tmp].hdr.magic != AFS_DIR_MAGIC) {
135 printk("kAFS: %s(%lx): bad magic %d/%d is %04hx\n",
136 __func__, dvnode->vfs_inode.i_ino, tmp, qty,
137 ntohs(dbuf->blocks[tmp].hdr.magic));
138 trace_afs_dir_check_failed(dvnode, off, i_size);
139 kunmap(page);
140 trace_afs_file_error(dvnode, -EIO, afs_file_error_dir_bad_magic);
141 goto error;
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.
148 ((u8 *)&dbuf->blocks[tmp])[AFS_DIR_BLOCK_SIZE - 1] = 0;
151 kunmap(page);
153 checked:
154 afs_stat_v(dvnode, n_read_dir);
155 return true;
157 error:
158 return false;
162 * Check the contents of a directory that we've just read.
164 static bool afs_dir_check_pages(struct afs_vnode *dvnode, struct afs_read *req)
166 struct afs_xdr_dir_page *dbuf;
167 unsigned int i, j, qty = PAGE_SIZE / sizeof(union afs_xdr_dir_block);
169 for (i = 0; i < req->nr_pages; i++)
170 if (!afs_dir_check_page(dvnode, req->pages[i], req->actual_len))
171 goto bad;
172 return true;
174 bad:
175 pr_warn("DIR %llx:%llx f=%llx l=%llx al=%llx r=%llx\n",
176 dvnode->fid.vid, dvnode->fid.vnode,
177 req->file_size, req->len, req->actual_len, req->remain);
178 pr_warn("DIR %llx %x %x %x\n",
179 req->pos, req->index, req->nr_pages, req->offset);
181 for (i = 0; i < req->nr_pages; i++) {
182 dbuf = kmap(req->pages[i]);
183 for (j = 0; j < qty; j++) {
184 union afs_xdr_dir_block *block = &dbuf->blocks[j];
186 pr_warn("[%02x] %32phN\n", i * qty + j, block);
188 kunmap(req->pages[i]);
190 return false;
194 * open an AFS directory file
196 static int afs_dir_open(struct inode *inode, struct file *file)
198 _enter("{%lu}", inode->i_ino);
200 BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
201 BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
203 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(inode)->flags))
204 return -ENOENT;
206 return afs_open(inode, file);
210 * Read the directory into the pagecache in one go, scrubbing the previous
211 * contents. The list of pages is returned, pinning them so that they don't
212 * get reclaimed during the iteration.
214 static struct afs_read *afs_read_dir(struct afs_vnode *dvnode, struct key *key)
215 __acquires(&dvnode->validate_lock)
217 struct afs_read *req;
218 loff_t i_size;
219 int nr_pages, nr_inline, i, n;
220 int ret = -ENOMEM;
222 retry:
223 i_size = i_size_read(&dvnode->vfs_inode);
224 if (i_size < 2048)
225 return ERR_PTR(afs_bad(dvnode, afs_file_error_dir_small));
226 if (i_size > 2048 * 1024) {
227 trace_afs_file_error(dvnode, -EFBIG, afs_file_error_dir_big);
228 return ERR_PTR(-EFBIG);
231 _enter("%llu", i_size);
233 /* Get a request record to hold the page list. We want to hold it
234 * inline if we can, but we don't want to make an order 1 allocation.
236 nr_pages = (i_size + PAGE_SIZE - 1) / PAGE_SIZE;
237 nr_inline = nr_pages;
238 if (nr_inline > (PAGE_SIZE - sizeof(*req)) / sizeof(struct page *))
239 nr_inline = 0;
241 req = kzalloc(sizeof(*req) + sizeof(struct page *) * nr_inline,
242 GFP_KERNEL);
243 if (!req)
244 return ERR_PTR(-ENOMEM);
246 refcount_set(&req->usage, 1);
247 req->nr_pages = nr_pages;
248 req->actual_len = i_size; /* May change */
249 req->len = nr_pages * PAGE_SIZE; /* We can ask for more than there is */
250 req->data_version = dvnode->status.data_version; /* May change */
251 if (nr_inline > 0) {
252 req->pages = req->array;
253 } else {
254 req->pages = kcalloc(nr_pages, sizeof(struct page *),
255 GFP_KERNEL);
256 if (!req->pages)
257 goto error;
260 /* Get a list of all the pages that hold or will hold the directory
261 * content. We need to fill in any gaps that we might find where the
262 * memory reclaimer has been at work. If there are any gaps, we will
263 * need to reread the entire directory contents.
265 i = 0;
266 do {
267 n = find_get_pages_contig(dvnode->vfs_inode.i_mapping, i,
268 req->nr_pages - i,
269 req->pages + i);
270 _debug("find %u at %u/%u", n, i, req->nr_pages);
271 if (n == 0) {
272 gfp_t gfp = dvnode->vfs_inode.i_mapping->gfp_mask;
274 if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
275 afs_stat_v(dvnode, n_inval);
277 ret = -ENOMEM;
278 req->pages[i] = __page_cache_alloc(gfp);
279 if (!req->pages[i])
280 goto error;
281 ret = add_to_page_cache_lru(req->pages[i],
282 dvnode->vfs_inode.i_mapping,
283 i, gfp);
284 if (ret < 0)
285 goto error;
287 set_page_private(req->pages[i], 1);
288 SetPagePrivate(req->pages[i]);
289 unlock_page(req->pages[i]);
290 i++;
291 } else {
292 i += n;
294 } while (i < req->nr_pages);
296 /* If we're going to reload, we need to lock all the pages to prevent
297 * races.
299 ret = -ERESTARTSYS;
300 if (down_read_killable(&dvnode->validate_lock) < 0)
301 goto error;
303 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
304 goto success;
306 up_read(&dvnode->validate_lock);
307 if (down_write_killable(&dvnode->validate_lock) < 0)
308 goto error;
310 if (!test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) {
311 trace_afs_reload_dir(dvnode);
312 ret = afs_fetch_data(dvnode, key, req);
313 if (ret < 0)
314 goto error_unlock;
316 task_io_account_read(PAGE_SIZE * req->nr_pages);
318 if (req->len < req->file_size)
319 goto content_has_grown;
321 /* Validate the data we just read. */
322 ret = -EIO;
323 if (!afs_dir_check_pages(dvnode, req))
324 goto error_unlock;
326 // TODO: Trim excess pages
328 set_bit(AFS_VNODE_DIR_VALID, &dvnode->flags);
331 downgrade_write(&dvnode->validate_lock);
332 success:
333 return req;
335 error_unlock:
336 up_write(&dvnode->validate_lock);
337 error:
338 afs_put_read(req);
339 _leave(" = %d", ret);
340 return ERR_PTR(ret);
342 content_has_grown:
343 up_write(&dvnode->validate_lock);
344 afs_put_read(req);
345 goto retry;
349 * deal with one block in an AFS directory
351 static int afs_dir_iterate_block(struct afs_vnode *dvnode,
352 struct dir_context *ctx,
353 union afs_xdr_dir_block *block,
354 unsigned blkoff)
356 union afs_xdr_dirent *dire;
357 unsigned offset, next, curr;
358 size_t nlen;
359 int tmp;
361 _enter("%u,%x,%p,,",(unsigned)ctx->pos,blkoff,block);
363 curr = (ctx->pos - blkoff) / sizeof(union afs_xdr_dirent);
365 /* walk through the block, an entry at a time */
366 for (offset = (blkoff == 0 ? AFS_DIR_RESV_BLOCKS0 : AFS_DIR_RESV_BLOCKS);
367 offset < AFS_DIR_SLOTS_PER_BLOCK;
368 offset = next
370 next = offset + 1;
372 /* skip entries marked unused in the bitmap */
373 if (!(block->hdr.bitmap[offset / 8] &
374 (1 << (offset % 8)))) {
375 _debug("ENT[%zu.%u]: unused",
376 blkoff / sizeof(union afs_xdr_dir_block), offset);
377 if (offset >= curr)
378 ctx->pos = blkoff +
379 next * sizeof(union afs_xdr_dirent);
380 continue;
383 /* got a valid entry */
384 dire = &block->dirents[offset];
385 nlen = strnlen(dire->u.name,
386 sizeof(*block) -
387 offset * sizeof(union afs_xdr_dirent));
389 _debug("ENT[%zu.%u]: %s %zu \"%s\"",
390 blkoff / sizeof(union afs_xdr_dir_block), offset,
391 (offset < curr ? "skip" : "fill"),
392 nlen, dire->u.name);
394 /* work out where the next possible entry is */
395 for (tmp = nlen; tmp > 15; tmp -= sizeof(union afs_xdr_dirent)) {
396 if (next >= AFS_DIR_SLOTS_PER_BLOCK) {
397 _debug("ENT[%zu.%u]:"
398 " %u travelled beyond end dir block"
399 " (len %u/%zu)",
400 blkoff / sizeof(union afs_xdr_dir_block),
401 offset, next, tmp, nlen);
402 return afs_bad(dvnode, afs_file_error_dir_over_end);
404 if (!(block->hdr.bitmap[next / 8] &
405 (1 << (next % 8)))) {
406 _debug("ENT[%zu.%u]:"
407 " %u unmarked extension (len %u/%zu)",
408 blkoff / sizeof(union afs_xdr_dir_block),
409 offset, next, tmp, nlen);
410 return afs_bad(dvnode, afs_file_error_dir_unmarked_ext);
413 _debug("ENT[%zu.%u]: ext %u/%zu",
414 blkoff / sizeof(union afs_xdr_dir_block),
415 next, tmp, nlen);
416 next++;
419 /* skip if starts before the current position */
420 if (offset < curr)
421 continue;
423 /* found the next entry */
424 if (!dir_emit(ctx, dire->u.name, nlen,
425 ntohl(dire->u.vnode),
426 (ctx->actor == afs_lookup_filldir ||
427 ctx->actor == afs_lookup_one_filldir)?
428 ntohl(dire->u.unique) : DT_UNKNOWN)) {
429 _leave(" = 0 [full]");
430 return 0;
433 ctx->pos = blkoff + next * sizeof(union afs_xdr_dirent);
436 _leave(" = 1 [more]");
437 return 1;
441 * iterate through the data blob that lists the contents of an AFS directory
443 static int afs_dir_iterate(struct inode *dir, struct dir_context *ctx,
444 struct key *key)
446 struct afs_vnode *dvnode = AFS_FS_I(dir);
447 struct afs_xdr_dir_page *dbuf;
448 union afs_xdr_dir_block *dblock;
449 struct afs_read *req;
450 struct page *page;
451 unsigned blkoff, limit;
452 int ret;
454 _enter("{%lu},%u,,", dir->i_ino, (unsigned)ctx->pos);
456 if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) {
457 _leave(" = -ESTALE");
458 return -ESTALE;
461 req = afs_read_dir(dvnode, key);
462 if (IS_ERR(req))
463 return PTR_ERR(req);
465 /* round the file position up to the next entry boundary */
466 ctx->pos += sizeof(union afs_xdr_dirent) - 1;
467 ctx->pos &= ~(sizeof(union afs_xdr_dirent) - 1);
469 /* walk through the blocks in sequence */
470 ret = 0;
471 while (ctx->pos < req->actual_len) {
472 blkoff = ctx->pos & ~(sizeof(union afs_xdr_dir_block) - 1);
474 /* Fetch the appropriate page from the directory and re-add it
475 * to the LRU.
477 page = req->pages[blkoff / PAGE_SIZE];
478 if (!page) {
479 ret = afs_bad(dvnode, afs_file_error_dir_missing_page);
480 break;
482 mark_page_accessed(page);
484 limit = blkoff & ~(PAGE_SIZE - 1);
486 dbuf = kmap(page);
488 /* deal with the individual blocks stashed on this page */
489 do {
490 dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) /
491 sizeof(union afs_xdr_dir_block)];
492 ret = afs_dir_iterate_block(dvnode, ctx, dblock, blkoff);
493 if (ret != 1) {
494 kunmap(page);
495 goto out;
498 blkoff += sizeof(union afs_xdr_dir_block);
500 } while (ctx->pos < dir->i_size && blkoff < limit);
502 kunmap(page);
503 ret = 0;
506 out:
507 up_read(&dvnode->validate_lock);
508 afs_put_read(req);
509 _leave(" = %d", ret);
510 return ret;
514 * read an AFS directory
516 static int afs_readdir(struct file *file, struct dir_context *ctx)
518 return afs_dir_iterate(file_inode(file), ctx, afs_file_key(file));
522 * Search the directory for a single name
523 * - if afs_dir_iterate_block() spots this function, it'll pass the FID
524 * uniquifier through dtype
526 static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name,
527 int nlen, loff_t fpos, u64 ino, unsigned dtype)
529 struct afs_lookup_one_cookie *cookie =
530 container_of(ctx, struct afs_lookup_one_cookie, ctx);
532 _enter("{%s,%u},%s,%u,,%llu,%u",
533 cookie->name.name, cookie->name.len, name, nlen,
534 (unsigned long long) ino, dtype);
536 /* insanity checks first */
537 BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
538 BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
540 if (cookie->name.len != nlen ||
541 memcmp(cookie->name.name, name, nlen) != 0) {
542 _leave(" = 0 [no]");
543 return 0;
546 cookie->fid.vnode = ino;
547 cookie->fid.unique = dtype;
548 cookie->found = 1;
550 _leave(" = -1 [found]");
551 return -1;
555 * Do a lookup of a single name in a directory
556 * - just returns the FID the dentry name maps to if found
558 static int afs_do_lookup_one(struct inode *dir, struct dentry *dentry,
559 struct afs_fid *fid, struct key *key)
561 struct afs_super_info *as = dir->i_sb->s_fs_info;
562 struct afs_lookup_one_cookie cookie = {
563 .ctx.actor = afs_lookup_one_filldir,
564 .name = dentry->d_name,
565 .fid.vid = as->volume->vid
567 int ret;
569 _enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
571 /* search the directory */
572 ret = afs_dir_iterate(dir, &cookie.ctx, key);
573 if (ret < 0) {
574 _leave(" = %d [iter]", ret);
575 return ret;
578 ret = -ENOENT;
579 if (!cookie.found) {
580 _leave(" = -ENOENT [not found]");
581 return -ENOENT;
584 *fid = cookie.fid;
585 _leave(" = 0 { vn=%llu u=%u }", fid->vnode, fid->unique);
586 return 0;
590 * search the directory for a name
591 * - if afs_dir_iterate_block() spots this function, it'll pass the FID
592 * uniquifier through dtype
594 static int afs_lookup_filldir(struct dir_context *ctx, const char *name,
595 int nlen, loff_t fpos, u64 ino, unsigned dtype)
597 struct afs_lookup_cookie *cookie =
598 container_of(ctx, struct afs_lookup_cookie, ctx);
599 int ret;
601 _enter("{%s,%u},%s,%u,,%llu,%u",
602 cookie->name.name, cookie->name.len, name, nlen,
603 (unsigned long long) ino, dtype);
605 /* insanity checks first */
606 BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048);
607 BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32);
609 if (cookie->found) {
610 if (cookie->nr_fids < 50) {
611 cookie->fids[cookie->nr_fids].vnode = ino;
612 cookie->fids[cookie->nr_fids].unique = dtype;
613 cookie->nr_fids++;
615 } else if (cookie->name.len == nlen &&
616 memcmp(cookie->name.name, name, nlen) == 0) {
617 cookie->fids[0].vnode = ino;
618 cookie->fids[0].unique = dtype;
619 cookie->found = 1;
620 if (cookie->one_only)
621 return -1;
624 ret = cookie->nr_fids >= 50 ? -1 : 0;
625 _leave(" = %d", ret);
626 return ret;
630 * Do a lookup in a directory. We make use of bulk lookup to query a slew of
631 * files in one go and create inodes for them. The inode of the file we were
632 * asked for is returned.
634 static struct inode *afs_do_lookup(struct inode *dir, struct dentry *dentry,
635 struct key *key)
637 struct afs_lookup_cookie *cookie;
638 struct afs_cb_interest *dcbi, *cbi = NULL;
639 struct afs_super_info *as = dir->i_sb->s_fs_info;
640 struct afs_status_cb *scb;
641 struct afs_iget_data iget_data;
642 struct afs_fs_cursor fc;
643 struct afs_server *server;
644 struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode;
645 struct inode *inode = NULL, *ti;
646 int ret, i;
648 _enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry);
650 cookie = kzalloc(sizeof(struct afs_lookup_cookie), GFP_KERNEL);
651 if (!cookie)
652 return ERR_PTR(-ENOMEM);
654 cookie->ctx.actor = afs_lookup_filldir;
655 cookie->name = dentry->d_name;
656 cookie->nr_fids = 1; /* slot 0 is saved for the fid we actually want */
658 read_seqlock_excl(&dvnode->cb_lock);
659 dcbi = rcu_dereference_protected(dvnode->cb_interest,
660 lockdep_is_held(&dvnode->cb_lock.lock));
661 if (dcbi) {
662 server = dcbi->server;
663 if (server &&
664 test_bit(AFS_SERVER_FL_NO_IBULK, &server->flags))
665 cookie->one_only = true;
667 read_sequnlock_excl(&dvnode->cb_lock);
669 for (i = 0; i < 50; i++)
670 cookie->fids[i].vid = as->volume->vid;
672 /* search the directory */
673 ret = afs_dir_iterate(dir, &cookie->ctx, key);
674 if (ret < 0) {
675 inode = ERR_PTR(ret);
676 goto out;
679 inode = ERR_PTR(-ENOENT);
680 if (!cookie->found)
681 goto out;
683 /* Check to see if we already have an inode for the primary fid. */
684 iget_data.fid = cookie->fids[0];
685 iget_data.volume = dvnode->volume;
686 iget_data.cb_v_break = dvnode->volume->cb_v_break;
687 iget_data.cb_s_break = 0;
688 inode = ilookup5(dir->i_sb, cookie->fids[0].vnode,
689 afs_iget5_test, &iget_data);
690 if (inode)
691 goto out;
693 /* Need space for examining all the selected files */
694 inode = ERR_PTR(-ENOMEM);
695 cookie->statuses = kvcalloc(cookie->nr_fids, sizeof(struct afs_status_cb),
696 GFP_KERNEL);
697 if (!cookie->statuses)
698 goto out;
700 cookie->inodes = kcalloc(cookie->nr_fids, sizeof(struct inode *),
701 GFP_KERNEL);
702 if (!cookie->inodes)
703 goto out_s;
705 for (i = 1; i < cookie->nr_fids; i++) {
706 scb = &cookie->statuses[i];
708 /* Find any inodes that already exist and get their
709 * callback counters.
711 iget_data.fid = cookie->fids[i];
712 ti = ilookup5_nowait(dir->i_sb, iget_data.fid.vnode,
713 afs_iget5_test, &iget_data);
714 if (!IS_ERR_OR_NULL(ti)) {
715 vnode = AFS_FS_I(ti);
716 scb->cb_break = afs_calc_vnode_cb_break(vnode);
717 cookie->inodes[i] = ti;
721 /* Try FS.InlineBulkStatus first. Abort codes for the individual
722 * lookups contained therein are stored in the reply without aborting
723 * the whole operation.
725 if (cookie->one_only)
726 goto no_inline_bulk_status;
728 inode = ERR_PTR(-ERESTARTSYS);
729 if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
730 while (afs_select_fileserver(&fc)) {
731 if (test_bit(AFS_SERVER_FL_NO_IBULK,
732 &fc.cbi->server->flags)) {
733 fc.ac.abort_code = RX_INVALID_OPERATION;
734 fc.ac.error = -ECONNABORTED;
735 break;
737 iget_data.cb_v_break = dvnode->volume->cb_v_break;
738 iget_data.cb_s_break = fc.cbi->server->cb_s_break;
739 afs_fs_inline_bulk_status(&fc,
740 afs_v2net(dvnode),
741 cookie->fids,
742 cookie->statuses,
743 cookie->nr_fids, NULL);
746 if (fc.ac.error == 0)
747 cbi = afs_get_cb_interest(fc.cbi);
748 if (fc.ac.abort_code == RX_INVALID_OPERATION)
749 set_bit(AFS_SERVER_FL_NO_IBULK, &fc.cbi->server->flags);
750 inode = ERR_PTR(afs_end_vnode_operation(&fc));
753 if (!IS_ERR(inode))
754 goto success;
755 if (fc.ac.abort_code != RX_INVALID_OPERATION)
756 goto out_c;
758 no_inline_bulk_status:
759 /* We could try FS.BulkStatus next, but this aborts the entire op if
760 * any of the lookups fails - so, for the moment, revert to
761 * FS.FetchStatus for just the primary fid.
763 inode = ERR_PTR(-ERESTARTSYS);
764 if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
765 while (afs_select_fileserver(&fc)) {
766 iget_data.cb_v_break = dvnode->volume->cb_v_break;
767 iget_data.cb_s_break = fc.cbi->server->cb_s_break;
768 scb = &cookie->statuses[0];
769 afs_fs_fetch_status(&fc,
770 afs_v2net(dvnode),
771 cookie->fids,
772 scb,
773 NULL);
776 if (fc.ac.error == 0)
777 cbi = afs_get_cb_interest(fc.cbi);
778 inode = ERR_PTR(afs_end_vnode_operation(&fc));
781 if (IS_ERR(inode))
782 goto out_c;
784 success:
785 /* Turn all the files into inodes and save the first one - which is the
786 * one we actually want.
788 scb = &cookie->statuses[0];
789 if (scb->status.abort_code != 0)
790 inode = ERR_PTR(afs_abort_to_error(scb->status.abort_code));
792 for (i = 0; i < cookie->nr_fids; i++) {
793 struct afs_status_cb *scb = &cookie->statuses[i];
795 if (!scb->have_status && !scb->have_error)
796 continue;
798 if (cookie->inodes[i]) {
799 afs_vnode_commit_status(&fc, AFS_FS_I(cookie->inodes[i]),
800 scb->cb_break, NULL, scb);
801 continue;
804 if (scb->status.abort_code != 0)
805 continue;
807 iget_data.fid = cookie->fids[i];
808 ti = afs_iget(dir->i_sb, key, &iget_data, scb, cbi, dvnode);
809 if (!IS_ERR(ti))
810 afs_cache_permit(AFS_FS_I(ti), key,
811 0 /* Assume vnode->cb_break is 0 */ +
812 iget_data.cb_v_break,
813 scb);
814 if (i == 0) {
815 inode = ti;
816 } else {
817 if (!IS_ERR(ti))
818 iput(ti);
822 out_c:
823 afs_put_cb_interest(afs_v2net(dvnode), cbi);
824 if (cookie->inodes) {
825 for (i = 0; i < cookie->nr_fids; i++)
826 iput(cookie->inodes[i]);
827 kfree(cookie->inodes);
829 out_s:
830 kvfree(cookie->statuses);
831 out:
832 kfree(cookie);
833 return inode;
837 * Look up an entry in a directory with @sys substitution.
839 static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry,
840 struct key *key)
842 struct afs_sysnames *subs;
843 struct afs_net *net = afs_i2net(dir);
844 struct dentry *ret;
845 char *buf, *p, *name;
846 int len, i;
848 _enter("");
850 ret = ERR_PTR(-ENOMEM);
851 p = buf = kmalloc(AFSNAMEMAX, GFP_KERNEL);
852 if (!buf)
853 goto out_p;
854 if (dentry->d_name.len > 4) {
855 memcpy(p, dentry->d_name.name, dentry->d_name.len - 4);
856 p += dentry->d_name.len - 4;
859 /* There is an ordered list of substitutes that we have to try. */
860 read_lock(&net->sysnames_lock);
861 subs = net->sysnames;
862 refcount_inc(&subs->usage);
863 read_unlock(&net->sysnames_lock);
865 for (i = 0; i < subs->nr; i++) {
866 name = subs->subs[i];
867 len = dentry->d_name.len - 4 + strlen(name);
868 if (len >= AFSNAMEMAX) {
869 ret = ERR_PTR(-ENAMETOOLONG);
870 goto out_s;
873 strcpy(p, name);
874 ret = lookup_one_len(buf, dentry->d_parent, len);
875 if (IS_ERR(ret) || d_is_positive(ret))
876 goto out_s;
877 dput(ret);
880 /* We don't want to d_add() the @sys dentry here as we don't want to
881 * the cached dentry to hide changes to the sysnames list.
883 ret = NULL;
884 out_s:
885 afs_put_sysnames(subs);
886 kfree(buf);
887 out_p:
888 key_put(key);
889 return ret;
893 * look up an entry in a directory
895 static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry,
896 unsigned int flags)
898 struct afs_vnode *dvnode = AFS_FS_I(dir);
899 struct inode *inode;
900 struct dentry *d;
901 struct key *key;
902 int ret;
904 _enter("{%llx:%llu},%p{%pd},",
905 dvnode->fid.vid, dvnode->fid.vnode, dentry, dentry);
907 ASSERTCMP(d_inode(dentry), ==, NULL);
909 if (dentry->d_name.len >= AFSNAMEMAX) {
910 _leave(" = -ENAMETOOLONG");
911 return ERR_PTR(-ENAMETOOLONG);
914 if (test_bit(AFS_VNODE_DELETED, &dvnode->flags)) {
915 _leave(" = -ESTALE");
916 return ERR_PTR(-ESTALE);
919 key = afs_request_key(dvnode->volume->cell);
920 if (IS_ERR(key)) {
921 _leave(" = %ld [key]", PTR_ERR(key));
922 return ERR_CAST(key);
925 ret = afs_validate(dvnode, key);
926 if (ret < 0) {
927 key_put(key);
928 _leave(" = %d [val]", ret);
929 return ERR_PTR(ret);
932 if (dentry->d_name.len >= 4 &&
933 dentry->d_name.name[dentry->d_name.len - 4] == '@' &&
934 dentry->d_name.name[dentry->d_name.len - 3] == 's' &&
935 dentry->d_name.name[dentry->d_name.len - 2] == 'y' &&
936 dentry->d_name.name[dentry->d_name.len - 1] == 's')
937 return afs_lookup_atsys(dir, dentry, key);
939 afs_stat_v(dvnode, n_lookup);
940 inode = afs_do_lookup(dir, dentry, key);
941 key_put(key);
942 if (inode == ERR_PTR(-ENOENT)) {
943 inode = afs_try_auto_mntpt(dentry, dir);
944 } else {
945 dentry->d_fsdata =
946 (void *)(unsigned long)dvnode->status.data_version;
948 d = d_splice_alias(inode, dentry);
949 if (!IS_ERR_OR_NULL(d)) {
950 d->d_fsdata = dentry->d_fsdata;
951 trace_afs_lookup(dvnode, &d->d_name,
952 inode ? AFS_FS_I(inode) : NULL);
953 } else {
954 trace_afs_lookup(dvnode, &dentry->d_name,
955 inode ? AFS_FS_I(inode) : NULL);
957 return d;
961 * check that a dentry lookup hit has found a valid entry
962 * - NOTE! the hit can be a negative hit too, so we can't assume we have an
963 * inode
965 static int afs_d_revalidate(struct dentry *dentry, unsigned int flags)
967 struct afs_vnode *vnode, *dir;
968 struct afs_fid uninitialized_var(fid);
969 struct dentry *parent;
970 struct inode *inode;
971 struct key *key;
972 long dir_version, de_version;
973 int ret;
975 if (flags & LOOKUP_RCU)
976 return -ECHILD;
978 if (d_really_is_positive(dentry)) {
979 vnode = AFS_FS_I(d_inode(dentry));
980 _enter("{v={%llx:%llu} n=%pd fl=%lx},",
981 vnode->fid.vid, vnode->fid.vnode, dentry,
982 vnode->flags);
983 } else {
984 _enter("{neg n=%pd}", dentry);
987 key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell);
988 if (IS_ERR(key))
989 key = NULL;
991 if (d_really_is_positive(dentry)) {
992 inode = d_inode(dentry);
993 if (inode) {
994 vnode = AFS_FS_I(inode);
995 afs_validate(vnode, key);
996 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
997 goto out_bad;
1001 /* lock down the parent dentry so we can peer at it */
1002 parent = dget_parent(dentry);
1003 dir = AFS_FS_I(d_inode(parent));
1005 /* validate the parent directory */
1006 afs_validate(dir, key);
1008 if (test_bit(AFS_VNODE_DELETED, &dir->flags)) {
1009 _debug("%pd: parent dir deleted", dentry);
1010 goto out_bad_parent;
1013 /* We only need to invalidate a dentry if the server's copy changed
1014 * behind our back. If we made the change, it's no problem. Note that
1015 * on a 32-bit system, we only have 32 bits in the dentry to store the
1016 * version.
1018 dir_version = (long)dir->status.data_version;
1019 de_version = (long)dentry->d_fsdata;
1020 if (de_version == dir_version)
1021 goto out_valid;
1023 dir_version = (long)dir->invalid_before;
1024 if (de_version - dir_version >= 0)
1025 goto out_valid;
1027 _debug("dir modified");
1028 afs_stat_v(dir, n_reval);
1030 /* search the directory for this vnode */
1031 ret = afs_do_lookup_one(&dir->vfs_inode, dentry, &fid, key);
1032 switch (ret) {
1033 case 0:
1034 /* the filename maps to something */
1035 if (d_really_is_negative(dentry))
1036 goto out_bad_parent;
1037 inode = d_inode(dentry);
1038 if (is_bad_inode(inode)) {
1039 printk("kAFS: afs_d_revalidate: %pd2 has bad inode\n",
1040 dentry);
1041 goto out_bad_parent;
1044 vnode = AFS_FS_I(inode);
1046 /* if the vnode ID has changed, then the dirent points to a
1047 * different file */
1048 if (fid.vnode != vnode->fid.vnode) {
1049 _debug("%pd: dirent changed [%llu != %llu]",
1050 dentry, fid.vnode,
1051 vnode->fid.vnode);
1052 goto not_found;
1055 /* if the vnode ID uniqifier has changed, then the file has
1056 * been deleted and replaced, and the original vnode ID has
1057 * been reused */
1058 if (fid.unique != vnode->fid.unique) {
1059 _debug("%pd: file deleted (uq %u -> %u I:%u)",
1060 dentry, fid.unique,
1061 vnode->fid.unique,
1062 vnode->vfs_inode.i_generation);
1063 write_seqlock(&vnode->cb_lock);
1064 set_bit(AFS_VNODE_DELETED, &vnode->flags);
1065 write_sequnlock(&vnode->cb_lock);
1066 goto not_found;
1068 goto out_valid;
1070 case -ENOENT:
1071 /* the filename is unknown */
1072 _debug("%pd: dirent not found", dentry);
1073 if (d_really_is_positive(dentry))
1074 goto not_found;
1075 goto out_valid;
1077 default:
1078 _debug("failed to iterate dir %pd: %d",
1079 parent, ret);
1080 goto out_bad_parent;
1083 out_valid:
1084 dentry->d_fsdata = (void *)dir_version;
1085 dput(parent);
1086 key_put(key);
1087 _leave(" = 1 [valid]");
1088 return 1;
1090 /* the dirent, if it exists, now points to a different vnode */
1091 not_found:
1092 spin_lock(&dentry->d_lock);
1093 dentry->d_flags |= DCACHE_NFSFS_RENAMED;
1094 spin_unlock(&dentry->d_lock);
1096 out_bad_parent:
1097 _debug("dropping dentry %pd2", dentry);
1098 dput(parent);
1099 out_bad:
1100 key_put(key);
1102 _leave(" = 0 [bad]");
1103 return 0;
1107 * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't
1108 * sleep)
1109 * - called from dput() when d_count is going to 0.
1110 * - return 1 to request dentry be unhashed, 0 otherwise
1112 static int afs_d_delete(const struct dentry *dentry)
1114 _enter("%pd", dentry);
1116 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1117 goto zap;
1119 if (d_really_is_positive(dentry) &&
1120 (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(d_inode(dentry))->flags) ||
1121 test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(d_inode(dentry))->flags)))
1122 goto zap;
1124 _leave(" = 0 [keep]");
1125 return 0;
1127 zap:
1128 _leave(" = 1 [zap]");
1129 return 1;
1133 * Clean up sillyrename files on dentry removal.
1135 static void afs_d_iput(struct dentry *dentry, struct inode *inode)
1137 if (dentry->d_flags & DCACHE_NFSFS_RENAMED)
1138 afs_silly_iput(dentry, inode);
1139 iput(inode);
1143 * handle dentry release
1145 void afs_d_release(struct dentry *dentry)
1147 _enter("%pd", dentry);
1151 * Create a new inode for create/mkdir/symlink
1153 static void afs_vnode_new_inode(struct afs_fs_cursor *fc,
1154 struct dentry *new_dentry,
1155 struct afs_iget_data *new_data,
1156 struct afs_status_cb *new_scb)
1158 struct afs_vnode *vnode;
1159 struct inode *inode;
1161 if (fc->ac.error < 0)
1162 return;
1164 inode = afs_iget(fc->vnode->vfs_inode.i_sb, fc->key,
1165 new_data, new_scb, fc->cbi, fc->vnode);
1166 if (IS_ERR(inode)) {
1167 /* ENOMEM or EINTR at a really inconvenient time - just abandon
1168 * the new directory on the server.
1170 fc->ac.error = PTR_ERR(inode);
1171 return;
1174 vnode = AFS_FS_I(inode);
1175 set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
1176 if (fc->ac.error == 0)
1177 afs_cache_permit(vnode, fc->key, vnode->cb_break, new_scb);
1178 d_instantiate(new_dentry, inode);
1181 static void afs_prep_for_new_inode(struct afs_fs_cursor *fc,
1182 struct afs_iget_data *iget_data)
1184 iget_data->volume = fc->vnode->volume;
1185 iget_data->cb_v_break = fc->vnode->volume->cb_v_break;
1186 iget_data->cb_s_break = fc->cbi->server->cb_s_break;
1190 * create a directory on an AFS filesystem
1192 static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1194 struct afs_iget_data iget_data;
1195 struct afs_status_cb *scb;
1196 struct afs_fs_cursor fc;
1197 struct afs_vnode *dvnode = AFS_FS_I(dir);
1198 struct key *key;
1199 int ret;
1201 mode |= S_IFDIR;
1203 _enter("{%llx:%llu},{%pd},%ho",
1204 dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
1206 ret = -ENOMEM;
1207 scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1208 if (!scb)
1209 goto error;
1211 key = afs_request_key(dvnode->volume->cell);
1212 if (IS_ERR(key)) {
1213 ret = PTR_ERR(key);
1214 goto error_scb;
1217 ret = -ERESTARTSYS;
1218 if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1219 afs_dataversion_t data_version = dvnode->status.data_version + 1;
1221 while (afs_select_fileserver(&fc)) {
1222 fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1223 afs_prep_for_new_inode(&fc, &iget_data);
1224 afs_fs_create(&fc, dentry->d_name.name, mode,
1225 &scb[0], &iget_data.fid, &scb[1]);
1228 afs_check_for_remote_deletion(&fc, dvnode);
1229 afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1230 &data_version, &scb[0]);
1231 afs_vnode_new_inode(&fc, dentry, &iget_data, &scb[1]);
1232 ret = afs_end_vnode_operation(&fc);
1233 if (ret < 0)
1234 goto error_key;
1235 } else {
1236 goto error_key;
1239 if (ret == 0 &&
1240 test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1241 afs_edit_dir_add(dvnode, &dentry->d_name, &iget_data.fid,
1242 afs_edit_dir_for_create);
1244 key_put(key);
1245 kfree(scb);
1246 _leave(" = 0");
1247 return 0;
1249 error_key:
1250 key_put(key);
1251 error_scb:
1252 kfree(scb);
1253 error:
1254 d_drop(dentry);
1255 _leave(" = %d", ret);
1256 return ret;
1260 * Remove a subdir from a directory.
1262 static void afs_dir_remove_subdir(struct dentry *dentry)
1264 if (d_really_is_positive(dentry)) {
1265 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1267 clear_nlink(&vnode->vfs_inode);
1268 set_bit(AFS_VNODE_DELETED, &vnode->flags);
1269 clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
1270 clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
1275 * remove a directory from an AFS filesystem
1277 static int afs_rmdir(struct inode *dir, struct dentry *dentry)
1279 struct afs_status_cb *scb;
1280 struct afs_fs_cursor fc;
1281 struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode = NULL;
1282 struct key *key;
1283 int ret;
1285 _enter("{%llx:%llu},{%pd}",
1286 dvnode->fid.vid, dvnode->fid.vnode, dentry);
1288 scb = kzalloc(sizeof(struct afs_status_cb), GFP_KERNEL);
1289 if (!scb)
1290 return -ENOMEM;
1292 key = afs_request_key(dvnode->volume->cell);
1293 if (IS_ERR(key)) {
1294 ret = PTR_ERR(key);
1295 goto error;
1298 /* Try to make sure we have a callback promise on the victim. */
1299 if (d_really_is_positive(dentry)) {
1300 vnode = AFS_FS_I(d_inode(dentry));
1301 ret = afs_validate(vnode, key);
1302 if (ret < 0)
1303 goto error_key;
1306 if (vnode) {
1307 ret = down_write_killable(&vnode->rmdir_lock);
1308 if (ret < 0)
1309 goto error_key;
1312 ret = -ERESTARTSYS;
1313 if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1314 afs_dataversion_t data_version = dvnode->status.data_version + 1;
1316 while (afs_select_fileserver(&fc)) {
1317 fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1318 afs_fs_remove(&fc, vnode, dentry->d_name.name, true, scb);
1321 afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1322 &data_version, scb);
1323 ret = afs_end_vnode_operation(&fc);
1324 if (ret == 0) {
1325 afs_dir_remove_subdir(dentry);
1326 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1327 afs_edit_dir_remove(dvnode, &dentry->d_name,
1328 afs_edit_dir_for_rmdir);
1332 if (vnode)
1333 up_write(&vnode->rmdir_lock);
1334 error_key:
1335 key_put(key);
1336 error:
1337 kfree(scb);
1338 return ret;
1342 * Remove a link to a file or symlink from a directory.
1344 * If the file was not deleted due to excess hard links, the fileserver will
1345 * break the callback promise on the file - if it had one - before it returns
1346 * to us, and if it was deleted, it won't
1348 * However, if we didn't have a callback promise outstanding, or it was
1349 * outstanding on a different server, then it won't break it either...
1351 static int afs_dir_remove_link(struct afs_vnode *dvnode, struct dentry *dentry,
1352 struct key *key)
1354 int ret = 0;
1356 if (d_really_is_positive(dentry)) {
1357 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
1359 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
1360 /* Already done */
1361 } else if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) {
1362 write_seqlock(&vnode->cb_lock);
1363 drop_nlink(&vnode->vfs_inode);
1364 if (vnode->vfs_inode.i_nlink == 0) {
1365 set_bit(AFS_VNODE_DELETED, &vnode->flags);
1366 __afs_break_callback(vnode);
1368 write_sequnlock(&vnode->cb_lock);
1369 ret = 0;
1370 } else {
1371 afs_break_callback(vnode);
1373 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
1374 kdebug("AFS_VNODE_DELETED");
1376 ret = afs_validate(vnode, key);
1377 if (ret == -ESTALE)
1378 ret = 0;
1380 _debug("nlink %d [val %d]", vnode->vfs_inode.i_nlink, ret);
1383 return ret;
1387 * Remove a file or symlink from an AFS filesystem.
1389 static int afs_unlink(struct inode *dir, struct dentry *dentry)
1391 struct afs_fs_cursor fc;
1392 struct afs_status_cb *scb;
1393 struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode = NULL;
1394 struct key *key;
1395 bool need_rehash = false;
1396 int ret;
1398 _enter("{%llx:%llu},{%pd}",
1399 dvnode->fid.vid, dvnode->fid.vnode, dentry);
1401 if (dentry->d_name.len >= AFSNAMEMAX)
1402 return -ENAMETOOLONG;
1404 ret = -ENOMEM;
1405 scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1406 if (!scb)
1407 goto error;
1409 key = afs_request_key(dvnode->volume->cell);
1410 if (IS_ERR(key)) {
1411 ret = PTR_ERR(key);
1412 goto error_scb;
1415 /* Try to make sure we have a callback promise on the victim. */
1416 if (d_really_is_positive(dentry)) {
1417 vnode = AFS_FS_I(d_inode(dentry));
1418 ret = afs_validate(vnode, key);
1419 if (ret < 0)
1420 goto error_key;
1423 spin_lock(&dentry->d_lock);
1424 if (vnode && d_count(dentry) > 1) {
1425 spin_unlock(&dentry->d_lock);
1426 /* Start asynchronous writeout of the inode */
1427 write_inode_now(d_inode(dentry), 0);
1428 ret = afs_sillyrename(dvnode, vnode, dentry, key);
1429 goto error_key;
1431 if (!d_unhashed(dentry)) {
1432 /* Prevent a race with RCU lookup. */
1433 __d_drop(dentry);
1434 need_rehash = true;
1436 spin_unlock(&dentry->d_lock);
1438 ret = -ERESTARTSYS;
1439 if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1440 afs_dataversion_t data_version = dvnode->status.data_version + 1;
1441 afs_dataversion_t data_version_2 = vnode->status.data_version;
1443 while (afs_select_fileserver(&fc)) {
1444 fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1445 fc.cb_break_2 = afs_calc_vnode_cb_break(vnode);
1447 if (test_bit(AFS_SERVER_FL_IS_YFS, &fc.cbi->server->flags) &&
1448 !test_bit(AFS_SERVER_FL_NO_RM2, &fc.cbi->server->flags)) {
1449 yfs_fs_remove_file2(&fc, vnode, dentry->d_name.name,
1450 &scb[0], &scb[1]);
1451 if (fc.ac.error != -ECONNABORTED ||
1452 fc.ac.abort_code != RXGEN_OPCODE)
1453 continue;
1454 set_bit(AFS_SERVER_FL_NO_RM2, &fc.cbi->server->flags);
1457 afs_fs_remove(&fc, vnode, dentry->d_name.name, false, &scb[0]);
1460 afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1461 &data_version, &scb[0]);
1462 afs_vnode_commit_status(&fc, vnode, fc.cb_break_2,
1463 &data_version_2, &scb[1]);
1464 ret = afs_end_vnode_operation(&fc);
1465 if (ret == 0 && !(scb[1].have_status || scb[1].have_error))
1466 ret = afs_dir_remove_link(dvnode, dentry, key);
1467 if (ret == 0 &&
1468 test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1469 afs_edit_dir_remove(dvnode, &dentry->d_name,
1470 afs_edit_dir_for_unlink);
1473 if (need_rehash && ret < 0 && ret != -ENOENT)
1474 d_rehash(dentry);
1476 error_key:
1477 key_put(key);
1478 error_scb:
1479 kfree(scb);
1480 error:
1481 _leave(" = %d", ret);
1482 return ret;
1486 * create a regular file on an AFS filesystem
1488 static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
1489 bool excl)
1491 struct afs_iget_data iget_data;
1492 struct afs_fs_cursor fc;
1493 struct afs_status_cb *scb;
1494 struct afs_vnode *dvnode = AFS_FS_I(dir);
1495 struct key *key;
1496 int ret;
1498 mode |= S_IFREG;
1500 _enter("{%llx:%llu},{%pd},%ho,",
1501 dvnode->fid.vid, dvnode->fid.vnode, dentry, mode);
1503 ret = -ENAMETOOLONG;
1504 if (dentry->d_name.len >= AFSNAMEMAX)
1505 goto error;
1507 key = afs_request_key(dvnode->volume->cell);
1508 if (IS_ERR(key)) {
1509 ret = PTR_ERR(key);
1510 goto error;
1513 ret = -ENOMEM;
1514 scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1515 if (!scb)
1516 goto error_scb;
1518 ret = -ERESTARTSYS;
1519 if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1520 afs_dataversion_t data_version = dvnode->status.data_version + 1;
1522 while (afs_select_fileserver(&fc)) {
1523 fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1524 afs_prep_for_new_inode(&fc, &iget_data);
1525 afs_fs_create(&fc, dentry->d_name.name, mode,
1526 &scb[0], &iget_data.fid, &scb[1]);
1529 afs_check_for_remote_deletion(&fc, dvnode);
1530 afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1531 &data_version, &scb[0]);
1532 afs_vnode_new_inode(&fc, dentry, &iget_data, &scb[1]);
1533 ret = afs_end_vnode_operation(&fc);
1534 if (ret < 0)
1535 goto error_key;
1536 } else {
1537 goto error_key;
1540 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1541 afs_edit_dir_add(dvnode, &dentry->d_name, &iget_data.fid,
1542 afs_edit_dir_for_create);
1544 kfree(scb);
1545 key_put(key);
1546 _leave(" = 0");
1547 return 0;
1549 error_scb:
1550 kfree(scb);
1551 error_key:
1552 key_put(key);
1553 error:
1554 d_drop(dentry);
1555 _leave(" = %d", ret);
1556 return ret;
1560 * create a hard link between files in an AFS filesystem
1562 static int afs_link(struct dentry *from, struct inode *dir,
1563 struct dentry *dentry)
1565 struct afs_fs_cursor fc;
1566 struct afs_status_cb *scb;
1567 struct afs_vnode *dvnode = AFS_FS_I(dir);
1568 struct afs_vnode *vnode = AFS_FS_I(d_inode(from));
1569 struct key *key;
1570 int ret;
1572 _enter("{%llx:%llu},{%llx:%llu},{%pd}",
1573 vnode->fid.vid, vnode->fid.vnode,
1574 dvnode->fid.vid, dvnode->fid.vnode,
1575 dentry);
1577 ret = -ENAMETOOLONG;
1578 if (dentry->d_name.len >= AFSNAMEMAX)
1579 goto error;
1581 ret = -ENOMEM;
1582 scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1583 if (!scb)
1584 goto error;
1586 key = afs_request_key(dvnode->volume->cell);
1587 if (IS_ERR(key)) {
1588 ret = PTR_ERR(key);
1589 goto error_scb;
1592 ret = -ERESTARTSYS;
1593 if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1594 afs_dataversion_t data_version = dvnode->status.data_version + 1;
1596 if (mutex_lock_interruptible_nested(&vnode->io_lock, 1) < 0) {
1597 afs_end_vnode_operation(&fc);
1598 goto error_key;
1601 while (afs_select_fileserver(&fc)) {
1602 fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1603 fc.cb_break_2 = afs_calc_vnode_cb_break(vnode);
1604 afs_fs_link(&fc, vnode, dentry->d_name.name,
1605 &scb[0], &scb[1]);
1608 afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1609 &data_version, &scb[0]);
1610 afs_vnode_commit_status(&fc, vnode, fc.cb_break_2,
1611 NULL, &scb[1]);
1612 ihold(&vnode->vfs_inode);
1613 d_instantiate(dentry, &vnode->vfs_inode);
1615 mutex_unlock(&vnode->io_lock);
1616 ret = afs_end_vnode_operation(&fc);
1617 if (ret < 0)
1618 goto error_key;
1619 } else {
1620 goto error_key;
1623 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1624 afs_edit_dir_add(dvnode, &dentry->d_name, &vnode->fid,
1625 afs_edit_dir_for_link);
1627 key_put(key);
1628 kfree(scb);
1629 _leave(" = 0");
1630 return 0;
1632 error_key:
1633 key_put(key);
1634 error_scb:
1635 kfree(scb);
1636 error:
1637 d_drop(dentry);
1638 _leave(" = %d", ret);
1639 return ret;
1643 * create a symlink in an AFS filesystem
1645 static int afs_symlink(struct inode *dir, struct dentry *dentry,
1646 const char *content)
1648 struct afs_iget_data iget_data;
1649 struct afs_fs_cursor fc;
1650 struct afs_status_cb *scb;
1651 struct afs_vnode *dvnode = AFS_FS_I(dir);
1652 struct key *key;
1653 int ret;
1655 _enter("{%llx:%llu},{%pd},%s",
1656 dvnode->fid.vid, dvnode->fid.vnode, dentry,
1657 content);
1659 ret = -ENAMETOOLONG;
1660 if (dentry->d_name.len >= AFSNAMEMAX)
1661 goto error;
1663 ret = -EINVAL;
1664 if (strlen(content) >= AFSPATHMAX)
1665 goto error;
1667 ret = -ENOMEM;
1668 scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1669 if (!scb)
1670 goto error;
1672 key = afs_request_key(dvnode->volume->cell);
1673 if (IS_ERR(key)) {
1674 ret = PTR_ERR(key);
1675 goto error_scb;
1678 ret = -ERESTARTSYS;
1679 if (afs_begin_vnode_operation(&fc, dvnode, key, true)) {
1680 afs_dataversion_t data_version = dvnode->status.data_version + 1;
1682 while (afs_select_fileserver(&fc)) {
1683 fc.cb_break = afs_calc_vnode_cb_break(dvnode);
1684 afs_prep_for_new_inode(&fc, &iget_data);
1685 afs_fs_symlink(&fc, dentry->d_name.name, content,
1686 &scb[0], &iget_data.fid, &scb[1]);
1689 afs_check_for_remote_deletion(&fc, dvnode);
1690 afs_vnode_commit_status(&fc, dvnode, fc.cb_break,
1691 &data_version, &scb[0]);
1692 afs_vnode_new_inode(&fc, dentry, &iget_data, &scb[1]);
1693 ret = afs_end_vnode_operation(&fc);
1694 if (ret < 0)
1695 goto error_key;
1696 } else {
1697 goto error_key;
1700 if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1701 afs_edit_dir_add(dvnode, &dentry->d_name, &iget_data.fid,
1702 afs_edit_dir_for_symlink);
1704 key_put(key);
1705 kfree(scb);
1706 _leave(" = 0");
1707 return 0;
1709 error_key:
1710 key_put(key);
1711 error_scb:
1712 kfree(scb);
1713 error:
1714 d_drop(dentry);
1715 _leave(" = %d", ret);
1716 return ret;
1720 * rename a file in an AFS filesystem and/or move it between directories
1722 static int afs_rename(struct inode *old_dir, struct dentry *old_dentry,
1723 struct inode *new_dir, struct dentry *new_dentry,
1724 unsigned int flags)
1726 struct afs_fs_cursor fc;
1727 struct afs_status_cb *scb;
1728 struct afs_vnode *orig_dvnode, *new_dvnode, *vnode;
1729 struct dentry *tmp = NULL, *rehash = NULL;
1730 struct inode *new_inode;
1731 struct key *key;
1732 bool new_negative = d_is_negative(new_dentry);
1733 int ret;
1735 if (flags)
1736 return -EINVAL;
1738 /* Don't allow silly-rename files be moved around. */
1739 if (old_dentry->d_flags & DCACHE_NFSFS_RENAMED)
1740 return -EINVAL;
1742 vnode = AFS_FS_I(d_inode(old_dentry));
1743 orig_dvnode = AFS_FS_I(old_dir);
1744 new_dvnode = AFS_FS_I(new_dir);
1746 _enter("{%llx:%llu},{%llx:%llu},{%llx:%llu},{%pd}",
1747 orig_dvnode->fid.vid, orig_dvnode->fid.vnode,
1748 vnode->fid.vid, vnode->fid.vnode,
1749 new_dvnode->fid.vid, new_dvnode->fid.vnode,
1750 new_dentry);
1752 ret = -ENOMEM;
1753 scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL);
1754 if (!scb)
1755 goto error;
1757 key = afs_request_key(orig_dvnode->volume->cell);
1758 if (IS_ERR(key)) {
1759 ret = PTR_ERR(key);
1760 goto error_scb;
1763 /* For non-directories, check whether the target is busy and if so,
1764 * make a copy of the dentry and then do a silly-rename. If the
1765 * silly-rename succeeds, the copied dentry is hashed and becomes the
1766 * new target.
1768 if (d_is_positive(new_dentry) && !d_is_dir(new_dentry)) {
1769 /* To prevent any new references to the target during the
1770 * rename, we unhash the dentry in advance.
1772 if (!d_unhashed(new_dentry)) {
1773 d_drop(new_dentry);
1774 rehash = new_dentry;
1777 if (d_count(new_dentry) > 2) {
1778 /* copy the target dentry's name */
1779 ret = -ENOMEM;
1780 tmp = d_alloc(new_dentry->d_parent,
1781 &new_dentry->d_name);
1782 if (!tmp)
1783 goto error_rehash;
1785 ret = afs_sillyrename(new_dvnode,
1786 AFS_FS_I(d_inode(new_dentry)),
1787 new_dentry, key);
1788 if (ret)
1789 goto error_rehash;
1791 new_dentry = tmp;
1792 rehash = NULL;
1793 new_negative = true;
1797 ret = -ERESTARTSYS;
1798 if (afs_begin_vnode_operation(&fc, orig_dvnode, key, true)) {
1799 afs_dataversion_t orig_data_version;
1800 afs_dataversion_t new_data_version;
1801 struct afs_status_cb *new_scb = &scb[1];
1803 orig_data_version = orig_dvnode->status.data_version + 1;
1805 if (orig_dvnode != new_dvnode) {
1806 if (mutex_lock_interruptible_nested(&new_dvnode->io_lock, 1) < 0) {
1807 afs_end_vnode_operation(&fc);
1808 goto error_rehash;
1810 new_data_version = new_dvnode->status.data_version;
1811 } else {
1812 new_data_version = orig_data_version;
1813 new_scb = &scb[0];
1816 while (afs_select_fileserver(&fc)) {
1817 fc.cb_break = afs_calc_vnode_cb_break(orig_dvnode);
1818 fc.cb_break_2 = afs_calc_vnode_cb_break(new_dvnode);
1819 afs_fs_rename(&fc, old_dentry->d_name.name,
1820 new_dvnode, new_dentry->d_name.name,
1821 &scb[0], new_scb);
1824 afs_vnode_commit_status(&fc, orig_dvnode, fc.cb_break,
1825 &orig_data_version, &scb[0]);
1826 if (new_dvnode != orig_dvnode) {
1827 afs_vnode_commit_status(&fc, new_dvnode, fc.cb_break_2,
1828 &new_data_version, &scb[1]);
1829 mutex_unlock(&new_dvnode->io_lock);
1831 ret = afs_end_vnode_operation(&fc);
1832 if (ret < 0)
1833 goto error_rehash;
1836 if (ret == 0) {
1837 if (rehash)
1838 d_rehash(rehash);
1839 if (test_bit(AFS_VNODE_DIR_VALID, &orig_dvnode->flags))
1840 afs_edit_dir_remove(orig_dvnode, &old_dentry->d_name,
1841 afs_edit_dir_for_rename_0);
1843 if (!new_negative &&
1844 test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags))
1845 afs_edit_dir_remove(new_dvnode, &new_dentry->d_name,
1846 afs_edit_dir_for_rename_1);
1848 if (test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags))
1849 afs_edit_dir_add(new_dvnode, &new_dentry->d_name,
1850 &vnode->fid, afs_edit_dir_for_rename_2);
1852 new_inode = d_inode(new_dentry);
1853 if (new_inode) {
1854 spin_lock(&new_inode->i_lock);
1855 if (new_inode->i_nlink > 0)
1856 drop_nlink(new_inode);
1857 spin_unlock(&new_inode->i_lock);
1859 d_move(old_dentry, new_dentry);
1860 goto error_tmp;
1863 error_rehash:
1864 if (rehash)
1865 d_rehash(rehash);
1866 error_tmp:
1867 if (tmp)
1868 dput(tmp);
1869 key_put(key);
1870 error_scb:
1871 kfree(scb);
1872 error:
1873 _leave(" = %d", ret);
1874 return ret;
1878 * Release a directory page and clean up its private state if it's not busy
1879 * - return true if the page can now be released, false if not
1881 static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags)
1883 struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host);
1885 _enter("{{%llx:%llu}[%lu]}", dvnode->fid.vid, dvnode->fid.vnode, page->index);
1887 set_page_private(page, 0);
1888 ClearPagePrivate(page);
1890 /* The directory will need reloading. */
1891 if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1892 afs_stat_v(dvnode, n_relpg);
1893 return 1;
1897 * invalidate part or all of a page
1898 * - release a page and clean up its private data if offset is 0 (indicating
1899 * the entire page)
1901 static void afs_dir_invalidatepage(struct page *page, unsigned int offset,
1902 unsigned int length)
1904 struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host);
1906 _enter("{%lu},%u,%u", page->index, offset, length);
1908 BUG_ON(!PageLocked(page));
1910 /* The directory will need reloading. */
1911 if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags))
1912 afs_stat_v(dvnode, n_inval);
1914 /* we clean up only if the entire page is being invalidated */
1915 if (offset == 0 && length == PAGE_SIZE) {
1916 set_page_private(page, 0);
1917 ClearPagePrivate(page);