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