^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * linux/fs/fat/dir.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * directory handling functions for fat-based filesystems
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Written 1992,1993 by Werner Almesberger
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Hidden files 1995 by Albert Cahalan <albert@ccs.neu.edu> <adc@coe.neu.edu>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * VFAT extensions by Gordon Chaffee <chaffee@plateau.cs.berkeley.edu>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * Merged with msdos fs by Henrik Storner <storner@osiris.ping.dk>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * Rewritten for constant inumbers. Plugged buffer overrun in readdir(). AV
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * Short name translation 1999, 2001 by Wolfram Pienkoss <wp@bszh.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/compat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/iversion.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include "fat.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * Maximum buffer size of short name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * [(MSDOS_NAME + '.') * max one char + nul]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * For msdos style, ['.' (hidden) + MSDOS_NAME + '.' + nul]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define FAT_MAX_SHORT_SIZE ((MSDOS_NAME + 1) * NLS_MAX_CHARSET_SIZE + 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * Maximum buffer size of unicode chars from slots.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * [(max longname slots * 13 (size in a slot) + nul) * sizeof(wchar_t)]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define FAT_MAX_UNI_CHARS ((MSDOS_SLOTS - 1) * 13 + 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #define FAT_MAX_UNI_SIZE (FAT_MAX_UNI_CHARS * sizeof(wchar_t))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static inline unsigned char fat_tolower(unsigned char c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) return ((c >= 'A') && (c <= 'Z')) ? c+32 : c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) static inline loff_t fat_make_i_pos(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct buffer_head *bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) struct msdos_dir_entry *de)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return ((loff_t)bh->b_blocknr << MSDOS_SB(sb)->dir_per_block_bits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) | (de - (struct msdos_dir_entry *)bh->b_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) static inline void fat_dir_readahead(struct inode *dir, sector_t iblock,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) sector_t phys)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) struct super_block *sb = dir->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct msdos_sb_info *sbi = MSDOS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) int sec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) /* This is not a first sector of cluster, or sec_per_clus == 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if ((iblock & (sbi->sec_per_clus - 1)) || sbi->sec_per_clus == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) /* root dir of FAT12/FAT16 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (!is_fat32(sbi) && (dir->i_ino == MSDOS_ROOT_INO))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) bh = sb_find_get_block(sb, phys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (bh == NULL || !buffer_uptodate(bh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) for (sec = 0; sec < sbi->sec_per_clus; sec++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) sb_breadahead(sb, phys + sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) /* Returns the inode number of the directory entry at offset pos. If bh is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) non-NULL, it is brelse'd before. Pos is incremented. The buffer header is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) returned in bh.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) AV. Most often we do it item-by-item. Makes sense to optimize.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) AV. OK, there we go: if both bh and de are non-NULL we assume that we just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) AV. want the next entry (took one explicit de=NULL in vfat/namei.c).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) AV. It's done in fat_get_entry() (inlined), here the slow case lives.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) AV. Additionally, when we return -1 (i.e. reached the end of directory)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) AV. we make bh NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) static int fat__get_entry(struct inode *dir, loff_t *pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) struct buffer_head **bh, struct msdos_dir_entry **de)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) struct super_block *sb = dir->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) sector_t phys, iblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) unsigned long mapped_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) int err, offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) brelse(*bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) *bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) iblock = *pos >> sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) err = fat_bmap(dir, iblock, &phys, &mapped_blocks, 0, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) if (err || !phys)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) return -1; /* beyond EOF or error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) fat_dir_readahead(dir, iblock, phys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) *bh = sb_bread(sb, phys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (*bh == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) fat_msg_ratelimit(sb, KERN_ERR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) "Directory bread(block %llu) failed", (llu)phys);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) /* skip this block */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) *pos = (iblock + 1) << sb->s_blocksize_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) offset = *pos & (sb->s_blocksize - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) *pos += sizeof(struct msdos_dir_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) *de = (struct msdos_dir_entry *)((*bh)->b_data + offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static inline int fat_get_entry(struct inode *dir, loff_t *pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) struct buffer_head **bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) struct msdos_dir_entry **de)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) /* Fast stuff first */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (*bh && *de &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) (*de - (struct msdos_dir_entry *)(*bh)->b_data) <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) MSDOS_SB(dir->i_sb)->dir_per_block - 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) *pos += sizeof(struct msdos_dir_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) (*de)++;
^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) return fat__get_entry(dir, pos, bh, de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * Convert Unicode 16 to UTF-8, translated Unicode, or ASCII.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * If uni_xlate is enabled and we can't get a 1:1 conversion, use a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * colon as an escape character since it is normally invalid on the vfat
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * filesystem. The following four characters are the hexadecimal digits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * of Unicode value. This lets us do a full dump and restore of Unicode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) * filenames. We could get into some trouble with long Unicode names,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * but ignore that right now.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * Ahem... Stack smashing in ring 0 isn't fun. Fixed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) static int uni16_to_x8(struct super_block *sb, unsigned char *ascii,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) const wchar_t *uni, int len, struct nls_table *nls)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) int uni_xlate = MSDOS_SB(sb)->options.unicode_xlate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) const wchar_t *ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) wchar_t ec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) unsigned char *op;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) int charlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) ip = uni;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) op = ascii;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) while (*ip && ((len - NLS_MAX_CHARSET_SIZE) > 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) ec = *ip++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) charlen = nls->uni2char(ec, op, NLS_MAX_CHARSET_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) if (charlen > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) op += charlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) len -= charlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (uni_xlate == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) *op++ = ':';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) op = hex_byte_pack(op, ec >> 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) op = hex_byte_pack(op, ec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) len -= 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) *op++ = '?';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) len--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) if (unlikely(*ip)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) fat_msg(sb, KERN_WARNING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) "filename was truncated while converting.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) *op = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) return op - ascii;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) static inline int fat_uni_to_x8(struct super_block *sb, const wchar_t *uni,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) unsigned char *buf, int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) struct msdos_sb_info *sbi = MSDOS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) if (sbi->options.utf8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) return utf16s_to_utf8s(uni, FAT_MAX_UNI_CHARS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) UTF16_HOST_ENDIAN, buf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) return uni16_to_x8(sb, buf, uni, size, sbi->nls_io);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) static inline int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) fat_short2uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) int charlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) charlen = t->char2uni(c, clen, uni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) if (charlen < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) *uni = 0x003f; /* a question mark */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) charlen = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) return charlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) static inline int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) fat_short2lower_uni(struct nls_table *t, unsigned char *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) int clen, wchar_t *uni)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) int charlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) wchar_t wc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) charlen = t->char2uni(c, clen, &wc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) if (charlen < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) *uni = 0x003f; /* a question mark */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) charlen = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) } else if (charlen <= 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) unsigned char nc = t->charset2lower[*c];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) if (!nc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) nc = *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) charlen = t->char2uni(&nc, 1, uni);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) if (charlen < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) *uni = 0x003f; /* a question mark */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) charlen = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) *uni = wc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) return charlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) static inline int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) fat_shortname2uni(struct nls_table *nls, unsigned char *buf, int buf_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) wchar_t *uni_buf, unsigned short opt, int lower)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) int len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) if (opt & VFAT_SFN_DISPLAY_LOWER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) len = fat_short2lower_uni(nls, buf, buf_size, uni_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) else if (opt & VFAT_SFN_DISPLAY_WIN95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) len = fat_short2uni(nls, buf, buf_size, uni_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) else if (opt & VFAT_SFN_DISPLAY_WINNT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) if (lower)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) len = fat_short2lower_uni(nls, buf, buf_size, uni_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) len = fat_short2uni(nls, buf, buf_size, uni_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) len = fat_short2uni(nls, buf, buf_size, uni_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) static inline int fat_name_match(struct msdos_sb_info *sbi,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) const unsigned char *a, int a_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) const unsigned char *b, int b_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) if (a_len != b_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) if (sbi->options.name_check != 's')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) return !nls_strnicmp(sbi->nls_io, a, b, a_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) return !memcmp(a, b, a_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) enum { PARSE_INVALID = 1, PARSE_NOT_LONGNAME, PARSE_EOF, };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) * fat_parse_long - Parse extended directory entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) * This function returns zero on success, negative value on error, or one of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) * the following:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) * %PARSE_INVALID - Directory entry is invalid.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) * %PARSE_NOT_LONGNAME - Directory entry does not contain longname.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) * %PARSE_EOF - Directory has no more entries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) static int fat_parse_long(struct inode *dir, loff_t *pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) struct buffer_head **bh, struct msdos_dir_entry **de,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) wchar_t **unicode, unsigned char *nr_slots)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) struct msdos_dir_slot *ds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) unsigned char id, slot, slots, alias_checksum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) if (!*unicode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) *unicode = __getname();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) if (!*unicode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) brelse(*bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) parse_long:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) ds = (struct msdos_dir_slot *)*de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) id = ds->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) if (!(id & 0x40))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) return PARSE_INVALID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) slots = id & ~0x40;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) if (slots > 20 || !slots) /* ceil(256 * 2 / 26) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) return PARSE_INVALID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) *nr_slots = slots;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) alias_checksum = ds->alias_checksum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) slot = slots;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) int offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) slot--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) offset = slot * 13;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) fat16_towchar(*unicode + offset, ds->name0_4, 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) fat16_towchar(*unicode + offset + 5, ds->name5_10, 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) fat16_towchar(*unicode + offset + 11, ds->name11_12, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) if (ds->id & 0x40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) (*unicode)[offset + 13] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) if (fat_get_entry(dir, pos, bh, de) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) return PARSE_EOF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) if (slot == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) ds = (struct msdos_dir_slot *)*de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) if (ds->attr != ATTR_EXT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) return PARSE_NOT_LONGNAME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) if ((ds->id & ~0x40) != slot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) goto parse_long;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) if (ds->alias_checksum != alias_checksum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) goto parse_long;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) if ((*de)->name[0] == DELETED_FLAG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) return PARSE_INVALID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) if ((*de)->attr == ATTR_EXT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) goto parse_long;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) if (IS_FREE((*de)->name) || ((*de)->attr & ATTR_VOLUME))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) return PARSE_INVALID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) if (fat_checksum((*de)->name) != alias_checksum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) *nr_slots = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) * fat_parse_short - Parse MS-DOS (short) directory entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) * @sb: superblock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) * @de: directory entry to parse
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) * @name: FAT_MAX_SHORT_SIZE array in which to place extracted name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) * @dot_hidden: Nonzero == prepend '.' to names with ATTR_HIDDEN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) * Returns the number of characters extracted into 'name'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) static int fat_parse_short(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) const struct msdos_dir_entry *de,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) unsigned char *name, int dot_hidden)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) const struct msdos_sb_info *sbi = MSDOS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) int isvfat = sbi->options.isvfat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) int nocase = sbi->options.nocase;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) unsigned short opt_shortname = sbi->options.shortname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) struct nls_table *nls_disk = sbi->nls_disk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) wchar_t uni_name[14];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) unsigned char c, work[MSDOS_NAME];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) unsigned char *ptname = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) int chi, chl, i, j, k;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) int dotoffset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) int name_len = 0, uni_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) if (!isvfat && dot_hidden && (de->attr & ATTR_HIDDEN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) *ptname++ = '.';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) dotoffset = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) memcpy(work, de->name, sizeof(work));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) /* For an explanation of the special treatment of 0x05 in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) * filenames, see msdos_format_name in namei_msdos.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) if (work[0] == 0x05)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) work[0] = 0xE5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) /* Filename */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) for (i = 0, j = 0; i < 8;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) c = work[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) if (!c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) &uni_name[j++], opt_shortname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) de->lcase & CASE_LOWER_BASE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) if (chl <= 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) if (!isvfat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) ptname[i] = nocase ? c : fat_tolower(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) if (c != ' ') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) name_len = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) uni_len = j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) uni_len = j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) if (isvfat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) i += min(chl, 8-i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) for (chi = 0; chi < chl && i < 8; chi++, i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) ptname[i] = work[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) if (chl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) name_len = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) i = name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) j = uni_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) fat_short2uni(nls_disk, ".", 1, &uni_name[j++]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) if (!isvfat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) ptname[i] = '.';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) /* Extension */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) for (k = 8; k < MSDOS_NAME;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) c = work[k];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) if (!c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) chl = fat_shortname2uni(nls_disk, &work[k], MSDOS_NAME - k,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) &uni_name[j++], opt_shortname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) de->lcase & CASE_LOWER_EXT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) if (chl <= 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) k++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) if (!isvfat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) ptname[i] = nocase ? c : fat_tolower(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) if (c != ' ') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) name_len = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) uni_len = j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) uni_len = j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) if (isvfat) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) int offset = min(chl, MSDOS_NAME-k);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) k += offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) i += offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) for (chi = 0; chi < chl && k < MSDOS_NAME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) chi++, i++, k++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) ptname[i] = work[k];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) if (chl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) name_len = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) if (name_len > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) name_len += dotoffset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) if (sbi->options.isvfat) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) uni_name[uni_len] = 0x0000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) name_len = fat_uni_to_x8(sb, uni_name, name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) FAT_MAX_SHORT_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) return name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) * Return values: negative -> error/not found, 0 -> found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) int fat_search_long(struct inode *inode, const unsigned char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) int name_len, struct fat_slot_info *sinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) struct msdos_sb_info *sbi = MSDOS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) struct buffer_head *bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) struct msdos_dir_entry *de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) unsigned char nr_slots;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) wchar_t *unicode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) unsigned char bufname[FAT_MAX_SHORT_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) loff_t cpos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) int err, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) goto end_of_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) parse_record:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) nr_slots = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) if (de->name[0] == DELETED_FLAG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) if (de->attr != ATTR_EXT && IS_FREE(de->name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) if (de->attr == ATTR_EXT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) int status = fat_parse_long(inode, &cpos, &bh, &de,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) &unicode, &nr_slots);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) err = status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) goto end_of_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) } else if (status == PARSE_INVALID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) else if (status == PARSE_NOT_LONGNAME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) goto parse_record;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) else if (status == PARSE_EOF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) goto end_of_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) /* Never prepend '.' to hidden files here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) * That is done only for msdos mounts (and only when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) * 'dotsOK=yes'); if we are executing here, it is in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) * context of a vfat mount.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) len = fat_parse_short(sb, de, bufname, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) if (len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) /* Compare shortname */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) if (fat_name_match(sbi, name, name_len, bufname, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) goto found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) if (nr_slots) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) void *longname = unicode + FAT_MAX_UNI_CHARS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) int size = PATH_MAX - FAT_MAX_UNI_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) /* Compare longname */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) len = fat_uni_to_x8(sb, unicode, longname, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) if (fat_name_match(sbi, name, name_len, longname, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) goto found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) found:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) nr_slots++; /* include the de */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) sinfo->slot_off = cpos - nr_slots * sizeof(*de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) sinfo->nr_slots = nr_slots;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) sinfo->de = de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) sinfo->bh = bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) end_of_dir:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) if (unicode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) __putname(unicode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) EXPORT_SYMBOL_GPL(fat_search_long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) struct fat_ioctl_filldir_callback {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) struct dir_context ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) void __user *dirent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) /* for dir ioctl */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) const char *longname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) int long_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) const char *shortname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) int short_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) static int __fat_readdir(struct inode *inode, struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) struct dir_context *ctx, int short_only,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) struct fat_ioctl_filldir_callback *both)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) struct super_block *sb = inode->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) struct msdos_sb_info *sbi = MSDOS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) struct msdos_dir_entry *de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) unsigned char nr_slots;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) wchar_t *unicode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) unsigned char bufname[FAT_MAX_SHORT_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) int isvfat = sbi->options.isvfat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) const char *fill_name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) int fake_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) loff_t cpos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) int short_len = 0, fill_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) mutex_lock(&sbi->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) cpos = ctx->pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) /* Fake . and .. for the root directory. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) if (inode->i_ino == MSDOS_ROOT_INO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) if (!dir_emit_dots(file, ctx))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) if (ctx->pos == 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) fake_offset = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) cpos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) if (cpos & (sizeof(struct msdos_dir_entry) - 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) get_new:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) goto end_of_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) parse_record:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) nr_slots = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) * Check for long filename entry, but if short_only, we don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) * need to parse long filename.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) if (isvfat && !short_only) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) if (de->name[0] == DELETED_FLAG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) goto record_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) goto record_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) if (de->attr != ATTR_EXT && IS_FREE(de->name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) goto record_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) if ((de->attr & ATTR_VOLUME) || IS_FREE(de->name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) goto record_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) if (isvfat && de->attr == ATTR_EXT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) int status = fat_parse_long(inode, &cpos, &bh, &de,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) &unicode, &nr_slots);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) if (status < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) ret = status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) goto end_of_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) } else if (status == PARSE_INVALID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) goto record_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) else if (status == PARSE_NOT_LONGNAME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) goto parse_record;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) else if (status == PARSE_EOF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) goto end_of_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) if (nr_slots) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) void *longname = unicode + FAT_MAX_UNI_CHARS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) int size = PATH_MAX - FAT_MAX_UNI_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) int len = fat_uni_to_x8(sb, unicode, longname, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) fill_name = longname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) fill_len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) /* !both && !short_only, so we don't need shortname. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) if (!both)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) goto start_filldir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) short_len = fat_parse_short(sb, de, bufname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) sbi->options.dotsOK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) if (short_len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) goto record_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) /* hack for fat_ioctl_filldir() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) both->longname = fill_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) both->long_len = fill_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) both->shortname = bufname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) both->short_len = short_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) fill_name = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) fill_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) goto start_filldir;
^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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) short_len = fat_parse_short(sb, de, bufname, sbi->options.dotsOK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) if (short_len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) goto record_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) fill_name = bufname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) fill_len = short_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) start_filldir:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) ctx->pos = cpos - (nr_slots + 1) * sizeof(struct msdos_dir_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) if (fake_offset && ctx->pos < 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) ctx->pos = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) if (!memcmp(de->name, MSDOS_DOT, MSDOS_NAME)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) if (!dir_emit_dot(file, ctx))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) goto fill_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) } else if (!memcmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) if (!dir_emit_dotdot(file, ctx))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) goto fill_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) unsigned long inum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) loff_t i_pos = fat_make_i_pos(sb, bh, de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) struct inode *tmp = fat_iget(sb, i_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) if (tmp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) inum = tmp->i_ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) iput(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) inum = iunique(sb, MSDOS_ROOT_INO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) if (!dir_emit(ctx, fill_name, fill_len, inum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) (de->attr & ATTR_DIR) ? DT_DIR : DT_REG))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) goto fill_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) record_end:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) fake_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) ctx->pos = cpos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) goto get_new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) end_of_dir:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) if (fake_offset && cpos < 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) ctx->pos = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) ctx->pos = cpos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) fill_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) if (unicode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) __putname(unicode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) mutex_unlock(&sbi->s_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) static int fat_readdir(struct file *file, struct dir_context *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) return __fat_readdir(file_inode(file), file, ctx, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) #define FAT_IOCTL_FILLDIR_FUNC(func, dirent_type) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) static int func(struct dir_context *ctx, const char *name, int name_len, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) loff_t offset, u64 ino, unsigned int d_type) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) struct fat_ioctl_filldir_callback *buf = \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) container_of(ctx, struct fat_ioctl_filldir_callback, ctx); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) struct dirent_type __user *d1 = buf->dirent; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) struct dirent_type __user *d2 = d1 + 1; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) if (buf->result) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) return -EINVAL; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) buf->result++; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) if (name != NULL) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) /* dirent has only short name */ \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) if (name_len >= sizeof(d1->d_name)) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) name_len = sizeof(d1->d_name) - 1; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) if (put_user(0, d2->d_name) || \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) put_user(0, &d2->d_reclen) || \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) copy_to_user(d1->d_name, name, name_len) || \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) put_user(0, d1->d_name + name_len) || \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) put_user(name_len, &d1->d_reclen)) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) goto efault; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) } else { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) /* dirent has short and long name */ \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) const char *longname = buf->longname; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) int long_len = buf->long_len; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) const char *shortname = buf->shortname; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) int short_len = buf->short_len; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) if (long_len >= sizeof(d1->d_name)) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) long_len = sizeof(d1->d_name) - 1; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) if (short_len >= sizeof(d1->d_name)) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) short_len = sizeof(d1->d_name) - 1; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) if (copy_to_user(d2->d_name, longname, long_len) || \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) put_user(0, d2->d_name + long_len) || \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) put_user(long_len, &d2->d_reclen) || \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) put_user(ino, &d2->d_ino) || \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) put_user(offset, &d2->d_off) || \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) copy_to_user(d1->d_name, shortname, short_len) || \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) put_user(0, d1->d_name + short_len) || \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) put_user(short_len, &d1->d_reclen)) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) goto efault; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) } \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) return 0; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) efault: \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) buf->result = -EFAULT; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) return -EFAULT; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) FAT_IOCTL_FILLDIR_FUNC(fat_ioctl_filldir, __fat_dirent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) static int fat_ioctl_readdir(struct inode *inode, struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) void __user *dirent, filldir_t filldir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) int short_only, int both)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) struct fat_ioctl_filldir_callback buf = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) .ctx.actor = filldir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) .dirent = dirent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) buf.dirent = dirent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) buf.result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) inode_lock_shared(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) buf.ctx.pos = file->f_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) ret = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) if (!IS_DEADDIR(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) ret = __fat_readdir(inode, file, &buf.ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) short_only, both ? &buf : NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) file->f_pos = buf.ctx.pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) inode_unlock_shared(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) if (ret >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) ret = buf.result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) return ret;
^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) static long fat_dir_ioctl(struct file *filp, unsigned int cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) struct inode *inode = file_inode(filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) struct __fat_dirent __user *d1 = (struct __fat_dirent __user *)arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) int short_only, both;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) case VFAT_IOCTL_READDIR_SHORT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) short_only = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) both = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) case VFAT_IOCTL_READDIR_BOTH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) short_only = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) both = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) return fat_generic_ioctl(filp, cmd, arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) }
^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) * Yes, we don't need this put_user() absolutely. However old
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) * code didn't return the right value. So, app use this value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) * in order to check whether it is EOF.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) if (put_user(0, &d1->d_reclen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) return fat_ioctl_readdir(inode, filp, d1, fat_ioctl_filldir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) short_only, both);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) #define VFAT_IOCTL_READDIR_BOTH32 _IOR('r', 1, struct compat_dirent[2])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) #define VFAT_IOCTL_READDIR_SHORT32 _IOR('r', 2, struct compat_dirent[2])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) FAT_IOCTL_FILLDIR_FUNC(fat_compat_ioctl_filldir, compat_dirent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) static long fat_compat_dir_ioctl(struct file *filp, unsigned cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) struct inode *inode = file_inode(filp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) struct compat_dirent __user *d1 = compat_ptr(arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) int short_only, both;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) case VFAT_IOCTL_READDIR_SHORT32:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) short_only = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) both = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) case VFAT_IOCTL_READDIR_BOTH32:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) short_only = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) both = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) return fat_generic_ioctl(filp, cmd, (unsigned long)arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) * Yes, we don't need this put_user() absolutely. However old
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) * code didn't return the right value. So, app use this value,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) * in order to check whether it is EOF.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) if (put_user(0, &d1->d_reclen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) return fat_ioctl_readdir(inode, filp, d1, fat_compat_ioctl_filldir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) short_only, both);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) #endif /* CONFIG_COMPAT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) const struct file_operations fat_dir_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) .llseek = generic_file_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) .read = generic_read_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) .iterate_shared = fat_readdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) .unlocked_ioctl = fat_dir_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) .compat_ioctl = fat_compat_dir_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) .fsync = fat_file_fsync,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) static int fat_get_short_entry(struct inode *dir, loff_t *pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) struct buffer_head **bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) struct msdos_dir_entry **de)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) while (fat_get_entry(dir, pos, bh, de) >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) /* free entry or long name entry or volume label */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) if (!IS_FREE((*de)->name) && !((*de)->attr & ATTR_VOLUME))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) * The ".." entry can not provide the "struct fat_slot_info" information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) * for inode, nor a usable i_pos. So, this function provides some information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) * only.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) * Since this function walks through the on-disk inodes within a directory,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) * callers are responsible for taking any locks necessary to prevent the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) * directory from changing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) int fat_get_dotdot_entry(struct inode *dir, struct buffer_head **bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) struct msdos_dir_entry **de)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) loff_t offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) *de = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) while (fat_get_short_entry(dir, &offset, bh, de) >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) if (!strncmp((*de)->name, MSDOS_DOTDOT, MSDOS_NAME))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) EXPORT_SYMBOL_GPL(fat_get_dotdot_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) /* See if directory is empty */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) int fat_dir_empty(struct inode *dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) struct msdos_dir_entry *de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) loff_t cpos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) int result = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) cpos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) if (strncmp(de->name, MSDOS_DOT , MSDOS_NAME) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) strncmp(de->name, MSDOS_DOTDOT, MSDOS_NAME)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) result = -ENOTEMPTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) EXPORT_SYMBOL_GPL(fat_dir_empty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) * fat_subdirs counts the number of sub-directories of dir. It can be run
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) * on directories being created.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) int fat_subdirs(struct inode *dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) struct msdos_dir_entry *de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) loff_t cpos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) int count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) cpos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) while (fat_get_short_entry(dir, &cpos, &bh, &de) >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) if (de->attr & ATTR_DIR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) * Scans a directory for a given file (name points to its formatted name).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) * Returns an error code or zero.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) int fat_scan(struct inode *dir, const unsigned char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) struct fat_slot_info *sinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) struct super_block *sb = dir->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) sinfo->slot_off = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) sinfo->bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) while (fat_get_short_entry(dir, &sinfo->slot_off, &sinfo->bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) &sinfo->de) >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) if (!strncmp(sinfo->de->name, name, MSDOS_NAME)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) sinfo->slot_off -= sizeof(*sinfo->de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) sinfo->nr_slots = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) EXPORT_SYMBOL_GPL(fat_scan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) * Scans a directory for a given logstart.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) * Returns an error code or zero.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) int fat_scan_logstart(struct inode *dir, int i_logstart,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) struct fat_slot_info *sinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) struct super_block *sb = dir->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) sinfo->slot_off = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) sinfo->bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982) while (fat_get_short_entry(dir, &sinfo->slot_off, &sinfo->bh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) &sinfo->de) >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) if (fat_get_start(MSDOS_SB(sb), sinfo->de) == i_logstart) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) sinfo->slot_off -= sizeof(*sinfo->de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) sinfo->nr_slots = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994) static int __fat_remove_entries(struct inode *dir, loff_t pos, int nr_slots)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) struct super_block *sb = dir->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) struct msdos_dir_entry *de, *endp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) int err = 0, orig_slots;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) while (nr_slots) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) if (fat_get_entry(dir, &pos, &bh, &de) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) orig_slots = nr_slots;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) endp = (struct msdos_dir_entry *)(bh->b_data + sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) while (nr_slots && de < endp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) de->name[0] = DELETED_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) de++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) nr_slots--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) mark_buffer_dirty_inode(bh, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) if (IS_DIRSYNC(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) err = sync_dirty_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) /* pos is *next* de's position, so this does `- sizeof(de)' */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) pos += ((orig_slots - nr_slots) * sizeof(*de)) - sizeof(*de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) int fat_remove_entries(struct inode *dir, struct fat_slot_info *sinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) struct super_block *sb = dir->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) struct msdos_dir_entry *de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) struct buffer_head *bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) int err = 0, nr_slots;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) * First stage: Remove the shortname. By this, the directory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) * entry is removed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) nr_slots = sinfo->nr_slots;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) de = sinfo->de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) sinfo->de = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) bh = sinfo->bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) sinfo->bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) while (nr_slots && de >= (struct msdos_dir_entry *)bh->b_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) de->name[0] = DELETED_FLAG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) de--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) nr_slots--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) mark_buffer_dirty_inode(bh, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) if (IS_DIRSYNC(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) err = sync_dirty_buffer(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) inode_inc_iversion(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) if (nr_slots) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) * Second stage: remove the remaining longname slots.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) * (This directory entry is already removed, and so return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) * the success)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) err = __fat_remove_entries(dir, sinfo->slot_off, nr_slots);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) fat_msg(sb, KERN_WARNING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) "Couldn't remove the long name slots");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) fat_truncate_time(dir, NULL, S_ATIME|S_MTIME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) if (IS_DIRSYNC(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) (void)fat_sync_inode(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) mark_inode_dirty(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) EXPORT_SYMBOL_GPL(fat_remove_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) static int fat_zeroed_cluster(struct inode *dir, sector_t blknr, int nr_used,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) struct buffer_head **bhs, int nr_bhs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) struct super_block *sb = dir->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) sector_t last_blknr = blknr + MSDOS_SB(sb)->sec_per_clus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) int err, i, n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) /* Zeroing the unused blocks on this cluster */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) blknr += nr_used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) n = nr_used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) while (blknr < last_blknr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) bhs[n] = sb_getblk(sb, blknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) if (!bhs[n]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) /* Avoid race with userspace read via bdev */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) lock_buffer(bhs[n]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) memset(bhs[n]->b_data, 0, sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) set_buffer_uptodate(bhs[n]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) unlock_buffer(bhs[n]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) mark_buffer_dirty_inode(bhs[n], dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) n++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) blknr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) if (n == nr_bhs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) if (IS_DIRSYNC(dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) err = fat_sync_bhs(bhs, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) for (i = 0; i < n; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) brelse(bhs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) n = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) if (IS_DIRSYNC(dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) err = fat_sync_bhs(bhs, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) for (i = 0; i < n; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) brelse(bhs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) for (i = 0; i < n; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) bforget(bhs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) int fat_alloc_new_dir(struct inode *dir, struct timespec64 *ts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) struct super_block *sb = dir->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) struct msdos_sb_info *sbi = MSDOS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) struct buffer_head *bhs[MAX_BUF_PER_PAGE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) struct msdos_dir_entry *de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) sector_t blknr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) __le16 date, time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) u8 time_cs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) int err, cluster;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) err = fat_alloc_clusters(dir, &cluster, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) blknr = fat_clus_to_blknr(sbi, cluster);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) bhs[0] = sb_getblk(sb, blknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) if (!bhs[0]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) goto error_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) fat_time_unix2fat(sbi, ts, &time, &date, &time_cs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) de = (struct msdos_dir_entry *)bhs[0]->b_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) /* Avoid race with userspace read via bdev */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) lock_buffer(bhs[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) /* filling the new directory slots ("." and ".." entries) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) memcpy(de[0].name, MSDOS_DOT, MSDOS_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) memcpy(de[1].name, MSDOS_DOTDOT, MSDOS_NAME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) de->attr = de[1].attr = ATTR_DIR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) de[0].lcase = de[1].lcase = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) de[0].time = de[1].time = time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) de[0].date = de[1].date = date;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) if (sbi->options.isvfat) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) /* extra timestamps */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) de[0].ctime = de[1].ctime = time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) de[0].ctime_cs = de[1].ctime_cs = time_cs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = date;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) de[0].ctime = de[1].ctime = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) de[0].ctime_cs = de[1].ctime_cs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) de[0].adate = de[0].cdate = de[1].adate = de[1].cdate = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) fat_set_start(&de[0], cluster);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) fat_set_start(&de[1], MSDOS_I(dir)->i_logstart);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) de[0].size = de[1].size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) memset(de + 2, 0, sb->s_blocksize - 2 * sizeof(*de));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) set_buffer_uptodate(bhs[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) unlock_buffer(bhs[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) mark_buffer_dirty_inode(bhs[0], dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) err = fat_zeroed_cluster(dir, blknr, 1, bhs, MAX_BUF_PER_PAGE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) goto error_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) return cluster;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) error_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) fat_free_clusters(dir, cluster);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) EXPORT_SYMBOL_GPL(fat_alloc_new_dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) static int fat_add_new_entries(struct inode *dir, void *slots, int nr_slots,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) int *nr_cluster, struct msdos_dir_entry **de,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) struct buffer_head **bh, loff_t *i_pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) struct super_block *sb = dir->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) struct msdos_sb_info *sbi = MSDOS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) struct buffer_head *bhs[MAX_BUF_PER_PAGE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) sector_t blknr, start_blknr, last_blknr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) unsigned long size, copy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) int err, i, n, offset, cluster[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) * The minimum cluster size is 512bytes, and maximum entry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) * size is 32*slots (672bytes). So, iff the cluster size is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) * 512bytes, we may need two clusters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) size = nr_slots * sizeof(struct msdos_dir_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) *nr_cluster = (size + (sbi->cluster_size - 1)) >> sbi->cluster_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) BUG_ON(*nr_cluster > 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) err = fat_alloc_clusters(dir, cluster, *nr_cluster);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) * First stage: Fill the directory entry. NOTE: This cluster
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) * is not referenced from any inode yet, so updates order is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) * not important.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) i = n = copy = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) start_blknr = blknr = fat_clus_to_blknr(sbi, cluster[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) last_blknr = start_blknr + sbi->sec_per_clus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) while (blknr < last_blknr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) bhs[n] = sb_getblk(sb, blknr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) if (!bhs[n]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) goto error_nomem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) /* fill the directory entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) copy = min(size, sb->s_blocksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) /* Avoid race with userspace read via bdev */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) lock_buffer(bhs[n]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) memcpy(bhs[n]->b_data, slots, copy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) set_buffer_uptodate(bhs[n]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) unlock_buffer(bhs[n]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) mark_buffer_dirty_inode(bhs[n], dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) slots += copy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) size -= copy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) if (!size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) n++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) blknr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) } while (++i < *nr_cluster);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) memset(bhs[n]->b_data + copy, 0, sb->s_blocksize - copy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) offset = copy - sizeof(struct msdos_dir_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) get_bh(bhs[n]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) *bh = bhs[n];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) *de = (struct msdos_dir_entry *)((*bh)->b_data + offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) *i_pos = fat_make_i_pos(sb, *bh, *de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) /* Second stage: clear the rest of cluster, and write outs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) err = fat_zeroed_cluster(dir, start_blknr, ++n, bhs, MAX_BUF_PER_PAGE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) goto error_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) return cluster[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) error_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) brelse(*bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) *bh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) n = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) error_nomem:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) for (i = 0; i < n; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) bforget(bhs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) fat_free_clusters(dir, cluster[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) int fat_add_entries(struct inode *dir, void *slots, int nr_slots,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) struct fat_slot_info *sinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) struct super_block *sb = dir->i_sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) struct msdos_sb_info *sbi = MSDOS_SB(sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) struct buffer_head *bh, *prev, *bhs[3]; /* 32*slots (672bytes) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) struct msdos_dir_entry *de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) int err, free_slots, i, nr_bhs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) loff_t pos, i_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) sinfo->nr_slots = nr_slots;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) /* First stage: search free directory entries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) free_slots = nr_bhs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) bh = prev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) err = -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) while (fat_get_entry(dir, &pos, &bh, &de) > -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) /* check the maximum size of directory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) if (pos >= FAT_MAX_DIR_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) if (IS_FREE(de->name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) if (prev != bh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) get_bh(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) bhs[nr_bhs] = prev = bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) nr_bhs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) free_slots++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) if (free_slots == nr_slots)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) goto found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) for (i = 0; i < nr_bhs; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) brelse(bhs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) prev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) free_slots = nr_bhs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) if (dir->i_ino == MSDOS_ROOT_INO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) if (!is_fat32(sbi))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) } else if (MSDOS_I(dir)->i_start == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) fat_msg(sb, KERN_ERR, "Corrupted directory (i_pos %lld)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) MSDOS_I(dir)->i_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) found:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) pos -= free_slots * sizeof(*de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) nr_slots -= free_slots;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) if (free_slots) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) * Second stage: filling the free entries with new entries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) * NOTE: If this slots has shortname, first, we write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) * the long name slots, then write the short name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) int size = free_slots * sizeof(*de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) int offset = pos & (sb->s_blocksize - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) int long_bhs = nr_bhs - (nr_slots == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) /* Fill the long name slots. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) for (i = 0; i < long_bhs; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) int copy = min_t(int, sb->s_blocksize - offset, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) memcpy(bhs[i]->b_data + offset, slots, copy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) mark_buffer_dirty_inode(bhs[i], dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) slots += copy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) size -= copy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) if (long_bhs && IS_DIRSYNC(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) err = fat_sync_bhs(bhs, long_bhs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) if (!err && i < nr_bhs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) /* Fill the short name slot. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) int copy = min_t(int, sb->s_blocksize - offset, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) memcpy(bhs[i]->b_data + offset, slots, copy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) mark_buffer_dirty_inode(bhs[i], dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) if (IS_DIRSYNC(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) err = sync_dirty_buffer(bhs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) for (i = 0; i < nr_bhs; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) brelse(bhs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) goto error_remove;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) if (nr_slots) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) int cluster, nr_cluster;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) * Third stage: allocate the cluster for new entries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) * And initialize the cluster with new entries, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) * add the cluster to dir.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) cluster = fat_add_new_entries(dir, slots, nr_slots, &nr_cluster,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) &de, &bh, &i_pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) if (cluster < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) err = cluster;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) goto error_remove;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) err = fat_chain_add(dir, cluster, nr_cluster);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) fat_free_clusters(dir, cluster);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) goto error_remove;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) if (dir->i_size & (sbi->cluster_size - 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) fat_fs_error(sb, "Odd directory size");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) dir->i_size = (dir->i_size + sbi->cluster_size - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) & ~((loff_t)sbi->cluster_size - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) dir->i_size += nr_cluster << sbi->cluster_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) MSDOS_I(dir)->mmu_private += nr_cluster << sbi->cluster_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) sinfo->slot_off = pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) sinfo->de = de;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) sinfo->bh = bh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) for (i = 0; i < nr_bhs; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) brelse(bhs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) error_remove:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) brelse(bh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) if (free_slots)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) __fat_remove_entries(dir, pos, free_slots);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) EXPORT_SYMBOL_GPL(fat_add_entries);