Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* dir.c: AFS filesystem directory handling |
| 2 | * |
| 3 | * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved. |
| 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> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/fs.h> |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 16 | #include <linux/namei.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <linux/pagemap.h> |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 18 | #include <linux/ctype.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 19 | #include <linux/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include "internal.h" |
| 21 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 22 | static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 23 | unsigned int flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | static int afs_dir_open(struct inode *inode, struct file *file); |
Al Viro | 1bbae9f | 2013-05-22 16:31:14 -0400 | [diff] [blame] | 25 | static int afs_readdir(struct file *file, struct dir_context *ctx); |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 26 | static int afs_d_revalidate(struct dentry *dentry, unsigned int flags); |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 27 | static int afs_d_delete(const struct dentry *dentry); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 28 | static void afs_d_release(struct dentry *dentry); |
Miklos Szeredi | ac7576f | 2014-10-30 17:37:34 +0100 | [diff] [blame] | 29 | static int afs_lookup_filldir(struct dir_context *ctx, const char *name, int nlen, |
David Howells | afefdbb | 2006-10-03 01:13:46 -0700 | [diff] [blame] | 30 | loff_t fpos, u64 ino, unsigned dtype); |
Al Viro | 4acdaf2 | 2011-07-26 01:42:34 -0400 | [diff] [blame] | 31 | static int afs_create(struct inode *dir, struct dentry *dentry, umode_t mode, |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 32 | bool excl); |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 33 | static int afs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 34 | static int afs_rmdir(struct inode *dir, struct dentry *dentry); |
| 35 | static int afs_unlink(struct inode *dir, struct dentry *dentry); |
| 36 | static int afs_link(struct dentry *from, struct inode *dir, |
| 37 | struct dentry *dentry); |
| 38 | static int afs_symlink(struct inode *dir, struct dentry *dentry, |
| 39 | const char *content); |
| 40 | static int afs_rename(struct inode *old_dir, struct dentry *old_dentry, |
Miklos Szeredi | 1cd66c9 | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 41 | struct inode *new_dir, struct dentry *new_dentry, |
| 42 | unsigned int flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 44 | const struct file_operations afs_dir_file_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | .open = afs_dir_open, |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 46 | .release = afs_release, |
Al Viro | 29884ef | 2016-05-10 14:27:44 -0400 | [diff] [blame] | 47 | .iterate_shared = afs_readdir, |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 48 | .lock = afs_lock, |
Christoph Hellwig | 3222a3e | 2008-09-03 21:53:01 +0200 | [diff] [blame] | 49 | .llseek = generic_file_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 52 | const struct inode_operations afs_dir_inode_operations = { |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 53 | .create = afs_create, |
| 54 | .lookup = afs_lookup, |
| 55 | .link = afs_link, |
| 56 | .unlink = afs_unlink, |
| 57 | .symlink = afs_symlink, |
| 58 | .mkdir = afs_mkdir, |
| 59 | .rmdir = afs_rmdir, |
Miklos Szeredi | 2773bf0 | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 60 | .rename = afs_rename, |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 61 | .permission = afs_permission, |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 62 | .getattr = afs_getattr, |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 63 | .setattr = afs_setattr, |
David Howells | d3e3b7ea | 2017-07-06 15:50:27 +0100 | [diff] [blame] | 64 | .listxattr = afs_listxattr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | }; |
| 66 | |
Al Viro | d61dcce | 2011-01-12 20:04:20 -0500 | [diff] [blame] | 67 | const struct dentry_operations afs_fs_dentry_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | .d_revalidate = afs_d_revalidate, |
| 69 | .d_delete = afs_d_delete, |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 70 | .d_release = afs_d_release, |
David Howells | d18610b | 2011-01-14 19:04:05 +0000 | [diff] [blame] | 71 | .d_automount = afs_d_automount, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | #define AFS_DIR_HASHTBL_SIZE 128 |
| 75 | #define AFS_DIR_DIRENT_SIZE 32 |
| 76 | #define AFS_DIRENT_PER_BLOCK 64 |
| 77 | |
| 78 | union afs_dirent { |
| 79 | struct { |
| 80 | uint8_t valid; |
| 81 | uint8_t unused[1]; |
| 82 | __be16 hash_next; |
| 83 | __be32 vnode; |
| 84 | __be32 unique; |
| 85 | uint8_t name[16]; |
| 86 | uint8_t overflow[4]; /* if any char of the name (inc |
| 87 | * NUL) reaches here, consume |
| 88 | * the next dirent too */ |
| 89 | } u; |
| 90 | uint8_t extended_name[32]; |
| 91 | }; |
| 92 | |
| 93 | /* AFS directory page header (one at the beginning of every 2048-byte chunk) */ |
| 94 | struct afs_dir_pagehdr { |
| 95 | __be16 npages; |
| 96 | __be16 magic; |
| 97 | #define AFS_DIR_MAGIC htons(1234) |
| 98 | uint8_t nentries; |
| 99 | uint8_t bitmap[8]; |
| 100 | uint8_t pad[19]; |
| 101 | }; |
| 102 | |
| 103 | /* directory block layout */ |
| 104 | union afs_dir_block { |
| 105 | |
| 106 | struct afs_dir_pagehdr pagehdr; |
| 107 | |
| 108 | struct { |
| 109 | struct afs_dir_pagehdr pagehdr; |
| 110 | uint8_t alloc_ctrs[128]; |
| 111 | /* dir hash table */ |
| 112 | uint16_t hashtable[AFS_DIR_HASHTBL_SIZE]; |
| 113 | } hdr; |
| 114 | |
| 115 | union afs_dirent dirents[AFS_DIRENT_PER_BLOCK]; |
| 116 | }; |
| 117 | |
| 118 | /* layout on a linux VM page */ |
| 119 | struct afs_dir_page { |
| 120 | union afs_dir_block blocks[PAGE_SIZE / sizeof(union afs_dir_block)]; |
| 121 | }; |
| 122 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 123 | struct afs_lookup_cookie { |
Al Viro | 1bbae9f | 2013-05-22 16:31:14 -0400 | [diff] [blame] | 124 | struct dir_context ctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | struct afs_fid fid; |
Al Viro | 1bbae9f | 2013-05-22 16:31:14 -0400 | [diff] [blame] | 126 | struct qstr name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | int found; |
| 128 | }; |
| 129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | /* |
| 131 | * check that a directory page is valid |
| 132 | */ |
David Howells | dab17c1 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 133 | bool afs_dir_check_page(struct inode *dir, struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | { |
| 135 | struct afs_dir_page *dbuf; |
David Howells | dab17c1 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 136 | struct afs_vnode *vnode = AFS_FS_I(dir); |
| 137 | loff_t latter, i_size, off; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | int tmp, qty; |
| 139 | |
| 140 | #if 0 |
| 141 | /* check the page count */ |
| 142 | qty = desc.size / sizeof(dbuf->blocks[0]); |
| 143 | if (qty == 0) |
| 144 | goto error; |
| 145 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 146 | if (page->index == 0 && qty != ntohs(dbuf->blocks[0].pagehdr.npages)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | printk("kAFS: %s(%lu): wrong number of dir blocks %d!=%hu\n", |
Harvey Harrison | 530b641 | 2008-04-30 00:55:09 -0700 | [diff] [blame] | 148 | __func__, dir->i_ino, qty, |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 149 | ntohs(dbuf->blocks[0].pagehdr.npages)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | goto error; |
| 151 | } |
| 152 | #endif |
| 153 | |
David Howells | dab17c1 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 154 | /* Determine how many magic numbers there should be in this page, but |
| 155 | * we must take care because the directory may change size under us. |
| 156 | */ |
| 157 | off = page_offset(page); |
| 158 | i_size = i_size_read(dir); |
| 159 | if (i_size <= off) |
| 160 | goto checked; |
| 161 | |
| 162 | latter = i_size - off; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | if (latter >= PAGE_SIZE) |
| 164 | qty = PAGE_SIZE; |
| 165 | else |
| 166 | qty = latter; |
| 167 | qty /= sizeof(union afs_dir_block); |
| 168 | |
| 169 | /* check them */ |
| 170 | dbuf = page_address(page); |
| 171 | for (tmp = 0; tmp < qty; tmp++) { |
| 172 | if (dbuf->blocks[tmp].pagehdr.magic != AFS_DIR_MAGIC) { |
David Howells | dab17c1 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 173 | printk("kAFS: %s(%lx): bad magic %d/%d is %04hx\n", |
Harvey Harrison | 530b641 | 2008-04-30 00:55:09 -0700 | [diff] [blame] | 174 | __func__, dir->i_ino, tmp, qty, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | ntohs(dbuf->blocks[tmp].pagehdr.magic)); |
David Howells | dab17c1 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 176 | trace_afs_dir_check_failed(vnode, off, i_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | goto error; |
| 178 | } |
| 179 | } |
| 180 | |
David Howells | dab17c1 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 181 | checked: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | SetPageChecked(page); |
Al Viro | be5b82d | 2016-04-22 15:06:44 -0400 | [diff] [blame] | 183 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 185 | error: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | SetPageError(page); |
Al Viro | be5b82d | 2016-04-22 15:06:44 -0400 | [diff] [blame] | 187 | return false; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 188 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | /* |
| 191 | * discard a page cached in the pagecache |
| 192 | */ |
| 193 | static inline void afs_dir_put_page(struct page *page) |
| 194 | { |
| 195 | kunmap(page); |
David Howells | dab17c1 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 196 | unlock_page(page); |
Kirill A. Shutemov | 09cbfea | 2016-04-01 15:29:47 +0300 | [diff] [blame] | 197 | put_page(page); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 198 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | /* |
| 201 | * get a page into the pagecache |
| 202 | */ |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 203 | static struct page *afs_dir_get_page(struct inode *dir, unsigned long index, |
| 204 | struct key *key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | { |
| 206 | struct page *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | _enter("{%lu},%lu", dir->i_ino, index); |
| 208 | |
Al Viro | f6d335c | 2010-05-21 15:27:09 +0100 | [diff] [blame] | 209 | page = read_cache_page(dir->i_mapping, index, afs_page_filler, key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | if (!IS_ERR(page)) { |
David Howells | dab17c1 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 211 | lock_page(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | kmap(page); |
Al Viro | be5b82d | 2016-04-22 15:06:44 -0400 | [diff] [blame] | 213 | if (unlikely(!PageChecked(page))) { |
David Howells | dab17c1 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 214 | if (PageError(page)) |
Al Viro | be5b82d | 2016-04-22 15:06:44 -0400 | [diff] [blame] | 215 | goto fail; |
| 216 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | } |
| 218 | return page; |
| 219 | |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 220 | fail: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | afs_dir_put_page(page); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 222 | _leave(" = -EIO"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | return ERR_PTR(-EIO); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 224 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | /* |
| 227 | * open an AFS directory file |
| 228 | */ |
| 229 | static int afs_dir_open(struct inode *inode, struct file *file) |
| 230 | { |
| 231 | _enter("{%lu}", inode->i_ino); |
| 232 | |
Alexey Dobriyan | 2ecd05a | 2006-10-11 01:22:05 -0700 | [diff] [blame] | 233 | BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048); |
| 234 | BUILD_BUG_ON(sizeof(union afs_dirent) != 32); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 236 | if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(inode)->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | return -ENOENT; |
| 238 | |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 239 | return afs_open(inode, file); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 240 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | /* |
| 243 | * deal with one block in an AFS directory |
| 244 | */ |
Al Viro | 1bbae9f | 2013-05-22 16:31:14 -0400 | [diff] [blame] | 245 | static int afs_dir_iterate_block(struct dir_context *ctx, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | union afs_dir_block *block, |
Al Viro | 1bbae9f | 2013-05-22 16:31:14 -0400 | [diff] [blame] | 247 | unsigned blkoff) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | { |
| 249 | union afs_dirent *dire; |
| 250 | unsigned offset, next, curr; |
| 251 | size_t nlen; |
Al Viro | 1bbae9f | 2013-05-22 16:31:14 -0400 | [diff] [blame] | 252 | int tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
Al Viro | 1bbae9f | 2013-05-22 16:31:14 -0400 | [diff] [blame] | 254 | _enter("%u,%x,%p,,",(unsigned)ctx->pos,blkoff,block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | |
Al Viro | 1bbae9f | 2013-05-22 16:31:14 -0400 | [diff] [blame] | 256 | curr = (ctx->pos - blkoff) / sizeof(union afs_dirent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | |
| 258 | /* walk through the block, an entry at a time */ |
| 259 | for (offset = AFS_DIRENT_PER_BLOCK - block->pagehdr.nentries; |
| 260 | offset < AFS_DIRENT_PER_BLOCK; |
| 261 | offset = next |
| 262 | ) { |
| 263 | next = offset + 1; |
| 264 | |
| 265 | /* skip entries marked unused in the bitmap */ |
| 266 | if (!(block->pagehdr.bitmap[offset / 8] & |
| 267 | (1 << (offset % 8)))) { |
Alexey Dobriyan | 5b5e092 | 2017-02-27 14:30:02 -0800 | [diff] [blame] | 268 | _debug("ENT[%zu.%u]: unused", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | blkoff / sizeof(union afs_dir_block), offset); |
| 270 | if (offset >= curr) |
Al Viro | 1bbae9f | 2013-05-22 16:31:14 -0400 | [diff] [blame] | 271 | ctx->pos = blkoff + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | next * sizeof(union afs_dirent); |
| 273 | continue; |
| 274 | } |
| 275 | |
| 276 | /* got a valid entry */ |
| 277 | dire = &block->dirents[offset]; |
| 278 | nlen = strnlen(dire->u.name, |
| 279 | sizeof(*block) - |
| 280 | offset * sizeof(union afs_dirent)); |
| 281 | |
Alexey Dobriyan | 5b5e092 | 2017-02-27 14:30:02 -0800 | [diff] [blame] | 282 | _debug("ENT[%zu.%u]: %s %zu \"%s\"", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | blkoff / sizeof(union afs_dir_block), offset, |
| 284 | (offset < curr ? "skip" : "fill"), |
| 285 | nlen, dire->u.name); |
| 286 | |
| 287 | /* work out where the next possible entry is */ |
| 288 | for (tmp = nlen; tmp > 15; tmp -= sizeof(union afs_dirent)) { |
| 289 | if (next >= AFS_DIRENT_PER_BLOCK) { |
Alexey Dobriyan | 5b5e092 | 2017-02-27 14:30:02 -0800 | [diff] [blame] | 290 | _debug("ENT[%zu.%u]:" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | " %u travelled beyond end dir block" |
Alexey Dobriyan | 5b5e092 | 2017-02-27 14:30:02 -0800 | [diff] [blame] | 292 | " (len %u/%zu)", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | blkoff / sizeof(union afs_dir_block), |
| 294 | offset, next, tmp, nlen); |
| 295 | return -EIO; |
| 296 | } |
| 297 | if (!(block->pagehdr.bitmap[next / 8] & |
| 298 | (1 << (next % 8)))) { |
Alexey Dobriyan | 5b5e092 | 2017-02-27 14:30:02 -0800 | [diff] [blame] | 299 | _debug("ENT[%zu.%u]:" |
| 300 | " %u unmarked extension (len %u/%zu)", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | blkoff / sizeof(union afs_dir_block), |
| 302 | offset, next, tmp, nlen); |
| 303 | return -EIO; |
| 304 | } |
| 305 | |
Alexey Dobriyan | 5b5e092 | 2017-02-27 14:30:02 -0800 | [diff] [blame] | 306 | _debug("ENT[%zu.%u]: ext %u/%zu", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | blkoff / sizeof(union afs_dir_block), |
| 308 | next, tmp, nlen); |
| 309 | next++; |
| 310 | } |
| 311 | |
| 312 | /* skip if starts before the current position */ |
| 313 | if (offset < curr) |
| 314 | continue; |
| 315 | |
| 316 | /* found the next entry */ |
Al Viro | 1bbae9f | 2013-05-22 16:31:14 -0400 | [diff] [blame] | 317 | if (!dir_emit(ctx, dire->u.name, nlen, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | ntohl(dire->u.vnode), |
Al Viro | 1bbae9f | 2013-05-22 16:31:14 -0400 | [diff] [blame] | 319 | ctx->actor == afs_lookup_filldir ? |
| 320 | ntohl(dire->u.unique) : DT_UNKNOWN)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | _leave(" = 0 [full]"); |
| 322 | return 0; |
| 323 | } |
| 324 | |
Al Viro | 1bbae9f | 2013-05-22 16:31:14 -0400 | [diff] [blame] | 325 | ctx->pos = blkoff + next * sizeof(union afs_dirent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | _leave(" = 1 [more]"); |
| 329 | return 1; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 330 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | /* |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 333 | * 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] | 334 | */ |
Al Viro | 1bbae9f | 2013-05-22 16:31:14 -0400 | [diff] [blame] | 335 | static int afs_dir_iterate(struct inode *dir, struct dir_context *ctx, |
| 336 | struct key *key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | { |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 338 | union afs_dir_block *dblock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | struct afs_dir_page *dbuf; |
| 340 | struct page *page; |
| 341 | unsigned blkoff, limit; |
| 342 | int ret; |
| 343 | |
Al Viro | 1bbae9f | 2013-05-22 16:31:14 -0400 | [diff] [blame] | 344 | _enter("{%lu},%u,,", dir->i_ino, (unsigned)ctx->pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 346 | if (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(dir)->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | _leave(" = -ESTALE"); |
| 348 | return -ESTALE; |
| 349 | } |
| 350 | |
| 351 | /* round the file position up to the next entry boundary */ |
Al Viro | 1bbae9f | 2013-05-22 16:31:14 -0400 | [diff] [blame] | 352 | ctx->pos += sizeof(union afs_dirent) - 1; |
| 353 | ctx->pos &= ~(sizeof(union afs_dirent) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | |
| 355 | /* walk through the blocks in sequence */ |
| 356 | ret = 0; |
Al Viro | 1bbae9f | 2013-05-22 16:31:14 -0400 | [diff] [blame] | 357 | while (ctx->pos < dir->i_size) { |
| 358 | blkoff = ctx->pos & ~(sizeof(union afs_dir_block) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | |
| 360 | /* fetch the appropriate page from the directory */ |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 361 | page = afs_dir_get_page(dir, blkoff / PAGE_SIZE, key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | if (IS_ERR(page)) { |
| 363 | ret = PTR_ERR(page); |
| 364 | break; |
| 365 | } |
| 366 | |
| 367 | limit = blkoff & ~(PAGE_SIZE - 1); |
| 368 | |
| 369 | dbuf = page_address(page); |
| 370 | |
| 371 | /* deal with the individual blocks stashed on this page */ |
| 372 | do { |
| 373 | dblock = &dbuf->blocks[(blkoff % PAGE_SIZE) / |
| 374 | sizeof(union afs_dir_block)]; |
Al Viro | 1bbae9f | 2013-05-22 16:31:14 -0400 | [diff] [blame] | 375 | ret = afs_dir_iterate_block(ctx, dblock, blkoff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | if (ret != 1) { |
| 377 | afs_dir_put_page(page); |
| 378 | goto out; |
| 379 | } |
| 380 | |
| 381 | blkoff += sizeof(union afs_dir_block); |
| 382 | |
Al Viro | 1bbae9f | 2013-05-22 16:31:14 -0400 | [diff] [blame] | 383 | } while (ctx->pos < dir->i_size && blkoff < limit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | |
| 385 | afs_dir_put_page(page); |
| 386 | ret = 0; |
| 387 | } |
| 388 | |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 389 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | _leave(" = %d", ret); |
| 391 | return ret; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 392 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | /* |
| 395 | * read an AFS directory |
| 396 | */ |
Al Viro | 1bbae9f | 2013-05-22 16:31:14 -0400 | [diff] [blame] | 397 | static int afs_readdir(struct file *file, struct dir_context *ctx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | { |
David Howells | 215804a | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 399 | return afs_dir_iterate(file_inode(file), ctx, afs_file_key(file)); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 400 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | /* |
| 403 | * search the directory for a name |
| 404 | * - if afs_dir_iterate_block() spots this function, it'll pass the FID |
| 405 | * uniquifier through dtype |
| 406 | */ |
Miklos Szeredi | ac7576f | 2014-10-30 17:37:34 +0100 | [diff] [blame] | 407 | static int afs_lookup_filldir(struct dir_context *ctx, const char *name, |
| 408 | int nlen, loff_t fpos, u64 ino, unsigned dtype) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | { |
Miklos Szeredi | ac7576f | 2014-10-30 17:37:34 +0100 | [diff] [blame] | 410 | struct afs_lookup_cookie *cookie = |
| 411 | container_of(ctx, struct afs_lookup_cookie, ctx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | |
Al Viro | 1bbae9f | 2013-05-22 16:31:14 -0400 | [diff] [blame] | 413 | _enter("{%s,%u},%s,%u,,%llu,%u", |
| 414 | cookie->name.name, cookie->name.len, name, nlen, |
David S. Miller | ba3e0e1 | 2007-04-26 16:06:22 -0700 | [diff] [blame] | 415 | (unsigned long long) ino, dtype); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 417 | /* insanity checks first */ |
| 418 | BUILD_BUG_ON(sizeof(union afs_dir_block) != 2048); |
| 419 | BUILD_BUG_ON(sizeof(union afs_dirent) != 32); |
| 420 | |
Al Viro | 1bbae9f | 2013-05-22 16:31:14 -0400 | [diff] [blame] | 421 | if (cookie->name.len != nlen || |
| 422 | memcmp(cookie->name.name, name, nlen) != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | _leave(" = 0 [no]"); |
| 424 | return 0; |
| 425 | } |
| 426 | |
| 427 | cookie->fid.vnode = ino; |
| 428 | cookie->fid.unique = dtype; |
| 429 | cookie->found = 1; |
| 430 | |
| 431 | _leave(" = -1 [found]"); |
| 432 | return -1; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 433 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | /* |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 436 | * do a lookup in a directory |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 437 | * - just returns the FID the dentry name maps to if found |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | */ |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 439 | static int afs_do_lookup(struct inode *dir, struct dentry *dentry, |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 440 | struct afs_fid *fid, struct key *key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | { |
Al Viro | 1bbae9f | 2013-05-22 16:31:14 -0400 | [diff] [blame] | 442 | struct afs_super_info *as = dir->i_sb->s_fs_info; |
| 443 | struct afs_lookup_cookie cookie = { |
| 444 | .ctx.actor = afs_lookup_filldir, |
| 445 | .name = dentry->d_name, |
| 446 | .fid.vid = as->volume->vid |
| 447 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | int ret; |
| 449 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 450 | _enter("{%lu},%p{%pd},", dir->i_ino, dentry, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | /* search the directory */ |
Al Viro | 1bbae9f | 2013-05-22 16:31:14 -0400 | [diff] [blame] | 453 | ret = afs_dir_iterate(dir, &cookie.ctx, key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | if (ret < 0) { |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 455 | _leave(" = %d [iter]", ret); |
| 456 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | ret = -ENOENT; |
| 460 | if (!cookie.found) { |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 461 | _leave(" = -ENOENT [not found]"); |
| 462 | return -ENOENT; |
| 463 | } |
| 464 | |
| 465 | *fid = cookie.fid; |
| 466 | _leave(" = 0 { vn=%u u=%u }", fid->vnode, fid->unique); |
| 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | /* |
wanglei | bec5eb6 | 2010-08-11 09:38:04 +0100 | [diff] [blame] | 471 | * Try to auto mount the mountpoint with pseudo directory, if the autocell |
| 472 | * operation is setted. |
| 473 | */ |
| 474 | static struct inode *afs_try_auto_mntpt( |
| 475 | int ret, struct dentry *dentry, struct inode *dir, struct key *key, |
| 476 | struct afs_fid *fid) |
| 477 | { |
| 478 | const char *devname = dentry->d_name.name; |
| 479 | struct afs_vnode *vnode = AFS_FS_I(dir); |
| 480 | struct inode *inode; |
| 481 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 482 | _enter("%d, %p{%pd}, {%x:%u}, %p", |
| 483 | ret, dentry, dentry, vnode->fid.vid, vnode->fid.vnode, key); |
wanglei | bec5eb6 | 2010-08-11 09:38:04 +0100 | [diff] [blame] | 484 | |
| 485 | if (ret != -ENOENT || |
| 486 | !test_bit(AFS_VNODE_AUTOCELL, &vnode->flags)) |
| 487 | goto out; |
| 488 | |
| 489 | inode = afs_iget_autocell(dir, devname, strlen(devname), key); |
| 490 | if (IS_ERR(inode)) { |
| 491 | ret = PTR_ERR(inode); |
| 492 | goto out; |
| 493 | } |
| 494 | |
| 495 | *fid = AFS_FS_I(inode)->fid; |
| 496 | _leave("= %p", inode); |
| 497 | return inode; |
| 498 | |
| 499 | out: |
| 500 | _leave("= %d", ret); |
| 501 | return ERR_PTR(ret); |
| 502 | } |
| 503 | |
| 504 | /* |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 505 | * look up an entry in a directory |
| 506 | */ |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 507 | static struct dentry *afs_lookup(struct inode *dir, struct dentry *dentry, |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 508 | unsigned int flags) |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 509 | { |
| 510 | struct afs_vnode *vnode; |
| 511 | struct afs_fid fid; |
| 512 | struct inode *inode; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 513 | struct key *key; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 514 | int ret; |
| 515 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 516 | vnode = AFS_FS_I(dir); |
| 517 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 518 | _enter("{%x:%u},%p{%pd},", |
| 519 | vnode->fid.vid, vnode->fid.vnode, dentry, dentry); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 520 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 521 | ASSERTCMP(d_inode(dentry), ==, NULL); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 522 | |
David Howells | 45222b9 | 2007-05-10 22:22:20 -0700 | [diff] [blame] | 523 | if (dentry->d_name.len >= AFSNAMEMAX) { |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 524 | _leave(" = -ENAMETOOLONG"); |
| 525 | return ERR_PTR(-ENAMETOOLONG); |
| 526 | } |
| 527 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 528 | if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) { |
| 529 | _leave(" = -ESTALE"); |
| 530 | return ERR_PTR(-ESTALE); |
| 531 | } |
| 532 | |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 533 | key = afs_request_key(vnode->volume->cell); |
| 534 | if (IS_ERR(key)) { |
| 535 | _leave(" = %ld [key]", PTR_ERR(key)); |
David Howells | e231c2e | 2008-02-07 00:15:26 -0800 | [diff] [blame] | 536 | return ERR_CAST(key); |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 537 | } |
| 538 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 539 | ret = afs_validate(vnode, key); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 540 | if (ret < 0) { |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 541 | key_put(key); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 542 | _leave(" = %d [val]", ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | return ERR_PTR(ret); |
| 544 | } |
| 545 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 546 | ret = afs_do_lookup(dir, dentry, &fid, key); |
| 547 | if (ret < 0) { |
wanglei | bec5eb6 | 2010-08-11 09:38:04 +0100 | [diff] [blame] | 548 | inode = afs_try_auto_mntpt(ret, dentry, dir, key, &fid); |
| 549 | if (!IS_ERR(inode)) { |
| 550 | key_put(key); |
| 551 | goto success; |
| 552 | } |
| 553 | |
| 554 | ret = PTR_ERR(inode); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 555 | key_put(key); |
| 556 | if (ret == -ENOENT) { |
| 557 | d_add(dentry, NULL); |
| 558 | _leave(" = NULL [negative]"); |
| 559 | return NULL; |
| 560 | } |
| 561 | _leave(" = %d [do]", ret); |
| 562 | return ERR_PTR(ret); |
| 563 | } |
| 564 | dentry->d_fsdata = (void *)(unsigned long) vnode->status.data_version; |
| 565 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | /* instantiate the dentry */ |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 567 | inode = afs_iget(dir->i_sb, key, &fid, NULL, NULL, NULL); |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 568 | key_put(key); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 569 | if (IS_ERR(inode)) { |
| 570 | _leave(" = %ld", PTR_ERR(inode)); |
David Howells | e231c2e | 2008-02-07 00:15:26 -0800 | [diff] [blame] | 571 | return ERR_CAST(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | } |
| 573 | |
wanglei | bec5eb6 | 2010-08-11 09:38:04 +0100 | [diff] [blame] | 574 | success: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | d_add(dentry, inode); |
David Howells | d6e43f7 | 2011-06-14 00:45:44 +0100 | [diff] [blame] | 576 | _leave(" = 0 { vn=%u u=%u } -> { ino=%lu v=%u }", |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 577 | fid.vnode, |
| 578 | fid.unique, |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 579 | d_inode(dentry)->i_ino, |
| 580 | d_inode(dentry)->i_generation); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | |
| 582 | return NULL; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 583 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | /* |
| 586 | * check that a dentry lookup hit has found a valid entry |
| 587 | * - NOTE! the hit can be a negative hit too, so we can't assume we have an |
| 588 | * inode |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | */ |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 590 | static int afs_d_revalidate(struct dentry *dentry, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | { |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 592 | struct afs_vnode *vnode, *dir; |
Artem Bityutskiy | dd0d9a4 | 2009-07-09 10:44:30 +0100 | [diff] [blame] | 593 | struct afs_fid uninitialized_var(fid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | struct dentry *parent; |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 595 | struct inode *inode; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 596 | struct key *key; |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 597 | void *dir_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | int ret; |
| 599 | |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 600 | if (flags & LOOKUP_RCU) |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 601 | return -ECHILD; |
| 602 | |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 603 | if (d_really_is_positive(dentry)) { |
| 604 | vnode = AFS_FS_I(d_inode(dentry)); |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 605 | _enter("{v={%x:%u} n=%pd fl=%lx},", |
| 606 | vnode->fid.vid, vnode->fid.vnode, dentry, |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 607 | vnode->flags); |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 608 | } else { |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 609 | _enter("{neg n=%pd}", dentry); |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 610 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 612 | key = afs_request_key(AFS_FS_S(dentry->d_sb)->volume->cell); |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 613 | if (IS_ERR(key)) |
| 614 | key = NULL; |
| 615 | |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 616 | if (d_really_is_positive(dentry)) { |
| 617 | inode = d_inode(dentry); |
| 618 | if (inode) { |
| 619 | vnode = AFS_FS_I(inode); |
| 620 | afs_validate(vnode, key); |
| 621 | if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) |
| 622 | goto out_bad; |
| 623 | } |
| 624 | } |
| 625 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | /* lock down the parent dentry so we can peer at it */ |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 627 | parent = dget_parent(dentry); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 628 | dir = AFS_FS_I(d_inode(parent)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 630 | /* validate the parent directory */ |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 631 | afs_validate(dir, key); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 632 | |
| 633 | if (test_bit(AFS_VNODE_DELETED, &dir->flags)) { |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 634 | _debug("%pd: parent dir deleted", dentry); |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 635 | goto out_bad_parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | } |
| 637 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 638 | dir_version = (void *) (unsigned long) dir->status.data_version; |
| 639 | if (dentry->d_fsdata == dir_version) |
| 640 | goto out_valid; /* the dir contents are unchanged */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 642 | _debug("dir modified"); |
| 643 | |
| 644 | /* search the directory for this vnode */ |
| 645 | ret = afs_do_lookup(&dir->vfs_inode, dentry, &fid, key); |
| 646 | switch (ret) { |
| 647 | case 0: |
| 648 | /* the filename maps to something */ |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 649 | if (d_really_is_negative(dentry)) |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 650 | goto out_bad_parent; |
| 651 | inode = d_inode(dentry); |
| 652 | if (is_bad_inode(inode)) { |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 653 | printk("kAFS: afs_d_revalidate: %pd2 has bad inode\n", |
| 654 | dentry); |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 655 | goto out_bad_parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | } |
| 657 | |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 658 | vnode = AFS_FS_I(inode); |
| 659 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | /* if the vnode ID has changed, then the dirent points to a |
| 661 | * different file */ |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 662 | if (fid.vnode != vnode->fid.vnode) { |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 663 | _debug("%pd: dirent changed [%u != %u]", |
| 664 | dentry, fid.vnode, |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 665 | vnode->fid.vnode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | goto not_found; |
| 667 | } |
| 668 | |
| 669 | /* if the vnode ID uniqifier has changed, then the file has |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 670 | * been deleted and replaced, and the original vnode ID has |
| 671 | * been reused */ |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 672 | if (fid.unique != vnode->fid.unique) { |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 673 | _debug("%pd: file deleted (uq %u -> %u I:%u)", |
| 674 | dentry, fid.unique, |
Jean Noel Cordenner | 7a22422 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 675 | vnode->fid.unique, |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 676 | vnode->vfs_inode.i_generation); |
| 677 | write_seqlock(&vnode->cb_lock); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 678 | set_bit(AFS_VNODE_DELETED, &vnode->flags); |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 679 | write_sequnlock(&vnode->cb_lock); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 680 | goto not_found; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | } |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 682 | goto out_valid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 684 | case -ENOENT: |
| 685 | /* the filename is unknown */ |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 686 | _debug("%pd: dirent not found", dentry); |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 687 | if (d_really_is_positive(dentry)) |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 688 | goto not_found; |
| 689 | goto out_valid; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 690 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 691 | default: |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 692 | _debug("failed to iterate dir %pd: %d", |
| 693 | parent, ret); |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 694 | goto out_bad_parent; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 695 | } |
| 696 | |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 697 | out_valid: |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 698 | dentry->d_fsdata = dir_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | dput(parent); |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 700 | key_put(key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | _leave(" = 1 [valid]"); |
| 702 | return 1; |
| 703 | |
| 704 | /* the dirent, if it exists, now points to a different vnode */ |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 705 | not_found: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | spin_lock(&dentry->d_lock); |
| 707 | dentry->d_flags |= DCACHE_NFSFS_RENAMED; |
| 708 | spin_unlock(&dentry->d_lock); |
| 709 | |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 710 | out_bad_parent: |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 711 | _debug("dropping dentry %pd2", dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | dput(parent); |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 713 | out_bad: |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 714 | key_put(key); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | |
| 716 | _leave(" = 0 [bad]"); |
| 717 | return 0; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 718 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 720 | /* |
| 721 | * allow the VFS to enquire as to whether a dentry should be unhashed (mustn't |
| 722 | * sleep) |
| 723 | * - called from dput() when d_count is going to 0. |
| 724 | * - return 1 to request dentry be unhashed, 0 otherwise |
| 725 | */ |
Nick Piggin | fe15ce4 | 2011-01-07 17:49:23 +1100 | [diff] [blame] | 726 | static int afs_d_delete(const struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | { |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 728 | _enter("%pd", dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | |
| 730 | if (dentry->d_flags & DCACHE_NFSFS_RENAMED) |
| 731 | goto zap; |
| 732 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 733 | if (d_really_is_positive(dentry) && |
| 734 | (test_bit(AFS_VNODE_DELETED, &AFS_FS_I(d_inode(dentry))->flags) || |
| 735 | test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(d_inode(dentry))->flags))) |
wanglei | bec5eb6 | 2010-08-11 09:38:04 +0100 | [diff] [blame] | 736 | goto zap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | |
| 738 | _leave(" = 0 [keep]"); |
| 739 | return 0; |
| 740 | |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 741 | zap: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | _leave(" = 1 [zap]"); |
| 743 | return 1; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 744 | } |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 745 | |
| 746 | /* |
| 747 | * handle dentry release |
| 748 | */ |
| 749 | static void afs_d_release(struct dentry *dentry) |
| 750 | { |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 751 | _enter("%pd", dentry); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | /* |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 755 | * Create a new inode for create/mkdir/symlink |
| 756 | */ |
| 757 | static void afs_vnode_new_inode(struct afs_fs_cursor *fc, |
| 758 | struct dentry *new_dentry, |
| 759 | struct afs_fid *newfid, |
| 760 | struct afs_file_status *newstatus, |
| 761 | struct afs_callback *newcb) |
| 762 | { |
| 763 | struct inode *inode; |
| 764 | |
| 765 | if (fc->ac.error < 0) |
| 766 | return; |
| 767 | |
David Howells | bc1527d | 2017-11-20 23:04:08 +0000 | [diff] [blame] | 768 | d_drop(new_dentry); |
| 769 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 770 | inode = afs_iget(fc->vnode->vfs_inode.i_sb, fc->key, |
| 771 | newfid, newstatus, newcb, fc->cbi); |
| 772 | if (IS_ERR(inode)) { |
| 773 | /* ENOMEM or EINTR at a really inconvenient time - just abandon |
| 774 | * the new directory on the server. |
| 775 | */ |
| 776 | fc->ac.error = PTR_ERR(inode); |
| 777 | return; |
| 778 | } |
| 779 | |
David Howells | bc1527d | 2017-11-20 23:04:08 +0000 | [diff] [blame] | 780 | d_add(new_dentry, inode); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | /* |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 784 | * create a directory on an AFS filesystem |
| 785 | */ |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 786 | 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] | 787 | { |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 788 | struct afs_file_status newstatus; |
| 789 | struct afs_fs_cursor fc; |
| 790 | struct afs_callback newcb; |
| 791 | struct afs_vnode *dvnode = AFS_FS_I(dir); |
| 792 | struct afs_fid newfid; |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 793 | struct key *key; |
| 794 | int ret; |
| 795 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 796 | mode |= S_IFDIR; |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 797 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 798 | _enter("{%x:%u},{%pd},%ho", |
| 799 | dvnode->fid.vid, dvnode->fid.vnode, dentry, mode); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 800 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 801 | key = afs_request_key(dvnode->volume->cell); |
| 802 | if (IS_ERR(key)) { |
| 803 | ret = PTR_ERR(key); |
| 804 | goto error; |
| 805 | } |
| 806 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 807 | ret = -ERESTARTSYS; |
| 808 | if (afs_begin_vnode_operation(&fc, dvnode, key)) { |
| 809 | while (afs_select_fileserver(&fc)) { |
| 810 | fc.cb_break = dvnode->cb_break + dvnode->cb_s_break; |
| 811 | afs_fs_create(&fc, dentry->d_name.name, mode, |
| 812 | &newfid, &newstatus, &newcb); |
| 813 | } |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 814 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 815 | afs_check_for_remote_deletion(&fc, fc.vnode); |
| 816 | afs_vnode_commit_status(&fc, dvnode, fc.cb_break); |
| 817 | afs_vnode_new_inode(&fc, dentry, &newfid, &newstatus, &newcb); |
| 818 | ret = afs_end_vnode_operation(&fc); |
| 819 | if (ret < 0) |
| 820 | goto error_key; |
David Howells | 4433b69 | 2017-11-20 22:41:00 +0000 | [diff] [blame^] | 821 | } else { |
| 822 | goto error_key; |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 823 | } |
| 824 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 825 | key_put(key); |
| 826 | _leave(" = 0"); |
| 827 | return 0; |
| 828 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 829 | error_key: |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 830 | key_put(key); |
| 831 | error: |
| 832 | d_drop(dentry); |
| 833 | _leave(" = %d", ret); |
| 834 | return ret; |
| 835 | } |
| 836 | |
| 837 | /* |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 838 | * Remove a subdir from a directory. |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 839 | */ |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 840 | static void afs_dir_remove_subdir(struct dentry *dentry) |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 841 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 842 | if (d_really_is_positive(dentry)) { |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 843 | struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry)); |
| 844 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 845 | clear_nlink(&vnode->vfs_inode); |
| 846 | set_bit(AFS_VNODE_DELETED, &vnode->flags); |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 847 | clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 848 | } |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | /* |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 852 | * remove a directory from an AFS filesystem |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 853 | */ |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 854 | static int afs_rmdir(struct inode *dir, struct dentry *dentry) |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 855 | { |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 856 | struct afs_fs_cursor fc; |
| 857 | struct afs_vnode *dvnode = AFS_FS_I(dir); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 858 | struct key *key; |
| 859 | int ret; |
| 860 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 861 | _enter("{%x:%u},{%pd}", |
| 862 | dvnode->fid.vid, dvnode->fid.vnode, dentry); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 863 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 864 | key = afs_request_key(dvnode->volume->cell); |
| 865 | if (IS_ERR(key)) { |
| 866 | ret = PTR_ERR(key); |
| 867 | goto error; |
| 868 | } |
| 869 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 870 | ret = -ERESTARTSYS; |
| 871 | if (afs_begin_vnode_operation(&fc, dvnode, key)) { |
| 872 | while (afs_select_fileserver(&fc)) { |
| 873 | fc.cb_break = dvnode->cb_break + dvnode->cb_s_break; |
| 874 | afs_fs_remove(&fc, dentry->d_name.name, true); |
| 875 | } |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 876 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 877 | afs_vnode_commit_status(&fc, dvnode, fc.cb_break); |
| 878 | ret = afs_end_vnode_operation(&fc); |
| 879 | if (ret == 0) |
| 880 | afs_dir_remove_subdir(dentry); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | key_put(key); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 884 | error: |
| 885 | return ret; |
| 886 | } |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 887 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 888 | /* |
| 889 | * Remove a link to a file or symlink from a directory. |
| 890 | * |
| 891 | * If the file was not deleted due to excess hard links, the fileserver will |
| 892 | * break the callback promise on the file - if it had one - before it returns |
| 893 | * to us, and if it was deleted, it won't |
| 894 | * |
| 895 | * However, if we didn't have a callback promise outstanding, or it was |
| 896 | * outstanding on a different server, then it won't break it either... |
| 897 | */ |
| 898 | static int afs_dir_remove_link(struct dentry *dentry, struct key *key) |
| 899 | { |
| 900 | int ret = 0; |
| 901 | |
| 902 | if (d_really_is_positive(dentry)) { |
| 903 | struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry)); |
| 904 | |
| 905 | if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) |
| 906 | kdebug("AFS_VNODE_DELETED"); |
| 907 | clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags); |
| 908 | |
| 909 | ret = afs_validate(vnode, key); |
| 910 | if (ret == -ESTALE) |
| 911 | ret = 0; |
| 912 | _debug("nlink %d [val %d]", vnode->vfs_inode.i_nlink, ret); |
| 913 | } |
| 914 | |
| 915 | return ret; |
| 916 | } |
| 917 | |
| 918 | /* |
| 919 | * Remove a file or symlink from an AFS filesystem. |
| 920 | */ |
| 921 | static int afs_unlink(struct inode *dir, struct dentry *dentry) |
| 922 | { |
| 923 | struct afs_fs_cursor fc; |
| 924 | struct afs_vnode *dvnode = AFS_FS_I(dir), *vnode; |
| 925 | struct key *key; |
| 926 | int ret; |
| 927 | |
| 928 | _enter("{%x:%u},{%pd}", |
| 929 | dvnode->fid.vid, dvnode->fid.vnode, dentry); |
| 930 | |
| 931 | if (dentry->d_name.len >= AFSNAMEMAX) |
| 932 | return -ENAMETOOLONG; |
| 933 | |
| 934 | key = afs_request_key(dvnode->volume->cell); |
| 935 | if (IS_ERR(key)) { |
| 936 | ret = PTR_ERR(key); |
| 937 | goto error; |
| 938 | } |
| 939 | |
| 940 | /* Try to make sure we have a callback promise on the victim. */ |
| 941 | if (d_really_is_positive(dentry)) { |
| 942 | vnode = AFS_FS_I(d_inode(dentry)); |
| 943 | ret = afs_validate(vnode, key); |
| 944 | if (ret < 0) |
| 945 | goto error_key; |
| 946 | } |
| 947 | |
| 948 | ret = -ERESTARTSYS; |
| 949 | if (afs_begin_vnode_operation(&fc, dvnode, key)) { |
| 950 | while (afs_select_fileserver(&fc)) { |
| 951 | fc.cb_break = dvnode->cb_break + dvnode->cb_s_break; |
| 952 | afs_fs_remove(&fc, dentry->d_name.name, false); |
| 953 | } |
| 954 | |
| 955 | afs_vnode_commit_status(&fc, dvnode, fc.cb_break); |
| 956 | ret = afs_end_vnode_operation(&fc); |
| 957 | if (ret == 0) |
| 958 | ret = afs_dir_remove_link(dentry, key); |
| 959 | } |
| 960 | |
| 961 | error_key: |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 962 | key_put(key); |
| 963 | error: |
| 964 | _leave(" = %d", ret); |
| 965 | return ret; |
| 966 | } |
| 967 | |
| 968 | /* |
| 969 | * create a regular file on an AFS filesystem |
| 970 | */ |
Al Viro | 4acdaf2 | 2011-07-26 01:42:34 -0400 | [diff] [blame] | 971 | 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] | 972 | bool excl) |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 973 | { |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 974 | struct afs_fs_cursor fc; |
| 975 | struct afs_file_status newstatus; |
| 976 | struct afs_callback newcb; |
| 977 | struct afs_vnode *dvnode = dvnode = AFS_FS_I(dir); |
| 978 | struct afs_fid newfid; |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 979 | struct key *key; |
| 980 | int ret; |
| 981 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 982 | mode |= S_IFREG; |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 983 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 984 | _enter("{%x:%u},{%pd},%ho,", |
| 985 | dvnode->fid.vid, dvnode->fid.vnode, dentry, mode); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 986 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 987 | ret = -ENAMETOOLONG; |
| 988 | if (dentry->d_name.len >= AFSNAMEMAX) |
| 989 | goto error; |
| 990 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 991 | key = afs_request_key(dvnode->volume->cell); |
| 992 | if (IS_ERR(key)) { |
| 993 | ret = PTR_ERR(key); |
| 994 | goto error; |
| 995 | } |
| 996 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 997 | ret = -ERESTARTSYS; |
| 998 | if (afs_begin_vnode_operation(&fc, dvnode, key)) { |
| 999 | while (afs_select_fileserver(&fc)) { |
| 1000 | fc.cb_break = dvnode->cb_break + dvnode->cb_s_break; |
| 1001 | afs_fs_create(&fc, dentry->d_name.name, mode, |
| 1002 | &newfid, &newstatus, &newcb); |
| 1003 | } |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1004 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1005 | afs_check_for_remote_deletion(&fc, fc.vnode); |
| 1006 | afs_vnode_commit_status(&fc, dvnode, fc.cb_break); |
| 1007 | afs_vnode_new_inode(&fc, dentry, &newfid, &newstatus, &newcb); |
| 1008 | ret = afs_end_vnode_operation(&fc); |
| 1009 | if (ret < 0) |
| 1010 | goto error_key; |
David Howells | 4433b69 | 2017-11-20 22:41:00 +0000 | [diff] [blame^] | 1011 | } else { |
| 1012 | goto error_key; |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1015 | key_put(key); |
| 1016 | _leave(" = 0"); |
| 1017 | return 0; |
| 1018 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1019 | error_key: |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1020 | key_put(key); |
| 1021 | error: |
| 1022 | d_drop(dentry); |
| 1023 | _leave(" = %d", ret); |
| 1024 | return ret; |
| 1025 | } |
| 1026 | |
| 1027 | /* |
| 1028 | * create a hard link between files in an AFS filesystem |
| 1029 | */ |
| 1030 | static int afs_link(struct dentry *from, struct inode *dir, |
| 1031 | struct dentry *dentry) |
| 1032 | { |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1033 | struct afs_fs_cursor fc; |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1034 | struct afs_vnode *dvnode, *vnode; |
| 1035 | struct key *key; |
| 1036 | int ret; |
| 1037 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1038 | vnode = AFS_FS_I(d_inode(from)); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1039 | dvnode = AFS_FS_I(dir); |
| 1040 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 1041 | _enter("{%x:%u},{%x:%u},{%pd}", |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1042 | vnode->fid.vid, vnode->fid.vnode, |
| 1043 | dvnode->fid.vid, dvnode->fid.vnode, |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 1044 | dentry); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1045 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1046 | ret = -ENAMETOOLONG; |
| 1047 | if (dentry->d_name.len >= AFSNAMEMAX) |
| 1048 | goto error; |
| 1049 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1050 | key = afs_request_key(dvnode->volume->cell); |
| 1051 | if (IS_ERR(key)) { |
| 1052 | ret = PTR_ERR(key); |
| 1053 | goto error; |
| 1054 | } |
| 1055 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1056 | ret = -ERESTARTSYS; |
| 1057 | if (afs_begin_vnode_operation(&fc, dvnode, key)) { |
| 1058 | if (mutex_lock_interruptible_nested(&vnode->io_lock, 1) < 0) { |
| 1059 | afs_end_vnode_operation(&fc); |
David Howells | bc1527d | 2017-11-20 23:04:08 +0000 | [diff] [blame] | 1060 | goto error_key; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1061 | } |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1062 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1063 | while (afs_select_fileserver(&fc)) { |
| 1064 | fc.cb_break = dvnode->cb_break + dvnode->cb_s_break; |
| 1065 | fc.cb_break_2 = vnode->cb_break + vnode->cb_s_break; |
| 1066 | afs_fs_link(&fc, vnode, dentry->d_name.name); |
| 1067 | } |
| 1068 | |
| 1069 | afs_vnode_commit_status(&fc, dvnode, fc.cb_break); |
| 1070 | afs_vnode_commit_status(&fc, vnode, fc.cb_break_2); |
| 1071 | ihold(&vnode->vfs_inode); |
| 1072 | d_instantiate(dentry, &vnode->vfs_inode); |
| 1073 | |
| 1074 | mutex_unlock(&vnode->io_lock); |
| 1075 | ret = afs_end_vnode_operation(&fc); |
| 1076 | if (ret < 0) |
| 1077 | goto error_key; |
David Howells | 4433b69 | 2017-11-20 22:41:00 +0000 | [diff] [blame^] | 1078 | } else { |
| 1079 | goto error_key; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1082 | key_put(key); |
| 1083 | _leave(" = 0"); |
| 1084 | return 0; |
| 1085 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1086 | error_key: |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1087 | key_put(key); |
| 1088 | error: |
| 1089 | d_drop(dentry); |
| 1090 | _leave(" = %d", ret); |
| 1091 | return ret; |
| 1092 | } |
| 1093 | |
| 1094 | /* |
| 1095 | * create a symlink in an AFS filesystem |
| 1096 | */ |
| 1097 | static int afs_symlink(struct inode *dir, struct dentry *dentry, |
| 1098 | const char *content) |
| 1099 | { |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1100 | struct afs_fs_cursor fc; |
| 1101 | struct afs_file_status newstatus; |
| 1102 | struct afs_vnode *dvnode = AFS_FS_I(dir); |
| 1103 | struct afs_fid newfid; |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1104 | struct key *key; |
| 1105 | int ret; |
| 1106 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 1107 | _enter("{%x:%u},{%pd},%s", |
| 1108 | dvnode->fid.vid, dvnode->fid.vnode, dentry, |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1109 | content); |
| 1110 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1111 | ret = -ENAMETOOLONG; |
| 1112 | if (dentry->d_name.len >= AFSNAMEMAX) |
| 1113 | goto error; |
| 1114 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1115 | ret = -EINVAL; |
David Howells | 45222b9 | 2007-05-10 22:22:20 -0700 | [diff] [blame] | 1116 | if (strlen(content) >= AFSPATHMAX) |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1117 | goto error; |
| 1118 | |
| 1119 | key = afs_request_key(dvnode->volume->cell); |
| 1120 | if (IS_ERR(key)) { |
| 1121 | ret = PTR_ERR(key); |
| 1122 | goto error; |
| 1123 | } |
| 1124 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1125 | ret = -ERESTARTSYS; |
| 1126 | if (afs_begin_vnode_operation(&fc, dvnode, key)) { |
| 1127 | while (afs_select_fileserver(&fc)) { |
| 1128 | fc.cb_break = dvnode->cb_break + dvnode->cb_s_break; |
| 1129 | afs_fs_symlink(&fc, dentry->d_name.name, content, |
| 1130 | &newfid, &newstatus); |
| 1131 | } |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1132 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1133 | afs_check_for_remote_deletion(&fc, fc.vnode); |
| 1134 | afs_vnode_commit_status(&fc, dvnode, fc.cb_break); |
| 1135 | afs_vnode_new_inode(&fc, dentry, &newfid, &newstatus, NULL); |
| 1136 | ret = afs_end_vnode_operation(&fc); |
| 1137 | if (ret < 0) |
| 1138 | goto error_key; |
David Howells | 4433b69 | 2017-11-20 22:41:00 +0000 | [diff] [blame^] | 1139 | } else { |
| 1140 | goto error_key; |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1141 | } |
| 1142 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1143 | key_put(key); |
| 1144 | _leave(" = 0"); |
| 1145 | return 0; |
| 1146 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1147 | error_key: |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1148 | key_put(key); |
| 1149 | error: |
| 1150 | d_drop(dentry); |
| 1151 | _leave(" = %d", ret); |
| 1152 | return ret; |
| 1153 | } |
| 1154 | |
| 1155 | /* |
| 1156 | * rename a file in an AFS filesystem and/or move it between directories |
| 1157 | */ |
| 1158 | static int afs_rename(struct inode *old_dir, struct dentry *old_dentry, |
Miklos Szeredi | 1cd66c9 | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 1159 | struct inode *new_dir, struct dentry *new_dentry, |
| 1160 | unsigned int flags) |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1161 | { |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1162 | struct afs_fs_cursor fc; |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1163 | struct afs_vnode *orig_dvnode, *new_dvnode, *vnode; |
| 1164 | struct key *key; |
| 1165 | int ret; |
| 1166 | |
Miklos Szeredi | 1cd66c9 | 2016-09-27 11:03:58 +0200 | [diff] [blame] | 1167 | if (flags) |
| 1168 | return -EINVAL; |
| 1169 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 1170 | vnode = AFS_FS_I(d_inode(old_dentry)); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1171 | orig_dvnode = AFS_FS_I(old_dir); |
| 1172 | new_dvnode = AFS_FS_I(new_dir); |
| 1173 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 1174 | _enter("{%x:%u},{%x:%u},{%x:%u},{%pd}", |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1175 | orig_dvnode->fid.vid, orig_dvnode->fid.vnode, |
| 1176 | vnode->fid.vid, vnode->fid.vnode, |
| 1177 | new_dvnode->fid.vid, new_dvnode->fid.vnode, |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 1178 | new_dentry); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1179 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1180 | key = afs_request_key(orig_dvnode->volume->cell); |
| 1181 | if (IS_ERR(key)) { |
| 1182 | ret = PTR_ERR(key); |
| 1183 | goto error; |
| 1184 | } |
| 1185 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1186 | ret = -ERESTARTSYS; |
| 1187 | if (afs_begin_vnode_operation(&fc, orig_dvnode, key)) { |
| 1188 | if (orig_dvnode != new_dvnode) { |
| 1189 | if (mutex_lock_interruptible_nested(&new_dvnode->io_lock, 1) < 0) { |
| 1190 | afs_end_vnode_operation(&fc); |
David Howells | bc1527d | 2017-11-20 23:04:08 +0000 | [diff] [blame] | 1191 | goto error_key; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1192 | } |
| 1193 | } |
| 1194 | while (afs_select_fileserver(&fc)) { |
| 1195 | fc.cb_break = orig_dvnode->cb_break + orig_dvnode->cb_s_break; |
| 1196 | fc.cb_break_2 = new_dvnode->cb_break + new_dvnode->cb_s_break; |
| 1197 | afs_fs_rename(&fc, old_dentry->d_name.name, |
| 1198 | new_dvnode, new_dentry->d_name.name); |
| 1199 | } |
| 1200 | |
| 1201 | afs_vnode_commit_status(&fc, orig_dvnode, fc.cb_break); |
| 1202 | afs_vnode_commit_status(&fc, new_dvnode, fc.cb_break_2); |
| 1203 | if (orig_dvnode != new_dvnode) |
| 1204 | mutex_unlock(&new_dvnode->io_lock); |
| 1205 | ret = afs_end_vnode_operation(&fc); |
| 1206 | if (ret < 0) |
| 1207 | goto error_key; |
| 1208 | } |
| 1209 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1210 | error_key: |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1211 | key_put(key); |
| 1212 | error: |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 1213 | _leave(" = %d", ret); |
| 1214 | return ret; |
| 1215 | } |