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