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 /* 106*c4508464SDavid Howells * Drop the refs that we're holding on the pages we were reading into. We've 107*c4508464SDavid Howells * got refs on the first nr_pages pages. 108*c4508464SDavid Howells */ 109*c4508464SDavid Howells static void afs_dir_read_cleanup(struct afs_read *req) 110*c4508464SDavid Howells { 111*c4508464SDavid Howells struct address_space *mapping = req->vnode->vfs_inode.i_mapping; 112*c4508464SDavid Howells struct page *page; 113*c4508464SDavid Howells pgoff_t last = req->nr_pages - 1; 114*c4508464SDavid Howells 115*c4508464SDavid Howells XA_STATE(xas, &mapping->i_pages, 0); 116*c4508464SDavid Howells 117*c4508464SDavid Howells if (unlikely(!req->nr_pages)) 118*c4508464SDavid Howells return; 119*c4508464SDavid Howells 120*c4508464SDavid Howells rcu_read_lock(); 121*c4508464SDavid Howells xas_for_each(&xas, page, last) { 122*c4508464SDavid Howells if (xas_retry(&xas, page)) 123*c4508464SDavid Howells continue; 124*c4508464SDavid Howells BUG_ON(xa_is_value(page)); 125*c4508464SDavid Howells BUG_ON(PageCompound(page)); 126*c4508464SDavid Howells ASSERTCMP(page->mapping, ==, mapping); 127*c4508464SDavid Howells 128*c4508464SDavid Howells put_page(page); 129*c4508464SDavid Howells } 130*c4508464SDavid Howells 131*c4508464SDavid Howells rcu_read_unlock(); 132*c4508464SDavid Howells } 133*c4508464SDavid Howells 134*c4508464SDavid 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 */ 159*c4508464SDavid 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 178*c4508464SDavid 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 /* 189*c4508464SDavid Howells * Dump the contents of a directory. 190445b1028SDavid Howells */ 191*c4508464SDavid Howells static void afs_dir_dump(struct afs_vnode *dvnode, struct afs_read *req) 192445b1028SDavid Howells { 193445b1028SDavid Howells struct afs_xdr_dir_page *dbuf; 194*c4508464SDavid Howells struct address_space *mapping = dvnode->vfs_inode.i_mapping; 195*c4508464SDavid Howells struct page *page; 196*c4508464SDavid Howells unsigned int i, qty = PAGE_SIZE / sizeof(union afs_xdr_dir_block); 197*c4508464SDavid Howells pgoff_t last = req->nr_pages - 1; 198445b1028SDavid Howells 199*c4508464SDavid Howells XA_STATE(xas, &mapping->i_pages, 0); 200445b1028SDavid Howells 201*c4508464SDavid Howells pr_warn("DIR %llx:%llx f=%llx l=%llx al=%llx\n", 202445b1028SDavid Howells dvnode->fid.vid, dvnode->fid.vnode, 203*c4508464SDavid Howells req->file_size, req->len, req->actual_len); 204*c4508464SDavid Howells pr_warn("DIR %llx %x %zx %zx\n", 205*c4508464SDavid Howells req->pos, req->nr_pages, 206*c4508464SDavid Howells req->iter->iov_offset, iov_iter_count(req->iter)); 207445b1028SDavid Howells 208*c4508464SDavid Howells xas_for_each(&xas, page, last) { 209*c4508464SDavid Howells if (xas_retry(&xas, page)) 210*c4508464SDavid Howells continue; 211445b1028SDavid Howells 212*c4508464SDavid Howells BUG_ON(PageCompound(page)); 213*c4508464SDavid Howells BUG_ON(page->mapping != mapping); 214*c4508464SDavid Howells 215*c4508464SDavid Howells dbuf = kmap_atomic(page); 216*c4508464SDavid Howells for (i = 0; i < qty; i++) { 217*c4508464SDavid Howells union afs_xdr_dir_block *block = &dbuf->blocks[i]; 218*c4508464SDavid Howells 219*c4508464SDavid Howells pr_warn("[%02lx] %32phN\n", page->index * qty + i, block); 220445b1028SDavid Howells } 221*c4508464SDavid Howells kunmap_atomic(dbuf); 222445b1028SDavid Howells } 223*c4508464SDavid Howells } 224*c4508464SDavid Howells 225*c4508464SDavid Howells /* 226*c4508464SDavid Howells * Check all the pages in a directory. All the pages are held pinned. 227*c4508464SDavid Howells */ 228*c4508464SDavid Howells static int afs_dir_check(struct afs_vnode *dvnode, struct afs_read *req) 229*c4508464SDavid Howells { 230*c4508464SDavid Howells struct address_space *mapping = dvnode->vfs_inode.i_mapping; 231*c4508464SDavid Howells struct page *page; 232*c4508464SDavid Howells pgoff_t last = req->nr_pages - 1; 233*c4508464SDavid Howells int ret = 0; 234*c4508464SDavid Howells 235*c4508464SDavid Howells XA_STATE(xas, &mapping->i_pages, 0); 236*c4508464SDavid Howells 237*c4508464SDavid Howells if (unlikely(!req->nr_pages)) 238*c4508464SDavid Howells return 0; 239*c4508464SDavid Howells 240*c4508464SDavid Howells rcu_read_lock(); 241*c4508464SDavid Howells xas_for_each(&xas, page, last) { 242*c4508464SDavid Howells if (xas_retry(&xas, page)) 243*c4508464SDavid Howells continue; 244*c4508464SDavid Howells 245*c4508464SDavid Howells BUG_ON(PageCompound(page)); 246*c4508464SDavid Howells BUG_ON(page->mapping != mapping); 247*c4508464SDavid Howells 248*c4508464SDavid Howells if (!afs_dir_check_page(dvnode, page, req->file_size)) { 249*c4508464SDavid Howells afs_dir_dump(dvnode, req); 250*c4508464SDavid Howells ret = -EIO; 251*c4508464SDavid Howells break; 252*c4508464SDavid Howells } 253*c4508464SDavid Howells } 254*c4508464SDavid Howells 255*c4508464SDavid Howells rcu_read_unlock(); 256*c4508464SDavid 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; 285*c4508464SDavid Howells int nr_pages, i, n; 286*c4508464SDavid Howells int ret; 287f3ddee8dSDavid Howells 288*c4508464SDavid Howells _enter(""); 289f3ddee8dSDavid Howells 290*c4508464SDavid 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); 295*c4508464SDavid Howells req->vnode = dvnode; 296c69bf479SDavid Howells req->key = key_get(key); 297*c4508464SDavid Howells req->cleanup = afs_dir_read_cleanup; 298*c4508464SDavid Howells 299*c4508464SDavid Howells expand: 300*c4508464SDavid Howells i_size = i_size_read(&dvnode->vfs_inode); 301*c4508464SDavid Howells if (i_size < 2048) { 302*c4508464SDavid Howells ret = afs_bad(dvnode, afs_file_error_dir_small); 303*c4508464SDavid Howells goto error; 304*c4508464SDavid Howells } 305*c4508464SDavid Howells if (i_size > 2048 * 1024) { 306*c4508464SDavid Howells trace_afs_file_error(dvnode, -EFBIG, afs_file_error_dir_big); 307*c4508464SDavid Howells ret = -EFBIG; 308f3ddee8dSDavid Howells goto error; 309f3ddee8dSDavid Howells } 310f3ddee8dSDavid Howells 311*c4508464SDavid Howells _enter("%llu", i_size); 312*c4508464SDavid Howells 313*c4508464SDavid Howells nr_pages = (i_size + PAGE_SIZE - 1) / PAGE_SIZE; 314*c4508464SDavid Howells 315*c4508464SDavid Howells req->actual_len = i_size; /* May change */ 316*c4508464SDavid Howells req->len = nr_pages * PAGE_SIZE; /* We can ask for more than there is */ 317*c4508464SDavid Howells req->data_version = dvnode->status.data_version; /* May change */ 318*c4508464SDavid Howells iov_iter_xarray(&req->def_iter, READ, &dvnode->vfs_inode.i_mapping->i_pages, 319*c4508464SDavid Howells 0, i_size); 320*c4508464SDavid Howells req->iter = &req->def_iter; 321*c4508464SDavid Howells 322*c4508464SDavid Howells /* Fill in any gaps that we might find where the memory reclaimer has 323*c4508464SDavid 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 */ 326*c4508464SDavid Howells i = req->nr_pages; 327*c4508464SDavid Howells while (i < nr_pages) { 328*c4508464SDavid Howells struct page *pages[8], *page; 329*c4508464SDavid Howells 330f3ddee8dSDavid Howells n = find_get_pages_contig(dvnode->vfs_inode.i_mapping, i, 331*c4508464SDavid Howells min_t(unsigned int, nr_pages - i, 332*c4508464SDavid Howells ARRAY_SIZE(pages)), 333*c4508464SDavid Howells pages); 334*c4508464SDavid Howells _debug("find %u at %u/%u", n, i, nr_pages); 335*c4508464SDavid 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; 343*c4508464SDavid Howells page = __page_cache_alloc(gfp); 344*c4508464SDavid Howells if (!page) 345f3ddee8dSDavid Howells goto error; 346*c4508464SDavid 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 352*c4508464SDavid Howells attach_page_private(page, (void *)1); 353*c4508464SDavid Howells unlock_page(page); 354*c4508464SDavid Howells req->nr_pages++; 355f3ddee8dSDavid Howells i++; 356f3ddee8dSDavid Howells } else { 357*c4508464SDavid Howells req->nr_pages += n; 358f3ddee8dSDavid Howells i += n; 359f3ddee8dSDavid Howells } 360*c4508464SDavid 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 384*c4508464SDavid Howells if (req->len < req->file_size) { 385*c4508464SDavid Howells /* The content has grown, so we need to expand the 386*c4508464SDavid Howells * buffer. 387*c4508464SDavid Howells */ 388*c4508464SDavid Howells up_write(&dvnode->validate_lock); 389*c4508464SDavid Howells goto expand; 390*c4508464SDavid Howells } 391f3ddee8dSDavid Howells 392f3ddee8dSDavid Howells /* Validate the data we just read. */ 393*c4508464SDavid Howells ret = afs_dir_check(dvnode, req); 394*c4508464SDavid 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; 521*c4508464SDavid 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 546*c4508464SDavid Howells * to the LRU. We have all the pages pinned with an extra ref. 547f3ddee8dSDavid Howells */ 548*c4508464SDavid Howells rcu_read_lock(); 549*c4508464SDavid Howells page = NULL; 550*c4508464SDavid Howells slot = radix_tree_lookup_slot(&dvnode->vfs_inode.i_mapping->i_pages, 551*c4508464SDavid Howells blkoff / PAGE_SIZE); 552*c4508464SDavid Howells if (slot) 553*c4508464SDavid Howells page = radix_tree_deref_slot(slot); 554*c4508464SDavid 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 ret = -ENOENT; 6601da177e4SLinus Torvalds if (!cookie.found) { 66108e0e7c8SDavid Howells _leave(" = -ENOENT [not found]"); 66208e0e7c8SDavid Howells return -ENOENT; 66308e0e7c8SDavid Howells } 66408e0e7c8SDavid Howells 66508e0e7c8SDavid Howells *fid = cookie.fid; 6663b6492dfSDavid Howells _leave(" = 0 { vn=%llu u=%u }", fid->vnode, fid->unique); 66708e0e7c8SDavid Howells return 0; 66808e0e7c8SDavid Howells } 66908e0e7c8SDavid Howells 67008e0e7c8SDavid Howells /* 6715cf9dd55SDavid Howells * search the directory for a name 6725cf9dd55SDavid Howells * - if afs_dir_iterate_block() spots this function, it'll pass the FID 6735cf9dd55SDavid Howells * uniquifier through dtype 6745cf9dd55SDavid Howells */ 6755cf9dd55SDavid Howells static int afs_lookup_filldir(struct dir_context *ctx, const char *name, 6765cf9dd55SDavid Howells int nlen, loff_t fpos, u64 ino, unsigned dtype) 6775cf9dd55SDavid Howells { 6785cf9dd55SDavid Howells struct afs_lookup_cookie *cookie = 6795cf9dd55SDavid Howells container_of(ctx, struct afs_lookup_cookie, ctx); 6805cf9dd55SDavid Howells int ret; 6815cf9dd55SDavid Howells 6825cf9dd55SDavid Howells _enter("{%s,%u},%s,%u,,%llu,%u", 6835cf9dd55SDavid Howells cookie->name.name, cookie->name.len, name, nlen, 6845cf9dd55SDavid Howells (unsigned long long) ino, dtype); 6855cf9dd55SDavid Howells 6865cf9dd55SDavid Howells /* insanity checks first */ 68700317636SDavid Howells BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048); 68800317636SDavid Howells BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32); 6895cf9dd55SDavid Howells 6905cf9dd55SDavid Howells if (cookie->found) { 6915cf9dd55SDavid Howells if (cookie->nr_fids < 50) { 6925cf9dd55SDavid Howells cookie->fids[cookie->nr_fids].vnode = ino; 6935cf9dd55SDavid Howells cookie->fids[cookie->nr_fids].unique = dtype; 6945cf9dd55SDavid Howells cookie->nr_fids++; 6955cf9dd55SDavid Howells } 6965cf9dd55SDavid Howells } else if (cookie->name.len == nlen && 6975cf9dd55SDavid Howells memcmp(cookie->name.name, name, nlen) == 0) { 698e49c7b2fSDavid Howells cookie->fids[1].vnode = ino; 699e49c7b2fSDavid Howells cookie->fids[1].unique = dtype; 7005cf9dd55SDavid Howells cookie->found = 1; 7015cf9dd55SDavid Howells if (cookie->one_only) 7025cf9dd55SDavid Howells return -1; 7035cf9dd55SDavid Howells } 7045cf9dd55SDavid Howells 7055cf9dd55SDavid Howells ret = cookie->nr_fids >= 50 ? -1 : 0; 7065cf9dd55SDavid Howells _leave(" = %d", ret); 7075cf9dd55SDavid Howells return ret; 7085cf9dd55SDavid Howells } 7095cf9dd55SDavid Howells 7105cf9dd55SDavid Howells /* 711e49c7b2fSDavid Howells * Deal with the result of a successful lookup operation. Turn all the files 712e49c7b2fSDavid Howells * into inodes and save the first one - which is the one we actually want. 713e49c7b2fSDavid Howells */ 714e49c7b2fSDavid Howells static void afs_do_lookup_success(struct afs_operation *op) 715e49c7b2fSDavid Howells { 716e49c7b2fSDavid Howells struct afs_vnode_param *vp; 717e49c7b2fSDavid Howells struct afs_vnode *vnode; 718e49c7b2fSDavid Howells struct inode *inode; 719e49c7b2fSDavid Howells u32 abort_code; 720e49c7b2fSDavid Howells int i; 721e49c7b2fSDavid Howells 722e49c7b2fSDavid Howells _enter(""); 723e49c7b2fSDavid Howells 724e49c7b2fSDavid Howells for (i = 0; i < op->nr_files; i++) { 725e49c7b2fSDavid Howells switch (i) { 726e49c7b2fSDavid Howells case 0: 727e49c7b2fSDavid Howells vp = &op->file[0]; 728e49c7b2fSDavid Howells abort_code = vp->scb.status.abort_code; 729e49c7b2fSDavid Howells if (abort_code != 0) { 73044767c35SDavid Howells op->ac.abort_code = abort_code; 731e49c7b2fSDavid Howells op->error = afs_abort_to_error(abort_code); 732e49c7b2fSDavid Howells } 733e49c7b2fSDavid Howells break; 734e49c7b2fSDavid Howells 735e49c7b2fSDavid Howells case 1: 736e49c7b2fSDavid Howells vp = &op->file[1]; 737e49c7b2fSDavid Howells break; 738e49c7b2fSDavid Howells 739e49c7b2fSDavid Howells default: 740e49c7b2fSDavid Howells vp = &op->more_files[i - 2]; 741e49c7b2fSDavid Howells break; 742e49c7b2fSDavid Howells } 743e49c7b2fSDavid Howells 744e49c7b2fSDavid Howells if (!vp->scb.have_status && !vp->scb.have_error) 745e49c7b2fSDavid Howells continue; 746e49c7b2fSDavid Howells 747e49c7b2fSDavid Howells _debug("do [%u]", i); 748e49c7b2fSDavid Howells if (vp->vnode) { 749e49c7b2fSDavid Howells if (!test_bit(AFS_VNODE_UNSET, &vp->vnode->flags)) 750e49c7b2fSDavid Howells afs_vnode_commit_status(op, vp); 751e49c7b2fSDavid Howells } else if (vp->scb.status.abort_code == 0) { 752e49c7b2fSDavid Howells inode = afs_iget(op, vp); 753e49c7b2fSDavid Howells if (!IS_ERR(inode)) { 754e49c7b2fSDavid Howells vnode = AFS_FS_I(inode); 755e49c7b2fSDavid Howells afs_cache_permit(vnode, op->key, 756e49c7b2fSDavid Howells 0 /* Assume vnode->cb_break is 0 */ + 757e49c7b2fSDavid Howells op->cb_v_break, 758e49c7b2fSDavid Howells &vp->scb); 759e49c7b2fSDavid Howells vp->vnode = vnode; 760e49c7b2fSDavid Howells vp->put_vnode = true; 761e49c7b2fSDavid Howells } 762e49c7b2fSDavid Howells } else { 763e49c7b2fSDavid Howells _debug("- abort %d %llx:%llx.%x", 764e49c7b2fSDavid Howells vp->scb.status.abort_code, 765e49c7b2fSDavid Howells vp->fid.vid, vp->fid.vnode, vp->fid.unique); 766e49c7b2fSDavid Howells } 767e49c7b2fSDavid Howells } 768e49c7b2fSDavid Howells 769e49c7b2fSDavid Howells _leave(""); 770e49c7b2fSDavid Howells } 771e49c7b2fSDavid Howells 772e49c7b2fSDavid Howells static const struct afs_operation_ops afs_inline_bulk_status_operation = { 773e49c7b2fSDavid Howells .issue_afs_rpc = afs_fs_inline_bulk_status, 774e49c7b2fSDavid Howells .issue_yfs_rpc = yfs_fs_inline_bulk_status, 775e49c7b2fSDavid Howells .success = afs_do_lookup_success, 776e49c7b2fSDavid Howells }; 777e49c7b2fSDavid Howells 778b6489a49SDavid Howells static const struct afs_operation_ops afs_lookup_fetch_status_operation = { 779e49c7b2fSDavid Howells .issue_afs_rpc = afs_fs_fetch_status, 780e49c7b2fSDavid Howells .issue_yfs_rpc = yfs_fs_fetch_status, 781e49c7b2fSDavid Howells .success = afs_do_lookup_success, 782728279a5SDavid Howells .aborted = afs_check_for_remote_deletion, 783e49c7b2fSDavid Howells }; 784e49c7b2fSDavid Howells 785e49c7b2fSDavid Howells /* 78620325960SDavid Howells * See if we know that the server we expect to use doesn't support 78720325960SDavid Howells * FS.InlineBulkStatus. 78820325960SDavid Howells */ 78920325960SDavid Howells static bool afs_server_supports_ibulk(struct afs_vnode *dvnode) 79020325960SDavid Howells { 79120325960SDavid Howells struct afs_server_list *slist; 79220325960SDavid Howells struct afs_volume *volume = dvnode->volume; 79320325960SDavid Howells struct afs_server *server; 79420325960SDavid Howells bool ret = true; 79520325960SDavid Howells int i; 79620325960SDavid Howells 79720325960SDavid Howells if (!test_bit(AFS_VOLUME_MAYBE_NO_IBULK, &volume->flags)) 79820325960SDavid Howells return true; 79920325960SDavid Howells 80020325960SDavid Howells rcu_read_lock(); 80120325960SDavid Howells slist = rcu_dereference(volume->servers); 80220325960SDavid Howells 80320325960SDavid Howells for (i = 0; i < slist->nr_servers; i++) { 80420325960SDavid Howells server = slist->servers[i].server; 80520325960SDavid Howells if (server == dvnode->cb_server) { 80620325960SDavid Howells if (test_bit(AFS_SERVER_FL_NO_IBULK, &server->flags)) 80720325960SDavid Howells ret = false; 80820325960SDavid Howells break; 80920325960SDavid Howells } 81020325960SDavid Howells } 81120325960SDavid Howells 81220325960SDavid Howells rcu_read_unlock(); 81320325960SDavid Howells return ret; 81420325960SDavid Howells } 81520325960SDavid Howells 81620325960SDavid Howells /* 8175cf9dd55SDavid Howells * Do a lookup in a directory. We make use of bulk lookup to query a slew of 8185cf9dd55SDavid Howells * files in one go and create inodes for them. The inode of the file we were 8195cf9dd55SDavid Howells * asked for is returned. 8205cf9dd55SDavid Howells */ 8215cf9dd55SDavid Howells static struct inode *afs_do_lookup(struct inode *dir, struct dentry *dentry, 8225cf9dd55SDavid Howells struct key *key) 8235cf9dd55SDavid Howells { 8245cf9dd55SDavid Howells struct afs_lookup_cookie *cookie; 825e49c7b2fSDavid Howells struct afs_vnode_param *vp; 826e49c7b2fSDavid Howells struct afs_operation *op; 82739db9815SDavid Howells struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode; 82839db9815SDavid Howells struct inode *inode = NULL, *ti; 8299dd0b82eSDavid Howells afs_dataversion_t data_version = READ_ONCE(dvnode->status.data_version); 830e49c7b2fSDavid Howells long ret; 831e49c7b2fSDavid Howells int i; 8325cf9dd55SDavid Howells 8335cf9dd55SDavid Howells _enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry); 8345cf9dd55SDavid Howells 8355cf9dd55SDavid Howells cookie = kzalloc(sizeof(struct afs_lookup_cookie), GFP_KERNEL); 8365cf9dd55SDavid Howells if (!cookie) 8375cf9dd55SDavid Howells return ERR_PTR(-ENOMEM); 8385cf9dd55SDavid Howells 839e49c7b2fSDavid Howells for (i = 0; i < ARRAY_SIZE(cookie->fids); i++) 840e49c7b2fSDavid Howells cookie->fids[i].vid = dvnode->fid.vid; 8415cf9dd55SDavid Howells cookie->ctx.actor = afs_lookup_filldir; 8425cf9dd55SDavid Howells cookie->name = dentry->d_name; 84313fcc635SDavid Howells cookie->nr_fids = 2; /* slot 0 is saved for the fid we actually want 84413fcc635SDavid Howells * and slot 1 for the directory */ 8455cf9dd55SDavid Howells 84620325960SDavid Howells if (!afs_server_supports_ibulk(dvnode)) 8475cf9dd55SDavid Howells cookie->one_only = true; 8485cf9dd55SDavid Howells 8495cf9dd55SDavid Howells /* search the directory */ 8509dd0b82eSDavid Howells ret = afs_dir_iterate(dir, &cookie->ctx, key, &data_version); 851e49c7b2fSDavid Howells if (ret < 0) 8525cf9dd55SDavid Howells goto out; 8535cf9dd55SDavid Howells 8549dd0b82eSDavid Howells dentry->d_fsdata = (void *)(unsigned long)data_version; 8559dd0b82eSDavid Howells 856e49c7b2fSDavid Howells ret = -ENOENT; 8575cf9dd55SDavid Howells if (!cookie->found) 8585cf9dd55SDavid Howells goto out; 8595cf9dd55SDavid Howells 8605cf9dd55SDavid Howells /* Check to see if we already have an inode for the primary fid. */ 861e49c7b2fSDavid Howells inode = ilookup5(dir->i_sb, cookie->fids[1].vnode, 862e49c7b2fSDavid Howells afs_ilookup5_test_by_fid, &cookie->fids[1]); 8635cf9dd55SDavid Howells if (inode) 864e49c7b2fSDavid Howells goto out; /* We do */ 865e49c7b2fSDavid Howells 866e49c7b2fSDavid Howells /* Okay, we didn't find it. We need to query the server - and whilst 867e49c7b2fSDavid Howells * we're doing that, we're going to attempt to look up a bunch of other 868e49c7b2fSDavid Howells * vnodes also. 869e49c7b2fSDavid Howells */ 870e49c7b2fSDavid Howells op = afs_alloc_operation(NULL, dvnode->volume); 871e49c7b2fSDavid Howells if (IS_ERR(op)) { 872e49c7b2fSDavid Howells ret = PTR_ERR(op); 8735cf9dd55SDavid Howells goto out; 874e49c7b2fSDavid Howells } 875e49c7b2fSDavid Howells 876e49c7b2fSDavid Howells afs_op_set_vnode(op, 0, dvnode); 877e49c7b2fSDavid Howells afs_op_set_fid(op, 1, &cookie->fids[1]); 878e49c7b2fSDavid Howells 879e49c7b2fSDavid Howells op->nr_files = cookie->nr_fids; 880e49c7b2fSDavid Howells _debug("nr_files %u", op->nr_files); 8815cf9dd55SDavid Howells 8825cf9dd55SDavid Howells /* Need space for examining all the selected files */ 883e49c7b2fSDavid Howells op->error = -ENOMEM; 884e49c7b2fSDavid Howells if (op->nr_files > 2) { 885e49c7b2fSDavid Howells op->more_files = kvcalloc(op->nr_files - 2, 886e49c7b2fSDavid Howells sizeof(struct afs_vnode_param), 8875cf9dd55SDavid Howells GFP_KERNEL); 888e49c7b2fSDavid Howells if (!op->more_files) 889e49c7b2fSDavid Howells goto out_op; 8905cf9dd55SDavid Howells 891e49c7b2fSDavid Howells for (i = 2; i < op->nr_files; i++) { 892e49c7b2fSDavid Howells vp = &op->more_files[i - 2]; 893e49c7b2fSDavid Howells vp->fid = cookie->fids[i]; 89439db9815SDavid Howells 89539db9815SDavid Howells /* Find any inodes that already exist and get their 89639db9815SDavid Howells * callback counters. 89739db9815SDavid Howells */ 898e49c7b2fSDavid Howells ti = ilookup5_nowait(dir->i_sb, vp->fid.vnode, 899e49c7b2fSDavid Howells afs_ilookup5_test_by_fid, &vp->fid); 90039db9815SDavid Howells if (!IS_ERR_OR_NULL(ti)) { 90139db9815SDavid Howells vnode = AFS_FS_I(ti); 902e49c7b2fSDavid Howells vp->dv_before = vnode->status.data_version; 903e49c7b2fSDavid Howells vp->cb_break_before = afs_calc_vnode_cb_break(vnode); 904e49c7b2fSDavid Howells vp->vnode = vnode; 905e49c7b2fSDavid Howells vp->put_vnode = true; 906a9e5c87cSDavid Howells vp->speculative = true; /* vnode not locked */ 907e49c7b2fSDavid Howells } 90839db9815SDavid Howells } 90939db9815SDavid Howells } 91039db9815SDavid Howells 9115cf9dd55SDavid Howells /* Try FS.InlineBulkStatus first. Abort codes for the individual 9125cf9dd55SDavid Howells * lookups contained therein are stored in the reply without aborting 9135cf9dd55SDavid Howells * the whole operation. 9145cf9dd55SDavid Howells */ 915e49c7b2fSDavid Howells op->error = -ENOTSUPP; 916e49c7b2fSDavid Howells if (!cookie->one_only) { 917e49c7b2fSDavid Howells op->ops = &afs_inline_bulk_status_operation; 918e49c7b2fSDavid Howells afs_begin_vnode_operation(op); 919e49c7b2fSDavid Howells afs_wait_for_operation(op); 9205cf9dd55SDavid Howells } 9215cf9dd55SDavid Howells 922e49c7b2fSDavid Howells if (op->error == -ENOTSUPP) { 923e49c7b2fSDavid Howells /* We could try FS.BulkStatus next, but this aborts the entire 924e49c7b2fSDavid Howells * op if any of the lookups fails - so, for the moment, revert 925e49c7b2fSDavid Howells * to FS.FetchStatus for op->file[1]. 9265cf9dd55SDavid Howells */ 927e49c7b2fSDavid Howells op->fetch_status.which = 1; 928f8ea5c7bSDavid Howells op->ops = &afs_lookup_fetch_status_operation; 929e49c7b2fSDavid Howells afs_begin_vnode_operation(op); 930e49c7b2fSDavid Howells afs_wait_for_operation(op); 931e49c7b2fSDavid Howells } 932e49c7b2fSDavid Howells inode = ERR_PTR(op->error); 933e49c7b2fSDavid Howells 934e49c7b2fSDavid Howells out_op: 935e49c7b2fSDavid Howells if (op->error == 0) { 936e49c7b2fSDavid Howells inode = &op->file[1].vnode->vfs_inode; 937e49c7b2fSDavid Howells op->file[1].vnode = NULL; 9385cf9dd55SDavid Howells } 9395cf9dd55SDavid Howells 940e49c7b2fSDavid Howells if (op->file[0].scb.have_status) 941e49c7b2fSDavid Howells dentry->d_fsdata = (void *)(unsigned long)op->file[0].scb.status.data_version; 942e49c7b2fSDavid Howells else 943e49c7b2fSDavid Howells dentry->d_fsdata = (void *)(unsigned long)op->file[0].dv_before; 944e49c7b2fSDavid Howells ret = afs_put_operation(op); 9455cf9dd55SDavid Howells out: 9465cf9dd55SDavid Howells kfree(cookie); 947e49c7b2fSDavid Howells _leave(""); 948e49c7b2fSDavid Howells return inode ?: ERR_PTR(ret); 9495cf9dd55SDavid Howells } 9505cf9dd55SDavid Howells 9515cf9dd55SDavid Howells /* 9526f8880d8SDavid Howells * Look up an entry in a directory with @sys substitution. 9536f8880d8SDavid Howells */ 9546f8880d8SDavid Howells static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry, 9556f8880d8SDavid Howells struct key *key) 9566f8880d8SDavid Howells { 9576f8880d8SDavid Howells struct afs_sysnames *subs; 9586f8880d8SDavid Howells struct afs_net *net = afs_i2net(dir); 9596f8880d8SDavid Howells struct dentry *ret; 9606f8880d8SDavid Howells char *buf, *p, *name; 9616f8880d8SDavid Howells int len, i; 9626f8880d8SDavid Howells 9636f8880d8SDavid Howells _enter(""); 9646f8880d8SDavid Howells 9656f8880d8SDavid Howells ret = ERR_PTR(-ENOMEM); 9666f8880d8SDavid Howells p = buf = kmalloc(AFSNAMEMAX, GFP_KERNEL); 9676f8880d8SDavid Howells if (!buf) 9686f8880d8SDavid Howells goto out_p; 9696f8880d8SDavid Howells if (dentry->d_name.len > 4) { 9706f8880d8SDavid Howells memcpy(p, dentry->d_name.name, dentry->d_name.len - 4); 9716f8880d8SDavid Howells p += dentry->d_name.len - 4; 9726f8880d8SDavid Howells } 9736f8880d8SDavid Howells 9746f8880d8SDavid Howells /* There is an ordered list of substitutes that we have to try. */ 9756f8880d8SDavid Howells read_lock(&net->sysnames_lock); 9766f8880d8SDavid Howells subs = net->sysnames; 9776f8880d8SDavid Howells refcount_inc(&subs->usage); 9786f8880d8SDavid Howells read_unlock(&net->sysnames_lock); 9796f8880d8SDavid Howells 9806f8880d8SDavid Howells for (i = 0; i < subs->nr; i++) { 9816f8880d8SDavid Howells name = subs->subs[i]; 9826f8880d8SDavid Howells len = dentry->d_name.len - 4 + strlen(name); 9836f8880d8SDavid Howells if (len >= AFSNAMEMAX) { 9846f8880d8SDavid Howells ret = ERR_PTR(-ENAMETOOLONG); 9856f8880d8SDavid Howells goto out_s; 9866f8880d8SDavid Howells } 9876f8880d8SDavid Howells 9886f8880d8SDavid Howells strcpy(p, name); 9896f8880d8SDavid Howells ret = lookup_one_len(buf, dentry->d_parent, len); 9906f8880d8SDavid Howells if (IS_ERR(ret) || d_is_positive(ret)) 9916f8880d8SDavid Howells goto out_s; 9926f8880d8SDavid Howells dput(ret); 9936f8880d8SDavid Howells } 9946f8880d8SDavid Howells 9956f8880d8SDavid Howells /* We don't want to d_add() the @sys dentry here as we don't want to 9966f8880d8SDavid Howells * the cached dentry to hide changes to the sysnames list. 9976f8880d8SDavid Howells */ 9986f8880d8SDavid Howells ret = NULL; 9996f8880d8SDavid Howells out_s: 10006f8880d8SDavid Howells afs_put_sysnames(subs); 10016f8880d8SDavid Howells kfree(buf); 10026f8880d8SDavid Howells out_p: 10036f8880d8SDavid Howells key_put(key); 10046f8880d8SDavid Howells return ret; 10056f8880d8SDavid Howells } 10066f8880d8SDavid Howells 10076f8880d8SDavid Howells /* 100808e0e7c8SDavid Howells * look up an entry in a directory 100908e0e7c8SDavid Howells */ 1010260a9803SDavid Howells static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry, 101100cd8dd3SAl Viro unsigned int flags) 101208e0e7c8SDavid Howells { 10135cf9dd55SDavid Howells struct afs_vnode *dvnode = AFS_FS_I(dir); 101440a708bdSDavid Howells struct afs_fid fid = {}; 101508e0e7c8SDavid Howells struct inode *inode; 101634b2a88fSAl Viro struct dentry *d; 101700d3b7a4SDavid Howells struct key *key; 101808e0e7c8SDavid Howells int ret; 101908e0e7c8SDavid Howells 10203b6492dfSDavid Howells _enter("{%llx:%llu},%p{%pd},", 10215cf9dd55SDavid Howells dvnode->fid.vid, dvnode->fid.vnode, dentry, dentry); 1022260a9803SDavid Howells 10232b0143b5SDavid Howells ASSERTCMP(d_inode(dentry), ==, NULL); 102408e0e7c8SDavid Howells 102545222b9eSDavid Howells if (dentry->d_name.len >= AFSNAMEMAX) { 102608e0e7c8SDavid Howells _leave(" = -ENAMETOOLONG"); 102708e0e7c8SDavid Howells return ERR_PTR(-ENAMETOOLONG); 102808e0e7c8SDavid Howells } 102908e0e7c8SDavid Howells 10305cf9dd55SDavid Howells if (test_bit(AFS_VNODE_DELETED, &dvnode->flags)) { 103108e0e7c8SDavid Howells _leave(" = -ESTALE"); 103208e0e7c8SDavid Howells return ERR_PTR(-ESTALE); 103308e0e7c8SDavid Howells } 103408e0e7c8SDavid Howells 10355cf9dd55SDavid Howells key = afs_request_key(dvnode->volume->cell); 103600d3b7a4SDavid Howells if (IS_ERR(key)) { 103700d3b7a4SDavid Howells _leave(" = %ld [key]", PTR_ERR(key)); 1038e231c2eeSDavid Howells return ERR_CAST(key); 103900d3b7a4SDavid Howells } 104000d3b7a4SDavid Howells 10415cf9dd55SDavid Howells ret = afs_validate(dvnode, key); 104208e0e7c8SDavid Howells if (ret < 0) { 104300d3b7a4SDavid Howells key_put(key); 1044260a9803SDavid Howells _leave(" = %d [val]", ret); 10451da177e4SLinus Torvalds return ERR_PTR(ret); 10461da177e4SLinus Torvalds } 10471da177e4SLinus Torvalds 10486f8880d8SDavid Howells if (dentry->d_name.len >= 4 && 10496f8880d8SDavid Howells dentry->d_name.name[dentry->d_name.len - 4] == '@' && 10506f8880d8SDavid Howells dentry->d_name.name[dentry->d_name.len - 3] == 's' && 10516f8880d8SDavid Howells dentry->d_name.name[dentry->d_name.len - 2] == 'y' && 10526f8880d8SDavid Howells dentry->d_name.name[dentry->d_name.len - 1] == 's') 10536f8880d8SDavid Howells return afs_lookup_atsys(dir, dentry, key); 10546f8880d8SDavid Howells 1055d55b4da4SDavid Howells afs_stat_v(dvnode, n_lookup); 10565cf9dd55SDavid Howells inode = afs_do_lookup(dir, dentry, key); 105734b2a88fSAl Viro key_put(key); 1058f52b83b0SDavid Howells if (inode == ERR_PTR(-ENOENT)) 10595cf9dd55SDavid Howells inode = afs_try_auto_mntpt(dentry, dir); 106040a708bdSDavid Howells 106140a708bdSDavid Howells if (!IS_ERR_OR_NULL(inode)) 106240a708bdSDavid Howells fid = AFS_FS_I(inode)->fid; 106340a708bdSDavid Howells 1064fed79fd7SDavid Howells _debug("splice %p", dentry->d_inode); 106534b2a88fSAl Viro d = d_splice_alias(inode, dentry); 106680548b03SDavid Howells if (!IS_ERR_OR_NULL(d)) { 106734b2a88fSAl Viro d->d_fsdata = dentry->d_fsdata; 106840a708bdSDavid Howells trace_afs_lookup(dvnode, &d->d_name, &fid); 106980548b03SDavid Howells } else { 107040a708bdSDavid Howells trace_afs_lookup(dvnode, &dentry->d_name, &fid); 107180548b03SDavid Howells } 1072e49c7b2fSDavid Howells _leave(""); 107334b2a88fSAl Viro return d; 1074ec26815aSDavid Howells } 10751da177e4SLinus Torvalds 10761da177e4SLinus Torvalds /* 1077a0753c29SDavid Howells * Check the validity of a dentry under RCU conditions. 1078a0753c29SDavid Howells */ 1079a0753c29SDavid Howells static int afs_d_revalidate_rcu(struct dentry *dentry) 1080a0753c29SDavid Howells { 1081a0753c29SDavid Howells struct afs_vnode *dvnode, *vnode; 1082a0753c29SDavid Howells struct dentry *parent; 1083a0753c29SDavid Howells struct inode *dir, *inode; 1084a0753c29SDavid Howells long dir_version, de_version; 1085a0753c29SDavid Howells 1086a0753c29SDavid Howells _enter("%p", dentry); 1087a0753c29SDavid Howells 1088a0753c29SDavid Howells /* Check the parent directory is still valid first. */ 1089a0753c29SDavid Howells parent = READ_ONCE(dentry->d_parent); 1090a0753c29SDavid Howells dir = d_inode_rcu(parent); 1091a0753c29SDavid Howells if (!dir) 1092a0753c29SDavid Howells return -ECHILD; 1093a0753c29SDavid Howells dvnode = AFS_FS_I(dir); 1094a0753c29SDavid Howells if (test_bit(AFS_VNODE_DELETED, &dvnode->flags)) 1095a0753c29SDavid Howells return -ECHILD; 1096a0753c29SDavid Howells 1097a0753c29SDavid Howells if (!afs_check_validity(dvnode)) 1098a0753c29SDavid Howells return -ECHILD; 1099a0753c29SDavid Howells 1100a0753c29SDavid Howells /* We only need to invalidate a dentry if the server's copy changed 1101a0753c29SDavid Howells * behind our back. If we made the change, it's no problem. Note that 1102a0753c29SDavid Howells * on a 32-bit system, we only have 32 bits in the dentry to store the 1103a0753c29SDavid Howells * version. 1104a0753c29SDavid Howells */ 1105a0753c29SDavid Howells dir_version = (long)READ_ONCE(dvnode->status.data_version); 1106a0753c29SDavid Howells de_version = (long)READ_ONCE(dentry->d_fsdata); 1107a0753c29SDavid Howells if (de_version != dir_version) { 1108a0753c29SDavid Howells dir_version = (long)READ_ONCE(dvnode->invalid_before); 1109a0753c29SDavid Howells if (de_version - dir_version < 0) 1110a0753c29SDavid Howells return -ECHILD; 1111a0753c29SDavid Howells } 1112a0753c29SDavid Howells 1113a0753c29SDavid Howells /* Check to see if the vnode referred to by the dentry still 1114a0753c29SDavid Howells * has a callback. 1115a0753c29SDavid Howells */ 1116a0753c29SDavid Howells if (d_really_is_positive(dentry)) { 1117a0753c29SDavid Howells inode = d_inode_rcu(dentry); 1118a0753c29SDavid Howells if (inode) { 1119a0753c29SDavid Howells vnode = AFS_FS_I(inode); 1120a0753c29SDavid Howells if (!afs_check_validity(vnode)) 1121a0753c29SDavid Howells return -ECHILD; 1122a0753c29SDavid Howells } 1123a0753c29SDavid Howells } 1124a0753c29SDavid Howells 1125a0753c29SDavid Howells return 1; /* Still valid */ 1126a0753c29SDavid Howells } 1127a0753c29SDavid Howells 1128a0753c29SDavid Howells /* 11291da177e4SLinus Torvalds * check that a dentry lookup hit has found a valid entry 11301da177e4SLinus Torvalds * - NOTE! the hit can be a negative hit too, so we can't assume we have an 11311da177e4SLinus Torvalds * inode 11321da177e4SLinus Torvalds */ 11330b728e19SAl Viro static int afs_d_revalidate(struct dentry *dentry, unsigned int flags) 11341da177e4SLinus Torvalds { 1135260a9803SDavid Howells struct afs_vnode *vnode, *dir; 11363f649ab7SKees Cook struct afs_fid fid; 11371da177e4SLinus Torvalds struct dentry *parent; 1138c435ee34SDavid Howells struct inode *inode; 113900d3b7a4SDavid Howells struct key *key; 114040fc8102SDavid Howells afs_dataversion_t dir_version, invalid_before; 11419dd0b82eSDavid Howells long de_version; 11421da177e4SLinus Torvalds int ret; 11431da177e4SLinus Torvalds 11440b728e19SAl Viro if (flags & LOOKUP_RCU) 1145a0753c29SDavid Howells return afs_d_revalidate_rcu(dentry); 114634286d66SNick Piggin 1147c435ee34SDavid Howells if (d_really_is_positive(dentry)) { 11482b0143b5SDavid Howells vnode = AFS_FS_I(d_inode(dentry)); 11493b6492dfSDavid Howells _enter("{v={%llx:%llu} n=%pd fl=%lx},", 1150a455589fSAl Viro vnode->fid.vid, vnode->fid.vnode, dentry, 1151260a9803SDavid Howells vnode->flags); 1152c435ee34SDavid Howells } else { 1153a455589fSAl Viro _enter("{neg n=%pd}", dentry); 1154c435ee34SDavid Howells } 11551da177e4SLinus Torvalds 1156260a9803SDavid Howells key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell); 115700d3b7a4SDavid Howells if (IS_ERR(key)) 115800d3b7a4SDavid Howells key = NULL; 115900d3b7a4SDavid Howells 1160c435ee34SDavid Howells if (d_really_is_positive(dentry)) { 1161c435ee34SDavid Howells inode = d_inode(dentry); 1162c435ee34SDavid Howells if (inode) { 1163c435ee34SDavid Howells vnode = AFS_FS_I(inode); 1164c435ee34SDavid Howells afs_validate(vnode, key); 1165c435ee34SDavid Howells if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) 1166c435ee34SDavid Howells goto out_bad; 1167c435ee34SDavid Howells } 1168c435ee34SDavid Howells } 1169c435ee34SDavid Howells 11701da177e4SLinus Torvalds /* lock down the parent dentry so we can peer at it */ 117108e0e7c8SDavid Howells parent = dget_parent(dentry); 11722b0143b5SDavid Howells dir = AFS_FS_I(d_inode(parent)); 11731da177e4SLinus Torvalds 1174260a9803SDavid Howells /* validate the parent directory */ 1175260a9803SDavid Howells afs_validate(dir, key); 1176260a9803SDavid Howells 1177260a9803SDavid Howells if (test_bit(AFS_VNODE_DELETED, &dir->flags)) { 1178a455589fSAl Viro _debug("%pd: parent dir deleted", dentry); 1179c435ee34SDavid Howells goto out_bad_parent; 11801da177e4SLinus Torvalds } 11811da177e4SLinus Torvalds 1182a4ff7401SDavid Howells /* We only need to invalidate a dentry if the server's copy changed 1183a4ff7401SDavid Howells * behind our back. If we made the change, it's no problem. Note that 1184a4ff7401SDavid Howells * on a 32-bit system, we only have 32 bits in the dentry to store the 1185a4ff7401SDavid Howells * version. 1186a4ff7401SDavid Howells */ 11879dd0b82eSDavid Howells dir_version = dir->status.data_version; 1188a4ff7401SDavid Howells de_version = (long)dentry->d_fsdata; 11899dd0b82eSDavid Howells if (de_version == (long)dir_version) 11905dc84855SDavid Howells goto out_valid_noupdate; 1191a4ff7401SDavid Howells 119240fc8102SDavid Howells invalid_before = dir->invalid_before; 119340fc8102SDavid Howells if (de_version - (long)invalid_before >= 0) 1194a4ff7401SDavid Howells goto out_valid; 1195260a9803SDavid Howells 119608e0e7c8SDavid Howells _debug("dir modified"); 1197d55b4da4SDavid Howells afs_stat_v(dir, n_reval); 11981da177e4SLinus Torvalds 11991da177e4SLinus Torvalds /* search the directory for this vnode */ 12009dd0b82eSDavid Howells ret = afs_do_lookup_one(&dir->vfs_inode, dentry, &fid, key, &dir_version); 1201260a9803SDavid Howells switch (ret) { 1202260a9803SDavid Howells case 0: 1203260a9803SDavid Howells /* the filename maps to something */ 12042b0143b5SDavid Howells if (d_really_is_negative(dentry)) 1205c435ee34SDavid Howells goto out_bad_parent; 1206c435ee34SDavid Howells inode = d_inode(dentry); 1207c435ee34SDavid Howells if (is_bad_inode(inode)) { 1208a455589fSAl Viro printk("kAFS: afs_d_revalidate: %pd2 has bad inode\n", 1209a455589fSAl Viro dentry); 1210c435ee34SDavid Howells goto out_bad_parent; 12111da177e4SLinus Torvalds } 12121da177e4SLinus Torvalds 1213c435ee34SDavid Howells vnode = AFS_FS_I(inode); 1214c435ee34SDavid Howells 12151da177e4SLinus Torvalds /* if the vnode ID has changed, then the dirent points to a 12161da177e4SLinus Torvalds * different file */ 121708e0e7c8SDavid Howells if (fid.vnode != vnode->fid.vnode) { 12183b6492dfSDavid Howells _debug("%pd: dirent changed [%llu != %llu]", 1219a455589fSAl Viro dentry, fid.vnode, 122008e0e7c8SDavid Howells vnode->fid.vnode); 12211da177e4SLinus Torvalds goto not_found; 12221da177e4SLinus Torvalds } 12231da177e4SLinus Torvalds 12241da177e4SLinus Torvalds /* if the vnode ID uniqifier has changed, then the file has 1225260a9803SDavid Howells * been deleted and replaced, and the original vnode ID has 1226260a9803SDavid Howells * been reused */ 122708e0e7c8SDavid Howells if (fid.unique != vnode->fid.unique) { 1228a455589fSAl Viro _debug("%pd: file deleted (uq %u -> %u I:%u)", 1229a455589fSAl Viro dentry, fid.unique, 12307a224228SJean Noel Cordenner vnode->fid.unique, 1231c435ee34SDavid Howells vnode->vfs_inode.i_generation); 1232c435ee34SDavid Howells write_seqlock(&vnode->cb_lock); 123308e0e7c8SDavid Howells set_bit(AFS_VNODE_DELETED, &vnode->flags); 1234c435ee34SDavid Howells write_sequnlock(&vnode->cb_lock); 1235260a9803SDavid Howells goto not_found; 1236260a9803SDavid Howells } 1237260a9803SDavid Howells goto out_valid; 1238260a9803SDavid Howells 1239260a9803SDavid Howells case -ENOENT: 1240260a9803SDavid Howells /* the filename is unknown */ 1241a455589fSAl Viro _debug("%pd: dirent not found", dentry); 12422b0143b5SDavid Howells if (d_really_is_positive(dentry)) 1243260a9803SDavid Howells goto not_found; 1244260a9803SDavid Howells goto out_valid; 1245260a9803SDavid Howells 1246260a9803SDavid Howells default: 1247a455589fSAl Viro _debug("failed to iterate dir %pd: %d", 1248a455589fSAl Viro parent, ret); 1249c435ee34SDavid Howells goto out_bad_parent; 12501da177e4SLinus Torvalds } 125108e0e7c8SDavid Howells 12521da177e4SLinus Torvalds out_valid: 12539dd0b82eSDavid Howells dentry->d_fsdata = (void *)(unsigned long)dir_version; 12545dc84855SDavid Howells out_valid_noupdate: 12551da177e4SLinus Torvalds dput(parent); 125600d3b7a4SDavid Howells key_put(key); 12571da177e4SLinus Torvalds _leave(" = 1 [valid]"); 12581da177e4SLinus Torvalds return 1; 12591da177e4SLinus Torvalds 12601da177e4SLinus Torvalds /* the dirent, if it exists, now points to a different vnode */ 12611da177e4SLinus Torvalds not_found: 12621da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 12631da177e4SLinus Torvalds dentry->d_flags |= DCACHE_NFSFS_RENAMED; 12641da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 12651da177e4SLinus Torvalds 1266c435ee34SDavid Howells out_bad_parent: 1267a455589fSAl Viro _debug("dropping dentry %pd2", dentry); 12681da177e4SLinus Torvalds dput(parent); 1269c435ee34SDavid Howells out_bad: 127000d3b7a4SDavid Howells key_put(key); 12711da177e4SLinus Torvalds 12721da177e4SLinus Torvalds _leave(" = 0 [bad]"); 12731da177e4SLinus Torvalds return 0; 1274ec26815aSDavid Howells } 12751da177e4SLinus Torvalds 12761da177e4SLinus Torvalds /* 12771da177e4SLinus Torvalds * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't 12781da177e4SLinus Torvalds * sleep) 12791da177e4SLinus Torvalds * - called from dput() when d_count is going to 0. 12801da177e4SLinus Torvalds * - return 1 to request dentry be unhashed, 0 otherwise 12811da177e4SLinus Torvalds */ 1282fe15ce44SNick Piggin static int afs_d_delete(const struct dentry *dentry) 12831da177e4SLinus Torvalds { 1284a455589fSAl Viro _enter("%pd", dentry); 12851da177e4SLinus Torvalds 12861da177e4SLinus Torvalds if (dentry->d_flags & DCACHE_NFSFS_RENAMED) 12871da177e4SLinus Torvalds goto zap; 12881da177e4SLinus Torvalds 12892b0143b5SDavid Howells if (d_really_is_positive(dentry) && 12902b0143b5SDavid Howells (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(d_inode(dentry))->flags) || 12912b0143b5SDavid Howells test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(d_inode(dentry))->flags))) 12921da177e4SLinus Torvalds goto zap; 12931da177e4SLinus Torvalds 12941da177e4SLinus Torvalds _leave(" = 0 [keep]"); 12951da177e4SLinus Torvalds return 0; 12961da177e4SLinus Torvalds 12971da177e4SLinus Torvalds zap: 12981da177e4SLinus Torvalds _leave(" = 1 [zap]"); 12991da177e4SLinus Torvalds return 1; 1300ec26815aSDavid Howells } 1301260a9803SDavid Howells 1302260a9803SDavid Howells /* 130379ddbfa5SDavid Howells * Clean up sillyrename files on dentry removal. 130479ddbfa5SDavid Howells */ 130579ddbfa5SDavid Howells static void afs_d_iput(struct dentry *dentry, struct inode *inode) 130679ddbfa5SDavid Howells { 130779ddbfa5SDavid Howells if (dentry->d_flags & DCACHE_NFSFS_RENAMED) 130879ddbfa5SDavid Howells afs_silly_iput(dentry, inode); 130979ddbfa5SDavid Howells iput(inode); 131079ddbfa5SDavid Howells } 131179ddbfa5SDavid Howells 131279ddbfa5SDavid Howells /* 1313260a9803SDavid Howells * handle dentry release 1314260a9803SDavid Howells */ 131566c7e1d3SDavid Howells void afs_d_release(struct dentry *dentry) 1316260a9803SDavid Howells { 1317a455589fSAl Viro _enter("%pd", dentry); 1318260a9803SDavid Howells } 1319260a9803SDavid Howells 1320728279a5SDavid Howells void afs_check_for_remote_deletion(struct afs_operation *op) 1321728279a5SDavid Howells { 1322728279a5SDavid Howells struct afs_vnode *vnode = op->file[0].vnode; 1323728279a5SDavid Howells 1324728279a5SDavid Howells switch (op->ac.abort_code) { 1325728279a5SDavid Howells case VNOVNODE: 1326728279a5SDavid Howells set_bit(AFS_VNODE_DELETED, &vnode->flags); 1327728279a5SDavid Howells afs_break_callback(vnode, afs_cb_break_for_deleted); 1328728279a5SDavid Howells } 1329728279a5SDavid Howells } 1330728279a5SDavid Howells 1331260a9803SDavid Howells /* 1332d2ddc776SDavid Howells * Create a new inode for create/mkdir/symlink 1333d2ddc776SDavid Howells */ 1334e49c7b2fSDavid Howells static void afs_vnode_new_inode(struct afs_operation *op) 1335d2ddc776SDavid Howells { 1336e49c7b2fSDavid Howells struct afs_vnode_param *vp = &op->file[1]; 13375a813276SDavid Howells struct afs_vnode *vnode; 1338d2ddc776SDavid Howells struct inode *inode; 1339d2ddc776SDavid Howells 1340e49c7b2fSDavid Howells _enter(""); 1341d2ddc776SDavid Howells 1342e49c7b2fSDavid Howells ASSERTCMP(op->error, ==, 0); 1343e49c7b2fSDavid Howells 1344e49c7b2fSDavid Howells inode = afs_iget(op, vp); 1345d2ddc776SDavid Howells if (IS_ERR(inode)) { 1346d2ddc776SDavid Howells /* ENOMEM or EINTR at a really inconvenient time - just abandon 1347d2ddc776SDavid Howells * the new directory on the server. 1348d2ddc776SDavid Howells */ 1349e49c7b2fSDavid Howells op->error = PTR_ERR(inode); 1350d2ddc776SDavid Howells return; 1351d2ddc776SDavid Howells } 1352d2ddc776SDavid Howells 13535a813276SDavid Howells vnode = AFS_FS_I(inode); 13545a813276SDavid Howells set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags); 1355e49c7b2fSDavid Howells if (!op->error) 1356e49c7b2fSDavid Howells afs_cache_permit(vnode, op->key, vnode->cb_break, &vp->scb); 1357e49c7b2fSDavid Howells d_instantiate(op->dentry, inode); 1358d2ddc776SDavid Howells } 1359d2ddc776SDavid Howells 1360e49c7b2fSDavid Howells static void afs_create_success(struct afs_operation *op) 1361b8359153SDavid Howells { 1362e49c7b2fSDavid Howells _enter("op=%08x", op->debug_id); 1363da8d0755SDavid Howells op->ctime = op->file[0].scb.status.mtime_client; 1364e49c7b2fSDavid Howells afs_vnode_commit_status(op, &op->file[0]); 1365e49c7b2fSDavid Howells afs_update_dentry_version(op, &op->file[0], op->dentry); 1366e49c7b2fSDavid Howells afs_vnode_new_inode(op); 1367b8359153SDavid Howells } 1368b8359153SDavid Howells 1369e49c7b2fSDavid Howells static void afs_create_edit_dir(struct afs_operation *op) 13709dd0b82eSDavid Howells { 1371e49c7b2fSDavid Howells struct afs_vnode_param *dvp = &op->file[0]; 1372e49c7b2fSDavid Howells struct afs_vnode_param *vp = &op->file[1]; 1373e49c7b2fSDavid Howells struct afs_vnode *dvnode = dvp->vnode; 1374e49c7b2fSDavid Howells 1375e49c7b2fSDavid Howells _enter("op=%08x", op->debug_id); 1376e49c7b2fSDavid Howells 1377e49c7b2fSDavid Howells down_write(&dvnode->validate_lock); 1378e49c7b2fSDavid Howells if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) && 1379e49c7b2fSDavid Howells dvnode->status.data_version == dvp->dv_before + dvp->dv_delta) 1380e49c7b2fSDavid Howells afs_edit_dir_add(dvnode, &op->dentry->d_name, &vp->fid, 1381e49c7b2fSDavid Howells op->create.reason); 1382e49c7b2fSDavid Howells up_write(&dvnode->validate_lock); 13839dd0b82eSDavid Howells } 13849dd0b82eSDavid Howells 1385e49c7b2fSDavid Howells static void afs_create_put(struct afs_operation *op) 1386e49c7b2fSDavid Howells { 1387e49c7b2fSDavid Howells _enter("op=%08x", op->debug_id); 1388e49c7b2fSDavid Howells 1389e49c7b2fSDavid Howells if (op->error) 1390e49c7b2fSDavid Howells d_drop(op->dentry); 1391e49c7b2fSDavid Howells } 1392e49c7b2fSDavid Howells 1393e49c7b2fSDavid Howells static const struct afs_operation_ops afs_mkdir_operation = { 1394e49c7b2fSDavid Howells .issue_afs_rpc = afs_fs_make_dir, 1395e49c7b2fSDavid Howells .issue_yfs_rpc = yfs_fs_make_dir, 1396e49c7b2fSDavid Howells .success = afs_create_success, 1397728279a5SDavid Howells .aborted = afs_check_for_remote_deletion, 1398e49c7b2fSDavid Howells .edit_dir = afs_create_edit_dir, 1399e49c7b2fSDavid Howells .put = afs_create_put, 1400e49c7b2fSDavid Howells }; 1401e49c7b2fSDavid Howells 14029dd0b82eSDavid Howells /* 1403260a9803SDavid Howells * create a directory on an AFS filesystem 1404260a9803SDavid Howells */ 1405549c7297SChristian Brauner static int afs_mkdir(struct user_namespace *mnt_userns, struct inode *dir, 1406549c7297SChristian Brauner struct dentry *dentry, umode_t mode) 1407260a9803SDavid Howells { 1408e49c7b2fSDavid Howells struct afs_operation *op; 1409d2ddc776SDavid Howells struct afs_vnode *dvnode = AFS_FS_I(dir); 1410260a9803SDavid Howells 14113b6492dfSDavid Howells _enter("{%llx:%llu},{%pd},%ho", 1412a455589fSAl Viro dvnode->fid.vid, dvnode->fid.vnode, dentry, mode); 1413260a9803SDavid Howells 1414e49c7b2fSDavid Howells op = afs_alloc_operation(NULL, dvnode->volume); 1415e49c7b2fSDavid Howells if (IS_ERR(op)) { 1416260a9803SDavid Howells d_drop(dentry); 1417e49c7b2fSDavid Howells return PTR_ERR(op); 1418e49c7b2fSDavid Howells } 1419e49c7b2fSDavid Howells 1420e49c7b2fSDavid Howells afs_op_set_vnode(op, 0, dvnode); 1421e49c7b2fSDavid Howells op->file[0].dv_delta = 1; 1422da8d0755SDavid Howells op->file[0].update_ctime = true; 1423e49c7b2fSDavid Howells op->dentry = dentry; 1424e49c7b2fSDavid Howells op->create.mode = S_IFDIR | mode; 1425e49c7b2fSDavid Howells op->create.reason = afs_edit_dir_for_mkdir; 1426e49c7b2fSDavid Howells op->ops = &afs_mkdir_operation; 1427e49c7b2fSDavid Howells return afs_do_sync_operation(op); 1428260a9803SDavid Howells } 1429260a9803SDavid Howells 1430260a9803SDavid Howells /* 1431d2ddc776SDavid Howells * Remove a subdir from a directory. 1432260a9803SDavid Howells */ 1433d2ddc776SDavid Howells static void afs_dir_remove_subdir(struct dentry *dentry) 1434260a9803SDavid Howells { 14352b0143b5SDavid Howells if (d_really_is_positive(dentry)) { 1436d2ddc776SDavid Howells struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry)); 1437d2ddc776SDavid Howells 1438260a9803SDavid Howells clear_nlink(&vnode->vfs_inode); 1439260a9803SDavid Howells set_bit(AFS_VNODE_DELETED, &vnode->flags); 1440c435ee34SDavid Howells clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags); 144163a4681fSDavid Howells clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags); 1442260a9803SDavid Howells } 1443260a9803SDavid Howells } 1444260a9803SDavid Howells 1445e49c7b2fSDavid Howells static void afs_rmdir_success(struct afs_operation *op) 1446e49c7b2fSDavid Howells { 1447e49c7b2fSDavid Howells _enter("op=%08x", op->debug_id); 1448da8d0755SDavid Howells op->ctime = op->file[0].scb.status.mtime_client; 1449e49c7b2fSDavid Howells afs_vnode_commit_status(op, &op->file[0]); 1450e49c7b2fSDavid Howells afs_update_dentry_version(op, &op->file[0], op->dentry); 1451e49c7b2fSDavid Howells } 1452e49c7b2fSDavid Howells 1453e49c7b2fSDavid Howells static void afs_rmdir_edit_dir(struct afs_operation *op) 1454e49c7b2fSDavid Howells { 1455e49c7b2fSDavid Howells struct afs_vnode_param *dvp = &op->file[0]; 1456e49c7b2fSDavid Howells struct afs_vnode *dvnode = dvp->vnode; 1457e49c7b2fSDavid Howells 1458e49c7b2fSDavid Howells _enter("op=%08x", op->debug_id); 1459e49c7b2fSDavid Howells afs_dir_remove_subdir(op->dentry); 1460e49c7b2fSDavid Howells 1461e49c7b2fSDavid Howells down_write(&dvnode->validate_lock); 1462e49c7b2fSDavid Howells if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) && 1463e49c7b2fSDavid Howells dvnode->status.data_version == dvp->dv_before + dvp->dv_delta) 1464e49c7b2fSDavid Howells afs_edit_dir_remove(dvnode, &op->dentry->d_name, 1465e49c7b2fSDavid Howells afs_edit_dir_for_rmdir); 1466e49c7b2fSDavid Howells up_write(&dvnode->validate_lock); 1467e49c7b2fSDavid Howells } 1468e49c7b2fSDavid Howells 1469e49c7b2fSDavid Howells static void afs_rmdir_put(struct afs_operation *op) 1470e49c7b2fSDavid Howells { 1471e49c7b2fSDavid Howells _enter("op=%08x", op->debug_id); 1472e49c7b2fSDavid Howells if (op->file[1].vnode) 1473e49c7b2fSDavid Howells up_write(&op->file[1].vnode->rmdir_lock); 1474e49c7b2fSDavid Howells } 1475e49c7b2fSDavid Howells 1476e49c7b2fSDavid Howells static const struct afs_operation_ops afs_rmdir_operation = { 1477e49c7b2fSDavid Howells .issue_afs_rpc = afs_fs_remove_dir, 1478e49c7b2fSDavid Howells .issue_yfs_rpc = yfs_fs_remove_dir, 1479e49c7b2fSDavid Howells .success = afs_rmdir_success, 1480728279a5SDavid Howells .aborted = afs_check_for_remote_deletion, 1481e49c7b2fSDavid Howells .edit_dir = afs_rmdir_edit_dir, 1482e49c7b2fSDavid Howells .put = afs_rmdir_put, 1483e49c7b2fSDavid Howells }; 1484e49c7b2fSDavid Howells 1485260a9803SDavid Howells /* 1486d2ddc776SDavid Howells * remove a directory from an AFS filesystem 1487260a9803SDavid Howells */ 1488d2ddc776SDavid Howells static int afs_rmdir(struct inode *dir, struct dentry *dentry) 1489260a9803SDavid Howells { 1490e49c7b2fSDavid Howells struct afs_operation *op; 1491f58db83fSDavid Howells struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode = NULL; 1492260a9803SDavid Howells int ret; 1493260a9803SDavid Howells 14943b6492dfSDavid Howells _enter("{%llx:%llu},{%pd}", 1495a455589fSAl Viro dvnode->fid.vid, dvnode->fid.vnode, dentry); 1496260a9803SDavid Howells 1497e49c7b2fSDavid Howells op = afs_alloc_operation(NULL, dvnode->volume); 1498e49c7b2fSDavid Howells if (IS_ERR(op)) 1499e49c7b2fSDavid Howells return PTR_ERR(op); 1500a58823acSDavid Howells 1501e49c7b2fSDavid Howells afs_op_set_vnode(op, 0, dvnode); 1502e49c7b2fSDavid Howells op->file[0].dv_delta = 1; 1503da8d0755SDavid Howells op->file[0].update_ctime = true; 1504e49c7b2fSDavid Howells 1505e49c7b2fSDavid Howells op->dentry = dentry; 1506e49c7b2fSDavid Howells op->ops = &afs_rmdir_operation; 1507260a9803SDavid Howells 1508f58db83fSDavid Howells /* Try to make sure we have a callback promise on the victim. */ 1509f58db83fSDavid Howells if (d_really_is_positive(dentry)) { 1510f58db83fSDavid Howells vnode = AFS_FS_I(d_inode(dentry)); 1511e49c7b2fSDavid Howells ret = afs_validate(vnode, op->key); 1512f58db83fSDavid Howells if (ret < 0) 1513e49c7b2fSDavid Howells goto error; 1514f58db83fSDavid Howells } 1515f58db83fSDavid Howells 151679ddbfa5SDavid Howells if (vnode) { 151779ddbfa5SDavid Howells ret = down_write_killable(&vnode->rmdir_lock); 151879ddbfa5SDavid Howells if (ret < 0) 1519e49c7b2fSDavid Howells goto error; 1520e49c7b2fSDavid Howells op->file[1].vnode = vnode; 152179ddbfa5SDavid Howells } 152279ddbfa5SDavid Howells 1523e49c7b2fSDavid Howells return afs_do_sync_operation(op); 1524a58823acSDavid Howells 1525d2ddc776SDavid Howells error: 1526e49c7b2fSDavid Howells return afs_put_operation(op); 1527d2ddc776SDavid Howells } 1528260a9803SDavid Howells 1529d2ddc776SDavid Howells /* 1530d2ddc776SDavid Howells * Remove a link to a file or symlink from a directory. 1531d2ddc776SDavid Howells * 1532d2ddc776SDavid Howells * If the file was not deleted due to excess hard links, the fileserver will 1533d2ddc776SDavid Howells * break the callback promise on the file - if it had one - before it returns 1534d2ddc776SDavid Howells * to us, and if it was deleted, it won't 1535d2ddc776SDavid Howells * 1536d2ddc776SDavid Howells * However, if we didn't have a callback promise outstanding, or it was 1537d2ddc776SDavid Howells * outstanding on a different server, then it won't break it either... 1538d2ddc776SDavid Howells */ 1539e49c7b2fSDavid Howells static void afs_dir_remove_link(struct afs_operation *op) 1540d2ddc776SDavid Howells { 1541e49c7b2fSDavid Howells struct afs_vnode *dvnode = op->file[0].vnode; 1542e49c7b2fSDavid Howells struct afs_vnode *vnode = op->file[1].vnode; 1543e49c7b2fSDavid Howells struct dentry *dentry = op->dentry; 1544e49c7b2fSDavid Howells int ret; 1545d2ddc776SDavid Howells 1546e49c7b2fSDavid Howells if (op->error != 0 || 1547e49c7b2fSDavid Howells (op->file[1].scb.have_status && op->file[1].scb.have_error)) 1548e49c7b2fSDavid Howells return; 1549e49c7b2fSDavid Howells if (d_really_is_positive(dentry)) 1550e49c7b2fSDavid Howells return; 1551d2ddc776SDavid Howells 155230062bd1SDavid Howells if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) { 155330062bd1SDavid Howells /* Already done */ 1554a38a7558SDavid Howells } else if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) { 1555a38a7558SDavid Howells write_seqlock(&vnode->cb_lock); 1556440fbc3aSDavid Howells drop_nlink(&vnode->vfs_inode); 1557440fbc3aSDavid Howells if (vnode->vfs_inode.i_nlink == 0) { 1558440fbc3aSDavid Howells set_bit(AFS_VNODE_DELETED, &vnode->flags); 1559051d2525SDavid Howells __afs_break_callback(vnode, afs_cb_break_for_unlink); 1560440fbc3aSDavid Howells } 1561a38a7558SDavid Howells write_sequnlock(&vnode->cb_lock); 1562440fbc3aSDavid Howells } else { 1563051d2525SDavid Howells afs_break_callback(vnode, afs_cb_break_for_unlink); 1564440fbc3aSDavid Howells 1565d2ddc776SDavid Howells if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) 1566e49c7b2fSDavid Howells _debug("AFS_VNODE_DELETED"); 1567d2ddc776SDavid Howells 1568e49c7b2fSDavid Howells ret = afs_validate(vnode, op->key); 1569e49c7b2fSDavid Howells if (ret != -ESTALE) 1570e49c7b2fSDavid Howells op->error = ret; 1571d2ddc776SDavid Howells } 1572d2ddc776SDavid Howells 1573e49c7b2fSDavid Howells _debug("nlink %d [val %d]", vnode->vfs_inode.i_nlink, op->error); 1574d2ddc776SDavid Howells } 1575d2ddc776SDavid Howells 1576e49c7b2fSDavid Howells static void afs_unlink_success(struct afs_operation *op) 1577e49c7b2fSDavid Howells { 1578e49c7b2fSDavid Howells _enter("op=%08x", op->debug_id); 1579da8d0755SDavid Howells op->ctime = op->file[0].scb.status.mtime_client; 1580b6489a49SDavid Howells afs_check_dir_conflict(op, &op->file[0]); 1581e49c7b2fSDavid Howells afs_vnode_commit_status(op, &op->file[0]); 1582e49c7b2fSDavid Howells afs_vnode_commit_status(op, &op->file[1]); 1583e49c7b2fSDavid Howells afs_update_dentry_version(op, &op->file[0], op->dentry); 1584e49c7b2fSDavid Howells afs_dir_remove_link(op); 1585e49c7b2fSDavid Howells } 1586e49c7b2fSDavid Howells 1587e49c7b2fSDavid Howells static void afs_unlink_edit_dir(struct afs_operation *op) 1588e49c7b2fSDavid Howells { 1589e49c7b2fSDavid Howells struct afs_vnode_param *dvp = &op->file[0]; 1590e49c7b2fSDavid Howells struct afs_vnode *dvnode = dvp->vnode; 1591e49c7b2fSDavid Howells 1592e49c7b2fSDavid Howells _enter("op=%08x", op->debug_id); 1593e49c7b2fSDavid Howells down_write(&dvnode->validate_lock); 1594e49c7b2fSDavid Howells if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags) && 1595e49c7b2fSDavid Howells dvnode->status.data_version == dvp->dv_before + dvp->dv_delta) 1596e49c7b2fSDavid Howells afs_edit_dir_remove(dvnode, &op->dentry->d_name, 1597e49c7b2fSDavid Howells afs_edit_dir_for_unlink); 1598e49c7b2fSDavid Howells up_write(&dvnode->validate_lock); 1599e49c7b2fSDavid Howells } 1600e49c7b2fSDavid Howells 1601e49c7b2fSDavid Howells static void afs_unlink_put(struct afs_operation *op) 1602e49c7b2fSDavid Howells { 1603e49c7b2fSDavid Howells _enter("op=%08x", op->debug_id); 1604e49c7b2fSDavid Howells if (op->unlink.need_rehash && op->error < 0 && op->error != -ENOENT) 1605e49c7b2fSDavid Howells d_rehash(op->dentry); 1606e49c7b2fSDavid Howells } 1607e49c7b2fSDavid Howells 1608e49c7b2fSDavid Howells static const struct afs_operation_ops afs_unlink_operation = { 1609e49c7b2fSDavid Howells .issue_afs_rpc = afs_fs_remove_file, 1610e49c7b2fSDavid Howells .issue_yfs_rpc = yfs_fs_remove_file, 1611e49c7b2fSDavid Howells .success = afs_unlink_success, 1612728279a5SDavid Howells .aborted = afs_check_for_remote_deletion, 1613e49c7b2fSDavid Howells .edit_dir = afs_unlink_edit_dir, 1614e49c7b2fSDavid Howells .put = afs_unlink_put, 1615e49c7b2fSDavid Howells }; 1616e49c7b2fSDavid Howells 1617d2ddc776SDavid Howells /* 1618d2ddc776SDavid Howells * Remove a file or symlink from an AFS filesystem. 1619d2ddc776SDavid Howells */ 1620d2ddc776SDavid Howells static int afs_unlink(struct inode *dir, struct dentry *dentry) 1621d2ddc776SDavid Howells { 1622e49c7b2fSDavid Howells struct afs_operation *op; 1623fa59f52fSDavid Howells struct afs_vnode *dvnode = AFS_FS_I(dir); 1624fa59f52fSDavid Howells struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry)); 1625d2ddc776SDavid Howells int ret; 1626d2ddc776SDavid Howells 16273b6492dfSDavid Howells _enter("{%llx:%llu},{%pd}", 1628d2ddc776SDavid Howells dvnode->fid.vid, dvnode->fid.vnode, dentry); 1629d2ddc776SDavid Howells 1630d2ddc776SDavid Howells if (dentry->d_name.len >= AFSNAMEMAX) 1631d2ddc776SDavid Howells return -ENAMETOOLONG; 1632d2ddc776SDavid Howells 1633e49c7b2fSDavid Howells op = afs_alloc_operation(NULL, dvnode->volume); 1634e49c7b2fSDavid Howells if (IS_ERR(op)) 1635e49c7b2fSDavid Howells return PTR_ERR(op); 1636a58823acSDavid Howells 1637e49c7b2fSDavid Howells afs_op_set_vnode(op, 0, dvnode); 1638e49c7b2fSDavid Howells op->file[0].dv_delta = 1; 1639da8d0755SDavid Howells op->file[0].update_ctime = true; 1640d2ddc776SDavid Howells 1641d2ddc776SDavid Howells /* Try to make sure we have a callback promise on the victim. */ 1642e49c7b2fSDavid Howells ret = afs_validate(vnode, op->key); 1643e49c7b2fSDavid Howells if (ret < 0) { 1644e49c7b2fSDavid Howells op->error = ret; 1645e49c7b2fSDavid Howells goto error; 1646e49c7b2fSDavid Howells } 1647d2ddc776SDavid Howells 164879ddbfa5SDavid Howells spin_lock(&dentry->d_lock); 1649fa59f52fSDavid Howells if (d_count(dentry) > 1) { 165079ddbfa5SDavid Howells spin_unlock(&dentry->d_lock); 165179ddbfa5SDavid Howells /* Start asynchronous writeout of the inode */ 165279ddbfa5SDavid Howells write_inode_now(d_inode(dentry), 0); 1653e49c7b2fSDavid Howells op->error = afs_sillyrename(dvnode, vnode, dentry, op->key); 1654e49c7b2fSDavid Howells goto error; 165579ddbfa5SDavid Howells } 165679ddbfa5SDavid Howells if (!d_unhashed(dentry)) { 165779ddbfa5SDavid Howells /* Prevent a race with RCU lookup. */ 165879ddbfa5SDavid Howells __d_drop(dentry); 1659e49c7b2fSDavid Howells op->unlink.need_rehash = true; 166079ddbfa5SDavid Howells } 166179ddbfa5SDavid Howells spin_unlock(&dentry->d_lock); 166279ddbfa5SDavid Howells 1663e49c7b2fSDavid Howells op->file[1].vnode = vnode; 1664da8d0755SDavid Howells op->file[1].update_ctime = true; 1665b6489a49SDavid Howells op->file[1].op_unlinked = true; 1666e49c7b2fSDavid Howells op->dentry = dentry; 1667e49c7b2fSDavid Howells op->ops = &afs_unlink_operation; 1668b6489a49SDavid Howells afs_begin_vnode_operation(op); 1669b6489a49SDavid Howells afs_wait_for_operation(op); 1670b6489a49SDavid Howells 1671b6489a49SDavid Howells /* If there was a conflict with a third party, check the status of the 1672b6489a49SDavid Howells * unlinked vnode. 1673b6489a49SDavid Howells */ 1674b6489a49SDavid Howells if (op->error == 0 && (op->flags & AFS_OPERATION_DIR_CONFLICT)) { 1675b6489a49SDavid Howells op->file[1].update_ctime = false; 1676b6489a49SDavid Howells op->fetch_status.which = 1; 1677b6489a49SDavid Howells op->ops = &afs_fetch_status_operation; 1678b6489a49SDavid Howells afs_begin_vnode_operation(op); 1679b6489a49SDavid Howells afs_wait_for_operation(op); 1680b6489a49SDavid Howells } 1681b6489a49SDavid Howells 1682b6489a49SDavid Howells return afs_put_operation(op); 1683a58823acSDavid Howells 1684260a9803SDavid Howells error: 1685e49c7b2fSDavid Howells return afs_put_operation(op); 1686260a9803SDavid Howells } 1687260a9803SDavid Howells 1688e49c7b2fSDavid Howells static const struct afs_operation_ops afs_create_operation = { 1689e49c7b2fSDavid Howells .issue_afs_rpc = afs_fs_create_file, 1690e49c7b2fSDavid Howells .issue_yfs_rpc = yfs_fs_create_file, 1691e49c7b2fSDavid Howells .success = afs_create_success, 1692728279a5SDavid Howells .aborted = afs_check_for_remote_deletion, 1693e49c7b2fSDavid Howells .edit_dir = afs_create_edit_dir, 1694e49c7b2fSDavid Howells .put = afs_create_put, 1695e49c7b2fSDavid Howells }; 1696e49c7b2fSDavid Howells 1697260a9803SDavid Howells /* 1698260a9803SDavid Howells * create a regular file on an AFS filesystem 1699260a9803SDavid Howells */ 1700549c7297SChristian Brauner static int afs_create(struct user_namespace *mnt_userns, struct inode *dir, 1701549c7297SChristian Brauner struct dentry *dentry, umode_t mode, bool excl) 1702260a9803SDavid Howells { 1703e49c7b2fSDavid Howells struct afs_operation *op; 170443dd388bSColin Ian King struct afs_vnode *dvnode = AFS_FS_I(dir); 1705e49c7b2fSDavid Howells int ret = -ENAMETOOLONG; 1706260a9803SDavid Howells 1707e49c7b2fSDavid Howells _enter("{%llx:%llu},{%pd},%ho", 1708a455589fSAl Viro dvnode->fid.vid, dvnode->fid.vnode, dentry, mode); 1709260a9803SDavid Howells 1710d2ddc776SDavid Howells if (dentry->d_name.len >= AFSNAMEMAX) 1711d2ddc776SDavid Howells goto error; 1712d2ddc776SDavid Howells 1713e49c7b2fSDavid Howells op = afs_alloc_operation(NULL, dvnode->volume); 1714e49c7b2fSDavid Howells if (IS_ERR(op)) { 1715e49c7b2fSDavid Howells ret = PTR_ERR(op); 1716260a9803SDavid Howells goto error; 1717260a9803SDavid Howells } 1718260a9803SDavid Howells 1719e49c7b2fSDavid Howells afs_op_set_vnode(op, 0, dvnode); 1720e49c7b2fSDavid Howells op->file[0].dv_delta = 1; 1721da8d0755SDavid Howells op->file[0].update_ctime = true; 1722a58823acSDavid Howells 1723e49c7b2fSDavid Howells op->dentry = dentry; 1724e49c7b2fSDavid Howells op->create.mode = S_IFREG | mode; 1725e49c7b2fSDavid Howells op->create.reason = afs_edit_dir_for_create; 1726e49c7b2fSDavid Howells op->ops = &afs_create_operation; 1727e49c7b2fSDavid Howells return afs_do_sync_operation(op); 1728a58823acSDavid Howells 1729260a9803SDavid Howells error: 1730260a9803SDavid Howells d_drop(dentry); 1731260a9803SDavid Howells _leave(" = %d", ret); 1732260a9803SDavid Howells return ret; 1733260a9803SDavid Howells } 1734260a9803SDavid Howells 1735e49c7b2fSDavid Howells static void afs_link_success(struct afs_operation *op) 1736e49c7b2fSDavid Howells { 1737e49c7b2fSDavid Howells struct afs_vnode_param *dvp = &op->file[0]; 1738e49c7b2fSDavid Howells struct afs_vnode_param *vp = &op->file[1]; 1739e49c7b2fSDavid Howells 1740e49c7b2fSDavid Howells _enter("op=%08x", op->debug_id); 1741da8d0755SDavid Howells op->ctime = dvp->scb.status.mtime_client; 1742e49c7b2fSDavid Howells afs_vnode_commit_status(op, dvp); 1743e49c7b2fSDavid Howells afs_vnode_commit_status(op, vp); 1744e49c7b2fSDavid Howells afs_update_dentry_version(op, dvp, op->dentry); 1745e49c7b2fSDavid Howells if (op->dentry_2->d_parent == op->dentry->d_parent) 1746e49c7b2fSDavid Howells afs_update_dentry_version(op, dvp, op->dentry_2); 1747e49c7b2fSDavid Howells ihold(&vp->vnode->vfs_inode); 1748e49c7b2fSDavid Howells d_instantiate(op->dentry, &vp->vnode->vfs_inode); 1749e49c7b2fSDavid Howells } 1750e49c7b2fSDavid Howells 1751e49c7b2fSDavid Howells static void afs_link_put(struct afs_operation *op) 1752e49c7b2fSDavid Howells { 1753e49c7b2fSDavid Howells _enter("op=%08x", op->debug_id); 1754e49c7b2fSDavid Howells if (op->error) 1755e49c7b2fSDavid Howells d_drop(op->dentry); 1756e49c7b2fSDavid Howells } 1757e49c7b2fSDavid Howells 1758e49c7b2fSDavid Howells static const struct afs_operation_ops afs_link_operation = { 1759e49c7b2fSDavid Howells .issue_afs_rpc = afs_fs_link, 1760e49c7b2fSDavid Howells .issue_yfs_rpc = yfs_fs_link, 1761e49c7b2fSDavid Howells .success = afs_link_success, 1762728279a5SDavid Howells .aborted = afs_check_for_remote_deletion, 1763e49c7b2fSDavid Howells .edit_dir = afs_create_edit_dir, 1764e49c7b2fSDavid Howells .put = afs_link_put, 1765e49c7b2fSDavid Howells }; 1766e49c7b2fSDavid Howells 1767260a9803SDavid Howells /* 1768260a9803SDavid Howells * create a hard link between files in an AFS filesystem 1769260a9803SDavid Howells */ 1770260a9803SDavid Howells static int afs_link(struct dentry *from, struct inode *dir, 1771260a9803SDavid Howells struct dentry *dentry) 1772260a9803SDavid Howells { 1773e49c7b2fSDavid Howells struct afs_operation *op; 1774a58823acSDavid Howells struct afs_vnode *dvnode = AFS_FS_I(dir); 1775a58823acSDavid Howells struct afs_vnode *vnode = AFS_FS_I(d_inode(from)); 1776e49c7b2fSDavid Howells int ret = -ENAMETOOLONG; 1777260a9803SDavid Howells 17783b6492dfSDavid Howells _enter("{%llx:%llu},{%llx:%llu},{%pd}", 1779260a9803SDavid Howells vnode->fid.vid, vnode->fid.vnode, 1780260a9803SDavid Howells dvnode->fid.vid, dvnode->fid.vnode, 1781a455589fSAl Viro dentry); 1782260a9803SDavid Howells 1783d2ddc776SDavid Howells if (dentry->d_name.len >= AFSNAMEMAX) 1784d2ddc776SDavid Howells goto error; 1785d2ddc776SDavid Howells 1786e49c7b2fSDavid Howells op = afs_alloc_operation(NULL, dvnode->volume); 1787e49c7b2fSDavid Howells if (IS_ERR(op)) { 1788e49c7b2fSDavid Howells ret = PTR_ERR(op); 1789a58823acSDavid Howells goto error; 1790260a9803SDavid Howells } 1791260a9803SDavid Howells 1792e49c7b2fSDavid Howells afs_op_set_vnode(op, 0, dvnode); 1793e49c7b2fSDavid Howells afs_op_set_vnode(op, 1, vnode); 1794e49c7b2fSDavid Howells op->file[0].dv_delta = 1; 1795da8d0755SDavid Howells op->file[0].update_ctime = true; 1796da8d0755SDavid Howells op->file[1].update_ctime = true; 1797a58823acSDavid Howells 1798e49c7b2fSDavid Howells op->dentry = dentry; 1799e49c7b2fSDavid Howells op->dentry_2 = from; 1800e49c7b2fSDavid Howells op->ops = &afs_link_operation; 1801e49c7b2fSDavid Howells op->create.reason = afs_edit_dir_for_link; 1802e49c7b2fSDavid Howells return afs_do_sync_operation(op); 1803260a9803SDavid Howells 1804260a9803SDavid Howells error: 1805260a9803SDavid Howells d_drop(dentry); 1806260a9803SDavid Howells _leave(" = %d", ret); 1807260a9803SDavid Howells return ret; 1808260a9803SDavid Howells } 1809260a9803SDavid Howells 1810e49c7b2fSDavid Howells static const struct afs_operation_ops afs_symlink_operation = { 1811e49c7b2fSDavid Howells .issue_afs_rpc = afs_fs_symlink, 1812e49c7b2fSDavid Howells .issue_yfs_rpc = yfs_fs_symlink, 1813e49c7b2fSDavid Howells .success = afs_create_success, 1814728279a5SDavid Howells .aborted = afs_check_for_remote_deletion, 1815e49c7b2fSDavid Howells .edit_dir = afs_create_edit_dir, 1816e49c7b2fSDavid Howells .put = afs_create_put, 1817e49c7b2fSDavid Howells }; 1818e49c7b2fSDavid Howells 1819260a9803SDavid Howells /* 1820260a9803SDavid Howells * create a symlink in an AFS filesystem 1821260a9803SDavid Howells */ 1822549c7297SChristian Brauner static int afs_symlink(struct user_namespace *mnt_userns, struct inode *dir, 1823549c7297SChristian Brauner struct dentry *dentry, const char *content) 1824260a9803SDavid Howells { 1825e49c7b2fSDavid Howells struct afs_operation *op; 1826d2ddc776SDavid Howells struct afs_vnode *dvnode = AFS_FS_I(dir); 1827260a9803SDavid Howells int ret; 1828260a9803SDavid Howells 18293b6492dfSDavid Howells _enter("{%llx:%llu},{%pd},%s", 1830a455589fSAl Viro dvnode->fid.vid, dvnode->fid.vnode, dentry, 1831260a9803SDavid Howells content); 1832260a9803SDavid Howells 1833d2ddc776SDavid Howells ret = -ENAMETOOLONG; 1834d2ddc776SDavid Howells if (dentry->d_name.len >= AFSNAMEMAX) 1835d2ddc776SDavid Howells goto error; 1836d2ddc776SDavid Howells 1837260a9803SDavid Howells ret = -EINVAL; 183845222b9eSDavid Howells if (strlen(content) >= AFSPATHMAX) 1839260a9803SDavid Howells goto error; 1840260a9803SDavid Howells 1841e49c7b2fSDavid Howells op = afs_alloc_operation(NULL, dvnode->volume); 1842e49c7b2fSDavid Howells if (IS_ERR(op)) { 1843e49c7b2fSDavid Howells ret = PTR_ERR(op); 1844a58823acSDavid Howells goto error; 1845260a9803SDavid Howells } 1846260a9803SDavid Howells 1847e49c7b2fSDavid Howells afs_op_set_vnode(op, 0, dvnode); 1848e49c7b2fSDavid Howells op->file[0].dv_delta = 1; 1849a58823acSDavid Howells 1850e49c7b2fSDavid Howells op->dentry = dentry; 1851e49c7b2fSDavid Howells op->ops = &afs_symlink_operation; 1852e49c7b2fSDavid Howells op->create.reason = afs_edit_dir_for_symlink; 1853e49c7b2fSDavid Howells op->create.symlink = content; 1854e49c7b2fSDavid Howells return afs_do_sync_operation(op); 1855d2ddc776SDavid Howells 1856260a9803SDavid Howells error: 1857260a9803SDavid Howells d_drop(dentry); 1858260a9803SDavid Howells _leave(" = %d", ret); 1859260a9803SDavid Howells return ret; 1860260a9803SDavid Howells } 1861260a9803SDavid Howells 1862e49c7b2fSDavid Howells static void afs_rename_success(struct afs_operation *op) 1863260a9803SDavid Howells { 1864e49c7b2fSDavid Howells _enter("op=%08x", op->debug_id); 1865e49c7b2fSDavid Howells 1866da8d0755SDavid Howells op->ctime = op->file[0].scb.status.mtime_client; 1867b6489a49SDavid Howells afs_check_dir_conflict(op, &op->file[1]); 1868e49c7b2fSDavid Howells afs_vnode_commit_status(op, &op->file[0]); 1869da8d0755SDavid Howells if (op->file[1].vnode != op->file[0].vnode) { 1870da8d0755SDavid Howells op->ctime = op->file[1].scb.status.mtime_client; 1871e49c7b2fSDavid Howells afs_vnode_commit_status(op, &op->file[1]); 1872e49c7b2fSDavid Howells } 1873da8d0755SDavid Howells } 1874e49c7b2fSDavid Howells 1875e49c7b2fSDavid Howells static void afs_rename_edit_dir(struct afs_operation *op) 1876e49c7b2fSDavid Howells { 1877e49c7b2fSDavid Howells struct afs_vnode_param *orig_dvp = &op->file[0]; 1878e49c7b2fSDavid Howells struct afs_vnode_param *new_dvp = &op->file[1]; 1879e49c7b2fSDavid Howells struct afs_vnode *orig_dvnode = orig_dvp->vnode; 1880e49c7b2fSDavid Howells struct afs_vnode *new_dvnode = new_dvp->vnode; 1881e49c7b2fSDavid Howells struct afs_vnode *vnode = AFS_FS_I(d_inode(op->dentry)); 1882e49c7b2fSDavid Howells struct dentry *old_dentry = op->dentry; 1883e49c7b2fSDavid Howells struct dentry *new_dentry = op->dentry_2; 188479ddbfa5SDavid Howells struct inode *new_inode; 1885260a9803SDavid Howells 1886e49c7b2fSDavid Howells _enter("op=%08x", op->debug_id); 18871cd66c93SMiklos Szeredi 1888e49c7b2fSDavid Howells if (op->rename.rehash) { 1889e49c7b2fSDavid Howells d_rehash(op->rename.rehash); 1890e49c7b2fSDavid Howells op->rename.rehash = NULL; 1891260a9803SDavid Howells } 1892260a9803SDavid Howells 18932105c282SDavid Howells down_write(&orig_dvnode->validate_lock); 18942105c282SDavid Howells if (test_bit(AFS_VNODE_DIR_VALID, &orig_dvnode->flags) && 1895e49c7b2fSDavid Howells orig_dvnode->status.data_version == orig_dvp->dv_before + orig_dvp->dv_delta) 189663a4681fSDavid Howells afs_edit_dir_remove(orig_dvnode, &old_dentry->d_name, 189779ddbfa5SDavid Howells afs_edit_dir_for_rename_0); 189863a4681fSDavid Howells 1899e49c7b2fSDavid Howells if (new_dvnode != orig_dvnode) { 1900e49c7b2fSDavid Howells up_write(&orig_dvnode->validate_lock); 19012105c282SDavid Howells down_write(&new_dvnode->validate_lock); 19022105c282SDavid Howells } 1903e49c7b2fSDavid Howells 19042105c282SDavid Howells if (test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags) && 1905e49c7b2fSDavid Howells new_dvnode->status.data_version == new_dvp->dv_before + new_dvp->dv_delta) { 1906e49c7b2fSDavid Howells if (!op->rename.new_negative) 190763a4681fSDavid Howells afs_edit_dir_remove(new_dvnode, &new_dentry->d_name, 190879ddbfa5SDavid Howells afs_edit_dir_for_rename_1); 190963a4681fSDavid Howells 191063a4681fSDavid Howells afs_edit_dir_add(new_dvnode, &new_dentry->d_name, 191179ddbfa5SDavid Howells &vnode->fid, afs_edit_dir_for_rename_2); 19122105c282SDavid Howells } 191379ddbfa5SDavid Howells 191479ddbfa5SDavid Howells new_inode = d_inode(new_dentry); 191579ddbfa5SDavid Howells if (new_inode) { 191679ddbfa5SDavid Howells spin_lock(&new_inode->i_lock); 191779ddbfa5SDavid Howells if (new_inode->i_nlink > 0) 191879ddbfa5SDavid Howells drop_nlink(new_inode); 191979ddbfa5SDavid Howells spin_unlock(&new_inode->i_lock); 192079ddbfa5SDavid Howells } 19219dd0b82eSDavid Howells 19229dd0b82eSDavid Howells /* Now we can update d_fsdata on the dentries to reflect their 19239dd0b82eSDavid Howells * new parent's data_version. 19249dd0b82eSDavid Howells * 19259dd0b82eSDavid Howells * Note that if we ever implement RENAME_EXCHANGE, we'll have 19269dd0b82eSDavid Howells * to update both dentries with opposing dir versions. 19279dd0b82eSDavid Howells */ 1928e49c7b2fSDavid Howells afs_update_dentry_version(op, new_dvp, op->dentry); 1929e49c7b2fSDavid Howells afs_update_dentry_version(op, new_dvp, op->dentry_2); 1930e49c7b2fSDavid Howells 193179ddbfa5SDavid Howells d_move(old_dentry, new_dentry); 1932e49c7b2fSDavid Howells 19332105c282SDavid Howells up_write(&new_dvnode->validate_lock); 193463a4681fSDavid Howells } 193563a4681fSDavid Howells 1936e49c7b2fSDavid Howells static void afs_rename_put(struct afs_operation *op) 1937e49c7b2fSDavid Howells { 1938e49c7b2fSDavid Howells _enter("op=%08x", op->debug_id); 1939e49c7b2fSDavid Howells if (op->rename.rehash) 1940e49c7b2fSDavid Howells d_rehash(op->rename.rehash); 1941e49c7b2fSDavid Howells dput(op->rename.tmp); 1942e49c7b2fSDavid Howells if (op->error) 1943e49c7b2fSDavid Howells d_rehash(op->dentry); 1944e49c7b2fSDavid Howells } 1945e49c7b2fSDavid Howells 1946e49c7b2fSDavid Howells static const struct afs_operation_ops afs_rename_operation = { 1947e49c7b2fSDavid Howells .issue_afs_rpc = afs_fs_rename, 1948e49c7b2fSDavid Howells .issue_yfs_rpc = yfs_fs_rename, 1949e49c7b2fSDavid Howells .success = afs_rename_success, 1950e49c7b2fSDavid Howells .edit_dir = afs_rename_edit_dir, 1951e49c7b2fSDavid Howells .put = afs_rename_put, 1952e49c7b2fSDavid Howells }; 1953e49c7b2fSDavid Howells 1954e49c7b2fSDavid Howells /* 1955e49c7b2fSDavid Howells * rename a file in an AFS filesystem and/or move it between directories 1956e49c7b2fSDavid Howells */ 1957549c7297SChristian Brauner static int afs_rename(struct user_namespace *mnt_userns, struct inode *old_dir, 1958549c7297SChristian Brauner struct dentry *old_dentry, struct inode *new_dir, 1959549c7297SChristian Brauner struct dentry *new_dentry, unsigned int flags) 1960e49c7b2fSDavid Howells { 1961e49c7b2fSDavid Howells struct afs_operation *op; 1962e49c7b2fSDavid Howells struct afs_vnode *orig_dvnode, *new_dvnode, *vnode; 1963e49c7b2fSDavid Howells int ret; 1964e49c7b2fSDavid Howells 1965e49c7b2fSDavid Howells if (flags) 1966e49c7b2fSDavid Howells return -EINVAL; 1967e49c7b2fSDavid Howells 1968e49c7b2fSDavid Howells /* Don't allow silly-rename files be moved around. */ 1969e49c7b2fSDavid Howells if (old_dentry->d_flags & DCACHE_NFSFS_RENAMED) 1970e49c7b2fSDavid Howells return -EINVAL; 1971e49c7b2fSDavid Howells 1972e49c7b2fSDavid Howells vnode = AFS_FS_I(d_inode(old_dentry)); 1973e49c7b2fSDavid Howells orig_dvnode = AFS_FS_I(old_dir); 1974e49c7b2fSDavid Howells new_dvnode = AFS_FS_I(new_dir); 1975e49c7b2fSDavid Howells 1976e49c7b2fSDavid Howells _enter("{%llx:%llu},{%llx:%llu},{%llx:%llu},{%pd}", 1977e49c7b2fSDavid Howells orig_dvnode->fid.vid, orig_dvnode->fid.vnode, 1978e49c7b2fSDavid Howells vnode->fid.vid, vnode->fid.vnode, 1979e49c7b2fSDavid Howells new_dvnode->fid.vid, new_dvnode->fid.vnode, 1980e49c7b2fSDavid Howells new_dentry); 1981e49c7b2fSDavid Howells 1982e49c7b2fSDavid Howells op = afs_alloc_operation(NULL, orig_dvnode->volume); 1983e49c7b2fSDavid Howells if (IS_ERR(op)) 1984e49c7b2fSDavid Howells return PTR_ERR(op); 1985e49c7b2fSDavid Howells 1986e49c7b2fSDavid Howells afs_op_set_vnode(op, 0, orig_dvnode); 1987e49c7b2fSDavid Howells afs_op_set_vnode(op, 1, new_dvnode); /* May be same as orig_dvnode */ 1988e49c7b2fSDavid Howells op->file[0].dv_delta = 1; 1989e49c7b2fSDavid Howells op->file[1].dv_delta = 1; 1990da8d0755SDavid Howells op->file[0].update_ctime = true; 1991da8d0755SDavid Howells op->file[1].update_ctime = true; 1992e49c7b2fSDavid Howells 1993e49c7b2fSDavid Howells op->dentry = old_dentry; 1994e49c7b2fSDavid Howells op->dentry_2 = new_dentry; 1995e49c7b2fSDavid Howells op->rename.new_negative = d_is_negative(new_dentry); 1996e49c7b2fSDavid Howells op->ops = &afs_rename_operation; 1997e49c7b2fSDavid Howells 1998e49c7b2fSDavid Howells /* For non-directories, check whether the target is busy and if so, 1999e49c7b2fSDavid Howells * make a copy of the dentry and then do a silly-rename. If the 2000e49c7b2fSDavid Howells * silly-rename succeeds, the copied dentry is hashed and becomes the 2001e49c7b2fSDavid Howells * new target. 2002e49c7b2fSDavid Howells */ 2003e49c7b2fSDavid Howells if (d_is_positive(new_dentry) && !d_is_dir(new_dentry)) { 2004e49c7b2fSDavid Howells /* To prevent any new references to the target during the 2005e49c7b2fSDavid Howells * rename, we unhash the dentry in advance. 2006e49c7b2fSDavid Howells */ 2007e49c7b2fSDavid Howells if (!d_unhashed(new_dentry)) { 2008e49c7b2fSDavid Howells d_drop(new_dentry); 2009e49c7b2fSDavid Howells op->rename.rehash = new_dentry; 2010e49c7b2fSDavid Howells } 2011e49c7b2fSDavid Howells 2012e49c7b2fSDavid Howells if (d_count(new_dentry) > 2) { 2013e49c7b2fSDavid Howells /* copy the target dentry's name */ 2014e49c7b2fSDavid Howells ret = -ENOMEM; 2015e49c7b2fSDavid Howells op->rename.tmp = d_alloc(new_dentry->d_parent, 2016e49c7b2fSDavid Howells &new_dentry->d_name); 2017e49c7b2fSDavid Howells if (!op->rename.tmp) 2018e49c7b2fSDavid Howells goto error; 2019e49c7b2fSDavid Howells 2020e49c7b2fSDavid Howells ret = afs_sillyrename(new_dvnode, 2021e49c7b2fSDavid Howells AFS_FS_I(d_inode(new_dentry)), 2022e49c7b2fSDavid Howells new_dentry, op->key); 2023e49c7b2fSDavid Howells if (ret) 2024e49c7b2fSDavid Howells goto error; 2025e49c7b2fSDavid Howells 2026e49c7b2fSDavid Howells op->dentry_2 = op->rename.tmp; 2027e49c7b2fSDavid Howells op->rename.rehash = NULL; 2028e49c7b2fSDavid Howells op->rename.new_negative = true; 2029e49c7b2fSDavid Howells } 2030e49c7b2fSDavid Howells } 2031e49c7b2fSDavid Howells 2032e49c7b2fSDavid Howells /* This bit is potentially nasty as there's a potential race with 2033e49c7b2fSDavid Howells * afs_d_revalidate{,_rcu}(). We have to change d_fsdata on the dentry 2034e49c7b2fSDavid Howells * to reflect it's new parent's new data_version after the op, but 2035e49c7b2fSDavid Howells * d_revalidate may see old_dentry between the op having taken place 2036e49c7b2fSDavid Howells * and the version being updated. 2037e49c7b2fSDavid Howells * 2038e49c7b2fSDavid Howells * So drop the old_dentry for now to make other threads go through 2039e49c7b2fSDavid Howells * lookup instead - which we hold a lock against. 2040e49c7b2fSDavid Howells */ 2041e49c7b2fSDavid Howells d_drop(old_dentry); 2042e49c7b2fSDavid Howells 2043e49c7b2fSDavid Howells return afs_do_sync_operation(op); 2044e49c7b2fSDavid Howells 2045260a9803SDavid Howells error: 2046e49c7b2fSDavid Howells return afs_put_operation(op); 2047260a9803SDavid Howells } 2048f3ddee8dSDavid Howells 2049f3ddee8dSDavid Howells /* 2050f3ddee8dSDavid Howells * Release a directory page and clean up its private state if it's not busy 2051f3ddee8dSDavid Howells * - return true if the page can now be released, false if not 2052f3ddee8dSDavid Howells */ 2053f3ddee8dSDavid Howells static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags) 2054f3ddee8dSDavid Howells { 2055f3ddee8dSDavid Howells struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host); 2056f3ddee8dSDavid Howells 20573b6492dfSDavid Howells _enter("{{%llx:%llu}[%lu]}", dvnode->fid.vid, dvnode->fid.vnode, page->index); 2058f3ddee8dSDavid Howells 2059fa04a40bSDavid Howells detach_page_private(page); 2060f3ddee8dSDavid Howells 2061f3ddee8dSDavid Howells /* The directory will need reloading. */ 2062f3ddee8dSDavid Howells if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) 2063f3ddee8dSDavid Howells afs_stat_v(dvnode, n_relpg); 2064f3ddee8dSDavid Howells return 1; 2065f3ddee8dSDavid Howells } 2066f3ddee8dSDavid Howells 2067f3ddee8dSDavid Howells /* 2068f3ddee8dSDavid Howells * invalidate part or all of a page 2069f3ddee8dSDavid Howells * - release a page and clean up its private data if offset is 0 (indicating 2070f3ddee8dSDavid Howells * the entire page) 2071f3ddee8dSDavid Howells */ 2072f3ddee8dSDavid Howells static void afs_dir_invalidatepage(struct page *page, unsigned int offset, 2073f3ddee8dSDavid Howells unsigned int length) 2074f3ddee8dSDavid Howells { 2075f3ddee8dSDavid Howells struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host); 2076f3ddee8dSDavid Howells 2077f3ddee8dSDavid Howells _enter("{%lu},%u,%u", page->index, offset, length); 2078f3ddee8dSDavid Howells 2079f3ddee8dSDavid Howells BUG_ON(!PageLocked(page)); 2080f3ddee8dSDavid Howells 2081f3ddee8dSDavid Howells /* The directory will need reloading. */ 2082f3ddee8dSDavid Howells if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) 2083f3ddee8dSDavid Howells afs_stat_v(dvnode, n_inval); 2084f3ddee8dSDavid Howells 2085f3ddee8dSDavid Howells /* we clean up only if the entire page is being invalidated */ 2086fa04a40bSDavid Howells if (offset == 0 && length == PAGE_SIZE) 2087fa04a40bSDavid Howells detach_page_private(page); 2088f3ddee8dSDavid Howells } 2089