Release 4.10 fs/fat/dir.c
/*
* 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>
*/
#include <linux/slab.h>
#include <linux/compat.h>
#include <linux/uaccess.h>
#include "fat.h"
/*
* Maximum buffer size of short name.
* [(MSDOS_NAME + '.') * max one char + nul]
* For msdos style, ['.' (hidden) + MSDOS_NAME + '.' + nul]
*/
#define FAT_MAX_SHORT_SIZE ((MSDOS_NAME + 1) * NLS_MAX_CHARSET_SIZE + 1)
/*
* Maximum buffer size of unicode chars from slots.
* [(max longname slots * 13 (size in a slot) + nul) * sizeof(wchar_t)]
*/
#define FAT_MAX_UNI_CHARS ((MSDOS_SLOTS - 1) * 13 + 1)
#define FAT_MAX_UNI_SIZE (FAT_MAX_UNI_CHARS * sizeof(wchar_t))
static inline unsigned char fat_tolower(unsigned char c)
{
return ((c >= 'A') && (c <= 'Z')) ? c+32 : c;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
steven j. magnani | steven j. magnani | 33 | 100.00% | 1 | 100.00% |
| Total | 33 | 100.00% | 1 | 100.00% |
static inline loff_t fat_make_i_pos(struct super_block *sb,
struct buffer_head *bh,
struct msdos_dir_entry *de)
{
return ((loff_t)bh->b_blocknr << MSDOS_SB(sb)->dir_per_block_bits)
| (de - (struct msdos_dir_entry *)bh->b_data);
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
hirofumi ogawa | hirofumi ogawa | 52 | 100.00% | 1 | 100.00% |
| Total | 52 | 100.00% | 1 | 100.00% |
static inline void fat_dir_readahead(struct inode *dir, sector_t iblock,
sector_t phys)
{
struct super_block *sb = dir->i_sb;
struct msdos_sb_info *sbi = MSDOS_SB(sb);
struct buffer_head *bh;
int sec;
/* 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 ((sbi->fat_bits != 32) && (dir->i_ino == MSDOS_ROOT_INO))
return;
bh = sb_find_get_block(sb, phys);
if (bh == NULL || !buffer_uptodate(bh)) {
for (sec = 0; sec < sbi->sec_per_clus; sec++)
sb_breadahead(sb, phys + sec);
}
brelse(bh);
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
karsten wiese | karsten wiese | 135 | 97.12% | 1 | 50.00% |
hirofumi ogawa | hirofumi ogawa | 4 | 2.88% | 1 | 50.00% |
| Total | 139 | 100.00% | 2 | 100.00% |
/* 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.
*/
static int 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;
unsigned long mapped_blocks;
int err, offset;
next:
if (*bh)
brelse(*bh);
*bh = NULL;
iblock = *pos >> sb->s_blocksize_bits;
err = fat_bmap(dir, iblock, &phys, &mapped_blocks, 0, false);
if (err || !phys)
return -1; /* beyond EOF or error */
fat_dir_readahead(dir, iblock, phys);
*bh = sb_bread(sb, phys);
if (*bh == NULL) {
fat_msg_ratelimit(sb, KERN_ERR,
"Directory bread(block %llu) failed", (llu)phys);
/* skip this block */
*pos = (iblock + 1) << sb->s_blocksize_bits;
goto next;
}
offset = *pos & (sb->s_blocksize - 1);
*pos += sizeof(struct msdos_dir_entry);
*de = (struct msdos_dir_entry *)((*bh)->b_data + offset);
return 0;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
hirofumi ogawa | hirofumi ogawa | 195 | 92.42% | 5 | 55.56% |
karsten wiese | karsten wiese | 9 | 4.27% | 1 | 11.11% |
alexey fisher | alexey fisher | 4 | 1.90% | 1 | 11.11% |
namjae jeon | namjae jeon | 3 | 1.42% | 2 | 22.22% |
| Total | 211 | 100.00% | 9 | 100.00% |
static inline int fat_get_entry(struct inode *dir, loff_t *pos,
struct buffer_head **bh,
struct msdos_dir_entry **de)
{
/* Fast stuff first */
if (*bh && *de &&
(*de - (struct msdos_dir_entry *)(*bh)->b_data) <
MSDOS_SB(dir->i_sb)->dir_per_block - 1) {
*pos += sizeof(struct msdos_dir_entry);
(*de)++;
return 0;
}
return fat__get_entry(dir, pos, bh, de);
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
hirofumi ogawa | hirofumi ogawa | 97 | 100.00% | 1 | 100.00% |
| Total | 97 | 100.00% | 1 | 100.00% |
/*
* 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.
*/
static int uni16_to_x8(struct super_block *sb, unsigned char *ascii,
const wchar_t *uni, int len, struct nls_table *nls)
{
int uni_xlate = MSDOS_SB(sb)->options.unicode_xlate;
const wchar_t *ip;
wchar_t ec;
unsigned char *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.");
}
*op = 0;
return op - ascii;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
pre-git | pre-git | 122 | 61.31% | 5 | 41.67% |
keith mok | keith mok | 32 | 16.08% | 1 | 8.33% |
alexey fisher | alexey fisher | 21 | 10.55% | 1 | 8.33% |
andy shevchenko | andy shevchenko | 14 | 7.04% | 2 | 16.67% |
cruz julian bishop | cruz julian bishop | 5 | 2.51% | 1 | 8.33% |
hirofumi ogawa | hirofumi ogawa | 4 | 2.01% | 1 | 8.33% |
linus torvalds | linus torvalds | 1 | 0.50% | 1 | 8.33% |
| Total | 199 | 100.00% | 12 | 100.00% |
static inline int fat_uni_to_x8(struct super_block *sb, const wchar_t *uni,
unsigned char *buf, int size)
{
struct msdos_sb_info *sbi = MSDOS_SB(sb);
if (sbi->options.utf8)
return utf16s_to_utf8s(uni, FAT_MAX_UNI_CHARS,
UTF16_HOST_ENDIAN, buf, size);
else
return uni16_to_x8(sb, buf, uni, size, sbi->nls_io);
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
hirofumi ogawa | hirofumi ogawa | 53 | 71.62% | 1 | 33.33% |
alexey fisher | alexey fisher | 14 | 18.92% | 1 | 33.33% |
alan stern | alan stern | 7 | 9.46% | 1 | 33.33% |
| Total | 74 | 100.00% | 3 | 100.00% |
static inline int
fat_short2uni(struct nls_table *t, unsigned char *c, int clen, wchar_t *uni)
{
int charlen;
charlen = t->char2uni(c, clen, uni);
if (charlen < 0) {
*uni = 0x003f; /* a question mark */
charlen = 1;
}
return charlen;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
linus torvalds | linus torvalds | 58 | 95.08% | 1 | 33.33% |
pre-git | pre-git | 3 | 4.92% | 2 | 66.67% |
| Total | 61 | 100.00% | 3 | 100.00% |
static inline int
fat_short2lower_uni(struct nls_table *t, unsigned char *c,
int clen, wchar_t *uni)
{
int charlen;
wchar_t wc;
charlen = t->char2uni(c, clen, &wc);
if (charlen < 0) {
*uni = 0x003f; /* a question mark */
charlen = 1;
} else if (charlen <= 1) {
unsigned char nc = t->charset2lower[*c];
if (!nc)
nc = *c;
charlen = t->char2uni(&nc, 1, uni);
if (charlen < 0) {
*uni = 0x003f; /* a question mark */
charlen = 1;
}
} else
*uni = wc;
return charlen;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
pre-git | pre-git | 67 | 50.00% | 2 | 50.00% |
linus torvalds | linus torvalds | 63 | 47.01% | 1 | 25.00% |
cruz julian bishop | cruz julian bishop | 4 | 2.99% | 1 | 25.00% |
| Total | 134 | 100.00% | 4 | 100.00% |
static inline int
fat_shortname2uni(struct nls_table *nls, unsigned char *buf, int buf_size,
wchar_t *uni_buf, unsigned short opt, int lower)
{
int len = 0;
if (opt & VFAT_SFN_DISPLAY_LOWER)
len = fat_short2lower_uni(nls, buf, buf_size, uni_buf);
else if (opt & VFAT_SFN_DISPLAY_WIN95)
len = fat_short2uni(nls, buf, buf_size, uni_buf);
else if (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;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
linus torvalds | linus torvalds | 130 | 98.48% | 1 | 33.33% |
hirofumi ogawa | hirofumi ogawa | 1 | 0.76% | 1 | 33.33% |
pre-git | pre-git | 1 | 0.76% | 1 | 33.33% |
| Total | 132 | 100.00% | 3 | 100.00% |
static inline int fat_name_match(struct msdos_sb_info *sbi,
const unsigned char *a, int a_len,
const unsigned char *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);
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
hirofumi ogawa | hirofumi ogawa | 76 | 100.00% | 1 | 100.00% |
| Total | 76 | 100.00% | 1 | 100.00% |
enum { PARSE_INVALID = 1, PARSE_NOT_LONGNAME, PARSE_EOF, };
/**
* fat_parse_long - Parse extended directory entry.
*
* 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.
*/
static int fat_parse_long(struct inode *dir, loff_t *pos,
struct buffer_head **bh, struct msdos_dir_entry **de,
wchar_t **unicode, unsigned char *nr_slots)
{
struct msdos_dir_slot *ds;
unsigned char id, slot, slots, alias_checksum;
if (!*unicode) {
*unicode = __getname();
if (!*unicode) {
brelse(*bh);
return -ENOMEM;
}
}
parse_long:
slots = 0;
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;
slot = slots;
while (1) {
int offset;
slot--;
offset = slot * 13;
fat16_towchar(*unicode + offset, ds->name0_4, 5);
fat16_towchar(*unicode + offset + 5, ds->name5_10, 6);
fat16_towchar(*unicode + offset + 11, ds->name11_12, 2);
if (ds->id & 0x40)
(*unicode)[offset + 13] = 0;
if (fat_get_entry(dir, pos, bh, de) < 0)
return PARSE_EOF;
if (slot == 0)
break;
ds = (struct msdos_dir_slot *)*de;
if (ds->attr != ATTR_EXT)
return PARSE_NOT_LONGNAME;
if ((ds->id & ~0x40) != slot)
goto parse_long;
if (ds->alias_checksum != alias_checksum)
goto parse_long;
}
if ((*de)->name[0] == DELETED_FLAG)
return PARSE_INVALID;
if ((*de)->attr == ATTR_EXT)
goto parse_long;
if (IS_FREE((*de)->name) || ((*de)->attr & ATTR_VOLUME))
return PARSE_INVALID;
if (fat_checksum((*de)->name) != alias_checksum)
*nr_slots = 0;
return 0;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
pre-git | pre-git | 254 | 65.30% | 7 | 50.00% |
pekka j enberg | pekka j enberg | 107 | 27.51% | 1 | 7.14% |
linus torvalds | linus torvalds | 16 | 4.11% | 1 | 7.14% |
hirofumi ogawa | hirofumi ogawa | 12 | 3.08% | 5 | 35.71% |
| Total | 389 | 100.00% | 14 | 100.00% |
/**
* fat_parse_short - Parse MS-DOS (short) directory entry.
* @sb: superblock
* @de: directory entry to parse
* @name: FAT_MAX_SHORT_SIZE array in which to place extracted name
* @dot_hidden: Nonzero == prepend '.' to names with ATTR_HIDDEN
*
* Returns the number of characters extracted into 'name'.
*/
static int fat_parse_short(struct super_block *sb,
const struct msdos_dir_entry *de,
unsigned char *name, int dot_hidden)
{
const struct msdos_sb_info *sbi = MSDOS_SB(sb);
int isvfat = sbi->options.isvfat;
int nocase = sbi->options.nocase;
unsigned short opt_shortname = sbi->options.shortname;
struct nls_table *nls_disk = sbi->nls_disk;
wchar_t uni_name[14];
unsigned char c, work[MSDOS_NAME];
unsigned char *ptname = name;
int chi, chl, i, j, k;
int dotoffset = 0;
int name_len = 0, uni_len = 0;
if (!isvfat && dot_hidden && (de->attr & ATTR_HIDDEN)) {
*ptname++ = '.';
dotoffset = 1;
}
memcpy(work, de->name, sizeof(work));
/* see namei.c, msdos_format_name */
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;
}
}
if (name_len > 0) {
name_len += dotoffset;
if (sbi->options.isvfat) {
uni_name[uni_len] = 0x0000;
name_len = fat_uni_to_x8(sb, uni_name, name,
FAT_MAX_SHORT_SIZE);
}
}
return name_len;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
steven j. magnani | steven j. magnani | 334 | 53.78% | 1 | 6.25% |
linus torvalds | linus torvalds | 119 | 19.16% | 2 | 12.50% |
pre-git | pre-git | 75 | 12.08% | 6 | 37.50% |
pekka j enberg | pekka j enberg | 72 | 11.59% | 1 | 6.25% |
hirofumi ogawa | hirofumi ogawa | 18 | 2.90% | 5 | 31.25% |
keith mok | keith mok | 3 | 0.48% | 1 | 6.25% |
| Total | 621 | 100.00% | 16 | 100.00% |
/*
* Return values: negative -> error/not found, 0 -> found.
*/
int fat_search_long(struct inode *inode, const unsigned char *name,
int name_len, struct fat_slot_info *sinfo)
{
struct super_block *sb = inode->i_sb;
struct msdos_sb_info *sbi = MSDOS_SB(sb);
struct buffer_head *bh = NULL;
struct msdos_dir_entry *de;
unsigned char nr_slots;
wchar_t *unicode = NULL;
unsigned char bufname[FAT_MAX_SHORT_SIZE];
loff_t cpos = 0;
int err, len;
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;
} else if (status == PARSE_INVALID)
continue;
else if (status == PARSE_NOT_LONGNAME)
goto parse_record;
else if (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;
/* Compare shortname */
if (fat_name_match(sbi, name, name_len, bufname, len))
goto found;
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;
}
Contributors
| Person | Tokens | Prop | Commits | CommitProp |
steven j. magnani | steven j. magnani | 133 | 32.60% | 1 | 4.55% |
pre-git | pre-git | 104 | 25.49% | 7 | 31.82% |
pekka j enberg | pekka j enberg | 83 | 20.34% | 1 | 4.55% |
hirofumi ogawa | hirofumi ogawa | 81 | 19.85% | 10 | 45.45% |
linus torvalds | linus torvalds | 4 | 0.98% | 1 | 4.55% |
dave hansen | dave hansen | 2 | 0.49% | 1 | 4.55% |
alexey fisher | alexey fisher | 1 | 0.25% | 1 | 4.55% |
| Total | 408 | 100.00% | 22 | 100.00% |
EXPORT_SYMBOL_GPL(fat_search_long);
struct fat_ioctl_filldir_callback {
struct dir_context ctx;
void __user *dirent;
int result;
/* for dir ioctl */
const char *longname;
int long_len;
const char *shortname;
int short_len;
};
static int __fat_readdir(struct inode *inode, struct file *file,
struct dir_context *ctx, int short_only,
struct fat_ioctl_filldir_callback *both)
{
struct super_block *sb = inode->i_sb;
struct msdos_sb_info *sbi = MSDOS_SB(sb);
struct buffer_head *bh;
struct msdos_dir_entry *de;
unsigned char nr_slots;
wchar_t *unicode = NULL;
unsigned char bufname[FAT_MAX_SHORT_SIZE];
int isvfat = sbi->options.isvfat;
const char *fill_name = NULL;
int fake_offset = 0;
loff_t cpos;
int short_len = 0, fill_len = 0;
int ret = 0;
mutex_lock(&sbi->s_lock);
cpos = ctx->pos;
/* Fake . and .. for the root directory. */
if (inode->i_ino == MSDOS_ROOT_INO) {
if (!dir_emit_dots(file, ctx))
goto out;
if (ctx->pos == 2) {
fake_offset = 1;
cpos = 0;
}
}
if (cpos & (sizeof(struct msdos_dir_entry) - 1)) {
ret = -ENOENT;
goto out;
}
bh = NULL;
get_new:
if (fat_get_entry(inode, &cpos, &bh, &de) == -1)
goto end_of_dir;
parse_record:
nr_slots = 0;
/*
* Check for long filename entry, but if short_only, we don't
* need to parse long filename.
*/
if (isvfat && !short_only) {
if (de->name[0] == DELETED_FLAG)
goto record_end;
if (de->attr != ATTR_EXT && (de->attr & ATTR_VOLUME))
goto record_end;
if (de->attr != ATTR_EXT && IS_FREE(de->name))
goto record_end;
} else {
if ((de->attr & ATTR_VOLUME) || IS_FREE(de->name))
goto record_end;
}
if (isvfat && de->attr == ATTR_EXT) {
int status = fat_parse_long(inode, &cpos, &bh, &