// SPDX-License-Identifier: GPL-2.0-only /* * linux/fs/fat/dir.c * * directory handling functions for fat-based filesystems * * Written 1992,1993 by Werner Almesberger * * Hidden files 1995 by Albert Cahalan <albert@ccs.neu.edu> <adc@coe.neu.edu> * * VFAT extensions by Gordon Chaffee <chaffee@plateau.cs.berkeley.edu> * Merged with msdos fs by Henrik Storner <storner@osiris.ping.dk> * Rewritten for constant inumbers. Plugged buffer overrun in readdir(). AV * Short name translation 1999, 2001 by Wolfram Pienkoss <wp@bszh.de>
*/
/* This is not a first sector of cluster, or sec_per_clus == 1 */ if ((iblock & (sbi->sec_per_clus - 1)) || sbi->sec_per_clus == 1) return; /* root dir of FAT12/FAT16 */ if (!is_fat32(sbi) && (dir->i_ino == MSDOS_ROOT_INO)) return;
/* Returns the inode number of the directory entry at offset pos. If bh is non-NULL, it is brelse'd before. Pos is incremented. The buffer header is returned in bh. AV. Most often we do it item-by-item. Makes sense to optimize. AV. OK, there we go: if both bh and de are non-NULL we assume that we just AV. want the next entry (took one explicit de=NULL in vfat/namei.c). AV. It's done in fat_get_entry() (inlined), here the slow case lives. AV. Additionally, when we return -1 (i.e. reached the end of directory) AV. we make bh NULL.
*/ staticint fat__get_entry(struct inode *dir, loff_t *pos, struct buffer_head **bh, struct msdos_dir_entry **de)
{ struct super_block *sb = dir->i_sb;
sector_t phys, iblock; unsignedlong mapped_blocks; int err, offset;
/* * Convert Unicode 16 to UTF-8, translated Unicode, or ASCII. * If uni_xlate is enabled and we can't get a 1:1 conversion, use a * colon as an escape character since it is normally invalid on the vfat * filesystem. The following four characters are the hexadecimal digits * of Unicode value. This lets us do a full dump and restore of Unicode * filenames. We could get into some trouble with long Unicode names, * but ignore that right now. * Ahem... Stack smashing in ring 0 isn't fun. Fixed.
*/ staticint uni16_to_x8(struct super_block *sb, unsignedchar *ascii, constwchar_t *uni, int len, struct nls_table *nls)
{ int uni_xlate = MSDOS_SB(sb)->options.unicode_xlate; constwchar_t *ip; wchar_t ec; unsignedchar *op; int charlen;
ip = uni;
op = ascii;
while (*ip && ((len - NLS_MAX_CHARSET_SIZE) > 0)) {
ec = *ip++;
charlen = nls->uni2char(ec, op, NLS_MAX_CHARSET_SIZE); if (charlen > 0) {
op += charlen;
len -= charlen;
} else { if (uni_xlate == 1) {
*op++ = ':';
op = hex_byte_pack(op, ec >> 8);
op = hex_byte_pack(op, ec);
len -= 5;
} else {
*op++ = '?';
len--;
}
}
}
if (unlikely(*ip)) {
fat_msg(sb, KERN_WARNING, "filename was truncated while converting.");
}
charlen = t->char2uni(&nc, 1, uni); if (charlen < 0) {
*uni = 0x003f; /* a question mark */
charlen = 1;
}
} else
*uni = wc;
return charlen;
}
staticinlineint
fat_shortname2uni(struct nls_table *nls, unsignedchar *buf, int buf_size, wchar_t *uni_buf, unsignedshort opt, int lower)
{ int len = 0;
if (opt & VFAT_SFN_DISPLAY_LOWER)
len = fat_short2lower_uni(nls, buf, buf_size, uni_buf); elseif (opt & VFAT_SFN_DISPLAY_WIN95)
len = fat_short2uni(nls, buf, buf_size, uni_buf); elseif (opt & VFAT_SFN_DISPLAY_WINNT) { if (lower)
len = fat_short2lower_uni(nls, buf, buf_size, uni_buf); else
len = fat_short2uni(nls, buf, buf_size, uni_buf);
} else
len = fat_short2uni(nls, buf, buf_size, uni_buf);
return len;
}
staticinlineint fat_name_match(struct msdos_sb_info *sbi, constunsignedchar *a, int a_len, constunsignedchar *b, int b_len)
{ if (a_len != b_len) return 0;
if (sbi->options.name_check != 's') return !nls_strnicmp(sbi->nls_io, a, b, a_len); else return !memcmp(a, b, a_len);
}
/** * fat_parse_long - Parse extended directory entry. * * @dir: Pointer to the inode that represents the directory. * @pos: On input, contains the starting position to read from. * On output, updated with the new position. * @bh: Pointer to the buffer head that may be used for reading directory * entries. May be updated. * @de: On input, points to the current directory entry. * On output, points to the next directory entry. * @unicode: Pointer to a buffer where the parsed Unicode long filename will be * stored. * @nr_slots: Pointer to a variable that will store the number of longname * slots found. * * This function returns zero on success, negative value on error, or one of * the following: * * %PARSE_INVALID - Directory entry is invalid. * %PARSE_NOT_LONGNAME - Directory entry does not contain longname. * %PARSE_EOF - Directory has no more entries.
*/ staticint fat_parse_long(struct inode *dir, loff_t *pos, struct buffer_head **bh, struct msdos_dir_entry **de, wchar_t **unicode, unsignedchar *nr_slots)
{ struct msdos_dir_slot *ds; unsignedchar id, slot, slots, alias_checksum;
if (!*unicode) {
*unicode = __getname(); if (!*unicode) {
brelse(*bh); return -ENOMEM;
}
}
parse_long:
ds = (struct msdos_dir_slot *)*de;
id = ds->id; if (!(id & 0x40)) return PARSE_INVALID;
slots = id & ~0x40; if (slots > 20 || !slots) /* ceil(256 * 2 / 26) */ return PARSE_INVALID;
*nr_slots = slots;
alias_checksum = ds->alias_checksum;
memcpy(work, de->name, sizeof(work)); /* For an explanation of the special treatment of 0x05 in * filenames, see msdos_format_name in namei_msdos.c
*/ if (work[0] == 0x05)
work[0] = 0xE5;
/* Filename */ for (i = 0, j = 0; i < 8;) {
c = work[i]; if (!c) break;
chl = fat_shortname2uni(nls_disk, &work[i], 8 - i,
&uni_name[j++], opt_shortname,
de->lcase & CASE_LOWER_BASE); if (chl <= 1) { if (!isvfat)
ptname[i] = nocase ? c : fat_tolower(c);
i++; if (c != ' ') {
name_len = i;
uni_len = j;
}
} else {
uni_len = j; if (isvfat)
i += min(chl, 8-i); else { for (chi = 0; chi < chl && i < 8; chi++, i++)
ptname[i] = work[i];
} if (chl)
name_len = i;
}
}
i = name_len;
j = uni_len;
fat_short2uni(nls_disk, ".", 1, &uni_name[j++]); if (!isvfat)
ptname[i] = '.';
i++;
/* Extension */ for (k = 8; k < MSDOS_NAME;) {
c = work[k]; if (!c) break;
chl = fat_shortname2uni(nls_disk, &work[k], MSDOS_NAME - k,
&uni_name[j++], opt_shortname,
de->lcase & CASE_LOWER_EXT); if (chl <= 1) {
k++; if (!isvfat)
ptname[i] = nocase ? c : fat_tolower(c);
i++; if (c != ' ') {
name_len = i;
uni_len = j;
}
} else {
uni_len = j; if (isvfat) { int offset = min(chl, MSDOS_NAME-k);
k += offset;
i += offset;
} else { for (chi = 0; chi < chl && k < MSDOS_NAME;
chi++, i++, k++) {
ptname[i] = work[k];
}
} if (chl)
name_len = i;
}
}
err = -ENOENT; while (1) { if (fat_get_entry(inode, &cpos, &bh, &de) == -1) goto end_of_dir;
parse_record:
nr_slots = 0; if (de->name[0] == DELETED_FLAG) continue; if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME)) continue; if (de->attr != ATTR_EXT && IS_FREE(de->name)) continue; if (de->attr == ATTR_EXT) { int status = fat_parse_long(inode, &cpos, &bh, &de,
&unicode, &nr_slots); if (status < 0) {
err = status; goto end_of_dir;
} elseif (status == PARSE_INVALID) continue; elseif (status == PARSE_NOT_LONGNAME) goto parse_record; elseif (status == PARSE_EOF) goto end_of_dir;
}
/* Never prepend '.' to hidden files here. * That is done only for msdos mounts (and only when * 'dotsOK=yes'); if we are executing here, it is in the * context of a vfat mount.
*/
len = fat_parse_short(sb, de, bufname, 0); if (len == 0) continue;
if (nr_slots) { void *longname = unicode + FAT_MAX_UNI_CHARS; int size = PATH_MAX - FAT_MAX_UNI_SIZE;
/* Compare longname */
len = fat_uni_to_x8(sb, unicode, longname, size); if (fat_name_match(sbi, name, name_len, longname, len)) goto found;
}
}
found:
nr_slots++; /* include the de */
sinfo->slot_off = cpos - nr_slots * sizeof(*de);
sinfo->nr_slots = nr_slots;
sinfo->de = de;
sinfo->bh = bh;
sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
err = 0;
end_of_dir: if (unicode)
__putname(unicode);
return err;
}
EXPORT_SYMBOL_GPL(fat_search_long);
struct fat_ioctl_filldir_callback { struct dir_context ctx; void __user *dirent; int result; /* for dir ioctl */ constchar *longname; int long_len; constchar *shortname; int short_len;
};
switch (cmd) { case VFAT_IOCTL_READDIR_SHORT:
short_only = 1;
both = 0; break; case VFAT_IOCTL_READDIR_BOTH:
short_only = 0;
both = 1; break; default: return fat_generic_ioctl(filp, cmd, arg);
}
/* * Yes, we don't need this put_user() absolutely. However old * code didn't return the right value. So, app use this value, * in order to check whether it is EOF.
*/ if (put_user(0, &d1->d_reclen)) return -EFAULT;
switch (cmd) { case VFAT_IOCTL_READDIR_SHORT32:
short_only = 1;
both = 0; break; case VFAT_IOCTL_READDIR_BOTH32:
short_only = 0;
both = 1; break; default: return fat_generic_ioctl(filp, cmd, (unsignedlong)arg);
}
/* * Yes, we don't need this put_user() absolutely. However old * code didn't return the right value. So, app use this value, * in order to check whether it is EOF.
*/ if (put_user(0, &d1->d_reclen)) return -EFAULT;
staticint fat_get_short_entry(struct inode *dir, loff_t *pos, struct buffer_head **bh, struct msdos_dir_entry **de)
{ while (fat_get_entry(dir, pos, bh, de) >= 0) { /* free entry or long name entry or volume label */ if (!IS_FREE((*de)->name) && !((*de)->attr & ATTR_VOLUME)) return 0;
} return -ENOENT;
}
/* * The ".." entry can not provide the "struct fat_slot_info" information * for inode, nor a usable i_pos. So, this function provides some information * only. * * Since this function walks through the on-disk inodes within a directory, * callers are responsible for taking any locks necessary to prevent the * directory from changing.
*/ int fat_get_dotdot_entry(struct inode *dir, struct buffer_head **bh, struct msdos_dir_entry **de)
{
loff_t offset = 0;
/* See if directory is empty */ int fat_dir_empty(struct inode *dir)
{ struct buffer_head *bh; struct msdos_dir_entry *de;
loff_t cpos; int result = 0;
/* * fat_subdirs counts the number of sub-directories of dir. It can be run * on directories being created.
*/ int fat_subdirs(struct inode *dir)
{ struct buffer_head *bh; struct msdos_dir_entry *de;
loff_t cpos; int count = 0;
/* * Scans a directory for a given file (name points to its formatted name). * Returns an error code or zero.
*/ int fat_scan(struct inode *dir, constunsignedchar *name, struct fat_slot_info *sinfo)
{ struct super_block *sb = dir->i_sb;
/* * Scans a directory for a given logstart. * Returns an error code or zero.
*/ int fat_scan_logstart(struct inode *dir, int i_logstart, struct fat_slot_info *sinfo)
{ struct super_block *sb = dir->i_sb;
/* * First stage: Remove the shortname. By this, the directory * entry is removed.
*/
nr_slots = sinfo->nr_slots;
de = sinfo->de;
sinfo->de = NULL;
bh = sinfo->bh;
sinfo->bh = NULL; while (nr_slots && de >= (struct msdos_dir_entry *)bh->b_data) {
de->name[0] = DELETED_FLAG;
de--;
nr_slots--;
}
mark_buffer_dirty_inode(bh, dir); if (IS_DIRSYNC(dir))
err = sync_dirty_buffer(bh);
brelse(bh); if (err) return err;
inode_inc_iversion(dir);
if (nr_slots) { /* * Second stage: remove the remaining longname slots. * (This directory entry is already removed, and so return * the success)
*/
err = __fat_remove_entries(dir, sinfo->slot_off, nr_slots); if (err) {
fat_msg(sb, KERN_WARNING, "Couldn't remove the long name slots");
}
}
fat_truncate_time(dir, NULL, S_ATIME|S_MTIME); if (IS_DIRSYNC(dir))
(void)fat_sync_inode(dir); else
mark_inode_dirty(dir);
staticint fat_zeroed_cluster(struct inode *dir, sector_t blknr, int nr_used, struct buffer_head **bhs, int nr_bhs)
{ struct super_block *sb = dir->i_sb;
sector_t last_blknr = blknr + MSDOS_SB(sb)->sec_per_clus; int err, i, n;
/* Zeroing the unused blocks on this cluster */
blknr += nr_used;
n = nr_used; while (blknr < last_blknr) {
bhs[n] = sb_getblk(sb, blknr); if (!bhs[n]) {
err = -ENOMEM; goto error;
} /* Avoid race with userspace read via bdev */
lock_buffer(bhs[n]);
memset(bhs[n]->b_data, 0, sb->s_blocksize);
set_buffer_uptodate(bhs[n]);
unlock_buffer(bhs[n]);
mark_buffer_dirty_inode(bhs[n], dir);
n++;
blknr++; if (n == nr_bhs) { if (IS_DIRSYNC(dir)) {
err = fat_sync_bhs(bhs, n); if (err) goto error;
} for (i = 0; i < n; i++)
brelse(bhs[i]);
n = 0;
}
} if (IS_DIRSYNC(dir)) {
err = fat_sync_bhs(bhs, n); if (err) goto error;
} for (i = 0; i < n; i++)
brelse(bhs[i]);
return 0;
error: for (i = 0; i < n; i++)
bforget(bhs[i]); return err;
}
staticint fat_add_new_entries(struct inode *dir, void *slots, int nr_slots, int *nr_cluster, struct msdos_dir_entry **de, struct buffer_head **bh, loff_t *i_pos)
{ struct super_block *sb = dir->i_sb; struct msdos_sb_info *sbi = MSDOS_SB(sb); struct buffer_head *bhs[MAX_BUF_PER_PAGE];
sector_t blknr, start_blknr, last_blknr; unsignedlong size, copy; int err, i, n, offset, cluster[2];
/* * The minimum cluster size is 512bytes, and maximum entry * size is 32*slots (672bytes). So, iff the cluster size is * 512bytes, we may need two clusters.
*/
size = nr_slots * sizeof(struct msdos_dir_entry);
*nr_cluster = (size + (sbi->cluster_size - 1)) >> sbi->cluster_bits;
BUG_ON(*nr_cluster > 2);
err = fat_alloc_clusters(dir, cluster, *nr_cluster); if (err) goto error;
/* * First stage: Fill the directory entry. NOTE: This cluster * is not referenced from any inode yet, so updates order is * not important.
*/
i = n = copy = 0; do {
start_blknr = blknr = fat_clus_to_blknr(sbi, cluster[i]);
last_blknr = start_blknr + sbi->sec_per_clus; while (blknr < last_blknr) {
bhs[n] = sb_getblk(sb, blknr); if (!bhs[n]) {
err = -ENOMEM; goto error_nomem;
}
/* fill the directory entry */
copy = min(size, sb->s_blocksize); /* Avoid race with userspace read via bdev */
lock_buffer(bhs[n]);
memcpy(bhs[n]->b_data, slots, copy);
set_buffer_uptodate(bhs[n]);
unlock_buffer(bhs[n]);
mark_buffer_dirty_inode(bhs[n], dir);
slots += copy;
size -= copy; if (!size) break;
n++;
blknr++;
}
} while (++i < *nr_cluster);
/* Second stage: clear the rest of cluster, and write outs */
err = fat_zeroed_cluster(dir, start_blknr, ++n, bhs, MAX_BUF_PER_PAGE); if (err) goto error_free;
return cluster[0];
error_free:
brelse(*bh);
*bh = NULL;
n = 0;
error_nomem: for (i = 0; i < n; i++)
bforget(bhs[i]);
fat_free_clusters(dir, cluster[0]);
error: return err;
}
/* First stage: search free directory entries */
free_slots = nr_bhs = 0;
bh = prev = NULL;
pos = 0;
err = -ENOSPC; while (fat_get_entry(dir, &pos, &bh, &de) > -1) { /* check the maximum size of directory */ if (pos >= FAT_MAX_DIR_SIZE) goto error;
if (IS_FREE(de->name)) { if (prev != bh) {
get_bh(bh);
bhs[nr_bhs] = prev = bh;
nr_bhs++;
}
free_slots++; if (free_slots == nr_slots) goto found;
} else { for (i = 0; i < nr_bhs; i++)
brelse(bhs[i]);
prev = NULL;
free_slots = nr_bhs = 0;
}
} if (dir->i_ino == MSDOS_ROOT_INO) { if (!is_fat32(sbi)) goto error;
} elseif (MSDOS_I(dir)->i_start == 0) {
fat_msg(sb, KERN_ERR, "Corrupted directory (i_pos %lld)",
MSDOS_I(dir)->i_pos);
err = -EIO; goto error;
}
found:
err = 0;
pos -= free_slots * sizeof(*de);
nr_slots -= free_slots; if (free_slots) { /* * Second stage: filling the free entries with new entries. * NOTE: If this slots has shortname, first, we write * the long name slots, then write the short name.
*/ int size = free_slots * sizeof(*de); int offset = pos & (sb->s_blocksize - 1); int long_bhs = nr_bhs - (nr_slots == 0);
/* Fill the long name slots. */ for (i = 0; i < long_bhs; i++) { int copy = min_t(int, sb->s_blocksize - offset, size);
memcpy(bhs[i]->b_data + offset, slots, copy);
mark_buffer_dirty_inode(bhs[i], dir);
offset = 0;
slots += copy;
size -= copy;
} if (long_bhs && IS_DIRSYNC(dir))
err = fat_sync_bhs(bhs, long_bhs); if (!err && i < nr_bhs) { /* Fill the short name slot. */ int copy = min_t(int, sb->s_blocksize - offset, size);
memcpy(bhs[i]->b_data + offset, slots, copy);
mark_buffer_dirty_inode(bhs[i], dir); if (IS_DIRSYNC(dir))
err = sync_dirty_buffer(bhs[i]);
} for (i = 0; i < nr_bhs; i++)
brelse(bhs[i]); if (err) goto error_remove;
}
if (nr_slots) { int cluster, nr_cluster;
/* * Third stage: allocate the cluster for new entries. * And initialize the cluster with new entries, then * add the cluster to dir.
*/
cluster = fat_add_new_entries(dir, slots, nr_slots, &nr_cluster,
&de, &bh, &i_pos); if (cluster < 0) {
err = cluster; goto error_remove;
}
err = fat_chain_add(dir, cluster, nr_cluster); if (err) {
fat_free_clusters(dir, cluster); goto error_remove;
} if (dir->i_size & (sbi->cluster_size - 1)) {
fat_fs_error(sb, "Odd directory size");
dir->i_size = (dir->i_size + sbi->cluster_size - 1)
& ~((loff_t)sbi->cluster_size - 1);
}
dir->i_size += nr_cluster << sbi->cluster_bits;
MSDOS_I(dir)->mmu_private += nr_cluster << sbi->cluster_bits;
}
sinfo->slot_off = pos;
sinfo->de = de;
sinfo->bh = bh;
sinfo->i_pos = fat_make_i_pos(sb, sinfo->bh, sinfo->de);
return 0;
error:
brelse(bh); for (i = 0; i < nr_bhs; i++)
brelse(bhs[i]); return err;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.