^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/inode.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 ISO9660 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) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/cred.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/gfp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include "affs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) struct inode *affs_iget(struct super_block *sb, unsigned long ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct affs_sb_info *sbi = AFFS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct affs_tail *tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) u32 block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) u32 size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) u32 prot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) u16 id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) inode = iget_locked(sb, ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) if (!inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) if (!(inode->i_state & I_NEW))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) pr_debug("affs_iget(%lu)\n", inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) block = inode->i_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) bh = affs_bread(sb, block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if (!bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) affs_warning(sb, "read_inode", "Cannot read block %d", block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) goto bad_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) if (affs_checksum_block(sb, bh) || be32_to_cpu(AFFS_HEAD(bh)->ptype) != T_SHORT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) affs_warning(sb,"read_inode",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) "Checksum or type (ptype=%d) error on inode %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) AFFS_HEAD(bh)->ptype, block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) goto bad_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) tail = AFFS_TAIL(sb, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) prot = be32_to_cpu(tail->protect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) inode->i_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) set_nlink(inode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) inode->i_mode = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) AFFS_I(inode)->i_extcnt = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) AFFS_I(inode)->i_ext_last = ~1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) AFFS_I(inode)->i_protect = prot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) atomic_set(&AFFS_I(inode)->i_opencnt, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) AFFS_I(inode)->i_blkcnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) AFFS_I(inode)->i_lc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) AFFS_I(inode)->i_lc_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) AFFS_I(inode)->i_lc_shift = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) AFFS_I(inode)->i_lc_mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) AFFS_I(inode)->i_ac = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) AFFS_I(inode)->i_ext_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) AFFS_I(inode)->mmu_private = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) AFFS_I(inode)->i_lastalloc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) AFFS_I(inode)->i_pa_cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (affs_test_opt(sbi->s_flags, SF_SETMODE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) inode->i_mode = sbi->s_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) inode->i_mode = affs_prot_to_mode(prot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) id = be16_to_cpu(tail->uid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (id == 0 || affs_test_opt(sbi->s_flags, SF_SETUID))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) inode->i_uid = sbi->s_uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) else if (id == 0xFFFF && affs_test_opt(sbi->s_flags, SF_MUFS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) i_uid_write(inode, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) i_uid_write(inode, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) id = be16_to_cpu(tail->gid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (id == 0 || affs_test_opt(sbi->s_flags, SF_SETGID))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) inode->i_gid = sbi->s_gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) else if (id == 0xFFFF && affs_test_opt(sbi->s_flags, SF_MUFS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) i_gid_write(inode, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) i_gid_write(inode, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) switch (be32_to_cpu(tail->stype)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) case ST_ROOT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) inode->i_uid = sbi->s_uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) inode->i_gid = sbi->s_gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) case ST_USERDIR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (be32_to_cpu(tail->stype) == ST_USERDIR ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) affs_test_opt(sbi->s_flags, SF_SETMODE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (inode->i_mode & S_IRUSR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) inode->i_mode |= S_IXUSR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (inode->i_mode & S_IRGRP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) inode->i_mode |= S_IXGRP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (inode->i_mode & S_IROTH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) inode->i_mode |= S_IXOTH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) inode->i_mode |= S_IFDIR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) inode->i_mode = S_IRUGO | S_IXUGO | S_IWUSR | S_IFDIR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) /* Maybe it should be controlled by mount parameter? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) //inode->i_mode |= S_ISVTX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) inode->i_op = &affs_dir_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) inode->i_fop = &affs_dir_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) case ST_LINKDIR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) #if 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) affs_warning(sb, "read_inode", "inode is LINKDIR");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) goto bad_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) inode->i_mode |= S_IFDIR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) /* ... and leave ->i_op and ->i_fop pointing to empty */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) case ST_LINKFILE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) affs_warning(sb, "read_inode", "inode is LINKFILE");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) goto bad_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) case ST_FILE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) size = be32_to_cpu(tail->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) inode->i_mode |= S_IFREG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) AFFS_I(inode)->mmu_private = inode->i_size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (inode->i_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) AFFS_I(inode)->i_blkcnt = (size - 1) /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) sbi->s_data_blksize + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) AFFS_I(inode)->i_extcnt = (AFFS_I(inode)->i_blkcnt - 1) /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) sbi->s_hashsize + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) if (tail->link_chain)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) set_nlink(inode, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) inode->i_mapping->a_ops = affs_test_opt(sbi->s_flags, SF_OFS) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) &affs_aops_ofs : &affs_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) inode->i_op = &affs_file_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) inode->i_fop = &affs_file_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) case ST_SOFTLINK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) inode->i_size = strlen((char *)AFFS_HEAD(bh)->table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) inode->i_mode |= S_IFLNK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) inode_nohighmem(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) inode->i_op = &affs_symlink_inode_operations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) inode->i_data.a_ops = &affs_symlink_aops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) inode->i_mtime.tv_sec = inode->i_atime.tv_sec = inode->i_ctime.tv_sec
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) = (be32_to_cpu(tail->change.days) * 86400LL +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) be32_to_cpu(tail->change.mins) * 60 +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) be32_to_cpu(tail->change.ticks) / 50 +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) AFFS_EPOCH_DELTA) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) sys_tz.tz_minuteswest * 60;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec = inode->i_atime.tv_nsec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) affs_brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) unlock_new_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) bad_inode:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) affs_brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) iget_failed(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) return ERR_PTR(-EIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) affs_write_inode(struct inode *inode, struct writeback_control *wbc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) struct affs_tail *tail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) uid_t uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) gid_t gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) pr_debug("write_inode(%lu)\n", inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (!inode->i_nlink)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) // possibly free block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) bh = affs_bread(sb, inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (!bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) affs_error(sb,"write_inode","Cannot read block %lu",inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) tail = AFFS_TAIL(sb, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) if (tail->stype == cpu_to_be32(ST_ROOT)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) affs_secs_to_datestamp(inode->i_mtime.tv_sec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) &AFFS_ROOT_TAIL(sb, bh)->root_change);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) tail->protect = cpu_to_be32(AFFS_I(inode)->i_protect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) tail->size = cpu_to_be32(inode->i_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) affs_secs_to_datestamp(inode->i_mtime.tv_sec, &tail->change);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) if (!(inode->i_ino == AFFS_SB(sb)->s_root_block)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) uid = i_uid_read(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) gid = i_gid_read(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) if (affs_test_opt(AFFS_SB(sb)->s_flags, SF_MUFS)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) if (uid == 0 || uid == 0xFFFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) uid = uid ^ ~0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) if (gid == 0 || gid == 0xFFFF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) gid = gid ^ ~0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) if (!affs_test_opt(AFFS_SB(sb)->s_flags, SF_SETUID))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) tail->uid = cpu_to_be16(uid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) if (!affs_test_opt(AFFS_SB(sb)->s_flags, SF_SETGID))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) tail->gid = cpu_to_be16(gid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) affs_fix_checksum(sb, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) mark_buffer_dirty_inode(bh, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) affs_brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) affs_free_prealloc(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) affs_notify_change(struct dentry *dentry, struct iattr *attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) struct inode *inode = d_inode(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) pr_debug("notify_change(%lu,0x%x)\n", inode->i_ino, attr->ia_valid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) error = setattr_prepare(dentry, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) if (((attr->ia_valid & ATTR_UID) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) affs_test_opt(AFFS_SB(inode->i_sb)->s_flags, SF_SETUID)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) ((attr->ia_valid & ATTR_GID) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) affs_test_opt(AFFS_SB(inode->i_sb)->s_flags, SF_SETGID)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) ((attr->ia_valid & ATTR_MODE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) (AFFS_SB(inode->i_sb)->s_flags &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) (AFFS_MOUNT_SF_SETMODE | AFFS_MOUNT_SF_IMMUTABLE)))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) if (!affs_test_opt(AFFS_SB(inode->i_sb)->s_flags, SF_QUIET))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) error = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) if ((attr->ia_valid & ATTR_SIZE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) attr->ia_size != i_size_read(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) error = inode_newsize_ok(inode, attr->ia_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) truncate_setsize(inode, attr->ia_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) affs_truncate(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) setattr_copy(inode, attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) mark_inode_dirty(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) if (attr->ia_valid & ATTR_MODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) affs_mode_to_prot(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) affs_evict_inode(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) unsigned long cache_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) pr_debug("evict_inode(ino=%lu, nlink=%u)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) inode->i_ino, inode->i_nlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) truncate_inode_pages_final(&inode->i_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) if (!inode->i_nlink) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) inode->i_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) affs_truncate(inode);
^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) invalidate_inode_buffers(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) clear_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) affs_free_prealloc(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) cache_page = (unsigned long)AFFS_I(inode)->i_lc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) if (cache_page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) pr_debug("freeing ext cache\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) AFFS_I(inode)->i_lc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) AFFS_I(inode)->i_ac = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) free_page(cache_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) affs_brelse(AFFS_I(inode)->i_ext_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) AFFS_I(inode)->i_ext_last = ~1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) AFFS_I(inode)->i_ext_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) if (!inode->i_nlink)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) affs_free_block(inode->i_sb, inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) struct inode *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) affs_new_inode(struct inode *dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) struct super_block *sb = dir->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) u32 block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) if (!(inode = new_inode(sb)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) goto err_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) if (!(block = affs_alloc_block(dir, dir->i_ino)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) goto err_block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) bh = affs_getzeroblk(sb, block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) if (!bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) goto err_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) mark_buffer_dirty_inode(bh, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) affs_brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) inode->i_uid = current_fsuid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) inode->i_gid = current_fsgid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) inode->i_ino = block;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) set_nlink(inode, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) atomic_set(&AFFS_I(inode)->i_opencnt, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) AFFS_I(inode)->i_blkcnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) AFFS_I(inode)->i_lc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) AFFS_I(inode)->i_lc_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) AFFS_I(inode)->i_lc_shift = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) AFFS_I(inode)->i_lc_mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) AFFS_I(inode)->i_ac = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) AFFS_I(inode)->i_ext_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) AFFS_I(inode)->mmu_private = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) AFFS_I(inode)->i_protect = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) AFFS_I(inode)->i_lastalloc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) AFFS_I(inode)->i_pa_cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) AFFS_I(inode)->i_extcnt = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) AFFS_I(inode)->i_ext_last = ~1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) insert_inode_hash(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) return inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) err_bh:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) affs_free_block(sb, block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) err_block:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) err_inode:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) * Add an entry to a directory. Create the header block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) * and insert it into the hash table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) affs_add_entry(struct inode *dir, struct inode *inode, struct dentry *dentry, s32 type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) struct super_block *sb = dir->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) struct buffer_head *inode_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) u32 block = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) pr_debug("%s(dir=%lu, inode=%lu, \"%pd\", type=%d)\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) dir->i_ino, inode->i_ino, dentry, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) retval = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) bh = affs_bread(sb, inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) if (!bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) affs_lock_link(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) switch (type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) case ST_LINKFILE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) case ST_LINKDIR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) retval = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) block = affs_alloc_block(dir, dir->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) if (!block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) retval = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) inode_bh = bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) bh = affs_getzeroblk(sb, block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) if (!bh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) AFFS_HEAD(bh)->ptype = cpu_to_be32(T_SHORT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) AFFS_HEAD(bh)->key = cpu_to_be32(bh->b_blocknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) affs_copy_name(AFFS_TAIL(sb, bh)->name, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) AFFS_TAIL(sb, bh)->stype = cpu_to_be32(type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) AFFS_TAIL(sb, bh)->parent = cpu_to_be32(dir->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) if (inode_bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) __be32 chain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) chain = AFFS_TAIL(sb, inode_bh)->link_chain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) AFFS_TAIL(sb, bh)->original = cpu_to_be32(inode->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) AFFS_TAIL(sb, bh)->link_chain = chain;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) AFFS_TAIL(sb, inode_bh)->link_chain = cpu_to_be32(block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) affs_adjust_checksum(inode_bh, block - be32_to_cpu(chain));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) mark_buffer_dirty_inode(inode_bh, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) set_nlink(inode, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) ihold(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) affs_fix_checksum(sb, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) mark_buffer_dirty_inode(bh, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) dentry->d_fsdata = (void *)(long)bh->b_blocknr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) affs_lock_dir(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) retval = affs_insert_hash(dir, bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) mark_buffer_dirty_inode(bh, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) affs_unlock_dir(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) affs_unlock_link(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) d_instantiate(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) affs_brelse(inode_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) affs_brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) if (block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) affs_free_block(sb, block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) affs_unlock_link(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) }