Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/ext2/namei.c |
| 3 | * |
| 4 | * Rewrite to pagecache. Almost all code had been changed, so blame me |
| 5 | * if the things go wrong. Please, send bug reports to |
| 6 | * viro@parcelfarce.linux.theplanet.co.uk |
| 7 | * |
| 8 | * Stuff here is basically a glue between the VFS and generic UNIXish |
| 9 | * filesystem that keeps everything in pagecache. All knowledge of the |
| 10 | * directory layout is in fs/ext2/dir.c - it turned out to be easily separatable |
| 11 | * and it's easier to debug that way. In principle we might want to |
| 12 | * generalize that a bit and turn it into a library. Or not. |
| 13 | * |
| 14 | * The only non-static object here is ext2_dir_inode_operations. |
| 15 | * |
| 16 | * TODO: get rid of kmap() use, add readahead. |
| 17 | * |
| 18 | * Copyright (C) 1992, 1993, 1994, 1995 |
| 19 | * Remy Card (card@masi.ibp.fr) |
| 20 | * Laboratoire MASI - Institut Blaise Pascal |
| 21 | * Universite Pierre et Marie Curie (Paris VI) |
| 22 | * |
| 23 | * from |
| 24 | * |
| 25 | * linux/fs/minix/namei.c |
| 26 | * |
| 27 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 28 | * |
| 29 | * Big-endian to little-endian byte-swapping/bitmaps by |
| 30 | * David S. Miller (davem@caip.rutgers.edu), 1995 |
| 31 | */ |
| 32 | |
| 33 | #include <linux/pagemap.h> |
| 34 | #include "ext2.h" |
| 35 | #include "xattr.h" |
| 36 | #include "acl.h" |
Carsten Otte | 6d79125 | 2005-06-23 22:05:26 -0700 | [diff] [blame] | 37 | #include "xip.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
| 39 | /* |
| 40 | * Couple of helper functions - make the code slightly cleaner. |
| 41 | */ |
| 42 | |
| 43 | static inline void ext2_inc_count(struct inode *inode) |
| 44 | { |
| 45 | inode->i_nlink++; |
| 46 | mark_inode_dirty(inode); |
| 47 | } |
| 48 | |
| 49 | static inline void ext2_dec_count(struct inode *inode) |
| 50 | { |
| 51 | inode->i_nlink--; |
| 52 | mark_inode_dirty(inode); |
| 53 | } |
| 54 | |
| 55 | static inline int ext2_add_nondir(struct dentry *dentry, struct inode *inode) |
| 56 | { |
| 57 | int err = ext2_add_link(dentry, inode); |
| 58 | if (!err) { |
| 59 | d_instantiate(dentry, inode); |
| 60 | return 0; |
| 61 | } |
| 62 | ext2_dec_count(inode); |
| 63 | iput(inode); |
| 64 | return err; |
| 65 | } |
| 66 | |
| 67 | /* |
| 68 | * Methods themselves. |
| 69 | */ |
| 70 | |
| 71 | static struct dentry *ext2_lookup(struct inode * dir, struct dentry *dentry, struct nameidata *nd) |
| 72 | { |
| 73 | struct inode * inode; |
| 74 | ino_t ino; |
| 75 | |
| 76 | if (dentry->d_name.len > EXT2_NAME_LEN) |
| 77 | return ERR_PTR(-ENAMETOOLONG); |
| 78 | |
| 79 | ino = ext2_inode_by_name(dir, dentry); |
| 80 | inode = NULL; |
| 81 | if (ino) { |
| 82 | inode = iget(dir->i_sb, ino); |
| 83 | if (!inode) |
| 84 | return ERR_PTR(-EACCES); |
| 85 | } |
Pekka Enberg | 082a05c | 2006-01-14 13:21:07 -0800 | [diff] [blame] | 86 | return d_splice_alias(inode, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | struct dentry *ext2_get_parent(struct dentry *child) |
| 90 | { |
| 91 | unsigned long ino; |
| 92 | struct dentry *parent; |
| 93 | struct inode *inode; |
| 94 | struct dentry dotdot; |
| 95 | |
| 96 | dotdot.d_name.name = ".."; |
| 97 | dotdot.d_name.len = 2; |
| 98 | |
| 99 | ino = ext2_inode_by_name(child->d_inode, &dotdot); |
| 100 | if (!ino) |
| 101 | return ERR_PTR(-ENOENT); |
| 102 | inode = iget(child->d_inode->i_sb, ino); |
| 103 | |
| 104 | if (!inode) |
| 105 | return ERR_PTR(-EACCES); |
| 106 | parent = d_alloc_anon(inode); |
| 107 | if (!parent) { |
| 108 | iput(inode); |
| 109 | parent = ERR_PTR(-ENOMEM); |
| 110 | } |
| 111 | return parent; |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | * By the time this is called, we already have created |
| 116 | * the directory cache entry for the new file, but it |
| 117 | * is so far negative - it has no inode. |
| 118 | * |
| 119 | * If the create succeeds, we fill in the inode information |
| 120 | * with d_instantiate(). |
| 121 | */ |
| 122 | static int ext2_create (struct inode * dir, struct dentry * dentry, int mode, struct nameidata *nd) |
| 123 | { |
| 124 | struct inode * inode = ext2_new_inode (dir, mode); |
| 125 | int err = PTR_ERR(inode); |
| 126 | if (!IS_ERR(inode)) { |
| 127 | inode->i_op = &ext2_file_inode_operations; |
Carsten Otte | 6d79125 | 2005-06-23 22:05:26 -0700 | [diff] [blame] | 128 | if (ext2_use_xip(inode->i_sb)) { |
| 129 | inode->i_mapping->a_ops = &ext2_aops_xip; |
| 130 | inode->i_fop = &ext2_xip_file_operations; |
| 131 | } else if (test_opt(inode->i_sb, NOBH)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | inode->i_mapping->a_ops = &ext2_nobh_aops; |
Carsten Otte | 6d79125 | 2005-06-23 22:05:26 -0700 | [diff] [blame] | 133 | inode->i_fop = &ext2_file_operations; |
| 134 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | inode->i_mapping->a_ops = &ext2_aops; |
Carsten Otte | 6d79125 | 2005-06-23 22:05:26 -0700 | [diff] [blame] | 136 | inode->i_fop = &ext2_file_operations; |
| 137 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | mark_inode_dirty(inode); |
| 139 | err = ext2_add_nondir(dentry, inode); |
| 140 | } |
| 141 | return err; |
| 142 | } |
| 143 | |
| 144 | static int ext2_mknod (struct inode * dir, struct dentry *dentry, int mode, dev_t rdev) |
| 145 | { |
| 146 | struct inode * inode; |
| 147 | int err; |
| 148 | |
| 149 | if (!new_valid_dev(rdev)) |
| 150 | return -EINVAL; |
| 151 | |
| 152 | inode = ext2_new_inode (dir, mode); |
| 153 | err = PTR_ERR(inode); |
| 154 | if (!IS_ERR(inode)) { |
| 155 | init_special_inode(inode, inode->i_mode, rdev); |
| 156 | #ifdef CONFIG_EXT2_FS_XATTR |
| 157 | inode->i_op = &ext2_special_inode_operations; |
| 158 | #endif |
| 159 | mark_inode_dirty(inode); |
| 160 | err = ext2_add_nondir(dentry, inode); |
| 161 | } |
| 162 | return err; |
| 163 | } |
| 164 | |
| 165 | static int ext2_symlink (struct inode * dir, struct dentry * dentry, |
| 166 | const char * symname) |
| 167 | { |
| 168 | struct super_block * sb = dir->i_sb; |
| 169 | int err = -ENAMETOOLONG; |
| 170 | unsigned l = strlen(symname)+1; |
| 171 | struct inode * inode; |
| 172 | |
| 173 | if (l > sb->s_blocksize) |
| 174 | goto out; |
| 175 | |
| 176 | inode = ext2_new_inode (dir, S_IFLNK | S_IRWXUGO); |
| 177 | err = PTR_ERR(inode); |
| 178 | if (IS_ERR(inode)) |
| 179 | goto out; |
| 180 | |
| 181 | if (l > sizeof (EXT2_I(inode)->i_data)) { |
| 182 | /* slow symlink */ |
| 183 | inode->i_op = &ext2_symlink_inode_operations; |
| 184 | if (test_opt(inode->i_sb, NOBH)) |
| 185 | inode->i_mapping->a_ops = &ext2_nobh_aops; |
| 186 | else |
| 187 | inode->i_mapping->a_ops = &ext2_aops; |
| 188 | err = page_symlink(inode, symname, l); |
| 189 | if (err) |
| 190 | goto out_fail; |
| 191 | } else { |
| 192 | /* fast symlink */ |
| 193 | inode->i_op = &ext2_fast_symlink_inode_operations; |
| 194 | memcpy((char*)(EXT2_I(inode)->i_data),symname,l); |
| 195 | inode->i_size = l-1; |
| 196 | } |
| 197 | mark_inode_dirty(inode); |
| 198 | |
| 199 | err = ext2_add_nondir(dentry, inode); |
| 200 | out: |
| 201 | return err; |
| 202 | |
| 203 | out_fail: |
| 204 | ext2_dec_count(inode); |
| 205 | iput (inode); |
| 206 | goto out; |
| 207 | } |
| 208 | |
| 209 | static int ext2_link (struct dentry * old_dentry, struct inode * dir, |
| 210 | struct dentry *dentry) |
| 211 | { |
| 212 | struct inode *inode = old_dentry->d_inode; |
| 213 | |
| 214 | if (inode->i_nlink >= EXT2_LINK_MAX) |
| 215 | return -EMLINK; |
| 216 | |
| 217 | inode->i_ctime = CURRENT_TIME_SEC; |
| 218 | ext2_inc_count(inode); |
| 219 | atomic_inc(&inode->i_count); |
| 220 | |
| 221 | return ext2_add_nondir(dentry, inode); |
| 222 | } |
| 223 | |
| 224 | static int ext2_mkdir(struct inode * dir, struct dentry * dentry, int mode) |
| 225 | { |
| 226 | struct inode * inode; |
| 227 | int err = -EMLINK; |
| 228 | |
| 229 | if (dir->i_nlink >= EXT2_LINK_MAX) |
| 230 | goto out; |
| 231 | |
| 232 | ext2_inc_count(dir); |
| 233 | |
| 234 | inode = ext2_new_inode (dir, S_IFDIR | mode); |
| 235 | err = PTR_ERR(inode); |
| 236 | if (IS_ERR(inode)) |
| 237 | goto out_dir; |
| 238 | |
| 239 | inode->i_op = &ext2_dir_inode_operations; |
| 240 | inode->i_fop = &ext2_dir_operations; |
| 241 | if (test_opt(inode->i_sb, NOBH)) |
| 242 | inode->i_mapping->a_ops = &ext2_nobh_aops; |
| 243 | else |
| 244 | inode->i_mapping->a_ops = &ext2_aops; |
| 245 | |
| 246 | ext2_inc_count(inode); |
| 247 | |
| 248 | err = ext2_make_empty(inode, dir); |
| 249 | if (err) |
| 250 | goto out_fail; |
| 251 | |
| 252 | err = ext2_add_link(dentry, inode); |
| 253 | if (err) |
| 254 | goto out_fail; |
| 255 | |
| 256 | d_instantiate(dentry, inode); |
| 257 | out: |
| 258 | return err; |
| 259 | |
| 260 | out_fail: |
| 261 | ext2_dec_count(inode); |
| 262 | ext2_dec_count(inode); |
| 263 | iput(inode); |
| 264 | out_dir: |
| 265 | ext2_dec_count(dir); |
| 266 | goto out; |
| 267 | } |
| 268 | |
| 269 | static int ext2_unlink(struct inode * dir, struct dentry *dentry) |
| 270 | { |
| 271 | struct inode * inode = dentry->d_inode; |
| 272 | struct ext2_dir_entry_2 * de; |
| 273 | struct page * page; |
| 274 | int err = -ENOENT; |
| 275 | |
| 276 | de = ext2_find_entry (dir, dentry, &page); |
| 277 | if (!de) |
| 278 | goto out; |
| 279 | |
| 280 | err = ext2_delete_entry (de, page); |
| 281 | if (err) |
| 282 | goto out; |
| 283 | |
| 284 | inode->i_ctime = dir->i_ctime; |
| 285 | ext2_dec_count(inode); |
| 286 | err = 0; |
| 287 | out: |
| 288 | return err; |
| 289 | } |
| 290 | |
| 291 | static int ext2_rmdir (struct inode * dir, struct dentry *dentry) |
| 292 | { |
| 293 | struct inode * inode = dentry->d_inode; |
| 294 | int err = -ENOTEMPTY; |
| 295 | |
| 296 | if (ext2_empty_dir(inode)) { |
| 297 | err = ext2_unlink(dir, dentry); |
| 298 | if (!err) { |
| 299 | inode->i_size = 0; |
| 300 | ext2_dec_count(inode); |
| 301 | ext2_dec_count(dir); |
| 302 | } |
| 303 | } |
| 304 | return err; |
| 305 | } |
| 306 | |
| 307 | static int ext2_rename (struct inode * old_dir, struct dentry * old_dentry, |
| 308 | struct inode * new_dir, struct dentry * new_dentry ) |
| 309 | { |
| 310 | struct inode * old_inode = old_dentry->d_inode; |
| 311 | struct inode * new_inode = new_dentry->d_inode; |
| 312 | struct page * dir_page = NULL; |
| 313 | struct ext2_dir_entry_2 * dir_de = NULL; |
| 314 | struct page * old_page; |
| 315 | struct ext2_dir_entry_2 * old_de; |
| 316 | int err = -ENOENT; |
| 317 | |
| 318 | old_de = ext2_find_entry (old_dir, old_dentry, &old_page); |
| 319 | if (!old_de) |
| 320 | goto out; |
| 321 | |
| 322 | if (S_ISDIR(old_inode->i_mode)) { |
| 323 | err = -EIO; |
| 324 | dir_de = ext2_dotdot(old_inode, &dir_page); |
| 325 | if (!dir_de) |
| 326 | goto out_old; |
| 327 | } |
| 328 | |
| 329 | if (new_inode) { |
| 330 | struct page *new_page; |
| 331 | struct ext2_dir_entry_2 *new_de; |
| 332 | |
| 333 | err = -ENOTEMPTY; |
| 334 | if (dir_de && !ext2_empty_dir (new_inode)) |
| 335 | goto out_dir; |
| 336 | |
| 337 | err = -ENOENT; |
| 338 | new_de = ext2_find_entry (new_dir, new_dentry, &new_page); |
| 339 | if (!new_de) |
| 340 | goto out_dir; |
| 341 | ext2_inc_count(old_inode); |
| 342 | ext2_set_link(new_dir, new_de, new_page, old_inode); |
| 343 | new_inode->i_ctime = CURRENT_TIME_SEC; |
| 344 | if (dir_de) |
| 345 | new_inode->i_nlink--; |
| 346 | ext2_dec_count(new_inode); |
| 347 | } else { |
| 348 | if (dir_de) { |
| 349 | err = -EMLINK; |
| 350 | if (new_dir->i_nlink >= EXT2_LINK_MAX) |
| 351 | goto out_dir; |
| 352 | } |
| 353 | ext2_inc_count(old_inode); |
| 354 | err = ext2_add_link(new_dentry, old_inode); |
| 355 | if (err) { |
| 356 | ext2_dec_count(old_inode); |
| 357 | goto out_dir; |
| 358 | } |
| 359 | if (dir_de) |
| 360 | ext2_inc_count(new_dir); |
| 361 | } |
| 362 | |
| 363 | /* |
| 364 | * Like most other Unix systems, set the ctime for inodes on a |
| 365 | * rename. |
| 366 | * ext2_dec_count() will mark the inode dirty. |
| 367 | */ |
| 368 | old_inode->i_ctime = CURRENT_TIME_SEC; |
| 369 | |
| 370 | ext2_delete_entry (old_de, old_page); |
| 371 | ext2_dec_count(old_inode); |
| 372 | |
| 373 | if (dir_de) { |
| 374 | ext2_set_link(old_inode, dir_de, dir_page, new_dir); |
| 375 | ext2_dec_count(old_dir); |
| 376 | } |
| 377 | return 0; |
| 378 | |
| 379 | |
| 380 | out_dir: |
| 381 | if (dir_de) { |
| 382 | kunmap(dir_page); |
| 383 | page_cache_release(dir_page); |
| 384 | } |
| 385 | out_old: |
| 386 | kunmap(old_page); |
| 387 | page_cache_release(old_page); |
| 388 | out: |
| 389 | return err; |
| 390 | } |
| 391 | |
| 392 | struct inode_operations ext2_dir_inode_operations = { |
| 393 | .create = ext2_create, |
| 394 | .lookup = ext2_lookup, |
| 395 | .link = ext2_link, |
| 396 | .unlink = ext2_unlink, |
| 397 | .symlink = ext2_symlink, |
| 398 | .mkdir = ext2_mkdir, |
| 399 | .rmdir = ext2_rmdir, |
| 400 | .mknod = ext2_mknod, |
| 401 | .rename = ext2_rename, |
| 402 | #ifdef CONFIG_EXT2_FS_XATTR |
| 403 | .setxattr = generic_setxattr, |
| 404 | .getxattr = generic_getxattr, |
| 405 | .listxattr = ext2_listxattr, |
| 406 | .removexattr = generic_removexattr, |
| 407 | #endif |
| 408 | .setattr = ext2_setattr, |
| 409 | .permission = ext2_permission, |
| 410 | }; |
| 411 | |
| 412 | struct inode_operations ext2_special_inode_operations = { |
| 413 | #ifdef CONFIG_EXT2_FS_XATTR |
| 414 | .setxattr = generic_setxattr, |
| 415 | .getxattr = generic_getxattr, |
| 416 | .listxattr = ext2_listxattr, |
| 417 | .removexattr = generic_removexattr, |
| 418 | #endif |
| 419 | .setattr = ext2_setattr, |
| 420 | .permission = ext2_permission, |
| 421 | }; |