^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/affs/dir.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * (c) 1996 Hans-Joachim Widmaier - Rewritten
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * (C) 1993 Ray Burr - Modified for Amiga FFS filesystem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * (C) 1992 Eric Youngdale Modified for ISO 9660 filesystem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * (C) 1991 Linus Torvalds - minix filesystem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * affs directory handling functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/iversion.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "affs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) static int affs_readdir(struct file *, struct dir_context *);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) const struct file_operations affs_dir_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) .read = generic_read_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) .llseek = generic_file_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) .iterate_shared = affs_readdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) .fsync = affs_file_fsync,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * directories can handle most operations...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) const struct inode_operations affs_dir_inode_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) .create = affs_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) .lookup = affs_lookup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) .link = affs_link,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) .unlink = affs_unlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) .symlink = affs_symlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) .mkdir = affs_mkdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) .rmdir = affs_rmdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) .rename = affs_rename2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) .setattr = affs_notify_change,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) affs_readdir(struct file *file, struct dir_context *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct buffer_head *dir_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) struct buffer_head *fh_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) unsigned char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) int namelen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) u32 i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) int hash_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) int chain_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) u32 ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) pr_debug("%s(ino=%lu,f_pos=%llx)\n", __func__, inode->i_ino, ctx->pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (ctx->pos < 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) file->private_data = (void *)0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (!dir_emit_dots(file, ctx))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) affs_lock_dir(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) chain_pos = (ctx->pos - 2) & 0xffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) hash_pos = (ctx->pos - 2) >> 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) if (chain_pos == 0xffff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) affs_warning(sb, "readdir", "More than 65535 entries in chain");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) chain_pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) hash_pos++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) ctx->pos = ((hash_pos << 16) | chain_pos) + 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) dir_bh = affs_bread(sb, inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (!dir_bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) goto out_unlock_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) /* If the directory hasn't changed since the last call to readdir(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * we can jump directly to where we left off.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) ino = (u32)(long)file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (ino && inode_eq_iversion(inode, file->f_version)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) pr_debug("readdir() left off=%d\n", ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) goto inside;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) ino = be32_to_cpu(AFFS_HEAD(dir_bh)->table[hash_pos]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) for (i = 0; ino && i < chain_pos; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) fh_bh = affs_bread(sb, ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (!fh_bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) affs_error(sb, "readdir","Cannot read block %d", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) error = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) goto out_brelse_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) ino = be32_to_cpu(AFFS_TAIL(sb, fh_bh)->hash_chain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) affs_brelse(fh_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) fh_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) goto inside;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) hash_pos++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) for (; hash_pos < AFFS_SB(sb)->s_hashsize; hash_pos++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) ino = be32_to_cpu(AFFS_HEAD(dir_bh)->table[hash_pos]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (!ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) ctx->pos = (hash_pos << 16) + 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) inside:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) fh_bh = affs_bread(sb, ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) if (!fh_bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) affs_error(sb, "readdir",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) "Cannot read block %d", ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) namelen = min(AFFS_TAIL(sb, fh_bh)->name[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) (u8)AFFSNAMEMAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) name = AFFS_TAIL(sb, fh_bh)->name + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) pr_debug("readdir(): dir_emit(\"%.*s\", ino=%u), hash=%d, f_pos=%llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) namelen, name, ino, hash_pos, ctx->pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (!dir_emit(ctx, name, namelen, ino, DT_UNKNOWN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) ctx->pos++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) ino = be32_to_cpu(AFFS_TAIL(sb, fh_bh)->hash_chain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) affs_brelse(fh_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) fh_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) } while (ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) file->f_version = inode_query_iversion(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) file->private_data = (void *)(long)ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) affs_brelse(fh_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) out_brelse_dir:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) affs_brelse(dir_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) out_unlock_dir:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) affs_unlock_dir(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }