Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/affs/namei.c |
| 3 | * |
| 4 | * (c) 1996 Hans-Joachim Widmaier - Rewritten |
| 5 | * |
| 6 | * (C) 1993 Ray Burr - Modified for Amiga FFS filesystem. |
| 7 | * |
| 8 | * (C) 1991 Linus Torvalds - minix filesystem |
| 9 | */ |
| 10 | |
| 11 | #include "affs.h" |
| 12 | |
| 13 | typedef int (*toupper_t)(int); |
| 14 | |
| 15 | static int affs_toupper(int ch); |
Linus Torvalds | da53be1 | 2013-05-21 15:22:44 -0700 | [diff] [blame] | 16 | static int affs_hash_dentry(const struct dentry *, struct qstr *); |
| 17 | static int affs_compare_dentry(const struct dentry *parent, const struct dentry *dentry, |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 18 | unsigned int len, const char *str, const struct qstr *name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | static int affs_intl_toupper(int ch); |
Linus Torvalds | da53be1 | 2013-05-21 15:22:44 -0700 | [diff] [blame] | 20 | static int affs_intl_hash_dentry(const struct dentry *, struct qstr *); |
| 21 | static int affs_intl_compare_dentry(const struct dentry *parent, const struct dentry *dentry, |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 22 | unsigned int len, const char *str, const struct qstr *name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Al Viro | e16404e | 2009-02-20 05:55:13 +0000 | [diff] [blame] | 24 | const struct dentry_operations affs_dentry_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | .d_hash = affs_hash_dentry, |
| 26 | .d_compare = affs_compare_dentry, |
| 27 | }; |
| 28 | |
Al Viro | a129880 | 2011-01-12 16:45:19 -0500 | [diff] [blame] | 29 | const struct dentry_operations affs_intl_dentry_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | .d_hash = affs_intl_hash_dentry, |
| 31 | .d_compare = affs_intl_compare_dentry, |
| 32 | }; |
| 33 | |
| 34 | |
| 35 | /* Simple toupper() for DOS\1 */ |
| 36 | |
| 37 | static int |
| 38 | affs_toupper(int ch) |
| 39 | { |
| 40 | return ch >= 'a' && ch <= 'z' ? ch -= ('a' - 'A') : ch; |
| 41 | } |
| 42 | |
| 43 | /* International toupper() for DOS\3 ("international") */ |
| 44 | |
| 45 | static int |
| 46 | affs_intl_toupper(int ch) |
| 47 | { |
| 48 | return (ch >= 'a' && ch <= 'z') || (ch >= 0xE0 |
| 49 | && ch <= 0xFE && ch != 0xF7) ? |
| 50 | ch - ('a' - 'A') : ch; |
| 51 | } |
| 52 | |
| 53 | static inline toupper_t |
| 54 | affs_get_toupper(struct super_block *sb) |
| 55 | { |
| 56 | return AFFS_SB(sb)->s_flags & SF_INTL ? affs_intl_toupper : affs_toupper; |
| 57 | } |
| 58 | |
| 59 | /* |
| 60 | * Note: the dentry argument is the parent dentry. |
| 61 | */ |
| 62 | static inline int |
Nick Piggin | b1e6a01 | 2011-01-07 17:49:28 +1100 | [diff] [blame] | 63 | __affs_hash_dentry(struct qstr *qstr, toupper_t toupper) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | { |
| 65 | const u8 *name = qstr->name; |
| 66 | unsigned long hash; |
| 67 | int i; |
| 68 | |
Nick Piggin | b1e6a01 | 2011-01-07 17:49:28 +1100 | [diff] [blame] | 69 | i = affs_check_name(qstr->name, qstr->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | if (i) |
| 71 | return i; |
| 72 | |
| 73 | hash = init_name_hash(); |
| 74 | i = min(qstr->len, 30u); |
| 75 | for (; i > 0; name++, i--) |
| 76 | hash = partial_name_hash(toupper(*name), hash); |
| 77 | qstr->hash = end_name_hash(hash); |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | static int |
Linus Torvalds | da53be1 | 2013-05-21 15:22:44 -0700 | [diff] [blame] | 83 | affs_hash_dentry(const struct dentry *dentry, struct qstr *qstr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | { |
Nick Piggin | b1e6a01 | 2011-01-07 17:49:28 +1100 | [diff] [blame] | 85 | return __affs_hash_dentry(qstr, affs_toupper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | } |
| 87 | static int |
Linus Torvalds | da53be1 | 2013-05-21 15:22:44 -0700 | [diff] [blame] | 88 | affs_intl_hash_dentry(const struct dentry *dentry, struct qstr *qstr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | { |
Nick Piggin | b1e6a01 | 2011-01-07 17:49:28 +1100 | [diff] [blame] | 90 | return __affs_hash_dentry(qstr, affs_intl_toupper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 93 | static inline int __affs_compare_dentry(unsigned int len, |
| 94 | const char *str, const struct qstr *name, toupper_t toupper) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | { |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 96 | const u8 *aname = str; |
| 97 | const u8 *bname = name->name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 99 | /* |
| 100 | * 'str' is the name of an already existing dentry, so the name |
| 101 | * must be valid. 'name' must be validated first. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | */ |
| 103 | |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 104 | if (affs_check_name(name->name, name->len)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | return 1; |
| 106 | |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 107 | /* |
| 108 | * If the names are longer than the allowed 30 chars, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | * the excess is ignored, so their length may differ. |
| 110 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | if (len >= 30) { |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 112 | if (name->len < 30) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | return 1; |
| 114 | len = 30; |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 115 | } else if (len != name->len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | return 1; |
| 117 | |
| 118 | for (; len > 0; len--) |
| 119 | if (toupper(*aname++) != toupper(*bname++)) |
| 120 | return 1; |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | static int |
Linus Torvalds | da53be1 | 2013-05-21 15:22:44 -0700 | [diff] [blame] | 126 | affs_compare_dentry(const struct dentry *parent, const struct dentry *dentry, |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 127 | unsigned int len, const char *str, const struct qstr *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | { |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 129 | return __affs_compare_dentry(len, str, name, affs_toupper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | } |
| 131 | static int |
Linus Torvalds | da53be1 | 2013-05-21 15:22:44 -0700 | [diff] [blame] | 132 | affs_intl_compare_dentry(const struct dentry *parent, const struct dentry *dentry, |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 133 | unsigned int len, const char *str, const struct qstr *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | { |
Nick Piggin | 621e155 | 2011-01-07 17:49:27 +1100 | [diff] [blame] | 135 | return __affs_compare_dentry(len, str, name, affs_intl_toupper); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /* |
| 139 | * NOTE! unlike strncmp, affs_match returns 1 for success, 0 for failure. |
| 140 | */ |
| 141 | |
| 142 | static inline int |
| 143 | affs_match(struct dentry *dentry, const u8 *name2, toupper_t toupper) |
| 144 | { |
| 145 | const u8 *name = dentry->d_name.name; |
| 146 | int len = dentry->d_name.len; |
| 147 | |
| 148 | if (len >= 30) { |
| 149 | if (*name2 < 30) |
| 150 | return 0; |
| 151 | len = 30; |
| 152 | } else if (len != *name2) |
| 153 | return 0; |
| 154 | |
| 155 | for (name2++; len > 0; len--) |
| 156 | if (toupper(*name++) != toupper(*name2++)) |
| 157 | return 0; |
| 158 | return 1; |
| 159 | } |
| 160 | |
| 161 | int |
| 162 | affs_hash_name(struct super_block *sb, const u8 *name, unsigned int len) |
| 163 | { |
| 164 | toupper_t toupper = affs_get_toupper(sb); |
| 165 | int hash; |
| 166 | |
| 167 | hash = len = min(len, 30u); |
| 168 | for (; len > 0; len--) |
| 169 | hash = (hash * 13 + toupper(*name++)) & 0x7ff; |
| 170 | |
| 171 | return hash % AFFS_SB(sb)->s_hashsize; |
| 172 | } |
| 173 | |
| 174 | static struct buffer_head * |
| 175 | affs_find_entry(struct inode *dir, struct dentry *dentry) |
| 176 | { |
| 177 | struct super_block *sb = dir->i_sb; |
| 178 | struct buffer_head *bh; |
| 179 | toupper_t toupper = affs_get_toupper(sb); |
| 180 | u32 key; |
| 181 | |
| 182 | pr_debug("AFFS: find_entry(\"%.*s\")\n", (int)dentry->d_name.len, dentry->d_name.name); |
| 183 | |
| 184 | bh = affs_bread(sb, dir->i_ino); |
| 185 | if (!bh) |
| 186 | return ERR_PTR(-EIO); |
| 187 | |
| 188 | key = be32_to_cpu(AFFS_HEAD(bh)->table[affs_hash_name(sb, dentry->d_name.name, dentry->d_name.len)]); |
| 189 | |
| 190 | for (;;) { |
| 191 | affs_brelse(bh); |
| 192 | if (key == 0) |
| 193 | return NULL; |
| 194 | bh = affs_bread(sb, key); |
| 195 | if (!bh) |
| 196 | return ERR_PTR(-EIO); |
| 197 | if (affs_match(dentry, AFFS_TAIL(sb, bh)->name, toupper)) |
| 198 | return bh; |
| 199 | key = be32_to_cpu(AFFS_TAIL(sb, bh)->hash_chain); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | struct dentry * |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 204 | affs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | { |
| 206 | struct super_block *sb = dir->i_sb; |
| 207 | struct buffer_head *bh; |
| 208 | struct inode *inode = NULL; |
| 209 | |
| 210 | pr_debug("AFFS: lookup(\"%.*s\")\n",(int)dentry->d_name.len,dentry->d_name.name); |
| 211 | |
| 212 | affs_lock_dir(dir); |
| 213 | bh = affs_find_entry(dir, dentry); |
| 214 | affs_unlock_dir(dir); |
David Howells | 210f855 | 2008-02-07 00:15:29 -0800 | [diff] [blame] | 215 | if (IS_ERR(bh)) |
David Howells | e231c2e | 2008-02-07 00:15:26 -0800 | [diff] [blame] | 216 | return ERR_CAST(bh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | if (bh) { |
| 218 | u32 ino = bh->b_blocknr; |
| 219 | |
| 220 | /* store the real header ino in d_fsdata for faster lookups */ |
| 221 | dentry->d_fsdata = (void *)(long)ino; |
| 222 | switch (be32_to_cpu(AFFS_TAIL(sb, bh)->stype)) { |
| 223 | //link to dirs disabled |
| 224 | //case ST_LINKDIR: |
| 225 | case ST_LINKFILE: |
| 226 | ino = be32_to_cpu(AFFS_TAIL(sb, bh)->original); |
| 227 | } |
| 228 | affs_brelse(bh); |
David Howells | 210f855 | 2008-02-07 00:15:29 -0800 | [diff] [blame] | 229 | inode = affs_iget(sb, ino); |
| 230 | if (IS_ERR(inode)) |
Julia Lawall | cccad8f | 2010-05-26 14:44:23 -0700 | [diff] [blame] | 231 | return ERR_CAST(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | d_add(dentry, inode); |
| 234 | return NULL; |
| 235 | } |
| 236 | |
| 237 | int |
| 238 | affs_unlink(struct inode *dir, struct dentry *dentry) |
| 239 | { |
Roman Zippel | dca3c33 | 2008-04-29 17:02:20 +0200 | [diff] [blame] | 240 | pr_debug("AFFS: unlink(dir=%d, %lu \"%.*s\")\n", (u32)dir->i_ino, |
| 241 | dentry->d_inode->i_ino, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | (int)dentry->d_name.len, dentry->d_name.name); |
| 243 | |
| 244 | return affs_remove_header(dentry); |
| 245 | } |
| 246 | |
| 247 | int |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 248 | affs_create(struct inode *dir, struct dentry *dentry, umode_t mode, bool excl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | { |
| 250 | struct super_block *sb = dir->i_sb; |
| 251 | struct inode *inode; |
| 252 | int error; |
| 253 | |
Al Viro | 4acdaf2 | 2011-07-26 01:42:34 -0400 | [diff] [blame] | 254 | pr_debug("AFFS: create(%lu,\"%.*s\",0%ho)\n",dir->i_ino,(int)dentry->d_name.len, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | dentry->d_name.name,mode); |
| 256 | |
| 257 | inode = affs_new_inode(dir); |
| 258 | if (!inode) |
| 259 | return -ENOSPC; |
| 260 | |
| 261 | inode->i_mode = mode; |
| 262 | mode_to_prot(inode); |
| 263 | mark_inode_dirty(inode); |
| 264 | |
| 265 | inode->i_op = &affs_file_inode_operations; |
| 266 | inode->i_fop = &affs_file_operations; |
| 267 | inode->i_mapping->a_ops = (AFFS_SB(sb)->s_flags & SF_OFS) ? &affs_aops_ofs : &affs_aops; |
| 268 | error = affs_add_entry(dir, inode, dentry, ST_FILE); |
| 269 | if (error) { |
Miklos Szeredi | 6d6b77f | 2011-10-28 14:13:28 +0200 | [diff] [blame] | 270 | clear_nlink(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | iput(inode); |
| 272 | return error; |
| 273 | } |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | int |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 278 | affs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | { |
| 280 | struct inode *inode; |
| 281 | int error; |
| 282 | |
Al Viro | 18bb1db | 2011-07-26 01:41:39 -0400 | [diff] [blame] | 283 | pr_debug("AFFS: mkdir(%lu,\"%.*s\",0%ho)\n",dir->i_ino, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | (int)dentry->d_name.len,dentry->d_name.name,mode); |
| 285 | |
| 286 | inode = affs_new_inode(dir); |
| 287 | if (!inode) |
| 288 | return -ENOSPC; |
| 289 | |
| 290 | inode->i_mode = S_IFDIR | mode; |
| 291 | mode_to_prot(inode); |
| 292 | |
| 293 | inode->i_op = &affs_dir_inode_operations; |
| 294 | inode->i_fop = &affs_dir_operations; |
| 295 | |
| 296 | error = affs_add_entry(dir, inode, dentry, ST_USERDIR); |
| 297 | if (error) { |
Miklos Szeredi | 6d6b77f | 2011-10-28 14:13:28 +0200 | [diff] [blame] | 298 | clear_nlink(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | mark_inode_dirty(inode); |
| 300 | iput(inode); |
| 301 | return error; |
| 302 | } |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | int |
| 307 | affs_rmdir(struct inode *dir, struct dentry *dentry) |
| 308 | { |
Roman Zippel | dca3c33 | 2008-04-29 17:02:20 +0200 | [diff] [blame] | 309 | pr_debug("AFFS: rmdir(dir=%u, %lu \"%.*s\")\n", (u32)dir->i_ino, |
| 310 | dentry->d_inode->i_ino, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | (int)dentry->d_name.len, dentry->d_name.name); |
| 312 | |
| 313 | return affs_remove_header(dentry); |
| 314 | } |
| 315 | |
| 316 | int |
| 317 | affs_symlink(struct inode *dir, struct dentry *dentry, const char *symname) |
| 318 | { |
| 319 | struct super_block *sb = dir->i_sb; |
| 320 | struct buffer_head *bh; |
| 321 | struct inode *inode; |
| 322 | char *p; |
| 323 | int i, maxlen, error; |
| 324 | char c, lc; |
| 325 | |
| 326 | pr_debug("AFFS: symlink(%lu,\"%.*s\" -> \"%s\")\n",dir->i_ino, |
| 327 | (int)dentry->d_name.len,dentry->d_name.name,symname); |
| 328 | |
| 329 | maxlen = AFFS_SB(sb)->s_hashsize * sizeof(u32) - 1; |
| 330 | inode = affs_new_inode(dir); |
| 331 | if (!inode) |
| 332 | return -ENOSPC; |
| 333 | |
| 334 | inode->i_op = &affs_symlink_inode_operations; |
| 335 | inode->i_data.a_ops = &affs_symlink_aops; |
| 336 | inode->i_mode = S_IFLNK | 0777; |
| 337 | mode_to_prot(inode); |
| 338 | |
| 339 | error = -EIO; |
| 340 | bh = affs_bread(sb, inode->i_ino); |
| 341 | if (!bh) |
| 342 | goto err; |
| 343 | i = 0; |
| 344 | p = (char *)AFFS_HEAD(bh)->table; |
| 345 | lc = '/'; |
| 346 | if (*symname == '/') { |
Al Viro | 2933392 | 2010-01-24 00:04:07 -0500 | [diff] [blame] | 347 | struct affs_sb_info *sbi = AFFS_SB(sb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | while (*symname == '/') |
| 349 | symname++; |
Al Viro | 2933392 | 2010-01-24 00:04:07 -0500 | [diff] [blame] | 350 | spin_lock(&sbi->symlink_lock); |
| 351 | while (sbi->s_volume[i]) /* Cannot overflow */ |
| 352 | *p++ = sbi->s_volume[i++]; |
| 353 | spin_unlock(&sbi->symlink_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | } |
| 355 | while (i < maxlen && (c = *symname++)) { |
| 356 | if (c == '.' && lc == '/' && *symname == '.' && symname[1] == '/') { |
| 357 | *p++ = '/'; |
| 358 | i++; |
| 359 | symname += 2; |
| 360 | lc = '/'; |
| 361 | } else if (c == '.' && lc == '/' && *symname == '/') { |
| 362 | symname++; |
| 363 | lc = '/'; |
| 364 | } else { |
| 365 | *p++ = c; |
| 366 | lc = c; |
| 367 | i++; |
| 368 | } |
| 369 | if (lc == '/') |
| 370 | while (*symname == '/') |
| 371 | symname++; |
| 372 | } |
| 373 | *p = 0; |
| 374 | mark_buffer_dirty_inode(bh, inode); |
| 375 | affs_brelse(bh); |
| 376 | mark_inode_dirty(inode); |
| 377 | |
| 378 | error = affs_add_entry(dir, inode, dentry, ST_SOFTLINK); |
| 379 | if (error) |
| 380 | goto err; |
| 381 | |
| 382 | return 0; |
| 383 | |
| 384 | err: |
Miklos Szeredi | 6d6b77f | 2011-10-28 14:13:28 +0200 | [diff] [blame] | 385 | clear_nlink(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | mark_inode_dirty(inode); |
| 387 | iput(inode); |
| 388 | return error; |
| 389 | } |
| 390 | |
| 391 | int |
| 392 | affs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) |
| 393 | { |
| 394 | struct inode *inode = old_dentry->d_inode; |
| 395 | |
| 396 | pr_debug("AFFS: link(%u, %u, \"%.*s\")\n", (u32)inode->i_ino, (u32)dir->i_ino, |
| 397 | (int)dentry->d_name.len,dentry->d_name.name); |
| 398 | |
| 399 | return affs_add_entry(dir, inode, dentry, ST_LINKFILE); |
| 400 | } |
| 401 | |
| 402 | int |
| 403 | affs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 404 | struct inode *new_dir, struct dentry *new_dentry) |
| 405 | { |
| 406 | struct super_block *sb = old_dir->i_sb; |
| 407 | struct buffer_head *bh = NULL; |
| 408 | int retval; |
| 409 | |
| 410 | pr_debug("AFFS: rename(old=%u,\"%*s\" to new=%u,\"%*s\")\n", |
| 411 | (u32)old_dir->i_ino, (int)old_dentry->d_name.len, old_dentry->d_name.name, |
| 412 | (u32)new_dir->i_ino, (int)new_dentry->d_name.len, new_dentry->d_name.name); |
| 413 | |
| 414 | retval = affs_check_name(new_dentry->d_name.name,new_dentry->d_name.len); |
| 415 | if (retval) |
| 416 | return retval; |
| 417 | |
| 418 | /* Unlink destination if it already exists */ |
| 419 | if (new_dentry->d_inode) { |
| 420 | retval = affs_remove_header(new_dentry); |
| 421 | if (retval) |
| 422 | return retval; |
| 423 | } |
| 424 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | bh = affs_bread(sb, old_dentry->d_inode->i_ino); |
| 426 | if (!bh) |
Florin Malita | 3ac8141 | 2006-05-25 18:44:23 -0700 | [diff] [blame] | 427 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | |
| 429 | /* Remove header from its parent directory. */ |
| 430 | affs_lock_dir(old_dir); |
| 431 | retval = affs_remove_hash(old_dir, bh); |
| 432 | affs_unlock_dir(old_dir); |
| 433 | if (retval) |
| 434 | goto done; |
| 435 | |
| 436 | /* And insert it into the new directory with the new name. */ |
| 437 | affs_copy_name(AFFS_TAIL(sb, bh)->name, new_dentry); |
| 438 | affs_fix_checksum(sb, bh); |
| 439 | affs_lock_dir(new_dir); |
| 440 | retval = affs_insert_hash(new_dir, bh); |
| 441 | affs_unlock_dir(new_dir); |
| 442 | /* TODO: move it back to old_dir, if error? */ |
| 443 | |
| 444 | done: |
| 445 | mark_buffer_dirty_inode(bh, retval ? old_dir : new_dir); |
| 446 | affs_brelse(bh); |
| 447 | return retval; |
| 448 | } |