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