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