^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * linux/fs/hfs/dir.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 1995-1997 Paul H. Hargrove
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * (C) 2003 Ardis Technologies <roman@ardistech.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * This file may be distributed under the terms of the GNU General Public License.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * This file contains directory-related functions independent of which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * scheme is being used to represent forks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Based on the minix file system code, (C) 1991, 1992 by Linus Torvalds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include "hfs_fs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include "btree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * hfs_lookup()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) static struct dentry *hfs_lookup(struct inode *dir, struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) hfs_cat_rec rec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct hfs_find_data fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct inode *inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) res = hfs_find_init(HFS_SB(dir->i_sb)->cat_tree, &fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) return ERR_PTR(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) hfs_cat_build_key(dir->i_sb, fd.search_key, dir->i_ino, &dentry->d_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) res = hfs_brec_read(&fd, &rec, sizeof(rec));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) if (res) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) if (res != -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) inode = ERR_PTR(res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) inode = hfs_iget(dir->i_sb, &fd.search_key->cat, &rec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (!inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) inode = ERR_PTR(-EACCES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) hfs_find_exit(&fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) return d_splice_alias(inode, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * hfs_readdir
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) static int hfs_readdir(struct file *file, struct dir_context *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) int len, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) char strbuf[HFS_MAX_NAMELEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) union hfs_cat_rec entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) struct hfs_find_data fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) struct hfs_readdir_data *rd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) u16 type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if (ctx->pos >= inode->i_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) err = hfs_find_init(HFS_SB(sb)->cat_tree, &fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) hfs_cat_build_key(sb, fd.search_key, inode->i_ino, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) err = hfs_brec_find(&fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) if (ctx->pos == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) /* This is completely artificial... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (!dir_emit_dot(file, ctx))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) ctx->pos = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (ctx->pos == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) goto out;
^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) hfs_bnode_read(fd.bnode, &entry, fd.entryoffset, fd.entrylength);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (entry.type != HFS_CDR_THD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) pr_err("bad catalog folder thread\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) //if (fd.entrylength < HFS_MIN_THREAD_SZ) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) // pr_err("truncated catalog thread\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) // err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) // goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) //}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (!dir_emit(ctx, "..", 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) be32_to_cpu(entry.thread.ParID), DT_DIR))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) ctx->pos = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (ctx->pos >= inode->i_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) err = hfs_brec_goto(&fd, ctx->pos - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if (be32_to_cpu(fd.key->cat.ParID) != inode->i_ino) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) pr_err("walked past end of dir\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (fd.entrylength > sizeof(entry) || fd.entrylength < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) hfs_bnode_read(fd.bnode, &entry, fd.entryoffset, fd.entrylength);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) type = entry.type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) len = hfs_mac2asc(sb, strbuf, &fd.key->cat.CName);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (type == HFS_CDR_DIR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (fd.entrylength < sizeof(struct hfs_cat_dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) pr_err("small dir entry\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (!dir_emit(ctx, strbuf, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) be32_to_cpu(entry.dir.DirID), DT_DIR))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) } else if (type == HFS_CDR_FIL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (fd.entrylength < sizeof(struct hfs_cat_file)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) pr_err("small file entry\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (!dir_emit(ctx, strbuf, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) be32_to_cpu(entry.file.FlNum), DT_REG))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) pr_err("bad catalog entry type %d\n", type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) ctx->pos++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (ctx->pos >= inode->i_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) err = hfs_brec_goto(&fd, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) rd = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) if (!rd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) rd = kmalloc(sizeof(struct hfs_readdir_data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if (!rd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) file->private_data = rd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) rd->file = file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) spin_lock(&HFS_I(inode)->open_dir_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) list_add(&rd->list, &HFS_I(inode)->open_dir_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) spin_unlock(&HFS_I(inode)->open_dir_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) * Can be done after the list insertion; exclusion with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) * hfs_delete_cat() is provided by directory lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) memcpy(&rd->key, &fd.key->cat, sizeof(struct hfs_cat_key));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) hfs_find_exit(&fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) static int hfs_dir_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) struct hfs_readdir_data *rd = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (rd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) spin_lock(&HFS_I(inode)->open_dir_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) list_del(&rd->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) spin_unlock(&HFS_I(inode)->open_dir_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) kfree(rd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) * hfs_create()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) * This is the create() entry in the inode_operations structure for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) * regular HFS directories. The purpose is to create a new file in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) * a directory and return a corresponding inode, given the inode for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) * the directory and the name (and its length) of the new file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) static int hfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) bool excl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) inode = hfs_new_inode(dir, &dentry->d_name, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) if (!inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) res = hfs_cat_create(inode->i_ino, dir, &dentry->d_name, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) if (res) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) clear_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) hfs_delete_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) d_instantiate(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) * hfs_mkdir()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) * This is the mkdir() entry in the inode_operations structure for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) * regular HFS directories. The purpose is to create a new directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) * in a directory, given the inode for the parent directory and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) * name (and its length) of the new directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) static int hfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) inode = hfs_new_inode(dir, &dentry->d_name, S_IFDIR | mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) if (!inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) res = hfs_cat_create(inode->i_ino, dir, &dentry->d_name, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) if (res) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) clear_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) hfs_delete_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) d_instantiate(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) * hfs_remove()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) * This serves as both unlink() and rmdir() in the inode_operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) * structure for regular HFS directories. The purpose is to delete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) * an existing child, given the inode for the parent directory and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) * the name (and its length) of the existing directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * HFS does not have hardlinks, so both rmdir and unlink set the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) * link count to 0. The only difference is the emptiness check.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) static int hfs_remove(struct inode *dir, struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) struct inode *inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) if (S_ISDIR(inode->i_mode) && inode->i_size != 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) return -ENOTEMPTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) res = hfs_cat_delete(inode->i_ino, dir, &dentry->d_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) clear_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) inode->i_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) hfs_delete_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) * hfs_rename()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) * This is the rename() entry in the inode_operations structure for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) * regular HFS directories. The purpose is to rename an existing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) * file or directory, given the inode for the current directory and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) * the name (and its length) of the existing file/directory and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) * inode for the new directory and the name (and its length) of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) * new file/directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) * XXX: how do you handle must_be dir?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) static int hfs_rename(struct inode *old_dir, struct dentry *old_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) struct inode *new_dir, struct dentry *new_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) if (flags & ~RENAME_NOREPLACE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) /* Unlink destination if it already exists */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) if (d_really_is_positive(new_dentry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) res = hfs_remove(new_dir, new_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) res = hfs_cat_move(d_inode(old_dentry)->i_ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) old_dir, &old_dentry->d_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) new_dir, &new_dentry->d_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) if (!res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) hfs_cat_build_key(old_dir->i_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) (btree_key *)&HFS_I(d_inode(old_dentry))->cat_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) new_dir->i_ino, &new_dentry->d_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) const struct file_operations hfs_dir_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) .read = generic_read_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) .iterate_shared = hfs_readdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) .llseek = generic_file_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) .release = hfs_dir_release,
^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) const struct inode_operations hfs_dir_inode_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) .create = hfs_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) .lookup = hfs_lookup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) .unlink = hfs_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) .mkdir = hfs_mkdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) .rmdir = hfs_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) .rename = hfs_rename,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) .setattr = hfs_inode_setattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) };