Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* dir.c: AFS filesystem directory handling |
| 3 | * |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 4 | * Copyright (C) 2002, 2018 Red Hat, Inc. All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * Written by David Howells (dhowells@redhat.com) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/fs.h> |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 10 | #include <linux/namei.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/pagemap.h> |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 12 | #include <linux/swap.h> |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 13 | #include <linux/ctype.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 14 | #include <linux/sched.h> |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 15 | #include <linux/task_io_accounting_ops.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include "internal.h" |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 17 | #include "afs_fs.h" |
David Howells | 4ea219a | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 18 | #include "xdr_fs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 20 | static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 21 | unsigned int flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | static int afs_dir_open(struct inode *inode, struct file *file); |
Al Viro | 1bbae9f | 2013-05-22 16:31:14 -0400 | [diff] [blame] | 23 | static int afs_readdir(struct file *file, struct dir_context *ctx); |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 24 | static int afs_d_revalidate(struct dentry *dentry, unsigned int flags); |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 25 | static int afs_d_delete(const struct dentry *dentry); |
David Howells | 79ddbfa | 2019-04-25 14:26:51 +0100 | [diff] [blame] | 26 | static void afs_d_iput(struct dentry *dentry, struct inode *inode); |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 27 | static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name, int nlen, |
David Howells | afefdbb | 2006-10-03 01:13:46 -0700 | [diff] [blame] | 28 | loff_t fpos, u64 ino, unsigned dtype); |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 29 | static int afs_lookup_filldir(struct dir_context *ctx, const char *name, int nlen, |
| 30 | loff_t fpos, u64 ino, unsigned dtype); |
Al Viro | 4acdaf2 | 2011-07-26 01:42:34 -0400 | [diff] [blame] | 31 | static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode, |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 32 | bool excl); |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 33 | static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 34 | static int afs_rmdir(struct inode *dir, struct dentry *dentry); |
| 35 | static int afs_unlink(struct inode *dir, struct dentry *dentry); |
| 36 | static int afs_link(struct dentry *from, struct inode *dir, |
| 37 | struct dentry *dentry); |
| 38 | static int afs_symlink(struct inode *dir, struct dentry *dentry, |
| 39 | const char *content); |
| 40 | static int afs_rename(struct inode *old_dir, struct dentry *old_dentry, |
Miklos Szeredi | 1cd66c9 | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 41 | struct inode *new_dir, struct dentry *new_dentry, |
| 42 | unsigned int flags); |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 43 | static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags); |
| 44 | static void afs_dir_invalidatepage(struct page *page, unsigned int offset, |
| 45 | unsigned int length); |
| 46 | |
| 47 | static int afs_dir_set_page_dirty(struct page *page) |
| 48 | { |
| 49 | BUG(); /* This should never happen. */ |
| 50 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 52 | const struct file_operations afs_dir_file_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | .open = afs_dir_open, |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 54 | .release = afs_release, |
Al Viro | 29884ef | 2016-05-10 14:27:44 -0400 | [diff] [blame] | 55 | .iterate_shared = afs_readdir, |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 56 | .lock = afs_lock, |
Christoph Hellwig | 3222a3e | 2008-09-03 21:53:01 +0200 | [diff] [blame] | 57 | .llseek = generic_file_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | }; |
| 59 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 60 | const struct inode_operations afs_dir_inode_operations = { |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 61 | .create = afs_create, |
| 62 | .lookup = afs_lookup, |
| 63 | .link = afs_link, |
| 64 | .unlink = afs_unlink, |
| 65 | .symlink = afs_symlink, |
| 66 | .mkdir = afs_mkdir, |
| 67 | .rmdir = afs_rmdir, |
Miklos Szeredi | 2773bf0 | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 68 | .rename = afs_rename, |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 69 | .permission = afs_permission, |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 70 | .getattr = afs_getattr, |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 71 | .setattr = afs_setattr, |
David Howells | d3e3b7ea | 2017-07-06 15:50:27 +0100 | [diff] [blame] | 72 | .listxattr = afs_listxattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | }; |
| 74 | |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 75 | const struct address_space_operations afs_dir_aops = { |
| 76 | .set_page_dirty = afs_dir_set_page_dirty, |
| 77 | .releasepage = afs_dir_releasepage, |
| 78 | .invalidatepage = afs_dir_invalidatepage, |
| 79 | }; |
| 80 | |
Al Viro | d61dcce | 2011-01-12 20:04:20 -0500 | [diff] [blame] | 81 | const struct dentry_operations afs_fs_dentry_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | .d_revalidate = afs_d_revalidate, |
| 83 | .d_delete = afs_d_delete, |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 84 | .d_release = afs_d_release, |
David Howells | d18610b | 2011-01-14 19:04:05 +0000 | [diff] [blame] | 85 | .d_automount = afs_d_automount, |
David Howells | 79ddbfa | 2019-04-25 14:26:51 +0100 | [diff] [blame] | 86 | .d_iput = afs_d_iput, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | }; |
| 88 | |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 89 | struct afs_lookup_one_cookie { |
| 90 | struct dir_context ctx; |
| 91 | struct qstr name; |
| 92 | bool found; |
| 93 | struct afs_fid fid; |
| 94 | }; |
| 95 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 96 | struct afs_lookup_cookie { |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 97 | struct dir_context ctx; |
| 98 | struct qstr name; |
| 99 | bool found; |
| 100 | bool one_only; |
| 101 | unsigned short nr_fids; |
David Howells | 39db981 | 2019-05-14 12:33:10 +0100 | [diff] [blame] | 102 | struct inode **inodes; |
David Howells | 8718275 | 2019-05-09 16:17:05 +0100 | [diff] [blame] | 103 | struct afs_status_cb *statuses; |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 104 | struct afs_fid fids[50]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | /* |
| 108 | * check that a directory page is valid |
| 109 | */ |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 110 | static bool afs_dir_check_page(struct afs_vnode *dvnode, struct page *page, |
| 111 | loff_t i_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | { |
David Howells | 0031763 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 113 | struct afs_xdr_dir_page *dbuf; |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 114 | loff_t latter, off; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | int tmp, qty; |
| 116 | |
David Howells | dab17c1 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 117 | /* Determine how many magic numbers there should be in this page, but |
| 118 | * we must take care because the directory may change size under us. |
| 119 | */ |
| 120 | off = page_offset(page); |
David Howells | dab17c1 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 121 | if (i_size <= off) |
| 122 | goto checked; |
| 123 | |
| 124 | latter = i_size - off; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | if (latter >= PAGE_SIZE) |
| 126 | qty = PAGE_SIZE; |
| 127 | else |
| 128 | qty = latter; |
David Howells | 0031763 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 129 | qty /= sizeof(union afs_xdr_dir_block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
| 131 | /* check them */ |
David Howells | 63a4681 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 132 | dbuf = kmap(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | for (tmp = 0; tmp < qty; tmp++) { |
David Howells | 0031763 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 134 | if (dbuf->blocks[tmp].hdr.magic != AFS_DIR_MAGIC) { |
David Howells | dab17c1 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 135 | printk("kAFS: %s(%lx): bad magic %d/%d is %04hx\n", |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 136 | __func__, dvnode->vfs_inode.i_ino, tmp, qty, |
David Howells | 0031763 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 137 | ntohs(dbuf->blocks[tmp].hdr.magic)); |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 138 | trace_afs_dir_check_failed(dvnode, off, i_size); |
David Howells | 63a4681 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 139 | kunmap(page); |
David Howells | f51375c | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 140 | trace_afs_file_error(dvnode, -EIO, afs_file_error_dir_bad_magic); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | goto error; |
| 142 | } |
David Howells | 63a4681 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 143 | |
| 144 | /* Make sure each block is NUL terminated so we can reasonably |
| 145 | * use string functions on it. The filenames in the page |
| 146 | * *should* be NUL-terminated anyway. |
| 147 | */ |
| 148 | ((u8 *)&dbuf->blocks[tmp])[AFS_DIR_BLOCK_SIZE - 1] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | } |
| 150 | |
David Howells | 63a4681 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 151 | kunmap(page); |
| 152 | |
David Howells | dab17c1 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 153 | checked: |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 154 | afs_stat_v(dvnode, n_read_dir); |
Al Viro | be5b82d | 2016-04-22 15:06:44 -0400 | [diff] [blame] | 155 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 157 | error: |
Al Viro | be5b82d | 2016-04-22 15:06:44 -0400 | [diff] [blame] | 158 | return false; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 159 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | /* |
David Howells | 445b102 | 2019-04-25 14:26:51 +0100 | [diff] [blame] | 162 | * Check the contents of a directory that we've just read. |
| 163 | */ |
| 164 | static bool afs_dir_check_pages(struct afs_vnode *dvnode, struct afs_read *req) |
| 165 | { |
| 166 | struct afs_xdr_dir_page *dbuf; |
| 167 | unsigned int i, j, qty = PAGE_SIZE / sizeof(union afs_xdr_dir_block); |
| 168 | |
| 169 | for (i = 0; i < req->nr_pages; i++) |
| 170 | if (!afs_dir_check_page(dvnode, req->pages[i], req->actual_len)) |
| 171 | goto bad; |
| 172 | return true; |
| 173 | |
| 174 | bad: |
| 175 | pr_warn("DIR %llx:%llx f=%llx l=%llx al=%llx r=%llx\n", |
| 176 | dvnode->fid.vid, dvnode->fid.vnode, |
| 177 | req->file_size, req->len, req->actual_len, req->remain); |
| 178 | pr_warn("DIR %llx %x %x %x\n", |
| 179 | req->pos, req->index, req->nr_pages, req->offset); |
| 180 | |
| 181 | for (i = 0; i < req->nr_pages; i++) { |
| 182 | dbuf = kmap(req->pages[i]); |
| 183 | for (j = 0; j < qty; j++) { |
| 184 | union afs_xdr_dir_block *block = &dbuf->blocks[j]; |
| 185 | |
| 186 | pr_warn("[%02x] %32phN\n", i * qty + j, block); |
| 187 | } |
| 188 | kunmap(req->pages[i]); |
| 189 | } |
| 190 | return false; |
| 191 | } |
| 192 | |
| 193 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | * open an AFS directory file |
| 195 | */ |
| 196 | static int afs_dir_open(struct inode *inode, struct file *file) |
| 197 | { |
| 198 | _enter("{%lu}", inode->i_ino); |
| 199 | |
David Howells | 0031763 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 200 | BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048); |
| 201 | BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 203 | if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(inode)->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | return -ENOENT; |
| 205 | |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 206 | return afs_open(inode, file); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 207 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | /* |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 210 | * Read the directory into the pagecache in one go, scrubbing the previous |
| 211 | * contents. The list of pages is returned, pinning them so that they don't |
| 212 | * get reclaimed during the iteration. |
| 213 | */ |
| 214 | static struct afs_read *afs_read_dir(struct afs_vnode *dvnode, struct key *key) |
David Howells | b61f7dc | 2018-04-27 20:46:22 +0100 | [diff] [blame] | 215 | __acquires(&dvnode->validate_lock) |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 216 | { |
| 217 | struct afs_read *req; |
| 218 | loff_t i_size; |
| 219 | int nr_pages, nr_inline, i, n; |
| 220 | int ret = -ENOMEM; |
| 221 | |
| 222 | retry: |
| 223 | i_size = i_size_read(&dvnode->vfs_inode); |
| 224 | if (i_size < 2048) |
David Howells | f51375c | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 225 | return ERR_PTR(afs_bad(dvnode, afs_file_error_dir_small)); |
| 226 | if (i_size > 2048 * 1024) { |
| 227 | trace_afs_file_error(dvnode, -EFBIG, afs_file_error_dir_big); |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 228 | return ERR_PTR(-EFBIG); |
David Howells | f51375c | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 229 | } |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 230 | |
| 231 | _enter("%llu", i_size); |
| 232 | |
| 233 | /* Get a request record to hold the page list. We want to hold it |
| 234 | * inline if we can, but we don't want to make an order 1 allocation. |
| 235 | */ |
| 236 | nr_pages = (i_size + PAGE_SIZE - 1) / PAGE_SIZE; |
| 237 | nr_inline = nr_pages; |
| 238 | if (nr_inline > (PAGE_SIZE - sizeof(*req)) / sizeof(struct page *)) |
| 239 | nr_inline = 0; |
| 240 | |
Zhengyuan Liu | ee10258 | 2019-06-20 18:12:17 +0100 | [diff] [blame] | 241 | req = kzalloc(struct_size(req, array, nr_inline), GFP_KERNEL); |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 242 | if (!req) |
| 243 | return ERR_PTR(-ENOMEM); |
| 244 | |
| 245 | refcount_set(&req->usage, 1); |
| 246 | req->nr_pages = nr_pages; |
| 247 | req->actual_len = i_size; /* May change */ |
| 248 | req->len = nr_pages * PAGE_SIZE; /* We can ask for more than there is */ |
| 249 | req->data_version = dvnode->status.data_version; /* May change */ |
| 250 | if (nr_inline > 0) { |
| 251 | req->pages = req->array; |
| 252 | } else { |
| 253 | req->pages = kcalloc(nr_pages, sizeof(struct page *), |
| 254 | GFP_KERNEL); |
| 255 | if (!req->pages) |
| 256 | goto error; |
| 257 | } |
| 258 | |
| 259 | /* Get a list of all the pages that hold or will hold the directory |
| 260 | * content. We need to fill in any gaps that we might find where the |
| 261 | * memory reclaimer has been at work. If there are any gaps, we will |
| 262 | * need to reread the entire directory contents. |
| 263 | */ |
| 264 | i = 0; |
| 265 | do { |
| 266 | n = find_get_pages_contig(dvnode->vfs_inode.i_mapping, i, |
| 267 | req->nr_pages - i, |
| 268 | req->pages + i); |
| 269 | _debug("find %u at %u/%u", n, i, req->nr_pages); |
| 270 | if (n == 0) { |
| 271 | gfp_t gfp = dvnode->vfs_inode.i_mapping->gfp_mask; |
| 272 | |
| 273 | if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) |
| 274 | afs_stat_v(dvnode, n_inval); |
| 275 | |
| 276 | ret = -ENOMEM; |
| 277 | req->pages[i] = __page_cache_alloc(gfp); |
| 278 | if (!req->pages[i]) |
| 279 | goto error; |
| 280 | ret = add_to_page_cache_lru(req->pages[i], |
| 281 | dvnode->vfs_inode.i_mapping, |
| 282 | i, gfp); |
| 283 | if (ret < 0) |
| 284 | goto error; |
| 285 | |
| 286 | set_page_private(req->pages[i], 1); |
| 287 | SetPagePrivate(req->pages[i]); |
| 288 | unlock_page(req->pages[i]); |
| 289 | i++; |
| 290 | } else { |
| 291 | i += n; |
| 292 | } |
| 293 | } while (i < req->nr_pages); |
| 294 | |
| 295 | /* If we're going to reload, we need to lock all the pages to prevent |
| 296 | * races. |
| 297 | */ |
David Howells | b61f7dc | 2018-04-27 20:46:22 +0100 | [diff] [blame] | 298 | ret = -ERESTARTSYS; |
| 299 | if (down_read_killable(&dvnode->validate_lock) < 0) |
| 300 | goto error; |
| 301 | |
| 302 | if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) |
| 303 | goto success; |
| 304 | |
| 305 | up_read(&dvnode->validate_lock); |
| 306 | if (down_write_killable(&dvnode->validate_lock) < 0) |
| 307 | goto error; |
| 308 | |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 309 | if (!test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) { |
David Howells | 99987c56 | 2019-04-25 14:26:51 +0100 | [diff] [blame] | 310 | trace_afs_reload_dir(dvnode); |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 311 | ret = afs_fetch_data(dvnode, key, req); |
| 312 | if (ret < 0) |
David Howells | b61f7dc | 2018-04-27 20:46:22 +0100 | [diff] [blame] | 313 | goto error_unlock; |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 314 | |
| 315 | task_io_account_read(PAGE_SIZE * req->nr_pages); |
| 316 | |
| 317 | if (req->len < req->file_size) |
| 318 | goto content_has_grown; |
| 319 | |
| 320 | /* Validate the data we just read. */ |
| 321 | ret = -EIO; |
David Howells | 445b102 | 2019-04-25 14:26:51 +0100 | [diff] [blame] | 322 | if (!afs_dir_check_pages(dvnode, req)) |
| 323 | goto error_unlock; |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 324 | |
| 325 | // TODO: Trim excess pages |
| 326 | |
| 327 | set_bit(AFS_VNODE_DIR_VALID, &dvnode->flags); |
| 328 | } |
| 329 | |
David Howells | b61f7dc | 2018-04-27 20:46:22 +0100 | [diff] [blame] | 330 | downgrade_write(&dvnode->validate_lock); |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 331 | success: |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 332 | return req; |
| 333 | |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 334 | error_unlock: |
David Howells | b61f7dc | 2018-04-27 20:46:22 +0100 | [diff] [blame] | 335 | up_write(&dvnode->validate_lock); |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 336 | error: |
| 337 | afs_put_read(req); |
| 338 | _leave(" = %d", ret); |
| 339 | return ERR_PTR(ret); |
| 340 | |
| 341 | content_has_grown: |
David Howells | b61f7dc | 2018-04-27 20:46:22 +0100 | [diff] [blame] | 342 | up_write(&dvnode->validate_lock); |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 343 | afs_put_read(req); |
| 344 | goto retry; |
| 345 | } |
| 346 | |
| 347 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | * deal with one block in an AFS directory |
| 349 | */ |
David Howells | f51375c | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 350 | static int afs_dir_iterate_block(struct afs_vnode *dvnode, |
| 351 | struct dir_context *ctx, |
David Howells | 0031763 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 352 | union afs_xdr_dir_block *block, |
Al Viro | 1bbae9f | 2013-05-22 16:31:14 -0400 | [diff] [blame] | 353 | unsigned blkoff) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | { |
David Howells | 0031763 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 355 | union afs_xdr_dirent *dire; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | unsigned offset, next, curr; |
| 357 | size_t nlen; |
Al Viro | 1bbae9f | 2013-05-22 16:31:14 -0400 | [diff] [blame] | 358 | int tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | |
Al Viro | 1bbae9f | 2013-05-22 16:31:14 -0400 | [diff] [blame] | 360 | _enter("%u,%x,%p,,",(unsigned)ctx->pos,blkoff,block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | |
David Howells | 0031763 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 362 | curr = (ctx->pos - blkoff) / sizeof(union afs_xdr_dirent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | |
| 364 | /* walk through the block, an entry at a time */ |
David Howells | 4ea219a | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 365 | for (offset = (blkoff == 0 ? AFS_DIR_RESV_BLOCKS0 : AFS_DIR_RESV_BLOCKS); |
| 366 | offset < AFS_DIR_SLOTS_PER_BLOCK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | offset = next |
| 368 | ) { |
| 369 | next = offset + 1; |
| 370 | |
| 371 | /* skip entries marked unused in the bitmap */ |
David Howells | 0031763 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 372 | if (!(block->hdr.bitmap[offset / 8] & |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | (1 << (offset % 8)))) { |
Alexey Dobriyan | 5b5e092 | 2017-02-27 14:30:02 -0800 | [diff] [blame] | 374 | _debug("ENT[%zu.%u]: unused", |
David Howells | 0031763 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 375 | blkoff / sizeof(union afs_xdr_dir_block), offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | if (offset >= curr) |
Al Viro | 1bbae9f | 2013-05-22 16:31:14 -0400 | [diff] [blame] | 377 | ctx->pos = blkoff + |
David Howells | 0031763 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 378 | next * sizeof(union afs_xdr_dirent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | continue; |
| 380 | } |
| 381 | |
| 382 | /* got a valid entry */ |
| 383 | dire = &block->dirents[offset]; |
| 384 | nlen = strnlen(dire->u.name, |
| 385 | sizeof(*block) - |
David Howells | 0031763 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 386 | offset * sizeof(union afs_xdr_dirent)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | |
Alexey Dobriyan | 5b5e092 | 2017-02-27 14:30:02 -0800 | [diff] [blame] | 388 | _debug("ENT[%zu.%u]: %s %zu \"%s\"", |
David Howells | 0031763 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 389 | blkoff / sizeof(union afs_xdr_dir_block), offset, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | (offset < curr ? "skip" : "fill"), |
| 391 | nlen, dire->u.name); |
| 392 | |
| 393 | /* work out where the next possible entry is */ |
David Howells | 0031763 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 394 | for (tmp = nlen; tmp > 15; tmp -= sizeof(union afs_xdr_dirent)) { |
David Howells | 4ea219a | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 395 | if (next >= AFS_DIR_SLOTS_PER_BLOCK) { |
Alexey Dobriyan | 5b5e092 | 2017-02-27 14:30:02 -0800 | [diff] [blame] | 396 | _debug("ENT[%zu.%u]:" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | " %u travelled beyond end dir block" |
Alexey Dobriyan | 5b5e092 | 2017-02-27 14:30:02 -0800 | [diff] [blame] | 398 | " (len %u/%zu)", |
David Howells | 0031763 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 399 | blkoff / sizeof(union afs_xdr_dir_block), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | offset, next, tmp, nlen); |
David Howells | f51375c | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 401 | return afs_bad(dvnode, afs_file_error_dir_over_end); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | } |
David Howells | 0031763 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 403 | if (!(block->hdr.bitmap[next / 8] & |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | (1 << (next % 8)))) { |
Alexey Dobriyan | 5b5e092 | 2017-02-27 14:30:02 -0800 | [diff] [blame] | 405 | _debug("ENT[%zu.%u]:" |
| 406 | " %u unmarked extension (len %u/%zu)", |
David Howells | 0031763 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 407 | blkoff / sizeof(union afs_xdr_dir_block), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | offset, next, tmp, nlen); |
David Howells | f51375c | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 409 | return afs_bad(dvnode, afs_file_error_dir_unmarked_ext); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Alexey Dobriyan | 5b5e092 | 2017-02-27 14:30:02 -0800 | [diff] [blame] | 412 | _debug("ENT[%zu.%u]: ext %u/%zu", |
David Howells | 0031763 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 413 | blkoff / sizeof(union afs_xdr_dir_block), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | next, tmp, nlen); |
| 415 | next++; |
| 416 | } |
| 417 | |
| 418 | /* skip if starts before the current position */ |
| 419 | if (offset < curr) |
| 420 | continue; |
| 421 | |
| 422 | /* found the next entry */ |
Al Viro | 1bbae9f | 2013-05-22 16:31:14 -0400 | [diff] [blame] | 423 | if (!dir_emit(ctx, dire->u.name, nlen, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | ntohl(dire->u.vnode), |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 425 | (ctx->actor == afs_lookup_filldir || |
| 426 | ctx->actor == afs_lookup_one_filldir)? |
Al Viro | 1bbae9f | 2013-05-22 16:31:14 -0400 | [diff] [blame] | 427 | ntohl(dire->u.unique) : DT_UNKNOWN)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | _leave(" = 0 [full]"); |
| 429 | return 0; |
| 430 | } |
| 431 | |
David Howells | 0031763 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 432 | ctx->pos = blkoff + next * sizeof(union afs_xdr_dirent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | _leave(" = 1 [more]"); |
| 436 | return 1; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 437 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | /* |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 440 | * iterate through the data blob that lists the contents of an AFS directory |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | */ |
Al Viro | 1bbae9f | 2013-05-22 16:31:14 -0400 | [diff] [blame] | 442 | static int afs_dir_iterate(struct inode *dir, struct dir_context *ctx, |
David Howells | 9dd0b82 | 2019-07-30 14:38:52 +0100 | [diff] [blame] | 443 | struct key *key, afs_dataversion_t *_dir_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | { |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 445 | struct afs_vnode *dvnode = AFS_FS_I(dir); |
David Howells | 0031763 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 446 | struct afs_xdr_dir_page *dbuf; |
| 447 | union afs_xdr_dir_block *dblock; |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 448 | struct afs_read *req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | struct page *page; |
| 450 | unsigned blkoff, limit; |
| 451 | int ret; |
| 452 | |
Al Viro | 1bbae9f | 2013-05-22 16:31:14 -0400 | [diff] [blame] | 453 | _enter("{%lu},%u,,", dir->i_ino, (unsigned)ctx->pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 455 | if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | _leave(" = -ESTALE"); |
| 457 | return -ESTALE; |
| 458 | } |
| 459 | |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 460 | req = afs_read_dir(dvnode, key); |
| 461 | if (IS_ERR(req)) |
| 462 | return PTR_ERR(req); |
David Howells | 9dd0b82 | 2019-07-30 14:38:52 +0100 | [diff] [blame] | 463 | *_dir_version = req->data_version; |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 464 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | /* round the file position up to the next entry boundary */ |
David Howells | 0031763 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 466 | ctx->pos += sizeof(union afs_xdr_dirent) - 1; |
| 467 | ctx->pos &= ~(sizeof(union afs_xdr_dirent) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | |
| 469 | /* walk through the blocks in sequence */ |
| 470 | ret = 0; |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 471 | while (ctx->pos < req->actual_len) { |
David Howells | 0031763 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 472 | blkoff = ctx->pos & ~(sizeof(union afs_xdr_dir_block) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 474 | /* Fetch the appropriate page from the directory and re-add it |
| 475 | * to the LRU. |
| 476 | */ |
| 477 | page = req->pages[blkoff / PAGE_SIZE]; |
| 478 | if (!page) { |
David Howells | f51375c | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 479 | ret = afs_bad(dvnode, afs_file_error_dir_missing_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | break; |
| 481 | } |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 482 | mark_page_accessed(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | |
| 484 | limit = blkoff & ~(PAGE_SIZE - 1); |
| 485 | |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 486 | dbuf = kmap(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | |
| 488 | /* deal with the individual blocks stashed on this page */ |
| 489 | do { |
| 490 | dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) / |
David Howells | 0031763 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 491 | sizeof(union afs_xdr_dir_block)]; |
David Howells | f51375c | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 492 | ret = afs_dir_iterate_block(dvnode, ctx, dblock, blkoff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | if (ret != 1) { |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 494 | kunmap(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | goto out; |
| 496 | } |
| 497 | |
David Howells | 0031763 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 498 | blkoff += sizeof(union afs_xdr_dir_block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | |
Al Viro | 1bbae9f | 2013-05-22 16:31:14 -0400 | [diff] [blame] | 500 | } while (ctx->pos < dir->i_size && blkoff < limit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 502 | kunmap(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | ret = 0; |
| 504 | } |
| 505 | |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 506 | out: |
David Howells | b61f7dc | 2018-04-27 20:46:22 +0100 | [diff] [blame] | 507 | up_read(&dvnode->validate_lock); |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 508 | afs_put_read(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | _leave(" = %d", ret); |
| 510 | return ret; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 511 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | /* |
| 514 | * read an AFS directory |
| 515 | */ |
Al Viro | 1bbae9f | 2013-05-22 16:31:14 -0400 | [diff] [blame] | 516 | static int afs_readdir(struct file *file, struct dir_context *ctx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | { |
David Howells | 9dd0b82 | 2019-07-30 14:38:52 +0100 | [diff] [blame] | 518 | afs_dataversion_t dir_version; |
| 519 | |
| 520 | return afs_dir_iterate(file_inode(file), ctx, afs_file_key(file), |
| 521 | &dir_version); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 522 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | /* |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 525 | * Search the directory for a single name |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | * - if afs_dir_iterate_block() spots this function, it'll pass the FID |
| 527 | * uniquifier through dtype |
| 528 | */ |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 529 | static int afs_lookup_one_filldir(struct dir_context *ctx, const char *name, |
| 530 | int nlen, loff_t fpos, u64 ino, unsigned dtype) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | { |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 532 | struct afs_lookup_one_cookie *cookie = |
| 533 | container_of(ctx, struct afs_lookup_one_cookie, ctx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | |
Al Viro | 1bbae9f | 2013-05-22 16:31:14 -0400 | [diff] [blame] | 535 | _enter("{%s,%u},%s,%u,,%llu,%u", |
| 536 | cookie->name.name, cookie->name.len, name, nlen, |
David S. Miller | ba3e0e1 | 2007-04-26 16:06:22 -0700 | [diff] [blame] | 537 | (unsigned long long) ino, dtype); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 539 | /* insanity checks first */ |
David Howells | 0031763 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 540 | BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048); |
| 541 | BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 542 | |
Al Viro | 1bbae9f | 2013-05-22 16:31:14 -0400 | [diff] [blame] | 543 | if (cookie->name.len != nlen || |
| 544 | memcmp(cookie->name.name, name, nlen) != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | _leave(" = 0 [no]"); |
| 546 | return 0; |
| 547 | } |
| 548 | |
| 549 | cookie->fid.vnode = ino; |
| 550 | cookie->fid.unique = dtype; |
| 551 | cookie->found = 1; |
| 552 | |
| 553 | _leave(" = -1 [found]"); |
| 554 | return -1; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 555 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | /* |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 558 | * Do a lookup of a single name in a directory |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 559 | * - just returns the FID the dentry name maps to if found |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | */ |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 561 | static int afs_do_lookup_one(struct inode *dir, struct dentry *dentry, |
David Howells | 9dd0b82 | 2019-07-30 14:38:52 +0100 | [diff] [blame] | 562 | struct afs_fid *fid, struct key *key, |
| 563 | afs_dataversion_t *_dir_version) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | { |
Al Viro | 1bbae9f | 2013-05-22 16:31:14 -0400 | [diff] [blame] | 565 | struct afs_super_info *as = dir->i_sb->s_fs_info; |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 566 | struct afs_lookup_one_cookie cookie = { |
| 567 | .ctx.actor = afs_lookup_one_filldir, |
Al Viro | 1bbae9f | 2013-05-22 16:31:14 -0400 | [diff] [blame] | 568 | .name = dentry->d_name, |
| 569 | .fid.vid = as->volume->vid |
| 570 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | int ret; |
| 572 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 573 | _enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | /* search the directory */ |
David Howells | 9dd0b82 | 2019-07-30 14:38:52 +0100 | [diff] [blame] | 576 | ret = afs_dir_iterate(dir, &cookie.ctx, key, _dir_version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | if (ret < 0) { |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 578 | _leave(" = %d [iter]", ret); |
| 579 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | ret = -ENOENT; |
| 583 | if (!cookie.found) { |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 584 | _leave(" = -ENOENT [not found]"); |
| 585 | return -ENOENT; |
| 586 | } |
| 587 | |
| 588 | *fid = cookie.fid; |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 589 | _leave(" = 0 { vn=%llu u=%u }", fid->vnode, fid->unique); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 590 | return 0; |
| 591 | } |
| 592 | |
| 593 | /* |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 594 | * search the directory for a name |
| 595 | * - if afs_dir_iterate_block() spots this function, it'll pass the FID |
| 596 | * uniquifier through dtype |
| 597 | */ |
| 598 | static int afs_lookup_filldir(struct dir_context *ctx, const char *name, |
| 599 | int nlen, loff_t fpos, u64 ino, unsigned dtype) |
| 600 | { |
| 601 | struct afs_lookup_cookie *cookie = |
| 602 | container_of(ctx, struct afs_lookup_cookie, ctx); |
| 603 | int ret; |
| 604 | |
| 605 | _enter("{%s,%u},%s,%u,,%llu,%u", |
| 606 | cookie->name.name, cookie->name.len, name, nlen, |
| 607 | (unsigned long long) ino, dtype); |
| 608 | |
| 609 | /* insanity checks first */ |
David Howells | 0031763 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 610 | BUILD_BUG_ON(sizeof(union afs_xdr_dir_block) != 2048); |
| 611 | BUILD_BUG_ON(sizeof(union afs_xdr_dirent) != 32); |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 612 | |
| 613 | if (cookie->found) { |
| 614 | if (cookie->nr_fids < 50) { |
| 615 | cookie->fids[cookie->nr_fids].vnode = ino; |
| 616 | cookie->fids[cookie->nr_fids].unique = dtype; |
| 617 | cookie->nr_fids++; |
| 618 | } |
| 619 | } else if (cookie->name.len == nlen && |
| 620 | memcmp(cookie->name.name, name, nlen) == 0) { |
| 621 | cookie->fids[0].vnode = ino; |
| 622 | cookie->fids[0].unique = dtype; |
| 623 | cookie->found = 1; |
| 624 | if (cookie->one_only) |
| 625 | return -1; |
| 626 | } |
| 627 | |
| 628 | ret = cookie->nr_fids >= 50 ? -1 : 0; |
| 629 | _leave(" = %d", ret); |
| 630 | return ret; |
| 631 | } |
| 632 | |
| 633 | /* |
| 634 | * Do a lookup in a directory. We make use of bulk lookup to query a slew of |
| 635 | * files in one go and create inodes for them. The inode of the file we were |
| 636 | * asked for is returned. |
| 637 | */ |
| 638 | static struct inode *afs_do_lookup(struct inode *dir, struct dentry *dentry, |
| 639 | struct key *key) |
| 640 | { |
| 641 | struct afs_lookup_cookie *cookie; |
David Howells | f642404 | 2019-05-13 16:14:32 +0100 | [diff] [blame] | 642 | struct afs_cb_interest *dcbi, *cbi = NULL; |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 643 | struct afs_super_info *as = dir->i_sb->s_fs_info; |
David Howells | 8718275 | 2019-05-09 16:17:05 +0100 | [diff] [blame] | 644 | struct afs_status_cb *scb; |
David Howells | b835915 | 2019-05-14 12:23:43 +0100 | [diff] [blame] | 645 | struct afs_iget_data iget_data; |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 646 | struct afs_fs_cursor fc; |
David Howells | f642404 | 2019-05-13 16:14:32 +0100 | [diff] [blame] | 647 | struct afs_server *server; |
David Howells | 39db981 | 2019-05-14 12:33:10 +0100 | [diff] [blame] | 648 | struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode; |
| 649 | struct inode *inode = NULL, *ti; |
David Howells | 9dd0b82 | 2019-07-30 14:38:52 +0100 | [diff] [blame] | 650 | afs_dataversion_t data_version = READ_ONCE(dvnode->status.data_version); |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 651 | int ret, i; |
| 652 | |
| 653 | _enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry); |
| 654 | |
| 655 | cookie = kzalloc(sizeof(struct afs_lookup_cookie), GFP_KERNEL); |
| 656 | if (!cookie) |
| 657 | return ERR_PTR(-ENOMEM); |
| 658 | |
| 659 | cookie->ctx.actor = afs_lookup_filldir; |
| 660 | cookie->name = dentry->d_name; |
| 661 | cookie->nr_fids = 1; /* slot 0 is saved for the fid we actually want */ |
| 662 | |
| 663 | read_seqlock_excl(&dvnode->cb_lock); |
David Howells | f642404 | 2019-05-13 16:14:32 +0100 | [diff] [blame] | 664 | dcbi = rcu_dereference_protected(dvnode->cb_interest, |
| 665 | lockdep_is_held(&dvnode->cb_lock.lock)); |
| 666 | if (dcbi) { |
| 667 | server = dcbi->server; |
| 668 | if (server && |
| 669 | test_bit(AFS_SERVER_FL_NO_IBULK, &server->flags)) |
| 670 | cookie->one_only = true; |
| 671 | } |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 672 | read_sequnlock_excl(&dvnode->cb_lock); |
| 673 | |
| 674 | for (i = 0; i < 50; i++) |
| 675 | cookie->fids[i].vid = as->volume->vid; |
| 676 | |
| 677 | /* search the directory */ |
David Howells | 9dd0b82 | 2019-07-30 14:38:52 +0100 | [diff] [blame] | 678 | ret = afs_dir_iterate(dir, &cookie->ctx, key, &data_version); |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 679 | if (ret < 0) { |
| 680 | inode = ERR_PTR(ret); |
| 681 | goto out; |
| 682 | } |
| 683 | |
David Howells | 9dd0b82 | 2019-07-30 14:38:52 +0100 | [diff] [blame] | 684 | dentry->d_fsdata = (void *)(unsigned long)data_version; |
| 685 | |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 686 | inode = ERR_PTR(-ENOENT); |
| 687 | if (!cookie->found) |
| 688 | goto out; |
| 689 | |
| 690 | /* Check to see if we already have an inode for the primary fid. */ |
David Howells | b835915 | 2019-05-14 12:23:43 +0100 | [diff] [blame] | 691 | iget_data.fid = cookie->fids[0]; |
| 692 | iget_data.volume = dvnode->volume; |
| 693 | iget_data.cb_v_break = dvnode->volume->cb_v_break; |
| 694 | iget_data.cb_s_break = 0; |
| 695 | inode = ilookup5(dir->i_sb, cookie->fids[0].vnode, |
| 696 | afs_iget5_test, &iget_data); |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 697 | if (inode) |
| 698 | goto out; |
| 699 | |
| 700 | /* Need space for examining all the selected files */ |
| 701 | inode = ERR_PTR(-ENOMEM); |
David Howells | 8718275 | 2019-05-09 16:17:05 +0100 | [diff] [blame] | 702 | cookie->statuses = kvcalloc(cookie->nr_fids, sizeof(struct afs_status_cb), |
| 703 | GFP_KERNEL); |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 704 | if (!cookie->statuses) |
| 705 | goto out; |
| 706 | |
David Howells | 39db981 | 2019-05-14 12:33:10 +0100 | [diff] [blame] | 707 | cookie->inodes = kcalloc(cookie->nr_fids, sizeof(struct inode *), |
| 708 | GFP_KERNEL); |
| 709 | if (!cookie->inodes) |
| 710 | goto out_s; |
| 711 | |
| 712 | for (i = 1; i < cookie->nr_fids; i++) { |
| 713 | scb = &cookie->statuses[i]; |
| 714 | |
| 715 | /* Find any inodes that already exist and get their |
| 716 | * callback counters. |
| 717 | */ |
| 718 | iget_data.fid = cookie->fids[i]; |
| 719 | ti = ilookup5_nowait(dir->i_sb, iget_data.fid.vnode, |
| 720 | afs_iget5_test, &iget_data); |
| 721 | if (!IS_ERR_OR_NULL(ti)) { |
| 722 | vnode = AFS_FS_I(ti); |
| 723 | scb->cb_break = afs_calc_vnode_cb_break(vnode); |
| 724 | cookie->inodes[i] = ti; |
| 725 | } |
| 726 | } |
| 727 | |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 728 | /* Try FS.InlineBulkStatus first. Abort codes for the individual |
| 729 | * lookups contained therein are stored in the reply without aborting |
| 730 | * the whole operation. |
| 731 | */ |
| 732 | if (cookie->one_only) |
| 733 | goto no_inline_bulk_status; |
| 734 | |
| 735 | inode = ERR_PTR(-ERESTARTSYS); |
David Howells | 20b8391 | 2019-05-08 16:16:31 +0100 | [diff] [blame] | 736 | if (afs_begin_vnode_operation(&fc, dvnode, key, true)) { |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 737 | while (afs_select_fileserver(&fc)) { |
| 738 | if (test_bit(AFS_SERVER_FL_NO_IBULK, |
| 739 | &fc.cbi->server->flags)) { |
| 740 | fc.ac.abort_code = RX_INVALID_OPERATION; |
| 741 | fc.ac.error = -ECONNABORTED; |
| 742 | break; |
| 743 | } |
David Howells | b835915 | 2019-05-14 12:23:43 +0100 | [diff] [blame] | 744 | iget_data.cb_v_break = dvnode->volume->cb_v_break; |
| 745 | iget_data.cb_s_break = fc.cbi->server->cb_s_break; |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 746 | afs_fs_inline_bulk_status(&fc, |
| 747 | afs_v2net(dvnode), |
| 748 | cookie->fids, |
| 749 | cookie->statuses, |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 750 | cookie->nr_fids, NULL); |
| 751 | } |
| 752 | |
| 753 | if (fc.ac.error == 0) |
| 754 | cbi = afs_get_cb_interest(fc.cbi); |
| 755 | if (fc.ac.abort_code == RX_INVALID_OPERATION) |
| 756 | set_bit(AFS_SERVER_FL_NO_IBULK, &fc.cbi->server->flags); |
| 757 | inode = ERR_PTR(afs_end_vnode_operation(&fc)); |
| 758 | } |
| 759 | |
| 760 | if (!IS_ERR(inode)) |
| 761 | goto success; |
| 762 | if (fc.ac.abort_code != RX_INVALID_OPERATION) |
| 763 | goto out_c; |
| 764 | |
| 765 | no_inline_bulk_status: |
| 766 | /* We could try FS.BulkStatus next, but this aborts the entire op if |
| 767 | * any of the lookups fails - so, for the moment, revert to |
| 768 | * FS.FetchStatus for just the primary fid. |
| 769 | */ |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 770 | inode = ERR_PTR(-ERESTARTSYS); |
David Howells | 20b8391 | 2019-05-08 16:16:31 +0100 | [diff] [blame] | 771 | if (afs_begin_vnode_operation(&fc, dvnode, key, true)) { |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 772 | while (afs_select_fileserver(&fc)) { |
David Howells | b835915 | 2019-05-14 12:23:43 +0100 | [diff] [blame] | 773 | iget_data.cb_v_break = dvnode->volume->cb_v_break; |
| 774 | iget_data.cb_s_break = fc.cbi->server->cb_s_break; |
David Howells | 8718275 | 2019-05-09 16:17:05 +0100 | [diff] [blame] | 775 | scb = &cookie->statuses[0]; |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 776 | afs_fs_fetch_status(&fc, |
| 777 | afs_v2net(dvnode), |
| 778 | cookie->fids, |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 779 | scb, |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 780 | NULL); |
| 781 | } |
| 782 | |
| 783 | if (fc.ac.error == 0) |
| 784 | cbi = afs_get_cb_interest(fc.cbi); |
| 785 | inode = ERR_PTR(afs_end_vnode_operation(&fc)); |
| 786 | } |
| 787 | |
| 788 | if (IS_ERR(inode)) |
| 789 | goto out_c; |
| 790 | |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 791 | success: |
| 792 | /* Turn all the files into inodes and save the first one - which is the |
| 793 | * one we actually want. |
| 794 | */ |
David Howells | 8718275 | 2019-05-09 16:17:05 +0100 | [diff] [blame] | 795 | scb = &cookie->statuses[0]; |
| 796 | if (scb->status.abort_code != 0) |
| 797 | inode = ERR_PTR(afs_abort_to_error(scb->status.abort_code)); |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 798 | |
| 799 | for (i = 0; i < cookie->nr_fids; i++) { |
David Howells | 8718275 | 2019-05-09 16:17:05 +0100 | [diff] [blame] | 800 | struct afs_status_cb *scb = &cookie->statuses[i]; |
David Howells | 39db981 | 2019-05-14 12:33:10 +0100 | [diff] [blame] | 801 | |
| 802 | if (!scb->have_status && !scb->have_error) |
| 803 | continue; |
| 804 | |
| 805 | if (cookie->inodes[i]) { |
David Howells | a28f239 | 2019-11-14 18:41:03 +0000 | [diff] [blame] | 806 | struct afs_vnode *iv = AFS_FS_I(cookie->inodes[i]); |
| 807 | |
| 808 | if (test_bit(AFS_VNODE_UNSET, &iv->flags)) |
| 809 | continue; |
| 810 | |
| 811 | afs_vnode_commit_status(&fc, iv, |
David Howells | 39db981 | 2019-05-14 12:33:10 +0100 | [diff] [blame] | 812 | scb->cb_break, NULL, scb); |
| 813 | continue; |
| 814 | } |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 815 | |
David Howells | 8718275 | 2019-05-09 16:17:05 +0100 | [diff] [blame] | 816 | if (scb->status.abort_code != 0) |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 817 | continue; |
| 818 | |
David Howells | b835915 | 2019-05-14 12:23:43 +0100 | [diff] [blame] | 819 | iget_data.fid = cookie->fids[i]; |
| 820 | ti = afs_iget(dir->i_sb, key, &iget_data, scb, cbi, dvnode); |
David Howells | 39db981 | 2019-05-14 12:33:10 +0100 | [diff] [blame] | 821 | if (!IS_ERR(ti)) |
| 822 | afs_cache_permit(AFS_FS_I(ti), key, |
| 823 | 0 /* Assume vnode->cb_break is 0 */ + |
| 824 | iget_data.cb_v_break, |
| 825 | scb); |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 826 | if (i == 0) { |
| 827 | inode = ti; |
| 828 | } else { |
| 829 | if (!IS_ERR(ti)) |
| 830 | iput(ti); |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | out_c: |
| 835 | afs_put_cb_interest(afs_v2net(dvnode), cbi); |
David Howells | 39db981 | 2019-05-14 12:33:10 +0100 | [diff] [blame] | 836 | if (cookie->inodes) { |
| 837 | for (i = 0; i < cookie->nr_fids; i++) |
| 838 | iput(cookie->inodes[i]); |
| 839 | kfree(cookie->inodes); |
| 840 | } |
| 841 | out_s: |
David Howells | 8718275 | 2019-05-09 16:17:05 +0100 | [diff] [blame] | 842 | kvfree(cookie->statuses); |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 843 | out: |
| 844 | kfree(cookie); |
| 845 | return inode; |
| 846 | } |
| 847 | |
| 848 | /* |
David Howells | 6f8880d | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 849 | * Look up an entry in a directory with @sys substitution. |
| 850 | */ |
| 851 | static struct dentry *afs_lookup_atsys(struct inode *dir, struct dentry *dentry, |
| 852 | struct key *key) |
| 853 | { |
| 854 | struct afs_sysnames *subs; |
| 855 | struct afs_net *net = afs_i2net(dir); |
| 856 | struct dentry *ret; |
| 857 | char *buf, *p, *name; |
| 858 | int len, i; |
| 859 | |
| 860 | _enter(""); |
| 861 | |
| 862 | ret = ERR_PTR(-ENOMEM); |
| 863 | p = buf = kmalloc(AFSNAMEMAX, GFP_KERNEL); |
| 864 | if (!buf) |
| 865 | goto out_p; |
| 866 | if (dentry->d_name.len > 4) { |
| 867 | memcpy(p, dentry->d_name.name, dentry->d_name.len - 4); |
| 868 | p += dentry->d_name.len - 4; |
| 869 | } |
| 870 | |
| 871 | /* There is an ordered list of substitutes that we have to try. */ |
| 872 | read_lock(&net->sysnames_lock); |
| 873 | subs = net->sysnames; |
| 874 | refcount_inc(&subs->usage); |
| 875 | read_unlock(&net->sysnames_lock); |
| 876 | |
| 877 | for (i = 0; i < subs->nr; i++) { |
| 878 | name = subs->subs[i]; |
| 879 | len = dentry->d_name.len - 4 + strlen(name); |
| 880 | if (len >= AFSNAMEMAX) { |
| 881 | ret = ERR_PTR(-ENAMETOOLONG); |
| 882 | goto out_s; |
| 883 | } |
| 884 | |
| 885 | strcpy(p, name); |
| 886 | ret = lookup_one_len(buf, dentry->d_parent, len); |
| 887 | if (IS_ERR(ret) || d_is_positive(ret)) |
| 888 | goto out_s; |
| 889 | dput(ret); |
| 890 | } |
| 891 | |
| 892 | /* We don't want to d_add() the @sys dentry here as we don't want to |
| 893 | * the cached dentry to hide changes to the sysnames list. |
| 894 | */ |
| 895 | ret = NULL; |
| 896 | out_s: |
| 897 | afs_put_sysnames(subs); |
| 898 | kfree(buf); |
| 899 | out_p: |
| 900 | key_put(key); |
| 901 | return ret; |
| 902 | } |
| 903 | |
| 904 | /* |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 905 | * look up an entry in a directory |
| 906 | */ |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 907 | static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 908 | unsigned int flags) |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 909 | { |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 910 | struct afs_vnode *dvnode = AFS_FS_I(dir); |
David Howells | 40a708b | 2020-01-14 16:16:25 +0000 | [diff] [blame^] | 911 | struct afs_fid fid = {}; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 912 | struct inode *inode; |
Al Viro | 34b2a88 | 2018-06-24 10:43:51 -0400 | [diff] [blame] | 913 | struct dentry *d; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 914 | struct key *key; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 915 | int ret; |
| 916 | |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 917 | _enter("{%llx:%llu},%p{%pd},", |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 918 | dvnode->fid.vid, dvnode->fid.vnode, dentry, dentry); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 919 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 920 | ASSERTCMP(d_inode(dentry), ==, NULL); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 921 | |
David Howells | 45222b9 | 2007-05-10 22:22:20 -0700 | [diff] [blame] | 922 | if (dentry->d_name.len >= AFSNAMEMAX) { |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 923 | _leave(" = -ENAMETOOLONG"); |
| 924 | return ERR_PTR(-ENAMETOOLONG); |
| 925 | } |
| 926 | |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 927 | if (test_bit(AFS_VNODE_DELETED, &dvnode->flags)) { |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 928 | _leave(" = -ESTALE"); |
| 929 | return ERR_PTR(-ESTALE); |
| 930 | } |
| 931 | |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 932 | key = afs_request_key(dvnode->volume->cell); |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 933 | if (IS_ERR(key)) { |
| 934 | _leave(" = %ld [key]", PTR_ERR(key)); |
David Howells | e231c2e | 2008-02-07 00:15:26 -0800 | [diff] [blame] | 935 | return ERR_CAST(key); |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 936 | } |
| 937 | |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 938 | ret = afs_validate(dvnode, key); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 939 | if (ret < 0) { |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 940 | key_put(key); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 941 | _leave(" = %d [val]", ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | return ERR_PTR(ret); |
| 943 | } |
| 944 | |
David Howells | 6f8880d | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 945 | if (dentry->d_name.len >= 4 && |
| 946 | dentry->d_name.name[dentry->d_name.len - 4] == '@' && |
| 947 | dentry->d_name.name[dentry->d_name.len - 3] == 's' && |
| 948 | dentry->d_name.name[dentry->d_name.len - 2] == 'y' && |
| 949 | dentry->d_name.name[dentry->d_name.len - 1] == 's') |
| 950 | return afs_lookup_atsys(dir, dentry, key); |
| 951 | |
David Howells | d55b4da | 2018-04-06 14:17:24 +0100 | [diff] [blame] | 952 | afs_stat_v(dvnode, n_lookup); |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 953 | inode = afs_do_lookup(dir, dentry, key); |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 954 | key_put(key); |
Al Viro | 34b2a88 | 2018-06-24 10:43:51 -0400 | [diff] [blame] | 955 | if (inode == ERR_PTR(-ENOENT)) { |
| 956 | inode = afs_try_auto_mntpt(dentry, dir); |
Al Viro | 34b2a88 | 2018-06-24 10:43:51 -0400 | [diff] [blame] | 957 | } else { |
| 958 | dentry->d_fsdata = |
| 959 | (void *)(unsigned long)dvnode->status.data_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | } |
David Howells | 40a708b | 2020-01-14 16:16:25 +0000 | [diff] [blame^] | 961 | |
| 962 | if (!IS_ERR_OR_NULL(inode)) |
| 963 | fid = AFS_FS_I(inode)->fid; |
| 964 | |
Al Viro | 34b2a88 | 2018-06-24 10:43:51 -0400 | [diff] [blame] | 965 | d = d_splice_alias(inode, dentry); |
David Howells | 80548b0 | 2019-04-25 14:26:51 +0100 | [diff] [blame] | 966 | if (!IS_ERR_OR_NULL(d)) { |
Al Viro | 34b2a88 | 2018-06-24 10:43:51 -0400 | [diff] [blame] | 967 | d->d_fsdata = dentry->d_fsdata; |
David Howells | 40a708b | 2020-01-14 16:16:25 +0000 | [diff] [blame^] | 968 | trace_afs_lookup(dvnode, &d->d_name, &fid); |
David Howells | 80548b0 | 2019-04-25 14:26:51 +0100 | [diff] [blame] | 969 | } else { |
David Howells | 40a708b | 2020-01-14 16:16:25 +0000 | [diff] [blame^] | 970 | trace_afs_lookup(dvnode, &dentry->d_name, &fid); |
David Howells | 80548b0 | 2019-04-25 14:26:51 +0100 | [diff] [blame] | 971 | } |
Al Viro | 34b2a88 | 2018-06-24 10:43:51 -0400 | [diff] [blame] | 972 | return d; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 973 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | /* |
David Howells | a0753c2 | 2019-05-20 08:48:46 +0100 | [diff] [blame] | 976 | * Check the validity of a dentry under RCU conditions. |
| 977 | */ |
| 978 | static int afs_d_revalidate_rcu(struct dentry *dentry) |
| 979 | { |
| 980 | struct afs_vnode *dvnode, *vnode; |
| 981 | struct dentry *parent; |
| 982 | struct inode *dir, *inode; |
| 983 | long dir_version, de_version; |
| 984 | |
| 985 | _enter("%p", dentry); |
| 986 | |
| 987 | /* Check the parent directory is still valid first. */ |
| 988 | parent = READ_ONCE(dentry->d_parent); |
| 989 | dir = d_inode_rcu(parent); |
| 990 | if (!dir) |
| 991 | return -ECHILD; |
| 992 | dvnode = AFS_FS_I(dir); |
| 993 | if (test_bit(AFS_VNODE_DELETED, &dvnode->flags)) |
| 994 | return -ECHILD; |
| 995 | |
| 996 | if (!afs_check_validity(dvnode)) |
| 997 | return -ECHILD; |
| 998 | |
| 999 | /* We only need to invalidate a dentry if the server's copy changed |
| 1000 | * behind our back. If we made the change, it's no problem. Note that |
| 1001 | * on a 32-bit system, we only have 32 bits in the dentry to store the |
| 1002 | * version. |
| 1003 | */ |
| 1004 | dir_version = (long)READ_ONCE(dvnode->status.data_version); |
| 1005 | de_version = (long)READ_ONCE(dentry->d_fsdata); |
| 1006 | if (de_version != dir_version) { |
| 1007 | dir_version = (long)READ_ONCE(dvnode->invalid_before); |
| 1008 | if (de_version - dir_version < 0) |
| 1009 | return -ECHILD; |
| 1010 | } |
| 1011 | |
| 1012 | /* Check to see if the vnode referred to by the dentry still |
| 1013 | * has a callback. |
| 1014 | */ |
| 1015 | if (d_really_is_positive(dentry)) { |
| 1016 | inode = d_inode_rcu(dentry); |
| 1017 | if (inode) { |
| 1018 | vnode = AFS_FS_I(inode); |
| 1019 | if (!afs_check_validity(vnode)) |
| 1020 | return -ECHILD; |
| 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | return 1; /* Still valid */ |
| 1025 | } |
| 1026 | |
| 1027 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | * check that a dentry lookup hit has found a valid entry |
| 1029 | * - NOTE! the hit can be a negative hit too, so we can't assume we have an |
| 1030 | * inode |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1031 | */ |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 1032 | static int afs_d_revalidate(struct dentry *dentry, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | { |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1034 | struct afs_vnode *vnode, *dir; |
Artem Bityutskiy | dd0d9a4 | 2009-07-09 10:44:30 +0100 | [diff] [blame] | 1035 | struct afs_fid uninitialized_var(fid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | struct dentry *parent; |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 1037 | struct inode *inode; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 1038 | struct key *key; |
David Howells | 9dd0b82 | 2019-07-30 14:38:52 +0100 | [diff] [blame] | 1039 | afs_dataversion_t dir_version; |
| 1040 | long de_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | int ret; |
| 1042 | |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 1043 | if (flags & LOOKUP_RCU) |
David Howells | a0753c2 | 2019-05-20 08:48:46 +0100 | [diff] [blame] | 1044 | return afs_d_revalidate_rcu(dentry); |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 1045 | |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 1046 | if (d_really_is_positive(dentry)) { |
| 1047 | vnode = AFS_FS_I(d_inode(dentry)); |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 1048 | _enter("{v={%llx:%llu} n=%pd fl=%lx},", |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 1049 | vnode->fid.vid, vnode->fid.vnode, dentry, |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1050 | vnode->flags); |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 1051 | } else { |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 1052 | _enter("{neg n=%pd}", dentry); |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 1053 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1055 | key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell); |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 1056 | if (IS_ERR(key)) |
| 1057 | key = NULL; |
| 1058 | |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 1059 | if (d_really_is_positive(dentry)) { |
| 1060 | inode = d_inode(dentry); |
| 1061 | if (inode) { |
| 1062 | vnode = AFS_FS_I(inode); |
| 1063 | afs_validate(vnode, key); |
| 1064 | if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) |
| 1065 | goto out_bad; |
| 1066 | } |
| 1067 | } |
| 1068 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | /* lock down the parent dentry so we can peer at it */ |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 1070 | parent = dget_parent(dentry); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1071 | dir = AFS_FS_I(d_inode(parent)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1073 | /* validate the parent directory */ |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 1074 | afs_validate(dir, key); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1075 | |
| 1076 | if (test_bit(AFS_VNODE_DELETED, &dir->flags)) { |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 1077 | _debug("%pd: parent dir deleted", dentry); |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 1078 | goto out_bad_parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | } |
| 1080 | |
David Howells | a4ff740 | 2018-04-06 14:17:24 +0100 | [diff] [blame] | 1081 | /* We only need to invalidate a dentry if the server's copy changed |
| 1082 | * behind our back. If we made the change, it's no problem. Note that |
| 1083 | * on a 32-bit system, we only have 32 bits in the dentry to store the |
| 1084 | * version. |
| 1085 | */ |
David Howells | 9dd0b82 | 2019-07-30 14:38:52 +0100 | [diff] [blame] | 1086 | dir_version = dir->status.data_version; |
David Howells | a4ff740 | 2018-04-06 14:17:24 +0100 | [diff] [blame] | 1087 | de_version = (long)dentry->d_fsdata; |
David Howells | 9dd0b82 | 2019-07-30 14:38:52 +0100 | [diff] [blame] | 1088 | if (de_version == (long)dir_version) |
David Howells | 5dc8485 | 2019-07-30 14:38:51 +0100 | [diff] [blame] | 1089 | goto out_valid_noupdate; |
David Howells | a4ff740 | 2018-04-06 14:17:24 +0100 | [diff] [blame] | 1090 | |
David Howells | 9dd0b82 | 2019-07-30 14:38:52 +0100 | [diff] [blame] | 1091 | dir_version = dir->invalid_before; |
| 1092 | if (de_version - (long)dir_version >= 0) |
David Howells | a4ff740 | 2018-04-06 14:17:24 +0100 | [diff] [blame] | 1093 | goto out_valid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1095 | _debug("dir modified"); |
David Howells | d55b4da | 2018-04-06 14:17:24 +0100 | [diff] [blame] | 1096 | afs_stat_v(dir, n_reval); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1097 | |
| 1098 | /* search the directory for this vnode */ |
David Howells | 9dd0b82 | 2019-07-30 14:38:52 +0100 | [diff] [blame] | 1099 | ret = afs_do_lookup_one(&dir->vfs_inode, dentry, &fid, key, &dir_version); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1100 | switch (ret) { |
| 1101 | case 0: |
| 1102 | /* the filename maps to something */ |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1103 | if (d_really_is_negative(dentry)) |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 1104 | goto out_bad_parent; |
| 1105 | inode = d_inode(dentry); |
| 1106 | if (is_bad_inode(inode)) { |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 1107 | printk("kAFS: afs_d_revalidate: %pd2 has bad inode\n", |
| 1108 | dentry); |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 1109 | goto out_bad_parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | } |
| 1111 | |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 1112 | vnode = AFS_FS_I(inode); |
| 1113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | /* if the vnode ID has changed, then the dirent points to a |
| 1115 | * different file */ |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 1116 | if (fid.vnode != vnode->fid.vnode) { |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 1117 | _debug("%pd: dirent changed [%llu != %llu]", |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 1118 | dentry, fid.vnode, |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 1119 | vnode->fid.vnode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | goto not_found; |
| 1121 | } |
| 1122 | |
| 1123 | /* if the vnode ID uniqifier has changed, then the file has |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1124 | * been deleted and replaced, and the original vnode ID has |
| 1125 | * been reused */ |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 1126 | if (fid.unique != vnode->fid.unique) { |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 1127 | _debug("%pd: file deleted (uq %u -> %u I:%u)", |
| 1128 | dentry, fid.unique, |
Jean Noel Cordenner | 7a22422 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1129 | vnode->fid.unique, |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 1130 | vnode->vfs_inode.i_generation); |
| 1131 | write_seqlock(&vnode->cb_lock); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 1132 | set_bit(AFS_VNODE_DELETED, &vnode->flags); |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 1133 | write_sequnlock(&vnode->cb_lock); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1134 | goto not_found; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1135 | } |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1136 | goto out_valid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1138 | case -ENOENT: |
| 1139 | /* the filename is unknown */ |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 1140 | _debug("%pd: dirent not found", dentry); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1141 | if (d_really_is_positive(dentry)) |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1142 | goto not_found; |
| 1143 | goto out_valid; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 1144 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1145 | default: |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 1146 | _debug("failed to iterate dir %pd: %d", |
| 1147 | parent, ret); |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 1148 | goto out_bad_parent; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 1149 | } |
| 1150 | |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 1151 | out_valid: |
David Howells | 9dd0b82 | 2019-07-30 14:38:52 +0100 | [diff] [blame] | 1152 | dentry->d_fsdata = (void *)(unsigned long)dir_version; |
David Howells | 5dc8485 | 2019-07-30 14:38:51 +0100 | [diff] [blame] | 1153 | out_valid_noupdate: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | dput(parent); |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 1155 | key_put(key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1156 | _leave(" = 1 [valid]"); |
| 1157 | return 1; |
| 1158 | |
| 1159 | /* the dirent, if it exists, now points to a different vnode */ |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 1160 | not_found: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | spin_lock(&dentry->d_lock); |
| 1162 | dentry->d_flags |= DCACHE_NFSFS_RENAMED; |
| 1163 | spin_unlock(&dentry->d_lock); |
| 1164 | |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 1165 | out_bad_parent: |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 1166 | _debug("dropping dentry %pd2", dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | dput(parent); |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 1168 | out_bad: |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 1169 | key_put(key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | |
| 1171 | _leave(" = 0 [bad]"); |
| 1172 | return 0; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 1173 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1174 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | /* |
| 1176 | * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't |
| 1177 | * sleep) |
| 1178 | * - called from dput() when d_count is going to 0. |
| 1179 | * - return 1 to request dentry be unhashed, 0 otherwise |
| 1180 | */ |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 1181 | static int afs_d_delete(const struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | { |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 1183 | _enter("%pd", dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | |
| 1185 | if (dentry->d_flags & DCACHE_NFSFS_RENAMED) |
| 1186 | goto zap; |
| 1187 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1188 | if (d_really_is_positive(dentry) && |
| 1189 | (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(d_inode(dentry))->flags) || |
| 1190 | test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(d_inode(dentry))->flags))) |
wanglei | bec5eb6 | 2010-08-11 09:38:04 +0100 | [diff] [blame] | 1191 | goto zap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | |
| 1193 | _leave(" = 0 [keep]"); |
| 1194 | return 0; |
| 1195 | |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 1196 | zap: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | _leave(" = 1 [zap]"); |
| 1198 | return 1; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 1199 | } |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1200 | |
| 1201 | /* |
David Howells | 79ddbfa | 2019-04-25 14:26:51 +0100 | [diff] [blame] | 1202 | * Clean up sillyrename files on dentry removal. |
| 1203 | */ |
| 1204 | static void afs_d_iput(struct dentry *dentry, struct inode *inode) |
| 1205 | { |
| 1206 | if (dentry->d_flags & DCACHE_NFSFS_RENAMED) |
| 1207 | afs_silly_iput(dentry, inode); |
| 1208 | iput(inode); |
| 1209 | } |
| 1210 | |
| 1211 | /* |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1212 | * handle dentry release |
| 1213 | */ |
David Howells | 66c7e1d | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 1214 | void afs_d_release(struct dentry *dentry) |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1215 | { |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 1216 | _enter("%pd", dentry); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | /* |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1220 | * Create a new inode for create/mkdir/symlink |
| 1221 | */ |
| 1222 | static void afs_vnode_new_inode(struct afs_fs_cursor *fc, |
| 1223 | struct dentry *new_dentry, |
David Howells | b835915 | 2019-05-14 12:23:43 +0100 | [diff] [blame] | 1224 | struct afs_iget_data *new_data, |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1225 | struct afs_status_cb *new_scb) |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1226 | { |
David Howells | 5a81327 | 2018-04-06 14:17:26 +0100 | [diff] [blame] | 1227 | struct afs_vnode *vnode; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1228 | struct inode *inode; |
| 1229 | |
| 1230 | if (fc->ac.error < 0) |
| 1231 | return; |
| 1232 | |
| 1233 | inode = afs_iget(fc->vnode->vfs_inode.i_sb, fc->key, |
David Howells | b835915 | 2019-05-14 12:23:43 +0100 | [diff] [blame] | 1234 | new_data, new_scb, fc->cbi, fc->vnode); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1235 | if (IS_ERR(inode)) { |
| 1236 | /* ENOMEM or EINTR at a really inconvenient time - just abandon |
| 1237 | * the new directory on the server. |
| 1238 | */ |
| 1239 | fc->ac.error = PTR_ERR(inode); |
| 1240 | return; |
| 1241 | } |
| 1242 | |
David Howells | 5a81327 | 2018-04-06 14:17:26 +0100 | [diff] [blame] | 1243 | vnode = AFS_FS_I(inode); |
| 1244 | set_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags); |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1245 | if (fc->ac.error == 0) |
| 1246 | afs_cache_permit(vnode, fc->key, vnode->cb_break, new_scb); |
David Howells | 73116df | 2018-11-13 23:20:35 +0000 | [diff] [blame] | 1247 | d_instantiate(new_dentry, inode); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1248 | } |
| 1249 | |
David Howells | b835915 | 2019-05-14 12:23:43 +0100 | [diff] [blame] | 1250 | static void afs_prep_for_new_inode(struct afs_fs_cursor *fc, |
| 1251 | struct afs_iget_data *iget_data) |
| 1252 | { |
| 1253 | iget_data->volume = fc->vnode->volume; |
| 1254 | iget_data->cb_v_break = fc->vnode->volume->cb_v_break; |
| 1255 | iget_data->cb_s_break = fc->cbi->server->cb_s_break; |
| 1256 | } |
| 1257 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1258 | /* |
David Howells | 9dd0b82 | 2019-07-30 14:38:52 +0100 | [diff] [blame] | 1259 | * Note that a dentry got changed. We need to set d_fsdata to the data version |
| 1260 | * number derived from the result of the operation. It doesn't matter if |
| 1261 | * d_fsdata goes backwards as we'll just revalidate. |
| 1262 | */ |
| 1263 | static void afs_update_dentry_version(struct afs_fs_cursor *fc, |
| 1264 | struct dentry *dentry, |
| 1265 | struct afs_status_cb *scb) |
| 1266 | { |
| 1267 | if (fc->ac.error == 0) |
| 1268 | dentry->d_fsdata = |
| 1269 | (void *)(unsigned long)scb->status.data_version; |
| 1270 | } |
| 1271 | |
| 1272 | /* |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1273 | * create a directory on an AFS filesystem |
| 1274 | */ |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 1275 | static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1276 | { |
David Howells | b835915 | 2019-05-14 12:23:43 +0100 | [diff] [blame] | 1277 | struct afs_iget_data iget_data; |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1278 | struct afs_status_cb *scb; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1279 | struct afs_fs_cursor fc; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1280 | struct afs_vnode *dvnode = AFS_FS_I(dir); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1281 | struct key *key; |
| 1282 | int ret; |
| 1283 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1284 | mode |= S_IFDIR; |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1285 | |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 1286 | _enter("{%llx:%llu},{%pd},%ho", |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 1287 | dvnode->fid.vid, dvnode->fid.vnode, dentry, mode); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1288 | |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1289 | ret = -ENOMEM; |
| 1290 | scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL); |
| 1291 | if (!scb) |
| 1292 | goto error; |
| 1293 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1294 | key = afs_request_key(dvnode->volume->cell); |
| 1295 | if (IS_ERR(key)) { |
| 1296 | ret = PTR_ERR(key); |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1297 | goto error_scb; |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1298 | } |
| 1299 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1300 | ret = -ERESTARTSYS; |
David Howells | 20b8391 | 2019-05-08 16:16:31 +0100 | [diff] [blame] | 1301 | if (afs_begin_vnode_operation(&fc, dvnode, key, true)) { |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1302 | afs_dataversion_t data_version = dvnode->status.data_version + 1; |
| 1303 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1304 | while (afs_select_fileserver(&fc)) { |
David Howells | 68251f0 | 2018-05-12 22:31:33 +0100 | [diff] [blame] | 1305 | fc.cb_break = afs_calc_vnode_cb_break(dvnode); |
David Howells | b835915 | 2019-05-14 12:23:43 +0100 | [diff] [blame] | 1306 | afs_prep_for_new_inode(&fc, &iget_data); |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1307 | afs_fs_create(&fc, dentry->d_name.name, mode, |
David Howells | b835915 | 2019-05-14 12:23:43 +0100 | [diff] [blame] | 1308 | &scb[0], &iget_data.fid, &scb[1]); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1309 | } |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1310 | |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1311 | afs_check_for_remote_deletion(&fc, dvnode); |
| 1312 | afs_vnode_commit_status(&fc, dvnode, fc.cb_break, |
| 1313 | &data_version, &scb[0]); |
David Howells | 9dd0b82 | 2019-07-30 14:38:52 +0100 | [diff] [blame] | 1314 | afs_update_dentry_version(&fc, dentry, &scb[0]); |
David Howells | b835915 | 2019-05-14 12:23:43 +0100 | [diff] [blame] | 1315 | afs_vnode_new_inode(&fc, dentry, &iget_data, &scb[1]); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1316 | ret = afs_end_vnode_operation(&fc); |
| 1317 | if (ret < 0) |
| 1318 | goto error_key; |
David Howells | 4433b69 | 2017-11-20 22:41:00 +0000 | [diff] [blame] | 1319 | } else { |
| 1320 | goto error_key; |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1321 | } |
| 1322 | |
David Howells | 63a4681 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 1323 | if (ret == 0 && |
| 1324 | test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) |
David Howells | b835915 | 2019-05-14 12:23:43 +0100 | [diff] [blame] | 1325 | afs_edit_dir_add(dvnode, &dentry->d_name, &iget_data.fid, |
David Howells | 63a4681 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 1326 | afs_edit_dir_for_create); |
| 1327 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1328 | key_put(key); |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1329 | kfree(scb); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1330 | _leave(" = 0"); |
| 1331 | return 0; |
| 1332 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1333 | error_key: |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1334 | key_put(key); |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1335 | error_scb: |
| 1336 | kfree(scb); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1337 | error: |
| 1338 | d_drop(dentry); |
| 1339 | _leave(" = %d", ret); |
| 1340 | return ret; |
| 1341 | } |
| 1342 | |
| 1343 | /* |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1344 | * Remove a subdir from a directory. |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1345 | */ |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1346 | static void afs_dir_remove_subdir(struct dentry *dentry) |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1347 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1348 | if (d_really_is_positive(dentry)) { |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1349 | struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry)); |
| 1350 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1351 | clear_nlink(&vnode->vfs_inode); |
| 1352 | set_bit(AFS_VNODE_DELETED, &vnode->flags); |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 1353 | clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags); |
David Howells | 63a4681 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 1354 | clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1355 | } |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | /* |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1359 | * remove a directory from an AFS filesystem |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1360 | */ |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1361 | static int afs_rmdir(struct inode *dir, struct dentry *dentry) |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1362 | { |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1363 | struct afs_status_cb *scb; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1364 | struct afs_fs_cursor fc; |
David Howells | f58db83 | 2018-10-20 00:57:58 +0100 | [diff] [blame] | 1365 | struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode = NULL; |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1366 | struct key *key; |
| 1367 | int ret; |
| 1368 | |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 1369 | _enter("{%llx:%llu},{%pd}", |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 1370 | dvnode->fid.vid, dvnode->fid.vnode, dentry); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1371 | |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1372 | scb = kzalloc(sizeof(struct afs_status_cb), GFP_KERNEL); |
| 1373 | if (!scb) |
| 1374 | return -ENOMEM; |
| 1375 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1376 | key = afs_request_key(dvnode->volume->cell); |
| 1377 | if (IS_ERR(key)) { |
| 1378 | ret = PTR_ERR(key); |
| 1379 | goto error; |
| 1380 | } |
| 1381 | |
David Howells | f58db83 | 2018-10-20 00:57:58 +0100 | [diff] [blame] | 1382 | /* Try to make sure we have a callback promise on the victim. */ |
| 1383 | if (d_really_is_positive(dentry)) { |
| 1384 | vnode = AFS_FS_I(d_inode(dentry)); |
| 1385 | ret = afs_validate(vnode, key); |
| 1386 | if (ret < 0) |
| 1387 | goto error_key; |
| 1388 | } |
| 1389 | |
David Howells | 79ddbfa | 2019-04-25 14:26:51 +0100 | [diff] [blame] | 1390 | if (vnode) { |
| 1391 | ret = down_write_killable(&vnode->rmdir_lock); |
| 1392 | if (ret < 0) |
| 1393 | goto error_key; |
| 1394 | } |
| 1395 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1396 | ret = -ERESTARTSYS; |
David Howells | 20b8391 | 2019-05-08 16:16:31 +0100 | [diff] [blame] | 1397 | if (afs_begin_vnode_operation(&fc, dvnode, key, true)) { |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1398 | afs_dataversion_t data_version = dvnode->status.data_version + 1; |
| 1399 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1400 | while (afs_select_fileserver(&fc)) { |
David Howells | 68251f0 | 2018-05-12 22:31:33 +0100 | [diff] [blame] | 1401 | fc.cb_break = afs_calc_vnode_cb_break(dvnode); |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1402 | afs_fs_remove(&fc, vnode, dentry->d_name.name, true, scb); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1403 | } |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1404 | |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1405 | afs_vnode_commit_status(&fc, dvnode, fc.cb_break, |
| 1406 | &data_version, scb); |
David Howells | 9dd0b82 | 2019-07-30 14:38:52 +0100 | [diff] [blame] | 1407 | afs_update_dentry_version(&fc, dentry, scb); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1408 | ret = afs_end_vnode_operation(&fc); |
David Howells | 63a4681 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 1409 | if (ret == 0) { |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1410 | afs_dir_remove_subdir(dentry); |
David Howells | 63a4681 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 1411 | if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) |
| 1412 | afs_edit_dir_remove(dvnode, &dentry->d_name, |
| 1413 | afs_edit_dir_for_rmdir); |
| 1414 | } |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1415 | } |
| 1416 | |
David Howells | 79ddbfa | 2019-04-25 14:26:51 +0100 | [diff] [blame] | 1417 | if (vnode) |
| 1418 | up_write(&vnode->rmdir_lock); |
David Howells | f58db83 | 2018-10-20 00:57:58 +0100 | [diff] [blame] | 1419 | error_key: |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1420 | key_put(key); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1421 | error: |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1422 | kfree(scb); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1423 | return ret; |
| 1424 | } |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1425 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1426 | /* |
| 1427 | * Remove a link to a file or symlink from a directory. |
| 1428 | * |
| 1429 | * If the file was not deleted due to excess hard links, the fileserver will |
| 1430 | * break the callback promise on the file - if it had one - before it returns |
| 1431 | * to us, and if it was deleted, it won't |
| 1432 | * |
| 1433 | * However, if we didn't have a callback promise outstanding, or it was |
| 1434 | * outstanding on a different server, then it won't break it either... |
| 1435 | */ |
David Howells | a38a755 | 2019-05-14 12:29:11 +0100 | [diff] [blame] | 1436 | static int afs_dir_remove_link(struct afs_vnode *dvnode, struct dentry *dentry, |
| 1437 | struct key *key) |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1438 | { |
| 1439 | int ret = 0; |
| 1440 | |
| 1441 | if (d_really_is_positive(dentry)) { |
| 1442 | struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry)); |
| 1443 | |
David Howells | 30062bd | 2018-10-20 00:57:58 +0100 | [diff] [blame] | 1444 | if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) { |
| 1445 | /* Already done */ |
David Howells | a38a755 | 2019-05-14 12:29:11 +0100 | [diff] [blame] | 1446 | } else if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) { |
| 1447 | write_seqlock(&vnode->cb_lock); |
David Howells | 440fbc3 | 2018-01-02 10:02:19 +0000 | [diff] [blame] | 1448 | drop_nlink(&vnode->vfs_inode); |
| 1449 | if (vnode->vfs_inode.i_nlink == 0) { |
| 1450 | set_bit(AFS_VNODE_DELETED, &vnode->flags); |
David Howells | 051d252 | 2019-06-20 18:12:16 +0100 | [diff] [blame] | 1451 | __afs_break_callback(vnode, afs_cb_break_for_unlink); |
David Howells | 440fbc3 | 2018-01-02 10:02:19 +0000 | [diff] [blame] | 1452 | } |
David Howells | a38a755 | 2019-05-14 12:29:11 +0100 | [diff] [blame] | 1453 | write_sequnlock(&vnode->cb_lock); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1454 | ret = 0; |
David Howells | 440fbc3 | 2018-01-02 10:02:19 +0000 | [diff] [blame] | 1455 | } else { |
David Howells | 051d252 | 2019-06-20 18:12:16 +0100 | [diff] [blame] | 1456 | afs_break_callback(vnode, afs_cb_break_for_unlink); |
David Howells | 440fbc3 | 2018-01-02 10:02:19 +0000 | [diff] [blame] | 1457 | |
| 1458 | if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) |
| 1459 | kdebug("AFS_VNODE_DELETED"); |
| 1460 | |
| 1461 | ret = afs_validate(vnode, key); |
| 1462 | if (ret == -ESTALE) |
| 1463 | ret = 0; |
| 1464 | } |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1465 | _debug("nlink %d [val %d]", vnode->vfs_inode.i_nlink, ret); |
| 1466 | } |
| 1467 | |
| 1468 | return ret; |
| 1469 | } |
| 1470 | |
| 1471 | /* |
| 1472 | * Remove a file or symlink from an AFS filesystem. |
| 1473 | */ |
| 1474 | static int afs_unlink(struct inode *dir, struct dentry *dentry) |
| 1475 | { |
| 1476 | struct afs_fs_cursor fc; |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1477 | struct afs_status_cb *scb; |
David Howells | fa59f52 | 2019-06-20 18:12:16 +0100 | [diff] [blame] | 1478 | struct afs_vnode *dvnode = AFS_FS_I(dir); |
| 1479 | struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry)); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1480 | struct key *key; |
David Howells | 79ddbfa | 2019-04-25 14:26:51 +0100 | [diff] [blame] | 1481 | bool need_rehash = false; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1482 | int ret; |
| 1483 | |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 1484 | _enter("{%llx:%llu},{%pd}", |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1485 | dvnode->fid.vid, dvnode->fid.vnode, dentry); |
| 1486 | |
| 1487 | if (dentry->d_name.len >= AFSNAMEMAX) |
| 1488 | return -ENAMETOOLONG; |
| 1489 | |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1490 | ret = -ENOMEM; |
| 1491 | scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL); |
| 1492 | if (!scb) |
| 1493 | goto error; |
| 1494 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1495 | key = afs_request_key(dvnode->volume->cell); |
| 1496 | if (IS_ERR(key)) { |
| 1497 | ret = PTR_ERR(key); |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1498 | goto error_scb; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1499 | } |
| 1500 | |
| 1501 | /* Try to make sure we have a callback promise on the victim. */ |
David Howells | fa59f52 | 2019-06-20 18:12:16 +0100 | [diff] [blame] | 1502 | ret = afs_validate(vnode, key); |
| 1503 | if (ret < 0) |
| 1504 | goto error_key; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1505 | |
David Howells | 79ddbfa | 2019-04-25 14:26:51 +0100 | [diff] [blame] | 1506 | spin_lock(&dentry->d_lock); |
David Howells | fa59f52 | 2019-06-20 18:12:16 +0100 | [diff] [blame] | 1507 | if (d_count(dentry) > 1) { |
David Howells | 79ddbfa | 2019-04-25 14:26:51 +0100 | [diff] [blame] | 1508 | spin_unlock(&dentry->d_lock); |
| 1509 | /* Start asynchronous writeout of the inode */ |
| 1510 | write_inode_now(d_inode(dentry), 0); |
| 1511 | ret = afs_sillyrename(dvnode, vnode, dentry, key); |
| 1512 | goto error_key; |
| 1513 | } |
| 1514 | if (!d_unhashed(dentry)) { |
| 1515 | /* Prevent a race with RCU lookup. */ |
| 1516 | __d_drop(dentry); |
| 1517 | need_rehash = true; |
| 1518 | } |
| 1519 | spin_unlock(&dentry->d_lock); |
| 1520 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1521 | ret = -ERESTARTSYS; |
David Howells | 20b8391 | 2019-05-08 16:16:31 +0100 | [diff] [blame] | 1522 | if (afs_begin_vnode_operation(&fc, dvnode, key, true)) { |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1523 | afs_dataversion_t data_version = dvnode->status.data_version + 1; |
David Howells | a38a755 | 2019-05-14 12:29:11 +0100 | [diff] [blame] | 1524 | afs_dataversion_t data_version_2 = vnode->status.data_version; |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1525 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1526 | while (afs_select_fileserver(&fc)) { |
David Howells | 68251f0 | 2018-05-12 22:31:33 +0100 | [diff] [blame] | 1527 | fc.cb_break = afs_calc_vnode_cb_break(dvnode); |
David Howells | a38a755 | 2019-05-14 12:29:11 +0100 | [diff] [blame] | 1528 | fc.cb_break_2 = afs_calc_vnode_cb_break(vnode); |
David Howells | 30062bd | 2018-10-20 00:57:58 +0100 | [diff] [blame] | 1529 | |
| 1530 | if (test_bit(AFS_SERVER_FL_IS_YFS, &fc.cbi->server->flags) && |
| 1531 | !test_bit(AFS_SERVER_FL_NO_RM2, &fc.cbi->server->flags)) { |
| 1532 | yfs_fs_remove_file2(&fc, vnode, dentry->d_name.name, |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1533 | &scb[0], &scb[1]); |
David Howells | 30062bd | 2018-10-20 00:57:58 +0100 | [diff] [blame] | 1534 | if (fc.ac.error != -ECONNABORTED || |
| 1535 | fc.ac.abort_code != RXGEN_OPCODE) |
| 1536 | continue; |
| 1537 | set_bit(AFS_SERVER_FL_NO_RM2, &fc.cbi->server->flags); |
| 1538 | } |
| 1539 | |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1540 | afs_fs_remove(&fc, vnode, dentry->d_name.name, false, &scb[0]); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1541 | } |
| 1542 | |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1543 | afs_vnode_commit_status(&fc, dvnode, fc.cb_break, |
| 1544 | &data_version, &scb[0]); |
David Howells | a38a755 | 2019-05-14 12:29:11 +0100 | [diff] [blame] | 1545 | afs_vnode_commit_status(&fc, vnode, fc.cb_break_2, |
| 1546 | &data_version_2, &scb[1]); |
David Howells | 9dd0b82 | 2019-07-30 14:38:52 +0100 | [diff] [blame] | 1547 | afs_update_dentry_version(&fc, dentry, &scb[0]); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1548 | ret = afs_end_vnode_operation(&fc); |
David Howells | a38a755 | 2019-05-14 12:29:11 +0100 | [diff] [blame] | 1549 | if (ret == 0 && !(scb[1].have_status || scb[1].have_error)) |
| 1550 | ret = afs_dir_remove_link(dvnode, dentry, key); |
David Howells | 63a4681 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 1551 | if (ret == 0 && |
| 1552 | test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) |
| 1553 | afs_edit_dir_remove(dvnode, &dentry->d_name, |
| 1554 | afs_edit_dir_for_unlink); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1555 | } |
| 1556 | |
David Howells | 79ddbfa | 2019-04-25 14:26:51 +0100 | [diff] [blame] | 1557 | if (need_rehash && ret < 0 && ret != -ENOENT) |
| 1558 | d_rehash(dentry); |
| 1559 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1560 | error_key: |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1561 | key_put(key); |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1562 | error_scb: |
| 1563 | kfree(scb); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1564 | error: |
| 1565 | _leave(" = %d", ret); |
| 1566 | return ret; |
| 1567 | } |
| 1568 | |
| 1569 | /* |
| 1570 | * create a regular file on an AFS filesystem |
| 1571 | */ |
Al Viro | 4acdaf2 | 2011-07-26 01:42:34 -0400 | [diff] [blame] | 1572 | static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode, |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 1573 | bool excl) |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1574 | { |
David Howells | b835915 | 2019-05-14 12:23:43 +0100 | [diff] [blame] | 1575 | struct afs_iget_data iget_data; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1576 | struct afs_fs_cursor fc; |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1577 | struct afs_status_cb *scb; |
Colin Ian King | 43dd388 | 2017-11-20 13:58:20 +0000 | [diff] [blame] | 1578 | struct afs_vnode *dvnode = AFS_FS_I(dir); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1579 | struct key *key; |
| 1580 | int ret; |
| 1581 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1582 | mode |= S_IFREG; |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1583 | |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 1584 | _enter("{%llx:%llu},{%pd},%ho,", |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 1585 | dvnode->fid.vid, dvnode->fid.vnode, dentry, mode); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1586 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1587 | ret = -ENAMETOOLONG; |
| 1588 | if (dentry->d_name.len >= AFSNAMEMAX) |
| 1589 | goto error; |
| 1590 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1591 | key = afs_request_key(dvnode->volume->cell); |
| 1592 | if (IS_ERR(key)) { |
| 1593 | ret = PTR_ERR(key); |
| 1594 | goto error; |
| 1595 | } |
| 1596 | |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1597 | ret = -ENOMEM; |
| 1598 | scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL); |
| 1599 | if (!scb) |
| 1600 | goto error_scb; |
| 1601 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1602 | ret = -ERESTARTSYS; |
David Howells | 20b8391 | 2019-05-08 16:16:31 +0100 | [diff] [blame] | 1603 | if (afs_begin_vnode_operation(&fc, dvnode, key, true)) { |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1604 | afs_dataversion_t data_version = dvnode->status.data_version + 1; |
| 1605 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1606 | while (afs_select_fileserver(&fc)) { |
David Howells | 68251f0 | 2018-05-12 22:31:33 +0100 | [diff] [blame] | 1607 | fc.cb_break = afs_calc_vnode_cb_break(dvnode); |
David Howells | b835915 | 2019-05-14 12:23:43 +0100 | [diff] [blame] | 1608 | afs_prep_for_new_inode(&fc, &iget_data); |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1609 | afs_fs_create(&fc, dentry->d_name.name, mode, |
David Howells | b835915 | 2019-05-14 12:23:43 +0100 | [diff] [blame] | 1610 | &scb[0], &iget_data.fid, &scb[1]); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1611 | } |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1612 | |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1613 | afs_check_for_remote_deletion(&fc, dvnode); |
| 1614 | afs_vnode_commit_status(&fc, dvnode, fc.cb_break, |
| 1615 | &data_version, &scb[0]); |
David Howells | 9dd0b82 | 2019-07-30 14:38:52 +0100 | [diff] [blame] | 1616 | afs_update_dentry_version(&fc, dentry, &scb[0]); |
David Howells | b835915 | 2019-05-14 12:23:43 +0100 | [diff] [blame] | 1617 | afs_vnode_new_inode(&fc, dentry, &iget_data, &scb[1]); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1618 | ret = afs_end_vnode_operation(&fc); |
| 1619 | if (ret < 0) |
| 1620 | goto error_key; |
David Howells | 4433b69 | 2017-11-20 22:41:00 +0000 | [diff] [blame] | 1621 | } else { |
| 1622 | goto error_key; |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1623 | } |
| 1624 | |
David Howells | 63a4681 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 1625 | if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) |
David Howells | b835915 | 2019-05-14 12:23:43 +0100 | [diff] [blame] | 1626 | afs_edit_dir_add(dvnode, &dentry->d_name, &iget_data.fid, |
David Howells | 63a4681 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 1627 | afs_edit_dir_for_create); |
| 1628 | |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1629 | kfree(scb); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1630 | key_put(key); |
| 1631 | _leave(" = 0"); |
| 1632 | return 0; |
| 1633 | |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1634 | error_scb: |
| 1635 | kfree(scb); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1636 | error_key: |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1637 | key_put(key); |
| 1638 | error: |
| 1639 | d_drop(dentry); |
| 1640 | _leave(" = %d", ret); |
| 1641 | return ret; |
| 1642 | } |
| 1643 | |
| 1644 | /* |
| 1645 | * create a hard link between files in an AFS filesystem |
| 1646 | */ |
| 1647 | static int afs_link(struct dentry *from, struct inode *dir, |
| 1648 | struct dentry *dentry) |
| 1649 | { |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1650 | struct afs_fs_cursor fc; |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1651 | struct afs_status_cb *scb; |
| 1652 | struct afs_vnode *dvnode = AFS_FS_I(dir); |
| 1653 | struct afs_vnode *vnode = AFS_FS_I(d_inode(from)); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1654 | struct key *key; |
| 1655 | int ret; |
| 1656 | |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 1657 | _enter("{%llx:%llu},{%llx:%llu},{%pd}", |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1658 | vnode->fid.vid, vnode->fid.vnode, |
| 1659 | dvnode->fid.vid, dvnode->fid.vnode, |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 1660 | dentry); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1661 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1662 | ret = -ENAMETOOLONG; |
| 1663 | if (dentry->d_name.len >= AFSNAMEMAX) |
| 1664 | goto error; |
| 1665 | |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1666 | ret = -ENOMEM; |
| 1667 | scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL); |
| 1668 | if (!scb) |
| 1669 | goto error; |
| 1670 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1671 | key = afs_request_key(dvnode->volume->cell); |
| 1672 | if (IS_ERR(key)) { |
| 1673 | ret = PTR_ERR(key); |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1674 | goto error_scb; |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1675 | } |
| 1676 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1677 | ret = -ERESTARTSYS; |
David Howells | 20b8391 | 2019-05-08 16:16:31 +0100 | [diff] [blame] | 1678 | if (afs_begin_vnode_operation(&fc, dvnode, key, true)) { |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1679 | afs_dataversion_t data_version = dvnode->status.data_version + 1; |
| 1680 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1681 | if (mutex_lock_interruptible_nested(&vnode->io_lock, 1) < 0) { |
| 1682 | afs_end_vnode_operation(&fc); |
David Howells | bc1527d | 2017-11-20 23:04:08 +0000 | [diff] [blame] | 1683 | goto error_key; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1684 | } |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1685 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1686 | while (afs_select_fileserver(&fc)) { |
David Howells | 68251f0 | 2018-05-12 22:31:33 +0100 | [diff] [blame] | 1687 | fc.cb_break = afs_calc_vnode_cb_break(dvnode); |
| 1688 | fc.cb_break_2 = afs_calc_vnode_cb_break(vnode); |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1689 | afs_fs_link(&fc, vnode, dentry->d_name.name, |
| 1690 | &scb[0], &scb[1]); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1691 | } |
| 1692 | |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1693 | afs_vnode_commit_status(&fc, dvnode, fc.cb_break, |
| 1694 | &data_version, &scb[0]); |
| 1695 | afs_vnode_commit_status(&fc, vnode, fc.cb_break_2, |
| 1696 | NULL, &scb[1]); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1697 | ihold(&vnode->vfs_inode); |
David Howells | 9dd0b82 | 2019-07-30 14:38:52 +0100 | [diff] [blame] | 1698 | afs_update_dentry_version(&fc, dentry, &scb[0]); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1699 | d_instantiate(dentry, &vnode->vfs_inode); |
| 1700 | |
| 1701 | mutex_unlock(&vnode->io_lock); |
| 1702 | ret = afs_end_vnode_operation(&fc); |
| 1703 | if (ret < 0) |
| 1704 | goto error_key; |
David Howells | 4433b69 | 2017-11-20 22:41:00 +0000 | [diff] [blame] | 1705 | } else { |
| 1706 | goto error_key; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1707 | } |
| 1708 | |
David Howells | 63a4681 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 1709 | if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) |
| 1710 | afs_edit_dir_add(dvnode, &dentry->d_name, &vnode->fid, |
| 1711 | afs_edit_dir_for_link); |
| 1712 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1713 | key_put(key); |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1714 | kfree(scb); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1715 | _leave(" = 0"); |
| 1716 | return 0; |
| 1717 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1718 | error_key: |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1719 | key_put(key); |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1720 | error_scb: |
| 1721 | kfree(scb); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1722 | error: |
| 1723 | d_drop(dentry); |
| 1724 | _leave(" = %d", ret); |
| 1725 | return ret; |
| 1726 | } |
| 1727 | |
| 1728 | /* |
| 1729 | * create a symlink in an AFS filesystem |
| 1730 | */ |
| 1731 | static int afs_symlink(struct inode *dir, struct dentry *dentry, |
| 1732 | const char *content) |
| 1733 | { |
David Howells | b835915 | 2019-05-14 12:23:43 +0100 | [diff] [blame] | 1734 | struct afs_iget_data iget_data; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1735 | struct afs_fs_cursor fc; |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1736 | struct afs_status_cb *scb; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1737 | struct afs_vnode *dvnode = AFS_FS_I(dir); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1738 | struct key *key; |
| 1739 | int ret; |
| 1740 | |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 1741 | _enter("{%llx:%llu},{%pd},%s", |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 1742 | dvnode->fid.vid, dvnode->fid.vnode, dentry, |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1743 | content); |
| 1744 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1745 | ret = -ENAMETOOLONG; |
| 1746 | if (dentry->d_name.len >= AFSNAMEMAX) |
| 1747 | goto error; |
| 1748 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1749 | ret = -EINVAL; |
David Howells | 45222b9 | 2007-05-10 22:22:20 -0700 | [diff] [blame] | 1750 | if (strlen(content) >= AFSPATHMAX) |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1751 | goto error; |
| 1752 | |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1753 | ret = -ENOMEM; |
| 1754 | scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL); |
| 1755 | if (!scb) |
| 1756 | goto error; |
| 1757 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1758 | key = afs_request_key(dvnode->volume->cell); |
| 1759 | if (IS_ERR(key)) { |
| 1760 | ret = PTR_ERR(key); |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1761 | goto error_scb; |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1762 | } |
| 1763 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1764 | ret = -ERESTARTSYS; |
David Howells | 20b8391 | 2019-05-08 16:16:31 +0100 | [diff] [blame] | 1765 | if (afs_begin_vnode_operation(&fc, dvnode, key, true)) { |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1766 | afs_dataversion_t data_version = dvnode->status.data_version + 1; |
| 1767 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1768 | while (afs_select_fileserver(&fc)) { |
David Howells | 68251f0 | 2018-05-12 22:31:33 +0100 | [diff] [blame] | 1769 | fc.cb_break = afs_calc_vnode_cb_break(dvnode); |
David Howells | b835915 | 2019-05-14 12:23:43 +0100 | [diff] [blame] | 1770 | afs_prep_for_new_inode(&fc, &iget_data); |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1771 | afs_fs_symlink(&fc, dentry->d_name.name, content, |
David Howells | b835915 | 2019-05-14 12:23:43 +0100 | [diff] [blame] | 1772 | &scb[0], &iget_data.fid, &scb[1]); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1773 | } |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1774 | |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1775 | afs_check_for_remote_deletion(&fc, dvnode); |
| 1776 | afs_vnode_commit_status(&fc, dvnode, fc.cb_break, |
| 1777 | &data_version, &scb[0]); |
David Howells | 9dd0b82 | 2019-07-30 14:38:52 +0100 | [diff] [blame] | 1778 | afs_update_dentry_version(&fc, dentry, &scb[0]); |
David Howells | b835915 | 2019-05-14 12:23:43 +0100 | [diff] [blame] | 1779 | afs_vnode_new_inode(&fc, dentry, &iget_data, &scb[1]); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1780 | ret = afs_end_vnode_operation(&fc); |
| 1781 | if (ret < 0) |
| 1782 | goto error_key; |
David Howells | 4433b69 | 2017-11-20 22:41:00 +0000 | [diff] [blame] | 1783 | } else { |
| 1784 | goto error_key; |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1785 | } |
| 1786 | |
David Howells | 63a4681 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 1787 | if (test_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) |
David Howells | b835915 | 2019-05-14 12:23:43 +0100 | [diff] [blame] | 1788 | afs_edit_dir_add(dvnode, &dentry->d_name, &iget_data.fid, |
David Howells | 63a4681 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 1789 | afs_edit_dir_for_symlink); |
| 1790 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1791 | key_put(key); |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1792 | kfree(scb); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1793 | _leave(" = 0"); |
| 1794 | return 0; |
| 1795 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1796 | error_key: |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1797 | key_put(key); |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1798 | error_scb: |
| 1799 | kfree(scb); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1800 | error: |
| 1801 | d_drop(dentry); |
| 1802 | _leave(" = %d", ret); |
| 1803 | return ret; |
| 1804 | } |
| 1805 | |
| 1806 | /* |
| 1807 | * rename a file in an AFS filesystem and/or move it between directories |
| 1808 | */ |
| 1809 | static int afs_rename(struct inode *old_dir, struct dentry *old_dentry, |
Miklos Szeredi | 1cd66c9 | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 1810 | struct inode *new_dir, struct dentry *new_dentry, |
| 1811 | unsigned int flags) |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1812 | { |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1813 | struct afs_fs_cursor fc; |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1814 | struct afs_status_cb *scb; |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1815 | struct afs_vnode *orig_dvnode, *new_dvnode, *vnode; |
David Howells | 79ddbfa | 2019-04-25 14:26:51 +0100 | [diff] [blame] | 1816 | struct dentry *tmp = NULL, *rehash = NULL; |
| 1817 | struct inode *new_inode; |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1818 | struct key *key; |
David Howells | 63a4681 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 1819 | bool new_negative = d_is_negative(new_dentry); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1820 | int ret; |
| 1821 | |
Miklos Szeredi | 1cd66c9 | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 1822 | if (flags) |
| 1823 | return -EINVAL; |
| 1824 | |
David Howells | 79ddbfa | 2019-04-25 14:26:51 +0100 | [diff] [blame] | 1825 | /* Don't allow silly-rename files be moved around. */ |
| 1826 | if (old_dentry->d_flags & DCACHE_NFSFS_RENAMED) |
| 1827 | return -EINVAL; |
| 1828 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1829 | vnode = AFS_FS_I(d_inode(old_dentry)); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1830 | orig_dvnode = AFS_FS_I(old_dir); |
| 1831 | new_dvnode = AFS_FS_I(new_dir); |
| 1832 | |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 1833 | _enter("{%llx:%llu},{%llx:%llu},{%llx:%llu},{%pd}", |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1834 | orig_dvnode->fid.vid, orig_dvnode->fid.vnode, |
| 1835 | vnode->fid.vid, vnode->fid.vnode, |
| 1836 | new_dvnode->fid.vid, new_dvnode->fid.vnode, |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 1837 | new_dentry); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1838 | |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1839 | ret = -ENOMEM; |
| 1840 | scb = kcalloc(2, sizeof(struct afs_status_cb), GFP_KERNEL); |
| 1841 | if (!scb) |
| 1842 | goto error; |
| 1843 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1844 | key = afs_request_key(orig_dvnode->volume->cell); |
| 1845 | if (IS_ERR(key)) { |
| 1846 | ret = PTR_ERR(key); |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1847 | goto error_scb; |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1848 | } |
| 1849 | |
David Howells | 79ddbfa | 2019-04-25 14:26:51 +0100 | [diff] [blame] | 1850 | /* For non-directories, check whether the target is busy and if so, |
| 1851 | * make a copy of the dentry and then do a silly-rename. If the |
| 1852 | * silly-rename succeeds, the copied dentry is hashed and becomes the |
| 1853 | * new target. |
| 1854 | */ |
| 1855 | if (d_is_positive(new_dentry) && !d_is_dir(new_dentry)) { |
| 1856 | /* To prevent any new references to the target during the |
| 1857 | * rename, we unhash the dentry in advance. |
| 1858 | */ |
| 1859 | if (!d_unhashed(new_dentry)) { |
| 1860 | d_drop(new_dentry); |
| 1861 | rehash = new_dentry; |
| 1862 | } |
| 1863 | |
| 1864 | if (d_count(new_dentry) > 2) { |
| 1865 | /* copy the target dentry's name */ |
| 1866 | ret = -ENOMEM; |
| 1867 | tmp = d_alloc(new_dentry->d_parent, |
| 1868 | &new_dentry->d_name); |
| 1869 | if (!tmp) |
| 1870 | goto error_rehash; |
| 1871 | |
| 1872 | ret = afs_sillyrename(new_dvnode, |
| 1873 | AFS_FS_I(d_inode(new_dentry)), |
| 1874 | new_dentry, key); |
| 1875 | if (ret) |
| 1876 | goto error_rehash; |
| 1877 | |
| 1878 | new_dentry = tmp; |
| 1879 | rehash = NULL; |
| 1880 | new_negative = true; |
David Howells | 79ddbfa | 2019-04-25 14:26:51 +0100 | [diff] [blame] | 1881 | } |
| 1882 | } |
| 1883 | |
David Howells | 9dd0b82 | 2019-07-30 14:38:52 +0100 | [diff] [blame] | 1884 | /* This bit is potentially nasty as there's a potential race with |
| 1885 | * afs_d_revalidate{,_rcu}(). We have to change d_fsdata on the dentry |
| 1886 | * to reflect it's new parent's new data_version after the op, but |
| 1887 | * d_revalidate may see old_dentry between the op having taken place |
| 1888 | * and the version being updated. |
| 1889 | * |
| 1890 | * So drop the old_dentry for now to make other threads go through |
| 1891 | * lookup instead - which we hold a lock against. |
| 1892 | */ |
| 1893 | d_drop(old_dentry); |
| 1894 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1895 | ret = -ERESTARTSYS; |
David Howells | 20b8391 | 2019-05-08 16:16:31 +0100 | [diff] [blame] | 1896 | if (afs_begin_vnode_operation(&fc, orig_dvnode, key, true)) { |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1897 | afs_dataversion_t orig_data_version; |
| 1898 | afs_dataversion_t new_data_version; |
| 1899 | struct afs_status_cb *new_scb = &scb[1]; |
| 1900 | |
| 1901 | orig_data_version = orig_dvnode->status.data_version + 1; |
| 1902 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1903 | if (orig_dvnode != new_dvnode) { |
| 1904 | if (mutex_lock_interruptible_nested(&new_dvnode->io_lock, 1) < 0) { |
| 1905 | afs_end_vnode_operation(&fc); |
David Howells | 9dd0b82 | 2019-07-30 14:38:52 +0100 | [diff] [blame] | 1906 | goto error_rehash_old; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1907 | } |
David Howells | 37c0bbb | 2019-07-30 14:38:51 +0100 | [diff] [blame] | 1908 | new_data_version = new_dvnode->status.data_version + 1; |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1909 | } else { |
| 1910 | new_data_version = orig_data_version; |
| 1911 | new_scb = &scb[0]; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1912 | } |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1913 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1914 | while (afs_select_fileserver(&fc)) { |
David Howells | 68251f0 | 2018-05-12 22:31:33 +0100 | [diff] [blame] | 1915 | fc.cb_break = afs_calc_vnode_cb_break(orig_dvnode); |
| 1916 | fc.cb_break_2 = afs_calc_vnode_cb_break(new_dvnode); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1917 | afs_fs_rename(&fc, old_dentry->d_name.name, |
David Howells | 63a4681 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 1918 | new_dvnode, new_dentry->d_name.name, |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1919 | &scb[0], new_scb); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1920 | } |
| 1921 | |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1922 | afs_vnode_commit_status(&fc, orig_dvnode, fc.cb_break, |
| 1923 | &orig_data_version, &scb[0]); |
| 1924 | if (new_dvnode != orig_dvnode) { |
| 1925 | afs_vnode_commit_status(&fc, new_dvnode, fc.cb_break_2, |
| 1926 | &new_data_version, &scb[1]); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1927 | mutex_unlock(&new_dvnode->io_lock); |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1928 | } |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1929 | ret = afs_end_vnode_operation(&fc); |
| 1930 | if (ret < 0) |
David Howells | 9dd0b82 | 2019-07-30 14:38:52 +0100 | [diff] [blame] | 1931 | goto error_rehash_old; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1932 | } |
| 1933 | |
David Howells | 63a4681 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 1934 | if (ret == 0) { |
David Howells | 79ddbfa | 2019-04-25 14:26:51 +0100 | [diff] [blame] | 1935 | if (rehash) |
| 1936 | d_rehash(rehash); |
David Howells | 63a4681 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 1937 | if (test_bit(AFS_VNODE_DIR_VALID, &orig_dvnode->flags)) |
| 1938 | afs_edit_dir_remove(orig_dvnode, &old_dentry->d_name, |
David Howells | 79ddbfa | 2019-04-25 14:26:51 +0100 | [diff] [blame] | 1939 | afs_edit_dir_for_rename_0); |
David Howells | 63a4681 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 1940 | |
| 1941 | if (!new_negative && |
| 1942 | test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags)) |
| 1943 | afs_edit_dir_remove(new_dvnode, &new_dentry->d_name, |
David Howells | 79ddbfa | 2019-04-25 14:26:51 +0100 | [diff] [blame] | 1944 | afs_edit_dir_for_rename_1); |
David Howells | 63a4681 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 1945 | |
| 1946 | if (test_bit(AFS_VNODE_DIR_VALID, &new_dvnode->flags)) |
| 1947 | afs_edit_dir_add(new_dvnode, &new_dentry->d_name, |
David Howells | 79ddbfa | 2019-04-25 14:26:51 +0100 | [diff] [blame] | 1948 | &vnode->fid, afs_edit_dir_for_rename_2); |
| 1949 | |
| 1950 | new_inode = d_inode(new_dentry); |
| 1951 | if (new_inode) { |
| 1952 | spin_lock(&new_inode->i_lock); |
| 1953 | if (new_inode->i_nlink > 0) |
| 1954 | drop_nlink(new_inode); |
| 1955 | spin_unlock(&new_inode->i_lock); |
| 1956 | } |
David Howells | 9dd0b82 | 2019-07-30 14:38:52 +0100 | [diff] [blame] | 1957 | |
| 1958 | /* Now we can update d_fsdata on the dentries to reflect their |
| 1959 | * new parent's data_version. |
| 1960 | * |
| 1961 | * Note that if we ever implement RENAME_EXCHANGE, we'll have |
| 1962 | * to update both dentries with opposing dir versions. |
| 1963 | */ |
| 1964 | if (new_dvnode != orig_dvnode) { |
| 1965 | afs_update_dentry_version(&fc, old_dentry, &scb[1]); |
| 1966 | afs_update_dentry_version(&fc, new_dentry, &scb[1]); |
| 1967 | } else { |
| 1968 | afs_update_dentry_version(&fc, old_dentry, &scb[0]); |
| 1969 | afs_update_dentry_version(&fc, new_dentry, &scb[0]); |
| 1970 | } |
David Howells | 79ddbfa | 2019-04-25 14:26:51 +0100 | [diff] [blame] | 1971 | d_move(old_dentry, new_dentry); |
| 1972 | goto error_tmp; |
David Howells | 63a4681 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 1973 | } |
| 1974 | |
David Howells | 9dd0b82 | 2019-07-30 14:38:52 +0100 | [diff] [blame] | 1975 | error_rehash_old: |
| 1976 | d_rehash(new_dentry); |
David Howells | 79ddbfa | 2019-04-25 14:26:51 +0100 | [diff] [blame] | 1977 | error_rehash: |
| 1978 | if (rehash) |
| 1979 | d_rehash(rehash); |
| 1980 | error_tmp: |
| 1981 | if (tmp) |
| 1982 | dput(tmp); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1983 | key_put(key); |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 1984 | error_scb: |
| 1985 | kfree(scb); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1986 | error: |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1987 | _leave(" = %d", ret); |
| 1988 | return ret; |
| 1989 | } |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 1990 | |
| 1991 | /* |
| 1992 | * Release a directory page and clean up its private state if it's not busy |
| 1993 | * - return true if the page can now be released, false if not |
| 1994 | */ |
| 1995 | static int afs_dir_releasepage(struct page *page, gfp_t gfp_flags) |
| 1996 | { |
| 1997 | struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host); |
| 1998 | |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 1999 | _enter("{{%llx:%llu}[%lu]}", dvnode->fid.vid, dvnode->fid.vnode, page->index); |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 2000 | |
| 2001 | set_page_private(page, 0); |
| 2002 | ClearPagePrivate(page); |
| 2003 | |
| 2004 | /* The directory will need reloading. */ |
| 2005 | if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) |
| 2006 | afs_stat_v(dvnode, n_relpg); |
| 2007 | return 1; |
| 2008 | } |
| 2009 | |
| 2010 | /* |
| 2011 | * invalidate part or all of a page |
| 2012 | * - release a page and clean up its private data if offset is 0 (indicating |
| 2013 | * the entire page) |
| 2014 | */ |
| 2015 | static void afs_dir_invalidatepage(struct page *page, unsigned int offset, |
| 2016 | unsigned int length) |
| 2017 | { |
| 2018 | struct afs_vnode *dvnode = AFS_FS_I(page->mapping->host); |
| 2019 | |
| 2020 | _enter("{%lu},%u,%u", page->index, offset, length); |
| 2021 | |
| 2022 | BUG_ON(!PageLocked(page)); |
| 2023 | |
| 2024 | /* The directory will need reloading. */ |
| 2025 | if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &dvnode->flags)) |
| 2026 | afs_stat_v(dvnode, n_inval); |
| 2027 | |
| 2028 | /* we clean up only if the entire page is being invalidated */ |
| 2029 | if (offset == 0 && length == PAGE_SIZE) { |
| 2030 | set_page_private(page, 0); |
| 2031 | ClearPagePrivate(page); |
| 2032 | } |
| 2033 | } |