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