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