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