12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 21da177e4SLinus Torvalds /* dir.c: AFS filesystem directory handling 31da177e4SLinus Torvalds * 4f3ddee8dSDavid Howells * Copyright (C) 2002, 2018 Red Hat, Inc. All Rights Reserved. 51da177e4SLinus Torvalds * Written by David Howells (dhowells@redhat.com) 61da177e4SLinus Torvalds */ 71da177e4SLinus Torvalds 81da177e4SLinus Torvalds #include <linux/kernel.h> 91da177e4SLinus Torvalds #include <linux/fs.h> 1034286d66SNick Piggin #include <linux/namei.h> 111da177e4SLinus Torvalds #include <linux/pagemap.h> 12f3ddee8dSDavid Howells #include <linux/swap.h> 1300d3b7a4SDavid Howells #include <linux/ctype.h> 14e8edc6e0SAlexey Dobriyan #include <linux/sched.h> 15f3ddee8dSDavid Howells #include <linux/task_io_accounting_ops.h> 161da177e4SLinus Torvalds #include "internal.h" 17a58823acSDavid Howells #include "afs_fs.h" 184ea219a8SDavid Howells #include "xdr_fs.h" 191da177e4SLinus Torvalds 20260a9803SDavid Howells static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry, 2100cd8dd3SAl Viro unsigned int flags); 221da177e4SLinus Torvalds static int afs_dir_open(struct inode *inode, struct file *file); 231bbae9f8SAl Viro static int afs_readdir(struct file *file, struct dir_context *ctx); 240b728e19SAl Viro static int afs_d_revalidate(struct dentry *dentry, unsigned int flags); 25fe15ce44SNick Piggin static int afs_d_delete(const struct dentry *dentry); 2679ddbfa5SDavid Howells static void afs_d_iput(struct dentry *dentry, struct inode *inode); 275cf9dd55SDavid Howells static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name, int nlen, 285cf9dd55SDavid Howells loff_t fpos, u64 ino, unsigned dtype); 29ac7576f4SMiklos Szeredi static int afs_lookup_filldir(struct dir_context *ctx, const char *name, int nlen, 30afefdbb2SDavid Howells loff_t fpos, u64 ino, unsigned dtype); 31549c7297SChristian Brauner static int afs_create(struct user_namespace *mnt_userns, struct inode *dir, 32549c7297SChristian Brauner struct dentry *dentry, umode_t mode, bool excl); 33549c7297SChristian Brauner static int afs_mkdir(struct user_namespace *mnt_userns, struct inode *dir, 34549c7297SChristian Brauner struct dentry *dentry, umode_t mode); 35260a9803SDavid Howells static int afs_rmdir(struct inode *dir, struct dentry *dentry); 36260a9803SDavid Howells static int afs_unlink(struct inode *dir, struct dentry *dentry); 37260a9803SDavid Howells static int afs_link(struct dentry *from, struct inode *dir, 38260a9803SDavid Howells struct dentry *dentry); 39549c7297SChristian Brauner static int afs_symlink(struct user_namespace *mnt_userns, struct inode *dir, 40549c7297SChristian Brauner struct dentry *dentry, const char *content); 41549c7297SChristian Brauner static int afs_rename(struct user_namespace *mnt_userns, struct inode *old_dir, 42549c7297SChristian Brauner struct dentry *old_dentry, struct inode *new_dir, 43549c7297SChristian Brauner struct dentry *new_dentry, unsigned int flags); 44f3ddee8dSDavid Howells static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags); 45f3ddee8dSDavid Howells static void afs_dir_invalidatepage(struct page *page, unsigned int offset, 46f3ddee8dSDavid Howells unsigned int length); 47f3ddee8dSDavid Howells 48f3ddee8dSDavid Howells static int afs_dir_set_page_dirty(struct page *page) 49f3ddee8dSDavid Howells { 50f3ddee8dSDavid Howells BUG(); /* This should never happen. */ 51f3ddee8dSDavid Howells } 521da177e4SLinus Torvalds 534b6f5d20SArjan van de Ven const struct file_operations afs_dir_file_operations = { 541da177e4SLinus Torvalds .open = afs_dir_open, 5500d3b7a4SDavid Howells .release = afs_release, 5629884effSAl Viro .iterate_shared = afs_readdir, 57e8d6c554SDavid Howells .lock = afs_lock, 583222a3e5SChristoph Hellwig .llseek = generic_file_llseek, 591da177e4SLinus Torvalds }; 601da177e4SLinus Torvalds 61754661f1SArjan van de Ven const struct inode_operations afs_dir_inode_operations = { 62260a9803SDavid Howells .create = afs_create, 63260a9803SDavid Howells .lookup = afs_lookup, 64260a9803SDavid Howells .link = afs_link, 65260a9803SDavid Howells .unlink = afs_unlink, 66260a9803SDavid Howells .symlink = afs_symlink, 67260a9803SDavid Howells .mkdir = afs_mkdir, 68260a9803SDavid Howells .rmdir = afs_rmdir, 692773bf00SMiklos Szeredi .rename = afs_rename, 7000d3b7a4SDavid Howells .permission = afs_permission, 71416351f2SDavid Howells .getattr = afs_getattr, 7231143d5dSDavid Howells .setattr = afs_setattr, 731da177e4SLinus Torvalds }; 741da177e4SLinus Torvalds 75f3ddee8dSDavid Howells const struct address_space_operations afs_dir_aops = { 76f3ddee8dSDavid Howells .set_page_dirty = afs_dir_set_page_dirty, 77f3ddee8dSDavid Howells .releasepage = afs_dir_releasepage, 78f3ddee8dSDavid Howells .invalidatepage = afs_dir_invalidatepage, 79f3ddee8dSDavid Howells }; 80f3ddee8dSDavid Howells 81d61dcce2SAl Viro const struct dentry_operations afs_fs_dentry_operations = { 821da177e4SLinus Torvalds .d_revalidate = afs_d_revalidate, 831da177e4SLinus Torvalds .d_delete = afs_d_delete, 84260a9803SDavid Howells .d_release = afs_d_release, 85d18610b0SDavid Howells .d_automount = afs_d_automount, 8679ddbfa5SDavid Howells .d_iput = afs_d_iput, 871da177e4SLinus Torvalds }; 881da177e4SLinus Torvalds 895cf9dd55SDavid Howells struct afs_lookup_one_cookie { 905cf9dd55SDavid Howells struct dir_context ctx; 915cf9dd55SDavid Howells struct qstr name; 925cf9dd55SDavid Howells bool found; 935cf9dd55SDavid Howells struct afs_fid fid; 945cf9dd55SDavid Howells }; 955cf9dd55SDavid Howells 96260a9803SDavid Howells struct afs_lookup_cookie { 971bbae9f8SAl Viro struct dir_context ctx; 981bbae9f8SAl Viro struct qstr name; 995cf9dd55SDavid Howells bool found; 1005cf9dd55SDavid Howells bool one_only; 1015cf9dd55SDavid Howells unsigned short nr_fids; 1025cf9dd55SDavid Howells struct afs_fid fids[50]; 1031da177e4SLinus Torvalds }; 1041da177e4SLinus Torvalds 1051da177e4SLinus Torvalds /* 106c4508464SDavid Howells * Drop the refs that we're holding on the pages we were reading into. We've 107c4508464SDavid Howells * got refs on the first nr_pages pages. 108c4508464SDavid Howells */ 109c4508464SDavid Howells static void afs_dir_read_cleanup(struct afs_read *req) 110c4508464SDavid Howells { 111c4508464SDavid Howells struct address_space *mapping = req->vnode->vfs_inode.i_mapping; 112c4508464SDavid Howells struct page *page; 113c4508464SDavid Howells pgoff_t last = req->nr_pages - 1; 114c4508464SDavid Howells 115c4508464SDavid Howells XA_STATE(xas, &mapping->i_pages, 0); 116c4508464SDavid Howells 117c4508464SDavid Howells if (unlikely(!req->nr_pages)) 118c4508464SDavid Howells return; 119c4508464SDavid Howells 120c4508464SDavid Howells rcu_read_lock(); 121c4508464SDavid Howells xas_for_each(&xas, page, last) { 122c4508464SDavid Howells if (xas_retry(&xas, page)) 123c4508464SDavid Howells continue; 124c4508464SDavid Howells BUG_ON(xa_is_value(page)); 125c4508464SDavid Howells BUG_ON(PageCompound(page)); 126c4508464SDavid Howells ASSERTCMP(page->mapping, ==, mapping); 127c4508464SDavid Howells 128c4508464SDavid Howells put_page(page); 129c4508464SDavid Howells } 130c4508464SDavid Howells 131c4508464SDavid Howells rcu_read_unlock(); 132c4508464SDavid Howells } 133c4508464SDavid Howells 134c4508464SDavid Howells /* 1351da177e4SLinus Torvalds * check that a directory page is valid 1361da177e4SLinus Torvalds */ 137f3ddee8dSDavid Howells static bool afs_dir_check_page(struct afs_vnode *dvnode, struct page *page, 138f3ddee8dSDavid Howells loff_t i_size) 1391da177e4SLinus Torvalds { 14000317636SDavid Howells struct afs_xdr_dir_page *dbuf; 141f3ddee8dSDavid Howells loff_t latter, off; 1421da177e4SLinus Torvalds int tmp, qty; 1431da177e4SLinus Torvalds 144dab17c1aSDavid Howells /* Determine how many magic numbers there should be in this page, but 145dab17c1aSDavid Howells * we must take care because the directory may change size under us. 146dab17c1aSDavid Howells */ 147dab17c1aSDavid Howells off = page_offset(page); 148dab17c1aSDavid Howells if (i_size <= off) 149dab17c1aSDavid Howells goto checked; 150dab17c1aSDavid Howells 151dab17c1aSDavid Howells latter = i_size - off; 1521da177e4SLinus Torvalds if (latter >= PAGE_SIZE) 1531da177e4SLinus Torvalds qty = PAGE_SIZE; 1541da177e4SLinus Torvalds else 1551da177e4SLinus Torvalds qty = latter; 15600317636SDavid Howells qty /= sizeof(union afs_xdr_dir_block); 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds /* check them */ 159c4508464SDavid Howells dbuf = kmap_atomic(page); 1601da177e4SLinus Torvalds for (tmp = 0; tmp < qty; tmp++) { 16100317636SDavid Howells if (dbuf->blocks[tmp].hdr.magic != AFS_DIR_MAGIC) { 162dab17c1aSDavid Howells printk("kAFS: %s(%lx): bad magic %d/%d is %04hx\n", 163f3ddee8dSDavid Howells __func__, dvnode->vfs_inode.i_ino, tmp, qty, 16400317636SDavid Howells ntohs(dbuf->blocks[tmp].hdr.magic)); 165f3ddee8dSDavid Howells trace_afs_dir_check_failed(dvnode, off, i_size); 16663a4681fSDavid Howells kunmap(page); 167f51375cdSDavid Howells trace_afs_file_error(dvnode, -EIO, afs_file_error_dir_bad_magic); 1681da177e4SLinus Torvalds goto error; 1691da177e4SLinus Torvalds } 17063a4681fSDavid Howells 17163a4681fSDavid Howells /* Make sure each block is NUL terminated so we can reasonably 17263a4681fSDavid Howells * use string functions on it. The filenames in the page 17363a4681fSDavid Howells * *should* be NUL-terminated anyway. 17463a4681fSDavid Howells */ 17563a4681fSDavid Howells ((u8 *)&dbuf->blocks[tmp])[AFS_DIR_BLOCK_SIZE - 1] = 0; 1761da177e4SLinus Torvalds } 1771da177e4SLinus Torvalds 178c4508464SDavid Howells kunmap_atomic(dbuf); 17963a4681fSDavid Howells 180dab17c1aSDavid Howells checked: 181f3ddee8dSDavid Howells afs_stat_v(dvnode, n_read_dir); 182be5b82dbSAl Viro return true; 1831da177e4SLinus Torvalds 1841da177e4SLinus Torvalds error: 185be5b82dbSAl Viro return false; 186ec26815aSDavid Howells } 1871da177e4SLinus Torvalds 1881da177e4SLinus Torvalds /* 189c4508464SDavid Howells * Dump the contents of a directory. 190445b1028SDavid Howells */ 191c4508464SDavid Howells static void afs_dir_dump(struct afs_vnode *dvnode, struct afs_read *req) 192445b1028SDavid Howells { 193445b1028SDavid Howells struct afs_xdr_dir_page *dbuf; 194c4508464SDavid Howells struct address_space *mapping = dvnode->vfs_inode.i_mapping; 195c4508464SDavid Howells struct page *page; 196c4508464SDavid Howells unsigned int i, qty = PAGE_SIZE / sizeof(union afs_xdr_dir_block); 197c4508464SDavid Howells pgoff_t last = req->nr_pages - 1; 198445b1028SDavid Howells 199c4508464SDavid Howells XA_STATE(xas, &mapping->i_pages, 0); 200445b1028SDavid Howells 201c4508464SDavid Howells pr_warn("DIR %llx:%llx f=%llx l=%llx al=%llx\n", 202445b1028SDavid Howells dvnode->fid.vid, dvnode->fid.vnode, 203c4508464SDavid Howells req->file_size, req->len, req->actual_len); 204c4508464SDavid Howells pr_warn("DIR %llx %x %zx %zx\n", 205c4508464SDavid Howells req->pos, req->nr_pages, 206c4508464SDavid Howells req->iter->iov_offset, iov_iter_count(req->iter)); 207445b1028SDavid Howells 208c4508464SDavid Howells xas_for_each(&xas, page, last) { 209c4508464SDavid Howells if (xas_retry(&xas, page)) 210c4508464SDavid Howells continue; 211445b1028SDavid Howells 212c4508464SDavid Howells BUG_ON(PageCompound(page)); 213c4508464SDavid Howells BUG_ON(page->mapping != mapping); 214c4508464SDavid Howells 215c4508464SDavid Howells dbuf = kmap_atomic(page); 216c4508464SDavid Howells for (i = 0; i < qty; i++) { 217c4508464SDavid Howells union afs_xdr_dir_block *block = &dbuf->blocks[i]; 218c4508464SDavid Howells 219c4508464SDavid Howells pr_warn("[%02lx] %32phN\n", page->index * qty + i, block); 220445b1028SDavid Howells } 221c4508464SDavid Howells kunmap_atomic(dbuf); 222445b1028SDavid Howells } 223c4508464SDavid Howells } 224c4508464SDavid Howells 225c4508464SDavid Howells /* 226c4508464SDavid Howells * Check all the pages in a directory. All the pages are held pinned. 227c4508464SDavid Howells */ 228c4508464SDavid Howells static int afs_dir_check(struct afs_vnode *dvnode, struct afs_read *req) 229c4508464SDavid Howells { 230c4508464SDavid Howells struct address_space *mapping = dvnode->vfs_inode.i_mapping; 231c4508464SDavid Howells struct page *page; 232c4508464SDavid Howells pgoff_t last = req->nr_pages - 1; 233c4508464SDavid Howells int ret = 0; 234c4508464SDavid Howells 235c4508464SDavid Howells XA_STATE(xas, &mapping->i_pages, 0); 236c4508464SDavid Howells 237c4508464SDavid Howells if (unlikely(!req->nr_pages)) 238c4508464SDavid Howells return 0; 239c4508464SDavid Howells 240c4508464SDavid Howells rcu_read_lock(); 241c4508464SDavid Howells xas_for_each(&xas, page, last) { 242c4508464SDavid Howells if (xas_retry(&xas, page)) 243c4508464SDavid Howells continue; 244c4508464SDavid Howells 245c4508464SDavid Howells BUG_ON(PageCompound(page)); 246c4508464SDavid Howells BUG_ON(page->mapping != mapping); 247c4508464SDavid Howells 248c4508464SDavid Howells if (!afs_dir_check_page(dvnode, page, req->file_size)) { 249c4508464SDavid Howells afs_dir_dump(dvnode, req); 250c4508464SDavid Howells ret = -EIO; 251c4508464SDavid Howells break; 252c4508464SDavid Howells } 253c4508464SDavid Howells } 254c4508464SDavid Howells 255c4508464SDavid Howells rcu_read_unlock(); 256c4508464SDavid Howells return ret; 257445b1028SDavid Howells } 258445b1028SDavid Howells 259445b1028SDavid Howells /* 2601da177e4SLinus Torvalds * open an AFS directory file 2611da177e4SLinus Torvalds */ 2621da177e4SLinus Torvalds static int afs_dir_open(struct inode *inode, struct file *file) 2631da177e4SLinus Torvalds { 2641da177e4SLinus Torvalds _enter("{%lu}", inode->i_ino); 2651da177e4SLinus Torvalds 26600317636SDavid Howells BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048); 26700317636SDavid Howells BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32); 2681da177e4SLinus Torvalds 26908e0e7c8SDavid Howells if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(inode)->flags)) 2701da177e4SLinus Torvalds return -ENOENT; 2711da177e4SLinus Torvalds 27200d3b7a4SDavid Howells return afs_open(inode, file); 273ec26815aSDavid Howells } 2741da177e4SLinus Torvalds 2751da177e4SLinus Torvalds /* 276f3ddee8dSDavid Howells * Read the directory into the pagecache in one go, scrubbing the previous 277f3ddee8dSDavid Howells * contents. The list of pages is returned, pinning them so that they don't 278f3ddee8dSDavid Howells * get reclaimed during the iteration. 279f3ddee8dSDavid Howells */ 280f3ddee8dSDavid Howells static struct afs_read *afs_read_dir(struct afs_vnode *dvnode, struct key *key) 281b61f7dcfSDavid Howells __acquires(&dvnode->validate_lock) 282f3ddee8dSDavid Howells { 283f3ddee8dSDavid Howells struct afs_read *req; 284f3ddee8dSDavid Howells loff_t i_size; 285c4508464SDavid Howells int nr_pages, i, n; 286c4508464SDavid Howells int ret; 287f3ddee8dSDavid Howells 288c4508464SDavid Howells _enter(""); 289f3ddee8dSDavid Howells 290c4508464SDavid Howells req = kzalloc(sizeof(*req), GFP_KERNEL); 291f3ddee8dSDavid Howells if (!req) 292f3ddee8dSDavid Howells return ERR_PTR(-ENOMEM); 293f3ddee8dSDavid Howells 294f3ddee8dSDavid Howells refcount_set(&req->usage, 1); 295c4508464SDavid Howells req->vnode = dvnode; 296c69bf479SDavid Howells req->key = key_get(key); 297c4508464SDavid Howells req->cleanup = afs_dir_read_cleanup; 298c4508464SDavid Howells 299c4508464SDavid Howells expand: 300c4508464SDavid Howells i_size = i_size_read(&dvnode->vfs_inode); 301c4508464SDavid Howells if (i_size < 2048) { 302c4508464SDavid Howells ret = afs_bad(dvnode, afs_file_error_dir_small); 303c4508464SDavid Howells goto error; 304c4508464SDavid Howells } 305c4508464SDavid Howells if (i_size > 2048 * 1024) { 306c4508464SDavid Howells trace_afs_file_error(dvnode, -EFBIG, afs_file_error_dir_big); 307c4508464SDavid Howells ret = -EFBIG; 308f3ddee8dSDavid Howells goto error; 309f3ddee8dSDavid Howells } 310f3ddee8dSDavid Howells 311c4508464SDavid Howells _enter("%llu", i_size); 312c4508464SDavid Howells 313c4508464SDavid Howells nr_pages = (i_size + PAGE_SIZE - 1) / PAGE_SIZE; 314c4508464SDavid Howells 315c4508464SDavid Howells req->actual_len = i_size; /* May change */ 316c4508464SDavid Howells req->len = nr_pages * PAGE_SIZE; /* We can ask for more than there is */ 317c4508464SDavid Howells req->data_version = dvnode->status.data_version; /* May change */ 318c4508464SDavid Howells iov_iter_xarray(&req->def_iter, READ, &dvnode->vfs_inode.i_mapping->i_pages, 319c4508464SDavid Howells 0, i_size); 320c4508464SDavid Howells req->iter = &req->def_iter; 321c4508464SDavid Howells 322c4508464SDavid Howells /* Fill in any gaps that we might find where the memory reclaimer has 323c4508464SDavid Howells * been at work and pin all the pages. If there are any gaps, we will 324f3ddee8dSDavid Howells * need to reread the entire directory contents. 325f3ddee8dSDavid Howells */ 326c4508464SDavid Howells i = req->nr_pages; 327c4508464SDavid Howells while (i < nr_pages) { 328c4508464SDavid Howells struct page *pages[8], *page; 329c4508464SDavid Howells 330f3ddee8dSDavid Howells n = find_get_pages_contig(dvnode->vfs_inode.i_mapping, i, 331c4508464SDavid Howells min_t(unsigned int, nr_pages - i, 332c4508464SDavid Howells ARRAY_SIZE(pages)), 333c4508464SDavid Howells pages); 334c4508464SDavid Howells _debug("find %u at %u/%u", n, i, nr_pages); 335c4508464SDavid Howells 336f3ddee8dSDavid Howells if (n == 0) { 337f3ddee8dSDavid Howells gfp_t gfp = dvnode->vfs_inode.i_mapping->gfp_mask; 338f3ddee8dSDavid Howells 339f3ddee8dSDavid Howells if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) 340f3ddee8dSDavid Howells afs_stat_v(dvnode, n_inval); 341f3ddee8dSDavid Howells 342f3ddee8dSDavid Howells ret = -ENOMEM; 343c4508464SDavid Howells page = __page_cache_alloc(gfp); 344c4508464SDavid Howells if (!page) 345f3ddee8dSDavid Howells goto error; 346c4508464SDavid Howells ret = add_to_page_cache_lru(page, 347f3ddee8dSDavid Howells dvnode->vfs_inode.i_mapping, 348f3ddee8dSDavid Howells i, gfp); 349f3ddee8dSDavid Howells if (ret < 0) 350f3ddee8dSDavid Howells goto error; 351f3ddee8dSDavid Howells 352c4508464SDavid Howells attach_page_private(page, (void *)1); 353c4508464SDavid Howells unlock_page(page); 354c4508464SDavid Howells req->nr_pages++; 355f3ddee8dSDavid Howells i++; 356f3ddee8dSDavid Howells } else { 357c4508464SDavid Howells req->nr_pages += n; 358f3ddee8dSDavid Howells i += n; 359f3ddee8dSDavid Howells } 360c4508464SDavid Howells } 361f3ddee8dSDavid Howells 362f3ddee8dSDavid Howells /* If we're going to reload, we need to lock all the pages to prevent 363f3ddee8dSDavid Howells * races. 364f3ddee8dSDavid Howells */ 365f3ddee8dSDavid Howells ret = -ERESTARTSYS; 366b61f7dcfSDavid Howells if (down_read_killable(&dvnode->validate_lock) < 0) 367b61f7dcfSDavid Howells goto error; 368f3ddee8dSDavid Howells 369f3ddee8dSDavid Howells if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) 370f3ddee8dSDavid Howells goto success; 371f3ddee8dSDavid Howells 372b61f7dcfSDavid Howells up_read(&dvnode->validate_lock); 373b61f7dcfSDavid Howells if (down_write_killable(&dvnode->validate_lock) < 0) 374b61f7dcfSDavid Howells goto error; 375b61f7dcfSDavid Howells 376b61f7dcfSDavid Howells if (!test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) { 37799987c56SDavid Howells trace_afs_reload_dir(dvnode); 378c69bf479SDavid Howells ret = afs_fetch_data(dvnode, req); 379f3ddee8dSDavid Howells if (ret < 0) 380b61f7dcfSDavid Howells goto error_unlock; 381f3ddee8dSDavid Howells 382f3ddee8dSDavid Howells task_io_account_read(PAGE_SIZE * req->nr_pages); 383f3ddee8dSDavid Howells 384c4508464SDavid Howells if (req->len < req->file_size) { 385c4508464SDavid Howells /* The content has grown, so we need to expand the 386c4508464SDavid Howells * buffer. 387c4508464SDavid Howells */ 388c4508464SDavid Howells up_write(&dvnode->validate_lock); 389c4508464SDavid Howells goto expand; 390c4508464SDavid Howells } 391f3ddee8dSDavid Howells 392f3ddee8dSDavid Howells /* Validate the data we just read. */ 393c4508464SDavid Howells ret = afs_dir_check(dvnode, req); 394c4508464SDavid Howells if (ret < 0) 395b61f7dcfSDavid Howells goto error_unlock; 396f3ddee8dSDavid Howells 397f3ddee8dSDavid Howells // TODO: Trim excess pages 398f3ddee8dSDavid Howells 399f3ddee8dSDavid Howells set_bit(AFS_VNODE_DIR_VALID, &dvnode->flags); 400f3ddee8dSDavid Howells } 401f3ddee8dSDavid Howells 402b61f7dcfSDavid Howells downgrade_write(&dvnode->validate_lock); 403f3ddee8dSDavid Howells success: 404f3ddee8dSDavid Howells return req; 405f3ddee8dSDavid Howells 406f3ddee8dSDavid Howells error_unlock: 407b61f7dcfSDavid Howells up_write(&dvnode->validate_lock); 408f3ddee8dSDavid Howells error: 409f3ddee8dSDavid Howells afs_put_read(req); 410f3ddee8dSDavid Howells _leave(" = %d", ret); 411f3ddee8dSDavid Howells return ERR_PTR(ret); 412f3ddee8dSDavid Howells } 413f3ddee8dSDavid Howells 414f3ddee8dSDavid Howells /* 4151da177e4SLinus Torvalds * deal with one block in an AFS directory 4161da177e4SLinus Torvalds */ 417f51375cdSDavid Howells static int afs_dir_iterate_block(struct afs_vnode *dvnode, 418f51375cdSDavid Howells struct dir_context *ctx, 41900317636SDavid Howells union afs_xdr_dir_block *block, 4201bbae9f8SAl Viro unsigned blkoff) 4211da177e4SLinus Torvalds { 42200317636SDavid Howells union afs_xdr_dirent *dire; 423366911cdSDavid Howells unsigned offset, next, curr, nr_slots; 4241da177e4SLinus Torvalds size_t nlen; 4251bbae9f8SAl Viro int tmp; 4261da177e4SLinus Torvalds 4271bbae9f8SAl Viro _enter("%u,%x,%p,,",(unsigned)ctx->pos,blkoff,block); 4281da177e4SLinus Torvalds 42900317636SDavid Howells curr = (ctx->pos - blkoff) / sizeof(union afs_xdr_dirent); 4301da177e4SLinus Torvalds 4311da177e4SLinus Torvalds /* walk through the block, an entry at a time */ 4324ea219a8SDavid Howells for (offset = (blkoff == 0 ? AFS_DIR_RESV_BLOCKS0 : AFS_DIR_RESV_BLOCKS); 4334ea219a8SDavid Howells offset < AFS_DIR_SLOTS_PER_BLOCK; 4341da177e4SLinus Torvalds offset = next 4351da177e4SLinus Torvalds ) { 4361da177e4SLinus Torvalds /* skip entries marked unused in the bitmap */ 43700317636SDavid Howells if (!(block->hdr.bitmap[offset / 8] & 4381da177e4SLinus Torvalds (1 << (offset % 8)))) { 4395b5e0928SAlexey Dobriyan _debug("ENT[%zu.%u]: unused", 44000317636SDavid Howells blkoff / sizeof(union afs_xdr_dir_block), offset); 441366911cdSDavid Howells next = offset + 1; 4421da177e4SLinus Torvalds if (offset >= curr) 4431bbae9f8SAl Viro ctx->pos = blkoff + 44400317636SDavid Howells next * sizeof(union afs_xdr_dirent); 4451da177e4SLinus Torvalds continue; 4461da177e4SLinus Torvalds } 4471da177e4SLinus Torvalds 4481da177e4SLinus Torvalds /* got a valid entry */ 4491da177e4SLinus Torvalds dire = &block->dirents[offset]; 4501da177e4SLinus Torvalds nlen = strnlen(dire->u.name, 4511da177e4SLinus Torvalds sizeof(*block) - 45200317636SDavid Howells offset * sizeof(union afs_xdr_dirent)); 453366911cdSDavid Howells if (nlen > AFSNAMEMAX - 1) { 454366911cdSDavid Howells _debug("ENT[%zu]: name too long (len %u/%zu)", 455366911cdSDavid Howells blkoff / sizeof(union afs_xdr_dir_block), 456366911cdSDavid Howells offset, nlen); 457366911cdSDavid Howells return afs_bad(dvnode, afs_file_error_dir_name_too_long); 458366911cdSDavid Howells } 4591da177e4SLinus Torvalds 4605b5e0928SAlexey Dobriyan _debug("ENT[%zu.%u]: %s %zu \"%s\"", 46100317636SDavid Howells blkoff / sizeof(union afs_xdr_dir_block), offset, 4621da177e4SLinus Torvalds (offset < curr ? "skip" : "fill"), 4631da177e4SLinus Torvalds nlen, dire->u.name); 4641da177e4SLinus Torvalds 465366911cdSDavid Howells nr_slots = afs_dir_calc_slots(nlen); 466366911cdSDavid Howells next = offset + nr_slots; 467366911cdSDavid Howells if (next > AFS_DIR_SLOTS_PER_BLOCK) { 4685b5e0928SAlexey Dobriyan _debug("ENT[%zu.%u]:" 469366911cdSDavid Howells " %u extends beyond end dir block" 470366911cdSDavid Howells " (len %zu)", 47100317636SDavid Howells blkoff / sizeof(union afs_xdr_dir_block), 472366911cdSDavid Howells offset, next, nlen); 473f51375cdSDavid Howells return afs_bad(dvnode, afs_file_error_dir_over_end); 4741da177e4SLinus Torvalds } 475366911cdSDavid Howells 476366911cdSDavid Howells /* Check that the name-extension dirents are all allocated */ 477366911cdSDavid Howells for (tmp = 1; tmp < nr_slots; tmp++) { 478366911cdSDavid Howells unsigned int ix = offset + tmp; 479366911cdSDavid Howells if (!(block->hdr.bitmap[ix / 8] & (1 << (ix % 8)))) { 480366911cdSDavid Howells _debug("ENT[%zu.u]:" 481366911cdSDavid Howells " %u unmarked extension (%u/%u)", 48200317636SDavid Howells blkoff / sizeof(union afs_xdr_dir_block), 483366911cdSDavid Howells offset, tmp, nr_slots); 484f51375cdSDavid Howells return afs_bad(dvnode, afs_file_error_dir_unmarked_ext); 4851da177e4SLinus Torvalds } 4861da177e4SLinus Torvalds } 4871da177e4SLinus Torvalds 4881da177e4SLinus Torvalds /* skip if starts before the current position */ 4891da177e4SLinus Torvalds if (offset < curr) 4901da177e4SLinus Torvalds continue; 4911da177e4SLinus Torvalds 4921da177e4SLinus Torvalds /* found the next entry */ 4931bbae9f8SAl Viro if (!dir_emit(ctx, dire->u.name, nlen, 4941da177e4SLinus Torvalds ntohl(dire->u.vnode), 4955cf9dd55SDavid Howells (ctx->actor == afs_lookup_filldir || 4965cf9dd55SDavid Howells ctx->actor == afs_lookup_one_filldir)? 4971bbae9f8SAl Viro ntohl(dire->u.unique) : DT_UNKNOWN)) { 4981da177e4SLinus Torvalds _leave(" = 0 [full]"); 4991da177e4SLinus Torvalds return 0; 5001da177e4SLinus Torvalds } 5011da177e4SLinus Torvalds 50200317636SDavid Howells ctx->pos = blkoff + next * sizeof(union afs_xdr_dirent); 5031da177e4SLinus Torvalds } 5041da177e4SLinus Torvalds 5051da177e4SLinus Torvalds _leave(" = 1 [more]"); 5061da177e4SLinus Torvalds return 1; 507ec26815aSDavid Howells } 5081da177e4SLinus Torvalds 5091da177e4SLinus Torvalds /* 51008e0e7c8SDavid Howells * iterate through the data blob that lists the contents of an AFS directory 5111da177e4SLinus Torvalds */ 5121bbae9f8SAl Viro static int afs_dir_iterate(struct inode *dir, struct dir_context *ctx, 5139dd0b82eSDavid Howells struct key *key, afs_dataversion_t *_dir_version) 5141da177e4SLinus Torvalds { 515f3ddee8dSDavid Howells struct afs_vnode *dvnode = AFS_FS_I(dir); 51600317636SDavid Howells struct afs_xdr_dir_page *dbuf; 51700317636SDavid Howells union afs_xdr_dir_block *dblock; 518f3ddee8dSDavid Howells struct afs_read *req; 5191da177e4SLinus Torvalds struct page *page; 5201da177e4SLinus Torvalds unsigned blkoff, limit; 521c4508464SDavid Howells void __rcu **slot; 5221da177e4SLinus Torvalds int ret; 5231da177e4SLinus Torvalds 5241bbae9f8SAl Viro _enter("{%lu},%u,,", dir->i_ino, (unsigned)ctx->pos); 5251da177e4SLinus Torvalds 52608e0e7c8SDavid Howells if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) { 5271da177e4SLinus Torvalds _leave(" = -ESTALE"); 5281da177e4SLinus Torvalds return -ESTALE; 5291da177e4SLinus Torvalds } 5301da177e4SLinus Torvalds 531f3ddee8dSDavid Howells req = afs_read_dir(dvnode, key); 532f3ddee8dSDavid Howells if (IS_ERR(req)) 533f3ddee8dSDavid Howells return PTR_ERR(req); 5349dd0b82eSDavid Howells *_dir_version = req->data_version; 535f3ddee8dSDavid Howells 5361da177e4SLinus Torvalds /* round the file position up to the next entry boundary */ 53700317636SDavid Howells ctx->pos += sizeof(union afs_xdr_dirent) - 1; 53800317636SDavid Howells ctx->pos &= ~(sizeof(union afs_xdr_dirent) - 1); 5391da177e4SLinus Torvalds 5401da177e4SLinus Torvalds /* walk through the blocks in sequence */ 5411da177e4SLinus Torvalds ret = 0; 542f3ddee8dSDavid Howells while (ctx->pos < req->actual_len) { 54300317636SDavid Howells blkoff = ctx->pos & ~(sizeof(union afs_xdr_dir_block) - 1); 5441da177e4SLinus Torvalds 545f3ddee8dSDavid Howells /* Fetch the appropriate page from the directory and re-add it 546c4508464SDavid Howells * to the LRU. We have all the pages pinned with an extra ref. 547f3ddee8dSDavid Howells */ 548c4508464SDavid Howells rcu_read_lock(); 549c4508464SDavid Howells page = NULL; 550c4508464SDavid Howells slot = radix_tree_lookup_slot(&dvnode->vfs_inode.i_mapping->i_pages, 551c4508464SDavid Howells blkoff / PAGE_SIZE); 552c4508464SDavid Howells if (slot) 553c4508464SDavid Howells page = radix_tree_deref_slot(slot); 554c4508464SDavid Howells rcu_read_unlock(); 555f3ddee8dSDavid Howells if (!page) { 556f51375cdSDavid Howells ret = afs_bad(dvnode, afs_file_error_dir_missing_page); 5571da177e4SLinus Torvalds break; 5581da177e4SLinus Torvalds } 559f3ddee8dSDavid Howells mark_page_accessed(page); 5601da177e4SLinus Torvalds 5611da177e4SLinus Torvalds limit = blkoff & ~(PAGE_SIZE - 1); 5621da177e4SLinus Torvalds 563f3ddee8dSDavid Howells dbuf = kmap(page); 5641da177e4SLinus Torvalds 5651da177e4SLinus Torvalds /* deal with the individual blocks stashed on this page */ 5661da177e4SLinus Torvalds do { 5671da177e4SLinus Torvalds dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) / 56800317636SDavid Howells sizeof(union afs_xdr_dir_block)]; 569f51375cdSDavid Howells ret = afs_dir_iterate_block(dvnode, ctx, dblock, blkoff); 5701da177e4SLinus Torvalds if (ret != 1) { 571f3ddee8dSDavid Howells kunmap(page); 5721da177e4SLinus Torvalds goto out; 5731da177e4SLinus Torvalds } 5741da177e4SLinus Torvalds 57500317636SDavid Howells blkoff += sizeof(union afs_xdr_dir_block); 5761da177e4SLinus Torvalds 5771bbae9f8SAl Viro } while (ctx->pos < dir->i_size && blkoff < limit); 5781da177e4SLinus Torvalds 579f3ddee8dSDavid Howells kunmap(page); 5801da177e4SLinus Torvalds ret = 0; 5811da177e4SLinus Torvalds } 5821da177e4SLinus Torvalds 5831da177e4SLinus Torvalds out: 584b61f7dcfSDavid Howells up_read(&dvnode->validate_lock); 585f3ddee8dSDavid Howells afs_put_read(req); 5861da177e4SLinus Torvalds _leave(" = %d", ret); 5871da177e4SLinus Torvalds return ret; 588ec26815aSDavid Howells } 5891da177e4SLinus Torvalds 5901da177e4SLinus Torvalds /* 5911da177e4SLinus Torvalds * read an AFS directory 5921da177e4SLinus Torvalds */ 5931bbae9f8SAl Viro static int afs_readdir(struct file *file, struct dir_context *ctx) 5941da177e4SLinus Torvalds { 5959dd0b82eSDavid Howells afs_dataversion_t dir_version; 5969dd0b82eSDavid Howells 5979dd0b82eSDavid Howells return afs_dir_iterate(file_inode(file), ctx, afs_file_key(file), 5989dd0b82eSDavid Howells &dir_version); 599ec26815aSDavid Howells } 6001da177e4SLinus Torvalds 6011da177e4SLinus Torvalds /* 6025cf9dd55SDavid Howells * Search the directory for a single name 6031da177e4SLinus Torvalds * - if afs_dir_iterate_block() spots this function, it'll pass the FID 6041da177e4SLinus Torvalds * uniquifier through dtype 6051da177e4SLinus Torvalds */ 6065cf9dd55SDavid Howells static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name, 607ac7576f4SMiklos Szeredi int nlen, loff_t fpos, u64 ino, unsigned dtype) 6081da177e4SLinus Torvalds { 6095cf9dd55SDavid Howells struct afs_lookup_one_cookie *cookie = 6105cf9dd55SDavid Howells container_of(ctx, struct afs_lookup_one_cookie, ctx); 6111da177e4SLinus Torvalds 6121bbae9f8SAl Viro _enter("{%s,%u},%s,%u,,%llu,%u", 6131bbae9f8SAl Viro cookie->name.name, cookie->name.len, name, nlen, 614ba3e0e1aSDavid S. Miller (unsigned long long) ino, dtype); 6151da177e4SLinus Torvalds 61608e0e7c8SDavid Howells /* insanity checks first */ 61700317636SDavid Howells BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048); 61800317636SDavid Howells BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32); 61908e0e7c8SDavid Howells 6201bbae9f8SAl Viro if (cookie->name.len != nlen || 6211bbae9f8SAl Viro memcmp(cookie->name.name, name, nlen) != 0) { 6221da177e4SLinus Torvalds _leave(" = 0 [no]"); 6231da177e4SLinus Torvalds return 0; 6241da177e4SLinus Torvalds } 6251da177e4SLinus Torvalds 6261da177e4SLinus Torvalds cookie->fid.vnode = ino; 6271da177e4SLinus Torvalds cookie->fid.unique = dtype; 6281da177e4SLinus Torvalds cookie->found = 1; 6291da177e4SLinus Torvalds 6301da177e4SLinus Torvalds _leave(" = -1 [found]"); 6311da177e4SLinus Torvalds return -1; 632ec26815aSDavid Howells } 6331da177e4SLinus Torvalds 6341da177e4SLinus Torvalds /* 6355cf9dd55SDavid Howells * Do a lookup of a single name in a directory 636260a9803SDavid Howells * - just returns the FID the dentry name maps to if found 6371da177e4SLinus Torvalds */ 6385cf9dd55SDavid Howells static int afs_do_lookup_one(struct inode *dir, struct dentry *dentry, 6399dd0b82eSDavid Howells struct afs_fid *fid, struct key *key, 6409dd0b82eSDavid Howells afs_dataversion_t *_dir_version) 6411da177e4SLinus Torvalds { 6421bbae9f8SAl Viro struct afs_super_info *as = dir->i_sb->s_fs_info; 6435cf9dd55SDavid Howells struct afs_lookup_one_cookie cookie = { 6445cf9dd55SDavid Howells .ctx.actor = afs_lookup_one_filldir, 6451bbae9f8SAl Viro .name = dentry->d_name, 6461bbae9f8SAl Viro .fid.vid = as->volume->vid 6471bbae9f8SAl Viro }; 6481da177e4SLinus Torvalds int ret; 6491da177e4SLinus Torvalds 650a455589fSAl Viro _enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry); 6511da177e4SLinus Torvalds 6521da177e4SLinus Torvalds /* search the directory */ 6539dd0b82eSDavid Howells ret = afs_dir_iterate(dir, &cookie.ctx, key, _dir_version); 6541da177e4SLinus Torvalds if (ret < 0) { 65508e0e7c8SDavid Howells _leave(" = %d [iter]", ret); 65608e0e7c8SDavid Howells return ret; 6571da177e4SLinus Torvalds } 6581da177e4SLinus Torvalds 6591da177e4SLinus Torvalds if (!cookie.found) { 66008e0e7c8SDavid Howells _leave(" = -ENOENT [not found]"); 66108e0e7c8SDavid Howells return -ENOENT; 66208e0e7c8SDavid Howells } 66308e0e7c8SDavid Howells 66408e0e7c8SDavid Howells *fid = cookie.fid; 6653b6492dfSDavid Howells _leave(" = 0 { vn=%llu u=%u }", fid->vnode, fid->unique); 66608e0e7c8SDavid Howells return 0; 66708e0e7c8SDavid Howells } 66808e0e7c8SDavid Howells 66908e0e7c8SDavid Howells /* 6705cf9dd55SDavid Howells * search the directory for a name 6715cf9dd55SDavid Howells * - if afs_dir_iterate_block() spots this function, it'll pass the FID 6725cf9dd55SDavid Howells * uniquifier through dtype 6735cf9dd55SDavid Howells */ 6745cf9dd55SDavid Howells static int afs_lookup_filldir(struct dir_context *ctx, const char *name, 6755cf9dd55SDavid Howells int nlen, loff_t fpos, u64 ino, unsigned dtype) 6765cf9dd55SDavid Howells { 6775cf9dd55SDavid Howells struct afs_lookup_cookie *cookie = 6785cf9dd55SDavid Howells container_of(ctx, struct afs_lookup_cookie, ctx); 6795cf9dd55SDavid Howells int ret; 6805cf9dd55SDavid Howells 6815cf9dd55SDavid Howells _enter("{%s,%u},%s,%u,,%llu,%u", 6825cf9dd55SDavid Howells cookie->name.name, cookie->name.len, name, nlen, 6835cf9dd55SDavid Howells (unsigned long long) ino, dtype); 6845cf9dd55SDavid Howells 6855cf9dd55SDavid Howells /* insanity checks first */ 68600317636SDavid Howells BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048); 68700317636SDavid Howells BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32); 6885cf9dd55SDavid Howells 6895cf9dd55SDavid Howells if (cookie->found) { 6905cf9dd55SDavid Howells if (cookie->nr_fids < 50) { 6915cf9dd55SDavid Howells cookie->fids[cookie->nr_fids].vnode = ino; 6925cf9dd55SDavid Howells cookie->fids[cookie->nr_fids].unique = dtype; 6935cf9dd55SDavid Howells cookie->nr_fids++; 6945cf9dd55SDavid Howells } 6955cf9dd55SDavid Howells } else if (cookie->name.len == nlen && 6965cf9dd55SDavid Howells memcmp(cookie->name.name, name, nlen) == 0) { 697e49c7b2fSDavid Howells cookie->fids[1].vnode = ino; 698e49c7b2fSDavid Howells cookie->fids[1].unique = dtype; 6995cf9dd55SDavid Howells cookie->found = 1; 7005cf9dd55SDavid Howells if (cookie->one_only) 7015cf9dd55SDavid Howells return -1; 7025cf9dd55SDavid Howells } 7035cf9dd55SDavid Howells 7045cf9dd55SDavid Howells ret = cookie->nr_fids >= 50 ? -1 : 0; 7055cf9dd55SDavid Howells _leave(" = %d", ret); 7065cf9dd55SDavid Howells return ret; 7075cf9dd55SDavid Howells } 7085cf9dd55SDavid Howells 7095cf9dd55SDavid Howells /* 710e49c7b2fSDavid Howells * Deal with the result of a successful lookup operation. Turn all the files 711e49c7b2fSDavid Howells * into inodes and save the first one - which is the one we actually want. 712e49c7b2fSDavid Howells */ 713e49c7b2fSDavid Howells static void afs_do_lookup_success(struct afs_operation *op) 714e49c7b2fSDavid Howells { 715e49c7b2fSDavid Howells struct afs_vnode_param *vp; 716e49c7b2fSDavid Howells struct afs_vnode *vnode; 717e49c7b2fSDavid Howells struct inode *inode; 718e49c7b2fSDavid Howells u32 abort_code; 719e49c7b2fSDavid Howells int i; 720e49c7b2fSDavid Howells 721e49c7b2fSDavid Howells _enter(""); 722e49c7b2fSDavid Howells 723e49c7b2fSDavid Howells for (i = 0; i < op->nr_files; i++) { 724e49c7b2fSDavid Howells switch (i) { 725e49c7b2fSDavid Howells case 0: 726e49c7b2fSDavid Howells vp = &op->file[0]; 727e49c7b2fSDavid Howells abort_code = vp->scb.status.abort_code; 728e49c7b2fSDavid Howells if (abort_code != 0) { 72944767c35SDavid Howells op->ac.abort_code = abort_code; 730e49c7b2fSDavid Howells op->error = afs_abort_to_error(abort_code); 731e49c7b2fSDavid Howells } 732e49c7b2fSDavid Howells break; 733e49c7b2fSDavid Howells 734e49c7b2fSDavid Howells case 1: 735e49c7b2fSDavid Howells vp = &op->file[1]; 736e49c7b2fSDavid Howells break; 737e49c7b2fSDavid Howells 738e49c7b2fSDavid Howells default: 739e49c7b2fSDavid Howells vp = &op->more_files[i - 2]; 740e49c7b2fSDavid Howells break; 741e49c7b2fSDavid Howells } 742e49c7b2fSDavid Howells 743e49c7b2fSDavid Howells if (!vp->scb.have_status && !vp->scb.have_error) 744e49c7b2fSDavid Howells continue; 745e49c7b2fSDavid Howells 746e49c7b2fSDavid Howells _debug("do [%u]", i); 747e49c7b2fSDavid Howells if (vp->vnode) { 748e49c7b2fSDavid Howells if (!test_bit(AFS_VNODE_UNSET, &vp->vnode->flags)) 749e49c7b2fSDavid Howells afs_vnode_commit_status(op, vp); 750e49c7b2fSDavid Howells } else if (vp->scb.status.abort_code == 0) { 751e49c7b2fSDavid Howells inode = afs_iget(op, vp); 752e49c7b2fSDavid Howells if (!IS_ERR(inode)) { 753e49c7b2fSDavid Howells vnode = AFS_FS_I(inode); 754e49c7b2fSDavid Howells afs_cache_permit(vnode, op->key, 755e49c7b2fSDavid Howells 0 /* Assume vnode->cb_break is 0 */ + 756e49c7b2fSDavid Howells op->cb_v_break, 757e49c7b2fSDavid Howells &vp->scb); 758e49c7b2fSDavid Howells vp->vnode = vnode; 759e49c7b2fSDavid Howells vp->put_vnode = true; 760e49c7b2fSDavid Howells } 761e49c7b2fSDavid Howells } else { 762e49c7b2fSDavid Howells _debug("- abort %d %llx:%llx.%x", 763e49c7b2fSDavid Howells vp->scb.status.abort_code, 764e49c7b2fSDavid Howells vp->fid.vid, vp->fid.vnode, vp->fid.unique); 765e49c7b2fSDavid Howells } 766e49c7b2fSDavid Howells } 767e49c7b2fSDavid Howells 768e49c7b2fSDavid Howells _leave(""); 769e49c7b2fSDavid Howells } 770e49c7b2fSDavid Howells 771e49c7b2fSDavid Howells static const struct afs_operation_ops afs_inline_bulk_status_operation = { 772e49c7b2fSDavid Howells .issue_afs_rpc = afs_fs_inline_bulk_status, 773e49c7b2fSDavid Howells .issue_yfs_rpc = yfs_fs_inline_bulk_status, 774e49c7b2fSDavid Howells .success = afs_do_lookup_success, 775e49c7b2fSDavid Howells }; 776e49c7b2fSDavid Howells 777b6489a49SDavid Howells static const struct afs_operation_ops afs_lookup_fetch_status_operation = { 778e49c7b2fSDavid Howells .issue_afs_rpc = afs_fs_fetch_status, 779e49c7b2fSDavid Howells .issue_yfs_rpc = yfs_fs_fetch_status, 780e49c7b2fSDavid Howells .success = afs_do_lookup_success, 781728279a5SDavid Howells .aborted = afs_check_for_remote_deletion, 782e49c7b2fSDavid Howells }; 783e49c7b2fSDavid Howells 784e49c7b2fSDavid Howells /* 78520325960SDavid Howells * See if we know that the server we expect to use doesn't support 78620325960SDavid Howells * FS.InlineBulkStatus. 78720325960SDavid Howells */ 78820325960SDavid Howells static bool afs_server_supports_ibulk(struct afs_vnode *dvnode) 78920325960SDavid Howells { 79020325960SDavid Howells struct afs_server_list *slist; 79120325960SDavid Howells struct afs_volume *volume = dvnode->volume; 79220325960SDavid Howells struct afs_server *server; 79320325960SDavid Howells bool ret = true; 79420325960SDavid Howells int i; 79520325960SDavid Howells 79620325960SDavid Howells if (!test_bit(AFS_VOLUME_MAYBE_NO_IBULK, &volume->flags)) 79720325960SDavid Howells return true; 79820325960SDavid Howells 79920325960SDavid Howells rcu_read_lock(); 80020325960SDavid Howells slist = rcu_dereference(volume->servers); 80120325960SDavid Howells 80220325960SDavid Howells for (i = 0; i < slist->nr_servers; i++) { 80320325960SDavid Howells server = slist->servers[i].server; 80420325960SDavid Howells if (server == dvnode->cb_server) { 80520325960SDavid Howells if (test_bit(AFS_SERVER_FL_NO_IBULK, &server->flags)) 80620325960SDavid Howells ret = false; 80720325960SDavid Howells break; 80820325960SDavid Howells } 80920325960SDavid Howells } 81020325960SDavid Howells 81120325960SDavid Howells rcu_read_unlock(); 81220325960SDavid Howells return ret; 81320325960SDavid Howells } 81420325960SDavid Howells 81520325960SDavid Howells /* 8165cf9dd55SDavid Howells * Do a lookup in a directory. We make use of bulk lookup to query a slew of 8175cf9dd55SDavid Howells * files in one go and create inodes for them. The inode of the file we were 8185cf9dd55SDavid Howells * asked for is returned. 8195cf9dd55SDavid Howells */ 8205cf9dd55SDavid Howells static struct inode *afs_do_lookup(struct inode *dir, struct dentry *dentry, 8215cf9dd55SDavid Howells struct key *key) 8225cf9dd55SDavid Howells { 8235cf9dd55SDavid Howells struct afs_lookup_cookie *cookie; 824e49c7b2fSDavid Howells struct afs_vnode_param *vp; 825e49c7b2fSDavid Howells struct afs_operation *op; 82639db9815SDavid Howells struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode; 82739db9815SDavid Howells struct inode *inode = NULL, *ti; 8289dd0b82eSDavid Howells afs_dataversion_t data_version = READ_ONCE(dvnode->status.data_version); 829e49c7b2fSDavid Howells long ret; 830e49c7b2fSDavid Howells int i; 8315cf9dd55SDavid Howells 8325cf9dd55SDavid Howells _enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry); 8335cf9dd55SDavid Howells 8345cf9dd55SDavid Howells cookie = kzalloc(sizeof(struct afs_lookup_cookie), GFP_KERNEL); 8355cf9dd55SDavid Howells if (!cookie) 8365cf9dd55SDavid Howells return ERR_PTR(-ENOMEM); 8375cf9dd55SDavid Howells 838e49c7b2fSDavid Howells for (i = 0; i < ARRAY_SIZE(cookie->fids); i++) 839e49c7b2fSDavid Howells cookie->fids[i].vid = dvnode->fid.vid; 8405cf9dd55SDavid Howells cookie->ctx.actor = afs_lookup_filldir; 8415cf9dd55SDavid Howells cookie->name = dentry->d_name; 84213fcc635SDavid Howells cookie->nr_fids = 2; /* slot 0 is saved for the fid we actually want 84313fcc635SDavid Howells * and slot 1 for the directory */ 8445cf9dd55SDavid Howells 84520325960SDavid Howells if (!afs_server_supports_ibulk(dvnode)) 8465cf9dd55SDavid Howells cookie->one_only = true; 8475cf9dd55SDavid Howells 8485cf9dd55SDavid Howells /* search the directory */ 8499dd0b82eSDavid Howells ret = afs_dir_iterate(dir, &cookie->ctx, key, &data_version); 850e49c7b2fSDavid Howells if (ret < 0) 8515cf9dd55SDavid Howells goto out; 8525cf9dd55SDavid Howells 8539dd0b82eSDavid Howells dentry->d_fsdata = (void *)(unsigned long)data_version; 8549dd0b82eSDavid Howells 855e49c7b2fSDavid Howells ret = -ENOENT; 8565cf9dd55SDavid Howells if (!cookie->found) 8575cf9dd55SDavid Howells goto out; 8585cf9dd55SDavid Howells 8595cf9dd55SDavid Howells /* Check to see if we already have an inode for the primary fid. */ 860e49c7b2fSDavid Howells inode = ilookup5(dir->i_sb, cookie->fids[1].vnode, 861e49c7b2fSDavid Howells afs_ilookup5_test_by_fid, &cookie->fids[1]); 8625cf9dd55SDavid Howells if (inode) 863e49c7b2fSDavid Howells goto out; /* We do */ 864e49c7b2fSDavid Howells 865e49c7b2fSDavid Howells /* Okay, we didn't find it. We need to query the server - and whilst 866e49c7b2fSDavid Howells * we're doing that, we're going to attempt to look up a bunch of other 867e49c7b2fSDavid Howells * vnodes also. 868e49c7b2fSDavid Howells */ 869e49c7b2fSDavid Howells op = afs_alloc_operation(NULL, dvnode->volume); 870e49c7b2fSDavid Howells if (IS_ERR(op)) { 871e49c7b2fSDavid Howells ret = PTR_ERR(op); 8725cf9dd55SDavid Howells goto out; 873e49c7b2fSDavid Howells } 874e49c7b2fSDavid Howells 875e49c7b2fSDavid Howells afs_op_set_vnode(op, 0, dvnode); 876e49c7b2fSDavid Howells afs_op_set_fid(op, 1, &cookie->fids[1]); 877e49c7b2fSDavid Howells 878e49c7b2fSDavid Howells op->nr_files = cookie->nr_fids; 879e49c7b2fSDavid Howells _debug("nr_files %u", op->nr_files); 8805cf9dd55SDavid Howells 8815cf9dd55SDavid Howells /* Need space for examining all the selected files */ 882e49c7b2fSDavid Howells op->error = -ENOMEM; 883e49c7b2fSDavid Howells if (op->nr_files > 2) { 884e49c7b2fSDavid Howells op->more_files = kvcalloc(op->nr_files - 2, 885e49c7b2fSDavid Howells sizeof(struct afs_vnode_param), 8865cf9dd55SDavid Howells GFP_KERNEL); 887e49c7b2fSDavid Howells if (!op->more_files) 888e49c7b2fSDavid Howells goto out_op; 8895cf9dd55SDavid Howells 890e49c7b2fSDavid Howells for (i = 2; i < op->nr_files; i++) { 891e49c7b2fSDavid Howells vp = &op->more_files[i - 2]; 892e49c7b2fSDavid Howells vp->fid = cookie->fids[i]; 89339db9815SDavid Howells 89439db9815SDavid Howells /* Find any inodes that already exist and get their 89539db9815SDavid Howells * callback counters. 89639db9815SDavid Howells */ 897e49c7b2fSDavid Howells ti = ilookup5_nowait(dir->i_sb, vp->fid.vnode, 898e49c7b2fSDavid Howells afs_ilookup5_test_by_fid, &vp->fid); 89939db9815SDavid Howells if (!IS_ERR_OR_NULL(ti)) { 90039db9815SDavid Howells vnode = AFS_FS_I(ti); 901e49c7b2fSDavid Howells vp->dv_before = vnode->status.data_version; 902e49c7b2fSDavid Howells vp->cb_break_before = afs_calc_vnode_cb_break(vnode); 903e49c7b2fSDavid Howells vp->vnode = vnode; 904e49c7b2fSDavid Howells vp->put_vnode = true; 905a9e5c87cSDavid Howells vp->speculative = true; /* vnode not locked */ 906e49c7b2fSDavid Howells } 90739db9815SDavid Howells } 90839db9815SDavid Howells } 90939db9815SDavid Howells 9105cf9dd55SDavid Howells /* Try FS.InlineBulkStatus first. Abort codes for the individual 9115cf9dd55SDavid Howells * lookups contained therein are stored in the reply without aborting 9125cf9dd55SDavid Howells * the whole operation. 9135cf9dd55SDavid Howells */ 914e49c7b2fSDavid Howells op->error = -ENOTSUPP; 915e49c7b2fSDavid Howells if (!cookie->one_only) { 916e49c7b2fSDavid Howells op->ops = &afs_inline_bulk_status_operation; 917e49c7b2fSDavid Howells afs_begin_vnode_operation(op); 918e49c7b2fSDavid Howells afs_wait_for_operation(op); 9195cf9dd55SDavid Howells } 9205cf9dd55SDavid Howells 921e49c7b2fSDavid Howells if (op->error == -ENOTSUPP) { 922e49c7b2fSDavid Howells /* We could try FS.BulkStatus next, but this aborts the entire 923e49c7b2fSDavid Howells * op if any of the lookups fails - so, for the moment, revert 924e49c7b2fSDavid Howells * to FS.FetchStatus for op->file[1]. 9255cf9dd55SDavid Howells */ 926e49c7b2fSDavid Howells op->fetch_status.which = 1; 927f8ea5c7bSDavid Howells op->ops = &afs_lookup_fetch_status_operation; 928e49c7b2fSDavid Howells afs_begin_vnode_operation(op); 929e49c7b2fSDavid Howells afs_wait_for_operation(op); 930e49c7b2fSDavid Howells } 931e49c7b2fSDavid Howells inode = ERR_PTR(op->error); 932e49c7b2fSDavid Howells 933e49c7b2fSDavid Howells out_op: 934e49c7b2fSDavid Howells if (op->error == 0) { 935e49c7b2fSDavid Howells inode = &op->file[1].vnode->vfs_inode; 936e49c7b2fSDavid Howells op->file[1].vnode = NULL; 9375cf9dd55SDavid Howells } 9385cf9dd55SDavid Howells 939e49c7b2fSDavid Howells if (op->file[0].scb.have_status) 940e49c7b2fSDavid Howells dentry->d_fsdata = (void *)(unsigned long)op->file[0].scb.status.data_version; 941e49c7b2fSDavid Howells else 942e49c7b2fSDavid Howells dentry->d_fsdata = (void *)(unsigned long)op->file[0].dv_before; 943e49c7b2fSDavid Howells ret = afs_put_operation(op); 9445cf9dd55SDavid Howells out: 9455cf9dd55SDavid Howells kfree(cookie); 946e49c7b2fSDavid Howells _leave(""); 947e49c7b2fSDavid Howells return inode ?: ERR_PTR(ret); 9485cf9dd55SDavid Howells } 9495cf9dd55SDavid Howells 9505cf9dd55SDavid Howells /* 9516f8880d8SDavid Howells * Look up an entry in a directory with @sys substitution. 9526f8880d8SDavid Howells */ 9536f8880d8SDavid Howells static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry, 9546f8880d8SDavid Howells struct key *key) 9556f8880d8SDavid Howells { 9566f8880d8SDavid Howells struct afs_sysnames *subs; 9576f8880d8SDavid Howells struct afs_net *net = afs_i2net(dir); 9586f8880d8SDavid Howells struct dentry *ret; 9596f8880d8SDavid Howells char *buf, *p, *name; 9606f8880d8SDavid Howells int len, i; 9616f8880d8SDavid Howells 9626f8880d8SDavid Howells _enter(""); 9636f8880d8SDavid Howells 9646f8880d8SDavid Howells ret = ERR_PTR(-ENOMEM); 9656f8880d8SDavid Howells p = buf = kmalloc(AFSNAMEMAX, GFP_KERNEL); 9666f8880d8SDavid Howells if (!buf) 9676f8880d8SDavid Howells goto out_p; 9686f8880d8SDavid Howells if (dentry->d_name.len > 4) { 9696f8880d8SDavid Howells memcpy(p, dentry->d_name.name, dentry->d_name.len - 4); 9706f8880d8SDavid Howells p += dentry->d_name.len - 4; 9716f8880d8SDavid Howells } 9726f8880d8SDavid Howells 9736f8880d8SDavid Howells /* There is an ordered list of substitutes that we have to try. */ 9746f8880d8SDavid Howells read_lock(&net->sysnames_lock); 9756f8880d8SDavid Howells subs = net->sysnames; 9766f8880d8SDavid Howells refcount_inc(&subs->usage); 9776f8880d8SDavid Howells read_unlock(&net->sysnames_lock); 9786f8880d8SDavid Howells 9796f8880d8SDavid Howells for (i = 0; i < subs->nr; i++) { 9806f8880d8SDavid Howells name = subs->subs[i]; 9816f8880d8SDavid Howells len = dentry->d_name.len - 4 + strlen(name); 9826f8880d8SDavid Howells if (len >= AFSNAMEMAX) { 9836f8880d8SDavid Howells ret = ERR_PTR(-ENAMETOOLONG); 9846f8880d8SDavid Howells goto out_s; 9856f8880d8SDavid Howells } 9866f8880d8SDavid Howells 9876f8880d8SDavid Howells strcpy(p, name); 9886f8880d8SDavid Howells ret = lookup_one_len(buf, dentry->d_parent, len); 9896f8880d8SDavid Howells if (IS_ERR(ret) || d_is_positive(ret)) 9906f8880d8SDavid Howells goto out_s; 9916f8880d8SDavid Howells dput(ret); 9926f8880d8SDavid Howells } 9936f8880d8SDavid Howells 9946f8880d8SDavid Howells /* We don't want to d_add() the @sys dentry here as we don't want to 9956f8880d8SDavid Howells * the cached dentry to hide changes to the sysnames list. 9966f8880d8SDavid Howells */ 9976f8880d8SDavid Howells ret = NULL; 9986f8880d8SDavid Howells out_s: 9996f8880d8SDavid Howells afs_put_sysnames(subs); 10006f8880d8SDavid Howells kfree(buf); 10016f8880d8SDavid Howells out_p: 10026f8880d8SDavid Howells key_put(key); 10036f8880d8SDavid Howells return ret; 10046f8880d8SDavid Howells } 10056f8880d8SDavid Howells 10066f8880d8SDavid Howells /* 100708e0e7c8SDavid Howells * look up an entry in a directory 100808e0e7c8SDavid Howells */ 1009260a9803SDavid Howells static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry, 101000cd8dd3SAl Viro unsigned int flags) 101108e0e7c8SDavid Howells { 10125cf9dd55SDavid Howells struct afs_vnode *dvnode = AFS_FS_I(dir); 101340a708bdSDavid Howells struct afs_fid fid = {}; 101408e0e7c8SDavid Howells struct inode *inode; 101534b2a88fSAl Viro struct dentry *d; 101600d3b7a4SDavid Howells struct key *key; 101708e0e7c8SDavid Howells int ret; 101808e0e7c8SDavid Howells 10193b6492dfSDavid Howells _enter("{%llx:%llu},%p{%pd},", 10205cf9dd55SDavid Howells dvnode->fid.vid, dvnode->fid.vnode, dentry, dentry); 1021260a9803SDavid Howells 10222b0143b5SDavid Howells ASSERTCMP(d_inode(dentry), ==, NULL); 102308e0e7c8SDavid Howells 102445222b9eSDavid Howells if (dentry->d_name.len >= AFSNAMEMAX) { 102508e0e7c8SDavid Howells _leave(" = -ENAMETOOLONG"); 102608e0e7c8SDavid Howells return ERR_PTR(-ENAMETOOLONG); 102708e0e7c8SDavid Howells } 102808e0e7c8SDavid Howells 10295cf9dd55SDavid Howells if (test_bit(AFS_VNODE_DELETED, &dvnode->flags)) { 103008e0e7c8SDavid Howells _leave(" = -ESTALE"); 103108e0e7c8SDavid Howells return ERR_PTR(-ESTALE); 103208e0e7c8SDavid Howells } 103308e0e7c8SDavid Howells 10345cf9dd55SDavid Howells key = afs_request_key(dvnode->volume->cell); 103500d3b7a4SDavid Howells if (IS_ERR(key)) { 103600d3b7a4SDavid Howells _leave(" = %ld [key]", PTR_ERR(key)); 1037e231c2eeSDavid Howells return ERR_CAST(key); 103800d3b7a4SDavid Howells } 103900d3b7a4SDavid Howells 10405cf9dd55SDavid Howells ret = afs_validate(dvnode, key); 104108e0e7c8SDavid Howells if (ret < 0) { 104200d3b7a4SDavid Howells key_put(key); 1043260a9803SDavid Howells _leave(" = %d [val]", ret); 10441da177e4SLinus Torvalds return ERR_PTR(ret); 10451da177e4SLinus Torvalds } 10461da177e4SLinus Torvalds 10476f8880d8SDavid Howells if (dentry->d_name.len >= 4 && 10486f8880d8SDavid Howells dentry->d_name.name[dentry->d_name.len - 4] == '@' && 10496f8880d8SDavid Howells dentry->d_name.name[dentry->d_name.len - 3] == 's' && 10506f8880d8SDavid Howells dentry->d_name.name[dentry->d_name.len - 2] == 'y' && 10516f8880d8SDavid Howells dentry->d_name.name[dentry->d_name.len - 1] == 's') 10526f8880d8SDavid Howells return afs_lookup_atsys(dir, dentry, key); 10536f8880d8SDavid Howells 1054d55b4da4SDavid Howells afs_stat_v(dvnode, n_lookup); 10555cf9dd55SDavid Howells inode = afs_do_lookup(dir, dentry, key); 105634b2a88fSAl Viro key_put(key); 1057f52b83b0SDavid Howells if (inode == ERR_PTR(-ENOENT)) 10585cf9dd55SDavid Howells inode = afs_try_auto_mntpt(dentry, dir); 105940a708bdSDavid Howells 106040a708bdSDavid Howells if (!IS_ERR_OR_NULL(inode)) 106140a708bdSDavid Howells fid = AFS_FS_I(inode)->fid; 106240a708bdSDavid Howells 1063fed79fd7SDavid Howells _debug("splice %p", dentry->d_inode); 106434b2a88fSAl Viro d = d_splice_alias(inode, dentry); 106580548b03SDavid Howells if (!IS_ERR_OR_NULL(d)) { 106634b2a88fSAl Viro d->d_fsdata = dentry->d_fsdata; 106740a708bdSDavid Howells trace_afs_lookup(dvnode, &d->d_name, &fid); 106880548b03SDavid Howells } else { 106940a708bdSDavid Howells trace_afs_lookup(dvnode, &dentry->d_name, &fid); 107080548b03SDavid Howells } 1071e49c7b2fSDavid Howells _leave(""); 107234b2a88fSAl Viro return d; 1073ec26815aSDavid Howells } 10741da177e4SLinus Torvalds 10751da177e4SLinus Torvalds /* 1076a0753c29SDavid Howells * Check the validity of a dentry under RCU conditions. 1077a0753c29SDavid Howells */ 1078a0753c29SDavid Howells static int afs_d_revalidate_rcu(struct dentry *dentry) 1079a0753c29SDavid Howells { 1080*63d49d84SDavid Howells struct afs_vnode *dvnode; 1081a0753c29SDavid Howells struct dentry *parent; 1082*63d49d84SDavid Howells struct inode *dir; 1083a0753c29SDavid Howells long dir_version, de_version; 1084a0753c29SDavid Howells 1085a0753c29SDavid Howells _enter("%p", dentry); 1086a0753c29SDavid Howells 1087a0753c29SDavid Howells /* Check the parent directory is still valid first. */ 1088a0753c29SDavid Howells parent = READ_ONCE(dentry->d_parent); 1089a0753c29SDavid Howells dir = d_inode_rcu(parent); 1090a0753c29SDavid Howells if (!dir) 1091a0753c29SDavid Howells return -ECHILD; 1092a0753c29SDavid Howells dvnode = AFS_FS_I(dir); 1093a0753c29SDavid Howells if (test_bit(AFS_VNODE_DELETED, &dvnode->flags)) 1094a0753c29SDavid Howells return -ECHILD; 1095a0753c29SDavid Howells 1096a0753c29SDavid Howells if (!afs_check_validity(dvnode)) 1097a0753c29SDavid Howells return -ECHILD; 1098a0753c29SDavid Howells 1099a0753c29SDavid Howells /* We only need to invalidate a dentry if the server's copy changed 1100a0753c29SDavid Howells * behind our back. If we made the change, it's no problem. Note that 1101a0753c29SDavid Howells * on a 32-bit system, we only have 32 bits in the dentry to store the 1102a0753c29SDavid Howells * version. 1103a0753c29SDavid Howells */ 1104a0753c29SDavid Howells dir_version = (long)READ_ONCE(dvnode->status.data_version); 1105a0753c29SDavid Howells de_version = (long)READ_ONCE(dentry->d_fsdata); 1106a0753c29SDavid Howells if (de_version != dir_version) { 1107a0753c29SDavid Howells dir_version = (long)READ_ONCE(dvnode->invalid_before); 1108a0753c29SDavid Howells if (de_version - dir_version < 0) 1109a0753c29SDavid Howells return -ECHILD; 1110a0753c29SDavid Howells } 1111a0753c29SDavid Howells 1112a0753c29SDavid Howells return 1; /* Still valid */ 1113a0753c29SDavid Howells } 1114a0753c29SDavid Howells 1115a0753c29SDavid Howells /* 11161da177e4SLinus Torvalds * check that a dentry lookup hit has found a valid entry 11171da177e4SLinus Torvalds * - NOTE! the hit can be a negative hit too, so we can't assume we have an 11181da177e4SLinus Torvalds * inode 11191da177e4SLinus Torvalds */ 11200b728e19SAl Viro static int afs_d_revalidate(struct dentry *dentry, unsigned int flags) 11211da177e4SLinus Torvalds { 1122260a9803SDavid Howells struct afs_vnode *vnode, *dir; 11233f649ab7SKees Cook struct afs_fid fid; 11241da177e4SLinus Torvalds struct dentry *parent; 1125c435ee34SDavid Howells struct inode *inode; 112600d3b7a4SDavid Howells struct key *key; 112740fc8102SDavid Howells afs_dataversion_t dir_version, invalid_before; 11289dd0b82eSDavid Howells long de_version; 11291da177e4SLinus Torvalds int ret; 11301da177e4SLinus Torvalds 11310b728e19SAl Viro if (flags & LOOKUP_RCU) 1132a0753c29SDavid Howells return afs_d_revalidate_rcu(dentry); 113334286d66SNick Piggin 1134c435ee34SDavid Howells if (d_really_is_positive(dentry)) { 11352b0143b5SDavid Howells vnode = AFS_FS_I(d_inode(dentry)); 11363b6492dfSDavid Howells _enter("{v={%llx:%llu} n=%pd fl=%lx},", 1137a455589fSAl Viro vnode->fid.vid, vnode->fid.vnode, dentry, 1138260a9803SDavid Howells vnode->flags); 1139c435ee34SDavid Howells } else { 1140a455589fSAl Viro _enter("{neg n=%pd}", dentry); 1141c435ee34SDavid Howells } 11421da177e4SLinus Torvalds 1143260a9803SDavid Howells key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell); 114400d3b7a4SDavid Howells if (IS_ERR(key)) 114500d3b7a4SDavid Howells key = NULL; 114600d3b7a4SDavid Howells 1147*63d49d84SDavid Howells /* Hold the parent dentry so we can peer at it */ 114808e0e7c8SDavid Howells parent = dget_parent(dentry); 11492b0143b5SDavid Howells dir = AFS_FS_I(d_inode(parent)); 11501da177e4SLinus Torvalds 1151260a9803SDavid Howells /* validate the parent directory */ 1152260a9803SDavid Howells afs_validate(dir, key); 1153260a9803SDavid Howells 1154260a9803SDavid Howells if (test_bit(AFS_VNODE_DELETED, &dir->flags)) { 1155a455589fSAl Viro _debug("%pd: parent dir deleted", dentry); 1156*63d49d84SDavid Howells goto not_found; 11571da177e4SLinus Torvalds } 11581da177e4SLinus Torvalds 1159a4ff7401SDavid Howells /* We only need to invalidate a dentry if the server's copy changed 1160a4ff7401SDavid Howells * behind our back. If we made the change, it's no problem. Note that 1161a4ff7401SDavid Howells * on a 32-bit system, we only have 32 bits in the dentry to store the 1162a4ff7401SDavid Howells * version. 1163a4ff7401SDavid Howells */ 11649dd0b82eSDavid Howells dir_version = dir->status.data_version; 1165a4ff7401SDavid Howells de_version = (long)dentry->d_fsdata; 11669dd0b82eSDavid Howells if (de_version == (long)dir_version) 11675dc84855SDavid Howells goto out_valid_noupdate; 1168a4ff7401SDavid Howells 116940fc8102SDavid Howells invalid_before = dir->invalid_before; 117040fc8102SDavid Howells if (de_version - (long)invalid_before >= 0) 1171a4ff7401SDavid Howells goto out_valid; 1172260a9803SDavid Howells 117308e0e7c8SDavid Howells _debug("dir modified"); 1174d55b4da4SDavid Howells afs_stat_v(dir, n_reval); 11751da177e4SLinus Torvalds 11761da177e4SLinus Torvalds /* search the directory for this vnode */ 11779dd0b82eSDavid Howells ret = afs_do_lookup_one(&dir->vfs_inode, dentry, &fid, key, &dir_version); 1178260a9803SDavid Howells switch (ret) { 1179260a9803SDavid Howells case 0: 1180260a9803SDavid Howells /* the filename maps to something */ 11812b0143b5SDavid Howells if (d_really_is_negative(dentry)) 1182*63d49d84SDavid Howells goto not_found; 1183c435ee34SDavid Howells inode = d_inode(dentry); 1184c435ee34SDavid Howells if (is_bad_inode(inode)) { 1185a455589fSAl Viro printk("kAFS: afs_d_revalidate: %pd2 has bad inode\n", 1186a455589fSAl Viro dentry); 1187*63d49d84SDavid Howells goto not_found; 11881da177e4SLinus Torvalds } 11891da177e4SLinus Torvalds 1190c435ee34SDavid Howells vnode = AFS_FS_I(inode); 1191c435ee34SDavid Howells 11921da177e4SLinus Torvalds /* if the vnode ID has changed, then the dirent points to a 11931da177e4SLinus Torvalds * different file */ 119408e0e7c8SDavid Howells if (fid.vnode != vnode->fid.vnode) { 11953b6492dfSDavid Howells _debug("%pd: dirent changed [%llu != %llu]", 1196a455589fSAl Viro dentry, fid.vnode, 119708e0e7c8SDavid Howells vnode->fid.vnode); 11981da177e4SLinus Torvalds goto not_found; 11991da177e4SLinus Torvalds } 12001da177e4SLinus Torvalds 12011da177e4SLinus Torvalds /* if the vnode ID uniqifier has changed, then the file has 1202260a9803SDavid Howells * been deleted and replaced, and the original vnode ID has 1203260a9803SDavid Howells * been reused */ 120408e0e7c8SDavid Howells if (fid.unique != vnode->fid.unique) { 1205a455589fSAl Viro _debug("%pd: file deleted (uq %u -> %u I:%u)", 1206a455589fSAl Viro dentry, fid.unique, 12077a224228SJean Noel Cordenner vnode->fid.unique, 1208c435ee34SDavid Howells vnode->vfs_inode.i_generation); 1209260a9803SDavid Howells goto not_found; 1210260a9803SDavid Howells } 1211260a9803SDavid Howells goto out_valid; 1212260a9803SDavid Howells 1213260a9803SDavid Howells case -ENOENT: 1214260a9803SDavid Howells /* the filename is unknown */ 1215a455589fSAl Viro _debug("%pd: dirent not found", dentry); 12162b0143b5SDavid Howells if (d_really_is_positive(dentry)) 1217260a9803SDavid Howells goto not_found; 1218260a9803SDavid Howells goto out_valid; 1219260a9803SDavid Howells 1220260a9803SDavid Howells default: 1221a455589fSAl Viro _debug("failed to iterate dir %pd: %d", 1222a455589fSAl Viro parent, ret); 1223*63d49d84SDavid Howells goto not_found; 12241da177e4SLinus Torvalds } 122508e0e7c8SDavid Howells 12261da177e4SLinus Torvalds out_valid: 12279dd0b82eSDavid Howells dentry->d_fsdata = (void *)(unsigned long)dir_version; 12285dc84855SDavid Howells out_valid_noupdate: 12291da177e4SLinus Torvalds dput(parent); 123000d3b7a4SDavid Howells key_put(key); 12311da177e4SLinus Torvalds _leave(" = 1 [valid]"); 12321da177e4SLinus Torvalds return 1; 12331da177e4SLinus Torvalds 12341da177e4SLinus Torvalds not_found: 1235a455589fSAl Viro _debug("dropping dentry %pd2", dentry); 12361da177e4SLinus Torvalds dput(parent); 123700d3b7a4SDavid Howells key_put(key); 12381da177e4SLinus Torvalds 12391da177e4SLinus Torvalds _leave(" = 0 [bad]"); 12401da177e4SLinus Torvalds return 0; 1241ec26815aSDavid Howells } 12421da177e4SLinus Torvalds 12431da177e4SLinus Torvalds /* 12441da177e4SLinus Torvalds * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't 12451da177e4SLinus Torvalds * sleep) 12461da177e4SLinus Torvalds * - called from dput() when d_count is going to 0. 12471da177e4SLinus Torvalds * - return 1 to request dentry be unhashed, 0 otherwise 12481da177e4SLinus Torvalds */ 1249fe15ce44SNick Piggin static int afs_d_delete(const struct dentry *dentry) 12501da177e4SLinus Torvalds { 1251a455589fSAl Viro _enter("%pd", dentry); 12521da177e4SLinus Torvalds 12531da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) 12541da177e4SLinus Torvalds goto zap; 12551da177e4SLinus Torvalds 12562b0143b5SDavid Howells if (d_really_is_positive(dentry) && 12572b0143b5SDavid Howells (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(d_inode(dentry))->flags) || 12582b0143b5SDavid Howells test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(d_inode(dentry))->flags))) 12591da177e4SLinus Torvalds goto zap; 12601da177e4SLinus Torvalds 12611da177e4SLinus Torvalds _leave(" = 0 [keep]"); 12621da177e4SLinus Torvalds return 0; 12631da177e4SLinus Torvalds 12641da177e4SLinus Torvalds zap: 12651da177e4SLinus Torvalds _leave(" = 1 [zap]"); 12661da177e4SLinus Torvalds return 1; 1267ec26815aSDavid Howells } 1268260a9803SDavid Howells 1269260a9803SDavid Howells /* 127079ddbfa5SDavid Howells * Clean up sillyrename files on dentry removal. 127179ddbfa5SDavid Howells */ 127279ddbfa5SDavid Howells static void afs_d_iput(struct dentry *dentry, struct inode *inode) 127379ddbfa5SDavid Howells { 127479ddbfa5SDavid Howells if (dentry->d_flags & DCACHE_NFSFS_RENAMED) 127579ddbfa5SDavid Howells afs_silly_iput(dentry, inode); 127679ddbfa5SDavid Howells iput(inode); 127779ddbfa5SDavid Howells } 127879ddbfa5SDavid Howells 127979ddbfa5SDavid Howells /* 1280260a9803SDavid Howells * handle dentry release 1281260a9803SDavid Howells */ 128266c7e1d3SDavid Howells void afs_d_release(struct dentry *dentry) 1283260a9803SDavid Howells { 1284a455589fSAl Viro _enter("%pd", dentry); 1285260a9803SDavid Howells } 1286260a9803SDavid Howells 1287728279a5SDavid Howells void afs_check_for_remote_deletion(struct afs_operation *op) 1288728279a5SDavid Howells { 1289728279a5SDavid Howells struct afs_vnode *vnode = op->file[0].vnode; 1290728279a5SDavid Howells 1291728279a5SDavid Howells switch (op->ac.abort_code) { 1292728279a5SDavid Howells case VNOVNODE: 1293728279a5SDavid Howells set_bit(AFS_VNODE_DELETED, &vnode->flags); 1294728279a5SDavid Howells afs_break_callback(vnode, afs_cb_break_for_deleted); 1295728279a5SDavid Howells } 1296728279a5SDavid Howells } 1297728279a5SDavid Howells 1298260a9803SDavid Howells /* 1299d2ddc776SDavid Howells * Create a new inode for create/mkdir/symlink 1300d2ddc776SDavid Howells */ 1301e49c7b2fSDavid Howells static void afs_vnode_new_inode(struct afs_operation *op) 1302d2ddc776SDavid Howells { 1303e49c7b2fSDavid Howells struct afs_vnode_param *vp = &op->file[1]; 13045a813276SDavid Howells struct afs_vnode *vnode; 1305d2ddc776SDavid Howells struct inode *inode; 1306d2ddc776SDavid Howells 1307e49c7b2fSDavid Howells _enter(""); 1308d2ddc776SDavid Howells 1309e49c7b2fSDavid Howells ASSERTCMP(op->error, ==, 0); 1310e49c7b2fSDavid Howells 1311e49c7b2fSDavid Howells inode = afs_iget(op, vp); 1312d2ddc776SDavid Howells if (IS_ERR(inode)) { 1313d2ddc776SDavid Howells /* ENOMEM or EINTR at a really inconvenient time - just abandon 1314d2ddc776SDavid Howells * the new directory on the server. 1315d2ddc776SDavid Howells */ 1316e49c7b2fSDavid Howells op->error = PTR_ERR(inode); 1317d2ddc776SDavid Howells return; 1318d2ddc776SDavid Howells } 1319d2ddc776SDavid Howells 13205a813276SDavid Howells vnode = AFS_FS_I(inode); 13215a813276SDavid Howells set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags); 1322e49c7b2fSDavid Howells if (!op->error) 1323e49c7b2fSDavid Howells afs_cache_permit(vnode, op->key, vnode->cb_break, &vp->scb); 1324e49c7b2fSDavid Howells d_instantiate(op->dentry, inode); 1325d2ddc776SDavid Howells } 1326d2ddc776SDavid Howells 1327e49c7b2fSDavid Howells static void afs_create_success(struct afs_operation *op) 1328b8359153SDavid Howells { 1329e49c7b2fSDavid Howells _enter("op=%08x", op->debug_id); 1330da8d0755SDavid Howells op->ctime = op->file[0].scb.status.mtime_client; 1331e49c7b2fSDavid Howells afs_vnode_commit_status(op, &op->file[0]); 1332e49c7b2fSDavid Howells afs_update_dentry_version(op, &op->file[0], op->dentry); 1333e49c7b2fSDavid Howells afs_vnode_new_inode(op); 1334b8359153SDavid Howells } 1335b8359153SDavid Howells 1336e49c7b2fSDavid Howells static void afs_create_edit_dir(struct afs_operation *op) 13379dd0b82eSDavid Howells { 1338e49c7b2fSDavid Howells struct afs_vnode_param *dvp = &op->file[0]; 1339e49c7b2fSDavid Howells struct afs_vnode_param *vp = &op->file[1]; 1340e49c7b2fSDavid Howells struct afs_vnode *dvnode = dvp->vnode; 1341e49c7b2fSDavid Howells 1342e49c7b2fSDavid Howells _enter("op=%08x", op->debug_id); 1343e49c7b2fSDavid Howells 1344e49c7b2fSDavid Howells down_write(&dvnode->validate_lock); 1345e49c7b2fSDavid Howells if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) && 1346e49c7b2fSDavid Howells dvnode->status.data_version == dvp->dv_before + dvp->dv_delta) 1347e49c7b2fSDavid Howells afs_edit_dir_add(dvnode, &op->dentry->d_name, &vp->fid, 1348e49c7b2fSDavid Howells op->create.reason); 1349e49c7b2fSDavid Howells up_write(&dvnode->validate_lock); 13509dd0b82eSDavid Howells } 13519dd0b82eSDavid Howells 1352e49c7b2fSDavid Howells static void afs_create_put(struct afs_operation *op) 1353e49c7b2fSDavid Howells { 1354e49c7b2fSDavid Howells _enter("op=%08x", op->debug_id); 1355e49c7b2fSDavid Howells 1356e49c7b2fSDavid Howells if (op->error) 1357e49c7b2fSDavid Howells d_drop(op->dentry); 1358e49c7b2fSDavid Howells } 1359e49c7b2fSDavid Howells 1360e49c7b2fSDavid Howells static const struct afs_operation_ops afs_mkdir_operation = { 1361e49c7b2fSDavid Howells .issue_afs_rpc = afs_fs_make_dir, 1362e49c7b2fSDavid Howells .issue_yfs_rpc = yfs_fs_make_dir, 1363e49c7b2fSDavid Howells .success = afs_create_success, 1364728279a5SDavid Howells .aborted = afs_check_for_remote_deletion, 1365e49c7b2fSDavid Howells .edit_dir = afs_create_edit_dir, 1366e49c7b2fSDavid Howells .put = afs_create_put, 1367e49c7b2fSDavid Howells }; 1368e49c7b2fSDavid Howells 13699dd0b82eSDavid Howells /* 1370260a9803SDavid Howells * create a directory on an AFS filesystem 1371260a9803SDavid Howells */ 1372549c7297SChristian Brauner static int afs_mkdir(struct user_namespace *mnt_userns, struct inode *dir, 1373549c7297SChristian Brauner struct dentry *dentry, umode_t mode) 1374260a9803SDavid Howells { 1375e49c7b2fSDavid Howells struct afs_operation *op; 1376d2ddc776SDavid Howells struct afs_vnode *dvnode = AFS_FS_I(dir); 1377260a9803SDavid Howells 13783b6492dfSDavid Howells _enter("{%llx:%llu},{%pd},%ho", 1379a455589fSAl Viro dvnode->fid.vid, dvnode->fid.vnode, dentry, mode); 1380260a9803SDavid Howells 1381e49c7b2fSDavid Howells op = afs_alloc_operation(NULL, dvnode->volume); 1382e49c7b2fSDavid Howells if (IS_ERR(op)) { 1383260a9803SDavid Howells d_drop(dentry); 1384e49c7b2fSDavid Howells return PTR_ERR(op); 1385e49c7b2fSDavid Howells } 1386e49c7b2fSDavid Howells 1387e49c7b2fSDavid Howells afs_op_set_vnode(op, 0, dvnode); 1388e49c7b2fSDavid Howells op->file[0].dv_delta = 1; 138922650f14SDavid Howells op->file[0].modification = true; 1390da8d0755SDavid Howells op->file[0].update_ctime = true; 1391e49c7b2fSDavid Howells op->dentry = dentry; 1392e49c7b2fSDavid Howells op->create.mode = S_IFDIR | mode; 1393e49c7b2fSDavid Howells op->create.reason = afs_edit_dir_for_mkdir; 1394e49c7b2fSDavid Howells op->ops = &afs_mkdir_operation; 1395e49c7b2fSDavid Howells return afs_do_sync_operation(op); 1396260a9803SDavid Howells } 1397260a9803SDavid Howells 1398260a9803SDavid Howells /* 1399d2ddc776SDavid Howells * Remove a subdir from a directory. 1400260a9803SDavid Howells */ 1401d2ddc776SDavid Howells static void afs_dir_remove_subdir(struct dentry *dentry) 1402260a9803SDavid Howells { 14032b0143b5SDavid Howells if (d_really_is_positive(dentry)) { 1404d2ddc776SDavid Howells struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry)); 1405d2ddc776SDavid Howells 1406260a9803SDavid Howells clear_nlink(&vnode->vfs_inode); 1407260a9803SDavid Howells set_bit(AFS_VNODE_DELETED, &vnode->flags); 1408c435ee34SDavid Howells clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags); 140963a4681fSDavid Howells clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags); 1410260a9803SDavid Howells } 1411260a9803SDavid Howells } 1412260a9803SDavid Howells 1413e49c7b2fSDavid Howells static void afs_rmdir_success(struct afs_operation *op) 1414e49c7b2fSDavid Howells { 1415e49c7b2fSDavid Howells _enter("op=%08x", op->debug_id); 1416da8d0755SDavid Howells op->ctime = op->file[0].scb.status.mtime_client; 1417e49c7b2fSDavid Howells afs_vnode_commit_status(op, &op->file[0]); 1418e49c7b2fSDavid Howells afs_update_dentry_version(op, &op->file[0], op->dentry); 1419e49c7b2fSDavid Howells } 1420e49c7b2fSDavid Howells 1421e49c7b2fSDavid Howells static void afs_rmdir_edit_dir(struct afs_operation *op) 1422e49c7b2fSDavid Howells { 1423e49c7b2fSDavid Howells struct afs_vnode_param *dvp = &op->file[0]; 1424e49c7b2fSDavid Howells struct afs_vnode *dvnode = dvp->vnode; 1425e49c7b2fSDavid Howells 1426e49c7b2fSDavid Howells _enter("op=%08x", op->debug_id); 1427e49c7b2fSDavid Howells afs_dir_remove_subdir(op->dentry); 1428e49c7b2fSDavid Howells 1429e49c7b2fSDavid Howells down_write(&dvnode->validate_lock); 1430e49c7b2fSDavid Howells if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) && 1431e49c7b2fSDavid Howells dvnode->status.data_version == dvp->dv_before + dvp->dv_delta) 1432e49c7b2fSDavid Howells afs_edit_dir_remove(dvnode, &op->dentry->d_name, 1433e49c7b2fSDavid Howells afs_edit_dir_for_rmdir); 1434e49c7b2fSDavid Howells up_write(&dvnode->validate_lock); 1435e49c7b2fSDavid Howells } 1436e49c7b2fSDavid Howells 1437e49c7b2fSDavid Howells static void afs_rmdir_put(struct afs_operation *op) 1438e49c7b2fSDavid Howells { 1439e49c7b2fSDavid Howells _enter("op=%08x", op->debug_id); 1440e49c7b2fSDavid Howells if (op->file[1].vnode) 1441e49c7b2fSDavid Howells up_write(&op->file[1].vnode->rmdir_lock); 1442e49c7b2fSDavid Howells } 1443e49c7b2fSDavid Howells 1444e49c7b2fSDavid Howells static const struct afs_operation_ops afs_rmdir_operation = { 1445e49c7b2fSDavid Howells .issue_afs_rpc = afs_fs_remove_dir, 1446e49c7b2fSDavid Howells .issue_yfs_rpc = yfs_fs_remove_dir, 1447e49c7b2fSDavid Howells .success = afs_rmdir_success, 1448728279a5SDavid Howells .aborted = afs_check_for_remote_deletion, 1449e49c7b2fSDavid Howells .edit_dir = afs_rmdir_edit_dir, 1450e49c7b2fSDavid Howells .put = afs_rmdir_put, 1451e49c7b2fSDavid Howells }; 1452e49c7b2fSDavid Howells 1453260a9803SDavid Howells /* 1454d2ddc776SDavid Howells * remove a directory from an AFS filesystem 1455260a9803SDavid Howells */ 1456d2ddc776SDavid Howells static int afs_rmdir(struct inode *dir, struct dentry *dentry) 1457260a9803SDavid Howells { 1458e49c7b2fSDavid Howells struct afs_operation *op; 1459f58db83fSDavid Howells struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode = NULL; 1460260a9803SDavid Howells int ret; 1461260a9803SDavid Howells 14623b6492dfSDavid Howells _enter("{%llx:%llu},{%pd}", 1463a455589fSAl Viro dvnode->fid.vid, dvnode->fid.vnode, dentry); 1464260a9803SDavid Howells 1465e49c7b2fSDavid Howells op = afs_alloc_operation(NULL, dvnode->volume); 1466e49c7b2fSDavid Howells if (IS_ERR(op)) 1467e49c7b2fSDavid Howells return PTR_ERR(op); 1468a58823acSDavid Howells 1469e49c7b2fSDavid Howells afs_op_set_vnode(op, 0, dvnode); 1470e49c7b2fSDavid Howells op->file[0].dv_delta = 1; 147122650f14SDavid Howells op->file[0].modification = true; 1472da8d0755SDavid Howells op->file[0].update_ctime = true; 1473e49c7b2fSDavid Howells 1474e49c7b2fSDavid Howells op->dentry = dentry; 1475e49c7b2fSDavid Howells op->ops = &afs_rmdir_operation; 1476260a9803SDavid Howells 1477f58db83fSDavid Howells /* Try to make sure we have a callback promise on the victim. */ 1478f58db83fSDavid Howells if (d_really_is_positive(dentry)) { 1479f58db83fSDavid Howells vnode = AFS_FS_I(d_inode(dentry)); 1480e49c7b2fSDavid Howells ret = afs_validate(vnode, op->key); 1481f58db83fSDavid Howells if (ret < 0) 1482e49c7b2fSDavid Howells goto error; 1483f58db83fSDavid Howells } 1484f58db83fSDavid Howells 148579ddbfa5SDavid Howells if (vnode) { 148679ddbfa5SDavid Howells ret = down_write_killable(&vnode->rmdir_lock); 148779ddbfa5SDavid Howells if (ret < 0) 1488e49c7b2fSDavid Howells goto error; 1489e49c7b2fSDavid Howells op->file[1].vnode = vnode; 149079ddbfa5SDavid Howells } 149179ddbfa5SDavid Howells 1492e49c7b2fSDavid Howells return afs_do_sync_operation(op); 1493a58823acSDavid Howells 1494d2ddc776SDavid Howells error: 1495e49c7b2fSDavid Howells return afs_put_operation(op); 1496d2ddc776SDavid Howells } 1497260a9803SDavid Howells 1498d2ddc776SDavid Howells /* 1499d2ddc776SDavid Howells * Remove a link to a file or symlink from a directory. 1500d2ddc776SDavid Howells * 1501d2ddc776SDavid Howells * If the file was not deleted due to excess hard links, the fileserver will 1502d2ddc776SDavid Howells * break the callback promise on the file - if it had one - before it returns 1503d2ddc776SDavid Howells * to us, and if it was deleted, it won't 1504d2ddc776SDavid Howells * 1505d2ddc776SDavid Howells * However, if we didn't have a callback promise outstanding, or it was 1506d2ddc776SDavid Howells * outstanding on a different server, then it won't break it either... 1507d2ddc776SDavid Howells */ 1508e49c7b2fSDavid Howells static void afs_dir_remove_link(struct afs_operation *op) 1509d2ddc776SDavid Howells { 1510e49c7b2fSDavid Howells struct afs_vnode *dvnode = op->file[0].vnode; 1511e49c7b2fSDavid Howells struct afs_vnode *vnode = op->file[1].vnode; 1512e49c7b2fSDavid Howells struct dentry *dentry = op->dentry; 1513e49c7b2fSDavid Howells int ret; 1514d2ddc776SDavid Howells 1515e49c7b2fSDavid Howells if (op->error != 0 || 1516e49c7b2fSDavid Howells (op->file[1].scb.have_status && op->file[1].scb.have_error)) 1517e49c7b2fSDavid Howells return; 1518e49c7b2fSDavid Howells if (d_really_is_positive(dentry)) 1519e49c7b2fSDavid Howells return; 1520d2ddc776SDavid Howells 152130062bd1SDavid Howells if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) { 152230062bd1SDavid Howells /* Already done */ 1523a38a7558SDavid Howells } else if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) { 1524a38a7558SDavid Howells write_seqlock(&vnode->cb_lock); 1525440fbc3aSDavid Howells drop_nlink(&vnode->vfs_inode); 1526440fbc3aSDavid Howells if (vnode->vfs_inode.i_nlink == 0) { 1527440fbc3aSDavid Howells set_bit(AFS_VNODE_DELETED, &vnode->flags); 1528051d2525SDavid Howells __afs_break_callback(vnode, afs_cb_break_for_unlink); 1529440fbc3aSDavid Howells } 1530a38a7558SDavid Howells write_sequnlock(&vnode->cb_lock); 1531440fbc3aSDavid Howells } else { 1532051d2525SDavid Howells afs_break_callback(vnode, afs_cb_break_for_unlink); 1533440fbc3aSDavid Howells 1534d2ddc776SDavid Howells if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) 1535e49c7b2fSDavid Howells _debug("AFS_VNODE_DELETED"); 1536d2ddc776SDavid Howells 1537e49c7b2fSDavid Howells ret = afs_validate(vnode, op->key); 1538e49c7b2fSDavid Howells if (ret != -ESTALE) 1539e49c7b2fSDavid Howells op->error = ret; 1540d2ddc776SDavid Howells } 1541d2ddc776SDavid Howells 1542e49c7b2fSDavid Howells _debug("nlink %d [val %d]", vnode->vfs_inode.i_nlink, op->error); 1543d2ddc776SDavid Howells } 1544d2ddc776SDavid Howells 1545e49c7b2fSDavid Howells static void afs_unlink_success(struct afs_operation *op) 1546e49c7b2fSDavid Howells { 1547e49c7b2fSDavid Howells _enter("op=%08x", op->debug_id); 1548da8d0755SDavid Howells op->ctime = op->file[0].scb.status.mtime_client; 1549b6489a49SDavid Howells afs_check_dir_conflict(op, &op->file[0]); 1550e49c7b2fSDavid Howells afs_vnode_commit_status(op, &op->file[0]); 1551e49c7b2fSDavid Howells afs_vnode_commit_status(op, &op->file[1]); 1552e49c7b2fSDavid Howells afs_update_dentry_version(op, &op->file[0], op->dentry); 1553e49c7b2fSDavid Howells afs_dir_remove_link(op); 1554e49c7b2fSDavid Howells } 1555e49c7b2fSDavid Howells 1556e49c7b2fSDavid Howells static void afs_unlink_edit_dir(struct afs_operation *op) 1557e49c7b2fSDavid Howells { 1558e49c7b2fSDavid Howells struct afs_vnode_param *dvp = &op->file[0]; 1559e49c7b2fSDavid Howells struct afs_vnode *dvnode = dvp->vnode; 1560e49c7b2fSDavid Howells 1561e49c7b2fSDavid Howells _enter("op=%08x", op->debug_id); 1562e49c7b2fSDavid Howells down_write(&dvnode->validate_lock); 1563e49c7b2fSDavid Howells if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) && 1564e49c7b2fSDavid Howells dvnode->status.data_version == dvp->dv_before + dvp->dv_delta) 1565e49c7b2fSDavid Howells afs_edit_dir_remove(dvnode, &op->dentry->d_name, 1566e49c7b2fSDavid Howells afs_edit_dir_for_unlink); 1567e49c7b2fSDavid Howells up_write(&dvnode->validate_lock); 1568e49c7b2fSDavid Howells } 1569e49c7b2fSDavid Howells 1570e49c7b2fSDavid Howells static void afs_unlink_put(struct afs_operation *op) 1571e49c7b2fSDavid Howells { 1572e49c7b2fSDavid Howells _enter("op=%08x", op->debug_id); 1573e49c7b2fSDavid Howells if (op->unlink.need_rehash && op->error < 0 && op->error != -ENOENT) 1574e49c7b2fSDavid Howells d_rehash(op->dentry); 1575e49c7b2fSDavid Howells } 1576e49c7b2fSDavid Howells 1577e49c7b2fSDavid Howells static const struct afs_operation_ops afs_unlink_operation = { 1578e49c7b2fSDavid Howells .issue_afs_rpc = afs_fs_remove_file, 1579e49c7b2fSDavid Howells .issue_yfs_rpc = yfs_fs_remove_file, 1580e49c7b2fSDavid Howells .success = afs_unlink_success, 1581728279a5SDavid Howells .aborted = afs_check_for_remote_deletion, 1582e49c7b2fSDavid Howells .edit_dir = afs_unlink_edit_dir, 1583e49c7b2fSDavid Howells .put = afs_unlink_put, 1584e49c7b2fSDavid Howells }; 1585e49c7b2fSDavid Howells 1586d2ddc776SDavid Howells /* 1587d2ddc776SDavid Howells * Remove a file or symlink from an AFS filesystem. 1588d2ddc776SDavid Howells */ 1589d2ddc776SDavid Howells static int afs_unlink(struct inode *dir, struct dentry *dentry) 1590d2ddc776SDavid Howells { 1591e49c7b2fSDavid Howells struct afs_operation *op; 1592fa59f52fSDavid Howells struct afs_vnode *dvnode = AFS_FS_I(dir); 1593fa59f52fSDavid Howells struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry)); 1594d2ddc776SDavid Howells int ret; 1595d2ddc776SDavid Howells 15963b6492dfSDavid Howells _enter("{%llx:%llu},{%pd}", 1597d2ddc776SDavid Howells dvnode->fid.vid, dvnode->fid.vnode, dentry); 1598d2ddc776SDavid Howells 1599d2ddc776SDavid Howells if (dentry->d_name.len >= AFSNAMEMAX) 1600d2ddc776SDavid Howells return -ENAMETOOLONG; 1601d2ddc776SDavid Howells 1602e49c7b2fSDavid Howells op = afs_alloc_operation(NULL, dvnode->volume); 1603e49c7b2fSDavid Howells if (IS_ERR(op)) 1604e49c7b2fSDavid Howells return PTR_ERR(op); 1605a58823acSDavid Howells 1606e49c7b2fSDavid Howells afs_op_set_vnode(op, 0, dvnode); 1607e49c7b2fSDavid Howells op->file[0].dv_delta = 1; 160822650f14SDavid Howells op->file[0].modification = true; 1609da8d0755SDavid Howells op->file[0].update_ctime = true; 1610d2ddc776SDavid Howells 1611d2ddc776SDavid Howells /* Try to make sure we have a callback promise on the victim. */ 1612e49c7b2fSDavid Howells ret = afs_validate(vnode, op->key); 1613e49c7b2fSDavid Howells if (ret < 0) { 1614e49c7b2fSDavid Howells op->error = ret; 1615e49c7b2fSDavid Howells goto error; 1616e49c7b2fSDavid Howells } 1617d2ddc776SDavid Howells 161879ddbfa5SDavid Howells spin_lock(&dentry->d_lock); 1619fa59f52fSDavid Howells if (d_count(dentry) > 1) { 162079ddbfa5SDavid Howells spin_unlock(&dentry->d_lock); 162179ddbfa5SDavid Howells /* Start asynchronous writeout of the inode */ 162279ddbfa5SDavid Howells write_inode_now(d_inode(dentry), 0); 1623e49c7b2fSDavid Howells op->error = afs_sillyrename(dvnode, vnode, dentry, op->key); 1624e49c7b2fSDavid Howells goto error; 162579ddbfa5SDavid Howells } 162679ddbfa5SDavid Howells if (!d_unhashed(dentry)) { 162779ddbfa5SDavid Howells /* Prevent a race with RCU lookup. */ 162879ddbfa5SDavid Howells __d_drop(dentry); 1629e49c7b2fSDavid Howells op->unlink.need_rehash = true; 163079ddbfa5SDavid Howells } 163179ddbfa5SDavid Howells spin_unlock(&dentry->d_lock); 163279ddbfa5SDavid Howells 1633e49c7b2fSDavid Howells op->file[1].vnode = vnode; 1634da8d0755SDavid Howells op->file[1].update_ctime = true; 1635b6489a49SDavid Howells op->file[1].op_unlinked = true; 1636e49c7b2fSDavid Howells op->dentry = dentry; 1637e49c7b2fSDavid Howells op->ops = &afs_unlink_operation; 1638b6489a49SDavid Howells afs_begin_vnode_operation(op); 1639b6489a49SDavid Howells afs_wait_for_operation(op); 1640b6489a49SDavid Howells 1641b6489a49SDavid Howells /* If there was a conflict with a third party, check the status of the 1642b6489a49SDavid Howells * unlinked vnode. 1643b6489a49SDavid Howells */ 1644b6489a49SDavid Howells if (op->error == 0 && (op->flags & AFS_OPERATION_DIR_CONFLICT)) { 1645b6489a49SDavid Howells op->file[1].update_ctime = false; 1646b6489a49SDavid Howells op->fetch_status.which = 1; 1647b6489a49SDavid Howells op->ops = &afs_fetch_status_operation; 1648b6489a49SDavid Howells afs_begin_vnode_operation(op); 1649b6489a49SDavid Howells afs_wait_for_operation(op); 1650b6489a49SDavid Howells } 1651b6489a49SDavid Howells 1652b6489a49SDavid Howells return afs_put_operation(op); 1653a58823acSDavid Howells 1654260a9803SDavid Howells error: 1655e49c7b2fSDavid Howells return afs_put_operation(op); 1656260a9803SDavid Howells } 1657260a9803SDavid Howells 1658e49c7b2fSDavid Howells static const struct afs_operation_ops afs_create_operation = { 1659e49c7b2fSDavid Howells .issue_afs_rpc = afs_fs_create_file, 1660e49c7b2fSDavid Howells .issue_yfs_rpc = yfs_fs_create_file, 1661e49c7b2fSDavid Howells .success = afs_create_success, 1662728279a5SDavid Howells .aborted = afs_check_for_remote_deletion, 1663e49c7b2fSDavid Howells .edit_dir = afs_create_edit_dir, 1664e49c7b2fSDavid Howells .put = afs_create_put, 1665e49c7b2fSDavid Howells }; 1666e49c7b2fSDavid Howells 1667260a9803SDavid Howells /* 1668260a9803SDavid Howells * create a regular file on an AFS filesystem 1669260a9803SDavid Howells */ 1670549c7297SChristian Brauner static int afs_create(struct user_namespace *mnt_userns, struct inode *dir, 1671549c7297SChristian Brauner struct dentry *dentry, umode_t mode, bool excl) 1672260a9803SDavid Howells { 1673e49c7b2fSDavid Howells struct afs_operation *op; 167443dd388bSColin Ian King struct afs_vnode *dvnode = AFS_FS_I(dir); 1675e49c7b2fSDavid Howells int ret = -ENAMETOOLONG; 1676260a9803SDavid Howells 1677e49c7b2fSDavid Howells _enter("{%llx:%llu},{%pd},%ho", 1678a455589fSAl Viro dvnode->fid.vid, dvnode->fid.vnode, dentry, mode); 1679260a9803SDavid Howells 1680d2ddc776SDavid Howells if (dentry->d_name.len >= AFSNAMEMAX) 1681d2ddc776SDavid Howells goto error; 1682d2ddc776SDavid Howells 1683e49c7b2fSDavid Howells op = afs_alloc_operation(NULL, dvnode->volume); 1684e49c7b2fSDavid Howells if (IS_ERR(op)) { 1685e49c7b2fSDavid Howells ret = PTR_ERR(op); 1686260a9803SDavid Howells goto error; 1687260a9803SDavid Howells } 1688260a9803SDavid Howells 1689e49c7b2fSDavid Howells afs_op_set_vnode(op, 0, dvnode); 1690e49c7b2fSDavid Howells op->file[0].dv_delta = 1; 169122650f14SDavid Howells op->file[0].modification = true; 1692da8d0755SDavid Howells op->file[0].update_ctime = true; 1693a58823acSDavid Howells 1694e49c7b2fSDavid Howells op->dentry = dentry; 1695e49c7b2fSDavid Howells op->create.mode = S_IFREG | mode; 1696e49c7b2fSDavid Howells op->create.reason = afs_edit_dir_for_create; 1697e49c7b2fSDavid Howells op->ops = &afs_create_operation; 1698e49c7b2fSDavid Howells return afs_do_sync_operation(op); 1699a58823acSDavid Howells 1700260a9803SDavid Howells error: 1701260a9803SDavid Howells d_drop(dentry); 1702260a9803SDavid Howells _leave(" = %d", ret); 1703260a9803SDavid Howells return ret; 1704260a9803SDavid Howells } 1705260a9803SDavid Howells 1706e49c7b2fSDavid Howells static void afs_link_success(struct afs_operation *op) 1707e49c7b2fSDavid Howells { 1708e49c7b2fSDavid Howells struct afs_vnode_param *dvp = &op->file[0]; 1709e49c7b2fSDavid Howells struct afs_vnode_param *vp = &op->file[1]; 1710e49c7b2fSDavid Howells 1711e49c7b2fSDavid Howells _enter("op=%08x", op->debug_id); 1712da8d0755SDavid Howells op->ctime = dvp->scb.status.mtime_client; 1713e49c7b2fSDavid Howells afs_vnode_commit_status(op, dvp); 1714e49c7b2fSDavid Howells afs_vnode_commit_status(op, vp); 1715e49c7b2fSDavid Howells afs_update_dentry_version(op, dvp, op->dentry); 1716e49c7b2fSDavid Howells if (op->dentry_2->d_parent == op->dentry->d_parent) 1717e49c7b2fSDavid Howells afs_update_dentry_version(op, dvp, op->dentry_2); 1718e49c7b2fSDavid Howells ihold(&vp->vnode->vfs_inode); 1719e49c7b2fSDavid Howells d_instantiate(op->dentry, &vp->vnode->vfs_inode); 1720e49c7b2fSDavid Howells } 1721e49c7b2fSDavid Howells 1722e49c7b2fSDavid Howells static void afs_link_put(struct afs_operation *op) 1723e49c7b2fSDavid Howells { 1724e49c7b2fSDavid Howells _enter("op=%08x", op->debug_id); 1725e49c7b2fSDavid Howells if (op->error) 1726e49c7b2fSDavid Howells d_drop(op->dentry); 1727e49c7b2fSDavid Howells } 1728e49c7b2fSDavid Howells 1729e49c7b2fSDavid Howells static const struct afs_operation_ops afs_link_operation = { 1730e49c7b2fSDavid Howells .issue_afs_rpc = afs_fs_link, 1731e49c7b2fSDavid Howells .issue_yfs_rpc = yfs_fs_link, 1732e49c7b2fSDavid Howells .success = afs_link_success, 1733728279a5SDavid Howells .aborted = afs_check_for_remote_deletion, 1734e49c7b2fSDavid Howells .edit_dir = afs_create_edit_dir, 1735e49c7b2fSDavid Howells .put = afs_link_put, 1736e49c7b2fSDavid Howells }; 1737e49c7b2fSDavid Howells 1738260a9803SDavid Howells /* 1739260a9803SDavid Howells * create a hard link between files in an AFS filesystem 1740260a9803SDavid Howells */ 1741260a9803SDavid Howells static int afs_link(struct dentry *from, struct inode *dir, 1742260a9803SDavid Howells struct dentry *dentry) 1743260a9803SDavid Howells { 1744e49c7b2fSDavid Howells struct afs_operation *op; 1745a58823acSDavid Howells struct afs_vnode *dvnode = AFS_FS_I(dir); 1746a58823acSDavid Howells struct afs_vnode *vnode = AFS_FS_I(d_inode(from)); 1747e49c7b2fSDavid Howells int ret = -ENAMETOOLONG; 1748260a9803SDavid Howells 17493b6492dfSDavid Howells _enter("{%llx:%llu},{%llx:%llu},{%pd}", 1750260a9803SDavid Howells vnode->fid.vid, vnode->fid.vnode, 1751260a9803SDavid Howells dvnode->fid.vid, dvnode->fid.vnode, 1752a455589fSAl Viro dentry); 1753260a9803SDavid Howells 1754d2ddc776SDavid Howells if (dentry->d_name.len >= AFSNAMEMAX) 1755d2ddc776SDavid Howells goto error; 1756d2ddc776SDavid Howells 1757e49c7b2fSDavid Howells op = afs_alloc_operation(NULL, dvnode->volume); 1758e49c7b2fSDavid Howells if (IS_ERR(op)) { 1759e49c7b2fSDavid Howells ret = PTR_ERR(op); 1760a58823acSDavid Howells goto error; 1761260a9803SDavid Howells } 1762260a9803SDavid Howells 17633978d816SDavid Howells ret = afs_validate(vnode, op->key); 17643978d816SDavid Howells if (ret < 0) 17653978d816SDavid Howells goto error_op; 17663978d816SDavid Howells 1767e49c7b2fSDavid Howells afs_op_set_vnode(op, 0, dvnode); 1768e49c7b2fSDavid Howells afs_op_set_vnode(op, 1, vnode); 1769e49c7b2fSDavid Howells op->file[0].dv_delta = 1; 177022650f14SDavid Howells op->file[0].modification = true; 1771da8d0755SDavid Howells op->file[0].update_ctime = true; 1772da8d0755SDavid Howells op->file[1].update_ctime = true; 1773a58823acSDavid Howells 1774e49c7b2fSDavid Howells op->dentry = dentry; 1775e49c7b2fSDavid Howells op->dentry_2 = from; 1776e49c7b2fSDavid Howells op->ops = &afs_link_operation; 1777e49c7b2fSDavid Howells op->create.reason = afs_edit_dir_for_link; 1778e49c7b2fSDavid Howells return afs_do_sync_operation(op); 1779260a9803SDavid Howells 17803978d816SDavid Howells error_op: 17813978d816SDavid Howells afs_put_operation(op); 1782260a9803SDavid Howells error: 1783260a9803SDavid Howells d_drop(dentry); 1784260a9803SDavid Howells _leave(" = %d", ret); 1785260a9803SDavid Howells return ret; 1786260a9803SDavid Howells } 1787260a9803SDavid Howells 1788e49c7b2fSDavid Howells static const struct afs_operation_ops afs_symlink_operation = { 1789e49c7b2fSDavid Howells .issue_afs_rpc = afs_fs_symlink, 1790e49c7b2fSDavid Howells .issue_yfs_rpc = yfs_fs_symlink, 1791e49c7b2fSDavid Howells .success = afs_create_success, 1792728279a5SDavid Howells .aborted = afs_check_for_remote_deletion, 1793e49c7b2fSDavid Howells .edit_dir = afs_create_edit_dir, 1794e49c7b2fSDavid Howells .put = afs_create_put, 1795e49c7b2fSDavid Howells }; 1796e49c7b2fSDavid Howells 1797260a9803SDavid Howells /* 1798260a9803SDavid Howells * create a symlink in an AFS filesystem 1799260a9803SDavid Howells */ 1800549c7297SChristian Brauner static int afs_symlink(struct user_namespace *mnt_userns, struct inode *dir, 1801549c7297SChristian Brauner struct dentry *dentry, const char *content) 1802260a9803SDavid Howells { 1803e49c7b2fSDavid Howells struct afs_operation *op; 1804d2ddc776SDavid Howells struct afs_vnode *dvnode = AFS_FS_I(dir); 1805260a9803SDavid Howells int ret; 1806260a9803SDavid Howells 18073b6492dfSDavid Howells _enter("{%llx:%llu},{%pd},%s", 1808a455589fSAl Viro dvnode->fid.vid, dvnode->fid.vnode, dentry, 1809260a9803SDavid Howells content); 1810260a9803SDavid Howells 1811d2ddc776SDavid Howells ret = -ENAMETOOLONG; 1812d2ddc776SDavid Howells if (dentry->d_name.len >= AFSNAMEMAX) 1813d2ddc776SDavid Howells goto error; 1814d2ddc776SDavid Howells 1815260a9803SDavid Howells ret = -EINVAL; 181645222b9eSDavid Howells if (strlen(content) >= AFSPATHMAX) 1817260a9803SDavid Howells goto error; 1818260a9803SDavid Howells 1819e49c7b2fSDavid Howells op = afs_alloc_operation(NULL, dvnode->volume); 1820e49c7b2fSDavid Howells if (IS_ERR(op)) { 1821e49c7b2fSDavid Howells ret = PTR_ERR(op); 1822a58823acSDavid Howells goto error; 1823260a9803SDavid Howells } 1824260a9803SDavid Howells 1825e49c7b2fSDavid Howells afs_op_set_vnode(op, 0, dvnode); 1826e49c7b2fSDavid Howells op->file[0].dv_delta = 1; 1827a58823acSDavid Howells 1828e49c7b2fSDavid Howells op->dentry = dentry; 1829e49c7b2fSDavid Howells op->ops = &afs_symlink_operation; 1830e49c7b2fSDavid Howells op->create.reason = afs_edit_dir_for_symlink; 1831e49c7b2fSDavid Howells op->create.symlink = content; 1832e49c7b2fSDavid Howells return afs_do_sync_operation(op); 1833d2ddc776SDavid Howells 1834260a9803SDavid Howells error: 1835260a9803SDavid Howells d_drop(dentry); 1836260a9803SDavid Howells _leave(" = %d", ret); 1837260a9803SDavid Howells return ret; 1838260a9803SDavid Howells } 1839260a9803SDavid Howells 1840e49c7b2fSDavid Howells static void afs_rename_success(struct afs_operation *op) 1841260a9803SDavid Howells { 1842e49c7b2fSDavid Howells _enter("op=%08x", op->debug_id); 1843e49c7b2fSDavid Howells 1844da8d0755SDavid Howells op->ctime = op->file[0].scb.status.mtime_client; 1845b6489a49SDavid Howells afs_check_dir_conflict(op, &op->file[1]); 1846e49c7b2fSDavid Howells afs_vnode_commit_status(op, &op->file[0]); 1847da8d0755SDavid Howells if (op->file[1].vnode != op->file[0].vnode) { 1848da8d0755SDavid Howells op->ctime = op->file[1].scb.status.mtime_client; 1849e49c7b2fSDavid Howells afs_vnode_commit_status(op, &op->file[1]); 1850e49c7b2fSDavid Howells } 1851da8d0755SDavid Howells } 1852e49c7b2fSDavid Howells 1853e49c7b2fSDavid Howells static void afs_rename_edit_dir(struct afs_operation *op) 1854e49c7b2fSDavid Howells { 1855e49c7b2fSDavid Howells struct afs_vnode_param *orig_dvp = &op->file[0]; 1856e49c7b2fSDavid Howells struct afs_vnode_param *new_dvp = &op->file[1]; 1857e49c7b2fSDavid Howells struct afs_vnode *orig_dvnode = orig_dvp->vnode; 1858e49c7b2fSDavid Howells struct afs_vnode *new_dvnode = new_dvp->vnode; 1859e49c7b2fSDavid Howells struct afs_vnode *vnode = AFS_FS_I(d_inode(op->dentry)); 1860e49c7b2fSDavid Howells struct dentry *old_dentry = op->dentry; 1861e49c7b2fSDavid Howells struct dentry *new_dentry = op->dentry_2; 186279ddbfa5SDavid Howells struct inode *new_inode; 1863260a9803SDavid Howells 1864e49c7b2fSDavid Howells _enter("op=%08x", op->debug_id); 18651cd66c93SMiklos Szeredi 1866e49c7b2fSDavid Howells if (op->rename.rehash) { 1867e49c7b2fSDavid Howells d_rehash(op->rename.rehash); 1868e49c7b2fSDavid Howells op->rename.rehash = NULL; 1869260a9803SDavid Howells } 1870260a9803SDavid Howells 18712105c282SDavid Howells down_write(&orig_dvnode->validate_lock); 18722105c282SDavid Howells if (test_bit(AFS_VNODE_DIR_VALID, &orig_dvnode->flags) && 1873e49c7b2fSDavid Howells orig_dvnode->status.data_version == orig_dvp->dv_before + orig_dvp->dv_delta) 187463a4681fSDavid Howells afs_edit_dir_remove(orig_dvnode, &old_dentry->d_name, 187579ddbfa5SDavid Howells afs_edit_dir_for_rename_0); 187663a4681fSDavid Howells 1877e49c7b2fSDavid Howells if (new_dvnode != orig_dvnode) { 1878e49c7b2fSDavid Howells up_write(&orig_dvnode->validate_lock); 18792105c282SDavid Howells down_write(&new_dvnode->validate_lock); 18802105c282SDavid Howells } 1881e49c7b2fSDavid Howells 18822105c282SDavid Howells if (test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags) && 1883e49c7b2fSDavid Howells new_dvnode->status.data_version == new_dvp->dv_before + new_dvp->dv_delta) { 1884e49c7b2fSDavid Howells if (!op->rename.new_negative) 188563a4681fSDavid Howells afs_edit_dir_remove(new_dvnode, &new_dentry->d_name, 188679ddbfa5SDavid Howells afs_edit_dir_for_rename_1); 188763a4681fSDavid Howells 188863a4681fSDavid Howells afs_edit_dir_add(new_dvnode, &new_dentry->d_name, 188979ddbfa5SDavid Howells &vnode->fid, afs_edit_dir_for_rename_2); 18902105c282SDavid Howells } 189179ddbfa5SDavid Howells 189279ddbfa5SDavid Howells new_inode = d_inode(new_dentry); 189379ddbfa5SDavid Howells if (new_inode) { 189479ddbfa5SDavid Howells spin_lock(&new_inode->i_lock); 1895f610a5a2SDavid Howells if (S_ISDIR(new_inode->i_mode)) 1896f610a5a2SDavid Howells clear_nlink(new_inode); 1897f610a5a2SDavid Howells else if (new_inode->i_nlink > 0) 189879ddbfa5SDavid Howells drop_nlink(new_inode); 189979ddbfa5SDavid Howells spin_unlock(&new_inode->i_lock); 190079ddbfa5SDavid Howells } 19019dd0b82eSDavid Howells 19029dd0b82eSDavid Howells /* Now we can update d_fsdata on the dentries to reflect their 19039dd0b82eSDavid Howells * new parent's data_version. 19049dd0b82eSDavid Howells * 19059dd0b82eSDavid Howells * Note that if we ever implement RENAME_EXCHANGE, we'll have 19069dd0b82eSDavid Howells * to update both dentries with opposing dir versions. 19079dd0b82eSDavid Howells */ 1908e49c7b2fSDavid Howells afs_update_dentry_version(op, new_dvp, op->dentry); 1909e49c7b2fSDavid Howells afs_update_dentry_version(op, new_dvp, op->dentry_2); 1910e49c7b2fSDavid Howells 191179ddbfa5SDavid Howells d_move(old_dentry, new_dentry); 1912e49c7b2fSDavid Howells 19132105c282SDavid Howells up_write(&new_dvnode->validate_lock); 191463a4681fSDavid Howells } 191563a4681fSDavid Howells 1916e49c7b2fSDavid Howells static void afs_rename_put(struct afs_operation *op) 1917e49c7b2fSDavid Howells { 1918e49c7b2fSDavid Howells _enter("op=%08x", op->debug_id); 1919e49c7b2fSDavid Howells if (op->rename.rehash) 1920e49c7b2fSDavid Howells d_rehash(op->rename.rehash); 1921e49c7b2fSDavid Howells dput(op->rename.tmp); 1922e49c7b2fSDavid Howells if (op->error) 1923e49c7b2fSDavid Howells d_rehash(op->dentry); 1924e49c7b2fSDavid Howells } 1925e49c7b2fSDavid Howells 1926e49c7b2fSDavid Howells static const struct afs_operation_ops afs_rename_operation = { 1927e49c7b2fSDavid Howells .issue_afs_rpc = afs_fs_rename, 1928e49c7b2fSDavid Howells .issue_yfs_rpc = yfs_fs_rename, 1929e49c7b2fSDavid Howells .success = afs_rename_success, 1930e49c7b2fSDavid Howells .edit_dir = afs_rename_edit_dir, 1931e49c7b2fSDavid Howells .put = afs_rename_put, 1932e49c7b2fSDavid Howells }; 1933e49c7b2fSDavid Howells 1934e49c7b2fSDavid Howells /* 1935e49c7b2fSDavid Howells * rename a file in an AFS filesystem and/or move it between directories 1936e49c7b2fSDavid Howells */ 1937549c7297SChristian Brauner static int afs_rename(struct user_namespace *mnt_userns, struct inode *old_dir, 1938549c7297SChristian Brauner struct dentry *old_dentry, struct inode *new_dir, 1939549c7297SChristian Brauner struct dentry *new_dentry, unsigned int flags) 1940e49c7b2fSDavid Howells { 1941e49c7b2fSDavid Howells struct afs_operation *op; 1942e49c7b2fSDavid Howells struct afs_vnode *orig_dvnode, *new_dvnode, *vnode; 1943e49c7b2fSDavid Howells int ret; 1944e49c7b2fSDavid Howells 1945e49c7b2fSDavid Howells if (flags) 1946e49c7b2fSDavid Howells return -EINVAL; 1947e49c7b2fSDavid Howells 1948e49c7b2fSDavid Howells /* Don't allow silly-rename files be moved around. */ 1949e49c7b2fSDavid Howells if (old_dentry->d_flags & DCACHE_NFSFS_RENAMED) 1950e49c7b2fSDavid Howells return -EINVAL; 1951e49c7b2fSDavid Howells 1952e49c7b2fSDavid Howells vnode = AFS_FS_I(d_inode(old_dentry)); 1953e49c7b2fSDavid Howells orig_dvnode = AFS_FS_I(old_dir); 1954e49c7b2fSDavid Howells new_dvnode = AFS_FS_I(new_dir); 1955e49c7b2fSDavid Howells 1956e49c7b2fSDavid Howells _enter("{%llx:%llu},{%llx:%llu},{%llx:%llu},{%pd}", 1957e49c7b2fSDavid Howells orig_dvnode->fid.vid, orig_dvnode->fid.vnode, 1958e49c7b2fSDavid Howells vnode->fid.vid, vnode->fid.vnode, 1959e49c7b2fSDavid Howells new_dvnode->fid.vid, new_dvnode->fid.vnode, 1960e49c7b2fSDavid Howells new_dentry); 1961e49c7b2fSDavid Howells 1962e49c7b2fSDavid Howells op = afs_alloc_operation(NULL, orig_dvnode->volume); 1963e49c7b2fSDavid Howells if (IS_ERR(op)) 1964e49c7b2fSDavid Howells return PTR_ERR(op); 1965e49c7b2fSDavid Howells 19663978d816SDavid Howells ret = afs_validate(vnode, op->key); 19673978d816SDavid Howells op->error = ret; 19683978d816SDavid Howells if (ret < 0) 19693978d816SDavid Howells goto error; 19703978d816SDavid Howells 1971e49c7b2fSDavid Howells afs_op_set_vnode(op, 0, orig_dvnode); 1972e49c7b2fSDavid Howells afs_op_set_vnode(op, 1, new_dvnode); /* May be same as orig_dvnode */ 1973e49c7b2fSDavid Howells op->file[0].dv_delta = 1; 1974e49c7b2fSDavid Howells op->file[1].dv_delta = 1; 197522650f14SDavid Howells op->file[0].modification = true; 197622650f14SDavid Howells op->file[1].modification = true; 1977da8d0755SDavid Howells op->file[0].update_ctime = true; 1978da8d0755SDavid Howells op->file[1].update_ctime = true; 1979e49c7b2fSDavid Howells 1980e49c7b2fSDavid Howells op->dentry = old_dentry; 1981e49c7b2fSDavid Howells op->dentry_2 = new_dentry; 1982e49c7b2fSDavid Howells op->rename.new_negative = d_is_negative(new_dentry); 1983e49c7b2fSDavid Howells op->ops = &afs_rename_operation; 1984e49c7b2fSDavid Howells 1985e49c7b2fSDavid Howells /* For non-directories, check whether the target is busy and if so, 1986e49c7b2fSDavid Howells * make a copy of the dentry and then do a silly-rename. If the 1987e49c7b2fSDavid Howells * silly-rename succeeds, the copied dentry is hashed and becomes the 1988e49c7b2fSDavid Howells * new target. 1989e49c7b2fSDavid Howells */ 1990e49c7b2fSDavid Howells if (d_is_positive(new_dentry) && !d_is_dir(new_dentry)) { 1991e49c7b2fSDavid Howells /* To prevent any new references to the target during the 1992e49c7b2fSDavid Howells * rename, we unhash the dentry in advance. 1993e49c7b2fSDavid Howells */ 1994e49c7b2fSDavid Howells if (!d_unhashed(new_dentry)) { 1995e49c7b2fSDavid Howells d_drop(new_dentry); 1996e49c7b2fSDavid Howells op->rename.rehash = new_dentry; 1997e49c7b2fSDavid Howells } 1998e49c7b2fSDavid Howells 1999e49c7b2fSDavid Howells if (d_count(new_dentry) > 2) { 2000e49c7b2fSDavid Howells /* copy the target dentry's name */ 2001e49c7b2fSDavid Howells op->rename.tmp = d_alloc(new_dentry->d_parent, 2002e49c7b2fSDavid Howells &new_dentry->d_name); 2003b4280812SJiapeng Chong if (!op->rename.tmp) { 2004b4280812SJiapeng Chong op->error = -ENOMEM; 2005e49c7b2fSDavid Howells goto error; 2006b4280812SJiapeng Chong } 2007e49c7b2fSDavid Howells 2008e49c7b2fSDavid Howells ret = afs_sillyrename(new_dvnode, 2009e49c7b2fSDavid Howells AFS_FS_I(d_inode(new_dentry)), 2010e49c7b2fSDavid Howells new_dentry, op->key); 2011b4280812SJiapeng Chong if (ret) { 2012b4280812SJiapeng Chong op->error = ret; 2013e49c7b2fSDavid Howells goto error; 2014b4280812SJiapeng Chong } 2015e49c7b2fSDavid Howells 2016e49c7b2fSDavid Howells op->dentry_2 = op->rename.tmp; 2017e49c7b2fSDavid Howells op->rename.rehash = NULL; 2018e49c7b2fSDavid Howells op->rename.new_negative = true; 2019e49c7b2fSDavid Howells } 2020e49c7b2fSDavid Howells } 2021e49c7b2fSDavid Howells 2022e49c7b2fSDavid Howells /* This bit is potentially nasty as there's a potential race with 2023e49c7b2fSDavid Howells * afs_d_revalidate{,_rcu}(). We have to change d_fsdata on the dentry 2024e49c7b2fSDavid Howells * to reflect it's new parent's new data_version after the op, but 2025e49c7b2fSDavid Howells * d_revalidate may see old_dentry between the op having taken place 2026e49c7b2fSDavid Howells * and the version being updated. 2027e49c7b2fSDavid Howells * 2028e49c7b2fSDavid Howells * So drop the old_dentry for now to make other threads go through 2029e49c7b2fSDavid Howells * lookup instead - which we hold a lock against. 2030e49c7b2fSDavid Howells */ 2031e49c7b2fSDavid Howells d_drop(old_dentry); 2032e49c7b2fSDavid Howells 2033e49c7b2fSDavid Howells return afs_do_sync_operation(op); 2034e49c7b2fSDavid Howells 2035260a9803SDavid Howells error: 2036e49c7b2fSDavid Howells return afs_put_operation(op); 2037260a9803SDavid Howells } 2038f3ddee8dSDavid Howells 2039f3ddee8dSDavid Howells /* 2040f3ddee8dSDavid Howells * Release a directory page and clean up its private state if it's not busy 2041f3ddee8dSDavid Howells * - return true if the page can now be released, false if not 2042f3ddee8dSDavid Howells */ 2043f3ddee8dSDavid Howells static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags) 2044f3ddee8dSDavid Howells { 2045f3ddee8dSDavid Howells struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host); 2046f3ddee8dSDavid Howells 20473b6492dfSDavid Howells _enter("{{%llx:%llu}[%lu]}", dvnode->fid.vid, dvnode->fid.vnode, page->index); 2048f3ddee8dSDavid Howells 2049fa04a40bSDavid Howells detach_page_private(page); 2050f3ddee8dSDavid Howells 2051f3ddee8dSDavid Howells /* The directory will need reloading. */ 2052f3ddee8dSDavid Howells if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) 2053f3ddee8dSDavid Howells afs_stat_v(dvnode, n_relpg); 2054f3ddee8dSDavid Howells return 1; 2055f3ddee8dSDavid Howells } 2056f3ddee8dSDavid Howells 2057f3ddee8dSDavid Howells /* 2058f3ddee8dSDavid Howells * invalidate part or all of a page 2059f3ddee8dSDavid Howells * - release a page and clean up its private data if offset is 0 (indicating 2060f3ddee8dSDavid Howells * the entire page) 2061f3ddee8dSDavid Howells */ 2062f3ddee8dSDavid Howells static void afs_dir_invalidatepage(struct page *page, unsigned int offset, 2063f3ddee8dSDavid Howells unsigned int length) 2064f3ddee8dSDavid Howells { 2065f3ddee8dSDavid Howells struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host); 2066f3ddee8dSDavid Howells 2067f3ddee8dSDavid Howells _enter("{%lu},%u,%u", page->index, offset, length); 2068f3ddee8dSDavid Howells 2069f3ddee8dSDavid Howells BUG_ON(!PageLocked(page)); 2070f3ddee8dSDavid Howells 2071f3ddee8dSDavid Howells /* The directory will need reloading. */ 2072f3ddee8dSDavid Howells if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) 2073f3ddee8dSDavid Howells afs_stat_v(dvnode, n_inval); 2074f3ddee8dSDavid Howells 2075f3ddee8dSDavid Howells /* we clean up only if the entire page is being invalidated */ 2076e87b03f5SDavid Howells if (offset == 0 && length == thp_size(page)) 2077fa04a40bSDavid Howells detach_page_private(page); 2078f3ddee8dSDavid Howells } 2079