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