^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/iversion.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/namei.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/buffer_head.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/nls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "exfat_raw.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include "exfat_fs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) static inline unsigned long exfat_d_version(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) return (unsigned long) dentry->d_fsdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) static inline void exfat_d_version_set(struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) unsigned long version)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) dentry->d_fsdata = (void *) version;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * If new entry was created in the parent, it could create the 8.3 alias (the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * shortname of logname). So, the parent may have the negative-dentry which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * matches the created 8.3 alias.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * If it happened, the negative dentry isn't actually negative anymore. So,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * drop it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static int exfat_d_revalidate(struct dentry *dentry, unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (flags & LOOKUP_RCU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return -ECHILD;
^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) * This is not negative dentry. Always valid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * Note, rename() to existing directory entry will have ->d_inode, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * will use existing name which isn't specified name by user.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * We may be able to drop this positive dentry here. But dropping
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * positive dentry isn't good idea. So it's unsupported like
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * rename("filename", "FILENAME") for now.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) if (d_really_is_positive(dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * Drop the negative dentry, in order to make sure to use the case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * sensitive name which is specified by user if this is for creation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (flags & (LOOKUP_CREATE | LOOKUP_RENAME_TARGET))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) spin_lock(&dentry->d_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) ret = inode_eq_iversion(d_inode(dentry->d_parent),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) exfat_d_version(dentry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) spin_unlock(&dentry->d_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) /* returns the length of a struct qstr, ignoring trailing dots */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static unsigned int exfat_striptail_len(unsigned int len, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) while (len && name[len - 1] == '.')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) len--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^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) * Compute the hash for the exfat name corresponding to the dentry. If the name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * is invalid, we leave the hash code unchanged so that the existing dentry can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * be used. The exfat fs routines will return ENOENT or EINVAL as appropriate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) static int exfat_d_hash(const struct dentry *dentry, struct qstr *qstr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) struct super_block *sb = dentry->d_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) struct nls_table *t = EXFAT_SB(sb)->nls_io;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) const unsigned char *name = qstr->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) unsigned int len = exfat_striptail_len(qstr->len, qstr->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) unsigned long hash = init_name_hash(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) int i, charlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) wchar_t c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) for (i = 0; i < len; i += charlen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) charlen = t->char2uni(&name[i], len - i, &c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (charlen < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return charlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) hash = partial_name_hash(exfat_toupper(sb, c), hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) qstr->hash = end_name_hash(hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static int exfat_d_cmp(const struct dentry *dentry, unsigned int len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) const char *str, const struct qstr *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) struct super_block *sb = dentry->d_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) struct nls_table *t = EXFAT_SB(sb)->nls_io;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) unsigned int alen = exfat_striptail_len(name->len, name->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) unsigned int blen = exfat_striptail_len(len, str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) wchar_t c1, c2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) int charlen, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (alen != blen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) for (i = 0; i < len; i += charlen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) charlen = t->char2uni(&name->name[i], alen - i, &c1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (charlen < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (charlen != t->char2uni(&str[i], blen - i, &c2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (exfat_toupper(sb, c1) != exfat_toupper(sb, c2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) const struct dentry_operations exfat_dentry_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) .d_revalidate = exfat_d_revalidate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) .d_hash = exfat_d_hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) .d_compare = exfat_d_cmp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) static int exfat_utf8_d_hash(const struct dentry *dentry, struct qstr *qstr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) struct super_block *sb = dentry->d_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) const unsigned char *name = qstr->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) unsigned int len = exfat_striptail_len(qstr->len, qstr->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) unsigned long hash = init_name_hash(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) int i, charlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) unicode_t u;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) for (i = 0; i < len; i += charlen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) charlen = utf8_to_utf32(&name[i], len - i, &u);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) if (charlen < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) return charlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) * exfat_toupper() works only for code points up to the U+FFFF.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) hash = partial_name_hash(u <= 0xFFFF ? exfat_toupper(sb, u) : u,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) hash);
^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) qstr->hash = end_name_hash(hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static int exfat_utf8_d_cmp(const struct dentry *dentry, unsigned int len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) const char *str, const struct qstr *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) struct super_block *sb = dentry->d_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) unsigned int alen = exfat_striptail_len(name->len, name->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) unsigned int blen = exfat_striptail_len(len, str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) unicode_t u_a, u_b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) int charlen, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (alen != blen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) for (i = 0; i < alen; i += charlen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) charlen = utf8_to_utf32(&name->name[i], alen - i, &u_a);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (charlen < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) if (charlen != utf8_to_utf32(&str[i], blen - i, &u_b))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) if (u_a <= 0xFFFF && u_b <= 0xFFFF) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (exfat_toupper(sb, u_a) != exfat_toupper(sb, u_b))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) if (u_a != u_b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) const struct dentry_operations exfat_utf8_dentry_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) .d_revalidate = exfat_d_revalidate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) .d_hash = exfat_utf8_d_hash,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) .d_compare = exfat_utf8_d_cmp,
^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) /* used only in search empty_slot() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) #define CNT_UNUSED_NOHIT (-1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) #define CNT_UNUSED_HIT (-2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) /* search EMPTY CONTINUOUS "num_entries" entries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) static int exfat_search_empty_slot(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) struct exfat_hint_femp *hint_femp, struct exfat_chain *p_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) int num_entries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) int i, dentry, num_empty = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) int dentries_per_clu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) unsigned int type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) struct exfat_chain clu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) struct exfat_dentry *ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) struct exfat_sb_info *sbi = EXFAT_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) dentries_per_clu = sbi->dentries_per_clu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) if (hint_femp->eidx != EXFAT_HINT_NONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) dentry = hint_femp->eidx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) if (num_entries <= hint_femp->count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) hint_femp->eidx = EXFAT_HINT_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) return dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) exfat_chain_dup(&clu, &hint_femp->cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) exfat_chain_dup(&clu, p_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) dentry = 0;
^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) while (clu.dir != EXFAT_EOF_CLUSTER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) i = dentry & (dentries_per_clu - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) for (; i < dentries_per_clu; i++, dentry++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) ep = exfat_get_dentry(sb, &clu, i, &bh, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) if (!ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) type = exfat_get_entry_type(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (type == TYPE_UNUSED || type == TYPE_DELETED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) num_empty++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) if (hint_femp->eidx == EXFAT_HINT_NONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) hint_femp->eidx = dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) hint_femp->count = CNT_UNUSED_NOHIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) exfat_chain_set(&hint_femp->cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) clu.dir, clu.size, clu.flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) if (type == TYPE_UNUSED &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) hint_femp->count != CNT_UNUSED_HIT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) hint_femp->count = CNT_UNUSED_HIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) if (hint_femp->eidx != EXFAT_HINT_NONE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) hint_femp->count == CNT_UNUSED_HIT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) /* unused empty group means
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) * an empty group which includes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) * unused dentry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) exfat_fs_error(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) "found bogus dentry(%d) beyond unused empty group(%d) (start_clu : %u, cur_clu : %u)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) dentry, hint_femp->eidx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) p_dir->dir, clu.dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) num_empty = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) hint_femp->eidx = EXFAT_HINT_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) if (num_empty >= num_entries) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) /* found and invalidate hint_femp */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) hint_femp->eidx = EXFAT_HINT_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) return (dentry - (num_entries - 1));
^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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) if (clu.flags == ALLOC_NO_FAT_CHAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) if (--clu.size > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) clu.dir++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) clu.dir = EXFAT_EOF_CLUSTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) if (exfat_get_next_cluster(sb, &clu.dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) static int exfat_check_max_dentries(struct inode *inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) if (EXFAT_B_TO_DEN(i_size_read(inode)) >= MAX_EXFAT_DENTRIES) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) * exFAT spec allows a dir to grow up to 8388608(256MB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) * dentries
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) /* find empty directory entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) * if there isn't any empty slot, expand cluster chain.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) static int exfat_find_empty_entry(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) struct exfat_chain *p_dir, int num_entries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) int dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) unsigned int ret, last_clu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) sector_t sector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) loff_t size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) struct exfat_chain clu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) struct exfat_dentry *ep = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) struct exfat_sb_info *sbi = EXFAT_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) struct exfat_inode_info *ei = EXFAT_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) struct exfat_hint_femp hint_femp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) hint_femp.eidx = EXFAT_HINT_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) if (ei->hint_femp.eidx != EXFAT_HINT_NONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) hint_femp = ei->hint_femp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) ei->hint_femp.eidx = EXFAT_HINT_NONE;
^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) while ((dentry = exfat_search_empty_slot(sb, &hint_femp, p_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) num_entries)) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) if (dentry == -EIO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) if (exfat_check_max_dentries(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) /* we trust p_dir->size regardless of FAT type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) if (exfat_find_last_cluster(sb, p_dir, &last_clu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) * Allocate new cluster to this directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) exfat_chain_set(&clu, last_clu + 1, 0, p_dir->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) /* allocate a cluster */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) ret = exfat_alloc_cluster(inode, 1, &clu, IS_DIRSYNC(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) if (exfat_zeroed_cluster(inode, clu.dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) /* append to the FAT chain */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) if (clu.flags != p_dir->flags) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) /* no-fat-chain bit is disabled,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) * so fat-chain should be synced with alloc-bitmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) exfat_chain_cont_cluster(sb, p_dir->dir, p_dir->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) p_dir->flags = ALLOC_FAT_CHAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) hint_femp.cur.flags = ALLOC_FAT_CHAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) if (clu.flags == ALLOC_FAT_CHAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) if (exfat_ent_set(sb, last_clu, clu.dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) if (hint_femp.eidx == EXFAT_HINT_NONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) /* the special case that new dentry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) * should be allocated from the start of new cluster
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) hint_femp.eidx = EXFAT_B_TO_DEN_IDX(p_dir->size, sbi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) hint_femp.count = sbi->dentries_per_clu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) exfat_chain_set(&hint_femp.cur, clu.dir, 0, clu.flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) hint_femp.cur.size++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) p_dir->size++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) size = EXFAT_CLU_TO_B(p_dir->size, sbi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) /* update the directory entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) if (p_dir->dir != sbi->root_dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) ep = exfat_get_dentry(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) &(ei->dir), ei->entry + 1, &bh, §or);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) if (!ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) ep->dentry.stream.valid_size = cpu_to_le64(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) ep->dentry.stream.size = ep->dentry.stream.valid_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) ep->dentry.stream.flags = p_dir->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) exfat_update_bh(bh, IS_DIRSYNC(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) if (exfat_update_dir_chksum(inode, &(ei->dir),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) ei->entry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) /* directory inode should be updated in here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) i_size_write(inode, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) ei->i_size_ondisk += sbi->cluster_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) ei->i_size_aligned += sbi->cluster_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) ei->flags = p_dir->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) inode->i_blocks += 1 << sbi->sect_per_clus_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) return dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) * Name Resolution Functions :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) * Zero if it was successful; otherwise nonzero.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) static int __exfat_resolve_path(struct inode *inode, const unsigned char *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) int lookup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) int namelen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) int lossy = NLS_NAME_NO_LOSSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) struct exfat_sb_info *sbi = EXFAT_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) struct exfat_inode_info *ei = EXFAT_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) /* strip all trailing periods */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) namelen = exfat_striptail_len(strlen(path), path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) if (!namelen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) if (strlen(path) > (MAX_NAME_LENGTH * MAX_CHARSET_SIZE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) return -ENAMETOOLONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) * strip all leading spaces :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) * "MS windows 7" supports leading spaces.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) * So we should skip this preprocessing for compatibility.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) /* file name conversion :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) * If lookup case, we allow bad-name for compatibility.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) namelen = exfat_nls_to_utf16(sb, path, namelen, p_uniname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) &lossy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) if (namelen < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) return namelen; /* return error value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) if ((lossy && !lookup) || !namelen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) exfat_chain_set(p_dir, ei->start_clu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) EXFAT_B_TO_CLU(i_size_read(inode), sbi), ei->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) return 0;
^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) static inline int exfat_resolve_path(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) const unsigned char *path, struct exfat_chain *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) struct exfat_uni_name *uni)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) return __exfat_resolve_path(inode, path, dir, uni, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) static inline int exfat_resolve_path_for_lookup(struct inode *inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) const unsigned char *path, struct exfat_chain *dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) struct exfat_uni_name *uni)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) return __exfat_resolve_path(inode, path, dir, uni, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) static inline loff_t exfat_make_i_pos(struct exfat_dir_entry *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) return ((loff_t) info->dir.dir << 32) | (info->entry & 0xffffffff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) static int exfat_add_entry(struct inode *inode, const char *path,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) struct exfat_chain *p_dir, unsigned int type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) struct exfat_dir_entry *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) int ret, dentry, num_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) struct exfat_sb_info *sbi = EXFAT_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) struct exfat_uni_name uniname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) struct exfat_chain clu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) int clu_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) unsigned int start_clu = EXFAT_FREE_CLUSTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) ret = exfat_resolve_path(inode, path, p_dir, &uniname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) num_entries = exfat_calc_num_entries(&uniname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) if (num_entries < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) ret = num_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) /* exfat_find_empty_entry must be called before alloc_cluster() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) dentry = exfat_find_empty_entry(inode, p_dir, num_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) if (dentry < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) ret = dentry; /* -EIO or -ENOSPC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) if (type == TYPE_DIR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) ret = exfat_alloc_new_dir(inode, &clu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) start_clu = clu.dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) clu_size = sbi->cluster_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) /* update the directory entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) /* fill the dos name directory entry information of the created file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) * the first cluster is not determined yet. (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) ret = exfat_init_dir_entry(inode, p_dir, dentry, type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) start_clu, clu_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) ret = exfat_init_ext_entry(inode, p_dir, dentry, num_entries, &uniname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) info->dir = *p_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) info->entry = dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) info->flags = ALLOC_NO_FAT_CHAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) info->type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) if (type == TYPE_FILE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) info->attr = ATTR_ARCHIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) info->start_clu = EXFAT_EOF_CLUSTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) info->size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) info->num_subdirs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) info->attr = ATTR_SUBDIR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) info->start_clu = start_clu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) info->size = clu_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) info->num_subdirs = EXFAT_MIN_SUBDIR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) memset(&info->crtime, 0, sizeof(info->crtime));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) memset(&info->mtime, 0, sizeof(info->mtime));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) memset(&info->atime, 0, sizeof(info->atime));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) static int exfat_create(struct inode *dir, struct dentry *dentry, umode_t mode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) bool excl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) struct super_block *sb = dir->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) struct exfat_chain cdir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) struct exfat_dir_entry info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) loff_t i_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) mutex_lock(&EXFAT_SB(sb)->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) exfat_set_volume_dirty(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) err = exfat_add_entry(dir, dentry->d_name.name, &cdir, TYPE_FILE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) &info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) exfat_clear_volume_dirty(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) inode_inc_iversion(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) dir->i_ctime = dir->i_mtime = current_time(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) if (IS_DIRSYNC(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) exfat_sync_inode(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) mark_inode_dirty(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) i_pos = exfat_make_i_pos(&info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) inode = exfat_build_inode(sb, &info, i_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) err = PTR_ERR_OR_ZERO(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) inode_inc_iversion(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) inode->i_mtime = inode->i_atime = inode->i_ctime =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) EXFAT_I(inode)->i_crtime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) exfat_truncate_atime(&inode->i_atime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) /* timestamp is already written, so mark_inode_dirty() is unneeded. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) d_instantiate(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) mutex_unlock(&EXFAT_SB(sb)->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) /* lookup a file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) static int exfat_find(struct inode *dir, struct qstr *qname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) struct exfat_dir_entry *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) int ret, dentry, num_entries, count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) struct exfat_chain cdir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) struct exfat_uni_name uni_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) struct super_block *sb = dir->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) struct exfat_sb_info *sbi = EXFAT_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) struct exfat_inode_info *ei = EXFAT_I(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) struct exfat_dentry *ep, *ep2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) struct exfat_entry_set_cache *es;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) if (qname->len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) /* check the validity of directory name in the given pathname */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) ret = exfat_resolve_path_for_lookup(dir, qname->name, &cdir, &uni_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) num_entries = exfat_calc_num_entries(&uni_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) if (num_entries < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) return num_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) /* check the validation of hint_stat and initialize it if required */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) if (ei->version != (inode_peek_iversion_raw(dir) & 0xffffffff)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) ei->hint_stat.clu = cdir.dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) ei->hint_stat.eidx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) ei->version = (inode_peek_iversion_raw(dir) & 0xffffffff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) ei->hint_femp.eidx = EXFAT_HINT_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) /* search the file name for directories */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) dentry = exfat_find_dir_entry(sb, ei, &cdir, &uni_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) num_entries, TYPE_ALL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) if (dentry < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) return dentry; /* -error value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) info->dir = cdir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) info->entry = dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) info->num_subdirs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) es = exfat_get_dentry_set(sb, &cdir, dentry, ES_2_ENTRIES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) if (!es)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) ep = exfat_get_dentry_cached(es, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) ep2 = exfat_get_dentry_cached(es, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) info->type = exfat_get_entry_type(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) info->attr = le16_to_cpu(ep->dentry.file.attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) info->size = le64_to_cpu(ep2->dentry.stream.valid_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) if ((info->type == TYPE_FILE) && (info->size == 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) info->flags = ALLOC_NO_FAT_CHAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) info->start_clu = EXFAT_EOF_CLUSTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) info->flags = ep2->dentry.stream.flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) info->start_clu =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) le32_to_cpu(ep2->dentry.stream.start_clu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) exfat_get_entry_time(sbi, &info->crtime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) ep->dentry.file.create_tz,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) ep->dentry.file.create_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) ep->dentry.file.create_date,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) ep->dentry.file.create_time_cs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) exfat_get_entry_time(sbi, &info->mtime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) ep->dentry.file.modify_tz,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) ep->dentry.file.modify_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) ep->dentry.file.modify_date,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) ep->dentry.file.modify_time_cs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) exfat_get_entry_time(sbi, &info->atime,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) ep->dentry.file.access_tz,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) ep->dentry.file.access_time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) ep->dentry.file.access_date,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) exfat_free_dentry_set(es, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) if (ei->start_clu == EXFAT_FREE_CLUSTER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) exfat_fs_error(sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) "non-zero size file starts with zero cluster (size : %llu, p_dir : %u, entry : 0x%08x)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) i_size_read(dir), ei->dir.dir, ei->entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) if (info->type == TYPE_DIR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) exfat_chain_set(&cdir, info->start_clu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) EXFAT_B_TO_CLU(info->size, sbi), info->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) count = exfat_count_dir_entries(sb, &cdir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) if (count < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) info->num_subdirs = count + EXFAT_MIN_SUBDIR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) static int exfat_d_anon_disconn(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) return IS_ROOT(dentry) && (dentry->d_flags & DCACHE_DISCONNECTED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) static struct dentry *exfat_lookup(struct inode *dir, struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) struct super_block *sb = dir->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) struct dentry *alias;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) struct exfat_dir_entry info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) loff_t i_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) mode_t i_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) mutex_lock(&EXFAT_SB(sb)->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) err = exfat_find(dir, &dentry->d_name, &info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) if (err == -ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) i_pos = exfat_make_i_pos(&info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) inode = exfat_build_inode(sb, &info, i_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) err = PTR_ERR_OR_ZERO(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) i_mode = inode->i_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) alias = d_find_alias(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) * Checking "alias->d_parent == dentry->d_parent" to make sure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) * FS is not corrupted (especially double linked dir).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) if (alias && alias->d_parent == dentry->d_parent &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) !exfat_d_anon_disconn(alias)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) * Unhashed alias is able to exist because of revalidate()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) * called by lookup_fast. You can easily make this status
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) * by calling create and lookup concurrently
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) * In such case, we reuse an alias instead of new dentry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) if (d_unhashed(alias)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) WARN_ON(alias->d_name.hash_len !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) dentry->d_name.hash_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) exfat_info(sb, "rehashed a dentry(%p) in read lookup",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) alias);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) d_drop(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) d_rehash(alias);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) } else if (!S_ISDIR(i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) * This inode has non anonymous-DCACHE_DISCONNECTED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) * dentry. This means, the user did ->lookup() by an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) * another name (longname vs 8.3 alias of it) in past.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) * Switch to new one for reason of locality if possible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) d_move(alias, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) mutex_unlock(&EXFAT_SB(sb)->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) return alias;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) dput(alias);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) mutex_unlock(&EXFAT_SB(sb)->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) if (!inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) exfat_d_version_set(dentry, inode_query_iversion(dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) return d_splice_alias(inode, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) mutex_unlock(&EXFAT_SB(sb)->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) /* remove an entry, BUT don't truncate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) static int exfat_unlink(struct inode *dir, struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) struct exfat_chain cdir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) struct exfat_dentry *ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) struct super_block *sb = dir->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) struct inode *inode = dentry->d_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) struct exfat_inode_info *ei = EXFAT_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) sector_t sector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) int num_entries, entry, err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) mutex_lock(&EXFAT_SB(sb)->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) exfat_chain_dup(&cdir, &ei->dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) entry = ei->entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) if (ei->dir.dir == DIR_DELETED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) exfat_err(sb, "abnormal access to deleted dentry");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) ep = exfat_get_dentry(sb, &cdir, entry, &bh, §or);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) if (!ep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) num_entries = exfat_count_ext_entries(sb, &cdir, entry, ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) if (num_entries < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) num_entries++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) exfat_set_volume_dirty(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) /* update the directory entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) if (exfat_remove_entries(dir, &cdir, entry, 0, num_entries)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) /* This doesn't modify ei */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) ei->dir.dir = DIR_DELETED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) exfat_clear_volume_dirty(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) inode_inc_iversion(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) dir->i_mtime = dir->i_atime = current_time(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) exfat_truncate_atime(&dir->i_atime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) if (IS_DIRSYNC(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) exfat_sync_inode(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) mark_inode_dirty(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) clear_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) inode->i_mtime = inode->i_atime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) exfat_truncate_atime(&inode->i_atime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) exfat_unhash_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) exfat_d_version_set(dentry, inode_query_iversion(dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) mutex_unlock(&EXFAT_SB(sb)->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) static int exfat_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) struct super_block *sb = dir->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) struct exfat_dir_entry info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) struct exfat_chain cdir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) loff_t i_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) mutex_lock(&EXFAT_SB(sb)->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) exfat_set_volume_dirty(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) err = exfat_add_entry(dir, dentry->d_name.name, &cdir, TYPE_DIR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) &info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) exfat_clear_volume_dirty(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) inode_inc_iversion(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) dir->i_ctime = dir->i_mtime = current_time(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) if (IS_DIRSYNC(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) exfat_sync_inode(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) mark_inode_dirty(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) inc_nlink(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) i_pos = exfat_make_i_pos(&info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) inode = exfat_build_inode(sb, &info, i_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) err = PTR_ERR_OR_ZERO(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) inode_inc_iversion(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) inode->i_mtime = inode->i_atime = inode->i_ctime =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) EXFAT_I(inode)->i_crtime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) exfat_truncate_atime(&inode->i_atime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) /* timestamp is already written, so mark_inode_dirty() is unneeded. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) d_instantiate(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) mutex_unlock(&EXFAT_SB(sb)->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) static int exfat_check_dir_empty(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) struct exfat_chain *p_dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) int i, dentries_per_clu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) unsigned int type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) struct exfat_chain clu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) struct exfat_dentry *ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) struct exfat_sb_info *sbi = EXFAT_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) dentries_per_clu = sbi->dentries_per_clu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) exfat_chain_dup(&clu, p_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) while (clu.dir != EXFAT_EOF_CLUSTER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) for (i = 0; i < dentries_per_clu; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) ep = exfat_get_dentry(sb, &clu, i, &bh, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) if (!ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) type = exfat_get_entry_type(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) if (type == TYPE_UNUSED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) if (type != TYPE_FILE && type != TYPE_DIR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) return -ENOTEMPTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) if (clu.flags == ALLOC_NO_FAT_CHAIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) if (--clu.size > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) clu.dir++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) clu.dir = EXFAT_EOF_CLUSTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) if (exfat_get_next_cluster(sb, &(clu.dir)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) static int exfat_rmdir(struct inode *dir, struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) struct inode *inode = dentry->d_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) struct exfat_dentry *ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) struct exfat_chain cdir, clu_to_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) struct exfat_sb_info *sbi = EXFAT_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) struct exfat_inode_info *ei = EXFAT_I(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) sector_t sector;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) int num_entries, entry, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) mutex_lock(&EXFAT_SB(inode->i_sb)->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) exfat_chain_dup(&cdir, &ei->dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) entry = ei->entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) if (ei->dir.dir == DIR_DELETED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) exfat_err(sb, "abnormal access to deleted dentry");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) exfat_chain_set(&clu_to_free, ei->start_clu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) EXFAT_B_TO_CLU_ROUND_UP(i_size_read(inode), sbi), ei->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) err = exfat_check_dir_empty(sb, &clu_to_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) if (err == -EIO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) exfat_err(sb, "failed to exfat_check_dir_empty : err(%d)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) ep = exfat_get_dentry(sb, &cdir, entry, &bh, §or);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) if (!ep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) num_entries = exfat_count_ext_entries(sb, &cdir, entry, ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) if (num_entries < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) num_entries++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) exfat_set_volume_dirty(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) err = exfat_remove_entries(dir, &cdir, entry, 0, num_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) exfat_err(sb, "failed to exfat_remove_entries : err(%d)", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) ei->dir.dir = DIR_DELETED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) exfat_clear_volume_dirty(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) inode_inc_iversion(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) dir->i_mtime = dir->i_atime = current_time(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) exfat_truncate_atime(&dir->i_atime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) if (IS_DIRSYNC(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) exfat_sync_inode(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) mark_inode_dirty(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) drop_nlink(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) clear_nlink(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) inode->i_mtime = inode->i_atime = current_time(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) exfat_truncate_atime(&inode->i_atime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) exfat_unhash_inode(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) exfat_d_version_set(dentry, inode_query_iversion(dir));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) mutex_unlock(&EXFAT_SB(inode->i_sb)->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) static int exfat_rename_file(struct inode *inode, struct exfat_chain *p_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) int oldentry, struct exfat_uni_name *p_uniname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) struct exfat_inode_info *ei)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) int ret, num_old_entries, num_new_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) sector_t sector_old, sector_new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) struct exfat_dentry *epold, *epnew;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) struct buffer_head *new_bh, *old_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) int sync = IS_DIRSYNC(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) epold = exfat_get_dentry(sb, p_dir, oldentry, &old_bh, §or_old);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) if (!epold)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) num_old_entries = exfat_count_ext_entries(sb, p_dir, oldentry, epold);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) if (num_old_entries < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) num_old_entries++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) num_new_entries = exfat_calc_num_entries(p_uniname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) if (num_new_entries < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) return num_new_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) if (num_old_entries < num_new_entries) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) int newentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) newentry =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) exfat_find_empty_entry(inode, p_dir, num_new_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) if (newentry < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) return newentry; /* -EIO or -ENOSPC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) epnew = exfat_get_dentry(sb, p_dir, newentry, &new_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) §or_new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) if (!epnew)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) *epnew = *epold;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) if (exfat_get_entry_type(epnew) == TYPE_FILE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) epnew->dentry.file.attr |= cpu_to_le16(ATTR_ARCHIVE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) ei->attr |= ATTR_ARCHIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) exfat_update_bh(new_bh, sync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) brelse(old_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) brelse(new_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) epold = exfat_get_dentry(sb, p_dir, oldentry + 1, &old_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) §or_old);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) if (!epold)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) epnew = exfat_get_dentry(sb, p_dir, newentry + 1, &new_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) §or_new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) if (!epnew) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) brelse(old_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) *epnew = *epold;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) exfat_update_bh(new_bh, sync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) brelse(old_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) brelse(new_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) ret = exfat_init_ext_entry(inode, p_dir, newentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) num_new_entries, p_uniname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) exfat_remove_entries(inode, p_dir, oldentry, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) num_old_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) ei->entry = newentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) if (exfat_get_entry_type(epold) == TYPE_FILE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) epold->dentry.file.attr |= cpu_to_le16(ATTR_ARCHIVE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) ei->attr |= ATTR_ARCHIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) exfat_update_bh(old_bh, sync);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) brelse(old_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) ret = exfat_init_ext_entry(inode, p_dir, oldentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) num_new_entries, p_uniname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) exfat_remove_entries(inode, p_dir, oldentry, num_new_entries,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) num_old_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) static int exfat_move_file(struct inode *inode, struct exfat_chain *p_olddir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) int oldentry, struct exfat_chain *p_newdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) struct exfat_uni_name *p_uniname, struct exfat_inode_info *ei)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) int ret, newentry, num_new_entries, num_old_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) sector_t sector_mov, sector_new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) struct exfat_dentry *epmov, *epnew;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) struct buffer_head *mov_bh, *new_bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) epmov = exfat_get_dentry(sb, p_olddir, oldentry, &mov_bh, §or_mov);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) if (!epmov)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) num_old_entries = exfat_count_ext_entries(sb, p_olddir, oldentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) epmov);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) if (num_old_entries < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) num_old_entries++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) num_new_entries = exfat_calc_num_entries(p_uniname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) if (num_new_entries < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) return num_new_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) newentry = exfat_find_empty_entry(inode, p_newdir, num_new_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) if (newentry < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) return newentry; /* -EIO or -ENOSPC */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) epnew = exfat_get_dentry(sb, p_newdir, newentry, &new_bh, §or_new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) if (!epnew)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) *epnew = *epmov;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) if (exfat_get_entry_type(epnew) == TYPE_FILE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) epnew->dentry.file.attr |= cpu_to_le16(ATTR_ARCHIVE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) ei->attr |= ATTR_ARCHIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) exfat_update_bh(new_bh, IS_DIRSYNC(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) brelse(mov_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) brelse(new_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) epmov = exfat_get_dentry(sb, p_olddir, oldentry + 1, &mov_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) §or_mov);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) if (!epmov)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) epnew = exfat_get_dentry(sb, p_newdir, newentry + 1, &new_bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) §or_new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) if (!epnew) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) brelse(mov_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) *epnew = *epmov;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) exfat_update_bh(new_bh, IS_DIRSYNC(inode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) brelse(mov_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) brelse(new_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) ret = exfat_init_ext_entry(inode, p_newdir, newentry, num_new_entries,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) p_uniname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) exfat_remove_entries(inode, p_olddir, oldentry, 0, num_old_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) exfat_chain_set(&ei->dir, p_newdir->dir, p_newdir->size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) p_newdir->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) ei->entry = newentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) static void exfat_update_parent_info(struct exfat_inode_info *ei,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) struct inode *parent_inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) struct exfat_sb_info *sbi = EXFAT_SB(parent_inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) struct exfat_inode_info *parent_ei = EXFAT_I(parent_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) loff_t parent_isize = i_size_read(parent_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) * the problem that struct exfat_inode_info caches wrong parent info.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) * because of flag-mismatch of ei->dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) * there is abnormal traversing cluster chain.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) if (unlikely(parent_ei->flags != ei->dir.flags ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) parent_isize != EXFAT_CLU_TO_B(ei->dir.size, sbi) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) parent_ei->start_clu != ei->dir.dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) exfat_chain_set(&ei->dir, parent_ei->start_clu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) EXFAT_B_TO_CLU_ROUND_UP(parent_isize, sbi),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) parent_ei->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) /* rename or move a old file into a new file */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) static int __exfat_rename(struct inode *old_parent_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) struct exfat_inode_info *ei, struct inode *new_parent_inode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) struct dentry *new_dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) int dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) struct exfat_chain olddir, newdir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) struct exfat_chain *p_dir = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) struct exfat_uni_name uni_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) struct exfat_dentry *ep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) struct super_block *sb = old_parent_inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) struct exfat_sb_info *sbi = EXFAT_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) const unsigned char *new_path = new_dentry->d_name.name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) struct inode *new_inode = new_dentry->d_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) int num_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) struct exfat_inode_info *new_ei = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) unsigned int new_entry_type = TYPE_UNUSED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) int new_entry = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) struct buffer_head *old_bh, *new_bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) /* check the validity of pointer parameters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) if (new_path == NULL || strlen(new_path) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) if (ei->dir.dir == DIR_DELETED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) exfat_err(sb, "abnormal access to deleted source dentry");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) exfat_update_parent_info(ei, old_parent_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) exfat_chain_dup(&olddir, &ei->dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) dentry = ei->entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) ep = exfat_get_dentry(sb, &olddir, dentry, &old_bh, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) if (!ep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) brelse(old_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) /* check whether new dir is existing directory and empty */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) if (new_inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) new_ei = EXFAT_I(new_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) if (new_ei->dir.dir == DIR_DELETED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) exfat_err(sb, "abnormal access to deleted target dentry");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) exfat_update_parent_info(new_ei, new_parent_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) p_dir = &(new_ei->dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) new_entry = new_ei->entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) ep = exfat_get_dentry(sb, p_dir, new_entry, &new_bh, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) if (!ep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) new_entry_type = exfat_get_entry_type(ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) brelse(new_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) /* if new_inode exists, update ei */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) if (new_entry_type == TYPE_DIR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) struct exfat_chain new_clu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) new_clu.dir = new_ei->start_clu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) new_clu.size =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) EXFAT_B_TO_CLU_ROUND_UP(i_size_read(new_inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) sbi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) new_clu.flags = new_ei->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) ret = exfat_check_dir_empty(sb, &new_clu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) /* check the validity of directory name in the given new pathname */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) ret = exfat_resolve_path(new_parent_inode, new_path, &newdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) &uni_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) exfat_set_volume_dirty(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) if (olddir.dir == newdir.dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) ret = exfat_rename_file(new_parent_inode, &olddir, dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) &uni_name, ei);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) ret = exfat_move_file(new_parent_inode, &olddir, dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) &newdir, &uni_name, ei);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) if (!ret && new_inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) /* delete entries of new_dir */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) ep = exfat_get_dentry(sb, p_dir, new_entry, &new_bh, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) if (!ep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) goto del_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) num_entries = exfat_count_ext_entries(sb, p_dir, new_entry, ep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) if (num_entries < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) goto del_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) brelse(new_bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) if (exfat_remove_entries(new_inode, p_dir, new_entry, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) num_entries + 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) goto del_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) /* Free the clusters if new_inode is a dir(as if exfat_rmdir) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) if (new_entry_type == TYPE_DIR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) /* new_ei, new_clu_to_free */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) struct exfat_chain new_clu_to_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) exfat_chain_set(&new_clu_to_free, new_ei->start_clu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) EXFAT_B_TO_CLU_ROUND_UP(i_size_read(new_inode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) sbi), new_ei->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) if (exfat_free_cluster(new_inode, &new_clu_to_free)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) /* just set I/O error only */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) ret = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) i_size_write(new_inode, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) new_ei->start_clu = EXFAT_EOF_CLUSTER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) new_ei->flags = ALLOC_NO_FAT_CHAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) del_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) /* Update new_inode ei
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) * Prevent syncing removed new_inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) * (new_ei is already initialized above code ("if (new_inode)")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) new_ei->dir.dir = DIR_DELETED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) exfat_clear_volume_dirty(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) static int exfat_rename(struct inode *old_dir, struct dentry *old_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) struct inode *new_dir, struct dentry *new_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) struct inode *old_inode, *new_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) struct super_block *sb = old_dir->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) loff_t i_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) * The VFS already checks for existence, so for local filesystems
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) * the RENAME_NOREPLACE implementation is equivalent to plain rename.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) * Don't support any other flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) if (flags & ~RENAME_NOREPLACE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) mutex_lock(&EXFAT_SB(sb)->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) old_inode = old_dentry->d_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) new_inode = new_dentry->d_inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) err = __exfat_rename(old_dir, EXFAT_I(old_inode), new_dir, new_dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) inode_inc_iversion(new_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) new_dir->i_ctime = new_dir->i_mtime = new_dir->i_atime =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) EXFAT_I(new_dir)->i_crtime = current_time(new_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) exfat_truncate_atime(&new_dir->i_atime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) if (IS_DIRSYNC(new_dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) exfat_sync_inode(new_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) mark_inode_dirty(new_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) i_pos = ((loff_t)EXFAT_I(old_inode)->dir.dir << 32) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) (EXFAT_I(old_inode)->entry & 0xffffffff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) exfat_unhash_inode(old_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) exfat_hash_inode(old_inode, i_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) if (IS_DIRSYNC(new_dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) exfat_sync_inode(old_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) mark_inode_dirty(old_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) drop_nlink(old_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) if (!new_inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) inc_nlink(new_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) inode_inc_iversion(old_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) old_dir->i_ctime = old_dir->i_mtime = current_time(old_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) if (IS_DIRSYNC(old_dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) exfat_sync_inode(old_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) mark_inode_dirty(old_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) if (new_inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) exfat_unhash_inode(new_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) /* skip drop_nlink if new_inode already has been dropped */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) if (new_inode->i_nlink) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) drop_nlink(new_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) if (S_ISDIR(new_inode->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) drop_nlink(new_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) exfat_warn(sb, "abnormal access to an inode dropped");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) WARN_ON(new_inode->i_nlink == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) new_inode->i_ctime = EXFAT_I(new_inode)->i_crtime =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) current_time(new_inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) mutex_unlock(&EXFAT_SB(sb)->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) const struct inode_operations exfat_dir_inode_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) .create = exfat_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) .lookup = exfat_lookup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) .unlink = exfat_unlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) .mkdir = exfat_mkdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) .rmdir = exfat_rmdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) .rename = exfat_rename,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) .setattr = exfat_setattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) .getattr = exfat_getattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) };