^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * linux/fs/adfs/dir.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 1999-2000 Russell King
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Common directory handling for ADFS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "adfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * For future. This should probably be per-directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) static DECLARE_RWSEM(adfs_dir_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) int adfs_dir_copyfrom(void *dst, struct adfs_dir *dir, unsigned int offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct super_block *sb = dir->sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) unsigned int index, remain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) index = offset >> sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) offset &= sb->s_blocksize - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) remain = sb->s_blocksize - offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) if (index + (remain < len) >= dir->nr_buffers)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) if (remain < len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) memcpy(dst, dir->bhs[index]->b_data + offset, remain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) dst += remain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) len -= remain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) index += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) memcpy(dst, dir->bhs[index]->b_data + offset, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) int adfs_dir_copyto(struct adfs_dir *dir, unsigned int offset, const void *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct super_block *sb = dir->sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) unsigned int index, remain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) index = offset >> sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) offset &= sb->s_blocksize - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) remain = sb->s_blocksize - offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) if (index + (remain < len) >= dir->nr_buffers)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) if (remain < len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) memcpy(dir->bhs[index]->b_data + offset, src, remain);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) src += remain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) len -= remain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) index += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) memcpy(dir->bhs[index]->b_data + offset, src, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^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) static void __adfs_dir_cleanup(struct adfs_dir *dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) dir->nr_buffers = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (dir->bhs != dir->bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) kfree(dir->bhs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) dir->bhs = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) dir->sb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) void adfs_dir_relse(struct adfs_dir *dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) for (i = 0; i < dir->nr_buffers; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) brelse(dir->bhs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) __adfs_dir_cleanup(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) static void adfs_dir_forget(struct adfs_dir *dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) for (i = 0; i < dir->nr_buffers; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) bforget(dir->bhs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) __adfs_dir_cleanup(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) int adfs_dir_read_buffers(struct super_block *sb, u32 indaddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) unsigned int size, struct adfs_dir *dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) struct buffer_head **bhs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) unsigned int i, num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) int block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) num = ALIGN(size, sb->s_blocksize) >> sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if (num > ARRAY_SIZE(dir->bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) /* We only allow one extension */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (dir->bhs != dir->bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) bhs = kcalloc(num, sizeof(*bhs), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (!bhs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (dir->nr_buffers)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) memcpy(bhs, dir->bhs, dir->nr_buffers * sizeof(*bhs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) dir->bhs = bhs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) for (i = dir->nr_buffers; i < num; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) block = __adfs_block_map(sb, indaddr, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (!block) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) adfs_error(sb, "dir %06x has a hole at offset %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) indaddr, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) dir->bhs[i] = sb_bread(sb, block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if (!dir->bhs[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) adfs_error(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) "dir %06x failed read at offset %u, mapped block 0x%08x",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) indaddr, i, block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) dir->nr_buffers++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) adfs_dir_relse(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) static int adfs_dir_read(struct super_block *sb, u32 indaddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) unsigned int size, struct adfs_dir *dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) dir->sb = sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) dir->bhs = dir->bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) dir->nr_buffers = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) return ADFS_SB(sb)->s_dir->read(sb, indaddr, size, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) static int adfs_dir_read_inode(struct super_block *sb, struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) struct adfs_dir *dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) ret = adfs_dir_read(sb, ADFS_I(inode)->indaddr, inode->i_size, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) if (ADFS_I(inode)->parent_id != dir->parent_id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) adfs_error(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) "parent directory id changed under me! (%06x but got %06x)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) ADFS_I(inode)->parent_id, dir->parent_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) adfs_dir_relse(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) static void adfs_dir_mark_dirty(struct adfs_dir *dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) /* Mark the buffers dirty */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) for (i = 0; i < dir->nr_buffers; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) mark_buffer_dirty(dir->bhs[i]);
^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) static int adfs_dir_sync(struct adfs_dir *dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) for (i = dir->nr_buffers - 1; i >= 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) struct buffer_head *bh = dir->bhs[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) sync_dirty_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) if (buffer_req(bh) && !buffer_uptodate(bh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) void adfs_object_fixup(struct adfs_dir *dir, struct object_info *obj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) unsigned int dots, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) * RISC OS allows the use of '/' in directory entry names, so we need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) * to fix these up. '/' is typically used for FAT compatibility to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) * represent '.', so do the same conversion here. In any case, '.'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) * will never be in a RISC OS name since it is used as the pathname
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) * separator. Handle the case where we may generate a '.' or '..'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) * name, replacing the first character with '^' (the RISC OS "parent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * directory" character.)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) for (i = dots = 0; i < obj->name_len; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) if (obj->name[i] == '/') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) obj->name[i] = '.';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) dots++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) if (obj->name_len <= 2 && dots == obj->name_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) obj->name[0] = '^';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) * If the object is a file, and the user requested the ,xyz hex
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) * filetype suffix to the name, check the filetype and append.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) if (!(obj->attr & ADFS_NDA_DIRECTORY) && ADFS_SB(dir->sb)->s_ftsuffix) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) u16 filetype = adfs_filetype(obj->loadaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) if (filetype != ADFS_FILETYPE_NONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) obj->name[obj->name_len++] = ',';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) obj->name[obj->name_len++] = hex_asc_lo(filetype >> 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) obj->name[obj->name_len++] = hex_asc_lo(filetype >> 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) obj->name[obj->name_len++] = hex_asc_lo(filetype >> 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) static int adfs_iterate(struct file *file, struct dir_context *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) struct inode *inode = file_inode(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) const struct adfs_dir_ops *ops = ADFS_SB(sb)->s_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) struct adfs_dir dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) down_read(&adfs_dir_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) ret = adfs_dir_read_inode(sb, inode, &dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) if (ctx->pos == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) if (!dir_emit_dot(file, ctx))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) goto unlock_relse;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) ctx->pos = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) if (ctx->pos == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) if (!dir_emit(ctx, "..", 2, dir.parent_id, DT_DIR))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) goto unlock_relse;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) ctx->pos = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) ret = ops->iterate(&dir, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) unlock_relse:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) up_read(&adfs_dir_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) adfs_dir_relse(&dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) up_read(&adfs_dir_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) return ret;
^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) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) adfs_dir_update(struct super_block *sb, struct object_info *obj, int wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) const struct adfs_dir_ops *ops = ADFS_SB(sb)->s_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) struct adfs_dir dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) if (!IS_ENABLED(CONFIG_ADFS_FS_RW))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) if (!ops->update)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) down_write(&adfs_dir_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) ret = adfs_dir_read(sb, obj->parent_id, 0, &dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) ret = ops->update(&dir, obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) goto forget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) ret = ops->commit(&dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) goto forget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) up_write(&adfs_dir_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) adfs_dir_mark_dirty(&dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) if (wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) ret = adfs_dir_sync(&dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) adfs_dir_relse(&dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) * If the updated failed because the entry wasn't found, we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) * just release the buffers. If it was any other error, forget
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) * the dirtied buffers so they aren't written back to the media.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) forget:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) if (ret == -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) adfs_dir_relse(&dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) adfs_dir_forget(&dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) up_write(&adfs_dir_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) static unsigned char adfs_tolower(unsigned char c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) if (c >= 'A' && c <= 'Z')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) c += 'a' - 'A';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) return c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) static int __adfs_compare(const unsigned char *qstr, u32 qlen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) const char *str, u32 len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) u32 i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) if (qlen != len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) for (i = 0; i < qlen; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) if (adfs_tolower(qstr[i]) != adfs_tolower(str[i]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) return 0;
^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) static int adfs_dir_lookup_byname(struct inode *inode, const struct qstr *qstr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) struct object_info *obj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) const struct adfs_dir_ops *ops = ADFS_SB(sb)->s_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) const unsigned char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) struct adfs_dir dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) u32 name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) down_read(&adfs_dir_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) ret = adfs_dir_read_inode(sb, inode, &dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) ret = ops->setpos(&dir, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) goto unlock_relse;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) name = qstr->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) name_len = qstr->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) while (ops->getnext(&dir, obj) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) if (!__adfs_compare(name, name_len, obj->name, obj->name_len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) obj->parent_id = ADFS_I(inode)->indaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) unlock_relse:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) up_read(&adfs_dir_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) adfs_dir_relse(&dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) up_read(&adfs_dir_rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) const struct file_operations adfs_dir_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) .read = generic_read_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) .llseek = generic_file_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) .iterate_shared = adfs_iterate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) .fsync = generic_file_fsync,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) adfs_hash(const struct dentry *parent, struct qstr *qstr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) const unsigned char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) unsigned long hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) u32 len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) if (qstr->len > ADFS_SB(parent->d_sb)->s_namelen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) return -ENAMETOOLONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) len = qstr->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) name = qstr->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) hash = init_name_hash(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) while (len--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) hash = partial_name_hash(adfs_tolower(*name++), hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) qstr->hash = end_name_hash(hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) * Compare two names, taking note of the name length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) * requirements of the underlying filesystem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) static int adfs_compare(const struct dentry *dentry, unsigned int len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) const char *str, const struct qstr *qstr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) return __adfs_compare(qstr->name, qstr->len, str, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) const struct dentry_operations adfs_dentry_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) .d_hash = adfs_hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) .d_compare = adfs_compare,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) static struct dentry *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) adfs_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) struct inode *inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) struct object_info obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) error = adfs_dir_lookup_byname(dir, &dentry->d_name, &obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) if (error == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) * This only returns NULL if get_empty_inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) * fails.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) inode = adfs_iget(dir->i_sb, &obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) if (!inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) inode = ERR_PTR(-EACCES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) } else if (error != -ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) inode = ERR_PTR(error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) return d_splice_alias(inode, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) * directories can handle most operations...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) const struct inode_operations adfs_dir_inode_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) .lookup = adfs_lookup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) .setattr = adfs_notify_change,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) };